pm24xx.c 9.7 KB

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