blob: b5e5bcbb4964f6c66f3f7d38be77d06e3daef2ce [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;
Ari Kauppi8e2efde2010-03-23 09:04:59 +020061u32 wakeup_timer_milliseconds;
Kevin Hilmanc40552b2009-10-06 14:25:09 -070062
Kevin Hilman8bd22942009-05-28 10:56:16 -070063struct power_state {
64 struct powerdomain *pwrdm;
65 u32 next_state;
Kevin Hilman10f90ed2009-06-24 11:39:18 -070066#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -070067 u32 saved_state;
Kevin Hilman10f90ed2009-06-24 11:39:18 -070068#endif
Kevin Hilman8bd22942009-05-28 10:56:16 -070069 struct list_head node;
70};
71
72static LIST_HEAD(pwrst_list);
73
74static void (*_omap_sram_idle)(u32 *addr, int save_state);
75
Tero Kristo27d59a42008-10-13 13:15:00 +030076static int (*_omap_save_secure_sram)(u32 *addr);
77
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +053078static struct powerdomain *mpu_pwrdm, *neon_pwrdm;
79static struct powerdomain *core_pwrdm, *per_pwrdm;
Tero Kristoc16c3f62008-12-11 16:46:57 +020080static struct powerdomain *cam_pwrdm;
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +053081
Rajendra Nayak2f5939c2008-09-26 17:50:07 +053082static inline void omap3_per_save_context(void)
83{
84 omap_gpio_save_context();
85}
86
87static inline void omap3_per_restore_context(void)
88{
89 omap_gpio_restore_context();
90}
91
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +020092static void omap3_enable_io_chain(void)
93{
94 int timeout = 0;
95
96 if (omap_rev() >= OMAP3430_REV_ES3_1) {
Paul Walmsley2bc4ef72010-05-18 18:47:24 -060097 prm_set_mod_reg_bits(OMAP3430_EN_IO_CHAIN_MASK, WKUP_MOD,
98 PM_WKEN);
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +020099 /* Do a readback to assure write has been done */
100 prm_read_mod_reg(WKUP_MOD, PM_WKEN);
101
Kevin Hilman0b96a3a2010-06-09 13:53:09 +0300102 while (!(prm_read_mod_reg(WKUP_MOD, PM_WKEN) &
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600103 OMAP3430_ST_IO_CHAIN_MASK)) {
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200104 timeout++;
105 if (timeout > 1000) {
106 printk(KERN_ERR "Wake up daisy chain "
107 "activation failed.\n");
108 return;
109 }
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600110 prm_set_mod_reg_bits(OMAP3430_ST_IO_CHAIN_MASK,
Kevin Hilman0b96a3a2010-06-09 13:53:09 +0300111 WKUP_MOD, PM_WKEN);
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200112 }
113 }
114}
115
116static void omap3_disable_io_chain(void)
117{
118 if (omap_rev() >= OMAP3430_REV_ES3_1)
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600119 prm_clear_mod_reg_bits(OMAP3430_EN_IO_CHAIN_MASK, WKUP_MOD,
120 PM_WKEN);
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200121}
122
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530123static void omap3_core_save_context(void)
124{
125 u32 control_padconf_off;
126
127 /* Save the padconf registers */
128 control_padconf_off = omap_ctrl_readl(OMAP343X_CONTROL_PADCONF_OFF);
129 control_padconf_off |= START_PADCONF_SAVE;
130 omap_ctrl_writel(control_padconf_off, OMAP343X_CONTROL_PADCONF_OFF);
131 /* wait for the save to complete */
Roel Kluin1b6e8212010-01-08 10:29:07 -0800132 while (!(omap_ctrl_readl(OMAP343X_CONTROL_GENERAL_PURPOSE_STATUS)
133 & PADCONF_SAVE_DONE))
Tero Kristodccaad82009-11-17 18:34:53 +0200134 udelay(1);
135
136 /*
137 * Force write last pad into memory, as this can fail in some
138 * cases according to erratas 1.157, 1.185
139 */
140 omap_ctrl_writel(omap_ctrl_readl(OMAP343X_PADCONF_ETK_D14),
141 OMAP343X_CONTROL_MEM_WKUP + 0x2a0);
142
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530143 /* Save the Interrupt controller context */
144 omap_intc_save_context();
145 /* Save the GPMC context */
146 omap3_gpmc_save_context();
147 /* Save the system control module context, padconf already save above*/
148 omap3_control_save_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000149 omap_dma_global_context_save();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530150}
151
152static void omap3_core_restore_context(void)
153{
154 /* Restore the control module context, padconf restored by h/w */
155 omap3_control_restore_context();
156 /* Restore the GPMC context */
157 omap3_gpmc_restore_context();
158 /* Restore the interrupt controller context */
159 omap_intc_restore_context();
Tero Kristof2d11852008-08-28 13:13:31 +0000160 omap_dma_global_context_restore();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530161}
162
Tero Kristo9d971402008-12-12 11:20:05 +0200163/*
164 * FIXME: This function should be called before entering off-mode after
165 * OMAP3 secure services have been accessed. Currently it is only called
166 * once during boot sequence, but this works as we are not using secure
167 * services.
168 */
Tero Kristo27d59a42008-10-13 13:15:00 +0300169static void omap3_save_secure_ram_context(u32 target_mpu_state)
170{
171 u32 ret;
172
173 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
Tero Kristo27d59a42008-10-13 13:15:00 +0300174 /*
175 * MPU next state must be set to POWER_ON temporarily,
176 * otherwise the WFI executed inside the ROM code
177 * will hang the system.
178 */
179 pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_ON);
180 ret = _omap_save_secure_sram((u32 *)
181 __pa(omap3_secure_ram_storage));
182 pwrdm_set_next_pwrst(mpu_pwrdm, target_mpu_state);
183 /* Following is for error tracking, it should not happen */
184 if (ret) {
185 printk(KERN_ERR "save_secure_sram() returns %08x\n",
186 ret);
187 while (1)
188 ;
189 }
190 }
191}
192
Jon Hunter77da2d92009-06-27 00:07:25 -0500193/*
194 * PRCM Interrupt Handler Helper Function
195 *
196 * The purpose of this function is to clear any wake-up events latched
197 * in the PRCM PM_WKST_x registers. It is possible that a wake-up event
198 * may occur whilst attempting to clear a PM_WKST_x register and thus
199 * set another bit in this register. A while loop is used to ensure
200 * that any peripheral wake-up events occurring while attempting to
201 * clear the PM_WKST_x are detected and cleared.
202 */
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700203static int prcm_clear_mod_irqs(s16 module, u8 regs)
Jon Hunter77da2d92009-06-27 00:07:25 -0500204{
Vikram Pandita71a80772009-07-17 19:33:09 -0500205 u32 wkst, fclk, iclk, clken;
Jon Hunter77da2d92009-06-27 00:07:25 -0500206 u16 wkst_off = (regs == 3) ? OMAP3430ES2_PM_WKST3 : PM_WKST1;
207 u16 fclk_off = (regs == 3) ? OMAP3430ES2_CM_FCLKEN3 : CM_FCLKEN1;
208 u16 iclk_off = (regs == 3) ? CM_ICLKEN3 : CM_ICLKEN1;
Paul Walmsley5d805972009-07-22 10:18:07 -0700209 u16 grpsel_off = (regs == 3) ?
210 OMAP3430ES2_PM_MPUGRPSEL3 : OMAP3430_PM_MPUGRPSEL;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700211 int c = 0;
Jon Hunter77da2d92009-06-27 00:07:25 -0500212
213 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley5d805972009-07-22 10:18:07 -0700214 wkst &= prm_read_mod_reg(module, grpsel_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500215 if (wkst) {
216 iclk = cm_read_mod_reg(module, iclk_off);
217 fclk = cm_read_mod_reg(module, fclk_off);
218 while (wkst) {
Vikram Pandita71a80772009-07-17 19:33:09 -0500219 clken = wkst;
220 cm_set_mod_reg_bits(clken, module, iclk_off);
221 /*
222 * For USBHOST, we don't know whether HOST1 or
223 * HOST2 woke us up, so enable both f-clocks
224 */
225 if (module == OMAP3430ES2_USBHOST_MOD)
226 clken |= 1 << OMAP3430ES2_EN_USBHOST2_SHIFT;
227 cm_set_mod_reg_bits(clken, module, fclk_off);
Jon Hunter77da2d92009-06-27 00:07:25 -0500228 prm_write_mod_reg(wkst, module, wkst_off);
229 wkst = prm_read_mod_reg(module, wkst_off);
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700230 c++;
Jon Hunter77da2d92009-06-27 00:07:25 -0500231 }
232 cm_write_mod_reg(iclk, module, iclk_off);
233 cm_write_mod_reg(fclk, module, fclk_off);
234 }
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700235
236 return c;
237}
238
239static int _prcm_int_handle_wakeup(void)
240{
241 int c;
242
243 c = prcm_clear_mod_irqs(WKUP_MOD, 1);
244 c += prcm_clear_mod_irqs(CORE_MOD, 1);
245 c += prcm_clear_mod_irqs(OMAP3430_PER_MOD, 1);
246 if (omap_rev() > OMAP3430_REV_ES1_0) {
247 c += prcm_clear_mod_irqs(CORE_MOD, 3);
248 c += prcm_clear_mod_irqs(OMAP3430ES2_USBHOST_MOD, 1);
249 }
250
251 return c;
Jon Hunter77da2d92009-06-27 00:07:25 -0500252}
253
254/*
255 * PRCM Interrupt Handler
256 *
257 * The PRM_IRQSTATUS_MPU register indicates if there are any pending
258 * interrupts from the PRCM for the MPU. These bits must be cleared in
259 * order to clear the PRCM interrupt. The PRCM interrupt handler is
260 * implemented to simply clear the PRM_IRQSTATUS_MPU in order to clear
261 * the PRCM interrupt. Please note that bit 0 of the PRM_IRQSTATUS_MPU
262 * register indicates that a wake-up event is pending for the MPU and
263 * this bit can only be cleared if the all the wake-up events latched
264 * in the various PM_WKST_x registers have been cleared. The interrupt
265 * handler is implemented using a do-while loop so that if a wake-up
266 * event occurred during the processing of the prcm interrupt handler
267 * (setting a bit in the corresponding PM_WKST_x register and thus
268 * preventing us from clearing bit 0 of the PRM_IRQSTATUS_MPU register)
269 * this would be handled.
270 */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700271static irqreturn_t prcm_interrupt_handler (int irq, void *dev_id)
272{
Kevin Hilmand6290a32010-04-26 14:59:09 -0700273 u32 irqenable_mpu, irqstatus_mpu;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700274 int c = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700275
Kevin Hilmand6290a32010-04-26 14:59:09 -0700276 irqenable_mpu = prm_read_mod_reg(OCP_MOD,
277 OMAP3_PRM_IRQENABLE_MPU_OFFSET);
278 irqstatus_mpu = prm_read_mod_reg(OCP_MOD,
279 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
280 irqstatus_mpu &= irqenable_mpu;
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700281
Kevin Hilmand6290a32010-04-26 14:59:09 -0700282 do {
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600283 if (irqstatus_mpu & (OMAP3430_WKUP_ST_MASK |
284 OMAP3430_IO_ST_MASK)) {
Paul Walmsley8cb0ac92009-07-22 10:29:02 -0700285 c = _prcm_int_handle_wakeup();
286
287 /*
288 * Is the MPU PRCM interrupt handler racing with the
289 * IVA2 PRCM interrupt handler ?
290 */
291 WARN(c == 0, "prcm: WARNING: PRCM indicated MPU wakeup "
292 "but no wakeup sources are marked\n");
293 } else {
294 /* XXX we need to expand our PRCM interrupt handler */
295 WARN(1, "prcm: WARNING: PRCM interrupt received, but "
296 "no code to handle it (%08x)\n", irqstatus_mpu);
297 }
298
Jon Hunter77da2d92009-06-27 00:07:25 -0500299 prm_write_mod_reg(irqstatus_mpu, OCP_MOD,
300 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700301
Kevin Hilmand6290a32010-04-26 14:59:09 -0700302 irqstatus_mpu = prm_read_mod_reg(OCP_MOD,
303 OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
304 irqstatus_mpu &= irqenable_mpu;
305
306 } while (irqstatus_mpu);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700307
308 return IRQ_HANDLED;
309}
310
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530311static void restore_control_register(u32 val)
312{
313 __asm__ __volatile__ ("mcr p15, 0, %0, c1, c0, 0" : : "r" (val));
314}
315
316/* Function to restore the table entry that was modified for enabling MMU */
317static void restore_table_entry(void)
318{
319 u32 *scratchpad_address;
320 u32 previous_value, control_reg_value;
321 u32 *address;
322
323 scratchpad_address = OMAP2_L4_IO_ADDRESS(OMAP343X_SCRATCHPAD);
324
325 /* Get address of entry that was modified */
326 address = (u32 *)__raw_readl(scratchpad_address +
327 OMAP343X_TABLE_ADDRESS_OFFSET);
328 /* Get the previous value which needs to be restored */
329 previous_value = __raw_readl(scratchpad_address +
330 OMAP343X_TABLE_VALUE_OFFSET);
331 address = __va(address);
332 *address = previous_value;
333 flush_tlb_all();
334 control_reg_value = __raw_readl(scratchpad_address
335 + OMAP343X_CONTROL_REG_VALUE_OFFSET);
336 /* This will enable caches and prediction */
337 restore_control_register(control_reg_value);
338}
339
Rajendra Nayak99e6a4d2008-10-08 17:30:58 +0530340void omap_sram_idle(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700341{
342 /* Variable to tell what needs to be saved and restored
343 * in omap_sram_idle*/
344 /* save_state = 0 => Nothing to save and restored */
345 /* save_state = 1 => Only L1 and logic lost */
346 /* save_state = 2 => Only L2 lost */
347 /* save_state = 3 => L1, L2 and logic lost */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530348 int save_state = 0;
349 int mpu_next_state = PWRDM_POWER_ON;
350 int per_next_state = PWRDM_POWER_ON;
351 int core_next_state = PWRDM_POWER_ON;
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530352 int core_prev_state, per_prev_state;
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300353 u32 sdrc_pwr = 0;
Tero Kristoecf157d2008-12-01 13:17:29 +0200354 int per_state_modified = 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700355
356 if (!_omap_sram_idle)
357 return;
358
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530359 pwrdm_clear_all_prev_pwrst(mpu_pwrdm);
360 pwrdm_clear_all_prev_pwrst(neon_pwrdm);
361 pwrdm_clear_all_prev_pwrst(core_pwrdm);
362 pwrdm_clear_all_prev_pwrst(per_pwrdm);
363
Kevin Hilman8bd22942009-05-28 10:56:16 -0700364 mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
365 switch (mpu_next_state) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530366 case PWRDM_POWER_ON:
Kevin Hilman8bd22942009-05-28 10:56:16 -0700367 case PWRDM_POWER_RET:
368 /* No need to save context */
369 save_state = 0;
370 break;
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530371 case PWRDM_POWER_OFF:
372 save_state = 3;
373 break;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700374 default:
375 /* Invalid state */
376 printk(KERN_ERR "Invalid mpu state in sram_idle\n");
377 return;
378 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300379 pwrdm_pre_transition();
380
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530381 /* NEON control */
382 if (pwrdm_read_pwrst(neon_pwrdm) == PWRDM_POWER_ON)
Jouni Hogander71391782008-10-28 10:59:05 +0200383 pwrdm_set_next_pwrst(neon_pwrdm, mpu_next_state);
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530384
Mike Chan40742fa2010-05-03 16:04:06 -0700385 /* Enable IO-PAD and IO-CHAIN wakeups */
Kevin Hilman658ce972008-11-04 20:50:52 -0800386 per_next_state = pwrdm_read_next_pwrst(per_pwrdm);
Tero Kristoecf157d2008-12-01 13:17:29 +0200387 core_next_state = pwrdm_read_next_pwrst(core_pwrdm);
Kevin Hilmand5c47d72010-08-10 16:04:35 -0700388 if (omap3_has_io_wakeup() &&
389 (per_next_state < PWRDM_POWER_ON ||
390 core_next_state < PWRDM_POWER_ON)) {
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600391 prm_set_mod_reg_bits(OMAP3430_EN_IO_MASK, WKUP_MOD, PM_WKEN);
Mike Chan40742fa2010-05-03 16:04:06 -0700392 omap3_enable_io_chain();
393 }
394
395 /* PER */
Kevin Hilman658ce972008-11-04 20:50:52 -0800396 if (per_next_state < PWRDM_POWER_ON) {
Kevin Hilman658ce972008-11-04 20:50:52 -0800397 omap_uart_prepare_idle(2);
Kevin Hilman43ffcd92009-01-27 11:09:24 -0800398 omap2_gpio_prepare_for_idle(per_next_state);
Tero Kristoecf157d2008-12-01 13:17:29 +0200399 if (per_next_state == PWRDM_POWER_OFF) {
400 if (core_next_state == PWRDM_POWER_ON) {
401 per_next_state = PWRDM_POWER_RET;
402 pwrdm_set_next_pwrst(per_pwrdm, per_next_state);
403 per_state_modified = 1;
Kevin Hilman43ffcd92009-01-27 11:09:24 -0800404 } else
Tero Kristoecf157d2008-12-01 13:17:29 +0200405 omap3_per_save_context();
406 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800407 }
408
Tero Kristoc16c3f62008-12-11 16:46:57 +0200409 if (pwrdm_read_pwrst(cam_pwrdm) == PWRDM_POWER_ON)
410 omap2_clkdm_deny_idle(mpu_pwrdm->pwrdm_clkdms[0]);
411
Kevin Hilman658ce972008-11-04 20:50:52 -0800412 /* CORE */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530413 if (core_next_state < PWRDM_POWER_ON) {
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530414 omap_uart_prepare_idle(0);
415 omap_uart_prepare_idle(1);
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530416 if (core_next_state == PWRDM_POWER_OFF) {
417 omap3_core_save_context();
418 omap3_prcm_save_context();
419 }
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530420 }
Mike Chan40742fa2010-05-03 16:04:06 -0700421
Tero Kristof18cc2f2009-10-23 19:03:50 +0300422 omap3_intc_prepare_idle();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700423
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530424 /*
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530425 * On EMU/HS devices ROM code restores a SRDC value
426 * from scratchpad which has automatic self refresh on timeout
427 * of AUTO_CNT = 1 enabled. This takes care of errata 1.142.
428 * Hence store/restore the SDRC_POWER register here.
429 */
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300430 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
431 omap_type() != OMAP2_DEVICE_TYPE_GP &&
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530432 core_next_state == PWRDM_POWER_OFF)
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300433 sdrc_pwr = sdrc_read_reg(SDRC_POWER);
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300434
435 /*
Rajendra Nayak61255ab2008-09-26 17:49:56 +0530436 * omap3_arm_context is the location where ARM registers
437 * get saved. The restore path then reads from this
438 * location and restores them back.
439 */
440 _omap_sram_idle(omap3_arm_context, save_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700441 cpu_init();
442
Rajendra Nayakf265dc42009-06-09 22:30:41 +0530443 /* Restore normal SDRC POWER settings */
Tero Kristo13a6fe0f2008-10-13 13:17:06 +0300444 if (omap_rev() >= OMAP3430_REV_ES3_0 &&
445 omap_type() != OMAP2_DEVICE_TYPE_GP &&
446 core_next_state == PWRDM_POWER_OFF)
447 sdrc_write_reg(sdrc_pwr, SDRC_POWER);
448
Rajendra Nayak57f277b2008-09-26 17:49:34 +0530449 /* Restore table entry modified during MMU restoration */
450 if (pwrdm_read_prev_pwrst(mpu_pwrdm) == PWRDM_POWER_OFF)
451 restore_table_entry();
452
Kevin Hilman658ce972008-11-04 20:50:52 -0800453 /* CORE */
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530454 if (core_next_state < PWRDM_POWER_ON) {
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530455 core_prev_state = pwrdm_read_prev_pwrst(core_pwrdm);
456 if (core_prev_state == PWRDM_POWER_OFF) {
457 omap3_core_restore_context();
458 omap3_prcm_restore_context();
459 omap3_sram_restore_context();
Kalle Jokiniemi8a917d22009-05-13 13:32:11 +0300460 omap2_sms_restore_context();
Rajendra Nayak2f5939c2008-09-26 17:50:07 +0530461 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800462 omap_uart_resume_idle(0);
463 omap_uart_resume_idle(1);
464 if (core_next_state == PWRDM_POWER_OFF)
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600465 prm_clear_mod_reg_bits(OMAP3430_AUTO_OFF_MASK,
Kevin Hilman658ce972008-11-04 20:50:52 -0800466 OMAP3430_GR_MOD,
467 OMAP3_PRM_VOLTCTRL_OFFSET);
468 }
Tero Kristof18cc2f2009-10-23 19:03:50 +0300469 omap3_intc_resume_idle();
Kevin Hilman658ce972008-11-04 20:50:52 -0800470
471 /* PER */
472 if (per_next_state < PWRDM_POWER_ON) {
473 per_prev_state = pwrdm_read_prev_pwrst(per_pwrdm);
Kevin Hilman43ffcd92009-01-27 11:09:24 -0800474 omap2_gpio_resume_after_idle();
475 if (per_prev_state == PWRDM_POWER_OFF)
Kevin Hilman658ce972008-11-04 20:50:52 -0800476 omap3_per_restore_context();
Tero Kristoecf157d2008-12-01 13:17:29 +0200477 omap_uart_resume_idle(2);
478 if (per_state_modified)
479 pwrdm_set_next_pwrst(per_pwrdm, PWRDM_POWER_OFF);
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +0530480 }
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300481
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200482 /* Disable IO-PAD and IO-CHAIN wakeup */
Kevin Hilman58a55592010-08-16 09:21:19 +0300483 if (omap3_has_io_wakeup() &&
484 (per_next_state < PWRDM_POWER_ON ||
485 core_next_state < PWRDM_POWER_ON)) {
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600486 prm_clear_mod_reg_bits(OMAP3430_EN_IO_MASK, WKUP_MOD, PM_WKEN);
Kalle Jokiniemi3a7ec262009-03-26 15:59:01 +0200487 omap3_disable_io_chain();
488 }
Kevin Hilman658ce972008-11-04 20:50:52 -0800489
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300490 pwrdm_post_transition();
491
Tero Kristoc16c3f62008-12-11 16:46:57 +0200492 omap2_clkdm_allow_idle(mpu_pwrdm->pwrdm_clkdms[0]);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700493}
494
Rajendra Nayak20b01662008-10-08 17:31:22 +0530495int omap3_can_sleep(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700496{
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700497 if (!sleep_while_idle)
498 return 0;
Kevin Hilman4af40162009-02-04 10:51:40 -0800499 if (!omap_uart_can_sleep())
500 return 0;
Kevin Hilman8bd22942009-05-28 10:56:16 -0700501 return 1;
502}
503
504/* This sets pwrdm state (other than mpu & core. Currently only ON &
505 * RET are supported. Function is assuming that clkdm doesn't have
506 * hw_sup mode enabled. */
Rajendra Nayak20b01662008-10-08 17:31:22 +0530507int set_pwrdm_state(struct powerdomain *pwrdm, u32 state)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700508{
509 u32 cur_state;
510 int sleep_switch = 0;
511 int ret = 0;
512
513 if (pwrdm == NULL || IS_ERR(pwrdm))
514 return -EINVAL;
515
516 while (!(pwrdm->pwrsts & (1 << state))) {
517 if (state == PWRDM_POWER_OFF)
518 return ret;
519 state--;
520 }
521
522 cur_state = pwrdm_read_next_pwrst(pwrdm);
523 if (cur_state == state)
524 return ret;
525
526 if (pwrdm_read_pwrst(pwrdm) < PWRDM_POWER_ON) {
527 omap2_clkdm_wakeup(pwrdm->pwrdm_clkdms[0]);
528 sleep_switch = 1;
529 pwrdm_wait_transition(pwrdm);
530 }
531
532 ret = pwrdm_set_next_pwrst(pwrdm, state);
533 if (ret) {
534 printk(KERN_ERR "Unable to set state of powerdomain: %s\n",
535 pwrdm->name);
536 goto err;
537 }
538
539 if (sleep_switch) {
540 omap2_clkdm_allow_idle(pwrdm->pwrdm_clkdms[0]);
541 pwrdm_wait_transition(pwrdm);
Peter 'p2' De Schrijverfe617af2008-10-15 17:48:44 +0300542 pwrdm_state_switch(pwrdm);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700543 }
544
545err:
546 return ret;
547}
548
549static void omap3_pm_idle(void)
550{
551 local_irq_disable();
552 local_fiq_disable();
553
554 if (!omap3_can_sleep())
555 goto out;
556
Tero Kristocf228542009-03-20 15:21:02 +0200557 if (omap_irq_pending() || need_resched())
Kevin Hilman8bd22942009-05-28 10:56:16 -0700558 goto out;
559
560 omap_sram_idle();
561
562out:
563 local_fiq_enable();
564 local_irq_enable();
565}
566
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700567#ifdef CONFIG_SUSPEND
Tero Kristo24662112009-03-05 16:32:23 +0200568static suspend_state_t suspend_state;
569
Ari Kauppi8e2efde2010-03-23 09:04:59 +0200570static void omap2_pm_wakeup_on_timer(u32 seconds, u32 milliseconds)
Kevin Hilmand7814e42009-10-06 14:30:23 -0700571{
572 u32 tick_rate, cycles;
573
Ari Kauppi8e2efde2010-03-23 09:04:59 +0200574 if (!seconds && !milliseconds)
Kevin Hilmand7814e42009-10-06 14:30:23 -0700575 return;
576
577 tick_rate = clk_get_rate(omap_dm_timer_get_fclk(gptimer_wakeup));
Ari Kauppi8e2efde2010-03-23 09:04:59 +0200578 cycles = tick_rate * seconds + tick_rate * milliseconds / 1000;
Kevin Hilmand7814e42009-10-06 14:30:23 -0700579 omap_dm_timer_stop(gptimer_wakeup);
580 omap_dm_timer_set_load_start(gptimer_wakeup, 0, 0xffffffff - cycles);
581
Ari Kauppi8e2efde2010-03-23 09:04:59 +0200582 pr_info("PM: Resume timer in %u.%03u secs"
583 " (%d ticks at %d ticks/sec.)\n",
584 seconds, milliseconds, cycles, tick_rate);
Kevin Hilmand7814e42009-10-06 14:30:23 -0700585}
586
Kevin Hilman8bd22942009-05-28 10:56:16 -0700587static int omap3_pm_prepare(void)
588{
589 disable_hlt();
590 return 0;
591}
592
593static int omap3_pm_suspend(void)
594{
595 struct power_state *pwrst;
596 int state, ret = 0;
597
Ari Kauppi8e2efde2010-03-23 09:04:59 +0200598 if (wakeup_timer_seconds || wakeup_timer_milliseconds)
599 omap2_pm_wakeup_on_timer(wakeup_timer_seconds,
600 wakeup_timer_milliseconds);
Kevin Hilmand7814e42009-10-06 14:30:23 -0700601
Kevin Hilman8bd22942009-05-28 10:56:16 -0700602 /* Read current next_pwrsts */
603 list_for_each_entry(pwrst, &pwrst_list, node)
604 pwrst->saved_state = pwrdm_read_next_pwrst(pwrst->pwrdm);
605 /* Set ones wanted by suspend */
606 list_for_each_entry(pwrst, &pwrst_list, node) {
607 if (set_pwrdm_state(pwrst->pwrdm, pwrst->next_state))
608 goto restore;
609 if (pwrdm_clear_all_prev_pwrst(pwrst->pwrdm))
610 goto restore;
611 }
612
Kevin Hilman4af40162009-02-04 10:51:40 -0800613 omap_uart_prepare_suspend();
Tero Kristo2bbe3af2009-10-23 19:03:48 +0300614 omap3_intc_suspend();
615
Kevin Hilman8bd22942009-05-28 10:56:16 -0700616 omap_sram_idle();
617
618restore:
619 /* Restore next_pwrsts */
620 list_for_each_entry(pwrst, &pwrst_list, node) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700621 state = pwrdm_read_prev_pwrst(pwrst->pwrdm);
622 if (state > pwrst->next_state) {
623 printk(KERN_INFO "Powerdomain (%s) didn't enter "
624 "target state %d\n",
625 pwrst->pwrdm->name, pwrst->next_state);
626 ret = -1;
627 }
Jouni Hogander6c5f8032008-10-29 12:06:04 +0200628 set_pwrdm_state(pwrst->pwrdm, pwrst->saved_state);
Kevin Hilman8bd22942009-05-28 10:56:16 -0700629 }
630 if (ret)
631 printk(KERN_ERR "Could not enter target state in pm_suspend\n");
632 else
633 printk(KERN_INFO "Successfully put all powerdomains "
634 "to target state\n");
635
636 return ret;
637}
638
Tero Kristo24662112009-03-05 16:32:23 +0200639static int omap3_pm_enter(suspend_state_t unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700640{
641 int ret = 0;
642
Tero Kristo24662112009-03-05 16:32:23 +0200643 switch (suspend_state) {
Kevin Hilman8bd22942009-05-28 10:56:16 -0700644 case PM_SUSPEND_STANDBY:
645 case PM_SUSPEND_MEM:
646 ret = omap3_pm_suspend();
647 break;
648 default:
649 ret = -EINVAL;
650 }
651
652 return ret;
653}
654
655static void omap3_pm_finish(void)
656{
657 enable_hlt();
658}
659
Tero Kristo24662112009-03-05 16:32:23 +0200660/* Hooks to enable / disable UART interrupts during suspend */
661static int omap3_pm_begin(suspend_state_t state)
662{
663 suspend_state = state;
664 omap_uart_enable_irqs(0);
665 return 0;
666}
667
668static void omap3_pm_end(void)
669{
670 suspend_state = PM_SUSPEND_ON;
671 omap_uart_enable_irqs(1);
672 return;
673}
674
Kevin Hilman8bd22942009-05-28 10:56:16 -0700675static struct platform_suspend_ops omap_pm_ops = {
Tero Kristo24662112009-03-05 16:32:23 +0200676 .begin = omap3_pm_begin,
677 .end = omap3_pm_end,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700678 .prepare = omap3_pm_prepare,
679 .enter = omap3_pm_enter,
680 .finish = omap3_pm_finish,
681 .valid = suspend_valid_only_mem,
682};
Kevin Hilman10f90ed2009-06-24 11:39:18 -0700683#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -0700684
Kevin Hilman1155e422008-11-25 11:48:24 -0800685
686/**
687 * omap3_iva_idle(): ensure IVA is in idle so it can be put into
688 * retention
689 *
690 * In cases where IVA2 is activated by bootcode, it may prevent
691 * full-chip retention or off-mode because it is not idle. This
692 * function forces the IVA2 into idle state so it can go
693 * into retention/off and thus allow full-chip retention/off.
694 *
695 **/
696static void __init omap3_iva_idle(void)
697{
698 /* ensure IVA2 clock is disabled */
699 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
700
701 /* if no clock activity, nothing else to do */
702 if (!(cm_read_mod_reg(OMAP3430_IVA2_MOD, OMAP3430_CM_CLKSTST) &
703 OMAP3430_CLKACTIVITY_IVA2_MASK))
704 return;
705
706 /* Reset IVA2 */
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600707 prm_write_mod_reg(OMAP3430_RST1_IVA2_MASK |
708 OMAP3430_RST2_IVA2_MASK |
709 OMAP3430_RST3_IVA2_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700710 OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
Kevin Hilman1155e422008-11-25 11:48:24 -0800711
712 /* Enable IVA2 clock */
Kevin Hilmandfa6d6f2010-02-24 12:05:48 -0700713 cm_write_mod_reg(OMAP3430_CM_FCLKEN_IVA2_EN_IVA2_MASK,
Kevin Hilman1155e422008-11-25 11:48:24 -0800714 OMAP3430_IVA2_MOD, CM_FCLKEN);
715
716 /* Set IVA2 boot mode to 'idle' */
717 omap_ctrl_writel(OMAP3_IVA2_BOOTMOD_IDLE,
718 OMAP343X_CONTROL_IVA2_BOOTMOD);
719
720 /* Un-reset IVA2 */
Abhijit Pagare37903002010-01-26 20:12:51 -0700721 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
Kevin Hilman1155e422008-11-25 11:48:24 -0800722
723 /* Disable IVA2 clock */
724 cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
725
726 /* Reset IVA2 */
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600727 prm_write_mod_reg(OMAP3430_RST1_IVA2_MASK |
728 OMAP3430_RST2_IVA2_MASK |
729 OMAP3430_RST3_IVA2_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700730 OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
Kevin Hilman1155e422008-11-25 11:48:24 -0800731}
732
Kevin Hilman8111b222009-04-28 15:27:44 -0700733static void __init omap3_d2d_idle(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -0700734{
Kevin Hilman8111b222009-04-28 15:27:44 -0700735 u16 mask, padconf;
736
737 /* In a stand alone OMAP3430 where there is not a stacked
738 * modem for the D2D Idle Ack and D2D MStandby must be pulled
739 * high. S CONTROL_PADCONF_SAD2D_IDLEACK and
740 * CONTROL_PADCONF_SAD2D_MSTDBY to have a pull up. */
741 mask = (1 << 4) | (1 << 3); /* pull-up, enabled */
742 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_MSTANDBY);
743 padconf |= mask;
744 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_MSTANDBY);
745
746 padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_IDLEACK);
747 padconf |= mask;
748 omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_IDLEACK);
749
Kevin Hilman8bd22942009-05-28 10:56:16 -0700750 /* reset modem */
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600751 prm_write_mod_reg(OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RSTPWRON_MASK |
752 OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RST_MASK,
Abhijit Pagare37903002010-01-26 20:12:51 -0700753 CORE_MOD, OMAP2_RM_RSTCTRL);
754 prm_write_mod_reg(0, CORE_MOD, OMAP2_RM_RSTCTRL);
Kevin Hilman8111b222009-04-28 15:27:44 -0700755}
Kevin Hilman8bd22942009-05-28 10:56:16 -0700756
Kevin Hilman8111b222009-04-28 15:27:44 -0700757static void __init prcm_setup_regs(void)
758{
Kevin Hilman8bd22942009-05-28 10:56:16 -0700759 /* XXX Reset all wkdeps. This should be done when initializing
760 * powerdomains */
761 prm_write_mod_reg(0, OMAP3430_IVA2_MOD, PM_WKDEP);
762 prm_write_mod_reg(0, MPU_MOD, PM_WKDEP);
763 prm_write_mod_reg(0, OMAP3430_DSS_MOD, PM_WKDEP);
764 prm_write_mod_reg(0, OMAP3430_NEON_MOD, PM_WKDEP);
765 prm_write_mod_reg(0, OMAP3430_CAM_MOD, PM_WKDEP);
766 prm_write_mod_reg(0, OMAP3430_PER_MOD, PM_WKDEP);
767 if (omap_rev() > OMAP3430_REV_ES1_0) {
768 prm_write_mod_reg(0, OMAP3430ES2_SGX_MOD, PM_WKDEP);
769 prm_write_mod_reg(0, OMAP3430ES2_USBHOST_MOD, PM_WKDEP);
770 } else
771 prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
772
773 /*
774 * Enable interface clock autoidle for all modules.
775 * Note that in the long run this should be done by clockfw
776 */
777 cm_write_mod_reg(
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600778 OMAP3430_AUTO_MODEM_MASK |
779 OMAP3430ES2_AUTO_MMC3_MASK |
780 OMAP3430ES2_AUTO_ICR_MASK |
781 OMAP3430_AUTO_AES2_MASK |
782 OMAP3430_AUTO_SHA12_MASK |
783 OMAP3430_AUTO_DES2_MASK |
784 OMAP3430_AUTO_MMC2_MASK |
785 OMAP3430_AUTO_MMC1_MASK |
786 OMAP3430_AUTO_MSPRO_MASK |
787 OMAP3430_AUTO_HDQ_MASK |
788 OMAP3430_AUTO_MCSPI4_MASK |
789 OMAP3430_AUTO_MCSPI3_MASK |
790 OMAP3430_AUTO_MCSPI2_MASK |
791 OMAP3430_AUTO_MCSPI1_MASK |
792 OMAP3430_AUTO_I2C3_MASK |
793 OMAP3430_AUTO_I2C2_MASK |
794 OMAP3430_AUTO_I2C1_MASK |
795 OMAP3430_AUTO_UART2_MASK |
796 OMAP3430_AUTO_UART1_MASK |
797 OMAP3430_AUTO_GPT11_MASK |
798 OMAP3430_AUTO_GPT10_MASK |
799 OMAP3430_AUTO_MCBSP5_MASK |
800 OMAP3430_AUTO_MCBSP1_MASK |
801 OMAP3430ES1_AUTO_FAC_MASK | /* This is es1 only */
802 OMAP3430_AUTO_MAILBOXES_MASK |
803 OMAP3430_AUTO_OMAPCTRL_MASK |
804 OMAP3430ES1_AUTO_FSHOSTUSB_MASK |
805 OMAP3430_AUTO_HSOTGUSB_MASK |
806 OMAP3430_AUTO_SAD2D_MASK |
807 OMAP3430_AUTO_SSI_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700808 CORE_MOD, CM_AUTOIDLE1);
809
810 cm_write_mod_reg(
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600811 OMAP3430_AUTO_PKA_MASK |
812 OMAP3430_AUTO_AES1_MASK |
813 OMAP3430_AUTO_RNG_MASK |
814 OMAP3430_AUTO_SHA11_MASK |
815 OMAP3430_AUTO_DES1_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700816 CORE_MOD, CM_AUTOIDLE2);
817
818 if (omap_rev() > OMAP3430_REV_ES1_0) {
819 cm_write_mod_reg(
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600820 OMAP3430_AUTO_MAD2D_MASK |
821 OMAP3430ES2_AUTO_USBTLL_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700822 CORE_MOD, CM_AUTOIDLE3);
823 }
824
825 cm_write_mod_reg(
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600826 OMAP3430_AUTO_WDT2_MASK |
827 OMAP3430_AUTO_WDT1_MASK |
828 OMAP3430_AUTO_GPIO1_MASK |
829 OMAP3430_AUTO_32KSYNC_MASK |
830 OMAP3430_AUTO_GPT12_MASK |
831 OMAP3430_AUTO_GPT1_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700832 WKUP_MOD, CM_AUTOIDLE);
833
834 cm_write_mod_reg(
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600835 OMAP3430_AUTO_DSS_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700836 OMAP3430_DSS_MOD,
837 CM_AUTOIDLE);
838
839 cm_write_mod_reg(
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600840 OMAP3430_AUTO_CAM_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700841 OMAP3430_CAM_MOD,
842 CM_AUTOIDLE);
843
844 cm_write_mod_reg(
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600845 OMAP3430_AUTO_GPIO6_MASK |
846 OMAP3430_AUTO_GPIO5_MASK |
847 OMAP3430_AUTO_GPIO4_MASK |
848 OMAP3430_AUTO_GPIO3_MASK |
849 OMAP3430_AUTO_GPIO2_MASK |
850 OMAP3430_AUTO_WDT3_MASK |
851 OMAP3430_AUTO_UART3_MASK |
852 OMAP3430_AUTO_GPT9_MASK |
853 OMAP3430_AUTO_GPT8_MASK |
854 OMAP3430_AUTO_GPT7_MASK |
855 OMAP3430_AUTO_GPT6_MASK |
856 OMAP3430_AUTO_GPT5_MASK |
857 OMAP3430_AUTO_GPT4_MASK |
858 OMAP3430_AUTO_GPT3_MASK |
859 OMAP3430_AUTO_GPT2_MASK |
860 OMAP3430_AUTO_MCBSP4_MASK |
861 OMAP3430_AUTO_MCBSP3_MASK |
862 OMAP3430_AUTO_MCBSP2_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700863 OMAP3430_PER_MOD,
864 CM_AUTOIDLE);
865
866 if (omap_rev() > OMAP3430_REV_ES1_0) {
867 cm_write_mod_reg(
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600868 OMAP3430ES2_AUTO_USBHOST_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700869 OMAP3430ES2_USBHOST_MOD,
870 CM_AUTOIDLE);
871 }
872
Paul Walmsley2fd0f752010-05-18 18:40:23 -0600873 omap_ctrl_writel(OMAP3430_AUTOIDLE_MASK, OMAP2_CONTROL_SYSCONFIG);
Tero Kristob296c812009-10-23 19:03:49 +0300874
Kevin Hilman8bd22942009-05-28 10:56:16 -0700875 /*
876 * Set all plls to autoidle. This is needed until autoidle is
877 * enabled by clockfw
878 */
879 cm_write_mod_reg(1 << OMAP3430_AUTO_IVA2_DPLL_SHIFT,
880 OMAP3430_IVA2_MOD, CM_AUTOIDLE2);
881 cm_write_mod_reg(1 << OMAP3430_AUTO_MPU_DPLL_SHIFT,
882 MPU_MOD,
883 CM_AUTOIDLE2);
884 cm_write_mod_reg((1 << OMAP3430_AUTO_PERIPH_DPLL_SHIFT) |
885 (1 << OMAP3430_AUTO_CORE_DPLL_SHIFT),
886 PLL_MOD,
887 CM_AUTOIDLE);
888 cm_write_mod_reg(1 << OMAP3430ES2_AUTO_PERIPH2_DPLL_SHIFT,
889 PLL_MOD,
890 CM_AUTOIDLE2);
891
892 /*
893 * Enable control of expternal oscillator through
894 * sys_clkreq. In the long run clock framework should
895 * take care of this.
896 */
897 prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK,
898 1 << OMAP_AUTOEXTCLKMODE_SHIFT,
899 OMAP3430_GR_MOD,
900 OMAP3_PRM_CLKSRC_CTRL_OFFSET);
901
902 /* setup wakup source */
Paul Walmsley2fd0f752010-05-18 18:40:23 -0600903 prm_write_mod_reg(OMAP3430_EN_IO_MASK | OMAP3430_EN_GPIO1_MASK |
904 OMAP3430_EN_GPT1_MASK | OMAP3430_EN_GPT12_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700905 WKUP_MOD, PM_WKEN);
906 /* No need to write EN_IO, that is always enabled */
Paul Walmsley275f6752010-05-18 18:40:23 -0600907 prm_write_mod_reg(OMAP3430_GRPSEL_GPIO1_MASK |
908 OMAP3430_GRPSEL_GPT1_MASK |
909 OMAP3430_GRPSEL_GPT12_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700910 WKUP_MOD, OMAP3430_PM_MPUGRPSEL);
911 /* For some reason IO doesn't generate wakeup event even if
912 * it is selected to mpu wakeup goup */
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600913 prm_write_mod_reg(OMAP3430_IO_EN_MASK | OMAP3430_WKUP_EN_MASK,
Kevin Hilman8bd22942009-05-28 10:56:16 -0700914 OCP_MOD, OMAP3_PRM_IRQENABLE_MPU_OFFSET);
Kevin Hilman1155e422008-11-25 11:48:24 -0800915
Subramani Venkateshb92c5722009-12-22 15:07:50 +0530916 /* Enable PM_WKEN to support DSS LPR */
Paul Walmsley2bc4ef72010-05-18 18:47:24 -0600917 prm_write_mod_reg(OMAP3430_PM_WKEN_DSS_EN_DSS_MASK,
Subramani Venkateshb92c5722009-12-22 15:07:50 +0530918 OMAP3430_DSS_MOD, PM_WKEN);
919
Kevin Hilmanb427f922009-10-22 14:48:13 -0700920 /* Enable wakeups in PER */
Paul Walmsley2fd0f752010-05-18 18:40:23 -0600921 prm_write_mod_reg(OMAP3430_EN_GPIO2_MASK | OMAP3430_EN_GPIO3_MASK |
922 OMAP3430_EN_GPIO4_MASK | OMAP3430_EN_GPIO5_MASK |
923 OMAP3430_EN_GPIO6_MASK | OMAP3430_EN_UART3_MASK |
924 OMAP3430_EN_MCBSP2_MASK | OMAP3430_EN_MCBSP3_MASK |
925 OMAP3430_EN_MCBSP4_MASK,
Kevin Hilmanb427f922009-10-22 14:48:13 -0700926 OMAP3430_PER_MOD, PM_WKEN);
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000927 /* and allow them to wake up MPU */
Paul Walmsley275f6752010-05-18 18:40:23 -0600928 prm_write_mod_reg(OMAP3430_GRPSEL_GPIO2_MASK |
929 OMAP3430_GRPSEL_GPIO3_MASK |
930 OMAP3430_GRPSEL_GPIO4_MASK |
931 OMAP3430_GRPSEL_GPIO5_MASK |
932 OMAP3430_GRPSEL_GPIO6_MASK |
933 OMAP3430_GRPSEL_UART3_MASK |
934 OMAP3430_GRPSEL_MCBSP2_MASK |
935 OMAP3430_GRPSEL_MCBSP3_MASK |
936 OMAP3430_GRPSEL_MCBSP4_MASK,
Kevin Hilmaneb350f72009-09-10 15:53:08 +0000937 OMAP3430_PER_MOD, OMAP3430_PM_MPUGRPSEL);
938
Kevin Hilmand3fd3292009-05-05 16:34:25 -0700939 /* Don't attach IVA interrupts */
940 prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
941 prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
942 prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
943 prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
944
Kevin Hilmanb1340d12009-04-27 16:14:54 -0700945 /* Clear any pending 'reset' flags */
Abhijit Pagare37903002010-01-26 20:12:51 -0700946 prm_write_mod_reg(0xffffffff, MPU_MOD, OMAP2_RM_RSTST);
947 prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP2_RM_RSTST);
948 prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, OMAP2_RM_RSTST);
949 prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, OMAP2_RM_RSTST);
950 prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, OMAP2_RM_RSTST);
951 prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, OMAP2_RM_RSTST);
952 prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, OMAP2_RM_RSTST);
Kevin Hilmanb1340d12009-04-27 16:14:54 -0700953
Kevin Hilman014c46d2009-04-27 07:50:23 -0700954 /* Clear any pending PRCM interrupts */
955 prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
956
Kevin Hilman1155e422008-11-25 11:48:24 -0800957 omap3_iva_idle();
Kevin Hilman8111b222009-04-28 15:27:44 -0700958 omap3_d2d_idle();
Kevin Hilman8bd22942009-05-28 10:56:16 -0700959}
960
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700961void omap3_pm_off_mode_enable(int enable)
962{
963 struct power_state *pwrst;
964 u32 state;
965
966 if (enable)
967 state = PWRDM_POWER_OFF;
968 else
969 state = PWRDM_POWER_RET;
970
Sanjeev Premi6af83b32010-01-28 23:16:43 +0530971#ifdef CONFIG_CPU_IDLE
972 omap3_cpuidle_update_states();
973#endif
974
Kevin Hilmanc40552b2009-10-06 14:25:09 -0700975 list_for_each_entry(pwrst, &pwrst_list, node) {
976 pwrst->next_state = state;
977 set_pwrdm_state(pwrst->pwrdm, state);
978 }
979}
980
Tero Kristo68d47782008-11-26 12:26:24 +0200981int omap3_pm_get_suspend_state(struct powerdomain *pwrdm)
982{
983 struct power_state *pwrst;
984
985 list_for_each_entry(pwrst, &pwrst_list, node) {
986 if (pwrst->pwrdm == pwrdm)
987 return pwrst->next_state;
988 }
989 return -EINVAL;
990}
991
992int omap3_pm_set_suspend_state(struct powerdomain *pwrdm, int state)
993{
994 struct power_state *pwrst;
995
996 list_for_each_entry(pwrst, &pwrst_list, node) {
997 if (pwrst->pwrdm == pwrdm) {
998 pwrst->next_state = state;
999 return 0;
1000 }
1001 }
1002 return -EINVAL;
1003}
1004
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001005static int __init pwrdms_setup(struct powerdomain *pwrdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -07001006{
1007 struct power_state *pwrst;
1008
1009 if (!pwrdm->pwrsts)
1010 return 0;
1011
Ming Leid3d381c2009-08-22 21:20:26 +08001012 pwrst = kmalloc(sizeof(struct power_state), GFP_ATOMIC);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001013 if (!pwrst)
1014 return -ENOMEM;
1015 pwrst->pwrdm = pwrdm;
1016 pwrst->next_state = PWRDM_POWER_RET;
1017 list_add(&pwrst->node, &pwrst_list);
1018
1019 if (pwrdm_has_hdwr_sar(pwrdm))
1020 pwrdm_enable_hdwr_sar(pwrdm);
1021
1022 return set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
1023}
1024
1025/*
1026 * Enable hw supervised mode for all clockdomains if it's
1027 * supported. Initiate sleep transition for other clockdomains, if
1028 * they are not used
1029 */
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001030static int __init clkdms_setup(struct clockdomain *clkdm, void *unused)
Kevin Hilman8bd22942009-05-28 10:56:16 -07001031{
Paul Walmsley369d5612010-01-26 20:13:01 -07001032 clkdm_clear_all_wkdeps(clkdm);
1033 clkdm_clear_all_sleepdeps(clkdm);
1034
Kevin Hilman8bd22942009-05-28 10:56:16 -07001035 if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
1036 omap2_clkdm_allow_idle(clkdm);
1037 else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
1038 atomic_read(&clkdm->usecount) == 0)
1039 omap2_clkdm_sleep(clkdm);
1040 return 0;
1041}
1042
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301043void omap_push_sram_idle(void)
1044{
1045 _omap_sram_idle = omap_sram_push(omap34xx_cpu_suspend,
1046 omap34xx_cpu_suspend_sz);
Tero Kristo27d59a42008-10-13 13:15:00 +03001047 if (omap_type() != OMAP2_DEVICE_TYPE_GP)
1048 _omap_save_secure_sram = omap_sram_push(save_secure_ram_context,
1049 save_secure_ram_context_sz);
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301050}
1051
Kevin Hilman7cc515f2009-06-10 09:02:25 -07001052static int __init omap3_pm_init(void)
Kevin Hilman8bd22942009-05-28 10:56:16 -07001053{
1054 struct power_state *pwrst, *tmp;
Paul Walmsley55ed9692010-01-26 20:12:59 -07001055 struct clockdomain *neon_clkdm, *per_clkdm, *mpu_clkdm, *core_clkdm;
Kevin Hilman8bd22942009-05-28 10:56:16 -07001056 int ret;
1057
1058 if (!cpu_is_omap34xx())
1059 return -ENODEV;
1060
1061 printk(KERN_ERR "Power Management for TI OMAP3.\n");
1062
1063 /* XXX prcm_setup_regs needs to be before enabling hw
1064 * supervised mode for powerdomains */
1065 prcm_setup_regs();
1066
1067 ret = request_irq(INT_34XX_PRCM_MPU_IRQ,
1068 (irq_handler_t)prcm_interrupt_handler,
1069 IRQF_DISABLED, "prcm", NULL);
1070 if (ret) {
1071 printk(KERN_ERR "request_irq failed to register for 0x%x\n",
1072 INT_34XX_PRCM_MPU_IRQ);
1073 goto err1;
1074 }
1075
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001076 ret = pwrdm_for_each(pwrdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001077 if (ret) {
1078 printk(KERN_ERR "Failed to setup powerdomains\n");
1079 goto err2;
1080 }
1081
Peter 'p2' De Schrijvera23456e2008-10-15 18:13:47 +03001082 (void) clkdm_for_each(clkdms_setup, NULL);
Kevin Hilman8bd22942009-05-28 10:56:16 -07001083
1084 mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
1085 if (mpu_pwrdm == NULL) {
1086 printk(KERN_ERR "Failed to get mpu_pwrdm\n");
1087 goto err2;
1088 }
1089
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301090 neon_pwrdm = pwrdm_lookup("neon_pwrdm");
1091 per_pwrdm = pwrdm_lookup("per_pwrdm");
1092 core_pwrdm = pwrdm_lookup("core_pwrdm");
Tero Kristoc16c3f62008-12-11 16:46:57 +02001093 cam_pwrdm = pwrdm_lookup("cam_pwrdm");
Rajendra Nayakfa3c2a42008-09-26 17:49:22 +05301094
Paul Walmsley55ed9692010-01-26 20:12:59 -07001095 neon_clkdm = clkdm_lookup("neon_clkdm");
1096 mpu_clkdm = clkdm_lookup("mpu_clkdm");
1097 per_clkdm = clkdm_lookup("per_clkdm");
1098 core_clkdm = clkdm_lookup("core_clkdm");
1099
Rajendra Nayak3231fc82008-09-26 17:49:14 +05301100 omap_push_sram_idle();
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001101#ifdef CONFIG_SUSPEND
Kevin Hilman8bd22942009-05-28 10:56:16 -07001102 suspend_set_ops(&omap_pm_ops);
Kevin Hilman10f90ed2009-06-24 11:39:18 -07001103#endif /* CONFIG_SUSPEND */
Kevin Hilman8bd22942009-05-28 10:56:16 -07001104
1105 pm_idle = omap3_pm_idle;
Kalle Jokiniemi03433712008-09-26 11:04:20 +03001106 omap3_idle_init();
Kevin Hilman8bd22942009-05-28 10:56:16 -07001107
Paul Walmsley55ed9692010-01-26 20:12:59 -07001108 clkdm_add_wkdep(neon_clkdm, mpu_clkdm);
Tero Kristo27d59a42008-10-13 13:15:00 +03001109 if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
1110 omap3_secure_ram_storage =
1111 kmalloc(0x803F, GFP_KERNEL);
1112 if (!omap3_secure_ram_storage)
1113 printk(KERN_ERR "Memory allocation failed when"
1114 "allocating for secure sram context\n");
Tero Kristo27d59a42008-10-13 13:15:00 +03001115
Tero Kristo9d971402008-12-12 11:20:05 +02001116 local_irq_disable();
1117 local_fiq_disable();
1118
1119 omap_dma_global_context_save();
1120 omap3_save_secure_ram_context(PWRDM_POWER_ON);
1121 omap_dma_global_context_restore();
1122
1123 local_irq_enable();
1124 local_fiq_enable();
1125 }
1126
1127 omap3_save_scratchpad_contents();
Kevin Hilman8bd22942009-05-28 10:56:16 -07001128err1:
1129 return ret;
1130err2:
1131 free_irq(INT_34XX_PRCM_MPU_IRQ, NULL);
1132 list_for_each_entry_safe(pwrst, tmp, &pwrst_list, node) {
1133 list_del(&pwrst->node);
1134 kfree(pwrst);
1135 }
1136 return ret;
1137}
1138
1139late_initcall(omap3_pm_init);