blob: 3f1f656348fcc0313f60f2bc24ef12e81bebc9e7 [file] [log] [blame]
Kevin Hilman8bd22942009-05-28 10:56:16 -07001/*
2 * OMAP3 Power Management Routines
3 *
4 * Copyright (C) 2006-2008 Nokia Corporation
5 * Tony Lindgren <tony@atomide.com>
6 * Jouni Hogander
7 *
Rajendra Nayak2f5939c2008-09-26 17:50:07 +05308 * Copyright (C) 2007 Texas Instruments, Inc.
9 * Rajendra Nayak <rnayak@ti.com>
10 *
Kevin Hilman8bd22942009-05-28 10:56:16 -070011 * Copyright (C) 2005 Texas Instruments, Inc.
12 * Richard Woodruff <r-woodruff2@ti.com>
13 *
14 * Based on pm.c for omap1
15 *
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.
19 */
20
21#include <linux/pm.h>
22#include <linux/suspend.h>
23#include <linux/interrupt.h>
24#include <linux/module.h>
25#include <linux/list.h>
26#include <linux/err.h>
27#include <linux/gpio.h>
28
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/sram.h>
30#include <plat/clockdomain.h>
31#include <plat/powerdomain.h>
32#include <plat/control.h>
33#include <plat/serial.h>
Rajendra Nayak61255ab2008-09-26 17:49:56 +053034#include <plat/sdrc.h>
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053035#include <plat/prcm.h>
36#include <plat/gpmc.h>
Tero Kristof2d11852008-08-28 13:13:31 +000037#include <plat/dma.h>
Kevin Hilman8bd22942009-05-28 10:56:16 -070038
Rajendra Nayak57f277b2008-09-26 17:49:34 +053039#include <asm/tlbflush.h>
40
Kevin Hilman8bd22942009-05-28 10:56:16 -070041#include "cm.h"
42#include "cm-regbits-34xx.h"
43#include "prm-regbits-34xx.h"
44
45#include "prm.h"
46#include "pm.h"
Tero Kristo13a6fe0f2008-10-13 13:17:06 +030047#include "sdrc.h"
48
49#define SDRC_POWER_AUTOCOUNT_SHIFT 8
50#define SDRC_POWER_AUTOCOUNT_MASK (0xffff << SDRC_POWER_AUTOCOUNT_SHIFT)
51#define SDRC_POWER_CLKCTRL_SHIFT 4
52#define SDRC_POWER_CLKCTRL_MASK (0x3 << SDRC_POWER_CLKCTRL_SHIFT)
53#define SDRC_SELF_REFRESH_ON_AUTOCOUNT (0x2 << SDRC_POWER_CLKCTRL_SHIFT)
Kevin Hilman8bd22942009-05-28 10:56:16 -070054
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053055/* Scratchpad offsets */
56#define OMAP343X_TABLE_ADDRESS_OFFSET 0x31
57#define OMAP343X_TABLE_VALUE_OFFSET 0x30
58#define OMAP343X_CONTROL_REG_VALUE_OFFSET 0x32
59
Kevin Hilman8bd22942009-05-28 10:56:16 -070060struct power_state {
61 struct powerdomain *pwrdm;
62 u32 next_state;
Kevin Hilman10f90ed2009-06-24 11:39:18 -070063#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -070064 u32 saved_state;
Kevin Hilman10f90ed2009-06-24 11:39:18 -070065#endif
Kevin Hilman8bd22942009-05-28 10:56:16 -070066 struct list_head node;
67};
68
69static LIST_HEAD(pwrst_list);
70
71static void (*_omap_sram_idle)(u32 *addr, int save_state);
72
Tero Kristo27d59a42008-10-13 13:15:00 +030073static int (*_omap_save_secure_sram)(u32 *addr);
74
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +053075static struct powerdomain *mpu_pwrdm, *neon_pwrdm;
76static struct powerdomain *core_pwrdm, *per_pwrdm;
77
78static int set_pwrdm_state(struct powerdomain *pwrdm, u32 state);
Kevin Hilman8bd22942009-05-28 10:56:16 -070079
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053080static inline void omap3_per_save_context(void)
81{
82 omap_gpio_save_context();
83}
84
85static inline void omap3_per_restore_context(void)
86{
87 omap_gpio_restore_context();
88}
89
90static void omap3_core_save_context(void)
91{
92 u32 control_padconf_off;
93
94 /* Save the padconf registers */
95 control_padconf_off = omap_ctrl_readl(OMAP343X_CONTROL_PADCONF_OFF);
96 control_padconf_off |= START_PADCONF_SAVE;
97 omap_ctrl_writel(control_padconf_off, OMAP343X_CONTROL_PADCONF_OFF);
98 /* wait for the save to complete */
99 while (!omap_ctrl_readl(OMAP343X_CONTROL_GENERAL_PURPOSE_STATUS)
100 & PADCONF_SAVE_DONE)
101 ;
102 /* Save the Interrupt controller context */
103 omap_intc_save_context();
104 /* Save the GPMC context */
105 omap3_gpmc_save_context();
106 /* Save the system control module context, padconf already save above*/
107 omap3_control_save_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000108 omap_dma_global_context_save();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530109}
110
111static void omap3_core_restore_context(void)
112{
113 /* Restore the control module context, padconf restored by h/w */
114 omap3_control_restore_context();
115 /* Restore the GPMC context */
116 omap3_gpmc_restore_context();
117 /* Restore the interrupt controller context */
118 omap_intc_restore_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000119 omap_dma_global_context_restore();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530120}
121
Tero Kristo9d971402008-12-12 11:20:05 +0200122/*
123 * FIXME: This function should be called before entering off-mode after
124 * OMAP3 secure services have been accessed. Currently it is only called
125 * once during boot sequence, but this works as we are not using secure
126 * services.
127 */
Tero Kristo27d59a42008-10-13 13:15:00 +0300128static void omap3_save_secure_ram_context(u32 target_mpu_state)
129{
130 u32 ret;
131
132 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
133 /* Disable dma irq before calling secure rom code API */
134 omap_dma_disable_irq(0);
135 omap_dma_disable_irq(1);
136 /*
137 * MPU next state must be set to POWER_ON temporarily,
138 * otherwise the WFI executed inside the ROM code
139 * will hang the system.
140 */
141 pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_ON);
142 ret = _omap_save_secure_sram((u32 *)
143 __pa(omap3_secure_ram_storage));
144 pwrdm_set_next_pwrst(mpu_pwrdm, target_mpu_state);
145 /* Following is for error tracking, it should not happen */
146 if (ret) {
147 printk(KERN_ERR "save_secure_sram() returns %08x\n",
148 ret);
149 while (1)
150 ;
151 }
152 }
153}
154
Jon Hunter77da2d92009-06-27 00:07:25 -0500155/*
156 * PRCM Interrupt Handler Helper Function
157 *
158 * The purpose of this function is to clear any wake-up events latched
159 * in the PRCM PM_WKST_x registers. It is possible that a wake-up event
160 * may occur whilst attempting to clear a PM_WKST_x register and thus
161 * set another bit in this register. A while loop is used to ensure
162 * that any peripheral wake-up events occurring while attempting to
163 * clear the PM_WKST_x are detected and cleared.
164 */
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700165static int prcm_clear_mod_irqs(s16 module, u8 regs)
Jon Hunter77da2d92009-06-27 00:07:25 -0500166{
Vikram Pandita71a80772009-07-17 19:33:09 -0500167 u32 wkst, fclk, iclk, clken;
Jon Hunter77da2d92009-06-27 00:07:25 -0500168 u16 wkst_off = (regs == 3) ? OMAP3430ES2_PM_WKST3 : PM_WKST1;
169 u16 fclk_off = (regs == 3) ? OMAP3430ES2_CM_FCLKEN3 : CM_FCLKEN1;
170 u16 iclk_off = (regs == 3) ? CM_ICLKEN3 : CM_ICLKEN1;
Paul Walmsley5d805972009-07-22 10:18:07 -0700171 u16 grpsel_off = (regs == 3) ?
172 OMAP3430ES2_PM_MPUGRPSEL3 : OMAP3430_PM_MPUGRPSEL;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700173 int c = 0;
Jon Hunter77da2d92009-06-27 00:07:25 -0500174
175 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley5d805972009-07-22 10:18:07 -0700176 wkst &= prm_read_mod_reg(module, grpsel_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500177 if (wkst) {
178 iclk = cm_read_mod_reg(module, iclk_off);
179 fclk = cm_read_mod_reg(module, fclk_off);
180 while (wkst) {
Vikram Pandita71a80772009-07-17 19:33:09 -0500181 clken = wkst;
182 cm_set_mod_reg_bits(clken, module, iclk_off);
183 /*
184 * For USBHOST, we don't know whether HOST1 or
185 * HOST2 woke us up, so enable both f-clocks
186 */
187 if (module == OMAP3430ES2_USBHOST_MOD)
188 clken |= 1 << OMAP3430ES2_EN_USBHOST2_SHIFT;
189 cm_set_mod_reg_bits(clken, module, fclk_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500190 prm_write_mod_reg(wkst, module, wkst_off);
191 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700192 c++;
Jon Hunter77da2d92009-06-27 00:07:25 -0500193 }
194 cm_write_mod_reg(iclk, module, iclk_off);
195 cm_write_mod_reg(fclk, module, fclk_off);
196 }
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700197
198 return c;
199}
200
201static int _prcm_int_handle_wakeup(void)
202{
203 int c;
204
205 c = prcm_clear_mod_irqs(WKUP_MOD, 1);
206 c += prcm_clear_mod_irqs(CORE_MOD, 1);
207 c += prcm_clear_mod_irqs(OMAP3430_PER_MOD, 1);
208 if (omap_rev() > OMAP3430_REV_ES1_0) {
209 c += prcm_clear_mod_irqs(CORE_MOD, 3);
210 c += prcm_clear_mod_irqs(OMAP3430ES2_USBHOST_MOD, 1);
211 }
212
213 return c;
Jon Hunter77da2d92009-06-27 00:07:25 -0500214}
215
216/*
217 * PRCM Interrupt Handler
218 *
219 * The PRM_IRQSTATUS_MPU register indicates if there are any pending
220 * interrupts from the PRCM for the MPU. These bits must be cleared in
221 * order to clear the PRCM interrupt. The PRCM interrupt handler is
222 * implemented to simply clear the PRM_IRQSTATUS_MPU in order to clear
223 * the PRCM interrupt. Please note that bit 0 of the PRM_IRQSTATUS_MPU
224 * register indicates that a wake-up event is pending for the MPU and
225 * this bit can only be cleared if the all the wake-up events latched
226 * in the various PM_WKST_x registers have been cleared. The interrupt
227 * handler is implemented using a do-while loop so that if a wake-up
228 * event occurred during the processing of the prcm interrupt handler
229 * (setting a bit in the corresponding PM_WKST_x register and thus
230 * preventing us from clearing bit 0 of the PRM_IRQSTATUS_MPU register)
231 * this would be handled.
232 */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700233static irqreturn_t prcm_interrupt_handler (int irq, void *dev_id)
234{
Jon Hunter77da2d92009-06-27 00:07:25 -0500235 u32 irqstatus_mpu;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700236 int c = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700237
Jon Hunter77da2d92009-06-27 00:07:25 -0500238 do {
Jon Hunter77da2d92009-06-27 00:07:25 -0500239 irqstatus_mpu = prm_read_mod_reg(OCP_MOD,
240 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700241
242 if (irqstatus_mpu & (OMAP3430_WKUP_ST | OMAP3430_IO_ST)) {
243 c = _prcm_int_handle_wakeup();
244
245 /*
246 * Is the MPU PRCM interrupt handler racing with the
247 * IVA2 PRCM interrupt handler ?
248 */
249 WARN(c == 0, "prcm: WARNING: PRCM indicated MPU wakeup "
250 "but no wakeup sources are marked\n");
251 } else {
252 /* XXX we need to expand our PRCM interrupt handler */
253 WARN(1, "prcm: WARNING: PRCM interrupt received, but "
254 "no code to handle it (%08x)\n", irqstatus_mpu);
255 }
256
Jon Hunter77da2d92009-06-27 00:07:25 -0500257 prm_write_mod_reg(irqstatus_mpu, OCP_MOD,
258 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700259
Jon Hunter77da2d92009-06-27 00:07:25 -0500260 } while (prm_read_mod_reg(OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET));
Kevin Hilman8bd22942009-05-28 10:56:16 -0700261
262 return IRQ_HANDLED;
263}
264
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530265static void restore_control_register(u32 val)
266{
267 __asm__ __volatile__ ("mcr p15, 0, %0, c1, c0, 0" : : "r" (val));
268}
269
270/* Function to restore the table entry that was modified for enabling MMU */
271static void restore_table_entry(void)
272{
273 u32 *scratchpad_address;
274 u32 previous_value, control_reg_value;
275 u32 *address;
276
277 scratchpad_address = OMAP2_L4_IO_ADDRESS(OMAP343X_SCRATCHPAD);
278
279 /* Get address of entry that was modified */
280 address = (u32 *)__raw_readl(scratchpad_address +
281 OMAP343X_TABLE_ADDRESS_OFFSET);
282 /* Get the previous value which needs to be restored */
283 previous_value = __raw_readl(scratchpad_address +
284 OMAP343X_TABLE_VALUE_OFFSET);
285 address = __va(address);
286 *address = previous_value;
287 flush_tlb_all();
288 control_reg_value = __raw_readl(scratchpad_address
289 + OMAP343X_CONTROL_REG_VALUE_OFFSET);
290 /* This will enable caches and prediction */
291 restore_control_register(control_reg_value);
292}
293
Kevin Hilman8bd22942009-05-28 10:56:16 -0700294static void omap_sram_idle(void)
295{
296 /* Variable to tell what needs to be saved and restored
297 * in omap_sram_idle*/
298 /* save_state = 0 => Nothing to save and restored */
299 /* save_state = 1 => Only L1 and logic lost */
300 /* save_state = 2 => Only L2 lost */
301 /* save_state = 3 => L1, L2 and logic lost */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530302 int save_state = 0;
303 int mpu_next_state = PWRDM_POWER_ON;
304 int per_next_state = PWRDM_POWER_ON;
305 int core_next_state = PWRDM_POWER_ON;
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530306 int core_prev_state, per_prev_state;
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300307 u32 sdrc_pwr = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700308
309 if (!_omap_sram_idle)
310 return;
311
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530312 pwrdm_clear_all_prev_pwrst(mpu_pwrdm);
313 pwrdm_clear_all_prev_pwrst(neon_pwrdm);
314 pwrdm_clear_all_prev_pwrst(core_pwrdm);
315 pwrdm_clear_all_prev_pwrst(per_pwrdm);
316
Kevin Hilman8bd22942009-05-28 10:56:16 -0700317 mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
318 switch (mpu_next_state) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530319 case PWRDM_POWER_ON:
Kevin Hilman8bd22942009-05-28 10:56:16 -0700320 case PWRDM_POWER_RET:
321 /* No need to save context */
322 save_state = 0;
323 break;
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530324 case PWRDM_POWER_OFF:
325 save_state = 3;
326 break;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700327 default:
328 /* Invalid state */
329 printk(KERN_ERR "Invalid mpu state in sram_idle\n");
330 return;
331 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300332 pwrdm_pre_transition();
333
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530334 /* NEON control */
335 if (pwrdm_read_pwrst(neon_pwrdm) == PWRDM_POWER_ON)
336 set_pwrdm_state(neon_pwrdm, mpu_next_state);
337
338 /* CORE & PER */
339 core_next_state = pwrdm_read_next_pwrst(core_pwrdm);
340 if (core_next_state < PWRDM_POWER_ON) {
341 omap2_gpio_prepare_for_retention();
342 omap_uart_prepare_idle(0);
343 omap_uart_prepare_idle(1);
344 /* PER changes only with core */
345 per_next_state = pwrdm_read_next_pwrst(per_pwrdm);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530346 if (per_next_state < PWRDM_POWER_ON) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530347 omap_uart_prepare_idle(2);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530348 if (per_next_state == PWRDM_POWER_OFF)
349 omap3_per_save_context();
350 }
351 if (core_next_state == PWRDM_POWER_OFF) {
352 omap3_core_save_context();
353 omap3_prcm_save_context();
354 }
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530355 /* Enable IO-PAD wakeup */
356 prm_set_mod_reg_bits(OMAP3430_EN_IO, WKUP_MOD, PM_WKEN);
357 }
Kevin Hilman8bd22942009-05-28 10:56:16 -0700358
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530359 /*
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300360 * Force SDRAM controller to self-refresh mode after timeout on
361 * autocount. This is needed on ES3.0 to avoid SDRAM controller
362 * hang-ups.
363 */
364 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
365 omap_type() != OMAP2_DEVICE_TYPE_GP &&
366 core_next_state == PWRDM_POWER_OFF) {
367 sdrc_pwr = sdrc_read_reg(SDRC_POWER);
368 sdrc_write_reg((sdrc_pwr &
369 ~(SDRC_POWER_AUTOCOUNT_MASK|SDRC_POWER_CLKCTRL_MASK)) |
370 (1 << SDRC_POWER_AUTOCOUNT_SHIFT) |
371 SDRC_SELF_REFRESH_ON_AUTOCOUNT, SDRC_POWER);
372 }
373
374 /*
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530375 * omap3_arm_context is the location where ARM registers
376 * get saved. The restore path then reads from this
377 * location and restores them back.
378 */
379 _omap_sram_idle(omap3_arm_context, save_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700380 cpu_init();
381
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300382 /* Restore normal SDRAM settings */
383 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
384 omap_type() != OMAP2_DEVICE_TYPE_GP &&
385 core_next_state == PWRDM_POWER_OFF)
386 sdrc_write_reg(sdrc_pwr, SDRC_POWER);
387
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530388 /* Restore table entry modified during MMU restoration */
389 if (pwrdm_read_prev_pwrst(mpu_pwrdm) == PWRDM_POWER_OFF)
390 restore_table_entry();
391
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530392 if (core_next_state < PWRDM_POWER_ON) {
393 if (per_next_state < PWRDM_POWER_ON)
394 omap_uart_resume_idle(2);
395 omap_uart_resume_idle(1);
396 omap_uart_resume_idle(0);
397
398 /* Disable IO-PAD wakeup */
399 prm_clear_mod_reg_bits(OMAP3430_EN_IO, WKUP_MOD, PM_WKEN);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530400 core_prev_state = pwrdm_read_prev_pwrst(core_pwrdm);
401 if (core_prev_state == PWRDM_POWER_OFF) {
402 omap3_core_restore_context();
403 omap3_prcm_restore_context();
404 omap3_sram_restore_context();
405 }
406 if (per_next_state < PWRDM_POWER_ON) {
407 per_prev_state =
408 pwrdm_read_prev_pwrst(per_pwrdm);
409 if (per_prev_state == PWRDM_POWER_OFF)
410 omap3_per_restore_context();
411 }
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530412 omap2_gpio_resume_after_retention();
413 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300414
415 pwrdm_post_transition();
416
Kevin Hilman8bd22942009-05-28 10:56:16 -0700417}
418
419/*
420 * Check if functional clocks are enabled before entering
421 * sleep. This function could be behind CONFIG_PM_DEBUG
422 * when all drivers are configuring their sysconfig registers
423 * properly and using their clocks properly.
424 */
425static int omap3_fclks_active(void)
426{
427 u32 fck_core1 = 0, fck_core3 = 0, fck_sgx = 0, fck_dss = 0,
428 fck_cam = 0, fck_per = 0, fck_usbhost = 0;
429
430 fck_core1 = cm_read_mod_reg(CORE_MOD,
431 CM_FCLKEN1);
432 if (omap_rev() > OMAP3430_REV_ES1_0) {
433 fck_core3 = cm_read_mod_reg(CORE_MOD,
434 OMAP3430ES2_CM_FCLKEN3);
435 fck_sgx = cm_read_mod_reg(OMAP3430ES2_SGX_MOD,
436 CM_FCLKEN);
437 fck_usbhost = cm_read_mod_reg(OMAP3430ES2_USBHOST_MOD,
438 CM_FCLKEN);
439 } else
440 fck_sgx = cm_read_mod_reg(GFX_MOD,
441 OMAP3430ES2_CM_FCLKEN3);
442 fck_dss = cm_read_mod_reg(OMAP3430_DSS_MOD,
443 CM_FCLKEN);
444 fck_cam = cm_read_mod_reg(OMAP3430_CAM_MOD,
445 CM_FCLKEN);
446 fck_per = cm_read_mod_reg(OMAP3430_PER_MOD,
447 CM_FCLKEN);
Kevin Hilman4af40162009-02-04 10:51:40 -0800448
449 /* Ignore UART clocks. These are handled by UART core (serial.c) */
450 fck_core1 &= ~(OMAP3430_EN_UART1 | OMAP3430_EN_UART2);
451 fck_per &= ~OMAP3430_EN_UART3;
452
Kevin Hilman8bd22942009-05-28 10:56:16 -0700453 if (fck_core1 | fck_core3 | fck_sgx | fck_dss |
454 fck_cam | fck_per | fck_usbhost)
455 return 1;
456 return 0;
457}
458
459static int omap3_can_sleep(void)
460{
Kevin Hilman4af40162009-02-04 10:51:40 -0800461 if (!omap_uart_can_sleep())
462 return 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700463 if (omap3_fclks_active())
464 return 0;
465 return 1;
466}
467
468/* This sets pwrdm state (other than mpu & core. Currently only ON &
469 * RET are supported. Function is assuming that clkdm doesn't have
470 * hw_sup mode enabled. */
471static int set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
472{
473 u32 cur_state;
474 int sleep_switch = 0;
475 int ret = 0;
476
477 if (pwrdm == NULL || IS_ERR(pwrdm))
478 return -EINVAL;
479
480 while (!(pwrdm->pwrsts & (1 << state))) {
481 if (state == PWRDM_POWER_OFF)
482 return ret;
483 state--;
484 }
485
486 cur_state = pwrdm_read_next_pwrst(pwrdm);
487 if (cur_state == state)
488 return ret;
489
490 if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
491 omap2_clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
492 sleep_switch = 1;
493 pwrdm_wait_transition(pwrdm);
494 }
495
496 ret = pwrdm_set_next_pwrst(pwrdm, state);
497 if (ret) {
498 printk(KERN_ERR "Unable to set state of powerdomain: %s\n",
499 pwrdm->name);
500 goto err;
501 }
502
503 if (sleep_switch) {
504 omap2_clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
505 pwrdm_wait_transition(pwrdm);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300506 pwrdm_state_switch(pwrdm);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700507 }
508
509err:
510 return ret;
511}
512
513static void omap3_pm_idle(void)
514{
515 local_irq_disable();
516 local_fiq_disable();
517
518 if (!omap3_can_sleep())
519 goto out;
520
521 if (omap_irq_pending())
522 goto out;
523
524 omap_sram_idle();
525
526out:
527 local_fiq_enable();
528 local_irq_enable();
529}
530
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700531#ifdef CONFIG_SUSPEND
Tero Kristo24662112009-03-05 16:32:23 +0200532static suspend_state_t suspend_state;
533
Kevin Hilman8bd22942009-05-28 10:56:16 -0700534static int omap3_pm_prepare(void)
535{
536 disable_hlt();
537 return 0;
538}
539
540static int omap3_pm_suspend(void)
541{
542 struct power_state *pwrst;
543 int state, ret = 0;
544
545 /* Read current next_pwrsts */
546 list_for_each_entry(pwrst, &pwrst_list, node)
547 pwrst->saved_state = pwrdm_read_next_pwrst(pwrst->pwrdm);
548 /* Set ones wanted by suspend */
549 list_for_each_entry(pwrst, &pwrst_list, node) {
550 if (set_pwrdm_state(pwrst->pwrdm, pwrst->next_state))
551 goto restore;
552 if (pwrdm_clear_all_prev_pwrst(pwrst->pwrdm))
553 goto restore;
554 }
555
Kevin Hilman4af40162009-02-04 10:51:40 -0800556 omap_uart_prepare_suspend();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700557 omap_sram_idle();
558
559restore:
560 /* Restore next_pwrsts */
561 list_for_each_entry(pwrst, &pwrst_list, node) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700562 state = pwrdm_read_prev_pwrst(pwrst->pwrdm);
563 if (state > pwrst->next_state) {
564 printk(KERN_INFO "Powerdomain (%s) didn't enter "
565 "target state %d\n",
566 pwrst->pwrdm->name, pwrst->next_state);
567 ret = -1;
568 }
Jouni Hogander6c5f8032008-10-29 12:06:04 +0200569 set_pwrdm_state(pwrst->pwrdm, pwrst->saved_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700570 }
571 if (ret)
572 printk(KERN_ERR "Could not enter target state in pm_suspend\n");
573 else
574 printk(KERN_INFO "Successfully put all powerdomains "
575 "to target state\n");
576
577 return ret;
578}
579
Tero Kristo24662112009-03-05 16:32:23 +0200580static int omap3_pm_enter(suspend_state_t unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700581{
582 int ret = 0;
583
Tero Kristo24662112009-03-05 16:32:23 +0200584 switch (suspend_state) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700585 case PM_SUSPEND_STANDBY:
586 case PM_SUSPEND_MEM:
587 ret = omap3_pm_suspend();
588 break;
589 default:
590 ret = -EINVAL;
591 }
592
593 return ret;
594}
595
596static void omap3_pm_finish(void)
597{
598 enable_hlt();
599}
600
Tero Kristo24662112009-03-05 16:32:23 +0200601/* Hooks to enable / disable UART interrupts during suspend */
602static int omap3_pm_begin(suspend_state_t state)
603{
604 suspend_state = state;
605 omap_uart_enable_irqs(0);
606 return 0;
607}
608
609static void omap3_pm_end(void)
610{
611 suspend_state = PM_SUSPEND_ON;
612 omap_uart_enable_irqs(1);
613 return;
614}
615
Kevin Hilman8bd22942009-05-28 10:56:16 -0700616static struct platform_suspend_ops omap_pm_ops = {
Tero Kristo24662112009-03-05 16:32:23 +0200617 .begin = omap3_pm_begin,
618 .end = omap3_pm_end,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700619 .prepare = omap3_pm_prepare,
620 .enter = omap3_pm_enter,
621 .finish = omap3_pm_finish,
622 .valid = suspend_valid_only_mem,
623};
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700624#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700625
Kevin Hilman1155e422008-11-25 11:48:24 -0800626
627/**
628 * omap3_iva_idle(): ensure IVA is in idle so it can be put into
629 * retention
630 *
631 * In cases where IVA2 is activated by bootcode, it may prevent
632 * full-chip retention or off-mode because it is not idle. This
633 * function forces the IVA2 into idle state so it can go
634 * into retention/off and thus allow full-chip retention/off.
635 *
636 **/
637static void __init omap3_iva_idle(void)
638{
639 /* ensure IVA2 clock is disabled */
640 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
641
642 /* if no clock activity, nothing else to do */
643 if (!(cm_read_mod_reg(OMAP3430_IVA2_MOD, OMAP3430_CM_CLKSTST) &
644 OMAP3430_CLKACTIVITY_IVA2_MASK))
645 return;
646
647 /* Reset IVA2 */
648 prm_write_mod_reg(OMAP3430_RST1_IVA2 |
649 OMAP3430_RST2_IVA2 |
650 OMAP3430_RST3_IVA2,
651 OMAP3430_IVA2_MOD, RM_RSTCTRL);
652
653 /* Enable IVA2 clock */
654 cm_write_mod_reg(OMAP3430_CM_FCLKEN_IVA2_EN_IVA2,
655 OMAP3430_IVA2_MOD, CM_FCLKEN);
656
657 /* Set IVA2 boot mode to 'idle' */
658 omap_ctrl_writel(OMAP3_IVA2_BOOTMOD_IDLE,
659 OMAP343X_CONTROL_IVA2_BOOTMOD);
660
661 /* Un-reset IVA2 */
662 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, RM_RSTCTRL);
663
664 /* Disable IVA2 clock */
665 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
666
667 /* Reset IVA2 */
668 prm_write_mod_reg(OMAP3430_RST1_IVA2 |
669 OMAP3430_RST2_IVA2 |
670 OMAP3430_RST3_IVA2,
671 OMAP3430_IVA2_MOD, RM_RSTCTRL);
672}
673
Kevin Hilman8111b222009-04-28 15:27:44 -0700674static void __init omap3_d2d_idle(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700675{
Kevin Hilman8111b222009-04-28 15:27:44 -0700676 u16 mask, padconf;
677
678 /* In a stand alone OMAP3430 where there is not a stacked
679 * modem for the D2D Idle Ack and D2D MStandby must be pulled
680 * high. S CONTROL_PADCONF_SAD2D_IDLEACK and
681 * CONTROL_PADCONF_SAD2D_MSTDBY to have a pull up. */
682 mask = (1 << 4) | (1 << 3); /* pull-up, enabled */
683 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_MSTANDBY);
684 padconf |= mask;
685 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_MSTANDBY);
686
687 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_IDLEACK);
688 padconf |= mask;
689 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_IDLEACK);
690
Kevin Hilman8bd22942009-05-28 10:56:16 -0700691 /* reset modem */
692 prm_write_mod_reg(OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RSTPWRON |
693 OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RST,
694 CORE_MOD, RM_RSTCTRL);
695 prm_write_mod_reg(0, CORE_MOD, RM_RSTCTRL);
Kevin Hilman8111b222009-04-28 15:27:44 -0700696}
Kevin Hilman8bd22942009-05-28 10:56:16 -0700697
Kevin Hilman8111b222009-04-28 15:27:44 -0700698static void __init prcm_setup_regs(void)
699{
Kevin Hilman8bd22942009-05-28 10:56:16 -0700700 /* XXX Reset all wkdeps. This should be done when initializing
701 * powerdomains */
702 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, PM_WKDEP);
703 prm_write_mod_reg(0, MPU_MOD, PM_WKDEP);
704 prm_write_mod_reg(0, OMAP3430_DSS_MOD, PM_WKDEP);
705 prm_write_mod_reg(0, OMAP3430_NEON_MOD, PM_WKDEP);
706 prm_write_mod_reg(0, OMAP3430_CAM_MOD, PM_WKDEP);
707 prm_write_mod_reg(0, OMAP3430_PER_MOD, PM_WKDEP);
708 if (omap_rev() > OMAP3430_REV_ES1_0) {
709 prm_write_mod_reg(0, OMAP3430ES2_SGX_MOD, PM_WKDEP);
710 prm_write_mod_reg(0, OMAP3430ES2_USBHOST_MOD, PM_WKDEP);
711 } else
712 prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
713
714 /*
715 * Enable interface clock autoidle for all modules.
716 * Note that in the long run this should be done by clockfw
717 */
718 cm_write_mod_reg(
Kevin Hilman8111b222009-04-28 15:27:44 -0700719 OMAP3430_AUTO_MODEM |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700720 OMAP3430ES2_AUTO_MMC3 |
721 OMAP3430ES2_AUTO_ICR |
722 OMAP3430_AUTO_AES2 |
723 OMAP3430_AUTO_SHA12 |
724 OMAP3430_AUTO_DES2 |
725 OMAP3430_AUTO_MMC2 |
726 OMAP3430_AUTO_MMC1 |
727 OMAP3430_AUTO_MSPRO |
728 OMAP3430_AUTO_HDQ |
729 OMAP3430_AUTO_MCSPI4 |
730 OMAP3430_AUTO_MCSPI3 |
731 OMAP3430_AUTO_MCSPI2 |
732 OMAP3430_AUTO_MCSPI1 |
733 OMAP3430_AUTO_I2C3 |
734 OMAP3430_AUTO_I2C2 |
735 OMAP3430_AUTO_I2C1 |
736 OMAP3430_AUTO_UART2 |
737 OMAP3430_AUTO_UART1 |
738 OMAP3430_AUTO_GPT11 |
739 OMAP3430_AUTO_GPT10 |
740 OMAP3430_AUTO_MCBSP5 |
741 OMAP3430_AUTO_MCBSP1 |
742 OMAP3430ES1_AUTO_FAC | /* This is es1 only */
743 OMAP3430_AUTO_MAILBOXES |
744 OMAP3430_AUTO_OMAPCTRL |
745 OMAP3430ES1_AUTO_FSHOSTUSB |
746 OMAP3430_AUTO_HSOTGUSB |
Kevin Hilman8111b222009-04-28 15:27:44 -0700747 OMAP3430_AUTO_SAD2D |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700748 OMAP3430_AUTO_SSI,
749 CORE_MOD, CM_AUTOIDLE1);
750
751 cm_write_mod_reg(
752 OMAP3430_AUTO_PKA |
753 OMAP3430_AUTO_AES1 |
754 OMAP3430_AUTO_RNG |
755 OMAP3430_AUTO_SHA11 |
756 OMAP3430_AUTO_DES1,
757 CORE_MOD, CM_AUTOIDLE2);
758
759 if (omap_rev() > OMAP3430_REV_ES1_0) {
760 cm_write_mod_reg(
Kevin Hilman8111b222009-04-28 15:27:44 -0700761 OMAP3430_AUTO_MAD2D |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700762 OMAP3430ES2_AUTO_USBTLL,
763 CORE_MOD, CM_AUTOIDLE3);
764 }
765
766 cm_write_mod_reg(
767 OMAP3430_AUTO_WDT2 |
768 OMAP3430_AUTO_WDT1 |
769 OMAP3430_AUTO_GPIO1 |
770 OMAP3430_AUTO_32KSYNC |
771 OMAP3430_AUTO_GPT12 |
772 OMAP3430_AUTO_GPT1 ,
773 WKUP_MOD, CM_AUTOIDLE);
774
775 cm_write_mod_reg(
776 OMAP3430_AUTO_DSS,
777 OMAP3430_DSS_MOD,
778 CM_AUTOIDLE);
779
780 cm_write_mod_reg(
781 OMAP3430_AUTO_CAM,
782 OMAP3430_CAM_MOD,
783 CM_AUTOIDLE);
784
785 cm_write_mod_reg(
786 OMAP3430_AUTO_GPIO6 |
787 OMAP3430_AUTO_GPIO5 |
788 OMAP3430_AUTO_GPIO4 |
789 OMAP3430_AUTO_GPIO3 |
790 OMAP3430_AUTO_GPIO2 |
791 OMAP3430_AUTO_WDT3 |
792 OMAP3430_AUTO_UART3 |
793 OMAP3430_AUTO_GPT9 |
794 OMAP3430_AUTO_GPT8 |
795 OMAP3430_AUTO_GPT7 |
796 OMAP3430_AUTO_GPT6 |
797 OMAP3430_AUTO_GPT5 |
798 OMAP3430_AUTO_GPT4 |
799 OMAP3430_AUTO_GPT3 |
800 OMAP3430_AUTO_GPT2 |
801 OMAP3430_AUTO_MCBSP4 |
802 OMAP3430_AUTO_MCBSP3 |
803 OMAP3430_AUTO_MCBSP2,
804 OMAP3430_PER_MOD,
805 CM_AUTOIDLE);
806
807 if (omap_rev() > OMAP3430_REV_ES1_0) {
808 cm_write_mod_reg(
809 OMAP3430ES2_AUTO_USBHOST,
810 OMAP3430ES2_USBHOST_MOD,
811 CM_AUTOIDLE);
812 }
813
814 /*
815 * Set all plls to autoidle. This is needed until autoidle is
816 * enabled by clockfw
817 */
818 cm_write_mod_reg(1 << OMAP3430_AUTO_IVA2_DPLL_SHIFT,
819 OMAP3430_IVA2_MOD, CM_AUTOIDLE2);
820 cm_write_mod_reg(1 << OMAP3430_AUTO_MPU_DPLL_SHIFT,
821 MPU_MOD,
822 CM_AUTOIDLE2);
823 cm_write_mod_reg((1 << OMAP3430_AUTO_PERIPH_DPLL_SHIFT) |
824 (1 << OMAP3430_AUTO_CORE_DPLL_SHIFT),
825 PLL_MOD,
826 CM_AUTOIDLE);
827 cm_write_mod_reg(1 << OMAP3430ES2_AUTO_PERIPH2_DPLL_SHIFT,
828 PLL_MOD,
829 CM_AUTOIDLE2);
830
831 /*
832 * Enable control of expternal oscillator through
833 * sys_clkreq. In the long run clock framework should
834 * take care of this.
835 */
836 prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK,
837 1 << OMAP_AUTOEXTCLKMODE_SHIFT,
838 OMAP3430_GR_MOD,
839 OMAP3_PRM_CLKSRC_CTRL_OFFSET);
840
841 /* setup wakup source */
842 prm_write_mod_reg(OMAP3430_EN_IO | OMAP3430_EN_GPIO1 |
843 OMAP3430_EN_GPT1 | OMAP3430_EN_GPT12,
844 WKUP_MOD, PM_WKEN);
845 /* No need to write EN_IO, that is always enabled */
846 prm_write_mod_reg(OMAP3430_EN_GPIO1 | OMAP3430_EN_GPT1 |
847 OMAP3430_EN_GPT12,
848 WKUP_MOD, OMAP3430_PM_MPUGRPSEL);
849 /* For some reason IO doesn't generate wakeup event even if
850 * it is selected to mpu wakeup goup */
851 prm_write_mod_reg(OMAP3430_IO_EN | OMAP3430_WKUP_EN,
852 OCP_MOD, OMAP3_PRM_IRQENABLE_MPU_OFFSET);
Kevin Hilman1155e422008-11-25 11:48:24 -0800853
Kevin Hilmanb427f922009-10-22 14:48:13 -0700854 /* Enable wakeups in PER */
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000855 prm_write_mod_reg(OMAP3430_EN_GPIO2 | OMAP3430_EN_GPIO3 |
856 OMAP3430_EN_GPIO4 | OMAP3430_EN_GPIO5 |
Kevin Hilmanb427f922009-10-22 14:48:13 -0700857 OMAP3430_EN_GPIO6 | OMAP3430_EN_UART3,
858 OMAP3430_PER_MOD, PM_WKEN);
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000859 /* and allow them to wake up MPU */
860 prm_write_mod_reg(OMAP3430_GRPSEL_GPIO2 | OMAP3430_EN_GPIO3 |
861 OMAP3430_GRPSEL_GPIO4 | OMAP3430_EN_GPIO5 |
Kevin Hilmanb427f922009-10-22 14:48:13 -0700862 OMAP3430_GRPSEL_GPIO6 | OMAP3430_EN_UART3,
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000863 OMAP3430_PER_MOD, OMAP3430_PM_MPUGRPSEL);
864
Kevin Hilmand3fd3292009-05-05 16:34:25 -0700865 /* Don't attach IVA interrupts */
866 prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
867 prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
868 prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
869 prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
870
Kevin Hilmanb1340d12009-04-27 16:14:54 -0700871 /* Clear any pending 'reset' flags */
872 prm_write_mod_reg(0xffffffff, MPU_MOD, RM_RSTST);
873 prm_write_mod_reg(0xffffffff, CORE_MOD, RM_RSTST);
874 prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, RM_RSTST);
875 prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, RM_RSTST);
876 prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, RM_RSTST);
877 prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, RM_RSTST);
878 prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, RM_RSTST);
879
Kevin Hilman014c46d2009-04-27 07:50:23 -0700880 /* Clear any pending PRCM interrupts */
881 prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
882
Kevin Hilman040fed02009-05-05 16:34:25 -0700883 /* Don't attach IVA interrupts */
884 prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
885 prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
886 prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
887 prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
888
Kevin Hilman3a07ae32009-04-27 16:14:54 -0700889 /* Clear any pending 'reset' flags */
890 prm_write_mod_reg(0xffffffff, MPU_MOD, RM_RSTST);
891 prm_write_mod_reg(0xffffffff, CORE_MOD, RM_RSTST);
892 prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, RM_RSTST);
893 prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, RM_RSTST);
894 prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, RM_RSTST);
895 prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, RM_RSTST);
896 prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, RM_RSTST);
897
Kevin Hilman3a6667a2009-04-27 07:50:23 -0700898 /* Clear any pending PRCM interrupts */
899 prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
900
Kevin Hilman1155e422008-11-25 11:48:24 -0800901 omap3_iva_idle();
Kevin Hilman8111b222009-04-28 15:27:44 -0700902 omap3_d2d_idle();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700903}
904
Tero Kristo68d47782008-11-26 12:26:24 +0200905int omap3_pm_get_suspend_state(struct powerdomain *pwrdm)
906{
907 struct power_state *pwrst;
908
909 list_for_each_entry(pwrst, &pwrst_list, node) {
910 if (pwrst->pwrdm == pwrdm)
911 return pwrst->next_state;
912 }
913 return -EINVAL;
914}
915
916int omap3_pm_set_suspend_state(struct powerdomain *pwrdm, int state)
917{
918 struct power_state *pwrst;
919
920 list_for_each_entry(pwrst, &pwrst_list, node) {
921 if (pwrst->pwrdm == pwrdm) {
922 pwrst->next_state = state;
923 return 0;
924 }
925 }
926 return -EINVAL;
927}
928
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300929static int __init pwrdms_setup(struct powerdomain *pwrdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700930{
931 struct power_state *pwrst;
932
933 if (!pwrdm->pwrsts)
934 return 0;
935
Ming Leid3d381c2009-08-22 21:20:26 +0800936 pwrst = kmalloc(sizeof(struct power_state), GFP_ATOMIC);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700937 if (!pwrst)
938 return -ENOMEM;
939 pwrst->pwrdm = pwrdm;
940 pwrst->next_state = PWRDM_POWER_RET;
941 list_add(&pwrst->node, &pwrst_list);
942
943 if (pwrdm_has_hdwr_sar(pwrdm))
944 pwrdm_enable_hdwr_sar(pwrdm);
945
946 return set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
947}
948
949/*
950 * Enable hw supervised mode for all clockdomains if it's
951 * supported. Initiate sleep transition for other clockdomains, if
952 * they are not used
953 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300954static int __init clkdms_setup(struct clockdomain *clkdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700955{
956 if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
957 omap2_clkdm_allow_idle(clkdm);
958 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
959 atomic_read(&clkdm->usecount) == 0)
960 omap2_clkdm_sleep(clkdm);
961 return 0;
962}
963
Rajendra Nayak3231fc82008-09-26 17:49:14 +0530964void omap_push_sram_idle(void)
965{
966 _omap_sram_idle = omap_sram_push(omap34xx_cpu_suspend,
967 omap34xx_cpu_suspend_sz);
Tero Kristo27d59a42008-10-13 13:15:00 +0300968 if (omap_type() != OMAP2_DEVICE_TYPE_GP)
969 _omap_save_secure_sram = omap_sram_push(save_secure_ram_context,
970 save_secure_ram_context_sz);
Rajendra Nayak3231fc82008-09-26 17:49:14 +0530971}
972
Kevin Hilman7cc515f2009-06-10 09:02:25 -0700973static int __init omap3_pm_init(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700974{
975 struct power_state *pwrst, *tmp;
976 int ret;
977
978 if (!cpu_is_omap34xx())
979 return -ENODEV;
980
981 printk(KERN_ERR "Power Management for TI OMAP3.\n");
982
983 /* XXX prcm_setup_regs needs to be before enabling hw
984 * supervised mode for powerdomains */
985 prcm_setup_regs();
986
987 ret = request_irq(INT_34XX_PRCM_MPU_IRQ,
988 (irq_handler_t)prcm_interrupt_handler,
989 IRQF_DISABLED, "prcm", NULL);
990 if (ret) {
991 printk(KERN_ERR "request_irq failed to register for 0x%x\n",
992 INT_34XX_PRCM_MPU_IRQ);
993 goto err1;
994 }
995
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300996 ret = pwrdm_for_each(pwrdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700997 if (ret) {
998 printk(KERN_ERR "Failed to setup powerdomains\n");
999 goto err2;
1000 }
1001
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001002 (void) clkdm_for_each(clkdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001003
1004 mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
1005 if (mpu_pwrdm == NULL) {
1006 printk(KERN_ERR "Failed to get mpu_pwrdm\n");
1007 goto err2;
1008 }
1009
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301010 neon_pwrdm = pwrdm_lookup("neon_pwrdm");
1011 per_pwrdm = pwrdm_lookup("per_pwrdm");
1012 core_pwrdm = pwrdm_lookup("core_pwrdm");
1013
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301014 omap_push_sram_idle();
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001015#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -07001016 suspend_set_ops(&omap_pm_ops);
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001017#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -07001018
1019 pm_idle = omap3_pm_idle;
1020
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301021 pwrdm_add_wkdep(neon_pwrdm, mpu_pwrdm);
1022 /*
1023 * REVISIT: This wkdep is only necessary when GPIO2-6 are enabled for
1024 * IO-pad wakeup. Otherwise it will unnecessarily waste power
1025 * waking up PER with every CORE wakeup - see
1026 * http://marc.info/?l=linux-omap&m=121852150710062&w=2
1027 */
1028 pwrdm_add_wkdep(per_pwrdm, core_pwrdm);
1029
Tero Kristo27d59a42008-10-13 13:15:00 +03001030 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
1031 omap3_secure_ram_storage =
1032 kmalloc(0x803F, GFP_KERNEL);
1033 if (!omap3_secure_ram_storage)
1034 printk(KERN_ERR "Memory allocation failed when"
1035 "allocating for secure sram context\n");
Tero Kristo27d59a42008-10-13 13:15:00 +03001036
Tero Kristo9d971402008-12-12 11:20:05 +02001037 local_irq_disable();
1038 local_fiq_disable();
1039
1040 omap_dma_global_context_save();
1041 omap3_save_secure_ram_context(PWRDM_POWER_ON);
1042 omap_dma_global_context_restore();
1043
1044 local_irq_enable();
1045 local_fiq_enable();
1046 }
1047
1048 omap3_save_scratchpad_contents();
Kevin Hilman8bd22942009-05-28 10:56:16 -07001049err1:
1050 return ret;
1051err2:
1052 free_irq(INT_34XX_PRCM_MPU_IRQ, NULL);
1053 list_for_each_entry_safe(pwrst, tmp, &pwrst_list, node) {
1054 list_del(&pwrst->node);
1055 kfree(pwrst);
1056 }
1057 return ret;
1058}
1059
1060late_initcall(omap3_pm_init);