blob: ec9f030334fa31264d6bbc8358fcd5d8ae851214 [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 Nalajalad1def3a2012-11-19 17:11:22 +05306 * Copyright (c) 2008-2012 The Linux Foundation. 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>
Steve Mucklef132c6c2012-06-06 18:30:57 -070024#include <linux/pm_qos.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025#include <linux/suspend.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070026#include <linux/io.h>
Murali Nalajala8fda4492012-03-19 18:22:59 +053027#include <linux/tick.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070028#include <linux/memory.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#include <mach/msm_iomap.h>
30#include <mach/system.h>
31#ifdef CONFIG_CPU_V7
32#include <asm/pgtable.h>
33#include <asm/pgalloc.h>
34#endif
35#ifdef CONFIG_CACHE_L2X0
36#include <asm/hardware/cache-l2x0.h>
37#endif
38#ifdef CONFIG_VFP
39#include <asm/vfp.h>
40#endif
41
42#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN
43#include <mach/msm_migrate_pages.h>
44#endif
Murali Nalajala41786ab2012-03-06 10:47:32 +053045#include <mach/socinfo.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070046#include <mach/proc_comm.h>
Anji jonnala1f2377c2012-03-27 14:35:55 +053047#include <asm/smp_scu.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048
49#include "smd_private.h"
50#include "smd_rpcrouter.h"
51#include "acpuclock.h"
52#include "clock.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070053#include "idle.h"
54#include "irq.h"
55#include "gpio.h"
56#include "timer.h"
Matt Wagantall7cca4642012-02-01 16:43:24 -080057#include "pm.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070058#include "spm.h"
59#include "sirc.h"
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -060060#include "pm-boot.h"
Murali Nalajala19d33a22012-05-18 14:11:19 +053061#include "devices-msm7x2xa.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062
63/******************************************************************************
64 * Debug Definitions
65 *****************************************************************************/
66
67enum {
Murali Nalajalaa7efba12012-02-23 18:13:52 +053068 MSM_PM_DEBUG_SUSPEND = BIT(0),
69 MSM_PM_DEBUG_POWER_COLLAPSE = BIT(1),
70 MSM_PM_DEBUG_STATE = BIT(2),
71 MSM_PM_DEBUG_CLOCK = BIT(3),
72 MSM_PM_DEBUG_RESET_VECTOR = BIT(4),
73 MSM_PM_DEBUG_SMSM_STATE = BIT(5),
74 MSM_PM_DEBUG_IDLE = BIT(6),
75 MSM_PM_DEBUG_HOTPLUG = BIT(7),
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070076};
77
Murali Nalajalad1def3a2012-11-19 17:11:22 +053078DEFINE_PER_CPU(int, power_collapsed);
79
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070080static int msm_pm_debug_mask;
81module_param_named(
82 debug_mask, msm_pm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP
83);
84
85#define MSM_PM_DPRINTK(mask, level, message, ...) \
86 do { \
87 if ((mask) & msm_pm_debug_mask) \
88 printk(level message, ## __VA_ARGS__); \
89 } while (0)
90
91#define MSM_PM_DEBUG_PRINT_STATE(tag) \
92 do { \
93 MSM_PM_DPRINTK(MSM_PM_DEBUG_STATE, \
94 KERN_INFO, "%s: " \
95 "APPS_CLK_SLEEP_EN %x, APPS_PWRDOWN %x, " \
96 "SMSM_POWER_MASTER_DEM %x, SMSM_MODEM_STATE %x, " \
97 "SMSM_APPS_DEM %x\n", \
98 tag, \
99 __raw_readl(APPS_CLK_SLEEP_EN), \
100 __raw_readl(APPS_PWRDOWN), \
101 smsm_get_state(SMSM_POWER_MASTER_DEM), \
102 smsm_get_state(SMSM_MODEM_STATE), \
103 smsm_get_state(SMSM_APPS_DEM)); \
104 } while (0)
105
106#define MSM_PM_DEBUG_PRINT_SLEEP_INFO() \
107 do { \
108 if (msm_pm_debug_mask & MSM_PM_DEBUG_SMSM_STATE) \
109 smsm_print_sleep_info(msm_pm_smem_data->sleep_time, \
110 msm_pm_smem_data->resources_used, \
111 msm_pm_smem_data->irq_mask, \
112 msm_pm_smem_data->wakeup_reason, \
113 msm_pm_smem_data->pending_irqs); \
114 } while (0)
115
116
117/******************************************************************************
118 * Sleep Modes and Parameters
119 *****************************************************************************/
120
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700121static int msm_pm_idle_sleep_min_time = CONFIG_MSM7X00A_IDLE_SLEEP_MIN_TIME;
122module_param_named(
123 idle_sleep_min_time, msm_pm_idle_sleep_min_time,
124 int, S_IRUGO | S_IWUSR | S_IWGRP
125);
126
127enum {
128 MSM_PM_MODE_ATTR_SUSPEND,
129 MSM_PM_MODE_ATTR_IDLE,
130 MSM_PM_MODE_ATTR_LATENCY,
131 MSM_PM_MODE_ATTR_RESIDENCY,
132 MSM_PM_MODE_ATTR_NR,
133};
134
135static char *msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_NR] = {
136 [MSM_PM_MODE_ATTR_SUSPEND] = "suspend_enabled",
137 [MSM_PM_MODE_ATTR_IDLE] = "idle_enabled",
138 [MSM_PM_MODE_ATTR_LATENCY] = "latency",
139 [MSM_PM_MODE_ATTR_RESIDENCY] = "residency",
140};
141
142static char *msm_pm_sleep_mode_labels[MSM_PM_SLEEP_MODE_NR] = {
143 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_SUSPEND] = " ",
144 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = "power_collapse",
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700145 [MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT] =
146 "ramp_down_and_wfi",
147 [MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT] = "wfi",
148 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] =
149 "power_collapse_no_xo_shutdown",
150 [MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE] =
151 "standalone_power_collapse",
152};
153
154static struct msm_pm_platform_data *msm_pm_modes;
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530155static struct msm_pm_irq_calls *msm_pm_irq_extns;
Murali Nalajalaff723ec2012-07-13 16:54:40 +0530156static struct msm_pm_cpr_ops *msm_cpr_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700157
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530158struct msm_pm_kobj_attribute {
159 unsigned int cpu;
160 struct kobj_attribute ka;
161};
162
163#define GET_CPU_OF_ATTR(attr) \
164 (container_of(attr, struct msm_pm_kobj_attribute, ka)->cpu)
165
166struct msm_pm_sysfs_sleep_mode {
167 struct kobject *kobj;
168 struct attribute_group attr_group;
169 struct attribute *attrs[MSM_PM_MODE_ATTR_NR + 1];
170 struct msm_pm_kobj_attribute kas[MSM_PM_MODE_ATTR_NR];
171};
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700172
173/*
174 * Write out the attribute.
175 */
176static ssize_t msm_pm_mode_attr_show(
177 struct kobject *kobj, struct kobj_attribute *attr, char *buf)
178{
179 int ret = -EINVAL;
180 int i;
181
182 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
183 struct kernel_param kp;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530184 unsigned int cpu;
185 struct msm_pm_platform_data *mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700186
187 if (msm_pm_sleep_mode_labels[i] == NULL)
188 continue;
189
190 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
191 continue;
192
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530193 cpu = GET_CPU_OF_ATTR(attr);
194 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
195
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700196 if (!strcmp(attr->attr.name,
197 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530198 u32 arg = mode->suspend_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199 kp.arg = &arg;
200 ret = param_get_ulong(buf, &kp);
201 } else if (!strcmp(attr->attr.name,
202 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530203 u32 arg = mode->idle_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700204 kp.arg = &arg;
205 ret = param_get_ulong(buf, &kp);
206 } else if (!strcmp(attr->attr.name,
207 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530208 u32 arg = mode->latency;
209 kp.arg = &arg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700210 ret = param_get_ulong(buf, &kp);
211 } else if (!strcmp(attr->attr.name,
212 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530213 u32 arg = mode->residency;
214 kp.arg = &arg;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215 ret = param_get_ulong(buf, &kp);
216 }
217
218 break;
219 }
220
221 if (ret > 0) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530222 strlcat(buf, "\n", PAGE_SIZE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 ret++;
224 }
225
226 return ret;
227}
228
229/*
230 * Read in the new attribute value.
231 */
232static ssize_t msm_pm_mode_attr_store(struct kobject *kobj,
233 struct kobj_attribute *attr, const char *buf, size_t count)
234{
235 int ret = -EINVAL;
236 int i;
237
238 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
239 struct kernel_param kp;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530240 unsigned int cpu;
241 struct msm_pm_platform_data *mode;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700242
243 if (msm_pm_sleep_mode_labels[i] == NULL)
244 continue;
245
246 if (strcmp(kobj->name, msm_pm_sleep_mode_labels[i]))
247 continue;
248
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530249 cpu = GET_CPU_OF_ATTR(attr);
250 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
251
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700252 if (!strcmp(attr->attr.name,
253 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_SUSPEND])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530254 kp.arg = &mode->suspend_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700255 ret = param_set_byte(buf, &kp);
256 } else if (!strcmp(attr->attr.name,
257 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_IDLE])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530258 kp.arg = &mode->idle_enabled;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700259 ret = param_set_byte(buf, &kp);
260 } else if (!strcmp(attr->attr.name,
261 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_LATENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530262 kp.arg = &mode->latency;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700263 ret = param_set_ulong(buf, &kp);
264 } else if (!strcmp(attr->attr.name,
265 msm_pm_mode_attr_labels[MSM_PM_MODE_ATTR_RESIDENCY])) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530266 kp.arg = &mode->residency;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 ret = param_set_ulong(buf, &kp);
268 }
269
270 break;
271 }
272
273 return ret ? ret : count;
274}
275
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530276 /* Add sysfs entries for one cpu. */
277static int __init msm_pm_mode_sysfs_add_cpu(
278 unsigned int cpu, struct kobject *modes_kobj)
279{
280 char cpu_name[8];
281 struct kobject *cpu_kobj;
282 struct msm_pm_sysfs_sleep_mode *mode = NULL;
283 int i, j, k;
284 int ret;
285
286 snprintf(cpu_name, sizeof(cpu_name), "cpu%u", cpu);
287 cpu_kobj = kobject_create_and_add(cpu_name, modes_kobj);
288 if (!cpu_kobj) {
289 pr_err("%s: cannot create %s kobject\n", __func__, cpu_name);
290 ret = -ENOMEM;
291 goto mode_sysfs_add_cpu_exit;
292 }
293
294 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
295 int idx = MSM_PM_MODE(cpu, i);
296
297 if ((!msm_pm_modes[idx].suspend_supported) &&
298 (!msm_pm_modes[idx].idle_supported))
299 continue;
300
301 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
302 if (!mode) {
303 pr_err("%s: cannot allocate memory for attributes\n",
304 __func__);
305 ret = -ENOMEM;
306 goto mode_sysfs_add_cpu_exit;
307 }
308
309 mode->kobj = kobject_create_and_add(
310 msm_pm_sleep_mode_labels[i], cpu_kobj);
311 if (!mode->kobj) {
312 pr_err("%s: cannot create kobject\n", __func__);
313 ret = -ENOMEM;
314 goto mode_sysfs_add_cpu_exit;
315 }
316
317 for (k = 0, j = 0; k < MSM_PM_MODE_ATTR_NR; k++) {
318 if ((k == MSM_PM_MODE_ATTR_IDLE) &&
319 !msm_pm_modes[idx].idle_supported)
320 continue;
321 if ((k == MSM_PM_MODE_ATTR_SUSPEND) &&
322 !msm_pm_modes[idx].suspend_supported)
323 continue;
324 mode->kas[j].cpu = cpu;
325 mode->kas[j].ka.attr.mode = 0644;
326 mode->kas[j].ka.show = msm_pm_mode_attr_show;
327 mode->kas[j].ka.store = msm_pm_mode_attr_store;
328 mode->kas[j].ka.attr.name = msm_pm_mode_attr_labels[k];
329 mode->attrs[j] = &mode->kas[j].ka.attr;
330 j++;
331 }
332 mode->attrs[j] = NULL;
333
334 mode->attr_group.attrs = mode->attrs;
335 ret = sysfs_create_group(mode->kobj, &mode->attr_group);
336 if (ret) {
337 printk(KERN_ERR
338 "%s: cannot create kobject attribute group\n",
339 __func__);
340 goto mode_sysfs_add_cpu_exit;
341 }
342 }
343
344 ret = 0;
345
346mode_sysfs_add_cpu_exit:
347 if (ret) {
348 if (mode && mode->kobj)
349 kobject_del(mode->kobj);
350 kfree(mode);
351 }
352
353 return ret;
354}
355
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700356/*
357 * Add sysfs entries for the sleep modes.
358 */
359static int __init msm_pm_mode_sysfs_add(void)
360{
361 struct kobject *module_kobj = NULL;
362 struct kobject *modes_kobj = NULL;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530363 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700364 int ret;
365
366 module_kobj = kset_find_obj(module_kset, KBUILD_MODNAME);
367 if (!module_kobj) {
368 printk(KERN_ERR "%s: cannot find kobject for module %s\n",
369 __func__, KBUILD_MODNAME);
370 ret = -ENOENT;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530371 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700372 }
373
374 modes_kobj = kobject_create_and_add("modes", module_kobj);
375 if (!modes_kobj) {
376 printk(KERN_ERR "%s: cannot create modes kobject\n", __func__);
377 ret = -ENOMEM;
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530378 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700379 }
380
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530381 for_each_possible_cpu(cpu) {
382 ret = msm_pm_mode_sysfs_add_cpu(cpu, modes_kobj);
383 if (ret)
384 goto mode_sysfs_add_exit;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700385 }
386
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530387 ret = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700388
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530389mode_sysfs_add_exit:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700390 return ret;
391}
392
Stephen Boyd3f4bac22012-05-30 10:03:13 -0700393s32 msm_cpuidle_get_deep_idle_latency(void)
394{
395 int i = MSM_PM_MODE(0, MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN);
396 return msm_pm_modes[i].latency - 1;
397}
398
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700399void __init msm_pm_set_platform_data(
400 struct msm_pm_platform_data *data, int count)
401{
Murali Nalajala0df9fee2012-01-12 15:26:09 +0530402 BUG_ON(MSM_PM_SLEEP_MODE_NR * num_possible_cpus() > count);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403 msm_pm_modes = data;
404}
405
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530406void __init msm_pm_set_irq_extns(struct msm_pm_irq_calls *irq_calls)
407{
408 /* sanity check */
409 BUG_ON(irq_calls == NULL || irq_calls->irq_pending == NULL ||
410 irq_calls->idle_sleep_allowed == NULL ||
411 irq_calls->enter_sleep1 == NULL ||
412 irq_calls->enter_sleep2 == NULL ||
413 irq_calls->exit_sleep1 == NULL ||
414 irq_calls->exit_sleep2 == NULL ||
415 irq_calls->exit_sleep3 == NULL);
416
417 msm_pm_irq_extns = irq_calls;
418}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700419
Murali Nalajalaff723ec2012-07-13 16:54:40 +0530420void __init msm_pm_set_cpr_ops(struct msm_pm_cpr_ops *ops)
421{
422 msm_cpr_ops = ops;
423}
424
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700425/******************************************************************************
426 * Sleep Limitations
427 *****************************************************************************/
428enum {
429 SLEEP_LIMIT_NONE = 0,
430 SLEEP_LIMIT_NO_TCXO_SHUTDOWN = 2,
431 SLEEP_LIMIT_MASK = 0x03,
432};
433
Praveen Chidambaram3895bde2012-05-14 19:42:40 +0530434static uint32_t msm_pm_sleep_limit = SLEEP_LIMIT_NONE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700435#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE
436enum {
437 SLEEP_RESOURCE_MEMORY_BIT0 = 0x0200,
438 SLEEP_RESOURCE_MEMORY_BIT1 = 0x0010,
439};
440#endif
441
442
443/******************************************************************************
444 * Configure Hardware for Power Down/Up
445 *****************************************************************************/
446
447#if defined(CONFIG_ARCH_MSM7X30)
Taniya Das298de8c2012-02-16 11:45:31 +0530448#define APPS_CLK_SLEEP_EN (MSM_APCS_GCC_BASE + 0x020)
449#define APPS_PWRDOWN (MSM_ACC0_BASE + 0x01c)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700450#define APPS_SECOP (MSM_TCSR_BASE + 0x038)
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530451#define APPS_STANDBY_CTL NULL
452#else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700453#define APPS_CLK_SLEEP_EN (MSM_CSR_BASE + 0x11c)
454#define APPS_PWRDOWN (MSM_CSR_BASE + 0x440)
455#define APPS_STANDBY_CTL (MSM_CSR_BASE + 0x108)
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530456#define APPS_SECOP NULL
457#endif
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700458
459/*
460 * Configure hardware registers in preparation for Apps power down.
461 */
462static void msm_pm_config_hw_before_power_down(void)
463{
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530464 if (cpu_is_msm7x30() || cpu_is_msm8x55()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530465 __raw_writel(4, APPS_SECOP);
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530466 } else if (cpu_is_msm7x27()) {
467 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530468 } else if (cpu_is_msm7x27a() || cpu_is_msm7x27aa() ||
Pankaj Kumarfee56a82012-04-17 14:26:49 +0530469 cpu_is_msm7x25a() || cpu_is_msm7x25aa() ||
470 cpu_is_msm7x25ab()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530471 __raw_writel(0x7, APPS_CLK_SLEEP_EN);
Murali Nalajalab87e88c2012-05-18 15:12:13 +0530472 } else if (cpu_is_qsd8x50()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530473 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
474 mb();
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530475 __raw_writel(0, APPS_STANDBY_CTL);
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530476 }
Murali Nalajalab87e88c2012-05-18 15:12:13 +0530477 mb();
478 __raw_writel(1, APPS_PWRDOWN);
479 mb();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480}
481
482/*
Anji jonnala1f2377c2012-03-27 14:35:55 +0530483 * Program the top csr from core0 context to put the
484 * core1 into GDFS, as core1 is not running yet.
485 */
Murali Nalajala1989f822012-10-19 19:50:03 +0530486static void msm_pm_configure_top_csr(void)
Anji jonnala1f2377c2012-03-27 14:35:55 +0530487{
Murali Nalajala1989f822012-10-19 19:50:03 +0530488 /*
489 * Enable TCSR for core
490 * Set reset bit for SPM
491 * Set CLK_OFF bit
492 * Set clamps bit
493 * Set power_up bit
494 * Disable TSCR for core
495 */
496 uint32_t bit_pos[][6] = {
497 /* c2 */
498 {17, 15, 13, 16, 14, 17},
499 /* c1 & c3*/
500 {22, 20, 18, 21, 19, 22},
501 };
502 uint32_t mpa5_cfg_ctl[2] = {0x30, 0x48};
Anji jonnala1f2377c2012-03-27 14:35:55 +0530503 void __iomem *base_ptr;
504 unsigned int value = 0;
Murali Nalajala1989f822012-10-19 19:50:03 +0530505 unsigned int cpu;
506 int i;
Anji jonnala1f2377c2012-03-27 14:35:55 +0530507
Murali Nalajala1989f822012-10-19 19:50:03 +0530508 /* Initialize all the SPM registers */
Anji jonnala1f2377c2012-03-27 14:35:55 +0530509 msm_spm_reinit();
510
Murali Nalajala1989f822012-10-19 19:50:03 +0530511 for_each_possible_cpu(cpu) {
512 /* skip for C0 */
513 if (!cpu)
514 continue;
Anji jonnala1f2377c2012-03-27 14:35:55 +0530515
Murali Nalajala1989f822012-10-19 19:50:03 +0530516 base_ptr = core_reset_base(cpu);
517 if (!base_ptr)
518 return;
Anji jonnala1f2377c2012-03-27 14:35:55 +0530519
Murali Nalajala1989f822012-10-19 19:50:03 +0530520 /* bring the core out of reset */
521 __raw_writel(0x3, base_ptr);
522 mb();
Anji jonnala1f2377c2012-03-27 14:35:55 +0530523
Murali Nalajala1989f822012-10-19 19:50:03 +0530524 /*
525 * i == 0, Enable TCSR for core
526 * i == 1, Set reset bit for SPM
527 * i == 2, Set CLK_OFF bit
528 * i == 3, Set clamps bit
529 * i == 4, Set power_up bit
530 */
531 for (i = 0; i < 5; i++) {
532 value = __raw_readl(MSM_CFG_CTL_BASE +
533 mpa5_cfg_ctl[cpu/2]);
534 value |= BIT(bit_pos[cpu%2][i]);
535 __raw_writel(value, MSM_CFG_CTL_BASE +
536 mpa5_cfg_ctl[cpu/2]);
537 mb();
538 }
Anji jonnala1f2377c2012-03-27 14:35:55 +0530539
Murali Nalajala1989f822012-10-19 19:50:03 +0530540 /* i == 5, Disable TCSR for core */
541 value = __raw_readl(MSM_CFG_CTL_BASE +
542 mpa5_cfg_ctl[cpu/2]);
543 value &= ~BIT(bit_pos[cpu%2][i]);
544 __raw_writel(value, MSM_CFG_CTL_BASE +
545 mpa5_cfg_ctl[cpu/2]);
546 mb();
Anji jonnala1f2377c2012-03-27 14:35:55 +0530547
Murali Nalajala1989f822012-10-19 19:50:03 +0530548 __raw_writel(0x0, base_ptr);
549 mb();
550 }
Anji jonnala1f2377c2012-03-27 14:35:55 +0530551}
552
553/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700554 * Clear hardware registers after Apps powers up.
555 */
556static void msm_pm_config_hw_after_power_up(void)
557{
Anji jonnala1f2377c2012-03-27 14:35:55 +0530558
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530559 if (cpu_is_msm7x30() || cpu_is_msm8x55()) {
560 __raw_writel(0, APPS_SECOP);
561 mb();
562 __raw_writel(0, APPS_PWRDOWN);
563 mb();
564 msm_spm_reinit();
Utsab Bose4ed4ba12012-11-08 18:52:38 +0530565 } else if (cpu_is_msm8625() || cpu_is_msm8625q()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530566 __raw_writel(0, APPS_PWRDOWN);
567 mb();
Anji jonnala1f2377c2012-03-27 14:35:55 +0530568
Murali Nalajalad1def3a2012-11-19 17:11:22 +0530569 if (per_cpu(power_collapsed, 1)) {
Anji jonnala1f2377c2012-03-27 14:35:55 +0530570 /*
571 * enable the SCU while coming out of power
572 * collapse.
573 */
574 scu_enable(MSM_SCU_BASE);
575 /*
576 * Program the top csr to put the core1 into GDFS.
577 */
Murali Nalajala1989f822012-10-19 19:50:03 +0530578 msm_pm_configure_top_csr();
Anji jonnala1f2377c2012-03-27 14:35:55 +0530579 }
Murali Nalajalab87e88c2012-05-18 15:12:13 +0530580 } else {
581 __raw_writel(0, APPS_PWRDOWN);
582 mb();
583 __raw_writel(0, APPS_CLK_SLEEP_EN);
584 mb();
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530585 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700586}
587
588/*
589 * Configure hardware registers in preparation for SWFI.
590 */
591static void msm_pm_config_hw_before_swfi(void)
592{
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530593 if (cpu_is_qsd8x50()) {
594 __raw_writel(0x1f, APPS_CLK_SLEEP_EN);
595 mb();
596 } else if (cpu_is_msm7x27()) {
597 __raw_writel(0x0f, APPS_CLK_SLEEP_EN);
598 mb();
599 } else if (cpu_is_msm7x27a() || cpu_is_msm7x27aa() ||
Pankaj Kumarfee56a82012-04-17 14:26:49 +0530600 cpu_is_msm7x25a() || cpu_is_msm7x25aa() ||
601 cpu_is_msm7x25ab()) {
Murali Nalajala1bc1d7c2012-02-09 11:18:42 +0530602 __raw_writel(0x7, APPS_CLK_SLEEP_EN);
603 mb();
604 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700605}
606
607/*
608 * Respond to timing out waiting for Modem
609 *
610 * NOTE: The function never returns.
611 */
612static void msm_pm_timeout(void)
613{
614#if defined(CONFIG_MSM_PM_TIMEOUT_RESET_CHIP)
615 printk(KERN_EMERG "%s(): resetting chip\n", __func__);
616 msm_proc_comm(PCOM_RESET_CHIP_IMM, NULL, NULL);
617#elif defined(CONFIG_MSM_PM_TIMEOUT_RESET_MODEM)
618 printk(KERN_EMERG "%s(): resetting modem\n", __func__);
619 msm_proc_comm_reset_modem_now();
620#elif defined(CONFIG_MSM_PM_TIMEOUT_HALT)
621 printk(KERN_EMERG "%s(): halting\n", __func__);
622#endif
623 for (;;)
624 ;
625}
626
627
628/******************************************************************************
629 * State Polling Definitions
630 *****************************************************************************/
631
632struct msm_pm_polled_group {
633 uint32_t group_id;
634
635 uint32_t bits_all_set;
636 uint32_t bits_all_clear;
637 uint32_t bits_any_set;
638 uint32_t bits_any_clear;
639
640 uint32_t value_read;
641};
642
643/*
644 * Return true if all bits indicated by flag are set in source.
645 */
646static inline bool msm_pm_all_set(uint32_t source, uint32_t flag)
647{
648 return (source & flag) == flag;
649}
650
651/*
652 * Return true if any bit indicated by flag are set in source.
653 */
654static inline bool msm_pm_any_set(uint32_t source, uint32_t flag)
655{
656 return !flag || (source & flag);
657}
658
659/*
660 * Return true if all bits indicated by flag are cleared in source.
661 */
662static inline bool msm_pm_all_clear(uint32_t source, uint32_t flag)
663{
664 return (~source & flag) == flag;
665}
666
667/*
668 * Return true if any bit indicated by flag are cleared in source.
669 */
670static inline bool msm_pm_any_clear(uint32_t source, uint32_t flag)
671{
672 return !flag || (~source & flag);
673}
674
675/*
676 * Poll the shared memory states as indicated by the poll groups.
677 *
678 * nr_grps: number of groups in the array
679 * grps: array of groups
680 *
681 * The function returns when conditions specified by any of the poll
682 * groups become true. The conditions specified by a poll group are
683 * deemed true when 1) at least one bit from bits_any_set is set OR one
684 * bit from bits_any_clear is cleared; and 2) all bits in bits_all_set
685 * are set; and 3) all bits in bits_all_clear are cleared.
686 *
687 * Return value:
688 * >=0: index of the poll group whose conditions have become true
689 * -ETIMEDOUT: timed out
690 */
691static int msm_pm_poll_state(int nr_grps, struct msm_pm_polled_group *grps)
692{
693 int i, k;
694
695 for (i = 0; i < 50000; i++) {
696 for (k = 0; k < nr_grps; k++) {
697 bool all_set, all_clear;
698 bool any_set, any_clear;
699
700 grps[k].value_read = smsm_get_state(grps[k].group_id);
701
702 all_set = msm_pm_all_set(grps[k].value_read,
703 grps[k].bits_all_set);
704 all_clear = msm_pm_all_clear(grps[k].value_read,
705 grps[k].bits_all_clear);
706 any_set = msm_pm_any_set(grps[k].value_read,
707 grps[k].bits_any_set);
708 any_clear = msm_pm_any_clear(grps[k].value_read,
709 grps[k].bits_any_clear);
710
711 if (all_set && all_clear && (any_set || any_clear))
712 return k;
713 }
714 udelay(50);
715 }
716
717 printk(KERN_ERR "%s failed:\n", __func__);
718 for (k = 0; k < nr_grps; k++)
719 printk(KERN_ERR "(%x, %x, %x, %x) %x\n",
720 grps[k].bits_all_set, grps[k].bits_all_clear,
721 grps[k].bits_any_set, grps[k].bits_any_clear,
722 grps[k].value_read);
723
724 return -ETIMEDOUT;
725}
726
727
728/******************************************************************************
729 * Suspend Max Sleep Time
730 *****************************************************************************/
731
732#define SCLK_HZ (32768)
733#define MSM_PM_SLEEP_TICK_LIMIT (0x6DDD000)
734
735#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
736static int msm_pm_sleep_time_override;
737module_param_named(sleep_time_override,
738 msm_pm_sleep_time_override, int, S_IRUGO | S_IWUSR | S_IWGRP);
739#endif
740
741static uint32_t msm_pm_max_sleep_time;
742
743/*
744 * Convert time from nanoseconds to slow clock ticks, then cap it to the
745 * specified limit
746 */
747static int64_t msm_pm_convert_and_cap_time(int64_t time_ns, int64_t limit)
748{
749 do_div(time_ns, NSEC_PER_SEC / SCLK_HZ);
750 return (time_ns > limit) ? limit : time_ns;
751}
752
753/*
754 * Set the sleep time for suspend. 0 means infinite sleep time.
755 */
756void msm_pm_set_max_sleep_time(int64_t max_sleep_time_ns)
757{
758 unsigned long flags;
759
760 local_irq_save(flags);
761 if (max_sleep_time_ns == 0) {
762 msm_pm_max_sleep_time = 0;
763 } else {
764 msm_pm_max_sleep_time = (uint32_t)msm_pm_convert_and_cap_time(
765 max_sleep_time_ns, MSM_PM_SLEEP_TICK_LIMIT);
766
767 if (msm_pm_max_sleep_time == 0)
768 msm_pm_max_sleep_time = 1;
769 }
770
771 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
772 "%s(): Requested %lld ns Giving %u sclk ticks\n", __func__,
773 max_sleep_time_ns, msm_pm_max_sleep_time);
774 local_irq_restore(flags);
775}
776EXPORT_SYMBOL(msm_pm_set_max_sleep_time);
777
778
779/******************************************************************************
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700780 * Shared Memory Bits
781 *****************************************************************************/
782
783#define DEM_MASTER_BITS_PER_CPU 6
784
785/* Power Master State Bits - Per CPU */
786#define DEM_MASTER_SMSM_RUN \
787 (0x01UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
788#define DEM_MASTER_SMSM_RSA \
789 (0x02UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
790#define DEM_MASTER_SMSM_PWRC_EARLY_EXIT \
791 (0x04UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
792#define DEM_MASTER_SMSM_SLEEP_EXIT \
793 (0x08UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
794#define DEM_MASTER_SMSM_READY \
795 (0x10UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
796#define DEM_MASTER_SMSM_SLEEP \
797 (0x20UL << (DEM_MASTER_BITS_PER_CPU * SMSM_APPS_STATE))
798
799/* Power Slave State Bits */
800#define DEM_SLAVE_SMSM_RUN (0x0001)
801#define DEM_SLAVE_SMSM_PWRC (0x0002)
802#define DEM_SLAVE_SMSM_PWRC_DELAY (0x0004)
803#define DEM_SLAVE_SMSM_PWRC_EARLY_EXIT (0x0008)
804#define DEM_SLAVE_SMSM_WFPI (0x0010)
805#define DEM_SLAVE_SMSM_SLEEP (0x0020)
806#define DEM_SLAVE_SMSM_SLEEP_EXIT (0x0040)
807#define DEM_SLAVE_SMSM_MSGS_REDUCED (0x0080)
808#define DEM_SLAVE_SMSM_RESET (0x0100)
809#define DEM_SLAVE_SMSM_PWRC_SUSPEND (0x0200)
810
811
812/******************************************************************************
813 * Shared Memory Data
814 *****************************************************************************/
815
816#define DEM_MAX_PORT_NAME_LEN (20)
817
818struct msm_pm_smem_t {
819 uint32_t sleep_time;
820 uint32_t irq_mask;
821 uint32_t resources_used;
822 uint32_t reserved1;
823
824 uint32_t wakeup_reason;
825 uint32_t pending_irqs;
826 uint32_t rpc_prog;
827 uint32_t rpc_proc;
828 char smd_port_name[DEM_MAX_PORT_NAME_LEN];
829 uint32_t reserved2;
830};
831
832
833/******************************************************************************
834 *
835 *****************************************************************************/
836static struct msm_pm_smem_t *msm_pm_smem_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700837static atomic_t msm_pm_init_done = ATOMIC_INIT(0);
838
839static int msm_pm_modem_busy(void)
840{
841 if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) {
842 MSM_PM_DPRINTK(MSM_PM_DEBUG_POWER_COLLAPSE,
843 KERN_INFO, "%s(): master not ready\n", __func__);
844 return -EBUSY;
845 }
846
847 return 0;
848}
849
850/*
851 * Power collapse the Apps processor. This function executes the handshake
852 * protocol with Modem.
853 *
854 * Return value:
855 * -EAGAIN: modem reset occurred or early exit from power collapse
856 * -EBUSY: modem not ready for our power collapse -- no power loss
857 * -ETIMEDOUT: timed out waiting for modem's handshake -- no power loss
858 * 0: success
859 */
860static int msm_pm_power_collapse
861 (bool from_idle, uint32_t sleep_delay, uint32_t sleep_limit)
862{
863 struct msm_pm_polled_group state_grps[2];
864 unsigned long saved_acpuclk_rate;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865 int collapsed = 0;
866 int ret;
Murali Nalajala07b04022012-04-10 16:00:49 +0530867 int val;
868 int modem_early_exit = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869
870 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
871 KERN_INFO, "%s(): idle %d, delay %u, limit %u\n", __func__,
872 (int)from_idle, sleep_delay, sleep_limit);
873
874 if (!(smsm_get_state(SMSM_POWER_MASTER_DEM) & DEM_MASTER_SMSM_READY)) {
875 MSM_PM_DPRINTK(
876 MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
877 KERN_INFO, "%s(): master not ready\n", __func__);
878 ret = -EBUSY;
879 goto power_collapse_bail;
880 }
881
882 memset(msm_pm_smem_data, 0, sizeof(*msm_pm_smem_data));
883
Utsab Bose4ed4ba12012-11-08 18:52:38 +0530884 if (cpu_is_msm8625() || cpu_is_msm8625q()) {
Murali Nalajala41786ab2012-03-06 10:47:32 +0530885 /* Program the SPM */
886 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE,
887 false);
888 WARN_ON(ret);
889 }
890
Murali Nalajalaff723ec2012-07-13 16:54:40 +0530891 /* Call CPR suspend only for "idlePC" case */
892 if (msm_cpr_ops && from_idle)
893 msm_cpr_ops->cpr_suspend();
894
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530895 msm_pm_irq_extns->enter_sleep1(true, from_idle,
896 &msm_pm_smem_data->irq_mask);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700897 msm_sirc_enter_sleep();
898 msm_gpio_enter_sleep(from_idle);
899
900 msm_pm_smem_data->sleep_time = sleep_delay;
901 msm_pm_smem_data->resources_used = sleep_limit;
902
903 /* Enter PWRC/PWRC_SUSPEND */
904
905 if (from_idle)
906 smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN,
907 DEM_SLAVE_SMSM_PWRC);
908 else
909 smsm_change_state(SMSM_APPS_DEM, DEM_SLAVE_SMSM_RUN,
910 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND);
911
912 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC");
913 MSM_PM_DEBUG_PRINT_SLEEP_INFO();
914
915 memset(state_grps, 0, sizeof(state_grps));
916 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
917 state_grps[0].bits_all_set = DEM_MASTER_SMSM_RSA;
918 state_grps[1].group_id = SMSM_MODEM_STATE;
919 state_grps[1].bits_all_set = SMSM_RESET;
920
921 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
922
923 if (ret < 0) {
924 printk(KERN_EMERG "%s(): power collapse entry "
925 "timed out waiting for Modem's response\n", __func__);
926 msm_pm_timeout();
927 }
928
929 if (ret == 1) {
930 MSM_PM_DPRINTK(
931 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
932 KERN_INFO,
933 "%s(): msm_pm_poll_state detected Modem reset\n",
934 __func__);
935 goto power_collapse_early_exit;
936 }
937
938 /* DEM Master in RSA */
939
940 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): PWRC RSA");
941
Murali Nalajala2a0bbda2012-03-28 12:12:54 +0530942 ret = msm_pm_irq_extns->enter_sleep2(true, from_idle);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700943 if (ret < 0) {
944 MSM_PM_DPRINTK(
945 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
946 KERN_INFO,
947 "%s(): msm_irq_enter_sleep2 aborted, %d\n", __func__,
948 ret);
949 goto power_collapse_early_exit;
950 }
951
952 msm_pm_config_hw_before_power_down();
953 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): pre power down");
954
955 saved_acpuclk_rate = acpuclk_power_collapse();
956 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
957 "%s(): change clock rate (old rate = %lu)\n", __func__,
958 saved_acpuclk_rate);
959
960 if (saved_acpuclk_rate == 0) {
961 msm_pm_config_hw_after_power_up();
962 goto power_collapse_early_exit;
963 }
964
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -0600965 msm_pm_boot_config_before_pc(smp_processor_id(),
966 virt_to_phys(msm_pm_collapse_exit));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700967
968#ifdef CONFIG_VFP
969 if (from_idle)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700970 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971#endif
972
973#ifdef CONFIG_CACHE_L2X0
Utsab Bose4ed4ba12012-11-08 18:52:38 +0530974 if (!cpu_is_msm8625() && !cpu_is_msm8625q())
Murali Nalajala73c13332012-05-15 11:30:59 +0530975 l2cc_suspend();
976 else
977 apps_power_collapse = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700978#endif
979
980 collapsed = msm_pm_collapse();
Murali Nalajala07b04022012-04-10 16:00:49 +0530981
982 /*
983 * TBD: Currently recognise the MODEM early exit
984 * path by reading the MPA5_GDFS_CNT_VAL register.
985 */
Utsab Bose4ed4ba12012-11-08 18:52:38 +0530986 if (cpu_is_msm8625() || cpu_is_msm8625q()) {
Murali Nalajalad1def3a2012-11-19 17:11:22 +0530987 int cpu;
Murali Nalajala07b04022012-04-10 16:00:49 +0530988 /*
Murali Nalajala93e6ed02012-05-13 12:57:22 +0530989 * on system reset, default value of MPA5_GDFS_CNT_VAL
990 * is = 0x0, later modem reprogram this value to
Murali Nalajalaaa7310b2012-10-19 18:47:27 +0530991 * 0x00030004/0x000F0004(8x25Q). Once APPS did
992 * a power collapse and coming out of it expected value
993 * of this register always be 0x00030004/0x000F0004(8x25Q).
994 * Incase if APPS sees the value as 0x00030002/0x000F0002(8x25Q)
995 * consider this case as a modem early exit.
Murali Nalajala07b04022012-04-10 16:00:49 +0530996 */
997 val = __raw_readl(MSM_CFG_CTL_BASE + 0x38);
Murali Nalajalaaa7310b2012-10-19 18:47:27 +0530998
999 /* 8x25Q */
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301000 if (cpu_is_msm8625q()) {
Murali Nalajala1989f822012-10-19 19:50:03 +05301001 if (val != 0x000F0002) {
Murali Nalajalad1def3a2012-11-19 17:11:22 +05301002 for_each_possible_cpu(cpu) {
1003 if (!cpu)
1004 continue;
1005 per_cpu(power_collapsed, cpu) = 1;
1006 }
Murali Nalajala1989f822012-10-19 19:50:03 +05301007 /*
1008 * override DBGNOPOWERDN and program the GDFS
1009 * count val
1010 */
1011 __raw_writel(0x000F0002,
1012 (MSM_CFG_CTL_BASE + 0x38));
1013 } else
Murali Nalajalaaa7310b2012-10-19 18:47:27 +05301014 modem_early_exit = 1;
1015 } else {
Murali Nalajala1989f822012-10-19 19:50:03 +05301016 if (val != 0x00030002) {
Murali Nalajalad1def3a2012-11-19 17:11:22 +05301017 for_each_possible_cpu(cpu) {
1018 if (!cpu)
1019 continue;
1020 per_cpu(power_collapsed, cpu) = 1;
1021 }
Murali Nalajala1989f822012-10-19 19:50:03 +05301022 /*
1023 * override DBGNOPOWERDN and program the GDFS
1024 * count val
1025 */
1026 __raw_writel(0x00030002,
1027 (MSM_CFG_CTL_BASE + 0x38));
1028 } else
Murali Nalajalaaa7310b2012-10-19 18:47:27 +05301029 modem_early_exit = 1;
1030 }
Murali Nalajala07b04022012-04-10 16:00:49 +05301031 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001032
1033#ifdef CONFIG_CACHE_L2X0
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301034 if (!cpu_is_msm8625() && !cpu_is_msm8625q())
Murali Nalajala73c13332012-05-15 11:30:59 +05301035 l2cc_resume();
1036 else
1037 apps_power_collapse = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001038#endif
1039
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001040 msm_pm_boot_config_after_pc(smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001041
1042 if (collapsed) {
1043#ifdef CONFIG_VFP
1044 if (from_idle)
Steve Mucklef132c6c2012-06-06 18:30:57 -07001045 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001046#endif
1047 cpu_init();
1048 local_fiq_enable();
1049 }
1050
1051 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1052 KERN_INFO,
1053 "%s(): msm_pm_collapse returned %d\n", __func__, collapsed);
1054
1055 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1056 "%s(): restore clock rate to %lu\n", __func__,
1057 saved_acpuclk_rate);
1058 if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate,
1059 SETRATE_PC) < 0)
1060 printk(KERN_ERR "%s(): failed to restore clock rate(%lu)\n",
1061 __func__, saved_acpuclk_rate);
1062
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301063 msm_pm_irq_extns->exit_sleep1(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001064 msm_pm_smem_data->wakeup_reason,
1065 msm_pm_smem_data->pending_irqs);
1066
1067 msm_pm_config_hw_after_power_up();
1068 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): post power up");
1069
1070 memset(state_grps, 0, sizeof(state_grps));
1071 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1072 state_grps[0].bits_any_set =
1073 DEM_MASTER_SMSM_RSA | DEM_MASTER_SMSM_PWRC_EARLY_EXIT;
1074 state_grps[1].group_id = SMSM_MODEM_STATE;
1075 state_grps[1].bits_all_set = SMSM_RESET;
1076
1077 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1078
1079 if (ret < 0) {
1080 printk(KERN_EMERG "%s(): power collapse exit "
1081 "timed out waiting for Modem's response\n", __func__);
1082 msm_pm_timeout();
1083 }
1084
1085 if (ret == 1) {
1086 MSM_PM_DPRINTK(
1087 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1088 KERN_INFO,
1089 "%s(): msm_pm_poll_state detected Modem reset\n",
1090 __func__);
1091 goto power_collapse_early_exit;
1092 }
1093
1094 /* Sanity check */
Murali Nalajala07b04022012-04-10 16:00:49 +05301095 if (collapsed && !modem_early_exit) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001096 BUG_ON(!(state_grps[0].value_read & DEM_MASTER_SMSM_RSA));
1097 } else {
1098 BUG_ON(!(state_grps[0].value_read &
1099 DEM_MASTER_SMSM_PWRC_EARLY_EXIT));
1100 goto power_collapse_early_exit;
1101 }
1102
1103 /* Enter WFPI */
1104
1105 smsm_change_state(SMSM_APPS_DEM,
1106 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND,
1107 DEM_SLAVE_SMSM_WFPI);
1108
1109 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI");
1110
1111 memset(state_grps, 0, sizeof(state_grps));
1112 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1113 state_grps[0].bits_all_set = DEM_MASTER_SMSM_RUN;
1114 state_grps[1].group_id = SMSM_MODEM_STATE;
1115 state_grps[1].bits_all_set = SMSM_RESET;
1116
1117 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1118
1119 if (ret < 0) {
1120 printk(KERN_EMERG "%s(): power collapse WFPI "
1121 "timed out waiting for Modem's response\n", __func__);
1122 msm_pm_timeout();
1123 }
1124
1125 if (ret == 1) {
1126 MSM_PM_DPRINTK(
1127 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1128 KERN_INFO,
1129 "%s(): msm_pm_poll_state detected Modem reset\n",
1130 __func__);
1131 ret = -EAGAIN;
1132 goto power_collapse_restore_gpio_bail;
1133 }
1134
1135 /* DEM Master == RUN */
1136
1137 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): WFPI RUN");
1138 MSM_PM_DEBUG_PRINT_SLEEP_INFO();
1139
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301140 msm_pm_irq_extns->exit_sleep2(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001141 msm_pm_smem_data->wakeup_reason,
1142 msm_pm_smem_data->pending_irqs);
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301143 msm_pm_irq_extns->exit_sleep3(msm_pm_smem_data->irq_mask,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001144 msm_pm_smem_data->wakeup_reason,
1145 msm_pm_smem_data->pending_irqs);
1146 msm_gpio_exit_sleep();
1147 msm_sirc_exit_sleep();
1148
1149 smsm_change_state(SMSM_APPS_DEM,
1150 DEM_SLAVE_SMSM_WFPI, DEM_SLAVE_SMSM_RUN);
1151
1152 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN");
1153
1154 smd_sleep_exit();
Murali Nalajala41786ab2012-03-06 10:47:32 +05301155
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301156 if (cpu_is_msm8625() || cpu_is_msm8625q()) {
Murali Nalajala41786ab2012-03-06 10:47:32 +05301157 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING,
1158 false);
1159 WARN_ON(ret);
1160 }
1161
Murali Nalajalaff723ec2012-07-13 16:54:40 +05301162 /* Call CPR resume only for "idlePC" case */
1163 if (msm_cpr_ops && from_idle)
1164 msm_cpr_ops->cpr_resume();
1165
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001166 return 0;
1167
1168power_collapse_early_exit:
1169 /* Enter PWRC_EARLY_EXIT */
1170
1171 smsm_change_state(SMSM_APPS_DEM,
1172 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND,
1173 DEM_SLAVE_SMSM_PWRC_EARLY_EXIT);
1174
1175 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT");
1176
1177 memset(state_grps, 0, sizeof(state_grps));
1178 state_grps[0].group_id = SMSM_POWER_MASTER_DEM;
1179 state_grps[0].bits_all_set = DEM_MASTER_SMSM_PWRC_EARLY_EXIT;
1180 state_grps[1].group_id = SMSM_MODEM_STATE;
1181 state_grps[1].bits_all_set = SMSM_RESET;
1182
1183 ret = msm_pm_poll_state(ARRAY_SIZE(state_grps), state_grps);
1184 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): EARLY_EXIT EE");
1185
1186 if (ret < 0) {
1187 printk(KERN_EMERG "%s(): power collapse EARLY_EXIT "
1188 "timed out waiting for Modem's response\n", __func__);
1189 msm_pm_timeout();
1190 }
1191
1192 if (ret == 1) {
1193 MSM_PM_DPRINTK(
1194 MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1195 KERN_INFO,
1196 "%s(): msm_pm_poll_state detected Modem reset\n",
1197 __func__);
1198 }
1199
1200 /* DEM Master == RESET or PWRC_EARLY_EXIT */
1201
1202 ret = -EAGAIN;
1203
1204power_collapse_restore_gpio_bail:
1205 msm_gpio_exit_sleep();
1206 msm_sirc_exit_sleep();
1207
1208 /* Enter RUN */
1209 smsm_change_state(SMSM_APPS_DEM,
1210 DEM_SLAVE_SMSM_PWRC | DEM_SLAVE_SMSM_PWRC_SUSPEND |
1211 DEM_SLAVE_SMSM_PWRC_EARLY_EXIT, DEM_SLAVE_SMSM_RUN);
1212
1213 MSM_PM_DEBUG_PRINT_STATE("msm_pm_power_collapse(): RUN");
1214
1215 if (collapsed)
1216 smd_sleep_exit();
1217
Murali Nalajalaff723ec2012-07-13 16:54:40 +05301218 /* Call CPR resume only for "idlePC" case */
1219 if (msm_cpr_ops && from_idle)
1220 msm_cpr_ops->cpr_resume();
1221
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001222power_collapse_bail:
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301223 if (cpu_is_msm8625() || cpu_is_msm8625q()) {
Murali Nalajala41786ab2012-03-06 10:47:32 +05301224 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING,
1225 false);
1226 WARN_ON(ret);
1227 }
1228
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229 return ret;
1230}
1231
1232/*
1233 * Power collapse the Apps processor without involving Modem.
1234 *
1235 * Return value:
1236 * 0: success
1237 */
Stephen Boydb29750d2012-02-21 01:21:32 -08001238static int __ref msm_pm_power_collapse_standalone(bool from_idle)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001239{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001240 int collapsed = 0;
1241 int ret;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301242 void *entry;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001243
1244 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND|MSM_PM_DEBUG_POWER_COLLAPSE,
1245 KERN_INFO, "%s()\n", __func__);
1246
1247 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_POWER_COLLAPSE, false);
1248 WARN_ON(ret);
1249
Murali Nalajala41786ab2012-03-06 10:47:32 +05301250 entry = (!smp_processor_id() || from_idle) ?
1251 msm_pm_collapse_exit : msm_secondary_startup;
1252
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001253 msm_pm_boot_config_before_pc(smp_processor_id(),
Murali Nalajala41786ab2012-03-06 10:47:32 +05301254 virt_to_phys(entry));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001255
1256#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -07001257 vfp_pm_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001258#endif
1259
1260#ifdef CONFIG_CACHE_L2X0
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301261 if (!cpu_is_msm8625() && !cpu_is_msm8625q())
Taniya Das38a8c6e2012-05-09 20:34:39 +05301262 l2cc_suspend();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001263#endif
1264
1265 collapsed = msm_pm_collapse();
1266
1267#ifdef CONFIG_CACHE_L2X0
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301268 if (!cpu_is_msm8625() && !cpu_is_msm8625q())
Taniya Das38a8c6e2012-05-09 20:34:39 +05301269 l2cc_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001270#endif
1271
Maheshkumar Sivasubramanian8ccc16e2011-10-25 15:59:57 -06001272 msm_pm_boot_config_after_pc(smp_processor_id());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001273
1274 if (collapsed) {
1275#ifdef CONFIG_VFP
Steve Mucklef132c6c2012-06-06 18:30:57 -07001276 vfp_pm_resume();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001277#endif
1278 cpu_init();
1279 local_fiq_enable();
1280 }
1281
1282 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND | MSM_PM_DEBUG_POWER_COLLAPSE,
1283 KERN_INFO,
1284 "%s(): msm_pm_collapse returned %d\n", __func__, collapsed);
1285
1286 ret = msm_spm_set_low_power_mode(MSM_SPM_MODE_CLOCK_GATING, false);
1287 WARN_ON(ret);
1288
Anji jonnalac6816222012-03-31 10:55:14 +05301289 return !collapsed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001290}
1291
1292/*
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001293 * Bring the Apps processor to SWFI.
1294 *
1295 * Return value:
1296 * -EIO: could not ramp Apps processor clock
1297 * 0: success
1298 */
1299static int msm_pm_swfi(bool ramp_acpu)
1300{
1301 unsigned long saved_acpuclk_rate = 0;
1302
1303 if (ramp_acpu) {
1304 saved_acpuclk_rate = acpuclk_wait_for_irq();
1305 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1306 "%s(): change clock rate (old rate = %lu)\n", __func__,
1307 saved_acpuclk_rate);
1308
1309 if (!saved_acpuclk_rate)
1310 return -EIO;
1311 }
1312
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301313 if (!cpu_is_msm8625() && !cpu_is_msm8625q())
Murali Nalajala41786ab2012-03-06 10:47:32 +05301314 msm_pm_config_hw_before_swfi();
1315
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001316 msm_arch_idle();
1317
1318 if (ramp_acpu) {
1319 MSM_PM_DPRINTK(MSM_PM_DEBUG_CLOCK, KERN_INFO,
1320 "%s(): restore clock rate to %lu\n", __func__,
1321 saved_acpuclk_rate);
1322 if (acpuclk_set_rate(smp_processor_id(), saved_acpuclk_rate,
1323 SETRATE_SWFI) < 0)
1324 printk(KERN_ERR
1325 "%s(): failed to restore clock rate(%lu)\n",
1326 __func__, saved_acpuclk_rate);
1327 }
1328
1329 return 0;
1330}
1331
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301332static int64_t msm_pm_timer_enter_suspend(int64_t *period)
1333{
Anji Jonnalac02367a2012-07-01 02:56:11 +05301334 int64_t time = 0;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301335
1336 time = msm_timer_get_sclk_time(period);
1337 if (!time)
1338 pr_err("%s: Unable to read sclk.\n", __func__);
1339 return time;
1340}
1341
1342static int64_t msm_pm_timer_exit_suspend(int64_t time, int64_t period)
1343{
1344
1345 if (time != 0) {
1346 int64_t end_time = msm_timer_get_sclk_time(NULL);
1347 if (end_time != 0) {
1348 time = end_time - time;
1349 if (time < 0)
1350 time += period;
1351 } else
1352 time = 0;
1353 }
1354 return time;
1355}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001356
1357/******************************************************************************
1358 * External Idle/Suspend Functions
1359 *****************************************************************************/
1360
1361/*
1362 * Put CPU in low power mode.
1363 */
1364void arch_idle(void)
1365{
1366 bool allow[MSM_PM_SLEEP_MODE_NR];
1367 uint32_t sleep_limit = SLEEP_LIMIT_NONE;
1368
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001369 int64_t timer_expiration;
Murali Nalajala8fda4492012-03-19 18:22:59 +05301370 int latency_qos;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001371 int ret;
1372 int i;
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301373 unsigned int cpu;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001374 int64_t t1;
Murali Nalajalab86f3702012-03-30 17:54:57 +05301375 static DEFINE_PER_CPU(int64_t, t2);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001376 int exit_stat;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001377
1378 if (!atomic_read(&msm_pm_init_done))
1379 return;
1380
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301381 cpu = smp_processor_id();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382 latency_qos = pm_qos_request(PM_QOS_CPU_DMA_LATENCY);
Murali Nalajala8fda4492012-03-19 18:22:59 +05301383 /* get the next timer expiration */
1384 timer_expiration = ktime_to_ns(tick_nohz_get_sleep_length());
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001385
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001386 t1 = ktime_to_ns(ktime_get());
Murali Nalajalab86f3702012-03-30 17:54:57 +05301387 msm_pm_add_stat(MSM_PM_STAT_NOT_IDLE, t1 - __get_cpu_var(t2));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001388 msm_pm_add_stat(MSM_PM_STAT_REQUESTED_IDLE, timer_expiration);
Murali Nalajala7744d162012-01-13 13:06:03 +05301389 exit_stat = MSM_PM_STAT_IDLE_SPIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001390
1391 for (i = 0; i < ARRAY_SIZE(allow); i++)
1392 allow[i] = true;
1393
Murali Nalajala41786ab2012-03-06 10:47:32 +05301394 if (num_online_cpus() > 1 ||
1395 (timer_expiration < msm_pm_idle_sleep_min_time) ||
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301396 !msm_pm_irq_extns->idle_sleep_allowed()) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001397 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false;
1398 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN] = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001399 }
1400
1401 for (i = 0; i < ARRAY_SIZE(allow); i++) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301402 struct msm_pm_platform_data *mode =
1403 &msm_pm_modes[MSM_PM_MODE(cpu, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001404 if (!mode->idle_supported || !mode->idle_enabled ||
1405 mode->latency >= latency_qos ||
1406 mode->residency * 1000ULL >= timer_expiration)
1407 allow[i] = false;
1408 }
1409
1410 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1411 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
1412 uint32_t wait_us = CONFIG_MSM_IDLE_WAIT_ON_MODEM;
1413 while (msm_pm_modem_busy() && wait_us) {
1414 if (wait_us > 100) {
1415 udelay(100);
1416 wait_us -= 100;
1417 } else {
1418 udelay(wait_us);
1419 wait_us = 0;
1420 }
1421 }
1422
1423 if (msm_pm_modem_busy()) {
1424 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] = false;
1425 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]
1426 = false;
1427 }
1428 }
1429
1430 MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO,
1431 "%s(): latency qos %d, next timer %lld, sleep limit %u\n",
1432 __func__, latency_qos, timer_expiration, sleep_limit);
1433
1434 for (i = 0; i < ARRAY_SIZE(allow); i++)
1435 MSM_PM_DPRINTK(MSM_PM_DEBUG_IDLE, KERN_INFO,
1436 "%s(): allow %s: %d\n", __func__,
1437 msm_pm_sleep_mode_labels[i], (int)allow[i]);
1438
1439 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1440 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
Murali Nalajala8fda4492012-03-19 18:22:59 +05301441 /* Sync the timer with SCLK, it is needed only for modem
1442 * assissted pollapse case.
1443 */
1444 int64_t next_timer_exp = msm_timer_enter_idle();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001445 uint32_t sleep_delay;
Murali Nalajala8fda4492012-03-19 18:22:59 +05301446 bool low_power = false;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001447
1448 sleep_delay = (uint32_t) msm_pm_convert_and_cap_time(
Murali Nalajala8fda4492012-03-19 18:22:59 +05301449 next_timer_exp, MSM_PM_SLEEP_TICK_LIMIT);
1450
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001451 if (sleep_delay == 0) /* 0 would mean infinite time */
1452 sleep_delay = 1;
1453
1454 if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
1455 sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN;
1456
1457#if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_ACTIVE)
1458 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1;
1459#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_IDLE_RETENTION)
1460 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1461#endif
1462
1463 ret = msm_pm_power_collapse(true, sleep_delay, sleep_limit);
1464 low_power = (ret != -EBUSY && ret != -ETIMEDOUT);
Murali Nalajala8fda4492012-03-19 18:22:59 +05301465 msm_timer_exit_idle(low_power);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001466
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001467 if (ret)
1468 exit_stat = MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE;
1469 else {
1470 exit_stat = MSM_PM_STAT_IDLE_POWER_COLLAPSE;
1471 msm_pm_sleep_limit = sleep_limit;
1472 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001473 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
Murali Nalajala41786ab2012-03-06 10:47:32 +05301474 ret = msm_pm_power_collapse_standalone(true);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001475 exit_stat = ret ?
1476 MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE :
1477 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001478 } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) {
1479 ret = msm_pm_swfi(true);
1480 if (ret)
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301481 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001482 udelay(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001483 exit_stat = ret ? MSM_PM_STAT_IDLE_SPIN : MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001484 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1485 msm_pm_swfi(false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001486 exit_stat = MSM_PM_STAT_IDLE_WFI;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001487 } else {
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301488 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001489 udelay(1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001490 exit_stat = MSM_PM_STAT_IDLE_SPIN;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001491 }
1492
Murali Nalajalab86f3702012-03-30 17:54:57 +05301493 __get_cpu_var(t2) = ktime_to_ns(ktime_get());
1494 msm_pm_add_stat(exit_stat, __get_cpu_var(t2) - t1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001495}
1496
1497/*
1498 * Suspend the Apps processor.
1499 *
1500 * Return value:
Murali Nalajala41786ab2012-03-06 10:47:32 +05301501 * -EPERM: Suspend happened by a not permitted core
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001502 * -EAGAIN: modem reset occurred or early exit from suspend
1503 * -EBUSY: modem not ready for our suspend
1504 * -EINVAL: invalid sleep mode
1505 * -EIO: could not ramp Apps processor clock
1506 * -ETIMEDOUT: timed out waiting for modem's handshake
1507 * 0: success
1508 */
1509static int msm_pm_enter(suspend_state_t state)
1510{
1511 bool allow[MSM_PM_SLEEP_MODE_NR];
1512 uint32_t sleep_limit = SLEEP_LIMIT_NONE;
Murali Nalajala41786ab2012-03-06 10:47:32 +05301513 int ret = -EPERM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001514 int i;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001515 int64_t period = 0;
1516 int64_t time = 0;
1517
Murali Nalajala41786ab2012-03-06 10:47:32 +05301518 /* Must executed by CORE0 */
1519 if (smp_processor_id()) {
1520 __WARN();
1521 goto suspend_exit;
1522 }
1523
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301524 time = msm_pm_timer_enter_suspend(&period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001525
1526 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
1527 "%s(): sleep limit %u\n", __func__, sleep_limit);
1528
1529 for (i = 0; i < ARRAY_SIZE(allow); i++)
1530 allow[i] = true;
1531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001532 for (i = 0; i < ARRAY_SIZE(allow); i++) {
Murali Nalajala0df9fee2012-01-12 15:26:09 +05301533 struct msm_pm_platform_data *mode;
1534 mode = &msm_pm_modes[MSM_PM_MODE(0, i)];
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001535 if (!mode->suspend_supported || !mode->suspend_enabled)
1536 allow[i] = false;
1537 }
1538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001539 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE] ||
1540 allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_NO_XO_SHUTDOWN]) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001541 enum msm_pm_time_stats_id id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001542
1543 clock_debug_print_enabled();
1544
1545#ifdef CONFIG_MSM_SLEEP_TIME_OVERRIDE
1546 if (msm_pm_sleep_time_override > 0) {
1547 int64_t ns;
1548 ns = NSEC_PER_SEC * (int64_t)msm_pm_sleep_time_override;
1549 msm_pm_set_max_sleep_time(ns);
1550 msm_pm_sleep_time_override = 0;
1551 }
1552#endif
1553 if (!allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE])
1554 sleep_limit = SLEEP_LIMIT_NO_TCXO_SHUTDOWN;
1555
1556#if defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_ACTIVE)
1557 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT1;
1558#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_RETENTION)
1559 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1560#elif defined(CONFIG_MSM_MEMORY_LOW_POWER_MODE_SUSPEND_DEEP_POWER_DOWN)
1561 if (get_msm_migrate_pages_status() != MEM_OFFLINE)
1562 sleep_limit |= SLEEP_RESOURCE_MEMORY_BIT0;
1563#endif
1564
1565 for (i = 0; i < 30 && msm_pm_modem_busy(); i++)
1566 udelay(500);
1567
1568 ret = msm_pm_power_collapse(
1569 false, msm_pm_max_sleep_time, sleep_limit);
1570
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001571 if (ret)
1572 id = MSM_PM_STAT_FAILED_SUSPEND;
1573 else {
1574 id = MSM_PM_STAT_SUSPEND;
1575 msm_pm_sleep_limit = sleep_limit;
1576 }
1577
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301578 time = msm_pm_timer_exit_suspend(time, period);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001579 msm_pm_add_stat(id, time);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001580 } else if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
Murali Nalajala41786ab2012-03-06 10:47:32 +05301581 ret = msm_pm_power_collapse_standalone(false);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001582 } else if (allow[MSM_PM_SLEEP_MODE_RAMP_DOWN_AND_WAIT_FOR_INTERRUPT]) {
1583 ret = msm_pm_swfi(true);
1584 if (ret)
Murali Nalajala2a0bbda2012-03-28 12:12:54 +05301585 while (!msm_pm_irq_extns->irq_pending())
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001586 udelay(1);
1587 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1588 msm_pm_swfi(false);
1589 }
1590
Murali Nalajala41786ab2012-03-06 10:47:32 +05301591suspend_exit:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001592 MSM_PM_DPRINTK(MSM_PM_DEBUG_SUSPEND, KERN_INFO,
1593 "%s(): return %d\n", __func__, ret);
1594
1595 return ret;
1596}
1597
1598static struct platform_suspend_ops msm_pm_ops = {
1599 .enter = msm_pm_enter,
1600 .valid = suspend_valid_only_mem,
1601};
1602
Murali Nalajalac89f2f32012-02-07 19:23:52 +05301603/* Hotplug the "non boot" CPU's and put
1604 * the cores into low power mode
1605 */
1606void msm_pm_cpu_enter_lowpower(unsigned int cpu)
1607{
Murali Nalajalaa7efba12012-02-23 18:13:52 +05301608 bool allow[MSM_PM_SLEEP_MODE_NR];
1609 int i;
1610
1611 for (i = 0; i < MSM_PM_SLEEP_MODE_NR; i++) {
1612 struct msm_pm_platform_data *mode;
1613
1614 mode = &msm_pm_modes[MSM_PM_MODE(cpu, i)];
1615 allow[i] = mode->suspend_supported && mode->suspend_enabled;
1616 }
1617
1618 MSM_PM_DPRINTK(MSM_PM_DEBUG_HOTPLUG, KERN_INFO,
1619 "CPU%u: %s: shutting down cpu\n", cpu, __func__);
1620
1621 if (allow[MSM_PM_SLEEP_MODE_POWER_COLLAPSE_STANDALONE]) {
1622 msm_pm_power_collapse_standalone(false);
1623 } else if (allow[MSM_PM_SLEEP_MODE_WAIT_FOR_INTERRUPT]) {
1624 msm_pm_swfi(false);
1625 } else {
1626 MSM_PM_DPRINTK(MSM_PM_DEBUG_HOTPLUG, KERN_INFO,
1627 "CPU%u: %s: shutting down failed!!!\n", cpu, __func__);
1628 }
Murali Nalajalac89f2f32012-02-07 19:23:52 +05301629}
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001630
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001631/*
1632 * Initialize the power management subsystem.
1633 *
1634 * Return value:
1635 * -ENODEV: initialization failed
1636 * 0: success
1637 */
1638static int __init msm_pm_init(void)
1639{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001640 int ret;
Murali Nalajala93f29992012-03-21 15:59:27 +05301641 int val;
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301642 enum msm_pm_time_stats_id enable_stats[] = {
1643 MSM_PM_STAT_REQUESTED_IDLE,
1644 MSM_PM_STAT_IDLE_SPIN,
1645 MSM_PM_STAT_IDLE_WFI,
1646 MSM_PM_STAT_IDLE_STANDALONE_POWER_COLLAPSE,
1647 MSM_PM_STAT_IDLE_FAILED_STANDALONE_POWER_COLLAPSE,
1648 MSM_PM_STAT_IDLE_POWER_COLLAPSE,
1649 MSM_PM_STAT_IDLE_FAILED_POWER_COLLAPSE,
1650 MSM_PM_STAT_SUSPEND,
1651 MSM_PM_STAT_FAILED_SUSPEND,
1652 MSM_PM_STAT_NOT_IDLE,
1653 };
1654
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001655#ifdef CONFIG_CPU_V7
1656 pgd_t *pc_pgd;
1657 pmd_t *pmd;
1658 unsigned long pmdval;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001659 unsigned long exit_phys;
1660
1661 exit_phys = virt_to_phys(msm_pm_collapse_exit);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001662
1663 /* Page table for cores to come back up safely. */
1664 pc_pgd = pgd_alloc(&init_mm);
1665 if (!pc_pgd)
1666 return -ENOMEM;
Steve Mucklef132c6c2012-06-06 18:30:57 -07001667 pmd = pmd_offset(pud_offset(pc_pgd + pgd_index(exit_phys), exit_phys),
1668 exit_phys);
1669 pmdval = (exit_phys & PGDIR_MASK) |
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001670 PMD_TYPE_SECT | PMD_SECT_AP_WRITE;
1671 pmd[0] = __pmd(pmdval);
1672 pmd[1] = __pmd(pmdval + (1 << (PGDIR_SHIFT - 1)));
1673
Steve Mucklefcece052012-02-18 20:09:58 -08001674 msm_saved_state_phys =
1675 allocate_contiguous_ebi_nomap(CPU_SAVED_STATE_SIZE *
1676 num_possible_cpus(), 4);
1677 if (!msm_saved_state_phys)
1678 return -ENOMEM;
1679 msm_saved_state = ioremap_nocache(msm_saved_state_phys,
1680 CPU_SAVED_STATE_SIZE *
1681 num_possible_cpus());
1682 if (!msm_saved_state)
1683 return -ENOMEM;
1684
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001685 /* It is remotely possible that the code in msm_pm_collapse_exit()
1686 * which turns on the MMU with this mapping is in the
1687 * next even-numbered megabyte beyond the
1688 * start of msm_pm_collapse_exit().
1689 * Map this megabyte in as well.
1690 */
1691 pmd[2] = __pmd(pmdval + (2 << (PGDIR_SHIFT - 1)));
1692 flush_pmd_entry(pmd);
1693 msm_pm_pc_pgd = virt_to_phys(pc_pgd);
Steve Muckle730ad7a2012-02-21 15:26:37 -08001694 clean_caches((unsigned long)&msm_pm_pc_pgd, sizeof(msm_pm_pc_pgd),
1695 virt_to_phys(&msm_pm_pc_pgd));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001696#endif
1697
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001698 msm_pm_smem_data = smem_alloc(SMEM_APPS_DEM_SLAVE_DATA,
1699 sizeof(*msm_pm_smem_data));
1700 if (msm_pm_smem_data == NULL) {
1701 printk(KERN_ERR "%s: failed to get smsm_data\n", __func__);
1702 return -ENODEV;
1703 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001704
1705 ret = msm_timer_init_time_sync(msm_pm_timeout);
1706 if (ret)
1707 return ret;
1708
1709 ret = smsm_change_intr_mask(SMSM_POWER_MASTER_DEM, 0xFFFFFFFF, 0);
1710 if (ret) {
1711 printk(KERN_ERR "%s: failed to clear interrupt mask, %d\n",
1712 __func__, ret);
1713 return ret;
1714 }
1715
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301716 if (cpu_is_msm8625() || cpu_is_msm8625q()) {
Murali Nalajala93f29992012-03-21 15:59:27 +05301717 target_type = TARGET_IS_8625;
1718 clean_caches((unsigned long)&target_type, sizeof(target_type),
1719 virt_to_phys(&target_type));
1720
Anji jonnalae644f8e2012-05-09 19:52:18 +05301721 /*
1722 * Configure the MPA5_GDFS_CNT_VAL register for
Murali Nalajalaaa7310b2012-10-19 18:47:27 +05301723 * DBGPWRUPEREQ_OVERRIDE[19:16] = Override the
Anji jonnalae644f8e2012-05-09 19:52:18 +05301724 * DBGNOPOWERDN for each cpu.
1725 * MPA5_GDFS_CNT_VAL[9:0] = Delay counter for
1726 * GDFS control.
Murali Nalajala93f29992012-03-21 15:59:27 +05301727 */
Utsab Bose4ed4ba12012-11-08 18:52:38 +05301728 if (cpu_is_msm8625q())
Murali Nalajalaaa7310b2012-10-19 18:47:27 +05301729 val = 0x000F0002;
1730 else
1731 val = 0x00030002;
1732
Murali Nalajala93f29992012-03-21 15:59:27 +05301733 __raw_writel(val, (MSM_CFG_CTL_BASE + 0x38));
Murali Nalajala73c13332012-05-15 11:30:59 +05301734
1735 l2x0_base_addr = MSM_L2CC_BASE;
Murali Nalajala93f29992012-03-21 15:59:27 +05301736 }
1737
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001738#ifdef CONFIG_MSM_MEMORY_LOW_POWER_MODE
1739 /* The wakeup_reason field is overloaded during initialization time
1740 to signal Modem that Apps will control the low power modes of
1741 the memory.
1742 */
1743 msm_pm_smem_data->wakeup_reason = 1;
1744 smsm_change_state(SMSM_APPS_DEM, 0, DEM_SLAVE_SMSM_RUN);
1745#endif
1746
1747 BUG_ON(msm_pm_modes == NULL);
1748
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001749 suspend_set_ops(&msm_pm_ops);
1750
1751 msm_pm_mode_sysfs_add();
Praveen Chidambaram3895bde2012-05-14 19:42:40 +05301752 msm_pm_add_stats(enable_stats, ARRAY_SIZE(enable_stats));
Murali Nalajala558c0ce2012-03-29 19:42:08 +05301753
1754 atomic_set(&msm_pm_init_done, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001755 return 0;
1756}
1757
1758late_initcall_sync(msm_pm_init);