pm-sh7372.c 11 KB

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