blob: 3c80ad2c8346b1c158aae499722b1bbda7462fa9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk87249ba2004-01-06 22:38:14 +00002/*
3 * (C) Copyright 2004
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk87249ba2004-01-06 22:38:14 +00005 */
6
York Sun863e8d82014-02-11 11:57:26 -08007#include <common.h>
Simon Glass8e16b1e2019-12-28 10:45:05 -07008#include <init.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
York Sun863e8d82014-02-11 11:57:26 -080010
11DECLARE_GLOBAL_DATA_PTR;
12
Wolfgang Denkc0cc46e2006-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
wdenk87249ba2004-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 ARIBAUDa9606732011-07-03 05:55:33 +000028long get_ram_size(long *base, long maxsize)
wdenk87249ba2004-01-06 22:38:14 +000029{
30 volatile long *addr;
Tien Fong Chee62fd0c12018-06-20 15:06:20 +080031 long save[BITS_PER_LONG - 1];
Patrick Delaunay7544ec52018-01-25 18:07:45 +010032 long save_base;
wdenk87249ba2004-01-06 22:38:14 +000033 long cnt;
34 long val;
35 long size;
36 int i = 0;
37
Hans de Goedeff945792016-02-09 22:38:31 +010038 for (cnt = (maxsize / sizeof(long)) >> 1; cnt > 0; cnt >>= 1) {
wdenk87249ba2004-01-06 22:38:14 +000039 addr = base + cnt; /* pointer arith! */
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020040 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010041 save[i++] = *addr;
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020042 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010043 *addr = ~cnt;
wdenk87249ba2004-01-06 22:38:14 +000044 }
wdenk87249ba2004-01-06 22:38:14 +000045
Hans de Goedeff945792016-02-09 22:38:31 +010046 addr = base;
47 sync();
Patrick Delaunay7544ec52018-01-25 18:07:45 +010048 save_base = *addr;
Hans de Goedeff945792016-02-09 22:38:31 +010049 sync();
50 *addr = 0;
51
Eddy Petrișor3ef69de2016-02-02 22:15:28 +020052 sync();
Hans de Goedeff945792016-02-09 22:38:31 +010053 if ((val = *addr) != 0) {
54 /* Restore the original data before leaving the function. */
55 sync();
Patrick Delaunay7544ec52018-01-25 18:07:45 +010056 *base = save_base;
Hans de Goedeff945792016-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) {
wdenk87249ba2004-01-06 22:38:14 +000066 addr = base + cnt; /* pointer arith! */
67 val = *addr;
Hans de Goedeff945792016-02-09 22:38:31 +010068 *addr = save[--i];
69 if (val != ~cnt) {
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020070 size = cnt * sizeof(long);
71 /*
72 * Restore the original data
73 * before leaving the function.
wdenk87249ba2004-01-06 22:38:14 +000074 */
Wolfgang Denkcc17fd92014-10-21 22:14:10 +020075 for (cnt <<= 1;
76 cnt < maxsize / sizeof(long);
77 cnt <<= 1) {
wdenk87249ba2004-01-06 22:38:14 +000078 addr = base + cnt;
Hans de Goedeff945792016-02-09 22:38:31 +010079 *addr = save[--i];
wdenk87249ba2004-01-06 22:38:14 +000080 }
Patrick Delaunay0e806a72018-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 */
wdenk87249ba2004-01-06 22:38:14 +000087 return (size);
88 }
Hans de Goedeff945792016-02-09 22:38:31 +010089 }
Patrick Delaunay0e806a72018-01-25 18:07:46 +010090 *base = save_base;
wdenk87249ba2004-01-06 22:38:14 +000091
92 return (maxsize);
93}
York Sun863e8d82014-02-11 11:57:26 -080094
95phys_size_t __weak get_effective_memsize(void)
96{
Pali Rohár38ab9872022-09-09 17:32:39 +020097 phys_size_t ram_size = gd->ram_size;
98
99 /*
100 * Check for overflow and limit ram size to some representable value.
101 * It is required that ram_base + ram_size must be representable by
102 * phys_size_t type and must be aligned by direct access, therefore
103 * calculate it from last 4kB sector which should work as alignment
104 * on any platform.
105 */
106 if (gd->ram_base + ram_size < gd->ram_base)
107 ram_size = ((phys_size_t)~0xfffULL) - gd->ram_base;
108
Pali Rohár212ef412022-09-09 17:32:38 +0200109#ifndef CONFIG_MAX_MEM_MAPPED
Pali Rohár38ab9872022-09-09 17:32:39 +0200110 return ram_size;
York Sun863e8d82014-02-11 11:57:26 -0800111#else
112 /* limit stack to what we can reasonable map */
Pali Rohár38ab9872022-09-09 17:32:39 +0200113 return ((ram_size > CONFIG_MAX_MEM_MAPPED) ?
114 CONFIG_MAX_MEM_MAPPED : ram_size);
York Sun863e8d82014-02-11 11:57:26 -0800115#endif
116}