pm.c 19 KB

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