1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012-2015 Panasonic Corporation
4 * Copyright (C) 2015-2017 Socionext Inc.
5 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
9 #include <linux/errno.h>
10 #include <linux/kernel.h>
11 #include <linux/printk.h>
12 #include <linux/sizes.h>
13 #include <asm/global_data.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 struct uniphier_memif_data {
22 unsigned long sparse_ch1_base;
26 static const struct uniphier_memif_data uniphier_memif_data[] = {
28 .soc_id = UNIPHIER_LD4_ID,
29 .sparse_ch1_base = 0xc0000000,
32 .soc_id = UNIPHIER_PRO4_ID,
33 .sparse_ch1_base = 0xa0000000,
36 .soc_id = UNIPHIER_SLD8_ID,
37 .sparse_ch1_base = 0xc0000000,
40 .soc_id = UNIPHIER_PRO5_ID,
41 .sparse_ch1_base = 0xc0000000,
44 .soc_id = UNIPHIER_PXS2_ID,
45 .sparse_ch1_base = 0xc0000000,
49 .soc_id = UNIPHIER_LD6B_ID,
50 .sparse_ch1_base = 0xc0000000,
54 .soc_id = UNIPHIER_LD11_ID,
55 .sparse_ch1_base = 0xc0000000,
58 .soc_id = UNIPHIER_LD20_ID,
59 .sparse_ch1_base = 0xc0000000,
63 .soc_id = UNIPHIER_PXS3_ID,
64 .sparse_ch1_base = 0xc0000000,
68 UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data)
70 struct uniphier_dram_map {
75 static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map)
77 const struct uniphier_memif_data *data;
81 data = uniphier_get_memif_data();
83 pr_err("unsupported SoC\n");
87 val = readl(SG_MEMCONF);
90 dram_map[0].base = CONFIG_SYS_SDRAM_BASE;
92 switch (val & SG_MEMCONF_CH0_SZ_MASK) {
93 case SG_MEMCONF_CH0_SZ_64M:
96 case SG_MEMCONF_CH0_SZ_128M:
99 case SG_MEMCONF_CH0_SZ_256M:
102 case SG_MEMCONF_CH0_SZ_512M:
105 case SG_MEMCONF_CH0_SZ_1G:
109 pr_err("error: invalid value is set to MEMCONF ch0 size\n");
113 if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2)
116 dram_map[0].size = size;
119 dram_map[1].base = dram_map[0].base + size;
121 if (val & SG_MEMCONF_SPARSEMEM) {
122 if (dram_map[1].base > data->sparse_ch1_base) {
123 pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n");
124 pr_warn("Only ch0 is available\n");
125 dram_map[1].base = 0;
129 dram_map[1].base = data->sparse_ch1_base;
132 switch (val & SG_MEMCONF_CH1_SZ_MASK) {
133 case SG_MEMCONF_CH1_SZ_64M:
136 case SG_MEMCONF_CH1_SZ_128M:
139 case SG_MEMCONF_CH1_SZ_256M:
142 case SG_MEMCONF_CH1_SZ_512M:
145 case SG_MEMCONF_CH1_SZ_1G:
149 pr_err("error: invalid value is set to MEMCONF ch1 size\n");
153 if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2)
156 dram_map[1].size = size;
158 if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE)
162 dram_map[2].base = dram_map[1].base + size;
164 switch (val & SG_MEMCONF_CH2_SZ_MASK) {
165 case SG_MEMCONF_CH2_SZ_64M:
168 case SG_MEMCONF_CH2_SZ_128M:
171 case SG_MEMCONF_CH2_SZ_256M:
174 case SG_MEMCONF_CH2_SZ_512M:
177 case SG_MEMCONF_CH2_SZ_1G:
181 pr_err("error: invalid value is set to MEMCONF ch2 size\n");
185 if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2)
188 dram_map[2].size = size;
195 struct uniphier_dram_map dram_map[3] = {};
200 ret = uniphier_memconf_decode(dram_map);
204 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
205 unsigned long max_size;
207 if (!dram_map[i].size)
211 * U-Boot relocates itself to the tail of the memory region,
212 * but it does not expect sparse memory. We use the first
213 * contiguous chunk here.
215 if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size <
220 * Do not use memory that exceeds 32bit address range. U-Boot
221 * relocates itself to the end of the effectively available RAM.
222 * This could be a problem for DMA engines that do not support
223 * 64bit address (SDMA of SDHCI, UniPhier AV-ether, etc.)
225 if (dram_map[i].base >= 1ULL << 32)
228 max_size = (1ULL << 32) - dram_map[i].base;
230 if (dram_map[i].size > max_size) {
231 gd->ram_size += max_size;
235 gd->ram_size += dram_map[i].size;
239 * LD20 uses the last 64 byte for each channel for dynamic
242 if (uniphier_get_soc_id() == UNIPHIER_LD20_ID)
248 int dram_init_banksize(void)
250 struct uniphier_dram_map dram_map[3] = {};
253 uniphier_memconf_decode(dram_map);
255 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
256 if (i >= ARRAY_SIZE(gd->bd->bi_dram))
259 gd->bd->bi_dram[i].start = dram_map[i].base;
260 gd->bd->bi_dram[i].size = dram_map[i].size;