pm-sh7372.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * sh7372 Power management support
  3. *
  4. * Copyright (C) 2011 Magnus Damm
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #include <linux/pm.h>
  11. #include <linux/suspend.h>
  12. #include <linux/cpuidle.h>
  13. #include <linux/module.h>
  14. #include <linux/list.h>
  15. #include <linux/err.h>
  16. #include <linux/slab.h>
  17. #include <linux/pm_clock.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/delay.h>
  20. #include <linux/irq.h>
  21. #include <linux/bitrev.h>
  22. #include <linux/console.h>
  23. #include <asm/io.h>
  24. #include <asm/tlbflush.h>
  25. #include <asm/suspend.h>
  26. #include <mach/common.h>
  27. #include <mach/sh7372.h>
  28. #include <mach/pm-rmobile.h>
  29. /* DBG */
  30. #define DBGREG1 IOMEM(0xe6100020)
  31. #define DBGREG9 IOMEM(0xe6100040)
  32. /* CPGA */
  33. #define SYSTBCR IOMEM(0xe6150024)
  34. #define MSTPSR0 IOMEM(0xe6150030)
  35. #define MSTPSR1 IOMEM(0xe6150038)
  36. #define MSTPSR2 IOMEM(0xe6150040)
  37. #define MSTPSR3 IOMEM(0xe6150048)
  38. #define MSTPSR4 IOMEM(0xe615004c)
  39. #define PLLC01STPCR IOMEM(0xe61500c8)
  40. /* SYSC */
  41. #define SBAR IOMEM(0xe6180020)
  42. #define WUPRMSK IOMEM(0xe6180028)
  43. #define WUPSMSK IOMEM(0xe618002c)
  44. #define WUPSMSK2 IOMEM(0xe6180048)
  45. #define WUPSFAC IOMEM(0xe6180098)
  46. #define IRQCR IOMEM(0xe618022c)
  47. #define IRQCR2 IOMEM(0xe6180238)
  48. #define IRQCR3 IOMEM(0xe6180244)
  49. #define IRQCR4 IOMEM(0xe6180248)
  50. #define PDNSEL IOMEM(0xe6180254)
  51. /* INTC */
  52. #define ICR1A IOMEM(0xe6900000)
  53. #define ICR2A IOMEM(0xe6900004)
  54. #define ICR3A IOMEM(0xe6900008)
  55. #define ICR4A IOMEM(0xe690000c)
  56. #define INTMSK00A IOMEM(0xe6900040)
  57. #define INTMSK10A IOMEM(0xe6900044)
  58. #define INTMSK20A IOMEM(0xe6900048)
  59. #define INTMSK30A IOMEM(0xe690004c)
  60. /* MFIS */
  61. /* FIXME: pointing where? */
  62. #define SMFRAM 0xe6a70000
  63. /* AP-System Core */
  64. #define APARMBAREA IOMEM(0xe6f10020)
  65. #ifdef CONFIG_PM
  66. struct rmobile_pm_domain sh7372_pd_a4lc = {
  67. .genpd.name = "A4LC",
  68. .bit_shift = 1,
  69. };
  70. struct rmobile_pm_domain sh7372_pd_a4mp = {
  71. .genpd.name = "A4MP",
  72. .bit_shift = 2,
  73. };
  74. struct rmobile_pm_domain sh7372_pd_d4 = {
  75. .genpd.name = "D4",
  76. .bit_shift = 3,
  77. };
  78. static int sh7372_a4r_pd_suspend(void)
  79. {
  80. sh7372_intcs_suspend();
  81. __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
  82. return 0;
  83. }
  84. struct rmobile_pm_domain sh7372_pd_a4r = {
  85. .genpd.name = "A4R",
  86. .bit_shift = 5,
  87. .suspend = sh7372_a4r_pd_suspend,
  88. .resume = sh7372_intcs_resume,
  89. };
  90. struct rmobile_pm_domain sh7372_pd_a3rv = {
  91. .genpd.name = "A3RV",
  92. .bit_shift = 6,
  93. };
  94. struct rmobile_pm_domain sh7372_pd_a3ri = {
  95. .genpd.name = "A3RI",
  96. .bit_shift = 8,
  97. };
  98. static int sh7372_pd_a4s_suspend(void)
  99. {
  100. /*
  101. * The A4S domain contains the CPU core and therefore it should
  102. * only be turned off if the CPU is in use.
  103. */
  104. return -EBUSY;
  105. }
  106. struct rmobile_pm_domain sh7372_pd_a4s = {
  107. .genpd.name = "A4S",
  108. .bit_shift = 10,
  109. .gov = &pm_domain_always_on_gov,
  110. .no_debug = true,
  111. .suspend = sh7372_pd_a4s_suspend,
  112. };
  113. static int sh7372_a3sp_pd_suspend(void)
  114. {
  115. /*
  116. * Serial consoles make use of SCIF hardware located in A3SP,
  117. * keep such power domain on if "no_console_suspend" is set.
  118. */
  119. return console_suspend_enabled ? 0 : -EBUSY;
  120. }
  121. struct rmobile_pm_domain sh7372_pd_a3sp = {
  122. .genpd.name = "A3SP",
  123. .bit_shift = 11,
  124. .gov = &pm_domain_always_on_gov,
  125. .no_debug = true,
  126. .suspend = sh7372_a3sp_pd_suspend,
  127. };
  128. struct rmobile_pm_domain sh7372_pd_a3sg = {
  129. .genpd.name = "A3SG",
  130. .bit_shift = 13,
  131. };
  132. #endif /* CONFIG_PM */
  133. #if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
  134. static void sh7372_set_reset_vector(unsigned long address)
  135. {
  136. /* set reset vector, translate 4k */
  137. __raw_writel(address, SBAR);
  138. __raw_writel(0, APARMBAREA);
  139. }
  140. static void sh7372_enter_sysc(int pllc0_on, unsigned long sleep_mode)
  141. {
  142. if (pllc0_on)
  143. __raw_writel(0, PLLC01STPCR);
  144. else
  145. __raw_writel(1 << 28, PLLC01STPCR);
  146. __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
  147. cpu_suspend(sleep_mode, sh7372_do_idle_sysc);
  148. __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
  149. /* disable reset vector translation */
  150. __raw_writel(0, SBAR);
  151. }
  152. static int sh7372_sysc_valid(unsigned long *mskp, unsigned long *msk2p)
  153. {
  154. unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
  155. unsigned long msk, msk2;
  156. /* check active clocks to determine potential wakeup sources */
  157. mstpsr0 = __raw_readl(MSTPSR0);
  158. if ((mstpsr0 & 0x00000003) != 0x00000003) {
  159. pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
  160. return 0;
  161. }
  162. mstpsr1 = __raw_readl(MSTPSR1);
  163. if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
  164. pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
  165. return 0;
  166. }
  167. mstpsr2 = __raw_readl(MSTPSR2);
  168. if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
  169. pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
  170. return 0;
  171. }
  172. mstpsr3 = __raw_readl(MSTPSR3);
  173. if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
  174. pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
  175. return 0;
  176. }
  177. mstpsr4 = __raw_readl(MSTPSR4);
  178. if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
  179. pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
  180. return 0;
  181. }
  182. msk = 0;
  183. msk2 = 0;
  184. /* make bitmaps of limited number of wakeup sources */
  185. if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
  186. msk |= 1 << 31;
  187. if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
  188. msk |= 1 << 21;
  189. if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
  190. msk |= 1 << 2;
  191. if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
  192. msk |= 1 << 1;
  193. if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
  194. msk |= 1 << 1;
  195. if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
  196. msk |= 1 << 1;
  197. if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
  198. msk2 |= 1 << 17;
  199. *mskp = msk;
  200. *msk2p = msk2;
  201. return 1;
  202. }
  203. static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
  204. {
  205. u16 tmp, irqcr1, irqcr2;
  206. int k;
  207. irqcr1 = 0;
  208. irqcr2 = 0;
  209. /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
  210. for (k = 0; k <= 7; k++) {
  211. tmp = (icr >> ((7 - k) * 4)) & 0xf;
  212. irqcr1 |= (tmp & 0x03) << (k * 2);
  213. irqcr2 |= (tmp >> 2) << (k * 2);
  214. }
  215. *irqcr1p = irqcr1;
  216. *irqcr2p = irqcr2;
  217. }
  218. static void sh7372_setup_sysc(unsigned long msk, unsigned long msk2)
  219. {
  220. u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
  221. unsigned long tmp;
  222. /* read IRQ0A -> IRQ15A mask */
  223. tmp = bitrev8(__raw_readb(INTMSK00A));
  224. tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
  225. /* setup WUPSMSK from clocks and external IRQ mask */
  226. msk = (~msk & 0xc030000f) | (tmp << 4);
  227. __raw_writel(msk, WUPSMSK);
  228. /* propage level/edge trigger for external IRQ 0->15 */
  229. sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
  230. sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
  231. __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
  232. __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
  233. /* read IRQ16A -> IRQ31A mask */
  234. tmp = bitrev8(__raw_readb(INTMSK20A));
  235. tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
  236. /* setup WUPSMSK2 from clocks and external IRQ mask */
  237. msk2 = (~msk2 & 0x00030000) | tmp;
  238. __raw_writel(msk2, WUPSMSK2);
  239. /* propage level/edge trigger for external IRQ 16->31 */
  240. sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
  241. sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
  242. __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
  243. __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
  244. }
  245. static void sh7372_enter_a3sm_common(int pllc0_on)
  246. {
  247. /* use INTCA together with SYSC for wakeup */
  248. sh7372_setup_sysc(1 << 0, 0);
  249. sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
  250. sh7372_enter_sysc(pllc0_on, 1 << 12);
  251. }
  252. #endif /* CONFIG_SUSPEND || CONFIG_CPU_IDLE */
  253. #ifdef CONFIG_CPU_IDLE
  254. static int sh7372_do_idle_core_standby(unsigned long unused)
  255. {
  256. cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
  257. return 0;
  258. }
  259. static void sh7372_enter_core_standby(void)
  260. {
  261. sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
  262. /* enter sleep mode with SYSTBCR to 0x10 */
  263. __raw_writel(0x10, SYSTBCR);
  264. cpu_suspend(0, sh7372_do_idle_core_standby);
  265. __raw_writel(0, SYSTBCR);
  266. /* disable reset vector translation */
  267. __raw_writel(0, SBAR);
  268. }
  269. static void sh7372_enter_a3sm_pll_on(void)
  270. {
  271. sh7372_enter_a3sm_common(1);
  272. }
  273. static void sh7372_enter_a3sm_pll_off(void)
  274. {
  275. sh7372_enter_a3sm_common(0);
  276. }
  277. static void sh7372_cpuidle_setup(struct cpuidle_driver *drv)
  278. {
  279. struct cpuidle_state *state = &drv->states[drv->state_count];
  280. snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
  281. strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
  282. state->exit_latency = 10;
  283. state->target_residency = 20 + 10;
  284. state->flags = CPUIDLE_FLAG_TIME_VALID;
  285. shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_core_standby;
  286. drv->state_count++;
  287. state = &drv->states[drv->state_count];
  288. snprintf(state->name, CPUIDLE_NAME_LEN, "C3");
  289. strncpy(state->desc, "A3SM PLL ON", CPUIDLE_DESC_LEN);
  290. state->exit_latency = 20;
  291. state->target_residency = 30 + 20;
  292. state->flags = CPUIDLE_FLAG_TIME_VALID;
  293. shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_on;
  294. drv->state_count++;
  295. state = &drv->states[drv->state_count];
  296. snprintf(state->name, CPUIDLE_NAME_LEN, "C4");
  297. strncpy(state->desc, "A3SM PLL OFF", CPUIDLE_DESC_LEN);
  298. state->exit_latency = 120;
  299. state->target_residency = 30 + 120;
  300. state->flags = CPUIDLE_FLAG_TIME_VALID;
  301. shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_a3sm_pll_off;
  302. drv->state_count++;
  303. }
  304. static void sh7372_cpuidle_init(void)
  305. {
  306. shmobile_cpuidle_setup = sh7372_cpuidle_setup;
  307. }
  308. #else
  309. static void sh7372_cpuidle_init(void) {}
  310. #endif
  311. #ifdef CONFIG_SUSPEND
  312. static void sh7372_enter_a4s_common(int pllc0_on)
  313. {
  314. sh7372_intca_suspend();
  315. memcpy((void *)SMFRAM, sh7372_resume_core_standby_sysc, 0x100);
  316. sh7372_set_reset_vector(SMFRAM);
  317. sh7372_enter_sysc(pllc0_on, 1 << 10);
  318. sh7372_intca_resume();
  319. }
  320. static int sh7372_enter_suspend(suspend_state_t suspend_state)
  321. {
  322. unsigned long msk, msk2;
  323. /* check active clocks to determine potential wakeup sources */
  324. if (sh7372_sysc_valid(&msk, &msk2)) {
  325. if (!console_suspend_enabled &&
  326. sh7372_pd_a4s.genpd.status == GPD_STATE_POWER_OFF) {
  327. /* convert INTC mask/sense to SYSC mask/sense */
  328. sh7372_setup_sysc(msk, msk2);
  329. /* enter A4S sleep with PLLC0 off */
  330. pr_debug("entering A4S\n");
  331. sh7372_enter_a4s_common(0);
  332. return 0;
  333. }
  334. }
  335. /* default to enter A3SM sleep with PLLC0 off */
  336. pr_debug("entering A3SM\n");
  337. sh7372_enter_a3sm_common(0);
  338. return 0;
  339. }
  340. /**
  341. * sh7372_pm_notifier_fn - SH7372 PM notifier routine.
  342. * @notifier: Unused.
  343. * @pm_event: Event being handled.
  344. * @unused: Unused.
  345. */
  346. static int sh7372_pm_notifier_fn(struct notifier_block *notifier,
  347. unsigned long pm_event, void *unused)
  348. {
  349. switch (pm_event) {
  350. case PM_SUSPEND_PREPARE:
  351. /*
  352. * This is necessary, because the A4R domain has to be "on"
  353. * when suspend_device_irqs() and resume_device_irqs() are
  354. * executed during system suspend and resume, respectively, so
  355. * that those functions don't crash while accessing the INTCS.
  356. */
  357. pm_genpd_poweron(&sh7372_pd_a4r.genpd);
  358. break;
  359. case PM_POST_SUSPEND:
  360. pm_genpd_poweroff_unused();
  361. break;
  362. }
  363. return NOTIFY_DONE;
  364. }
  365. static void sh7372_suspend_init(void)
  366. {
  367. shmobile_suspend_ops.enter = sh7372_enter_suspend;
  368. pm_notifier(sh7372_pm_notifier_fn, 0);
  369. }
  370. #else
  371. static void sh7372_suspend_init(void) {}
  372. #endif
  373. void __init sh7372_pm_init(void)
  374. {
  375. /* enable DBG hardware block to kick SYSC */
  376. __raw_writel(0x0000a500, DBGREG9);
  377. __raw_writel(0x0000a501, DBGREG9);
  378. __raw_writel(0x00000000, DBGREG1);
  379. /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
  380. __raw_writel(0, PDNSEL);
  381. sh7372_suspend_init();
  382. sh7372_cpuidle_init();
  383. }