blob: d646df8b04cb6f5d2e26c84e6d6b34d84ad14f66 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc83bf6a2004-01-06 22:38:14 +00002/*
3 * (C) Copyright 2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenkc83bf6a2004-01-06 22:38:14 +00005 */
6
York Sune3866162014-02-11 11:57:26 -08007#include <common.h>
Simon Glass9b4a2052019-12-28 10:45:05 -07008#include <init.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Emanuele Ghidoli1c64b982023-05-30 15:33:27 +020010#include <cpu_func.h>
11#include <stdint.h>
York Sune3866162014-02-11 11:57:26 -080012
13DECLARE_GLOBAL_DATA_PTR;
14
Emanuele Ghidoli1c64b982023-05-30 15:33:27 +020015#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 Denk91650b32006-11-06 17:06:36 +010022#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
wdenkc83bf6a2004-01-06 22:38:14 +000031
Emanuele Ghidoli1c64b982023-05-30 15:33:27 +020032static 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
wdenkc83bf6a2004-01-06 22:38:14 +000041/*
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 ARIBAUDa55d23c2011-07-03 05:55:33 +000046long get_ram_size(long *base, long maxsize)
wdenkc83bf6a2004-01-06 22:38:14 +000047{
48 volatile long *addr;
Tien Fong Chee67a26162018-06-20 15:06:20 +080049 long save[BITS_PER_LONG - 1];
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010050 long save_base;
wdenkc83bf6a2004-01-06 22:38:14 +000051 long cnt;
52 long val;
53 long size;
54 int i = 0;
Emanuele Ghidoli1c64b982023-05-30 15:33:27 +020055 int dcache_en = dcache_status();
wdenkc83bf6a2004-01-06 22:38:14 +000056
Hans de Goedecc8d6982016-02-09 22:38:31 +010057 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +000058 addr = base + cnt; /* pointer arith! */
Wolfgang Denk95099fe2014-10-21 22:14:10 +020059 sync();
Hans de Goedecc8d6982016-02-09 22:38:31 +010060 save[i++] = *addr;
Wolfgang Denk95099fe2014-10-21 22:14:10 +020061 sync();
Hans de Goedecc8d6982016-02-09 22:38:31 +010062 *addr = ~cnt;
Emanuele Ghidoli1c64b982023-05-30 15:33:27 +020063 if (dcache_en)
64 dcache_flush_invalidate(addr);
wdenkc83bf6a2004-01-06 22:38:14 +000065 }
wdenkc83bf6a2004-01-06 22:38:14 +000066
Hans de Goedecc8d6982016-02-09 22:38:31 +010067 addr = base;
Eddy Petrișor8e7cba02016-02-02 22:15:28 +020068 sync();
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010069 save_base = *addr;
Hans de Goedecc8d6982016-02-09 22:38:31 +010070 sync();
71 *addr = 0;
72
73 sync();
Emanuele Ghidoli1c64b982023-05-30 15:33:27 +020074 if (dcache_en)
75 dcache_flush_invalidate(addr);
76
Hans de Goedecc8d6982016-02-09 22:38:31 +010077 if ((val = *addr) != 0) {
78 /* Restore the original data before leaving the function. */
79 sync();
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010080 *base = save_base;
Hans de Goedecc8d6982016-02-09 22:38:31 +010081 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) {
wdenkc83bf6a2004-01-06 22:38:14 +000090 addr = base + cnt; /* pointer arith! */
91 val = *addr;
Hans de Goedecc8d6982016-02-09 22:38:31 +010092 *addr = save[--i];
93 if (val != ~cnt) {
Wolfgang Denk95099fe2014-10-21 22:14:10 +020094 size = cnt * sizeof(long);
95 /*
96 * Restore the original data
97 * before leaving the function.
wdenkc83bf6a2004-01-06 22:38:14 +000098 */
Wolfgang Denk95099fe2014-10-21 22:14:10 +020099 for (cnt <<= 1;
100 cnt < maxsize / sizeof(long);
101 cnt <<= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +0000102 addr = base + cnt;
Hans de Goedecc8d6982016-02-09 22:38:31 +0100103 *addr = save[--i];
wdenkc83bf6a2004-01-06 22:38:14 +0000104 }
Patrick Delaunay218da802018-01-25 18:07:46 +0100105 /* 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 */
wdenkc83bf6a2004-01-06 22:38:14 +0000111 return (size);
112 }
Hans de Goedecc8d6982016-02-09 22:38:31 +0100113 }
Patrick Delaunay218da802018-01-25 18:07:46 +0100114 *base = save_base;
wdenkc83bf6a2004-01-06 22:38:14 +0000115
116 return (maxsize);
117}
York Sune3866162014-02-11 11:57:26 -0800118
119phys_size_t __weak get_effective_memsize(void)
120{
Pali Rohár777aaaa2022-09-09 17:32:39 +0200121 phys_size_t ram_size = gd->ram_size;
122
Pali Rohárd1f4b092023-01-07 22:55:26 +0100123#ifdef CONFIG_MPC85xx
Pali Rohár777aaaa2022-09-09 17:32:39 +0200124 /*
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árd1f4b092023-01-07 22:55:26 +0100133#endif
Pali Rohár777aaaa2022-09-09 17:32:39 +0200134
Tom Rini1d457db2022-12-04 10:04:50 -0500135#ifndef CFG_MAX_MEM_MAPPED
Pali Rohár777aaaa2022-09-09 17:32:39 +0200136 return ram_size;
York Sune3866162014-02-11 11:57:26 -0800137#else
138 /* limit stack to what we can reasonable map */
Tom Rini1d457db2022-12-04 10:04:50 -0500139 return ((ram_size > CFG_MAX_MEM_MAPPED) ?
140 CFG_MAX_MEM_MAPPED : ram_size);
York Sune3866162014-02-11 11:57:26 -0800141#endif
142}