Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 1 | /* |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 2 | * Copyright (C) 2012-2015 Panasonic Corporation |
| 3 | * Copyright (C) 2015-2017 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Masahiro Yamada | 1f75280 | 2016-04-21 14:43:12 +0900 | [diff] [blame] | 10 | #include <fdtdec.h> |
Masahiro Yamada | e4e789d | 2017-01-21 18:05:24 +0900 | [diff] [blame] | 11 | #include <linux/errno.h> |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 12 | #include <linux/sizes.h> |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 13 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 14 | #include "sg-regs.h" |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 15 | #include "soc-info.h" |
| 16 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 17 | #define pr_warn(fmt, args...) printf(fmt, ##args) |
| 18 | #define pr_err(fmt, args...) printf(fmt, ##args) |
| 19 | |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 22 | struct uniphier_memif_data { |
| 23 | unsigned int soc_id; |
| 24 | unsigned long sparse_ch1_base; |
| 25 | int have_ch2; |
| 26 | }; |
| 27 | |
| 28 | static const struct uniphier_memif_data uniphier_memif_data[] = { |
| 29 | { |
| 30 | .soc_id = UNIPHIER_SLD3_ID, |
| 31 | .sparse_ch1_base = 0xc0000000, |
| 32 | /* |
| 33 | * In fact, SLD3 has DRAM ch2, but the memory regions for ch1 |
| 34 | * and ch2 overlap, and host cannot get access to them at the |
| 35 | * same time. Hide the ch2 from U-Boot. |
| 36 | */ |
| 37 | }, |
| 38 | { |
| 39 | .soc_id = UNIPHIER_LD4_ID, |
| 40 | .sparse_ch1_base = 0xc0000000, |
| 41 | }, |
| 42 | { |
| 43 | .soc_id = UNIPHIER_PRO4_ID, |
| 44 | .sparse_ch1_base = 0xa0000000, |
| 45 | }, |
| 46 | { |
| 47 | .soc_id = UNIPHIER_SLD8_ID, |
| 48 | .sparse_ch1_base = 0xc0000000, |
| 49 | }, |
| 50 | { |
| 51 | .soc_id = UNIPHIER_PRO5_ID, |
| 52 | .sparse_ch1_base = 0xc0000000, |
| 53 | }, |
| 54 | { |
| 55 | .soc_id = UNIPHIER_PXS2_ID, |
| 56 | .sparse_ch1_base = 0xc0000000, |
| 57 | .have_ch2 = 1, |
| 58 | }, |
| 59 | { |
| 60 | .soc_id = UNIPHIER_LD6B_ID, |
| 61 | .sparse_ch1_base = 0xc0000000, |
| 62 | .have_ch2 = 1, |
| 63 | }, |
| 64 | { |
| 65 | .soc_id = UNIPHIER_LD11_ID, |
| 66 | .sparse_ch1_base = 0xc0000000, |
| 67 | }, |
| 68 | { |
| 69 | .soc_id = UNIPHIER_LD20_ID, |
| 70 | .sparse_ch1_base = 0xc0000000, |
| 71 | .have_ch2 = 1, |
| 72 | }, |
| 73 | { |
| 74 | .soc_id = UNIPHIER_PXS3_ID, |
| 75 | .sparse_ch1_base = 0xc0000000, |
| 76 | .have_ch2 = 1, |
| 77 | }, |
| 78 | }; |
| 79 | UNIPHIER_DEFINE_SOCDATA_FUNC(uniphier_get_memif_data, uniphier_memif_data) |
| 80 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 81 | struct uniphier_dram_map { |
| 82 | unsigned long base; |
| 83 | unsigned long size; |
| 84 | }; |
| 85 | |
| 86 | static int uniphier_memconf_decode(struct uniphier_dram_map *dram_map) |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 87 | { |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 88 | const struct uniphier_memif_data *data; |
| 89 | unsigned long size; |
| 90 | u32 val; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 91 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 92 | data = uniphier_get_memif_data(); |
| 93 | if (!data) { |
| 94 | pr_err("unsupported SoC\n"); |
| 95 | return -EINVAL; |
| 96 | } |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 97 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 98 | val = readl(SG_MEMCONF); |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 99 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 100 | /* set up ch0 */ |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 101 | dram_map[0].base = CONFIG_SYS_SDRAM_BASE; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 102 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 103 | switch (val & SG_MEMCONF_CH0_SZ_MASK) { |
| 104 | case SG_MEMCONF_CH0_SZ_64M: |
| 105 | size = SZ_64M; |
| 106 | break; |
| 107 | case SG_MEMCONF_CH0_SZ_128M: |
| 108 | size = SZ_128M; |
| 109 | break; |
| 110 | case SG_MEMCONF_CH0_SZ_256M: |
| 111 | size = SZ_256M; |
| 112 | break; |
| 113 | case SG_MEMCONF_CH0_SZ_512M: |
| 114 | size = SZ_512M; |
| 115 | break; |
| 116 | case SG_MEMCONF_CH0_SZ_1G: |
| 117 | size = SZ_1G; |
| 118 | break; |
| 119 | default: |
Masahiro Yamada | 1566db9 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 120 | pr_err("error: invalid value is set to MEMCONF ch0 size\n"); |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 121 | return -EINVAL; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 122 | } |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 123 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 124 | if ((val & SG_MEMCONF_CH0_NUM_MASK) == SG_MEMCONF_CH0_NUM_2) |
| 125 | size *= 2; |
| 126 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 127 | dram_map[0].size = size; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 128 | |
| 129 | /* set up ch1 */ |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 130 | dram_map[1].base = dram_map[0].base + size; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 131 | |
| 132 | if (val & SG_MEMCONF_SPARSEMEM) { |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 133 | if (dram_map[1].base > data->sparse_ch1_base) { |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 134 | pr_warn("Sparse mem is enabled, but ch0 and ch1 overlap\n"); |
| 135 | pr_warn("Only ch0 is available\n"); |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 136 | dram_map[1].base = 0; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 140 | dram_map[1].base = data->sparse_ch1_base; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | switch (val & SG_MEMCONF_CH1_SZ_MASK) { |
| 144 | case SG_MEMCONF_CH1_SZ_64M: |
| 145 | size = SZ_64M; |
| 146 | break; |
| 147 | case SG_MEMCONF_CH1_SZ_128M: |
| 148 | size = SZ_128M; |
| 149 | break; |
| 150 | case SG_MEMCONF_CH1_SZ_256M: |
| 151 | size = SZ_256M; |
| 152 | break; |
| 153 | case SG_MEMCONF_CH1_SZ_512M: |
| 154 | size = SZ_512M; |
| 155 | break; |
| 156 | case SG_MEMCONF_CH1_SZ_1G: |
| 157 | size = SZ_1G; |
| 158 | break; |
| 159 | default: |
Masahiro Yamada | 1566db9 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 160 | pr_err("error: invalid value is set to MEMCONF ch1 size\n"); |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 161 | return -EINVAL; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 162 | } |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 163 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 164 | if ((val & SG_MEMCONF_CH1_NUM_MASK) == SG_MEMCONF_CH1_NUM_2) |
| 165 | size *= 2; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 166 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 167 | dram_map[1].size = size; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 168 | |
Masahiro Yamada | 95beeed | 2017-02-20 12:10:05 +0900 | [diff] [blame] | 169 | if (!data->have_ch2 || val & SG_MEMCONF_CH2_DISABLE) |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 170 | return 0; |
| 171 | |
| 172 | /* set up ch2 */ |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 173 | dram_map[2].base = dram_map[1].base + size; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 174 | |
| 175 | switch (val & SG_MEMCONF_CH2_SZ_MASK) { |
| 176 | case SG_MEMCONF_CH2_SZ_64M: |
| 177 | size = SZ_64M; |
| 178 | break; |
| 179 | case SG_MEMCONF_CH2_SZ_128M: |
| 180 | size = SZ_128M; |
| 181 | break; |
| 182 | case SG_MEMCONF_CH2_SZ_256M: |
| 183 | size = SZ_256M; |
| 184 | break; |
| 185 | case SG_MEMCONF_CH2_SZ_512M: |
| 186 | size = SZ_512M; |
| 187 | break; |
| 188 | case SG_MEMCONF_CH2_SZ_1G: |
| 189 | size = SZ_1G; |
| 190 | break; |
| 191 | default: |
Masahiro Yamada | 1566db9 | 2017-02-20 12:09:00 +0900 | [diff] [blame] | 192 | pr_err("error: invalid value is set to MEMCONF ch2 size\n"); |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 193 | return -EINVAL; |
| 194 | } |
| 195 | |
| 196 | if ((val & SG_MEMCONF_CH2_NUM_MASK) == SG_MEMCONF_CH2_NUM_2) |
| 197 | size *= 2; |
| 198 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 199 | dram_map[2].size = size; |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 200 | |
Masahiro Yamada | bb2ff9d | 2014-10-03 19:21:06 +0900 | [diff] [blame] | 201 | return 0; |
| 202 | } |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 203 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 204 | int dram_init(void) |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 205 | { |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 206 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 207 | int ret, i; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 208 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 209 | gd->ram_size = 0; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 210 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 211 | ret = uniphier_memconf_decode(dram_map); |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 212 | if (ret) |
| 213 | return ret; |
| 214 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 215 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 216 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 217 | if (!dram_map[i].size) |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 218 | break; |
| 219 | |
| 220 | /* |
| 221 | * U-Boot relocates itself to the tail of the memory region, |
| 222 | * but it does not expect sparse memory. We use the first |
| 223 | * contiguous chunk here. |
| 224 | */ |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 225 | if (i > 0 && dram_map[i - 1].base + dram_map[i - 1].size < |
| 226 | dram_map[i].base) |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 227 | break; |
| 228 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 229 | gd->ram_size += dram_map[i].size; |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 230 | } |
| 231 | |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 232 | return 0; |
| 233 | } |
Masahiro Yamada | a90b110 | 2016-03-29 20:18:45 +0900 | [diff] [blame] | 234 | |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 235 | int dram_init_banksize(void) |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 236 | { |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 237 | struct uniphier_dram_map dram_map[3] = {}; |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 238 | int i; |
| 239 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 240 | uniphier_memconf_decode(dram_map); |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 241 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 242 | for (i = 0; i < ARRAY_SIZE(dram_map); i++) { |
Masahiro Yamada | d97c1cb | 2017-01-28 06:53:43 +0900 | [diff] [blame] | 243 | if (i >= ARRAY_SIZE(gd->bd->bi_dram)) |
| 244 | break; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 245 | |
Masahiro Yamada | 3dc8097 | 2017-02-05 10:52:12 +0900 | [diff] [blame] | 246 | gd->bd->bi_dram[i].start = dram_map[i].base; |
| 247 | gd->bd->bi_dram[i].size = dram_map[i].size; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 248 | } |
Simon Glass | 2f949c3 | 2017-03-31 08:40:32 -0600 | [diff] [blame] | 249 | |
| 250 | return 0; |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | #ifdef CONFIG_OF_BOARD_SETUP |
| 254 | /* |
| 255 | * The DRAM PHY requires 64 byte scratch area in each DRAM channel |
| 256 | * for its dynamic PHY training feature. |
| 257 | */ |
| 258 | int ft_board_setup(void *fdt, bd_t *bd) |
| 259 | { |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 260 | unsigned long rsv_addr; |
| 261 | const unsigned long rsv_size = 64; |
Masahiro Yamada | 0197f63 | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 262 | int i, ret; |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 263 | |
Masahiro Yamada | 3164905 | 2017-01-21 18:05:26 +0900 | [diff] [blame] | 264 | if (uniphier_get_soc_id() != UNIPHIER_LD20_ID) |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 265 | return 0; |
| 266 | |
Masahiro Yamada | 0197f63 | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 267 | for (i = 0; i < ARRAY_SIZE(gd->bd->bi_dram); i++) { |
Masahiro Yamada | b14071d | 2017-02-20 17:13:32 +0900 | [diff] [blame] | 268 | if (!gd->bd->bi_dram[i].size) |
| 269 | continue; |
| 270 | |
Masahiro Yamada | 0197f63 | 2017-01-28 06:53:44 +0900 | [diff] [blame] | 271 | rsv_addr = gd->bd->bi_dram[i].start + gd->bd->bi_dram[i].size; |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 272 | rsv_addr -= rsv_size; |
| 273 | |
| 274 | ret = fdt_add_mem_rsv(fdt, rsv_addr, rsv_size); |
| 275 | if (ret) |
| 276 | return -ENOSPC; |
| 277 | |
| 278 | printf(" Reserved memory region for DRAM PHY training: addr=%lx size=%lx\n", |
| 279 | rsv_addr, rsv_size); |
| 280 | } |
| 281 | |
| 282 | return 0; |
Masahiro Yamada | b4782cd | 2015-09-11 20:17:49 +0900 | [diff] [blame] | 283 | } |
Masahiro Yamada | 460483c | 2016-06-17 19:24:29 +0900 | [diff] [blame] | 284 | #endif |