Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 2 | /* |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 3 | * Copyright (C) 2012-2015 Panasonic Corporation |
| 4 | * Copyright (C) 2015-2017 Socionext Inc. |
| 5 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Masahiro Yamada | 1d21e1b | 2017-06-22 16:42:04 +0900 | [diff] [blame] | 9 | #include <fdt_support.h> |
Masahiro Yamada | 7b3a032 | 2016-04-21 14:43:12 +0900 | [diff] [blame] | 10 | #include <fdtdec.h> |
Masahiro Yamada | 0f4ec05 | 2017-01-21 18:05:24 +0900 | [diff] [blame] | 11 | #include <linux/errno.h> |
Masahiro Yamada | dd74b94 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/printk.h> |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 14 | #include <linux/sizes.h> |
Masahiro Yamada | dd74b94 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 15 | #include <asm/global_data.h> |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 16 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 17 | #include "sg-regs.h" |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 18 | #include "soc-info.h" |
| 19 | |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 22 | struct uniphier_memif_data { |
| 23 | unsigned int soc_id; |
| 24 | unsigned long sparse_ch1_base; |
| 25 | int have_ch2; |
| 26 | }; |
| 27 | |
| 28 | static const struct uniphier_memif_data uniphier_memif_data[] = { |
| 29 | { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 30 | .soc_id = UNIPHIER_LD4_ID, |
| 31 | .sparse_ch1_base = 0xc0000000, |
| 32 | }, |
| 33 | { |
| 34 | .soc_id = UNIPHIER_PRO4_ID, |
| 35 | .sparse_ch1_base = 0xa0000000, |
| 36 | }, |
| 37 | { |
| 38 | .soc_id = UNIPHIER_SLD8_ID, |
| 39 | .sparse_ch1_base = 0xc0000000, |
| 40 | }, |
| 41 | { |
| 42 | .soc_id = UNIPHIER_PRO5_ID, |
| 43 | .sparse_ch1_base = 0xc0000000, |
| 44 | }, |
| 45 | { |
| 46 | .soc_id = UNIPHIER_PXS2_ID, |
| 47 | .sparse_ch1_base = 0xc0000000, |
| 48 | .have_ch2 = 1, |
| 49 | }, |
| 50 | { |
| 51 | .soc_id = UNIPHIER_LD6B_ID, |
| 52 | .sparse_ch1_base = 0xc0000000, |
| 53 | .have_ch2 = 1, |
| 54 | }, |
| 55 | { |
| 56 | .soc_id = UNIPHIER_LD11_ID, |
| 57 | .sparse_ch1_base = 0xc0000000, |
| 58 | }, |
| 59 | { |
| 60 | .soc_id = UNIPHIER_LD20_ID, |
| 61 | .sparse_ch1_base = 0xc0000000, |
| 62 | .have_ch2 = 1, |
| 63 | }, |
| 64 | { |
| 65 | .soc_id = UNIPHIER_PXS3_ID, |
| 66 | .sparse_ch1_base = 0xc0000000, |
| 67 | .have_ch2 = 1, |
| 68 | }, |
| 69 | }; |
| 70 | UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data) |
| 71 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 72 | struct uniphier_dram_map { |
| 73 | unsigned long base; |
| 74 | unsigned long size; |
| 75 | }; |
| 76 | |
| 77 | static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map) |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 78 | { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 79 | const struct uniphier_memif_data *data; |
| 80 | unsigned long size; |
| 81 | u32 val; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 82 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 83 | data = uniphier_get_memif_data(); |
| 84 | if (!data) { |
| 85 | pr_err("unsupported SoC\n"); |
| 86 | return -EINVAL; |
| 87 | } |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 88 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 89 | val = readl(SG_MEMCONF); |
| 90 | |
| 91 | /* set up ch0 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 92 | dram_map[0].base = CONFIG_SYS_SDRAM_BASE; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 93 | |
| 94 | switch (val & SG_MEMCONF_CH0_SZ_MASK) { |
| 95 | case SG_MEMCONF_CH0_SZ_64M: |
| 96 | size = SZ_64M; |
| 97 | break; |
| 98 | case SG_MEMCONF_CH0_SZ_128M: |
| 99 | size = SZ_128M; |
| 100 | break; |
| 101 | case SG_MEMCONF_CH0_SZ_256M: |
| 102 | size = SZ_256M; |
| 103 | break; |
| 104 | case SG_MEMCONF_CH0_SZ_512M: |
| 105 | size = SZ_512M; |
| 106 | break; |
| 107 | case SG_MEMCONF_CH0_SZ_1G: |
| 108 | size = SZ_1G; |
| 109 | break; |
| 110 | default: |
Masahiro Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 111 | pr_err("error: invalid value is set to MEMCONF ch0 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 112 | return -EINVAL; |
| 113 | } |
| 114 | |
| 115 | if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2) |
| 116 | size *= 2; |
| 117 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 118 | dram_map[0].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 119 | |
| 120 | /* set up ch1 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 121 | dram_map[1].base = dram_map[0].base + size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 122 | |
| 123 | if (val & SG_MEMCONF_SPARSEMEM) { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 124 | if (dram_map[1].base > data->sparse_ch1_base) { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 125 | pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n"); |
| 126 | pr_warn("Only ch0 is available\n"); |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 127 | dram_map[1].base = 0; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 131 | dram_map[1].base = data->sparse_ch1_base; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | switch (val & SG_MEMCONF_CH1_SZ_MASK) { |
| 135 | case SG_MEMCONF_CH1_SZ_64M: |
| 136 | size = SZ_64M; |
| 137 | break; |
| 138 | case SG_MEMCONF_CH1_SZ_128M: |
| 139 | size = SZ_128M; |
| 140 | break; |
| 141 | case SG_MEMCONF_CH1_SZ_256M: |
| 142 | size = SZ_256M; |
| 143 | break; |
| 144 | case SG_MEMCONF_CH1_SZ_512M: |
| 145 | size = SZ_512M; |
| 146 | break; |
| 147 | case SG_MEMCONF_CH1_SZ_1G: |
| 148 | size = SZ_1G; |
| 149 | break; |
| 150 | default: |
Masahiro Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 151 | pr_err("error: invalid value is set to MEMCONF ch1 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 152 | return -EINVAL; |
| 153 | } |
| 154 | |
| 155 | if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2) |
| 156 | size *= 2; |
| 157 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 158 | dram_map[1].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 159 | |
Masahiro Yamada | bed1624 | 2017-02-20 12:10:05 +0900 | [diff] [blame] | 160 | if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 161 | return 0; |
| 162 | |
| 163 | /* set up ch2 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 164 | dram_map[2].base = dram_map[1].base + size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 165 | |
| 166 | switch (val & SG_MEMCONF_CH2_SZ_MASK) { |
| 167 | case SG_MEMCONF_CH2_SZ_64M: |
| 168 | size = SZ_64M; |
| 169 | break; |
| 170 | case SG_MEMCONF_CH2_SZ_128M: |
| 171 | size = SZ_128M; |
| 172 | break; |
| 173 | case SG_MEMCONF_CH2_SZ_256M: |
| 174 | size = SZ_256M; |
| 175 | break; |
| 176 | case SG_MEMCONF_CH2_SZ_512M: |
| 177 | size = SZ_512M; |
| 178 | break; |
| 179 | case SG_MEMCONF_CH2_SZ_1G: |
| 180 | size = SZ_1G; |
| 181 | break; |
| 182 | default: |
Masahiro Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 183 | pr_err("error: invalid value is set to MEMCONF ch2 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 184 | return -EINVAL; |
| 185 | } |
| 186 | |
| 187 | if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2) |
| 188 | size *= 2; |
| 189 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 190 | dram_map[2].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 191 | |
| 192 | return 0; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 193 | } |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 194 | |
| 195 | int dram_init(void) |
| 196 | { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 197 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 198 | int ret, i; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 199 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 200 | gd->ram_size = 0; |
| 201 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 202 | ret = uniphier_memconf_decode(dram_map); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 203 | if (ret) |
| 204 | return ret; |
| 205 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 206 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | be893a5 | 2018-01-06 22:59:24 +0900 | [diff] [blame] | 207 | unsigned long max_size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 208 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 209 | if (!dram_map[i].size) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 210 | 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 Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 217 | if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size < |
| 218 | dram_map[i].base) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 219 | break; |
| 220 | |
Masahiro Yamada | be893a5 | 2018-01-06 22:59:24 +0900 | [diff] [blame] | 221 | /* |
| 222 | * Do not use memory that exceeds 32bit address range. U-Boot |
| 223 | * relocates itself to the end of the effectively available RAM. |
| 224 | * This could be a problem for DMA engines that do not support |
| 225 | * 64bit address (SDMA of SDHCI, UniPhier AV-ether, etc.) |
| 226 | */ |
| 227 | if (dram_map[i].base >= 1ULL << 32) |
| 228 | break; |
| 229 | |
| 230 | max_size = (1ULL << 32) - dram_map[i].base; |
| 231 | |
| 232 | if (dram_map[i].size > max_size) { |
| 233 | gd->ram_size += max_size; |
| 234 | break; |
| 235 | } |
| 236 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 237 | gd->ram_size += dram_map[i].size; |
Masahiro Yamada | ac2a103 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 238 | } |
| 239 | |
Masahiro Yamada | a322eb9 | 2018-01-06 22:59:26 +0900 | [diff] [blame] | 240 | /* |
| 241 | * LD20 uses the last 64 byte for each channel for dynamic |
| 242 | * DDR PHY training |
| 243 | */ |
| 244 | if (uniphier_get_soc_id() == UNIPHIER_LD20_ID) |
| 245 | gd->ram_size -= 64; |
| 246 | |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 247 | return 0; |
| 248 | } |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 249 | |
Simon Glass | 76b00ac | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 250 | int dram_init_banksize(void) |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 251 | { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 252 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 253 | int i; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 254 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 255 | uniphier_memconf_decode(dram_map); |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 256 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 257 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 258 | if (i >= ARRAY_SIZE(gd->bd->bi_dram)) |
| 259 | break; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 260 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 261 | gd->bd->bi_dram[i].start = dram_map[i].base; |
| 262 | gd->bd->bi_dram[i].size = dram_map[i].size; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 263 | } |
Simon Glass | 76b00ac | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 264 | |
| 265 | return 0; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 266 | } |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 267 | |
| 268 | #ifdef CONFIG_OF_BOARD_SETUP |
| 269 | /* |
| 270 | * The DRAM PHY requires 64 byte scratch area in each DRAM channel |
| 271 | * for its dynamic PHY training feature. |
| 272 | */ |
| 273 | int ft_board_setup(void *fdt, bd_t *bd) |
| 274 | { |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 275 | unsigned long rsv_addr; |
| 276 | const unsigned long rsv_size = 64; |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 277 | int i, ret; |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 278 | |
Masahiro Yamada | e27d6c7 | 2017-01-21 18:05:26 +0900 | [diff] [blame] | 279 | if (uniphier_get_soc_id() != UNIPHIER_LD20_ID) |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 280 | return 0; |
| 281 | |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 282 | for (i = 0; i < ARRAY_SIZE(gd->bd->bi_dram); i++) { |
Masahiro Yamada | 87c3308 | 2017-02-20 17:13:32 +0900 | [diff] [blame] | 283 | if (!gd->bd->bi_dram[i].size) |
| 284 | continue; |
| 285 | |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 286 | 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] | 287 | rsv_addr -= rsv_size; |
| 288 | |
| 289 | ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size); |
| 290 | if (ret) |
| 291 | return -ENOSPC; |
| 292 | |
Masahiro Yamada | dd74b94 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 293 | pr_notice(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n", |
| 294 | rsv_addr, rsv_size); |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | #endif |