pm34xx.c 28 KB

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