pm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* linux/arch/arm/plat-s3c24xx/pm.c
  2. *
  3. * Copyright (c) 2004,2006 Simtec Electronics
  4. * Ben Dooks <ben@simtec.co.uk>
  5. *
  6. * S3C24XX Power Manager (Suspend-To-RAM) support
  7. *
  8. * See Documentation/arm/Samsung-S3C24XX/Suspend.txt for more information
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. * Parts based on arch/arm/mach-pxa/pm.c
  25. *
  26. * Thanks to Dimitry Andric for debugging
  27. */
  28. #include <linux/init.h>
  29. #include <linux/suspend.h>
  30. #include <linux/errno.h>
  31. #include <linux/time.h>
  32. #include <linux/interrupt.h>
  33. #include <linux/serial_core.h>
  34. #include <linux/io.h>
  35. #include <asm/cacheflush.h>
  36. #include <mach/hardware.h>
  37. #include <plat/regs-serial.h>
  38. #include <mach/regs-clock.h>
  39. #include <mach/regs-gpio.h>
  40. #include <mach/regs-mem.h>
  41. #include <mach/regs-irq.h>
  42. #include <asm/mach/time.h>
  43. #include <plat/pm.h>
  44. #define PFX "s3c24xx-pm: "
  45. static struct sleep_save core_save[] = {
  46. SAVE_ITEM(S3C2410_LOCKTIME),
  47. SAVE_ITEM(S3C2410_CLKCON),
  48. /* we restore the timings here, with the proviso that the board
  49. * brings the system up in an slower, or equal frequency setting
  50. * to the original system.
  51. *
  52. * if we cannot guarantee this, then things are going to go very
  53. * wrong here, as we modify the refresh and both pll settings.
  54. */
  55. SAVE_ITEM(S3C2410_BWSCON),
  56. SAVE_ITEM(S3C2410_BANKCON0),
  57. SAVE_ITEM(S3C2410_BANKCON1),
  58. SAVE_ITEM(S3C2410_BANKCON2),
  59. SAVE_ITEM(S3C2410_BANKCON3),
  60. SAVE_ITEM(S3C2410_BANKCON4),
  61. SAVE_ITEM(S3C2410_BANKCON5),
  62. #ifndef CONFIG_CPU_FREQ
  63. SAVE_ITEM(S3C2410_CLKDIVN),
  64. SAVE_ITEM(S3C2410_MPLLCON),
  65. SAVE_ITEM(S3C2410_REFRESH),
  66. #endif
  67. SAVE_ITEM(S3C2410_UPLLCON),
  68. SAVE_ITEM(S3C2410_CLKSLOW),
  69. };
  70. static struct gpio_sleep {
  71. void __iomem *base;
  72. unsigned int gpcon;
  73. unsigned int gpdat;
  74. unsigned int gpup;
  75. } gpio_save[] = {
  76. [0] = {
  77. .base = S3C2410_GPACON,
  78. },
  79. [1] = {
  80. .base = S3C2410_GPBCON,
  81. },
  82. [2] = {
  83. .base = S3C2410_GPCCON,
  84. },
  85. [3] = {
  86. .base = S3C2410_GPDCON,
  87. },
  88. [4] = {
  89. .base = S3C2410_GPECON,
  90. },
  91. [5] = {
  92. .base = S3C2410_GPFCON,
  93. },
  94. [6] = {
  95. .base = S3C2410_GPGCON,
  96. },
  97. [7] = {
  98. .base = S3C2410_GPHCON,
  99. },
  100. };
  101. static struct sleep_save misc_save[] = {
  102. SAVE_ITEM(S3C2410_DCLKCON),
  103. };
  104. #ifdef CONFIG_S3C2410_PM_DEBUG
  105. #define SAVE_UART(va) \
  106. SAVE_ITEM((va) + S3C2410_ULCON), \
  107. SAVE_ITEM((va) + S3C2410_UCON), \
  108. SAVE_ITEM((va) + S3C2410_UFCON), \
  109. SAVE_ITEM((va) + S3C2410_UMCON), \
  110. SAVE_ITEM((va) + S3C2410_UBRDIV)
  111. static struct sleep_save uart_save[] = {
  112. SAVE_UART(S3C24XX_VA_UART0),
  113. SAVE_UART(S3C24XX_VA_UART1),
  114. #ifndef CONFIG_CPU_S3C2400
  115. SAVE_UART(S3C24XX_VA_UART2),
  116. #endif
  117. };
  118. /* debug
  119. *
  120. * we send the debug to printascii() to allow it to be seen if the
  121. * system never wakes up from the sleep
  122. */
  123. static void s3c2410_pm_debug_init(void)
  124. {
  125. unsigned long tmp = __raw_readl(S3C2410_CLKCON);
  126. /* re-start uart clocks */
  127. tmp |= S3C2410_CLKCON_UART0;
  128. tmp |= S3C2410_CLKCON_UART1;
  129. tmp |= S3C2410_CLKCON_UART2;
  130. __raw_writel(tmp, S3C2410_CLKCON);
  131. udelay(10);
  132. }
  133. #else
  134. #define s3c2410_pm_debug_init() do { } while(0)
  135. static struct sleep_save uart_save[] = {};
  136. #endif
  137. /* s3c2410_pm_show_resume_irqs
  138. *
  139. * print any IRQs asserted at resume time (ie, we woke from)
  140. */
  141. static void s3c2410_pm_show_resume_irqs(int start, unsigned long which,
  142. unsigned long mask)
  143. {
  144. int i;
  145. which &= ~mask;
  146. for (i = 0; i <= 31; i++) {
  147. if ((which) & (1L<<i)) {
  148. S3C_PMDBG("IRQ %d asserted at resume\n", start+i);
  149. }
  150. }
  151. }
  152. /* s3c_pm_check_resume_pin
  153. *
  154. * check to see if the pin is configured correctly for sleep mode, and
  155. * make any necessary adjustments if it is not
  156. */
  157. static void s3c_pm_check_resume_pin(unsigned int pin, unsigned int irqoffs)
  158. {
  159. unsigned long irqstate;
  160. unsigned long pinstate;
  161. int irq = s3c2410_gpio_getirq(pin);
  162. if (irqoffs < 4)
  163. irqstate = s3c_irqwake_intmask & (1L<<irqoffs);
  164. else
  165. irqstate = s3c_irqwake_eintmask & (1L<<irqoffs);
  166. pinstate = s3c2410_gpio_getcfg(pin);
  167. if (!irqstate) {
  168. if (pinstate == S3C2410_GPIO_IRQ)
  169. S3C_PMDBG("Leaving IRQ %d (pin %d) enabled\n", irq, pin);
  170. } else {
  171. if (pinstate == S3C2410_GPIO_IRQ) {
  172. S3C_PMDBG("Disabling IRQ %d (pin %d)\n", irq, pin);
  173. s3c2410_gpio_cfgpin(pin, S3C2410_GPIO_INPUT);
  174. }
  175. }
  176. }
  177. /* s3c2410_pm_configure_extint
  178. *
  179. * configure all external interrupt pins
  180. */
  181. static void s3c2410_pm_configure_extint(void)
  182. {
  183. int pin;
  184. /* for each of the external interrupts (EINT0..EINT15) we
  185. * need to check wether it is an external interrupt source,
  186. * and then configure it as an input if it is not
  187. */
  188. for (pin = S3C2410_GPF0; pin <= S3C2410_GPF7; pin++) {
  189. s3c_pm_check_resume_pin(pin, pin - S3C2410_GPF0);
  190. }
  191. for (pin = S3C2410_GPG0; pin <= S3C2410_GPG7; pin++) {
  192. s3c_pm_check_resume_pin(pin, (pin - S3C2410_GPG0)+8);
  193. }
  194. }
  195. /* offsets for CON/DAT/UP registers */
  196. #define OFFS_CON (S3C2410_GPACON - S3C2410_GPACON)
  197. #define OFFS_DAT (S3C2410_GPADAT - S3C2410_GPACON)
  198. #define OFFS_UP (S3C2410_GPBUP - S3C2410_GPBCON)
  199. /* s3c2410_pm_save_gpios()
  200. *
  201. * Save the state of the GPIOs
  202. */
  203. static void s3c2410_pm_save_gpios(void)
  204. {
  205. struct gpio_sleep *gps = gpio_save;
  206. unsigned int gpio;
  207. for (gpio = 0; gpio < ARRAY_SIZE(gpio_save); gpio++, gps++) {
  208. void __iomem *base = gps->base;
  209. gps->gpcon = __raw_readl(base + OFFS_CON);
  210. gps->gpdat = __raw_readl(base + OFFS_DAT);
  211. if (gpio > 0)
  212. gps->gpup = __raw_readl(base + OFFS_UP);
  213. }
  214. }
  215. /* Test whether the given masked+shifted bits of an GPIO configuration
  216. * are one of the SFN (special function) modes. */
  217. static inline int is_sfn(unsigned long con)
  218. {
  219. return (con == 2 || con == 3);
  220. }
  221. /* Test if the given masked+shifted GPIO configuration is an input */
  222. static inline int is_in(unsigned long con)
  223. {
  224. return con == 0;
  225. }
  226. /* Test if the given masked+shifted GPIO configuration is an output */
  227. static inline int is_out(unsigned long con)
  228. {
  229. return con == 1;
  230. }
  231. /* s3c2410_pm_restore_gpio()
  232. *
  233. * Restore one of the GPIO banks that was saved during suspend. This is
  234. * not as simple as once thought, due to the possibility of glitches
  235. * from the order that the CON and DAT registers are set in.
  236. *
  237. * The three states the pin can be are {IN,OUT,SFN} which gives us 9
  238. * combinations of changes to check. Three of these, if the pin stays
  239. * in the same configuration can be discounted. This leaves us with
  240. * the following:
  241. *
  242. * { IN => OUT } Change DAT first
  243. * { IN => SFN } Change CON first
  244. * { OUT => SFN } Change CON first, so new data will not glitch
  245. * { OUT => IN } Change CON first, so new data will not glitch
  246. * { SFN => IN } Change CON first
  247. * { SFN => OUT } Change DAT first, so new data will not glitch [1]
  248. *
  249. * We do not currently deal with the UP registers as these control
  250. * weak resistors, so a small delay in change should not need to bring
  251. * these into the calculations.
  252. *
  253. * [1] this assumes that writing to a pin DAT whilst in SFN will set the
  254. * state for when it is next output.
  255. */
  256. static void s3c2410_pm_restore_gpio(int index, struct gpio_sleep *gps)
  257. {
  258. void __iomem *base = gps->base;
  259. unsigned long gps_gpcon = gps->gpcon;
  260. unsigned long gps_gpdat = gps->gpdat;
  261. unsigned long old_gpcon;
  262. unsigned long old_gpdat;
  263. unsigned long old_gpup = 0x0;
  264. unsigned long gpcon;
  265. int nr;
  266. old_gpcon = __raw_readl(base + OFFS_CON);
  267. old_gpdat = __raw_readl(base + OFFS_DAT);
  268. if (base == S3C2410_GPACON) {
  269. /* GPACON only has one bit per control / data and no PULLUPs.
  270. * GPACON[x] = 0 => Output, 1 => SFN */
  271. /* first set all SFN bits to SFN */
  272. gpcon = old_gpcon | gps->gpcon;
  273. __raw_writel(gpcon, base + OFFS_CON);
  274. /* now set all the other bits */
  275. __raw_writel(gps_gpdat, base + OFFS_DAT);
  276. __raw_writel(gps_gpcon, base + OFFS_CON);
  277. } else {
  278. unsigned long old, new, mask;
  279. unsigned long change_mask = 0x0;
  280. old_gpup = __raw_readl(base + OFFS_UP);
  281. /* Create a change_mask of all the items that need to have
  282. * their CON value changed before their DAT value, so that
  283. * we minimise the work between the two settings.
  284. */
  285. for (nr = 0, mask = 0x03; nr < 32; nr += 2, mask <<= 2) {
  286. old = (old_gpcon & mask) >> nr;
  287. new = (gps_gpcon & mask) >> nr;
  288. /* If there is no change, then skip */
  289. if (old == new)
  290. continue;
  291. /* If both are special function, then skip */
  292. if (is_sfn(old) && is_sfn(new))
  293. continue;
  294. /* Change is IN => OUT, do not change now */
  295. if (is_in(old) && is_out(new))
  296. continue;
  297. /* Change is SFN => OUT, do not change now */
  298. if (is_sfn(old) && is_out(new))
  299. continue;
  300. /* We should now be at the case of IN=>SFN,
  301. * OUT=>SFN, OUT=>IN, SFN=>IN. */
  302. change_mask |= mask;
  303. }
  304. /* Write the new CON settings */
  305. gpcon = old_gpcon & ~change_mask;
  306. gpcon |= gps_gpcon & change_mask;
  307. __raw_writel(gpcon, base + OFFS_CON);
  308. /* Now change any items that require DAT,CON */
  309. __raw_writel(gps_gpdat, base + OFFS_DAT);
  310. __raw_writel(gps_gpcon, base + OFFS_CON);
  311. __raw_writel(gps->gpup, base + OFFS_UP);
  312. }
  313. S3C_PMDBG("GPIO[%d] CON %08lx => %08lx, DAT %08lx => %08lx\n",
  314. index, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
  315. }
  316. /** s3c2410_pm_restore_gpios()
  317. *
  318. * Restore the state of the GPIOs
  319. */
  320. static void s3c2410_pm_restore_gpios(void)
  321. {
  322. struct gpio_sleep *gps = gpio_save;
  323. int gpio;
  324. for (gpio = 0; gpio < ARRAY_SIZE(gpio_save); gpio++, gps++) {
  325. s3c2410_pm_restore_gpio(gpio, gps);
  326. }
  327. }
  328. void (*pm_cpu_prep)(void);
  329. void (*pm_cpu_sleep)(void);
  330. #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
  331. /* s3c2410_pm_enter
  332. *
  333. * central control for sleep/resume process
  334. */
  335. static int s3c2410_pm_enter(suspend_state_t state)
  336. {
  337. unsigned long regs_save[16];
  338. /* ensure the debug is initialised (if enabled) */
  339. s3c2410_pm_debug_init();
  340. S3C_PMDBG("s3c2410_pm_enter(%d)\n", state);
  341. if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
  342. printk(KERN_ERR PFX "error: no cpu sleep functions set\n");
  343. return -EINVAL;
  344. }
  345. /* check if we have anything to wake-up with... bad things seem
  346. * to happen if you suspend with no wakeup (system will often
  347. * require a full power-cycle)
  348. */
  349. if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
  350. !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
  351. printk(KERN_ERR PFX "No sources enabled for wake-up!\n");
  352. printk(KERN_ERR PFX "Aborting sleep\n");
  353. return -EINVAL;
  354. }
  355. /* prepare check area if configured */
  356. s3c_pm_check_prepare();
  357. /* store the physical address of the register recovery block */
  358. s3c_sleep_save_phys = virt_to_phys(regs_save);
  359. S3C_PMDBG("s3c_sleep_save_phys=0x%08lx\n", s3c_sleep_save_phys);
  360. /* save all necessary core registers not covered by the drivers */
  361. s3c2410_pm_save_gpios();
  362. s3c_pm_do_save(misc_save, ARRAY_SIZE(misc_save));
  363. s3c_pm_do_save(core_save, ARRAY_SIZE(core_save));
  364. s3c_pm_do_save(uart_save, ARRAY_SIZE(uart_save));
  365. /* set the irq configuration for wake */
  366. s3c2410_pm_configure_extint();
  367. S3C_PMDBG("sleep: irq wakeup masks: %08lx,%08lx\n",
  368. s3c_irqwake_intmask, s3c_irqwake_eintmask);
  369. __raw_writel(s3c_irqwake_intmask, S3C2410_INTMSK);
  370. __raw_writel(s3c_irqwake_eintmask, S3C2410_EINTMASK);
  371. /* ack any outstanding external interrupts before we go to sleep */
  372. __raw_writel(__raw_readl(S3C2410_EINTPEND), S3C2410_EINTPEND);
  373. __raw_writel(__raw_readl(S3C2410_INTPND), S3C2410_INTPND);
  374. __raw_writel(__raw_readl(S3C2410_SRCPND), S3C2410_SRCPND);
  375. /* call cpu specific preparation */
  376. pm_cpu_prep();
  377. /* flush cache back to ram */
  378. flush_cache_all();
  379. s3c_pm_check_store();
  380. /* send the cpu to sleep... */
  381. __raw_writel(0x00, S3C2410_CLKCON); /* turn off clocks over sleep */
  382. /* s3c2410_cpu_save will also act as our return point from when
  383. * we resume as it saves its own register state, so use the return
  384. * code to differentiate return from save and return from sleep */
  385. if (s3c2410_cpu_save(regs_save) == 0) {
  386. flush_cache_all();
  387. pm_cpu_sleep();
  388. }
  389. /* restore the cpu state */
  390. cpu_init();
  391. /* restore the system state */
  392. s3c_pm_do_restore_core(core_save, ARRAY_SIZE(core_save));
  393. s3c_pm_do_restore(misc_save, ARRAY_SIZE(misc_save));
  394. s3c_pm_do_restore(uart_save, ARRAY_SIZE(uart_save));
  395. s3c2410_pm_restore_gpios();
  396. s3c2410_pm_debug_init();
  397. /* check what irq (if any) restored the system */
  398. S3C_PMDBG("post sleep: IRQs 0x%08x, 0x%08x\n",
  399. __raw_readl(S3C2410_SRCPND),
  400. __raw_readl(S3C2410_EINTPEND));
  401. s3c2410_pm_show_resume_irqs(IRQ_EINT0, __raw_readl(S3C2410_SRCPND),
  402. s3c_irqwake_intmask);
  403. s3c2410_pm_show_resume_irqs(IRQ_EINT4-4, __raw_readl(S3C2410_EINTPEND),
  404. s3c_irqwake_eintmask);
  405. S3C_PMDBG("post sleep, preparing to return\n");
  406. s3c_pm_check_restore();
  407. /* ok, let's return from sleep */
  408. S3C_PMDBG("S3C2410 PM Resume (post-restore)\n");
  409. return 0;
  410. }
  411. static struct platform_suspend_ops s3c2410_pm_ops = {
  412. .enter = s3c2410_pm_enter,
  413. .valid = suspend_valid_only_mem,
  414. };
  415. /* s3c2410_pm_init
  416. *
  417. * Attach the power management functions. This should be called
  418. * from the board specific initialisation if the board supports
  419. * it.
  420. */
  421. int __init s3c2410_pm_init(void)
  422. {
  423. printk("S3C2410 Power Management, (c) 2004 Simtec Electronics\n");
  424. suspend_set_ops(&s3c2410_pm_ops);
  425. return 0;
  426. }