pm24xx.c 11 KB

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