wdenk | c83bf6a | 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 | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 8 | #include <config.h> |
| 9 | #ifdef __PPC__ |
| 10 | /* |
| 11 | * At least on G2 PowerPC cores, sequential accesses to non-existent |
| 12 | * memory must be synchronized. |
| 13 | */ |
| 14 | # include <asm/io.h> /* for sync() */ |
| 15 | #else |
| 16 | # define sync() /* nothing */ |
| 17 | #endif |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 18 | |
| 19 | /* |
| 20 | * Check memory range for valid RAM. A simple memory test determines |
| 21 | * the actually available RAM size between addresses `base' and |
| 22 | * `base + maxsize'. |
| 23 | */ |
Albert ARIBAUD | a55d23c | 2011-07-03 05:55:33 +0000 | [diff] [blame] | 24 | long get_ram_size(long *base, long maxsize) |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 25 | { |
| 26 | volatile long *addr; |
| 27 | long save[32]; |
| 28 | long cnt; |
| 29 | long val; |
| 30 | long size; |
| 31 | int i = 0; |
| 32 | |
| 33 | for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) { |
| 34 | addr = base + cnt; /* pointer arith! */ |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 35 | sync (); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 36 | save[i++] = *addr; |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 37 | sync (); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 38 | *addr = ~cnt; |
| 39 | } |
| 40 | |
| 41 | addr = base; |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 42 | sync (); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 43 | save[i] = *addr; |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 44 | sync (); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 45 | *addr = 0; |
| 46 | |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 47 | sync (); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 48 | if ((val = *addr) != 0) { |
| 49 | /* Restore the original data before leaving the function. |
| 50 | */ |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 51 | sync (); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 52 | *addr = save[i]; |
| 53 | for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { |
| 54 | addr = base + cnt; |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 55 | sync (); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 56 | *addr = save[--i]; |
| 57 | } |
| 58 | return (0); |
| 59 | } |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 60 | |
| 61 | for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) { |
| 62 | addr = base + cnt; /* pointer arith! */ |
| 63 | val = *addr; |
| 64 | *addr = save[--i]; |
| 65 | if (val != ~cnt) { |
| 66 | size = cnt * sizeof (long); |
| 67 | /* Restore the original data before leaving the function. |
| 68 | */ |
| 69 | for (cnt <<= 1; cnt < maxsize / sizeof (long); cnt <<= 1) { |
| 70 | addr = base + cnt; |
| 71 | *addr = save[--i]; |
| 72 | } |
| 73 | return (size); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return (maxsize); |
| 78 | } |