Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kever Yang | a381bcf | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Rockchip Electronics Co., Ltd |
Kever Yang | a381bcf | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 7 | #include <fdt_support.h> |
Simon Glass | 691d719 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 8 | #include <init.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 10 | #include <spl.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 11 | #include <spl_gpio.h> |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 12 | #include <syscon.h> |
Kever Yang | a381bcf | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 13 | #include <asm/armv8/mmu.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Kever Yang | 27b95d2 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 15 | #include <asm/io.h> |
Kever Yang | 4e1aeb8 | 2019-07-22 19:59:40 +0800 | [diff] [blame] | 16 | #include <asm/arch-rockchip/bootrom.h> |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 17 | #include <asm/arch-rockchip/clock.h> |
Philipp Tomsich | 8c5805a | 2019-04-29 19:05:26 +0200 | [diff] [blame] | 18 | #include <asm/arch-rockchip/gpio.h> |
Kever Yang | f9e8145 | 2019-03-29 09:09:06 +0800 | [diff] [blame] | 19 | #include <asm/arch-rockchip/grf_rk3399.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 20 | #include <asm/arch-rockchip/hardware.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 22 | #include <power/regulator.h> |
Kever Yang | 27b95d2 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 23 | |
Kever Yang | 975e4ab | 2017-06-23 16:11:11 +0800 | [diff] [blame] | 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
Kever Yang | 27b95d2 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 26 | #define GRF_EMMCCORE_CON11 0xff77f02c |
Kever Yang | f9e8145 | 2019-03-29 09:09:06 +0800 | [diff] [blame] | 27 | #define GRF_BASE 0xff770000 |
Kever Yang | a381bcf | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 28 | |
Kever Yang | 4e1aeb8 | 2019-07-22 19:59:40 +0800 | [diff] [blame] | 29 | const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = { |
Quentin Schulz | 07b5d34 | 2022-07-11 16:15:33 +0200 | [diff] [blame^] | 30 | [BROM_BOOTSOURCE_EMMC] = "/mmc@fe330000", |
Artem Lapkin | e8a663c | 2021-05-26 17:32:27 +0800 | [diff] [blame] | 31 | [BROM_BOOTSOURCE_SPINOR] = "/spi@ff1d0000/flash@0", |
Jagan Teki | 97de393 | 2020-05-24 20:26:18 +0530 | [diff] [blame] | 32 | [BROM_BOOTSOURCE_SD] = "/mmc@fe320000", |
Kever Yang | 4e1aeb8 | 2019-07-22 19:59:40 +0800 | [diff] [blame] | 33 | }; |
| 34 | |
Kever Yang | a381bcf | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 35 | static struct mm_region rk3399_mem_map[] = { |
| 36 | { |
| 37 | .virt = 0x0UL, |
| 38 | .phys = 0x0UL, |
Kever Yang | 90c9127 | 2017-04-17 16:42:44 +0800 | [diff] [blame] | 39 | .size = 0xf8000000UL, |
Kever Yang | a381bcf | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 40 | .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | |
| 41 | PTE_BLOCK_INNER_SHARE |
| 42 | }, { |
Kever Yang | 90c9127 | 2017-04-17 16:42:44 +0800 | [diff] [blame] | 43 | .virt = 0xf8000000UL, |
| 44 | .phys = 0xf8000000UL, |
| 45 | .size = 0x08000000UL, |
Kever Yang | a381bcf | 2016-07-19 21:16:59 +0800 | [diff] [blame] | 46 | .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) | |
| 47 | PTE_BLOCK_NON_SHARE | |
| 48 | PTE_BLOCK_PXN | PTE_BLOCK_UXN |
| 49 | }, { |
| 50 | /* List terminator */ |
| 51 | 0, |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | struct mm_region *mem_map = rk3399_mem_map; |
Kever Yang | 27b95d2 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 56 | |
Kever Yang | 87ac550 | 2019-07-09 22:05:59 +0800 | [diff] [blame] | 57 | #ifdef CONFIG_SPL_BUILD |
| 58 | |
| 59 | #define TIMER_END_COUNT_L 0x00 |
| 60 | #define TIMER_END_COUNT_H 0x04 |
| 61 | #define TIMER_INIT_COUNT_L 0x10 |
| 62 | #define TIMER_INIT_COUNT_H 0x14 |
| 63 | #define TIMER_CONTROL_REG 0x1c |
| 64 | |
| 65 | #define TIMER_EN 0x1 |
| 66 | #define TIMER_FMODE BIT(0) |
| 67 | #define TIMER_RMODE BIT(1) |
| 68 | |
| 69 | void rockchip_stimer_init(void) |
| 70 | { |
| 71 | /* If Timer already enabled, don't re-init it */ |
| 72 | u32 reg = readl(CONFIG_ROCKCHIP_STIMER_BASE + TIMER_CONTROL_REG); |
| 73 | |
| 74 | if (reg & TIMER_EN) |
| 75 | return; |
| 76 | |
| 77 | writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_L); |
| 78 | writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_H); |
| 79 | writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_L); |
| 80 | writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_H); |
| 81 | writel(TIMER_EN | TIMER_FMODE, CONFIG_ROCKCHIP_STIMER_BASE + \ |
| 82 | TIMER_CONTROL_REG); |
| 83 | } |
| 84 | #endif |
| 85 | |
Kever Yang | 27b95d2 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 86 | int arch_cpu_init(void) |
| 87 | { |
Kever Yang | 27b95d2 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 88 | |
Kever Yang | bd06a7c | 2019-07-22 19:59:38 +0800 | [diff] [blame] | 89 | #ifdef CONFIG_SPL_BUILD |
| 90 | struct rk3399_pmusgrf_regs *sgrf; |
| 91 | struct rk3399_grf_regs *grf; |
| 92 | |
| 93 | /* |
| 94 | * Disable DDR and SRAM security regions. |
| 95 | * |
| 96 | * As we are entered from the BootROM, the region from |
| 97 | * 0x0 through 0xfffff (i.e. the first MB of memory) will |
| 98 | * be protected. This will cause issues with the DW_MMC |
| 99 | * driver, which tries to DMA from/to the stack (likely) |
| 100 | * located in this range. |
| 101 | */ |
| 102 | sgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF); |
| 103 | rk_clrsetreg(&sgrf->ddr_rgn_con[16], 0x1ff, 0); |
| 104 | rk_clrreg(&sgrf->slv_secure_con4, 0x2000); |
| 105 | |
| 106 | /* eMMC clock generator: disable the clock multipilier */ |
| 107 | grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
Kever Yang | f9e8145 | 2019-03-29 09:09:06 +0800 | [diff] [blame] | 108 | rk_clrreg(&grf->emmccore_con[11], 0x0ff); |
Kever Yang | bd06a7c | 2019-07-22 19:59:38 +0800 | [diff] [blame] | 109 | #endif |
Kever Yang | 27b95d2 | 2016-10-07 15:56:16 +0800 | [diff] [blame] | 110 | |
| 111 | return 0; |
| 112 | } |
Kever Yang | c79bce1 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 113 | |
| 114 | #ifdef CONFIG_DEBUG_UART_BOARD_INIT |
| 115 | void board_debug_uart_init(void) |
| 116 | { |
| 117 | #define GRF_BASE 0xff770000 |
| 118 | #define GPIO0_BASE 0xff720000 |
| 119 | #define PMUGRF_BASE 0xff320000 |
| 120 | struct rk3399_grf_regs * const grf = (void *)GRF_BASE; |
Kever Yang | c79bce1 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 121 | |
| 122 | #if defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff180000) |
| 123 | /* Enable early UART0 on the RK3399 */ |
| 124 | rk_clrsetreg(&grf->gpio2c_iomux, |
| 125 | GRF_GPIO2C0_SEL_MASK, |
| 126 | GRF_UART0BT_SIN << GRF_GPIO2C0_SEL_SHIFT); |
| 127 | rk_clrsetreg(&grf->gpio2c_iomux, |
| 128 | GRF_GPIO2C1_SEL_MASK, |
| 129 | GRF_UART0BT_SOUT << GRF_GPIO2C1_SEL_SHIFT); |
Christoph Muellner | 78a1ac3 | 2019-05-07 10:58:43 +0200 | [diff] [blame] | 130 | #elif defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff1B0000) |
| 131 | /* Enable early UART3 on the RK3399 */ |
| 132 | rk_clrsetreg(&grf->gpio3b_iomux, |
| 133 | GRF_GPIO3B6_SEL_MASK, |
| 134 | GRF_UART3_SIN << GRF_GPIO3B6_SEL_SHIFT); |
| 135 | rk_clrsetreg(&grf->gpio3b_iomux, |
| 136 | GRF_GPIO3B7_SEL_MASK, |
| 137 | GRF_UART3_SOUT << GRF_GPIO3B7_SEL_SHIFT); |
Kever Yang | c79bce1 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 138 | #else |
Simon Glass | 55de0c1 | 2021-11-03 07:16:08 -0600 | [diff] [blame] | 139 | struct rk3399_pmugrf_regs * const pmugrf = (void *)PMUGRF_BASE; |
| 140 | struct rockchip_gpio_regs * const gpio = (void *)GPIO0_BASE; |
Kever Yang | c79bce1 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 141 | |
Simon Glass | 55de0c1 | 2021-11-03 07:16:08 -0600 | [diff] [blame] | 142 | if (IS_ENABLED(CONFIG_SPL_BUILD) && |
Marty E. Plummer | 6d36e92 | 2021-12-24 16:43:46 +0300 | [diff] [blame] | 143 | (IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_BOB) || |
| 144 | IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_KEVIN))) { |
Simon Glass | 55de0c1 | 2021-11-03 07:16:08 -0600 | [diff] [blame] | 145 | rk_setreg(&grf->io_vsel, 1 << 0); |
Kever Yang | c79bce1 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 146 | |
Simon Glass | 55de0c1 | 2021-11-03 07:16:08 -0600 | [diff] [blame] | 147 | /* |
| 148 | * Let's enable these power rails here, we are already running |
| 149 | * the SPI-Flash-based code. |
| 150 | */ |
| 151 | spl_gpio_output(gpio, GPIO(BANK_B, 2), 1); /* PP1500_EN */ |
| 152 | spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 2), |
| 153 | GPIO_PULL_NORMAL); |
| 154 | |
| 155 | spl_gpio_output(gpio, GPIO(BANK_B, 4), 1); /* PP3000_EN */ |
| 156 | spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 4), |
| 157 | GPIO_PULL_NORMAL); |
| 158 | } |
Kever Yang | c79bce1 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 159 | |
| 160 | /* Enable early UART2 channel C on the RK3399 */ |
| 161 | rk_clrsetreg(&grf->gpio4c_iomux, |
| 162 | GRF_GPIO4C3_SEL_MASK, |
| 163 | GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT); |
| 164 | rk_clrsetreg(&grf->gpio4c_iomux, |
| 165 | GRF_GPIO4C4_SEL_MASK, |
| 166 | GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT); |
| 167 | /* Set channel C as UART2 input */ |
| 168 | rk_clrsetreg(&grf->soc_con7, |
| 169 | GRF_UART_DBG_SEL_MASK, |
| 170 | GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT); |
| 171 | #endif |
| 172 | } |
| 173 | #endif |
Kever Yang | 4238e52 | 2019-07-22 19:59:36 +0800 | [diff] [blame] | 174 | |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 175 | #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD) |
Kever Yang | 4238e52 | 2019-07-22 19:59:36 +0800 | [diff] [blame] | 176 | const char *spl_decode_boot_device(u32 boot_device) |
| 177 | { |
| 178 | int i; |
| 179 | static const struct { |
| 180 | u32 boot_device; |
| 181 | const char *ofpath; |
| 182 | } spl_boot_devices_tbl[] = { |
Jagan Teki | 97de393 | 2020-05-24 20:26:18 +0530 | [diff] [blame] | 183 | { BOOT_DEVICE_MMC1, "/mmc@fe320000" }, |
Quentin Schulz | 07b5d34 | 2022-07-11 16:15:33 +0200 | [diff] [blame^] | 184 | { BOOT_DEVICE_MMC2, "/mmc@fe330000" }, |
Kever Yang | 4238e52 | 2019-07-22 19:59:36 +0800 | [diff] [blame] | 185 | { BOOT_DEVICE_SPI, "/spi@ff1d0000" }, |
| 186 | }; |
| 187 | |
| 188 | for (i = 0; i < ARRAY_SIZE(spl_boot_devices_tbl); ++i) |
| 189 | if (spl_boot_devices_tbl[i].boot_device == boot_device) |
| 190 | return spl_boot_devices_tbl[i].ofpath; |
| 191 | |
| 192 | return NULL; |
| 193 | } |
| 194 | |
| 195 | void spl_perform_fixups(struct spl_image_info *spl_image) |
| 196 | { |
| 197 | void *blob = spl_image->fdt_addr; |
| 198 | const char *boot_ofpath; |
| 199 | int chosen; |
| 200 | |
| 201 | /* |
| 202 | * Inject the ofpath of the device the full U-Boot (or Linux in |
| 203 | * Falcon-mode) was booted from into the FDT, if a FDT has been |
| 204 | * loaded at the same time. |
| 205 | */ |
| 206 | if (!blob) |
| 207 | return; |
| 208 | |
| 209 | boot_ofpath = spl_decode_boot_device(spl_image->boot_device); |
| 210 | if (!boot_ofpath) { |
| 211 | pr_err("%s: could not map boot_device to ofpath\n", __func__); |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | chosen = fdt_find_or_add_subnode(blob, 0, "chosen"); |
| 216 | if (chosen < 0) { |
| 217 | pr_err("%s: could not find/create '/chosen'\n", __func__); |
| 218 | return; |
| 219 | } |
| 220 | fdt_setprop_string(blob, chosen, |
| 221 | "u-boot,spl-boot-device", boot_ofpath); |
| 222 | } |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 223 | |
Simon Glass | 83061db | 2021-07-10 21:14:30 -0600 | [diff] [blame] | 224 | #if defined(SPL_GPIO) |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 225 | static void rk3399_force_power_on_reset(void) |
| 226 | { |
| 227 | ofnode node; |
| 228 | struct gpio_desc sysreset_gpio; |
| 229 | |
| 230 | debug("%s: trying to force a power-on reset\n", __func__); |
| 231 | |
| 232 | node = ofnode_path("/config"); |
| 233 | if (!ofnode_valid(node)) { |
| 234 | debug("%s: no /config node?\n", __func__); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0, |
| 239 | &sysreset_gpio, GPIOD_IS_OUT)) { |
| 240 | debug("%s: could not find a /config/sysreset-gpio\n", __func__); |
| 241 | return; |
| 242 | } |
| 243 | |
| 244 | dm_gpio_set_value(&sysreset_gpio, 1); |
| 245 | } |
| 246 | #endif |
| 247 | |
Jagan Teki | 500d1e7 | 2020-07-21 20:36:00 +0530 | [diff] [blame] | 248 | void __weak led_setup(void) |
| 249 | { |
| 250 | } |
| 251 | |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 252 | void spl_board_init(void) |
| 253 | { |
Jagan Teki | 500d1e7 | 2020-07-21 20:36:00 +0530 | [diff] [blame] | 254 | led_setup(); |
| 255 | |
Simon Glass | 83061db | 2021-07-10 21:14:30 -0600 | [diff] [blame] | 256 | #if defined(SPL_GPIO) |
Jagan Teki | b52a199 | 2020-01-09 14:22:17 +0530 | [diff] [blame] | 257 | struct rockchip_cru *cru = rockchip_get_cru(); |
Kever Yang | 47b0ead | 2019-07-22 19:59:39 +0800 | [diff] [blame] | 258 | |
| 259 | /* |
| 260 | * The RK3399 resets only 'almost all logic' (see also in the TRM |
| 261 | * "3.9.4 Global software reset"), when issuing a software reset. |
| 262 | * This may cause issues during boot-up for some configurations of |
| 263 | * the application software stack. |
| 264 | * |
| 265 | * To work around this, we test whether the last reset reason was |
| 266 | * a power-on reset and (if not) issue an overtemp-reset to reset |
| 267 | * the entire module. |
| 268 | * |
| 269 | * While this was previously fixed by modifying the various places |
| 270 | * that could generate a software reset (e.g. U-Boot's sysreset |
| 271 | * driver, the ATF or Linux), we now have it here to ensure that |
| 272 | * we no longer have to track this through the various components. |
| 273 | */ |
| 274 | if (cru->glb_rst_st != 0) |
| 275 | rk3399_force_power_on_reset(); |
| 276 | #endif |
| 277 | |
| 278 | #if defined(SPL_DM_REGULATOR) |
| 279 | /* |
| 280 | * Turning the eMMC and SPI back on (if disabled via the Qseven |
| 281 | * BIOS_ENABLE) signal is done through a always-on regulator). |
| 282 | */ |
| 283 | if (regulators_enable_boot_on(false)) |
| 284 | debug("%s: Cannot enable boot on regulator\n", __func__); |
| 285 | #endif |
| 286 | } |
Kever Yang | 4238e52 | 2019-07-22 19:59:36 +0800 | [diff] [blame] | 287 | #endif |