pm24xx.c 9.5 KB

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