pm34xx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. /*
  2. * OMAP3 Power Management Routines
  3. *
  4. * Copyright (C) 2006-2008 Nokia Corporation
  5. * Tony Lindgren <tony@atomide.com>
  6. * Jouni Hogander
  7. *
  8. * Copyright (C) 2007 Texas Instruments, Inc.
  9. * Rajendra Nayak <rnayak@ti.com>
  10. *
  11. * Copyright (C) 2005 Texas Instruments, Inc.
  12. * Richard Woodruff <r-woodruff2@ti.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/pm.h>
  21. #include <linux/suspend.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/module.h>
  24. #include <linux/list.h>
  25. #include <linux/err.h>
  26. #include <linux/gpio.h>
  27. #include <linux/clk.h>
  28. #include <linux/delay.h>
  29. #include <linux/slab.h>
  30. #include <linux/omap-dma.h>
  31. #include <linux/omap-gpmc.h>
  32. #include <linux/platform_data/gpio-omap.h>
  33. #include <trace/events/power.h>
  34. #include <asm/fncpy.h>
  35. #include <asm/suspend.h>
  36. #include <asm/system_misc.h>
  37. #include "clockdomain.h"
  38. #include "powerdomain.h"
  39. #include "soc.h"
  40. #include "common.h"
  41. #include "cm3xxx.h"
  42. #include "cm-regbits-34xx.h"
  43. #include "prm-regbits-34xx.h"
  44. #include "prm3xxx.h"
  45. #include "pm.h"
  46. #include "sdrc.h"
  47. #include "omap-secure.h"
  48. #include "sram.h"
  49. #include "control.h"
  50. #include "vc.h"
  51. /* pm34xx errata defined in pm.h */
  52. u16 pm34xx_errata;
  53. struct power_state {
  54. struct powerdomain *pwrdm;
  55. u32 next_state;
  56. #ifdef CONFIG_SUSPEND
  57. u32 saved_state;
  58. #endif
  59. struct list_head node;
  60. };
  61. static LIST_HEAD(pwrst_list);
  62. void (*omap3_do_wfi_sram)(void);
  63. static struct powerdomain *mpu_pwrdm, *neon_pwrdm;
  64. static struct powerdomain *core_pwrdm, *per_pwrdm;
  65. static void omap3_core_save_context(void)
  66. {
  67. omap3_ctrl_save_padconf();
  68. /*
  69. * Force write last pad into memory, as this can fail in some
  70. * cases according to errata 1.157, 1.185
  71. */
  72. omap_ctrl_writel(omap_ctrl_readl(OMAP343X_PADCONF_ETK_D14),
  73. OMAP343X_CONTROL_MEM_WKUP + 0x2a0);
  74. /* Save the Interrupt controller context */
  75. omap_intc_save_context();
  76. /* Save the GPMC context */
  77. omap3_gpmc_save_context();
  78. /* Save the system control module context, padconf already save above*/
  79. omap3_control_save_context();
  80. omap_dma_global_context_save();
  81. }
  82. static void omap3_core_restore_context(void)
  83. {
  84. /* Restore the control module context, padconf restored by h/w */
  85. omap3_control_restore_context();
  86. /* Restore the GPMC context */
  87. omap3_gpmc_restore_context();
  88. /* Restore the interrupt controller context */
  89. omap_intc_restore_context();
  90. omap_dma_global_context_restore();
  91. }
  92. /*
  93. * FIXME: This function should be called before entering off-mode after
  94. * OMAP3 secure services have been accessed. Currently it is only called
  95. * once during boot sequence, but this works as we are not using secure
  96. * services.
  97. */
  98. static void omap3_save_secure_ram_context(void)
  99. {
  100. u32 ret;
  101. int mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
  102. if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
  103. /*
  104. * MPU next state must be set to POWER_ON temporarily,
  105. * otherwise the WFI executed inside the ROM code
  106. * will hang the system.
  107. */
  108. pwrdm_set_next_pwrst(mpu_pwrdm, PWRDM_POWER_ON);
  109. ret = omap3_save_secure_ram(omap3_secure_ram_storage,
  110. OMAP3_SAVE_SECURE_RAM_SZ);
  111. pwrdm_set_next_pwrst(mpu_pwrdm, mpu_next_state);
  112. /* Following is for error tracking, it should not happen */
  113. if (ret) {
  114. pr_err("save_secure_sram() returns %08x\n", ret);
  115. while (1)
  116. ;
  117. }
  118. }
  119. }
  120. static irqreturn_t _prcm_int_handle_io(int irq, void *unused)
  121. {
  122. int c;
  123. c = omap_prm_clear_mod_irqs(WKUP_MOD, 1, OMAP3430_ST_IO_MASK |
  124. OMAP3430_ST_IO_CHAIN_MASK);
  125. return c ? IRQ_HANDLED : IRQ_NONE;
  126. }
  127. static irqreturn_t _prcm_int_handle_wakeup(int irq, void *unused)
  128. {
  129. int c;
  130. /*
  131. * Clear all except ST_IO and ST_IO_CHAIN for wkup module,
  132. * these are handled in a separate handler to avoid acking
  133. * IO events before parsing in mux code
  134. */
  135. c = omap_prm_clear_mod_irqs(WKUP_MOD, 1, ~(OMAP3430_ST_IO_MASK |
  136. OMAP3430_ST_IO_CHAIN_MASK));
  137. c += omap_prm_clear_mod_irqs(CORE_MOD, 1, ~0);
  138. c += omap_prm_clear_mod_irqs(OMAP3430_PER_MOD, 1, ~0);
  139. if (omap_rev() > OMAP3430_REV_ES1_0) {
  140. c += omap_prm_clear_mod_irqs(CORE_MOD, 3, ~0);
  141. c += omap_prm_clear_mod_irqs(OMAP3430ES2_USBHOST_MOD, 1, ~0);
  142. }
  143. return c ? IRQ_HANDLED : IRQ_NONE;
  144. }
  145. static void omap34xx_save_context(u32 *save)
  146. {
  147. u32 val;
  148. /* Read Auxiliary Control Register */
  149. asm("mrc p15, 0, %0, c1, c0, 1" : "=r" (val));
  150. *save++ = 1;
  151. *save++ = val;
  152. /* Read L2 AUX ctrl register */
  153. asm("mrc p15, 1, %0, c9, c0, 2" : "=r" (val));
  154. *save++ = 1;
  155. *save++ = val;
  156. }
  157. static int omap34xx_do_sram_idle(unsigned long save_state)
  158. {
  159. omap34xx_cpu_suspend(save_state);
  160. return 0;
  161. }
  162. void omap_sram_idle(void)
  163. {
  164. /* Variable to tell what needs to be saved and restored
  165. * in omap_sram_idle*/
  166. /* save_state = 0 => Nothing to save and restored */
  167. /* save_state = 1 => Only L1 and logic lost */
  168. /* save_state = 2 => Only L2 lost */
  169. /* save_state = 3 => L1, L2 and logic lost */
  170. int save_state = 0;
  171. int mpu_next_state = PWRDM_POWER_ON;
  172. int per_next_state = PWRDM_POWER_ON;
  173. int core_next_state = PWRDM_POWER_ON;
  174. int per_going_off;
  175. u32 sdrc_pwr = 0;
  176. mpu_next_state = pwrdm_read_next_pwrst(mpu_pwrdm);
  177. switch (mpu_next_state) {
  178. case PWRDM_POWER_ON:
  179. case PWRDM_POWER_RET:
  180. /* No need to save context */
  181. save_state = 0;
  182. break;
  183. case PWRDM_POWER_OFF:
  184. save_state = 3;
  185. break;
  186. default:
  187. /* Invalid state */
  188. pr_err("Invalid mpu state in sram_idle\n");
  189. return;
  190. }
  191. /* NEON control */
  192. if (pwrdm_read_pwrst(neon_pwrdm) == PWRDM_POWER_ON)
  193. pwrdm_set_next_pwrst(neon_pwrdm, mpu_next_state);
  194. /* Enable IO-PAD and IO-CHAIN wakeups */
  195. per_next_state = pwrdm_read_next_pwrst(per_pwrdm);
  196. core_next_state = pwrdm_read_next_pwrst(core_pwrdm);
  197. pwrdm_pre_transition(NULL);
  198. /* PER */
  199. if (per_next_state < PWRDM_POWER_ON) {
  200. per_going_off = (per_next_state == PWRDM_POWER_OFF) ? 1 : 0;
  201. omap2_gpio_prepare_for_idle(per_going_off);
  202. }
  203. /* CORE */
  204. if (core_next_state < PWRDM_POWER_ON) {
  205. if (core_next_state == PWRDM_POWER_OFF) {
  206. omap3_core_save_context();
  207. omap3_cm_save_context();
  208. }
  209. }
  210. /* Configure PMIC signaling for I2C4 or sys_off_mode */
  211. omap3_vc_set_pmic_signaling(core_next_state);
  212. omap3_intc_prepare_idle();
  213. /*
  214. * On EMU/HS devices ROM code restores a SRDC value
  215. * from scratchpad which has automatic self refresh on timeout
  216. * of AUTO_CNT = 1 enabled. This takes care of erratum ID i443.
  217. * Hence store/restore the SDRC_POWER register here.
  218. */
  219. if (cpu_is_omap3430() && omap_rev() >= OMAP3430_REV_ES3_0 &&
  220. (omap_type() == OMAP2_DEVICE_TYPE_EMU ||
  221. omap_type() == OMAP2_DEVICE_TYPE_SEC) &&
  222. core_next_state == PWRDM_POWER_OFF)
  223. sdrc_pwr = sdrc_read_reg(SDRC_POWER);
  224. /*
  225. * omap3_arm_context is the location where some ARM context
  226. * get saved. The rest is placed on the stack, and restored
  227. * from there before resuming.
  228. */
  229. if (save_state)
  230. omap34xx_save_context(omap3_arm_context);
  231. if (save_state == 1 || save_state == 3)
  232. cpu_suspend(save_state, omap34xx_do_sram_idle);
  233. else
  234. omap34xx_do_sram_idle(save_state);
  235. /* Restore normal SDRC POWER settings */
  236. if (cpu_is_omap3430() && omap_rev() >= OMAP3430_REV_ES3_0 &&
  237. (omap_type() == OMAP2_DEVICE_TYPE_EMU ||
  238. omap_type() == OMAP2_DEVICE_TYPE_SEC) &&
  239. core_next_state == PWRDM_POWER_OFF)
  240. sdrc_write_reg(sdrc_pwr, SDRC_POWER);
  241. /* CORE */
  242. if (core_next_state < PWRDM_POWER_ON &&
  243. pwrdm_read_prev_pwrst(core_pwrdm) == PWRDM_POWER_OFF) {
  244. omap3_core_restore_context();
  245. omap3_cm_restore_context();
  246. omap3_sram_restore_context();
  247. omap2_sms_restore_context();
  248. } else {
  249. /*
  250. * In off-mode resume path above, omap3_core_restore_context
  251. * also handles the INTC autoidle restore done here so limit
  252. * this to non-off mode resume paths so we don't do it twice.
  253. */
  254. omap3_intc_resume_idle();
  255. }
  256. pwrdm_post_transition(NULL);
  257. /* PER */
  258. if (per_next_state < PWRDM_POWER_ON)
  259. omap2_gpio_resume_after_idle();
  260. }
  261. static void omap3_pm_idle(void)
  262. {
  263. if (omap_irq_pending())
  264. return;
  265. trace_cpu_idle_rcuidle(1, smp_processor_id());
  266. omap_sram_idle();
  267. trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, smp_processor_id());
  268. }
  269. #ifdef CONFIG_SUSPEND
  270. static int omap3_pm_suspend(void)
  271. {
  272. struct power_state *pwrst;
  273. int state, ret = 0;
  274. /* Read current next_pwrsts */
  275. list_for_each_entry(pwrst, &pwrst_list, node)
  276. pwrst->saved_state = pwrdm_read_next_pwrst(pwrst->pwrdm);
  277. /* Set ones wanted by suspend */
  278. list_for_each_entry(pwrst, &pwrst_list, node) {
  279. if (omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state))
  280. goto restore;
  281. if (pwrdm_clear_all_prev_pwrst(pwrst->pwrdm))
  282. goto restore;
  283. }
  284. omap3_intc_suspend();
  285. omap_sram_idle();
  286. restore:
  287. /* Restore next_pwrsts */
  288. list_for_each_entry(pwrst, &pwrst_list, node) {
  289. state = pwrdm_read_prev_pwrst(pwrst->pwrdm);
  290. if (state > pwrst->next_state) {
  291. pr_info("Powerdomain (%s) didn't enter target state %d\n",
  292. pwrst->pwrdm->name, pwrst->next_state);
  293. ret = -1;
  294. }
  295. omap_set_pwrdm_state(pwrst->pwrdm, pwrst->saved_state);
  296. }
  297. if (ret)
  298. pr_err("Could not enter target state in pm_suspend\n");
  299. else
  300. pr_info("Successfully put all powerdomains to target state\n");
  301. return ret;
  302. }
  303. #else
  304. #define omap3_pm_suspend NULL
  305. #endif /* CONFIG_SUSPEND */
  306. static void __init prcm_setup_regs(void)
  307. {
  308. omap3_ctrl_init();
  309. omap3_prm_init_pm(cpu_is_omap3630(), omap3_has_iva());
  310. }
  311. void omap3_pm_off_mode_enable(int enable)
  312. {
  313. struct power_state *pwrst;
  314. u32 state;
  315. if (enable)
  316. state = PWRDM_POWER_OFF;
  317. else
  318. state = PWRDM_POWER_RET;
  319. list_for_each_entry(pwrst, &pwrst_list, node) {
  320. if (IS_PM34XX_ERRATUM(PM_SDRC_WAKEUP_ERRATUM_i583) &&
  321. pwrst->pwrdm == core_pwrdm &&
  322. state == PWRDM_POWER_OFF) {
  323. pwrst->next_state = PWRDM_POWER_RET;
  324. pr_warn("%s: Core OFF disabled due to errata i583\n",
  325. __func__);
  326. } else {
  327. pwrst->next_state = state;
  328. }
  329. omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
  330. }
  331. }
  332. int omap3_pm_get_suspend_state(struct powerdomain *pwrdm)
  333. {
  334. struct power_state *pwrst;
  335. list_for_each_entry(pwrst, &pwrst_list, node) {
  336. if (pwrst->pwrdm == pwrdm)
  337. return pwrst->next_state;
  338. }
  339. return -EINVAL;
  340. }
  341. int omap3_pm_set_suspend_state(struct powerdomain *pwrdm, int state)
  342. {
  343. struct power_state *pwrst;
  344. list_for_each_entry(pwrst, &pwrst_list, node) {
  345. if (pwrst->pwrdm == pwrdm) {
  346. pwrst->next_state = state;
  347. return 0;
  348. }
  349. }
  350. return -EINVAL;
  351. }
  352. static int __init pwrdms_setup(struct powerdomain *pwrdm, void *unused)
  353. {
  354. struct power_state *pwrst;
  355. if (!pwrdm->pwrsts)
  356. return 0;
  357. pwrst = kmalloc(sizeof(struct power_state), GFP_ATOMIC);
  358. if (!pwrst)
  359. return -ENOMEM;
  360. pwrst->pwrdm = pwrdm;
  361. pwrst->next_state = PWRDM_POWER_RET;
  362. list_add(&pwrst->node, &pwrst_list);
  363. if (pwrdm_has_hdwr_sar(pwrdm))
  364. pwrdm_enable_hdwr_sar(pwrdm);
  365. return omap_set_pwrdm_state(pwrst->pwrdm, pwrst->next_state);
  366. }
  367. /*
  368. * Push functions to SRAM
  369. *
  370. * The minimum set of functions is pushed to SRAM for execution:
  371. * - omap3_do_wfi for erratum i581 WA,
  372. */
  373. void omap_push_sram_idle(void)
  374. {
  375. omap3_do_wfi_sram = omap_sram_push(omap3_do_wfi, omap3_do_wfi_sz);
  376. }
  377. static void __init pm_errata_configure(void)
  378. {
  379. if (cpu_is_omap3630()) {
  380. pm34xx_errata |= PM_RTA_ERRATUM_i608;
  381. /* Enable the l2 cache toggling in sleep logic */
  382. enable_omap3630_toggle_l2_on_restore();
  383. if (omap_rev() < OMAP3630_REV_ES1_2)
  384. pm34xx_errata |= (PM_SDRC_WAKEUP_ERRATUM_i583 |
  385. PM_PER_MEMORIES_ERRATUM_i582);
  386. } else if (cpu_is_omap34xx()) {
  387. pm34xx_errata |= PM_PER_MEMORIES_ERRATUM_i582;
  388. }
  389. }
  390. int __init omap3_pm_init(void)
  391. {
  392. struct power_state *pwrst, *tmp;
  393. struct clockdomain *neon_clkdm, *mpu_clkdm, *per_clkdm, *wkup_clkdm;
  394. int ret;
  395. if (!omap3_has_io_chain_ctrl())
  396. pr_warn("PM: no software I/O chain control; some wakeups may be lost\n");
  397. pm_errata_configure();
  398. /* XXX prcm_setup_regs needs to be before enabling hw
  399. * supervised mode for powerdomains */
  400. prcm_setup_regs();
  401. ret = request_irq(omap_prcm_event_to_irq("wkup"),
  402. _prcm_int_handle_wakeup, IRQF_NO_SUSPEND, "pm_wkup", NULL);
  403. if (ret) {
  404. pr_err("pm: Failed to request pm_wkup irq\n");
  405. goto err1;
  406. }
  407. /* IO interrupt is shared with mux code */
  408. ret = request_irq(omap_prcm_event_to_irq("io"),
  409. _prcm_int_handle_io, IRQF_SHARED | IRQF_NO_SUSPEND, "pm_io",
  410. omap3_pm_init);
  411. enable_irq(omap_prcm_event_to_irq("io"));
  412. if (ret) {
  413. pr_err("pm: Failed to request pm_io irq\n");
  414. goto err2;
  415. }
  416. ret = pwrdm_for_each(pwrdms_setup, NULL);
  417. if (ret) {
  418. pr_err("Failed to setup powerdomains\n");
  419. goto err3;
  420. }
  421. (void) clkdm_for_each(omap_pm_clkdms_setup, NULL);
  422. mpu_pwrdm = pwrdm_lookup("mpu_pwrdm");
  423. if (mpu_pwrdm == NULL) {
  424. pr_err("Failed to get mpu_pwrdm\n");
  425. ret = -EINVAL;
  426. goto err3;
  427. }
  428. neon_pwrdm = pwrdm_lookup("neon_pwrdm");
  429. per_pwrdm = pwrdm_lookup("per_pwrdm");
  430. core_pwrdm = pwrdm_lookup("core_pwrdm");
  431. neon_clkdm = clkdm_lookup("neon_clkdm");
  432. mpu_clkdm = clkdm_lookup("mpu_clkdm");
  433. per_clkdm = clkdm_lookup("per_clkdm");
  434. wkup_clkdm = clkdm_lookup("wkup_clkdm");
  435. omap_common_suspend_init(omap3_pm_suspend);
  436. arm_pm_idle = omap3_pm_idle;
  437. omap3_idle_init();
  438. /*
  439. * RTA is disabled during initialization as per erratum i608
  440. * it is safer to disable RTA by the bootloader, but we would like
  441. * to be doubly sure here and prevent any mishaps.
  442. */
  443. if (IS_PM34XX_ERRATUM(PM_RTA_ERRATUM_i608))
  444. omap3630_ctrl_disable_rta();
  445. /*
  446. * The UART3/4 FIFO and the sidetone memory in McBSP2/3 are
  447. * not correctly reset when the PER powerdomain comes back
  448. * from OFF or OSWR when the CORE powerdomain is kept active.
  449. * See OMAP36xx Erratum i582 "PER Domain reset issue after
  450. * Domain-OFF/OSWR Wakeup". This wakeup dependency is not a
  451. * complete workaround. The kernel must also prevent the PER
  452. * powerdomain from going to OSWR/OFF while the CORE
  453. * powerdomain is not going to OSWR/OFF. And if PER last
  454. * power state was off while CORE last power state was ON, the
  455. * UART3/4 and McBSP2/3 SIDETONE devices need to run a
  456. * self-test using their loopback tests; if that fails, those
  457. * devices are unusable until the PER/CORE can complete a transition
  458. * from ON to OSWR/OFF and then back to ON.
  459. *
  460. * XXX Technically this workaround is only needed if off-mode
  461. * or OSWR is enabled.
  462. */
  463. if (IS_PM34XX_ERRATUM(PM_PER_MEMORIES_ERRATUM_i582))
  464. clkdm_add_wkdep(per_clkdm, wkup_clkdm);
  465. clkdm_add_wkdep(neon_clkdm, mpu_clkdm);
  466. if (omap_type() != OMAP2_DEVICE_TYPE_GP) {
  467. omap3_secure_ram_storage =
  468. kmalloc(OMAP3_SAVE_SECURE_RAM_SZ, GFP_KERNEL);
  469. if (!omap3_secure_ram_storage)
  470. pr_err("Memory allocation failed when allocating for secure sram context\n");
  471. local_irq_disable();
  472. omap_dma_global_context_save();
  473. omap3_save_secure_ram_context();
  474. omap_dma_global_context_restore();
  475. local_irq_enable();
  476. }
  477. omap3_save_scratchpad_contents();
  478. return ret;
  479. err3:
  480. list_for_each_entry_safe(pwrst, tmp, &pwrst_list, node) {
  481. list_del(&pwrst->node);
  482. kfree(pwrst);
  483. }
  484. free_irq(omap_prcm_event_to_irq("io"), omap3_pm_init);
  485. err2:
  486. free_irq(omap_prcm_event_to_irq("wkup"), NULL);
  487. err1:
  488. return ret;
  489. }