]> git.sur5r.net Git - u-boot/blob - common/memsize.c
5c0d279625a005484f055e19b8dc7183d2c4dd56
[u-boot] / common / memsize.c
1 /*
2  * (C) Copyright 2004
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #include <common.h>
9
10 DECLARE_GLOBAL_DATA_PTR;
11
12 #ifdef __PPC__
13 /*
14  * At least on G2 PowerPC cores, sequential accesses to non-existent
15  * memory must be synchronized.
16  */
17 # include <asm/io.h>    /* for sync() */
18 #else
19 # define sync()         /* nothing */
20 #endif
21
22 /*
23  * Check memory range for valid RAM. A simple memory test determines
24  * the actually available RAM size between addresses `base' and
25  * `base + maxsize'.
26  */
27 long get_ram_size(long *base, long maxsize)
28 {
29         volatile long *addr;
30         long           save[32];
31         long           cnt;
32         long           val;
33         long           size;
34         int            i = 0;
35
36         for (cnt = (maxsize / sizeof(long)) >> 1; cnt >= 0; cnt >>= 1) {
37                 addr = base + cnt;      /* pointer arith! */
38                 sync();
39                 save[i] = *addr;
40                 sync();
41                 if (cnt) {
42                         i++;
43                         *addr = ~cnt;
44                 } else {
45                         *addr = 0;
46                 }
47         }
48
49         sync();
50         cnt = 0;
51         do {
52                 addr = base + cnt;      /* pointer arith! */
53                 val = *addr;
54                 *addr = save[i--];
55                 sync();
56                 if (((cnt == 0) && (val != 0)) ||
57                     ((cnt != 0) && (val != ~cnt))) {
58                         size = cnt * sizeof(long);
59                         /*
60                          * Restore the original data
61                          * before leaving the function.
62                          */
63                         for (cnt <<= 1;
64                              cnt < maxsize / sizeof(long);
65                              cnt <<= 1) {
66                                 addr  = base + cnt;
67                                 *addr = save[i--];
68                         }
69                         return (size);
70                 }
71
72                 if (cnt)
73                         cnt = cnt << 1;
74                 else
75                         cnt = 1;
76         } while (cnt < maxsize / sizeof(long));
77
78         return (maxsize);
79 }
80
81 phys_size_t __weak get_effective_memsize(void)
82 {
83 #ifndef CONFIG_VERY_BIG_RAM
84         return gd->ram_size;
85 #else
86         /* limit stack to what we can reasonable map */
87         return ((gd->ram_size > CONFIG_MAX_MEM_MAPPED) ?
88                 CONFIG_MAX_MEM_MAPPED : gd->ram_size);
89 #endif
90 }