blob: 6dc4bb21b01e18e4079df7f3c8a06744dd0bc562 [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;
Patrick Delaunay7544ec52018-01-25 18:07:45 +010030 long save[31];
31 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 }
80 return (size);
81 }
Hans de Goedeff945792016-02-09 22:38:31 +010082 }
wdenk87249ba2004-01-06 22:38:14 +000083
84 return (maxsize);
85}
York Sun863e8d82014-02-11 11:57:26 -080086
87phys_size_t __weak get_effective_memsize(void)
88{
89#ifndef CONFIG_VERY_BIG_RAM
90 return gd->ram_size;
91#else
92 /* limit stack to what we can reasonable map */
93 return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
94 CONFIG_MAX_MEM_MAPPED : gd->ram_size);
95#endif
96}