blob: e9672d2f1bc3211dd795479b07626f19d2e03e72 [file] [log] [blame]
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09001/*
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +09002 * Copyright (C) 2012-2015 Panasonic Corporation
3 * Copyright (C) 2015-2017 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +09005 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
Masahiro Yamada5e05c4d2017-06-22 16:42:04 +090010#include <fdt_support.h>
Masahiro Yamada1f752802016-04-21 14:43:12 +090011#include <fdtdec.h>
Masahiro Yamadae4e789d2017-01-21 18:05:24 +090012#include <linux/errno.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090013#include <linux/kernel.h>
14#include <linux/printk.h>
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090015#include <linux/sizes.h>
Masahiro Yamada609cd532017-10-13 19:21:55 +090016#include <asm/global_data.h>
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090017
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090018#include "sg-regs.h"
Masahiro Yamada460483c2016-06-17 19:24:29 +090019#include "soc-info.h"
20
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090021DECLARE_GLOBAL_DATA_PTR;
22
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090023struct uniphier_memif_data {
24 unsigned int soc_id;
25 unsigned long sparse_ch1_base;
26 int have_ch2;
27};
28
29static const struct uniphier_memif_data uniphier_memif_data[] = {
30 {
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090031 .soc_id = UNIPHIER_LD4_ID,
32 .sparse_ch1_base = 0xc0000000,
33 },
34 {
35 .soc_id = UNIPHIER_PRO4_ID,
36 .sparse_ch1_base = 0xa0000000,
37 },
38 {
39 .soc_id = UNIPHIER_SLD8_ID,
40 .sparse_ch1_base = 0xc0000000,
41 },
42 {
43 .soc_id = UNIPHIER_PRO5_ID,
44 .sparse_ch1_base = 0xc0000000,
45 },
46 {
47 .soc_id = UNIPHIER_PXS2_ID,
48 .sparse_ch1_base = 0xc0000000,
49 .have_ch2 = 1,
50 },
51 {
52 .soc_id = UNIPHIER_LD6B_ID,
53 .sparse_ch1_base = 0xc0000000,
54 .have_ch2 = 1,
55 },
56 {
57 .soc_id = UNIPHIER_LD11_ID,
58 .sparse_ch1_base = 0xc0000000,
59 },
60 {
61 .soc_id = UNIPHIER_LD20_ID,
62 .sparse_ch1_base = 0xc0000000,
63 .have_ch2 = 1,
64 },
65 {
66 .soc_id = UNIPHIER_PXS3_ID,
67 .sparse_ch1_base = 0xc0000000,
68 .have_ch2 = 1,
69 },
70};
71UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data)
72
Masahiro Yamada3dc80972017-02-05 10:52:12 +090073struct uniphier_dram_map {
74 unsigned long base;
75 unsigned long size;
76};
77
78static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map)
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090079{
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090080 const struct uniphier_memif_data *data;
81 unsigned long size;
82 u32 val;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090083
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090084 data = uniphier_get_memif_data();
85 if (!data) {
86 pr_err("unsupported SoC\n");
87 return -EINVAL;
88 }
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090089
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090090 val = readl(SG_MEMCONF);
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +090091
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090092 /* set up ch0 */
Masahiro Yamada3dc80972017-02-05 10:52:12 +090093 dram_map[0].base = CONFIG_SYS_SDRAM_BASE;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +090094
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +090095 switch (val & SG_MEMCONF_CH0_SZ_MASK) {
96 case SG_MEMCONF_CH0_SZ_64M:
97 size = SZ_64M;
98 break;
99 case SG_MEMCONF_CH0_SZ_128M:
100 size = SZ_128M;
101 break;
102 case SG_MEMCONF_CH0_SZ_256M:
103 size = SZ_256M;
104 break;
105 case SG_MEMCONF_CH0_SZ_512M:
106 size = SZ_512M;
107 break;
108 case SG_MEMCONF_CH0_SZ_1G:
109 size = SZ_1G;
110 break;
111 default:
Masahiro Yamada1566db92017-02-20 12:09:00 +0900112 pr_err("error: invalid value is set to MEMCONF ch0 size\n");
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900113 return -EINVAL;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +0900114 }
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900115
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900116 if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2)
117 size *= 2;
118
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900119 dram_map[0].size = size;
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900120
121 /* set up ch1 */
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900122 dram_map[1].base = dram_map[0].base + size;
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900123
124 if (val & SG_MEMCONF_SPARSEMEM) {
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900125 if (dram_map[1].base > data->sparse_ch1_base) {
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900126 pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n");
127 pr_warn("Only ch0 is available\n");
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900128 dram_map[1].base = 0;
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900129 return 0;
130 }
131
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900132 dram_map[1].base = data->sparse_ch1_base;
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900133 }
134
135 switch (val & SG_MEMCONF_CH1_SZ_MASK) {
136 case SG_MEMCONF_CH1_SZ_64M:
137 size = SZ_64M;
138 break;
139 case SG_MEMCONF_CH1_SZ_128M:
140 size = SZ_128M;
141 break;
142 case SG_MEMCONF_CH1_SZ_256M:
143 size = SZ_256M;
144 break;
145 case SG_MEMCONF_CH1_SZ_512M:
146 size = SZ_512M;
147 break;
148 case SG_MEMCONF_CH1_SZ_1G:
149 size = SZ_1G;
150 break;
151 default:
Masahiro Yamada1566db92017-02-20 12:09:00 +0900152 pr_err("error: invalid value is set to MEMCONF ch1 size\n");
Masahiro Yamadaa90b1102016-03-29 20:18:45 +0900153 return -EINVAL;
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900154 }
Masahiro Yamadaa90b1102016-03-29 20:18:45 +0900155
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900156 if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2)
157 size *= 2;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +0900158
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900159 dram_map[1].size = size;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900160
Masahiro Yamada95beeed2017-02-20 12:10:05 +0900161 if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE)
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900162 return 0;
163
164 /* set up ch2 */
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900165 dram_map[2].base = dram_map[1].base + size;
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900166
167 switch (val & SG_MEMCONF_CH2_SZ_MASK) {
168 case SG_MEMCONF_CH2_SZ_64M:
169 size = SZ_64M;
170 break;
171 case SG_MEMCONF_CH2_SZ_128M:
172 size = SZ_128M;
173 break;
174 case SG_MEMCONF_CH2_SZ_256M:
175 size = SZ_256M;
176 break;
177 case SG_MEMCONF_CH2_SZ_512M:
178 size = SZ_512M;
179 break;
180 case SG_MEMCONF_CH2_SZ_1G:
181 size = SZ_1G;
182 break;
183 default:
Masahiro Yamada1566db92017-02-20 12:09:00 +0900184 pr_err("error: invalid value is set to MEMCONF ch2 size\n");
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900185 return -EINVAL;
186 }
187
188 if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2)
189 size *= 2;
190
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900191 dram_map[2].size = size;
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900192
Masahiro Yamadabb2ff9d2014-10-03 19:21:06 +0900193 return 0;
194}
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900195
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900196int dram_init(void)
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900197{
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900198 struct uniphier_dram_map dram_map[3] = {};
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900199 int ret, i;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900200
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900201 gd->ram_size = 0;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900202
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900203 ret = uniphier_memconf_decode(dram_map);
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900204 if (ret)
205 return ret;
206
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900207 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900208
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900209 if (!dram_map[i].size)
Masahiro Yamadad97c1cb2017-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 Yamada3dc80972017-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 Yamadad97c1cb2017-01-28 06:53:43 +0900219 break;
220
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900221 gd->ram_size += dram_map[i].size;
Masahiro Yamadaa90b1102016-03-29 20:18:45 +0900222 }
223
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900224 return 0;
225}
Masahiro Yamadaa90b1102016-03-29 20:18:45 +0900226
Simon Glass2f949c32017-03-31 08:40:32 -0600227int dram_init_banksize(void)
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900228{
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900229 struct uniphier_dram_map dram_map[3] = {};
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900230 int i;
231
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900232 uniphier_memconf_decode(dram_map);
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900233
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900234 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
Masahiro Yamadad97c1cb2017-01-28 06:53:43 +0900235 if (i >= ARRAY_SIZE(gd->bd->bi_dram))
236 break;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900237
Masahiro Yamada3dc80972017-02-05 10:52:12 +0900238 gd->bd->bi_dram[i].start = dram_map[i].base;
239 gd->bd->bi_dram[i].size = dram_map[i].size;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900240 }
Simon Glass2f949c32017-03-31 08:40:32 -0600241
242 return 0;
Masahiro Yamada460483c2016-06-17 19:24:29 +0900243}
244
245#ifdef CONFIG_OF_BOARD_SETUP
246/*
247 * The DRAM PHY requires 64 byte scratch area in each DRAM channel
248 * for its dynamic PHY training feature.
249 */
250int ft_board_setup(void *fdt, bd_t *bd)
251{
Masahiro Yamada460483c2016-06-17 19:24:29 +0900252 unsigned long rsv_addr;
253 const unsigned long rsv_size = 64;
Masahiro Yamada0197f632017-01-28 06:53:44 +0900254 int i, ret;
Masahiro Yamada460483c2016-06-17 19:24:29 +0900255
Masahiro Yamada31649052017-01-21 18:05:26 +0900256 if (uniphier_get_soc_id() != UNIPHIER_LD20_ID)
Masahiro Yamada460483c2016-06-17 19:24:29 +0900257 return 0;
258
Masahiro Yamada0197f632017-01-28 06:53:44 +0900259 for (i = 0; i < ARRAY_SIZE(gd->bd->bi_dram); i++) {
Masahiro Yamadab14071d2017-02-20 17:13:32 +0900260 if (!gd->bd->bi_dram[i].size)
261 continue;
262
Masahiro Yamada0197f632017-01-28 06:53:44 +0900263 rsv_addr = gd->bd->bi_dram[i].start + gd->bd->bi_dram[i].size;
Masahiro Yamada460483c2016-06-17 19:24:29 +0900264 rsv_addr -= rsv_size;
265
266 ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size);
267 if (ret)
268 return -ENOSPC;
269
Masahiro Yamada609cd532017-10-13 19:21:55 +0900270 pr_notice(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n",
271 rsv_addr, rsv_size);
Masahiro Yamada460483c2016-06-17 19:24:29 +0900272 }
273
274 return 0;
Masahiro Yamadab4782cd2015-09-11 20:17:49 +0900275}
Masahiro Yamada460483c2016-06-17 19:24:29 +0900276#endif