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