blob: 0fb9ba57b62b21d99df1fc8214c67a0016d1e63b [file] [log] [blame]
wdenk87249ba2004-01-06 22:38:14 +00001/*
2 * (C) Copyright 2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk87249ba2004-01-06 22:38:14 +00006 */
7
York Sun863e8d82014-02-11 11:57:26 -08008#include <common.h>
9
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;
30 long save[32];
31 long cnt;
32 long val;
33 long size;
34 int i = 0;
35
Hans de Goedeff945792016-02-09 22:38:31 +010036 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
wdenk87249ba2004-01-06 22:38:14 +000037 addr = base + cnt; /* pointer arith! */
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020038 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010039 save[i++] = *addr;
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020040 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010041 *addr = ~cnt;
wdenk87249ba2004-01-06 22:38:14 +000042 }
wdenk87249ba2004-01-06 22:38:14 +000043
Hans de Goedeff945792016-02-09 22:38:31 +010044 addr = base;
45 sync();
46 save[i] = *addr;
47 sync();
48 *addr = 0;
49
Eddy Petrișor3ef69de2016-02-02 22:15:28 +020050 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010051 if ((val = *addr) != 0) {
52 /* Restore the original data before leaving the function. */
53 sync();
54 *addr = save[i];
55 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
56 addr = base + cnt;
57 sync();
58 *addr = save[--i];
59 }
60 return (0);
61 }
62
63 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
wdenk87249ba2004-01-06 22:38:14 +000064 addr = base + cnt; /* pointer arith! */
65 val = *addr;
Hans de Goedeff945792016-02-09 22:38:31 +010066 *addr = save[--i];
67 if (val != ~cnt) {
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020068 size = cnt * sizeof(long);
69 /*
70 * Restore the original data
71 * before leaving the function.
wdenk87249ba2004-01-06 22:38:14 +000072 */
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020073 for (cnt <<= 1;
74 cnt < maxsize / sizeof(long);
75 cnt <<= 1) {
wdenk87249ba2004-01-06 22:38:14 +000076 addr = base + cnt;
Hans de Goedeff945792016-02-09 22:38:31 +010077 *addr = save[--i];
wdenk87249ba2004-01-06 22:38:14 +000078 }
79 return (size);
80 }
Hans de Goedeff945792016-02-09 22:38:31 +010081 }
wdenk87249ba2004-01-06 22:38:14 +000082
83 return (maxsize);
84}
York Sun863e8d82014-02-11 11:57:26 -080085
86phys_size_t __weak get_effective_memsize(void)
87{
88#ifndef CONFIG_VERY_BIG_RAM
89 return gd->ram_size;
90#else
91 /* limit stack to what we can reasonable map */
92 return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
93 CONFIG_MAX_MEM_MAPPED : gd->ram_size);
94#endif
95}