pm-sh7372.c 12 KB

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