blob: af83555795ff98612cde080641d3018284772a73 [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 Hilmand7814e42009-10-06 14:30:23 -070039#include <plat/dmtimer.h>
Kevin Hilman8bd22942009-05-28 10:56:16 -070040
Rajendra Nayak57f277b2008-09-26 17:49:34 +053041#include <asm/tlbflush.h>
42
Kevin Hilman8bd22942009-05-28 10:56:16 -070043#include "cm.h"
44#include "cm-regbits-34xx.h"
45#include "prm-regbits-34xx.h"
46
47#include "prm.h"
48#include "pm.h"
Tero Kristo13a6fe0f2008-10-13 13:17:06 +030049#include "sdrc.h"
50
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053051/* Scratchpad offsets */
52#define OMAP343X_TABLE_ADDRESS_OFFSET 0x31
53#define OMAP343X_TABLE_VALUE_OFFSET 0x30
54#define OMAP343X_CONTROL_REG_VALUE_OFFSET 0x32
55
Kevin Hilmanc40552b2009-10-06 14:25:09 -070056u32 enable_off_mode;
57u32 sleep_while_idle;
Kevin Hilmand7814e42009-10-06 14:30:23 -070058u32 wakeup_timer_seconds;
Kevin Hilmanc40552b2009-10-06 14:25:09 -070059
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;
Tero Kristoc16c3f62008-12-11 16:46:57 +020077static struct powerdomain *cam_pwrdm;
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +053078
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053079static inline void omap3_per_save_context(void)
80{
81 omap_gpio_save_context();
82}
83
84static inline void omap3_per_restore_context(void)
85{
86 omap_gpio_restore_context();
87}
88
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +020089static void omap3_enable_io_chain(void)
90{
91 int timeout = 0;
92
93 if (omap_rev() >= OMAP3430_REV_ES3_1) {
94 prm_set_mod_reg_bits(OMAP3430_EN_IO_CHAIN, WKUP_MOD, PM_WKEN);
95 /* Do a readback to assure write has been done */
96 prm_read_mod_reg(WKUP_MOD, PM_WKEN);
97
98 while (!(prm_read_mod_reg(WKUP_MOD, PM_WKST) &
99 OMAP3430_ST_IO_CHAIN)) {
100 timeout++;
101 if (timeout > 1000) {
102 printk(KERN_ERR "Wake up daisy chain "
103 "activation failed.\n");
104 return;
105 }
106 prm_set_mod_reg_bits(OMAP3430_ST_IO_CHAIN,
107 WKUP_MOD, PM_WKST);
108 }
109 }
110}
111
112static void omap3_disable_io_chain(void)
113{
114 if (omap_rev() >= OMAP3430_REV_ES3_1)
115 prm_clear_mod_reg_bits(OMAP3430_EN_IO_CHAIN, WKUP_MOD, PM_WKEN);
116}
117
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530118static void omap3_core_save_context(void)
119{
120 u32 control_padconf_off;
121
122 /* Save the padconf registers */
123 control_padconf_off = omap_ctrl_readl(OMAP343X_CONTROL_PADCONF_OFF);
124 control_padconf_off |= START_PADCONF_SAVE;
125 omap_ctrl_writel(control_padconf_off, OMAP343X_CONTROL_PADCONF_OFF);
126 /* wait for the save to complete */
Roel Kluin1b6e8212010-01-08 10:29:07 -0800127 while (!(omap_ctrl_readl(OMAP343X_CONTROL_GENERAL_PURPOSE_STATUS)
128 & PADCONF_SAVE_DONE))
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530129 ;
130 /* Save the Interrupt controller context */
131 omap_intc_save_context();
132 /* Save the GPMC context */
133 omap3_gpmc_save_context();
134 /* Save the system control module context, padconf already save above*/
135 omap3_control_save_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000136 omap_dma_global_context_save();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530137}
138
139static void omap3_core_restore_context(void)
140{
141 /* Restore the control module context, padconf restored by h/w */
142 omap3_control_restore_context();
143 /* Restore the GPMC context */
144 omap3_gpmc_restore_context();
145 /* Restore the interrupt controller context */
146 omap_intc_restore_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000147 omap_dma_global_context_restore();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530148}
149
Tero Kristo9d971402008-12-12 11:20:05 +0200150/*
151 * FIXME: This function should be called before entering off-mode after
152 * OMAP3 secure services have been accessed. Currently it is only called
153 * once during boot sequence, but this works as we are not using secure
154 * services.
155 */
Tero Kristo27d59a42008-10-13 13:15:00 +0300156static void omap3_save_secure_ram_context(u32 target_mpu_state)
157{
158 u32 ret;
159
160 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
Tero Kristo27d59a42008-10-13 13:15:00 +0300161 /*
162 * MPU next state must be set to POWER_ON temporarily,
163 * otherwise the WFI executed inside the ROM code
164 * will hang the system.
165 */
166 pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_ON);
167 ret = _omap_save_secure_sram((u32 *)
168 __pa(omap3_secure_ram_storage));
169 pwrdm_set_next_pwrst(mpu_pwrdm, target_mpu_state);
170 /* Following is for error tracking, it should not happen */
171 if (ret) {
172 printk(KERN_ERR "save_secure_sram() returns %08x\n",
173 ret);
174 while (1)
175 ;
176 }
177 }
178}
179
Jon Hunter77da2d92009-06-27 00:07:25 -0500180/*
181 * PRCM Interrupt Handler Helper Function
182 *
183 * The purpose of this function is to clear any wake-up events latched
184 * in the PRCM PM_WKST_x registers. It is possible that a wake-up event
185 * may occur whilst attempting to clear a PM_WKST_x register and thus
186 * set another bit in this register. A while loop is used to ensure
187 * that any peripheral wake-up events occurring while attempting to
188 * clear the PM_WKST_x are detected and cleared.
189 */
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700190static int prcm_clear_mod_irqs(s16 module, u8 regs)
Jon Hunter77da2d92009-06-27 00:07:25 -0500191{
Vikram Pandita71a80772009-07-17 19:33:09 -0500192 u32 wkst, fclk, iclk, clken;
Jon Hunter77da2d92009-06-27 00:07:25 -0500193 u16 wkst_off = (regs == 3) ? OMAP3430ES2_PM_WKST3 : PM_WKST1;
194 u16 fclk_off = (regs == 3) ? OMAP3430ES2_CM_FCLKEN3 : CM_FCLKEN1;
195 u16 iclk_off = (regs == 3) ? CM_ICLKEN3 : CM_ICLKEN1;
Paul Walmsley5d805972009-07-22 10:18:07 -0700196 u16 grpsel_off = (regs == 3) ?
197 OMAP3430ES2_PM_MPUGRPSEL3 : OMAP3430_PM_MPUGRPSEL;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700198 int c = 0;
Jon Hunter77da2d92009-06-27 00:07:25 -0500199
200 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley5d805972009-07-22 10:18:07 -0700201 wkst &= prm_read_mod_reg(module, grpsel_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500202 if (wkst) {
203 iclk = cm_read_mod_reg(module, iclk_off);
204 fclk = cm_read_mod_reg(module, fclk_off);
205 while (wkst) {
Vikram Pandita71a80772009-07-17 19:33:09 -0500206 clken = wkst;
207 cm_set_mod_reg_bits(clken, module, iclk_off);
208 /*
209 * For USBHOST, we don't know whether HOST1 or
210 * HOST2 woke us up, so enable both f-clocks
211 */
212 if (module == OMAP3430ES2_USBHOST_MOD)
213 clken |= 1 << OMAP3430ES2_EN_USBHOST2_SHIFT;
214 cm_set_mod_reg_bits(clken, module, fclk_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500215 prm_write_mod_reg(wkst, module, wkst_off);
216 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700217 c++;
Jon Hunter77da2d92009-06-27 00:07:25 -0500218 }
219 cm_write_mod_reg(iclk, module, iclk_off);
220 cm_write_mod_reg(fclk, module, fclk_off);
221 }
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700222
223 return c;
224}
225
226static int _prcm_int_handle_wakeup(void)
227{
228 int c;
229
230 c = prcm_clear_mod_irqs(WKUP_MOD, 1);
231 c += prcm_clear_mod_irqs(CORE_MOD, 1);
232 c += prcm_clear_mod_irqs(OMAP3430_PER_MOD, 1);
233 if (omap_rev() > OMAP3430_REV_ES1_0) {
234 c += prcm_clear_mod_irqs(CORE_MOD, 3);
235 c += prcm_clear_mod_irqs(OMAP3430ES2_USBHOST_MOD, 1);
236 }
237
238 return c;
Jon Hunter77da2d92009-06-27 00:07:25 -0500239}
240
241/*
242 * PRCM Interrupt Handler
243 *
244 * The PRM_IRQSTATUS_MPU register indicates if there are any pending
245 * interrupts from the PRCM for the MPU. These bits must be cleared in
246 * order to clear the PRCM interrupt. The PRCM interrupt handler is
247 * implemented to simply clear the PRM_IRQSTATUS_MPU in order to clear
248 * the PRCM interrupt. Please note that bit 0 of the PRM_IRQSTATUS_MPU
249 * register indicates that a wake-up event is pending for the MPU and
250 * this bit can only be cleared if the all the wake-up events latched
251 * in the various PM_WKST_x registers have been cleared. The interrupt
252 * handler is implemented using a do-while loop so that if a wake-up
253 * event occurred during the processing of the prcm interrupt handler
254 * (setting a bit in the corresponding PM_WKST_x register and thus
255 * preventing us from clearing bit 0 of the PRM_IRQSTATUS_MPU register)
256 * this would be handled.
257 */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700258static irqreturn_t prcm_interrupt_handler (int irq, void *dev_id)
259{
Jon Hunter77da2d92009-06-27 00:07:25 -0500260 u32 irqstatus_mpu;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700261 int c = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700262
Jon Hunter77da2d92009-06-27 00:07:25 -0500263 do {
Jon Hunter77da2d92009-06-27 00:07:25 -0500264 irqstatus_mpu = prm_read_mod_reg(OCP_MOD,
265 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700266
267 if (irqstatus_mpu & (OMAP3430_WKUP_ST | OMAP3430_IO_ST)) {
268 c = _prcm_int_handle_wakeup();
269
270 /*
271 * Is the MPU PRCM interrupt handler racing with the
272 * IVA2 PRCM interrupt handler ?
273 */
274 WARN(c == 0, "prcm: WARNING: PRCM indicated MPU wakeup "
275 "but no wakeup sources are marked\n");
276 } else {
277 /* XXX we need to expand our PRCM interrupt handler */
278 WARN(1, "prcm: WARNING: PRCM interrupt received, but "
279 "no code to handle it (%08x)\n", irqstatus_mpu);
280 }
281
Jon Hunter77da2d92009-06-27 00:07:25 -0500282 prm_write_mod_reg(irqstatus_mpu, OCP_MOD,
283 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700284
Jon Hunter77da2d92009-06-27 00:07:25 -0500285 } while (prm_read_mod_reg(OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET));
Kevin Hilman8bd22942009-05-28 10:56:16 -0700286
287 return IRQ_HANDLED;
288}
289
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530290static void restore_control_register(u32 val)
291{
292 __asm__ __volatile__ ("mcr p15, 0, %0, c1, c0, 0" : : "r" (val));
293}
294
295/* Function to restore the table entry that was modified for enabling MMU */
296static void restore_table_entry(void)
297{
298 u32 *scratchpad_address;
299 u32 previous_value, control_reg_value;
300 u32 *address;
301
302 scratchpad_address = OMAP2_L4_IO_ADDRESS(OMAP343X_SCRATCHPAD);
303
304 /* Get address of entry that was modified */
305 address = (u32 *)__raw_readl(scratchpad_address +
306 OMAP343X_TABLE_ADDRESS_OFFSET);
307 /* Get the previous value which needs to be restored */
308 previous_value = __raw_readl(scratchpad_address +
309 OMAP343X_TABLE_VALUE_OFFSET);
310 address = __va(address);
311 *address = previous_value;
312 flush_tlb_all();
313 control_reg_value = __raw_readl(scratchpad_address
314 + OMAP343X_CONTROL_REG_VALUE_OFFSET);
315 /* This will enable caches and prediction */
316 restore_control_register(control_reg_value);
317}
318
Rajendra Nayak99e6a4d2008-10-08 17:30:58 +0530319void omap_sram_idle(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700320{
321 /* Variable to tell what needs to be saved and restored
322 * in omap_sram_idle*/
323 /* save_state = 0 => Nothing to save and restored */
324 /* save_state = 1 => Only L1 and logic lost */
325 /* save_state = 2 => Only L2 lost */
326 /* save_state = 3 => L1, L2 and logic lost */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530327 int save_state = 0;
328 int mpu_next_state = PWRDM_POWER_ON;
329 int per_next_state = PWRDM_POWER_ON;
330 int core_next_state = PWRDM_POWER_ON;
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530331 int core_prev_state, per_prev_state;
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300332 u32 sdrc_pwr = 0;
Tero Kristoecf157d2008-12-01 13:17:29 +0200333 int per_state_modified = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700334
335 if (!_omap_sram_idle)
336 return;
337
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530338 pwrdm_clear_all_prev_pwrst(mpu_pwrdm);
339 pwrdm_clear_all_prev_pwrst(neon_pwrdm);
340 pwrdm_clear_all_prev_pwrst(core_pwrdm);
341 pwrdm_clear_all_prev_pwrst(per_pwrdm);
342
Kevin Hilman8bd22942009-05-28 10:56:16 -0700343 mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
344 switch (mpu_next_state) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530345 case PWRDM_POWER_ON:
Kevin Hilman8bd22942009-05-28 10:56:16 -0700346 case PWRDM_POWER_RET:
347 /* No need to save context */
348 save_state = 0;
349 break;
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530350 case PWRDM_POWER_OFF:
351 save_state = 3;
352 break;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700353 default:
354 /* Invalid state */
355 printk(KERN_ERR "Invalid mpu state in sram_idle\n");
356 return;
357 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300358 pwrdm_pre_transition();
359
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530360 /* NEON control */
361 if (pwrdm_read_pwrst(neon_pwrdm) == PWRDM_POWER_ON)
Jouni Hogander71391782008-10-28 10:59:05 +0200362 pwrdm_set_next_pwrst(neon_pwrdm, mpu_next_state);
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530363
Kevin Hilman658ce972008-11-04 20:50:52 -0800364 /* PER */
365 per_next_state = pwrdm_read_next_pwrst(per_pwrdm);
Tero Kristoecf157d2008-12-01 13:17:29 +0200366 core_next_state = pwrdm_read_next_pwrst(core_pwrdm);
Kevin Hilman658ce972008-11-04 20:50:52 -0800367 if (per_next_state < PWRDM_POWER_ON) {
Kevin Hilman658ce972008-11-04 20:50:52 -0800368 omap_uart_prepare_idle(2);
Tero Kristoecf157d2008-12-01 13:17:29 +0200369 omap2_gpio_prepare_for_retention();
370 if (per_next_state == PWRDM_POWER_OFF) {
371 if (core_next_state == PWRDM_POWER_ON) {
372 per_next_state = PWRDM_POWER_RET;
373 pwrdm_set_next_pwrst(per_pwrdm, per_next_state);
374 per_state_modified = 1;
375 } else
376 omap3_per_save_context();
377 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800378 }
379
Tero Kristoc16c3f62008-12-11 16:46:57 +0200380 if (pwrdm_read_pwrst(cam_pwrdm) == PWRDM_POWER_ON)
381 omap2_clkdm_deny_idle(mpu_pwrdm->pwrdm_clkdms[0]);
382
Kevin Hilman658ce972008-11-04 20:50:52 -0800383 /* CORE */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530384 if (core_next_state < PWRDM_POWER_ON) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530385 omap_uart_prepare_idle(0);
386 omap_uart_prepare_idle(1);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530387 if (core_next_state == PWRDM_POWER_OFF) {
388 omap3_core_save_context();
389 omap3_prcm_save_context();
390 }
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200391 /* Enable IO-PAD and IO-CHAIN wakeups */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530392 prm_set_mod_reg_bits(OMAP3430_EN_IO, WKUP_MOD, PM_WKEN);
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200393 omap3_enable_io_chain();
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530394 }
Tero Kristof18cc2f2009-10-23 19:03:50 +0300395 omap3_intc_prepare_idle();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700396
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530397 /*
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530398 * On EMU/HS devices ROM code restores a SRDC value
399 * from scratchpad which has automatic self refresh on timeout
400 * of AUTO_CNT = 1 enabled. This takes care of errata 1.142.
401 * Hence store/restore the SDRC_POWER register here.
402 */
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300403 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
404 omap_type() != OMAP2_DEVICE_TYPE_GP &&
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530405 core_next_state == PWRDM_POWER_OFF)
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300406 sdrc_pwr = sdrc_read_reg(SDRC_POWER);
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300407
408 /*
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530409 * omap3_arm_context is the location where ARM registers
410 * get saved. The restore path then reads from this
411 * location and restores them back.
412 */
413 _omap_sram_idle(omap3_arm_context, save_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700414 cpu_init();
415
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530416 /* Restore normal SDRC POWER settings */
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300417 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
418 omap_type() != OMAP2_DEVICE_TYPE_GP &&
419 core_next_state == PWRDM_POWER_OFF)
420 sdrc_write_reg(sdrc_pwr, SDRC_POWER);
421
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530422 /* Restore table entry modified during MMU restoration */
423 if (pwrdm_read_prev_pwrst(mpu_pwrdm) == PWRDM_POWER_OFF)
424 restore_table_entry();
425
Kevin Hilman658ce972008-11-04 20:50:52 -0800426 /* CORE */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530427 if (core_next_state < PWRDM_POWER_ON) {
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530428 core_prev_state = pwrdm_read_prev_pwrst(core_pwrdm);
429 if (core_prev_state == PWRDM_POWER_OFF) {
430 omap3_core_restore_context();
431 omap3_prcm_restore_context();
432 omap3_sram_restore_context();
Kalle Jokiniemi8a917d22009-05-13 13:32:11 +0300433 omap2_sms_restore_context();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530434 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800435 omap_uart_resume_idle(0);
436 omap_uart_resume_idle(1);
437 if (core_next_state == PWRDM_POWER_OFF)
438 prm_clear_mod_reg_bits(OMAP3430_AUTO_OFF,
439 OMAP3430_GR_MOD,
440 OMAP3_PRM_VOLTCTRL_OFFSET);
441 }
Tero Kristof18cc2f2009-10-23 19:03:50 +0300442 omap3_intc_resume_idle();
Kevin Hilman658ce972008-11-04 20:50:52 -0800443
444 /* PER */
445 if (per_next_state < PWRDM_POWER_ON) {
446 per_prev_state = pwrdm_read_prev_pwrst(per_pwrdm);
Kevin Hilman658ce972008-11-04 20:50:52 -0800447 if (per_prev_state == PWRDM_POWER_OFF)
448 omap3_per_restore_context();
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530449 omap2_gpio_resume_after_retention();
Tero Kristoecf157d2008-12-01 13:17:29 +0200450 omap_uart_resume_idle(2);
451 if (per_state_modified)
452 pwrdm_set_next_pwrst(per_pwrdm, PWRDM_POWER_OFF);
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530453 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300454
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200455 /* Disable IO-PAD and IO-CHAIN wakeup */
456 if (core_next_state < PWRDM_POWER_ON) {
Kevin Hilman658ce972008-11-04 20:50:52 -0800457 prm_clear_mod_reg_bits(OMAP3430_EN_IO, WKUP_MOD, PM_WKEN);
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200458 omap3_disable_io_chain();
459 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800460
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300461 pwrdm_post_transition();
462
Tero Kristoc16c3f62008-12-11 16:46:57 +0200463 omap2_clkdm_allow_idle(mpu_pwrdm->pwrdm_clkdms[0]);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700464}
465
Rajendra Nayak20b01662008-10-08 17:31:22 +0530466int omap3_can_sleep(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700467{
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700468 if (!sleep_while_idle)
469 return 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800470 if (!omap_uart_can_sleep())
471 return 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700472 return 1;
473}
474
475/* This sets pwrdm state (other than mpu & core. Currently only ON &
476 * RET are supported. Function is assuming that clkdm doesn't have
477 * hw_sup mode enabled. */
Rajendra Nayak20b01662008-10-08 17:31:22 +0530478int set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700479{
480 u32 cur_state;
481 int sleep_switch = 0;
482 int ret = 0;
483
484 if (pwrdm == NULL || IS_ERR(pwrdm))
485 return -EINVAL;
486
487 while (!(pwrdm->pwrsts & (1 << state))) {
488 if (state == PWRDM_POWER_OFF)
489 return ret;
490 state--;
491 }
492
493 cur_state = pwrdm_read_next_pwrst(pwrdm);
494 if (cur_state == state)
495 return ret;
496
497 if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
498 omap2_clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
499 sleep_switch = 1;
500 pwrdm_wait_transition(pwrdm);
501 }
502
503 ret = pwrdm_set_next_pwrst(pwrdm, state);
504 if (ret) {
505 printk(KERN_ERR "Unable to set state of powerdomain: %s\n",
506 pwrdm->name);
507 goto err;
508 }
509
510 if (sleep_switch) {
511 omap2_clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
512 pwrdm_wait_transition(pwrdm);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300513 pwrdm_state_switch(pwrdm);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700514 }
515
516err:
517 return ret;
518}
519
520static void omap3_pm_idle(void)
521{
522 local_irq_disable();
523 local_fiq_disable();
524
525 if (!omap3_can_sleep())
526 goto out;
527
Tero Kristocf228542009-03-20 15:21:02 +0200528 if (omap_irq_pending() || need_resched())
Kevin Hilman8bd22942009-05-28 10:56:16 -0700529 goto out;
530
531 omap_sram_idle();
532
533out:
534 local_fiq_enable();
535 local_irq_enable();
536}
537
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700538#ifdef CONFIG_SUSPEND
Tero Kristo24662112009-03-05 16:32:23 +0200539static suspend_state_t suspend_state;
540
Kevin Hilmand7814e42009-10-06 14:30:23 -0700541static void omap2_pm_wakeup_on_timer(u32 seconds)
542{
543 u32 tick_rate, cycles;
544
545 if (!seconds)
546 return;
547
548 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gptimer_wakeup));
549 cycles = tick_rate * seconds;
550 omap_dm_timer_stop(gptimer_wakeup);
551 omap_dm_timer_set_load_start(gptimer_wakeup, 0, 0xffffffff - cycles);
552
553 pr_info("PM: Resume timer in %d secs (%d ticks at %d ticks/sec.)\n",
554 seconds, cycles, tick_rate);
555}
556
Kevin Hilman8bd22942009-05-28 10:56:16 -0700557static int omap3_pm_prepare(void)
558{
559 disable_hlt();
560 return 0;
561}
562
563static int omap3_pm_suspend(void)
564{
565 struct power_state *pwrst;
566 int state, ret = 0;
567
Kevin Hilmand7814e42009-10-06 14:30:23 -0700568 if (wakeup_timer_seconds)
569 omap2_pm_wakeup_on_timer(wakeup_timer_seconds);
570
Kevin Hilman8bd22942009-05-28 10:56:16 -0700571 /* Read current next_pwrsts */
572 list_for_each_entry(pwrst, &pwrst_list, node)
573 pwrst->saved_state = pwrdm_read_next_pwrst(pwrst->pwrdm);
574 /* Set ones wanted by suspend */
575 list_for_each_entry(pwrst, &pwrst_list, node) {
576 if (set_pwrdm_state(pwrst->pwrdm, pwrst->next_state))
577 goto restore;
578 if (pwrdm_clear_all_prev_pwrst(pwrst->pwrdm))
579 goto restore;
580 }
581
Kevin Hilman4af40162009-02-04 10:51:40 -0800582 omap_uart_prepare_suspend();
Tero Kristo2bbe3af2009-10-23 19:03:48 +0300583 omap3_intc_suspend();
584
Kevin Hilman8bd22942009-05-28 10:56:16 -0700585 omap_sram_idle();
586
587restore:
588 /* Restore next_pwrsts */
589 list_for_each_entry(pwrst, &pwrst_list, node) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700590 state = pwrdm_read_prev_pwrst(pwrst->pwrdm);
591 if (state > pwrst->next_state) {
592 printk(KERN_INFO "Powerdomain (%s) didn't enter "
593 "target state %d\n",
594 pwrst->pwrdm->name, pwrst->next_state);
595 ret = -1;
596 }
Jouni Hogander6c5f8032008-10-29 12:06:04 +0200597 set_pwrdm_state(pwrst->pwrdm, pwrst->saved_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700598 }
599 if (ret)
600 printk(KERN_ERR "Could not enter target state in pm_suspend\n");
601 else
602 printk(KERN_INFO "Successfully put all powerdomains "
603 "to target state\n");
604
605 return ret;
606}
607
Tero Kristo24662112009-03-05 16:32:23 +0200608static int omap3_pm_enter(suspend_state_t unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700609{
610 int ret = 0;
611
Tero Kristo24662112009-03-05 16:32:23 +0200612 switch (suspend_state) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700613 case PM_SUSPEND_STANDBY:
614 case PM_SUSPEND_MEM:
615 ret = omap3_pm_suspend();
616 break;
617 default:
618 ret = -EINVAL;
619 }
620
621 return ret;
622}
623
624static void omap3_pm_finish(void)
625{
626 enable_hlt();
627}
628
Tero Kristo24662112009-03-05 16:32:23 +0200629/* Hooks to enable / disable UART interrupts during suspend */
630static int omap3_pm_begin(suspend_state_t state)
631{
632 suspend_state = state;
633 omap_uart_enable_irqs(0);
634 return 0;
635}
636
637static void omap3_pm_end(void)
638{
639 suspend_state = PM_SUSPEND_ON;
640 omap_uart_enable_irqs(1);
641 return;
642}
643
Kevin Hilman8bd22942009-05-28 10:56:16 -0700644static struct platform_suspend_ops omap_pm_ops = {
Tero Kristo24662112009-03-05 16:32:23 +0200645 .begin = omap3_pm_begin,
646 .end = omap3_pm_end,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700647 .prepare = omap3_pm_prepare,
648 .enter = omap3_pm_enter,
649 .finish = omap3_pm_finish,
650 .valid = suspend_valid_only_mem,
651};
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700652#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700653
Kevin Hilman1155e422008-11-25 11:48:24 -0800654
655/**
656 * omap3_iva_idle(): ensure IVA is in idle so it can be put into
657 * retention
658 *
659 * In cases where IVA2 is activated by bootcode, it may prevent
660 * full-chip retention or off-mode because it is not idle. This
661 * function forces the IVA2 into idle state so it can go
662 * into retention/off and thus allow full-chip retention/off.
663 *
664 **/
665static void __init omap3_iva_idle(void)
666{
667 /* ensure IVA2 clock is disabled */
668 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
669
670 /* if no clock activity, nothing else to do */
671 if (!(cm_read_mod_reg(OMAP3430_IVA2_MOD, OMAP3430_CM_CLKSTST) &
672 OMAP3430_CLKACTIVITY_IVA2_MASK))
673 return;
674
675 /* Reset IVA2 */
676 prm_write_mod_reg(OMAP3430_RST1_IVA2 |
677 OMAP3430_RST2_IVA2 |
678 OMAP3430_RST3_IVA2,
679 OMAP3430_IVA2_MOD, RM_RSTCTRL);
680
681 /* Enable IVA2 clock */
682 cm_write_mod_reg(OMAP3430_CM_FCLKEN_IVA2_EN_IVA2,
683 OMAP3430_IVA2_MOD, CM_FCLKEN);
684
685 /* Set IVA2 boot mode to 'idle' */
686 omap_ctrl_writel(OMAP3_IVA2_BOOTMOD_IDLE,
687 OMAP343X_CONTROL_IVA2_BOOTMOD);
688
689 /* Un-reset IVA2 */
690 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, RM_RSTCTRL);
691
692 /* Disable IVA2 clock */
693 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
694
695 /* Reset IVA2 */
696 prm_write_mod_reg(OMAP3430_RST1_IVA2 |
697 OMAP3430_RST2_IVA2 |
698 OMAP3430_RST3_IVA2,
699 OMAP3430_IVA2_MOD, RM_RSTCTRL);
700}
701
Kevin Hilman8111b222009-04-28 15:27:44 -0700702static void __init omap3_d2d_idle(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700703{
Kevin Hilman8111b222009-04-28 15:27:44 -0700704 u16 mask, padconf;
705
706 /* In a stand alone OMAP3430 where there is not a stacked
707 * modem for the D2D Idle Ack and D2D MStandby must be pulled
708 * high. S CONTROL_PADCONF_SAD2D_IDLEACK and
709 * CONTROL_PADCONF_SAD2D_MSTDBY to have a pull up. */
710 mask = (1 << 4) | (1 << 3); /* pull-up, enabled */
711 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_MSTANDBY);
712 padconf |= mask;
713 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_MSTANDBY);
714
715 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_IDLEACK);
716 padconf |= mask;
717 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_IDLEACK);
718
Kevin Hilman8bd22942009-05-28 10:56:16 -0700719 /* reset modem */
720 prm_write_mod_reg(OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RSTPWRON |
721 OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RST,
722 CORE_MOD, RM_RSTCTRL);
723 prm_write_mod_reg(0, CORE_MOD, RM_RSTCTRL);
Kevin Hilman8111b222009-04-28 15:27:44 -0700724}
Kevin Hilman8bd22942009-05-28 10:56:16 -0700725
Kevin Hilman8111b222009-04-28 15:27:44 -0700726static void __init prcm_setup_regs(void)
727{
Kevin Hilman8bd22942009-05-28 10:56:16 -0700728 /* XXX Reset all wkdeps. This should be done when initializing
729 * powerdomains */
730 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, PM_WKDEP);
731 prm_write_mod_reg(0, MPU_MOD, PM_WKDEP);
732 prm_write_mod_reg(0, OMAP3430_DSS_MOD, PM_WKDEP);
733 prm_write_mod_reg(0, OMAP3430_NEON_MOD, PM_WKDEP);
734 prm_write_mod_reg(0, OMAP3430_CAM_MOD, PM_WKDEP);
735 prm_write_mod_reg(0, OMAP3430_PER_MOD, PM_WKDEP);
736 if (omap_rev() > OMAP3430_REV_ES1_0) {
737 prm_write_mod_reg(0, OMAP3430ES2_SGX_MOD, PM_WKDEP);
738 prm_write_mod_reg(0, OMAP3430ES2_USBHOST_MOD, PM_WKDEP);
739 } else
740 prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
741
742 /*
743 * Enable interface clock autoidle for all modules.
744 * Note that in the long run this should be done by clockfw
745 */
746 cm_write_mod_reg(
Kevin Hilman8111b222009-04-28 15:27:44 -0700747 OMAP3430_AUTO_MODEM |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700748 OMAP3430ES2_AUTO_MMC3 |
749 OMAP3430ES2_AUTO_ICR |
750 OMAP3430_AUTO_AES2 |
751 OMAP3430_AUTO_SHA12 |
752 OMAP3430_AUTO_DES2 |
753 OMAP3430_AUTO_MMC2 |
754 OMAP3430_AUTO_MMC1 |
755 OMAP3430_AUTO_MSPRO |
756 OMAP3430_AUTO_HDQ |
757 OMAP3430_AUTO_MCSPI4 |
758 OMAP3430_AUTO_MCSPI3 |
759 OMAP3430_AUTO_MCSPI2 |
760 OMAP3430_AUTO_MCSPI1 |
761 OMAP3430_AUTO_I2C3 |
762 OMAP3430_AUTO_I2C2 |
763 OMAP3430_AUTO_I2C1 |
764 OMAP3430_AUTO_UART2 |
765 OMAP3430_AUTO_UART1 |
766 OMAP3430_AUTO_GPT11 |
767 OMAP3430_AUTO_GPT10 |
768 OMAP3430_AUTO_MCBSP5 |
769 OMAP3430_AUTO_MCBSP1 |
770 OMAP3430ES1_AUTO_FAC | /* This is es1 only */
771 OMAP3430_AUTO_MAILBOXES |
772 OMAP3430_AUTO_OMAPCTRL |
773 OMAP3430ES1_AUTO_FSHOSTUSB |
774 OMAP3430_AUTO_HSOTGUSB |
Kevin Hilman8111b222009-04-28 15:27:44 -0700775 OMAP3430_AUTO_SAD2D |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700776 OMAP3430_AUTO_SSI,
777 CORE_MOD, CM_AUTOIDLE1);
778
779 cm_write_mod_reg(
780 OMAP3430_AUTO_PKA |
781 OMAP3430_AUTO_AES1 |
782 OMAP3430_AUTO_RNG |
783 OMAP3430_AUTO_SHA11 |
784 OMAP3430_AUTO_DES1,
785 CORE_MOD, CM_AUTOIDLE2);
786
787 if (omap_rev() > OMAP3430_REV_ES1_0) {
788 cm_write_mod_reg(
Kevin Hilman8111b222009-04-28 15:27:44 -0700789 OMAP3430_AUTO_MAD2D |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700790 OMAP3430ES2_AUTO_USBTLL,
791 CORE_MOD, CM_AUTOIDLE3);
792 }
793
794 cm_write_mod_reg(
795 OMAP3430_AUTO_WDT2 |
796 OMAP3430_AUTO_WDT1 |
797 OMAP3430_AUTO_GPIO1 |
798 OMAP3430_AUTO_32KSYNC |
799 OMAP3430_AUTO_GPT12 |
800 OMAP3430_AUTO_GPT1 ,
801 WKUP_MOD, CM_AUTOIDLE);
802
803 cm_write_mod_reg(
804 OMAP3430_AUTO_DSS,
805 OMAP3430_DSS_MOD,
806 CM_AUTOIDLE);
807
808 cm_write_mod_reg(
809 OMAP3430_AUTO_CAM,
810 OMAP3430_CAM_MOD,
811 CM_AUTOIDLE);
812
813 cm_write_mod_reg(
814 OMAP3430_AUTO_GPIO6 |
815 OMAP3430_AUTO_GPIO5 |
816 OMAP3430_AUTO_GPIO4 |
817 OMAP3430_AUTO_GPIO3 |
818 OMAP3430_AUTO_GPIO2 |
819 OMAP3430_AUTO_WDT3 |
820 OMAP3430_AUTO_UART3 |
821 OMAP3430_AUTO_GPT9 |
822 OMAP3430_AUTO_GPT8 |
823 OMAP3430_AUTO_GPT7 |
824 OMAP3430_AUTO_GPT6 |
825 OMAP3430_AUTO_GPT5 |
826 OMAP3430_AUTO_GPT4 |
827 OMAP3430_AUTO_GPT3 |
828 OMAP3430_AUTO_GPT2 |
829 OMAP3430_AUTO_MCBSP4 |
830 OMAP3430_AUTO_MCBSP3 |
831 OMAP3430_AUTO_MCBSP2,
832 OMAP3430_PER_MOD,
833 CM_AUTOIDLE);
834
835 if (omap_rev() > OMAP3430_REV_ES1_0) {
836 cm_write_mod_reg(
837 OMAP3430ES2_AUTO_USBHOST,
838 OMAP3430ES2_USBHOST_MOD,
839 CM_AUTOIDLE);
840 }
841
Tero Kristob296c812009-10-23 19:03:49 +0300842 omap_ctrl_writel(OMAP3430_AUTOIDLE, OMAP2_CONTROL_SYSCONFIG);
843
Kevin Hilman8bd22942009-05-28 10:56:16 -0700844 /*
845 * Set all plls to autoidle. This is needed until autoidle is
846 * enabled by clockfw
847 */
848 cm_write_mod_reg(1 << OMAP3430_AUTO_IVA2_DPLL_SHIFT,
849 OMAP3430_IVA2_MOD, CM_AUTOIDLE2);
850 cm_write_mod_reg(1 << OMAP3430_AUTO_MPU_DPLL_SHIFT,
851 MPU_MOD,
852 CM_AUTOIDLE2);
853 cm_write_mod_reg((1 << OMAP3430_AUTO_PERIPH_DPLL_SHIFT) |
854 (1 << OMAP3430_AUTO_CORE_DPLL_SHIFT),
855 PLL_MOD,
856 CM_AUTOIDLE);
857 cm_write_mod_reg(1 << OMAP3430ES2_AUTO_PERIPH2_DPLL_SHIFT,
858 PLL_MOD,
859 CM_AUTOIDLE2);
860
861 /*
862 * Enable control of expternal oscillator through
863 * sys_clkreq. In the long run clock framework should
864 * take care of this.
865 */
866 prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK,
867 1 << OMAP_AUTOEXTCLKMODE_SHIFT,
868 OMAP3430_GR_MOD,
869 OMAP3_PRM_CLKSRC_CTRL_OFFSET);
870
871 /* setup wakup source */
872 prm_write_mod_reg(OMAP3430_EN_IO | OMAP3430_EN_GPIO1 |
873 OMAP3430_EN_GPT1 | OMAP3430_EN_GPT12,
874 WKUP_MOD, PM_WKEN);
875 /* No need to write EN_IO, that is always enabled */
876 prm_write_mod_reg(OMAP3430_EN_GPIO1 | OMAP3430_EN_GPT1 |
877 OMAP3430_EN_GPT12,
878 WKUP_MOD, OMAP3430_PM_MPUGRPSEL);
879 /* For some reason IO doesn't generate wakeup event even if
880 * it is selected to mpu wakeup goup */
881 prm_write_mod_reg(OMAP3430_IO_EN | OMAP3430_WKUP_EN,
882 OCP_MOD, OMAP3_PRM_IRQENABLE_MPU_OFFSET);
Kevin Hilman1155e422008-11-25 11:48:24 -0800883
Kevin Hilmanb427f922009-10-22 14:48:13 -0700884 /* Enable wakeups in PER */
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000885 prm_write_mod_reg(OMAP3430_EN_GPIO2 | OMAP3430_EN_GPIO3 |
886 OMAP3430_EN_GPIO4 | OMAP3430_EN_GPIO5 |
Kevin Hilmanb427f922009-10-22 14:48:13 -0700887 OMAP3430_EN_GPIO6 | OMAP3430_EN_UART3,
888 OMAP3430_PER_MOD, PM_WKEN);
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000889 /* and allow them to wake up MPU */
890 prm_write_mod_reg(OMAP3430_GRPSEL_GPIO2 | OMAP3430_EN_GPIO3 |
891 OMAP3430_GRPSEL_GPIO4 | OMAP3430_EN_GPIO5 |
Kevin Hilmanb427f922009-10-22 14:48:13 -0700892 OMAP3430_GRPSEL_GPIO6 | OMAP3430_EN_UART3,
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000893 OMAP3430_PER_MOD, OMAP3430_PM_MPUGRPSEL);
894
Kevin Hilmand3fd3292009-05-05 16:34:25 -0700895 /* Don't attach IVA interrupts */
896 prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
897 prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
898 prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
899 prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
900
Kevin Hilmanb1340d12009-04-27 16:14:54 -0700901 /* Clear any pending 'reset' flags */
902 prm_write_mod_reg(0xffffffff, MPU_MOD, RM_RSTST);
903 prm_write_mod_reg(0xffffffff, CORE_MOD, RM_RSTST);
904 prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, RM_RSTST);
905 prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, RM_RSTST);
906 prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, RM_RSTST);
907 prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, RM_RSTST);
908 prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, RM_RSTST);
909
Kevin Hilman014c46d2009-04-27 07:50:23 -0700910 /* Clear any pending PRCM interrupts */
911 prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
912
Kevin Hilman040fed02009-05-05 16:34:25 -0700913 /* Don't attach IVA interrupts */
914 prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
915 prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
916 prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
917 prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
918
Kevin Hilman3a07ae32009-04-27 16:14:54 -0700919 /* Clear any pending 'reset' flags */
920 prm_write_mod_reg(0xffffffff, MPU_MOD, RM_RSTST);
921 prm_write_mod_reg(0xffffffff, CORE_MOD, RM_RSTST);
922 prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, RM_RSTST);
923 prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, RM_RSTST);
924 prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, RM_RSTST);
925 prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, RM_RSTST);
926 prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, RM_RSTST);
927
Kevin Hilman3a6667a2009-04-27 07:50:23 -0700928 /* Clear any pending PRCM interrupts */
929 prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
930
Kevin Hilman1155e422008-11-25 11:48:24 -0800931 omap3_iva_idle();
Kevin Hilman8111b222009-04-28 15:27:44 -0700932 omap3_d2d_idle();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700933}
934
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700935void omap3_pm_off_mode_enable(int enable)
936{
937 struct power_state *pwrst;
938 u32 state;
939
940 if (enable)
941 state = PWRDM_POWER_OFF;
942 else
943 state = PWRDM_POWER_RET;
944
945 list_for_each_entry(pwrst, &pwrst_list, node) {
946 pwrst->next_state = state;
947 set_pwrdm_state(pwrst->pwrdm, state);
948 }
949}
950
Tero Kristo68d47782008-11-26 12:26:24 +0200951int omap3_pm_get_suspend_state(struct powerdomain *pwrdm)
952{
953 struct power_state *pwrst;
954
955 list_for_each_entry(pwrst, &pwrst_list, node) {
956 if (pwrst->pwrdm == pwrdm)
957 return pwrst->next_state;
958 }
959 return -EINVAL;
960}
961
962int omap3_pm_set_suspend_state(struct powerdomain *pwrdm, int state)
963{
964 struct power_state *pwrst;
965
966 list_for_each_entry(pwrst, &pwrst_list, node) {
967 if (pwrst->pwrdm == pwrdm) {
968 pwrst->next_state = state;
969 return 0;
970 }
971 }
972 return -EINVAL;
973}
974
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300975static int __init pwrdms_setup(struct powerdomain *pwrdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700976{
977 struct power_state *pwrst;
978
979 if (!pwrdm->pwrsts)
980 return 0;
981
Ming Leid3d381c2009-08-22 21:20:26 +0800982 pwrst = kmalloc(sizeof(struct power_state), GFP_ATOMIC);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700983 if (!pwrst)
984 return -ENOMEM;
985 pwrst->pwrdm = pwrdm;
986 pwrst->next_state = PWRDM_POWER_RET;
987 list_add(&pwrst->node, &pwrst_list);
988
989 if (pwrdm_has_hdwr_sar(pwrdm))
990 pwrdm_enable_hdwr_sar(pwrdm);
991
992 return set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
993}
994
995/*
996 * Enable hw supervised mode for all clockdomains if it's
997 * supported. Initiate sleep transition for other clockdomains, if
998 * they are not used
999 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001000static int __init clkdms_setup(struct clockdomain *clkdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -07001001{
1002 if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
1003 omap2_clkdm_allow_idle(clkdm);
1004 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
1005 atomic_read(&clkdm->usecount) == 0)
1006 omap2_clkdm_sleep(clkdm);
1007 return 0;
1008}
1009
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301010void omap_push_sram_idle(void)
1011{
1012 _omap_sram_idle = omap_sram_push(omap34xx_cpu_suspend,
1013 omap34xx_cpu_suspend_sz);
Tero Kristo27d59a42008-10-13 13:15:00 +03001014 if (omap_type() != OMAP2_DEVICE_TYPE_GP)
1015 _omap_save_secure_sram = omap_sram_push(save_secure_ram_context,
1016 save_secure_ram_context_sz);
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301017}
1018
Kevin Hilman7cc515f2009-06-10 09:02:25 -07001019static int __init omap3_pm_init(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -07001020{
1021 struct power_state *pwrst, *tmp;
1022 int ret;
1023
1024 if (!cpu_is_omap34xx())
1025 return -ENODEV;
1026
1027 printk(KERN_ERR "Power Management for TI OMAP3.\n");
1028
1029 /* XXX prcm_setup_regs needs to be before enabling hw
1030 * supervised mode for powerdomains */
1031 prcm_setup_regs();
1032
1033 ret = request_irq(INT_34XX_PRCM_MPU_IRQ,
1034 (irq_handler_t)prcm_interrupt_handler,
1035 IRQF_DISABLED, "prcm", NULL);
1036 if (ret) {
1037 printk(KERN_ERR "request_irq failed to register for 0x%x\n",
1038 INT_34XX_PRCM_MPU_IRQ);
1039 goto err1;
1040 }
1041
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001042 ret = pwrdm_for_each(pwrdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001043 if (ret) {
1044 printk(KERN_ERR "Failed to setup powerdomains\n");
1045 goto err2;
1046 }
1047
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001048 (void) clkdm_for_each(clkdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001049
1050 mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
1051 if (mpu_pwrdm == NULL) {
1052 printk(KERN_ERR "Failed to get mpu_pwrdm\n");
1053 goto err2;
1054 }
1055
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301056 neon_pwrdm = pwrdm_lookup("neon_pwrdm");
1057 per_pwrdm = pwrdm_lookup("per_pwrdm");
1058 core_pwrdm = pwrdm_lookup("core_pwrdm");
Tero Kristoc16c3f62008-12-11 16:46:57 +02001059 cam_pwrdm = pwrdm_lookup("cam_pwrdm");
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301060
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301061 omap_push_sram_idle();
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001062#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -07001063 suspend_set_ops(&omap_pm_ops);
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001064#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -07001065
1066 pm_idle = omap3_pm_idle;
Kalle Jokiniemi03433712008-09-26 11:04:20 +03001067 omap3_idle_init();
Kevin Hilman8bd22942009-05-28 10:56:16 -07001068
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301069 pwrdm_add_wkdep(neon_pwrdm, mpu_pwrdm);
1070 /*
1071 * REVISIT: This wkdep is only necessary when GPIO2-6 are enabled for
1072 * IO-pad wakeup. Otherwise it will unnecessarily waste power
1073 * waking up PER with every CORE wakeup - see
1074 * http://marc.info/?l=linux-omap&m=121852150710062&w=2
1075 */
1076 pwrdm_add_wkdep(per_pwrdm, core_pwrdm);
1077
Tero Kristo27d59a42008-10-13 13:15:00 +03001078 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
1079 omap3_secure_ram_storage =
1080 kmalloc(0x803F, GFP_KERNEL);
1081 if (!omap3_secure_ram_storage)
1082 printk(KERN_ERR "Memory allocation failed when"
1083 "allocating for secure sram context\n");
Tero Kristo27d59a42008-10-13 13:15:00 +03001084
Tero Kristo9d971402008-12-12 11:20:05 +02001085 local_irq_disable();
1086 local_fiq_disable();
1087
1088 omap_dma_global_context_save();
1089 omap3_save_secure_ram_context(PWRDM_POWER_ON);
1090 omap_dma_global_context_restore();
1091
1092 local_irq_enable();
1093 local_fiq_enable();
1094 }
1095
1096 omap3_save_scratchpad_contents();
Kevin Hilman8bd22942009-05-28 10:56:16 -07001097err1:
1098 return ret;
1099err2:
1100 free_irq(INT_34XX_PRCM_MPU_IRQ, NULL);
1101 list_for_each_entry_safe(pwrst, tmp, &pwrst_list, node) {
1102 list_del(&pwrst->node);
1103 kfree(pwrst);
1104 }
1105 return ret;
1106}
1107
1108late_initcall(omap3_pm_init);