blob: e95c68265ab92b0f53ccc75a4216aa70f30b026e [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>
York Sun863e8d82014-02-11 11:57:26 -08009
10DECLARE_GLOBAL_DATA_PTR;
11
Wolfgang Denkc0cc46e2006-11-06 17:06:36 +010012#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
wdenk87249ba2004-01-06 22:38:14 +000021
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 ARIBAUDa9606732011-07-03 05:55:33 +000027long get_ram_size(long *base, long maxsize)
wdenk87249ba2004-01-06 22:38:14 +000028{
29 volatile long *addr;
Tien Fong Chee62fd0c12018-06-20 15:06:20 +080030 long save[BITS_PER_LONG - 1];
Patrick Delaunay7544ec52018-01-25 18:07:45 +010031 long save_base;
wdenk87249ba2004-01-06 22:38:14 +000032 long cnt;
33 long val;
34 long size;
35 int i = 0;
36
Hans de Goedeff945792016-02-09 22:38:31 +010037 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
wdenk87249ba2004-01-06 22:38:14 +000038 addr = base + cnt; /* pointer arith! */
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020039 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010040 save[i++] = *addr;
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020041 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010042 *addr = ~cnt;
wdenk87249ba2004-01-06 22:38:14 +000043 }
wdenk87249ba2004-01-06 22:38:14 +000044
Hans de Goedeff945792016-02-09 22:38:31 +010045 addr = base;
46 sync();
Patrick Delaunay7544ec52018-01-25 18:07:45 +010047 save_base = *addr;
Hans de Goedeff945792016-02-09 22:38:31 +010048 sync();
49 *addr = 0;
50
Eddy Petrișor3ef69de2016-02-02 22:15:28 +020051 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010052 if ((val = *addr) != 0) {
53 /* Restore the original data before leaving the function. */
54 sync();
Patrick Delaunay7544ec52018-01-25 18:07:45 +010055 *base = save_base;
Hans de Goedeff945792016-02-09 22:38:31 +010056 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) {
wdenk87249ba2004-01-06 22:38:14 +000065 addr = base + cnt; /* pointer arith! */
66 val = *addr;
Hans de Goedeff945792016-02-09 22:38:31 +010067 *addr = save[--i];
68 if (val != ~cnt) {
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020069 size = cnt * sizeof(long);
70 /*
71 * Restore the original data
72 * before leaving the function.
wdenk87249ba2004-01-06 22:38:14 +000073 */
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020074 for (cnt <<= 1;
75 cnt < maxsize / sizeof(long);
76 cnt <<= 1) {
wdenk87249ba2004-01-06 22:38:14 +000077 addr = base + cnt;
Hans de Goedeff945792016-02-09 22:38:31 +010078 *addr = save[--i];
wdenk87249ba2004-01-06 22:38:14 +000079 }
Patrick Delaunay0e806a72018-01-25 18:07:46 +010080 /* 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 */
wdenk87249ba2004-01-06 22:38:14 +000086 return (size);
87 }
Hans de Goedeff945792016-02-09 22:38:31 +010088 }
Patrick Delaunay0e806a72018-01-25 18:07:46 +010089 *base = save_base;
wdenk87249ba2004-01-06 22:38:14 +000090
91 return (maxsize);
92}
York Sun863e8d82014-02-11 11:57:26 -080093
94phys_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}