pm24xx.c 8.7 KB

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