2 * Copyright (C) 2012-2015 Panasonic Corporation
3 * Copyright (C) 2015-2017 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
6 * SPDX-License-Identifier: GPL-2.0+
11 #include <linux/errno.h>
12 #include <linux/sizes.h>
17 #define pr_warn(fmt, args...) printf(fmt, ##args)
18 #define pr_err(fmt, args...) printf(fmt, ##args)
20 DECLARE_GLOBAL_DATA_PTR;
22 struct uniphier_memif_data {
24 unsigned long sparse_ch1_base;
28 static const struct uniphier_memif_data uniphier_memif_data[] = {
30 .soc_id = UNIPHIER_SLD3_ID,
31 .sparse_ch1_base = 0xc0000000,
33 * In fact, SLD3 has DRAM ch2, but the memory regions for ch1
34 * and ch2 overlap, and host cannot get access to them at the
35 * same time. Hide the ch2 from U-Boot.
39 .soc_id = UNIPHIER_LD4_ID,
40 .sparse_ch1_base = 0xc0000000,
43 .soc_id = UNIPHIER_PRO4_ID,
44 .sparse_ch1_base = 0xa0000000,
47 .soc_id = UNIPHIER_SLD8_ID,
48 .sparse_ch1_base = 0xc0000000,
51 .soc_id = UNIPHIER_PRO5_ID,
52 .sparse_ch1_base = 0xc0000000,
55 .soc_id = UNIPHIER_PXS2_ID,
56 .sparse_ch1_base = 0xc0000000,
60 .soc_id = UNIPHIER_LD6B_ID,
61 .sparse_ch1_base = 0xc0000000,
65 .soc_id = UNIPHIER_LD11_ID,
66 .sparse_ch1_base = 0xc0000000,
69 .soc_id = UNIPHIER_LD20_ID,
70 .sparse_ch1_base = 0xc0000000,
74 .soc_id = UNIPHIER_PXS3_ID,
75 .sparse_ch1_base = 0xc0000000,
79 UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data)
81 struct uniphier_dram_map {
86 static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map)
88 const struct uniphier_memif_data *data;
92 data = uniphier_get_memif_data();
94 pr_err("unsupported SoC\n");
98 val = readl(SG_MEMCONF);
101 dram_map[0].base = CONFIG_SYS_SDRAM_BASE;
103 switch (val & SG_MEMCONF_CH0_SZ_MASK) {
104 case SG_MEMCONF_CH0_SZ_64M:
107 case SG_MEMCONF_CH0_SZ_128M:
110 case SG_MEMCONF_CH0_SZ_256M:
113 case SG_MEMCONF_CH0_SZ_512M:
116 case SG_MEMCONF_CH0_SZ_1G:
120 pr_err("error: invalid value is set to MEMCONF ch0 size\n");
124 if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2)
127 dram_map[0].size = size;
130 dram_map[1].base = dram_map[0].base + size;
132 if (val & SG_MEMCONF_SPARSEMEM) {
133 if (dram_map[1].base > data->sparse_ch1_base) {
134 pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n");
135 pr_warn("Only ch0 is available\n");
136 dram_map[1].base = 0;
140 dram_map[1].base = data->sparse_ch1_base;
143 switch (val & SG_MEMCONF_CH1_SZ_MASK) {
144 case SG_MEMCONF_CH1_SZ_64M:
147 case SG_MEMCONF_CH1_SZ_128M:
150 case SG_MEMCONF_CH1_SZ_256M:
153 case SG_MEMCONF_CH1_SZ_512M:
156 case SG_MEMCONF_CH1_SZ_1G:
160 pr_err("error: invalid value is set to MEMCONF ch1 size\n");
164 if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2)
167 dram_map[1].size = size;
169 if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE)
173 dram_map[2].base = dram_map[1].base + size;
175 switch (val & SG_MEMCONF_CH2_SZ_MASK) {
176 case SG_MEMCONF_CH2_SZ_64M:
179 case SG_MEMCONF_CH2_SZ_128M:
182 case SG_MEMCONF_CH2_SZ_256M:
185 case SG_MEMCONF_CH2_SZ_512M:
188 case SG_MEMCONF_CH2_SZ_1G:
192 pr_err("error: invalid value is set to MEMCONF ch2 size\n");
196 if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2)
199 dram_map[2].size = size;
206 struct uniphier_dram_map dram_map[3] = {};
211 ret = uniphier_memconf_decode(dram_map);
215 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
217 if (!dram_map[i].size)
221 * U-Boot relocates itself to the tail of the memory region,
222 * but it does not expect sparse memory. We use the first
223 * contiguous chunk here.
225 if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size <
229 gd->ram_size += dram_map[i].size;
235 int dram_init_banksize(void)
237 struct uniphier_dram_map dram_map[3] = {};
240 uniphier_memconf_decode(dram_map);
242 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
243 if (i >= ARRAY_SIZE(gd->bd->bi_dram))
246 gd->bd->bi_dram[i].start = dram_map[i].base;
247 gd->bd->bi_dram[i].size = dram_map[i].size;
253 #ifdef CONFIG_OF_BOARD_SETUP
255 * The DRAM PHY requires 64 byte scratch area in each DRAM channel
256 * for its dynamic PHY training feature.
258 int ft_board_setup(void *fdt, bd_t *bd)
260 unsigned long rsv_addr;
261 const unsigned long rsv_size = 64;
264 if (uniphier_get_soc_id() != UNIPHIER_LD20_ID)
267 for (i = 0; i < ARRAY_SIZE(gd->bd->bi_dram); i++) {
268 if (!gd->bd->bi_dram[i].size)
271 rsv_addr = gd->bd->bi_dram[i].start + gd->bd->bi_dram[i].size;
272 rsv_addr -= rsv_size;
274 ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size);
278 printf(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n",