pm24xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. /* 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, PM_PWSTCTRL);
  182. } else {
  183. /* Block MPU retention */
  184. prm_write_mod_reg(OMAP_LOGICRETSTATE, MPU_MOD, PM_PWSTCTRL);
  185. only_idle = 1;
  186. }
  187. if (omap2_pm_debug) {
  188. omap2_pm_dump(only_idle ? 2 : 1, 0, 0);
  189. getnstimeofday(&ts_preidle);
  190. }
  191. omap2_sram_idle();
  192. if (omap2_pm_debug) {
  193. unsigned long long tmp;
  194. getnstimeofday(&ts_postidle);
  195. ts_idle = timespec_sub(ts_postidle, ts_preidle);
  196. tmp = timespec_to_ns(&ts_idle) * NSEC_PER_USEC;
  197. omap2_pm_dump(only_idle ? 2 : 1, 1, tmp);
  198. }
  199. }
  200. static int omap2_can_sleep(void)
  201. {
  202. if (omap2_fclks_active())
  203. return 0;
  204. if (osc_ck->usecount > 1)
  205. return 0;
  206. if (omap_dma_running())
  207. return 0;
  208. return 1;
  209. }
  210. static void omap2_pm_idle(void)
  211. {
  212. local_irq_disable();
  213. local_fiq_disable();
  214. if (!omap2_can_sleep()) {
  215. if (omap_irq_pending())
  216. goto out;
  217. omap2_enter_mpu_retention();
  218. goto out;
  219. }
  220. if (omap_irq_pending())
  221. goto out;
  222. omap2_enter_full_retention();
  223. out:
  224. local_fiq_enable();
  225. local_irq_enable();
  226. }
  227. static int omap2_pm_prepare(void)
  228. {
  229. /* We cannot sleep in idle until we have resumed */
  230. disable_hlt();
  231. return 0;
  232. }
  233. static int omap2_pm_suspend(void)
  234. {
  235. u32 wken_wkup, mir1;
  236. wken_wkup = prm_read_mod_reg(WKUP_MOD, PM_WKEN);
  237. prm_write_mod_reg(wken_wkup & ~OMAP24XX_EN_GPT1, WKUP_MOD, PM_WKEN);
  238. /* Mask GPT1 */
  239. mir1 = omap_readl(0x480fe0a4);
  240. omap_writel(1 << 5, 0x480fe0ac);
  241. omap_uart_prepare_suspend();
  242. omap2_enter_full_retention();
  243. omap_writel(mir1, 0x480fe0a4);
  244. prm_write_mod_reg(wken_wkup, WKUP_MOD, PM_WKEN);
  245. return 0;
  246. }
  247. static int omap2_pm_enter(suspend_state_t state)
  248. {
  249. int ret = 0;
  250. switch (state) {
  251. case PM_SUSPEND_STANDBY:
  252. case PM_SUSPEND_MEM:
  253. ret = omap2_pm_suspend();
  254. break;
  255. default:
  256. ret = -EINVAL;
  257. }
  258. return ret;
  259. }
  260. static void omap2_pm_finish(void)
  261. {
  262. enable_hlt();
  263. }
  264. static struct platform_suspend_ops omap_pm_ops = {
  265. .prepare = omap2_pm_prepare,
  266. .enter = omap2_pm_enter,
  267. .finish = omap2_pm_finish,
  268. .valid = suspend_valid_only_mem,
  269. };
  270. static int _pm_clkdm_enable_hwsup(struct clockdomain *clkdm, void *unused)
  271. {
  272. omap2_clkdm_allow_idle(clkdm);
  273. return 0;
  274. }
  275. static void __init prcm_setup_regs(void)
  276. {
  277. int i, num_mem_banks;
  278. struct powerdomain *pwrdm;
  279. /* Enable autoidle */
  280. prm_write_mod_reg(OMAP24XX_AUTOIDLE, OCP_MOD,
  281. OMAP2_PRCM_SYSCONFIG_OFFSET);
  282. /* Set all domain wakeup dependencies */
  283. prm_write_mod_reg(OMAP_EN_WKUP_MASK, MPU_MOD, PM_WKDEP);
  284. prm_write_mod_reg(0, OMAP24XX_DSP_MOD, PM_WKDEP);
  285. prm_write_mod_reg(0, GFX_MOD, PM_WKDEP);
  286. prm_write_mod_reg(0, CORE_MOD, PM_WKDEP);
  287. if (cpu_is_omap2430())
  288. prm_write_mod_reg(0, OMAP2430_MDM_MOD, PM_WKDEP);
  289. /*
  290. * Set CORE powerdomain memory banks to retain their contents
  291. * during RETENTION
  292. */
  293. num_mem_banks = pwrdm_get_mem_bank_count(core_pwrdm);
  294. for (i = 0; i < num_mem_banks; i++)
  295. pwrdm_set_mem_retst(core_pwrdm, i, PWRDM_POWER_RET);
  296. /* Set CORE powerdomain's next power state to RETENTION */
  297. pwrdm_set_next_pwrst(core_pwrdm, PWRDM_POWER_RET);
  298. /*
  299. * Set MPU powerdomain's next power state to RETENTION;
  300. * preserve logic state during retention
  301. */
  302. pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
  303. pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
  304. /* Force-power down DSP, GFX powerdomains */
  305. pwrdm = clkdm_get_pwrdm(dsp_clkdm);
  306. pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
  307. omap2_clkdm_sleep(dsp_clkdm);
  308. pwrdm = clkdm_get_pwrdm(gfx_clkdm);
  309. pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
  310. omap2_clkdm_sleep(gfx_clkdm);
  311. /* Enable clockdomain hardware-supervised control for all clkdms */
  312. clkdm_for_each(_pm_clkdm_enable_hwsup, NULL);
  313. /* Enable clock autoidle for all domains */
  314. cm_write_mod_reg(OMAP24XX_AUTO_CAM |
  315. OMAP24XX_AUTO_MAILBOXES |
  316. OMAP24XX_AUTO_WDT4 |
  317. OMAP2420_AUTO_WDT3 |
  318. OMAP24XX_AUTO_MSPRO |
  319. OMAP2420_AUTO_MMC |
  320. OMAP24XX_AUTO_FAC |
  321. OMAP2420_AUTO_EAC |
  322. OMAP24XX_AUTO_HDQ |
  323. OMAP24XX_AUTO_UART2 |
  324. OMAP24XX_AUTO_UART1 |
  325. OMAP24XX_AUTO_I2C2 |
  326. OMAP24XX_AUTO_I2C1 |
  327. OMAP24XX_AUTO_MCSPI2 |
  328. OMAP24XX_AUTO_MCSPI1 |
  329. OMAP24XX_AUTO_MCBSP2 |
  330. OMAP24XX_AUTO_MCBSP1 |
  331. OMAP24XX_AUTO_GPT12 |
  332. OMAP24XX_AUTO_GPT11 |
  333. OMAP24XX_AUTO_GPT10 |
  334. OMAP24XX_AUTO_GPT9 |
  335. OMAP24XX_AUTO_GPT8 |
  336. OMAP24XX_AUTO_GPT7 |
  337. OMAP24XX_AUTO_GPT6 |
  338. OMAP24XX_AUTO_GPT5 |
  339. OMAP24XX_AUTO_GPT4 |
  340. OMAP24XX_AUTO_GPT3 |
  341. OMAP24XX_AUTO_GPT2 |
  342. OMAP2420_AUTO_VLYNQ |
  343. OMAP24XX_AUTO_DSS,
  344. CORE_MOD, CM_AUTOIDLE1);
  345. cm_write_mod_reg(OMAP24XX_AUTO_UART3 |
  346. OMAP24XX_AUTO_SSI |
  347. OMAP24XX_AUTO_USB,
  348. CORE_MOD, CM_AUTOIDLE2);
  349. cm_write_mod_reg(OMAP24XX_AUTO_SDRC |
  350. OMAP24XX_AUTO_GPMC |
  351. OMAP24XX_AUTO_SDMA,
  352. CORE_MOD, CM_AUTOIDLE3);
  353. cm_write_mod_reg(OMAP24XX_AUTO_PKA |
  354. OMAP24XX_AUTO_AES |
  355. OMAP24XX_AUTO_RNG |
  356. OMAP24XX_AUTO_SHA |
  357. OMAP24XX_AUTO_DES,
  358. CORE_MOD, OMAP24XX_CM_AUTOIDLE4);
  359. cm_write_mod_reg(OMAP2420_AUTO_DSP_IPI, OMAP24XX_DSP_MOD, CM_AUTOIDLE);
  360. /* Put DPLL and both APLLs into autoidle mode */
  361. cm_write_mod_reg((0x03 << OMAP24XX_AUTO_DPLL_SHIFT) |
  362. (0x03 << OMAP24XX_AUTO_96M_SHIFT) |
  363. (0x03 << OMAP24XX_AUTO_54M_SHIFT),
  364. PLL_MOD, CM_AUTOIDLE);
  365. cm_write_mod_reg(OMAP24XX_AUTO_OMAPCTRL |
  366. OMAP24XX_AUTO_WDT1 |
  367. OMAP24XX_AUTO_MPU_WDT |
  368. OMAP24XX_AUTO_GPIOS |
  369. OMAP24XX_AUTO_32KSYNC |
  370. OMAP24XX_AUTO_GPT1,
  371. WKUP_MOD, CM_AUTOIDLE);
  372. /* REVISIT: Configure number of 32 kHz clock cycles for sys_clk
  373. * stabilisation */
  374. prm_write_mod_reg(15 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
  375. OMAP2_PRCM_CLKSSETUP_OFFSET);
  376. /* Configure automatic voltage transition */
  377. prm_write_mod_reg(2 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
  378. OMAP2_PRCM_VOLTSETUP_OFFSET);
  379. prm_write_mod_reg(OMAP24XX_AUTO_EXTVOLT |
  380. (0x1 << OMAP24XX_SETOFF_LEVEL_SHIFT) |
  381. OMAP24XX_MEMRETCTRL |
  382. (0x1 << OMAP24XX_SETRET_LEVEL_SHIFT) |
  383. (0x0 << OMAP24XX_VOLT_LEVEL_SHIFT),
  384. OMAP24XX_GR_MOD, OMAP2_PRCM_VOLTCTRL_OFFSET);
  385. /* Enable wake-up events */
  386. prm_write_mod_reg(OMAP24XX_EN_GPIOS | OMAP24XX_EN_GPT1,
  387. WKUP_MOD, PM_WKEN);
  388. }
  389. static int __init omap2_pm_init(void)
  390. {
  391. u32 l;
  392. if (!cpu_is_omap24xx())
  393. return -ENODEV;
  394. printk(KERN_INFO "Power Management for OMAP2 initializing\n");
  395. l = prm_read_mod_reg(OCP_MOD, OMAP2_PRCM_REVISION_OFFSET);
  396. printk(KERN_INFO "PRCM revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  397. /* Look up important powerdomains, clockdomains */
  398. mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
  399. if (!mpu_pwrdm)
  400. pr_err("PM: mpu_pwrdm not found\n");
  401. core_pwrdm = pwrdm_lookup("core_pwrdm");
  402. if (!core_pwrdm)
  403. pr_err("PM: core_pwrdm not found\n");
  404. dsp_clkdm = clkdm_lookup("dsp_clkdm");
  405. if (!dsp_clkdm)
  406. pr_err("PM: mpu_clkdm not found\n");
  407. gfx_clkdm = clkdm_lookup("gfx_clkdm");
  408. if (!gfx_clkdm)
  409. pr_err("PM: gfx_clkdm not found\n");
  410. osc_ck = clk_get(NULL, "osc_ck");
  411. if (IS_ERR(osc_ck)) {
  412. printk(KERN_ERR "could not get osc_ck\n");
  413. return -ENODEV;
  414. }
  415. if (cpu_is_omap242x()) {
  416. emul_ck = clk_get(NULL, "emul_ck");
  417. if (IS_ERR(emul_ck)) {
  418. printk(KERN_ERR "could not get emul_ck\n");
  419. clk_put(osc_ck);
  420. return -ENODEV;
  421. }
  422. }
  423. prcm_setup_regs();
  424. /* Hack to prevent MPU retention when STI console is enabled. */
  425. {
  426. const struct omap_sti_console_config *sti;
  427. sti = omap_get_config(OMAP_TAG_STI_CONSOLE,
  428. struct omap_sti_console_config);
  429. if (sti != NULL && sti->enable)
  430. sti_console_enabled = 1;
  431. }
  432. /*
  433. * We copy the assembler sleep/wakeup routines to SRAM.
  434. * These routines need to be in SRAM as that's the only
  435. * memory the MPU can see when it wakes up.
  436. */
  437. if (cpu_is_omap24xx()) {
  438. omap2_sram_idle = omap_sram_push(omap24xx_idle_loop_suspend,
  439. omap24xx_idle_loop_suspend_sz);
  440. omap2_sram_suspend = omap_sram_push(omap24xx_cpu_suspend,
  441. omap24xx_cpu_suspend_sz);
  442. }
  443. suspend_set_ops(&omap_pm_ops);
  444. pm_idle = omap2_pm_idle;
  445. return 0;
  446. }
  447. late_initcall(omap2_pm_init);