pm34xx.c 26 KB

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