blob: 73b92c8a00e4f76ebde34a50b5da4847e4599455 [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
Wolfgang Denk91650b32006-11-06 17:06:36 +01008#include <config.h>
9#ifdef __PPC__
10/*
11 * At least on G2 PowerPC cores, sequential accesses to non-existent
12 * memory must be synchronized.
13 */
14# include <asm/io.h> /* for sync() */
15#else
16# define sync() /* nothing */
17#endif
wdenkc83bf6a2004-01-06 22:38:14 +000018
19/*
20 * Check memory range for valid RAM. A simple memory test determines
21 * the actually available RAM size between addresses `base' and
22 * `base + maxsize'.
23 */
Albert ARIBAUDa55d23c2011-07-03 05:55:33 +000024long get_ram_size(long *base, long maxsize)
wdenkc83bf6a2004-01-06 22:38:14 +000025{
26 volatile long *addr;
27 long save[32];
28 long cnt;
29 long val;
30 long size;
31 int i = 0;
32
33 for (cnt = (maxsize / sizeof (long)) >> 1; cnt > 0; cnt >>= 1) {
34 addr = base + cnt; /* pointer arith! */
Wolfgang Denk91650b32006-11-06 17:06:36 +010035 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000036 save[i++] = *addr;
Wolfgang Denk91650b32006-11-06 17:06:36 +010037 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000038 *addr = ~cnt;
39 }
40
41 addr = base;
Wolfgang Denk91650b32006-11-06 17:06:36 +010042 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000043 save[i] = *addr;
Wolfgang Denk91650b32006-11-06 17:06:36 +010044 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000045 *addr = 0;
46
Wolfgang Denk91650b32006-11-06 17:06:36 +010047 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000048 if ((val = *addr) != 0) {
49 /* Restore the original data before leaving the function.
50 */
Wolfgang Denk91650b32006-11-06 17:06:36 +010051 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000052 *addr = save[i];
53 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
54 addr = base + cnt;
Wolfgang Denk91650b32006-11-06 17:06:36 +010055 sync ();
wdenkc83bf6a2004-01-06 22:38:14 +000056 *addr = save[--i];
57 }
58 return (0);
59 }
wdenkc83bf6a2004-01-06 22:38:14 +000060
61 for (cnt = 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
62 addr = base + cnt; /* pointer arith! */
63 val = *addr;
64 *addr = save[--i];
65 if (val != ~cnt) {
66 size = cnt * sizeof (long);
67 /* Restore the original data before leaving the function.
68 */
69 for (cnt <<= 1; cnt < maxsize / sizeof (long); cnt <<= 1) {
70 addr = base + cnt;
71 *addr = save[--i];
72 }
73 return (size);
74 }
75 }
76
77 return (maxsize);
78}