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> |
Emanuele Ghidoli | 1c64b98 | 2023-05-30 15:33:27 +0200 | [diff] [blame] | 10 | #include <cpu_func.h> |
| 11 | #include <stdint.h> |
York Sun | e386616 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 12 | |
| 13 | DECLARE_GLOBAL_DATA_PTR; |
| 14 | |
Emanuele Ghidoli | 1c64b98 | 2023-05-30 15:33:27 +0200 | [diff] [blame] | 15 | #ifdef CONFIG_SYS_CACHELINE_SIZE |
| 16 | # define MEMSIZE_CACHELINE_SIZE CONFIG_SYS_CACHELINE_SIZE |
| 17 | #else |
| 18 | /* Just use the greatest cache flush alignment requirement I'm aware of */ |
| 19 | # define MEMSIZE_CACHELINE_SIZE 128 |
| 20 | #endif |
| 21 | |
Wolfgang Denk | 91650b3 | 2006-11-06 17:06:36 +0100 | [diff] [blame] | 22 | #ifdef __PPC__ |
| 23 | /* |
| 24 | * At least on G2 PowerPC cores, sequential accesses to non-existent |
| 25 | * memory must be synchronized. |
| 26 | */ |
| 27 | # include <asm/io.h> /* for sync() */ |
| 28 | #else |
| 29 | # define sync() /* nothing */ |
| 30 | #endif |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 31 | |
Emanuele Ghidoli | 1c64b98 | 2023-05-30 15:33:27 +0200 | [diff] [blame] | 32 | static void dcache_flush_invalidate(volatile long *p) |
| 33 | { |
| 34 | uintptr_t start, stop; |
| 35 | start = ALIGN_DOWN((uintptr_t)p, MEMSIZE_CACHELINE_SIZE); |
| 36 | stop = start + MEMSIZE_CACHELINE_SIZE; |
| 37 | flush_dcache_range(start, stop); |
| 38 | invalidate_dcache_range(start, stop); |
| 39 | } |
| 40 | |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 41 | /* |
| 42 | * Check memory range for valid RAM. A simple memory test determines |
| 43 | * the actually available RAM size between addresses `base' and |
| 44 | * `base + maxsize'. |
| 45 | */ |
Albert ARIBAUD | a55d23c | 2011-07-03 05:55:33 +0000 | [diff] [blame] | 46 | long get_ram_size(long *base, long maxsize) |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 47 | { |
| 48 | volatile long *addr; |
Tien Fong Chee | 67a2616 | 2018-06-20 15:06:20 +0800 | [diff] [blame] | 49 | long save[BITS_PER_LONG - 1]; |
Patrick Delaunay | c5da05c | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 50 | long save_base; |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 51 | long cnt; |
| 52 | long val; |
| 53 | long size; |
| 54 | int i = 0; |
Emanuele Ghidoli | 1c64b98 | 2023-05-30 15:33:27 +0200 | [diff] [blame] | 55 | int dcache_en = dcache_status(); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 56 | |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 57 | for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) { |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 58 | addr = base + cnt; /* pointer arith! */ |
Wolfgang Denk | 95099fe | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 59 | sync(); |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 60 | save[i++] = *addr; |
Wolfgang Denk | 95099fe | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 61 | sync(); |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 62 | *addr = ~cnt; |
Emanuele Ghidoli | 1c64b98 | 2023-05-30 15:33:27 +0200 | [diff] [blame] | 63 | if (dcache_en) |
| 64 | dcache_flush_invalidate(addr); |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 65 | } |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 66 | |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 67 | addr = base; |
Eddy Petrișor | 8e7cba0 | 2016-02-02 22:15:28 +0200 | [diff] [blame] | 68 | sync(); |
Patrick Delaunay | c5da05c | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 69 | save_base = *addr; |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 70 | sync(); |
| 71 | *addr = 0; |
| 72 | |
| 73 | sync(); |
Emanuele Ghidoli | 1c64b98 | 2023-05-30 15:33:27 +0200 | [diff] [blame] | 74 | if (dcache_en) |
| 75 | dcache_flush_invalidate(addr); |
| 76 | |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 77 | if ((val = *addr) != 0) { |
| 78 | /* Restore the original data before leaving the function. */ |
| 79 | sync(); |
Patrick Delaunay | c5da05c | 2018-01-25 18:07:45 +0100 | [diff] [blame] | 80 | *base = save_base; |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 81 | for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { |
| 82 | addr = base + cnt; |
| 83 | sync(); |
| 84 | *addr = save[--i]; |
| 85 | } |
| 86 | return (0); |
| 87 | } |
| 88 | |
| 89 | for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) { |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 90 | addr = base + cnt; /* pointer arith! */ |
| 91 | val = *addr; |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 92 | *addr = save[--i]; |
| 93 | if (val != ~cnt) { |
Wolfgang Denk | 95099fe | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 94 | size = cnt * sizeof(long); |
| 95 | /* |
| 96 | * Restore the original data |
| 97 | * before leaving the function. |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 98 | */ |
Wolfgang Denk | 95099fe | 2014-10-21 22:14:10 +0200 | [diff] [blame] | 99 | for (cnt <<= 1; |
| 100 | cnt < maxsize / sizeof(long); |
| 101 | cnt <<= 1) { |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 102 | addr = base + cnt; |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 103 | *addr = save[--i]; |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 104 | } |
Patrick Delaunay | 218da80 | 2018-01-25 18:07:46 +0100 | [diff] [blame] | 105 | /* warning: don't restore save_base in this case, |
| 106 | * it is already done in the loop because |
| 107 | * base and base+size share the same physical memory |
| 108 | * and *base is saved after *(base+size) modification |
| 109 | * in first loop |
| 110 | */ |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 111 | return (size); |
| 112 | } |
Hans de Goede | cc8d698 | 2016-02-09 22:38:31 +0100 | [diff] [blame] | 113 | } |
Patrick Delaunay | 218da80 | 2018-01-25 18:07:46 +0100 | [diff] [blame] | 114 | *base = save_base; |
wdenk | c83bf6a | 2004-01-06 22:38:14 +0000 | [diff] [blame] | 115 | |
| 116 | return (maxsize); |
| 117 | } |
York Sun | e386616 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 118 | |
| 119 | phys_size_t __weak get_effective_memsize(void) |
| 120 | { |
Pali Rohár | 777aaaa | 2022-09-09 17:32:39 +0200 | [diff] [blame] | 121 | phys_size_t ram_size = gd->ram_size; |
| 122 | |
Pali Rohár | d1f4b09 | 2023-01-07 22:55:26 +0100 | [diff] [blame] | 123 | #ifdef CONFIG_MPC85xx |
Pali Rohár | 777aaaa | 2022-09-09 17:32:39 +0200 | [diff] [blame] | 124 | /* |
| 125 | * Check for overflow and limit ram size to some representable value. |
| 126 | * It is required that ram_base + ram_size must be representable by |
| 127 | * phys_size_t type and must be aligned by direct access, therefore |
| 128 | * calculate it from last 4kB sector which should work as alignment |
| 129 | * on any platform. |
| 130 | */ |
| 131 | if (gd->ram_base + ram_size < gd->ram_base) |
| 132 | ram_size = ((phys_size_t)~0xfffULL) - gd->ram_base; |
Pali Rohár | d1f4b09 | 2023-01-07 22:55:26 +0100 | [diff] [blame] | 133 | #endif |
Pali Rohár | 777aaaa | 2022-09-09 17:32:39 +0200 | [diff] [blame] | 134 | |
Tom Rini | 1d457db | 2022-12-04 10:04:50 -0500 | [diff] [blame] | 135 | #ifndef CFG_MAX_MEM_MAPPED |
Pali Rohár | 777aaaa | 2022-09-09 17:32:39 +0200 | [diff] [blame] | 136 | return ram_size; |
York Sun | e386616 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 137 | #else |
| 138 | /* limit stack to what we can reasonable map */ |
Tom Rini | 1d457db | 2022-12-04 10:04:50 -0500 | [diff] [blame] | 139 | return ((ram_size > CFG_MAX_MEM_MAPPED) ? |
| 140 | CFG_MAX_MEM_MAPPED : ram_size); |
York Sun | e386616 | 2014-02-11 11:57:26 -0800 | [diff] [blame] | 141 | #endif |
| 142 | } |