2 * linux/arch/arm/mach-omap2/pm.c
4 * OMAP2 Power Management Routines
6 * Copyright (C) 2005 Texas Instruments, Inc.
7 * Copyright (C) 2006-2008 Nokia Corporation
10 * Richard Woodruff <r-woodruff2@ti.com>
13 * Amit Kucheria <amit.kucheria@nokia.com>
14 * Igor Stoppa <igor.stoppa@nokia.com>
16 * Based on pm.c for omap1
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2 as
20 * published by the Free Software Foundation.
23 #include <linux/suspend.h>
24 #include <linux/sched.h>
25 #include <linux/proc_fs.h>
26 #include <linux/interrupt.h>
27 #include <linux/sysfs.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/clk.h>
32 #include <linux/irq.h>
34 #include <asm/mach/time.h>
35 #include <asm/mach/irq.h>
36 #include <asm/mach-types.h>
38 #include <asm/arch/irqs.h>
39 #include <asm/arch/clock.h>
40 #include <asm/arch/sram.h>
41 #include <asm/arch/control.h>
42 #include <asm/arch/gpio.h>
43 #include <asm/arch/pm.h>
44 #include <asm/arch/mux.h>
45 #include <asm/arch/dma.h>
46 #include <asm/arch/board.h>
49 #include "prm-regbits-24xx.h"
51 #include "cm-regbits-24xx.h"
55 /* These addrs are in assembly language code to be patched at runtime */
56 extern void *omap2_ocs_sdrc_power;
57 extern void *omap2_ocs_sdrc_dlla_ctrl;
59 static void (*omap2_sram_idle)(void);
60 static void (*omap2_sram_suspend)(void __iomem *dllctrl);
61 static void (*saved_idle)(void);
63 static struct clk *osc_ck, *emul_ck;
65 static int omap2_fclks_active(void)
69 f1 = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
70 f2 = cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
71 serial_console_fclk_mask(&f1, &f2);
77 static void omap2_enter_full_retention(void)
79 u32 l, sleep_time = 0;
81 /* There is 1 reference hold for all children of the oscillator
82 * clock, the following will remove it. If no one else uses the
83 * oscillator itself it will be disabled if/when we enter retention
88 /* Clear old wake-up events */
89 /* REVISIT: These write to reserved bits? */
90 prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
91 prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
92 prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
94 /* Try to enter retention */
95 prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) | OMAP_LOGICRETSTATE,
96 MPU_MOD, PM_PWSTCTRL);
98 /* Workaround to kill USB */
99 l = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0) | OMAP24XX_USBSTANDBYCTRL;
100 omap_ctrl_writel(l, OMAP2_CONTROL_DEVCONF0);
102 omap2_gpio_prepare_for_retention();
104 if (omap2_pm_debug) {
105 omap2_pm_dump(0, 0, 0);
106 sleep_time = omap2_read_32k_sync_counter();
109 /* One last check for pending IRQs to avoid extra latency due
110 * to sleeping unnecessarily. */
111 if (omap_irq_pending())
114 serial_console_sleep(1);
115 /* Jump to SRAM suspend code */
116 omap2_sram_suspend(OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL));
118 serial_console_sleep(0);
120 if (omap2_pm_debug) {
121 unsigned long long tmp;
124 resume_time = omap2_read_32k_sync_counter();
125 tmp = resume_time - sleep_time;
127 omap2_pm_dump(0, 1, tmp / 32768);
129 omap2_gpio_resume_after_retention();
133 /* clear CORE wake-up events */
134 prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
135 prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
137 /* wakeup domain events - bit 1: GPT1, bit5 GPIO */
138 prm_clear_mod_reg_bits(0x4 | 0x1, WKUP_MOD, PM_WKST);
140 /* MPU domain wake events */
141 l = __raw_readl(OMAP24XX_PRCM_IRQSTATUS_MPU);
143 __raw_writel(0x01, OMAP24XX_PRCM_IRQSTATUS_MPU);
145 __raw_writel(0x20, OMAP24XX_PRCM_IRQSTATUS_MPU);
147 /* Mask future PRCM-to-MPU interrupts */
148 __raw_writel(0x0, OMAP24XX_PRCM_IRQSTATUS_MPU);
151 static int omap2_i2c_active(void)
155 l = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
156 return l & (OMAP2420_EN_I2C2 | OMAP2420_EN_I2C1);
159 static int sti_console_enabled;
161 static int omap2_allow_mpu_retention(void)
165 if (atomic_read(&sleep_block))
168 /* Check for MMC, UART2, UART1, McSPI2, McSPI1 and DSS1. */
169 l = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
170 if (l & (OMAP2420_EN_MMC | OMAP24XX_EN_UART2 |
171 OMAP24XX_EN_UART1 | OMAP24XX_EN_MCSPI2 |
172 OMAP24XX_EN_MCSPI1 | OMAP24XX_EN_DSS1))
174 /* Check for UART3. */
175 l = cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
176 if (l & OMAP24XX_EN_UART3)
178 if (sti_console_enabled)
184 static void omap2_enter_mpu_retention(void)
189 /* Putting MPU into the WFI state while a transfer is active
190 * seems to cause the I2C block to timeout. Why? Good question. */
191 if (omap2_i2c_active())
194 /* The peripherals seem not to be able to wake up the MPU when
195 * it is in retention mode. */
196 if (omap2_allow_mpu_retention()) {
197 /* REVISIT: These write to reserved bits? */
198 prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
199 prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
200 prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
202 /* Try to enter MPU retention */
203 prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) |
205 MPU_MOD, PM_PWSTCTRL);
207 /* Block MPU retention */
209 prm_write_mod_reg(OMAP_LOGICRETSTATE, MPU_MOD, PM_PWSTCTRL);
213 if (omap2_pm_debug) {
214 omap2_pm_dump(only_idle ? 2 : 1, 0, 0);
215 sleep_time = omap2_read_32k_sync_counter();
220 if (omap2_pm_debug) {
221 unsigned long long tmp;
224 resume_time = omap2_read_32k_sync_counter();
225 tmp = resume_time - sleep_time;
227 omap2_pm_dump(only_idle ? 2 : 1, 1, tmp / 32768);
231 static int omap2_can_sleep(void)
233 if (!enable_dyn_sleep)
235 if (omap2_fclks_active())
237 if (atomic_read(&sleep_block) > 0)
239 if (clk_get_usecount(osc_ck) > 1)
241 if (omap_dma_running())
248 * Note that you can use clock_event_device->min_delta_ns if you want to
249 * avoid reprogramming timer too often when using CONFIG_NO_HZ.
251 static void omap2_pm_idle(void)
256 if (!omap2_can_sleep()) {
257 if (!atomic_read(&sleep_block) && omap_irq_pending())
259 omap2_enter_mpu_retention();
263 if (omap_irq_pending())
266 omap2_enter_full_retention();
273 static int omap2_pm_prepare(void)
275 /* We cannot sleep in idle until we have resumed */
276 saved_idle = pm_idle;
282 static int omap2_pm_suspend(void)
286 wken_wkup = prm_read_mod_reg(WKUP_MOD, PM_WKEN);
287 prm_write_mod_reg(wken_wkup & ~OMAP24XX_EN_GPT1, WKUP_MOD, PM_WKEN);
290 mir1 = omap_readl(0x480fe0a4);
291 omap_writel(1 << 5, 0x480fe0ac);
293 omap2_enter_full_retention();
295 omap_writel(mir1, 0x480fe0a4);
296 prm_write_mod_reg(wken_wkup, WKUP_MOD, PM_WKEN);
301 static int omap2_pm_enter(suspend_state_t state)
306 case PM_SUSPEND_STANDBY:
308 ret = omap2_pm_suspend();
317 static void omap2_pm_finish(void)
319 pm_idle = saved_idle;
322 static struct platform_suspend_ops omap_pm_ops = {
323 .prepare = omap2_pm_prepare,
324 .enter = omap2_pm_enter,
325 .finish = omap2_pm_finish,
326 .valid = suspend_valid_only_mem,
329 static void __init prcm_setup_regs(void)
333 /* Enable autoidle */
334 __raw_writel(OMAP24XX_AUTOIDLE, OMAP24XX_PRCM_SYSCONFIG);
336 /* Set all domain wakeup dependencies */
337 prm_write_mod_reg(OMAP_EN_WKUP_MASK, MPU_MOD, PM_WKDEP);
338 prm_write_mod_reg(0, OMAP24XX_DSP_MOD, PM_WKDEP);
339 prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
340 prm_write_mod_reg(0, CORE_MOD, PM_WKDEP);
341 if (cpu_is_omap2430())
342 prm_write_mod_reg(0, OMAP2430_MDM_MOD, PM_WKDEP);
344 l = prm_read_mod_reg(CORE_MOD, PM_PWSTCTRL);
345 /* Enable retention for all memory blocks */
346 l |= OMAP24XX_MEM3RETSTATE | OMAP24XX_MEM2RETSTATE |
347 OMAP24XX_MEM1RETSTATE;
349 /* Set power state to RETENTION */
350 l &= ~OMAP_POWERSTATE_MASK;
351 l |= 0x01 << OMAP_POWERSTATE_SHIFT;
352 prm_write_mod_reg(l, CORE_MOD, PM_PWSTCTRL);
354 prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) |
356 MPU_MOD, PM_PWSTCTRL);
358 /* Power down DSP and GFX */
359 prm_write_mod_reg(OMAP24XX_FORCESTATE | (0x3 << OMAP_POWERSTATE_SHIFT),
360 OMAP24XX_DSP_MOD, PM_PWSTCTRL);
361 prm_write_mod_reg(OMAP24XX_FORCESTATE | (0x3 << OMAP_POWERSTATE_SHIFT),
362 GFX_MOD, PM_PWSTCTRL);
364 /* Enable clock auto control for all domains */
365 cm_write_mod_reg(OMAP24XX_AUTOSTATE_MPU_MASK, MPU_MOD, CM_CLKSTCTRL);
366 cm_write_mod_reg(OMAP24XX_AUTOSTATE_DSS_MASK |
367 OMAP24XX_AUTOSTATE_L4_MASK |
368 OMAP24XX_AUTOSTATE_L3_MASK,
369 CORE_MOD, CM_CLKSTCTRL);
370 cm_write_mod_reg(OMAP24XX_AUTOSTATE_GFX_MASK, GFX_MOD, CM_CLKSTCTRL);
371 cm_write_mod_reg(OMAP2420_AUTOSTATE_IVA_MASK |
372 OMAP24XX_AUTOSTATE_DSP_MASK,
373 OMAP24XX_DSP_MOD, CM_CLKSTCTRL);
375 /* Enable clock autoidle for all domains */
376 cm_write_mod_reg(OMAP24XX_AUTO_CAM |
377 OMAP24XX_AUTO_MAILBOXES |
380 OMAP24XX_AUTO_MSPRO |
385 OMAP24XX_AUTO_UART2 |
386 OMAP24XX_AUTO_UART1 |
389 OMAP24XX_AUTO_MCSPI2 |
390 OMAP24XX_AUTO_MCSPI1 |
391 OMAP24XX_AUTO_MCBSP2 |
392 OMAP24XX_AUTO_MCBSP1 |
393 OMAP24XX_AUTO_GPT12 |
394 OMAP24XX_AUTO_GPT11 |
395 OMAP24XX_AUTO_GPT10 |
404 OMAP2420_AUTO_VLYNQ |
406 CORE_MOD, CM_AUTOIDLE1);
407 cm_write_mod_reg(OMAP24XX_AUTO_UART3 |
410 CORE_MOD, CM_AUTOIDLE2);
411 cm_write_mod_reg(OMAP24XX_AUTO_SDRC |
414 CORE_MOD, CM_AUTOIDLE3);
415 cm_write_mod_reg(OMAP24XX_AUTO_PKA |
420 CORE_MOD, OMAP24XX_CM_AUTOIDLE4);
422 cm_write_mod_reg(OMAP2420_AUTO_DSP_IPI, OMAP24XX_DSP_MOD, CM_AUTOIDLE);
424 /* Put DPLL and both APLLs into autoidle mode */
425 cm_write_mod_reg((0x03 << OMAP24XX_AUTO_DPLL_SHIFT) |
426 (0x03 << OMAP24XX_AUTO_96M_SHIFT) |
427 (0x03 << OMAP24XX_AUTO_54M_SHIFT),
428 PLL_MOD, CM_AUTOIDLE);
430 cm_write_mod_reg(OMAP24XX_AUTO_OMAPCTRL |
432 OMAP24XX_AUTO_MPU_WDT |
433 OMAP24XX_AUTO_GPIOS |
434 OMAP24XX_AUTO_32KSYNC |
436 WKUP_MOD, CM_AUTOIDLE);
438 /* REVISIT: Configure number of 32 kHz clock cycles for sys_clk
440 __raw_writel(15 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_PRCM_CLKSSETUP);
442 /* Configure automatic voltage transition */
443 __raw_writel(2 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_PRCM_VOLTSETUP);
444 __raw_writel(OMAP24XX_AUTO_EXTVOLT |
445 (0x1 << OMAP24XX_SETOFF_LEVEL_SHIFT) |
446 OMAP24XX_MEMRETCTRL |
447 (0x1 << OMAP24XX_SETRET_LEVEL_SHIFT) |
448 (0x0 << OMAP24XX_VOLT_LEVEL_SHIFT),
449 OMAP24XX_PRCM_VOLTCTRL);
451 /* Enable wake-up events */
452 prm_write_mod_reg(OMAP24XX_EN_GPIOS | OMAP24XX_EN_GPT1,
456 int __init omap2_pm_init(void)
460 printk(KERN_INFO "Power Management for OMAP2 initializing\n");
461 l = __raw_readl(OMAP24XX_PRCM_REVISION);
462 printk(KERN_INFO "PRCM revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
464 osc_ck = clk_get(NULL, "osc_ck");
465 if (IS_ERR(osc_ck)) {
466 printk(KERN_ERR "could not get osc_ck\n");
470 if (cpu_is_omap242x()) {
471 emul_ck = clk_get(NULL, "emul_ck");
472 if (IS_ERR(emul_ck)) {
473 printk(KERN_ERR "could not get emul_ck\n");
481 pm_init_serial_console();
483 /* Hack to prevent MPU retention when STI console is enabled. */
485 const struct omap_sti_console_config *sti;
487 sti = omap_get_config(OMAP_TAG_STI_CONSOLE,
488 struct omap_sti_console_config);
489 if (sti != NULL && sti->enable)
490 sti_console_enabled = 1;
494 * We copy the assembler sleep/wakeup routines to SRAM.
495 * These routines need to be in SRAM as that's the only
496 * memory the MPU can see when it wakes up.
498 omap2_sram_idle = omap_sram_push(omap24xx_idle_loop_suspend,
499 omap24xx_idle_loop_suspend_sz);
501 omap2_sram_suspend = omap_sram_push(omap24xx_cpu_suspend,
502 omap24xx_cpu_suspend_sz);
504 /* Patch in the correct register addresses for multiboot */
505 omap_sram_patch_va(omap24xx_cpu_suspend, &omap2_ocs_sdrc_power,
507 OMAP_SDRC_REGADDR(SDRC_POWER));
508 omap_sram_patch_va(omap24xx_cpu_suspend, &omap2_ocs_sdrc_dlla_ctrl,
510 OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL));
512 suspend_set_ops(&omap_pm_ops);
513 pm_idle = omap2_pm_idle;