]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/pm24xx.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / pm24xx.c
1 /*
2  * linux/arch/arm/mach-omap2/pm.c
3  *
4  * OMAP2 Power Management Routines
5  *
6  * Copyright (C) 2005 Texas Instruments, Inc.
7  * Copyright (C) 2006-2008 Nokia Corporation
8  *
9  * Written by:
10  * Richard Woodruff <r-woodruff2@ti.com>
11  * Tony Lindgren
12  * Juha Yrjola
13  * Amit Kucheria <amit.kucheria@nokia.com>
14  * Igor Stoppa <igor.stoppa@nokia.com>
15  *
16  * Based on pm.c for omap1
17  *
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.
21  */
22
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>
31 #include <linux/io.h>
32 #include <linux/irq.h>
33
34 #include <asm/mach/time.h>
35 #include <asm/mach/irq.h>
36 #include <asm/mach-types.h>
37
38 #include <mach/irqs.h>
39 #include <mach/clock.h>
40 #include <mach/sram.h>
41 #include <mach/control.h>
42 #include <mach/gpio.h>
43 #include <mach/pm.h>
44 #include <mach/mux.h>
45 #include <mach/dma.h>
46 #include <mach/board.h>
47
48 #include "prm.h"
49 #include "prm-regbits-24xx.h"
50 #include "cm.h"
51 #include "cm-regbits-24xx.h"
52 #include "sdrc.h"
53 #include "pm.h"
54
55 #include <mach/powerdomain.h>
56 #include <mach/clockdomain.h>
57
58 static void (*omap2_sram_idle)(void);
59 static void (*omap2_sram_suspend)(void __iomem *dllctrl);
60 static void (*saved_idle)(void);
61
62 static struct powerdomain *mpu_pwrdm;
63 static struct powerdomain *core_pwrdm;
64
65 static struct clockdomain *dsp_clkdm;
66 static struct clockdomain *gfx_clkdm;
67
68 static struct clk *osc_ck, *emul_ck;
69
70 static int omap2_fclks_active(void)
71 {
72         u32 f1, f2;
73
74         f1 = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
75         f2 = cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
76         serial_console_fclk_mask(&f1, &f2);
77         if (f1 | f2)
78                 return 1;
79         return 0;
80 }
81
82 static void omap2_enter_full_retention(void)
83 {
84         u32 l, sleep_time = 0;
85
86         /* There is 1 reference hold for all children of the oscillator
87          * clock, the following will remove it. If no one else uses the
88          * oscillator itself it will be disabled if/when we enter retention
89          * mode.
90          */
91         clk_disable(osc_ck);
92
93         /* Clear old wake-up events */
94         /* REVISIT: These write to reserved bits? */
95         prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
96         prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
97         prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
98
99         /*
100          * Set MPU powerdomain's next power state to RETENTION;
101          * preserve logic state during retention
102          */
103         pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
104         pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
105
106         /* Workaround to kill USB */
107         l = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0) | OMAP24XX_USBSTANDBYCTRL;
108         omap_ctrl_writel(l, OMAP2_CONTROL_DEVCONF0);
109
110         omap2_gpio_prepare_for_retention();
111
112         if (omap2_pm_debug) {
113                 omap2_pm_dump(0, 0, 0);
114                 sleep_time = omap2_read_32k_sync_counter();
115         }
116
117         /* One last check for pending IRQs to avoid extra latency due
118          * to sleeping unnecessarily. */
119         if (omap_irq_pending())
120                 goto no_sleep;
121
122         serial_console_sleep(1);
123         /* Jump to SRAM suspend code */
124         omap2_sram_suspend(OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL));
125 no_sleep:
126         serial_console_sleep(0);
127
128         if (omap2_pm_debug) {
129                 unsigned long long tmp;
130                 u32 resume_time;
131
132                 resume_time = omap2_read_32k_sync_counter();
133                 tmp = resume_time - sleep_time;
134                 tmp *= 1000000;
135                 omap2_pm_dump(0, 1, tmp / 32768);
136         }
137         omap2_gpio_resume_after_retention();
138
139         clk_enable(osc_ck);
140
141         /* clear CORE wake-up events */
142         prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
143         prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
144
145         /* wakeup domain events - bit 1: GPT1, bit5 GPIO */
146         prm_clear_mod_reg_bits(0x4 | 0x1, WKUP_MOD, PM_WKST);
147
148         /* MPU domain wake events */
149         l = prm_read_mod_reg(OCP_MOD, OMAP2_PRM_IRQSTATUS_MPU_OFFSET);
150         if (l & 0x01)
151                 prm_write_mod_reg(0x01, OCP_MOD,
152                                 OMAP2_PRM_IRQSTATUS_MPU_OFFSET);
153         if (l & 0x20)
154                 prm_write_mod_reg(0x20, OCP_MOD,
155                                 OMAP2_PRM_IRQSTATUS_MPU_OFFSET);
156
157         /* Mask future PRCM-to-MPU interrupts */
158         prm_write_mod_reg(0x0, OCP_MOD, OMAP2_PRM_IRQSTATUS_MPU_OFFSET);
159 }
160
161 static int omap2_i2c_active(void)
162 {
163         u32 l;
164
165         l = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
166         return l & (OMAP2420_EN_I2C2 | OMAP2420_EN_I2C1);
167 }
168
169 static int sti_console_enabled;
170
171 static int omap2_allow_mpu_retention(void)
172 {
173         u32 l;
174
175         if (atomic_read(&sleep_block))
176                 return 0;
177
178         /* Check for MMC, UART2, UART1, McSPI2, McSPI1 and DSS1. */
179         l = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
180         if (l & (OMAP2420_EN_MMC | OMAP24XX_EN_UART2 |
181                  OMAP24XX_EN_UART1 | OMAP24XX_EN_MCSPI2 |
182                  OMAP24XX_EN_MCSPI1 | OMAP24XX_EN_DSS1))
183                 return 0;
184         /* Check for UART3. */
185         l = cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
186         if (l & OMAP24XX_EN_UART3)
187                 return 0;
188         if (sti_console_enabled)
189                 return 0;
190
191         return 1;
192 }
193
194 static void omap2_enter_mpu_retention(void)
195 {
196         u32 sleep_time = 0;
197         int only_idle = 0;
198
199         /* Putting MPU into the WFI state while a transfer is active
200          * seems to cause the I2C block to timeout. Why? Good question. */
201         if (omap2_i2c_active())
202                 return;
203
204         /* The peripherals seem not to be able to wake up the MPU when
205          * it is in retention mode. */
206         if (omap2_allow_mpu_retention()) {
207                 /* REVISIT: These write to reserved bits? */
208                 prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
209                 prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
210                 prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
211
212                 /* Try to enter MPU retention */
213                 prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) |
214                                   OMAP_LOGICRETSTATE,
215                                   MPU_MOD, PM_PWSTCTRL);
216         } else {
217                 /* Block MPU retention */
218
219                 prm_write_mod_reg(OMAP_LOGICRETSTATE, MPU_MOD, PM_PWSTCTRL);
220                 only_idle = 1;
221         }
222
223         if (omap2_pm_debug) {
224                 omap2_pm_dump(only_idle ? 2 : 1, 0, 0);
225                 sleep_time = omap2_read_32k_sync_counter();
226         }
227
228         omap2_sram_idle();
229
230         if (omap2_pm_debug) {
231                 unsigned long long tmp;
232                 u32 resume_time;
233
234                 resume_time = omap2_read_32k_sync_counter();
235                 tmp = resume_time - sleep_time;
236                 tmp *= 1000000;
237                 omap2_pm_dump(only_idle ? 2 : 1, 1, tmp / 32768);
238         }
239 }
240
241 static int omap2_can_sleep(void)
242 {
243         if (!enable_dyn_sleep)
244                 return 0;
245         if (omap2_fclks_active())
246                 return 0;
247         if (atomic_read(&sleep_block) > 0)
248                 return 0;
249         if (clk_get_usecount(osc_ck) > 1)
250                 return 0;
251         if (omap_dma_running())
252                 return 0;
253
254         return 1;
255 }
256
257 /*
258  * Note that you can use clock_event_device->min_delta_ns if you want to
259  * avoid reprogramming timer too often when using CONFIG_NO_HZ.
260  */
261 static void omap2_pm_idle(void)
262 {
263         local_irq_disable();
264         local_fiq_disable();
265
266         if (!omap2_can_sleep()) {
267                 if (!atomic_read(&sleep_block) && omap_irq_pending())
268                         goto out;
269                 omap2_enter_mpu_retention();
270                 goto out;
271         }
272
273         if (omap_irq_pending())
274                 goto out;
275
276         omap2_enter_full_retention();
277
278 out:
279         local_fiq_enable();
280         local_irq_enable();
281 }
282
283 static int omap2_pm_prepare(void)
284 {
285         /* We cannot sleep in idle until we have resumed */
286         saved_idle = pm_idle;
287         pm_idle = NULL;
288
289         return 0;
290 }
291
292 static int omap2_pm_suspend(void)
293 {
294         u32 wken_wkup, mir1;
295
296         wken_wkup = prm_read_mod_reg(WKUP_MOD, PM_WKEN);
297         prm_write_mod_reg(wken_wkup & ~OMAP24XX_EN_GPT1, WKUP_MOD, PM_WKEN);
298
299         /* Mask GPT1 */
300         mir1 = omap_readl(0x480fe0a4);
301         omap_writel(1 << 5, 0x480fe0ac);
302
303         omap2_enter_full_retention();
304
305         omap_writel(mir1, 0x480fe0a4);
306         prm_write_mod_reg(wken_wkup, WKUP_MOD, PM_WKEN);
307
308         return 0;
309 }
310
311 static int omap2_pm_enter(suspend_state_t state)
312 {
313         int ret = 0;
314
315         switch (state) {
316         case PM_SUSPEND_STANDBY:
317         case PM_SUSPEND_MEM:
318                 ret = omap2_pm_suspend();
319                 break;
320         default:
321                 ret = -EINVAL;
322         }
323
324         return ret;
325 }
326
327 static void omap2_pm_finish(void)
328 {
329         pm_idle = saved_idle;
330 }
331
332 static struct platform_suspend_ops omap_pm_ops = {
333         .prepare        = omap2_pm_prepare,
334         .enter          = omap2_pm_enter,
335         .finish         = omap2_pm_finish,
336         .valid          = suspend_valid_only_mem,
337 };
338
339 static int _pm_clkdm_enable_hwsup(struct clockdomain *clkdm)
340 {
341         omap2_clkdm_allow_idle(clkdm);
342         return 0;
343 }
344
345 static void __init prcm_setup_regs(void)
346 {
347         int i, num_mem_banks;
348         struct powerdomain *pwrdm;
349
350         /* Enable autoidle */
351         prm_write_mod_reg(OMAP24XX_AUTOIDLE, OCP_MOD,
352                                 OMAP24XX_PRM_SYSCONFIG_OFFSET);
353
354         /* Set all domain wakeup dependencies */
355         prm_write_mod_reg(OMAP_EN_WKUP_MASK, MPU_MOD, PM_WKDEP);
356         prm_write_mod_reg(0, OMAP24XX_DSP_MOD, PM_WKDEP);
357         prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
358         prm_write_mod_reg(0, CORE_MOD, PM_WKDEP);
359         if (cpu_is_omap2430())
360                 prm_write_mod_reg(0, OMAP2430_MDM_MOD, PM_WKDEP);
361
362         /*
363          * Set CORE powerdomain memory banks to retain their contents
364          * during RETENTION
365          */
366         num_mem_banks = pwrdm_get_mem_bank_count(core_pwrdm);
367         for (i = 0; i < num_mem_banks; i++)
368                 pwrdm_set_mem_retst(core_pwrdm, i, PWRDM_POWER_RET);
369
370         /* Set CORE powerdomain's next power state to RETENTION */
371         pwrdm_set_next_pwrst(core_pwrdm, PWRDM_POWER_RET);
372
373         /*
374          * Set MPU powerdomain's next power state to RETENTION;
375          * preserve logic state during retention
376          */
377         pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
378         pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
379
380         /* Force-power down DSP, GFX powerdomains */
381
382         pwrdm = clkdm_get_pwrdm(dsp_clkdm);
383         pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
384         omap2_clkdm_sleep(dsp_clkdm);
385
386         pwrdm = clkdm_get_pwrdm(gfx_clkdm);
387         pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
388         omap2_clkdm_sleep(gfx_clkdm);
389
390         /* Enable clockdomain hardware-supervised control for all clkdms */
391         clkdm_for_each(_pm_clkdm_enable_hwsup);
392
393         /* Enable clock autoidle for all domains */
394         cm_write_mod_reg(OMAP24XX_AUTO_CAM |
395                          OMAP24XX_AUTO_MAILBOXES |
396                          OMAP24XX_AUTO_WDT4 |
397                          OMAP2420_AUTO_WDT3 |
398                          OMAP24XX_AUTO_MSPRO |
399                          OMAP2420_AUTO_MMC |
400                          OMAP24XX_AUTO_FAC |
401                          OMAP2420_AUTO_EAC |
402                          OMAP24XX_AUTO_HDQ |
403                          OMAP24XX_AUTO_UART2 |
404                          OMAP24XX_AUTO_UART1 |
405                          OMAP24XX_AUTO_I2C2 |
406                          OMAP24XX_AUTO_I2C1 |
407                          OMAP24XX_AUTO_MCSPI2 |
408                          OMAP24XX_AUTO_MCSPI1 |
409                          OMAP24XX_AUTO_MCBSP2 |
410                          OMAP24XX_AUTO_MCBSP1 |
411                          OMAP24XX_AUTO_GPT12 |
412                          OMAP24XX_AUTO_GPT11 |
413                          OMAP24XX_AUTO_GPT10 |
414                          OMAP24XX_AUTO_GPT9 |
415                          OMAP24XX_AUTO_GPT8 |
416                          OMAP24XX_AUTO_GPT7 |
417                          OMAP24XX_AUTO_GPT6 |
418                          OMAP24XX_AUTO_GPT5 |
419                          OMAP24XX_AUTO_GPT4 |
420                          OMAP24XX_AUTO_GPT3 |
421                          OMAP24XX_AUTO_GPT2 |
422                          OMAP2420_AUTO_VLYNQ |
423                          OMAP24XX_AUTO_DSS,
424                          CORE_MOD, CM_AUTOIDLE1);
425         cm_write_mod_reg(OMAP24XX_AUTO_UART3 |
426                          OMAP24XX_AUTO_SSI |
427                          OMAP24XX_AUTO_USB,
428                          CORE_MOD, CM_AUTOIDLE2);
429         cm_write_mod_reg(OMAP24XX_AUTO_SDRC |
430                          OMAP24XX_AUTO_GPMC |
431                          OMAP24XX_AUTO_SDMA,
432                          CORE_MOD, CM_AUTOIDLE3);
433         cm_write_mod_reg(OMAP24XX_AUTO_PKA |
434                          OMAP24XX_AUTO_AES |
435                          OMAP24XX_AUTO_RNG |
436                          OMAP24XX_AUTO_SHA |
437                          OMAP24XX_AUTO_DES,
438                          CORE_MOD, OMAP24XX_CM_AUTOIDLE4);
439
440         cm_write_mod_reg(OMAP2420_AUTO_DSP_IPI, OMAP24XX_DSP_MOD, CM_AUTOIDLE);
441
442         /* Put DPLL and both APLLs into autoidle mode */
443         cm_write_mod_reg((0x03 << OMAP24XX_AUTO_DPLL_SHIFT) |
444                          (0x03 << OMAP24XX_AUTO_96M_SHIFT) |
445                          (0x03 << OMAP24XX_AUTO_54M_SHIFT),
446                          PLL_MOD, CM_AUTOIDLE);
447
448         cm_write_mod_reg(OMAP24XX_AUTO_OMAPCTRL |
449                          OMAP24XX_AUTO_WDT1 |
450                          OMAP24XX_AUTO_MPU_WDT |
451                          OMAP24XX_AUTO_GPIOS |
452                          OMAP24XX_AUTO_32KSYNC |
453                          OMAP24XX_AUTO_GPT1,
454                          WKUP_MOD, CM_AUTOIDLE);
455
456         /* REVISIT: Configure number of 32 kHz clock cycles for sys_clk
457          * stabilisation */
458         prm_write_mod_reg(15 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
459                                         OMAP24XX_PRCM_CLKSSETUP_OFFSET);
460
461         /* Configure automatic voltage transition */
462         prm_write_mod_reg(2 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
463                                         OMAP24XX_PRCM_VOLTSETUP_OFFSET);
464         prm_write_mod_reg(OMAP24XX_AUTO_EXTVOLT |
465                       (0x1 << OMAP24XX_SETOFF_LEVEL_SHIFT) |
466                       OMAP24XX_MEMRETCTRL |
467                       (0x1 << OMAP24XX_SETRET_LEVEL_SHIFT) |
468                       (0x0 << OMAP24XX_VOLT_LEVEL_SHIFT),
469                       OMAP24XX_GR_MOD, OMAP24XX_PRCM_VOLTCTRL_OFFSET);
470
471         /* Enable wake-up events */
472         prm_write_mod_reg(OMAP24XX_EN_GPIOS | OMAP24XX_EN_GPT1,
473                           WKUP_MOD, PM_WKEN);
474 }
475
476 int __init omap2_pm_init(void)
477 {
478         u32 l;
479
480         printk(KERN_INFO "Power Management for OMAP2 initializing\n");
481         l = prm_read_mod_reg(OCP_MOD, OMAP24XX_PRM_REVISION_OFFSET);
482         printk(KERN_INFO "PRCM revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
483
484         /* Look up important powerdomains, clockdomains */
485
486         mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
487         if (!mpu_pwrdm)
488                 pr_err("PM: mpu_pwrdm not found\n");
489
490         core_pwrdm = pwrdm_lookup("core_pwrdm");
491         if (!core_pwrdm)
492                 pr_err("PM: core_pwrdm not found\n");
493
494         dsp_clkdm = clkdm_lookup("dsp_clkdm");
495         if (!dsp_clkdm)
496                 pr_err("PM: mpu_clkdm not found\n");
497
498         gfx_clkdm = clkdm_lookup("gfx_clkdm");
499         if (!gfx_clkdm)
500                 pr_err("PM: gfx_clkdm not found\n");
501
502
503         osc_ck = clk_get(NULL, "osc_ck");
504         if (IS_ERR(osc_ck)) {
505                 printk(KERN_ERR "could not get osc_ck\n");
506                 return -ENODEV;
507         }
508
509         if (cpu_is_omap242x()) {
510                 emul_ck = clk_get(NULL, "emul_ck");
511                 if (IS_ERR(emul_ck)) {
512                         printk(KERN_ERR "could not get emul_ck\n");
513                         clk_put(osc_ck);
514                         return -ENODEV;
515                 }
516         }
517
518         prcm_setup_regs();
519
520         pm_init_serial_console();
521
522         /* Hack to prevent MPU retention when STI console is enabled. */
523         {
524                 const struct omap_sti_console_config *sti;
525
526                 sti = omap_get_config(OMAP_TAG_STI_CONSOLE,
527                                       struct omap_sti_console_config);
528                 if (sti != NULL && sti->enable)
529                         sti_console_enabled = 1;
530         }
531
532         /*
533          * We copy the assembler sleep/wakeup routines to SRAM.
534          * These routines need to be in SRAM as that's the only
535          * memory the MPU can see when it wakes up.
536          */
537         if (cpu_is_omap242x()) {
538                 omap2_sram_idle = omap_sram_push(omap242x_idle_loop_suspend,
539                                                  omap242x_idle_loop_suspend_sz);
540
541                 omap2_sram_suspend = omap_sram_push(omap242x_cpu_suspend,
542                                                     omap242x_cpu_suspend_sz);
543         } else {
544                 omap2_sram_idle = omap_sram_push(omap243x_idle_loop_suspend,
545                                                  omap243x_idle_loop_suspend_sz);
546
547                 omap2_sram_suspend = omap_sram_push(omap243x_cpu_suspend,
548                                                     omap243x_cpu_suspend_sz);
549         }
550
551         suspend_set_ops(&omap_pm_ops);
552         pm_idle = omap2_pm_idle;
553
554         return 0;
555 }