suspend.c 9.1 KB

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