]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
sunxi: dram: detect non-power-of-2 sized DRAM chips
authorAndre Przywara <andre.przywara@arm.com>
Tue, 21 Oct 2025 23:53:34 +0000 (00:53 +0100)
committerAndre Przywara <andre.przywara@arm.com>
Sun, 25 Jan 2026 23:29:32 +0000 (23:29 +0000)
Some boards feature an "odd" DRAM size, where the total RAM is 1.5GB or
3GB. Our existing DRAM size detection routines can only detect power-of-2
sized configuration, and on those boards the DRAM size is overestimated,
so this typically breaks the boot quite early.

There doesn't seem to be an easy explicit way to detect those odd-sized
chips, but we can test whether the later part of the memory behaves like
memory, by verifying that a written pattern can be read back.
Experiments show that there is no aliasing effect here, as all locations
in the unimplemented range always return some fixed pattern, and cannot
be changed.

Also so far all those boards use a factor of 3 of some lower power-of-2
number, or 3/4th of some higher number. The size detection routine
discovers the higher number, so we can check for some memory cells beyond
75% of the detected size to be legit.

Add a routine the inverts all bits at a given location in memory, and
reads that back to prove that the new value was stored.
Then test the memory cell at exactly 3/4th of the detected size, and cap
the size of the memory to 75% when this test fails. For good measure
also make sure that memory just below the assumed memory end really
works.

This enables boards which ship with such odd memory sizes.

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Reviewed-by: Jernej Skrabec <jernej.skrabec@gmail.com>
arch/arm/include/asm/arch-sunxi/dram.h
arch/arm/mach-sunxi/dram_dw_helpers.c
arch/arm/mach-sunxi/dram_helpers.c

index 0eccb1e6c28ffe3b4a55ab13c26d8d03d9fe7a4e..59e2e980bfa8808b28dbad62908d6e5c0e2e9d60 100644 (file)
@@ -45,5 +45,6 @@ unsigned long sunxi_dram_init(void);
 void mctl_await_completion(u32 *reg, u32 mask, u32 val);
 bool mctl_mem_matches(u32 offset);
 bool mctl_mem_matches_base(u32 offset, ulong base);
+bool mctl_check_memory(phys_addr_t addr);
 
 #endif /* _SUNXI_DRAM_H */
index 247673549356eb7e2f8c80027c0a9f6cdf9c633d..d2af2d57fdeda1c749cea85f7c551f3f5609393c 100644 (file)
@@ -143,8 +143,28 @@ void mctl_auto_detect_dram_size(const struct dram_para *para,
 
 unsigned long mctl_calc_size(const struct dram_config *config)
 {
+       unsigned long size;
        u8 width = config->bus_full_width ? 4 : 2;
 
        /* 8 banks */
-       return (1ULL << (config->cols + config->rows + 3)) * width * config->ranks;
+       size = (1ULL << (config->cols + config->rows + 3)) * width *
+               config->ranks;
+
+       /*
+        * There are boards with non-power-of-2 sized DRAM chips, like 1.5GB
+        * or 3GB. They are detected as the larger power-of-2 (2GB and 4GB),
+        * so test the last quarter for being able to store values.
+        */
+       if (!mctl_check_memory(CFG_SYS_SDRAM_BASE + size / 4 * 3)) {
+               if (mctl_check_memory(CFG_SYS_SDRAM_BASE + size / 4 * 3 - 64)) {
+                       size = (size / 4) * 3;
+                       debug("capping memory at %ld MB\n", size >> 20);
+               } else {
+                       printf("DRAM test failure at address 0x%lx\n",
+                              CFG_SYS_SDRAM_BASE + size / 4 * 3 - 64);
+                       return 0;
+               }
+       }
+
+       return size;
 }
index 83dbe4ca98f5fe849327605b73ea80502170d916..376b7d14f8681e61248515aeeea7a575a1abae1c 100644 (file)
@@ -62,3 +62,15 @@ bool mctl_mem_matches(u32 offset)
        return mctl_mem_matches_base(offset, CFG_SYS_SDRAM_BASE);
 }
 #endif
+
+bool mctl_check_memory(phys_addr_t addr)
+{
+       uint32_t orig, val;
+
+       orig = readl(addr);
+       writel(~orig, addr);
+       val = readl(addr);
+       writel(orig, addr);
+
+       return ~orig == val;
+}