blob: fa4b3e386b83075205bfe1edeb92b5d11952f0eb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Masahiro Yamada5894ca02014-10-03 19:21:06 +09002/*
Masahiro Yamada3e9952b2017-01-28 06:53:43 +09003 * Copyright (C) 2012-2015 Panasonic Corporation
4 * Copyright (C) 2015-2017 Socionext Inc.
5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamada5894ca02014-10-03 19:21:06 +09006 */
7
8#include <common.h>
Masahiro Yamada0f4ec052017-01-21 18:05:24 +09009#include <linux/errno.h>
Masahiro Yamada3b7fc3f2019-06-29 02:38:04 +090010#include <linux/io.h>
Masahiro Yamadadd74b942017-10-13 19:21:55 +090011#include <linux/kernel.h>
12#include <linux/printk.h>
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090013#include <linux/sizes.h>
Masahiro Yamadadd74b942017-10-13 19:21:55 +090014#include <asm/global_data.h>
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090015
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090016#include "sg-regs.h"
Masahiro Yamada51ea5a02016-06-17 19:24:29 +090017#include "soc-info.h"
18
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090019DECLARE_GLOBAL_DATA_PTR;
20
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090021struct uniphier_memif_data {
22 unsigned int soc_id;
23 unsigned long sparse_ch1_base;
24 int have_ch2;
25};
26
27static const struct uniphier_memif_data uniphier_memif_data[] = {
28 {
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090029 .soc_id = UNIPHIER_LD4_ID,
30 .sparse_ch1_base = 0xc0000000,
31 },
32 {
33 .soc_id = UNIPHIER_PRO4_ID,
34 .sparse_ch1_base = 0xa0000000,
35 },
36 {
37 .soc_id = UNIPHIER_SLD8_ID,
38 .sparse_ch1_base = 0xc0000000,
39 },
40 {
41 .soc_id = UNIPHIER_PRO5_ID,
42 .sparse_ch1_base = 0xc0000000,
43 },
44 {
45 .soc_id = UNIPHIER_PXS2_ID,
46 .sparse_ch1_base = 0xc0000000,
47 .have_ch2 = 1,
48 },
49 {
50 .soc_id = UNIPHIER_LD6B_ID,
51 .sparse_ch1_base = 0xc0000000,
52 .have_ch2 = 1,
53 },
54 {
55 .soc_id = UNIPHIER_LD11_ID,
56 .sparse_ch1_base = 0xc0000000,
57 },
58 {
59 .soc_id = UNIPHIER_LD20_ID,
60 .sparse_ch1_base = 0xc0000000,
61 .have_ch2 = 1,
62 },
63 {
64 .soc_id = UNIPHIER_PXS3_ID,
65 .sparse_ch1_base = 0xc0000000,
66 .have_ch2 = 1,
67 },
68};
69UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data)
70
Masahiro Yamada04cd4e72017-02-05 10:52:12 +090071struct uniphier_dram_map {
72 unsigned long base;
73 unsigned long size;
74};
75
76static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map)
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090077{
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090078 const struct uniphier_memif_data *data;
79 unsigned long size;
80 u32 val;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090081
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090082 data = uniphier_get_memif_data();
83 if (!data) {
84 pr_err("unsupported SoC\n");
85 return -EINVAL;
86 }
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090087
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090088 val = readl(SG_MEMCONF);
89
90 /* set up ch0 */
Masahiro Yamada04cd4e72017-02-05 10:52:12 +090091 dram_map[0].base = CONFIG_SYS_SDRAM_BASE;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090092
93 switch (val & SG_MEMCONF_CH0_SZ_MASK) {
94 case SG_MEMCONF_CH0_SZ_64M:
95 size = SZ_64M;
96 break;
97 case SG_MEMCONF_CH0_SZ_128M:
98 size = SZ_128M;
99 break;
100 case SG_MEMCONF_CH0_SZ_256M:
101 size = SZ_256M;
102 break;
103 case SG_MEMCONF_CH0_SZ_512M:
104 size = SZ_512M;
105 break;
106 case SG_MEMCONF_CH0_SZ_1G:
107 size = SZ_1G;
108 break;
109 default:
Masahiro Yamada0f5bf092017-02-20 12:09:00 +0900110 pr_err("error: invalid value is set to MEMCONF ch0 size\n");
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900111 return -EINVAL;
112 }
113
114 if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2)
115 size *= 2;
116
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900117 dram_map[0].size = size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900118
119 /* set up ch1 */
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900120 dram_map[1].base = dram_map[0].base + size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900121
122 if (val & SG_MEMCONF_SPARSEMEM) {
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900123 if (dram_map[1].base > data->sparse_ch1_base) {
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900124 pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n");
125 pr_warn("Only ch0 is available\n");
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900126 dram_map[1].base = 0;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900127 return 0;
128 }
129
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900130 dram_map[1].base = data->sparse_ch1_base;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900131 }
132
133 switch (val & SG_MEMCONF_CH1_SZ_MASK) {
134 case SG_MEMCONF_CH1_SZ_64M:
135 size = SZ_64M;
136 break;
137 case SG_MEMCONF_CH1_SZ_128M:
138 size = SZ_128M;
139 break;
140 case SG_MEMCONF_CH1_SZ_256M:
141 size = SZ_256M;
142 break;
143 case SG_MEMCONF_CH1_SZ_512M:
144 size = SZ_512M;
145 break;
146 case SG_MEMCONF_CH1_SZ_1G:
147 size = SZ_1G;
148 break;
149 default:
Masahiro Yamada0f5bf092017-02-20 12:09:00 +0900150 pr_err("error: invalid value is set to MEMCONF ch1 size\n");
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900151 return -EINVAL;
152 }
153
154 if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2)
155 size *= 2;
156
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900157 dram_map[1].size = size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900158
Masahiro Yamadabed16242017-02-20 12:10:05 +0900159 if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE)
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900160 return 0;
161
162 /* set up ch2 */
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900163 dram_map[2].base = dram_map[1].base + size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900164
165 switch (val & SG_MEMCONF_CH2_SZ_MASK) {
166 case SG_MEMCONF_CH2_SZ_64M:
167 size = SZ_64M;
168 break;
169 case SG_MEMCONF_CH2_SZ_128M:
170 size = SZ_128M;
171 break;
172 case SG_MEMCONF_CH2_SZ_256M:
173 size = SZ_256M;
174 break;
175 case SG_MEMCONF_CH2_SZ_512M:
176 size = SZ_512M;
177 break;
178 case SG_MEMCONF_CH2_SZ_1G:
179 size = SZ_1G;
180 break;
181 default:
Masahiro Yamada0f5bf092017-02-20 12:09:00 +0900182 pr_err("error: invalid value is set to MEMCONF ch2 size\n");
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900183 return -EINVAL;
184 }
185
186 if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2)
187 size *= 2;
188
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900189 dram_map[2].size = size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900190
191 return 0;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900192}
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900193
194int dram_init(void)
195{
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900196 struct uniphier_dram_map dram_map[3] = {};
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900197 int ret, i;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900198
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900199 gd->ram_size = 0;
200
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900201 ret = uniphier_memconf_decode(dram_map);
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900202 if (ret)
203 return ret;
204
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900205 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
Masahiro Yamadabe893a52018-01-06 22:59:24 +0900206 unsigned long max_size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900207
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900208 if (!dram_map[i].size)
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900209 break;
210
211 /*
212 * U-Boot relocates itself to the tail of the memory region,
213 * but it does not expect sparse memory. We use the first
214 * contiguous chunk here.
215 */
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900216 if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size <
217 dram_map[i].base)
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900218 break;
219
Masahiro Yamadabe893a52018-01-06 22:59:24 +0900220 /*
221 * Do not use memory that exceeds 32bit address range. U-Boot
222 * relocates itself to the end of the effectively available RAM.
223 * This could be a problem for DMA engines that do not support
224 * 64bit address (SDMA of SDHCI, UniPhier AV-ether, etc.)
225 */
226 if (dram_map[i].base >= 1ULL << 32)
227 break;
228
229 max_size = (1ULL << 32) - dram_map[i].base;
230
231 if (dram_map[i].size > max_size) {
232 gd->ram_size += max_size;
233 break;
234 }
235
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900236 gd->ram_size += dram_map[i].size;
Masahiro Yamadaac2a1032016-03-29 20:18:45 +0900237 }
238
Masahiro Yamadaa322eb92018-01-06 22:59:26 +0900239 /*
240 * LD20 uses the last 64 byte for each channel for dynamic
241 * DDR PHY training
242 */
243 if (uniphier_get_soc_id() == UNIPHIER_LD20_ID)
244 gd->ram_size -= 64;
245
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900246 return 0;
247}
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900248
Simon Glass76b00ac2017-03-31 08:40:32 -0600249int dram_init_banksize(void)
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900250{
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900251 struct uniphier_dram_map dram_map[3] = {};
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900252 int i;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900253
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900254 uniphier_memconf_decode(dram_map);
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900255
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900256 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900257 if (i >= ARRAY_SIZE(gd->bd->bi_dram))
258 break;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900259
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900260 gd->bd->bi_dram[i].start = dram_map[i].base;
261 gd->bd->bi_dram[i].size = dram_map[i].size;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900262 }
Simon Glass76b00ac2017-03-31 08:40:32 -0600263
264 return 0;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900265}