blob: 2eb4836256e23a6256d7fb75311e8b31b363cff3 [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 Yamada1d21e1b2017-06-22 16:42:04 +09009#include <fdt_support.h>
Masahiro Yamada7b3a0322016-04-21 14:43:12 +090010#include <fdtdec.h>
Masahiro Yamada0f4ec052017-01-21 18:05:24 +090011#include <linux/errno.h>
Masahiro Yamadadd74b942017-10-13 19:21:55 +090012#include <linux/kernel.h>
13#include <linux/printk.h>
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090014#include <linux/sizes.h>
Masahiro Yamadadd74b942017-10-13 19:21:55 +090015#include <asm/global_data.h>
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090016
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090017#include "sg-regs.h"
Masahiro Yamada51ea5a02016-06-17 19:24:29 +090018#include "soc-info.h"
19
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090020DECLARE_GLOBAL_DATA_PTR;
21
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090022struct uniphier_memif_data {
23 unsigned int soc_id;
24 unsigned long sparse_ch1_base;
25 int have_ch2;
26};
27
28static const struct uniphier_memif_data uniphier_memif_data[] = {
29 {
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090030 .soc_id = UNIPHIER_LD4_ID,
31 .sparse_ch1_base = 0xc0000000,
32 },
33 {
34 .soc_id = UNIPHIER_PRO4_ID,
35 .sparse_ch1_base = 0xa0000000,
36 },
37 {
38 .soc_id = UNIPHIER_SLD8_ID,
39 .sparse_ch1_base = 0xc0000000,
40 },
41 {
42 .soc_id = UNIPHIER_PRO5_ID,
43 .sparse_ch1_base = 0xc0000000,
44 },
45 {
46 .soc_id = UNIPHIER_PXS2_ID,
47 .sparse_ch1_base = 0xc0000000,
48 .have_ch2 = 1,
49 },
50 {
51 .soc_id = UNIPHIER_LD6B_ID,
52 .sparse_ch1_base = 0xc0000000,
53 .have_ch2 = 1,
54 },
55 {
56 .soc_id = UNIPHIER_LD11_ID,
57 .sparse_ch1_base = 0xc0000000,
58 },
59 {
60 .soc_id = UNIPHIER_LD20_ID,
61 .sparse_ch1_base = 0xc0000000,
62 .have_ch2 = 1,
63 },
64 {
65 .soc_id = UNIPHIER_PXS3_ID,
66 .sparse_ch1_base = 0xc0000000,
67 .have_ch2 = 1,
68 },
69};
70UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data)
71
Masahiro Yamada04cd4e72017-02-05 10:52:12 +090072struct uniphier_dram_map {
73 unsigned long base;
74 unsigned long size;
75};
76
77static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map)
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090078{
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090079 const struct uniphier_memif_data *data;
80 unsigned long size;
81 u32 val;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090082
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090083 data = uniphier_get_memif_data();
84 if (!data) {
85 pr_err("unsupported SoC\n");
86 return -EINVAL;
87 }
Masahiro Yamadacf88aff2015-09-11 20:17:49 +090088
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090089 val = readl(SG_MEMCONF);
90
91 /* set up ch0 */
Masahiro Yamada04cd4e72017-02-05 10:52:12 +090092 dram_map[0].base = CONFIG_SYS_SDRAM_BASE;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +090093
94 switch (val & SG_MEMCONF_CH0_SZ_MASK) {
95 case SG_MEMCONF_CH0_SZ_64M:
96 size = SZ_64M;
97 break;
98 case SG_MEMCONF_CH0_SZ_128M:
99 size = SZ_128M;
100 break;
101 case SG_MEMCONF_CH0_SZ_256M:
102 size = SZ_256M;
103 break;
104 case SG_MEMCONF_CH0_SZ_512M:
105 size = SZ_512M;
106 break;
107 case SG_MEMCONF_CH0_SZ_1G:
108 size = SZ_1G;
109 break;
110 default:
Masahiro Yamada0f5bf092017-02-20 12:09:00 +0900111 pr_err("error: invalid value is set to MEMCONF ch0 size\n");
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900112 return -EINVAL;
113 }
114
115 if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2)
116 size *= 2;
117
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900118 dram_map[0].size = size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900119
120 /* set up ch1 */
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900121 dram_map[1].base = dram_map[0].base + size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900122
123 if (val & SG_MEMCONF_SPARSEMEM) {
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900124 if (dram_map[1].base > data->sparse_ch1_base) {
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900125 pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n");
126 pr_warn("Only ch0 is available\n");
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900127 dram_map[1].base = 0;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900128 return 0;
129 }
130
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900131 dram_map[1].base = data->sparse_ch1_base;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900132 }
133
134 switch (val & SG_MEMCONF_CH1_SZ_MASK) {
135 case SG_MEMCONF_CH1_SZ_64M:
136 size = SZ_64M;
137 break;
138 case SG_MEMCONF_CH1_SZ_128M:
139 size = SZ_128M;
140 break;
141 case SG_MEMCONF_CH1_SZ_256M:
142 size = SZ_256M;
143 break;
144 case SG_MEMCONF_CH1_SZ_512M:
145 size = SZ_512M;
146 break;
147 case SG_MEMCONF_CH1_SZ_1G:
148 size = SZ_1G;
149 break;
150 default:
Masahiro Yamada0f5bf092017-02-20 12:09:00 +0900151 pr_err("error: invalid value is set to MEMCONF ch1 size\n");
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900152 return -EINVAL;
153 }
154
155 if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2)
156 size *= 2;
157
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900158 dram_map[1].size = size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900159
Masahiro Yamadabed16242017-02-20 12:10:05 +0900160 if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE)
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900161 return 0;
162
163 /* set up ch2 */
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900164 dram_map[2].base = dram_map[1].base + size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900165
166 switch (val & SG_MEMCONF_CH2_SZ_MASK) {
167 case SG_MEMCONF_CH2_SZ_64M:
168 size = SZ_64M;
169 break;
170 case SG_MEMCONF_CH2_SZ_128M:
171 size = SZ_128M;
172 break;
173 case SG_MEMCONF_CH2_SZ_256M:
174 size = SZ_256M;
175 break;
176 case SG_MEMCONF_CH2_SZ_512M:
177 size = SZ_512M;
178 break;
179 case SG_MEMCONF_CH2_SZ_1G:
180 size = SZ_1G;
181 break;
182 default:
Masahiro Yamada0f5bf092017-02-20 12:09:00 +0900183 pr_err("error: invalid value is set to MEMCONF ch2 size\n");
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900184 return -EINVAL;
185 }
186
187 if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2)
188 size *= 2;
189
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900190 dram_map[2].size = size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900191
192 return 0;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900193}
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900194
195int dram_init(void)
196{
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900197 struct uniphier_dram_map dram_map[3] = {};
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900198 int ret, i;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900199
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900200 gd->ram_size = 0;
201
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900202 ret = uniphier_memconf_decode(dram_map);
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900203 if (ret)
204 return ret;
205
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900206 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
Masahiro Yamadabe893a52018-01-06 22:59:24 +0900207 unsigned long max_size;
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900208
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900209 if (!dram_map[i].size)
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900210 break;
211
212 /*
213 * U-Boot relocates itself to the tail of the memory region,
214 * but it does not expect sparse memory. We use the first
215 * contiguous chunk here.
216 */
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900217 if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size <
218 dram_map[i].base)
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900219 break;
220
Masahiro Yamadabe893a52018-01-06 22:59:24 +0900221 /*
222 * Do not use memory that exceeds 32bit address range. U-Boot
223 * relocates itself to the end of the effectively available RAM.
224 * This could be a problem for DMA engines that do not support
225 * 64bit address (SDMA of SDHCI, UniPhier AV-ether, etc.)
226 */
227 if (dram_map[i].base >= 1ULL << 32)
228 break;
229
230 max_size = (1ULL << 32) - dram_map[i].base;
231
232 if (dram_map[i].size > max_size) {
233 gd->ram_size += max_size;
234 break;
235 }
236
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900237 gd->ram_size += dram_map[i].size;
Masahiro Yamadaac2a1032016-03-29 20:18:45 +0900238 }
239
Masahiro Yamadaa322eb92018-01-06 22:59:26 +0900240 /*
241 * LD20 uses the last 64 byte for each channel for dynamic
242 * DDR PHY training
243 */
244 if (uniphier_get_soc_id() == UNIPHIER_LD20_ID)
245 gd->ram_size -= 64;
246
Masahiro Yamada5894ca02014-10-03 19:21:06 +0900247 return 0;
248}
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900249
Simon Glass76b00ac2017-03-31 08:40:32 -0600250int dram_init_banksize(void)
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900251{
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900252 struct uniphier_dram_map dram_map[3] = {};
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900253 int i;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900254
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900255 uniphier_memconf_decode(dram_map);
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900256
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900257 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
Masahiro Yamada3e9952b2017-01-28 06:53:43 +0900258 if (i >= ARRAY_SIZE(gd->bd->bi_dram))
259 break;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900260
Masahiro Yamada04cd4e72017-02-05 10:52:12 +0900261 gd->bd->bi_dram[i].start = dram_map[i].base;
262 gd->bd->bi_dram[i].size = dram_map[i].size;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900263 }
Simon Glass76b00ac2017-03-31 08:40:32 -0600264
265 return 0;
Masahiro Yamadacf88aff2015-09-11 20:17:49 +0900266}
Masahiro Yamada51ea5a02016-06-17 19:24:29 +0900267
268#ifdef CONFIG_OF_BOARD_SETUP
269/*
270 * The DRAM PHY requires 64 byte scratch area in each DRAM channel
271 * for its dynamic PHY training feature.
272 */
273int ft_board_setup(void *fdt, bd_t *bd)
274{
Masahiro Yamada51ea5a02016-06-17 19:24:29 +0900275 unsigned long rsv_addr;
276 const unsigned long rsv_size = 64;
Masahiro Yamadac995f3a2017-01-28 06:53:44 +0900277 int i, ret;
Masahiro Yamada51ea5a02016-06-17 19:24:29 +0900278
Masahiro Yamadae27d6c72017-01-21 18:05:26 +0900279 if (uniphier_get_soc_id() != UNIPHIER_LD20_ID)
Masahiro Yamada51ea5a02016-06-17 19:24:29 +0900280 return 0;
281
Masahiro Yamadac995f3a2017-01-28 06:53:44 +0900282 for (i = 0; i < ARRAY_SIZE(gd->bd->bi_dram); i++) {
Masahiro Yamada87c33082017-02-20 17:13:32 +0900283 if (!gd->bd->bi_dram[i].size)
284 continue;
285
Masahiro Yamadac995f3a2017-01-28 06:53:44 +0900286 rsv_addr = gd->bd->bi_dram[i].start + gd->bd->bi_dram[i].size;
Masahiro Yamada51ea5a02016-06-17 19:24:29 +0900287 rsv_addr -= rsv_size;
288
289 ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size);
290 if (ret)
291 return -ENOSPC;
292
Masahiro Yamadadd74b942017-10-13 19:21:55 +0900293 pr_notice(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n",
294 rsv_addr, rsv_size);
Masahiro Yamada51ea5a02016-06-17 19:24:29 +0900295 }
296
297 return 0;
298}
299#endif