Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 1 | /* linux/arch/arm/mach-exynos4/cpuidle.c |
| 2 | * |
| 3 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. |
| 4 | * http://www.samsung.com |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/cpuidle.h> |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 14 | #include <linux/cpu_pm.h> |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 15 | #include <linux/io.h> |
Kyungmin Park | 76ee455 | 2011-11-08 19:57:59 +0900 | [diff] [blame] | 16 | #include <linux/export.h> |
Arnd Bergmann | 96c3a25 | 2014-03-19 18:29:36 +0100 | [diff] [blame^] | 17 | #include <linux/module.h> |
Kyungmin Park | 76ee455 | 2011-11-08 19:57:59 +0900 | [diff] [blame] | 18 | #include <linux/time.h> |
Bartlomiej Zolnierkiewicz | 35baa33 | 2013-08-30 12:15:04 +0200 | [diff] [blame] | 19 | #include <linux/platform_device.h> |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 20 | |
| 21 | #include <asm/proc-fns.h> |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 22 | #include <asm/smp_scu.h> |
| 23 | #include <asm/suspend.h> |
| 24 | #include <asm/unified.h> |
Amit Daniel Kachhap | 06c77b3 | 2012-05-12 16:29:21 +0900 | [diff] [blame] | 25 | #include <asm/cpuidle.h> |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 26 | |
| 27 | #include <plat/cpu.h> |
Amit Daniel Kachhap | 8969301 | 2013-07-24 14:06:13 +0900 | [diff] [blame] | 28 | #include <plat/pm.h> |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 29 | |
Kukjin Kim | b5fd130 | 2013-12-19 04:22:09 +0900 | [diff] [blame] | 30 | #include <mach/pm-core.h> |
| 31 | #include <mach/map.h> |
| 32 | |
Kukjin Kim | ccd458c | 2012-12-31 10:06:48 -0800 | [diff] [blame] | 33 | #include "common.h" |
Kukjin Kim | 65c9a85 | 2013-12-19 04:06:56 +0900 | [diff] [blame] | 34 | #include "regs-pmu.h" |
Kukjin Kim | ccd458c | 2012-12-31 10:06:48 -0800 | [diff] [blame] | 35 | |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 36 | #define REG_DIRECTGO_ADDR (samsung_rev() == EXYNOS4210_REV_1_1 ? \ |
| 37 | S5P_INFORM7 : (samsung_rev() == EXYNOS4210_REV_1_0 ? \ |
| 38 | (S5P_VA_SYSRAM + 0x24) : S5P_INFORM0)) |
| 39 | #define REG_DIRECTGO_FLAG (samsung_rev() == EXYNOS4210_REV_1_1 ? \ |
| 40 | S5P_INFORM6 : (samsung_rev() == EXYNOS4210_REV_1_0 ? \ |
| 41 | (S5P_VA_SYSRAM + 0x20) : S5P_INFORM1)) |
| 42 | |
| 43 | #define S5P_CHECK_AFTR 0xFCBA0D10 |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 44 | |
Kukjin Kim | b5fd130 | 2013-12-19 04:22:09 +0900 | [diff] [blame] | 45 | #define EXYNOS5_PWR_CTRL1 (S5P_VA_CMU + 0x01020) |
| 46 | #define EXYNOS5_PWR_CTRL2 (S5P_VA_CMU + 0x01024) |
| 47 | |
| 48 | #define PWR_CTRL1_CORE2_DOWN_RATIO (7 << 28) |
| 49 | #define PWR_CTRL1_CORE1_DOWN_RATIO (7 << 16) |
| 50 | #define PWR_CTRL1_DIV2_DOWN_EN (1 << 9) |
| 51 | #define PWR_CTRL1_DIV1_DOWN_EN (1 << 8) |
| 52 | #define PWR_CTRL1_USE_CORE1_WFE (1 << 5) |
| 53 | #define PWR_CTRL1_USE_CORE0_WFE (1 << 4) |
| 54 | #define PWR_CTRL1_USE_CORE1_WFI (1 << 1) |
| 55 | #define PWR_CTRL1_USE_CORE0_WFI (1 << 0) |
| 56 | |
| 57 | #define PWR_CTRL2_DIV2_UP_EN (1 << 25) |
| 58 | #define PWR_CTRL2_DIV1_UP_EN (1 << 24) |
| 59 | #define PWR_CTRL2_DUR_STANDBY2_VAL (1 << 16) |
| 60 | #define PWR_CTRL2_DUR_STANDBY1_VAL (1 << 8) |
| 61 | #define PWR_CTRL2_CORE2_UP_RATIO (1 << 4) |
| 62 | #define PWR_CTRL2_CORE1_UP_RATIO (1 << 0) |
| 63 | |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 64 | static int exynos4_enter_lowpower(struct cpuidle_device *dev, |
| 65 | struct cpuidle_driver *drv, |
| 66 | int index); |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 67 | |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 68 | static DEFINE_PER_CPU(struct cpuidle_device, exynos4_cpuidle_device); |
| 69 | |
| 70 | static struct cpuidle_driver exynos4_idle_driver = { |
Amit Daniel Kachhap | 06c77b3 | 2012-05-12 16:29:21 +0900 | [diff] [blame] | 71 | .name = "exynos4_idle", |
| 72 | .owner = THIS_MODULE, |
Daniel Lezcano | 2eb89f8 | 2013-01-18 21:57:58 -0800 | [diff] [blame] | 73 | .states = { |
| 74 | [0] = ARM_CPUIDLE_WFI_STATE, |
| 75 | [1] = { |
| 76 | .enter = exynos4_enter_lowpower, |
| 77 | .exit_latency = 300, |
| 78 | .target_residency = 100000, |
| 79 | .flags = CPUIDLE_FLAG_TIME_VALID, |
| 80 | .name = "C1", |
| 81 | .desc = "ARM power down", |
| 82 | }, |
| 83 | }, |
| 84 | .state_count = 2, |
| 85 | .safe_state_index = 0, |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 86 | }; |
| 87 | |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 88 | /* Ext-GIC nIRQ/nFIQ is the only wakeup source in AFTR */ |
| 89 | static void exynos4_set_wakeupmask(void) |
| 90 | { |
| 91 | __raw_writel(0x0000ff3e, S5P_WAKEUP_MASK); |
| 92 | } |
| 93 | |
| 94 | static unsigned int g_pwr_ctrl, g_diag_reg; |
| 95 | |
| 96 | static void save_cpu_arch_register(void) |
| 97 | { |
| 98 | /*read power control register*/ |
| 99 | asm("mrc p15, 0, %0, c15, c0, 0" : "=r"(g_pwr_ctrl) : : "cc"); |
| 100 | /*read diagnostic register*/ |
| 101 | asm("mrc p15, 0, %0, c15, c0, 1" : "=r"(g_diag_reg) : : "cc"); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | static void restore_cpu_arch_register(void) |
| 106 | { |
| 107 | /*write power control register*/ |
| 108 | asm("mcr p15, 0, %0, c15, c0, 0" : : "r"(g_pwr_ctrl) : "cc"); |
| 109 | /*write diagnostic register*/ |
| 110 | asm("mcr p15, 0, %0, c15, c0, 1" : : "r"(g_diag_reg) : "cc"); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | static int idle_finisher(unsigned long flags) |
| 115 | { |
| 116 | cpu_do_idle(); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | static int exynos4_enter_core0_aftr(struct cpuidle_device *dev, |
| 121 | struct cpuidle_driver *drv, |
| 122 | int index) |
| 123 | { |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 124 | unsigned long tmp; |
| 125 | |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 126 | exynos4_set_wakeupmask(); |
| 127 | |
| 128 | /* Set value of power down register for aftr mode */ |
Jongpill Lee | 7d44d2b | 2012-02-17 09:51:31 +0900 | [diff] [blame] | 129 | exynos_sys_powerdown_conf(SYS_AFTR); |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 130 | |
| 131 | __raw_writel(virt_to_phys(s3c_cpu_resume), REG_DIRECTGO_ADDR); |
| 132 | __raw_writel(S5P_CHECK_AFTR, REG_DIRECTGO_FLAG); |
| 133 | |
| 134 | save_cpu_arch_register(); |
| 135 | |
| 136 | /* Setting Central Sequence Register for power down mode */ |
| 137 | tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION); |
| 138 | tmp &= ~S5P_CENTRAL_LOWPWR_CFG; |
| 139 | __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION); |
| 140 | |
| 141 | cpu_pm_enter(); |
| 142 | cpu_suspend(0, idle_finisher); |
| 143 | |
| 144 | #ifdef CONFIG_SMP |
Abhilash Kesavan | a633208 | 2012-11-22 14:46:34 +0900 | [diff] [blame] | 145 | if (!soc_is_exynos5250()) |
| 146 | scu_enable(S5P_VA_SCU); |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 147 | #endif |
| 148 | cpu_pm_exit(); |
| 149 | |
| 150 | restore_cpu_arch_register(); |
| 151 | |
| 152 | /* |
| 153 | * If PMU failed while entering sleep mode, WFI will be |
| 154 | * ignored by PMU and then exiting cpu_do_idle(). |
| 155 | * S5P_CENTRAL_LOWPWR_CFG bit will not be set automatically |
| 156 | * in this situation. |
| 157 | */ |
| 158 | tmp = __raw_readl(S5P_CENTRAL_SEQ_CONFIGURATION); |
| 159 | if (!(tmp & S5P_CENTRAL_LOWPWR_CFG)) { |
| 160 | tmp |= S5P_CENTRAL_LOWPWR_CFG; |
| 161 | __raw_writel(tmp, S5P_CENTRAL_SEQ_CONFIGURATION); |
| 162 | } |
| 163 | |
| 164 | /* Clear wakeup state register */ |
| 165 | __raw_writel(0x0, S5P_WAKEUP_STAT); |
| 166 | |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 167 | return index; |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 168 | } |
| 169 | |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 170 | static int exynos4_enter_lowpower(struct cpuidle_device *dev, |
| 171 | struct cpuidle_driver *drv, |
| 172 | int index) |
| 173 | { |
| 174 | int new_index = index; |
| 175 | |
Bartlomiej Zolnierkiewicz | 118f5c1 | 2013-12-20 19:47:23 +0100 | [diff] [blame] | 176 | /* AFTR can only be entered when cores other than CPU0 are offline */ |
| 177 | if (num_online_cpus() > 1 || dev->cpu != 0) |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 178 | new_index = drv->safe_state_index; |
| 179 | |
| 180 | if (new_index == 0) |
Amit Daniel Kachhap | 06c77b3 | 2012-05-12 16:29:21 +0900 | [diff] [blame] | 181 | return arm_cpuidle_simple_enter(dev, drv, new_index); |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 182 | else |
| 183 | return exynos4_enter_core0_aftr(dev, drv, new_index); |
| 184 | } |
| 185 | |
Abhilash Kesavan | 0f9e035 | 2012-11-20 20:34:58 +0900 | [diff] [blame] | 186 | static void __init exynos5_core_down_clk(void) |
| 187 | { |
| 188 | unsigned int tmp; |
| 189 | |
| 190 | /* |
| 191 | * Enable arm clock down (in idle) and set arm divider |
| 192 | * ratios in WFI/WFE state. |
| 193 | */ |
| 194 | tmp = PWR_CTRL1_CORE2_DOWN_RATIO | \ |
| 195 | PWR_CTRL1_CORE1_DOWN_RATIO | \ |
| 196 | PWR_CTRL1_DIV2_DOWN_EN | \ |
| 197 | PWR_CTRL1_DIV1_DOWN_EN | \ |
| 198 | PWR_CTRL1_USE_CORE1_WFE | \ |
| 199 | PWR_CTRL1_USE_CORE0_WFE | \ |
| 200 | PWR_CTRL1_USE_CORE1_WFI | \ |
| 201 | PWR_CTRL1_USE_CORE0_WFI; |
| 202 | __raw_writel(tmp, EXYNOS5_PWR_CTRL1); |
| 203 | |
| 204 | /* |
| 205 | * Enable arm clock up (on exiting idle). Set arm divider |
| 206 | * ratios when not in idle along with the standby duration |
| 207 | * ratios. |
| 208 | */ |
| 209 | tmp = PWR_CTRL2_DIV2_UP_EN | \ |
| 210 | PWR_CTRL2_DIV1_UP_EN | \ |
| 211 | PWR_CTRL2_DUR_STANDBY2_VAL | \ |
| 212 | PWR_CTRL2_DUR_STANDBY1_VAL | \ |
| 213 | PWR_CTRL2_CORE2_UP_RATIO | \ |
| 214 | PWR_CTRL2_CORE1_UP_RATIO; |
| 215 | __raw_writel(tmp, EXYNOS5_PWR_CTRL2); |
| 216 | } |
| 217 | |
Jingoo Han | f612a4f | 2013-10-21 10:53:03 +0900 | [diff] [blame] | 218 | static int exynos_cpuidle_probe(struct platform_device *pdev) |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 219 | { |
Daniel Lezcano | 5db9f43 | 2013-01-18 21:57:58 -0800 | [diff] [blame] | 220 | int cpu_id, ret; |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 221 | struct cpuidle_device *device; |
| 222 | |
Abhilash Kesavan | 0f9e035 | 2012-11-20 20:34:58 +0900 | [diff] [blame] | 223 | if (soc_is_exynos5250()) |
| 224 | exynos5_core_down_clk(); |
| 225 | |
Amit Daniel Kachhap | 1e9fec0 | 2013-08-28 00:48:24 +0900 | [diff] [blame] | 226 | if (soc_is_exynos5440()) |
| 227 | exynos4_idle_driver.state_count = 1; |
| 228 | |
Daniel Lezcano | 5db9f43 | 2013-01-18 21:57:58 -0800 | [diff] [blame] | 229 | ret = cpuidle_register_driver(&exynos4_idle_driver); |
| 230 | if (ret) { |
Jingoo Han | ae7c4c8 | 2013-10-21 10:52:15 +0900 | [diff] [blame] | 231 | dev_err(&pdev->dev, "failed to register cpuidle driver\n"); |
Daniel Lezcano | 5db9f43 | 2013-01-18 21:57:58 -0800 | [diff] [blame] | 232 | return ret; |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 233 | } |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 234 | |
Daniel Lezcano | 329afd2 | 2013-01-18 21:57:58 -0800 | [diff] [blame] | 235 | for_each_online_cpu(cpu_id) { |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 236 | device = &per_cpu(exynos4_cpuidle_device, cpu_id); |
| 237 | device->cpu = cpu_id; |
| 238 | |
Daniel Lezcano | 5db9f43 | 2013-01-18 21:57:58 -0800 | [diff] [blame] | 239 | ret = cpuidle_register_device(device); |
| 240 | if (ret) { |
Jingoo Han | ae7c4c8 | 2013-10-21 10:52:15 +0900 | [diff] [blame] | 241 | dev_err(&pdev->dev, "failed to register cpuidle device\n"); |
Daniel Lezcano | 5db9f43 | 2013-01-18 21:57:58 -0800 | [diff] [blame] | 242 | return ret; |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 243 | } |
| 244 | } |
Amit Daniel Kachhap | 67173ca | 2012-03-08 02:07:27 -0800 | [diff] [blame] | 245 | |
Jaecheol Lee | 3d73998 | 2011-03-16 07:28:23 +0900 | [diff] [blame] | 246 | return 0; |
| 247 | } |
Bartlomiej Zolnierkiewicz | 35baa33 | 2013-08-30 12:15:04 +0200 | [diff] [blame] | 248 | |
| 249 | static struct platform_driver exynos_cpuidle_driver = { |
| 250 | .probe = exynos_cpuidle_probe, |
| 251 | .driver = { |
| 252 | .name = "exynos_cpuidle", |
| 253 | .owner = THIS_MODULE, |
| 254 | }, |
| 255 | }; |
| 256 | |
| 257 | module_platform_driver(exynos_cpuidle_driver); |