pm-sh7372.c 12 KB

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