pm24xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. /*
  2. * OMAP2 Power Management Routines
  3. *
  4. * Copyright (C) 2005 Texas Instruments, Inc.
  5. * Copyright (C) 2006-2008 Nokia Corporation
  6. *
  7. * Written by:
  8. * Richard Woodruff <r-woodruff2@ti.com>
  9. * Tony Lindgren
  10. * Juha Yrjola
  11. * Amit Kucheria <amit.kucheria@nokia.com>
  12. * Igor Stoppa <igor.stoppa@nokia.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/suspend.h>
  21. #include <linux/sched.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sysfs.h>
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/clk.h>
  28. #include <linux/io.h>
  29. #include <linux/irq.h>
  30. #include <linux/time.h>
  31. #include <linux/gpio.h>
  32. #include <asm/mach/time.h>
  33. #include <asm/mach/irq.h>
  34. #include <asm/mach-types.h>
  35. #include <mach/irqs.h>
  36. #include <mach/clock.h>
  37. #include <mach/sram.h>
  38. #include <mach/control.h>
  39. #include <mach/mux.h>
  40. #include <mach/dma.h>
  41. #include <mach/board.h>
  42. #include "prm.h"
  43. #include "prm-regbits-24xx.h"
  44. #include "cm.h"
  45. #include "cm-regbits-24xx.h"
  46. #include "sdrc.h"
  47. #include "pm.h"
  48. #include <mach/powerdomain.h>
  49. #include <mach/clockdomain.h>
  50. static void (*omap2_sram_idle)(void);
  51. static void (*omap2_sram_suspend)(u32 dllctrl, void __iomem *sdrc_dlla_ctrl,
  52. void __iomem *sdrc_power);
  53. static struct powerdomain *mpu_pwrdm;
  54. static struct powerdomain *core_pwrdm;
  55. static struct clockdomain *dsp_clkdm;
  56. static struct clockdomain *gfx_clkdm;
  57. static struct clk *osc_ck, *emul_ck;
  58. static int omap2_fclks_active(void)
  59. {
  60. u32 f1, f2;
  61. f1 = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
  62. f2 = cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
  63. if (f1 | f2)
  64. return 1;
  65. return 0;
  66. }
  67. static void omap2_enter_full_retention(void)
  68. {
  69. u32 l;
  70. struct timespec ts_preidle, ts_postidle, ts_idle;
  71. /* There is 1 reference hold for all children of the oscillator
  72. * clock, the following will remove it. If no one else uses the
  73. * oscillator itself it will be disabled if/when we enter retention
  74. * mode.
  75. */
  76. clk_disable(osc_ck);
  77. /* Clear old wake-up events */
  78. /* REVISIT: These write to reserved bits? */
  79. prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
  80. prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
  81. prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
  82. /*
  83. * Set MPU powerdomain's next power state to RETENTION;
  84. * preserve logic state during retention
  85. */
  86. pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
  87. pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
  88. /* Workaround to kill USB */
  89. l = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0) | OMAP24XX_USBSTANDBYCTRL;
  90. omap_ctrl_writel(l, OMAP2_CONTROL_DEVCONF0);
  91. omap2_gpio_prepare_for_retention();
  92. if (omap2_pm_debug) {
  93. omap2_pm_dump(0, 0, 0);
  94. getnstimeofday(&ts_preidle);
  95. }
  96. /* One last check for pending IRQs to avoid extra latency due
  97. * to sleeping unnecessarily. */
  98. if (omap_irq_pending())
  99. goto no_sleep;
  100. /* Jump to SRAM suspend code */
  101. omap2_sram_suspend(sdrc_read_reg(SDRC_DLLA_CTRL),
  102. OMAP_SDRC_REGADDR(SDRC_DLLA_CTRL),
  103. OMAP_SDRC_REGADDR(SDRC_POWER));
  104. no_sleep:
  105. if (omap2_pm_debug) {
  106. unsigned long long tmp;
  107. getnstimeofday(&ts_postidle);
  108. ts_idle = timespec_sub(ts_postidle, ts_preidle);
  109. tmp = timespec_to_ns(&ts_idle) * NSEC_PER_USEC;
  110. omap2_pm_dump(0, 1, tmp);
  111. }
  112. omap2_gpio_resume_after_retention();
  113. clk_enable(osc_ck);
  114. /* clear CORE wake-up events */
  115. prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
  116. prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
  117. /* wakeup domain events - bit 1: GPT1, bit5 GPIO */
  118. prm_clear_mod_reg_bits(0x4 | 0x1, WKUP_MOD, PM_WKST);
  119. /* MPU domain wake events */
  120. l = prm_read_mod_reg(OCP_MOD, OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
  121. if (l & 0x01)
  122. prm_write_mod_reg(0x01, OCP_MOD,
  123. OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
  124. if (l & 0x20)
  125. prm_write_mod_reg(0x20, OCP_MOD,
  126. OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
  127. /* Mask future PRCM-to-MPU interrupts */
  128. prm_write_mod_reg(0x0, OCP_MOD, OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
  129. }
  130. static int omap2_i2c_active(void)
  131. {
  132. u32 l;
  133. l = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
  134. return l & (OMAP2420_EN_I2C2 | OMAP2420_EN_I2C1);
  135. }
  136. static int sti_console_enabled;
  137. static int omap2_allow_mpu_retention(void)
  138. {
  139. u32 l;
  140. /* Check for MMC, UART2, UART1, McSPI2, McSPI1 and DSS1. */
  141. l = cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
  142. if (l & (OMAP2420_EN_MMC | OMAP24XX_EN_UART2 |
  143. OMAP24XX_EN_UART1 | OMAP24XX_EN_MCSPI2 |
  144. OMAP24XX_EN_MCSPI1 | OMAP24XX_EN_DSS1))
  145. return 0;
  146. /* Check for UART3. */
  147. l = cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
  148. if (l & OMAP24XX_EN_UART3)
  149. return 0;
  150. if (sti_console_enabled)
  151. return 0;
  152. return 1;
  153. }
  154. static void omap2_enter_mpu_retention(void)
  155. {
  156. int only_idle = 0;
  157. struct timespec ts_preidle, ts_postidle, ts_idle;
  158. /* Putting MPU into the WFI state while a transfer is active
  159. * seems to cause the I2C block to timeout. Why? Good question. */
  160. if (omap2_i2c_active())
  161. return;
  162. /* The peripherals seem not to be able to wake up the MPU when
  163. * it is in retention mode. */
  164. if (omap2_allow_mpu_retention()) {
  165. /* REVISIT: These write to reserved bits? */
  166. prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
  167. prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
  168. prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
  169. /* Try to enter MPU retention */
  170. prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) |
  171. OMAP_LOGICRETSTATE,
  172. MPU_MOD, PM_PWSTCTRL);
  173. } else {
  174. /* Block MPU retention */
  175. prm_write_mod_reg(OMAP_LOGICRETSTATE, MPU_MOD, PM_PWSTCTRL);
  176. only_idle = 1;
  177. }
  178. if (omap2_pm_debug) {
  179. omap2_pm_dump(only_idle ? 2 : 1, 0, 0);
  180. getnstimeofday(&ts_preidle);
  181. }
  182. omap2_sram_idle();
  183. if (omap2_pm_debug) {
  184. unsigned long long tmp;
  185. getnstimeofday(&ts_postidle);
  186. ts_idle = timespec_sub(ts_postidle, ts_preidle);
  187. tmp = timespec_to_ns(&ts_idle) * NSEC_PER_USEC;
  188. omap2_pm_dump(only_idle ? 2 : 1, 1, tmp);
  189. }
  190. }
  191. static int omap2_can_sleep(void)
  192. {
  193. if (omap2_fclks_active())
  194. return 0;
  195. if (osc_ck->usecount > 1)
  196. return 0;
  197. if (omap_dma_running())
  198. return 0;
  199. return 1;
  200. }
  201. static void omap2_pm_idle(void)
  202. {
  203. local_irq_disable();
  204. local_fiq_disable();
  205. if (!omap2_can_sleep()) {
  206. if (omap_irq_pending())
  207. goto out;
  208. omap2_enter_mpu_retention();
  209. goto out;
  210. }
  211. if (omap_irq_pending())
  212. goto out;
  213. omap2_enter_full_retention();
  214. out:
  215. local_fiq_enable();
  216. local_irq_enable();
  217. }
  218. static int omap2_pm_prepare(void)
  219. {
  220. /* We cannot sleep in idle until we have resumed */
  221. disable_hlt();
  222. return 0;
  223. }
  224. static int omap2_pm_suspend(void)
  225. {
  226. u32 wken_wkup, mir1;
  227. wken_wkup = prm_read_mod_reg(WKUP_MOD, PM_WKEN);
  228. prm_write_mod_reg(wken_wkup & ~OMAP24XX_EN_GPT1, WKUP_MOD, PM_WKEN);
  229. /* Mask GPT1 */
  230. mir1 = omap_readl(0x480fe0a4);
  231. omap_writel(1 << 5, 0x480fe0ac);
  232. omap2_enter_full_retention();
  233. omap_writel(mir1, 0x480fe0a4);
  234. prm_write_mod_reg(wken_wkup, WKUP_MOD, PM_WKEN);
  235. return 0;
  236. }
  237. static int omap2_pm_enter(suspend_state_t state)
  238. {
  239. int ret = 0;
  240. switch (state) {
  241. case PM_SUSPEND_STANDBY:
  242. case PM_SUSPEND_MEM:
  243. ret = omap2_pm_suspend();
  244. break;
  245. default:
  246. ret = -EINVAL;
  247. }
  248. return ret;
  249. }
  250. static void omap2_pm_finish(void)
  251. {
  252. enable_hlt();
  253. }
  254. static struct platform_suspend_ops omap_pm_ops = {
  255. .prepare = omap2_pm_prepare,
  256. .enter = omap2_pm_enter,
  257. .finish = omap2_pm_finish,
  258. .valid = suspend_valid_only_mem,
  259. };
  260. static int _pm_clkdm_enable_hwsup(struct clockdomain *clkdm)
  261. {
  262. omap2_clkdm_allow_idle(clkdm);
  263. return 0;
  264. }
  265. static void __init prcm_setup_regs(void)
  266. {
  267. int i, num_mem_banks;
  268. struct powerdomain *pwrdm;
  269. /* Enable autoidle */
  270. prm_write_mod_reg(OMAP24XX_AUTOIDLE, OCP_MOD,
  271. OMAP2_PRCM_SYSCONFIG_OFFSET);
  272. /* Set all domain wakeup dependencies */
  273. prm_write_mod_reg(OMAP_EN_WKUP_MASK, MPU_MOD, PM_WKDEP);
  274. prm_write_mod_reg(0, OMAP24XX_DSP_MOD, PM_WKDEP);
  275. prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
  276. prm_write_mod_reg(0, CORE_MOD, PM_WKDEP);
  277. if (cpu_is_omap2430())
  278. prm_write_mod_reg(0, OMAP2430_MDM_MOD, PM_WKDEP);
  279. /*
  280. * Set CORE powerdomain memory banks to retain their contents
  281. * during RETENTION
  282. */
  283. num_mem_banks = pwrdm_get_mem_bank_count(core_pwrdm);
  284. for (i = 0; i < num_mem_banks; i++)
  285. pwrdm_set_mem_retst(core_pwrdm, i, PWRDM_POWER_RET);
  286. /* Set CORE powerdomain's next power state to RETENTION */
  287. pwrdm_set_next_pwrst(core_pwrdm, PWRDM_POWER_RET);
  288. /*
  289. * Set MPU powerdomain's next power state to RETENTION;
  290. * preserve logic state during retention
  291. */
  292. pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
  293. pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
  294. /* Force-power down DSP, GFX powerdomains */
  295. pwrdm = clkdm_get_pwrdm(dsp_clkdm);
  296. pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
  297. omap2_clkdm_sleep(dsp_clkdm);
  298. pwrdm = clkdm_get_pwrdm(gfx_clkdm);
  299. pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
  300. omap2_clkdm_sleep(gfx_clkdm);
  301. /* Enable clockdomain hardware-supervised control for all clkdms */
  302. clkdm_for_each(_pm_clkdm_enable_hwsup);
  303. /* Enable clock autoidle for all domains */
  304. cm_write_mod_reg(OMAP24XX_AUTO_CAM |
  305. OMAP24XX_AUTO_MAILBOXES |
  306. OMAP24XX_AUTO_WDT4 |
  307. OMAP2420_AUTO_WDT3 |
  308. OMAP24XX_AUTO_MSPRO |
  309. OMAP2420_AUTO_MMC |
  310. OMAP24XX_AUTO_FAC |
  311. OMAP2420_AUTO_EAC |
  312. OMAP24XX_AUTO_HDQ |
  313. OMAP24XX_AUTO_UART2 |
  314. OMAP24XX_AUTO_UART1 |
  315. OMAP24XX_AUTO_I2C2 |
  316. OMAP24XX_AUTO_I2C1 |
  317. OMAP24XX_AUTO_MCSPI2 |
  318. OMAP24XX_AUTO_MCSPI1 |
  319. OMAP24XX_AUTO_MCBSP2 |
  320. OMAP24XX_AUTO_MCBSP1 |
  321. OMAP24XX_AUTO_GPT12 |
  322. OMAP24XX_AUTO_GPT11 |
  323. OMAP24XX_AUTO_GPT10 |
  324. OMAP24XX_AUTO_GPT9 |
  325. OMAP24XX_AUTO_GPT8 |
  326. OMAP24XX_AUTO_GPT7 |
  327. OMAP24XX_AUTO_GPT6 |
  328. OMAP24XX_AUTO_GPT5 |
  329. OMAP24XX_AUTO_GPT4 |
  330. OMAP24XX_AUTO_GPT3 |
  331. OMAP24XX_AUTO_GPT2 |
  332. OMAP2420_AUTO_VLYNQ |
  333. OMAP24XX_AUTO_DSS,
  334. CORE_MOD, CM_AUTOIDLE1);
  335. cm_write_mod_reg(OMAP24XX_AUTO_UART3 |
  336. OMAP24XX_AUTO_SSI |
  337. OMAP24XX_AUTO_USB,
  338. CORE_MOD, CM_AUTOIDLE2);
  339. cm_write_mod_reg(OMAP24XX_AUTO_SDRC |
  340. OMAP24XX_AUTO_GPMC |
  341. OMAP24XX_AUTO_SDMA,
  342. CORE_MOD, CM_AUTOIDLE3);
  343. cm_write_mod_reg(OMAP24XX_AUTO_PKA |
  344. OMAP24XX_AUTO_AES |
  345. OMAP24XX_AUTO_RNG |
  346. OMAP24XX_AUTO_SHA |
  347. OMAP24XX_AUTO_DES,
  348. CORE_MOD, OMAP24XX_CM_AUTOIDLE4);
  349. cm_write_mod_reg(OMAP2420_AUTO_DSP_IPI, OMAP24XX_DSP_MOD, CM_AUTOIDLE);
  350. /* Put DPLL and both APLLs into autoidle mode */
  351. cm_write_mod_reg((0x03 << OMAP24XX_AUTO_DPLL_SHIFT) |
  352. (0x03 << OMAP24XX_AUTO_96M_SHIFT) |
  353. (0x03 << OMAP24XX_AUTO_54M_SHIFT),
  354. PLL_MOD, CM_AUTOIDLE);
  355. cm_write_mod_reg(OMAP24XX_AUTO_OMAPCTRL |
  356. OMAP24XX_AUTO_WDT1 |
  357. OMAP24XX_AUTO_MPU_WDT |
  358. OMAP24XX_AUTO_GPIOS |
  359. OMAP24XX_AUTO_32KSYNC |
  360. OMAP24XX_AUTO_GPT1,
  361. WKUP_MOD, CM_AUTOIDLE);
  362. /* REVISIT: Configure number of 32 kHz clock cycles for sys_clk
  363. * stabilisation */
  364. prm_write_mod_reg(15 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
  365. OMAP2_PRCM_CLKSSETUP_OFFSET);
  366. /* Configure automatic voltage transition */
  367. prm_write_mod_reg(2 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
  368. OMAP2_PRCM_VOLTSETUP_OFFSET);
  369. prm_write_mod_reg(OMAP24XX_AUTO_EXTVOLT |
  370. (0x1 << OMAP24XX_SETOFF_LEVEL_SHIFT) |
  371. OMAP24XX_MEMRETCTRL |
  372. (0x1 << OMAP24XX_SETRET_LEVEL_SHIFT) |
  373. (0x0 << OMAP24XX_VOLT_LEVEL_SHIFT),
  374. OMAP24XX_GR_MOD, OMAP2_PRCM_VOLTCTRL_OFFSET);
  375. /* Enable wake-up events */
  376. prm_write_mod_reg(OMAP24XX_EN_GPIOS | OMAP24XX_EN_GPT1,
  377. WKUP_MOD, PM_WKEN);
  378. }
  379. int __init omap2_pm_init(void)
  380. {
  381. u32 l;
  382. if (!cpu_is_omap24xx())
  383. return -ENODEV;
  384. printk(KERN_INFO "Power Management for OMAP2 initializing\n");
  385. l = prm_read_mod_reg(OCP_MOD, OMAP2_PRCM_REVISION_OFFSET);
  386. printk(KERN_INFO "PRCM revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  387. /* Look up important powerdomains, clockdomains */
  388. mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
  389. if (!mpu_pwrdm)
  390. pr_err("PM: mpu_pwrdm not found\n");
  391. core_pwrdm = pwrdm_lookup("core_pwrdm");
  392. if (!core_pwrdm)
  393. pr_err("PM: core_pwrdm not found\n");
  394. dsp_clkdm = clkdm_lookup("dsp_clkdm");
  395. if (!dsp_clkdm)
  396. pr_err("PM: mpu_clkdm not found\n");
  397. gfx_clkdm = clkdm_lookup("gfx_clkdm");
  398. if (!gfx_clkdm)
  399. pr_err("PM: gfx_clkdm not found\n");
  400. osc_ck = clk_get(NULL, "osc_ck");
  401. if (IS_ERR(osc_ck)) {
  402. printk(KERN_ERR "could not get osc_ck\n");
  403. return -ENODEV;
  404. }
  405. if (cpu_is_omap242x()) {
  406. emul_ck = clk_get(NULL, "emul_ck");
  407. if (IS_ERR(emul_ck)) {
  408. printk(KERN_ERR "could not get emul_ck\n");
  409. clk_put(osc_ck);
  410. return -ENODEV;
  411. }
  412. }
  413. prcm_setup_regs();
  414. /* Hack to prevent MPU retention when STI console is enabled. */
  415. {
  416. const struct omap_sti_console_config *sti;
  417. sti = omap_get_config(OMAP_TAG_STI_CONSOLE,
  418. struct omap_sti_console_config);
  419. if (sti != NULL && sti->enable)
  420. sti_console_enabled = 1;
  421. }
  422. /*
  423. * We copy the assembler sleep/wakeup routines to SRAM.
  424. * These routines need to be in SRAM as that's the only
  425. * memory the MPU can see when it wakes up.
  426. */
  427. if (cpu_is_omap24xx()) {
  428. omap2_sram_idle = omap_sram_push(omap24xx_idle_loop_suspend,
  429. omap24xx_idle_loop_suspend_sz);
  430. omap2_sram_suspend = omap_sram_push(omap24xx_cpu_suspend,
  431. omap24xx_cpu_suspend_sz);
  432. }
  433. suspend_set_ops(&omap_pm_ops);
  434. pm_idle = omap2_pm_idle;
  435. return 0;
  436. }
  437. late_initcall(omap2_pm_init);