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