pm24xx.c 11 KB

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