blob: aaa7246393bb7a9e4535cbe69fc584b1cd38061f [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>
Kever Yang47b0ead2019-07-22 19:59:39 +08009#include <spl.h>
Kever Yang15f09a12019-03-28 11:01:23 +080010#include <spl_gpio.h>
Kever Yang47b0ead2019-07-22 19:59:39 +080011#include <syscon.h>
Kever Yanga381bcf2016-07-19 21:16:59 +080012#include <asm/armv8/mmu.h>
Kever Yang27b95d22016-10-07 15:56:16 +080013#include <asm/io.h>
Kever Yang4e1aeb82019-07-22 19:59:40 +080014#include <asm/arch-rockchip/bootrom.h>
Kever Yang47b0ead2019-07-22 19:59:39 +080015#include <asm/arch-rockchip/clock.h>
Philipp Tomsich8c5805a2019-04-29 19:05:26 +020016#include <asm/arch-rockchip/gpio.h>
Kever Yangf9e81452019-03-29 09:09:06 +080017#include <asm/arch-rockchip/grf_rk3399.h>
Kever Yang15f09a12019-03-28 11:01:23 +080018#include <asm/arch-rockchip/hardware.h>
Kever Yang47b0ead2019-07-22 19:59:39 +080019#include <power/regulator.h>
Kever Yang27b95d22016-10-07 15:56:16 +080020
Kever Yang975e4ab2017-06-23 16:11:11 +080021DECLARE_GLOBAL_DATA_PTR;
22
Kever Yang27b95d22016-10-07 15:56:16 +080023#define GRF_EMMCCORE_CON11 0xff77f02c
Kever Yangf9e81452019-03-29 09:09:06 +080024#define GRF_BASE 0xff770000
Kever Yanga381bcf2016-07-19 21:16:59 +080025
Kever Yang4e1aeb82019-07-22 19:59:40 +080026const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = {
27 [BROM_BOOTSOURCE_EMMC] = "/sdhci@fe330000",
28 [BROM_BOOTSOURCE_SPINOR] = "/spi@ff1d0000",
29 [BROM_BOOTSOURCE_SD] = "/dwmmc@fe320000",
30};
31
Kever Yanga381bcf2016-07-19 21:16:59 +080032static struct mm_region rk3399_mem_map[] = {
33 {
34 .virt = 0x0UL,
35 .phys = 0x0UL,
Kever Yang90c91272017-04-17 16:42:44 +080036 .size = 0xf8000000UL,
Kever Yanga381bcf2016-07-19 21:16:59 +080037 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
38 PTE_BLOCK_INNER_SHARE
39 }, {
Kever Yang90c91272017-04-17 16:42:44 +080040 .virt = 0xf8000000UL,
41 .phys = 0xf8000000UL,
42 .size = 0x08000000UL,
Kever Yanga381bcf2016-07-19 21:16:59 +080043 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
44 PTE_BLOCK_NON_SHARE |
45 PTE_BLOCK_PXN | PTE_BLOCK_UXN
46 }, {
47 /* List terminator */
48 0,
49 }
50};
51
52struct mm_region *mem_map = rk3399_mem_map;
Kever Yang27b95d22016-10-07 15:56:16 +080053
Kever Yang87ac5502019-07-09 22:05:59 +080054#ifdef CONFIG_SPL_BUILD
55
56#define TIMER_END_COUNT_L 0x00
57#define TIMER_END_COUNT_H 0x04
58#define TIMER_INIT_COUNT_L 0x10
59#define TIMER_INIT_COUNT_H 0x14
60#define TIMER_CONTROL_REG 0x1c
61
62#define TIMER_EN 0x1
63#define TIMER_FMODE BIT(0)
64#define TIMER_RMODE BIT(1)
65
66void rockchip_stimer_init(void)
67{
68 /* If Timer already enabled, don't re-init it */
69 u32 reg = readl(CONFIG_ROCKCHIP_STIMER_BASE + TIMER_CONTROL_REG);
70
71 if (reg & TIMER_EN)
72 return;
73
74 writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_L);
75 writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_H);
76 writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_L);
77 writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_H);
78 writel(TIMER_EN | TIMER_FMODE, CONFIG_ROCKCHIP_STIMER_BASE + \
79 TIMER_CONTROL_REG);
80}
81#endif
82
Kever Yang27b95d22016-10-07 15:56:16 +080083int arch_cpu_init(void)
84{
Kever Yang27b95d22016-10-07 15:56:16 +080085
Kever Yangbd06a7c2019-07-22 19:59:38 +080086#ifdef CONFIG_SPL_BUILD
87 struct rk3399_pmusgrf_regs *sgrf;
88 struct rk3399_grf_regs *grf;
89
90 /*
91 * Disable DDR and SRAM security regions.
92 *
93 * As we are entered from the BootROM, the region from
94 * 0x0 through 0xfffff (i.e. the first MB of memory) will
95 * be protected. This will cause issues with the DW_MMC
96 * driver, which tries to DMA from/to the stack (likely)
97 * located in this range.
98 */
99 sgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
100 rk_clrsetreg(&sgrf->ddr_rgn_con[16], 0x1ff, 0);
101 rk_clrreg(&sgrf->slv_secure_con4, 0x2000);
102
103 /* eMMC clock generator: disable the clock multipilier */
104 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Kever Yangf9e81452019-03-29 09:09:06 +0800105 rk_clrreg(&grf->emmccore_con[11], 0x0ff);
Kever Yangbd06a7c2019-07-22 19:59:38 +0800106#endif
Kever Yang27b95d22016-10-07 15:56:16 +0800107
108 return 0;
109}
Kever Yangc79bce12019-03-29 09:09:07 +0800110
111#ifdef CONFIG_DEBUG_UART_BOARD_INIT
112void board_debug_uart_init(void)
113{
114#define GRF_BASE 0xff770000
115#define GPIO0_BASE 0xff720000
116#define PMUGRF_BASE 0xff320000
117 struct rk3399_grf_regs * const grf = (void *)GRF_BASE;
118#ifdef CONFIG_TARGET_CHROMEBOOK_BOB
119 struct rk3399_pmugrf_regs * const pmugrf = (void *)PMUGRF_BASE;
120 struct rockchip_gpio_regs * const gpio = (void *)GPIO0_BASE;
121#endif
122
123#if defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff180000)
124 /* Enable early UART0 on the RK3399 */
125 rk_clrsetreg(&grf->gpio2c_iomux,
126 GRF_GPIO2C0_SEL_MASK,
127 GRF_UART0BT_SIN << GRF_GPIO2C0_SEL_SHIFT);
128 rk_clrsetreg(&grf->gpio2c_iomux,
129 GRF_GPIO2C1_SEL_MASK,
130 GRF_UART0BT_SOUT << GRF_GPIO2C1_SEL_SHIFT);
Christoph Muellner78a1ac32019-05-07 10:58:43 +0200131#elif defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff1B0000)
132 /* Enable early UART3 on the RK3399 */
133 rk_clrsetreg(&grf->gpio3b_iomux,
134 GRF_GPIO3B6_SEL_MASK,
135 GRF_UART3_SIN << GRF_GPIO3B6_SEL_SHIFT);
136 rk_clrsetreg(&grf->gpio3b_iomux,
137 GRF_GPIO3B7_SEL_MASK,
138 GRF_UART3_SOUT << GRF_GPIO3B7_SEL_SHIFT);
Kever Yangc79bce12019-03-29 09:09:07 +0800139#else
140# ifdef CONFIG_TARGET_CHROMEBOOK_BOB
141 rk_setreg(&grf->io_vsel, 1 << 0);
142
143 /*
144 * Let's enable these power rails here, we are already running the SPI
145 * Flash based code.
146 */
147 spl_gpio_output(gpio, GPIO(BANK_B, 2), 1); /* PP1500_EN */
148 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 2), GPIO_PULL_NORMAL);
149
150 spl_gpio_output(gpio, GPIO(BANK_B, 4), 1); /* PP3000_EN */
151 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 4), GPIO_PULL_NORMAL);
152#endif /* CONFIG_TARGET_CHROMEBOOK_BOB */
153
154 /* Enable early UART2 channel C on the RK3399 */
155 rk_clrsetreg(&grf->gpio4c_iomux,
156 GRF_GPIO4C3_SEL_MASK,
157 GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT);
158 rk_clrsetreg(&grf->gpio4c_iomux,
159 GRF_GPIO4C4_SEL_MASK,
160 GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT);
161 /* Set channel C as UART2 input */
162 rk_clrsetreg(&grf->soc_con7,
163 GRF_UART_DBG_SEL_MASK,
164 GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT);
165#endif
166}
167#endif
Kever Yang4238e522019-07-22 19:59:36 +0800168
Kever Yang47b0ead2019-07-22 19:59:39 +0800169#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
Kever Yang4238e522019-07-22 19:59:36 +0800170const char *spl_decode_boot_device(u32 boot_device)
171{
172 int i;
173 static const struct {
174 u32 boot_device;
175 const char *ofpath;
176 } spl_boot_devices_tbl[] = {
177 { BOOT_DEVICE_MMC1, "/dwmmc@fe320000" },
178 { BOOT_DEVICE_MMC2, "/sdhci@fe330000" },
179 { BOOT_DEVICE_SPI, "/spi@ff1d0000" },
180 };
181
182 for (i = 0; i < ARRAY_SIZE(spl_boot_devices_tbl); ++i)
183 if (spl_boot_devices_tbl[i].boot_device == boot_device)
184 return spl_boot_devices_tbl[i].ofpath;
185
186 return NULL;
187}
188
189void spl_perform_fixups(struct spl_image_info *spl_image)
190{
191 void *blob = spl_image->fdt_addr;
192 const char *boot_ofpath;
193 int chosen;
194
195 /*
196 * Inject the ofpath of the device the full U-Boot (or Linux in
197 * Falcon-mode) was booted from into the FDT, if a FDT has been
198 * loaded at the same time.
199 */
200 if (!blob)
201 return;
202
203 boot_ofpath = spl_decode_boot_device(spl_image->boot_device);
204 if (!boot_ofpath) {
205 pr_err("%s: could not map boot_device to ofpath\n", __func__);
206 return;
207 }
208
209 chosen = fdt_find_or_add_subnode(blob, 0, "chosen");
210 if (chosen < 0) {
211 pr_err("%s: could not find/create '/chosen'\n", __func__);
212 return;
213 }
214 fdt_setprop_string(blob, chosen,
215 "u-boot,spl-boot-device", boot_ofpath);
216}
Kever Yang47b0ead2019-07-22 19:59:39 +0800217
218#if defined(SPL_GPIO_SUPPORT)
219static void rk3399_force_power_on_reset(void)
220{
221 ofnode node;
222 struct gpio_desc sysreset_gpio;
223
224 debug("%s: trying to force a power-on reset\n", __func__);
225
226 node = ofnode_path("/config");
227 if (!ofnode_valid(node)) {
228 debug("%s: no /config node?\n", __func__);
229 return;
230 }
231
232 if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
233 &sysreset_gpio, GPIOD_IS_OUT)) {
234 debug("%s: could not find a /config/sysreset-gpio\n", __func__);
235 return;
236 }
237
238 dm_gpio_set_value(&sysreset_gpio, 1);
239}
240#endif
241
242void spl_board_init(void)
243{
244#if defined(SPL_GPIO_SUPPORT)
Jagan Tekib52a1992020-01-09 14:22:17 +0530245 struct rockchip_cru *cru = rockchip_get_cru();
Kever Yang47b0ead2019-07-22 19:59:39 +0800246
247 /*
248 * The RK3399 resets only 'almost all logic' (see also in the TRM
249 * "3.9.4 Global software reset"), when issuing a software reset.
250 * This may cause issues during boot-up for some configurations of
251 * the application software stack.
252 *
253 * To work around this, we test whether the last reset reason was
254 * a power-on reset and (if not) issue an overtemp-reset to reset
255 * the entire module.
256 *
257 * While this was previously fixed by modifying the various places
258 * that could generate a software reset (e.g. U-Boot's sysreset
259 * driver, the ATF or Linux), we now have it here to ensure that
260 * we no longer have to track this through the various components.
261 */
262 if (cru->glb_rst_st != 0)
263 rk3399_force_power_on_reset();
264#endif
265
266#if defined(SPL_DM_REGULATOR)
267 /*
268 * Turning the eMMC and SPI back on (if disabled via the Qseven
269 * BIOS_ENABLE) signal is done through a always-on regulator).
270 */
271 if (regulators_enable_boot_on(false))
272 debug("%s: Cannot enable boot on regulator\n", __func__);
273#endif
274}
Kever Yang4238e522019-07-22 19:59:36 +0800275#endif