blob: 73b92c8a00e4f76ebde34a50b5da4847e4599455 [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
Wolfgang Denkc0cc46e2006-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
wdenk87249ba2004-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 ARIBAUDa9606732011-07-03 05:55:33 +000024long get_ram_size(long *base, long maxsize)
wdenk87249ba2004-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 Denkc0cc46e2006-11-06 17:06:36 +010035 sync ();
wdenk87249ba2004-01-06 22:38:14 +000036 save[i++] = *addr;
Wolfgang Denkc0cc46e2006-11-06 17:06:36 +010037 sync ();
wdenk87249ba2004-01-06 22:38:14 +000038 *addr = ~cnt;
39 }
40
41 addr = base;
Wolfgang Denkc0cc46e2006-11-06 17:06:36 +010042 sync ();
wdenk87249ba2004-01-06 22:38:14 +000043 save[i] = *addr;
Wolfgang Denkc0cc46e2006-11-06 17:06:36 +010044 sync ();
wdenk87249ba2004-01-06 22:38:14 +000045 *addr = 0;
46
Wolfgang Denkc0cc46e2006-11-06 17:06:36 +010047 sync ();
wdenk87249ba2004-01-06 22:38:14 +000048 if ((val = *addr) != 0) {
49 /* Restore the original data before leaving the function.
50 */
Wolfgang Denkc0cc46e2006-11-06 17:06:36 +010051 sync ();
wdenk87249ba2004-01-06 22:38:14 +000052 *addr = save[i];
53 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
54 addr = base + cnt;
Wolfgang Denkc0cc46e2006-11-06 17:06:36 +010055 sync ();
wdenk87249ba2004-01-06 22:38:14 +000056 *addr = save[--i];
57 }
58 return (0);
59 }
wdenk87249ba2004-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}