pm-sh7372.c 14 KB

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