blob: 13b004778667f98d15b6f8dfbcbdea808ab0e0a8 [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>
8
9DECLARE_GLOBAL_DATA_PTR;
10
Wolfgang Denk91650b32006-11-06 17:06:36 +010011#ifdef __PPC__
12/*
13 * At least on G2 PowerPC cores, sequential accesses to non-existent
14 * memory must be synchronized.
15 */
16# include <asm/io.h> /* for sync() */
17#else
18# define sync() /* nothing */
19#endif
wdenkc83bf6a2004-01-06 22:38:14 +000020
21/*
22 * Check memory range for valid RAM. A simple memory test determines
23 * the actually available RAM size between addresses `base' and
24 * `base + maxsize'.
25 */
Albert ARIBAUDa55d23c2011-07-03 05:55:33 +000026long get_ram_size(long *base, long maxsize)
wdenkc83bf6a2004-01-06 22:38:14 +000027{
28 volatile long *addr;
Tien Fong Chee67a26162018-06-20 15:06:20 +080029 long save[BITS_PER_LONG - 1];
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010030 long save_base;
wdenkc83bf6a2004-01-06 22:38:14 +000031 long cnt;
32 long val;
33 long size;
34 int i = 0;
35
Hans de Goedecc8d6982016-02-09 22:38:31 +010036 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +000037 addr = base + cnt; /* pointer arith! */
Wolfgang Denk95099fe2014-10-21 22:14:10 +020038 sync();
Hans de Goedecc8d6982016-02-09 22:38:31 +010039 save[i++] = *addr;
Wolfgang Denk95099fe2014-10-21 22:14:10 +020040 sync();
Hans de Goedecc8d6982016-02-09 22:38:31 +010041 *addr = ~cnt;
wdenkc83bf6a2004-01-06 22:38:14 +000042 }
wdenkc83bf6a2004-01-06 22:38:14 +000043
Hans de Goedecc8d6982016-02-09 22:38:31 +010044 addr = base;
Eddy Petrișor8e7cba02016-02-02 22:15:28 +020045 sync();
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010046 save_base = *addr;
Hans de Goedecc8d6982016-02-09 22:38:31 +010047 sync();
48 *addr = 0;
49
50 sync();
51 if ((val = *addr) != 0) {
52 /* Restore the original data before leaving the function. */
53 sync();
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010054 *base = save_base;
Hans de Goedecc8d6982016-02-09 22:38:31 +010055 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) {
wdenkc83bf6a2004-01-06 22:38:14 +000064 addr = base + cnt; /* pointer arith! */
65 val = *addr;
Hans de Goedecc8d6982016-02-09 22:38:31 +010066 *addr = save[--i];
67 if (val != ~cnt) {
Wolfgang Denk95099fe2014-10-21 22:14:10 +020068 size = cnt * sizeof(long);
69 /*
70 * Restore the original data
71 * before leaving the function.
wdenkc83bf6a2004-01-06 22:38:14 +000072 */
Wolfgang Denk95099fe2014-10-21 22:14:10 +020073 for (cnt <<= 1;
74 cnt < maxsize / sizeof(long);
75 cnt <<= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +000076 addr = base + cnt;
Hans de Goedecc8d6982016-02-09 22:38:31 +010077 *addr = save[--i];
wdenkc83bf6a2004-01-06 22:38:14 +000078 }
Patrick Delaunay218da802018-01-25 18:07:46 +010079 /* warning: don't restore save_base in this case,
80 * it is already done in the loop because
81 * base and base+size share the same physical memory
82 * and *base is saved after *(base+size) modification
83 * in first loop
84 */
wdenkc83bf6a2004-01-06 22:38:14 +000085 return (size);
86 }
Hans de Goedecc8d6982016-02-09 22:38:31 +010087 }
Patrick Delaunay218da802018-01-25 18:07:46 +010088 *base = save_base;
wdenkc83bf6a2004-01-06 22:38:14 +000089
90 return (maxsize);
91}
York Sune3866162014-02-11 11:57:26 -080092
93phys_size_t __weak get_effective_memsize(void)
94{
95#ifndef CONFIG_VERY_BIG_RAM
96 return gd->ram_size;
97#else
98 /* limit stack to what we can reasonable map */
99 return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
100 CONFIG_MAX_MEM_MAPPED : gd->ram_size);
101#endif
102}