Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Rockchip Electronics Co., Ltd |
Philipp Tomsich | cbe18f1 | 2017-09-11 12:48:12 +0200 | [diff] [blame] | 4 | * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | c35f8e5 | 2019-01-21 14:53:31 -0700 | [diff] [blame] | 8 | #include <debug_uart.h> |
| 9 | #include <dm.h> |
| 10 | #include <ram.h> |
| 11 | #include <spl.h> |
Simon Glass | 9e92116 | 2019-01-21 14:53:36 -0700 | [diff] [blame] | 12 | #include <spl_gpio.h> |
Simon Glass | c35f8e5 | 2019-01-21 14:53:31 -0700 | [diff] [blame] | 13 | #include <syscon.h> |
Jagan Teki | 07586ee | 2019-06-21 00:25:00 +0530 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Simon Glass | c35f8e5 | 2019-01-21 14:53:31 -0700 | [diff] [blame] | 15 | #include <asm/io.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 16 | #include <asm/arch-rockchip/bootrom.h> |
| 17 | #include <asm/arch-rockchip/clock.h> |
Jagan Teki | 07586ee | 2019-06-21 00:25:00 +0530 | [diff] [blame] | 18 | #include <asm/arch-rockchip/cru_rk3399.h> |
Kever Yang | 15f09a1 | 2019-03-28 11:01:23 +0800 | [diff] [blame] | 19 | #include <asm/arch-rockchip/grf_rk3399.h> |
| 20 | #include <asm/arch-rockchip/hardware.h> |
| 21 | #include <asm/arch-rockchip/periph.h> |
| 22 | #include <asm/arch-rockchip/sys_proto.h> |
Jagan Teki | 07586ee | 2019-06-21 00:25:00 +0530 | [diff] [blame] | 23 | #include <power/regulator.h> |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 24 | |
Philipp Tomsich | cbe18f1 | 2017-09-11 12:48:12 +0200 | [diff] [blame] | 25 | void board_return_to_bootrom(void) |
| 26 | { |
Philipp Tomsich | b82bd1f | 2017-10-10 16:21:16 +0200 | [diff] [blame] | 27 | back_to_bootrom(BROM_BOOT_NEXTSTAGE); |
Philipp Tomsich | cbe18f1 | 2017-09-11 12:48:12 +0200 | [diff] [blame] | 28 | } |
| 29 | |
Philipp Tomsich | c55addd | 2017-09-29 19:27:58 +0200 | [diff] [blame] | 30 | static const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = { |
| 31 | [BROM_BOOTSOURCE_EMMC] = "/sdhci@fe330000", |
| 32 | [BROM_BOOTSOURCE_SPINOR] = "/spi@ff1d0000", |
| 33 | [BROM_BOOTSOURCE_SD] = "/dwmmc@fe320000", |
| 34 | }; |
| 35 | |
| 36 | const char *board_spl_was_booted_from(void) |
| 37 | { |
| 38 | u32 bootdevice_brom_id = readl(RK3399_BROM_BOOTSOURCE_ID_ADDR); |
| 39 | const char *bootdevice_ofpath = NULL; |
| 40 | |
| 41 | if (bootdevice_brom_id < ARRAY_SIZE(boot_devices)) |
| 42 | bootdevice_ofpath = boot_devices[bootdevice_brom_id]; |
| 43 | |
| 44 | if (bootdevice_ofpath) |
| 45 | debug("%s: brom_bootdevice_id %x maps to '%s'\n", |
| 46 | __func__, bootdevice_brom_id, bootdevice_ofpath); |
| 47 | else |
| 48 | debug("%s: failed to resolve brom_bootdevice_id %x\n", |
| 49 | __func__, bootdevice_brom_id); |
| 50 | |
| 51 | return bootdevice_ofpath; |
| 52 | } |
| 53 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 54 | u32 spl_boot_device(void) |
| 55 | { |
Philipp Tomsich | cbe18f1 | 2017-09-11 12:48:12 +0200 | [diff] [blame] | 56 | u32 boot_device = BOOT_DEVICE_MMC1; |
| 57 | |
| 58 | if (CONFIG_IS_ENABLED(ROCKCHIP_BACK_TO_BROM)) |
| 59 | return BOOT_DEVICE_BOOTROM; |
| 60 | |
| 61 | return boot_device; |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 62 | } |
| 63 | |
Philipp Tomsich | e5f2ecc | 2018-05-24 17:15:52 +0200 | [diff] [blame] | 64 | const char *spl_decode_boot_device(u32 boot_device) |
| 65 | { |
| 66 | int i; |
| 67 | static const struct { |
| 68 | u32 boot_device; |
| 69 | const char *ofpath; |
| 70 | } spl_boot_devices_tbl[] = { |
| 71 | { BOOT_DEVICE_MMC1, "/dwmmc@fe320000" }, |
| 72 | { BOOT_DEVICE_MMC2, "/sdhci@fe330000" }, |
| 73 | { BOOT_DEVICE_SPI, "/spi@ff1d0000" }, |
| 74 | }; |
| 75 | |
| 76 | for (i = 0; i < ARRAY_SIZE(spl_boot_devices_tbl); ++i) |
| 77 | if (spl_boot_devices_tbl[i].boot_device == boot_device) |
| 78 | return spl_boot_devices_tbl[i].ofpath; |
| 79 | |
| 80 | return NULL; |
| 81 | } |
| 82 | |
| 83 | void spl_perform_fixups(struct spl_image_info *spl_image) |
| 84 | { |
| 85 | void *blob = spl_image->fdt_addr; |
| 86 | const char *boot_ofpath; |
| 87 | int chosen; |
| 88 | |
| 89 | /* |
| 90 | * Inject the ofpath of the device the full U-Boot (or Linux in |
| 91 | * Falcon-mode) was booted from into the FDT, if a FDT has been |
| 92 | * loaded at the same time. |
| 93 | */ |
| 94 | if (!blob) |
| 95 | return; |
| 96 | |
| 97 | boot_ofpath = spl_decode_boot_device(spl_image->boot_device); |
| 98 | if (!boot_ofpath) { |
| 99 | pr_err("%s: could not map boot_device to ofpath\n", __func__); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | chosen = fdt_find_or_add_subnode(blob, 0, "chosen"); |
| 104 | if (chosen < 0) { |
| 105 | pr_err("%s: could not find/create '/chosen'\n", __func__); |
| 106 | return; |
| 107 | } |
| 108 | fdt_setprop_string(blob, chosen, |
| 109 | "u-boot,spl-boot-device", boot_ofpath); |
| 110 | } |
| 111 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 112 | #define TIMER_CHN10_BASE 0xff8680a0 |
| 113 | #define TIMER_END_COUNT_L 0x00 |
| 114 | #define TIMER_END_COUNT_H 0x04 |
| 115 | #define TIMER_INIT_COUNT_L 0x10 |
| 116 | #define TIMER_INIT_COUNT_H 0x14 |
| 117 | #define TIMER_CONTROL_REG 0x1c |
| 118 | |
| 119 | #define TIMER_EN 0x1 |
| 120 | #define TIMER_FMODE (0 << 1) |
| 121 | #define TIMER_RMODE (1 << 1) |
| 122 | |
| 123 | void secure_timer_init(void) |
| 124 | { |
| 125 | writel(0xffffffff, TIMER_CHN10_BASE + TIMER_END_COUNT_L); |
| 126 | writel(0xffffffff, TIMER_CHN10_BASE + TIMER_END_COUNT_H); |
| 127 | writel(0, TIMER_CHN10_BASE + TIMER_INIT_COUNT_L); |
| 128 | writel(0, TIMER_CHN10_BASE + TIMER_INIT_COUNT_H); |
| 129 | writel(TIMER_EN | TIMER_FMODE, TIMER_CHN10_BASE + TIMER_CONTROL_REG); |
| 130 | } |
| 131 | |
Philipp Tomsich | 7ee16de | 2017-04-01 12:59:25 +0200 | [diff] [blame] | 132 | |
Philipp Tomsich | 7ee16de | 2017-04-01 12:59:25 +0200 | [diff] [blame] | 133 | void board_init_f(ulong dummy) |
| 134 | { |
Philipp Tomsich | 7ee16de | 2017-04-01 12:59:25 +0200 | [diff] [blame] | 135 | struct udevice *dev; |
Philipp Tomsich | ba16573 | 2017-08-29 18:24:05 +0200 | [diff] [blame] | 136 | struct rk3399_pmusgrf_regs *sgrf; |
| 137 | struct rk3399_grf_regs *grf; |
Philipp Tomsich | 7ee16de | 2017-04-01 12:59:25 +0200 | [diff] [blame] | 138 | int ret; |
| 139 | |
Kever Yang | c79bce1 | 2019-03-29 09:09:07 +0800 | [diff] [blame] | 140 | #ifdef CONFIG_DEBUG_UART |
Philipp Tomsich | 73ced87 | 2019-02-01 16:48:31 +0100 | [diff] [blame] | 141 | debug_uart_init(); |
| 142 | |
Simon Glass | 9e92116 | 2019-01-21 14:53:36 -0700 | [diff] [blame] | 143 | # ifdef CONFIG_TARGET_CHROMEBOOK_BOB |
| 144 | int sum, i; |
| 145 | |
Simon Glass | 9e92116 | 2019-01-21 14:53:36 -0700 | [diff] [blame] | 146 | /* |
| 147 | * Add a delay and ensure that the compiler does not optimise this out. |
| 148 | * This is needed since the power rails tail a while to turn on, and |
| 149 | * we get garbage serial output otherwise. |
| 150 | */ |
| 151 | sum = 0; |
| 152 | for (i = 0; i < 150000; i++) |
| 153 | sum += i; |
| 154 | gru_dummy_function(sum); |
| 155 | #endif /* CONFIG_TARGET_CHROMEBOOK_BOB */ |
| 156 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 157 | /* |
| 158 | * Debug UART can be used from here if required: |
| 159 | * |
| 160 | * debug_uart_init(); |
| 161 | * printch('a'); |
| 162 | * printhex8(0x1234); |
| 163 | * printascii("string"); |
| 164 | */ |
Jagan Teki | 5e72164 | 2019-06-21 00:25:01 +0530 | [diff] [blame] | 165 | debug("U-Boot SPL board init\n"); |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 166 | #endif |
Kever Yang | c4a9215 | 2017-05-05 11:01:43 +0800 | [diff] [blame] | 167 | |
Kever Yang | 232cf96 | 2017-03-20 14:47:16 +0800 | [diff] [blame] | 168 | ret = spl_early_init(); |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 169 | if (ret) { |
Kever Yang | 232cf96 | 2017-03-20 14:47:16 +0800 | [diff] [blame] | 170 | debug("spl_early_init() failed: %d\n", ret); |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 171 | hang(); |
| 172 | } |
| 173 | |
Philipp Tomsich | 504b9f1 | 2017-03-29 21:20:28 +0200 | [diff] [blame] | 174 | /* |
Kever Yang | c4a9215 | 2017-05-05 11:01:43 +0800 | [diff] [blame] | 175 | * Disable DDR and SRAM security regions. |
Philipp Tomsich | 504b9f1 | 2017-03-29 21:20:28 +0200 | [diff] [blame] | 176 | * |
| 177 | * As we are entered from the BootROM, the region from |
| 178 | * 0x0 through 0xfffff (i.e. the first MB of memory) will |
| 179 | * be protected. This will cause issues with the DW_MMC |
| 180 | * driver, which tries to DMA from/to the stack (likely) |
| 181 | * located in this range. |
| 182 | */ |
Philipp Tomsich | ba16573 | 2017-08-29 18:24:05 +0200 | [diff] [blame] | 183 | sgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF); |
| 184 | rk_clrsetreg(&sgrf->ddr_rgn_con[16], 0x1ff, 0); |
| 185 | rk_clrreg(&sgrf->slv_secure_con4, 0x2000); |
| 186 | |
| 187 | /* eMMC clock generator: disable the clock multipilier */ |
| 188 | grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 189 | rk_clrreg(&grf->emmccore_con[11], 0x0ff); |
Philipp Tomsich | 504b9f1 | 2017-03-29 21:20:28 +0200 | [diff] [blame] | 190 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 191 | secure_timer_init(); |
| 192 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 193 | ret = uclass_get_device(UCLASS_RAM, 0, &dev); |
| 194 | if (ret) { |
Philipp Tomsich | adbca53 | 2018-11-19 13:03:51 +0100 | [diff] [blame] | 195 | pr_err("DRAM init failed: %d\n", ret); |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 196 | return; |
| 197 | } |
| 198 | } |
| 199 | |
Jagan Teki | 07586ee | 2019-06-21 00:25:00 +0530 | [diff] [blame] | 200 | #if defined(SPL_GPIO_SUPPORT) |
| 201 | static void rk3399_force_power_on_reset(void) |
| 202 | { |
| 203 | ofnode node; |
| 204 | struct gpio_desc sysreset_gpio; |
| 205 | |
| 206 | debug("%s: trying to force a power-on reset\n", __func__); |
| 207 | |
| 208 | node = ofnode_path("/config"); |
| 209 | if (!ofnode_valid(node)) { |
| 210 | debug("%s: no /config node?\n", __func__); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0, |
| 215 | &sysreset_gpio, GPIOD_IS_OUT)) { |
| 216 | debug("%s: could not find a /config/sysreset-gpio\n", __func__); |
| 217 | return; |
| 218 | } |
| 219 | |
| 220 | dm_gpio_set_value(&sysreset_gpio, 1); |
| 221 | } |
| 222 | #endif |
| 223 | |
| 224 | void spl_board_init(void) |
| 225 | { |
| 226 | #if defined(SPL_GPIO_SUPPORT) |
| 227 | struct rk3399_cru *cru = rockchip_get_cru(); |
| 228 | |
| 229 | /* |
| 230 | * The RK3399 resets only 'almost all logic' (see also in the TRM |
| 231 | * "3.9.4 Global software reset"), when issuing a software reset. |
| 232 | * This may cause issues during boot-up for some configurations of |
| 233 | * the application software stack. |
| 234 | * |
| 235 | * To work around this, we test whether the last reset reason was |
| 236 | * a power-on reset and (if not) issue an overtemp-reset to reset |
| 237 | * the entire module. |
| 238 | * |
| 239 | * While this was previously fixed by modifying the various places |
| 240 | * that could generate a software reset (e.g. U-Boot's sysreset |
| 241 | * driver, the ATF or Linux), we now have it here to ensure that |
| 242 | * we no longer have to track this through the various components. |
| 243 | */ |
| 244 | if (cru->glb_rst_st != 0) |
| 245 | rk3399_force_power_on_reset(); |
| 246 | #endif |
| 247 | |
| 248 | #if defined(SPL_DM_REGULATOR) |
| 249 | /* |
| 250 | * Turning the eMMC and SPI back on (if disabled via the Qseven |
| 251 | * BIOS_ENABLE) signal is done through a always-on regulator). |
| 252 | */ |
| 253 | if (regulators_enable_boot_on(false)) |
| 254 | debug("%s: Cannot enable boot on regulator\n", __func__); |
| 255 | #endif |
| 256 | |
| 257 | preloader_console_init(); |
| 258 | } |
| 259 | |
Kever Yang | 3012a84 | 2017-02-23 16:09:05 +0800 | [diff] [blame] | 260 | #ifdef CONFIG_SPL_LOAD_FIT |
| 261 | int board_fit_config_name_match(const char *name) |
| 262 | { |
| 263 | /* Just empty function now - can't decide what to choose */ |
| 264 | debug("%s: %s\n", __func__, name); |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | #endif |