Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 2 | /* |
Masahiro Yamada | d97c1cb | 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 | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Masahiro Yamada | e4e789d | 2017-01-21 18:05:24 +0900 | [diff] [blame] | 9 | #include <linux/errno.h> |
Masahiro Yamada | 2757ff0 | 2019-06-29 02:38:04 +0900 | [diff] [blame] | 10 | #include <linux/io.h> |
Masahiro Yamada | 609cd53 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 11 | #include <linux/kernel.h> |
| 12 | #include <linux/printk.h> |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 13 | #include <linux/sizes.h> |
Masahiro Yamada | 609cd53 | 2017-10-13 19:21:55 +0900 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 15 | |
Masahiro Yamada | f0f6a80 | 2019-07-10 20:07:45 +0900 | [diff] [blame] | 16 | #include "init.h" |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 17 | #include "sg-regs.h" |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 18 | #include "soc-info.h" |
| 19 | |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Masahiro Yamada | 3dc8097 | 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 | 12938bf | 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 | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 29 | { |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 30 | unsigned long size; |
| 31 | u32 val; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 32 | |
Masahiro Yamada | 76b3124 | 2019-07-10 20:07:40 +0900 | [diff] [blame] | 33 | val = readl(sg_base + SG_MEMCONF); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 34 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 35 | /* set up ch0 */ |
Masahiro Yamada | 7353ce3 | 2019-07-10 20:07:46 +0900 | [diff] [blame] | 36 | dram_map[0].base = 0x80000000; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 37 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 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 | 1566db9 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 55 | pr_err("error: invalid value is set to MEMCONF ch0 size\n"); |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 56 | return -EINVAL; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 57 | } |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 58 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 59 | if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2) |
| 60 | size *= 2; |
| 61 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 62 | dram_map[0].size = size; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 63 | |
| 64 | /* set up ch1 */ |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 65 | dram_map[1].base = dram_map[0].base + size; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 66 | |
| 67 | if (val & SG_MEMCONF_SPARSEMEM) { |
Masahiro Yamada | 12938bf | 2019-07-10 20:07:43 +0900 | [diff] [blame] | 68 | if (dram_map[1].base > sparse_ch1_base) { |
Masahiro Yamada | d97c1cb | 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 | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 71 | dram_map[1].base = 0; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
Masahiro Yamada | 12938bf | 2019-07-10 20:07:43 +0900 | [diff] [blame] | 75 | dram_map[1].base = sparse_ch1_base; |
Masahiro Yamada | d97c1cb | 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 | 1566db9 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 95 | pr_err("error: invalid value is set to MEMCONF ch1 size\n"); |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 96 | return -EINVAL; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 97 | } |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 98 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 99 | if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2) |
| 100 | size *= 2; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 101 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 102 | dram_map[1].size = size; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 103 | |
Masahiro Yamada | 12938bf | 2019-07-10 20:07:43 +0900 | [diff] [blame] | 104 | if (!have_ch2 || val & SG_MEMCONF_CH2_DISABLE) |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 105 | return 0; |
| 106 | |
| 107 | /* set up ch2 */ |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 108 | dram_map[2].base = dram_map[1].base + size; |
Masahiro Yamada | d97c1cb | 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 | 1566db9 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 127 | pr_err("error: invalid value is set to MEMCONF ch2 size\n"); |
Masahiro Yamada | d97c1cb | 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 | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 134 | dram_map[2].size = size; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 135 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 136 | return 0; |
| 137 | } |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 138 | |
Masahiro Yamada | 12938bf | 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 | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 213 | int dram_init(void) |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 214 | { |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 215 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | efcae8e | 2019-07-10 20:07:44 +0900 | [diff] [blame] | 216 | bool valid_bank_found = false; |
| 217 | unsigned long prev_top; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 218 | int ret, i; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 219 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 220 | gd->ram_size = 0; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 221 | |
Masahiro Yamada | 12938bf | 2019-07-10 20:07:43 +0900 | [diff] [blame] | 222 | ret = uniphier_dram_map_get(dram_map); |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 223 | if (ret) |
| 224 | return ret; |
| 225 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 226 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | 86f90c2 | 2018-01-06 22:59:24 +0900 | [diff] [blame] | 227 | unsigned long max_size; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 228 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 229 | if (!dram_map[i].size) |
Masahiro Yamada | efcae8e | 2019-07-10 20:07:44 +0900 | [diff] [blame] | 230 | continue; |
Masahiro Yamada | d97c1cb | 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 | efcae8e | 2019-07-10 20:07:44 +0900 | [diff] [blame] | 237 | if (valid_bank_found && prev_top < dram_map[i].base) |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 238 | break; |
| 239 | |
Masahiro Yamada | 86f90c2 | 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 | |
Masahiro Yamada | 23929fb | 2020-01-30 22:20:37 +0900 | [diff] [blame] | 251 | gd->ram_size = min(dram_map[i].size, max_size); |
Masahiro Yamada | efcae8e | 2019-07-10 20:07:44 +0900 | [diff] [blame] | 252 | |
Masahiro Yamada | 7353ce3 | 2019-07-10 20:07:46 +0900 | [diff] [blame] | 253 | if (!valid_bank_found) |
| 254 | gd->ram_base = dram_map[i].base; |
| 255 | |
Masahiro Yamada | efcae8e | 2019-07-10 20:07:44 +0900 | [diff] [blame] | 256 | prev_top = dram_map[i].base + dram_map[i].size; |
| 257 | valid_bank_found = true; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 258 | } |
| 259 | |
Masahiro Yamada | 5737e47 | 2018-01-06 22:59:26 +0900 | [diff] [blame] | 260 | /* |
| 261 | * LD20 uses the last 64 byte for each channel for dynamic |
| 262 | * DDR PHY training |
| 263 | */ |
| 264 | if (uniphier_get_soc_id() == UNIPHIER_LD20_ID) |
| 265 | gd->ram_size -= 64; |
| 266 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 267 | return 0; |
| 268 | } |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 269 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 270 | int dram_init_banksize(void) |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 271 | { |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 272 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | f0f6a80 | 2019-07-10 20:07:45 +0900 | [diff] [blame] | 273 | unsigned long base, top; |
| 274 | bool valid_bank_found = false; |
Masahiro Yamada | 12938bf | 2019-07-10 20:07:43 +0900 | [diff] [blame] | 275 | int ret, i; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 276 | |
Masahiro Yamada | 12938bf | 2019-07-10 20:07:43 +0900 | [diff] [blame] | 277 | ret = uniphier_dram_map_get(dram_map); |
| 278 | if (ret) |
| 279 | return ret; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 280 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 281 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | f0f6a80 | 2019-07-10 20:07:45 +0900 | [diff] [blame] | 282 | if (i < ARRAY_SIZE(gd->bd->bi_dram)) { |
| 283 | gd->bd->bi_dram[i].start = dram_map[i].base; |
| 284 | gd->bd->bi_dram[i].size = dram_map[i].size; |
| 285 | } |
| 286 | |
| 287 | if (!dram_map[i].size) |
| 288 | continue; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 289 | |
Masahiro Yamada | f0f6a80 | 2019-07-10 20:07:45 +0900 | [diff] [blame] | 290 | if (!valid_bank_found) |
| 291 | base = dram_map[i].base; |
| 292 | top = dram_map[i].base + dram_map[i].size; |
| 293 | valid_bank_found = true; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 294 | } |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 295 | |
Masahiro Yamada | f0f6a80 | 2019-07-10 20:07:45 +0900 | [diff] [blame] | 296 | if (!valid_bank_found) |
| 297 | return -EINVAL; |
| 298 | |
| 299 | /* map all the DRAM regions */ |
| 300 | uniphier_mem_map_init(base, top - base); |
| 301 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 302 | return 0; |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 303 | } |