Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2004 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
York Sun | e386616 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 9b4a205 | 2019-12-28 10:45:05 -0700 | [diff] [blame] | 8 | #include <init.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
York Sun | e386616 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 10 | |
| 11 | DECLARE_GLOBAL_DATA_PTR; |
| 12 | |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 13 | #ifdef __PPC__ |
| 14 | /* |
| 15 | * At least on G2 PowerPC cores, sequential accesses to non-existent |
| 16 | * memory must be synchronized. |
| 17 | */ |
| 18 | # include <asm/io.h> /* for sync() */ |
| 19 | #else |
| 20 | # define sync() /* nothing */ |
| 21 | #endif |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Check memory range for valid RAM. A simple memory test determines |
| 25 | * the actually available RAM size between addresses `base' and |
| 26 | * `base + maxsize'. |
| 27 | */ |
Albert ARIBAUD | a55d23c | 2011-07-03 05:55:33 +0000 | [diff] [blame] | 28 | long get_ram_size(long *base, long maxsize) |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 29 | { |
| 30 | volatile long *addr; |
Tien Fong Chee | 67a2616 | 2018-06-20 15:06:20 +0800 | [diff] [blame] | 31 | long save[BITS_PER_LONG - 1]; |
Patrick Delaunay | c5da05c | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 32 | long save_base; |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 33 | long cnt; |
| 34 | long val; |
| 35 | long size; |
| 36 | int i = 0; |
| 37 | |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 38 | for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) { |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 39 | addr = base + cnt; /* pointer arith! */ |
Wolfgang Denk | 95099fe | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 40 | sync(); |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 41 | save[i++] = *addr; |
Wolfgang Denk | 95099fe | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 42 | sync(); |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 43 | *addr = ~cnt; |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 44 | } |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 45 | |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 46 | addr = base; |
Eddy Petrișor | 8e7cba0 | 2016-02-02 22:15:28 +0200 | [diff] [blame] | 47 | sync(); |
Patrick Delaunay | c5da05c | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 48 | save_base = *addr; |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 49 | sync(); |
| 50 | *addr = 0; |
| 51 | |
| 52 | sync(); |
| 53 | if ((val = *addr) != 0) { |
| 54 | /* Restore the original data before leaving the function. */ |
| 55 | sync(); |
Patrick Delaunay | c5da05c | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 56 | *base = save_base; |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 57 | for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { |
| 58 | addr = base + cnt; |
| 59 | sync(); |
| 60 | *addr = save[--i]; |
| 61 | } |
| 62 | return (0); |
| 63 | } |
| 64 | |
| 65 | for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 66 | addr = base + cnt; /* pointer arith! */ |
| 67 | val = *addr; |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 68 | *addr = save[--i]; |
| 69 | if (val != ~cnt) { |
Wolfgang Denk | 95099fe | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 70 | size = cnt * sizeof(long); |
| 71 | /* |
| 72 | * Restore the original data |
| 73 | * before leaving the function. |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 74 | */ |
Wolfgang Denk | 95099fe | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 75 | for (cnt <<= 1; |
| 76 | cnt < maxsize / sizeof(long); |
| 77 | cnt <<= 1) { |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 78 | addr = base + cnt; |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 79 | *addr = save[--i]; |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 80 | } |
Patrick Delaunay | 218da80 | 2018-01-25 18:07:46 +0100 | [diff] [blame] | 81 | /* warning: don't restore save_base in this case, |
| 82 | * it is already done in the loop because |
| 83 | * base and base+size share the same physical memory |
| 84 | * and *base is saved after *(base+size) modification |
| 85 | * in first loop |
| 86 | */ |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 87 | return (size); |
| 88 | } |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 89 | } |
Patrick Delaunay | 218da80 | 2018-01-25 18:07:46 +0100 | [diff] [blame] | 90 | *base = save_base; |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 91 | |
| 92 | return (maxsize); |
| 93 | } |
York Sun | e386616 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 94 | |
| 95 | phys_size_t __weak get_effective_memsize(void) |
| 96 | { |
| 97 | #ifndef CONFIG_VERY_BIG_RAM |
| 98 | return gd->ram_size; |
| 99 | #else |
| 100 | /* limit stack to what we can reasonable map */ |
| 101 | return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ? |
| 102 | CONFIG_MAX_MEM_MAPPED : gd->ram_size); |
| 103 | #endif |
| 104 | } |