Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 2 | * Copyright (C) 2012-2015 Panasonic Corporation |
| 3 | * Copyright (C) 2015-2017 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Masahiro Yamada | 1d21e1b | 2017-06-22 16:42:04 +0900 | [diff] [blame] | 10 | #include <fdt_support.h> |
Masahiro Yamada | 7b3a032 | 2016-04-21 14:43:12 +0900 | [diff] [blame] | 11 | #include <fdtdec.h> |
Masahiro Yamada | 0f4ec05 | 2017-01-21 18:05:24 +0900 | [diff] [blame] | 12 | #include <linux/errno.h> |
Masahiro Yamada | dd74b94 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 13 | #include <linux/kernel.h> |
| 14 | #include <linux/printk.h> |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 15 | #include <linux/sizes.h> |
Masahiro Yamada | dd74b94 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 16 | #include <asm/global_data.h> |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 17 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 18 | #include "sg-regs.h" |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 19 | #include "soc-info.h" |
| 20 | |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 21 | DECLARE_GLOBAL_DATA_PTR; |
| 22 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 23 | struct uniphier_memif_data { |
| 24 | unsigned int soc_id; |
| 25 | unsigned long sparse_ch1_base; |
| 26 | int have_ch2; |
| 27 | }; |
| 28 | |
| 29 | static const struct uniphier_memif_data uniphier_memif_data[] = { |
| 30 | { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 31 | .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 | }; |
| 71 | UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data) |
| 72 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 73 | struct uniphier_dram_map { |
| 74 | unsigned long base; |
| 75 | unsigned long size; |
| 76 | }; |
| 77 | |
| 78 | static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map) |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 79 | { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 80 | const struct uniphier_memif_data *data; |
| 81 | unsigned long size; |
| 82 | u32 val; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 83 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 84 | data = uniphier_get_memif_data(); |
| 85 | if (!data) { |
| 86 | pr_err("unsupported SoC\n"); |
| 87 | return -EINVAL; |
| 88 | } |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 89 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 90 | val = readl(SG_MEMCONF); |
| 91 | |
| 92 | /* set up ch0 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 93 | dram_map[0].base = CONFIG_SYS_SDRAM_BASE; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 94 | |
| 95 | 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 Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 112 | pr_err("error: invalid value is set to MEMCONF ch0 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 113 | return -EINVAL; |
| 114 | } |
| 115 | |
| 116 | if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2) |
| 117 | size *= 2; |
| 118 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 119 | dram_map[0].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 120 | |
| 121 | /* set up ch1 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 122 | dram_map[1].base = dram_map[0].base + size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 123 | |
| 124 | if (val & SG_MEMCONF_SPARSEMEM) { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 125 | if (dram_map[1].base > data->sparse_ch1_base) { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 126 | pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n"); |
| 127 | pr_warn("Only ch0 is available\n"); |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 128 | dram_map[1].base = 0; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 132 | dram_map[1].base = data->sparse_ch1_base; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 133 | } |
| 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 Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 152 | pr_err("error: invalid value is set to MEMCONF ch1 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 153 | return -EINVAL; |
| 154 | } |
| 155 | |
| 156 | if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2) |
| 157 | size *= 2; |
| 158 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 159 | dram_map[1].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 160 | |
Masahiro Yamada | bed1624 | 2017-02-20 12:10:05 +0900 | [diff] [blame] | 161 | if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 162 | return 0; |
| 163 | |
| 164 | /* set up ch2 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 165 | dram_map[2].base = dram_map[1].base + size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 166 | |
| 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 Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 184 | pr_err("error: invalid value is set to MEMCONF ch2 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | } |
| 187 | |
| 188 | if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2) |
| 189 | size *= 2; |
| 190 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 191 | dram_map[2].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 192 | |
| 193 | return 0; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 194 | } |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 195 | |
| 196 | int dram_init(void) |
| 197 | { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 198 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 199 | int ret, i; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 200 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 201 | gd->ram_size = 0; |
| 202 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 203 | ret = uniphier_memconf_decode(dram_map); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 204 | if (ret) |
| 205 | return ret; |
| 206 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 207 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | be893a5 | 2018-01-06 22:59:24 +0900 | [diff] [blame] | 208 | unsigned long max_size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 209 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 210 | if (!dram_map[i].size) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 211 | break; |
| 212 | |
| 213 | /* |
| 214 | * U-Boot relocates itself to the tail of the memory region, |
| 215 | * but it does not expect sparse memory. We use the first |
| 216 | * contiguous chunk here. |
| 217 | */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 218 | if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size < |
| 219 | dram_map[i].base) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 220 | break; |
| 221 | |
Masahiro Yamada | be893a5 | 2018-01-06 22:59:24 +0900 | [diff] [blame] | 222 | /* |
| 223 | * Do not use memory that exceeds 32bit address range. U-Boot |
| 224 | * relocates itself to the end of the effectively available RAM. |
| 225 | * This could be a problem for DMA engines that do not support |
| 226 | * 64bit address (SDMA of SDHCI, UniPhier AV-ether, etc.) |
| 227 | */ |
| 228 | if (dram_map[i].base >= 1ULL << 32) |
| 229 | break; |
| 230 | |
| 231 | max_size = (1ULL << 32) - dram_map[i].base; |
| 232 | |
| 233 | if (dram_map[i].size > max_size) { |
| 234 | gd->ram_size += max_size; |
| 235 | break; |
| 236 | } |
| 237 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 238 | gd->ram_size += dram_map[i].size; |
Masahiro Yamada | ac2a103 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Masahiro Yamada | a322eb9 | 2018-01-06 22:59:26 +0900 | [diff] [blame] | 241 | /* |
| 242 | * LD20 uses the last 64 byte for each channel for dynamic |
| 243 | * DDR PHY training |
| 244 | */ |
| 245 | if (uniphier_get_soc_id() == UNIPHIER_LD20_ID) |
| 246 | gd->ram_size -= 64; |
| 247 | |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 248 | return 0; |
| 249 | } |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 250 | |
Simon Glass | 76b00ac | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 251 | int dram_init_banksize(void) |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 252 | { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 253 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 254 | int i; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 255 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 256 | uniphier_memconf_decode(dram_map); |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 257 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 258 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 259 | if (i >= ARRAY_SIZE(gd->bd->bi_dram)) |
| 260 | break; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 261 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 262 | gd->bd->bi_dram[i].start = dram_map[i].base; |
| 263 | gd->bd->bi_dram[i].size = dram_map[i].size; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 264 | } |
Simon Glass | 76b00ac | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 265 | |
| 266 | return 0; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 267 | } |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 268 | |
| 269 | #ifdef CONFIG_OF_BOARD_SETUP |
| 270 | /* |
| 271 | * The DRAM PHY requires 64 byte scratch area in each DRAM channel |
| 272 | * for its dynamic PHY training feature. |
| 273 | */ |
| 274 | int ft_board_setup(void *fdt, bd_t *bd) |
| 275 | { |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 276 | unsigned long rsv_addr; |
| 277 | const unsigned long rsv_size = 64; |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 278 | int i, ret; |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 279 | |
Masahiro Yamada | e27d6c7 | 2017-01-21 18:05:26 +0900 | [diff] [blame] | 280 | if (uniphier_get_soc_id() != UNIPHIER_LD20_ID) |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 281 | return 0; |
| 282 | |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 283 | for (i = 0; i < ARRAY_SIZE(gd->bd->bi_dram); i++) { |
Masahiro Yamada | 87c3308 | 2017-02-20 17:13:32 +0900 | [diff] [blame] | 284 | if (!gd->bd->bi_dram[i].size) |
| 285 | continue; |
| 286 | |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 287 | rsv_addr = gd->bd->bi_dram[i].start + gd->bd->bi_dram[i].size; |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 288 | rsv_addr -= rsv_size; |
| 289 | |
| 290 | ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size); |
| 291 | if (ret) |
| 292 | return -ENOSPC; |
| 293 | |
Masahiro Yamada | dd74b94 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 294 | pr_notice(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n", |
| 295 | rsv_addr, rsv_size); |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | #endif |