[PATCH 1/2] board: stemmy: Parse atags to get available memory

Stephan Gerhold stephan at gerhold.net
Wed Jul 7 12:58:54 CEST 2021


At the moment the "stemmy" board attempts to detect the RAM size with
a simple memory test (get_ram_size()). Unfortunately, this does not work
correctly for devices with 768 MiB RAM (e.g. Samsung Galaxy Ace 2
(GT-I8160), "codina"). Reading/writing memory after the 768 MiB RAM
succeeds but actually overwrites some earlier parts of the memory.

For U-Boot this does not result in any major problems, but on Linux
this will eventually lead to strange crashes because of the memory
corruption.

Since the "stemmy" U-Boot port is designed to be chainloaded from
the original Samsung bootloader, the most reliable way to get the
available amount of RAM is to look at the ATAGS passed by the Samsung
bootloader. Fortunately, the header used to generate ATAGS in U-Boot
(asm/setup.h) can also be easily used to parse them.

Also clarify and simplify stemmy.h a bit to make it more clear where
some of the magic values in there are actually coming from.

Signed-off-by: Stephan Gerhold <stephan at gerhold.net>
---

 arch/arm/mach-u8500/Kconfig |  1 +
 board/ste/stemmy/README     |  1 +
 board/ste/stemmy/stemmy.c   | 62 ++++++++++++++++++++++++++++++++++++-
 configs/stemmy_defconfig    |  2 +-
 include/configs/stemmy.h    | 23 ++++++--------
 5 files changed, 74 insertions(+), 15 deletions(-)

diff --git a/arch/arm/mach-u8500/Kconfig b/arch/arm/mach-u8500/Kconfig
index 7478deb25f..372fbeb80f 100644
--- a/arch/arm/mach-u8500/Kconfig
+++ b/arch/arm/mach-u8500/Kconfig
@@ -15,6 +15,7 @@ config TARGET_STEMMY
 	      - Samsung Galaxy S III mini (GT-I8190)	"golden"
 	      - Samsung Galaxy S Advance (GT-I9070)	"janice"
 	      - Samsung Galaxy Xcover 2 (GT-S7710)	"skomer"
+	      - Samsung Galaxy Ace 2 (GT-I8160)		"codina"
 
 	  and likely others as well (untested).
 
diff --git a/board/ste/stemmy/README b/board/ste/stemmy/README
index 81f72426f2..1b83b833c0 100644
--- a/board/ste/stemmy/README
+++ b/board/ste/stemmy/README
@@ -7,6 +7,7 @@ the ST-Ericsson NovaThor U8500 SoC, e.g.
 	- Samsung Galaxy S III mini (GT-I8190)	"golden"
 	- Samsung Galaxy S Advance (GT-I9070)	"janice"
 	- Samsung Galaxy Xcover 2 (GT-S7710)	"skomer"
+	- Samsung Galaxy Ace 2 (GT-I8160)	"codina"
 
 and likely others as well (untested).
 
diff --git a/board/ste/stemmy/stemmy.c b/board/ste/stemmy/stemmy.c
index b9b2a6fddc..9e6c8e208e 100644
--- a/board/ste/stemmy/stemmy.c
+++ b/board/ste/stemmy/stemmy.c
@@ -4,17 +4,77 @@
  */
 #include <common.h>
 #include <init.h>
+#include <log.h>
 #include <asm/global_data.h>
+#include <asm/setup.h>
+#include <asm/system.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/* Parse atags provided by Samsung bootloader to get available memory */
+static ulong fw_mach __section(".data");
+static ulong fw_atags __section(".data");
+
+void save_boot_params(ulong r0, ulong r1, ulong r2, ulong r3)
+{
+	fw_mach = r1;
+	fw_atags = r2;
+	save_boot_params_ret();
+}
+
+static const struct tag *fw_atags_get(void)
+{
+	const struct tag *tags = (const struct tag *)fw_atags;
+
+	if (tags->hdr.tag != ATAG_CORE) {
+		log_err("Invalid atags: tag 0x%x at %p\n", tags->hdr.tag, tags);
+		return NULL;
+	}
+
+	return tags;
+}
+
 int dram_init(void)
 {
-	gd->ram_size = get_ram_size(CONFIG_SYS_SDRAM_BASE, CONFIG_SYS_SDRAM_SIZE);
+	const struct tag *t, *tags = fw_atags_get();
+
+	if (!tags)
+		return -EINVAL;
+
+	for_each_tag(t, tags) {
+		if (t->hdr.tag != ATAG_MEM)
+			continue;
+
+		debug("Memory: %#x-%#x (size %#x)\n", t->u.mem.start,
+		      t->u.mem.start + t->u.mem.size, t->u.mem.size);
+		gd->ram_size += t->u.mem.size;
+	}
+	return 0;
+}
+
+int dram_init_banksize(void)
+{
+	const struct tag *t, *tags = fw_atags_get();
+	unsigned int bank = 0;
+
+	if (!tags)
+		return -EINVAL;
+
+	for_each_tag(t, tags) {
+		if (t->hdr.tag != ATAG_MEM)
+			continue;
+
+		gd->bd->bi_dram[bank].start = t->u.mem.start;
+		gd->bd->bi_dram[bank].size = t->u.mem.size;
+		if (++bank == CONFIG_NR_DRAM_BANKS)
+			break;
+	}
 	return 0;
 }
 
 int board_init(void)
 {
+	gd->bd->bi_arch_number = fw_mach;
+	gd->bd->bi_boot_params = fw_atags;
 	return 0;
 }
diff --git a/configs/stemmy_defconfig b/configs/stemmy_defconfig
index 79c05acc6a..f31960b814 100644
--- a/configs/stemmy_defconfig
+++ b/configs/stemmy_defconfig
@@ -1,7 +1,7 @@
 CONFIG_ARM=y
 CONFIG_ARCH_U8500=y
 CONFIG_SYS_TEXT_BASE=0x100000
-CONFIG_NR_DRAM_BANKS=1
+CONFIG_NR_DRAM_BANKS=2
 CONFIG_DEFAULT_DEVICE_TREE="ste-ux500-samsung-stemmy"
 CONFIG_SYS_CONSOLE_INFO_QUIET=y
 CONFIG_HUSH_PARSER=y
diff --git a/include/configs/stemmy.h b/include/configs/stemmy.h
index 922eec43ee..c446b09270 100644
--- a/include/configs/stemmy.h
+++ b/include/configs/stemmy.h
@@ -7,23 +7,20 @@
 
 #include <linux/sizes.h>
 
-#define CONFIG_SKIP_LOWLEVEL_INIT	/* Loaded by another bootloader */
-#define CONFIG_SYS_MALLOC_LEN		SZ_2M
+/*
+ * The "stemmy" U-Boot port is designed to be chainloaded by the Samsung
+ * bootloader on devices based on ST-Ericsson Ux500. Therefore, we skip most
+ * low-level initialization and rely on configuration provided by the Samsung
+ * bootloader. New images are loaded at the same address for compatibility.
+ */
+#define CONFIG_SKIP_LOWLEVEL_INIT
+#define CONFIG_SYS_INIT_SP_ADDR		CONFIG_SYS_TEXT_BASE
+#define CONFIG_SYS_LOAD_ADDR		CONFIG_SYS_TEXT_BASE
 
-/* Physical Memory Map */
-#define PHYS_SDRAM_1			0x00000000	/* DDR-SDRAM Bank #1 */
-#define CONFIG_SYS_SDRAM_BASE		PHYS_SDRAM_1
-#define CONFIG_SYS_SDRAM_SIZE		SZ_1G
-#define CONFIG_SYS_INIT_RAM_SIZE	0x00100000
-#define CONFIG_SYS_GBL_DATA_OFFSET	(CONFIG_SYS_SDRAM_BASE + \
-					 CONFIG_SYS_INIT_RAM_SIZE - \
-					 GENERATED_GBL_DATA_SIZE)
-#define CONFIG_SYS_INIT_SP_ADDR		CONFIG_SYS_GBL_DATA_OFFSET
+#define CONFIG_SYS_MALLOC_LEN		SZ_2M
 
 /* FIXME: This should be loaded from device tree... */
 #define CONFIG_SYS_L2_PL310
 #define CONFIG_SYS_PL310_BASE		0xa0412000
 
-#define CONFIG_SYS_LOAD_ADDR		0x00100000
-
 #endif
-- 
2.32.0



More information about the U-Boot mailing list