blob: d40969c88898348363470068044683c70cdd7441 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kever Yanga381bcf2016-07-19 21:16:59 +08002/*
3 * Copyright (c) 2016 Rockchip Electronics Co., Ltd
Kever Yanga381bcf2016-07-19 21:16:59 +08004 */
5
6#include <common.h>
Simon Glass4d72caa2020-05-10 11:40:01 -06007#include <fdt_support.h>
Simon Glass691d7192020-05-10 11:40:02 -06008#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Kever Yang47b0ead2019-07-22 19:59:39 +080010#include <spl.h>
Kever Yang15f09a12019-03-28 11:01:23 +080011#include <spl_gpio.h>
Kever Yang47b0ead2019-07-22 19:59:39 +080012#include <syscon.h>
Kever Yanga381bcf2016-07-19 21:16:59 +080013#include <asm/armv8/mmu.h>
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Kever Yang27b95d22016-10-07 15:56:16 +080015#include <asm/io.h>
Kever Yang4e1aeb82019-07-22 19:59:40 +080016#include <asm/arch-rockchip/bootrom.h>
Kever Yang47b0ead2019-07-22 19:59:39 +080017#include <asm/arch-rockchip/clock.h>
Philipp Tomsich8c5805a2019-04-29 19:05:26 +020018#include <asm/arch-rockchip/gpio.h>
Kever Yangf9e81452019-03-29 09:09:06 +080019#include <asm/arch-rockchip/grf_rk3399.h>
Kever Yang15f09a12019-03-28 11:01:23 +080020#include <asm/arch-rockchip/hardware.h>
Simon Glasscd93d622020-05-10 11:40:13 -060021#include <linux/bitops.h>
Kever Yang47b0ead2019-07-22 19:59:39 +080022#include <power/regulator.h>
Kever Yang27b95d22016-10-07 15:56:16 +080023
Kever Yang975e4ab2017-06-23 16:11:11 +080024DECLARE_GLOBAL_DATA_PTR;
25
Kever Yang27b95d22016-10-07 15:56:16 +080026#define GRF_EMMCCORE_CON11 0xff77f02c
Kever Yangf9e81452019-03-29 09:09:06 +080027#define GRF_BASE 0xff770000
Kever Yanga381bcf2016-07-19 21:16:59 +080028
Kever Yang4e1aeb82019-07-22 19:59:40 +080029const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = {
30 [BROM_BOOTSOURCE_EMMC] = "/sdhci@fe330000",
Artem Lapkine8a663c2021-05-26 17:32:27 +080031 [BROM_BOOTSOURCE_SPINOR] = "/spi@ff1d0000/flash@0",
Jagan Teki97de3932020-05-24 20:26:18 +053032 [BROM_BOOTSOURCE_SD] = "/mmc@fe320000",
Kever Yang4e1aeb82019-07-22 19:59:40 +080033};
34
Kever Yanga381bcf2016-07-19 21:16:59 +080035static struct mm_region rk3399_mem_map[] = {
36 {
37 .virt = 0x0UL,
38 .phys = 0x0UL,
Kever Yang90c91272017-04-17 16:42:44 +080039 .size = 0xf8000000UL,
Kever Yanga381bcf2016-07-19 21:16:59 +080040 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
41 PTE_BLOCK_INNER_SHARE
42 }, {
Kever Yang90c91272017-04-17 16:42:44 +080043 .virt = 0xf8000000UL,
44 .phys = 0xf8000000UL,
45 .size = 0x08000000UL,
Kever Yanga381bcf2016-07-19 21:16:59 +080046 .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
55struct mm_region *mem_map = rk3399_mem_map;
Kever Yang27b95d22016-10-07 15:56:16 +080056
Kever Yang87ac5502019-07-09 22:05:59 +080057#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
69void 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 Yang27b95d22016-10-07 15:56:16 +080086int arch_cpu_init(void)
87{
Kever Yang27b95d22016-10-07 15:56:16 +080088
Kever Yangbd06a7c2019-07-22 19:59:38 +080089#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 Yangf9e81452019-03-29 09:09:06 +0800108 rk_clrreg(&grf->emmccore_con[11], 0x0ff);
Kever Yangbd06a7c2019-07-22 19:59:38 +0800109#endif
Kever Yang27b95d22016-10-07 15:56:16 +0800110
111 return 0;
112}
Kever Yangc79bce12019-03-29 09:09:07 +0800113
114#ifdef CONFIG_DEBUG_UART_BOARD_INIT
115void 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 Yangc79bce12019-03-29 09:09:07 +0800121
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 Muellner78a1ac32019-05-07 10:58:43 +0200130#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 Yangc79bce12019-03-29 09:09:07 +0800138#else
Simon Glass55de0c12021-11-03 07:16:08 -0600139 struct rk3399_pmugrf_regs * const pmugrf = (void *)PMUGRF_BASE;
140 struct rockchip_gpio_regs * const gpio = (void *)GPIO0_BASE;
Kever Yangc79bce12019-03-29 09:09:07 +0800141
Simon Glass55de0c12021-11-03 07:16:08 -0600142 if (IS_ENABLED(CONFIG_SPL_BUILD) &&
143 IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_BOB)) {
144 rk_setreg(&grf->io_vsel, 1 << 0);
Kever Yangc79bce12019-03-29 09:09:07 +0800145
Simon Glass55de0c12021-11-03 07:16:08 -0600146 /*
147 * Let's enable these power rails here, we are already running
148 * the SPI-Flash-based code.
149 */
150 spl_gpio_output(gpio, GPIO(BANK_B, 2), 1); /* PP1500_EN */
151 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 2),
152 GPIO_PULL_NORMAL);
153
154 spl_gpio_output(gpio, GPIO(BANK_B, 4), 1); /* PP3000_EN */
155 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 4),
156 GPIO_PULL_NORMAL);
157 }
Kever Yangc79bce12019-03-29 09:09:07 +0800158
159 /* Enable early UART2 channel C on the RK3399 */
160 rk_clrsetreg(&grf->gpio4c_iomux,
161 GRF_GPIO4C3_SEL_MASK,
162 GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT);
163 rk_clrsetreg(&grf->gpio4c_iomux,
164 GRF_GPIO4C4_SEL_MASK,
165 GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT);
166 /* Set channel C as UART2 input */
167 rk_clrsetreg(&grf->soc_con7,
168 GRF_UART_DBG_SEL_MASK,
169 GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT);
170#endif
171}
172#endif
Kever Yang4238e522019-07-22 19:59:36 +0800173
Kever Yang47b0ead2019-07-22 19:59:39 +0800174#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
Kever Yang4238e522019-07-22 19:59:36 +0800175const char *spl_decode_boot_device(u32 boot_device)
176{
177 int i;
178 static const struct {
179 u32 boot_device;
180 const char *ofpath;
181 } spl_boot_devices_tbl[] = {
Jagan Teki97de3932020-05-24 20:26:18 +0530182 { BOOT_DEVICE_MMC1, "/mmc@fe320000" },
Kever Yang4238e522019-07-22 19:59:36 +0800183 { BOOT_DEVICE_MMC2, "/sdhci@fe330000" },
184 { BOOT_DEVICE_SPI, "/spi@ff1d0000" },
185 };
186
187 for (i = 0; i < ARRAY_SIZE(spl_boot_devices_tbl); ++i)
188 if (spl_boot_devices_tbl[i].boot_device == boot_device)
189 return spl_boot_devices_tbl[i].ofpath;
190
191 return NULL;
192}
193
194void spl_perform_fixups(struct spl_image_info *spl_image)
195{
196 void *blob = spl_image->fdt_addr;
197 const char *boot_ofpath;
198 int chosen;
199
200 /*
201 * Inject the ofpath of the device the full U-Boot (or Linux in
202 * Falcon-mode) was booted from into the FDT, if a FDT has been
203 * loaded at the same time.
204 */
205 if (!blob)
206 return;
207
208 boot_ofpath = spl_decode_boot_device(spl_image->boot_device);
209 if (!boot_ofpath) {
210 pr_err("%s: could not map boot_device to ofpath\n", __func__);
211 return;
212 }
213
214 chosen = fdt_find_or_add_subnode(blob, 0, "chosen");
215 if (chosen < 0) {
216 pr_err("%s: could not find/create '/chosen'\n", __func__);
217 return;
218 }
219 fdt_setprop_string(blob, chosen,
220 "u-boot,spl-boot-device", boot_ofpath);
221}
Kever Yang47b0ead2019-07-22 19:59:39 +0800222
Simon Glass83061db2021-07-10 21:14:30 -0600223#if defined(SPL_GPIO)
Kever Yang47b0ead2019-07-22 19:59:39 +0800224static void rk3399_force_power_on_reset(void)
225{
226 ofnode node;
227 struct gpio_desc sysreset_gpio;
228
229 debug("%s: trying to force a power-on reset\n", __func__);
230
231 node = ofnode_path("/config");
232 if (!ofnode_valid(node)) {
233 debug("%s: no /config node?\n", __func__);
234 return;
235 }
236
237 if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
238 &sysreset_gpio, GPIOD_IS_OUT)) {
239 debug("%s: could not find a /config/sysreset-gpio\n", __func__);
240 return;
241 }
242
243 dm_gpio_set_value(&sysreset_gpio, 1);
244}
245#endif
246
Jagan Teki500d1e72020-07-21 20:36:00 +0530247void __weak led_setup(void)
248{
249}
250
Kever Yang47b0ead2019-07-22 19:59:39 +0800251void spl_board_init(void)
252{
Jagan Teki500d1e72020-07-21 20:36:00 +0530253 led_setup();
254
Simon Glass83061db2021-07-10 21:14:30 -0600255#if defined(SPL_GPIO)
Jagan Tekib52a1992020-01-09 14:22:17 +0530256 struct rockchip_cru *cru = rockchip_get_cru();
Kever Yang47b0ead2019-07-22 19:59:39 +0800257
258 /*
259 * The RK3399 resets only 'almost all logic' (see also in the TRM
260 * "3.9.4 Global software reset"), when issuing a software reset.
261 * This may cause issues during boot-up for some configurations of
262 * the application software stack.
263 *
264 * To work around this, we test whether the last reset reason was
265 * a power-on reset and (if not) issue an overtemp-reset to reset
266 * the entire module.
267 *
268 * While this was previously fixed by modifying the various places
269 * that could generate a software reset (e.g. U-Boot's sysreset
270 * driver, the ATF or Linux), we now have it here to ensure that
271 * we no longer have to track this through the various components.
272 */
273 if (cru->glb_rst_st != 0)
274 rk3399_force_power_on_reset();
275#endif
276
277#if defined(SPL_DM_REGULATOR)
278 /*
279 * Turning the eMMC and SPI back on (if disabled via the Qseven
280 * BIOS_ENABLE) signal is done through a always-on regulator).
281 */
282 if (regulators_enable_boot_on(false))
283 debug("%s: Cannot enable boot on regulator\n", __func__);
284#endif
285}
Kever Yang4238e522019-07-22 19:59:36 +0800286#endif