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