Merge tag 'u-boot-stm32-20190723' of https://gitlab.denx.de/u-boot/custodians/u-boot-stm
[oweals/u-boot.git] / arch / arm / mach-rockchip / rk3368 / rk3368.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016 Rockchip Electronics Co., Ltd
4  * Copyright (c) 2016 Andreas Färber
5  */
6
7 #include <common.h>
8 #include <syscon.h>
9 #include <asm/armv8/mmu.h>
10 #include <asm/io.h>
11 #include <asm/arch-rockchip/clock.h>
12 #include <asm/arch-rockchip/cru_rk3368.h>
13 #include <asm/arch-rockchip/grf_rk3368.h>
14 #include <asm/arch-rockchip/hardware.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 #define IMEM_BASE                  0xFF8C0000
19
20 /* Max MCU's SRAM value is 8K, begin at (IMEM_BASE + 4K) */
21 #define MCU_SRAM_BASE                   (IMEM_BASE + 1024 * 4)
22 #define MCU_SRAM_BASE_BIT31_BIT28       ((MCU_SRAM_BASE & GENMASK(31, 28)) >> 28)
23 #define MCU_SRAM_BASE_BIT27_BIT12       ((MCU_SRAM_BASE & GENMASK(27, 12)) >> 12)
24 /* exsram may using by mcu to accessing dram(0x0-0x20000000) */
25 #define MCU_EXSRAM_BASE    (0)
26 #define MCU_EXSRAM_BASE_BIT31_BIT28       ((MCU_EXSRAM_BASE & GENMASK(31, 28)) >> 28)
27 #define MCU_EXSRAM_BASE_BIT27_BIT12       ((MCU_EXSRAM_BASE & GENMASK(27, 12)) >> 12)
28 /* experi no used, reserved value = 0 */
29 #define MCU_EXPERI_BASE    (0)
30 #define MCU_EXPERI_BASE_BIT31_BIT28       ((MCU_EXPERI_BASE & GENMASK(31, 28)) >> 28)
31 #define MCU_EXPERI_BASE_BIT27_BIT12       ((MCU_EXPERI_BASE & GENMASK(27, 12)) >> 12)
32
33 static struct mm_region rk3368_mem_map[] = {
34         {
35                 .virt = 0x0UL,
36                 .phys = 0x0UL,
37                 .size = 0x80000000UL,
38                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
39                          PTE_BLOCK_INNER_SHARE
40         }, {
41                 .virt = 0xf0000000UL,
42                 .phys = 0xf0000000UL,
43                 .size = 0x10000000UL,
44                 .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
53 struct mm_region *mem_map = rk3368_mem_map;
54
55 int dram_init_banksize(void)
56 {
57         size_t max_size = min((unsigned long)gd->ram_size, gd->ram_top);
58
59         /* Reserve 0x200000 for ATF bl31 */
60         gd->bd->bi_dram[0].start = 0x200000;
61         gd->bd->bi_dram[0].size = max_size - gd->bd->bi_dram[0].start;
62
63         return 0;
64 }
65
66 #ifdef CONFIG_ARCH_EARLY_INIT_R
67 static int mcu_init(void)
68 {
69         struct rk3368_grf *grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
70         struct rk3368_cru *cru = rockchip_get_cru();
71
72         rk_clrsetreg(&grf->soc_con14, MCU_SRAM_BASE_BIT31_BIT28_MASK,
73                      MCU_SRAM_BASE_BIT31_BIT28 << MCU_SRAM_BASE_BIT31_BIT28_SHIFT);
74         rk_clrsetreg(&grf->soc_con11, MCU_SRAM_BASE_BIT27_BIT12_MASK,
75                      MCU_SRAM_BASE_BIT27_BIT12 << MCU_SRAM_BASE_BIT27_BIT12_SHIFT);
76         rk_clrsetreg(&grf->soc_con14, MCU_EXSRAM_BASE_BIT31_BIT28_MASK,
77                      MCU_EXSRAM_BASE_BIT31_BIT28 << MCU_EXSRAM_BASE_BIT31_BIT28_SHIFT);
78         rk_clrsetreg(&grf->soc_con12, MCU_EXSRAM_BASE_BIT27_BIT12_MASK,
79                      MCU_EXSRAM_BASE_BIT27_BIT12 << MCU_EXSRAM_BASE_BIT27_BIT12_SHIFT);
80         rk_clrsetreg(&grf->soc_con14, MCU_EXPERI_BASE_BIT31_BIT28_MASK,
81                      MCU_EXPERI_BASE_BIT31_BIT28 << MCU_EXPERI_BASE_BIT31_BIT28_SHIFT);
82         rk_clrsetreg(&grf->soc_con13, MCU_EXPERI_BASE_BIT27_BIT12_MASK,
83                      MCU_EXPERI_BASE_BIT27_BIT12 << MCU_EXPERI_BASE_BIT27_BIT12_SHIFT);
84
85         rk_clrsetreg(&cru->clksel_con[12], MCU_PLL_SEL_MASK | MCU_CLK_DIV_MASK,
86                      (MCU_PLL_SEL_GPLL << MCU_PLL_SEL_SHIFT) |
87                      (5 << MCU_CLK_DIV_SHIFT));
88
89          /* mcu dereset, for start running */
90         rk_clrreg(&cru->softrst_con[1], MCU_PO_SRST_MASK | MCU_SYS_SRST_MASK);
91
92         return 0;
93 }
94
95 int arch_early_init_r(void)
96 {
97         return mcu_init();
98 }
99 #endif
100
101 #ifdef CONFIG_SPL_BUILD
102 /*
103  * The SPL (and also the full U-Boot stage on the RK3368) will run in
104  * secure mode (i.e. EL3) and an ATF will eventually be booted before
105  * starting up the operating system... so we can initialize the SGRF
106  * here and rely on the ATF installing the final (secure) policy
107  * later.
108  */
109 static inline uintptr_t sgrf_soc_con_addr(unsigned int no)
110 {
111         const uintptr_t SGRF_BASE =
112                 (uintptr_t)syscon_get_first_range(ROCKCHIP_SYSCON_SGRF);
113
114         return SGRF_BASE + sizeof(u32) * no;
115 }
116
117 static inline uintptr_t sgrf_busdmac_addr(unsigned int no)
118 {
119         const uintptr_t SGRF_BASE =
120                 (uintptr_t)syscon_get_first_range(ROCKCHIP_SYSCON_SGRF);
121         const uintptr_t SGRF_BUSDMAC_OFFSET = 0x100;
122         const uintptr_t SGRF_BUSDMAC_BASE = SGRF_BASE + SGRF_BUSDMAC_OFFSET;
123
124         return SGRF_BUSDMAC_BASE + sizeof(u32) * no;
125 }
126
127 static void sgrf_init(void)
128 {
129         struct rk3368_cru * const cru =
130                 (struct rk3368_cru * const)rockchip_get_cru();
131         const u16 SGRF_SOC_CON_SEC = GENMASK(15, 0);
132         const u16 SGRF_BUSDMAC_CON0_SEC = BIT(2);
133         const u16 SGRF_BUSDMAC_CON1_SEC = GENMASK(15, 12);
134
135         /* Set all configurable IP to 'non secure'-mode */
136         rk_setreg(sgrf_soc_con_addr(5), SGRF_SOC_CON_SEC);
137         rk_setreg(sgrf_soc_con_addr(6), SGRF_SOC_CON_SEC);
138         rk_setreg(sgrf_soc_con_addr(7), SGRF_SOC_CON_SEC);
139
140         /*
141          * From rockchip-uboot/arch/arm/cpu/armv8/rk33xx/cpu.c
142          * Original comment: "ddr space set no secure mode"
143          */
144         rk_clrreg(sgrf_soc_con_addr(8), SGRF_SOC_CON_SEC);
145         rk_clrreg(sgrf_soc_con_addr(9), SGRF_SOC_CON_SEC);
146         rk_clrreg(sgrf_soc_con_addr(10), SGRF_SOC_CON_SEC);
147
148         /* Set 'secure dma' to 'non secure'-mode */
149         rk_setreg(sgrf_busdmac_addr(0), SGRF_BUSDMAC_CON0_SEC);
150         rk_setreg(sgrf_busdmac_addr(1), SGRF_BUSDMAC_CON1_SEC);
151
152         dsb();  /* barrier */
153
154         rk_setreg(&cru->softrst_con[1], DMA1_SRST_REQ);
155         rk_setreg(&cru->softrst_con[4], DMA2_SRST_REQ);
156
157         dsb();  /* barrier */
158         udelay(10);
159
160         rk_clrreg(&cru->softrst_con[1], DMA1_SRST_REQ);
161         rk_clrreg(&cru->softrst_con[4], DMA2_SRST_REQ);
162 }
163
164 int arch_cpu_init(void)
165 {
166         /* Reset security, so we can use DMA in the MMC drivers */
167         sgrf_init();
168
169         return 0;
170 }
171 #endif
172
173 #ifdef CONFIG_DEBUG_UART_BOARD_INIT
174 void board_debug_uart_init(void)
175 {
176         /*
177          * N.B.: This is called before the device-model has been
178          *       initialised. For this reason, we can not access
179          *       the GRF address range using the syscon API.
180          */
181 #if defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff180000)
182         struct rk3368_grf * const grf =
183                 (struct rk3368_grf * const)0xff770000;
184
185         enum {
186                 GPIO2D1_MASK            = GENMASK(3, 2),
187                 GPIO2D1_GPIO            = 0,
188                 GPIO2D1_UART0_SOUT      = (1 << 2),
189
190                 GPIO2D0_MASK            = GENMASK(1, 0),
191                 GPIO2D0_GPIO            = 0,
192                 GPIO2D0_UART0_SIN       = (1 << 0),
193         };
194
195         /* Enable early UART0 on the RK3368 */
196         rk_clrsetreg(&grf->gpio2d_iomux,
197                      GPIO2D0_MASK, GPIO2D0_UART0_SIN);
198         rk_clrsetreg(&grf->gpio2d_iomux,
199                      GPIO2D1_MASK, GPIO2D1_UART0_SOUT);
200 #elif defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff1c0000)
201         struct rk3368_pmu_grf * const pmugrf __maybe_unused =
202                 (struct rk3368_pmu_grf * const)0xff738000;
203
204         enum {
205                 /* UART4 */
206                 GPIO0D2_MASK            = GENMASK(5, 4),
207                 GPIO0D2_GPIO            = 0,
208                 GPIO0D2_UART4_SOUT      = (3 << 4),
209
210                 GPIO0D3_MASK            = GENMASK(7, 6),
211                 GPIO0D3_GPIO            = 0,
212                 GPIO0D3_UART4_SIN       = (3 << 6),
213         };
214
215         /* Enable early UART4 on the PX5 */
216         rk_clrsetreg(&pmugrf->gpio0d_iomux,
217                      GPIO0D2_MASK | GPIO0D3_MASK,
218                      GPIO0D2_UART4_SOUT | GPIO0D3_UART4_SIN);
219 #elif defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff690000)
220         struct rk3368_grf * const grf =
221                 (struct rk3368_grf * const)0xff770000;
222
223         enum {
224                 GPIO2A6_SHIFT           = 12,
225                 GPIO2A6_MASK            = GENMASK(13, 12),
226                 GPIO2A6_GPIO            = 0,
227                 GPIO2A6_UART2_SIN       = (2 << GPIO2A6_SHIFT),
228
229                 GPIO2A5_SHIFT           = 10,
230                 GPIO2A5_MASK            = GENMASK(11, 10),
231                 GPIO2A5_GPIO            = 0,
232                 GPIO2A5_UART2_SOUT      = (2 << GPIO2A5_SHIFT),
233         };
234
235         /* Enable early UART2 on the RK3368 */
236         rk_clrsetreg(&grf->gpio2a_iomux,
237                      GPIO2A6_MASK, GPIO2A6_UART2_SIN);
238         rk_clrsetreg(&grf->gpio2a_iomux,
239                      GPIO2A5_MASK, GPIO2A5_UART2_SOUT);
240 #endif
241 }
242 #endif