]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - arch/arm/mach-omap2/pm24xx.c
OMAP3: PM: UART: disable clocks when idle
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / pm24xx.c
index 593f62947f64d32d75bf438e1afa4ff291b08f69..90feb35937da62e7d486c6871b91d1d4296294ee 100644 (file)
 #include <linux/clk.h>
 #include <linux/io.h>
 #include <linux/irq.h>
+#include <linux/time.h>
 
-#include <asm/atomic.h>
 #include <asm/mach/time.h>
 #include <asm/mach/irq.h>
 #include <asm/mach-types.h>
 
-#include <asm/arch/irqs.h>
-#include <asm/arch/clock.h>
-#include <asm/arch/sram.h>
-#include <asm/arch/control.h>
-#include <asm/arch/gpio.h>
-#include <asm/arch/pm.h>
-#include <asm/arch/mux.h>
-#include <asm/arch/dma.h>
-#include <asm/arch/board.h>
+#include <mach/irqs.h>
+#include <mach/clock.h>
+#include <mach/sram.h>
+#include <mach/control.h>
+#include <mach/gpio.h>
+#include <mach/pm.h>
+#include <mach/mux.h>
+#include <mach/dma.h>
+#include <mach/board.h>
 
 #include "prm.h"
 #include "prm-regbits-24xx.h"
 #include "sdrc.h"
 #include "pm.h"
 
-/* These addrs are in assembly language code to be patched at runtime */
-extern void *omap2_ocs_sdrc_power;
-extern void *omap2_ocs_sdrc_dlla_ctrl;
+#include <mach/powerdomain.h>
+#include <mach/clockdomain.h>
 
 static void (*omap2_sram_idle)(void);
-static void (*omap2_sram_suspend)(void __iomem *dllctrl);
+static void (*omap2_sram_suspend)(u32 dllctrl, void __iomem *sdrc_dlla_ctrl,
+                                       void __iomem *sdrc_power);
 static void (*saved_idle)(void);
 
-static unsigned short enable_dyn_sleep = 0; /* disabled till drivers are fixed */
+static struct powerdomain *mpu_pwrdm;
+static struct powerdomain *core_pwrdm;
 
-static ssize_t idle_show(struct kobject *kobj, struct kobj_attribute *attr,
-                        char *buf)
-{
-       return sprintf(buf, "%hu\n", enable_dyn_sleep);
-}
-
-static ssize_t idle_store(struct kobject *kobj, struct kobj_attribute *attr,
-                         const char *buf, size_t n)
-{
-       unsigned short value;
-       if (sscanf(buf, "%hu", &value) != 1 ||
-           (value != 0 && value != 1)) {
-               printk(KERN_ERR "idle_sleep_store: Invalid value\n");
-               return -EINVAL;
-       }
-       enable_dyn_sleep = value;
-       return n;
-}
-
-static struct kobj_attribute sleep_while_idle_attr =
-       __ATTR(sleep_while_idle, 0644, idle_show, idle_store);
+static struct clockdomain *dsp_clkdm;
+static struct clockdomain *gfx_clkdm;
 
 static struct clk *osc_ck, *emul_ck;
 
@@ -93,43 +75,20 @@ static int omap2_fclks_active(void)
 
        f1 = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
        f2 = cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
-       serial_console_fclk_mask(&f1, &f2);
-       if (f1 | f2)
-               return 1;
-       return 0;
-}
 
-static int omap2_irq_pending(void)
-{
-       u32 pending_reg = 0x480fe098;
-       int i;
+       /* Ignore UART clocks.  These are handled by UART core (serial.c) */
+       f1 &= ~(OMAP24XX_EN_UART1 | OMAP24XX_EN_UART2);
+       f2 &= ~OMAP24XX_EN_UART3;
 
-       for (i = 0; i < 4; i++) {
-               if (omap_readl(pending_reg))
-                       return 1;
-               pending_reg += 0x20;
-       }
+       if (f1 | f2)
+               return 1;
        return 0;
 }
 
-static atomic_t sleep_block = ATOMIC_INIT(0);
-
-void omap2_block_sleep(void)
-{
-       atomic_inc(&sleep_block);
-}
-
-void omap2_allow_sleep(void)
-{
-       int i;
-
-       i = atomic_dec_return(&sleep_block);
-       BUG_ON(i < 0);
-}
-
 static void omap2_enter_full_retention(void)
 {
-       u32 l, sleep_time = 0;
+       u32 l;
+       struct timespec ts_preidle, ts_postidle, ts_idle;
 
        /* There is 1 reference hold for all children of the oscillator
         * clock, the following will remove it. If no one else uses the
@@ -144,9 +103,12 @@ static void omap2_enter_full_retention(void)
        prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
        prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
 
-       /* Try to enter retention */
-       prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) | OMAP_LOGICRETSTATE,
-                         MPU_MOD, PM_PWSTCTRL);
+       /*
+        * Set MPU powerdomain's next power state to RETENTION;
+        * preserve logic state during retention
+        */
+       pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
+       pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
 
        /* Workaround to kill USB */
        l = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0) | OMAP24XX_USBSTANDBYCTRL;
@@ -156,28 +118,34 @@ static void omap2_enter_full_retention(void)
 
        if (omap2_pm_debug) {
                omap2_pm_dump(0, 0, 0);
-               sleep_time = omap2_read_32k_sync_counter();
+               getnstimeofday(&ts_preidle);
        }
 
        /* One last check for pending IRQs to avoid extra latency due
         * to sleeping unnecessarily. */
-       if (omap2_irq_pending())
+       if (omap_irq_pending())
                goto no_sleep;
 
-       serial_console_sleep(1);
+       omap_uart_prepare_idle(0);
+       omap_uart_prepare_idle(1);
+       omap_uart_prepare_idle(2);
+
        /* Jump to SRAM suspend code */
-       omap2_sram_suspend(OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL));
+       omap2_sram_suspend(sdrc_read_reg(SDRC_DLLA_CTRL),
+                               OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL),
+                               OMAP_SDRC_REGADDR(SDRC_POWER));
 no_sleep:
-       serial_console_sleep(0);
+       omap_uart_resume_idle(2);
+       omap_uart_resume_idle(1);
+       omap_uart_resume_idle(0);
 
        if (omap2_pm_debug) {
                unsigned long long tmp;
-               u32 resume_time;
 
-               resume_time = omap2_read_32k_sync_counter();
-               tmp = resume_time - sleep_time;
-               tmp *= 1000000;
-               omap2_pm_dump(0, 1, tmp / 32768);
+               getnstimeofday(&ts_postidle);
+               ts_idle = timespec_sub(ts_postidle, ts_preidle);
+               tmp = timespec_to_ns(&ts_idle) * NSEC_PER_USEC;
+               omap2_pm_dump(0, 1, tmp);
        }
        omap2_gpio_resume_after_retention();
 
@@ -191,14 +159,16 @@ no_sleep:
        prm_clear_mod_reg_bits(0x4 | 0x1, WKUP_MOD, PM_WKST);
 
        /* MPU domain wake events */
-       l = __raw_readl(OMAP24XX_PRCM_IRQSTATUS_MPU);
+       l = prm_read_mod_reg(OCP_MOD, OMAP2_PRM_IRQSTATUS_MPU_OFFSET);
        if (l & 0x01)
-               __raw_writel(0x01, OMAP24XX_PRCM_IRQSTATUS_MPU);
+               prm_write_mod_reg(0x01, OCP_MOD,
+                               OMAP2_PRM_IRQSTATUS_MPU_OFFSET);
        if (l & 0x20)
-               __raw_writel(0x20, OMAP24XX_PRCM_IRQSTATUS_MPU);
+               prm_write_mod_reg(0x20, OCP_MOD,
+                               OMAP2_PRM_IRQSTATUS_MPU_OFFSET);
 
        /* Mask future PRCM-to-MPU interrupts */
-       __raw_writel(0x0, OMAP24XX_PRCM_IRQSTATUS_MPU);
+       prm_write_mod_reg(0x0, OCP_MOD, OMAP2_PRM_IRQSTATUS_MPU_OFFSET);
 }
 
 static int omap2_i2c_active(void)
@@ -236,8 +206,8 @@ static int omap2_allow_mpu_retention(void)
 
 static void omap2_enter_mpu_retention(void)
 {
-       u32 sleep_time = 0;
        int only_idle = 0;
+       struct timespec ts_preidle, ts_postidle, ts_idle;
 
        /* Putting MPU into the WFI state while a transfer is active
         * seems to cause the I2C block to timeout. Why? Good question. */
@@ -265,19 +235,18 @@ static void omap2_enter_mpu_retention(void)
 
        if (omap2_pm_debug) {
                omap2_pm_dump(only_idle ? 2 : 1, 0, 0);
-               sleep_time = omap2_read_32k_sync_counter();
+               getnstimeofday(&ts_preidle);
        }
 
        omap2_sram_idle();
 
        if (omap2_pm_debug) {
                unsigned long long tmp;
-               u32 resume_time;
 
-               resume_time = omap2_read_32k_sync_counter();
-               tmp = resume_time - sleep_time;
-               tmp *= 1000000;
-               omap2_pm_dump(only_idle ? 2 : 1, 1, tmp / 32768);
+               getnstimeofday(&ts_postidle);
+               ts_idle = timespec_sub(ts_postidle, ts_preidle);
+               tmp = timespec_to_ns(&ts_idle) * NSEC_PER_USEC;
+               omap2_pm_dump(only_idle ? 2 : 1, 1, tmp);
        }
 }
 
@@ -289,7 +258,7 @@ static int omap2_can_sleep(void)
                return 0;
        if (atomic_read(&sleep_block) > 0)
                return 0;
-       if (clk_get_usecount(osc_ck) > 1)
+       if (osc_ck->usecount > 1)
                return 0;
        if (omap_dma_running())
                return 0;
@@ -297,36 +266,23 @@ static int omap2_can_sleep(void)
        return 1;
 }
 
+/*
+ * Note that you can use clock_event_device->min_delta_ns if you want to
+ * avoid reprogramming timer too often when using CONFIG_NO_HZ.
+ */
 static void omap2_pm_idle(void)
 {
        local_irq_disable();
        local_fiq_disable();
 
        if (!omap2_can_sleep()) {
-               /* timer_dyn_reprogram() takes about 100-200 us to complete.
-                * In some contexts (e.g. when waiting for a GPMC-SDRAM DMA
-                * transfer to complete), the increased latency is too much.
-                *
-                * omap2_block_sleep() and omap2_allow_sleep() can be used
-                * to indicate this.
-                */
-               if (atomic_read(&sleep_block) == 0) {
-                       timer_dyn_reprogram();
-                       if (omap2_irq_pending())
-                               goto out;
-               }
+               if (!atomic_read(&sleep_block) && omap_irq_pending())
+                       goto out;
                omap2_enter_mpu_retention();
                goto out;
        }
 
-       /*
-        * Since an interrupt may set up a timer, we don't want to
-        * reprogram the hardware timer with interrupts enabled.
-        * Re-enable interrupts only after returning from idle.
-        */
-       timer_dyn_reprogram();
-
-       if (omap2_irq_pending())
+       if (omap_irq_pending())
                goto out;
 
        omap2_enter_full_retention();
@@ -356,6 +312,7 @@ static int omap2_pm_suspend(void)
        mir1 = omap_readl(0x480fe0a4);
        omap_writel(1 << 5, 0x480fe0ac);
 
+       omap_uart_prepare_suspend();
        omap2_enter_full_retention();
 
        omap_writel(mir1, 0x480fe0a4);
@@ -392,12 +349,20 @@ static struct platform_suspend_ops omap_pm_ops = {
        .valid          = suspend_valid_only_mem,
 };
 
+static int _pm_clkdm_enable_hwsup(struct clockdomain *clkdm)
+{
+       omap2_clkdm_allow_idle(clkdm);
+       return 0;
+}
+
 static void __init prcm_setup_regs(void)
 {
-       u32 l;
+       int i, num_mem_banks;
+       struct powerdomain *pwrdm;
 
        /* Enable autoidle */
-       __raw_writel(OMAP24XX_AUTOIDLE, OMAP24XX_PRCM_SYSCONFIG);
+       prm_write_mod_reg(OMAP24XX_AUTOIDLE, OCP_MOD,
+                               OMAP24XX_PRM_SYSCONFIG_OFFSET);
 
        /* Set all domain wakeup dependencies */
        prm_write_mod_reg(OMAP_EN_WKUP_MASK, MPU_MOD, PM_WKDEP);
@@ -407,36 +372,36 @@ static void __init prcm_setup_regs(void)
        if (cpu_is_omap2430())
                prm_write_mod_reg(0, OMAP2430_MDM_MOD, PM_WKDEP);
 
-       l = prm_read_mod_reg(CORE_MOD, PM_PWSTCTRL);
-       /* Enable retention for all memory blocks */
-       l |= OMAP24XX_MEM3RETSTATE | OMAP24XX_MEM2RETSTATE |
-               OMAP24XX_MEM1RETSTATE;
-
-       /* Set power state to RETENTION */
-       l &= ~OMAP_POWERSTATE_MASK;
-       l |= 0x01 << OMAP_POWERSTATE_SHIFT;
-       prm_write_mod_reg(l, CORE_MOD, PM_PWSTCTRL);
-
-       prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) |
-                         OMAP_LOGICRETSTATE,
-                         MPU_MOD, PM_PWSTCTRL);
-
-       /* Power down DSP and GFX */
-       prm_write_mod_reg(OMAP24XX_FORCESTATE | (0x3 << OMAP_POWERSTATE_SHIFT),
-                         OMAP24XX_DSP_MOD, PM_PWSTCTRL);
-       prm_write_mod_reg(OMAP24XX_FORCESTATE | (0x3 << OMAP_POWERSTATE_SHIFT),
-                         GFX_MOD, PM_PWSTCTRL);
-
-       /* Enable clock auto control for all domains */
-       cm_write_mod_reg(OMAP24XX_AUTOSTATE_MPU_MASK, MPU_MOD, CM_CLKSTCTRL);
-       cm_write_mod_reg(OMAP24XX_AUTOSTATE_DSS_MASK |
-                        OMAP24XX_AUTOSTATE_L4_MASK |
-                        OMAP24XX_AUTOSTATE_L3_MASK,
-                        CORE_MOD, CM_CLKSTCTRL);
-       cm_write_mod_reg(OMAP24XX_AUTOSTATE_GFX_MASK, GFX_MOD, CM_CLKSTCTRL);
-       cm_write_mod_reg(OMAP2420_AUTOSTATE_IVA_MASK |
-                        OMAP24XX_AUTOSTATE_DSP_MASK,
-                        OMAP24XX_DSP_MOD, CM_CLKSTCTRL);
+       /*
+        * Set CORE powerdomain memory banks to retain their contents
+        * during RETENTION
+        */
+       num_mem_banks = pwrdm_get_mem_bank_count(core_pwrdm);
+       for (i = 0; i < num_mem_banks; i++)
+               pwrdm_set_mem_retst(core_pwrdm, i, PWRDM_POWER_RET);
+
+       /* Set CORE powerdomain's next power state to RETENTION */
+       pwrdm_set_next_pwrst(core_pwrdm, PWRDM_POWER_RET);
+
+       /*
+        * Set MPU powerdomain's next power state to RETENTION;
+        * preserve logic state during retention
+        */
+       pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
+       pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
+
+       /* Force-power down DSP, GFX powerdomains */
+
+       pwrdm = clkdm_get_pwrdm(dsp_clkdm);
+       pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
+       omap2_clkdm_sleep(dsp_clkdm);
+
+       pwrdm = clkdm_get_pwrdm(gfx_clkdm);
+       pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
+       omap2_clkdm_sleep(gfx_clkdm);
+
+       /* Enable clockdomain hardware-supervised control for all clkdms */
+       clkdm_for_each(_pm_clkdm_enable_hwsup);
 
        /* Enable clock autoidle for all domains */
        cm_write_mod_reg(OMAP24XX_AUTO_CAM |
@@ -503,31 +468,51 @@ static void __init prcm_setup_regs(void)
 
        /* REVISIT: Configure number of 32 kHz clock cycles for sys_clk
         * stabilisation */
-       __raw_writel(15 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_PRCM_CLKSSETUP);
+       prm_write_mod_reg(15 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
+                                       OMAP24XX_PRCM_CLKSSETUP_OFFSET);
 
        /* Configure automatic voltage transition */
-       __raw_writel(2 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_PRCM_VOLTSETUP);
-       __raw_writel(OMAP24XX_AUTO_EXTVOLT |
+       prm_write_mod_reg(2 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
+                                       OMAP24XX_PRCM_VOLTSETUP_OFFSET);
+       prm_write_mod_reg(OMAP24XX_AUTO_EXTVOLT |
                      (0x1 << OMAP24XX_SETOFF_LEVEL_SHIFT) |
                      OMAP24XX_MEMRETCTRL |
                      (0x1 << OMAP24XX_SETRET_LEVEL_SHIFT) |
                      (0x0 << OMAP24XX_VOLT_LEVEL_SHIFT),
-                     OMAP24XX_PRCM_VOLTCTRL);
+                     OMAP24XX_GR_MOD, OMAP24XX_PRCM_VOLTCTRL_OFFSET);
 
        /* Enable wake-up events */
        prm_write_mod_reg(OMAP24XX_EN_GPIOS | OMAP24XX_EN_GPT1,
                          WKUP_MOD, PM_WKEN);
 }
 
-static int __init omap2_pm_init(void)
+int __init omap2_pm_init(void)
 {
        u32 l;
-       int error;
 
        printk(KERN_INFO "Power Management for OMAP2 initializing\n");
-       l = __raw_readl(OMAP24XX_PRCM_REVISION);
+       l = prm_read_mod_reg(OCP_MOD, OMAP24XX_PRM_REVISION_OFFSET);
        printk(KERN_INFO "PRCM revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
 
+       /* Look up important powerdomains, clockdomains */
+
+       mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
+       if (!mpu_pwrdm)
+               pr_err("PM: mpu_pwrdm not found\n");
+
+       core_pwrdm = pwrdm_lookup("core_pwrdm");
+       if (!core_pwrdm)
+               pr_err("PM: core_pwrdm not found\n");
+
+       dsp_clkdm = clkdm_lookup("dsp_clkdm");
+       if (!dsp_clkdm)
+               pr_err("PM: mpu_clkdm not found\n");
+
+       gfx_clkdm = clkdm_lookup("gfx_clkdm");
+       if (!gfx_clkdm)
+               pr_err("PM: gfx_clkdm not found\n");
+
+
        osc_ck = clk_get(NULL, "osc_ck");
        if (IS_ERR(osc_ck)) {
                printk(KERN_ERR "could not get osc_ck\n");
@@ -545,8 +530,6 @@ static int __init omap2_pm_init(void)
 
        prcm_setup_regs();
 
-       pm_init_serial_console();
-
        /* Hack to prevent MPU retention when STI console is enabled. */
        {
                const struct omap_sti_console_config *sti;
@@ -562,28 +545,16 @@ static int __init omap2_pm_init(void)
         * These routines need to be in SRAM as that's the only
         * memory the MPU can see when it wakes up.
         */
-       omap2_sram_idle = omap_sram_push(omap24xx_idle_loop_suspend,
-                                        omap24xx_idle_loop_suspend_sz);
-
-       omap2_sram_suspend = omap_sram_push(omap24xx_cpu_suspend,
-                                           omap24xx_cpu_suspend_sz);
+       if (cpu_is_omap24xx()) {
+               omap2_sram_idle = omap_sram_push(omap24xx_idle_loop_suspend,
+                                                omap24xx_idle_loop_suspend_sz);
 
-       /* Patch in the correct register addresses for multiboot */
-       omap_sram_patch_va(omap24xx_cpu_suspend, &omap2_ocs_sdrc_power,
-                          omap2_sram_suspend,
-                          OMAP_SDRC_REGADDR(SDRC_POWER));
-       omap_sram_patch_va(omap24xx_cpu_suspend, &omap2_ocs_sdrc_dlla_ctrl,
-                          omap2_sram_suspend,
-                          OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL));
+               omap2_sram_suspend = omap_sram_push(omap24xx_cpu_suspend,
+                                                   omap24xx_cpu_suspend_sz);
+       }
 
        suspend_set_ops(&omap_pm_ops);
        pm_idle = omap2_pm_idle;
 
-       error = sysfs_create_file(power_kobj, &sleep_while_idle_attr.attr);
-       if (error)
-               printk(KERN_ERR "sysfs_create_file failed: %d\n", error);
-
        return 0;
 }
-
-late_initcall(omap2_pm_init);