blob: 93d2decc112d629728d6736dbadc6690c1ce4f84 [file] [log] [blame]
Jaecheol Lee3d739982011-03-16 07:28:23 +09001/* 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 Kachhap67173ca2012-03-08 02:07:27 -080014#include <linux/cpu_pm.h>
Jaecheol Lee3d739982011-03-16 07:28:23 +090015#include <linux/io.h>
Kyungmin Park76ee4552011-11-08 19:57:59 +090016#include <linux/export.h>
Arnd Bergmann96c3a252014-03-19 18:29:36 +010017#include <linux/module.h>
Kyungmin Park76ee4552011-11-08 19:57:59 +090018#include <linux/time.h>
Bartlomiej Zolnierkiewicz35baa332013-08-30 12:15:04 +020019#include <linux/platform_device.h>
Jaecheol Lee3d739982011-03-16 07:28:23 +090020
21#include <asm/proc-fns.h>
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -080022#include <asm/smp_scu.h>
23#include <asm/suspend.h>
24#include <asm/unified.h>
Amit Daniel Kachhap06c77b32012-05-12 16:29:21 +090025#include <asm/cpuidle.h>
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -080026
27#include <plat/cpu.h>
Amit Daniel Kachhap89693012013-07-24 14:06:13 +090028#include <plat/pm.h>
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -080029
Kukjin Kimb5fd1302013-12-19 04:22:09 +090030#include <mach/pm-core.h>
31#include <mach/map.h>
32
Kukjin Kimccd458c2012-12-31 10:06:48 -080033#include "common.h"
Kukjin Kim65c9a852013-12-19 04:06:56 +090034#include "regs-pmu.h"
Kukjin Kimccd458c2012-12-31 10:06:48 -080035
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -080036#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 Lee3d739982011-03-16 07:28:23 +090044
Kukjin Kimb5fd1302013-12-19 04:22:09 +090045#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 Kachhap67173ca2012-03-08 02:07:27 -080064static int exynos4_enter_lowpower(struct cpuidle_device *dev,
65 struct cpuidle_driver *drv,
66 int index);
Jaecheol Lee3d739982011-03-16 07:28:23 +090067
Jaecheol Lee3d739982011-03-16 07:28:23 +090068static DEFINE_PER_CPU(struct cpuidle_device, exynos4_cpuidle_device);
69
70static struct cpuidle_driver exynos4_idle_driver = {
Amit Daniel Kachhap06c77b32012-05-12 16:29:21 +090071 .name = "exynos4_idle",
72 .owner = THIS_MODULE,
Daniel Lezcano2eb89f82013-01-18 21:57:58 -080073 .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 Lee3d739982011-03-16 07:28:23 +090086};
87
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -080088/* Ext-GIC nIRQ/nFIQ is the only wakeup source in AFTR */
89static void exynos4_set_wakeupmask(void)
90{
91 __raw_writel(0x0000ff3e, S5P_WAKEUP_MASK);
92}
93
94static unsigned int g_pwr_ctrl, g_diag_reg;
95
96static 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
105static 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
114static int idle_finisher(unsigned long flags)
115{
116 cpu_do_idle();
117 return 1;
118}
119
120static int exynos4_enter_core0_aftr(struct cpuidle_device *dev,
121 struct cpuidle_driver *drv,
122 int index)
123{
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -0800124 unsigned long tmp;
125
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -0800126 exynos4_set_wakeupmask();
127
128 /* Set value of power down register for aftr mode */
Jongpill Lee7d44d2b2012-02-17 09:51:31 +0900129 exynos_sys_powerdown_conf(SYS_AFTR);
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -0800130
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 Kesavana6332082012-11-22 14:46:34 +0900145 if (!soc_is_exynos5250())
146 scu_enable(S5P_VA_SCU);
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -0800147#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 Dharware978aa72011-10-28 16:20:09 +0530167 return index;
Jaecheol Lee3d739982011-03-16 07:28:23 +0900168}
169
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -0800170static int exynos4_enter_lowpower(struct cpuidle_device *dev,
171 struct cpuidle_driver *drv,
172 int index)
173{
174 int new_index = index;
175
Bartlomiej Zolnierkiewicz118f5c12013-12-20 19:47:23 +0100176 /* AFTR can only be entered when cores other than CPU0 are offline */
177 if (num_online_cpus() > 1 || dev->cpu != 0)
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -0800178 new_index = drv->safe_state_index;
179
180 if (new_index == 0)
Amit Daniel Kachhap06c77b32012-05-12 16:29:21 +0900181 return arm_cpuidle_simple_enter(dev, drv, new_index);
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -0800182 else
183 return exynos4_enter_core0_aftr(dev, drv, new_index);
184}
185
Abhilash Kesavan0f9e0352012-11-20 20:34:58 +0900186static 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 Hanf612a4f2013-10-21 10:53:03 +0900218static int exynos_cpuidle_probe(struct platform_device *pdev)
Jaecheol Lee3d739982011-03-16 07:28:23 +0900219{
Daniel Lezcano5db9f432013-01-18 21:57:58 -0800220 int cpu_id, ret;
Jaecheol Lee3d739982011-03-16 07:28:23 +0900221 struct cpuidle_device *device;
222
Abhilash Kesavan0f9e0352012-11-20 20:34:58 +0900223 if (soc_is_exynos5250())
224 exynos5_core_down_clk();
225
Amit Daniel Kachhap1e9fec02013-08-28 00:48:24 +0900226 if (soc_is_exynos5440())
227 exynos4_idle_driver.state_count = 1;
228
Daniel Lezcano5db9f432013-01-18 21:57:58 -0800229 ret = cpuidle_register_driver(&exynos4_idle_driver);
230 if (ret) {
Jingoo Hanae7c4c82013-10-21 10:52:15 +0900231 dev_err(&pdev->dev, "failed to register cpuidle driver\n");
Daniel Lezcano5db9f432013-01-18 21:57:58 -0800232 return ret;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530233 }
Jaecheol Lee3d739982011-03-16 07:28:23 +0900234
Daniel Lezcano329afd22013-01-18 21:57:58 -0800235 for_each_online_cpu(cpu_id) {
Jaecheol Lee3d739982011-03-16 07:28:23 +0900236 device = &per_cpu(exynos4_cpuidle_device, cpu_id);
237 device->cpu = cpu_id;
238
Daniel Lezcano5db9f432013-01-18 21:57:58 -0800239 ret = cpuidle_register_device(device);
240 if (ret) {
Jingoo Hanae7c4c82013-10-21 10:52:15 +0900241 dev_err(&pdev->dev, "failed to register cpuidle device\n");
Daniel Lezcano5db9f432013-01-18 21:57:58 -0800242 return ret;
Jaecheol Lee3d739982011-03-16 07:28:23 +0900243 }
244 }
Amit Daniel Kachhap67173ca2012-03-08 02:07:27 -0800245
Jaecheol Lee3d739982011-03-16 07:28:23 +0900246 return 0;
247}
Bartlomiej Zolnierkiewicz35baa332013-08-30 12:15:04 +0200248
249static struct platform_driver exynos_cpuidle_driver = {
250 .probe = exynos_cpuidle_probe,
251 .driver = {
252 .name = "exynos_cpuidle",
253 .owner = THIS_MODULE,
254 },
255};
256
257module_platform_driver(exynos_cpuidle_driver);