blob: 0c28241c603e343c5140322f2778678e95fa84fd [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
Simon Glass2dc9c342020-05-10 11:40:01 -06006#include <fdt_support.h>
Simon Glass97589732020-05-10 11:40:02 -06007#include <init.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Kever Yang7afd3852019-07-22 19:59:39 +08009#include <spl.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080010#include <spl_gpio.h>
Kever Yang7afd3852019-07-22 19:59:39 +080011#include <syscon.h>
Kever Yang0d3d7832016-07-19 21:16:59 +080012#include <asm/armv8/mmu.h>
Kever Yang243c0d32019-07-22 19:59:40 +080013#include <asm/arch-rockchip/bootrom.h>
Kever Yang7afd3852019-07-22 19:59:39 +080014#include <asm/arch-rockchip/clock.h>
Quentin Schulz65e713f2022-07-22 11:30:14 +020015#include <asm/arch-rockchip/cru.h>
Philipp Tomsichc3ee4622019-04-29 19:05:26 +020016#include <asm/arch-rockchip/gpio.h>
Kever Yang91379d92019-03-29 09:09:06 +080017#include <asm/arch-rockchip/grf_rk3399.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080018#include <asm/arch-rockchip/hardware.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060020#include <linux/printk.h>
Kever Yang7afd3852019-07-22 19:59:39 +080021#include <power/regulator.h>
Kever Yangf3ea0462016-10-07 15:56:16 +080022
23#define GRF_EMMCCORE_CON11 0xff77f02c
Kever Yang91379d92019-03-29 09:09:06 +080024#define GRF_BASE 0xff770000
Kever Yang0d3d7832016-07-19 21:16:59 +080025
Kever Yang243c0d32019-07-22 19:59:40 +080026const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = {
Quentin Schulzf2da9d62022-07-11 16:15:33 +020027 [BROM_BOOTSOURCE_EMMC] = "/mmc@fe330000",
Artem Lapkin103a1662021-05-26 17:32:27 +080028 [BROM_BOOTSOURCE_SPINOR] = "/spi@ff1d0000/flash@0",
Jagan Teki95b2b3e2020-05-24 20:26:18 +053029 [BROM_BOOTSOURCE_SD] = "/mmc@fe320000",
Kever Yang243c0d32019-07-22 19:59:40 +080030};
31
Kever Yang0d3d7832016-07-19 21:16:59 +080032static struct mm_region rk3399_mem_map[] = {
33 {
34 .virt = 0x0UL,
35 .phys = 0x0UL,
Kever Yangda77e492017-04-17 16:42:44 +080036 .size = 0xf8000000UL,
Kever Yang0d3d7832016-07-19 21:16:59 +080037 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
38 PTE_BLOCK_INNER_SHARE
39 }, {
Kever Yangda77e492017-04-17 16:42:44 +080040 .virt = 0xf8000000UL,
41 .phys = 0xf8000000UL,
42 .size = 0x08000000UL,
Kever Yang0d3d7832016-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 Yangf3ea0462016-10-07 15:56:16 +080053
Simon Glass85ed77d2024-09-29 19:49:46 -060054#ifdef CONFIG_XPL_BUILD
Kever Yange937a992019-07-09 22:05:59 +080055
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 Yangf3ea0462016-10-07 15:56:16 +080083int arch_cpu_init(void)
84{
Kever Yangf3ea0462016-10-07 15:56:16 +080085
Simon Glass85ed77d2024-09-29 19:49:46 -060086#ifdef CONFIG_XPL_BUILD
Kever Yangbeb30732019-07-22 19:59:38 +080087 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 Yang91379d92019-03-29 09:09:06 +0800105 rk_clrreg(&grf->emmccore_con[11], 0x0ff);
Kever Yangbeb30732019-07-22 19:59:38 +0800106#endif
Kever Yangf3ea0462016-10-07 15:56:16 +0800107
108 return 0;
109}
Kever Yang0f7c8242019-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;
Kever Yang0f7c8242019-03-29 09:09:07 +0800118
119#if defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff180000)
120 /* Enable early UART0 on the RK3399 */
121 rk_clrsetreg(&grf->gpio2c_iomux,
122 GRF_GPIO2C0_SEL_MASK,
123 GRF_UART0BT_SIN << GRF_GPIO2C0_SEL_SHIFT);
124 rk_clrsetreg(&grf->gpio2c_iomux,
125 GRF_GPIO2C1_SEL_MASK,
126 GRF_UART0BT_SOUT << GRF_GPIO2C1_SEL_SHIFT);
Christoph Muellnerfca44762019-05-07 10:58:43 +0200127#elif defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff1B0000)
128 /* Enable early UART3 on the RK3399 */
129 rk_clrsetreg(&grf->gpio3b_iomux,
130 GRF_GPIO3B6_SEL_MASK,
131 GRF_UART3_SIN << GRF_GPIO3B6_SEL_SHIFT);
132 rk_clrsetreg(&grf->gpio3b_iomux,
133 GRF_GPIO3B7_SEL_MASK,
134 GRF_UART3_SOUT << GRF_GPIO3B7_SEL_SHIFT);
Kever Yang0f7c8242019-03-29 09:09:07 +0800135#else
Simon Glassb247d022021-11-03 07:16:08 -0600136 struct rk3399_pmugrf_regs * const pmugrf = (void *)PMUGRF_BASE;
137 struct rockchip_gpio_regs * const gpio = (void *)GPIO0_BASE;
138
Simon Glass85ed77d2024-09-29 19:49:46 -0600139 if (IS_ENABLED(CONFIG_XPL_BUILD) &&
Marty E. Plummerb20a8dac2021-12-24 16:43:46 +0300140 (IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_BOB) ||
141 IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_KEVIN))) {
Simon Glassb247d022021-11-03 07:16:08 -0600142 rk_setreg(&grf->io_vsel, 1 << 0);
Kever Yang0f7c8242019-03-29 09:09:07 +0800143
Simon Glassb247d022021-11-03 07:16:08 -0600144 /*
145 * Let's enable these power rails here, we are already running
146 * the SPI-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),
150 GPIO_PULL_NORMAL);
Kever Yang0f7c8242019-03-29 09:09:07 +0800151
Simon Glassb247d022021-11-03 07:16:08 -0600152 spl_gpio_output(gpio, GPIO(BANK_B, 4), 1); /* PP3000_EN */
153 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 4),
154 GPIO_PULL_NORMAL);
155 }
Kever Yang0f7c8242019-03-29 09:09:07 +0800156
157 /* Enable early UART2 channel C on the RK3399 */
158 rk_clrsetreg(&grf->gpio4c_iomux,
159 GRF_GPIO4C3_SEL_MASK,
160 GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT);
161 rk_clrsetreg(&grf->gpio4c_iomux,
162 GRF_GPIO4C4_SEL_MASK,
163 GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT);
164 /* Set channel C as UART2 input */
165 rk_clrsetreg(&grf->soc_con7,
166 GRF_UART_DBG_SEL_MASK,
167 GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT);
168#endif
169}
170#endif
Kever Yange5a59612019-07-22 19:59:36 +0800171
Quentin Schulzea1361a2024-11-06 12:29:44 +0100172#if defined(CONFIG_XPL_BUILD)
173#if defined(CONFIG_TPL_BUILD)
Kever Yang7afd3852019-07-22 19:59:39 +0800174static void rk3399_force_power_on_reset(void)
175{
Quentin Schulz154654b2024-11-06 12:29:42 +0100176 const struct rockchip_cru *cru = rockchip_get_cru();
Kever Yang7afd3852019-07-22 19:59:39 +0800177 ofnode node;
178 struct gpio_desc sysreset_gpio;
179
Quentin Schulz154654b2024-11-06 12:29:42 +0100180 /*
181 * The RK3399 resets only 'almost all logic' (see also in the
182 * TRM "3.9.4 Global software reset"), when issuing a software
183 * reset. This may cause issues during boot-up for some
184 * configurations of the application software stack.
185 *
186 * To work around this, we test whether the last reset reason
187 * was a power-on reset and (if not) issue an overtemp-reset to
188 * reset the entire module.
189 *
190 * While this was previously fixed by modifying the various
191 * places that could generate a software reset (e.g. U-Boot's
192 * sysreset driver, the ATF or Linux), we now have it here to
193 * ensure that we no longer have to track this through the
194 * various components.
195 */
196 if (cru->glb_rst_st == 0)
197 return;
198
Quentin Schulzea1361a2024-11-06 12:29:44 +0100199 if (!IS_ENABLED(CONFIG_TPL_GPIO)) {
Quentin Schulz65e713f2022-07-22 11:30:14 +0200200 debug("%s: trying to force a power-on reset but no GPIO "
Quentin Schulzea1361a2024-11-06 12:29:44 +0100201 "support in TPL!\n", __func__);
Quentin Schulz65e713f2022-07-22 11:30:14 +0200202 return;
203 }
204
Kever Yang7afd3852019-07-22 19:59:39 +0800205 debug("%s: trying to force a power-on reset\n", __func__);
206
207 node = ofnode_path("/config");
208 if (!ofnode_valid(node)) {
209 debug("%s: no /config node?\n", __func__);
210 return;
211 }
212
213 if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
214 &sysreset_gpio, GPIOD_IS_OUT)) {
215 debug("%s: could not find a /config/sysreset-gpio\n", __func__);
216 return;
217 }
218
219 dm_gpio_set_value(&sysreset_gpio, 1);
220}
Kever Yang7afd3852019-07-22 19:59:39 +0800221
Quentin Schulzea1361a2024-11-06 12:29:44 +0100222void tpl_board_init(void)
223{
224 rk3399_force_power_on_reset();
225}
226# else
Jagan Tekib8536bc2020-07-21 20:36:00 +0530227void __weak led_setup(void)
228{
229}
230
Kever Yang7afd3852019-07-22 19:59:39 +0800231void spl_board_init(void)
232{
Jagan Tekib8536bc2020-07-21 20:36:00 +0530233 led_setup();
Kever Yang7afd3852019-07-22 19:59:39 +0800234}
Kever Yange5a59612019-07-22 19:59:36 +0800235#endif
Quentin Schulzea1361a2024-11-06 12:29:44 +0100236#endif