blob: ade2e4a6bb7d16ae4203793ef61d0463c70f172f [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>
Kevin Hilmanc40552b2009-10-06 14:25:09 -070028#include <linux/clk.h>
Kevin Hilman8bd22942009-05-28 10:56:16 -070029
Tony Lindgrence491cf2009-10-20 09:40:47 -070030#include <plat/sram.h>
31#include <plat/clockdomain.h>
32#include <plat/powerdomain.h>
33#include <plat/control.h>
34#include <plat/serial.h>
Rajendra Nayak61255ab2008-09-26 17:49:56 +053035#include <plat/sdrc.h>
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053036#include <plat/prcm.h>
37#include <plat/gpmc.h>
Tero Kristof2d11852008-08-28 13:13:31 +000038#include <plat/dma.h>
Kevin Hilman8bd22942009-05-28 10:56:16 -070039
Rajendra Nayak57f277b2008-09-26 17:49:34 +053040#include <asm/tlbflush.h>
41
Kevin Hilman8bd22942009-05-28 10:56:16 -070042#include "cm.h"
43#include "cm-regbits-34xx.h"
44#include "prm-regbits-34xx.h"
45
46#include "prm.h"
47#include "pm.h"
Tero Kristo13a6fe02008-10-13 13:17:06 +030048#include "sdrc.h"
49
50#define SDRC_POWER_AUTOCOUNT_SHIFT 8
51#define SDRC_POWER_AUTOCOUNT_MASK (0xffff << SDRC_POWER_AUTOCOUNT_SHIFT)
52#define SDRC_POWER_CLKCTRL_SHIFT 4
53#define SDRC_POWER_CLKCTRL_MASK (0x3 << SDRC_POWER_CLKCTRL_SHIFT)
54#define SDRC_SELF_REFRESH_ON_AUTOCOUNT (0x2 << SDRC_POWER_CLKCTRL_SHIFT)
Kevin Hilman8bd22942009-05-28 10:56:16 -070055
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053056/* Scratchpad offsets */
57#define OMAP343X_TABLE_ADDRESS_OFFSET 0x31
58#define OMAP343X_TABLE_VALUE_OFFSET 0x30
59#define OMAP343X_CONTROL_REG_VALUE_OFFSET 0x32
60
Kevin Hilmanc40552b2009-10-06 14:25:09 -070061u32 enable_off_mode;
62u32 sleep_while_idle;
63
Kevin Hilman8bd22942009-05-28 10:56:16 -070064struct power_state {
65 struct powerdomain *pwrdm;
66 u32 next_state;
Kevin Hilman10f90ed2009-06-24 11:39:18 -070067#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -070068 u32 saved_state;
Kevin Hilman10f90ed2009-06-24 11:39:18 -070069#endif
Kevin Hilman8bd22942009-05-28 10:56:16 -070070 struct list_head node;
71};
72
73static LIST_HEAD(pwrst_list);
74
75static void (*_omap_sram_idle)(u32 *addr, int save_state);
76
Tero Kristo27d59a42008-10-13 13:15:00 +030077static int (*_omap_save_secure_sram)(u32 *addr);
78
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +053079static struct powerdomain *mpu_pwrdm, *neon_pwrdm;
80static struct powerdomain *core_pwrdm, *per_pwrdm;
81
82static int set_pwrdm_state(struct powerdomain *pwrdm, u32 state);
Kevin Hilman8bd22942009-05-28 10:56:16 -070083
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053084static inline void omap3_per_save_context(void)
85{
86 omap_gpio_save_context();
87}
88
89static inline void omap3_per_restore_context(void)
90{
91 omap_gpio_restore_context();
92}
93
94static void omap3_core_save_context(void)
95{
96 u32 control_padconf_off;
97
98 /* Save the padconf registers */
99 control_padconf_off = omap_ctrl_readl(OMAP343X_CONTROL_PADCONF_OFF);
100 control_padconf_off |= START_PADCONF_SAVE;
101 omap_ctrl_writel(control_padconf_off, OMAP343X_CONTROL_PADCONF_OFF);
102 /* wait for the save to complete */
103 while (!omap_ctrl_readl(OMAP343X_CONTROL_GENERAL_PURPOSE_STATUS)
104 & PADCONF_SAVE_DONE)
105 ;
106 /* Save the Interrupt controller context */
107 omap_intc_save_context();
108 /* Save the GPMC context */
109 omap3_gpmc_save_context();
110 /* Save the system control module context, padconf already save above*/
111 omap3_control_save_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000112 omap_dma_global_context_save();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530113}
114
115static void omap3_core_restore_context(void)
116{
117 /* Restore the control module context, padconf restored by h/w */
118 omap3_control_restore_context();
119 /* Restore the GPMC context */
120 omap3_gpmc_restore_context();
121 /* Restore the interrupt controller context */
122 omap_intc_restore_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000123 omap_dma_global_context_restore();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530124}
125
Tero Kristo9d971402008-12-12 11:20:05 +0200126/*
127 * FIXME: This function should be called before entering off-mode after
128 * OMAP3 secure services have been accessed. Currently it is only called
129 * once during boot sequence, but this works as we are not using secure
130 * services.
131 */
Tero Kristo27d59a42008-10-13 13:15:00 +0300132static void omap3_save_secure_ram_context(u32 target_mpu_state)
133{
134 u32 ret;
135
136 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
Tero Kristo27d59a42008-10-13 13:15:00 +0300137 /*
138 * MPU next state must be set to POWER_ON temporarily,
139 * otherwise the WFI executed inside the ROM code
140 * will hang the system.
141 */
142 pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_ON);
143 ret = _omap_save_secure_sram((u32 *)
144 __pa(omap3_secure_ram_storage));
145 pwrdm_set_next_pwrst(mpu_pwrdm, target_mpu_state);
146 /* Following is for error tracking, it should not happen */
147 if (ret) {
148 printk(KERN_ERR "save_secure_sram() returns %08x\n",
149 ret);
150 while (1)
151 ;
152 }
153 }
154}
155
Jon Hunter77da2d92009-06-27 00:07:25 -0500156/*
157 * PRCM Interrupt Handler Helper Function
158 *
159 * The purpose of this function is to clear any wake-up events latched
160 * in the PRCM PM_WKST_x registers. It is possible that a wake-up event
161 * may occur whilst attempting to clear a PM_WKST_x register and thus
162 * set another bit in this register. A while loop is used to ensure
163 * that any peripheral wake-up events occurring while attempting to
164 * clear the PM_WKST_x are detected and cleared.
165 */
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700166static int prcm_clear_mod_irqs(s16 module, u8 regs)
Jon Hunter77da2d92009-06-27 00:07:25 -0500167{
Vikram Pandita71a80772009-07-17 19:33:09 -0500168 u32 wkst, fclk, iclk, clken;
Jon Hunter77da2d92009-06-27 00:07:25 -0500169 u16 wkst_off = (regs == 3) ? OMAP3430ES2_PM_WKST3 : PM_WKST1;
170 u16 fclk_off = (regs == 3) ? OMAP3430ES2_CM_FCLKEN3 : CM_FCLKEN1;
171 u16 iclk_off = (regs == 3) ? CM_ICLKEN3 : CM_ICLKEN1;
Paul Walmsley5d805972009-07-22 10:18:07 -0700172 u16 grpsel_off = (regs == 3) ?
173 OMAP3430ES2_PM_MPUGRPSEL3 : OMAP3430_PM_MPUGRPSEL;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700174 int c = 0;
Jon Hunter77da2d92009-06-27 00:07:25 -0500175
176 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley5d805972009-07-22 10:18:07 -0700177 wkst &= prm_read_mod_reg(module, grpsel_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500178 if (wkst) {
179 iclk = cm_read_mod_reg(module, iclk_off);
180 fclk = cm_read_mod_reg(module, fclk_off);
181 while (wkst) {
Vikram Pandita71a80772009-07-17 19:33:09 -0500182 clken = wkst;
183 cm_set_mod_reg_bits(clken, module, iclk_off);
184 /*
185 * For USBHOST, we don't know whether HOST1 or
186 * HOST2 woke us up, so enable both f-clocks
187 */
188 if (module == OMAP3430ES2_USBHOST_MOD)
189 clken |= 1 << OMAP3430ES2_EN_USBHOST2_SHIFT;
190 cm_set_mod_reg_bits(clken, module, fclk_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500191 prm_write_mod_reg(wkst, module, wkst_off);
192 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700193 c++;
Jon Hunter77da2d92009-06-27 00:07:25 -0500194 }
195 cm_write_mod_reg(iclk, module, iclk_off);
196 cm_write_mod_reg(fclk, module, fclk_off);
197 }
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700198
199 return c;
200}
201
202static int _prcm_int_handle_wakeup(void)
203{
204 int c;
205
206 c = prcm_clear_mod_irqs(WKUP_MOD, 1);
207 c += prcm_clear_mod_irqs(CORE_MOD, 1);
208 c += prcm_clear_mod_irqs(OMAP3430_PER_MOD, 1);
209 if (omap_rev() > OMAP3430_REV_ES1_0) {
210 c += prcm_clear_mod_irqs(CORE_MOD, 3);
211 c += prcm_clear_mod_irqs(OMAP3430ES2_USBHOST_MOD, 1);
212 }
213
214 return c;
Jon Hunter77da2d92009-06-27 00:07:25 -0500215}
216
217/*
218 * PRCM Interrupt Handler
219 *
220 * The PRM_IRQSTATUS_MPU register indicates if there are any pending
221 * interrupts from the PRCM for the MPU. These bits must be cleared in
222 * order to clear the PRCM interrupt. The PRCM interrupt handler is
223 * implemented to simply clear the PRM_IRQSTATUS_MPU in order to clear
224 * the PRCM interrupt. Please note that bit 0 of the PRM_IRQSTATUS_MPU
225 * register indicates that a wake-up event is pending for the MPU and
226 * this bit can only be cleared if the all the wake-up events latched
227 * in the various PM_WKST_x registers have been cleared. The interrupt
228 * handler is implemented using a do-while loop so that if a wake-up
229 * event occurred during the processing of the prcm interrupt handler
230 * (setting a bit in the corresponding PM_WKST_x register and thus
231 * preventing us from clearing bit 0 of the PRM_IRQSTATUS_MPU register)
232 * this would be handled.
233 */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700234static irqreturn_t prcm_interrupt_handler (int irq, void *dev_id)
235{
Jon Hunter77da2d92009-06-27 00:07:25 -0500236 u32 irqstatus_mpu;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700237 int c = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700238
Jon Hunter77da2d92009-06-27 00:07:25 -0500239 do {
Jon Hunter77da2d92009-06-27 00:07:25 -0500240 irqstatus_mpu = prm_read_mod_reg(OCP_MOD,
241 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700242
243 if (irqstatus_mpu & (OMAP3430_WKUP_ST | OMAP3430_IO_ST)) {
244 c = _prcm_int_handle_wakeup();
245
246 /*
247 * Is the MPU PRCM interrupt handler racing with the
248 * IVA2 PRCM interrupt handler ?
249 */
250 WARN(c == 0, "prcm: WARNING: PRCM indicated MPU wakeup "
251 "but no wakeup sources are marked\n");
252 } else {
253 /* XXX we need to expand our PRCM interrupt handler */
254 WARN(1, "prcm: WARNING: PRCM interrupt received, but "
255 "no code to handle it (%08x)\n", irqstatus_mpu);
256 }
257
Jon Hunter77da2d92009-06-27 00:07:25 -0500258 prm_write_mod_reg(irqstatus_mpu, OCP_MOD,
259 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700260
Jon Hunter77da2d92009-06-27 00:07:25 -0500261 } while (prm_read_mod_reg(OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET));
Kevin Hilman8bd22942009-05-28 10:56:16 -0700262
263 return IRQ_HANDLED;
264}
265
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530266static void restore_control_register(u32 val)
267{
268 __asm__ __volatile__ ("mcr p15, 0, %0, c1, c0, 0" : : "r" (val));
269}
270
271/* Function to restore the table entry that was modified for enabling MMU */
272static void restore_table_entry(void)
273{
274 u32 *scratchpad_address;
275 u32 previous_value, control_reg_value;
276 u32 *address;
277
278 scratchpad_address = OMAP2_L4_IO_ADDRESS(OMAP343X_SCRATCHPAD);
279
280 /* Get address of entry that was modified */
281 address = (u32 *)__raw_readl(scratchpad_address +
282 OMAP343X_TABLE_ADDRESS_OFFSET);
283 /* Get the previous value which needs to be restored */
284 previous_value = __raw_readl(scratchpad_address +
285 OMAP343X_TABLE_VALUE_OFFSET);
286 address = __va(address);
287 *address = previous_value;
288 flush_tlb_all();
289 control_reg_value = __raw_readl(scratchpad_address
290 + OMAP343X_CONTROL_REG_VALUE_OFFSET);
291 /* This will enable caches and prediction */
292 restore_control_register(control_reg_value);
293}
294
Kevin Hilman8bd22942009-05-28 10:56:16 -0700295static void omap_sram_idle(void)
296{
297 /* Variable to tell what needs to be saved and restored
298 * in omap_sram_idle*/
299 /* save_state = 0 => Nothing to save and restored */
300 /* save_state = 1 => Only L1 and logic lost */
301 /* save_state = 2 => Only L2 lost */
302 /* save_state = 3 => L1, L2 and logic lost */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530303 int save_state = 0;
304 int mpu_next_state = PWRDM_POWER_ON;
305 int per_next_state = PWRDM_POWER_ON;
306 int core_next_state = PWRDM_POWER_ON;
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530307 int core_prev_state, per_prev_state;
Tero Kristo13a6fe02008-10-13 13:17:06 +0300308 u32 sdrc_pwr = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700309
310 if (!_omap_sram_idle)
311 return;
312
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530313 pwrdm_clear_all_prev_pwrst(mpu_pwrdm);
314 pwrdm_clear_all_prev_pwrst(neon_pwrdm);
315 pwrdm_clear_all_prev_pwrst(core_pwrdm);
316 pwrdm_clear_all_prev_pwrst(per_pwrdm);
317
Kevin Hilman8bd22942009-05-28 10:56:16 -0700318 mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
319 switch (mpu_next_state) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530320 case PWRDM_POWER_ON:
Kevin Hilman8bd22942009-05-28 10:56:16 -0700321 case PWRDM_POWER_RET:
322 /* No need to save context */
323 save_state = 0;
324 break;
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530325 case PWRDM_POWER_OFF:
326 save_state = 3;
327 break;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700328 default:
329 /* Invalid state */
330 printk(KERN_ERR "Invalid mpu state in sram_idle\n");
331 return;
332 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300333 pwrdm_pre_transition();
334
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530335 /* NEON control */
336 if (pwrdm_read_pwrst(neon_pwrdm) == PWRDM_POWER_ON)
337 set_pwrdm_state(neon_pwrdm, mpu_next_state);
338
339 /* CORE & PER */
340 core_next_state = pwrdm_read_next_pwrst(core_pwrdm);
341 if (core_next_state < PWRDM_POWER_ON) {
342 omap2_gpio_prepare_for_retention();
343 omap_uart_prepare_idle(0);
344 omap_uart_prepare_idle(1);
345 /* PER changes only with core */
346 per_next_state = pwrdm_read_next_pwrst(per_pwrdm);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530347 if (per_next_state < PWRDM_POWER_ON) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530348 omap_uart_prepare_idle(2);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530349 if (per_next_state == PWRDM_POWER_OFF)
350 omap3_per_save_context();
351 }
352 if (core_next_state == PWRDM_POWER_OFF) {
353 omap3_core_save_context();
354 omap3_prcm_save_context();
355 }
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530356 /* Enable IO-PAD wakeup */
357 prm_set_mod_reg_bits(OMAP3430_EN_IO, WKUP_MOD, PM_WKEN);
358 }
Kevin Hilman8bd22942009-05-28 10:56:16 -0700359
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530360 /*
Tero Kristo13a6fe02008-10-13 13:17:06 +0300361 * Force SDRAM controller to self-refresh mode after timeout on
362 * autocount. This is needed on ES3.0 to avoid SDRAM controller
363 * hang-ups.
364 */
365 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
366 omap_type() != OMAP2_DEVICE_TYPE_GP &&
367 core_next_state == PWRDM_POWER_OFF) {
368 sdrc_pwr = sdrc_read_reg(SDRC_POWER);
369 sdrc_write_reg((sdrc_pwr &
370 ~(SDRC_POWER_AUTOCOUNT_MASK|SDRC_POWER_CLKCTRL_MASK)) |
371 (1 << SDRC_POWER_AUTOCOUNT_SHIFT) |
372 SDRC_SELF_REFRESH_ON_AUTOCOUNT, SDRC_POWER);
373 }
374
375 /*
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530376 * omap3_arm_context is the location where ARM registers
377 * get saved. The restore path then reads from this
378 * location and restores them back.
379 */
380 _omap_sram_idle(omap3_arm_context, save_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700381 cpu_init();
382
Tero Kristo13a6fe02008-10-13 13:17:06 +0300383 /* Restore normal SDRAM settings */
384 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
385 omap_type() != OMAP2_DEVICE_TYPE_GP &&
386 core_next_state == PWRDM_POWER_OFF)
387 sdrc_write_reg(sdrc_pwr, SDRC_POWER);
388
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530389 /* Restore table entry modified during MMU restoration */
390 if (pwrdm_read_prev_pwrst(mpu_pwrdm) == PWRDM_POWER_OFF)
391 restore_table_entry();
392
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530393 if (core_next_state < PWRDM_POWER_ON) {
394 if (per_next_state < PWRDM_POWER_ON)
395 omap_uart_resume_idle(2);
396 omap_uart_resume_idle(1);
397 omap_uart_resume_idle(0);
398
399 /* Disable IO-PAD wakeup */
400 prm_clear_mod_reg_bits(OMAP3430_EN_IO, WKUP_MOD, PM_WKEN);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530401 core_prev_state = pwrdm_read_prev_pwrst(core_pwrdm);
402 if (core_prev_state == PWRDM_POWER_OFF) {
403 omap3_core_restore_context();
404 omap3_prcm_restore_context();
405 omap3_sram_restore_context();
Kalle Jokiniemi8a917d22009-05-13 13:32:11 +0300406 omap2_sms_restore_context();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530407 }
408 if (per_next_state < PWRDM_POWER_ON) {
409 per_prev_state =
410 pwrdm_read_prev_pwrst(per_pwrdm);
411 if (per_prev_state == PWRDM_POWER_OFF)
412 omap3_per_restore_context();
413 }
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530414 omap2_gpio_resume_after_retention();
415 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300416
417 pwrdm_post_transition();
418
Kevin Hilman8bd22942009-05-28 10:56:16 -0700419}
420
421/*
422 * Check if functional clocks are enabled before entering
423 * sleep. This function could be behind CONFIG_PM_DEBUG
424 * when all drivers are configuring their sysconfig registers
425 * properly and using their clocks properly.
426 */
427static int omap3_fclks_active(void)
428{
429 u32 fck_core1 = 0, fck_core3 = 0, fck_sgx = 0, fck_dss = 0,
430 fck_cam = 0, fck_per = 0, fck_usbhost = 0;
431
432 fck_core1 = cm_read_mod_reg(CORE_MOD,
433 CM_FCLKEN1);
434 if (omap_rev() > OMAP3430_REV_ES1_0) {
435 fck_core3 = cm_read_mod_reg(CORE_MOD,
436 OMAP3430ES2_CM_FCLKEN3);
437 fck_sgx = cm_read_mod_reg(OMAP3430ES2_SGX_MOD,
438 CM_FCLKEN);
439 fck_usbhost = cm_read_mod_reg(OMAP3430ES2_USBHOST_MOD,
440 CM_FCLKEN);
441 } else
442 fck_sgx = cm_read_mod_reg(GFX_MOD,
443 OMAP3430ES2_CM_FCLKEN3);
444 fck_dss = cm_read_mod_reg(OMAP3430_DSS_MOD,
445 CM_FCLKEN);
446 fck_cam = cm_read_mod_reg(OMAP3430_CAM_MOD,
447 CM_FCLKEN);
448 fck_per = cm_read_mod_reg(OMAP3430_PER_MOD,
449 CM_FCLKEN);
Kevin Hilman4af40162009-02-04 10:51:40 -0800450
451 /* Ignore UART clocks. These are handled by UART core (serial.c) */
452 fck_core1 &= ~(OMAP3430_EN_UART1 | OMAP3430_EN_UART2);
453 fck_per &= ~OMAP3430_EN_UART3;
454
Kevin Hilman8bd22942009-05-28 10:56:16 -0700455 if (fck_core1 | fck_core3 | fck_sgx | fck_dss |
456 fck_cam | fck_per | fck_usbhost)
457 return 1;
458 return 0;
459}
460
461static int omap3_can_sleep(void)
462{
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700463 if (!sleep_while_idle)
464 return 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800465 if (!omap_uart_can_sleep())
466 return 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700467 if (omap3_fclks_active())
468 return 0;
469 return 1;
470}
471
472/* This sets pwrdm state (other than mpu & core. Currently only ON &
473 * RET are supported. Function is assuming that clkdm doesn't have
474 * hw_sup mode enabled. */
475static int set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
476{
477 u32 cur_state;
478 int sleep_switch = 0;
479 int ret = 0;
480
481 if (pwrdm == NULL || IS_ERR(pwrdm))
482 return -EINVAL;
483
484 while (!(pwrdm->pwrsts & (1 << state))) {
485 if (state == PWRDM_POWER_OFF)
486 return ret;
487 state--;
488 }
489
490 cur_state = pwrdm_read_next_pwrst(pwrdm);
491 if (cur_state == state)
492 return ret;
493
494 if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
495 omap2_clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
496 sleep_switch = 1;
497 pwrdm_wait_transition(pwrdm);
498 }
499
500 ret = pwrdm_set_next_pwrst(pwrdm, state);
501 if (ret) {
502 printk(KERN_ERR "Unable to set state of powerdomain: %s\n",
503 pwrdm->name);
504 goto err;
505 }
506
507 if (sleep_switch) {
508 omap2_clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
509 pwrdm_wait_transition(pwrdm);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300510 pwrdm_state_switch(pwrdm);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700511 }
512
513err:
514 return ret;
515}
516
517static void omap3_pm_idle(void)
518{
519 local_irq_disable();
520 local_fiq_disable();
521
522 if (!omap3_can_sleep())
523 goto out;
524
525 if (omap_irq_pending())
526 goto out;
527
528 omap_sram_idle();
529
530out:
531 local_fiq_enable();
532 local_irq_enable();
533}
534
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700535#ifdef CONFIG_SUSPEND
Tero Kristo24662112009-03-05 16:32:23 +0200536static suspend_state_t suspend_state;
537
Kevin Hilman8bd22942009-05-28 10:56:16 -0700538static int omap3_pm_prepare(void)
539{
540 disable_hlt();
541 return 0;
542}
543
544static int omap3_pm_suspend(void)
545{
546 struct power_state *pwrst;
547 int state, ret = 0;
548
549 /* Read current next_pwrsts */
550 list_for_each_entry(pwrst, &pwrst_list, node)
551 pwrst->saved_state = pwrdm_read_next_pwrst(pwrst->pwrdm);
552 /* Set ones wanted by suspend */
553 list_for_each_entry(pwrst, &pwrst_list, node) {
554 if (set_pwrdm_state(pwrst->pwrdm, pwrst->next_state))
555 goto restore;
556 if (pwrdm_clear_all_prev_pwrst(pwrst->pwrdm))
557 goto restore;
558 }
559
Kevin Hilman4af40162009-02-04 10:51:40 -0800560 omap_uart_prepare_suspend();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700561 omap_sram_idle();
562
563restore:
564 /* Restore next_pwrsts */
565 list_for_each_entry(pwrst, &pwrst_list, node) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700566 state = pwrdm_read_prev_pwrst(pwrst->pwrdm);
567 if (state > pwrst->next_state) {
568 printk(KERN_INFO "Powerdomain (%s) didn't enter "
569 "target state %d\n",
570 pwrst->pwrdm->name, pwrst->next_state);
571 ret = -1;
572 }
Jouni Hogander6c5f8032008-10-29 12:06:04 +0200573 set_pwrdm_state(pwrst->pwrdm, pwrst->saved_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700574 }
575 if (ret)
576 printk(KERN_ERR "Could not enter target state in pm_suspend\n");
577 else
578 printk(KERN_INFO "Successfully put all powerdomains "
579 "to target state\n");
580
581 return ret;
582}
583
Tero Kristo24662112009-03-05 16:32:23 +0200584static int omap3_pm_enter(suspend_state_t unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700585{
586 int ret = 0;
587
Tero Kristo24662112009-03-05 16:32:23 +0200588 switch (suspend_state) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700589 case PM_SUSPEND_STANDBY:
590 case PM_SUSPEND_MEM:
591 ret = omap3_pm_suspend();
592 break;
593 default:
594 ret = -EINVAL;
595 }
596
597 return ret;
598}
599
600static void omap3_pm_finish(void)
601{
602 enable_hlt();
603}
604
Tero Kristo24662112009-03-05 16:32:23 +0200605/* Hooks to enable / disable UART interrupts during suspend */
606static int omap3_pm_begin(suspend_state_t state)
607{
608 suspend_state = state;
609 omap_uart_enable_irqs(0);
610 return 0;
611}
612
613static void omap3_pm_end(void)
614{
615 suspend_state = PM_SUSPEND_ON;
616 omap_uart_enable_irqs(1);
617 return;
618}
619
Kevin Hilman8bd22942009-05-28 10:56:16 -0700620static struct platform_suspend_ops omap_pm_ops = {
Tero Kristo24662112009-03-05 16:32:23 +0200621 .begin = omap3_pm_begin,
622 .end = omap3_pm_end,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700623 .prepare = omap3_pm_prepare,
624 .enter = omap3_pm_enter,
625 .finish = omap3_pm_finish,
626 .valid = suspend_valid_only_mem,
627};
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700628#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700629
Kevin Hilman1155e422008-11-25 11:48:24 -0800630
631/**
632 * omap3_iva_idle(): ensure IVA is in idle so it can be put into
633 * retention
634 *
635 * In cases where IVA2 is activated by bootcode, it may prevent
636 * full-chip retention or off-mode because it is not idle. This
637 * function forces the IVA2 into idle state so it can go
638 * into retention/off and thus allow full-chip retention/off.
639 *
640 **/
641static void __init omap3_iva_idle(void)
642{
643 /* ensure IVA2 clock is disabled */
644 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
645
646 /* if no clock activity, nothing else to do */
647 if (!(cm_read_mod_reg(OMAP3430_IVA2_MOD, OMAP3430_CM_CLKSTST) &
648 OMAP3430_CLKACTIVITY_IVA2_MASK))
649 return;
650
651 /* Reset IVA2 */
652 prm_write_mod_reg(OMAP3430_RST1_IVA2 |
653 OMAP3430_RST2_IVA2 |
654 OMAP3430_RST3_IVA2,
655 OMAP3430_IVA2_MOD, RM_RSTCTRL);
656
657 /* Enable IVA2 clock */
658 cm_write_mod_reg(OMAP3430_CM_FCLKEN_IVA2_EN_IVA2,
659 OMAP3430_IVA2_MOD, CM_FCLKEN);
660
661 /* Set IVA2 boot mode to 'idle' */
662 omap_ctrl_writel(OMAP3_IVA2_BOOTMOD_IDLE,
663 OMAP343X_CONTROL_IVA2_BOOTMOD);
664
665 /* Un-reset IVA2 */
666 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, RM_RSTCTRL);
667
668 /* Disable IVA2 clock */
669 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
670
671 /* Reset IVA2 */
672 prm_write_mod_reg(OMAP3430_RST1_IVA2 |
673 OMAP3430_RST2_IVA2 |
674 OMAP3430_RST3_IVA2,
675 OMAP3430_IVA2_MOD, RM_RSTCTRL);
676}
677
Kevin Hilman8111b222009-04-28 15:27:44 -0700678static void __init omap3_d2d_idle(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700679{
Kevin Hilman8111b222009-04-28 15:27:44 -0700680 u16 mask, padconf;
681
682 /* In a stand alone OMAP3430 where there is not a stacked
683 * modem for the D2D Idle Ack and D2D MStandby must be pulled
684 * high. S CONTROL_PADCONF_SAD2D_IDLEACK and
685 * CONTROL_PADCONF_SAD2D_MSTDBY to have a pull up. */
686 mask = (1 << 4) | (1 << 3); /* pull-up, enabled */
687 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_MSTANDBY);
688 padconf |= mask;
689 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_MSTANDBY);
690
691 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_IDLEACK);
692 padconf |= mask;
693 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_IDLEACK);
694
Kevin Hilman8bd22942009-05-28 10:56:16 -0700695 /* reset modem */
696 prm_write_mod_reg(OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RSTPWRON |
697 OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RST,
698 CORE_MOD, RM_RSTCTRL);
699 prm_write_mod_reg(0, CORE_MOD, RM_RSTCTRL);
Kevin Hilman8111b222009-04-28 15:27:44 -0700700}
Kevin Hilman8bd22942009-05-28 10:56:16 -0700701
Kevin Hilman8111b222009-04-28 15:27:44 -0700702static void __init prcm_setup_regs(void)
703{
Kevin Hilman8bd22942009-05-28 10:56:16 -0700704 /* XXX Reset all wkdeps. This should be done when initializing
705 * powerdomains */
706 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, PM_WKDEP);
707 prm_write_mod_reg(0, MPU_MOD, PM_WKDEP);
708 prm_write_mod_reg(0, OMAP3430_DSS_MOD, PM_WKDEP);
709 prm_write_mod_reg(0, OMAP3430_NEON_MOD, PM_WKDEP);
710 prm_write_mod_reg(0, OMAP3430_CAM_MOD, PM_WKDEP);
711 prm_write_mod_reg(0, OMAP3430_PER_MOD, PM_WKDEP);
712 if (omap_rev() > OMAP3430_REV_ES1_0) {
713 prm_write_mod_reg(0, OMAP3430ES2_SGX_MOD, PM_WKDEP);
714 prm_write_mod_reg(0, OMAP3430ES2_USBHOST_MOD, PM_WKDEP);
715 } else
716 prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
717
718 /*
719 * Enable interface clock autoidle for all modules.
720 * Note that in the long run this should be done by clockfw
721 */
722 cm_write_mod_reg(
Kevin Hilman8111b222009-04-28 15:27:44 -0700723 OMAP3430_AUTO_MODEM |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700724 OMAP3430ES2_AUTO_MMC3 |
725 OMAP3430ES2_AUTO_ICR |
726 OMAP3430_AUTO_AES2 |
727 OMAP3430_AUTO_SHA12 |
728 OMAP3430_AUTO_DES2 |
729 OMAP3430_AUTO_MMC2 |
730 OMAP3430_AUTO_MMC1 |
731 OMAP3430_AUTO_MSPRO |
732 OMAP3430_AUTO_HDQ |
733 OMAP3430_AUTO_MCSPI4 |
734 OMAP3430_AUTO_MCSPI3 |
735 OMAP3430_AUTO_MCSPI2 |
736 OMAP3430_AUTO_MCSPI1 |
737 OMAP3430_AUTO_I2C3 |
738 OMAP3430_AUTO_I2C2 |
739 OMAP3430_AUTO_I2C1 |
740 OMAP3430_AUTO_UART2 |
741 OMAP3430_AUTO_UART1 |
742 OMAP3430_AUTO_GPT11 |
743 OMAP3430_AUTO_GPT10 |
744 OMAP3430_AUTO_MCBSP5 |
745 OMAP3430_AUTO_MCBSP1 |
746 OMAP3430ES1_AUTO_FAC | /* This is es1 only */
747 OMAP3430_AUTO_MAILBOXES |
748 OMAP3430_AUTO_OMAPCTRL |
749 OMAP3430ES1_AUTO_FSHOSTUSB |
750 OMAP3430_AUTO_HSOTGUSB |
Kevin Hilman8111b222009-04-28 15:27:44 -0700751 OMAP3430_AUTO_SAD2D |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700752 OMAP3430_AUTO_SSI,
753 CORE_MOD, CM_AUTOIDLE1);
754
755 cm_write_mod_reg(
756 OMAP3430_AUTO_PKA |
757 OMAP3430_AUTO_AES1 |
758 OMAP3430_AUTO_RNG |
759 OMAP3430_AUTO_SHA11 |
760 OMAP3430_AUTO_DES1,
761 CORE_MOD, CM_AUTOIDLE2);
762
763 if (omap_rev() > OMAP3430_REV_ES1_0) {
764 cm_write_mod_reg(
Kevin Hilman8111b222009-04-28 15:27:44 -0700765 OMAP3430_AUTO_MAD2D |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700766 OMAP3430ES2_AUTO_USBTLL,
767 CORE_MOD, CM_AUTOIDLE3);
768 }
769
770 cm_write_mod_reg(
771 OMAP3430_AUTO_WDT2 |
772 OMAP3430_AUTO_WDT1 |
773 OMAP3430_AUTO_GPIO1 |
774 OMAP3430_AUTO_32KSYNC |
775 OMAP3430_AUTO_GPT12 |
776 OMAP3430_AUTO_GPT1 ,
777 WKUP_MOD, CM_AUTOIDLE);
778
779 cm_write_mod_reg(
780 OMAP3430_AUTO_DSS,
781 OMAP3430_DSS_MOD,
782 CM_AUTOIDLE);
783
784 cm_write_mod_reg(
785 OMAP3430_AUTO_CAM,
786 OMAP3430_CAM_MOD,
787 CM_AUTOIDLE);
788
789 cm_write_mod_reg(
790 OMAP3430_AUTO_GPIO6 |
791 OMAP3430_AUTO_GPIO5 |
792 OMAP3430_AUTO_GPIO4 |
793 OMAP3430_AUTO_GPIO3 |
794 OMAP3430_AUTO_GPIO2 |
795 OMAP3430_AUTO_WDT3 |
796 OMAP3430_AUTO_UART3 |
797 OMAP3430_AUTO_GPT9 |
798 OMAP3430_AUTO_GPT8 |
799 OMAP3430_AUTO_GPT7 |
800 OMAP3430_AUTO_GPT6 |
801 OMAP3430_AUTO_GPT5 |
802 OMAP3430_AUTO_GPT4 |
803 OMAP3430_AUTO_GPT3 |
804 OMAP3430_AUTO_GPT2 |
805 OMAP3430_AUTO_MCBSP4 |
806 OMAP3430_AUTO_MCBSP3 |
807 OMAP3430_AUTO_MCBSP2,
808 OMAP3430_PER_MOD,
809 CM_AUTOIDLE);
810
811 if (omap_rev() > OMAP3430_REV_ES1_0) {
812 cm_write_mod_reg(
813 OMAP3430ES2_AUTO_USBHOST,
814 OMAP3430ES2_USBHOST_MOD,
815 CM_AUTOIDLE);
816 }
817
818 /*
819 * Set all plls to autoidle. This is needed until autoidle is
820 * enabled by clockfw
821 */
822 cm_write_mod_reg(1 << OMAP3430_AUTO_IVA2_DPLL_SHIFT,
823 OMAP3430_IVA2_MOD, CM_AUTOIDLE2);
824 cm_write_mod_reg(1 << OMAP3430_AUTO_MPU_DPLL_SHIFT,
825 MPU_MOD,
826 CM_AUTOIDLE2);
827 cm_write_mod_reg((1 << OMAP3430_AUTO_PERIPH_DPLL_SHIFT) |
828 (1 << OMAP3430_AUTO_CORE_DPLL_SHIFT),
829 PLL_MOD,
830 CM_AUTOIDLE);
831 cm_write_mod_reg(1 << OMAP3430ES2_AUTO_PERIPH2_DPLL_SHIFT,
832 PLL_MOD,
833 CM_AUTOIDLE2);
834
835 /*
836 * Enable control of expternal oscillator through
837 * sys_clkreq. In the long run clock framework should
838 * take care of this.
839 */
840 prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK,
841 1 << OMAP_AUTOEXTCLKMODE_SHIFT,
842 OMAP3430_GR_MOD,
843 OMAP3_PRM_CLKSRC_CTRL_OFFSET);
844
845 /* setup wakup source */
846 prm_write_mod_reg(OMAP3430_EN_IO | OMAP3430_EN_GPIO1 |
847 OMAP3430_EN_GPT1 | OMAP3430_EN_GPT12,
848 WKUP_MOD, PM_WKEN);
849 /* No need to write EN_IO, that is always enabled */
850 prm_write_mod_reg(OMAP3430_EN_GPIO1 | OMAP3430_EN_GPT1 |
851 OMAP3430_EN_GPT12,
852 WKUP_MOD, OMAP3430_PM_MPUGRPSEL);
853 /* For some reason IO doesn't generate wakeup event even if
854 * it is selected to mpu wakeup goup */
855 prm_write_mod_reg(OMAP3430_IO_EN | OMAP3430_WKUP_EN,
856 OCP_MOD, OMAP3_PRM_IRQENABLE_MPU_OFFSET);
Kevin Hilman1155e422008-11-25 11:48:24 -0800857
Kevin Hilmanb427f922009-10-22 14:48:13 -0700858 /* Enable wakeups in PER */
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000859 prm_write_mod_reg(OMAP3430_EN_GPIO2 | OMAP3430_EN_GPIO3 |
860 OMAP3430_EN_GPIO4 | OMAP3430_EN_GPIO5 |
Kevin Hilmanb427f922009-10-22 14:48:13 -0700861 OMAP3430_EN_GPIO6 | OMAP3430_EN_UART3,
862 OMAP3430_PER_MOD, PM_WKEN);
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000863 /* and allow them to wake up MPU */
864 prm_write_mod_reg(OMAP3430_GRPSEL_GPIO2 | OMAP3430_EN_GPIO3 |
865 OMAP3430_GRPSEL_GPIO4 | OMAP3430_EN_GPIO5 |
Kevin Hilmanb427f922009-10-22 14:48:13 -0700866 OMAP3430_GRPSEL_GPIO6 | OMAP3430_EN_UART3,
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000867 OMAP3430_PER_MOD, OMAP3430_PM_MPUGRPSEL);
868
Kevin Hilmand3fd3292009-05-05 16:34:25 -0700869 /* Don't attach IVA interrupts */
870 prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
871 prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
872 prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
873 prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
874
Kevin Hilmanb1340d12009-04-27 16:14:54 -0700875 /* Clear any pending 'reset' flags */
876 prm_write_mod_reg(0xffffffff, MPU_MOD, RM_RSTST);
877 prm_write_mod_reg(0xffffffff, CORE_MOD, RM_RSTST);
878 prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, RM_RSTST);
879 prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, RM_RSTST);
880 prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, RM_RSTST);
881 prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, RM_RSTST);
882 prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, RM_RSTST);
883
Kevin Hilman014c46d2009-04-27 07:50:23 -0700884 /* Clear any pending PRCM interrupts */
885 prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
886
Kevin Hilman040fed02009-05-05 16:34:25 -0700887 /* Don't attach IVA interrupts */
888 prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
889 prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
890 prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
891 prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
892
Kevin Hilman3a07ae32009-04-27 16:14:54 -0700893 /* Clear any pending 'reset' flags */
894 prm_write_mod_reg(0xffffffff, MPU_MOD, RM_RSTST);
895 prm_write_mod_reg(0xffffffff, CORE_MOD, RM_RSTST);
896 prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, RM_RSTST);
897 prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, RM_RSTST);
898 prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, RM_RSTST);
899 prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, RM_RSTST);
900 prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, RM_RSTST);
901
Kevin Hilman3a6667a2009-04-27 07:50:23 -0700902 /* Clear any pending PRCM interrupts */
903 prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
904
Kevin Hilman1155e422008-11-25 11:48:24 -0800905 omap3_iva_idle();
Kevin Hilman8111b222009-04-28 15:27:44 -0700906 omap3_d2d_idle();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700907}
908
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700909void omap3_pm_off_mode_enable(int enable)
910{
911 struct power_state *pwrst;
912 u32 state;
913
914 if (enable)
915 state = PWRDM_POWER_OFF;
916 else
917 state = PWRDM_POWER_RET;
918
919 list_for_each_entry(pwrst, &pwrst_list, node) {
920 pwrst->next_state = state;
921 set_pwrdm_state(pwrst->pwrdm, state);
922 }
923}
924
Tero Kristo68d47782008-11-26 12:26:24 +0200925int omap3_pm_get_suspend_state(struct powerdomain *pwrdm)
926{
927 struct power_state *pwrst;
928
929 list_for_each_entry(pwrst, &pwrst_list, node) {
930 if (pwrst->pwrdm == pwrdm)
931 return pwrst->next_state;
932 }
933 return -EINVAL;
934}
935
936int omap3_pm_set_suspend_state(struct powerdomain *pwrdm, int state)
937{
938 struct power_state *pwrst;
939
940 list_for_each_entry(pwrst, &pwrst_list, node) {
941 if (pwrst->pwrdm == pwrdm) {
942 pwrst->next_state = state;
943 return 0;
944 }
945 }
946 return -EINVAL;
947}
948
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300949static int __init pwrdms_setup(struct powerdomain *pwrdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700950{
951 struct power_state *pwrst;
952
953 if (!pwrdm->pwrsts)
954 return 0;
955
Ming Leid3d381c2009-08-22 21:20:26 +0800956 pwrst = kmalloc(sizeof(struct power_state), GFP_ATOMIC);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700957 if (!pwrst)
958 return -ENOMEM;
959 pwrst->pwrdm = pwrdm;
960 pwrst->next_state = PWRDM_POWER_RET;
961 list_add(&pwrst->node, &pwrst_list);
962
963 if (pwrdm_has_hdwr_sar(pwrdm))
964 pwrdm_enable_hdwr_sar(pwrdm);
965
966 return set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
967}
968
969/*
970 * Enable hw supervised mode for all clockdomains if it's
971 * supported. Initiate sleep transition for other clockdomains, if
972 * they are not used
973 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300974static int __init clkdms_setup(struct clockdomain *clkdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700975{
976 if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
977 omap2_clkdm_allow_idle(clkdm);
978 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
979 atomic_read(&clkdm->usecount) == 0)
980 omap2_clkdm_sleep(clkdm);
981 return 0;
982}
983
Rajendra Nayak3231fc82008-09-26 17:49:14 +0530984void omap_push_sram_idle(void)
985{
986 _omap_sram_idle = omap_sram_push(omap34xx_cpu_suspend,
987 omap34xx_cpu_suspend_sz);
Tero Kristo27d59a42008-10-13 13:15:00 +0300988 if (omap_type() != OMAP2_DEVICE_TYPE_GP)
989 _omap_save_secure_sram = omap_sram_push(save_secure_ram_context,
990 save_secure_ram_context_sz);
Rajendra Nayak3231fc82008-09-26 17:49:14 +0530991}
992
Kevin Hilman7cc515f2009-06-10 09:02:25 -0700993static int __init omap3_pm_init(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700994{
995 struct power_state *pwrst, *tmp;
996 int ret;
997
998 if (!cpu_is_omap34xx())
999 return -ENODEV;
1000
1001 printk(KERN_ERR "Power Management for TI OMAP3.\n");
1002
1003 /* XXX prcm_setup_regs needs to be before enabling hw
1004 * supervised mode for powerdomains */
1005 prcm_setup_regs();
1006
1007 ret = request_irq(INT_34XX_PRCM_MPU_IRQ,
1008 (irq_handler_t)prcm_interrupt_handler,
1009 IRQF_DISABLED, "prcm", NULL);
1010 if (ret) {
1011 printk(KERN_ERR "request_irq failed to register for 0x%x\n",
1012 INT_34XX_PRCM_MPU_IRQ);
1013 goto err1;
1014 }
1015
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001016 ret = pwrdm_for_each(pwrdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001017 if (ret) {
1018 printk(KERN_ERR "Failed to setup powerdomains\n");
1019 goto err2;
1020 }
1021
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001022 (void) clkdm_for_each(clkdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001023
1024 mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
1025 if (mpu_pwrdm == NULL) {
1026 printk(KERN_ERR "Failed to get mpu_pwrdm\n");
1027 goto err2;
1028 }
1029
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301030 neon_pwrdm = pwrdm_lookup("neon_pwrdm");
1031 per_pwrdm = pwrdm_lookup("per_pwrdm");
1032 core_pwrdm = pwrdm_lookup("core_pwrdm");
1033
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301034 omap_push_sram_idle();
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001035#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -07001036 suspend_set_ops(&omap_pm_ops);
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001037#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -07001038
1039 pm_idle = omap3_pm_idle;
1040
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301041 pwrdm_add_wkdep(neon_pwrdm, mpu_pwrdm);
1042 /*
1043 * REVISIT: This wkdep is only necessary when GPIO2-6 are enabled for
1044 * IO-pad wakeup. Otherwise it will unnecessarily waste power
1045 * waking up PER with every CORE wakeup - see
1046 * http://marc.info/?l=linux-omap&m=121852150710062&w=2
1047 */
1048 pwrdm_add_wkdep(per_pwrdm, core_pwrdm);
1049
Tero Kristo27d59a42008-10-13 13:15:00 +03001050 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
1051 omap3_secure_ram_storage =
1052 kmalloc(0x803F, GFP_KERNEL);
1053 if (!omap3_secure_ram_storage)
1054 printk(KERN_ERR "Memory allocation failed when"
1055 "allocating for secure sram context\n");
Tero Kristo27d59a42008-10-13 13:15:00 +03001056
Tero Kristo9d971402008-12-12 11:20:05 +02001057 local_irq_disable();
1058 local_fiq_disable();
1059
1060 omap_dma_global_context_save();
1061 omap3_save_secure_ram_context(PWRDM_POWER_ON);
1062 omap_dma_global_context_restore();
1063
1064 local_irq_enable();
1065 local_fiq_enable();
1066 }
1067
1068 omap3_save_scratchpad_contents();
Kevin Hilman8bd22942009-05-28 10:56:16 -07001069err1:
1070 return ret;
1071err2:
1072 free_irq(INT_34XX_PRCM_MPU_IRQ, NULL);
1073 list_for_each_entry_safe(pwrst, tmp, &pwrst_list, node) {
1074 list_del(&pwrst->node);
1075 kfree(pwrst);
1076 }
1077 return ret;
1078}
1079
1080late_initcall(omap3_pm_init);