blob: 589400d3b14d8a8c53e9908ca2aec7cce66f0e11 [file] [log] [blame]
wdenkc83bf6a2004-01-06 22:38:14 +00001/*
2 * (C) Copyright 2004
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkc83bf6a2004-01-06 22:38:14 +00006 */
7
York Sune3866162014-02-11 11:57:26 -08008#include <common.h>
9
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;
30 long save[32];
31 long cnt;
32 long val;
33 long size;
34 int i = 0;
35
36 for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
37 addr = base + cnt; /* pointer arith! */
Wolfgang Denk91650b32006-11-06 17:06:36 +010038 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000039 save[i++] = *addr;
Wolfgang Denk91650b32006-11-06 17:06:36 +010040 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000041 *addr = ~cnt;
42 }
43
44 addr = base;
Wolfgang Denk91650b32006-11-06 17:06:36 +010045 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000046 save[i] = *addr;
Wolfgang Denk91650b32006-11-06 17:06:36 +010047 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000048 *addr = 0;
49
Wolfgang Denk91650b32006-11-06 17:06:36 +010050 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000051 if ((val = *addr) != 0) {
52 /* Restore the original data before leaving the function.
53 */
Wolfgang Denk91650b32006-11-06 17:06:36 +010054 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000055 *addr = save[i];
56 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
57 addr = base + cnt;
Wolfgang Denk91650b32006-11-06 17:06:36 +010058 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000059 *addr = save[--i];
60 }
61 return (0);
62 }
wdenkc83bf6a2004-01-06 22:38:14 +000063
64 for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
65 addr = base + cnt; /* pointer arith! */
66 val = *addr;
67 *addr = save[--i];
68 if (val != ~cnt) {
69 size = cnt * sizeof (long);
70 /* Restore the original data before leaving the function.
71 */
72 for (cnt <<= 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
73 addr = base + cnt;
74 *addr = save[--i];
75 }
76 return (size);
77 }
78 }
79
80 return (maxsize);
81}
York Sune3866162014-02-11 11:57:26 -080082
83phys_size_t __weak get_effective_memsize(void)
84{
85#ifndef CONFIG_VERY_BIG_RAM
86 return gd->ram_size;
87#else
88 /* limit stack to what we can reasonable map */
89 return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
90 CONFIG_MAX_MEM_MAPPED : gd->ram_size);
91#endif
92}