pm24xx.c 13 KB

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