pm24xx.c 14 KB

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