pm34xx.c 28 KB

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