blob: d5d13d51bf1fd84f44e8ec8763257f88d8719fd6 [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
York Sun863e8d82014-02-11 11:57:26 -08007#include <common.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>
York Sun863e8d82014-02-11 11:57:26 -080010
11DECLARE_GLOBAL_DATA_PTR;
12
Wolfgang Denkc0cc46e2006-11-06 17:06:36 +010013#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
wdenk87249ba2004-01-06 22:38:14 +000022
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 ARIBAUDa9606732011-07-03 05:55:33 +000028long get_ram_size(long *base, long maxsize)
wdenk87249ba2004-01-06 22:38:14 +000029{
30 volatile long *addr;
Tien Fong Chee62fd0c12018-06-20 15:06:20 +080031 long save[BITS_PER_LONG - 1];
Patrick Delaunay7544ec52018-01-25 18:07:45 +010032 long save_base;
wdenk87249ba2004-01-06 22:38:14 +000033 long cnt;
34 long val;
35 long size;
36 int i = 0;
37
Hans de Goedeff945792016-02-09 22:38:31 +010038 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
wdenk87249ba2004-01-06 22:38:14 +000039 addr = base + cnt; /* pointer arith! */
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020040 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010041 save[i++] = *addr;
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020042 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010043 *addr = ~cnt;
wdenk87249ba2004-01-06 22:38:14 +000044 }
wdenk87249ba2004-01-06 22:38:14 +000045
Hans de Goedeff945792016-02-09 22:38:31 +010046 addr = base;
47 sync();
Patrick Delaunay7544ec52018-01-25 18:07:45 +010048 save_base = *addr;
Hans de Goedeff945792016-02-09 22:38:31 +010049 sync();
50 *addr = 0;
51
Eddy Petrișor3ef69de2016-02-02 22:15:28 +020052 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010053 if ((val = *addr) != 0) {
54 /* Restore the original data before leaving the function. */
55 sync();
Patrick Delaunay7544ec52018-01-25 18:07:45 +010056 *base = save_base;
Hans de Goedeff945792016-02-09 22:38:31 +010057 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) {
wdenk87249ba2004-01-06 22:38:14 +000066 addr = base + cnt; /* pointer arith! */
67 val = *addr;
Hans de Goedeff945792016-02-09 22:38:31 +010068 *addr = save[--i];
69 if (val != ~cnt) {
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020070 size = cnt * sizeof(long);
71 /*
72 * Restore the original data
73 * before leaving the function.
wdenk87249ba2004-01-06 22:38:14 +000074 */
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020075 for (cnt <<= 1;
76 cnt < maxsize / sizeof(long);
77 cnt <<= 1) {
wdenk87249ba2004-01-06 22:38:14 +000078 addr = base + cnt;
Hans de Goedeff945792016-02-09 22:38:31 +010079 *addr = save[--i];
wdenk87249ba2004-01-06 22:38:14 +000080 }
Patrick Delaunay0e806a72018-01-25 18:07:46 +010081 /* 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 */
wdenk87249ba2004-01-06 22:38:14 +000087 return (size);
88 }
Hans de Goedeff945792016-02-09 22:38:31 +010089 }
Patrick Delaunay0e806a72018-01-25 18:07:46 +010090 *base = save_base;
wdenk87249ba2004-01-06 22:38:14 +000091
92 return (maxsize);
93}
York Sun863e8d82014-02-11 11:57:26 -080094
95phys_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}