We previously added a fdtdec_setup_mem_ram_top() to determine the topmost address of the last DRAM bank. Similarly the overall memory size must be calculated when U-Boot gets relocated to the actual end of all available DRAM.
Pre-patch: U-Boot 2026.04-rc5-00012-g5789f787c8a6-dirty (Mar 31 2026 - 09:13:34 +0300) DRAM: 4 GiB (total 8 GiB) Post-patch: U-Boot 2026.04-rc5-00012-g5789f787c8a6 (Mar 31 2026 - 09:12:59 +0300) DRAM: 8 GiB Signed-off-by: Ilias Apalodimas <[email protected]> --- include/fdtdec.h | 15 +++++++++++++++ lib/fdtdec.c | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/fdtdec.h b/include/fdtdec.h index 26e40c3cdb46..9bccd70bbdb6 100644 --- a/include/fdtdec.h +++ b/include/fdtdec.h @@ -965,6 +965,21 @@ int fdtdec_setup_mem_ram_top(void); * invalid */ int fdtdec_setup_memory_banksize(void); +/** + * fdtdec_setup_ram_size() - decode and setup setup gd->ram_size to the sum of + * all banks + * + * Decode the /memory 'reg' property to determine the memory available in all + * banks and populate the global data ram_size with it. + * + * This function should be called from a boards board_get_usable_ram_top(). + * This helper function allows for boards to query the device tree for the + * overall memory described in as the sum of all memory banks. + * + * Return: 0 if OK, -EINVAL if the /memory node or reg property is missing or + * invalid + */ +int fdtdec_setup_ram_size(void); /** * fdtdec_set_ethernet_mac_address() - set MAC address for default interface diff --git a/lib/fdtdec.c b/lib/fdtdec.c index bdb26d8fb75b..715662d27a8c 100644 --- a/lib/fdtdec.c +++ b/lib/fdtdec.c @@ -1142,6 +1142,24 @@ int fdtdec_setup_memory_banksize(void) return fdtdec_setup_mem_for_each_bank(fdtdec_setup_memory_banksize_bankfn); } +static void fdtdec_setup_total_ram_fn(struct resource *res, int bank) +{ + gd->ram_size += res->end - res->start + 1; +} + +int fdtdec_setup_ram_size(void) +{ + int ret; + phys_addr_t old_size = gd->ram_size; + + gd->ram_size = 0; + ret = fdtdec_setup_mem_for_each_bank(fdtdec_setup_total_ram_fn); + if (ret) + gd->ram_size = old_size; + + return ret; +} + static void fdtdec_setup_mem_size_base_lowest_bankfn(struct resource *res, int bank) { unsigned long base = (unsigned long)res->start; -- 2.53.0

