pm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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/crc32.h>
  34. #include <linux/ioport.h>
  35. #include <linux/delay.h>
  36. #include <linux/serial_core.h>
  37. #include <linux/io.h>
  38. #include <asm/cacheflush.h>
  39. #include <mach/hardware.h>
  40. #include <plat/regs-serial.h>
  41. #include <mach/regs-clock.h>
  42. #include <mach/regs-gpio.h>
  43. #include <mach/regs-mem.h>
  44. #include <mach/regs-irq.h>
  45. #include <asm/mach/time.h>
  46. #include <plat/pm.h>
  47. /* for external use */
  48. unsigned long s3c_pm_flags;
  49. #define PFX "s3c24xx-pm: "
  50. static struct sleep_save core_save[] = {
  51. SAVE_ITEM(S3C2410_LOCKTIME),
  52. SAVE_ITEM(S3C2410_CLKCON),
  53. /* we restore the timings here, with the proviso that the board
  54. * brings the system up in an slower, or equal frequency setting
  55. * to the original system.
  56. *
  57. * if we cannot guarantee this, then things are going to go very
  58. * wrong here, as we modify the refresh and both pll settings.
  59. */
  60. SAVE_ITEM(S3C2410_BWSCON),
  61. SAVE_ITEM(S3C2410_BANKCON0),
  62. SAVE_ITEM(S3C2410_BANKCON1),
  63. SAVE_ITEM(S3C2410_BANKCON2),
  64. SAVE_ITEM(S3C2410_BANKCON3),
  65. SAVE_ITEM(S3C2410_BANKCON4),
  66. SAVE_ITEM(S3C2410_BANKCON5),
  67. SAVE_ITEM(S3C2410_CLKDIVN),
  68. SAVE_ITEM(S3C2410_MPLLCON),
  69. SAVE_ITEM(S3C2410_UPLLCON),
  70. SAVE_ITEM(S3C2410_CLKSLOW),
  71. SAVE_ITEM(S3C2410_REFRESH),
  72. };
  73. static struct gpio_sleep {
  74. void __iomem *base;
  75. unsigned int gpcon;
  76. unsigned int gpdat;
  77. unsigned int gpup;
  78. } gpio_save[] = {
  79. [0] = {
  80. .base = S3C2410_GPACON,
  81. },
  82. [1] = {
  83. .base = S3C2410_GPBCON,
  84. },
  85. [2] = {
  86. .base = S3C2410_GPCCON,
  87. },
  88. [3] = {
  89. .base = S3C2410_GPDCON,
  90. },
  91. [4] = {
  92. .base = S3C2410_GPECON,
  93. },
  94. [5] = {
  95. .base = S3C2410_GPFCON,
  96. },
  97. [6] = {
  98. .base = S3C2410_GPGCON,
  99. },
  100. [7] = {
  101. .base = S3C2410_GPHCON,
  102. },
  103. };
  104. static struct sleep_save misc_save[] = {
  105. SAVE_ITEM(S3C2410_DCLKCON),
  106. };
  107. #ifdef CONFIG_S3C2410_PM_DEBUG
  108. #define SAVE_UART(va) \
  109. SAVE_ITEM((va) + S3C2410_ULCON), \
  110. SAVE_ITEM((va) + S3C2410_UCON), \
  111. SAVE_ITEM((va) + S3C2410_UFCON), \
  112. SAVE_ITEM((va) + S3C2410_UMCON), \
  113. SAVE_ITEM((va) + S3C2410_UBRDIV)
  114. static struct sleep_save uart_save[] = {
  115. SAVE_UART(S3C24XX_VA_UART0),
  116. SAVE_UART(S3C24XX_VA_UART1),
  117. #ifndef CONFIG_CPU_S3C2400
  118. SAVE_UART(S3C24XX_VA_UART2),
  119. #endif
  120. };
  121. /* debug
  122. *
  123. * we send the debug to printascii() to allow it to be seen if the
  124. * system never wakes up from the sleep
  125. */
  126. extern void printascii(const char *);
  127. void pm_dbg(const char *fmt, ...)
  128. {
  129. va_list va;
  130. char buff[256];
  131. va_start(va, fmt);
  132. vsprintf(buff, fmt, va);
  133. va_end(va);
  134. printascii(buff);
  135. }
  136. static void s3c2410_pm_debug_init(void)
  137. {
  138. unsigned long tmp = __raw_readl(S3C2410_CLKCON);
  139. /* re-start uart clocks */
  140. tmp |= S3C2410_CLKCON_UART0;
  141. tmp |= S3C2410_CLKCON_UART1;
  142. tmp |= S3C2410_CLKCON_UART2;
  143. __raw_writel(tmp, S3C2410_CLKCON);
  144. udelay(10);
  145. }
  146. #define DBG(fmt...) pm_dbg(fmt)
  147. #else
  148. #define DBG(fmt...) printk(KERN_DEBUG fmt)
  149. #define s3c2410_pm_debug_init() do { } while(0)
  150. static struct sleep_save uart_save[] = {};
  151. #endif
  152. #if defined(CONFIG_S3C2410_PM_CHECK) && CONFIG_S3C2410_PM_CHECK_CHUNKSIZE != 0
  153. /* suspend checking code...
  154. *
  155. * this next area does a set of crc checks over all the installed
  156. * memory, so the system can verify if the resume was ok.
  157. *
  158. * CONFIG_S3C2410_PM_CHECK_CHUNKSIZE defines the block-size for the CRC,
  159. * increasing it will mean that the area corrupted will be less easy to spot,
  160. * and reducing the size will cause the CRC save area to grow
  161. */
  162. #define CHECK_CHUNKSIZE (CONFIG_S3C2410_PM_CHECK_CHUNKSIZE * 1024)
  163. static u32 crc_size; /* size needed for the crc block */
  164. static u32 *crcs; /* allocated over suspend/resume */
  165. typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
  166. /* s3c2410_pm_run_res
  167. *
  168. * go thorugh the given resource list, and look for system ram
  169. */
  170. static void s3c2410_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg)
  171. {
  172. while (ptr != NULL) {
  173. if (ptr->child != NULL)
  174. s3c2410_pm_run_res(ptr->child, fn, arg);
  175. if ((ptr->flags & IORESOURCE_MEM) &&
  176. strcmp(ptr->name, "System RAM") == 0) {
  177. DBG("Found system RAM at %08lx..%08lx\n",
  178. ptr->start, ptr->end);
  179. arg = (fn)(ptr, arg);
  180. }
  181. ptr = ptr->sibling;
  182. }
  183. }
  184. static void s3c2410_pm_run_sysram(run_fn_t fn, u32 *arg)
  185. {
  186. s3c2410_pm_run_res(&iomem_resource, fn, arg);
  187. }
  188. static u32 *s3c2410_pm_countram(struct resource *res, u32 *val)
  189. {
  190. u32 size = (u32)(res->end - res->start)+1;
  191. size += CHECK_CHUNKSIZE-1;
  192. size /= CHECK_CHUNKSIZE;
  193. DBG("Area %08lx..%08lx, %d blocks\n", res->start, res->end, size);
  194. *val += size * sizeof(u32);
  195. return val;
  196. }
  197. /* s3c2410_pm_prepare_check
  198. *
  199. * prepare the necessary information for creating the CRCs. This
  200. * must be done before the final save, as it will require memory
  201. * allocating, and thus touching bits of the kernel we do not
  202. * know about.
  203. */
  204. static void s3c2410_pm_check_prepare(void)
  205. {
  206. crc_size = 0;
  207. s3c2410_pm_run_sysram(s3c2410_pm_countram, &crc_size);
  208. DBG("s3c2410_pm_prepare_check: %u checks needed\n", crc_size);
  209. crcs = kmalloc(crc_size+4, GFP_KERNEL);
  210. if (crcs == NULL)
  211. printk(KERN_ERR "Cannot allocated CRC save area\n");
  212. }
  213. static u32 *s3c2410_pm_makecheck(struct resource *res, u32 *val)
  214. {
  215. unsigned long addr, left;
  216. for (addr = res->start; addr < res->end;
  217. addr += CHECK_CHUNKSIZE) {
  218. left = res->end - addr;
  219. if (left > CHECK_CHUNKSIZE)
  220. left = CHECK_CHUNKSIZE;
  221. *val = crc32_le(~0, phys_to_virt(addr), left);
  222. val++;
  223. }
  224. return val;
  225. }
  226. /* s3c2410_pm_check_store
  227. *
  228. * compute the CRC values for the memory blocks before the final
  229. * sleep.
  230. */
  231. static void s3c2410_pm_check_store(void)
  232. {
  233. if (crcs != NULL)
  234. s3c2410_pm_run_sysram(s3c2410_pm_makecheck, crcs);
  235. }
  236. /* in_region
  237. *
  238. * return TRUE if the area defined by ptr..ptr+size contatins the
  239. * what..what+whatsz
  240. */
  241. static inline int in_region(void *ptr, int size, void *what, size_t whatsz)
  242. {
  243. if ((what+whatsz) < ptr)
  244. return 0;
  245. if (what > (ptr+size))
  246. return 0;
  247. return 1;
  248. }
  249. static u32 *s3c2410_pm_runcheck(struct resource *res, u32 *val)
  250. {
  251. void *save_at = phys_to_virt(s3c2410_sleep_save_phys);
  252. unsigned long addr;
  253. unsigned long left;
  254. void *ptr;
  255. u32 calc;
  256. for (addr = res->start; addr < res->end;
  257. addr += CHECK_CHUNKSIZE) {
  258. left = res->end - addr;
  259. if (left > CHECK_CHUNKSIZE)
  260. left = CHECK_CHUNKSIZE;
  261. ptr = phys_to_virt(addr);
  262. if (in_region(ptr, left, crcs, crc_size)) {
  263. DBG("skipping %08lx, has crc block in\n", addr);
  264. goto skip_check;
  265. }
  266. if (in_region(ptr, left, save_at, 32*4 )) {
  267. DBG("skipping %08lx, has save block in\n", addr);
  268. goto skip_check;
  269. }
  270. /* calculate and check the checksum */
  271. calc = crc32_le(~0, ptr, left);
  272. if (calc != *val) {
  273. printk(KERN_ERR PFX "Restore CRC error at "
  274. "%08lx (%08x vs %08x)\n", addr, calc, *val);
  275. DBG("Restore CRC error at %08lx (%08x vs %08x)\n",
  276. addr, calc, *val);
  277. }
  278. skip_check:
  279. val++;
  280. }
  281. return val;
  282. }
  283. /* s3c2410_pm_check_restore
  284. *
  285. * check the CRCs after the restore event and free the memory used
  286. * to hold them
  287. */
  288. static void s3c2410_pm_check_restore(void)
  289. {
  290. if (crcs != NULL) {
  291. s3c2410_pm_run_sysram(s3c2410_pm_runcheck, crcs);
  292. kfree(crcs);
  293. crcs = NULL;
  294. }
  295. }
  296. #else
  297. #define s3c2410_pm_check_prepare() do { } while(0)
  298. #define s3c2410_pm_check_restore() do { } while(0)
  299. #define s3c2410_pm_check_store() do { } while(0)
  300. #endif
  301. /* helper functions to save and restore register state */
  302. void s3c2410_pm_do_save(struct sleep_save *ptr, int count)
  303. {
  304. for (; count > 0; count--, ptr++) {
  305. ptr->val = __raw_readl(ptr->reg);
  306. DBG("saved %p value %08lx\n", ptr->reg, ptr->val);
  307. }
  308. }
  309. /* s3c2410_pm_do_restore
  310. *
  311. * restore the system from the given list of saved registers
  312. *
  313. * Note, we do not use DBG() in here, as the system may not have
  314. * restore the UARTs state yet
  315. */
  316. void s3c2410_pm_do_restore(struct sleep_save *ptr, int count)
  317. {
  318. for (; count > 0; count--, ptr++) {
  319. printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
  320. ptr->reg, ptr->val, __raw_readl(ptr->reg));
  321. __raw_writel(ptr->val, ptr->reg);
  322. }
  323. }
  324. /* s3c2410_pm_do_restore_core
  325. *
  326. * similar to s3c2410_pm_do_restore_core
  327. *
  328. * WARNING: Do not put any debug in here that may effect memory or use
  329. * peripherals, as things may be changing!
  330. */
  331. static void s3c2410_pm_do_restore_core(struct sleep_save *ptr, int count)
  332. {
  333. for (; count > 0; count--, ptr++) {
  334. __raw_writel(ptr->val, ptr->reg);
  335. }
  336. }
  337. /* s3c2410_pm_show_resume_irqs
  338. *
  339. * print any IRQs asserted at resume time (ie, we woke from)
  340. */
  341. static void s3c2410_pm_show_resume_irqs(int start, unsigned long which,
  342. unsigned long mask)
  343. {
  344. int i;
  345. which &= ~mask;
  346. for (i = 0; i <= 31; i++) {
  347. if ((which) & (1L<<i)) {
  348. DBG("IRQ %d asserted at resume\n", start+i);
  349. }
  350. }
  351. }
  352. /* s3c2410_pm_check_resume_pin
  353. *
  354. * check to see if the pin is configured correctly for sleep mode, and
  355. * make any necessary adjustments if it is not
  356. */
  357. static void s3c2410_pm_check_resume_pin(unsigned int pin, unsigned int irqoffs)
  358. {
  359. unsigned long irqstate;
  360. unsigned long pinstate;
  361. int irq = s3c2410_gpio_getirq(pin);
  362. if (irqoffs < 4)
  363. irqstate = s3c_irqwake_intmask & (1L<<irqoffs);
  364. else
  365. irqstate = s3c_irqwake_eintmask & (1L<<irqoffs);
  366. pinstate = s3c2410_gpio_getcfg(pin);
  367. if (!irqstate) {
  368. if (pinstate == S3C2410_GPIO_IRQ)
  369. DBG("Leaving IRQ %d (pin %d) enabled\n", irq, pin);
  370. } else {
  371. if (pinstate == S3C2410_GPIO_IRQ) {
  372. DBG("Disabling IRQ %d (pin %d)\n", irq, pin);
  373. s3c2410_gpio_cfgpin(pin, S3C2410_GPIO_INPUT);
  374. }
  375. }
  376. }
  377. /* s3c2410_pm_configure_extint
  378. *
  379. * configure all external interrupt pins
  380. */
  381. static void s3c2410_pm_configure_extint(void)
  382. {
  383. int pin;
  384. /* for each of the external interrupts (EINT0..EINT15) we
  385. * need to check wether it is an external interrupt source,
  386. * and then configure it as an input if it is not
  387. */
  388. for (pin = S3C2410_GPF0; pin <= S3C2410_GPF7; pin++) {
  389. s3c2410_pm_check_resume_pin(pin, pin - S3C2410_GPF0);
  390. }
  391. for (pin = S3C2410_GPG0; pin <= S3C2410_GPG7; pin++) {
  392. s3c2410_pm_check_resume_pin(pin, (pin - S3C2410_GPG0)+8);
  393. }
  394. }
  395. /* offsets for CON/DAT/UP registers */
  396. #define OFFS_CON (S3C2410_GPACON - S3C2410_GPACON)
  397. #define OFFS_DAT (S3C2410_GPADAT - S3C2410_GPACON)
  398. #define OFFS_UP (S3C2410_GPBUP - S3C2410_GPBCON)
  399. /* s3c2410_pm_save_gpios()
  400. *
  401. * Save the state of the GPIOs
  402. */
  403. static void s3c2410_pm_save_gpios(void)
  404. {
  405. struct gpio_sleep *gps = gpio_save;
  406. unsigned int gpio;
  407. for (gpio = 0; gpio < ARRAY_SIZE(gpio_save); gpio++, gps++) {
  408. void __iomem *base = gps->base;
  409. gps->gpcon = __raw_readl(base + OFFS_CON);
  410. gps->gpdat = __raw_readl(base + OFFS_DAT);
  411. if (gpio > 0)
  412. gps->gpup = __raw_readl(base + OFFS_UP);
  413. }
  414. }
  415. /* Test whether the given masked+shifted bits of an GPIO configuration
  416. * are one of the SFN (special function) modes. */
  417. static inline int is_sfn(unsigned long con)
  418. {
  419. return (con == 2 || con == 3);
  420. }
  421. /* Test if the given masked+shifted GPIO configuration is an input */
  422. static inline int is_in(unsigned long con)
  423. {
  424. return con == 0;
  425. }
  426. /* Test if the given masked+shifted GPIO configuration is an output */
  427. static inline int is_out(unsigned long con)
  428. {
  429. return con == 1;
  430. }
  431. /* s3c2410_pm_restore_gpio()
  432. *
  433. * Restore one of the GPIO banks that was saved during suspend. This is
  434. * not as simple as once thought, due to the possibility of glitches
  435. * from the order that the CON and DAT registers are set in.
  436. *
  437. * The three states the pin can be are {IN,OUT,SFN} which gives us 9
  438. * combinations of changes to check. Three of these, if the pin stays
  439. * in the same configuration can be discounted. This leaves us with
  440. * the following:
  441. *
  442. * { IN => OUT } Change DAT first
  443. * { IN => SFN } Change CON first
  444. * { OUT => SFN } Change CON first, so new data will not glitch
  445. * { OUT => IN } Change CON first, so new data will not glitch
  446. * { SFN => IN } Change CON first
  447. * { SFN => OUT } Change DAT first, so new data will not glitch [1]
  448. *
  449. * We do not currently deal with the UP registers as these control
  450. * weak resistors, so a small delay in change should not need to bring
  451. * these into the calculations.
  452. *
  453. * [1] this assumes that writing to a pin DAT whilst in SFN will set the
  454. * state for when it is next output.
  455. */
  456. static void s3c2410_pm_restore_gpio(int index, struct gpio_sleep *gps)
  457. {
  458. void __iomem *base = gps->base;
  459. unsigned long gps_gpcon = gps->gpcon;
  460. unsigned long gps_gpdat = gps->gpdat;
  461. unsigned long old_gpcon;
  462. unsigned long old_gpdat;
  463. unsigned long old_gpup = 0x0;
  464. unsigned long gpcon;
  465. int nr;
  466. old_gpcon = __raw_readl(base + OFFS_CON);
  467. old_gpdat = __raw_readl(base + OFFS_DAT);
  468. if (base == S3C2410_GPACON) {
  469. /* GPACON only has one bit per control / data and no PULLUPs.
  470. * GPACON[x] = 0 => Output, 1 => SFN */
  471. /* first set all SFN bits to SFN */
  472. gpcon = old_gpcon | gps->gpcon;
  473. __raw_writel(gpcon, base + OFFS_CON);
  474. /* now set all the other bits */
  475. __raw_writel(gps_gpdat, base + OFFS_DAT);
  476. __raw_writel(gps_gpcon, base + OFFS_CON);
  477. } else {
  478. unsigned long old, new, mask;
  479. unsigned long change_mask = 0x0;
  480. old_gpup = __raw_readl(base + OFFS_UP);
  481. /* Create a change_mask of all the items that need to have
  482. * their CON value changed before their DAT value, so that
  483. * we minimise the work between the two settings.
  484. */
  485. for (nr = 0, mask = 0x03; nr < 32; nr += 2, mask <<= 2) {
  486. old = (old_gpcon & mask) >> nr;
  487. new = (gps_gpcon & mask) >> nr;
  488. /* If there is no change, then skip */
  489. if (old == new)
  490. continue;
  491. /* If both are special function, then skip */
  492. if (is_sfn(old) && is_sfn(new))
  493. continue;
  494. /* Change is IN => OUT, do not change now */
  495. if (is_in(old) && is_out(new))
  496. continue;
  497. /* Change is SFN => OUT, do not change now */
  498. if (is_sfn(old) && is_out(new))
  499. continue;
  500. /* We should now be at the case of IN=>SFN,
  501. * OUT=>SFN, OUT=>IN, SFN=>IN. */
  502. change_mask |= mask;
  503. }
  504. /* Write the new CON settings */
  505. gpcon = old_gpcon & ~change_mask;
  506. gpcon |= gps_gpcon & change_mask;
  507. __raw_writel(gpcon, base + OFFS_CON);
  508. /* Now change any items that require DAT,CON */
  509. __raw_writel(gps_gpdat, base + OFFS_DAT);
  510. __raw_writel(gps_gpcon, base + OFFS_CON);
  511. __raw_writel(gps->gpup, base + OFFS_UP);
  512. }
  513. DBG("GPIO[%d] CON %08lx => %08lx, DAT %08lx => %08lx\n",
  514. index, old_gpcon, gps_gpcon, old_gpdat, gps_gpdat);
  515. }
  516. /** s3c2410_pm_restore_gpios()
  517. *
  518. * Restore the state of the GPIOs
  519. */
  520. static void s3c2410_pm_restore_gpios(void)
  521. {
  522. struct gpio_sleep *gps = gpio_save;
  523. int gpio;
  524. for (gpio = 0; gpio < ARRAY_SIZE(gpio_save); gpio++, gps++) {
  525. s3c2410_pm_restore_gpio(gpio, gps);
  526. }
  527. }
  528. void (*pm_cpu_prep)(void);
  529. void (*pm_cpu_sleep)(void);
  530. #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
  531. /* s3c2410_pm_enter
  532. *
  533. * central control for sleep/resume process
  534. */
  535. static int s3c2410_pm_enter(suspend_state_t state)
  536. {
  537. unsigned long regs_save[16];
  538. /* ensure the debug is initialised (if enabled) */
  539. s3c2410_pm_debug_init();
  540. DBG("s3c2410_pm_enter(%d)\n", state);
  541. if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
  542. printk(KERN_ERR PFX "error: no cpu sleep functions set\n");
  543. return -EINVAL;
  544. }
  545. /* check if we have anything to wake-up with... bad things seem
  546. * to happen if you suspend with no wakeup (system will often
  547. * require a full power-cycle)
  548. */
  549. if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
  550. !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
  551. printk(KERN_ERR PFX "No sources enabled for wake-up!\n");
  552. printk(KERN_ERR PFX "Aborting sleep\n");
  553. return -EINVAL;
  554. }
  555. /* prepare check area if configured */
  556. s3c2410_pm_check_prepare();
  557. /* store the physical address of the register recovery block */
  558. s3c2410_sleep_save_phys = virt_to_phys(regs_save);
  559. DBG("s3c2410_sleep_save_phys=0x%08lx\n", s3c2410_sleep_save_phys);
  560. /* save all necessary core registers not covered by the drivers */
  561. s3c2410_pm_save_gpios();
  562. s3c2410_pm_do_save(misc_save, ARRAY_SIZE(misc_save));
  563. s3c2410_pm_do_save(core_save, ARRAY_SIZE(core_save));
  564. s3c2410_pm_do_save(uart_save, ARRAY_SIZE(uart_save));
  565. /* set the irq configuration for wake */
  566. s3c2410_pm_configure_extint();
  567. DBG("sleep: irq wakeup masks: %08lx,%08lx\n",
  568. s3c_irqwake_intmask, s3c_irqwake_eintmask);
  569. __raw_writel(s3c_irqwake_intmask, S3C2410_INTMSK);
  570. __raw_writel(s3c_irqwake_eintmask, S3C2410_EINTMASK);
  571. /* ack any outstanding external interrupts before we go to sleep */
  572. __raw_writel(__raw_readl(S3C2410_EINTPEND), S3C2410_EINTPEND);
  573. __raw_writel(__raw_readl(S3C2410_INTPND), S3C2410_INTPND);
  574. __raw_writel(__raw_readl(S3C2410_SRCPND), S3C2410_SRCPND);
  575. /* call cpu specific preparation */
  576. pm_cpu_prep();
  577. /* flush cache back to ram */
  578. flush_cache_all();
  579. s3c2410_pm_check_store();
  580. /* send the cpu to sleep... */
  581. __raw_writel(0x00, S3C2410_CLKCON); /* turn off clocks over sleep */
  582. /* s3c2410_cpu_save will also act as our return point from when
  583. * we resume as it saves its own register state, so use the return
  584. * code to differentiate return from save and return from sleep */
  585. if (s3c2410_cpu_save(regs_save) == 0) {
  586. flush_cache_all();
  587. pm_cpu_sleep();
  588. }
  589. /* restore the cpu state */
  590. cpu_init();
  591. /* restore the system state */
  592. s3c2410_pm_do_restore_core(core_save, ARRAY_SIZE(core_save));
  593. s3c2410_pm_do_restore(misc_save, ARRAY_SIZE(misc_save));
  594. s3c2410_pm_do_restore(uart_save, ARRAY_SIZE(uart_save));
  595. s3c2410_pm_restore_gpios();
  596. s3c2410_pm_debug_init();
  597. /* check what irq (if any) restored the system */
  598. DBG("post sleep: IRQs 0x%08x, 0x%08x\n",
  599. __raw_readl(S3C2410_SRCPND),
  600. __raw_readl(S3C2410_EINTPEND));
  601. s3c2410_pm_show_resume_irqs(IRQ_EINT0, __raw_readl(S3C2410_SRCPND),
  602. s3c_irqwake_intmask);
  603. s3c2410_pm_show_resume_irqs(IRQ_EINT4-4, __raw_readl(S3C2410_EINTPEND),
  604. s3c_irqwake_eintmask);
  605. DBG("post sleep, preparing to return\n");
  606. s3c2410_pm_check_restore();
  607. /* ok, let's return from sleep */
  608. DBG("S3C2410 PM Resume (post-restore)\n");
  609. return 0;
  610. }
  611. static struct platform_suspend_ops s3c2410_pm_ops = {
  612. .enter = s3c2410_pm_enter,
  613. .valid = suspend_valid_only_mem,
  614. };
  615. /* s3c2410_pm_init
  616. *
  617. * Attach the power management functions. This should be called
  618. * from the board specific initialisation if the board supports
  619. * it.
  620. */
  621. int __init s3c2410_pm_init(void)
  622. {
  623. printk("S3C2410 Power Management, (c) 2004 Simtec Electronics\n");
  624. suspend_set_ops(&s3c2410_pm_ops);
  625. return 0;
  626. }