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