Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2004 |
| 4 | * Wolfgang Denk, DENX Software Engineering, wd@denx.de. |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
York Sun | 863e8d8 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 8e16b1e | 2019-12-28 10:45:05 -0700 | [diff] [blame] | 8 | #include <init.h> |
York Sun | 863e8d8 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 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; |
Tien Fong Chee | 62fd0c1 | 2018-06-20 15:06:20 +0800 | [diff] [blame] | 30 | long save[BITS_PER_LONG - 1]; |
Patrick Delaunay | 7544ec5 | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 31 | long save_base; |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 32 | long cnt; |
| 33 | long val; |
| 34 | long size; |
| 35 | int i = 0; |
| 36 | |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 37 | for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) { |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 38 | addr = base + cnt; /* pointer arith! */ |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 39 | sync(); |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 40 | save[i++] = *addr; |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 41 | sync(); |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 42 | *addr = ~cnt; |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 43 | } |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 44 | |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 45 | addr = base; |
| 46 | sync(); |
Patrick Delaunay | 7544ec5 | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 47 | save_base = *addr; |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 48 | sync(); |
| 49 | *addr = 0; |
| 50 | |
Eddy Petrișor | 3ef69de | 2016-02-02 22:15:28 +0200 | [diff] [blame] | 51 | sync(); |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 52 | if ((val = *addr) != 0) { |
| 53 | /* Restore the original data before leaving the function. */ |
| 54 | sync(); |
Patrick Delaunay | 7544ec5 | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 55 | *base = save_base; |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 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) { |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 65 | addr = base + cnt; /* pointer arith! */ |
| 66 | val = *addr; |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 67 | *addr = save[--i]; |
| 68 | if (val != ~cnt) { |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 69 | size = cnt * sizeof(long); |
| 70 | /* |
| 71 | * Restore the original data |
| 72 | * before leaving the function. |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 73 | */ |
Wolfgang Denk | cc17fd9 | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 74 | for (cnt <<= 1; |
| 75 | cnt < maxsize / sizeof(long); |
| 76 | cnt <<= 1) { |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 77 | addr = base + cnt; |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 78 | *addr = save[--i]; |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 79 | } |
Patrick Delaunay | 0e806a7 | 2018-01-25 18:07:46 +0100 | [diff] [blame] | 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 | */ |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 86 | return (size); |
| 87 | } |
Hans de Goede | ff94579 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 88 | } |
Patrick Delaunay | 0e806a7 | 2018-01-25 18:07:46 +0100 | [diff] [blame] | 89 | *base = save_base; |
wdenk | 87249ba | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 90 | |
| 91 | return (maxsize); |
| 92 | } |
York Sun | 863e8d8 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 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 | } |