diff --git a/common/memsize.c b/common/memsize.c index 1abf3fc47d7..3ecf9bac2aa 100644 --- a/common/memsize.c +++ b/common/memsize.c @@ -127,6 +127,65 @@ long get_ram_size(long *base, long maxsize) return (maxsize); } +/** + * probe_ram_size_by_alias() - Detect RAM size using known alias addresses + * @checks: Array of RAM alias probe descriptors, terminated by a NULL + * @probe_addr entry + * + * Probe RAM size by writing a test pattern to each @probe_addr and checking + * whether the same pattern does not appear at the corresponding @alias_addr. + * This is useful on systems where address wrap-around does not alias to the + * base of memory in a linear way, so get_ram_size() cannot be used directly. + * It is also useful on systems where the base of the physical memory cannot + * be safely accessed, so get_ram_size() cannot be used at all. + * + * Return: The size associated with the first matching entry, or 0 if no match + * is found. + */ +long probe_ram_size_by_alias(const struct ram_alias_check *checks) +{ + long save[2]; + int dcache_en = 0; + long ret = 0; + + if (!CONFIG_IS_ENABLED(SYS_DCACHE_OFF)) + dcache_en = dcache_status(); + + while (checks->probe_addr && !ret) { + volatile long *d = checks->probe_addr; + volatile long *s = checks->alias_addr; + + save[0] = *s; + save[1] = *d; + /* Ensure s is written and not cached */ + if (dcache_en) + dcache_flush_invalidate(s); + + *d = ~save[0]; + sync(); + if (dcache_en) + dcache_flush_invalidate(d); + + if (*s != ~save[0]) + ret = checks->size; + + /* Restore content */ + *d = save[1]; + sync(); + if (dcache_en) + dcache_flush_invalidate(d); + + *s = save[0]; + sync(); + if (dcache_en) + dcache_flush_invalidate(s); + + checks++; + } + + return ret; +} + phys_size_t __weak get_effective_memsize(void) { phys_size_t ram_size = gd->ram_size; diff --git a/include/init.h b/include/init.h index 1e375da4893..c31ebd83b85 100644 --- a/include/init.h +++ b/include/init.h @@ -14,6 +14,12 @@ #include +struct ram_alias_check { + void *probe_addr; + void *alias_addr; + long size; +}; + /* * In case of the EFI app the UEFI firmware provides the low-level * initialisation. @@ -88,6 +94,7 @@ int dram_init(void); int dram_init_banksize(void); long get_ram_size(long *base, long size); +long probe_ram_size_by_alias(const struct ram_alias_check *checks); phys_size_t get_effective_memsize(void); int testdram(void);