Merge tag 'rockchip-for-2019.07' of git://git.denx.de/u-boot-rockchip
[oweals/u-boot.git] / arch / arm / mach-rockchip / rk3368-board-tpl.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH
4  */
5
6 #include <common.h>
7 #include <debug_uart.h>
8 #include <dm.h>
9 #include <ram.h>
10 #include <spl.h>
11 #include <syscon.h>
12 #include <asm/io.h>
13 #include <asm/arch-rockchip/bootrom.h>
14 #include <asm/arch-rockchip/clock.h>
15 #include <asm/arch-rockchip/cru_rk3368.h>
16 #include <asm/arch-rockchip/hardware.h>
17 #include <asm/arch-rockchip/timer.h>
18
19 /*
20  * The SPL (and also the full U-Boot stage on the RK3368) will run in
21  * secure mode (i.e. EL3) and an ATF will eventually be booted before
22  * starting up the operating system... so we can initialize the SGRF
23  * here and rely on the ATF installing the final (secure) policy
24  * later.
25  */
26 static inline uintptr_t sgrf_soc_con_addr(unsigned no)
27 {
28         const uintptr_t SGRF_BASE =
29                 (uintptr_t)syscon_get_first_range(ROCKCHIP_SYSCON_SGRF);
30
31         return SGRF_BASE + sizeof(u32) * no;
32 }
33
34 static inline uintptr_t sgrf_busdmac_addr(unsigned no)
35 {
36         const uintptr_t SGRF_BASE =
37                 (uintptr_t)syscon_get_first_range(ROCKCHIP_SYSCON_SGRF);
38         const uintptr_t SGRF_BUSDMAC_OFFSET = 0x100;
39         const uintptr_t SGRF_BUSDMAC_BASE = SGRF_BASE + SGRF_BUSDMAC_OFFSET;
40
41         return SGRF_BUSDMAC_BASE + sizeof(u32) * no;
42 }
43
44 static void sgrf_init(void)
45 {
46         struct rk3368_cru * const cru =
47                 (struct rk3368_cru * const)rockchip_get_cru();
48         const u16 SGRF_SOC_CON_SEC = GENMASK(15, 0);
49         const u16 SGRF_BUSDMAC_CON0_SEC = BIT(2);
50         const u16 SGRF_BUSDMAC_CON1_SEC = GENMASK(15, 12);
51
52         /* Set all configurable IP to 'non secure'-mode */
53         rk_setreg(sgrf_soc_con_addr(5), SGRF_SOC_CON_SEC);
54         rk_setreg(sgrf_soc_con_addr(6), SGRF_SOC_CON_SEC);
55         rk_setreg(sgrf_soc_con_addr(7), SGRF_SOC_CON_SEC);
56
57         /*
58          * From rockchip-uboot/arch/arm/cpu/armv8/rk33xx/cpu.c
59          * Original comment: "ddr space set no secure mode"
60          */
61         rk_clrreg(sgrf_soc_con_addr(8), SGRF_SOC_CON_SEC);
62         rk_clrreg(sgrf_soc_con_addr(9), SGRF_SOC_CON_SEC);
63         rk_clrreg(sgrf_soc_con_addr(10), SGRF_SOC_CON_SEC);
64
65         /* Set 'secure dma' to 'non secure'-mode */
66         rk_setreg(sgrf_busdmac_addr(0), SGRF_BUSDMAC_CON0_SEC);
67         rk_setreg(sgrf_busdmac_addr(1), SGRF_BUSDMAC_CON1_SEC);
68
69         dsb();  /* barrier */
70
71         rk_setreg(&cru->softrst_con[1], DMA1_SRST_REQ);
72         rk_setreg(&cru->softrst_con[4], DMA2_SRST_REQ);
73
74         dsb();  /* barrier */
75         udelay(10);
76
77         rk_clrreg(&cru->softrst_con[1], DMA1_SRST_REQ);
78         rk_clrreg(&cru->softrst_con[4], DMA2_SRST_REQ);
79 }
80
81 void board_init_f(ulong dummy)
82 {
83         struct udevice *dev;
84         int ret;
85
86 #ifdef CONFIG_DEBUG_UART
87         /*
88          * Debug UART can be used from here if required:
89          *
90          * debug_uart_init();
91          * printch('a');
92          * printhex8(0x1234);
93          * printascii("string");
94          */
95         debug_uart_init();
96         printascii("U-Boot TPL board init\n");
97 #endif
98
99         ret = spl_early_init();
100         if (ret) {
101                 debug("spl_early_init() failed: %d\n", ret);
102                 hang();
103         }
104
105         /* Reset security, so we can use DMA in the MMC drivers */
106         sgrf_init();
107
108         ret = uclass_get_device(UCLASS_RAM, 0, &dev);
109         if (ret) {
110                 debug("DRAM init failed: %d\n", ret);
111                 return;
112         }
113 }
114
115 void board_return_to_bootrom(void)
116 {
117         back_to_bootrom(BROM_BOOT_NEXTSTAGE);
118 }
119
120 u32 spl_boot_device(void)
121 {
122         return BOOT_DEVICE_BOOTROM;
123 }