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