pm34xx.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. /*
  2. * OMAP3 Power Management Routines
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation
  5. * Tony Lindgren <tony@atomide.com>
  6. * Jouni Hogander
  7. *
  8. * Copyright (C) 2007 Texas Instruments, Inc.
  9. * Rajendra Nayak <rnayak@ti.com>
  10. *
  11. * 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. #include <linux/pm.h>
  21. #include <linux/suspend.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/list.h>
  25. #include <linux/err.h>
  26. #include <linux/gpio.h>
  27. #include <linux/clk.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/console.h>
  31. #include <trace/events/power.h>
  32. #include <plat/sram.h>
  33. #include "clockdomain.h"
  34. #include "powerdomain.h"
  35. #include <plat/serial.h>
  36. #include <plat/sdrc.h>
  37. #include <plat/prcm.h>
  38. #include <plat/gpmc.h>
  39. #include <plat/dma.h>
  40. #include <asm/tlbflush.h>
  41. #include "cm2xxx_3xxx.h"
  42. #include "cm-regbits-34xx.h"
  43. #include "prm-regbits-34xx.h"
  44. #include "prm2xxx_3xxx.h"
  45. #include "pm.h"
  46. #include "sdrc.h"
  47. #include "control.h"
  48. #ifdef CONFIG_SUSPEND
  49. static suspend_state_t suspend_state = PM_SUSPEND_ON;
  50. static inline bool is_suspending(void)
  51. {
  52. return (suspend_state != PM_SUSPEND_ON);
  53. }
  54. #else
  55. static inline bool is_suspending(void)
  56. {
  57. return false;
  58. }
  59. #endif
  60. /* Scratchpad offsets */
  61. #define OMAP343X_TABLE_ADDRESS_OFFSET 0xc4
  62. #define OMAP343X_TABLE_VALUE_OFFSET 0xc0
  63. #define OMAP343X_CONTROL_REG_VALUE_OFFSET 0xc8
  64. /* pm34xx errata defined in pm.h */
  65. u16 pm34xx_errata;
  66. struct power_state {
  67. struct powerdomain *pwrdm;
  68. u32 next_state;
  69. #ifdef CONFIG_SUSPEND
  70. u32 saved_state;
  71. #endif
  72. struct list_head node;
  73. };
  74. static LIST_HEAD(pwrst_list);
  75. static void (*_omap_sram_idle)(u32 *addr, int save_state);
  76. static int (*_omap_save_secure_sram)(u32 *addr);
  77. static struct powerdomain *mpu_pwrdm, *neon_pwrdm;
  78. static struct powerdomain *core_pwrdm, *per_pwrdm;
  79. static struct powerdomain *cam_pwrdm;
  80. static inline void omap3_per_save_context(void)
  81. {
  82. omap_gpio_save_context();
  83. }
  84. static inline void omap3_per_restore_context(void)
  85. {
  86. omap_gpio_restore_context();
  87. }
  88. static void omap3_enable_io_chain(void)
  89. {
  90. int timeout = 0;
  91. if (omap_rev() >= OMAP3430_REV_ES3_1) {
  92. omap2_prm_set_mod_reg_bits(OMAP3430_EN_IO_CHAIN_MASK, WKUP_MOD,
  93. PM_WKEN);
  94. /* Do a readback to assure write has been done */
  95. omap2_prm_read_mod_reg(WKUP_MOD, PM_WKEN);
  96. while (!(omap2_prm_read_mod_reg(WKUP_MOD, PM_WKEN) &
  97. OMAP3430_ST_IO_CHAIN_MASK)) {
  98. timeout++;
  99. if (timeout > 1000) {
  100. printk(KERN_ERR "Wake up daisy chain "
  101. "activation failed.\n");
  102. return;
  103. }
  104. omap2_prm_set_mod_reg_bits(OMAP3430_ST_IO_CHAIN_MASK,
  105. WKUP_MOD, PM_WKEN);
  106. }
  107. }
  108. }
  109. static void omap3_disable_io_chain(void)
  110. {
  111. if (omap_rev() >= OMAP3430_REV_ES3_1)
  112. omap2_prm_clear_mod_reg_bits(OMAP3430_EN_IO_CHAIN_MASK, WKUP_MOD,
  113. PM_WKEN);
  114. }
  115. static void omap3_core_save_context(void)
  116. {
  117. omap3_ctrl_save_padconf();
  118. /*
  119. * Force write last pad into memory, as this can fail in some
  120. * cases according to errata 1.157, 1.185
  121. */
  122. omap_ctrl_writel(omap_ctrl_readl(OMAP343X_PADCONF_ETK_D14),
  123. OMAP343X_CONTROL_MEM_WKUP + 0x2a0);
  124. /* Save the Interrupt controller context */
  125. omap_intc_save_context();
  126. /* Save the GPMC context */
  127. omap3_gpmc_save_context();
  128. /* Save the system control module context, padconf already save above*/
  129. omap3_control_save_context();
  130. omap_dma_global_context_save();
  131. }
  132. static void omap3_core_restore_context(void)
  133. {
  134. /* Restore the control module context, padconf restored by h/w */
  135. omap3_control_restore_context();
  136. /* Restore the GPMC context */
  137. omap3_gpmc_restore_context();
  138. /* Restore the interrupt controller context */
  139. omap_intc_restore_context();
  140. omap_dma_global_context_restore();
  141. }
  142. /*
  143. * FIXME: This function should be called before entering off-mode after
  144. * OMAP3 secure services have been accessed. Currently it is only called
  145. * once during boot sequence, but this works as we are not using secure
  146. * services.
  147. */
  148. static void omap3_save_secure_ram_context(void)
  149. {
  150. u32 ret;
  151. int mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
  152. if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
  153. /*
  154. * MPU next state must be set to POWER_ON temporarily,
  155. * otherwise the WFI executed inside the ROM code
  156. * will hang the system.
  157. */
  158. pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_ON);
  159. ret = _omap_save_secure_sram((u32 *)
  160. __pa(omap3_secure_ram_storage));
  161. pwrdm_set_next_pwrst(mpu_pwrdm, mpu_next_state);
  162. /* Following is for error tracking, it should not happen */
  163. if (ret) {
  164. printk(KERN_ERR "save_secure_sram() returns %08x\n",
  165. ret);
  166. while (1)
  167. ;
  168. }
  169. }
  170. }
  171. /*
  172. * PRCM Interrupt Handler Helper Function
  173. *
  174. * The purpose of this function is to clear any wake-up events latched
  175. * in the PRCM PM_WKST_x registers. It is possible that a wake-up event
  176. * may occur whilst attempting to clear a PM_WKST_x register and thus
  177. * set another bit in this register. A while loop is used to ensure
  178. * that any peripheral wake-up events occurring while attempting to
  179. * clear the PM_WKST_x are detected and cleared.
  180. */
  181. static int prcm_clear_mod_irqs(s16 module, u8 regs)
  182. {
  183. u32 wkst, fclk, iclk, clken;
  184. u16 wkst_off = (regs == 3) ? OMAP3430ES2_PM_WKST3 : PM_WKST1;
  185. u16 fclk_off = (regs == 3) ? OMAP3430ES2_CM_FCLKEN3 : CM_FCLKEN1;
  186. u16 iclk_off = (regs == 3) ? CM_ICLKEN3 : CM_ICLKEN1;
  187. u16 grpsel_off = (regs == 3) ?
  188. OMAP3430ES2_PM_MPUGRPSEL3 : OMAP3430_PM_MPUGRPSEL;
  189. int c = 0;
  190. wkst = omap2_prm_read_mod_reg(module, wkst_off);
  191. wkst &= omap2_prm_read_mod_reg(module, grpsel_off);
  192. if (wkst) {
  193. iclk = omap2_cm_read_mod_reg(module, iclk_off);
  194. fclk = omap2_cm_read_mod_reg(module, fclk_off);
  195. while (wkst) {
  196. clken = wkst;
  197. omap2_cm_set_mod_reg_bits(clken, module, iclk_off);
  198. /*
  199. * For USBHOST, we don't know whether HOST1 or
  200. * HOST2 woke us up, so enable both f-clocks
  201. */
  202. if (module == OMAP3430ES2_USBHOST_MOD)
  203. clken |= 1 << OMAP3430ES2_EN_USBHOST2_SHIFT;
  204. omap2_cm_set_mod_reg_bits(clken, module, fclk_off);
  205. omap2_prm_write_mod_reg(wkst, module, wkst_off);
  206. wkst = omap2_prm_read_mod_reg(module, wkst_off);
  207. c++;
  208. }
  209. omap2_cm_write_mod_reg(iclk, module, iclk_off);
  210. omap2_cm_write_mod_reg(fclk, module, fclk_off);
  211. }
  212. return c;
  213. }
  214. static int _prcm_int_handle_wakeup(void)
  215. {
  216. int c;
  217. c = prcm_clear_mod_irqs(WKUP_MOD, 1);
  218. c += prcm_clear_mod_irqs(CORE_MOD, 1);
  219. c += prcm_clear_mod_irqs(OMAP3430_PER_MOD, 1);
  220. if (omap_rev() > OMAP3430_REV_ES1_0) {
  221. c += prcm_clear_mod_irqs(CORE_MOD, 3);
  222. c += prcm_clear_mod_irqs(OMAP3430ES2_USBHOST_MOD, 1);
  223. }
  224. return c;
  225. }
  226. /*
  227. * PRCM Interrupt Handler
  228. *
  229. * The PRM_IRQSTATUS_MPU register indicates if there are any pending
  230. * interrupts from the PRCM for the MPU. These bits must be cleared in
  231. * order to clear the PRCM interrupt. The PRCM interrupt handler is
  232. * implemented to simply clear the PRM_IRQSTATUS_MPU in order to clear
  233. * the PRCM interrupt. Please note that bit 0 of the PRM_IRQSTATUS_MPU
  234. * register indicates that a wake-up event is pending for the MPU and
  235. * this bit can only be cleared if the all the wake-up events latched
  236. * in the various PM_WKST_x registers have been cleared. The interrupt
  237. * handler is implemented using a do-while loop so that if a wake-up
  238. * event occurred during the processing of the prcm interrupt handler
  239. * (setting a bit in the corresponding PM_WKST_x register and thus
  240. * preventing us from clearing bit 0 of the PRM_IRQSTATUS_MPU register)
  241. * this would be handled.
  242. */
  243. static irqreturn_t prcm_interrupt_handler (int irq, void *dev_id)
  244. {
  245. u32 irqenable_mpu, irqstatus_mpu;
  246. int c = 0;
  247. irqenable_mpu = omap2_prm_read_mod_reg(OCP_MOD,
  248. OMAP3_PRM_IRQENABLE_MPU_OFFSET);
  249. irqstatus_mpu = omap2_prm_read_mod_reg(OCP_MOD,
  250. OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
  251. irqstatus_mpu &= irqenable_mpu;
  252. do {
  253. if (irqstatus_mpu & (OMAP3430_WKUP_ST_MASK |
  254. OMAP3430_IO_ST_MASK)) {
  255. c = _prcm_int_handle_wakeup();
  256. /*
  257. * Is the MPU PRCM interrupt handler racing with the
  258. * IVA2 PRCM interrupt handler ?
  259. */
  260. WARN(c == 0, "prcm: WARNING: PRCM indicated MPU wakeup "
  261. "but no wakeup sources are marked\n");
  262. } else {
  263. /* XXX we need to expand our PRCM interrupt handler */
  264. WARN(1, "prcm: WARNING: PRCM interrupt received, but "
  265. "no code to handle it (%08x)\n", irqstatus_mpu);
  266. }
  267. omap2_prm_write_mod_reg(irqstatus_mpu, OCP_MOD,
  268. OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
  269. irqstatus_mpu = omap2_prm_read_mod_reg(OCP_MOD,
  270. OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
  271. irqstatus_mpu &= irqenable_mpu;
  272. } while (irqstatus_mpu);
  273. return IRQ_HANDLED;
  274. }
  275. /* Function to restore the table entry that was modified for enabling MMU */
  276. static void restore_table_entry(void)
  277. {
  278. void __iomem *scratchpad_address;
  279. u32 previous_value, control_reg_value;
  280. u32 *address;
  281. scratchpad_address = OMAP2_L4_IO_ADDRESS(OMAP343X_SCRATCHPAD);
  282. /* Get address of entry that was modified */
  283. address = (u32 *)__raw_readl(scratchpad_address +
  284. OMAP343X_TABLE_ADDRESS_OFFSET);
  285. /* Get the previous value which needs to be restored */
  286. previous_value = __raw_readl(scratchpad_address +
  287. OMAP343X_TABLE_VALUE_OFFSET);
  288. address = __va(address);
  289. *address = previous_value;
  290. flush_tlb_all();
  291. control_reg_value = __raw_readl(scratchpad_address
  292. + OMAP343X_CONTROL_REG_VALUE_OFFSET);
  293. /* This will enable caches and prediction */
  294. set_cr(control_reg_value);
  295. }
  296. void omap_sram_idle(void)
  297. {
  298. /* Variable to tell what needs to be saved and restored
  299. * in omap_sram_idle*/
  300. /* save_state = 0 => Nothing to save and restored */
  301. /* save_state = 1 => Only L1 and logic lost */
  302. /* save_state = 2 => Only L2 lost */
  303. /* save_state = 3 => L1, L2 and logic lost */
  304. int save_state = 0;
  305. int mpu_next_state = PWRDM_POWER_ON;
  306. int per_next_state = PWRDM_POWER_ON;
  307. int core_next_state = PWRDM_POWER_ON;
  308. int per_going_off;
  309. int core_prev_state, per_prev_state;
  310. u32 sdrc_pwr = 0;
  311. if (!_omap_sram_idle)
  312. return;
  313. pwrdm_clear_all_prev_pwrst(mpu_pwrdm);
  314. pwrdm_clear_all_prev_pwrst(neon_pwrdm);
  315. pwrdm_clear_all_prev_pwrst(core_pwrdm);
  316. pwrdm_clear_all_prev_pwrst(per_pwrdm);
  317. mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
  318. switch (mpu_next_state) {
  319. case PWRDM_POWER_ON:
  320. case PWRDM_POWER_RET:
  321. /* No need to save context */
  322. save_state = 0;
  323. break;
  324. case PWRDM_POWER_OFF:
  325. save_state = 3;
  326. break;
  327. default:
  328. /* Invalid state */
  329. printk(KERN_ERR "Invalid mpu state in sram_idle\n");
  330. return;
  331. }
  332. pwrdm_pre_transition();
  333. /* NEON control */
  334. if (pwrdm_read_pwrst(neon_pwrdm) == PWRDM_POWER_ON)
  335. pwrdm_set_next_pwrst(neon_pwrdm, mpu_next_state);
  336. /* Enable IO-PAD and IO-CHAIN wakeups */
  337. per_next_state = pwrdm_read_next_pwrst(per_pwrdm);
  338. core_next_state = pwrdm_read_next_pwrst(core_pwrdm);
  339. if (omap3_has_io_wakeup() &&
  340. (per_next_state < PWRDM_POWER_ON ||
  341. core_next_state < PWRDM_POWER_ON)) {
  342. omap2_prm_set_mod_reg_bits(OMAP3430_EN_IO_MASK, WKUP_MOD, PM_WKEN);
  343. omap3_enable_io_chain();
  344. }
  345. /* Block console output in case it is on one of the OMAP UARTs */
  346. if (!is_suspending())
  347. if (per_next_state < PWRDM_POWER_ON ||
  348. core_next_state < PWRDM_POWER_ON)
  349. if (!console_trylock())
  350. goto console_still_active;
  351. /* PER */
  352. if (per_next_state < PWRDM_POWER_ON) {
  353. per_going_off = (per_next_state == PWRDM_POWER_OFF) ? 1 : 0;
  354. omap_uart_prepare_idle(2);
  355. omap_uart_prepare_idle(3);
  356. omap2_gpio_prepare_for_idle(per_going_off);
  357. if (per_next_state == PWRDM_POWER_OFF)
  358. omap3_per_save_context();
  359. }
  360. /* CORE */
  361. if (core_next_state < PWRDM_POWER_ON) {
  362. omap_uart_prepare_idle(0);
  363. omap_uart_prepare_idle(1);
  364. if (core_next_state == PWRDM_POWER_OFF) {
  365. omap3_core_save_context();
  366. omap3_cm_save_context();
  367. }
  368. }
  369. omap3_intc_prepare_idle();
  370. /*
  371. * On EMU/HS devices ROM code restores a SRDC value
  372. * from scratchpad which has automatic self refresh on timeout
  373. * of AUTO_CNT = 1 enabled. This takes care of erratum ID i443.
  374. * Hence store/restore the SDRC_POWER register here.
  375. */
  376. if (omap_rev() >= OMAP3430_REV_ES3_0 &&
  377. omap_type() != OMAP2_DEVICE_TYPE_GP &&
  378. core_next_state == PWRDM_POWER_OFF)
  379. sdrc_pwr = sdrc_read_reg(SDRC_POWER);
  380. /*
  381. * omap3_arm_context is the location where ARM registers
  382. * get saved. The restore path then reads from this
  383. * location and restores them back.
  384. */
  385. _omap_sram_idle(omap3_arm_context, save_state);
  386. cpu_init();
  387. /* Restore normal SDRC POWER settings */
  388. if (omap_rev() >= OMAP3430_REV_ES3_0 &&
  389. omap_type() != OMAP2_DEVICE_TYPE_GP &&
  390. core_next_state == PWRDM_POWER_OFF)
  391. sdrc_write_reg(sdrc_pwr, SDRC_POWER);
  392. /* Restore table entry modified during MMU restoration */
  393. if (pwrdm_read_prev_pwrst(mpu_pwrdm) == PWRDM_POWER_OFF)
  394. restore_table_entry();
  395. /* CORE */
  396. if (core_next_state < PWRDM_POWER_ON) {
  397. core_prev_state = pwrdm_read_prev_pwrst(core_pwrdm);
  398. if (core_prev_state == PWRDM_POWER_OFF) {
  399. omap3_core_restore_context();
  400. omap3_cm_restore_context();
  401. omap3_sram_restore_context();
  402. omap2_sms_restore_context();
  403. }
  404. omap_uart_resume_idle(0);
  405. omap_uart_resume_idle(1);
  406. if (core_next_state == PWRDM_POWER_OFF)
  407. omap2_prm_clear_mod_reg_bits(OMAP3430_AUTO_OFF_MASK,
  408. OMAP3430_GR_MOD,
  409. OMAP3_PRM_VOLTCTRL_OFFSET);
  410. }
  411. omap3_intc_resume_idle();
  412. /* PER */
  413. if (per_next_state < PWRDM_POWER_ON) {
  414. per_prev_state = pwrdm_read_prev_pwrst(per_pwrdm);
  415. omap2_gpio_resume_after_idle();
  416. if (per_prev_state == PWRDM_POWER_OFF)
  417. omap3_per_restore_context();
  418. omap_uart_resume_idle(2);
  419. omap_uart_resume_idle(3);
  420. }
  421. if (!is_suspending())
  422. console_unlock();
  423. console_still_active:
  424. /* Disable IO-PAD and IO-CHAIN wakeup */
  425. if (omap3_has_io_wakeup() &&
  426. (per_next_state < PWRDM_POWER_ON ||
  427. core_next_state < PWRDM_POWER_ON)) {
  428. omap2_prm_clear_mod_reg_bits(OMAP3430_EN_IO_MASK, WKUP_MOD,
  429. PM_WKEN);
  430. omap3_disable_io_chain();
  431. }
  432. pwrdm_post_transition();
  433. clkdm_allow_idle(mpu_pwrdm->pwrdm_clkdms[0]);
  434. }
  435. int omap3_can_sleep(void)
  436. {
  437. if (!sleep_while_idle)
  438. return 0;
  439. if (!omap_uart_can_sleep())
  440. return 0;
  441. return 1;
  442. }
  443. static void omap3_pm_idle(void)
  444. {
  445. local_irq_disable();
  446. local_fiq_disable();
  447. if (!omap3_can_sleep())
  448. goto out;
  449. if (omap_irq_pending() || need_resched())
  450. goto out;
  451. trace_power_start(POWER_CSTATE, 1, smp_processor_id());
  452. trace_cpu_idle(1, smp_processor_id());
  453. omap_sram_idle();
  454. trace_power_end(smp_processor_id());
  455. trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
  456. out:
  457. local_fiq_enable();
  458. local_irq_enable();
  459. }
  460. #ifdef CONFIG_SUSPEND
  461. static int omap3_pm_suspend(void)
  462. {
  463. struct power_state *pwrst;
  464. int state, ret = 0;
  465. if (wakeup_timer_seconds || wakeup_timer_milliseconds)
  466. omap2_pm_wakeup_on_timer(wakeup_timer_seconds,
  467. wakeup_timer_milliseconds);
  468. /* Read current next_pwrsts */
  469. list_for_each_entry(pwrst, &pwrst_list, node)
  470. pwrst->saved_state = pwrdm_read_next_pwrst(pwrst->pwrdm);
  471. /* Set ones wanted by suspend */
  472. list_for_each_entry(pwrst, &pwrst_list, node) {
  473. if (omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state))
  474. goto restore;
  475. if (pwrdm_clear_all_prev_pwrst(pwrst->pwrdm))
  476. goto restore;
  477. }
  478. omap_uart_prepare_suspend();
  479. omap3_intc_suspend();
  480. omap_sram_idle();
  481. restore:
  482. /* Restore next_pwrsts */
  483. list_for_each_entry(pwrst, &pwrst_list, node) {
  484. state = pwrdm_read_prev_pwrst(pwrst->pwrdm);
  485. if (state > pwrst->next_state) {
  486. printk(KERN_INFO "Powerdomain (%s) didn't enter "
  487. "target state %d\n",
  488. pwrst->pwrdm->name, pwrst->next_state);
  489. ret = -1;
  490. }
  491. omap_set_pwrdm_state(pwrst->pwrdm, pwrst->saved_state);
  492. }
  493. if (ret)
  494. printk(KERN_ERR "Could not enter target state in pm_suspend\n");
  495. else
  496. printk(KERN_INFO "Successfully put all powerdomains "
  497. "to target state\n");
  498. return ret;
  499. }
  500. static int omap3_pm_enter(suspend_state_t unused)
  501. {
  502. int ret = 0;
  503. switch (suspend_state) {
  504. case PM_SUSPEND_STANDBY:
  505. case PM_SUSPEND_MEM:
  506. ret = omap3_pm_suspend();
  507. break;
  508. default:
  509. ret = -EINVAL;
  510. }
  511. return ret;
  512. }
  513. /* Hooks to enable / disable UART interrupts during suspend */
  514. static int omap3_pm_begin(suspend_state_t state)
  515. {
  516. disable_hlt();
  517. suspend_state = state;
  518. omap_uart_enable_irqs(0);
  519. return 0;
  520. }
  521. static void omap3_pm_end(void)
  522. {
  523. suspend_state = PM_SUSPEND_ON;
  524. omap_uart_enable_irqs(1);
  525. enable_hlt();
  526. return;
  527. }
  528. static const struct platform_suspend_ops omap_pm_ops = {
  529. .begin = omap3_pm_begin,
  530. .end = omap3_pm_end,
  531. .enter = omap3_pm_enter,
  532. .valid = suspend_valid_only_mem,
  533. };
  534. #endif /* CONFIG_SUSPEND */
  535. /**
  536. * omap3_iva_idle(): ensure IVA is in idle so it can be put into
  537. * retention
  538. *
  539. * In cases where IVA2 is activated by bootcode, it may prevent
  540. * full-chip retention or off-mode because it is not idle. This
  541. * function forces the IVA2 into idle state so it can go
  542. * into retention/off and thus allow full-chip retention/off.
  543. *
  544. **/
  545. static void __init omap3_iva_idle(void)
  546. {
  547. /* ensure IVA2 clock is disabled */
  548. omap2_cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
  549. /* if no clock activity, nothing else to do */
  550. if (!(omap2_cm_read_mod_reg(OMAP3430_IVA2_MOD, OMAP3430_CM_CLKSTST) &
  551. OMAP3430_CLKACTIVITY_IVA2_MASK))
  552. return;
  553. /* Reset IVA2 */
  554. omap2_prm_write_mod_reg(OMAP3430_RST1_IVA2_MASK |
  555. OMAP3430_RST2_IVA2_MASK |
  556. OMAP3430_RST3_IVA2_MASK,
  557. OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
  558. /* Enable IVA2 clock */
  559. omap2_cm_write_mod_reg(OMAP3430_CM_FCLKEN_IVA2_EN_IVA2_MASK,
  560. OMAP3430_IVA2_MOD, CM_FCLKEN);
  561. /* Set IVA2 boot mode to 'idle' */
  562. omap_ctrl_writel(OMAP3_IVA2_BOOTMOD_IDLE,
  563. OMAP343X_CONTROL_IVA2_BOOTMOD);
  564. /* Un-reset IVA2 */
  565. omap2_prm_write_mod_reg(0, OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
  566. /* Disable IVA2 clock */
  567. omap2_cm_write_mod_reg(0, OMAP3430_IVA2_MOD, CM_FCLKEN);
  568. /* Reset IVA2 */
  569. omap2_prm_write_mod_reg(OMAP3430_RST1_IVA2_MASK |
  570. OMAP3430_RST2_IVA2_MASK |
  571. OMAP3430_RST3_IVA2_MASK,
  572. OMAP3430_IVA2_MOD, OMAP2_RM_RSTCTRL);
  573. }
  574. static void __init omap3_d2d_idle(void)
  575. {
  576. u16 mask, padconf;
  577. /* In a stand alone OMAP3430 where there is not a stacked
  578. * modem for the D2D Idle Ack and D2D MStandby must be pulled
  579. * high. S CONTROL_PADCONF_SAD2D_IDLEACK and
  580. * CONTROL_PADCONF_SAD2D_MSTDBY to have a pull up. */
  581. mask = (1 << 4) | (1 << 3); /* pull-up, enabled */
  582. padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_MSTANDBY);
  583. padconf |= mask;
  584. omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_MSTANDBY);
  585. padconf = omap_ctrl_readw(OMAP3_PADCONF_SAD2D_IDLEACK);
  586. padconf |= mask;
  587. omap_ctrl_writew(padconf, OMAP3_PADCONF_SAD2D_IDLEACK);
  588. /* reset modem */
  589. omap2_prm_write_mod_reg(OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RSTPWRON_MASK |
  590. OMAP3430_RM_RSTCTRL_CORE_MODEM_SW_RST_MASK,
  591. CORE_MOD, OMAP2_RM_RSTCTRL);
  592. omap2_prm_write_mod_reg(0, CORE_MOD, OMAP2_RM_RSTCTRL);
  593. }
  594. static void __init prcm_setup_regs(void)
  595. {
  596. u32 omap3630_en_uart4_mask = cpu_is_omap3630() ?
  597. OMAP3630_EN_UART4_MASK : 0;
  598. u32 omap3630_grpsel_uart4_mask = cpu_is_omap3630() ?
  599. OMAP3630_GRPSEL_UART4_MASK : 0;
  600. /* XXX This should be handled by hwmod code or SCM init code */
  601. omap_ctrl_writel(OMAP3430_AUTOIDLE_MASK, OMAP2_CONTROL_SYSCONFIG);
  602. /*
  603. * Enable control of expternal oscillator through
  604. * sys_clkreq. In the long run clock framework should
  605. * take care of this.
  606. */
  607. omap2_prm_rmw_mod_reg_bits(OMAP_AUTOEXTCLKMODE_MASK,
  608. 1 << OMAP_AUTOEXTCLKMODE_SHIFT,
  609. OMAP3430_GR_MOD,
  610. OMAP3_PRM_CLKSRC_CTRL_OFFSET);
  611. /* setup wakup source */
  612. omap2_prm_write_mod_reg(OMAP3430_EN_IO_MASK | OMAP3430_EN_GPIO1_MASK |
  613. OMAP3430_EN_GPT1_MASK | OMAP3430_EN_GPT12_MASK,
  614. WKUP_MOD, PM_WKEN);
  615. /* No need to write EN_IO, that is always enabled */
  616. omap2_prm_write_mod_reg(OMAP3430_GRPSEL_GPIO1_MASK |
  617. OMAP3430_GRPSEL_GPT1_MASK |
  618. OMAP3430_GRPSEL_GPT12_MASK,
  619. WKUP_MOD, OMAP3430_PM_MPUGRPSEL);
  620. /* For some reason IO doesn't generate wakeup event even if
  621. * it is selected to mpu wakeup goup */
  622. omap2_prm_write_mod_reg(OMAP3430_IO_EN_MASK | OMAP3430_WKUP_EN_MASK,
  623. OCP_MOD, OMAP3_PRM_IRQENABLE_MPU_OFFSET);
  624. /* Enable PM_WKEN to support DSS LPR */
  625. omap2_prm_write_mod_reg(OMAP3430_PM_WKEN_DSS_EN_DSS_MASK,
  626. OMAP3430_DSS_MOD, PM_WKEN);
  627. /* Enable wakeups in PER */
  628. omap2_prm_write_mod_reg(omap3630_en_uart4_mask |
  629. OMAP3430_EN_GPIO2_MASK | OMAP3430_EN_GPIO3_MASK |
  630. OMAP3430_EN_GPIO4_MASK | OMAP3430_EN_GPIO5_MASK |
  631. OMAP3430_EN_GPIO6_MASK | OMAP3430_EN_UART3_MASK |
  632. OMAP3430_EN_MCBSP2_MASK | OMAP3430_EN_MCBSP3_MASK |
  633. OMAP3430_EN_MCBSP4_MASK,
  634. OMAP3430_PER_MOD, PM_WKEN);
  635. /* and allow them to wake up MPU */
  636. omap2_prm_write_mod_reg(omap3630_grpsel_uart4_mask |
  637. OMAP3430_GRPSEL_GPIO2_MASK |
  638. OMAP3430_GRPSEL_GPIO3_MASK |
  639. OMAP3430_GRPSEL_GPIO4_MASK |
  640. OMAP3430_GRPSEL_GPIO5_MASK |
  641. OMAP3430_GRPSEL_GPIO6_MASK |
  642. OMAP3430_GRPSEL_UART3_MASK |
  643. OMAP3430_GRPSEL_MCBSP2_MASK |
  644. OMAP3430_GRPSEL_MCBSP3_MASK |
  645. OMAP3430_GRPSEL_MCBSP4_MASK,
  646. OMAP3430_PER_MOD, OMAP3430_PM_MPUGRPSEL);
  647. /* Don't attach IVA interrupts */
  648. omap2_prm_write_mod_reg(0, WKUP_MOD, OMAP3430_PM_IVAGRPSEL);
  649. omap2_prm_write_mod_reg(0, CORE_MOD, OMAP3430_PM_IVAGRPSEL1);
  650. omap2_prm_write_mod_reg(0, CORE_MOD, OMAP3430ES2_PM_IVAGRPSEL3);
  651. omap2_prm_write_mod_reg(0, OMAP3430_PER_MOD, OMAP3430_PM_IVAGRPSEL);
  652. /* Clear any pending 'reset' flags */
  653. omap2_prm_write_mod_reg(0xffffffff, MPU_MOD, OMAP2_RM_RSTST);
  654. omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP2_RM_RSTST);
  655. omap2_prm_write_mod_reg(0xffffffff, OMAP3430_PER_MOD, OMAP2_RM_RSTST);
  656. omap2_prm_write_mod_reg(0xffffffff, OMAP3430_EMU_MOD, OMAP2_RM_RSTST);
  657. omap2_prm_write_mod_reg(0xffffffff, OMAP3430_NEON_MOD, OMAP2_RM_RSTST);
  658. omap2_prm_write_mod_reg(0xffffffff, OMAP3430_DSS_MOD, OMAP2_RM_RSTST);
  659. omap2_prm_write_mod_reg(0xffffffff, OMAP3430ES2_USBHOST_MOD, OMAP2_RM_RSTST);
  660. /* Clear any pending PRCM interrupts */
  661. omap2_prm_write_mod_reg(0, OCP_MOD, OMAP3_PRM_IRQSTATUS_MPU_OFFSET);
  662. omap3_iva_idle();
  663. omap3_d2d_idle();
  664. }
  665. void omap3_pm_off_mode_enable(int enable)
  666. {
  667. struct power_state *pwrst;
  668. u32 state;
  669. if (enable)
  670. state = PWRDM_POWER_OFF;
  671. else
  672. state = PWRDM_POWER_RET;
  673. #ifdef CONFIG_CPU_IDLE
  674. /*
  675. * Erratum i583: implementation for ES rev < Es1.2 on 3630. We cannot
  676. * enable OFF mode in a stable form for previous revisions, restrict
  677. * instead to RET
  678. */
  679. if (IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583))
  680. omap3_cpuidle_update_states(state, PWRDM_POWER_RET);
  681. else
  682. omap3_cpuidle_update_states(state, state);
  683. #endif
  684. list_for_each_entry(pwrst, &pwrst_list, node) {
  685. if (IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583) &&
  686. pwrst->pwrdm == core_pwrdm &&
  687. state == PWRDM_POWER_OFF) {
  688. pwrst->next_state = PWRDM_POWER_RET;
  689. pr_warn("%s: Core OFF disabled due to errata i583\n",
  690. __func__);
  691. } else {
  692. pwrst->next_state = state;
  693. }
  694. omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
  695. }
  696. }
  697. int omap3_pm_get_suspend_state(struct powerdomain *pwrdm)
  698. {
  699. struct power_state *pwrst;
  700. list_for_each_entry(pwrst, &pwrst_list, node) {
  701. if (pwrst->pwrdm == pwrdm)
  702. return pwrst->next_state;
  703. }
  704. return -EINVAL;
  705. }
  706. int omap3_pm_set_suspend_state(struct powerdomain *pwrdm, int state)
  707. {
  708. struct power_state *pwrst;
  709. list_for_each_entry(pwrst, &pwrst_list, node) {
  710. if (pwrst->pwrdm == pwrdm) {
  711. pwrst->next_state = state;
  712. return 0;
  713. }
  714. }
  715. return -EINVAL;
  716. }
  717. static int __init pwrdms_setup(struct powerdomain *pwrdm, void *unused)
  718. {
  719. struct power_state *pwrst;
  720. if (!pwrdm->pwrsts)
  721. return 0;
  722. pwrst = kmalloc(sizeof(struct power_state), GFP_ATOMIC);
  723. if (!pwrst)
  724. return -ENOMEM;
  725. pwrst->pwrdm = pwrdm;
  726. pwrst->next_state = PWRDM_POWER_RET;
  727. list_add(&pwrst->node, &pwrst_list);
  728. if (pwrdm_has_hdwr_sar(pwrdm))
  729. pwrdm_enable_hdwr_sar(pwrdm);
  730. return omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
  731. }
  732. /*
  733. * Enable hw supervised mode for all clockdomains if it's
  734. * supported. Initiate sleep transition for other clockdomains, if
  735. * they are not used
  736. */
  737. static int __init clkdms_setup(struct clockdomain *clkdm, void *unused)
  738. {
  739. if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
  740. clkdm_allow_idle(clkdm);
  741. else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
  742. atomic_read(&clkdm->usecount) == 0)
  743. clkdm_sleep(clkdm);
  744. return 0;
  745. }
  746. void omap_push_sram_idle(void)
  747. {
  748. _omap_sram_idle = omap_sram_push(omap34xx_cpu_suspend,
  749. omap34xx_cpu_suspend_sz);
  750. if (omap_type() != OMAP2_DEVICE_TYPE_GP)
  751. _omap_save_secure_sram = omap_sram_push(save_secure_ram_context,
  752. save_secure_ram_context_sz);
  753. }
  754. static void __init pm_errata_configure(void)
  755. {
  756. if (cpu_is_omap3630()) {
  757. pm34xx_errata |= PM_RTA_ERRATUM_i608;
  758. /* Enable the l2 cache toggling in sleep logic */
  759. enable_omap3630_toggle_l2_on_restore();
  760. if (omap_rev() < OMAP3630_REV_ES1_2)
  761. pm34xx_errata |= PM_SDRC_WAKEUP_ERRATUM_i583;
  762. }
  763. }
  764. static int __init omap3_pm_init(void)
  765. {
  766. struct power_state *pwrst, *tmp;
  767. struct clockdomain *neon_clkdm, *per_clkdm, *mpu_clkdm, *core_clkdm;
  768. int ret;
  769. if (!cpu_is_omap34xx())
  770. return -ENODEV;
  771. pm_errata_configure();
  772. printk(KERN_ERR "Power Management for TI OMAP3.\n");
  773. /* XXX prcm_setup_regs needs to be before enabling hw
  774. * supervised mode for powerdomains */
  775. prcm_setup_regs();
  776. ret = request_irq(INT_34XX_PRCM_MPU_IRQ,
  777. (irq_handler_t)prcm_interrupt_handler,
  778. IRQF_DISABLED, "prcm", NULL);
  779. if (ret) {
  780. printk(KERN_ERR "request_irq failed to register for 0x%x\n",
  781. INT_34XX_PRCM_MPU_IRQ);
  782. goto err1;
  783. }
  784. ret = pwrdm_for_each(pwrdms_setup, NULL);
  785. if (ret) {
  786. printk(KERN_ERR "Failed to setup powerdomains\n");
  787. goto err2;
  788. }
  789. (void) clkdm_for_each(clkdms_setup, NULL);
  790. mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
  791. if (mpu_pwrdm == NULL) {
  792. printk(KERN_ERR "Failed to get mpu_pwrdm\n");
  793. goto err2;
  794. }
  795. neon_pwrdm = pwrdm_lookup("neon_pwrdm");
  796. per_pwrdm = pwrdm_lookup("per_pwrdm");
  797. core_pwrdm = pwrdm_lookup("core_pwrdm");
  798. cam_pwrdm = pwrdm_lookup("cam_pwrdm");
  799. neon_clkdm = clkdm_lookup("neon_clkdm");
  800. mpu_clkdm = clkdm_lookup("mpu_clkdm");
  801. per_clkdm = clkdm_lookup("per_clkdm");
  802. core_clkdm = clkdm_lookup("core_clkdm");
  803. omap_push_sram_idle();
  804. #ifdef CONFIG_SUSPEND
  805. suspend_set_ops(&omap_pm_ops);
  806. #endif /* CONFIG_SUSPEND */
  807. pm_idle = omap3_pm_idle;
  808. omap3_idle_init();
  809. /*
  810. * RTA is disabled during initialization as per erratum i608
  811. * it is safer to disable RTA by the bootloader, but we would like
  812. * to be doubly sure here and prevent any mishaps.
  813. */
  814. if (IS_PM34XX_ERRATUM(PM_RTA_ERRATUM_i608))
  815. omap3630_ctrl_disable_rta();
  816. clkdm_add_wkdep(neon_clkdm, mpu_clkdm);
  817. if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
  818. omap3_secure_ram_storage =
  819. kmalloc(0x803F, GFP_KERNEL);
  820. if (!omap3_secure_ram_storage)
  821. printk(KERN_ERR "Memory allocation failed when"
  822. "allocating for secure sram context\n");
  823. local_irq_disable();
  824. local_fiq_disable();
  825. omap_dma_global_context_save();
  826. omap3_save_secure_ram_context();
  827. omap_dma_global_context_restore();
  828. local_irq_enable();
  829. local_fiq_enable();
  830. }
  831. omap3_save_scratchpad_contents();
  832. err1:
  833. return ret;
  834. err2:
  835. free_irq(INT_34XX_PRCM_MPU_IRQ, NULL);
  836. list_for_each_entry_safe(pwrst, tmp, &pwrst_list, node) {
  837. list_del(&pwrst->node);
  838. kfree(pwrst);
  839. }
  840. return ret;
  841. }
  842. late_initcall(omap3_pm_init);