blob: e95c68265ab92b0f53ccc75a4216aa70f30b026e [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>
Simon Glass9b4a2052019-12-28 10:45:05 -07008#include <init.h>
York Sune3866162014-02-11 11:57:26 -08009
10DECLARE_GLOBAL_DATA_PTR;
11
Wolfgang Denk91650b32006-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
wdenkc83bf6a2004-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 ARIBAUDa55d23c2011-07-03 05:55:33 +000027long get_ram_size(long *base, long maxsize)
wdenkc83bf6a2004-01-06 22:38:14 +000028{
29 volatile long *addr;
Tien Fong Chee67a26162018-06-20 15:06:20 +080030 long save[BITS_PER_LONG - 1];
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010031 long save_base;
wdenkc83bf6a2004-01-06 22:38:14 +000032 long cnt;
33 long val;
34 long size;
35 int i = 0;
36
Hans de Goedecc8d6982016-02-09 22:38:31 +010037 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +000038 addr = base + cnt; /* pointer arith! */
Wolfgang Denk95099fe2014-10-21 22:14:10 +020039 sync();
Hans de Goedecc8d6982016-02-09 22:38:31 +010040 save[i++] = *addr;
Wolfgang Denk95099fe2014-10-21 22:14:10 +020041 sync();
Hans de Goedecc8d6982016-02-09 22:38:31 +010042 *addr = ~cnt;
wdenkc83bf6a2004-01-06 22:38:14 +000043 }
wdenkc83bf6a2004-01-06 22:38:14 +000044
Hans de Goedecc8d6982016-02-09 22:38:31 +010045 addr = base;
Eddy Petrișor8e7cba02016-02-02 22:15:28 +020046 sync();
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010047 save_base = *addr;
Hans de Goedecc8d6982016-02-09 22:38:31 +010048 sync();
49 *addr = 0;
50
51 sync();
52 if ((val = *addr) != 0) {
53 /* Restore the original data before leaving the function. */
54 sync();
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010055 *base = save_base;
Hans de Goedecc8d6982016-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) {
wdenkc83bf6a2004-01-06 22:38:14 +000065 addr = base + cnt; /* pointer arith! */
66 val = *addr;
Hans de Goedecc8d6982016-02-09 22:38:31 +010067 *addr = save[--i];
68 if (val != ~cnt) {
Wolfgang Denk95099fe2014-10-21 22:14:10 +020069 size = cnt * sizeof(long);
70 /*
71 * Restore the original data
72 * before leaving the function.
wdenkc83bf6a2004-01-06 22:38:14 +000073 */
Wolfgang Denk95099fe2014-10-21 22:14:10 +020074 for (cnt <<= 1;
75 cnt < maxsize / sizeof(long);
76 cnt <<= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +000077 addr = base + cnt;
Hans de Goedecc8d6982016-02-09 22:38:31 +010078 *addr = save[--i];
wdenkc83bf6a2004-01-06 22:38:14 +000079 }
Patrick Delaunay218da802018-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 */
wdenkc83bf6a2004-01-06 22:38:14 +000086 return (size);
87 }
Hans de Goedecc8d6982016-02-09 22:38:31 +010088 }
Patrick Delaunay218da802018-01-25 18:07:46 +010089 *base = save_base;
wdenkc83bf6a2004-01-06 22:38:14 +000090
91 return (maxsize);
92}
York Sune3866162014-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}