2 * OMAP2 Power Management Routines
4 * Copyright (C) 2005 Texas Instruments, Inc.
5 * Copyright (C) 2006-2008 Nokia Corporation
8 * Richard Woodruff <r-woodruff2@ti.com>
11 * Amit Kucheria <amit.kucheria@nokia.com>
12 * Igor Stoppa <igor.stoppa@nokia.com>
14 * Based on pm.c for omap1
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License version 2 as
18 * published by the Free Software Foundation.
21 #include <linux/suspend.h>
22 #include <linux/sched.h>
23 #include <linux/proc_fs.h>
24 #include <linux/interrupt.h>
25 #include <linux/sysfs.h>
26 #include <linux/module.h>
27 #include <linux/delay.h>
28 #include <linux/clk.h>
30 #include <linux/irq.h>
31 #include <linux/time.h>
32 #include <linux/gpio.h>
33 #include <linux/console.h>
35 #include <asm/mach/time.h>
36 #include <asm/mach/irq.h>
37 #include <asm/mach-types.h>
39 #include <mach/irqs.h>
40 #include <plat/clock.h>
41 #include <plat/sram.h>
43 #include <plat/board.h>
45 #include "prm2xxx_3xxx.h"
46 #include "prm-regbits-24xx.h"
47 #include "cm2xxx_3xxx.h"
48 #include "cm-regbits-24xx.h"
53 #include "powerdomain.h"
54 #include "clockdomain.h"
57 static suspend_state_t suspend_state = PM_SUSPEND_ON;
58 static inline bool is_suspending(void)
60 return (suspend_state != PM_SUSPEND_ON);
63 static inline bool is_suspending(void)
69 static void (*omap2_sram_idle)(void);
70 static void (*omap2_sram_suspend)(u32 dllctrl, void __iomem *sdrc_dlla_ctrl,
71 void __iomem *sdrc_power);
73 static struct powerdomain *mpu_pwrdm, *core_pwrdm;
74 static struct clockdomain *dsp_clkdm, *mpu_clkdm, *wkup_clkdm, *gfx_clkdm;
76 static struct clk *osc_ck, *emul_ck;
78 static int omap2_fclks_active(void)
82 f1 = omap2_cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
83 f2 = omap2_cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
85 /* Ignore UART clocks. These are handled by UART core (serial.c) */
86 f1 &= ~(OMAP24XX_EN_UART1_MASK | OMAP24XX_EN_UART2_MASK);
87 f2 &= ~OMAP24XX_EN_UART3_MASK;
94 static void omap2_enter_full_retention(void)
97 struct timespec ts_preidle, ts_postidle, ts_idle;
99 /* There is 1 reference hold for all children of the oscillator
100 * clock, the following will remove it. If no one else uses the
101 * oscillator itself it will be disabled if/when we enter retention
106 /* Clear old wake-up events */
107 /* REVISIT: These write to reserved bits? */
108 omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
109 omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
110 omap2_prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
113 * Set MPU powerdomain's next power state to RETENTION;
114 * preserve logic state during retention
116 pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
117 pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
119 /* Workaround to kill USB */
120 l = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0) | OMAP24XX_USBSTANDBYCTRL;
121 omap_ctrl_writel(l, OMAP2_CONTROL_DEVCONF0);
123 omap2_gpio_prepare_for_idle(0);
125 if (omap2_pm_debug) {
126 omap2_pm_dump(0, 0, 0);
127 getnstimeofday(&ts_preidle);
130 /* One last check for pending IRQs to avoid extra latency due
131 * to sleeping unnecessarily. */
132 if (omap_irq_pending())
135 /* Block console output in case it is on one of the OMAP UARTs */
136 if (!is_suspending())
137 if (!console_trylock())
140 omap_uart_prepare_idle(0);
141 omap_uart_prepare_idle(1);
142 omap_uart_prepare_idle(2);
144 /* Jump to SRAM suspend code */
145 omap2_sram_suspend(sdrc_read_reg(SDRC_DLLA_CTRL),
146 OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL),
147 OMAP_SDRC_REGADDR(SDRC_POWER));
149 omap_uart_resume_idle(2);
150 omap_uart_resume_idle(1);
151 omap_uart_resume_idle(0);
153 if (!is_suspending())
157 if (omap2_pm_debug) {
158 unsigned long long tmp;
160 getnstimeofday(&ts_postidle);
161 ts_idle = timespec_sub(ts_postidle, ts_preidle);
162 tmp = timespec_to_ns(&ts_idle) * NSEC_PER_USEC;
163 omap2_pm_dump(0, 1, tmp);
165 omap2_gpio_resume_after_idle();
169 /* clear CORE wake-up events */
170 omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
171 omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
173 /* wakeup domain events - bit 1: GPT1, bit5 GPIO */
174 omap2_prm_clear_mod_reg_bits(0x4 | 0x1, WKUP_MOD, PM_WKST);
176 /* MPU domain wake events */
177 l = omap2_prm_read_mod_reg(OCP_MOD, OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
179 omap2_prm_write_mod_reg(0x01, OCP_MOD,
180 OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
182 omap2_prm_write_mod_reg(0x20, OCP_MOD,
183 OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
185 /* Mask future PRCM-to-MPU interrupts */
186 omap2_prm_write_mod_reg(0x0, OCP_MOD, OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
189 static int omap2_i2c_active(void)
193 l = omap2_cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
194 return l & (OMAP2420_EN_I2C2_MASK | OMAP2420_EN_I2C1_MASK);
197 static int sti_console_enabled;
199 static int omap2_allow_mpu_retention(void)
203 /* Check for MMC, UART2, UART1, McSPI2, McSPI1 and DSS1. */
204 l = omap2_cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
205 if (l & (OMAP2420_EN_MMC_MASK | OMAP24XX_EN_UART2_MASK |
206 OMAP24XX_EN_UART1_MASK | OMAP24XX_EN_MCSPI2_MASK |
207 OMAP24XX_EN_MCSPI1_MASK | OMAP24XX_EN_DSS1_MASK))
209 /* Check for UART3. */
210 l = omap2_cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
211 if (l & OMAP24XX_EN_UART3_MASK)
213 if (sti_console_enabled)
219 static void omap2_enter_mpu_retention(void)
222 struct timespec ts_preidle, ts_postidle, ts_idle;
224 /* Putting MPU into the WFI state while a transfer is active
225 * seems to cause the I2C block to timeout. Why? Good question. */
226 if (omap2_i2c_active())
229 /* The peripherals seem not to be able to wake up the MPU when
230 * it is in retention mode. */
231 if (omap2_allow_mpu_retention()) {
232 /* REVISIT: These write to reserved bits? */
233 omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
234 omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
235 omap2_prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
237 /* Try to enter MPU retention */
238 omap2_prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) |
239 OMAP_LOGICRETSTATE_MASK,
240 MPU_MOD, OMAP2_PM_PWSTCTRL);
242 /* Block MPU retention */
244 omap2_prm_write_mod_reg(OMAP_LOGICRETSTATE_MASK, MPU_MOD,
249 if (omap2_pm_debug) {
250 omap2_pm_dump(only_idle ? 2 : 1, 0, 0);
251 getnstimeofday(&ts_preidle);
256 if (omap2_pm_debug) {
257 unsigned long long tmp;
259 getnstimeofday(&ts_postidle);
260 ts_idle = timespec_sub(ts_postidle, ts_preidle);
261 tmp = timespec_to_ns(&ts_idle) * NSEC_PER_USEC;
262 omap2_pm_dump(only_idle ? 2 : 1, 1, tmp);
266 static int omap2_can_sleep(void)
268 if (omap2_fclks_active())
270 if (!omap_uart_can_sleep())
272 if (osc_ck->usecount > 1)
274 if (omap_dma_running())
280 static void omap2_pm_idle(void)
285 if (!omap2_can_sleep()) {
286 if (omap_irq_pending())
288 omap2_enter_mpu_retention();
292 if (omap_irq_pending())
295 omap2_enter_full_retention();
302 #ifdef CONFIG_SUSPEND
303 static int omap2_pm_begin(suspend_state_t state)
306 suspend_state = state;
310 static int omap2_pm_suspend(void)
314 wken_wkup = omap2_prm_read_mod_reg(WKUP_MOD, PM_WKEN);
315 wken_wkup &= ~OMAP24XX_EN_GPT1_MASK;
316 omap2_prm_write_mod_reg(wken_wkup, WKUP_MOD, PM_WKEN);
319 mir1 = omap_readl(0x480fe0a4);
320 omap_writel(1 << 5, 0x480fe0ac);
322 omap_uart_prepare_suspend();
323 omap2_enter_full_retention();
325 omap_writel(mir1, 0x480fe0a4);
326 omap2_prm_write_mod_reg(wken_wkup, WKUP_MOD, PM_WKEN);
331 static int omap2_pm_enter(suspend_state_t state)
336 case PM_SUSPEND_STANDBY:
338 ret = omap2_pm_suspend();
347 static void omap2_pm_end(void)
349 suspend_state = PM_SUSPEND_ON;
353 static const struct platform_suspend_ops omap_pm_ops = {
354 .begin = omap2_pm_begin,
355 .enter = omap2_pm_enter,
357 .valid = suspend_valid_only_mem,
360 static const struct platform_suspend_ops __initdata omap_pm_ops;
361 #endif /* CONFIG_SUSPEND */
363 /* XXX This function should be shareable between OMAP2xxx and OMAP3 */
364 static int __init clkdms_setup(struct clockdomain *clkdm, void *unused)
366 clkdm_clear_all_wkdeps(clkdm);
367 clkdm_clear_all_sleepdeps(clkdm);
369 if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
370 omap2_clkdm_allow_idle(clkdm);
371 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
372 atomic_read(&clkdm->usecount) == 0)
373 omap2_clkdm_sleep(clkdm);
377 static void __init prcm_setup_regs(void)
379 int i, num_mem_banks;
380 struct powerdomain *pwrdm;
382 /* Enable autoidle */
383 omap2_prm_write_mod_reg(OMAP24XX_AUTOIDLE_MASK, OCP_MOD,
384 OMAP2_PRCM_SYSCONFIG_OFFSET);
387 * Set CORE powerdomain memory banks to retain their contents
390 num_mem_banks = pwrdm_get_mem_bank_count(core_pwrdm);
391 for (i = 0; i < num_mem_banks; i++)
392 pwrdm_set_mem_retst(core_pwrdm, i, PWRDM_POWER_RET);
394 /* Set CORE powerdomain's next power state to RETENTION */
395 pwrdm_set_next_pwrst(core_pwrdm, PWRDM_POWER_RET);
398 * Set MPU powerdomain's next power state to RETENTION;
399 * preserve logic state during retention
401 pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
402 pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
404 /* Force-power down DSP, GFX powerdomains */
406 pwrdm = clkdm_get_pwrdm(dsp_clkdm);
407 pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
408 omap2_clkdm_sleep(dsp_clkdm);
410 pwrdm = clkdm_get_pwrdm(gfx_clkdm);
411 pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
412 omap2_clkdm_sleep(gfx_clkdm);
415 * Clear clockdomain wakeup dependencies and enable
416 * hardware-supervised idle for all clkdms
418 clkdm_for_each(clkdms_setup, NULL);
419 clkdm_add_wkdep(mpu_clkdm, wkup_clkdm);
421 /* Enable clock autoidle for all domains */
422 omap2_cm_write_mod_reg(OMAP24XX_AUTO_CAM_MASK |
423 OMAP24XX_AUTO_MAILBOXES_MASK |
424 OMAP24XX_AUTO_WDT4_MASK |
425 OMAP2420_AUTO_WDT3_MASK |
426 OMAP24XX_AUTO_MSPRO_MASK |
427 OMAP2420_AUTO_MMC_MASK |
428 OMAP24XX_AUTO_FAC_MASK |
429 OMAP2420_AUTO_EAC_MASK |
430 OMAP24XX_AUTO_HDQ_MASK |
431 OMAP24XX_AUTO_UART2_MASK |
432 OMAP24XX_AUTO_UART1_MASK |
433 OMAP24XX_AUTO_I2C2_MASK |
434 OMAP24XX_AUTO_I2C1_MASK |
435 OMAP24XX_AUTO_MCSPI2_MASK |
436 OMAP24XX_AUTO_MCSPI1_MASK |
437 OMAP24XX_AUTO_MCBSP2_MASK |
438 OMAP24XX_AUTO_MCBSP1_MASK |
439 OMAP24XX_AUTO_GPT12_MASK |
440 OMAP24XX_AUTO_GPT11_MASK |
441 OMAP24XX_AUTO_GPT10_MASK |
442 OMAP24XX_AUTO_GPT9_MASK |
443 OMAP24XX_AUTO_GPT8_MASK |
444 OMAP24XX_AUTO_GPT7_MASK |
445 OMAP24XX_AUTO_GPT6_MASK |
446 OMAP24XX_AUTO_GPT5_MASK |
447 OMAP24XX_AUTO_GPT4_MASK |
448 OMAP24XX_AUTO_GPT3_MASK |
449 OMAP24XX_AUTO_GPT2_MASK |
450 OMAP2420_AUTO_VLYNQ_MASK |
451 OMAP24XX_AUTO_DSS_MASK,
452 CORE_MOD, CM_AUTOIDLE1);
453 omap2_cm_write_mod_reg(OMAP24XX_AUTO_UART3_MASK |
454 OMAP24XX_AUTO_SSI_MASK |
455 OMAP24XX_AUTO_USB_MASK,
456 CORE_MOD, CM_AUTOIDLE2);
457 omap2_cm_write_mod_reg(OMAP24XX_AUTO_SDRC_MASK |
458 OMAP24XX_AUTO_GPMC_MASK |
459 OMAP24XX_AUTO_SDMA_MASK,
460 CORE_MOD, CM_AUTOIDLE3);
461 omap2_cm_write_mod_reg(OMAP24XX_AUTO_PKA_MASK |
462 OMAP24XX_AUTO_AES_MASK |
463 OMAP24XX_AUTO_RNG_MASK |
464 OMAP24XX_AUTO_SHA_MASK |
465 OMAP24XX_AUTO_DES_MASK,
466 CORE_MOD, OMAP24XX_CM_AUTOIDLE4);
468 omap2_cm_write_mod_reg(OMAP2420_AUTO_DSP_IPI_MASK, OMAP24XX_DSP_MOD,
471 /* Put DPLL and both APLLs into autoidle mode */
472 omap2_cm_write_mod_reg((0x03 << OMAP24XX_AUTO_DPLL_SHIFT) |
473 (0x03 << OMAP24XX_AUTO_96M_SHIFT) |
474 (0x03 << OMAP24XX_AUTO_54M_SHIFT),
475 PLL_MOD, CM_AUTOIDLE);
477 omap2_cm_write_mod_reg(OMAP24XX_AUTO_OMAPCTRL_MASK |
478 OMAP24XX_AUTO_WDT1_MASK |
479 OMAP24XX_AUTO_MPU_WDT_MASK |
480 OMAP24XX_AUTO_GPIOS_MASK |
481 OMAP24XX_AUTO_32KSYNC_MASK |
482 OMAP24XX_AUTO_GPT1_MASK,
483 WKUP_MOD, CM_AUTOIDLE);
485 /* REVISIT: Configure number of 32 kHz clock cycles for sys_clk
487 omap2_prm_write_mod_reg(15 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
488 OMAP2_PRCM_CLKSSETUP_OFFSET);
490 /* Configure automatic voltage transition */
491 omap2_prm_write_mod_reg(2 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
492 OMAP2_PRCM_VOLTSETUP_OFFSET);
493 omap2_prm_write_mod_reg(OMAP24XX_AUTO_EXTVOLT_MASK |
494 (0x1 << OMAP24XX_SETOFF_LEVEL_SHIFT) |
495 OMAP24XX_MEMRETCTRL_MASK |
496 (0x1 << OMAP24XX_SETRET_LEVEL_SHIFT) |
497 (0x0 << OMAP24XX_VOLT_LEVEL_SHIFT),
498 OMAP24XX_GR_MOD, OMAP2_PRCM_VOLTCTRL_OFFSET);
500 /* Enable wake-up events */
501 omap2_prm_write_mod_reg(OMAP24XX_EN_GPIOS_MASK | OMAP24XX_EN_GPT1_MASK,
505 static int __init omap2_pm_init(void)
509 if (!cpu_is_omap24xx())
512 printk(KERN_INFO "Power Management for OMAP2 initializing\n");
513 l = omap2_prm_read_mod_reg(OCP_MOD, OMAP2_PRCM_REVISION_OFFSET);
514 printk(KERN_INFO "PRCM revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
516 /* Look up important powerdomains */
518 mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
520 pr_err("PM: mpu_pwrdm not found\n");
522 core_pwrdm = pwrdm_lookup("core_pwrdm");
524 pr_err("PM: core_pwrdm not found\n");
526 /* Look up important clockdomains */
528 mpu_clkdm = clkdm_lookup("mpu_clkdm");
530 pr_err("PM: mpu_clkdm not found\n");
532 wkup_clkdm = clkdm_lookup("wkup_clkdm");
534 pr_err("PM: wkup_clkdm not found\n");
536 dsp_clkdm = clkdm_lookup("dsp_clkdm");
538 pr_err("PM: dsp_clkdm not found\n");
540 gfx_clkdm = clkdm_lookup("gfx_clkdm");
542 pr_err("PM: gfx_clkdm not found\n");
545 osc_ck = clk_get(NULL, "osc_ck");
546 if (IS_ERR(osc_ck)) {
547 printk(KERN_ERR "could not get osc_ck\n");
551 if (cpu_is_omap242x()) {
552 emul_ck = clk_get(NULL, "emul_ck");
553 if (IS_ERR(emul_ck)) {
554 printk(KERN_ERR "could not get emul_ck\n");
562 /* Hack to prevent MPU retention when STI console is enabled. */
564 const struct omap_sti_console_config *sti;
566 sti = omap_get_config(OMAP_TAG_STI_CONSOLE,
567 struct omap_sti_console_config);
568 if (sti != NULL && sti->enable)
569 sti_console_enabled = 1;
573 * We copy the assembler sleep/wakeup routines to SRAM.
574 * These routines need to be in SRAM as that's the only
575 * memory the MPU can see when it wakes up.
577 if (cpu_is_omap24xx()) {
578 omap2_sram_idle = omap_sram_push(omap24xx_idle_loop_suspend,
579 omap24xx_idle_loop_suspend_sz);
581 omap2_sram_suspend = omap_sram_push(omap24xx_cpu_suspend,
582 omap24xx_cpu_suspend_sz);
585 suspend_set_ops(&omap_pm_ops);
586 pm_idle = omap2_pm_idle;
591 late_initcall(omap2_pm_init);