blob: a15aa92d384e16e42a8ece64d2f4497b08206252 [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>
Tero Kristodccaad82009-11-17 18:34:53 +020029#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Kevin Hilman8bd22942009-05-28 10:56:16 -070031
Tony Lindgrence491cf2009-10-20 09:40:47 -070032#include <plat/sram.h>
33#include <plat/clockdomain.h>
34#include <plat/powerdomain.h>
35#include <plat/control.h>
36#include <plat/serial.h>
Rajendra Nayak61255ab2008-09-26 17:49:56 +053037#include <plat/sdrc.h>
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053038#include <plat/prcm.h>
39#include <plat/gpmc.h>
Tero Kristof2d11852008-08-28 13:13:31 +000040#include <plat/dma.h>
Kevin Hilmand7814e42009-10-06 14:30:23 -070041#include <plat/dmtimer.h>
Kevin Hilman8bd22942009-05-28 10:56:16 -070042
Rajendra Nayak57f277b2008-09-26 17:49:34 +053043#include <asm/tlbflush.h>
44
Kevin Hilman8bd22942009-05-28 10:56:16 -070045#include "cm.h"
46#include "cm-regbits-34xx.h"
47#include "prm-regbits-34xx.h"
48
49#include "prm.h"
50#include "pm.h"
Tero Kristo13a6fe0f2008-10-13 13:17:06 +030051#include "sdrc.h"
52
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053053/* Scratchpad offsets */
54#define OMAP343X_TABLE_ADDRESS_OFFSET 0x31
55#define OMAP343X_TABLE_VALUE_OFFSET 0x30
56#define OMAP343X_CONTROL_REG_VALUE_OFFSET 0x32
57
Kevin Hilmanc40552b2009-10-06 14:25:09 -070058u32 enable_off_mode;
59u32 sleep_while_idle;
Kevin Hilmand7814e42009-10-06 14:30:23 -070060u32 wakeup_timer_seconds;
Kevin Hilmanc40552b2009-10-06 14:25:09 -070061
Kevin Hilman8bd22942009-05-28 10:56:16 -070062struct power_state {
63 struct powerdomain *pwrdm;
64 u32 next_state;
Kevin Hilman10f90ed2009-06-24 11:39:18 -070065#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -070066 u32 saved_state;
Kevin Hilman10f90ed2009-06-24 11:39:18 -070067#endif
Kevin Hilman8bd22942009-05-28 10:56:16 -070068 struct list_head node;
69};
70
71static LIST_HEAD(pwrst_list);
72
73static void (*_omap_sram_idle)(u32 *addr, int save_state);
74
Tero Kristo27d59a42008-10-13 13:15:00 +030075static int (*_omap_save_secure_sram)(u32 *addr);
76
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +053077static struct powerdomain *mpu_pwrdm, *neon_pwrdm;
78static struct powerdomain *core_pwrdm, *per_pwrdm;
Tero Kristoc16c3f62008-12-11 16:46:57 +020079static struct powerdomain *cam_pwrdm;
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +053080
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053081static inline void omap3_per_save_context(void)
82{
83 omap_gpio_save_context();
84}
85
86static inline void omap3_per_restore_context(void)
87{
88 omap_gpio_restore_context();
89}
90
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +020091static void omap3_enable_io_chain(void)
92{
93 int timeout = 0;
94
95 if (omap_rev() >= OMAP3430_REV_ES3_1) {
96 prm_set_mod_reg_bits(OMAP3430_EN_IO_CHAIN, WKUP_MOD, PM_WKEN);
97 /* Do a readback to assure write has been done */
98 prm_read_mod_reg(WKUP_MOD, PM_WKEN);
99
100 while (!(prm_read_mod_reg(WKUP_MOD, PM_WKST) &
101 OMAP3430_ST_IO_CHAIN)) {
102 timeout++;
103 if (timeout > 1000) {
104 printk(KERN_ERR "Wake up daisy chain "
105 "activation failed.\n");
106 return;
107 }
108 prm_set_mod_reg_bits(OMAP3430_ST_IO_CHAIN,
109 WKUP_MOD, PM_WKST);
110 }
111 }
112}
113
114static void omap3_disable_io_chain(void)
115{
116 if (omap_rev() >= OMAP3430_REV_ES3_1)
117 prm_clear_mod_reg_bits(OMAP3430_EN_IO_CHAIN, WKUP_MOD, PM_WKEN);
118}
119
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530120static void omap3_core_save_context(void)
121{
122 u32 control_padconf_off;
123
124 /* Save the padconf registers */
125 control_padconf_off = omap_ctrl_readl(OMAP343X_CONTROL_PADCONF_OFF);
126 control_padconf_off |= START_PADCONF_SAVE;
127 omap_ctrl_writel(control_padconf_off, OMAP343X_CONTROL_PADCONF_OFF);
128 /* wait for the save to complete */
Roel Kluin1b6e8212010-01-08 10:29:07 -0800129 while (!(omap_ctrl_readl(OMAP343X_CONTROL_GENERAL_PURPOSE_STATUS)
130 & PADCONF_SAVE_DONE))
Tero Kristodccaad82009-11-17 18:34:53 +0200131 udelay(1);
132
133 /*
134 * Force write last pad into memory, as this can fail in some
135 * cases according to erratas 1.157, 1.185
136 */
137 omap_ctrl_writel(omap_ctrl_readl(OMAP343X_PADCONF_ETK_D14),
138 OMAP343X_CONTROL_MEM_WKUP + 0x2a0);
139
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530140 /* Save the Interrupt controller context */
141 omap_intc_save_context();
142 /* Save the GPMC context */
143 omap3_gpmc_save_context();
144 /* Save the system control module context, padconf already save above*/
145 omap3_control_save_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000146 omap_dma_global_context_save();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530147}
148
149static void omap3_core_restore_context(void)
150{
151 /* Restore the control module context, padconf restored by h/w */
152 omap3_control_restore_context();
153 /* Restore the GPMC context */
154 omap3_gpmc_restore_context();
155 /* Restore the interrupt controller context */
156 omap_intc_restore_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000157 omap_dma_global_context_restore();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530158}
159
Tero Kristo9d971402008-12-12 11:20:05 +0200160/*
161 * FIXME: This function should be called before entering off-mode after
162 * OMAP3 secure services have been accessed. Currently it is only called
163 * once during boot sequence, but this works as we are not using secure
164 * services.
165 */
Tero Kristo27d59a42008-10-13 13:15:00 +0300166static void omap3_save_secure_ram_context(u32 target_mpu_state)
167{
168 u32 ret;
169
170 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
Tero Kristo27d59a42008-10-13 13:15:00 +0300171 /*
172 * MPU next state must be set to POWER_ON temporarily,
173 * otherwise the WFI executed inside the ROM code
174 * will hang the system.
175 */
176 pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_ON);
177 ret = _omap_save_secure_sram((u32 *)
178 __pa(omap3_secure_ram_storage));
179 pwrdm_set_next_pwrst(mpu_pwrdm, target_mpu_state);
180 /* Following is for error tracking, it should not happen */
181 if (ret) {
182 printk(KERN_ERR "save_secure_sram() returns %08x\n",
183 ret);
184 while (1)
185 ;
186 }
187 }
188}
189
Jon Hunter77da2d92009-06-27 00:07:25 -0500190/*
191 * PRCM Interrupt Handler Helper Function
192 *
193 * The purpose of this function is to clear any wake-up events latched
194 * in the PRCM PM_WKST_x registers. It is possible that a wake-up event
195 * may occur whilst attempting to clear a PM_WKST_x register and thus
196 * set another bit in this register. A while loop is used to ensure
197 * that any peripheral wake-up events occurring while attempting to
198 * clear the PM_WKST_x are detected and cleared.
199 */
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700200static int prcm_clear_mod_irqs(s16 module, u8 regs)
Jon Hunter77da2d92009-06-27 00:07:25 -0500201{
Vikram Pandita71a80772009-07-17 19:33:09 -0500202 u32 wkst, fclk, iclk, clken;
Jon Hunter77da2d92009-06-27 00:07:25 -0500203 u16 wkst_off = (regs == 3) ? OMAP3430ES2_PM_WKST3 : PM_WKST1;
204 u16 fclk_off = (regs == 3) ? OMAP3430ES2_CM_FCLKEN3 : CM_FCLKEN1;
205 u16 iclk_off = (regs == 3) ? CM_ICLKEN3 : CM_ICLKEN1;
Paul Walmsley5d805972009-07-22 10:18:07 -0700206 u16 grpsel_off = (regs == 3) ?
207 OMAP3430ES2_PM_MPUGRPSEL3 : OMAP3430_PM_MPUGRPSEL;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700208 int c = 0;
Jon Hunter77da2d92009-06-27 00:07:25 -0500209
210 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley5d805972009-07-22 10:18:07 -0700211 wkst &= prm_read_mod_reg(module, grpsel_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500212 if (wkst) {
213 iclk = cm_read_mod_reg(module, iclk_off);
214 fclk = cm_read_mod_reg(module, fclk_off);
215 while (wkst) {
Vikram Pandita71a80772009-07-17 19:33:09 -0500216 clken = wkst;
217 cm_set_mod_reg_bits(clken, module, iclk_off);
218 /*
219 * For USBHOST, we don't know whether HOST1 or
220 * HOST2 woke us up, so enable both f-clocks
221 */
222 if (module == OMAP3430ES2_USBHOST_MOD)
223 clken |= 1 << OMAP3430ES2_EN_USBHOST2_SHIFT;
224 cm_set_mod_reg_bits(clken, module, fclk_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500225 prm_write_mod_reg(wkst, module, wkst_off);
226 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700227 c++;
Jon Hunter77da2d92009-06-27 00:07:25 -0500228 }
229 cm_write_mod_reg(iclk, module, iclk_off);
230 cm_write_mod_reg(fclk, module, fclk_off);
231 }
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700232
233 return c;
234}
235
236static int _prcm_int_handle_wakeup(void)
237{
238 int c;
239
240 c = prcm_clear_mod_irqs(WKUP_MOD, 1);
241 c += prcm_clear_mod_irqs(CORE_MOD, 1);
242 c += prcm_clear_mod_irqs(OMAP3430_PER_MOD, 1);
243 if (omap_rev() > OMAP3430_REV_ES1_0) {
244 c += prcm_clear_mod_irqs(CORE_MOD, 3);
245 c += prcm_clear_mod_irqs(OMAP3430ES2_USBHOST_MOD, 1);
246 }
247
248 return c;
Jon Hunter77da2d92009-06-27 00:07:25 -0500249}
250
251/*
252 * PRCM Interrupt Handler
253 *
254 * The PRM_IRQSTATUS_MPU register indicates if there are any pending
255 * interrupts from the PRCM for the MPU. These bits must be cleared in
256 * order to clear the PRCM interrupt. The PRCM interrupt handler is
257 * implemented to simply clear the PRM_IRQSTATUS_MPU in order to clear
258 * the PRCM interrupt. Please note that bit 0 of the PRM_IRQSTATUS_MPU
259 * register indicates that a wake-up event is pending for the MPU and
260 * this bit can only be cleared if the all the wake-up events latched
261 * in the various PM_WKST_x registers have been cleared. The interrupt
262 * handler is implemented using a do-while loop so that if a wake-up
263 * event occurred during the processing of the prcm interrupt handler
264 * (setting a bit in the corresponding PM_WKST_x register and thus
265 * preventing us from clearing bit 0 of the PRM_IRQSTATUS_MPU register)
266 * this would be handled.
267 */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700268static irqreturn_t prcm_interrupt_handler (int irq, void *dev_id)
269{
Kevin Hilmand6290a32010-04-26 14:59:09 -0700270 u32 irqenable_mpu, irqstatus_mpu;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700271 int c = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700272
Kevin Hilmand6290a32010-04-26 14:59:09 -0700273 irqenable_mpu = prm_read_mod_reg(OCP_MOD,
274 OMAP3_PRM_IRQENABLE_MPU_OFFSET);
275 irqstatus_mpu = prm_read_mod_reg(OCP_MOD,
276 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
277 irqstatus_mpu &= irqenable_mpu;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700278
Kevin Hilmand6290a32010-04-26 14:59:09 -0700279 do {
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700280 if (irqstatus_mpu & (OMAP3430_WKUP_ST | OMAP3430_IO_ST)) {
281 c = _prcm_int_handle_wakeup();
282
283 /*
284 * Is the MPU PRCM interrupt handler racing with the
285 * IVA2 PRCM interrupt handler ?
286 */
287 WARN(c == 0, "prcm: WARNING: PRCM indicated MPU wakeup "
288 "but no wakeup sources are marked\n");
289 } else {
290 /* XXX we need to expand our PRCM interrupt handler */
291 WARN(1, "prcm: WARNING: PRCM interrupt received, but "
292 "no code to handle it (%08x)\n", irqstatus_mpu);
293 }
294
Jon Hunter77da2d92009-06-27 00:07:25 -0500295 prm_write_mod_reg(irqstatus_mpu, OCP_MOD,
296 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700297
Kevin Hilmand6290a32010-04-26 14:59:09 -0700298 irqstatus_mpu = prm_read_mod_reg(OCP_MOD,
299 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
300 irqstatus_mpu &= irqenable_mpu;
301
302 } while (irqstatus_mpu);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700303
304 return IRQ_HANDLED;
305}
306
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530307static void restore_control_register(u32 val)
308{
309 __asm__ __volatile__ ("mcr p15, 0, %0, c1, c0, 0" : : "r" (val));
310}
311
312/* Function to restore the table entry that was modified for enabling MMU */
313static void restore_table_entry(void)
314{
315 u32 *scratchpad_address;
316 u32 previous_value, control_reg_value;
317 u32 *address;
318
319 scratchpad_address = OMAP2_L4_IO_ADDRESS(OMAP343X_SCRATCHPAD);
320
321 /* Get address of entry that was modified */
322 address = (u32 *)__raw_readl(scratchpad_address +
323 OMAP343X_TABLE_ADDRESS_OFFSET);
324 /* Get the previous value which needs to be restored */
325 previous_value = __raw_readl(scratchpad_address +
326 OMAP343X_TABLE_VALUE_OFFSET);
327 address = __va(address);
328 *address = previous_value;
329 flush_tlb_all();
330 control_reg_value = __raw_readl(scratchpad_address
331 + OMAP343X_CONTROL_REG_VALUE_OFFSET);
332 /* This will enable caches and prediction */
333 restore_control_register(control_reg_value);
334}
335
Rajendra Nayak99e6a4d2008-10-08 17:30:58 +0530336void omap_sram_idle(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700337{
338 /* Variable to tell what needs to be saved and restored
339 * in omap_sram_idle*/
340 /* save_state = 0 => Nothing to save and restored */
341 /* save_state = 1 => Only L1 and logic lost */
342 /* save_state = 2 => Only L2 lost */
343 /* save_state = 3 => L1, L2 and logic lost */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530344 int save_state = 0;
345 int mpu_next_state = PWRDM_POWER_ON;
346 int per_next_state = PWRDM_POWER_ON;
347 int core_next_state = PWRDM_POWER_ON;
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530348 int core_prev_state, per_prev_state;
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300349 u32 sdrc_pwr = 0;
Tero Kristoecf157d2008-12-01 13:17:29 +0200350 int per_state_modified = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700351
352 if (!_omap_sram_idle)
353 return;
354
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530355 pwrdm_clear_all_prev_pwrst(mpu_pwrdm);
356 pwrdm_clear_all_prev_pwrst(neon_pwrdm);
357 pwrdm_clear_all_prev_pwrst(core_pwrdm);
358 pwrdm_clear_all_prev_pwrst(per_pwrdm);
359
Kevin Hilman8bd22942009-05-28 10:56:16 -0700360 mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
361 switch (mpu_next_state) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530362 case PWRDM_POWER_ON:
Kevin Hilman8bd22942009-05-28 10:56:16 -0700363 case PWRDM_POWER_RET:
364 /* No need to save context */
365 save_state = 0;
366 break;
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530367 case PWRDM_POWER_OFF:
368 save_state = 3;
369 break;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700370 default:
371 /* Invalid state */
372 printk(KERN_ERR "Invalid mpu state in sram_idle\n");
373 return;
374 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300375 pwrdm_pre_transition();
376
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530377 /* NEON control */
378 if (pwrdm_read_pwrst(neon_pwrdm) == PWRDM_POWER_ON)
Jouni Hogander71391782008-10-28 10:59:05 +0200379 pwrdm_set_next_pwrst(neon_pwrdm, mpu_next_state);
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530380
Kevin Hilman658ce972008-11-04 20:50:52 -0800381 /* PER */
382 per_next_state = pwrdm_read_next_pwrst(per_pwrdm);
Tero Kristoecf157d2008-12-01 13:17:29 +0200383 core_next_state = pwrdm_read_next_pwrst(core_pwrdm);
Kevin Hilman658ce972008-11-04 20:50:52 -0800384 if (per_next_state < PWRDM_POWER_ON) {
Kevin Hilman658ce972008-11-04 20:50:52 -0800385 omap_uart_prepare_idle(2);
Tero Kristoecf157d2008-12-01 13:17:29 +0200386 omap2_gpio_prepare_for_retention();
387 if (per_next_state == PWRDM_POWER_OFF) {
388 if (core_next_state == PWRDM_POWER_ON) {
389 per_next_state = PWRDM_POWER_RET;
390 pwrdm_set_next_pwrst(per_pwrdm, per_next_state);
391 per_state_modified = 1;
392 } else
393 omap3_per_save_context();
394 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800395 }
396
Tero Kristoc16c3f62008-12-11 16:46:57 +0200397 if (pwrdm_read_pwrst(cam_pwrdm) == PWRDM_POWER_ON)
398 omap2_clkdm_deny_idle(mpu_pwrdm->pwrdm_clkdms[0]);
399
Kevin Hilman658ce972008-11-04 20:50:52 -0800400 /* CORE */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530401 if (core_next_state < PWRDM_POWER_ON) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530402 omap_uart_prepare_idle(0);
403 omap_uart_prepare_idle(1);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530404 if (core_next_state == PWRDM_POWER_OFF) {
405 omap3_core_save_context();
406 omap3_prcm_save_context();
407 }
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200408 /* Enable IO-PAD and IO-CHAIN wakeups */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530409 prm_set_mod_reg_bits(OMAP3430_EN_IO, WKUP_MOD, PM_WKEN);
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200410 omap3_enable_io_chain();
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530411 }
Tero Kristof18cc2f2009-10-23 19:03:50 +0300412 omap3_intc_prepare_idle();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700413
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530414 /*
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530415 * On EMU/HS devices ROM code restores a SRDC value
416 * from scratchpad which has automatic self refresh on timeout
417 * of AUTO_CNT = 1 enabled. This takes care of errata 1.142.
418 * Hence store/restore the SDRC_POWER register here.
419 */
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300420 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
421 omap_type() != OMAP2_DEVICE_TYPE_GP &&
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530422 core_next_state == PWRDM_POWER_OFF)
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300423 sdrc_pwr = sdrc_read_reg(SDRC_POWER);
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300424
425 /*
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530426 * omap3_arm_context is the location where ARM registers
427 * get saved. The restore path then reads from this
428 * location and restores them back.
429 */
430 _omap_sram_idle(omap3_arm_context, save_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700431 cpu_init();
432
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530433 /* Restore normal SDRC POWER settings */
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300434 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
435 omap_type() != OMAP2_DEVICE_TYPE_GP &&
436 core_next_state == PWRDM_POWER_OFF)
437 sdrc_write_reg(sdrc_pwr, SDRC_POWER);
438
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530439 /* Restore table entry modified during MMU restoration */
440 if (pwrdm_read_prev_pwrst(mpu_pwrdm) == PWRDM_POWER_OFF)
441 restore_table_entry();
442
Kevin Hilman658ce972008-11-04 20:50:52 -0800443 /* CORE */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530444 if (core_next_state < PWRDM_POWER_ON) {
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530445 core_prev_state = pwrdm_read_prev_pwrst(core_pwrdm);
446 if (core_prev_state == PWRDM_POWER_OFF) {
447 omap3_core_restore_context();
448 omap3_prcm_restore_context();
449 omap3_sram_restore_context();
Kalle Jokiniemi8a917d22009-05-13 13:32:11 +0300450 omap2_sms_restore_context();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530451 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800452 omap_uart_resume_idle(0);
453 omap_uart_resume_idle(1);
454 if (core_next_state == PWRDM_POWER_OFF)
455 prm_clear_mod_reg_bits(OMAP3430_AUTO_OFF,
456 OMAP3430_GR_MOD,
457 OMAP3_PRM_VOLTCTRL_OFFSET);
458 }
Tero Kristof18cc2f2009-10-23 19:03:50 +0300459 omap3_intc_resume_idle();
Kevin Hilman658ce972008-11-04 20:50:52 -0800460
461 /* PER */
462 if (per_next_state < PWRDM_POWER_ON) {
463 per_prev_state = pwrdm_read_prev_pwrst(per_pwrdm);
Kevin Hilman658ce972008-11-04 20:50:52 -0800464 if (per_prev_state == PWRDM_POWER_OFF)
465 omap3_per_restore_context();
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530466 omap2_gpio_resume_after_retention();
Tero Kristoecf157d2008-12-01 13:17:29 +0200467 omap_uart_resume_idle(2);
468 if (per_state_modified)
469 pwrdm_set_next_pwrst(per_pwrdm, PWRDM_POWER_OFF);
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530470 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300471
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200472 /* Disable IO-PAD and IO-CHAIN wakeup */
473 if (core_next_state < PWRDM_POWER_ON) {
Kevin Hilman658ce972008-11-04 20:50:52 -0800474 prm_clear_mod_reg_bits(OMAP3430_EN_IO, WKUP_MOD, PM_WKEN);
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200475 omap3_disable_io_chain();
476 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800477
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300478 pwrdm_post_transition();
479
Tero Kristoc16c3f62008-12-11 16:46:57 +0200480 omap2_clkdm_allow_idle(mpu_pwrdm->pwrdm_clkdms[0]);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700481}
482
Rajendra Nayak20b01662008-10-08 17:31:22 +0530483int omap3_can_sleep(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700484{
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700485 if (!sleep_while_idle)
486 return 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800487 if (!omap_uart_can_sleep())
488 return 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700489 return 1;
490}
491
492/* This sets pwrdm state (other than mpu & core. Currently only ON &
493 * RET are supported. Function is assuming that clkdm doesn't have
494 * hw_sup mode enabled. */
Rajendra Nayak20b01662008-10-08 17:31:22 +0530495int set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700496{
497 u32 cur_state;
498 int sleep_switch = 0;
499 int ret = 0;
500
501 if (pwrdm == NULL || IS_ERR(pwrdm))
502 return -EINVAL;
503
504 while (!(pwrdm->pwrsts & (1 << state))) {
505 if (state == PWRDM_POWER_OFF)
506 return ret;
507 state--;
508 }
509
510 cur_state = pwrdm_read_next_pwrst(pwrdm);
511 if (cur_state == state)
512 return ret;
513
514 if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
515 omap2_clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
516 sleep_switch = 1;
517 pwrdm_wait_transition(pwrdm);
518 }
519
520 ret = pwrdm_set_next_pwrst(pwrdm, state);
521 if (ret) {
522 printk(KERN_ERR "Unable to set state of powerdomain: %s\n",
523 pwrdm->name);
524 goto err;
525 }
526
527 if (sleep_switch) {
528 omap2_clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
529 pwrdm_wait_transition(pwrdm);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300530 pwrdm_state_switch(pwrdm);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700531 }
532
533err:
534 return ret;
535}
536
537static void omap3_pm_idle(void)
538{
539 local_irq_disable();
540 local_fiq_disable();
541
542 if (!omap3_can_sleep())
543 goto out;
544
Tero Kristocf228542009-03-20 15:21:02 +0200545 if (omap_irq_pending() || need_resched())
Kevin Hilman8bd22942009-05-28 10:56:16 -0700546 goto out;
547
548 omap_sram_idle();
549
550out:
551 local_fiq_enable();
552 local_irq_enable();
553}
554
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700555#ifdef CONFIG_SUSPEND
Tero Kristo24662112009-03-05 16:32:23 +0200556static suspend_state_t suspend_state;
557
Kevin Hilmand7814e42009-10-06 14:30:23 -0700558static void omap2_pm_wakeup_on_timer(u32 seconds)
559{
560 u32 tick_rate, cycles;
561
562 if (!seconds)
563 return;
564
565 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gptimer_wakeup));
566 cycles = tick_rate * seconds;
567 omap_dm_timer_stop(gptimer_wakeup);
568 omap_dm_timer_set_load_start(gptimer_wakeup, 0, 0xffffffff - cycles);
569
570 pr_info("PM: Resume timer in %d secs (%d ticks at %d ticks/sec.)\n",
571 seconds, cycles, tick_rate);
572}
573
Kevin Hilman8bd22942009-05-28 10:56:16 -0700574static int omap3_pm_prepare(void)
575{
576 disable_hlt();
577 return 0;
578}
579
580static int omap3_pm_suspend(void)
581{
582 struct power_state *pwrst;
583 int state, ret = 0;
584
Kevin Hilmand7814e42009-10-06 14:30:23 -0700585 if (wakeup_timer_seconds)
586 omap2_pm_wakeup_on_timer(wakeup_timer_seconds);
587
Kevin Hilman8bd22942009-05-28 10:56:16 -0700588 /* Read current next_pwrsts */
589 list_for_each_entry(pwrst, &pwrst_list, node)
590 pwrst->saved_state = pwrdm_read_next_pwrst(pwrst->pwrdm);
591 /* Set ones wanted by suspend */
592 list_for_each_entry(pwrst, &pwrst_list, node) {
593 if (set_pwrdm_state(pwrst->pwrdm, pwrst->next_state))
594 goto restore;
595 if (pwrdm_clear_all_prev_pwrst(pwrst->pwrdm))
596 goto restore;
597 }
598
Kevin Hilman4af40162009-02-04 10:51:40 -0800599 omap_uart_prepare_suspend();
Tero Kristo2bbe3af2009-10-23 19:03:48 +0300600 omap3_intc_suspend();
601
Kevin Hilman8bd22942009-05-28 10:56:16 -0700602 omap_sram_idle();
603
604restore:
605 /* Restore next_pwrsts */
606 list_for_each_entry(pwrst, &pwrst_list, node) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700607 state = pwrdm_read_prev_pwrst(pwrst->pwrdm);
608 if (state > pwrst->next_state) {
609 printk(KERN_INFO "Powerdomain (%s) didn't enter "
610 "target state %d\n",
611 pwrst->pwrdm->name, pwrst->next_state);
612 ret = -1;
613 }
Jouni Hogander6c5f8032008-10-29 12:06:04 +0200614 set_pwrdm_state(pwrst->pwrdm, pwrst->saved_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700615 }
616 if (ret)
617 printk(KERN_ERR "Could not enter target state in pm_suspend\n");
618 else
619 printk(KERN_INFO "Successfully put all powerdomains "
620 "to target state\n");
621
622 return ret;
623}
624
Tero Kristo24662112009-03-05 16:32:23 +0200625static int omap3_pm_enter(suspend_state_t unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700626{
627 int ret = 0;
628
Tero Kristo24662112009-03-05 16:32:23 +0200629 switch (suspend_state) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700630 case PM_SUSPEND_STANDBY:
631 case PM_SUSPEND_MEM:
632 ret = omap3_pm_suspend();
633 break;
634 default:
635 ret = -EINVAL;
636 }
637
638 return ret;
639}
640
641static void omap3_pm_finish(void)
642{
643 enable_hlt();
644}
645
Tero Kristo24662112009-03-05 16:32:23 +0200646/* Hooks to enable / disable UART interrupts during suspend */
647static int omap3_pm_begin(suspend_state_t state)
648{
649 suspend_state = state;
650 omap_uart_enable_irqs(0);
651 return 0;
652}
653
654static void omap3_pm_end(void)
655{
656 suspend_state = PM_SUSPEND_ON;
657 omap_uart_enable_irqs(1);
658 return;
659}
660
Kevin Hilman8bd22942009-05-28 10:56:16 -0700661static struct platform_suspend_ops omap_pm_ops = {
Tero Kristo24662112009-03-05 16:32:23 +0200662 .begin = omap3_pm_begin,
663 .end = omap3_pm_end,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700664 .prepare = omap3_pm_prepare,
665 .enter = omap3_pm_enter,
666 .finish = omap3_pm_finish,
667 .valid = suspend_valid_only_mem,
668};
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700669#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700670
Kevin Hilman1155e422008-11-25 11:48:24 -0800671
672/**
673 * omap3_iva_idle(): ensure IVA is in idle so it can be put into
674 * retention
675 *
676 * In cases where IVA2 is activated by bootcode, it may prevent
677 * full-chip retention or off-mode because it is not idle. This
678 * function forces the IVA2 into idle state so it can go
679 * into retention/off and thus allow full-chip retention/off.
680 *
681 **/
682static void __init omap3_iva_idle(void)
683{
684 /* ensure IVA2 clock is disabled */
685 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
686
687 /* if no clock activity, nothing else to do */
688 if (!(cm_read_mod_reg(OMAP3430_IVA2_MOD, OMAP3430_CM_CLKSTST) &
689 OMAP3430_CLKACTIVITY_IVA2_MASK))
690 return;
691
692 /* Reset IVA2 */
693 prm_write_mod_reg(OMAP3430_RST1_IVA2 |
694 OMAP3430_RST2_IVA2 |
695 OMAP3430_RST3_IVA2,
Abhijit Pagare37903002010-01-26 20:12:51 -0700696 OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
Kevin Hilman1155e422008-11-25 11:48:24 -0800697
698 /* Enable IVA2 clock */
Kevin Hilmandfa6d6f2010-02-24 12:05:48 -0700699 cm_write_mod_reg(OMAP3430_CM_FCLKEN_IVA2_EN_IVA2_MASK,
Kevin Hilman1155e422008-11-25 11:48:24 -0800700 OMAP3430_IVA2_MOD, CM_FCLKEN);
701
702 /* Set IVA2 boot mode to 'idle' */
703 omap_ctrl_writel(OMAP3_IVA2_BOOTMOD_IDLE,
704 OMAP343X_CONTROL_IVA2_BOOTMOD);
705
706 /* Un-reset IVA2 */
Abhijit Pagare37903002010-01-26 20:12:51 -0700707 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
Kevin Hilman1155e422008-11-25 11:48:24 -0800708
709 /* Disable IVA2 clock */
710 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
711
712 /* Reset IVA2 */
713 prm_write_mod_reg(OMAP3430_RST1_IVA2 |
714 OMAP3430_RST2_IVA2 |
715 OMAP3430_RST3_IVA2,
Abhijit Pagare37903002010-01-26 20:12:51 -0700716 OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
Kevin Hilman1155e422008-11-25 11:48:24 -0800717}
718
Kevin Hilman8111b222009-04-28 15:27:44 -0700719static void __init omap3_d2d_idle(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700720{
Kevin Hilman8111b222009-04-28 15:27:44 -0700721 u16 mask, padconf;
722
723 /* In a stand alone OMAP3430 where there is not a stacked
724 * modem for the D2D Idle Ack and D2D MStandby must be pulled
725 * high. S CONTROL_PADCONF_SAD2D_IDLEACK and
726 * CONTROL_PADCONF_SAD2D_MSTDBY to have a pull up. */
727 mask = (1 << 4) | (1 << 3); /* pull-up, enabled */
728 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_MSTANDBY);
729 padconf |= mask;
730 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_MSTANDBY);
731
732 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_IDLEACK);
733 padconf |= mask;
734 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_IDLEACK);
735
Kevin Hilman8bd22942009-05-28 10:56:16 -0700736 /* reset modem */
737 prm_write_mod_reg(OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RSTPWRON |
738 OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RST,
Abhijit Pagare37903002010-01-26 20:12:51 -0700739 CORE_MOD, OMAP2_RM_RSTCTRL);
740 prm_write_mod_reg(0, CORE_MOD, OMAP2_RM_RSTCTRL);
Kevin Hilman8111b222009-04-28 15:27:44 -0700741}
Kevin Hilman8bd22942009-05-28 10:56:16 -0700742
Kevin Hilman8111b222009-04-28 15:27:44 -0700743static void __init prcm_setup_regs(void)
744{
Kevin Hilman8bd22942009-05-28 10:56:16 -0700745 /* XXX Reset all wkdeps. This should be done when initializing
746 * powerdomains */
747 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, PM_WKDEP);
748 prm_write_mod_reg(0, MPU_MOD, PM_WKDEP);
749 prm_write_mod_reg(0, OMAP3430_DSS_MOD, PM_WKDEP);
750 prm_write_mod_reg(0, OMAP3430_NEON_MOD, PM_WKDEP);
751 prm_write_mod_reg(0, OMAP3430_CAM_MOD, PM_WKDEP);
752 prm_write_mod_reg(0, OMAP3430_PER_MOD, PM_WKDEP);
753 if (omap_rev() > OMAP3430_REV_ES1_0) {
754 prm_write_mod_reg(0, OMAP3430ES2_SGX_MOD, PM_WKDEP);
755 prm_write_mod_reg(0, OMAP3430ES2_USBHOST_MOD, PM_WKDEP);
756 } else
757 prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
758
759 /*
760 * Enable interface clock autoidle for all modules.
761 * Note that in the long run this should be done by clockfw
762 */
763 cm_write_mod_reg(
Kevin Hilman8111b222009-04-28 15:27:44 -0700764 OMAP3430_AUTO_MODEM |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700765 OMAP3430ES2_AUTO_MMC3 |
766 OMAP3430ES2_AUTO_ICR |
767 OMAP3430_AUTO_AES2 |
768 OMAP3430_AUTO_SHA12 |
769 OMAP3430_AUTO_DES2 |
770 OMAP3430_AUTO_MMC2 |
771 OMAP3430_AUTO_MMC1 |
772 OMAP3430_AUTO_MSPRO |
773 OMAP3430_AUTO_HDQ |
774 OMAP3430_AUTO_MCSPI4 |
775 OMAP3430_AUTO_MCSPI3 |
776 OMAP3430_AUTO_MCSPI2 |
777 OMAP3430_AUTO_MCSPI1 |
778 OMAP3430_AUTO_I2C3 |
779 OMAP3430_AUTO_I2C2 |
780 OMAP3430_AUTO_I2C1 |
781 OMAP3430_AUTO_UART2 |
782 OMAP3430_AUTO_UART1 |
783 OMAP3430_AUTO_GPT11 |
784 OMAP3430_AUTO_GPT10 |
785 OMAP3430_AUTO_MCBSP5 |
786 OMAP3430_AUTO_MCBSP1 |
787 OMAP3430ES1_AUTO_FAC | /* This is es1 only */
788 OMAP3430_AUTO_MAILBOXES |
789 OMAP3430_AUTO_OMAPCTRL |
790 OMAP3430ES1_AUTO_FSHOSTUSB |
791 OMAP3430_AUTO_HSOTGUSB |
Kevin Hilman8111b222009-04-28 15:27:44 -0700792 OMAP3430_AUTO_SAD2D |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700793 OMAP3430_AUTO_SSI,
794 CORE_MOD, CM_AUTOIDLE1);
795
796 cm_write_mod_reg(
797 OMAP3430_AUTO_PKA |
798 OMAP3430_AUTO_AES1 |
799 OMAP3430_AUTO_RNG |
800 OMAP3430_AUTO_SHA11 |
801 OMAP3430_AUTO_DES1,
802 CORE_MOD, CM_AUTOIDLE2);
803
804 if (omap_rev() > OMAP3430_REV_ES1_0) {
805 cm_write_mod_reg(
Kevin Hilman8111b222009-04-28 15:27:44 -0700806 OMAP3430_AUTO_MAD2D |
Kevin Hilman8bd22942009-05-28 10:56:16 -0700807 OMAP3430ES2_AUTO_USBTLL,
808 CORE_MOD, CM_AUTOIDLE3);
809 }
810
811 cm_write_mod_reg(
812 OMAP3430_AUTO_WDT2 |
813 OMAP3430_AUTO_WDT1 |
814 OMAP3430_AUTO_GPIO1 |
815 OMAP3430_AUTO_32KSYNC |
816 OMAP3430_AUTO_GPT12 |
817 OMAP3430_AUTO_GPT1 ,
818 WKUP_MOD, CM_AUTOIDLE);
819
820 cm_write_mod_reg(
821 OMAP3430_AUTO_DSS,
822 OMAP3430_DSS_MOD,
823 CM_AUTOIDLE);
824
825 cm_write_mod_reg(
826 OMAP3430_AUTO_CAM,
827 OMAP3430_CAM_MOD,
828 CM_AUTOIDLE);
829
830 cm_write_mod_reg(
831 OMAP3430_AUTO_GPIO6 |
832 OMAP3430_AUTO_GPIO5 |
833 OMAP3430_AUTO_GPIO4 |
834 OMAP3430_AUTO_GPIO3 |
835 OMAP3430_AUTO_GPIO2 |
836 OMAP3430_AUTO_WDT3 |
837 OMAP3430_AUTO_UART3 |
838 OMAP3430_AUTO_GPT9 |
839 OMAP3430_AUTO_GPT8 |
840 OMAP3430_AUTO_GPT7 |
841 OMAP3430_AUTO_GPT6 |
842 OMAP3430_AUTO_GPT5 |
843 OMAP3430_AUTO_GPT4 |
844 OMAP3430_AUTO_GPT3 |
845 OMAP3430_AUTO_GPT2 |
846 OMAP3430_AUTO_MCBSP4 |
847 OMAP3430_AUTO_MCBSP3 |
848 OMAP3430_AUTO_MCBSP2,
849 OMAP3430_PER_MOD,
850 CM_AUTOIDLE);
851
852 if (omap_rev() > OMAP3430_REV_ES1_0) {
853 cm_write_mod_reg(
854 OMAP3430ES2_AUTO_USBHOST,
855 OMAP3430ES2_USBHOST_MOD,
856 CM_AUTOIDLE);
857 }
858
Tero Kristob296c812009-10-23 19:03:49 +0300859 omap_ctrl_writel(OMAP3430_AUTOIDLE, OMAP2_CONTROL_SYSCONFIG);
860
Kevin Hilman8bd22942009-05-28 10:56:16 -0700861 /*
862 * Set all plls to autoidle. This is needed until autoidle is
863 * enabled by clockfw
864 */
865 cm_write_mod_reg(1 << OMAP3430_AUTO_IVA2_DPLL_SHIFT,
866 OMAP3430_IVA2_MOD, CM_AUTOIDLE2);
867 cm_write_mod_reg(1 << OMAP3430_AUTO_MPU_DPLL_SHIFT,
868 MPU_MOD,
869 CM_AUTOIDLE2);
870 cm_write_mod_reg((1 << OMAP3430_AUTO_PERIPH_DPLL_SHIFT) |
871 (1 << OMAP3430_AUTO_CORE_DPLL_SHIFT),
872 PLL_MOD,
873 CM_AUTOIDLE);
874 cm_write_mod_reg(1 << OMAP3430ES2_AUTO_PERIPH2_DPLL_SHIFT,
875 PLL_MOD,
876 CM_AUTOIDLE2);
877
878 /*
879 * Enable control of expternal oscillator through
880 * sys_clkreq. In the long run clock framework should
881 * take care of this.
882 */
883 prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK,
884 1 << OMAP_AUTOEXTCLKMODE_SHIFT,
885 OMAP3430_GR_MOD,
886 OMAP3_PRM_CLKSRC_CTRL_OFFSET);
887
888 /* setup wakup source */
889 prm_write_mod_reg(OMAP3430_EN_IO | OMAP3430_EN_GPIO1 |
890 OMAP3430_EN_GPT1 | OMAP3430_EN_GPT12,
891 WKUP_MOD, PM_WKEN);
892 /* No need to write EN_IO, that is always enabled */
893 prm_write_mod_reg(OMAP3430_EN_GPIO1 | OMAP3430_EN_GPT1 |
894 OMAP3430_EN_GPT12,
895 WKUP_MOD, OMAP3430_PM_MPUGRPSEL);
896 /* For some reason IO doesn't generate wakeup event even if
897 * it is selected to mpu wakeup goup */
898 prm_write_mod_reg(OMAP3430_IO_EN | OMAP3430_WKUP_EN,
899 OCP_MOD, OMAP3_PRM_IRQENABLE_MPU_OFFSET);
Kevin Hilman1155e422008-11-25 11:48:24 -0800900
Subramani Venkateshb92c5722009-12-22 15:07:50 +0530901 /* Enable PM_WKEN to support DSS LPR */
902 prm_write_mod_reg(OMAP3430_PM_WKEN_DSS_EN_DSS,
903 OMAP3430_DSS_MOD, PM_WKEN);
904
Kevin Hilmanb427f922009-10-22 14:48:13 -0700905 /* Enable wakeups in PER */
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000906 prm_write_mod_reg(OMAP3430_EN_GPIO2 | OMAP3430_EN_GPIO3 |
907 OMAP3430_EN_GPIO4 | OMAP3430_EN_GPIO5 |
Peter Ujfalusie3d93292009-11-26 15:18:50 +0200908 OMAP3430_EN_GPIO6 | OMAP3430_EN_UART3 |
909 OMAP3430_EN_MCBSP2 | OMAP3430_EN_MCBSP3 |
910 OMAP3430_EN_MCBSP4,
Kevin Hilmanb427f922009-10-22 14:48:13 -0700911 OMAP3430_PER_MOD, PM_WKEN);
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000912 /* and allow them to wake up MPU */
913 prm_write_mod_reg(OMAP3430_GRPSEL_GPIO2 | OMAP3430_EN_GPIO3 |
914 OMAP3430_GRPSEL_GPIO4 | OMAP3430_EN_GPIO5 |
Peter Ujfalusie3d93292009-11-26 15:18:50 +0200915 OMAP3430_GRPSEL_GPIO6 | OMAP3430_EN_UART3 |
916 OMAP3430_EN_MCBSP2 | OMAP3430_EN_MCBSP3 |
917 OMAP3430_EN_MCBSP4,
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000918 OMAP3430_PER_MOD, OMAP3430_PM_MPUGRPSEL);
919
Kevin Hilmand3fd3292009-05-05 16:34:25 -0700920 /* Don't attach IVA interrupts */
921 prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
922 prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
923 prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
924 prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
925
Kevin Hilmanb1340d12009-04-27 16:14:54 -0700926 /* Clear any pending 'reset' flags */
Abhijit Pagare37903002010-01-26 20:12:51 -0700927 prm_write_mod_reg(0xffffffff, MPU_MOD, OMAP2_RM_RSTST);
928 prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP2_RM_RSTST);
929 prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, OMAP2_RM_RSTST);
930 prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, OMAP2_RM_RSTST);
931 prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, OMAP2_RM_RSTST);
932 prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, OMAP2_RM_RSTST);
933 prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, OMAP2_RM_RSTST);
Kevin Hilmanb1340d12009-04-27 16:14:54 -0700934
Kevin Hilman014c46d2009-04-27 07:50:23 -0700935 /* Clear any pending PRCM interrupts */
936 prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
937
Kevin Hilman1155e422008-11-25 11:48:24 -0800938 omap3_iva_idle();
Kevin Hilman8111b222009-04-28 15:27:44 -0700939 omap3_d2d_idle();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700940}
941
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700942void omap3_pm_off_mode_enable(int enable)
943{
944 struct power_state *pwrst;
945 u32 state;
946
947 if (enable)
948 state = PWRDM_POWER_OFF;
949 else
950 state = PWRDM_POWER_RET;
951
Sanjeev Premi6af83b32010-01-28 23:16:43 +0530952#ifdef CONFIG_CPU_IDLE
953 omap3_cpuidle_update_states();
954#endif
955
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700956 list_for_each_entry(pwrst, &pwrst_list, node) {
957 pwrst->next_state = state;
958 set_pwrdm_state(pwrst->pwrdm, state);
959 }
960}
961
Tero Kristo68d47782008-11-26 12:26:24 +0200962int omap3_pm_get_suspend_state(struct powerdomain *pwrdm)
963{
964 struct power_state *pwrst;
965
966 list_for_each_entry(pwrst, &pwrst_list, node) {
967 if (pwrst->pwrdm == pwrdm)
968 return pwrst->next_state;
969 }
970 return -EINVAL;
971}
972
973int omap3_pm_set_suspend_state(struct powerdomain *pwrdm, int state)
974{
975 struct power_state *pwrst;
976
977 list_for_each_entry(pwrst, &pwrst_list, node) {
978 if (pwrst->pwrdm == pwrdm) {
979 pwrst->next_state = state;
980 return 0;
981 }
982 }
983 return -EINVAL;
984}
985
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +0300986static int __init pwrdms_setup(struct powerdomain *pwrdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700987{
988 struct power_state *pwrst;
989
990 if (!pwrdm->pwrsts)
991 return 0;
992
Ming Leid3d381c2009-08-22 21:20:26 +0800993 pwrst = kmalloc(sizeof(struct power_state), GFP_ATOMIC);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700994 if (!pwrst)
995 return -ENOMEM;
996 pwrst->pwrdm = pwrdm;
997 pwrst->next_state = PWRDM_POWER_RET;
998 list_add(&pwrst->node, &pwrst_list);
999
1000 if (pwrdm_has_hdwr_sar(pwrdm))
1001 pwrdm_enable_hdwr_sar(pwrdm);
1002
1003 return set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
1004}
1005
1006/*
1007 * Enable hw supervised mode for all clockdomains if it's
1008 * supported. Initiate sleep transition for other clockdomains, if
1009 * they are not used
1010 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001011static int __init clkdms_setup(struct clockdomain *clkdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -07001012{
Paul Walmsley369d5612010-01-26 20:13:01 -07001013 clkdm_clear_all_wkdeps(clkdm);
1014 clkdm_clear_all_sleepdeps(clkdm);
1015
Kevin Hilman8bd22942009-05-28 10:56:16 -07001016 if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
1017 omap2_clkdm_allow_idle(clkdm);
1018 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
1019 atomic_read(&clkdm->usecount) == 0)
1020 omap2_clkdm_sleep(clkdm);
1021 return 0;
1022}
1023
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301024void omap_push_sram_idle(void)
1025{
1026 _omap_sram_idle = omap_sram_push(omap34xx_cpu_suspend,
1027 omap34xx_cpu_suspend_sz);
Tero Kristo27d59a42008-10-13 13:15:00 +03001028 if (omap_type() != OMAP2_DEVICE_TYPE_GP)
1029 _omap_save_secure_sram = omap_sram_push(save_secure_ram_context,
1030 save_secure_ram_context_sz);
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301031}
1032
Kevin Hilman7cc515f2009-06-10 09:02:25 -07001033static int __init omap3_pm_init(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -07001034{
1035 struct power_state *pwrst, *tmp;
Paul Walmsley55ed9692010-01-26 20:12:59 -07001036 struct clockdomain *neon_clkdm, *per_clkdm, *mpu_clkdm, *core_clkdm;
Kevin Hilman8bd22942009-05-28 10:56:16 -07001037 int ret;
1038
1039 if (!cpu_is_omap34xx())
1040 return -ENODEV;
1041
1042 printk(KERN_ERR "Power Management for TI OMAP3.\n");
1043
1044 /* XXX prcm_setup_regs needs to be before enabling hw
1045 * supervised mode for powerdomains */
1046 prcm_setup_regs();
1047
1048 ret = request_irq(INT_34XX_PRCM_MPU_IRQ,
1049 (irq_handler_t)prcm_interrupt_handler,
1050 IRQF_DISABLED, "prcm", NULL);
1051 if (ret) {
1052 printk(KERN_ERR "request_irq failed to register for 0x%x\n",
1053 INT_34XX_PRCM_MPU_IRQ);
1054 goto err1;
1055 }
1056
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001057 ret = pwrdm_for_each(pwrdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001058 if (ret) {
1059 printk(KERN_ERR "Failed to setup powerdomains\n");
1060 goto err2;
1061 }
1062
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001063 (void) clkdm_for_each(clkdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001064
1065 mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
1066 if (mpu_pwrdm == NULL) {
1067 printk(KERN_ERR "Failed to get mpu_pwrdm\n");
1068 goto err2;
1069 }
1070
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301071 neon_pwrdm = pwrdm_lookup("neon_pwrdm");
1072 per_pwrdm = pwrdm_lookup("per_pwrdm");
1073 core_pwrdm = pwrdm_lookup("core_pwrdm");
Tero Kristoc16c3f62008-12-11 16:46:57 +02001074 cam_pwrdm = pwrdm_lookup("cam_pwrdm");
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301075
Paul Walmsley55ed9692010-01-26 20:12:59 -07001076 neon_clkdm = clkdm_lookup("neon_clkdm");
1077 mpu_clkdm = clkdm_lookup("mpu_clkdm");
1078 per_clkdm = clkdm_lookup("per_clkdm");
1079 core_clkdm = clkdm_lookup("core_clkdm");
1080
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301081 omap_push_sram_idle();
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001082#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -07001083 suspend_set_ops(&omap_pm_ops);
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001084#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -07001085
1086 pm_idle = omap3_pm_idle;
Kalle Jokiniemi03433712008-09-26 11:04:20 +03001087 omap3_idle_init();
Kevin Hilman8bd22942009-05-28 10:56:16 -07001088
Paul Walmsley55ed9692010-01-26 20:12:59 -07001089 clkdm_add_wkdep(neon_clkdm, mpu_clkdm);
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301090 /*
1091 * REVISIT: This wkdep is only necessary when GPIO2-6 are enabled for
1092 * IO-pad wakeup. Otherwise it will unnecessarily waste power
1093 * waking up PER with every CORE wakeup - see
1094 * http://marc.info/?l=linux-omap&m=121852150710062&w=2
1095 */
Paul Walmsley55ed9692010-01-26 20:12:59 -07001096 clkdm_add_wkdep(per_clkdm, core_clkdm);
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301097
Tero Kristo27d59a42008-10-13 13:15:00 +03001098 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
1099 omap3_secure_ram_storage =
1100 kmalloc(0x803F, GFP_KERNEL);
1101 if (!omap3_secure_ram_storage)
1102 printk(KERN_ERR "Memory allocation failed when"
1103 "allocating for secure sram context\n");
Tero Kristo27d59a42008-10-13 13:15:00 +03001104
Tero Kristo9d971402008-12-12 11:20:05 +02001105 local_irq_disable();
1106 local_fiq_disable();
1107
1108 omap_dma_global_context_save();
1109 omap3_save_secure_ram_context(PWRDM_POWER_ON);
1110 omap_dma_global_context_restore();
1111
1112 local_irq_enable();
1113 local_fiq_enable();
1114 }
1115
1116 omap3_save_scratchpad_contents();
Kevin Hilman8bd22942009-05-28 10:56:16 -07001117err1:
1118 return ret;
1119err2:
1120 free_irq(INT_34XX_PRCM_MPU_IRQ, NULL);
1121 list_for_each_entry_safe(pwrst, tmp, &pwrst_list, node) {
1122 list_del(&pwrst->node);
1123 kfree(pwrst);
1124 }
1125 return ret;
1126}
1127
1128late_initcall(omap3_pm_init);