pm-sh7372.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 <asm/system.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. /* DBG */
  29. #define DBGREG1 0xe6100020
  30. #define DBGREG9 0xe6100040
  31. /* CPGA */
  32. #define SYSTBCR 0xe6150024
  33. #define MSTPSR0 0xe6150030
  34. #define MSTPSR1 0xe6150038
  35. #define MSTPSR2 0xe6150040
  36. #define MSTPSR3 0xe6150048
  37. #define MSTPSR4 0xe615004c
  38. #define PLLC01STPCR 0xe61500c8
  39. /* SYSC */
  40. #define SPDCR 0xe6180008
  41. #define SWUCR 0xe6180014
  42. #define SBAR 0xe6180020
  43. #define WUPRMSK 0xe6180028
  44. #define WUPSMSK 0xe618002c
  45. #define WUPSMSK2 0xe6180048
  46. #define PSTR 0xe6180080
  47. #define WUPSFAC 0xe6180098
  48. #define IRQCR 0xe618022c
  49. #define IRQCR2 0xe6180238
  50. #define IRQCR3 0xe6180244
  51. #define IRQCR4 0xe6180248
  52. #define PDNSEL 0xe6180254
  53. /* INTC */
  54. #define ICR1A 0xe6900000
  55. #define ICR2A 0xe6900004
  56. #define ICR3A 0xe6900008
  57. #define ICR4A 0xe690000c
  58. #define INTMSK00A 0xe6900040
  59. #define INTMSK10A 0xe6900044
  60. #define INTMSK20A 0xe6900048
  61. #define INTMSK30A 0xe690004c
  62. /* MFIS */
  63. #define SMFRAM 0xe6a70000
  64. /* AP-System Core */
  65. #define APARMBAREA 0xe6f10020
  66. #define PSTR_RETRIES 100
  67. #define PSTR_DELAY_US 10
  68. #ifdef CONFIG_PM
  69. static int pd_power_down(struct generic_pm_domain *genpd)
  70. {
  71. struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd);
  72. unsigned int mask = 1 << sh7372_pd->bit_shift;
  73. if (sh7372_pd->suspend)
  74. sh7372_pd->suspend();
  75. if (sh7372_pd->stay_on)
  76. return 0;
  77. if (__raw_readl(PSTR) & mask) {
  78. unsigned int retry_count;
  79. __raw_writel(mask, SPDCR);
  80. for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
  81. if (!(__raw_readl(SPDCR) & mask))
  82. break;
  83. cpu_relax();
  84. }
  85. }
  86. if (!sh7372_pd->no_debug)
  87. pr_debug("sh7372 power domain down 0x%08x -> PSTR = 0x%08x\n",
  88. mask, __raw_readl(PSTR));
  89. return 0;
  90. }
  91. static int pd_power_up(struct generic_pm_domain *genpd)
  92. {
  93. struct sh7372_pm_domain *sh7372_pd = to_sh7372_pd(genpd);
  94. unsigned int mask = 1 << sh7372_pd->bit_shift;
  95. unsigned int retry_count;
  96. int ret = 0;
  97. if (sh7372_pd->stay_on)
  98. goto out;
  99. if (__raw_readl(PSTR) & mask)
  100. goto out;
  101. __raw_writel(mask, SWUCR);
  102. for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
  103. if (!(__raw_readl(SWUCR) & mask))
  104. goto out;
  105. if (retry_count > PSTR_RETRIES)
  106. udelay(PSTR_DELAY_US);
  107. else
  108. cpu_relax();
  109. }
  110. if (__raw_readl(SWUCR) & mask)
  111. ret = -EIO;
  112. if (!sh7372_pd->no_debug)
  113. pr_debug("sh7372 power domain up 0x%08x -> PSTR = 0x%08x\n",
  114. mask, __raw_readl(PSTR));
  115. out:
  116. if (ret == 0 && sh7372_pd->resume)
  117. sh7372_pd->resume();
  118. return ret;
  119. }
  120. static void sh7372_a4r_suspend(void)
  121. {
  122. sh7372_intcs_suspend();
  123. __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
  124. }
  125. static bool pd_active_wakeup(struct device *dev)
  126. {
  127. return true;
  128. }
  129. static bool sh7372_power_down_forbidden(struct dev_pm_domain *domain)
  130. {
  131. return false;
  132. }
  133. struct dev_power_governor sh7372_always_on_gov = {
  134. .power_down_ok = sh7372_power_down_forbidden,
  135. };
  136. void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd)
  137. {
  138. struct generic_pm_domain *genpd = &sh7372_pd->genpd;
  139. pm_genpd_init(genpd, sh7372_pd->gov, false);
  140. genpd->stop_device = pm_clk_suspend;
  141. genpd->start_device = pm_clk_resume;
  142. genpd->dev_irq_safe = true;
  143. genpd->active_wakeup = pd_active_wakeup;
  144. genpd->power_off = pd_power_down;
  145. genpd->power_on = pd_power_up;
  146. genpd->power_on(&sh7372_pd->genpd);
  147. }
  148. void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd,
  149. struct platform_device *pdev)
  150. {
  151. struct device *dev = &pdev->dev;
  152. pm_genpd_add_device(&sh7372_pd->genpd, dev);
  153. if (pm_clk_no_clocks(dev))
  154. pm_clk_add(dev, NULL);
  155. }
  156. void sh7372_pm_add_subdomain(struct sh7372_pm_domain *sh7372_pd,
  157. struct sh7372_pm_domain *sh7372_sd)
  158. {
  159. pm_genpd_add_subdomain(&sh7372_pd->genpd, &sh7372_sd->genpd);
  160. }
  161. struct sh7372_pm_domain sh7372_a4lc = {
  162. .bit_shift = 1,
  163. };
  164. struct sh7372_pm_domain sh7372_a4mp = {
  165. .bit_shift = 2,
  166. };
  167. struct sh7372_pm_domain sh7372_d4 = {
  168. .bit_shift = 3,
  169. };
  170. struct sh7372_pm_domain sh7372_a4r = {
  171. .bit_shift = 5,
  172. .gov = &sh7372_always_on_gov,
  173. .suspend = sh7372_a4r_suspend,
  174. .resume = sh7372_intcs_resume,
  175. .stay_on = true,
  176. };
  177. struct sh7372_pm_domain sh7372_a3rv = {
  178. .bit_shift = 6,
  179. };
  180. struct sh7372_pm_domain sh7372_a3ri = {
  181. .bit_shift = 8,
  182. };
  183. struct sh7372_pm_domain sh7372_a3sp = {
  184. .bit_shift = 11,
  185. .gov = &sh7372_always_on_gov,
  186. .no_debug = true,
  187. };
  188. struct sh7372_pm_domain sh7372_a3sg = {
  189. .bit_shift = 13,
  190. };
  191. #endif /* CONFIG_PM */
  192. #if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
  193. static int sh7372_do_idle_core_standby(unsigned long unused)
  194. {
  195. cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
  196. return 0;
  197. }
  198. static void sh7372_enter_core_standby(void)
  199. {
  200. /* set reset vector, translate 4k */
  201. __raw_writel(__pa(sh7372_resume_core_standby_a3sm), SBAR);
  202. __raw_writel(0, APARMBAREA);
  203. /* enter sleep mode with SYSTBCR to 0x10 */
  204. __raw_writel(0x10, SYSTBCR);
  205. cpu_suspend(0, sh7372_do_idle_core_standby);
  206. __raw_writel(0, SYSTBCR);
  207. /* disable reset vector translation */
  208. __raw_writel(0, SBAR);
  209. }
  210. #endif
  211. #ifdef CONFIG_SUSPEND
  212. static void sh7372_enter_a3sm_common(int pllc0_on)
  213. {
  214. /* set reset vector, translate 4k */
  215. __raw_writel(__pa(sh7372_resume_core_standby_a3sm), SBAR);
  216. __raw_writel(0, APARMBAREA);
  217. if (pllc0_on)
  218. __raw_writel(0, PLLC01STPCR);
  219. else
  220. __raw_writel(1 << 28, PLLC01STPCR);
  221. __raw_writel(0, PDNSEL); /* power-down A3SM only, not A4S */
  222. __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
  223. cpu_suspend(0, sh7372_do_idle_a3sm);
  224. __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
  225. /* disable reset vector translation */
  226. __raw_writel(0, SBAR);
  227. }
  228. static int sh7372_a3sm_valid(unsigned long *mskp, unsigned long *msk2p)
  229. {
  230. unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
  231. unsigned long msk, msk2;
  232. /* check active clocks to determine potential wakeup sources */
  233. mstpsr0 = __raw_readl(MSTPSR0);
  234. if ((mstpsr0 & 0x00000003) != 0x00000003) {
  235. pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
  236. return 0;
  237. }
  238. mstpsr1 = __raw_readl(MSTPSR1);
  239. if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
  240. pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
  241. return 0;
  242. }
  243. mstpsr2 = __raw_readl(MSTPSR2);
  244. if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
  245. pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
  246. return 0;
  247. }
  248. mstpsr3 = __raw_readl(MSTPSR3);
  249. if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
  250. pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
  251. return 0;
  252. }
  253. mstpsr4 = __raw_readl(MSTPSR4);
  254. if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
  255. pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
  256. return 0;
  257. }
  258. msk = 0;
  259. msk2 = 0;
  260. /* make bitmaps of limited number of wakeup sources */
  261. if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
  262. msk |= 1 << 31;
  263. if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
  264. msk |= 1 << 21;
  265. if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
  266. msk |= 1 << 2;
  267. if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
  268. msk |= 1 << 1;
  269. if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
  270. msk |= 1 << 1;
  271. if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
  272. msk |= 1 << 1;
  273. if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
  274. msk2 |= 1 << 17;
  275. *mskp = msk;
  276. *msk2p = msk2;
  277. return 1;
  278. }
  279. static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
  280. {
  281. u16 tmp, irqcr1, irqcr2;
  282. int k;
  283. irqcr1 = 0;
  284. irqcr2 = 0;
  285. /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
  286. for (k = 0; k <= 7; k++) {
  287. tmp = (icr >> ((7 - k) * 4)) & 0xf;
  288. irqcr1 |= (tmp & 0x03) << (k * 2);
  289. irqcr2 |= (tmp >> 2) << (k * 2);
  290. }
  291. *irqcr1p = irqcr1;
  292. *irqcr2p = irqcr2;
  293. }
  294. static void sh7372_setup_a3sm(unsigned long msk, unsigned long msk2)
  295. {
  296. u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
  297. unsigned long tmp;
  298. /* read IRQ0A -> IRQ15A mask */
  299. tmp = bitrev8(__raw_readb(INTMSK00A));
  300. tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
  301. /* setup WUPSMSK from clocks and external IRQ mask */
  302. msk = (~msk & 0xc030000f) | (tmp << 4);
  303. __raw_writel(msk, WUPSMSK);
  304. /* propage level/edge trigger for external IRQ 0->15 */
  305. sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
  306. sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
  307. __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
  308. __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
  309. /* read IRQ16A -> IRQ31A mask */
  310. tmp = bitrev8(__raw_readb(INTMSK20A));
  311. tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
  312. /* setup WUPSMSK2 from clocks and external IRQ mask */
  313. msk2 = (~msk2 & 0x00030000) | tmp;
  314. __raw_writel(msk2, WUPSMSK2);
  315. /* propage level/edge trigger for external IRQ 16->31 */
  316. sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
  317. sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
  318. __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
  319. __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
  320. }
  321. #endif
  322. #ifdef CONFIG_CPU_IDLE
  323. static void sh7372_cpuidle_setup(struct cpuidle_device *dev)
  324. {
  325. struct cpuidle_state *state;
  326. int i = dev->state_count;
  327. state = &dev->states[i];
  328. snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
  329. strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
  330. state->exit_latency = 10;
  331. state->target_residency = 20 + 10;
  332. state->power_usage = 1; /* perhaps not */
  333. state->flags = 0;
  334. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  335. shmobile_cpuidle_modes[i] = sh7372_enter_core_standby;
  336. dev->state_count = i + 1;
  337. }
  338. static void sh7372_cpuidle_init(void)
  339. {
  340. shmobile_cpuidle_setup = sh7372_cpuidle_setup;
  341. }
  342. #else
  343. static void sh7372_cpuidle_init(void) {}
  344. #endif
  345. #ifdef CONFIG_SUSPEND
  346. static int sh7372_enter_suspend(suspend_state_t suspend_state)
  347. {
  348. unsigned long msk, msk2;
  349. /* check active clocks to determine potential wakeup sources */
  350. if (sh7372_a3sm_valid(&msk, &msk2)) {
  351. /* convert INTC mask and sense to SYSC mask and sense */
  352. sh7372_setup_a3sm(msk, msk2);
  353. /* enter A3SM sleep with PLLC0 off */
  354. pr_debug("entering A3SM\n");
  355. sh7372_enter_a3sm_common(0);
  356. } else {
  357. /* default to Core Standby that supports all wakeup sources */
  358. pr_debug("entering Core Standby\n");
  359. sh7372_enter_core_standby();
  360. }
  361. return 0;
  362. }
  363. static void sh7372_suspend_init(void)
  364. {
  365. shmobile_suspend_ops.enter = sh7372_enter_suspend;
  366. }
  367. #else
  368. static void sh7372_suspend_init(void) {}
  369. #endif
  370. void __init sh7372_pm_init(void)
  371. {
  372. /* enable DBG hardware block to kick SYSC */
  373. __raw_writel(0x0000a500, DBGREG9);
  374. __raw_writel(0x0000a501, DBGREG9);
  375. __raw_writel(0x00000000, DBGREG1);
  376. /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
  377. __raw_writel(0, PDNSEL);
  378. sh7372_suspend_init();
  379. sh7372_cpuidle_init();
  380. }