1 /* 2 * (C) Copyright 2004 3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 10 DECLARE_GLOBAL_DATA_PTR; 11 12 #ifdef __PPC__ 13 /* 14 * At least on G2 PowerPC cores, sequential accesses to non-existent 15 * memory must be synchronized. 16 */ 17 # include <asm/io.h> /* for sync() */ 18 #else 19 # define sync() /* nothing */ 20 #endif 21 22 /* 23 * Check memory range for valid RAM. A simple memory test determines 24 * the actually available RAM size between addresses `base' and 25 * `base + maxsize'. 26 */ 27 long get_ram_size(long *base, long maxsize) 28 { 29 volatile long *addr; 30 long save[31]; 31 long save_base; 32 long cnt; 33 long val; 34 long size; 35 int i = 0; 36 37 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) { 38 addr = base + cnt; /* pointer arith! */ 39 sync(); 40 save[i++] = *addr; 41 sync(); 42 *addr = ~cnt; 43 } 44 45 addr = base; 46 sync(); 47 save_base = *addr; 48 sync(); 49 *addr = 0; 50 51 sync(); 52 if ((val = *addr) != 0) { 53 /* Restore the original data before leaving the function. */ 54 sync(); 55 *base = save_base; 56 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { 57 addr = base + cnt; 58 sync(); 59 *addr = save[--i]; 60 } 61 return (0); 62 } 63 64 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { 65 addr = base + cnt; /* pointer arith! */ 66 val = *addr; 67 *addr = save[--i]; 68 if (val != ~cnt) { 69 size = cnt * sizeof(long); 70 /* 71 * Restore the original data 72 * before leaving the function. 73 */ 74 for (cnt <<= 1; 75 cnt < maxsize / sizeof(long); 76 cnt <<= 1) { 77 addr = base + cnt; 78 *addr = save[--i]; 79 } 80 /* warning: don't restore save_base in this case, 81 * it is already done in the loop because 82 * base and base+size share the same physical memory 83 * and *base is saved after *(base+size) modification 84 * in first loop 85 */ 86 return (size); 87 } 88 } 89 *base = save_base; 90 91 return (maxsize); 92 } 93 94 phys_size_t __weak get_effective_memsize(void) 95 { 96 #ifndef CONFIG_VERY_BIG_RAM 97 return gd->ram_size; 98 #else 99 /* limit stack to what we can reasonable map */ 100 return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ? 101 CONFIG_MAX_MEM_MAPPED : gd->ram_size); 102 #endif 103 } 104