blob: 2333694caa0b7a5a3800b2dcc763044cffeb1be7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kever Yang441a6d32017-02-23 16:09:05 +08002/*
3 * (C) Copyright 2016 Rockchip Electronics Co., Ltd
Philipp Tomsich7e00d862017-09-11 12:48:12 +02004 * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
Kever Yang441a6d32017-02-23 16:09:05 +08005 */
6
7#include <common.h>
Simon Glassf473eba2019-01-21 14:53:31 -07008#include <debug_uart.h>
9#include <dm.h>
10#include <ram.h>
11#include <spl.h>
Simon Glass8fbf9922019-01-21 14:53:36 -070012#include <spl_gpio.h>
Simon Glassf473eba2019-01-21 14:53:31 -070013#include <syscon.h>
Jagan Teki74bde5d2019-06-21 00:25:00 +053014#include <asm/gpio.h>
Simon Glassf473eba2019-01-21 14:53:31 -070015#include <asm/io.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080016#include <asm/arch-rockchip/bootrom.h>
17#include <asm/arch-rockchip/clock.h>
Jagan Teki74bde5d2019-06-21 00:25:00 +053018#include <asm/arch-rockchip/cru_rk3399.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080019#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 Teki74bde5d2019-06-21 00:25:00 +053023#include <power/regulator.h>
Kever Yang441a6d32017-02-23 16:09:05 +080024
Philipp Tomsich7e00d862017-09-11 12:48:12 +020025void board_return_to_bootrom(void)
26{
Philipp Tomsich7234c732017-10-10 16:21:16 +020027 back_to_bootrom(BROM_BOOT_NEXTSTAGE);
Philipp Tomsich7e00d862017-09-11 12:48:12 +020028}
29
Philipp Tomsich425e7172017-09-29 19:27:58 +020030static 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
36const 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 Yang441a6d32017-02-23 16:09:05 +080054u32 spl_boot_device(void)
55{
Philipp Tomsich7e00d862017-09-11 12:48:12 +020056 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 Yang441a6d32017-02-23 16:09:05 +080062}
63
Philipp Tomsich2dde6f72018-05-24 17:15:52 +020064const 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
83void 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 Yang441a6d32017-02-23 16:09:05 +0800112#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
123void 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 Tomsich41029e62017-04-01 12:59:25 +0200132
Philipp Tomsich41029e62017-04-01 12:59:25 +0200133void board_init_f(ulong dummy)
134{
Philipp Tomsich41029e62017-04-01 12:59:25 +0200135 struct udevice *dev;
Philipp Tomsich7deb0982017-08-29 18:24:05 +0200136 struct rk3399_pmusgrf_regs *sgrf;
137 struct rk3399_grf_regs *grf;
Philipp Tomsich41029e62017-04-01 12:59:25 +0200138 int ret;
139
Kever Yang0f7c8242019-03-29 09:09:07 +0800140#ifdef CONFIG_DEBUG_UART
Philipp Tomsichbfd86052019-02-01 16:48:31 +0100141 debug_uart_init();
142
Simon Glass8fbf9922019-01-21 14:53:36 -0700143# ifdef CONFIG_TARGET_CHROMEBOOK_BOB
144 int sum, i;
145
Simon Glass8fbf9922019-01-21 14:53:36 -0700146 /*
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 Yang441a6d32017-02-23 16:09:05 +0800157 /*
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 Teki552d2812019-06-21 00:25:01 +0530165 debug("U-Boot SPL board init\n");
Kever Yang441a6d32017-02-23 16:09:05 +0800166#endif
Kever Yanga6697732017-05-05 11:01:43 +0800167
Kever Yange603a3d2017-03-20 14:47:16 +0800168 ret = spl_early_init();
Kever Yang441a6d32017-02-23 16:09:05 +0800169 if (ret) {
Kever Yange603a3d2017-03-20 14:47:16 +0800170 debug("spl_early_init() failed: %d\n", ret);
Kever Yang441a6d32017-02-23 16:09:05 +0800171 hang();
172 }
173
Philipp Tomsich2a34cbb2017-03-29 21:20:28 +0200174 /*
Kever Yanga6697732017-05-05 11:01:43 +0800175 * Disable DDR and SRAM security regions.
Philipp Tomsich2a34cbb2017-03-29 21:20:28 +0200176 *
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 Tomsich7deb0982017-08-29 18:24:05 +0200183 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 Tomsich2a34cbb2017-03-29 21:20:28 +0200190
Kever Yang441a6d32017-02-23 16:09:05 +0800191 secure_timer_init();
192
Kever Yang441a6d32017-02-23 16:09:05 +0800193 ret = uclass_get_device(UCLASS_RAM, 0, &dev);
194 if (ret) {
Philipp Tomsichc03a36c2018-11-19 13:03:51 +0100195 pr_err("DRAM init failed: %d\n", ret);
Kever Yang441a6d32017-02-23 16:09:05 +0800196 return;
197 }
198}
199
Jagan Teki74bde5d2019-06-21 00:25:00 +0530200#if defined(SPL_GPIO_SUPPORT)
201static 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
224void 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 Yang441a6d32017-02-23 16:09:05 +0800260#ifdef CONFIG_SPL_LOAD_FIT
261int 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