1 // SPDX-License-Identifier: GPL-2.0+
9 #include <asm/global_data.h>
13 DECLARE_GLOBAL_DATA_PTR;
15 #ifdef CONFIG_SYS_CACHELINE_SIZE
16 # define MEMSIZE_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE
18 /* Just use the greatest cache flush alignment requirement I'm aware of */
19 # define MEMSIZE_CACHELINE_SIZE 128
24 * At least on G2 PowerPC cores, sequential accesses to non-existent
25 * memory must be synchronized.
27 # include <asm/io.h> /* for sync() */
29 # define sync() /* nothing */
32 static void dcache_flush_invalidate(volatile long *p)
34 uintptr_t start, stop;
35 start = ALIGN_DOWN((uintptr_t)p, MEMSIZE_CACHELINE_SIZE);
36 stop = start + MEMSIZE_CACHELINE_SIZE;
37 flush_dcache_range(start, stop);
38 invalidate_dcache_range(start, stop);
42 * Check memory range for valid RAM. A simple memory test determines
43 * the actually available RAM size between addresses `base' and
46 long get_ram_size(long *base, long maxsize)
49 long save[BITS_PER_LONG - 1];
55 int dcache_en = dcache_status();
57 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
58 addr = base + cnt; /* pointer arith! */
64 dcache_flush_invalidate(addr);
75 dcache_flush_invalidate(addr);
77 if ((val = *addr) != 0) {
78 /* Restore the original data before leaving the function. */
81 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
89 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
90 addr = base + cnt; /* pointer arith! */
94 size = cnt * sizeof(long);
96 * Restore the original data
97 * before leaving the function.
100 cnt < maxsize / sizeof(long);
105 /* warning: don't restore save_base in this case,
106 * it is already done in the loop because
107 * base and base+size share the same physical memory
108 * and *base is saved after *(base+size) modification
119 phys_size_t __weak get_effective_memsize(void)
121 phys_size_t ram_size = gd->ram_size;
123 #ifdef CONFIG_MPC85xx
125 * Check for overflow and limit ram size to some representable value.
126 * It is required that ram_base + ram_size must be representable by
127 * phys_size_t type and must be aligned by direct access, therefore
128 * calculate it from last 4kB sector which should work as alignment
131 if (gd->ram_base + ram_size < gd->ram_base)
132 ram_size = ((phys_size_t)~0xfffULL) - gd->ram_base;
135 #ifndef CFG_MAX_MEM_MAPPED
138 /* limit stack to what we can reasonable map */
139 return ((ram_size > CFG_MAX_MEM_MAPPED) ?
140 CFG_MAX_MEM_MAPPED : ram_size);