pm.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 <plat/regs-serial.h>
  36. #include <mach/regs-clock.h>
  37. #include <mach/regs-gpio.h>
  38. #include <mach/regs-mem.h>
  39. #include <mach/regs-irq.h>
  40. #include <asm/mach/time.h>
  41. #include <plat/pm.h>
  42. #define PFX "s3c24xx-pm: "
  43. static struct sleep_save core_save[] = {
  44. SAVE_ITEM(S3C2410_LOCKTIME),
  45. SAVE_ITEM(S3C2410_CLKCON),
  46. /* we restore the timings here, with the proviso that the board
  47. * brings the system up in an slower, or equal frequency setting
  48. * to the original system.
  49. *
  50. * if we cannot guarantee this, then things are going to go very
  51. * wrong here, as we modify the refresh and both pll settings.
  52. */
  53. SAVE_ITEM(S3C2410_BWSCON),
  54. SAVE_ITEM(S3C2410_BANKCON0),
  55. SAVE_ITEM(S3C2410_BANKCON1),
  56. SAVE_ITEM(S3C2410_BANKCON2),
  57. SAVE_ITEM(S3C2410_BANKCON3),
  58. SAVE_ITEM(S3C2410_BANKCON4),
  59. SAVE_ITEM(S3C2410_BANKCON5),
  60. #ifndef CONFIG_CPU_FREQ
  61. SAVE_ITEM(S3C2410_CLKDIVN),
  62. SAVE_ITEM(S3C2410_MPLLCON),
  63. SAVE_ITEM(S3C2410_REFRESH),
  64. #endif
  65. SAVE_ITEM(S3C2410_UPLLCON),
  66. SAVE_ITEM(S3C2410_CLKSLOW),
  67. };
  68. static struct gpio_sleep {
  69. void __iomem *base;
  70. unsigned int gpcon;
  71. unsigned int gpdat;
  72. unsigned int gpup;
  73. } gpio_save[] = {
  74. [0] = {
  75. .base = S3C2410_GPACON,
  76. },
  77. [1] = {
  78. .base = S3C2410_GPBCON,
  79. },
  80. [2] = {
  81. .base = S3C2410_GPCCON,
  82. },
  83. [3] = {
  84. .base = S3C2410_GPDCON,
  85. },
  86. [4] = {
  87. .base = S3C2410_GPECON,
  88. },
  89. [5] = {
  90. .base = S3C2410_GPFCON,
  91. },
  92. [6] = {
  93. .base = S3C2410_GPGCON,
  94. },
  95. [7] = {
  96. .base = S3C2410_GPHCON,
  97. },
  98. };
  99. static struct sleep_save misc_save[] = {
  100. SAVE_ITEM(S3C2410_DCLKCON),
  101. };
  102. /* s3c_pm_check_resume_pin
  103. *
  104. * check to see if the pin is configured correctly for sleep mode, and
  105. * make any necessary adjustments if it is not
  106. */
  107. static void s3c_pm_check_resume_pin(unsigned int pin, unsigned int irqoffs)
  108. {
  109. unsigned long irqstate;
  110. unsigned long pinstate;
  111. int irq = s3c2410_gpio_getirq(pin);
  112. if (irqoffs < 4)
  113. irqstate = s3c_irqwake_intmask & (1L<<irqoffs);
  114. else
  115. irqstate = s3c_irqwake_eintmask & (1L<<irqoffs);
  116. pinstate = s3c2410_gpio_getcfg(pin);
  117. if (!irqstate) {
  118. if (pinstate == S3C2410_GPIO_IRQ)
  119. S3C_PMDBG("Leaving IRQ %d (pin %d) enabled\n", irq, pin);
  120. } else {
  121. if (pinstate == S3C2410_GPIO_IRQ) {
  122. S3C_PMDBG("Disabling IRQ %d (pin %d)\n", irq, pin);
  123. s3c2410_gpio_cfgpin(pin, S3C2410_GPIO_INPUT);
  124. }
  125. }
  126. }
  127. /* s3c_pm_configure_extint
  128. *
  129. * configure all external interrupt pins
  130. */
  131. void s3c_pm_configure_extint(void)
  132. {
  133. int pin;
  134. /* for each of the external interrupts (EINT0..EINT15) we
  135. * need to check wether it is an external interrupt source,
  136. * and then configure it as an input if it is not
  137. */
  138. for (pin = S3C2410_GPF0; pin <= S3C2410_GPF7; pin++) {
  139. s3c_pm_check_resume_pin(pin, pin - S3C2410_GPF0);
  140. }
  141. for (pin = S3C2410_GPG0; pin <= S3C2410_GPG7; pin++) {
  142. s3c_pm_check_resume_pin(pin, (pin - S3C2410_GPG0)+8);
  143. }
  144. }
  145. /* offsets for CON/DAT/UP registers */
  146. #define OFFS_CON (S3C2410_GPACON - S3C2410_GPACON)
  147. #define OFFS_DAT (S3C2410_GPADAT - S3C2410_GPACON)
  148. #define OFFS_UP (S3C2410_GPBUP - S3C2410_GPBCON)
  149. /* s3c_pm_save_gpios()
  150. *
  151. * Save the state of the GPIOs
  152. */
  153. void s3c_pm_save_gpios(void)
  154. {
  155. struct gpio_sleep *gps = gpio_save;
  156. unsigned int gpio;
  157. for (gpio = 0; gpio < ARRAY_SIZE(gpio_save); gpio++, gps++) {
  158. void __iomem *base = gps->base;
  159. gps->gpcon = __raw_readl(base + OFFS_CON);
  160. gps->gpdat = __raw_readl(base + OFFS_DAT);
  161. if (gpio > 0)
  162. gps->gpup = __raw_readl(base + OFFS_UP);
  163. }
  164. }
  165. /* Test whether the given masked+shifted bits of an GPIO configuration
  166. * are one of the SFN (special function) modes. */
  167. static inline int is_sfn(unsigned long con)
  168. {
  169. return (con == 2 || con == 3);
  170. }
  171. /* Test if the given masked+shifted GPIO configuration is an input */
  172. static inline int is_in(unsigned long con)
  173. {
  174. return con == 0;
  175. }
  176. /* Test if the given masked+shifted GPIO configuration is an output */
  177. static inline int is_out(unsigned long con)
  178. {
  179. return con == 1;
  180. }
  181. /**
  182. * s3c2410_pm_restore_gpio() - restore the given GPIO bank
  183. * @index: The number of the GPIO bank being resumed.
  184. * @gps: The sleep confgiuration for the bank.
  185. *
  186. * Restore one of the GPIO banks that was saved during suspend. This is
  187. * not as simple as once thought, due to the possibility of glitches
  188. * from the order that the CON and DAT registers are set in.
  189. *
  190. * The three states the pin can be are {IN,OUT,SFN} which gives us 9
  191. * combinations of changes to check. Three of these, if the pin stays
  192. * in the same configuration can be discounted. This leaves us with
  193. * the following:
  194. *
  195. * { IN => OUT } Change DAT first
  196. * { IN => SFN } Change CON first
  197. * { OUT => SFN } Change CON first, so new data will not glitch
  198. * { OUT => IN } Change CON first, so new data will not glitch
  199. * { SFN => IN } Change CON first
  200. * { SFN => OUT } Change DAT first, so new data will not glitch [1]
  201. *
  202. * We do not currently deal with the UP registers as these control
  203. * weak resistors, so a small delay in change should not need to bring
  204. * these into the calculations.
  205. *
  206. * [1] this assumes that writing to a pin DAT whilst in SFN will set the
  207. * state for when it is next output.
  208. */
  209. static void s3c2410_pm_restore_gpio(int index, struct gpio_sleep *gps)
  210. {
  211. void __iomem *base = gps->base;
  212. unsigned long gps_gpcon = gps->gpcon;
  213. unsigned long gps_gpdat = gps->gpdat;
  214. unsigned long old_gpcon;
  215. unsigned long old_gpdat;
  216. unsigned long old_gpup = 0x0;
  217. unsigned long gpcon;
  218. int nr;
  219. old_gpcon = __raw_readl(base + OFFS_CON);
  220. old_gpdat = __raw_readl(base + OFFS_DAT);
  221. if (base == S3C2410_GPACON) {
  222. /* GPACON only has one bit per control / data and no PULLUPs.
  223. * GPACON[x] = 0 => Output, 1 => SFN */
  224. /* first set all SFN bits to SFN */
  225. gpcon = old_gpcon | gps->gpcon;
  226. __raw_writel(gpcon, base + OFFS_CON);
  227. /* now set all the other bits */
  228. __raw_writel(gps_gpdat, base + OFFS_DAT);
  229. __raw_writel(gps_gpcon, base + OFFS_CON);
  230. } else {
  231. unsigned long old, new, mask;
  232. unsigned long change_mask = 0x0;
  233. old_gpup = __raw_readl(base + OFFS_UP);
  234. /* Create a change_mask of all the items that need to have
  235. * their CON value changed before their DAT value, so that
  236. * we minimise the work between the two settings.
  237. */
  238. for (nr = 0, mask = 0x03; nr < 32; nr += 2, mask <<= 2) {
  239. old = (old_gpcon & mask) >> nr;
  240. new = (gps_gpcon & mask) >> nr;
  241. /* If there is no change, then skip */
  242. if (old == new)
  243. continue;
  244. /* If both are special function, then skip */
  245. if (is_sfn(old) && is_sfn(new))
  246. continue;
  247. /* Change is IN => OUT, do not change now */
  248. if (is_in(old) && is_out(new))
  249. continue;
  250. /* Change is SFN => OUT, do not change now */
  251. if (is_sfn(old) && is_out(new))
  252. continue;
  253. /* We should now be at the case of IN=>SFN,
  254. * OUT=>SFN, OUT=>IN, SFN=>IN. */
  255. change_mask |= mask;
  256. }
  257. /* Write the new CON settings */
  258. gpcon = old_gpcon & ~change_mask;
  259. gpcon |= gps_gpcon & change_mask;
  260. __raw_writel(gpcon, base + OFFS_CON);
  261. /* Now change any items that require DAT,CON */
  262. __raw_writel(gps_gpdat, base + OFFS_DAT);
  263. __raw_writel(gps_gpcon, base + OFFS_CON);
  264. __raw_writel(gps->gpup, base + OFFS_UP);
  265. }
  266. S3C_PMDBG("GPIO[%d] CON %08lx => %08lx, DAT %08lx => %08lx\n",
  267. index, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
  268. }
  269. /** s3c2410_pm_restore_gpios()
  270. *
  271. * Restore the state of the GPIOs
  272. */
  273. void s3c_pm_restore_gpios(void)
  274. {
  275. struct gpio_sleep *gps = gpio_save;
  276. int gpio;
  277. for (gpio = 0; gpio < ARRAY_SIZE(gpio_save); gpio++, gps++) {
  278. s3c2410_pm_restore_gpio(gpio, gps);
  279. }
  280. }
  281. void s3c_pm_restore_core(void)
  282. {
  283. s3c_pm_do_restore_core(core_save, ARRAY_SIZE(core_save));
  284. s3c_pm_do_restore(misc_save, ARRAY_SIZE(misc_save));
  285. }
  286. void s3c_pm_save_core(void)
  287. {
  288. s3c_pm_do_save(misc_save, ARRAY_SIZE(misc_save));
  289. s3c_pm_do_save(core_save, ARRAY_SIZE(core_save));
  290. }