pm-sh7372.c 13 KB

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