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>
11 #include <linux/kernel.h>
12 #include <linux/printk.h>
13 #include <linux/sizes.h>
14 #include <asm/global_data.h>
19 DECLARE_GLOBAL_DATA_PTR;
21 struct uniphier_memif_data {
23 unsigned long sparse_ch1_base;
27 static const struct uniphier_memif_data uniphier_memif_data[] = {
29 .soc_id = UNIPHIER_LD4_ID,
30 .sparse_ch1_base = 0xc0000000,
33 .soc_id = UNIPHIER_PRO4_ID,
34 .sparse_ch1_base = 0xa0000000,
37 .soc_id = UNIPHIER_SLD8_ID,
38 .sparse_ch1_base = 0xc0000000,
41 .soc_id = UNIPHIER_PRO5_ID,
42 .sparse_ch1_base = 0xc0000000,
45 .soc_id = UNIPHIER_PXS2_ID,
46 .sparse_ch1_base = 0xc0000000,
50 .soc_id = UNIPHIER_LD6B_ID,
51 .sparse_ch1_base = 0xc0000000,
55 .soc_id = UNIPHIER_LD11_ID,
56 .sparse_ch1_base = 0xc0000000,
59 .soc_id = UNIPHIER_LD20_ID,
60 .sparse_ch1_base = 0xc0000000,
64 .soc_id = UNIPHIER_PXS3_ID,
65 .sparse_ch1_base = 0xc0000000,
69 UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data)
71 struct uniphier_dram_map {
76 static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map)
78 const struct uniphier_memif_data *data;
82 data = uniphier_get_memif_data();
84 pr_err("unsupported SoC\n");
88 val = readl(sg_base + SG_MEMCONF);
91 dram_map[0].base = CONFIG_SYS_SDRAM_BASE;
93 switch (val & SG_MEMCONF_CH0_SZ_MASK) {
94 case SG_MEMCONF_CH0_SZ_64M:
97 case SG_MEMCONF_CH0_SZ_128M:
100 case SG_MEMCONF_CH0_SZ_256M:
103 case SG_MEMCONF_CH0_SZ_512M:
106 case SG_MEMCONF_CH0_SZ_1G:
110 pr_err("error: invalid value is set to MEMCONF ch0 size\n");
114 if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2)
117 dram_map[0].size = size;
120 dram_map[1].base = dram_map[0].base + size;
122 if (val & SG_MEMCONF_SPARSEMEM) {
123 if (dram_map[1].base > data->sparse_ch1_base) {
124 pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n");
125 pr_warn("Only ch0 is available\n");
126 dram_map[1].base = 0;
130 dram_map[1].base = data->sparse_ch1_base;
133 switch (val & SG_MEMCONF_CH1_SZ_MASK) {
134 case SG_MEMCONF_CH1_SZ_64M:
137 case SG_MEMCONF_CH1_SZ_128M:
140 case SG_MEMCONF_CH1_SZ_256M:
143 case SG_MEMCONF_CH1_SZ_512M:
146 case SG_MEMCONF_CH1_SZ_1G:
150 pr_err("error: invalid value is set to MEMCONF ch1 size\n");
154 if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2)
157 dram_map[1].size = size;
159 if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE)
163 dram_map[2].base = dram_map[1].base + size;
165 switch (val & SG_MEMCONF_CH2_SZ_MASK) {
166 case SG_MEMCONF_CH2_SZ_64M:
169 case SG_MEMCONF_CH2_SZ_128M:
172 case SG_MEMCONF_CH2_SZ_256M:
175 case SG_MEMCONF_CH2_SZ_512M:
178 case SG_MEMCONF_CH2_SZ_1G:
182 pr_err("error: invalid value is set to MEMCONF ch2 size\n");
186 if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2)
189 dram_map[2].size = size;
196 struct uniphier_dram_map dram_map[3] = {};
201 ret = uniphier_memconf_decode(dram_map);
205 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
206 unsigned long max_size;
208 if (!dram_map[i].size)
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.
216 if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size <
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.)
226 if (dram_map[i].base >= 1ULL << 32)
229 max_size = (1ULL << 32) - dram_map[i].base;
231 if (dram_map[i].size > max_size) {
232 gd->ram_size += max_size;
236 gd->ram_size += dram_map[i].size;
240 * LD20 uses the last 64 byte for each channel for dynamic
243 if (uniphier_get_soc_id() == UNIPHIER_LD20_ID)
249 int dram_init_banksize(void)
251 struct uniphier_dram_map dram_map[3] = {};
254 uniphier_memconf_decode(dram_map);
256 for (i = 0; i < ARRAY_SIZE(dram_map); i++) {
257 if (i >= ARRAY_SIZE(gd->bd->bi_dram))
260 gd->bd->bi_dram[i].start = dram_map[i].base;
261 gd->bd->bi_dram[i].size = dram_map[i].size;