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 | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 13 | #include <linux/sizes.h> |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 14 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 15 | #include "sg-regs.h" |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 16 | #include "soc-info.h" |
| 17 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 18 | #define pr_warn(fmt, args...) printf(fmt, ##args) |
| 19 | #define pr_err(fmt, args...) printf(fmt, ##args) |
| 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 | { |
| 31 | .soc_id = UNIPHIER_SLD3_ID, |
| 32 | .sparse_ch1_base = 0xc0000000, |
| 33 | /* |
| 34 | * In fact, SLD3 has DRAM ch2, but the memory regions for ch1 |
| 35 | * and ch2 overlap, and host cannot get access to them at the |
| 36 | * same time. Hide the ch2 from U-Boot. |
| 37 | */ |
| 38 | }, |
| 39 | { |
| 40 | .soc_id = UNIPHIER_LD4_ID, |
| 41 | .sparse_ch1_base = 0xc0000000, |
| 42 | }, |
| 43 | { |
| 44 | .soc_id = UNIPHIER_PRO4_ID, |
| 45 | .sparse_ch1_base = 0xa0000000, |
| 46 | }, |
| 47 | { |
| 48 | .soc_id = UNIPHIER_SLD8_ID, |
| 49 | .sparse_ch1_base = 0xc0000000, |
| 50 | }, |
| 51 | { |
| 52 | .soc_id = UNIPHIER_PRO5_ID, |
| 53 | .sparse_ch1_base = 0xc0000000, |
| 54 | }, |
| 55 | { |
| 56 | .soc_id = UNIPHIER_PXS2_ID, |
| 57 | .sparse_ch1_base = 0xc0000000, |
| 58 | .have_ch2 = 1, |
| 59 | }, |
| 60 | { |
| 61 | .soc_id = UNIPHIER_LD6B_ID, |
| 62 | .sparse_ch1_base = 0xc0000000, |
| 63 | .have_ch2 = 1, |
| 64 | }, |
| 65 | { |
| 66 | .soc_id = UNIPHIER_LD11_ID, |
| 67 | .sparse_ch1_base = 0xc0000000, |
| 68 | }, |
| 69 | { |
| 70 | .soc_id = UNIPHIER_LD20_ID, |
| 71 | .sparse_ch1_base = 0xc0000000, |
| 72 | .have_ch2 = 1, |
| 73 | }, |
| 74 | { |
| 75 | .soc_id = UNIPHIER_PXS3_ID, |
| 76 | .sparse_ch1_base = 0xc0000000, |
| 77 | .have_ch2 = 1, |
| 78 | }, |
| 79 | }; |
| 80 | UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data) |
| 81 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 82 | struct uniphier_dram_map { |
| 83 | unsigned long base; |
| 84 | unsigned long size; |
| 85 | }; |
| 86 | |
| 87 | static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map) |
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 | const struct uniphier_memif_data *data; |
| 90 | unsigned long size; |
| 91 | u32 val; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 92 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 93 | data = uniphier_get_memif_data(); |
| 94 | if (!data) { |
| 95 | pr_err("unsupported SoC\n"); |
| 96 | return -EINVAL; |
| 97 | } |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 98 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 99 | val = readl(SG_MEMCONF); |
| 100 | |
| 101 | /* set up ch0 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 102 | dram_map[0].base = CONFIG_SYS_SDRAM_BASE; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 103 | |
| 104 | switch (val & SG_MEMCONF_CH0_SZ_MASK) { |
| 105 | case SG_MEMCONF_CH0_SZ_64M: |
| 106 | size = SZ_64M; |
| 107 | break; |
| 108 | case SG_MEMCONF_CH0_SZ_128M: |
| 109 | size = SZ_128M; |
| 110 | break; |
| 111 | case SG_MEMCONF_CH0_SZ_256M: |
| 112 | size = SZ_256M; |
| 113 | break; |
| 114 | case SG_MEMCONF_CH0_SZ_512M: |
| 115 | size = SZ_512M; |
| 116 | break; |
| 117 | case SG_MEMCONF_CH0_SZ_1G: |
| 118 | size = SZ_1G; |
| 119 | break; |
| 120 | default: |
Masahiro Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 121 | pr_err("error: invalid value is set to MEMCONF ch0 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 122 | return -EINVAL; |
| 123 | } |
| 124 | |
| 125 | if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2) |
| 126 | size *= 2; |
| 127 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 128 | dram_map[0].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 129 | |
| 130 | /* set up ch1 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 131 | dram_map[1].base = dram_map[0].base + size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 132 | |
| 133 | if (val & SG_MEMCONF_SPARSEMEM) { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 134 | if (dram_map[1].base > data->sparse_ch1_base) { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 135 | pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n"); |
| 136 | pr_warn("Only ch0 is available\n"); |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 137 | dram_map[1].base = 0; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 141 | dram_map[1].base = data->sparse_ch1_base; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | switch (val & SG_MEMCONF_CH1_SZ_MASK) { |
| 145 | case SG_MEMCONF_CH1_SZ_64M: |
| 146 | size = SZ_64M; |
| 147 | break; |
| 148 | case SG_MEMCONF_CH1_SZ_128M: |
| 149 | size = SZ_128M; |
| 150 | break; |
| 151 | case SG_MEMCONF_CH1_SZ_256M: |
| 152 | size = SZ_256M; |
| 153 | break; |
| 154 | case SG_MEMCONF_CH1_SZ_512M: |
| 155 | size = SZ_512M; |
| 156 | break; |
| 157 | case SG_MEMCONF_CH1_SZ_1G: |
| 158 | size = SZ_1G; |
| 159 | break; |
| 160 | default: |
Masahiro Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 161 | pr_err("error: invalid value is set to MEMCONF ch1 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 162 | return -EINVAL; |
| 163 | } |
| 164 | |
| 165 | if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2) |
| 166 | size *= 2; |
| 167 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 168 | dram_map[1].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 169 | |
Masahiro Yamada | bed1624 | 2017-02-20 12:10:05 +0900 | [diff] [blame] | 170 | if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 171 | return 0; |
| 172 | |
| 173 | /* set up ch2 */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 174 | dram_map[2].base = dram_map[1].base + size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 175 | |
| 176 | switch (val & SG_MEMCONF_CH2_SZ_MASK) { |
| 177 | case SG_MEMCONF_CH2_SZ_64M: |
| 178 | size = SZ_64M; |
| 179 | break; |
| 180 | case SG_MEMCONF_CH2_SZ_128M: |
| 181 | size = SZ_128M; |
| 182 | break; |
| 183 | case SG_MEMCONF_CH2_SZ_256M: |
| 184 | size = SZ_256M; |
| 185 | break; |
| 186 | case SG_MEMCONF_CH2_SZ_512M: |
| 187 | size = SZ_512M; |
| 188 | break; |
| 189 | case SG_MEMCONF_CH2_SZ_1G: |
| 190 | size = SZ_1G; |
| 191 | break; |
| 192 | default: |
Masahiro Yamada | 0f5bf09 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 193 | pr_err("error: invalid value is set to MEMCONF ch2 size\n"); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2) |
| 198 | size *= 2; |
| 199 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 200 | dram_map[2].size = size; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 201 | |
| 202 | return 0; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 203 | } |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 204 | |
| 205 | int dram_init(void) |
| 206 | { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 207 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 208 | int ret, i; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 209 | |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 210 | gd->ram_size = 0; |
| 211 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 212 | ret = uniphier_memconf_decode(dram_map); |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 213 | if (ret) |
| 214 | return ret; |
| 215 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 216 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 217 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 218 | if (!dram_map[i].size) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 219 | break; |
| 220 | |
| 221 | /* |
| 222 | * U-Boot relocates itself to the tail of the memory region, |
| 223 | * but it does not expect sparse memory. We use the first |
| 224 | * contiguous chunk here. |
| 225 | */ |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 226 | if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size < |
| 227 | dram_map[i].base) |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 228 | break; |
| 229 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 230 | gd->ram_size += dram_map[i].size; |
Masahiro Yamada | ac2a103 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 231 | } |
| 232 | |
Masahiro Yamada | 5894ca0 | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 233 | return 0; |
| 234 | } |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 235 | |
Simon Glass | 76b00ac | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 236 | int dram_init_banksize(void) |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 237 | { |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 238 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 239 | int i; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 240 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 241 | uniphier_memconf_decode(dram_map); |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 242 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 243 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | 3e9952b | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 244 | if (i >= ARRAY_SIZE(gd->bd->bi_dram)) |
| 245 | break; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 246 | |
Masahiro Yamada | 04cd4e7 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 247 | gd->bd->bi_dram[i].start = dram_map[i].base; |
| 248 | gd->bd->bi_dram[i].size = dram_map[i].size; |
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 | |
| 251 | return 0; |
Masahiro Yamada | cf88aff | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 252 | } |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 253 | |
| 254 | #ifdef CONFIG_OF_BOARD_SETUP |
| 255 | /* |
| 256 | * The DRAM PHY requires 64 byte scratch area in each DRAM channel |
| 257 | * for its dynamic PHY training feature. |
| 258 | */ |
| 259 | int ft_board_setup(void *fdt, bd_t *bd) |
| 260 | { |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 261 | unsigned long rsv_addr; |
| 262 | const unsigned long rsv_size = 64; |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 263 | int i, ret; |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 264 | |
Masahiro Yamada | e27d6c7 | 2017-01-21 18:05:26 +0900 | [diff] [blame] | 265 | if (uniphier_get_soc_id() != UNIPHIER_LD20_ID) |
Masahiro Yamada | 51ea5a0 | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 266 | return 0; |
| 267 | |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 268 | for (i = 0; i < ARRAY_SIZE(gd->bd->bi_dram); i++) { |
Masahiro Yamada | 87c3308 | 2017-02-20 17:13:32 +0900 | [diff] [blame] | 269 | if (!gd->bd->bi_dram[i].size) |
| 270 | continue; |
| 271 | |
Masahiro Yamada | c995f3a | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 272 | 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] | 273 | rsv_addr -= rsv_size; |
| 274 | |
| 275 | ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size); |
| 276 | if (ret) |
| 277 | return -ENOSPC; |
| 278 | |
| 279 | printf(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n", |
| 280 | rsv_addr, rsv_size); |
| 281 | } |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | #endif |