blob: d5d13d51bf1fd84f44e8ec8763257f88d8719fd6 [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>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
York Sune3866162014-02-11 11:57:26 -080010
11DECLARE_GLOBAL_DATA_PTR;
12
Wolfgang Denk91650b32006-11-06 17:06:36 +010013#ifdef __PPC__
14/*
15 * At least on G2 PowerPC cores, sequential accesses to non-existent
16 * memory must be synchronized.
17 */
18# include <asm/io.h> /* for sync() */
19#else
20# define sync() /* nothing */
21#endif
wdenkc83bf6a2004-01-06 22:38:14 +000022
23/*
24 * Check memory range for valid RAM. A simple memory test determines
25 * the actually available RAM size between addresses `base' and
26 * `base + maxsize'.
27 */
Albert ARIBAUDa55d23c2011-07-03 05:55:33 +000028long get_ram_size(long *base, long maxsize)
wdenkc83bf6a2004-01-06 22:38:14 +000029{
30 volatile long *addr;
Tien Fong Chee67a26162018-06-20 15:06:20 +080031 long save[BITS_PER_LONG - 1];
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010032 long save_base;
wdenkc83bf6a2004-01-06 22:38:14 +000033 long cnt;
34 long val;
35 long size;
36 int i = 0;
37
Hans de Goedecc8d6982016-02-09 22:38:31 +010038 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +000039 addr = base + cnt; /* pointer arith! */
Wolfgang Denk95099fe2014-10-21 22:14:10 +020040 sync();
Hans de Goedecc8d6982016-02-09 22:38:31 +010041 save[i++] = *addr;
Wolfgang Denk95099fe2014-10-21 22:14:10 +020042 sync();
Hans de Goedecc8d6982016-02-09 22:38:31 +010043 *addr = ~cnt;
wdenkc83bf6a2004-01-06 22:38:14 +000044 }
wdenkc83bf6a2004-01-06 22:38:14 +000045
Hans de Goedecc8d6982016-02-09 22:38:31 +010046 addr = base;
Eddy Petrișor8e7cba02016-02-02 22:15:28 +020047 sync();
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010048 save_base = *addr;
Hans de Goedecc8d6982016-02-09 22:38:31 +010049 sync();
50 *addr = 0;
51
52 sync();
53 if ((val = *addr) != 0) {
54 /* Restore the original data before leaving the function. */
55 sync();
Patrick Delaunayc5da05c2018-01-25 18:07:45 +010056 *base = save_base;
Hans de Goedecc8d6982016-02-09 22:38:31 +010057 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
58 addr = base + cnt;
59 sync();
60 *addr = save[--i];
61 }
62 return (0);
63 }
64
65 for (cnt = 1; cnt < maxsize / sizeof(long); cnt <<= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +000066 addr = base + cnt; /* pointer arith! */
67 val = *addr;
Hans de Goedecc8d6982016-02-09 22:38:31 +010068 *addr = save[--i];
69 if (val != ~cnt) {
Wolfgang Denk95099fe2014-10-21 22:14:10 +020070 size = cnt * sizeof(long);
71 /*
72 * Restore the original data
73 * before leaving the function.
wdenkc83bf6a2004-01-06 22:38:14 +000074 */
Wolfgang Denk95099fe2014-10-21 22:14:10 +020075 for (cnt <<= 1;
76 cnt < maxsize / sizeof(long);
77 cnt <<= 1) {
wdenkc83bf6a2004-01-06 22:38:14 +000078 addr = base + cnt;
Hans de Goedecc8d6982016-02-09 22:38:31 +010079 *addr = save[--i];
wdenkc83bf6a2004-01-06 22:38:14 +000080 }
Patrick Delaunay218da802018-01-25 18:07:46 +010081 /* warning: don't restore save_base in this case,
82 * it is already done in the loop because
83 * base and base+size share the same physical memory
84 * and *base is saved after *(base+size) modification
85 * in first loop
86 */
wdenkc83bf6a2004-01-06 22:38:14 +000087 return (size);
88 }
Hans de Goedecc8d6982016-02-09 22:38:31 +010089 }
Patrick Delaunay218da802018-01-25 18:07:46 +010090 *base = save_base;
wdenkc83bf6a2004-01-06 22:38:14 +000091
92 return (maxsize);
93}
York Sune3866162014-02-11 11:57:26 -080094
95phys_size_t __weak get_effective_memsize(void)
96{
97#ifndef CONFIG_VERY_BIG_RAM
98 return gd->ram_size;
99#else
100 /* limit stack to what we can reasonable map */
101 return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
102 CONFIG_MAX_MEM_MAPPED : gd->ram_size);
103#endif
104}