2 * Device Tree support for Rockchip RK3288
4 * Copyright (C) 2014 ROCKCHIP, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include <linux/clk-provider.h>
18 #include <linux/clocksource.h>
19 #include <linux/cpuidle.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/irqchip.h>
23 #include <linux/kernel.h>
24 #include <linux/of_address.h>
25 #include <linux/of_platform.h>
26 #include <linux/rockchip/common.h>
27 #include <linux/rockchip/cpu.h>
28 #include <linux/rockchip/cru.h>
29 #include <linux/rockchip/dvfs.h>
30 #include <linux/rockchip/grf.h>
31 #include <linux/rockchip/iomap.h>
32 #include <linux/rockchip/pmu.h>
33 /*#include <asm/cpuidle.h>*/
34 #include <asm/cputype.h>
35 #include <asm/mach/arch.h>
36 #include <asm/mach/map.h>
42 #include "pm-rk312x.c"
43 #define RK312X_DEVICE(name) \
45 .virtual = (unsigned long) RK_##name##_VIRT, \
46 .pfn = __phys_to_pfn(RK312X_##name##_PHYS), \
47 .length = RK312X_##name##_SIZE, \
51 static const char * const rk3126_dt_compat[] __initconst = {
56 static const char * const rk3128_dt_compat[] __initconst = {
61 #define RK312X_IMEM_VIRT (RK_BOOTRAM_VIRT + SZ_32K)
62 #define RK312X_TIMER5_VIRT (RK_TIMER_VIRT + 0xa0)
64 static struct map_desc rk312x_io_desc[] __initdata = {
71 RK312X_DEVICE(CPU_AXI_BUS),
72 RK_DEVICE(RK_DEBUG_UART_VIRT, RK312X_UART2_PHYS, RK312X_UART_SIZE),
73 RK_DEVICE(RK_DDR_VIRT, RK312X_DDR_PCTL_PHYS, RK312X_DDR_PCTL_SIZE),
74 RK_DEVICE(RK_DDR_VIRT + RK312X_DDR_PCTL_SIZE, RK312X_DDR_PHY_PHYS, RK312X_DDR_PHY_SIZE),
75 RK_DEVICE(RK_GPIO_VIRT(0), RK312X_GPIO0_PHYS, RK312X_GPIO_SIZE),
76 RK_DEVICE(RK_GPIO_VIRT(1), RK312X_GPIO1_PHYS, RK312X_GPIO_SIZE),
77 RK_DEVICE(RK_GPIO_VIRT(2), RK312X_GPIO2_PHYS, RK312X_GPIO_SIZE),
78 RK_DEVICE(RK_GPIO_VIRT(3), RK312X_GPIO3_PHYS, RK312X_GPIO_SIZE),
79 RK_DEVICE(RK_GIC_VIRT, RK312X_GIC_DIST_PHYS, RK312X_GIC_DIST_SIZE),
80 RK_DEVICE(RK_GIC_VIRT + RK312X_GIC_DIST_SIZE, RK312X_GIC_CPU_PHYS, RK312X_GIC_CPU_SIZE),
81 RK_DEVICE(RK312X_IMEM_VIRT, RK312X_IMEM_PHYS, RK312X_IMEM_SIZE),
84 static void __init rk312x_boot_mode_init(void)
86 u32 flag = readl_relaxed(RK_PMU_VIRT + RK312X_PMU_SYS_REG0);
87 u32 mode = readl_relaxed(RK_PMU_VIRT + RK312X_PMU_SYS_REG1);
88 u32 rst_st = readl_relaxed(RK_CRU_VIRT + RK312X_CRU_GLB_RST_ST);
90 if (flag == (SYS_KERNRL_REBOOT_FLAG | BOOT_RECOVER))
91 mode = BOOT_MODE_RECOVERY;
92 if (rst_st & ((1 << 2) | (1 << 3)))
93 mode = BOOT_MODE_WATCHDOG;
95 rockchip_boot_mode_init(flag, mode);
98 static void usb_uart_init(void)
100 #ifdef CONFIG_RK_USB_UART
101 u32 soc_status0 = readl_relaxed(RK_GRF_VIRT + RK312X_GRF_SOC_STATUS0);
103 writel_relaxed(0x34000000, RK_GRF_VIRT + RK312X_GRF_UOC1_CON4);
104 #ifdef CONFIG_RK_USB_UART
105 if (!(soc_status0 & (1 << 5)) && (soc_status0 & (1 << 8))) {
106 /* software control usb phy enable */
107 writel_relaxed(0x007f0055, RK_GRF_VIRT + RK312X_GRF_UOC0_CON0);
108 writel_relaxed(0x34003000, RK_GRF_VIRT + RK312X_GRF_UOC1_CON4);
112 writel_relaxed(0x07, RK_DEBUG_UART_VIRT + 0x88);
113 writel_relaxed(0x00, RK_DEBUG_UART_VIRT + 0x04);
114 writel_relaxed(0x83, RK_DEBUG_UART_VIRT + 0x0c);
115 writel_relaxed(0x0d, RK_DEBUG_UART_VIRT + 0x00);
116 writel_relaxed(0x00, RK_DEBUG_UART_VIRT + 0x04);
117 writel_relaxed(0x03, RK_DEBUG_UART_VIRT + 0x0c);
120 static void __init rk312x_dt_map_io(void)
124 iotable_init(rk312x_io_desc, ARRAY_SIZE(rk312x_io_desc));
128 /* pmu reset by second global soft reset */
129 v = readl_relaxed(RK_CRU_VIRT + RK312X_CRU_GLB_CNT_TH);
132 writel_relaxed(v, RK_CRU_VIRT + RK312X_CRU_GLB_CNT_TH);
134 /* enable timer5 for core */
135 writel_relaxed(0, RK312X_TIMER5_VIRT + 0x10);
137 writel_relaxed(0xFFFFFFFF, RK312X_TIMER5_VIRT + 0x00);
138 writel_relaxed(0xFFFFFFFF, RK312X_TIMER5_VIRT + 0x04);
140 writel_relaxed(1, RK312X_TIMER5_VIRT + 0x10);
142 writel_relaxed(0x80000000, RK_CRU_VIRT + RK312X_CRU_MISC_CON);
145 rk312x_boot_mode_init();
148 static void __init rk3126_dt_map_io(void)
150 rockchip_soc_id = ROCKCHIP_SOC_RK3126;
155 static void __init rk3128_dt_map_io(void)
157 rockchip_soc_id = ROCKCHIP_SOC_RK3128;
161 static DEFINE_SPINLOCK(pmu_idle_lock);
162 static const u8 pmu_idle_map[] = {
164 [IDLE_REQ_VIDEO] = 1,
170 [IDLE_REQ_CRYPTO] = 7,
173 static int rk312x_pmu_set_idle_request(enum pmu_idle_req req, bool idle)
177 u32 bit = pmu_idle_map[req];
178 u32 idle_mask = BIT(bit) | BIT(bit + 16);
179 u32 idle_target = (idle << bit) | (idle << (bit + 16));
182 spin_lock_irqsave(&pmu_idle_lock, flags);
183 val = pmu_readl(RK312X_PMU_IDLE_REQ);
188 pmu_writel(val, RK312X_PMU_IDLE_REQ);
191 while (((pmu_readl(RK312X_PMU_IDLE_ST) & idle_mask) != idle_target))
193 spin_unlock_irqrestore(&pmu_idle_lock, flags);
196 static const u8 pmu_pd_map[] = {
202 static const u8 pmu_st_map[] = {
208 static noinline void rk312x_do_pmu_set_power_domain(enum pmu_power_domain domain
211 u8 pd = pmu_pd_map[domain];
212 u32 val = pmu_readl(RK312X_PMU_PWRDN_CON);
218 pmu_writel(val, RK312X_PMU_PWRDN_CON);
221 while ((pmu_readl(RK312X_PMU_PWRDN_ST) & BIT(pmu_st_map[domain])) == on)
225 static bool rk312x_pmu_power_domain_is_on(enum pmu_power_domain pd)
227 /*1"b0: power on, 1'b1: power off*/
228 return !(pmu_readl(RK312X_PMU_PWRDN_ST) & BIT(pmu_st_map[pd]));
230 static DEFINE_SPINLOCK(pmu_pd_lock);
231 static u32 rga_qos[RK312X_CPU_AXI_QOS_NUM_REGS];
232 static u32 ebc_qos[RK312X_CPU_AXI_QOS_NUM_REGS];
233 static u32 iep_qos[RK312X_CPU_AXI_QOS_NUM_REGS];
234 static u32 lcdc0_qos[RK312X_CPU_AXI_QOS_NUM_REGS];
235 static u32 vip0_qos[RK312X_CPU_AXI_QOS_NUM_REGS];
236 static u32 gpu_qos[RK312X_CPU_AXI_QOS_NUM_REGS];
237 static u32 video_qos[RK312X_CPU_AXI_QOS_NUM_REGS];
239 #define SAVE_QOS(array, NAME) RK312X_CPU_AXI_SAVE_QOS(array, RK312X_CPU_AXI_##NAME##_QOS_VIRT)
240 #define RESTORE_QOS(array, NAME) RK312X_CPU_AXI_RESTORE_QOS(array, RK312X_CPU_AXI_##NAME##_QOS_VIRT)
242 static int rk312x_pmu_set_power_domain(enum pmu_power_domain pd, bool on)
246 spin_lock_irqsave(&pmu_pd_lock, flags);
247 if (rk312x_pmu_power_domain_is_on(pd) == on)
251 SAVE_QOS(gpu_qos, GPU);
252 rk312x_pmu_set_idle_request(IDLE_REQ_GPU, true);
253 } else if (pd == PD_VIO) {
254 SAVE_QOS(rga_qos, VIO_RGA);
255 SAVE_QOS(ebc_qos, VIO_EBC);
256 SAVE_QOS(iep_qos, VIO_IEP);
257 SAVE_QOS(lcdc0_qos, VIO_LCDC0);
258 SAVE_QOS(vip0_qos, VIO_VIP0);
259 rk312x_pmu_set_idle_request(IDLE_REQ_VIO, true);
260 } else if (pd == PD_VIDEO) {
261 SAVE_QOS(video_qos, VIDEO);
262 rk312x_pmu_set_idle_request(IDLE_REQ_VIDEO, true);
266 rk312x_do_pmu_set_power_domain(pd, on);
270 rk312x_pmu_set_idle_request(IDLE_REQ_GPU, false);
271 RESTORE_QOS(gpu_qos, GPU);
272 } else if (pd == PD_VIO) {
273 rk312x_pmu_set_idle_request(IDLE_REQ_VIO, false);
274 RESTORE_QOS(rga_qos, VIO_RGA);
275 RESTORE_QOS(ebc_qos, VIO_EBC);
276 RESTORE_QOS(iep_qos, VIO_IEP);
277 RESTORE_QOS(lcdc0_qos, VIO_LCDC0);
278 RESTORE_QOS(vip0_qos, VIO_VIP0);
279 } else if (pd == PD_VIDEO) {
280 rk312x_pmu_set_idle_request(IDLE_REQ_VIDEO, false);
281 RESTORE_QOS(video_qos, VIDEO);
285 spin_unlock_irqrestore(&pmu_pd_lock, flags);
289 extern void secondary_startup(void);
290 static int rk312x_sys_set_power_domain(enum pmu_power_domain pd, bool on)
292 u32 clks_save[RK312X_CRU_CLKGATES_CON_CNT];
293 u32 clks_ungating[RK312X_CRU_CLKGATES_CON_CNT];
296 for (i = 0; i < RK312X_CRU_CLKGATES_CON_CNT; i++) {
297 clks_save[i] = cru_readl(RK312X_CRU_CLKGATES_CON(i));
298 clks_ungating[i] = 0;
300 for (i = 0; i < RK312X_CRU_CLKGATES_CON_CNT; i++)
301 cru_writel(0xffff0000, RK312X_CRU_CLKGATES_CON(i));
305 if (pd >= PD_CPU_1 && pd <= PD_CPU_3) {
306 writel_relaxed(0x20000 << (pd - PD_CPU_1),
307 RK_CRU_VIRT + RK312X_CRU_SOFTRSTS_CON(0));
310 writel_relaxed(virt_to_phys(secondary_startup),
311 RK312X_IMEM_VIRT + 8);
312 writel_relaxed(0xDEADBEAF, RK312X_IMEM_VIRT + 4);
318 if (pd >= PD_CPU_1 && pd <= PD_CPU_3) {
319 writel_relaxed(0x20002 << (pd - PD_CPU_1),
320 RK_CRU_VIRT + RK312X_CRU_SOFTRSTS_CON(0));
326 if (((pd == PD_GPU) || (pd == PD_VIO) || (pd == PD_VIDEO)))
327 ret = rk312x_pmu_set_power_domain(pd, on);
329 for (i = 0; i < RK312X_CRU_CLKGATES_CON_CNT; i++) {
330 cru_writel(clks_save[i] | 0xffff0000
331 , RK312X_CRU_CLKGATES_CON(i));
337 static void __init rk312x_dt_init_timer(void)
339 rockchip_pmu_ops.set_power_domain = rk312x_sys_set_power_domain;
340 rockchip_pmu_ops.power_domain_is_on = rk312x_pmu_power_domain_is_on;
341 rockchip_pmu_ops.set_idle_request = rk312x_pmu_set_idle_request;
343 clocksource_of_init();
347 static void __init rk312x_reserve(void)
349 /* reserve memory for ION */
350 rockchip_ion_reserve();
353 static void __init rk321x_init_suspend(void);
355 static void __init rk312x_init_late(void)
358 rk321x_init_suspend();
362 static void rk312x_restart(char mode, const char *cmd)
364 u32 boot_flag, boot_mode;
366 rockchip_restart_get_boot_mode(cmd, &boot_flag, &boot_mode);
369 writel_relaxed(boot_flag, RK_PMU_VIRT + RK312X_PMU_SYS_REG0);
371 writel_relaxed(boot_mode, RK_PMU_VIRT + RK312X_PMU_SYS_REG1);
375 /* pll enter slow mode */
376 writel_relaxed(0x11010000, RK_CRU_VIRT + RK312X_CRU_MODE_CON);
378 writel_relaxed(0xeca8, RK_CRU_VIRT + RK312X_CRU_GLB_SRST_SND_VALUE);
382 DT_MACHINE_START(RK3126_DT, "Rockchip RK3126")
383 .smp = smp_ops(rockchip_smp_ops),
384 .map_io = rk3126_dt_map_io,
385 .init_time = rk312x_dt_init_timer,
386 .dt_compat = rk3126_dt_compat,
387 .init_late = rk312x_init_late,
388 .reserve = rk312x_reserve,
389 .restart = rk312x_restart,
392 DT_MACHINE_START(RK3128_DT, "Rockchip RK3128")
393 .smp = smp_ops(rockchip_smp_ops),
394 .map_io = rk3128_dt_map_io,
395 .init_time = rk312x_dt_init_timer,
396 .dt_compat = rk3128_dt_compat,
397 .init_late = rk312x_init_late,
398 .reserve = rk312x_reserve,
399 .restart = rk312x_restart,
403 char PIE_DATA(sram_stack)[1024];
404 EXPORT_PIE_SYMBOL(DATA(sram_stack));
406 static int __init rk312x_pie_init(void)
410 if (!cpu_is_rk312x())
413 err = rockchip_pie_init();
417 rockchip_pie_chunk = pie_load_sections(rockchip_sram_pool, rk312x);
418 if (IS_ERR(rockchip_pie_chunk)) {
419 err = PTR_ERR(rockchip_pie_chunk);
420 pr_err("%s: failed to load section %d\n", __func__, err);
421 rockchip_pie_chunk = NULL;
425 rockchip_sram_virt = kern_to_pie(rockchip_pie_chunk, &__pie_common_start[0]);
426 rockchip_sram_stack = kern_to_pie(rockchip_pie_chunk, (char *)DATA(sram_stack) + sizeof(DATA(sram_stack)));
430 arch_initcall(rk312x_pie_init);
432 #include "ddr_rk3126.c"
433 static int __init rk312x_ddr_init(void)
435 if (cpu_is_rk312x()) {
436 ddr_change_freq = _ddr_change_freq;
437 ddr_round_rate = _ddr_round_rate;
438 ddr_set_auto_self_refresh = _ddr_set_auto_self_refresh;
439 ddr_bandwidth_get = _ddr_bandwidth_get;
440 ddr_init(DDR3_DEFAULT, 300);
444 arch_initcall_sync(rk312x_ddr_init);
447 static u32 rk_pmu_pwrdn_st;
448 static inline void rk_pm_soc_pd_suspend(void)
450 rk_pmu_pwrdn_st = pmu_readl(RK312X_PMU_PWRDN_ST);
451 if (!(rk_pmu_pwrdn_st & BIT(pmu_st_map[PD_GPU])))
452 rk312x_sys_set_power_domain(PD_GPU, false);
454 if (!(rk_pmu_pwrdn_st & BIT(pmu_st_map[PD_VIO])))
455 rk312x_sys_set_power_domain(PD_VIO, false);
457 if (!(rk_pmu_pwrdn_st & BIT(pmu_st_map[PD_VIDEO])))
458 rk312x_sys_set_power_domain(PD_VIDEO, false);
460 static inline void rk_pm_soc_pd_resume(void)
462 if (!(rk_pmu_pwrdn_st & BIT(pmu_st_map[PD_VIDEO])))
463 rk312x_sys_set_power_domain(PD_VIDEO, true);
465 if (!(rk_pmu_pwrdn_st & BIT(pmu_st_map[PD_VIO])))
466 rk312x_sys_set_power_domain(PD_VIO, true);
468 if (!(rk_pmu_pwrdn_st & BIT(pmu_st_map[PD_GPU])))
469 rk312x_sys_set_power_domain(PD_GPU, true);
471 static void __init rk321x_init_suspend(void)
473 pr_info("%s\n", __func__);
474 rockchip_suspend_init();
476 rk312x_suspend_init();
477 rkpm_set_ops_pwr_dmns(rk_pm_soc_pd_suspend, rk_pm_soc_pd_resume);