blob: 66d5be6a1ff3e31a824737cfebde70e48869c69a [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{
Pali Rohár777aaaa2022-09-09 17:32:39 +020097 phys_size_t ram_size = gd->ram_size;
98
Pali Rohárd1f4b092023-01-07 22:55:26 +010099#ifdef CONFIG_MPC85xx
Pali Rohár777aaaa2022-09-09 17:32:39 +0200100 /*
101 * Check for overflow and limit ram size to some representable value.
102 * It is required that ram_base + ram_size must be representable by
103 * phys_size_t type and must be aligned by direct access, therefore
104 * calculate it from last 4kB sector which should work as alignment
105 * on any platform.
106 */
107 if (gd->ram_base + ram_size < gd->ram_base)
108 ram_size = ((phys_size_t)~0xfffULL) - gd->ram_base;
Pali Rohárd1f4b092023-01-07 22:55:26 +0100109#endif
Pali Rohár777aaaa2022-09-09 17:32:39 +0200110
Tom Rini1d457db2022-12-04 10:04:50 -0500111#ifndef CFG_MAX_MEM_MAPPED
Pali Rohár777aaaa2022-09-09 17:32:39 +0200112 return ram_size;
York Sune3866162014-02-11 11:57:26 -0800113#else
114 /* limit stack to what we can reasonable map */
Tom Rini1d457db2022-12-04 10:04:50 -0500115 return ((ram_size > CFG_MAX_MEM_MAPPED) ?
116 CFG_MAX_MEM_MAPPED : ram_size);
York Sune3866162014-02-11 11:57:26 -0800117#endif
118}