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