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