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