pm24xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 <asm/system_misc.h>
  36. #include <mach/irqs.h>
  37. #include <plat/clock.h>
  38. #include <plat/sram.h>
  39. #include <plat/dma.h>
  40. #include <plat/board.h>
  41. #include "common.h"
  42. #include "prm2xxx_3xxx.h"
  43. #include "prm-regbits-24xx.h"
  44. #include "cm2xxx_3xxx.h"
  45. #include "cm-regbits-24xx.h"
  46. #include "sdrc.h"
  47. #include "pm.h"
  48. #include "control.h"
  49. #include "powerdomain.h"
  50. #include "clockdomain.h"
  51. #ifdef CONFIG_SUSPEND
  52. static suspend_state_t suspend_state = PM_SUSPEND_ON;
  53. static inline bool is_suspending(void)
  54. {
  55. return (suspend_state != PM_SUSPEND_ON);
  56. }
  57. #else
  58. static inline bool is_suspending(void)
  59. {
  60. return false;
  61. }
  62. #endif
  63. static void (*omap2_sram_idle)(void);
  64. static void (*omap2_sram_suspend)(u32 dllctrl, void __iomem *sdrc_dlla_ctrl,
  65. void __iomem *sdrc_power);
  66. static struct powerdomain *mpu_pwrdm, *core_pwrdm;
  67. static struct clockdomain *dsp_clkdm, *mpu_clkdm, *wkup_clkdm, *gfx_clkdm;
  68. static struct clk *osc_ck, *emul_ck;
  69. static int omap2_fclks_active(void)
  70. {
  71. u32 f1, f2;
  72. f1 = omap2_cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
  73. f2 = omap2_cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
  74. return (f1 | f2) ? 1 : 0;
  75. }
  76. static void omap2_enter_full_retention(void)
  77. {
  78. u32 l;
  79. /* There is 1 reference hold for all children of the oscillator
  80. * clock, the following will remove it. If no one else uses the
  81. * oscillator itself it will be disabled if/when we enter retention
  82. * mode.
  83. */
  84. clk_disable(osc_ck);
  85. /* Clear old wake-up events */
  86. /* REVISIT: These write to reserved bits? */
  87. omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
  88. omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
  89. omap2_prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
  90. /*
  91. * Set MPU powerdomain's next power state to RETENTION;
  92. * preserve logic state during retention
  93. */
  94. pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
  95. pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
  96. /* Workaround to kill USB */
  97. l = omap_ctrl_readl(OMAP2_CONTROL_DEVCONF0) | OMAP24XX_USBSTANDBYCTRL;
  98. omap_ctrl_writel(l, OMAP2_CONTROL_DEVCONF0);
  99. omap2_gpio_prepare_for_idle(0);
  100. /* One last check for pending IRQs to avoid extra latency due
  101. * to sleeping unnecessarily. */
  102. if (omap_irq_pending())
  103. goto no_sleep;
  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. no_sleep:
  109. omap2_gpio_resume_after_idle();
  110. clk_enable(osc_ck);
  111. /* clear CORE wake-up events */
  112. omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
  113. omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
  114. /* wakeup domain events - bit 1: GPT1, bit5 GPIO */
  115. omap2_prm_clear_mod_reg_bits(0x4 | 0x1, WKUP_MOD, PM_WKST);
  116. /* MPU domain wake events */
  117. l = omap2_prm_read_mod_reg(OCP_MOD, OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
  118. if (l & 0x01)
  119. omap2_prm_write_mod_reg(0x01, OCP_MOD,
  120. OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
  121. if (l & 0x20)
  122. omap2_prm_write_mod_reg(0x20, OCP_MOD,
  123. OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
  124. /* Mask future PRCM-to-MPU interrupts */
  125. omap2_prm_write_mod_reg(0x0, OCP_MOD, OMAP2_PRCM_IRQSTATUS_MPU_OFFSET);
  126. }
  127. static int omap2_i2c_active(void)
  128. {
  129. u32 l;
  130. l = omap2_cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
  131. return l & (OMAP2420_EN_I2C2_MASK | OMAP2420_EN_I2C1_MASK);
  132. }
  133. static int sti_console_enabled;
  134. static int omap2_allow_mpu_retention(void)
  135. {
  136. u32 l;
  137. /* Check for MMC, UART2, UART1, McSPI2, McSPI1 and DSS1. */
  138. l = omap2_cm_read_mod_reg(CORE_MOD, CM_FCLKEN1);
  139. if (l & (OMAP2420_EN_MMC_MASK | OMAP24XX_EN_UART2_MASK |
  140. OMAP24XX_EN_UART1_MASK | OMAP24XX_EN_MCSPI2_MASK |
  141. OMAP24XX_EN_MCSPI1_MASK | OMAP24XX_EN_DSS1_MASK))
  142. return 0;
  143. /* Check for UART3. */
  144. l = omap2_cm_read_mod_reg(CORE_MOD, OMAP24XX_CM_FCLKEN2);
  145. if (l & OMAP24XX_EN_UART3_MASK)
  146. return 0;
  147. if (sti_console_enabled)
  148. return 0;
  149. return 1;
  150. }
  151. static void omap2_enter_mpu_retention(void)
  152. {
  153. int only_idle = 0;
  154. /* Putting MPU into the WFI state while a transfer is active
  155. * seems to cause the I2C block to timeout. Why? Good question. */
  156. if (omap2_i2c_active())
  157. return;
  158. /* The peripherals seem not to be able to wake up the MPU when
  159. * it is in retention mode. */
  160. if (omap2_allow_mpu_retention()) {
  161. /* REVISIT: These write to reserved bits? */
  162. omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, PM_WKST1);
  163. omap2_prm_write_mod_reg(0xffffffff, CORE_MOD, OMAP24XX_PM_WKST2);
  164. omap2_prm_write_mod_reg(0xffffffff, WKUP_MOD, PM_WKST);
  165. /* Try to enter MPU retention */
  166. omap2_prm_write_mod_reg((0x01 << OMAP_POWERSTATE_SHIFT) |
  167. OMAP_LOGICRETSTATE_MASK,
  168. MPU_MOD, OMAP2_PM_PWSTCTRL);
  169. } else {
  170. /* Block MPU retention */
  171. omap2_prm_write_mod_reg(OMAP_LOGICRETSTATE_MASK, MPU_MOD,
  172. OMAP2_PM_PWSTCTRL);
  173. only_idle = 1;
  174. }
  175. omap2_sram_idle();
  176. }
  177. static int omap2_can_sleep(void)
  178. {
  179. if (omap2_fclks_active())
  180. return 0;
  181. if (osc_ck->usecount > 1)
  182. return 0;
  183. if (omap_dma_running())
  184. return 0;
  185. return 1;
  186. }
  187. static void omap2_pm_idle(void)
  188. {
  189. local_irq_disable();
  190. local_fiq_disable();
  191. if (!omap2_can_sleep()) {
  192. if (omap_irq_pending())
  193. goto out;
  194. omap2_enter_mpu_retention();
  195. goto out;
  196. }
  197. if (omap_irq_pending())
  198. goto out;
  199. omap2_enter_full_retention();
  200. out:
  201. local_fiq_enable();
  202. local_irq_enable();
  203. }
  204. #ifdef CONFIG_SUSPEND
  205. static int omap2_pm_begin(suspend_state_t state)
  206. {
  207. disable_hlt();
  208. suspend_state = state;
  209. return 0;
  210. }
  211. static int omap2_pm_suspend(void)
  212. {
  213. u32 wken_wkup, mir1;
  214. wken_wkup = omap2_prm_read_mod_reg(WKUP_MOD, PM_WKEN);
  215. wken_wkup &= ~OMAP24XX_EN_GPT1_MASK;
  216. omap2_prm_write_mod_reg(wken_wkup, WKUP_MOD, PM_WKEN);
  217. /* Mask GPT1 */
  218. mir1 = omap_readl(0x480fe0a4);
  219. omap_writel(1 << 5, 0x480fe0ac);
  220. omap2_enter_full_retention();
  221. omap_writel(mir1, 0x480fe0a4);
  222. omap2_prm_write_mod_reg(wken_wkup, WKUP_MOD, PM_WKEN);
  223. return 0;
  224. }
  225. static int omap2_pm_enter(suspend_state_t state)
  226. {
  227. int ret = 0;
  228. switch (state) {
  229. case PM_SUSPEND_STANDBY:
  230. case PM_SUSPEND_MEM:
  231. ret = omap2_pm_suspend();
  232. break;
  233. default:
  234. ret = -EINVAL;
  235. }
  236. return ret;
  237. }
  238. static void omap2_pm_end(void)
  239. {
  240. suspend_state = PM_SUSPEND_ON;
  241. enable_hlt();
  242. }
  243. static const struct platform_suspend_ops omap_pm_ops = {
  244. .begin = omap2_pm_begin,
  245. .enter = omap2_pm_enter,
  246. .end = omap2_pm_end,
  247. .valid = suspend_valid_only_mem,
  248. };
  249. #else
  250. static const struct platform_suspend_ops __initdata omap_pm_ops;
  251. #endif /* CONFIG_SUSPEND */
  252. /* XXX This function should be shareable between OMAP2xxx and OMAP3 */
  253. static int __init clkdms_setup(struct clockdomain *clkdm, void *unused)
  254. {
  255. if (clkdm->flags & CLKDM_CAN_ENABLE_AUTO)
  256. clkdm_allow_idle(clkdm);
  257. else if (clkdm->flags & CLKDM_CAN_FORCE_SLEEP &&
  258. atomic_read(&clkdm->usecount) == 0)
  259. clkdm_sleep(clkdm);
  260. return 0;
  261. }
  262. static void __init prcm_setup_regs(void)
  263. {
  264. int i, num_mem_banks;
  265. struct powerdomain *pwrdm;
  266. /*
  267. * Enable autoidle
  268. * XXX This should be handled by hwmod code or PRCM init code
  269. */
  270. omap2_prm_write_mod_reg(OMAP24XX_AUTOIDLE_MASK, OCP_MOD,
  271. OMAP2_PRCM_SYSCONFIG_OFFSET);
  272. /*
  273. * Set CORE powerdomain memory banks to retain their contents
  274. * during RETENTION
  275. */
  276. num_mem_banks = pwrdm_get_mem_bank_count(core_pwrdm);
  277. for (i = 0; i < num_mem_banks; i++)
  278. pwrdm_set_mem_retst(core_pwrdm, i, PWRDM_POWER_RET);
  279. /* Set CORE powerdomain's next power state to RETENTION */
  280. pwrdm_set_next_pwrst(core_pwrdm, PWRDM_POWER_RET);
  281. /*
  282. * Set MPU powerdomain's next power state to RETENTION;
  283. * preserve logic state during retention
  284. */
  285. pwrdm_set_logic_retst(mpu_pwrdm, PWRDM_POWER_RET);
  286. pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_RET);
  287. /* Force-power down DSP, GFX powerdomains */
  288. pwrdm = clkdm_get_pwrdm(dsp_clkdm);
  289. pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
  290. clkdm_sleep(dsp_clkdm);
  291. pwrdm = clkdm_get_pwrdm(gfx_clkdm);
  292. pwrdm_set_next_pwrst(pwrdm, PWRDM_POWER_OFF);
  293. clkdm_sleep(gfx_clkdm);
  294. /* Enable hardware-supervised idle for all clkdms */
  295. clkdm_for_each(clkdms_setup, NULL);
  296. clkdm_add_wkdep(mpu_clkdm, wkup_clkdm);
  297. /* REVISIT: Configure number of 32 kHz clock cycles for sys_clk
  298. * stabilisation */
  299. omap2_prm_write_mod_reg(15 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
  300. OMAP2_PRCM_CLKSSETUP_OFFSET);
  301. /* Configure automatic voltage transition */
  302. omap2_prm_write_mod_reg(2 << OMAP_SETUP_TIME_SHIFT, OMAP24XX_GR_MOD,
  303. OMAP2_PRCM_VOLTSETUP_OFFSET);
  304. omap2_prm_write_mod_reg(OMAP24XX_AUTO_EXTVOLT_MASK |
  305. (0x1 << OMAP24XX_SETOFF_LEVEL_SHIFT) |
  306. OMAP24XX_MEMRETCTRL_MASK |
  307. (0x1 << OMAP24XX_SETRET_LEVEL_SHIFT) |
  308. (0x0 << OMAP24XX_VOLT_LEVEL_SHIFT),
  309. OMAP24XX_GR_MOD, OMAP2_PRCM_VOLTCTRL_OFFSET);
  310. /* Enable wake-up events */
  311. omap2_prm_write_mod_reg(OMAP24XX_EN_GPIOS_MASK | OMAP24XX_EN_GPT1_MASK,
  312. WKUP_MOD, PM_WKEN);
  313. }
  314. static int __init omap2_pm_init(void)
  315. {
  316. u32 l;
  317. if (!cpu_is_omap24xx())
  318. return -ENODEV;
  319. printk(KERN_INFO "Power Management for OMAP2 initializing\n");
  320. l = omap2_prm_read_mod_reg(OCP_MOD, OMAP2_PRCM_REVISION_OFFSET);
  321. printk(KERN_INFO "PRCM revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  322. /* Look up important powerdomains */
  323. mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
  324. if (!mpu_pwrdm)
  325. pr_err("PM: mpu_pwrdm not found\n");
  326. core_pwrdm = pwrdm_lookup("core_pwrdm");
  327. if (!core_pwrdm)
  328. pr_err("PM: core_pwrdm not found\n");
  329. /* Look up important clockdomains */
  330. mpu_clkdm = clkdm_lookup("mpu_clkdm");
  331. if (!mpu_clkdm)
  332. pr_err("PM: mpu_clkdm not found\n");
  333. wkup_clkdm = clkdm_lookup("wkup_clkdm");
  334. if (!wkup_clkdm)
  335. pr_err("PM: wkup_clkdm not found\n");
  336. dsp_clkdm = clkdm_lookup("dsp_clkdm");
  337. if (!dsp_clkdm)
  338. pr_err("PM: dsp_clkdm not found\n");
  339. gfx_clkdm = clkdm_lookup("gfx_clkdm");
  340. if (!gfx_clkdm)
  341. pr_err("PM: gfx_clkdm not found\n");
  342. osc_ck = clk_get(NULL, "osc_ck");
  343. if (IS_ERR(osc_ck)) {
  344. printk(KERN_ERR "could not get osc_ck\n");
  345. return -ENODEV;
  346. }
  347. if (cpu_is_omap242x()) {
  348. emul_ck = clk_get(NULL, "emul_ck");
  349. if (IS_ERR(emul_ck)) {
  350. printk(KERN_ERR "could not get emul_ck\n");
  351. clk_put(osc_ck);
  352. return -ENODEV;
  353. }
  354. }
  355. prcm_setup_regs();
  356. /* Hack to prevent MPU retention when STI console is enabled. */
  357. {
  358. const struct omap_sti_console_config *sti;
  359. sti = omap_get_config(OMAP_TAG_STI_CONSOLE,
  360. struct omap_sti_console_config);
  361. if (sti != NULL && sti->enable)
  362. sti_console_enabled = 1;
  363. }
  364. /*
  365. * We copy the assembler sleep/wakeup routines to SRAM.
  366. * These routines need to be in SRAM as that's the only
  367. * memory the MPU can see when it wakes up.
  368. */
  369. if (cpu_is_omap24xx()) {
  370. omap2_sram_idle = omap_sram_push(omap24xx_idle_loop_suspend,
  371. omap24xx_idle_loop_suspend_sz);
  372. omap2_sram_suspend = omap_sram_push(omap24xx_cpu_suspend,
  373. omap24xx_cpu_suspend_sz);
  374. }
  375. suspend_set_ops(&omap_pm_ops);
  376. pm_idle = omap2_pm_idle;
  377. return 0;
  378. }
  379. late_initcall(omap2_pm_init);