blob: 3b78cf5f17d77a85823230db6ec794d246d0a3ef [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* arch/arm/mach-msm/pm2.c
2 *
3 * MSM Power Management Routines
4 *
5 * Copyright (C) 2007 Google, Inc.
Murali Nalajala0df9fee2012-01-12 15:26:09 +05306 * Copyright (c) 2008-2012 Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07007 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/clk.h>
22#include <linux/delay.h>
23#include <linux/init.h>
24#include <linux/pm.h>
25#include <linux/pm_qos_params.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/suspend.h>
27#include <linux/reboot.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <linux/io.h>
Murali Nalajala8fda4492012-03-19 18:22:59 +053029#include <linux/tick.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070030#include <linux/memory.h>
31#ifdef CONFIG_HAS_WAKELOCK
32#include <linux/wakelock.h>
33#endif
34#include <mach/msm_iomap.h>
35#include <mach/system.h>
36#ifdef CONFIG_CPU_V7
37#include <asm/pgtable.h>
38#include <asm/pgalloc.h>
39#endif
40#ifdef CONFIG_CACHE_L2X0
41#include <asm/hardware/cache-l2x0.h>
42#endif
43#ifdef CONFIG_VFP
44#include <asm/vfp.h>
45#endif
46
47#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN
48#include <mach/msm_migrate_pages.h>
49#endif
Murali Nalajala41786ab2012-03-06 10:47:32 +053050#include <mach/socinfo.h>
Anji jonnala1f2377c2012-03-27 14:35:55 +053051#include <asm/smp_scu.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070052
53#include "smd_private.h"
54#include "smd_rpcrouter.h"
55#include "acpuclock.h"
56#include "clock.h"
57#include "proc_comm.h"
58#include "idle.h"
59#include "irq.h"
60#include "gpio.h"
61#include "timer.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080062#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070063#include "spm.h"
64#include "sirc.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060065#include "pm-boot.h"
Murali Nalajala19d33a22012-05-18 14:11:19 +053066#include "devices-msm7x2xa.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067
68/******************************************************************************
69 * Debug Definitions
70 *****************************************************************************/
71
72enum {
Murali Nalajalaa7efba12012-02-23 18:13:52 +053073 MSM_PM_DEBUG_SUSPEND = BIT(0),
74 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
75 MSM_PM_DEBUG_STATE = BIT(2),
76 MSM_PM_DEBUG_CLOCK = BIT(3),
77 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
78 MSM_PM_DEBUG_SMSM_STATE = BIT(5),
79 MSM_PM_DEBUG_IDLE = BIT(6),
80 MSM_PM_DEBUG_HOTPLUG = BIT(7),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070081};
82
83static int msm_pm_debug_mask;
Taniya Dase30a6b22012-03-20 11:37:45 +053084int power_collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085module_param_named(
86 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
87);
88
89#define MSM_PM_DPRINTK(mask, level, message, ...) \
90 do { \
91 if ((mask) & msm_pm_debug_mask) \
92 printk(level message, ## __VA_ARGS__); \
93 } while (0)
94
95#define MSM_PM_DEBUG_PRINT_STATE(tag) \
96 do { \
97 MSM_PM_DPRINTK(MSM_PM_DEBUG_STATE, \
98 KERN_INFO, "%s: " \
99 "APPS_CLK_SLEEP_EN %x, APPS_PWRDOWN %x, " \
100 "SMSM_POWER_MASTER_DEM %x, SMSM_MODEM_STATE %x, " \
101 "SMSM_APPS_DEM %x\n", \
102 tag, \
103 __raw_readl(APPS_CLK_SLEEP_EN), \
104 __raw_readl(APPS_PWRDOWN), \
105 smsm_get_state(SMSM_POWER_MASTER_DEM), \
106 smsm_get_state(SMSM_MODEM_STATE), \
107 smsm_get_state(SMSM_APPS_DEM)); \
108 } while (0)
109
110#define MSM_PM_DEBUG_PRINT_SLEEP_INFO() \
111 do { \
112 if (msm_pm_debug_mask & MSM_PM_DEBUG_SMSM_STATE) \
113 smsm_print_sleep_info(msm_pm_smem_data->sleep_time, \
114 msm_pm_smem_data->resources_used, \
115 msm_pm_smem_data->irq_mask, \
116 msm_pm_smem_data->wakeup_reason, \
117 msm_pm_smem_data->pending_irqs); \
118 } while (0)
119
120
121/******************************************************************************
122 * Sleep Modes and Parameters
123 *****************************************************************************/
124
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700125static int msm_pm_idle_sleep_min_time = CONFIG_MSM7X00A_IDLE_SLEEP_MIN_TIME;
126module_param_named(
127 idle_sleep_min_time, msm_pm_idle_sleep_min_time,
128 int, S_IRUGO | S_IWUSR | S_IWGRP
129);
130
131enum {
132 MSM_PM_MODE_ATTR_SUSPEND,
133 MSM_PM_MODE_ATTR_IDLE,
134 MSM_PM_MODE_ATTR_LATENCY,
135 MSM_PM_MODE_ATTR_RESIDENCY,
136 MSM_PM_MODE_ATTR_NR,
137};
138
139static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
140 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
141 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
142 [MSM_PM_MODE_ATTR_LATENCY] = "latency",
143 [MSM_PM_MODE_ATTR_RESIDENCY] = "residency",
144};
145
146static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
147 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_SUSPEND] = " ",
148 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700149 [MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT] =
150 "ramp_down_and_wfi",
151 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
152 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] =
153 "power_collapse_no_xo_shutdown",
154 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
155 "standalone_power_collapse",
156};
157
158static struct msm_pm_platform_data *msm_pm_modes;
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530159static struct msm_pm_irq_calls *msm_pm_irq_extns;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700160
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530161struct msm_pm_kobj_attribute {
162 unsigned int cpu;
163 struct kobj_attribute ka;
164};
165
166#define GET_CPU_OF_ATTR(attr) \
167 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
168
169struct msm_pm_sysfs_sleep_mode {
170 struct kobject *kobj;
171 struct attribute_group attr_group;
172 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
173 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
174};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175
176/*
177 * Write out the attribute.
178 */
179static ssize_t msm_pm_mode_attr_show(
180 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
181{
182 int ret = -EINVAL;
183 int i;
184
185 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
186 struct kernel_param kp;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530187 unsigned int cpu;
188 struct msm_pm_platform_data *mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189
190 if (msm_pm_sleep_mode_labels[i] == NULL)
191 continue;
192
193 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
194 continue;
195
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530196 cpu = GET_CPU_OF_ATTR(attr);
197 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
198
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199 if (!strcmp(attr->attr.name,
200 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530201 u32 arg = mode->suspend_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202 kp.arg = &arg;
203 ret = param_get_ulong(buf, &kp);
204 } else if (!strcmp(attr->attr.name,
205 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530206 u32 arg = mode->idle_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700207 kp.arg = &arg;
208 ret = param_get_ulong(buf, &kp);
209 } else if (!strcmp(attr->attr.name,
210 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530211 u32 arg = mode->latency;
212 kp.arg = &arg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 ret = param_get_ulong(buf, &kp);
214 } else if (!strcmp(attr->attr.name,
215 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530216 u32 arg = mode->residency;
217 kp.arg = &arg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700218 ret = param_get_ulong(buf, &kp);
219 }
220
221 break;
222 }
223
224 if (ret > 0) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530225 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 ret++;
227 }
228
229 return ret;
230}
231
232/*
233 * Read in the new attribute value.
234 */
235static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
236 struct kobj_attribute *attr, const char *buf, size_t count)
237{
238 int ret = -EINVAL;
239 int i;
240
241 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
242 struct kernel_param kp;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530243 unsigned int cpu;
244 struct msm_pm_platform_data *mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700245
246 if (msm_pm_sleep_mode_labels[i] == NULL)
247 continue;
248
249 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
250 continue;
251
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530252 cpu = GET_CPU_OF_ATTR(attr);
253 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
254
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 if (!strcmp(attr->attr.name,
256 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530257 kp.arg = &mode->suspend_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700258 ret = param_set_byte(buf, &kp);
259 } else if (!strcmp(attr->attr.name,
260 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530261 kp.arg = &mode->idle_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262 ret = param_set_byte(buf, &kp);
263 } else if (!strcmp(attr->attr.name,
264 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530265 kp.arg = &mode->latency;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700266 ret = param_set_ulong(buf, &kp);
267 } else if (!strcmp(attr->attr.name,
268 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530269 kp.arg = &mode->residency;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270 ret = param_set_ulong(buf, &kp);
271 }
272
273 break;
274 }
275
276 return ret ? ret : count;
277}
278
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530279 /* Add sysfs entries for one cpu. */
280static int __init msm_pm_mode_sysfs_add_cpu(
281 unsigned int cpu, struct kobject *modes_kobj)
282{
283 char cpu_name[8];
284 struct kobject *cpu_kobj;
285 struct msm_pm_sysfs_sleep_mode *mode = NULL;
286 int i, j, k;
287 int ret;
288
289 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
290 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
291 if (!cpu_kobj) {
292 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
293 ret = -ENOMEM;
294 goto mode_sysfs_add_cpu_exit;
295 }
296
297 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
298 int idx = MSM_PM_MODE(cpu, i);
299
300 if ((!msm_pm_modes[idx].suspend_supported) &&
301 (!msm_pm_modes[idx].idle_supported))
302 continue;
303
304 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
305 if (!mode) {
306 pr_err("%s: cannot allocate memory for attributes\n",
307 __func__);
308 ret = -ENOMEM;
309 goto mode_sysfs_add_cpu_exit;
310 }
311
312 mode->kobj = kobject_create_and_add(
313 msm_pm_sleep_mode_labels[i], cpu_kobj);
314 if (!mode->kobj) {
315 pr_err("%s: cannot create kobject\n", __func__);
316 ret = -ENOMEM;
317 goto mode_sysfs_add_cpu_exit;
318 }
319
320 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
321 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
322 !msm_pm_modes[idx].idle_supported)
323 continue;
324 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
325 !msm_pm_modes[idx].suspend_supported)
326 continue;
327 mode->kas[j].cpu = cpu;
328 mode->kas[j].ka.attr.mode = 0644;
329 mode->kas[j].ka.show = msm_pm_mode_attr_show;
330 mode->kas[j].ka.store = msm_pm_mode_attr_store;
331 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
332 mode->attrs[j] = &mode->kas[j].ka.attr;
333 j++;
334 }
335 mode->attrs[j] = NULL;
336
337 mode->attr_group.attrs = mode->attrs;
338 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
339 if (ret) {
340 printk(KERN_ERR
341 "%s: cannot create kobject attribute group\n",
342 __func__);
343 goto mode_sysfs_add_cpu_exit;
344 }
345 }
346
347 ret = 0;
348
349mode_sysfs_add_cpu_exit:
350 if (ret) {
351 if (mode && mode->kobj)
352 kobject_del(mode->kobj);
353 kfree(mode);
354 }
355
356 return ret;
357}
358
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700359/*
360 * Add sysfs entries for the sleep modes.
361 */
362static int __init msm_pm_mode_sysfs_add(void)
363{
364 struct kobject *module_kobj = NULL;
365 struct kobject *modes_kobj = NULL;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530366 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700367 int ret;
368
369 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
370 if (!module_kobj) {
371 printk(KERN_ERR "%s: cannot find kobject for module %s\n",
372 __func__, KBUILD_MODNAME);
373 ret = -ENOENT;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530374 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700375 }
376
377 modes_kobj = kobject_create_and_add("modes", module_kobj);
378 if (!modes_kobj) {
379 printk(KERN_ERR "%s: cannot create modes kobject\n", __func__);
380 ret = -ENOMEM;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530381 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700382 }
383
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530384 for_each_possible_cpu(cpu) {
385 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
386 if (ret)
387 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388 }
389
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530390 ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530392mode_sysfs_add_exit:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700393 return ret;
394}
395
396void __init msm_pm_set_platform_data(
397 struct msm_pm_platform_data *data, int count)
398{
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530399 BUG_ON(MSM_PM_SLEEP_MODE_NR * num_possible_cpus() > count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700400 msm_pm_modes = data;
401}
402
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530403void __init msm_pm_set_irq_extns(struct msm_pm_irq_calls *irq_calls)
404{
405 /* sanity check */
406 BUG_ON(irq_calls == NULL || irq_calls->irq_pending == NULL ||
407 irq_calls->idle_sleep_allowed == NULL ||
408 irq_calls->enter_sleep1 == NULL ||
409 irq_calls->enter_sleep2 == NULL ||
410 irq_calls->exit_sleep1 == NULL ||
411 irq_calls->exit_sleep2 == NULL ||
412 irq_calls->exit_sleep3 == NULL);
413
414 msm_pm_irq_extns = irq_calls;
415}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700416
417/******************************************************************************
418 * Sleep Limitations
419 *****************************************************************************/
420enum {
421 SLEEP_LIMIT_NONE = 0,
422 SLEEP_LIMIT_NO_TCXO_SHUTDOWN = 2,
423 SLEEP_LIMIT_MASK = 0x03,
424};
425
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530426static uint32_t msm_pm_sleep_limit = SLEEP_LIMIT_NONE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700427#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE
428enum {
429 SLEEP_RESOURCE_MEMORY_BIT0 = 0x0200,
430 SLEEP_RESOURCE_MEMORY_BIT1 = 0x0010,
431};
432#endif
433
434
435/******************************************************************************
436 * Configure Hardware for Power Down/Up
437 *****************************************************************************/
438
439#if defined(CONFIG_ARCH_MSM7X30)
Taniya Das298de8c2012-02-16 11:45:31 +0530440#define APPS_CLK_SLEEP_EN (MSM_APCS_GCC_BASE + 0x020)
441#define APPS_PWRDOWN (MSM_ACC0_BASE + 0x01c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700442#define APPS_SECOP (MSM_TCSR_BASE + 0x038)
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530443#define APPS_STANDBY_CTL NULL
444#else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700445#define APPS_CLK_SLEEP_EN (MSM_CSR_BASE + 0x11c)
446#define APPS_PWRDOWN (MSM_CSR_BASE + 0x440)
447#define APPS_STANDBY_CTL (MSM_CSR_BASE + 0x108)
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530448#define APPS_SECOP NULL
449#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450
451/*
452 * Configure hardware registers in preparation for Apps power down.
453 */
454static void msm_pm_config_hw_before_power_down(void)
455{
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530456 if (cpu_is_msm7x30() || cpu_is_msm8x55()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530457 __raw_writel(4, APPS_SECOP);
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530458 } else if (cpu_is_msm7x27()) {
459 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530460 } else if (cpu_is_msm7x27a() || cpu_is_msm7x27aa() ||
Pankaj Kumarfee56a82012-04-17 14:26:49 +0530461 cpu_is_msm7x25a() || cpu_is_msm7x25aa() ||
462 cpu_is_msm7x25ab()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530463 __raw_writel(0x7, APPS_CLK_SLEEP_EN);
Murali Nalajalab87e88c2012-05-18 15:12:13 +0530464 } else if (cpu_is_qsd8x50()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530465 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
466 mb();
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530467 __raw_writel(0, APPS_STANDBY_CTL);
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530468 }
Murali Nalajalab87e88c2012-05-18 15:12:13 +0530469 mb();
470 __raw_writel(1, APPS_PWRDOWN);
471 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472}
473
474/*
Anji jonnala1f2377c2012-03-27 14:35:55 +0530475 * Program the top csr from core0 context to put the
476 * core1 into GDFS, as core1 is not running yet.
477 */
478static void configure_top_csr(void)
479{
480 void __iomem *base_ptr;
481 unsigned int value = 0;
482
Murali Nalajala19d33a22012-05-18 14:11:19 +0530483 base_ptr = core1_reset_base();
Anji jonnala1f2377c2012-03-27 14:35:55 +0530484 if (!base_ptr)
485 return;
486
487 /* bring the core1 out of reset */
488 __raw_writel(0x3, base_ptr);
489 mb();
490 /*
491 * override DBGNOPOWERDN and program the GDFS
492 * count val
493 */
494
495 __raw_writel(0x00030002, (MSM_CFG_CTL_BASE + 0x38));
496 mb();
497
498 /* Initialize the SPM0 and SPM1 registers */
499 msm_spm_reinit();
500
501 /* enable TCSR for core1 */
502 value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG));
503 value |= BIT(22);
504 __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG);
505 mb();
506
507 /* set reset bit for SPM1 */
508 value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG));
509 value |= BIT(20);
510 __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG);
511 mb();
512
513 /* set CLK_OFF bit */
514 value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG));
515 value |= BIT(18);
516 __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG);
517 mb();
518
519 /* set clamps bit */
520 value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG));
521 value |= BIT(21);
522 __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG);
523 mb();
524
525 /* set power_up bit */
526 value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG));
527 value |= BIT(19);
528 __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG);
529 mb();
530
531 /* Disable TSCR for core0 */
532 value = __raw_readl((MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG));
533 value &= ~BIT(22);
534 __raw_writel(value, MSM_CFG_CTL_BASE + MPA5_CFG_CTL_REG);
535 mb();
536 __raw_writel(0x0, base_ptr);
537 mb();
Anji jonnala1f2377c2012-03-27 14:35:55 +0530538}
539
540/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700541 * Clear hardware registers after Apps powers up.
542 */
543static void msm_pm_config_hw_after_power_up(void)
544{
Anji jonnala1f2377c2012-03-27 14:35:55 +0530545
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530546 if (cpu_is_msm7x30() || cpu_is_msm8x55()) {
547 __raw_writel(0, APPS_SECOP);
548 mb();
549 __raw_writel(0, APPS_PWRDOWN);
550 mb();
551 msm_spm_reinit();
Murali Nalajalab87e88c2012-05-18 15:12:13 +0530552 } else if (cpu_is_msm8625()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530553 __raw_writel(0, APPS_PWRDOWN);
554 mb();
Anji jonnala1f2377c2012-03-27 14:35:55 +0530555
Murali Nalajalab87e88c2012-05-18 15:12:13 +0530556 if (power_collapsed) {
Anji jonnala1f2377c2012-03-27 14:35:55 +0530557 /*
558 * enable the SCU while coming out of power
559 * collapse.
560 */
561 scu_enable(MSM_SCU_BASE);
562 /*
563 * Program the top csr to put the core1 into GDFS.
564 */
565 configure_top_csr();
566 }
Murali Nalajalab87e88c2012-05-18 15:12:13 +0530567 } else {
568 __raw_writel(0, APPS_PWRDOWN);
569 mb();
570 __raw_writel(0, APPS_CLK_SLEEP_EN);
571 mb();
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530572 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573}
574
575/*
576 * Configure hardware registers in preparation for SWFI.
577 */
578static void msm_pm_config_hw_before_swfi(void)
579{
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530580 if (cpu_is_qsd8x50()) {
581 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
582 mb();
583 } else if (cpu_is_msm7x27()) {
584 __raw_writel(0x0f, APPS_CLK_SLEEP_EN);
585 mb();
586 } else if (cpu_is_msm7x27a() || cpu_is_msm7x27aa() ||
Pankaj Kumarfee56a82012-04-17 14:26:49 +0530587 cpu_is_msm7x25a() || cpu_is_msm7x25aa() ||
588 cpu_is_msm7x25ab()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530589 __raw_writel(0x7, APPS_CLK_SLEEP_EN);
590 mb();
591 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700592}
593
594/*
595 * Respond to timing out waiting for Modem
596 *
597 * NOTE: The function never returns.
598 */
599static void msm_pm_timeout(void)
600{
601#if defined(CONFIG_MSM_PM_TIMEOUT_RESET_CHIP)
602 printk(KERN_EMERG "%s(): resetting chip\n", __func__);
603 msm_proc_comm(PCOM_RESET_CHIP_IMM, NULL, NULL);
604#elif defined(CONFIG_MSM_PM_TIMEOUT_RESET_MODEM)
605 printk(KERN_EMERG "%s(): resetting modem\n", __func__);
606 msm_proc_comm_reset_modem_now();
607#elif defined(CONFIG_MSM_PM_TIMEOUT_HALT)
608 printk(KERN_EMERG "%s(): halting\n", __func__);
609#endif
610 for (;;)
611 ;
612}
613
614
615/******************************************************************************
616 * State Polling Definitions
617 *****************************************************************************/
618
619struct msm_pm_polled_group {
620 uint32_t group_id;
621
622 uint32_t bits_all_set;
623 uint32_t bits_all_clear;
624 uint32_t bits_any_set;
625 uint32_t bits_any_clear;
626
627 uint32_t value_read;
628};
629
630/*
631 * Return true if all bits indicated by flag are set in source.
632 */
633static inline bool msm_pm_all_set(uint32_t source, uint32_t flag)
634{
635 return (source & flag) == flag;
636}
637
638/*
639 * Return true if any bit indicated by flag are set in source.
640 */
641static inline bool msm_pm_any_set(uint32_t source, uint32_t flag)
642{
643 return !flag || (source & flag);
644}
645
646/*
647 * Return true if all bits indicated by flag are cleared in source.
648 */
649static inline bool msm_pm_all_clear(uint32_t source, uint32_t flag)
650{
651 return (~source & flag) == flag;
652}
653
654/*
655 * Return true if any bit indicated by flag are cleared in source.
656 */
657static inline bool msm_pm_any_clear(uint32_t source, uint32_t flag)
658{
659 return !flag || (~source & flag);
660}
661
662/*
663 * Poll the shared memory states as indicated by the poll groups.
664 *
665 * nr_grps: number of groups in the array
666 * grps: array of groups
667 *
668 * The function returns when conditions specified by any of the poll
669 * groups become true. The conditions specified by a poll group are
670 * deemed true when 1) at least one bit from bits_any_set is set OR one
671 * bit from bits_any_clear is cleared; and 2) all bits in bits_all_set
672 * are set; and 3) all bits in bits_all_clear are cleared.
673 *
674 * Return value:
675 * >=0: index of the poll group whose conditions have become true
676 * -ETIMEDOUT: timed out
677 */
678static int msm_pm_poll_state(int nr_grps, struct msm_pm_polled_group *grps)
679{
680 int i, k;
681
682 for (i = 0; i < 50000; i++) {
683 for (k = 0; k < nr_grps; k++) {
684 bool all_set, all_clear;
685 bool any_set, any_clear;
686
687 grps[k].value_read = smsm_get_state(grps[k].group_id);
688
689 all_set = msm_pm_all_set(grps[k].value_read,
690 grps[k].bits_all_set);
691 all_clear = msm_pm_all_clear(grps[k].value_read,
692 grps[k].bits_all_clear);
693 any_set = msm_pm_any_set(grps[k].value_read,
694 grps[k].bits_any_set);
695 any_clear = msm_pm_any_clear(grps[k].value_read,
696 grps[k].bits_any_clear);
697
698 if (all_set && all_clear && (any_set || any_clear))
699 return k;
700 }
701 udelay(50);
702 }
703
704 printk(KERN_ERR "%s failed:\n", __func__);
705 for (k = 0; k < nr_grps; k++)
706 printk(KERN_ERR "(%x, %x, %x, %x) %x\n",
707 grps[k].bits_all_set, grps[k].bits_all_clear,
708 grps[k].bits_any_set, grps[k].bits_any_clear,
709 grps[k].value_read);
710
711 return -ETIMEDOUT;
712}
713
714
715/******************************************************************************
716 * Suspend Max Sleep Time
717 *****************************************************************************/
718
719#define SCLK_HZ (32768)
720#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
721
722#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
723static int msm_pm_sleep_time_override;
724module_param_named(sleep_time_override,
725 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
726#endif
727
728static uint32_t msm_pm_max_sleep_time;
729
730/*
731 * Convert time from nanoseconds to slow clock ticks, then cap it to the
732 * specified limit
733 */
734static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
735{
736 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
737 return (time_ns > limit) ? limit : time_ns;
738}
739
740/*
741 * Set the sleep time for suspend. 0 means infinite sleep time.
742 */
743void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
744{
745 unsigned long flags;
746
747 local_irq_save(flags);
748 if (max_sleep_time_ns == 0) {
749 msm_pm_max_sleep_time = 0;
750 } else {
751 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
752 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
753
754 if (msm_pm_max_sleep_time == 0)
755 msm_pm_max_sleep_time = 1;
756 }
757
758 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
759 "%s(): Requested %lld ns Giving %u sclk ticks\n", __func__,
760 max_sleep_time_ns, msm_pm_max_sleep_time);
761 local_irq_restore(flags);
762}
763EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
764
765
766/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700767 * Shared Memory Bits
768 *****************************************************************************/
769
770#define DEM_MASTER_BITS_PER_CPU 6
771
772/* Power Master State Bits - Per CPU */
773#define DEM_MASTER_SMSM_RUN \
774 (0x01UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
775#define DEM_MASTER_SMSM_RSA \
776 (0x02UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
777#define DEM_MASTER_SMSM_PWRC_EARLY_EXIT \
778 (0x04UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
779#define DEM_MASTER_SMSM_SLEEP_EXIT \
780 (0x08UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
781#define DEM_MASTER_SMSM_READY \
782 (0x10UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
783#define DEM_MASTER_SMSM_SLEEP \
784 (0x20UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
785
786/* Power Slave State Bits */
787#define DEM_SLAVE_SMSM_RUN (0x0001)
788#define DEM_SLAVE_SMSM_PWRC (0x0002)
789#define DEM_SLAVE_SMSM_PWRC_DELAY (0x0004)
790#define DEM_SLAVE_SMSM_PWRC_EARLY_EXIT (0x0008)
791#define DEM_SLAVE_SMSM_WFPI (0x0010)
792#define DEM_SLAVE_SMSM_SLEEP (0x0020)
793#define DEM_SLAVE_SMSM_SLEEP_EXIT (0x0040)
794#define DEM_SLAVE_SMSM_MSGS_REDUCED (0x0080)
795#define DEM_SLAVE_SMSM_RESET (0x0100)
796#define DEM_SLAVE_SMSM_PWRC_SUSPEND (0x0200)
797
798
799/******************************************************************************
800 * Shared Memory Data
801 *****************************************************************************/
802
803#define DEM_MAX_PORT_NAME_LEN (20)
804
805struct msm_pm_smem_t {
806 uint32_t sleep_time;
807 uint32_t irq_mask;
808 uint32_t resources_used;
809 uint32_t reserved1;
810
811 uint32_t wakeup_reason;
812 uint32_t pending_irqs;
813 uint32_t rpc_prog;
814 uint32_t rpc_proc;
815 char smd_port_name[DEM_MAX_PORT_NAME_LEN];
816 uint32_t reserved2;
817};
818
819
820/******************************************************************************
821 *
822 *****************************************************************************/
823static struct msm_pm_smem_t *msm_pm_smem_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700824static atomic_t msm_pm_init_done = ATOMIC_INIT(0);
825
826static int msm_pm_modem_busy(void)
827{
828 if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) {
829 MSM_PM_DPRINTK(MSM_PM_DEBUG_POWER_COLLAPSE,
830 KERN_INFO, "%s(): master not ready\n", __func__);
831 return -EBUSY;
832 }
833
834 return 0;
835}
836
837/*
838 * Power collapse the Apps processor. This function executes the handshake
839 * protocol with Modem.
840 *
841 * Return value:
842 * -EAGAIN: modem reset occurred or early exit from power collapse
843 * -EBUSY: modem not ready for our power collapse -- no power loss
844 * -ETIMEDOUT: timed out waiting for modem's handshake -- no power loss
845 * 0: success
846 */
847static int msm_pm_power_collapse
848 (bool from_idle, uint32_t sleep_delay, uint32_t sleep_limit)
849{
850 struct msm_pm_polled_group state_grps[2];
851 unsigned long saved_acpuclk_rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700852 int collapsed = 0;
853 int ret;
Murali Nalajala07b04022012-04-10 16:00:49 +0530854 int val;
855 int modem_early_exit = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700856
857 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
858 KERN_INFO, "%s(): idle %d, delay %u, limit %u\n", __func__,
859 (int)from_idle, sleep_delay, sleep_limit);
860
861 if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) {
862 MSM_PM_DPRINTK(
863 MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
864 KERN_INFO, "%s(): master not ready\n", __func__);
865 ret = -EBUSY;
866 goto power_collapse_bail;
867 }
868
869 memset(msm_pm_smem_data, 0, sizeof(*msm_pm_smem_data));
870
Murali Nalajala41786ab2012-03-06 10:47:32 +0530871 if (cpu_is_msm8625()) {
872 /* Program the SPM */
873 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE,
874 false);
875 WARN_ON(ret);
876 }
877
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530878 msm_pm_irq_extns->enter_sleep1(true, from_idle,
879 &msm_pm_smem_data->irq_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 msm_sirc_enter_sleep();
881 msm_gpio_enter_sleep(from_idle);
882
883 msm_pm_smem_data->sleep_time = sleep_delay;
884 msm_pm_smem_data->resources_used = sleep_limit;
885
886 /* Enter PWRC/PWRC_SUSPEND */
887
888 if (from_idle)
889 smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN,
890 DEM_SLAVE_SMSM_PWRC);
891 else
892 smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN,
893 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND);
894
895 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC");
896 MSM_PM_DEBUG_PRINT_SLEEP_INFO();
897
898 memset(state_grps, 0, sizeof(state_grps));
899 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
900 state_grps[0].bits_all_set = DEM_MASTER_SMSM_RSA;
901 state_grps[1].group_id = SMSM_MODEM_STATE;
902 state_grps[1].bits_all_set = SMSM_RESET;
903
904 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
905
906 if (ret < 0) {
907 printk(KERN_EMERG "%s(): power collapse entry "
908 "timed out waiting for Modem's response\n", __func__);
909 msm_pm_timeout();
910 }
911
912 if (ret == 1) {
913 MSM_PM_DPRINTK(
914 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
915 KERN_INFO,
916 "%s(): msm_pm_poll_state detected Modem reset\n",
917 __func__);
918 goto power_collapse_early_exit;
919 }
920
921 /* DEM Master in RSA */
922
923 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC RSA");
924
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530925 ret = msm_pm_irq_extns->enter_sleep2(true, from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 if (ret < 0) {
927 MSM_PM_DPRINTK(
928 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
929 KERN_INFO,
930 "%s(): msm_irq_enter_sleep2 aborted, %d\n", __func__,
931 ret);
932 goto power_collapse_early_exit;
933 }
934
935 msm_pm_config_hw_before_power_down();
936 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): pre power down");
937
938 saved_acpuclk_rate = acpuclk_power_collapse();
939 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
940 "%s(): change clock rate (old rate = %lu)\n", __func__,
941 saved_acpuclk_rate);
942
943 if (saved_acpuclk_rate == 0) {
944 msm_pm_config_hw_after_power_up();
945 goto power_collapse_early_exit;
946 }
947
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600948 msm_pm_boot_config_before_pc(smp_processor_id(),
949 virt_to_phys(msm_pm_collapse_exit));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700950
951#ifdef CONFIG_VFP
952 if (from_idle)
953 vfp_flush_context();
954#endif
955
956#ifdef CONFIG_CACHE_L2X0
Taniya Das38a8c6e2012-05-09 20:34:39 +0530957 l2cc_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700958#endif
959
960 collapsed = msm_pm_collapse();
Murali Nalajala07b04022012-04-10 16:00:49 +0530961
962 /*
963 * TBD: Currently recognise the MODEM early exit
964 * path by reading the MPA5_GDFS_CNT_VAL register.
965 */
966 if (cpu_is_msm8625()) {
967 /*
Murali Nalajala93e6ed02012-05-13 12:57:22 +0530968 * on system reset, default value of MPA5_GDFS_CNT_VAL
969 * is = 0x0, later modem reprogram this value to
970 * 0x00030004. Once APPS did a power collapse and
971 * coming out of it expected value of this register
972 * always be 0x00030004. Incase if APPS sees the value
973 * as 0x00030002 consider this case as a modem early
974 * exit.
Murali Nalajala07b04022012-04-10 16:00:49 +0530975 */
976 val = __raw_readl(MSM_CFG_CTL_BASE + 0x38);
Murali Nalajala93e6ed02012-05-13 12:57:22 +0530977 if (val != 0x00030002)
Murali Nalajala07b04022012-04-10 16:00:49 +0530978 power_collapsed = 1;
Murali Nalajala93e6ed02012-05-13 12:57:22 +0530979 else
980 modem_early_exit = 1;
Murali Nalajala07b04022012-04-10 16:00:49 +0530981 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700982
983#ifdef CONFIG_CACHE_L2X0
Taniya Das38a8c6e2012-05-09 20:34:39 +0530984 l2cc_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985#endif
986
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600987 msm_pm_boot_config_after_pc(smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700988
989 if (collapsed) {
990#ifdef CONFIG_VFP
991 if (from_idle)
992 vfp_reinit();
993#endif
994 cpu_init();
995 local_fiq_enable();
996 }
997
998 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
999 KERN_INFO,
1000 "%s(): msm_pm_collapse returned %d\n", __func__, collapsed);
1001
1002 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1003 "%s(): restore clock rate to %lu\n", __func__,
1004 saved_acpuclk_rate);
1005 if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate,
1006 SETRATE_PC) < 0)
1007 printk(KERN_ERR "%s(): failed to restore clock rate(%lu)\n",
1008 __func__, saved_acpuclk_rate);
1009
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301010 msm_pm_irq_extns->exit_sleep1(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001011 msm_pm_smem_data->wakeup_reason,
1012 msm_pm_smem_data->pending_irqs);
1013
1014 msm_pm_config_hw_after_power_up();
1015 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): post power up");
1016
1017 memset(state_grps, 0, sizeof(state_grps));
1018 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1019 state_grps[0].bits_any_set =
1020 DEM_MASTER_SMSM_RSA | DEM_MASTER_SMSM_PWRC_EARLY_EXIT;
1021 state_grps[1].group_id = SMSM_MODEM_STATE;
1022 state_grps[1].bits_all_set = SMSM_RESET;
1023
1024 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1025
1026 if (ret < 0) {
1027 printk(KERN_EMERG "%s(): power collapse exit "
1028 "timed out waiting for Modem's response\n", __func__);
1029 msm_pm_timeout();
1030 }
1031
1032 if (ret == 1) {
1033 MSM_PM_DPRINTK(
1034 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1035 KERN_INFO,
1036 "%s(): msm_pm_poll_state detected Modem reset\n",
1037 __func__);
1038 goto power_collapse_early_exit;
1039 }
1040
1041 /* Sanity check */
Murali Nalajala07b04022012-04-10 16:00:49 +05301042 if (collapsed && !modem_early_exit) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001043 BUG_ON(!(state_grps[0].value_read & DEM_MASTER_SMSM_RSA));
1044 } else {
1045 BUG_ON(!(state_grps[0].value_read &
1046 DEM_MASTER_SMSM_PWRC_EARLY_EXIT));
1047 goto power_collapse_early_exit;
1048 }
1049
1050 /* Enter WFPI */
1051
1052 smsm_change_state(SMSM_APPS_DEM,
1053 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND,
1054 DEM_SLAVE_SMSM_WFPI);
1055
1056 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI");
1057
1058 memset(state_grps, 0, sizeof(state_grps));
1059 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1060 state_grps[0].bits_all_set = DEM_MASTER_SMSM_RUN;
1061 state_grps[1].group_id = SMSM_MODEM_STATE;
1062 state_grps[1].bits_all_set = SMSM_RESET;
1063
1064 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1065
1066 if (ret < 0) {
1067 printk(KERN_EMERG "%s(): power collapse WFPI "
1068 "timed out waiting for Modem's response\n", __func__);
1069 msm_pm_timeout();
1070 }
1071
1072 if (ret == 1) {
1073 MSM_PM_DPRINTK(
1074 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1075 KERN_INFO,
1076 "%s(): msm_pm_poll_state detected Modem reset\n",
1077 __func__);
1078 ret = -EAGAIN;
1079 goto power_collapse_restore_gpio_bail;
1080 }
1081
1082 /* DEM Master == RUN */
1083
1084 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI RUN");
1085 MSM_PM_DEBUG_PRINT_SLEEP_INFO();
1086
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301087 msm_pm_irq_extns->exit_sleep2(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001088 msm_pm_smem_data->wakeup_reason,
1089 msm_pm_smem_data->pending_irqs);
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301090 msm_pm_irq_extns->exit_sleep3(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001091 msm_pm_smem_data->wakeup_reason,
1092 msm_pm_smem_data->pending_irqs);
1093 msm_gpio_exit_sleep();
1094 msm_sirc_exit_sleep();
1095
1096 smsm_change_state(SMSM_APPS_DEM,
1097 DEM_SLAVE_SMSM_WFPI, DEM_SLAVE_SMSM_RUN);
1098
1099 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN");
1100
1101 smd_sleep_exit();
Murali Nalajala41786ab2012-03-06 10:47:32 +05301102
1103 if (cpu_is_msm8625()) {
1104 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING,
1105 false);
1106 WARN_ON(ret);
1107 }
1108
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001109 return 0;
1110
1111power_collapse_early_exit:
1112 /* Enter PWRC_EARLY_EXIT */
1113
1114 smsm_change_state(SMSM_APPS_DEM,
1115 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND,
1116 DEM_SLAVE_SMSM_PWRC_EARLY_EXIT);
1117
1118 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT");
1119
1120 memset(state_grps, 0, sizeof(state_grps));
1121 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1122 state_grps[0].bits_all_set = DEM_MASTER_SMSM_PWRC_EARLY_EXIT;
1123 state_grps[1].group_id = SMSM_MODEM_STATE;
1124 state_grps[1].bits_all_set = SMSM_RESET;
1125
1126 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1127 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT EE");
1128
1129 if (ret < 0) {
1130 printk(KERN_EMERG "%s(): power collapse EARLY_EXIT "
1131 "timed out waiting for Modem's response\n", __func__);
1132 msm_pm_timeout();
1133 }
1134
1135 if (ret == 1) {
1136 MSM_PM_DPRINTK(
1137 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1138 KERN_INFO,
1139 "%s(): msm_pm_poll_state detected Modem reset\n",
1140 __func__);
1141 }
1142
1143 /* DEM Master == RESET or PWRC_EARLY_EXIT */
1144
1145 ret = -EAGAIN;
1146
1147power_collapse_restore_gpio_bail:
1148 msm_gpio_exit_sleep();
1149 msm_sirc_exit_sleep();
1150
1151 /* Enter RUN */
1152 smsm_change_state(SMSM_APPS_DEM,
1153 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND |
1154 DEM_SLAVE_SMSM_PWRC_EARLY_EXIT, DEM_SLAVE_SMSM_RUN);
1155
1156 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN");
1157
1158 if (collapsed)
1159 smd_sleep_exit();
1160
1161power_collapse_bail:
Murali Nalajala41786ab2012-03-06 10:47:32 +05301162 if (cpu_is_msm8625()) {
1163 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING,
1164 false);
1165 WARN_ON(ret);
1166 }
1167
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001168 return ret;
1169}
1170
1171/*
1172 * Power collapse the Apps processor without involving Modem.
1173 *
1174 * Return value:
1175 * 0: success
1176 */
Stephen Boydb29750d2012-02-21 01:21:32 -08001177static int __ref msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001178{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001179 int collapsed = 0;
1180 int ret;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301181 void *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001182
1183 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1184 KERN_INFO, "%s()\n", __func__);
1185
1186 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE, false);
1187 WARN_ON(ret);
1188
Murali Nalajala41786ab2012-03-06 10:47:32 +05301189 entry = (!smp_processor_id() || from_idle) ?
1190 msm_pm_collapse_exit : msm_secondary_startup;
1191
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001192 msm_pm_boot_config_before_pc(smp_processor_id(),
Murali Nalajala41786ab2012-03-06 10:47:32 +05301193 virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001194
1195#ifdef CONFIG_VFP
1196 vfp_flush_context();
1197#endif
1198
1199#ifdef CONFIG_CACHE_L2X0
Murali Nalajala41786ab2012-03-06 10:47:32 +05301200 if (!cpu_is_msm8625())
Taniya Das38a8c6e2012-05-09 20:34:39 +05301201 l2cc_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001202#endif
1203
1204 collapsed = msm_pm_collapse();
1205
1206#ifdef CONFIG_CACHE_L2X0
Murali Nalajala41786ab2012-03-06 10:47:32 +05301207 if (!cpu_is_msm8625())
Taniya Das38a8c6e2012-05-09 20:34:39 +05301208 l2cc_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001209#endif
1210
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001211 msm_pm_boot_config_after_pc(smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001212
1213 if (collapsed) {
1214#ifdef CONFIG_VFP
1215 vfp_reinit();
1216#endif
1217 cpu_init();
1218 local_fiq_enable();
1219 }
1220
1221 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1222 KERN_INFO,
1223 "%s(): msm_pm_collapse returned %d\n", __func__, collapsed);
1224
1225 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
1226 WARN_ON(ret);
1227
Anji jonnalac6816222012-03-31 10:55:14 +05301228 return !collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229}
1230
1231/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001232 * Bring the Apps processor to SWFI.
1233 *
1234 * Return value:
1235 * -EIO: could not ramp Apps processor clock
1236 * 0: success
1237 */
1238static int msm_pm_swfi(bool ramp_acpu)
1239{
1240 unsigned long saved_acpuclk_rate = 0;
1241
1242 if (ramp_acpu) {
1243 saved_acpuclk_rate = acpuclk_wait_for_irq();
1244 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1245 "%s(): change clock rate (old rate = %lu)\n", __func__,
1246 saved_acpuclk_rate);
1247
1248 if (!saved_acpuclk_rate)
1249 return -EIO;
1250 }
1251
Murali Nalajala41786ab2012-03-06 10:47:32 +05301252 if (!cpu_is_msm8625())
1253 msm_pm_config_hw_before_swfi();
1254
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255 msm_arch_idle();
1256
1257 if (ramp_acpu) {
1258 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1259 "%s(): restore clock rate to %lu\n", __func__,
1260 saved_acpuclk_rate);
1261 if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate,
1262 SETRATE_SWFI) < 0)
1263 printk(KERN_ERR
1264 "%s(): failed to restore clock rate(%lu)\n",
1265 __func__, saved_acpuclk_rate);
1266 }
1267
1268 return 0;
1269}
1270
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301271static int64_t msm_pm_timer_enter_suspend(int64_t *period)
1272{
1273 int time = 0;
1274
1275 time = msm_timer_get_sclk_time(period);
1276 if (!time)
1277 pr_err("%s: Unable to read sclk.\n", __func__);
1278 return time;
1279}
1280
1281static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
1282{
1283
1284 if (time != 0) {
1285 int64_t end_time = msm_timer_get_sclk_time(NULL);
1286 if (end_time != 0) {
1287 time = end_time - time;
1288 if (time < 0)
1289 time += period;
1290 } else
1291 time = 0;
1292 }
1293 return time;
1294}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295
1296/******************************************************************************
1297 * External Idle/Suspend Functions
1298 *****************************************************************************/
1299
1300/*
1301 * Put CPU in low power mode.
1302 */
1303void arch_idle(void)
1304{
1305 bool allow[MSM_PM_SLEEP_MODE_NR];
1306 uint32_t sleep_limit = SLEEP_LIMIT_NONE;
1307
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001308 int64_t timer_expiration;
Murali Nalajala8fda4492012-03-19 18:22:59 +05301309 int latency_qos;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001310 int ret;
1311 int i;
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301312 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001313 int64_t t1;
Murali Nalajalab86f3702012-03-30 17:54:57 +05301314 static DEFINE_PER_CPU(int64_t, t2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001315 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001316
1317 if (!atomic_read(&msm_pm_init_done))
1318 return;
1319
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301320 cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001321 latency_qos = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
Murali Nalajala8fda4492012-03-19 18:22:59 +05301322 /* get the next timer expiration */
1323 timer_expiration = ktime_to_ns(tick_nohz_get_sleep_length());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001324
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001325 t1 = ktime_to_ns(ktime_get());
Murali Nalajalab86f3702012-03-30 17:54:57 +05301326 msm_pm_add_stat(MSM_PM_STAT_NOT_IDLE, t1 - __get_cpu_var(t2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001327 msm_pm_add_stat(MSM_PM_STAT_REQUESTED_IDLE, timer_expiration);
Murali Nalajala7744d162012-01-13 13:06:03 +05301328 exit_stat = MSM_PM_STAT_IDLE_SPIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001329
1330 for (i = 0; i < ARRAY_SIZE(allow); i++)
1331 allow[i] = true;
1332
Murali Nalajala41786ab2012-03-06 10:47:32 +05301333 if (num_online_cpus() > 1 ||
1334 (timer_expiration < msm_pm_idle_sleep_min_time) ||
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001335#ifdef CONFIG_HAS_WAKELOCK
1336 has_wake_lock(WAKE_LOCK_IDLE) ||
1337#endif
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301338 !msm_pm_irq_extns->idle_sleep_allowed()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001339 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false;
1340 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001341 }
1342
1343 for (i = 0; i < ARRAY_SIZE(allow); i++) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301344 struct msm_pm_platform_data *mode =
1345 &msm_pm_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001346 if (!mode->idle_supported || !mode->idle_enabled ||
1347 mode->latency >= latency_qos ||
1348 mode->residency * 1000ULL >= timer_expiration)
1349 allow[i] = false;
1350 }
1351
1352 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1353 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
1354 uint32_t wait_us = CONFIG_MSM_IDLE_WAIT_ON_MODEM;
1355 while (msm_pm_modem_busy() && wait_us) {
1356 if (wait_us > 100) {
1357 udelay(100);
1358 wait_us -= 100;
1359 } else {
1360 udelay(wait_us);
1361 wait_us = 0;
1362 }
1363 }
1364
1365 if (msm_pm_modem_busy()) {
1366 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false;
1367 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]
1368 = false;
1369 }
1370 }
1371
1372 MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO,
1373 "%s(): latency qos %d, next timer %lld, sleep limit %u\n",
1374 __func__, latency_qos, timer_expiration, sleep_limit);
1375
1376 for (i = 0; i < ARRAY_SIZE(allow); i++)
1377 MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO,
1378 "%s(): allow %s: %d\n", __func__,
1379 msm_pm_sleep_mode_labels[i], (int)allow[i]);
1380
1381 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1382 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
Murali Nalajala8fda4492012-03-19 18:22:59 +05301383 /* Sync the timer with SCLK, it is needed only for modem
1384 * assissted pollapse case.
1385 */
1386 int64_t next_timer_exp = msm_timer_enter_idle();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001387 uint32_t sleep_delay;
Murali Nalajala8fda4492012-03-19 18:22:59 +05301388 bool low_power = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001389
1390 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
Murali Nalajala8fda4492012-03-19 18:22:59 +05301391 next_timer_exp, MSM_PM_SLEEP_TICK_LIMIT);
1392
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001393 if (sleep_delay == 0) /* 0 would mean infinite time */
1394 sleep_delay = 1;
1395
1396 if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
1397 sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN;
1398
1399#if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_ACTIVE)
1400 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1;
1401#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_RETENTION)
1402 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1403#endif
1404
1405 ret = msm_pm_power_collapse(true, sleep_delay, sleep_limit);
1406 low_power = (ret != -EBUSY && ret != -ETIMEDOUT);
Murali Nalajala8fda4492012-03-19 18:22:59 +05301407 msm_timer_exit_idle(low_power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001408
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001409 if (ret)
1410 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
1411 else {
1412 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
1413 msm_pm_sleep_limit = sleep_limit;
1414 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
Murali Nalajala41786ab2012-03-06 10:47:32 +05301416 ret = msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001417 exit_stat = ret ?
1418 MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE :
1419 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001420 } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) {
1421 ret = msm_pm_swfi(true);
1422 if (ret)
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301423 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001424 udelay(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001425 exit_stat = ret ? MSM_PM_STAT_IDLE_SPIN : MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001426 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1427 msm_pm_swfi(false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001428 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001429 } else {
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301430 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001431 udelay(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001432 exit_stat = MSM_PM_STAT_IDLE_SPIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001433 }
1434
Murali Nalajalab86f3702012-03-30 17:54:57 +05301435 __get_cpu_var(t2) = ktime_to_ns(ktime_get());
1436 msm_pm_add_stat(exit_stat, __get_cpu_var(t2) - t1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001437}
1438
1439/*
1440 * Suspend the Apps processor.
1441 *
1442 * Return value:
Murali Nalajala41786ab2012-03-06 10:47:32 +05301443 * -EPERM: Suspend happened by a not permitted core
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001444 * -EAGAIN: modem reset occurred or early exit from suspend
1445 * -EBUSY: modem not ready for our suspend
1446 * -EINVAL: invalid sleep mode
1447 * -EIO: could not ramp Apps processor clock
1448 * -ETIMEDOUT: timed out waiting for modem's handshake
1449 * 0: success
1450 */
1451static int msm_pm_enter(suspend_state_t state)
1452{
1453 bool allow[MSM_PM_SLEEP_MODE_NR];
1454 uint32_t sleep_limit = SLEEP_LIMIT_NONE;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301455 int ret = -EPERM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001456 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001457 int64_t period = 0;
1458 int64_t time = 0;
1459
Murali Nalajala41786ab2012-03-06 10:47:32 +05301460 /* Must executed by CORE0 */
1461 if (smp_processor_id()) {
1462 __WARN();
1463 goto suspend_exit;
1464 }
1465
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301466 time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467
1468 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
1469 "%s(): sleep limit %u\n", __func__, sleep_limit);
1470
1471 for (i = 0; i < ARRAY_SIZE(allow); i++)
1472 allow[i] = true;
1473
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001474 for (i = 0; i < ARRAY_SIZE(allow); i++) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301475 struct msm_pm_platform_data *mode;
1476 mode = &msm_pm_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001477 if (!mode->suspend_supported || !mode->suspend_enabled)
1478 allow[i] = false;
1479 }
1480
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001481 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1482 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001483 enum msm_pm_time_stats_id id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001484
1485 clock_debug_print_enabled();
1486
1487#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1488 if (msm_pm_sleep_time_override > 0) {
1489 int64_t ns;
1490 ns = NSEC_PER_SEC * (int64_t)msm_pm_sleep_time_override;
1491 msm_pm_set_max_sleep_time(ns);
1492 msm_pm_sleep_time_override = 0;
1493 }
1494#endif
1495 if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
1496 sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN;
1497
1498#if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_ACTIVE)
1499 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1;
1500#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_RETENTION)
1501 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1502#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN)
1503 if (get_msm_migrate_pages_status() != MEM_OFFLINE)
1504 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1505#endif
1506
1507 for (i = 0; i < 30 && msm_pm_modem_busy(); i++)
1508 udelay(500);
1509
1510 ret = msm_pm_power_collapse(
1511 false, msm_pm_max_sleep_time, sleep_limit);
1512
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001513 if (ret)
1514 id = MSM_PM_STAT_FAILED_SUSPEND;
1515 else {
1516 id = MSM_PM_STAT_SUSPEND;
1517 msm_pm_sleep_limit = sleep_limit;
1518 }
1519
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301520 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001521 msm_pm_add_stat(id, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001522 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
Murali Nalajala41786ab2012-03-06 10:47:32 +05301523 ret = msm_pm_power_collapse_standalone(false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001524 } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) {
1525 ret = msm_pm_swfi(true);
1526 if (ret)
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301527 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001528 udelay(1);
1529 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1530 msm_pm_swfi(false);
1531 }
1532
Murali Nalajala41786ab2012-03-06 10:47:32 +05301533suspend_exit:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001534 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
1535 "%s(): return %d\n", __func__, ret);
1536
1537 return ret;
1538}
1539
1540static struct platform_suspend_ops msm_pm_ops = {
1541 .enter = msm_pm_enter,
1542 .valid = suspend_valid_only_mem,
1543};
1544
Murali Nalajalac89f2f32012-02-07 19:23:52 +05301545/* Hotplug the "non boot" CPU's and put
1546 * the cores into low power mode
1547 */
1548void msm_pm_cpu_enter_lowpower(unsigned int cpu)
1549{
Murali Nalajalaa7efba12012-02-23 18:13:52 +05301550 bool allow[MSM_PM_SLEEP_MODE_NR];
1551 int i;
1552
1553 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1554 struct msm_pm_platform_data *mode;
1555
1556 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
1557 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1558 }
1559
1560 MSM_PM_DPRINTK(MSM_PM_DEBUG_HOTPLUG, KERN_INFO,
1561 "CPU%u: %s: shutting down cpu\n", cpu, __func__);
1562
1563 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1564 msm_pm_power_collapse_standalone(false);
1565 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1566 msm_pm_swfi(false);
1567 } else {
1568 MSM_PM_DPRINTK(MSM_PM_DEBUG_HOTPLUG, KERN_INFO,
1569 "CPU%u: %s: shutting down failed!!!\n", cpu, __func__);
1570 }
Murali Nalajalac89f2f32012-02-07 19:23:52 +05301571}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001572
1573/******************************************************************************
1574 * Restart Definitions
1575 *****************************************************************************/
1576
1577static uint32_t restart_reason = 0x776655AA;
1578
1579static void msm_pm_power_off(void)
1580{
1581 msm_rpcrouter_close();
1582 msm_proc_comm(PCOM_POWER_DOWN, 0, 0);
1583 for (;;)
1584 ;
1585}
1586
1587static void msm_pm_restart(char str, const char *cmd)
1588{
1589 msm_rpcrouter_close();
1590 msm_proc_comm(PCOM_RESET_CHIP, &restart_reason, 0);
1591
1592 for (;;)
1593 ;
1594}
1595
1596static int msm_reboot_call
1597 (struct notifier_block *this, unsigned long code, void *_cmd)
1598{
1599 if ((code == SYS_RESTART) && _cmd) {
1600 char *cmd = _cmd;
1601 if (!strcmp(cmd, "bootloader")) {
1602 restart_reason = 0x77665500;
1603 } else if (!strcmp(cmd, "recovery")) {
1604 restart_reason = 0x77665502;
1605 } else if (!strcmp(cmd, "eraseflash")) {
1606 restart_reason = 0x776655EF;
1607 } else if (!strncmp(cmd, "oem-", 4)) {
1608 unsigned code = simple_strtoul(cmd + 4, 0, 16) & 0xff;
1609 restart_reason = 0x6f656d00 | code;
1610 } else {
1611 restart_reason = 0x77665501;
1612 }
1613 }
1614 return NOTIFY_DONE;
1615}
1616
1617static struct notifier_block msm_reboot_notifier = {
1618 .notifier_call = msm_reboot_call,
1619};
1620
1621
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001622/*
1623 * Initialize the power management subsystem.
1624 *
1625 * Return value:
1626 * -ENODEV: initialization failed
1627 * 0: success
1628 */
1629static int __init msm_pm_init(void)
1630{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631 int ret;
Murali Nalajala93f29992012-03-21 15:59:27 +05301632 int val;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301633 enum msm_pm_time_stats_id enable_stats[] = {
1634 MSM_PM_STAT_REQUESTED_IDLE,
1635 MSM_PM_STAT_IDLE_SPIN,
1636 MSM_PM_STAT_IDLE_WFI,
1637 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1638 MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE,
1639 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1640 MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE,
1641 MSM_PM_STAT_SUSPEND,
1642 MSM_PM_STAT_FAILED_SUSPEND,
1643 MSM_PM_STAT_NOT_IDLE,
1644 };
1645
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001646#ifdef CONFIG_CPU_V7
1647 pgd_t *pc_pgd;
1648 pmd_t *pmd;
1649 unsigned long pmdval;
1650
1651 /* Page table for cores to come back up safely. */
1652 pc_pgd = pgd_alloc(&init_mm);
1653 if (!pc_pgd)
1654 return -ENOMEM;
1655 pmd = pmd_offset(pc_pgd +
1656 pgd_index(virt_to_phys(msm_pm_collapse_exit)),
1657 virt_to_phys(msm_pm_collapse_exit));
1658 pmdval = (virt_to_phys(msm_pm_collapse_exit) & PGDIR_MASK) |
1659 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1660 pmd[0] = __pmd(pmdval);
1661 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1662
Steve Mucklefcece052012-02-18 20:09:58 -08001663 msm_saved_state_phys =
1664 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1665 num_possible_cpus(), 4);
1666 if (!msm_saved_state_phys)
1667 return -ENOMEM;
1668 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1669 CPU_SAVED_STATE_SIZE *
1670 num_possible_cpus());
1671 if (!msm_saved_state)
1672 return -ENOMEM;
1673
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001674 /* It is remotely possible that the code in msm_pm_collapse_exit()
1675 * which turns on the MMU with this mapping is in the
1676 * next even-numbered megabyte beyond the
1677 * start of msm_pm_collapse_exit().
1678 * Map this megabyte in as well.
1679 */
1680 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1681 flush_pmd_entry(pmd);
1682 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001683 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1684 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001685#endif
1686
1687 pm_power_off = msm_pm_power_off;
1688 arm_pm_restart = msm_pm_restart;
1689 register_reboot_notifier(&msm_reboot_notifier);
1690
1691 msm_pm_smem_data = smem_alloc(SMEM_APPS_DEM_SLAVE_DATA,
1692 sizeof(*msm_pm_smem_data));
1693 if (msm_pm_smem_data == NULL) {
1694 printk(KERN_ERR "%s: failed to get smsm_data\n", __func__);
1695 return -ENODEV;
1696 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001697
1698 ret = msm_timer_init_time_sync(msm_pm_timeout);
1699 if (ret)
1700 return ret;
1701
1702 ret = smsm_change_intr_mask(SMSM_POWER_MASTER_DEM, 0xFFFFFFFF, 0);
1703 if (ret) {
1704 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
1705 __func__, ret);
1706 return ret;
1707 }
1708
Murali Nalajala93f29992012-03-21 15:59:27 +05301709 if (cpu_is_msm8625()) {
1710 target_type = TARGET_IS_8625;
1711 clean_caches((unsigned long)&target_type, sizeof(target_type),
1712 virt_to_phys(&target_type));
1713
Anji jonnalae644f8e2012-05-09 19:52:18 +05301714 /*
1715 * Configure the MPA5_GDFS_CNT_VAL register for
1716 * DBGPWRUPEREQ_OVERRIDE[17:16] = Override the
1717 * DBGNOPOWERDN for each cpu.
1718 * MPA5_GDFS_CNT_VAL[9:0] = Delay counter for
1719 * GDFS control.
Murali Nalajala93f29992012-03-21 15:59:27 +05301720 */
Anji jonnalae644f8e2012-05-09 19:52:18 +05301721 val = 0x00030002;
Murali Nalajala93f29992012-03-21 15:59:27 +05301722 __raw_writel(val, (MSM_CFG_CTL_BASE + 0x38));
1723 }
1724
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001725#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE
1726 /* The wakeup_reason field is overloaded during initialization time
1727 to signal Modem that Apps will control the low power modes of
1728 the memory.
1729 */
1730 msm_pm_smem_data->wakeup_reason = 1;
1731 smsm_change_state(SMSM_APPS_DEM, 0, DEM_SLAVE_SMSM_RUN);
1732#endif
1733
1734 BUG_ON(msm_pm_modes == NULL);
1735
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001736 suspend_set_ops(&msm_pm_ops);
1737
1738 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301739 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Murali Nalajala558c0ce2012-03-29 19:42:08 +05301740
1741 atomic_set(&msm_pm_init_done, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001742 return 0;
1743}
1744
1745late_initcall_sync(msm_pm_init);