wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2004 |
| 3 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
York Sun | 863e8d8 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 8 | #include <common.h> |
| 9 | |
| 10 | DECLARE_GLOBAL_DATA_PTR; |
| 11 | |
Wolfgang Denk | c0cc46e | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 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 |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 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 | */ |
Albert ARIBAUD | a960673 | 2011-07-03 05:55:33 +0000 | [diff] [blame] | 27 | long get_ram_size(long *base, long maxsize) |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 28 | { |
| 29 | volatile long *addr; |
| 30 | long save[32]; |
| 31 | long cnt; |
| 32 | long val; |
| 33 | long size; |
| 34 | int i = 0; |
| 35 | |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 36 | for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) { |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 37 | addr = base + cnt; /* pointer arith! */ |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 38 | sync(); |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 39 | save[i++] = *addr; |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 40 | sync(); |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 41 | *addr = ~cnt; |
| 42 | } |
| 43 | |
| 44 | addr = base; |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 45 | sync(); |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 46 | save[i] = *addr; |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 47 | sync(); |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 48 | *addr = 0; |
| 49 | |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 50 | sync(); |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 51 | if ((val = *addr) != 0) { |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 52 | /* Restore the original data before leaving the function. */ |
| 53 | sync(); |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 54 | *addr = save[i]; |
| 55 | for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { |
| 56 | addr = base + cnt; |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 57 | sync(); |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 58 | *addr = save[--i]; |
| 59 | } |
| 60 | return (0); |
| 61 | } |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 62 | |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 63 | for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 64 | addr = base + cnt; /* pointer arith! */ |
| 65 | val = *addr; |
| 66 | *addr = save[--i]; |
| 67 | if (val != ~cnt) { |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 68 | size = cnt * sizeof(long); |
| 69 | /* |
| 70 | * Restore the original data |
| 71 | * before leaving the function. |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 72 | */ |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame^] | 73 | for (cnt <<= 1; |
| 74 | cnt < maxsize / sizeof(long); |
| 75 | cnt <<= 1) { |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 76 | addr = base + cnt; |
| 77 | *addr = save[--i]; |
| 78 | } |
| 79 | return (size); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return (maxsize); |
| 84 | } |
York Sun | 863e8d8 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 85 | |
| 86 | phys_size_t __weak get_effective_memsize(void) |
| 87 | { |
| 88 | #ifndef CONFIG_VERY_BIG_RAM |
| 89 | return gd->ram_size; |
| 90 | #else |
| 91 | /* limit stack to what we can reasonable map */ |
| 92 | return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ? |
| 93 | CONFIG_MAX_MEM_MAPPED : gd->ram_size); |
| 94 | #endif |
| 95 | } |