suspend.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * MPC83xx suspend support
  4. *
  5. * Author: Scott Wood <[email protected]>
  6. *
  7. * Copyright (c) 2006-2007 Freescale Semiconductor, Inc.
  8. */
  9. #include <linux/pm.h>
  10. #include <linux/types.h>
  11. #include <linux/ioport.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/wait.h>
  14. #include <linux/sched/signal.h>
  15. #include <linux/kthread.h>
  16. #include <linux/freezer.h>
  17. #include <linux/suspend.h>
  18. #include <linux/fsl_devices.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of_irq.h>
  21. #include <linux/of_platform.h>
  22. #include <linux/export.h>
  23. #include <asm/reg.h>
  24. #include <asm/io.h>
  25. #include <asm/time.h>
  26. #include <asm/mpc6xx.h>
  27. #include <asm/switch_to.h>
  28. #include <sysdev/fsl_soc.h>
  29. #define PMCCR1_NEXT_STATE 0x0C /* Next state for power management */
  30. #define PMCCR1_NEXT_STATE_SHIFT 2
  31. #define PMCCR1_CURR_STATE 0x03 /* Current state for power management*/
  32. #define IMMR_SYSCR_OFFSET 0x100
  33. #define IMMR_RCW_OFFSET 0x900
  34. #define RCW_PCI_HOST 0x80000000
  35. void mpc83xx_enter_deep_sleep(phys_addr_t immrbase);
  36. struct mpc83xx_pmc {
  37. u32 config;
  38. #define PMCCR_DLPEN 2 /* DDR SDRAM low power enable */
  39. #define PMCCR_SLPEN 1 /* System low power enable */
  40. u32 event;
  41. u32 mask;
  42. /* All but PMCI are deep-sleep only */
  43. #define PMCER_GPIO 0x100
  44. #define PMCER_PCI 0x080
  45. #define PMCER_USB 0x040
  46. #define PMCER_ETSEC1 0x020
  47. #define PMCER_ETSEC2 0x010
  48. #define PMCER_TIMER 0x008
  49. #define PMCER_INT1 0x004
  50. #define PMCER_INT2 0x002
  51. #define PMCER_PMCI 0x001
  52. #define PMCER_ALL 0x1FF
  53. /* deep-sleep only */
  54. u32 config1;
  55. #define PMCCR1_USE_STATE 0x80000000
  56. #define PMCCR1_PME_EN 0x00000080
  57. #define PMCCR1_ASSERT_PME 0x00000040
  58. #define PMCCR1_POWER_OFF 0x00000020
  59. /* deep-sleep only */
  60. u32 config2;
  61. };
  62. struct mpc83xx_rcw {
  63. u32 rcwlr;
  64. u32 rcwhr;
  65. };
  66. struct mpc83xx_clock {
  67. u32 spmr;
  68. u32 occr;
  69. u32 sccr;
  70. };
  71. struct mpc83xx_syscr {
  72. __be32 sgprl;
  73. __be32 sgprh;
  74. __be32 spridr;
  75. __be32 :32;
  76. __be32 spcr;
  77. __be32 sicrl;
  78. __be32 sicrh;
  79. };
  80. struct mpc83xx_saved {
  81. u32 sicrl;
  82. u32 sicrh;
  83. u32 sccr;
  84. };
  85. struct pmc_type {
  86. int has_deep_sleep;
  87. };
  88. static int has_deep_sleep, deep_sleeping;
  89. static int pmc_irq;
  90. static struct mpc83xx_pmc __iomem *pmc_regs;
  91. static struct mpc83xx_clock __iomem *clock_regs;
  92. static struct mpc83xx_syscr __iomem *syscr_regs;
  93. static struct mpc83xx_saved saved_regs;
  94. static int is_pci_agent, wake_from_pci;
  95. static phys_addr_t immrbase;
  96. static int pci_pm_state;
  97. static DECLARE_WAIT_QUEUE_HEAD(agent_wq);
  98. int fsl_deep_sleep(void)
  99. {
  100. return deep_sleeping;
  101. }
  102. EXPORT_SYMBOL(fsl_deep_sleep);
  103. static int mpc83xx_change_state(void)
  104. {
  105. u32 curr_state;
  106. u32 reg_cfg1 = in_be32(&pmc_regs->config1);
  107. if (is_pci_agent) {
  108. pci_pm_state = (reg_cfg1 & PMCCR1_NEXT_STATE) >>
  109. PMCCR1_NEXT_STATE_SHIFT;
  110. curr_state = reg_cfg1 & PMCCR1_CURR_STATE;
  111. if (curr_state != pci_pm_state) {
  112. reg_cfg1 &= ~PMCCR1_CURR_STATE;
  113. reg_cfg1 |= pci_pm_state;
  114. out_be32(&pmc_regs->config1, reg_cfg1);
  115. wake_up(&agent_wq);
  116. return 1;
  117. }
  118. }
  119. return 0;
  120. }
  121. static irqreturn_t pmc_irq_handler(int irq, void *dev_id)
  122. {
  123. u32 event = in_be32(&pmc_regs->event);
  124. int ret = IRQ_NONE;
  125. if (mpc83xx_change_state())
  126. ret = IRQ_HANDLED;
  127. if (event) {
  128. out_be32(&pmc_regs->event, event);
  129. ret = IRQ_HANDLED;
  130. }
  131. return ret;
  132. }
  133. static void mpc83xx_suspend_restore_regs(void)
  134. {
  135. out_be32(&syscr_regs->sicrl, saved_regs.sicrl);
  136. out_be32(&syscr_regs->sicrh, saved_regs.sicrh);
  137. out_be32(&clock_regs->sccr, saved_regs.sccr);
  138. }
  139. static void mpc83xx_suspend_save_regs(void)
  140. {
  141. saved_regs.sicrl = in_be32(&syscr_regs->sicrl);
  142. saved_regs.sicrh = in_be32(&syscr_regs->sicrh);
  143. saved_regs.sccr = in_be32(&clock_regs->sccr);
  144. }
  145. static int mpc83xx_suspend_enter(suspend_state_t state)
  146. {
  147. int ret = -EAGAIN;
  148. /* Don't go to sleep if there's a race where pci_pm_state changes
  149. * between the agent thread checking it and the PM code disabling
  150. * interrupts.
  151. */
  152. if (wake_from_pci) {
  153. if (pci_pm_state != (deep_sleeping ? 3 : 2))
  154. goto out;
  155. out_be32(&pmc_regs->config1,
  156. in_be32(&pmc_regs->config1) | PMCCR1_PME_EN);
  157. }
  158. /* Put the system into low-power mode and the RAM
  159. * into self-refresh mode once the core goes to
  160. * sleep.
  161. */
  162. out_be32(&pmc_regs->config, PMCCR_SLPEN | PMCCR_DLPEN);
  163. /* If it has deep sleep (i.e. it's an 831x or compatible),
  164. * disable power to the core upon entering sleep mode. This will
  165. * require going through the boot firmware upon a wakeup event.
  166. */
  167. if (deep_sleeping) {
  168. mpc83xx_suspend_save_regs();
  169. out_be32(&pmc_regs->mask, PMCER_ALL);
  170. out_be32(&pmc_regs->config1,
  171. in_be32(&pmc_regs->config1) | PMCCR1_POWER_OFF);
  172. enable_kernel_fp();
  173. mpc83xx_enter_deep_sleep(immrbase);
  174. out_be32(&pmc_regs->config1,
  175. in_be32(&pmc_regs->config1) & ~PMCCR1_POWER_OFF);
  176. out_be32(&pmc_regs->mask, PMCER_PMCI);
  177. mpc83xx_suspend_restore_regs();
  178. } else {
  179. out_be32(&pmc_regs->mask, PMCER_PMCI);
  180. mpc6xx_enter_standby();
  181. }
  182. ret = 0;
  183. out:
  184. out_be32(&pmc_regs->config1,
  185. in_be32(&pmc_regs->config1) & ~PMCCR1_PME_EN);
  186. return ret;
  187. }
  188. static void mpc83xx_suspend_end(void)
  189. {
  190. deep_sleeping = 0;
  191. }
  192. static int mpc83xx_suspend_valid(suspend_state_t state)
  193. {
  194. return state == PM_SUSPEND_STANDBY || state == PM_SUSPEND_MEM;
  195. }
  196. static int mpc83xx_suspend_begin(suspend_state_t state)
  197. {
  198. switch (state) {
  199. case PM_SUSPEND_STANDBY:
  200. deep_sleeping = 0;
  201. return 0;
  202. case PM_SUSPEND_MEM:
  203. if (has_deep_sleep)
  204. deep_sleeping = 1;
  205. return 0;
  206. default:
  207. return -EINVAL;
  208. }
  209. }
  210. static int agent_thread_fn(void *data)
  211. {
  212. while (1) {
  213. wait_event_interruptible(agent_wq, pci_pm_state >= 2);
  214. try_to_freeze();
  215. if (signal_pending(current) || pci_pm_state < 2)
  216. continue;
  217. /* With a preemptible kernel (or SMP), this could race with
  218. * a userspace-driven suspend request. It's probably best
  219. * to avoid mixing the two with such a configuration (or
  220. * else fix it by adding a mutex to state_store that we can
  221. * synchronize with).
  222. */
  223. wake_from_pci = 1;
  224. pm_suspend(pci_pm_state == 3 ? PM_SUSPEND_MEM :
  225. PM_SUSPEND_STANDBY);
  226. wake_from_pci = 0;
  227. }
  228. return 0;
  229. }
  230. static void mpc83xx_set_agent(void)
  231. {
  232. out_be32(&pmc_regs->config1, PMCCR1_USE_STATE);
  233. out_be32(&pmc_regs->mask, PMCER_PMCI);
  234. kthread_run(agent_thread_fn, NULL, "PCI power mgt");
  235. }
  236. static int mpc83xx_is_pci_agent(void)
  237. {
  238. struct mpc83xx_rcw __iomem *rcw_regs;
  239. int ret;
  240. rcw_regs = ioremap(get_immrbase() + IMMR_RCW_OFFSET,
  241. sizeof(struct mpc83xx_rcw));
  242. if (!rcw_regs)
  243. return -ENOMEM;
  244. ret = !(in_be32(&rcw_regs->rcwhr) & RCW_PCI_HOST);
  245. iounmap(rcw_regs);
  246. return ret;
  247. }
  248. static const struct platform_suspend_ops mpc83xx_suspend_ops = {
  249. .valid = mpc83xx_suspend_valid,
  250. .begin = mpc83xx_suspend_begin,
  251. .enter = mpc83xx_suspend_enter,
  252. .end = mpc83xx_suspend_end,
  253. };
  254. static struct pmc_type pmc_types[] = {
  255. {
  256. .has_deep_sleep = 1,
  257. },
  258. {
  259. .has_deep_sleep = 0,
  260. }
  261. };
  262. static const struct of_device_id pmc_match[] = {
  263. {
  264. .compatible = "fsl,mpc8313-pmc",
  265. .data = &pmc_types[0],
  266. },
  267. {
  268. .compatible = "fsl,mpc8349-pmc",
  269. .data = &pmc_types[1],
  270. },
  271. {}
  272. };
  273. static int pmc_probe(struct platform_device *ofdev)
  274. {
  275. struct device_node *np = ofdev->dev.of_node;
  276. struct resource res;
  277. const struct pmc_type *type;
  278. int ret = 0;
  279. type = of_device_get_match_data(&ofdev->dev);
  280. if (!type)
  281. return -EINVAL;
  282. if (!of_device_is_available(np))
  283. return -ENODEV;
  284. has_deep_sleep = type->has_deep_sleep;
  285. immrbase = get_immrbase();
  286. is_pci_agent = mpc83xx_is_pci_agent();
  287. if (is_pci_agent < 0)
  288. return is_pci_agent;
  289. ret = of_address_to_resource(np, 0, &res);
  290. if (ret)
  291. return -ENODEV;
  292. pmc_irq = irq_of_parse_and_map(np, 0);
  293. if (pmc_irq) {
  294. ret = request_irq(pmc_irq, pmc_irq_handler, IRQF_SHARED,
  295. "pmc", ofdev);
  296. if (ret)
  297. return -EBUSY;
  298. }
  299. pmc_regs = ioremap(res.start, sizeof(*pmc_regs));
  300. if (!pmc_regs) {
  301. ret = -ENOMEM;
  302. goto out;
  303. }
  304. ret = of_address_to_resource(np, 1, &res);
  305. if (ret) {
  306. ret = -ENODEV;
  307. goto out_pmc;
  308. }
  309. clock_regs = ioremap(res.start, sizeof(*clock_regs));
  310. if (!clock_regs) {
  311. ret = -ENOMEM;
  312. goto out_pmc;
  313. }
  314. if (has_deep_sleep) {
  315. syscr_regs = ioremap(immrbase + IMMR_SYSCR_OFFSET,
  316. sizeof(*syscr_regs));
  317. if (!syscr_regs) {
  318. ret = -ENOMEM;
  319. goto out_syscr;
  320. }
  321. }
  322. if (is_pci_agent)
  323. mpc83xx_set_agent();
  324. suspend_set_ops(&mpc83xx_suspend_ops);
  325. return 0;
  326. out_syscr:
  327. iounmap(clock_regs);
  328. out_pmc:
  329. iounmap(pmc_regs);
  330. out:
  331. if (pmc_irq)
  332. free_irq(pmc_irq, ofdev);
  333. return ret;
  334. }
  335. static struct platform_driver pmc_driver = {
  336. .driver = {
  337. .name = "mpc83xx-pmc",
  338. .of_match_table = pmc_match,
  339. .suppress_bind_attrs = true,
  340. },
  341. .probe = pmc_probe,
  342. };
  343. builtin_platform_driver(pmc_driver);