pm-sh7372.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. int ret = sh7372_pd->suspend();
  76. if (ret)
  77. return ret;
  78. }
  79. if (__raw_readl(PSTR) & mask) {
  80. unsigned int retry_count;
  81. __raw_writel(mask, SPDCR);
  82. for (retry_count = PSTR_RETRIES; retry_count; retry_count--) {
  83. if (!(__raw_readl(SPDCR) & mask))
  84. break;
  85. cpu_relax();
  86. }
  87. }
  88. if (!sh7372_pd->no_debug)
  89. pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n",
  90. genpd->name, mask, __raw_readl(PSTR));
  91. return 0;
  92. }
  93. static int __pd_power_up(struct sh7372_pm_domain *sh7372_pd, bool do_resume)
  94. {
  95. unsigned int mask = 1 << sh7372_pd->bit_shift;
  96. unsigned int retry_count;
  97. int ret = 0;
  98. if (__raw_readl(PSTR) & mask)
  99. goto out;
  100. __raw_writel(mask, SWUCR);
  101. for (retry_count = 2 * PSTR_RETRIES; retry_count; retry_count--) {
  102. if (!(__raw_readl(SWUCR) & mask))
  103. break;
  104. if (retry_count > PSTR_RETRIES)
  105. udelay(PSTR_DELAY_US);
  106. else
  107. cpu_relax();
  108. }
  109. if (!retry_count)
  110. ret = -EIO;
  111. if (!sh7372_pd->no_debug)
  112. pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
  113. sh7372_pd->genpd.name, mask, __raw_readl(PSTR));
  114. out:
  115. if (ret == 0 && sh7372_pd->resume && do_resume)
  116. sh7372_pd->resume();
  117. return ret;
  118. }
  119. static int pd_power_up(struct generic_pm_domain *genpd)
  120. {
  121. return __pd_power_up(to_sh7372_pd(genpd), true);
  122. }
  123. static int sh7372_a4r_suspend(void)
  124. {
  125. sh7372_intcs_suspend();
  126. __raw_writel(0x300fffff, WUPRMSK); /* avoid wakeup */
  127. return 0;
  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 int sh7372_stop_dev(struct device *dev)
  136. {
  137. int (*stop)(struct device *dev);
  138. stop = dev_gpd_data(dev)->ops.stop;
  139. if (stop) {
  140. int ret = stop(dev);
  141. if (ret)
  142. return ret;
  143. }
  144. return pm_clk_suspend(dev);
  145. }
  146. static int sh7372_start_dev(struct device *dev)
  147. {
  148. int (*start)(struct device *dev);
  149. int ret;
  150. ret = pm_clk_resume(dev);
  151. if (ret)
  152. return ret;
  153. start = dev_gpd_data(dev)->ops.start;
  154. if (start)
  155. ret = start(dev);
  156. return ret;
  157. }
  158. void sh7372_init_pm_domain(struct sh7372_pm_domain *sh7372_pd)
  159. {
  160. struct generic_pm_domain *genpd = &sh7372_pd->genpd;
  161. struct dev_power_governor *gov = sh7372_pd->gov;
  162. pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
  163. genpd->dev_ops.stop = sh7372_stop_dev;
  164. genpd->dev_ops.start = sh7372_start_dev;
  165. genpd->dev_ops.active_wakeup = pd_active_wakeup;
  166. genpd->dev_irq_safe = true;
  167. genpd->power_off = pd_power_down;
  168. genpd->power_on = pd_power_up;
  169. __pd_power_up(sh7372_pd, false);
  170. }
  171. void sh7372_add_device_to_domain(struct sh7372_pm_domain *sh7372_pd,
  172. struct platform_device *pdev)
  173. {
  174. struct device *dev = &pdev->dev;
  175. pm_genpd_add_device(&sh7372_pd->genpd, dev);
  176. if (pm_clk_no_clocks(dev))
  177. pm_clk_add(dev, NULL);
  178. }
  179. void sh7372_pm_add_subdomain(struct sh7372_pm_domain *sh7372_pd,
  180. struct sh7372_pm_domain *sh7372_sd)
  181. {
  182. pm_genpd_add_subdomain(&sh7372_pd->genpd, &sh7372_sd->genpd);
  183. }
  184. struct sh7372_pm_domain sh7372_a4lc = {
  185. .genpd.name = "A4LC",
  186. .bit_shift = 1,
  187. };
  188. struct sh7372_pm_domain sh7372_a4mp = {
  189. .genpd.name = "A4MP",
  190. .bit_shift = 2,
  191. };
  192. struct sh7372_pm_domain sh7372_d4 = {
  193. .genpd.name = "D4",
  194. .bit_shift = 3,
  195. };
  196. struct sh7372_pm_domain sh7372_a4r = {
  197. .genpd.name = "A4R",
  198. .bit_shift = 5,
  199. .suspend = sh7372_a4r_suspend,
  200. .resume = sh7372_intcs_resume,
  201. };
  202. struct sh7372_pm_domain sh7372_a3rv = {
  203. .genpd.name = "A3RV",
  204. .bit_shift = 6,
  205. };
  206. struct sh7372_pm_domain sh7372_a3ri = {
  207. .genpd.name = "A3RI",
  208. .bit_shift = 8,
  209. };
  210. static int sh7372_a4s_suspend(void)
  211. {
  212. /*
  213. * The A4S domain contains the CPU core and therefore it should
  214. * only be turned off if the CPU is in use.
  215. */
  216. return -EBUSY;
  217. }
  218. struct sh7372_pm_domain sh7372_a4s = {
  219. .genpd.name = "A4S",
  220. .bit_shift = 10,
  221. .gov = &pm_domain_always_on_gov,
  222. .no_debug = true,
  223. .suspend = sh7372_a4s_suspend,
  224. };
  225. static int sh7372_a3sp_suspend(void)
  226. {
  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. return console_suspend_enabled ? 0 : -EBUSY;
  232. }
  233. struct sh7372_pm_domain sh7372_a3sp = {
  234. .genpd.name = "A3SP",
  235. .bit_shift = 11,
  236. .gov = &pm_domain_always_on_gov,
  237. .no_debug = true,
  238. .suspend = sh7372_a3sp_suspend,
  239. };
  240. struct sh7372_pm_domain sh7372_a3sg = {
  241. .genpd.name = "A3SG",
  242. .bit_shift = 13,
  243. };
  244. #else /* !CONFIG_PM */
  245. static inline void sh7372_a3sp_init(void) {}
  246. #endif /* !CONFIG_PM */
  247. #if defined(CONFIG_SUSPEND) || defined(CONFIG_CPU_IDLE)
  248. static int sh7372_do_idle_core_standby(unsigned long unused)
  249. {
  250. cpu_do_idle(); /* WFI when SYSTBCR == 0x10 -> Core Standby */
  251. return 0;
  252. }
  253. static void sh7372_set_reset_vector(unsigned long address)
  254. {
  255. /* set reset vector, translate 4k */
  256. __raw_writel(address, SBAR);
  257. __raw_writel(0, APARMBAREA);
  258. }
  259. static void sh7372_enter_core_standby(void)
  260. {
  261. sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
  262. /* enter sleep mode with SYSTBCR to 0x10 */
  263. __raw_writel(0x10, SYSTBCR);
  264. cpu_suspend(0, sh7372_do_idle_core_standby);
  265. __raw_writel(0, SYSTBCR);
  266. /* disable reset vector translation */
  267. __raw_writel(0, SBAR);
  268. }
  269. #endif
  270. #ifdef CONFIG_SUSPEND
  271. static void sh7372_enter_sysc(int pllc0_on, unsigned long sleep_mode)
  272. {
  273. if (pllc0_on)
  274. __raw_writel(0, PLLC01STPCR);
  275. else
  276. __raw_writel(1 << 28, PLLC01STPCR);
  277. __raw_readl(WUPSFAC); /* read wakeup int. factor before sleep */
  278. cpu_suspend(sleep_mode, sh7372_do_idle_sysc);
  279. __raw_readl(WUPSFAC); /* read wakeup int. factor after wakeup */
  280. /* disable reset vector translation */
  281. __raw_writel(0, SBAR);
  282. }
  283. static int sh7372_sysc_valid(unsigned long *mskp, unsigned long *msk2p)
  284. {
  285. unsigned long mstpsr0, mstpsr1, mstpsr2, mstpsr3, mstpsr4;
  286. unsigned long msk, msk2;
  287. /* check active clocks to determine potential wakeup sources */
  288. mstpsr0 = __raw_readl(MSTPSR0);
  289. if ((mstpsr0 & 0x00000003) != 0x00000003) {
  290. pr_debug("sh7372 mstpsr0 0x%08lx\n", mstpsr0);
  291. return 0;
  292. }
  293. mstpsr1 = __raw_readl(MSTPSR1);
  294. if ((mstpsr1 & 0xff079b7f) != 0xff079b7f) {
  295. pr_debug("sh7372 mstpsr1 0x%08lx\n", mstpsr1);
  296. return 0;
  297. }
  298. mstpsr2 = __raw_readl(MSTPSR2);
  299. if ((mstpsr2 & 0x000741ff) != 0x000741ff) {
  300. pr_debug("sh7372 mstpsr2 0x%08lx\n", mstpsr2);
  301. return 0;
  302. }
  303. mstpsr3 = __raw_readl(MSTPSR3);
  304. if ((mstpsr3 & 0x1a60f010) != 0x1a60f010) {
  305. pr_debug("sh7372 mstpsr3 0x%08lx\n", mstpsr3);
  306. return 0;
  307. }
  308. mstpsr4 = __raw_readl(MSTPSR4);
  309. if ((mstpsr4 & 0x00008cf0) != 0x00008cf0) {
  310. pr_debug("sh7372 mstpsr4 0x%08lx\n", mstpsr4);
  311. return 0;
  312. }
  313. msk = 0;
  314. msk2 = 0;
  315. /* make bitmaps of limited number of wakeup sources */
  316. if ((mstpsr2 & (1 << 23)) == 0) /* SPU2 */
  317. msk |= 1 << 31;
  318. if ((mstpsr2 & (1 << 12)) == 0) /* MFI_MFIM */
  319. msk |= 1 << 21;
  320. if ((mstpsr4 & (1 << 3)) == 0) /* KEYSC */
  321. msk |= 1 << 2;
  322. if ((mstpsr1 & (1 << 24)) == 0) /* CMT0 */
  323. msk |= 1 << 1;
  324. if ((mstpsr3 & (1 << 29)) == 0) /* CMT1 */
  325. msk |= 1 << 1;
  326. if ((mstpsr4 & (1 << 0)) == 0) /* CMT2 */
  327. msk |= 1 << 1;
  328. if ((mstpsr2 & (1 << 13)) == 0) /* MFI_MFIS */
  329. msk2 |= 1 << 17;
  330. *mskp = msk;
  331. *msk2p = msk2;
  332. return 1;
  333. }
  334. static void sh7372_icr_to_irqcr(unsigned long icr, u16 *irqcr1p, u16 *irqcr2p)
  335. {
  336. u16 tmp, irqcr1, irqcr2;
  337. int k;
  338. irqcr1 = 0;
  339. irqcr2 = 0;
  340. /* convert INTCA ICR register layout to SYSC IRQCR+IRQCR2 */
  341. for (k = 0; k <= 7; k++) {
  342. tmp = (icr >> ((7 - k) * 4)) & 0xf;
  343. irqcr1 |= (tmp & 0x03) << (k * 2);
  344. irqcr2 |= (tmp >> 2) << (k * 2);
  345. }
  346. *irqcr1p = irqcr1;
  347. *irqcr2p = irqcr2;
  348. }
  349. static void sh7372_setup_sysc(unsigned long msk, unsigned long msk2)
  350. {
  351. u16 irqcrx_low, irqcrx_high, irqcry_low, irqcry_high;
  352. unsigned long tmp;
  353. /* read IRQ0A -> IRQ15A mask */
  354. tmp = bitrev8(__raw_readb(INTMSK00A));
  355. tmp |= bitrev8(__raw_readb(INTMSK10A)) << 8;
  356. /* setup WUPSMSK from clocks and external IRQ mask */
  357. msk = (~msk & 0xc030000f) | (tmp << 4);
  358. __raw_writel(msk, WUPSMSK);
  359. /* propage level/edge trigger for external IRQ 0->15 */
  360. sh7372_icr_to_irqcr(__raw_readl(ICR1A), &irqcrx_low, &irqcry_low);
  361. sh7372_icr_to_irqcr(__raw_readl(ICR2A), &irqcrx_high, &irqcry_high);
  362. __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR);
  363. __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR2);
  364. /* read IRQ16A -> IRQ31A mask */
  365. tmp = bitrev8(__raw_readb(INTMSK20A));
  366. tmp |= bitrev8(__raw_readb(INTMSK30A)) << 8;
  367. /* setup WUPSMSK2 from clocks and external IRQ mask */
  368. msk2 = (~msk2 & 0x00030000) | tmp;
  369. __raw_writel(msk2, WUPSMSK2);
  370. /* propage level/edge trigger for external IRQ 16->31 */
  371. sh7372_icr_to_irqcr(__raw_readl(ICR3A), &irqcrx_low, &irqcry_low);
  372. sh7372_icr_to_irqcr(__raw_readl(ICR4A), &irqcrx_high, &irqcry_high);
  373. __raw_writel((irqcrx_high << 16) | irqcrx_low, IRQCR3);
  374. __raw_writel((irqcry_high << 16) | irqcry_low, IRQCR4);
  375. }
  376. static void sh7372_enter_a3sm_common(int pllc0_on)
  377. {
  378. sh7372_set_reset_vector(__pa(sh7372_resume_core_standby_sysc));
  379. sh7372_enter_sysc(pllc0_on, 1 << 12);
  380. }
  381. static void sh7372_enter_a4s_common(int pllc0_on)
  382. {
  383. sh7372_intca_suspend();
  384. memcpy((void *)SMFRAM, sh7372_resume_core_standby_sysc, 0x100);
  385. sh7372_set_reset_vector(SMFRAM);
  386. sh7372_enter_sysc(pllc0_on, 1 << 10);
  387. sh7372_intca_resume();
  388. }
  389. #endif
  390. #ifdef CONFIG_CPU_IDLE
  391. static void sh7372_cpuidle_setup(struct cpuidle_driver *drv)
  392. {
  393. struct cpuidle_state *state = &drv->states[drv->state_count];
  394. snprintf(state->name, CPUIDLE_NAME_LEN, "C2");
  395. strncpy(state->desc, "Core Standby Mode", CPUIDLE_DESC_LEN);
  396. state->exit_latency = 10;
  397. state->target_residency = 20 + 10;
  398. state->flags = CPUIDLE_FLAG_TIME_VALID;
  399. shmobile_cpuidle_modes[drv->state_count] = sh7372_enter_core_standby;
  400. drv->state_count++;
  401. }
  402. static void sh7372_cpuidle_init(void)
  403. {
  404. shmobile_cpuidle_setup = sh7372_cpuidle_setup;
  405. }
  406. #else
  407. static void sh7372_cpuidle_init(void) {}
  408. #endif
  409. #ifdef CONFIG_SUSPEND
  410. static int sh7372_enter_suspend(suspend_state_t suspend_state)
  411. {
  412. unsigned long msk, msk2;
  413. /* check active clocks to determine potential wakeup sources */
  414. if (sh7372_sysc_valid(&msk, &msk2)) {
  415. /* convert INTC mask and sense to SYSC mask and sense */
  416. sh7372_setup_sysc(msk, msk2);
  417. if (!console_suspend_enabled &&
  418. sh7372_a4s.genpd.status == GPD_STATE_POWER_OFF) {
  419. /* enter A4S sleep with PLLC0 off */
  420. pr_debug("entering A4S\n");
  421. sh7372_enter_a4s_common(0);
  422. } else {
  423. /* enter A3SM sleep with PLLC0 off */
  424. pr_debug("entering A3SM\n");
  425. sh7372_enter_a3sm_common(0);
  426. }
  427. } else {
  428. /* default to Core Standby that supports all wakeup sources */
  429. pr_debug("entering Core Standby\n");
  430. sh7372_enter_core_standby();
  431. }
  432. return 0;
  433. }
  434. /**
  435. * sh7372_pm_notifier_fn - SH7372 PM notifier routine.
  436. * @notifier: Unused.
  437. * @pm_event: Event being handled.
  438. * @unused: Unused.
  439. */
  440. static int sh7372_pm_notifier_fn(struct notifier_block *notifier,
  441. unsigned long pm_event, void *unused)
  442. {
  443. switch (pm_event) {
  444. case PM_SUSPEND_PREPARE:
  445. /*
  446. * This is necessary, because the A4R domain has to be "on"
  447. * when suspend_device_irqs() and resume_device_irqs() are
  448. * executed during system suspend and resume, respectively, so
  449. * that those functions don't crash while accessing the INTCS.
  450. */
  451. pm_genpd_poweron(&sh7372_a4r.genpd);
  452. break;
  453. case PM_POST_SUSPEND:
  454. pm_genpd_poweroff_unused();
  455. break;
  456. }
  457. return NOTIFY_DONE;
  458. }
  459. static void sh7372_suspend_init(void)
  460. {
  461. shmobile_suspend_ops.enter = sh7372_enter_suspend;
  462. pm_notifier(sh7372_pm_notifier_fn, 0);
  463. }
  464. #else
  465. static void sh7372_suspend_init(void) {}
  466. #endif
  467. void __init sh7372_pm_init(void)
  468. {
  469. /* enable DBG hardware block to kick SYSC */
  470. __raw_writel(0x0000a500, DBGREG9);
  471. __raw_writel(0x0000a501, DBGREG9);
  472. __raw_writel(0x00000000, DBGREG1);
  473. /* do not convert A3SM, A3SP, A3SG, A4R power down into A4S */
  474. __raw_writel(0, PDNSEL);
  475. sh7372_suspend_init();
  476. sh7372_cpuidle_init();
  477. }