blob: 7fa1d7c7b7af93bc2fa5457356fb8a677f2b7063 [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
Tom Riniabb9a042024-05-18 20:20:43 -06006#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>
Kever Yang243c0d32019-07-22 19:59:40 +080014#include <asm/arch-rockchip/bootrom.h>
Kever Yang7afd3852019-07-22 19:59:39 +080015#include <asm/arch-rockchip/clock.h>
Quentin Schulz65e713f2022-07-22 11:30:14 +020016#include <asm/arch-rockchip/cru.h>
Philipp Tomsichc3ee4622019-04-29 19:05:26 +020017#include <asm/arch-rockchip/gpio.h>
Kever Yang91379d92019-03-29 09:09:06 +080018#include <asm/arch-rockchip/grf_rk3399.h>
Kever Yang9fbe17c2019-03-28 11:01:23 +080019#include <asm/arch-rockchip/hardware.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060021#include <linux/printk.h>
Kever Yang7afd3852019-07-22 19:59:39 +080022#include <power/regulator.h>
Kever Yangf3ea0462016-10-07 15:56:16 +080023
24#define GRF_EMMCCORE_CON11 0xff77f02c
Kever Yang91379d92019-03-29 09:09:06 +080025#define GRF_BASE 0xff770000
Kever Yang0d3d7832016-07-19 21:16:59 +080026
Kever Yang243c0d32019-07-22 19:59:40 +080027const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = {
Quentin Schulzf2da9d62022-07-11 16:15:33 +020028 [BROM_BOOTSOURCE_EMMC] = "/mmc@fe330000",
Artem Lapkin103a1662021-05-26 17:32:27 +080029 [BROM_BOOTSOURCE_SPINOR] = "/spi@ff1d0000/flash@0",
Jagan Teki95b2b3e2020-05-24 20:26:18 +053030 [BROM_BOOTSOURCE_SD] = "/mmc@fe320000",
Kever Yang243c0d32019-07-22 19:59:40 +080031};
32
Kever Yang0d3d7832016-07-19 21:16:59 +080033static struct mm_region rk3399_mem_map[] = {
34 {
35 .virt = 0x0UL,
36 .phys = 0x0UL,
Kever Yangda77e492017-04-17 16:42:44 +080037 .size = 0xf8000000UL,
Kever Yang0d3d7832016-07-19 21:16:59 +080038 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
39 PTE_BLOCK_INNER_SHARE
40 }, {
Kever Yangda77e492017-04-17 16:42:44 +080041 .virt = 0xf8000000UL,
42 .phys = 0xf8000000UL,
43 .size = 0x08000000UL,
Kever Yang0d3d7832016-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 Yangf3ea0462016-10-07 15:56:16 +080054
Kever Yange937a992019-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 Yangf3ea0462016-10-07 15:56:16 +080084int arch_cpu_init(void)
85{
Kever Yangf3ea0462016-10-07 15:56:16 +080086
Kever Yangbeb30732019-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 Yang91379d92019-03-29 09:09:06 +0800106 rk_clrreg(&grf->emmccore_con[11], 0x0ff);
Kever Yangbeb30732019-07-22 19:59:38 +0800107#endif
Kever Yangf3ea0462016-10-07 15:56:16 +0800108
109 return 0;
110}
Kever Yang0f7c8242019-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;
Kever Yang0f7c8242019-03-29 09:09:07 +0800119
120#if defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff180000)
121 /* Enable early UART0 on the RK3399 */
122 rk_clrsetreg(&grf->gpio2c_iomux,
123 GRF_GPIO2C0_SEL_MASK,
124 GRF_UART0BT_SIN << GRF_GPIO2C0_SEL_SHIFT);
125 rk_clrsetreg(&grf->gpio2c_iomux,
126 GRF_GPIO2C1_SEL_MASK,
127 GRF_UART0BT_SOUT << GRF_GPIO2C1_SEL_SHIFT);
Christoph Muellnerfca44762019-05-07 10:58:43 +0200128#elif defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff1B0000)
129 /* Enable early UART3 on the RK3399 */
130 rk_clrsetreg(&grf->gpio3b_iomux,
131 GRF_GPIO3B6_SEL_MASK,
132 GRF_UART3_SIN << GRF_GPIO3B6_SEL_SHIFT);
133 rk_clrsetreg(&grf->gpio3b_iomux,
134 GRF_GPIO3B7_SEL_MASK,
135 GRF_UART3_SOUT << GRF_GPIO3B7_SEL_SHIFT);
Kever Yang0f7c8242019-03-29 09:09:07 +0800136#else
Simon Glassb247d022021-11-03 07:16:08 -0600137 struct rk3399_pmugrf_regs * const pmugrf = (void *)PMUGRF_BASE;
138 struct rockchip_gpio_regs * const gpio = (void *)GPIO0_BASE;
139
140 if (IS_ENABLED(CONFIG_SPL_BUILD) &&
Marty E. Plummerb20a8dac2021-12-24 16:43:46 +0300141 (IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_BOB) ||
142 IS_ENABLED(CONFIG_TARGET_CHROMEBOOK_KEVIN))) {
Simon Glassb247d022021-11-03 07:16:08 -0600143 rk_setreg(&grf->io_vsel, 1 << 0);
Kever Yang0f7c8242019-03-29 09:09:07 +0800144
Simon Glassb247d022021-11-03 07:16:08 -0600145 /*
146 * Let's enable these power rails here, we are already running
147 * the SPI-Flash-based code.
148 */
149 spl_gpio_output(gpio, GPIO(BANK_B, 2), 1); /* PP1500_EN */
150 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 2),
151 GPIO_PULL_NORMAL);
Kever Yang0f7c8242019-03-29 09:09:07 +0800152
Simon Glassb247d022021-11-03 07:16:08 -0600153 spl_gpio_output(gpio, GPIO(BANK_B, 4), 1); /* PP3000_EN */
154 spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 4),
155 GPIO_PULL_NORMAL);
156 }
Kever Yang0f7c8242019-03-29 09:09:07 +0800157
158 /* Enable early UART2 channel C on the RK3399 */
159 rk_clrsetreg(&grf->gpio4c_iomux,
160 GRF_GPIO4C3_SEL_MASK,
161 GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT);
162 rk_clrsetreg(&grf->gpio4c_iomux,
163 GRF_GPIO4C4_SEL_MASK,
164 GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT);
165 /* Set channel C as UART2 input */
166 rk_clrsetreg(&grf->soc_con7,
167 GRF_UART_DBG_SEL_MASK,
168 GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT);
169#endif
170}
171#endif
Kever Yange5a59612019-07-22 19:59:36 +0800172
Kever Yang7afd3852019-07-22 19:59:39 +0800173#if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
Kever Yang7afd3852019-07-22 19:59:39 +0800174static void rk3399_force_power_on_reset(void)
175{
176 ofnode node;
177 struct gpio_desc sysreset_gpio;
178
Quentin Schulz65e713f2022-07-22 11:30:14 +0200179 if (!IS_ENABLED(CONFIG_SPL_GPIO)) {
180 debug("%s: trying to force a power-on reset but no GPIO "
181 "support in SPL!\n", __func__);
182 return;
183 }
184
Kever Yang7afd3852019-07-22 19:59:39 +0800185 debug("%s: trying to force a power-on reset\n", __func__);
186
187 node = ofnode_path("/config");
188 if (!ofnode_valid(node)) {
189 debug("%s: no /config node?\n", __func__);
190 return;
191 }
192
193 if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
194 &sysreset_gpio, GPIOD_IS_OUT)) {
195 debug("%s: could not find a /config/sysreset-gpio\n", __func__);
196 return;
197 }
198
199 dm_gpio_set_value(&sysreset_gpio, 1);
200}
Kever Yang7afd3852019-07-22 19:59:39 +0800201
Jagan Tekib8536bc2020-07-21 20:36:00 +0530202void __weak led_setup(void)
203{
204}
205
Kever Yang7afd3852019-07-22 19:59:39 +0800206void spl_board_init(void)
207{
Jagan Tekib8536bc2020-07-21 20:36:00 +0530208 led_setup();
209
Quentin Schulz65e713f2022-07-22 11:30:14 +0200210 if (IS_ENABLED(CONFIG_SPL_GPIO)) {
211 struct rockchip_cru *cru = rockchip_get_cru();
Kever Yang7afd3852019-07-22 19:59:39 +0800212
Quentin Schulz65e713f2022-07-22 11:30:14 +0200213 /*
214 * The RK3399 resets only 'almost all logic' (see also in the
215 * TRM "3.9.4 Global software reset"), when issuing a software
216 * reset. This may cause issues during boot-up for some
217 * configurations of the application software stack.
218 *
219 * To work around this, we test whether the last reset reason
220 * was a power-on reset and (if not) issue an overtemp-reset to
221 * reset the entire module.
222 *
223 * While this was previously fixed by modifying the various
224 * places that could generate a software reset (e.g. U-Boot's
225 * sysreset driver, the ATF or Linux), we now have it here to
226 * ensure that we no longer have to track this through the
227 * various components.
228 */
229 if (cru->glb_rst_st != 0)
230 rk3399_force_power_on_reset();
231 }
Kever Yang7afd3852019-07-22 19:59:39 +0800232}
Kever Yange5a59612019-07-22 19:59:36 +0800233#endif