pm34xx.c 27 KB

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