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