183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2a55e4971SPhilipp Tomsich /*
3a55e4971SPhilipp Tomsich * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
4a55e4971SPhilipp Tomsich */
5a55e4971SPhilipp Tomsich
6a55e4971SPhilipp Tomsich #include <common.h>
7a55e4971SPhilipp Tomsich #include <debug_uart.h>
8a55e4971SPhilipp Tomsich #include <dm.h>
9a55e4971SPhilipp Tomsich #include <ram.h>
10a55e4971SPhilipp Tomsich #include <spl.h>
11*c35f8e50SSimon Glass #include <syscon.h>
12a55e4971SPhilipp Tomsich #include <asm/io.h>
13a55e4971SPhilipp Tomsich #include <asm/arch/bootrom.h>
14*c35f8e50SSimon Glass #include <asm/arch/clock.h>
15a55e4971SPhilipp Tomsich #include <asm/arch/cru_rk3368.h>
16a55e4971SPhilipp Tomsich #include <asm/arch/grf_rk3368.h>
17a55e4971SPhilipp Tomsich #include <asm/arch/hardware.h>
18a55e4971SPhilipp Tomsich #include <asm/arch/timer.h>
19a55e4971SPhilipp Tomsich
20a55e4971SPhilipp Tomsich /*
21a55e4971SPhilipp Tomsich * The SPL (and also the full U-Boot stage on the RK3368) will run in
22a55e4971SPhilipp Tomsich * secure mode (i.e. EL3) and an ATF will eventually be booted before
23a55e4971SPhilipp Tomsich * starting up the operating system... so we can initialize the SGRF
24a55e4971SPhilipp Tomsich * here and rely on the ATF installing the final (secure) policy
25a55e4971SPhilipp Tomsich * later.
26a55e4971SPhilipp Tomsich */
sgrf_soc_con_addr(unsigned no)27a55e4971SPhilipp Tomsich static inline uintptr_t sgrf_soc_con_addr(unsigned no)
28a55e4971SPhilipp Tomsich {
29a55e4971SPhilipp Tomsich const uintptr_t SGRF_BASE =
30a55e4971SPhilipp Tomsich (uintptr_t)syscon_get_first_range(ROCKCHIP_SYSCON_SGRF);
31a55e4971SPhilipp Tomsich
32a55e4971SPhilipp Tomsich return SGRF_BASE + sizeof(u32) * no;
33a55e4971SPhilipp Tomsich }
34a55e4971SPhilipp Tomsich
sgrf_busdmac_addr(unsigned no)35a55e4971SPhilipp Tomsich static inline uintptr_t sgrf_busdmac_addr(unsigned no)
36a55e4971SPhilipp Tomsich {
37a55e4971SPhilipp Tomsich const uintptr_t SGRF_BASE =
38a55e4971SPhilipp Tomsich (uintptr_t)syscon_get_first_range(ROCKCHIP_SYSCON_SGRF);
39a55e4971SPhilipp Tomsich const uintptr_t SGRF_BUSDMAC_OFFSET = 0x100;
40a55e4971SPhilipp Tomsich const uintptr_t SGRF_BUSDMAC_BASE = SGRF_BASE + SGRF_BUSDMAC_OFFSET;
41a55e4971SPhilipp Tomsich
42a55e4971SPhilipp Tomsich return SGRF_BUSDMAC_BASE + sizeof(u32) * no;
43a55e4971SPhilipp Tomsich }
44a55e4971SPhilipp Tomsich
sgrf_init(void)45a55e4971SPhilipp Tomsich static void sgrf_init(void)
46a55e4971SPhilipp Tomsich {
47a55e4971SPhilipp Tomsich struct rk3368_cru * const cru =
48a55e4971SPhilipp Tomsich (struct rk3368_cru * const)rockchip_get_cru();
49a55e4971SPhilipp Tomsich const u16 SGRF_SOC_CON_SEC = GENMASK(15, 0);
50a55e4971SPhilipp Tomsich const u16 SGRF_BUSDMAC_CON0_SEC = BIT(2);
51a55e4971SPhilipp Tomsich const u16 SGRF_BUSDMAC_CON1_SEC = GENMASK(15, 12);
52a55e4971SPhilipp Tomsich
53a55e4971SPhilipp Tomsich /* Set all configurable IP to 'non secure'-mode */
54a55e4971SPhilipp Tomsich rk_setreg(sgrf_soc_con_addr(5), SGRF_SOC_CON_SEC);
55a55e4971SPhilipp Tomsich rk_setreg(sgrf_soc_con_addr(6), SGRF_SOC_CON_SEC);
56a55e4971SPhilipp Tomsich rk_setreg(sgrf_soc_con_addr(7), SGRF_SOC_CON_SEC);
57a55e4971SPhilipp Tomsich
58a55e4971SPhilipp Tomsich /*
59a55e4971SPhilipp Tomsich * From rockchip-uboot/arch/arm/cpu/armv8/rk33xx/cpu.c
60a55e4971SPhilipp Tomsich * Original comment: "ddr space set no secure mode"
61a55e4971SPhilipp Tomsich */
62a55e4971SPhilipp Tomsich rk_clrreg(sgrf_soc_con_addr(8), SGRF_SOC_CON_SEC);
63a55e4971SPhilipp Tomsich rk_clrreg(sgrf_soc_con_addr(9), SGRF_SOC_CON_SEC);
64a55e4971SPhilipp Tomsich rk_clrreg(sgrf_soc_con_addr(10), SGRF_SOC_CON_SEC);
65a55e4971SPhilipp Tomsich
66a55e4971SPhilipp Tomsich /* Set 'secure dma' to 'non secure'-mode */
67a55e4971SPhilipp Tomsich rk_setreg(sgrf_busdmac_addr(0), SGRF_BUSDMAC_CON0_SEC);
68a55e4971SPhilipp Tomsich rk_setreg(sgrf_busdmac_addr(1), SGRF_BUSDMAC_CON1_SEC);
69a55e4971SPhilipp Tomsich
70a55e4971SPhilipp Tomsich dsb(); /* barrier */
71a55e4971SPhilipp Tomsich
72a55e4971SPhilipp Tomsich rk_setreg(&cru->softrst_con[1], DMA1_SRST_REQ);
73a55e4971SPhilipp Tomsich rk_setreg(&cru->softrst_con[4], DMA2_SRST_REQ);
74a55e4971SPhilipp Tomsich
75a55e4971SPhilipp Tomsich dsb(); /* barrier */
76a55e4971SPhilipp Tomsich udelay(10);
77a55e4971SPhilipp Tomsich
78a55e4971SPhilipp Tomsich rk_clrreg(&cru->softrst_con[1], DMA1_SRST_REQ);
79a55e4971SPhilipp Tomsich rk_clrreg(&cru->softrst_con[4], DMA2_SRST_REQ);
80a55e4971SPhilipp Tomsich }
81a55e4971SPhilipp Tomsich
board_debug_uart_init(void)82a55e4971SPhilipp Tomsich void board_debug_uart_init(void)
83a55e4971SPhilipp Tomsich {
84a55e4971SPhilipp Tomsich /*
85a55e4971SPhilipp Tomsich * N.B.: This is called before the device-model has been
86a55e4971SPhilipp Tomsich * initialised. For this reason, we can not access
87a55e4971SPhilipp Tomsich * the GRF address range using the syscon API.
88a55e4971SPhilipp Tomsich */
89a55e4971SPhilipp Tomsich struct rk3368_grf * const grf =
90a55e4971SPhilipp Tomsich (struct rk3368_grf * const)0xff770000;
91a55e4971SPhilipp Tomsich
92a55e4971SPhilipp Tomsich enum {
93a55e4971SPhilipp Tomsich GPIO2D1_MASK = GENMASK(3, 2),
94a55e4971SPhilipp Tomsich GPIO2D1_GPIO = 0,
95a55e4971SPhilipp Tomsich GPIO2D1_UART0_SOUT = (1 << 2),
96a55e4971SPhilipp Tomsich
97a55e4971SPhilipp Tomsich GPIO2D0_MASK = GENMASK(1, 0),
98a55e4971SPhilipp Tomsich GPIO2D0_GPIO = 0,
99a55e4971SPhilipp Tomsich GPIO2D0_UART0_SIN = (1 << 0),
100a55e4971SPhilipp Tomsich };
101a55e4971SPhilipp Tomsich
102a55e4971SPhilipp Tomsich #if defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff180000)
103a55e4971SPhilipp Tomsich /* Enable early UART0 on the RK3368 */
104a55e4971SPhilipp Tomsich rk_clrsetreg(&grf->gpio2d_iomux,
105a55e4971SPhilipp Tomsich GPIO2D0_MASK, GPIO2D0_UART0_SIN);
106a55e4971SPhilipp Tomsich rk_clrsetreg(&grf->gpio2d_iomux,
107a55e4971SPhilipp Tomsich GPIO2D1_MASK, GPIO2D1_UART0_SOUT);
108a55e4971SPhilipp Tomsich #endif
109a55e4971SPhilipp Tomsich }
110a55e4971SPhilipp Tomsich
board_init_f(ulong dummy)111a55e4971SPhilipp Tomsich void board_init_f(ulong dummy)
112a55e4971SPhilipp Tomsich {
113a55e4971SPhilipp Tomsich struct udevice *dev;
114a55e4971SPhilipp Tomsich int ret;
115a55e4971SPhilipp Tomsich
116a55e4971SPhilipp Tomsich #define EARLY_UART
117a55e4971SPhilipp Tomsich #ifdef EARLY_UART
118a55e4971SPhilipp Tomsich /*
119a55e4971SPhilipp Tomsich * Debug UART can be used from here if required:
120a55e4971SPhilipp Tomsich *
121a55e4971SPhilipp Tomsich * debug_uart_init();
122a55e4971SPhilipp Tomsich * printch('a');
123a55e4971SPhilipp Tomsich * printhex8(0x1234);
124a55e4971SPhilipp Tomsich * printascii("string");
125a55e4971SPhilipp Tomsich */
126a55e4971SPhilipp Tomsich debug_uart_init();
127a55e4971SPhilipp Tomsich printascii("U-Boot TPL board init\n");
128a55e4971SPhilipp Tomsich #endif
129a55e4971SPhilipp Tomsich
130a55e4971SPhilipp Tomsich ret = spl_early_init();
131a55e4971SPhilipp Tomsich if (ret) {
132a55e4971SPhilipp Tomsich debug("spl_early_init() failed: %d\n", ret);
133a55e4971SPhilipp Tomsich hang();
134a55e4971SPhilipp Tomsich }
135a55e4971SPhilipp Tomsich
136a55e4971SPhilipp Tomsich /* Reset security, so we can use DMA in the MMC drivers */
137a55e4971SPhilipp Tomsich sgrf_init();
138a55e4971SPhilipp Tomsich
139a55e4971SPhilipp Tomsich ret = uclass_get_device(UCLASS_RAM, 0, &dev);
140a55e4971SPhilipp Tomsich if (ret) {
141a55e4971SPhilipp Tomsich debug("DRAM init failed: %d\n", ret);
142a55e4971SPhilipp Tomsich return;
143a55e4971SPhilipp Tomsich }
144a55e4971SPhilipp Tomsich }
145a55e4971SPhilipp Tomsich
board_return_to_bootrom(void)146a55e4971SPhilipp Tomsich void board_return_to_bootrom(void)
147a55e4971SPhilipp Tomsich {
148b82bd1f8SPhilipp Tomsich back_to_bootrom(BROM_BOOT_NEXTSTAGE);
149a55e4971SPhilipp Tomsich }
150a55e4971SPhilipp Tomsich
spl_boot_device(void)151a55e4971SPhilipp Tomsich u32 spl_boot_device(void)
152a55e4971SPhilipp Tomsich {
153a55e4971SPhilipp Tomsich return BOOT_DEVICE_BOOTROM;
154a55e4971SPhilipp Tomsich }
155