3a511941080f903d320ef61eecfb7ecf889c5974
[pandora-u-boot.git] / arch / arm / mach-rockchip / rk3399 / rk3399.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2016 Rockchip Electronics Co., Ltd
4  */
5
6 #include <common.h>
7 #include <fdt_support.h>
8 #include <spl.h>
9 #include <spl_gpio.h>
10 #include <syscon.h>
11 #include <asm/armv8/mmu.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/gpio.h>
16 #include <asm/arch-rockchip/grf_rk3399.h>
17 #include <asm/arch-rockchip/hardware.h>
18 #include <power/regulator.h>
19
20 DECLARE_GLOBAL_DATA_PTR;
21
22 #define GRF_EMMCCORE_CON11 0xff77f02c
23 #define GRF_BASE        0xff770000
24
25 const char * const boot_devices[BROM_LAST_BOOTSOURCE + 1] = {
26         [BROM_BOOTSOURCE_EMMC] = "/sdhci@fe330000",
27         [BROM_BOOTSOURCE_SPINOR] = "/spi@ff1d0000",
28         [BROM_BOOTSOURCE_SD] = "/dwmmc@fe320000",
29 };
30
31 static struct mm_region rk3399_mem_map[] = {
32         {
33                 .virt = 0x0UL,
34                 .phys = 0x0UL,
35                 .size = 0xf8000000UL,
36                 .attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) |
37                          PTE_BLOCK_INNER_SHARE
38         }, {
39                 .virt = 0xf8000000UL,
40                 .phys = 0xf8000000UL,
41                 .size = 0x08000000UL,
42                 .attrs = PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE) |
43                          PTE_BLOCK_NON_SHARE |
44                          PTE_BLOCK_PXN | PTE_BLOCK_UXN
45         }, {
46                 /* List terminator */
47                 0,
48         }
49 };
50
51 struct mm_region *mem_map = rk3399_mem_map;
52
53 #ifdef CONFIG_SPL_BUILD
54
55 #define TIMER_END_COUNT_L       0x00
56 #define TIMER_END_COUNT_H       0x04
57 #define TIMER_INIT_COUNT_L      0x10
58 #define TIMER_INIT_COUNT_H      0x14
59 #define TIMER_CONTROL_REG       0x1c
60
61 #define TIMER_EN        0x1
62 #define TIMER_FMODE     BIT(0)
63 #define TIMER_RMODE     BIT(1)
64
65 void rockchip_stimer_init(void)
66 {
67         /* If Timer already enabled, don't re-init it */
68         u32 reg = readl(CONFIG_ROCKCHIP_STIMER_BASE + TIMER_CONTROL_REG);
69
70         if (reg & TIMER_EN)
71                 return;
72
73         writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_L);
74         writel(0xffffffff, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_END_COUNT_H);
75         writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_L);
76         writel(0, CONFIG_ROCKCHIP_STIMER_BASE + TIMER_INIT_COUNT_H);
77         writel(TIMER_EN | TIMER_FMODE, CONFIG_ROCKCHIP_STIMER_BASE + \
78                TIMER_CONTROL_REG);
79 }
80 #endif
81
82 int arch_cpu_init(void)
83 {
84
85 #ifdef CONFIG_SPL_BUILD
86         struct rk3399_pmusgrf_regs *sgrf;
87         struct rk3399_grf_regs *grf;
88
89         /*
90          * Disable DDR and SRAM security regions.
91          *
92          * As we are entered from the BootROM, the region from
93          * 0x0 through 0xfffff (i.e. the first MB of memory) will
94          * be protected. This will cause issues with the DW_MMC
95          * driver, which tries to DMA from/to the stack (likely)
96          * located in this range.
97          */
98         sgrf = syscon_get_first_range(ROCKCHIP_SYSCON_PMUSGRF);
99         rk_clrsetreg(&sgrf->ddr_rgn_con[16], 0x1ff, 0);
100         rk_clrreg(&sgrf->slv_secure_con4, 0x2000);
101
102         /*  eMMC clock generator: disable the clock multipilier */
103         grf = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
104         rk_clrreg(&grf->emmccore_con[11], 0x0ff);
105 #endif
106
107         return 0;
108 }
109
110 #ifdef CONFIG_DEBUG_UART_BOARD_INIT
111 void board_debug_uart_init(void)
112 {
113 #define GRF_BASE        0xff770000
114 #define GPIO0_BASE      0xff720000
115 #define PMUGRF_BASE     0xff320000
116         struct rk3399_grf_regs * const grf = (void *)GRF_BASE;
117 #ifdef CONFIG_TARGET_CHROMEBOOK_BOB
118         struct rk3399_pmugrf_regs * const pmugrf = (void *)PMUGRF_BASE;
119         struct rockchip_gpio_regs * const gpio = (void *)GPIO0_BASE;
120 #endif
121
122 #if defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff180000)
123         /* Enable early UART0 on the RK3399 */
124         rk_clrsetreg(&grf->gpio2c_iomux,
125                      GRF_GPIO2C0_SEL_MASK,
126                      GRF_UART0BT_SIN << GRF_GPIO2C0_SEL_SHIFT);
127         rk_clrsetreg(&grf->gpio2c_iomux,
128                      GRF_GPIO2C1_SEL_MASK,
129                      GRF_UART0BT_SOUT << GRF_GPIO2C1_SEL_SHIFT);
130 #elif defined(CONFIG_DEBUG_UART_BASE) && (CONFIG_DEBUG_UART_BASE == 0xff1B0000)
131         /* Enable early UART3 on the RK3399 */
132         rk_clrsetreg(&grf->gpio3b_iomux,
133                      GRF_GPIO3B6_SEL_MASK,
134                      GRF_UART3_SIN << GRF_GPIO3B6_SEL_SHIFT);
135         rk_clrsetreg(&grf->gpio3b_iomux,
136                      GRF_GPIO3B7_SEL_MASK,
137                      GRF_UART3_SOUT << GRF_GPIO3B7_SEL_SHIFT);
138 #else
139 # ifdef CONFIG_TARGET_CHROMEBOOK_BOB
140         rk_setreg(&grf->io_vsel, 1 << 0);
141
142         /*
143          * Let's enable these power rails here, we are already running the SPI
144          * Flash based code.
145          */
146         spl_gpio_output(gpio, GPIO(BANK_B, 2), 1);  /* PP1500_EN */
147         spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 2), GPIO_PULL_NORMAL);
148
149         spl_gpio_output(gpio, GPIO(BANK_B, 4), 1);  /* PP3000_EN */
150         spl_gpio_set_pull(&pmugrf->gpio0_p, GPIO(BANK_B, 4), GPIO_PULL_NORMAL);
151 #endif /* CONFIG_TARGET_CHROMEBOOK_BOB */
152
153         /* Enable early UART2 channel C on the RK3399 */
154         rk_clrsetreg(&grf->gpio4c_iomux,
155                      GRF_GPIO4C3_SEL_MASK,
156                      GRF_UART2DGBC_SIN << GRF_GPIO4C3_SEL_SHIFT);
157         rk_clrsetreg(&grf->gpio4c_iomux,
158                      GRF_GPIO4C4_SEL_MASK,
159                      GRF_UART2DBGC_SOUT << GRF_GPIO4C4_SEL_SHIFT);
160         /* Set channel C as UART2 input */
161         rk_clrsetreg(&grf->soc_con7,
162                      GRF_UART_DBG_SEL_MASK,
163                      GRF_UART_DBG_SEL_C << GRF_UART_DBG_SEL_SHIFT);
164 #endif
165 }
166 #endif
167
168 #if defined(CONFIG_SPL_BUILD) && !defined(CONFIG_TPL_BUILD)
169 const char *spl_decode_boot_device(u32 boot_device)
170 {
171         int i;
172         static const struct {
173                 u32 boot_device;
174                 const char *ofpath;
175         } spl_boot_devices_tbl[] = {
176                 { BOOT_DEVICE_MMC1, "/dwmmc@fe320000" },
177                 { BOOT_DEVICE_MMC2, "/sdhci@fe330000" },
178                 { BOOT_DEVICE_SPI, "/spi@ff1d0000" },
179         };
180
181         for (i = 0; i < ARRAY_SIZE(spl_boot_devices_tbl); ++i)
182                 if (spl_boot_devices_tbl[i].boot_device == boot_device)
183                         return spl_boot_devices_tbl[i].ofpath;
184
185         return NULL;
186 }
187
188 void spl_perform_fixups(struct spl_image_info *spl_image)
189 {
190         void *blob = spl_image->fdt_addr;
191         const char *boot_ofpath;
192         int chosen;
193
194         /*
195          * Inject the ofpath of the device the full U-Boot (or Linux in
196          * Falcon-mode) was booted from into the FDT, if a FDT has been
197          * loaded at the same time.
198          */
199         if (!blob)
200                 return;
201
202         boot_ofpath = spl_decode_boot_device(spl_image->boot_device);
203         if (!boot_ofpath) {
204                 pr_err("%s: could not map boot_device to ofpath\n", __func__);
205                 return;
206         }
207
208         chosen = fdt_find_or_add_subnode(blob, 0, "chosen");
209         if (chosen < 0) {
210                 pr_err("%s: could not find/create '/chosen'\n", __func__);
211                 return;
212         }
213         fdt_setprop_string(blob, chosen,
214                            "u-boot,spl-boot-device", boot_ofpath);
215 }
216
217 #if defined(SPL_GPIO_SUPPORT)
218 static void rk3399_force_power_on_reset(void)
219 {
220         ofnode node;
221         struct gpio_desc sysreset_gpio;
222
223         debug("%s: trying to force a power-on reset\n", __func__);
224
225         node = ofnode_path("/config");
226         if (!ofnode_valid(node)) {
227                 debug("%s: no /config node?\n", __func__);
228                 return;
229         }
230
231         if (gpio_request_by_name_nodev(node, "sysreset-gpio", 0,
232                                        &sysreset_gpio, GPIOD_IS_OUT)) {
233                 debug("%s: could not find a /config/sysreset-gpio\n", __func__);
234                 return;
235         }
236
237         dm_gpio_set_value(&sysreset_gpio, 1);
238 }
239 #endif
240
241 void spl_board_init(void)
242 {
243 #if defined(SPL_GPIO_SUPPORT)
244         struct rockchip_cru *cru = rockchip_get_cru();
245
246         /*
247          * The RK3399 resets only 'almost all logic' (see also in the TRM
248          * "3.9.4 Global software reset"), when issuing a software reset.
249          * This may cause issues during boot-up for some configurations of
250          * the application software stack.
251          *
252          * To work around this, we test whether the last reset reason was
253          * a power-on reset and (if not) issue an overtemp-reset to reset
254          * the entire module.
255          *
256          * While this was previously fixed by modifying the various places
257          * that could generate a software reset (e.g. U-Boot's sysreset
258          * driver, the ATF or Linux), we now have it here to ensure that
259          * we no longer have to track this through the various components.
260          */
261         if (cru->glb_rst_st != 0)
262                 rk3399_force_power_on_reset();
263 #endif
264
265 #if defined(SPL_DM_REGULATOR)
266         /*
267          * Turning the eMMC and SPI back on (if disabled via the Qseven
268          * BIOS_ENABLE) signal is done through a always-on regulator).
269          */
270         if (regulators_enable_boot_on(false))
271                 debug("%s: Cannot enable boot on regulator\n", __func__);
272 #endif
273 }
274 #endif