pm24xx.c 11 KB

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