blob: 14565d2ed9fabfd9e9034355d2b6306cc0e0f516 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kever Yang0d3d7832016-07-19 21:16:59 +08002/*
3 * Copyright (c) 2016 Rockchip Electronics Co., Ltd
Kever Yang0d3d7832016-07-19 21:16:59 +08004 */
5
6#include <common.h>
Simon Glass2dc9c342020-05-10 11:40:01 -06007#include <fdt_support.h>
Simon Glass97589732020-05-10 11:40:02 -06008#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Kever Yang7afd3852019-07-22 19:59:39 +080010#include <spl.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080011#include <spl_gpio.h>
Kever Yang7afd3852019-07-22 19:59:39 +080012#include <syscon.h>
Kever Yang0d3d7832016-07-19 21:16:59 +080013#include <asm/armv8/mmu.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Kever Yangf3ea0462016-10-07 15:56:16 +080015#include <asm/io.h>
Kever Yang243c0d32019-07-22 19:59:40 +080016#include <asm/arch-rockchip/bootrom.h>
Kever Yang7afd3852019-07-22 19:59:39 +080017#include <asm/arch-rockchip/clock.h>
Quentin Schulz65e713f2022-07-22 11:30:14 +020018#include <asm/arch-rockchip/cru.h>
Philipp Tomsichc3ee4622019-04-29 19:05:26 +020019#include <asm/arch-rockchip/gpio.h>
Kever Yang91379d92019-03-29 09:09:06 +080020#include <asm/arch-rockchip/grf_rk3399.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080021#include <asm/arch-rockchip/hardware.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060022#include <linux/bitops.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060023#include <linux/printk.h>
Kever Yang7afd3852019-07-22 19:59:39 +080024#include <power/regulator.h>
Kever Yangf3ea0462016-10-07 15:56:16 +080025
Kever Yangc2053262017-06-23 16:11:11 +080026DECLARE_GLOBAL_DATA_PTR;
27
Kever Yangf3ea0462016-10-07 15:56:16 +080028#define GRF_EMMCCORE_CON11 0xff77f02c
Kever Yang91379d92019-03-29 09:09:06 +080029#define GRF_BASE 0xff770000
Kever Yang0d3d7832016-07-19 21:16:59 +080030
Kever Yang243c0d32019-07-22 19:59:40 +080031const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = {
Quentin Schulzf2da9d62022-07-11 16:15:33 +020032 [BROM_BOOTSOURCE_EMMC] = "/mmc@fe330000",
Artem Lapkin103a1662021-05-26 17:32:27 +080033 [BROM_BOOTSOURCE_SPINOR] = "/spi@ff1d0000/flash@0",
Jagan Teki95b2b3e2020-05-24 20:26:18 +053034 [BROM_BOOTSOURCE_SD] = "/mmc@fe320000",
Kever Yang243c0d32019-07-22 19:59:40 +080035};
36
Kever Yang0d3d7832016-07-19 21:16:59 +080037static struct mm_region rk3399_mem_map[] = {
38 {
39 .virt = 0x0UL,
40 .phys = 0x0UL,
Kever Yangda77e492017-04-17 16:42:44 +080041 .size = 0xf8000000UL,
Kever Yang0d3d7832016-07-19 21:16:59 +080042 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
43 PTE_BLOCK_INNER_SHARE
44 }, {
Kever Yangda77e492017-04-17 16:42:44 +080045 .virt = 0xf8000000UL,
46 .phys = 0xf8000000UL,
47 .size = 0x08000000UL,
Kever Yang0d3d7832016-07-19 21:16:59 +080048 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
49 PTE_BLOCK_NON_SHARE |
50 PTE_BLOCK_PXN | PTE_BLOCK_UXN
51 }, {
52 /* List terminator */
53 0,
54 }
55};
56
57struct mm_region *mem_map = rk3399_mem_map;
Kever Yangf3ea0462016-10-07 15:56:16 +080058
Kever Yange937a992019-07-09 22:05:59 +080059#ifdef CONFIG_SPL_BUILD
60
61#define TIMER_END_COUNT_L 0x00
62#define TIMER_END_COUNT_H 0x04
63#define TIMER_INIT_COUNT_L 0x10
64#define TIMER_INIT_COUNT_H 0x14
65#define TIMER_CONTROL_REG 0x1c
66
67#define TIMER_EN 0x1
68#define TIMER_FMODE BIT(0)
69#define TIMER_RMODE BIT(1)
70
71void rockchip_stimer_init(void)
72{
73 /* If Timer already enabled, don't re-init it */
74 u32 reg = readl(CONFIG_ROCKCHIP_STIMER_BASE + TIMER_CONTROL_REG);
75
76 if (reg & TIMER_EN)
77 return;
78
79 writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_L);
80 writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_H);
81 writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_L);
82 writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_H);
83 writel(TIMER_EN | TIMER_FMODE, CONFIG_ROCKCHIP_STIMER_BASE + \
84 TIMER_CONTROL_REG);
85}
86#endif
87
Kever Yangf3ea0462016-10-07 15:56:16 +080088int arch_cpu_init(void)
89{
Kever Yangf3ea0462016-10-07 15:56:16 +080090
Kever Yangbeb30732019-07-22 19:59:38 +080091#ifdef CONFIG_SPL_BUILD
92 struct rk3399_pmusgrf_regs *sgrf;
93 struct rk3399_grf_regs *grf;
94
95 /*
96 * Disable DDR and SRAM security regions.
97 *
98 * As we are entered from the BootROM, the region from
99 * 0x0 through 0xfffff (i.e. the first MB of memory) will
100 * be protected. This will cause issues with the DW_MMC
101 * driver, which tries to DMA from/to the stack (likely)
102 * located in this range.
103 */
104 sgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
105 rk_clrsetreg(&sgrf->ddr_rgn_con[16], 0x1ff, 0);
106 rk_clrreg(&sgrf->slv_secure_con4, 0x2000);
107
108 /* eMMC clock generator: disable the clock multipilier */
109 grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
Kever Yang91379d92019-03-29 09:09:06 +0800110 rk_clrreg(&grf->emmccore_con[11], 0x0ff);
Kever Yangbeb30732019-07-22 19:59:38 +0800111#endif
Kever Yangf3ea0462016-10-07 15:56:16 +0800112
113 return 0;
114}
Kever Yang0f7c8242019-03-29 09:09:07 +0800115
116#ifdef CONFIG_DEBUG_UART_BOARD_INIT
117void board_debug_uart_init(void)
118{
119#define GRF_BASE 0xff770000
120#define GPIO0_BASE 0xff720000
121#define PMUGRF_BASE 0xff320000
122 struct rk3399_grf_regs * const grf = (void *)GRF_BASE;
Kever Yang0f7c8242019-03-29 09:09:07 +0800123
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 Muellnerfca44762019-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 Yang0f7c8242019-03-29 09:09:07 +0800140#else
Simon Glassb247d022021-11-03 07:16:08 -0600141 struct rk3399_pmugrf_regs * const pmugrf = (void *)PMUGRF_BASE;
142 struct rockchip_gpio_regs * const gpio = (void *)GPIO0_BASE;
143
144 if (IS_ENABLED(CONFIG_SPL_BUILD) &&
Marty E. Plummerb20a8dac2021-12-24 16:43:46 +0300145 (IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_BOB) ||
146 IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_KEVIN))) {
Simon Glassb247d022021-11-03 07:16:08 -0600147 rk_setreg(&grf->io_vsel, 1 << 0);
Kever Yang0f7c8242019-03-29 09:09:07 +0800148
Simon Glassb247d022021-11-03 07:16:08 -0600149 /*
150 * Let's enable these power rails here, we are already running
151 * the SPI-Flash-based code.
152 */
153 spl_gpio_output(gpio, GPIO(BANK_B, 2), 1); /* PP1500_EN */
154 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 2),
155 GPIO_PULL_NORMAL);
Kever Yang0f7c8242019-03-29 09:09:07 +0800156
Simon Glassb247d022021-11-03 07:16:08 -0600157 spl_gpio_output(gpio, GPIO(BANK_B, 4), 1); /* PP3000_EN */
158 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 4),
159 GPIO_PULL_NORMAL);
160 }
Kever Yang0f7c8242019-03-29 09:09:07 +0800161
162 /* Enable early UART2 channel C on the RK3399 */
163 rk_clrsetreg(&grf->gpio4c_iomux,
164 GRF_GPIO4C3_SEL_MASK,
165 GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT);
166 rk_clrsetreg(&grf->gpio4c_iomux,
167 GRF_GPIO4C4_SEL_MASK,
168 GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT);
169 /* Set channel C as UART2 input */
170 rk_clrsetreg(&grf->soc_con7,
171 GRF_UART_DBG_SEL_MASK,
172 GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT);
173#endif
174}
175#endif
Kever Yange5a59612019-07-22 19:59:36 +0800176
Kever Yang7afd3852019-07-22 19:59:39 +0800177#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
Kever Yange5a59612019-07-22 19:59:36 +0800178const char *spl_decode_boot_device(u32 boot_device)
179{
180 int i;
181 static const struct {
182 u32 boot_device;
183 const char *ofpath;
184 } spl_boot_devices_tbl[] = {
Quentin Schulz4592c642022-07-15 17:15:51 +0200185 { BOOT_DEVICE_MMC2, "/mmc@fe320000" },
186 { BOOT_DEVICE_MMC1, "/mmc@fe330000" },
Quentin Schulza52dfbd2022-07-15 17:15:52 +0200187 { BOOT_DEVICE_SPI, "/spi@ff1d0000/flash@0" },
Kever Yange5a59612019-07-22 19:59:36 +0800188 };
189
190 for (i = 0; i < ARRAY_SIZE(spl_boot_devices_tbl); ++i)
191 if (spl_boot_devices_tbl[i].boot_device == boot_device)
192 return spl_boot_devices_tbl[i].ofpath;
193
194 return NULL;
195}
196
197void spl_perform_fixups(struct spl_image_info *spl_image)
198{
199 void *blob = spl_image->fdt_addr;
200 const char *boot_ofpath;
201 int chosen;
202
203 /*
204 * Inject the ofpath of the device the full U-Boot (or Linux in
205 * Falcon-mode) was booted from into the FDT, if a FDT has been
206 * loaded at the same time.
207 */
208 if (!blob)
209 return;
210
211 boot_ofpath = spl_decode_boot_device(spl_image->boot_device);
212 if (!boot_ofpath) {
213 pr_err("%s: could not map boot_device to ofpath\n", __func__);
214 return;
215 }
216
217 chosen = fdt_find_or_add_subnode(blob, 0, "chosen");
218 if (chosen < 0) {
219 pr_err("%s: could not find/create '/chosen'\n", __func__);
220 return;
221 }
222 fdt_setprop_string(blob, chosen,
223 "u-boot,spl-boot-device", boot_ofpath);
224}
Kever Yang7afd3852019-07-22 19:59:39 +0800225
Kever Yang7afd3852019-07-22 19:59:39 +0800226static void rk3399_force_power_on_reset(void)
227{
228 ofnode node;
229 struct gpio_desc sysreset_gpio;
230
Quentin Schulz65e713f2022-07-22 11:30:14 +0200231 if (!IS_ENABLED(CONFIG_SPL_GPIO)) {
232 debug("%s: trying to force a power-on reset but no GPIO "
233 "support in SPL!\n", __func__);
234 return;
235 }
236
Kever Yang7afd3852019-07-22 19:59:39 +0800237 debug("%s: trying to force a power-on reset\n", __func__);
238
239 node = ofnode_path("/config");
240 if (!ofnode_valid(node)) {
241 debug("%s: no /config node?\n", __func__);
242 return;
243 }
244
245 if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
246 &sysreset_gpio, GPIOD_IS_OUT)) {
247 debug("%s: could not find a /config/sysreset-gpio\n", __func__);
248 return;
249 }
250
251 dm_gpio_set_value(&sysreset_gpio, 1);
252}
Kever Yang7afd3852019-07-22 19:59:39 +0800253
Jagan Tekib8536bc2020-07-21 20:36:00 +0530254void __weak led_setup(void)
255{
256}
257
Kever Yang7afd3852019-07-22 19:59:39 +0800258void spl_board_init(void)
259{
Jagan Tekib8536bc2020-07-21 20:36:00 +0530260 led_setup();
261
Quentin Schulz65e713f2022-07-22 11:30:14 +0200262 if (IS_ENABLED(CONFIG_SPL_GPIO)) {
263 struct rockchip_cru *cru = rockchip_get_cru();
Kever Yang7afd3852019-07-22 19:59:39 +0800264
Quentin Schulz65e713f2022-07-22 11:30:14 +0200265 /*
266 * The RK3399 resets only 'almost all logic' (see also in the
267 * TRM "3.9.4 Global software reset"), when issuing a software
268 * reset. This may cause issues during boot-up for some
269 * configurations of the application software stack.
270 *
271 * To work around this, we test whether the last reset reason
272 * was a power-on reset and (if not) issue an overtemp-reset to
273 * reset the entire module.
274 *
275 * While this was previously fixed by modifying the various
276 * places that could generate a software reset (e.g. U-Boot's
277 * sysreset driver, the ATF or Linux), we now have it here to
278 * ensure that we no longer have to track this through the
279 * various components.
280 */
281 if (cru->glb_rst_st != 0)
282 rk3399_force_power_on_reset();
283 }
Kever Yang7afd3852019-07-22 19:59:39 +0800284}
Kever Yange5a59612019-07-22 19:59:36 +0800285#endif