pm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /* linux/arch/arm/mach-s3c2410/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 <asm/cacheflush.h>
  37. #include <asm/hardware.h>
  38. #include <asm/io.h>
  39. #include <asm/arch/regs-serial.h>
  40. #include <asm/arch/regs-clock.h>
  41. #include <asm/arch/regs-gpio.h>
  42. #include <asm/arch/regs-mem.h>
  43. #include <asm/arch/regs-irq.h>
  44. #include <asm/mach/time.h>
  45. #include "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. SAVE_ITEM(S3C2410_CLKDIVN),
  67. SAVE_ITEM(S3C2410_MPLLCON),
  68. SAVE_ITEM(S3C2410_UPLLCON),
  69. SAVE_ITEM(S3C2410_CLKSLOW),
  70. SAVE_ITEM(S3C2410_REFRESH),
  71. };
  72. static struct sleep_save gpio_save[] = {
  73. SAVE_ITEM(S3C2410_GPACON),
  74. SAVE_ITEM(S3C2410_GPADAT),
  75. SAVE_ITEM(S3C2410_GPBCON),
  76. SAVE_ITEM(S3C2410_GPBDAT),
  77. SAVE_ITEM(S3C2410_GPBUP),
  78. SAVE_ITEM(S3C2410_GPCCON),
  79. SAVE_ITEM(S3C2410_GPCDAT),
  80. SAVE_ITEM(S3C2410_GPCUP),
  81. SAVE_ITEM(S3C2410_GPDCON),
  82. SAVE_ITEM(S3C2410_GPDDAT),
  83. SAVE_ITEM(S3C2410_GPDUP),
  84. SAVE_ITEM(S3C2410_GPECON),
  85. SAVE_ITEM(S3C2410_GPEDAT),
  86. SAVE_ITEM(S3C2410_GPEUP),
  87. SAVE_ITEM(S3C2410_GPFCON),
  88. SAVE_ITEM(S3C2410_GPFDAT),
  89. SAVE_ITEM(S3C2410_GPFUP),
  90. SAVE_ITEM(S3C2410_GPGCON),
  91. SAVE_ITEM(S3C2410_GPGDAT),
  92. SAVE_ITEM(S3C2410_GPGUP),
  93. SAVE_ITEM(S3C2410_GPHCON),
  94. SAVE_ITEM(S3C2410_GPHDAT),
  95. SAVE_ITEM(S3C2410_GPHUP),
  96. SAVE_ITEM(S3C2410_DCLKCON),
  97. };
  98. #ifdef CONFIG_S3C2410_PM_DEBUG
  99. #define SAVE_UART(va) \
  100. SAVE_ITEM((va) + S3C2410_ULCON), \
  101. SAVE_ITEM((va) + S3C2410_UCON), \
  102. SAVE_ITEM((va) + S3C2410_UFCON), \
  103. SAVE_ITEM((va) + S3C2410_UMCON), \
  104. SAVE_ITEM((va) + S3C2410_UBRDIV)
  105. static struct sleep_save uart_save[] = {
  106. SAVE_UART(S3C24XX_VA_UART0),
  107. SAVE_UART(S3C24XX_VA_UART1),
  108. #ifndef CONFIG_CPU_S3C2400
  109. SAVE_UART(S3C24XX_VA_UART2),
  110. #endif
  111. };
  112. /* debug
  113. *
  114. * we send the debug to printascii() to allow it to be seen if the
  115. * system never wakes up from the sleep
  116. */
  117. extern void printascii(const char *);
  118. void pm_dbg(const char *fmt, ...)
  119. {
  120. va_list va;
  121. char buff[256];
  122. va_start(va, fmt);
  123. vsprintf(buff, fmt, va);
  124. va_end(va);
  125. printascii(buff);
  126. }
  127. static void s3c2410_pm_debug_init(void)
  128. {
  129. unsigned long tmp = __raw_readl(S3C2410_CLKCON);
  130. /* re-start uart clocks */
  131. tmp |= S3C2410_CLKCON_UART0;
  132. tmp |= S3C2410_CLKCON_UART1;
  133. tmp |= S3C2410_CLKCON_UART2;
  134. __raw_writel(tmp, S3C2410_CLKCON);
  135. udelay(10);
  136. }
  137. #define DBG(fmt...) pm_dbg(fmt)
  138. #else
  139. #define DBG(fmt...) printk(KERN_DEBUG fmt)
  140. #define s3c2410_pm_debug_init() do { } while(0)
  141. static struct sleep_save uart_save[] = {};
  142. #endif
  143. #if defined(CONFIG_S3C2410_PM_CHECK) && CONFIG_S3C2410_PM_CHECK_CHUNKSIZE != 0
  144. /* suspend checking code...
  145. *
  146. * this next area does a set of crc checks over all the installed
  147. * memory, so the system can verify if the resume was ok.
  148. *
  149. * CONFIG_S3C2410_PM_CHECK_CHUNKSIZE defines the block-size for the CRC,
  150. * increasing it will mean that the area corrupted will be less easy to spot,
  151. * and reducing the size will cause the CRC save area to grow
  152. */
  153. #define CHECK_CHUNKSIZE (CONFIG_S3C2410_PM_CHECK_CHUNKSIZE * 1024)
  154. static u32 crc_size; /* size needed for the crc block */
  155. static u32 *crcs; /* allocated over suspend/resume */
  156. typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
  157. /* s3c2410_pm_run_res
  158. *
  159. * go thorugh the given resource list, and look for system ram
  160. */
  161. static void s3c2410_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg)
  162. {
  163. while (ptr != NULL) {
  164. if (ptr->child != NULL)
  165. s3c2410_pm_run_res(ptr->child, fn, arg);
  166. if ((ptr->flags & IORESOURCE_MEM) &&
  167. strcmp(ptr->name, "System RAM") == 0) {
  168. DBG("Found system RAM at %08lx..%08lx\n",
  169. ptr->start, ptr->end);
  170. arg = (fn)(ptr, arg);
  171. }
  172. ptr = ptr->sibling;
  173. }
  174. }
  175. static void s3c2410_pm_run_sysram(run_fn_t fn, u32 *arg)
  176. {
  177. s3c2410_pm_run_res(&iomem_resource, fn, arg);
  178. }
  179. static u32 *s3c2410_pm_countram(struct resource *res, u32 *val)
  180. {
  181. u32 size = (u32)(res->end - res->start)+1;
  182. size += CHECK_CHUNKSIZE-1;
  183. size /= CHECK_CHUNKSIZE;
  184. DBG("Area %08lx..%08lx, %d blocks\n", res->start, res->end, size);
  185. *val += size * sizeof(u32);
  186. return val;
  187. }
  188. /* s3c2410_pm_prepare_check
  189. *
  190. * prepare the necessary information for creating the CRCs. This
  191. * must be done before the final save, as it will require memory
  192. * allocating, and thus touching bits of the kernel we do not
  193. * know about.
  194. */
  195. static void s3c2410_pm_check_prepare(void)
  196. {
  197. crc_size = 0;
  198. s3c2410_pm_run_sysram(s3c2410_pm_countram, &crc_size);
  199. DBG("s3c2410_pm_prepare_check: %u checks needed\n", crc_size);
  200. crcs = kmalloc(crc_size+4, GFP_KERNEL);
  201. if (crcs == NULL)
  202. printk(KERN_ERR "Cannot allocated CRC save area\n");
  203. }
  204. static u32 *s3c2410_pm_makecheck(struct resource *res, u32 *val)
  205. {
  206. unsigned long addr, left;
  207. for (addr = res->start; addr < res->end;
  208. addr += CHECK_CHUNKSIZE) {
  209. left = res->end - addr;
  210. if (left > CHECK_CHUNKSIZE)
  211. left = CHECK_CHUNKSIZE;
  212. *val = crc32_le(~0, phys_to_virt(addr), left);
  213. val++;
  214. }
  215. return val;
  216. }
  217. /* s3c2410_pm_check_store
  218. *
  219. * compute the CRC values for the memory blocks before the final
  220. * sleep.
  221. */
  222. static void s3c2410_pm_check_store(void)
  223. {
  224. if (crcs != NULL)
  225. s3c2410_pm_run_sysram(s3c2410_pm_makecheck, crcs);
  226. }
  227. /* in_region
  228. *
  229. * return TRUE if the area defined by ptr..ptr+size contatins the
  230. * what..what+whatsz
  231. */
  232. static inline int in_region(void *ptr, int size, void *what, size_t whatsz)
  233. {
  234. if ((what+whatsz) < ptr)
  235. return 0;
  236. if (what > (ptr+size))
  237. return 0;
  238. return 1;
  239. }
  240. static u32 *s3c2410_pm_runcheck(struct resource *res, u32 *val)
  241. {
  242. void *save_at = phys_to_virt(s3c2410_sleep_save_phys);
  243. unsigned long addr;
  244. unsigned long left;
  245. void *ptr;
  246. u32 calc;
  247. for (addr = res->start; addr < res->end;
  248. addr += CHECK_CHUNKSIZE) {
  249. left = res->end - addr;
  250. if (left > CHECK_CHUNKSIZE)
  251. left = CHECK_CHUNKSIZE;
  252. ptr = phys_to_virt(addr);
  253. if (in_region(ptr, left, crcs, crc_size)) {
  254. DBG("skipping %08lx, has crc block in\n", addr);
  255. goto skip_check;
  256. }
  257. if (in_region(ptr, left, save_at, 32*4 )) {
  258. DBG("skipping %08lx, has save block in\n", addr);
  259. goto skip_check;
  260. }
  261. /* calculate and check the checksum */
  262. calc = crc32_le(~0, ptr, left);
  263. if (calc != *val) {
  264. printk(KERN_ERR PFX "Restore CRC error at "
  265. "%08lx (%08x vs %08x)\n", addr, calc, *val);
  266. DBG("Restore CRC error at %08lx (%08x vs %08x)\n",
  267. addr, calc, *val);
  268. }
  269. skip_check:
  270. val++;
  271. }
  272. return val;
  273. }
  274. /* s3c2410_pm_check_restore
  275. *
  276. * check the CRCs after the restore event and free the memory used
  277. * to hold them
  278. */
  279. static void s3c2410_pm_check_restore(void)
  280. {
  281. if (crcs != NULL) {
  282. s3c2410_pm_run_sysram(s3c2410_pm_runcheck, crcs);
  283. kfree(crcs);
  284. crcs = NULL;
  285. }
  286. }
  287. #else
  288. #define s3c2410_pm_check_prepare() do { } while(0)
  289. #define s3c2410_pm_check_restore() do { } while(0)
  290. #define s3c2410_pm_check_store() do { } while(0)
  291. #endif
  292. /* helper functions to save and restore register state */
  293. void s3c2410_pm_do_save(struct sleep_save *ptr, int count)
  294. {
  295. for (; count > 0; count--, ptr++) {
  296. ptr->val = __raw_readl(ptr->reg);
  297. DBG("saved %p value %08lx\n", ptr->reg, ptr->val);
  298. }
  299. }
  300. /* s3c2410_pm_do_restore
  301. *
  302. * restore the system from the given list of saved registers
  303. *
  304. * Note, we do not use DBG() in here, as the system may not have
  305. * restore the UARTs state yet
  306. */
  307. void s3c2410_pm_do_restore(struct sleep_save *ptr, int count)
  308. {
  309. for (; count > 0; count--, ptr++) {
  310. printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
  311. ptr->reg, ptr->val, __raw_readl(ptr->reg));
  312. __raw_writel(ptr->val, ptr->reg);
  313. }
  314. }
  315. /* s3c2410_pm_do_restore_core
  316. *
  317. * similar to s3c2410_pm_do_restore_core
  318. *
  319. * WARNING: Do not put any debug in here that may effect memory or use
  320. * peripherals, as things may be changing!
  321. */
  322. static void s3c2410_pm_do_restore_core(struct sleep_save *ptr, int count)
  323. {
  324. for (; count > 0; count--, ptr++) {
  325. __raw_writel(ptr->val, ptr->reg);
  326. }
  327. }
  328. /* s3c2410_pm_show_resume_irqs
  329. *
  330. * print any IRQs asserted at resume time (ie, we woke from)
  331. */
  332. static void s3c2410_pm_show_resume_irqs(int start, unsigned long which,
  333. unsigned long mask)
  334. {
  335. int i;
  336. which &= ~mask;
  337. for (i = 0; i <= 31; i++) {
  338. if ((which) & (1L<<i)) {
  339. DBG("IRQ %d asserted at resume\n", start+i);
  340. }
  341. }
  342. }
  343. /* s3c2410_pm_check_resume_pin
  344. *
  345. * check to see if the pin is configured correctly for sleep mode, and
  346. * make any necessary adjustments if it is not
  347. */
  348. static void s3c2410_pm_check_resume_pin(unsigned int pin, unsigned int irqoffs)
  349. {
  350. unsigned long irqstate;
  351. unsigned long pinstate;
  352. int irq = s3c2410_gpio_getirq(pin);
  353. if (irqoffs < 4)
  354. irqstate = s3c_irqwake_intmask & (1L<<irqoffs);
  355. else
  356. irqstate = s3c_irqwake_eintmask & (1L<<irqoffs);
  357. pinstate = s3c2410_gpio_getcfg(pin);
  358. pinstate >>= S3C2410_GPIO_OFFSET(pin)*2;
  359. if (!irqstate) {
  360. if (pinstate == 0x02)
  361. DBG("Leaving IRQ %d (pin %d) enabled\n", irq, pin);
  362. } else {
  363. if (pinstate == 0x02) {
  364. DBG("Disabling IRQ %d (pin %d)\n", irq, pin);
  365. s3c2410_gpio_cfgpin(pin, 0x00);
  366. }
  367. }
  368. }
  369. /* s3c2410_pm_configure_extint
  370. *
  371. * configure all external interrupt pins
  372. */
  373. static void s3c2410_pm_configure_extint(void)
  374. {
  375. int pin;
  376. /* for each of the external interrupts (EINT0..EINT15) we
  377. * need to check wether it is an external interrupt source,
  378. * and then configure it as an input if it is not
  379. */
  380. for (pin = S3C2410_GPF0; pin <= S3C2410_GPF7; pin++) {
  381. s3c2410_pm_check_resume_pin(pin, pin - S3C2410_GPF0);
  382. }
  383. for (pin = S3C2410_GPG0; pin <= S3C2410_GPG7; pin++) {
  384. s3c2410_pm_check_resume_pin(pin, (pin - S3C2410_GPG0)+8);
  385. }
  386. }
  387. void (*pm_cpu_prep)(void);
  388. void (*pm_cpu_sleep)(void);
  389. #define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
  390. /* s3c2410_pm_enter
  391. *
  392. * central control for sleep/resume process
  393. */
  394. static int s3c2410_pm_enter(suspend_state_t state)
  395. {
  396. unsigned long regs_save[16];
  397. /* ensure the debug is initialised (if enabled) */
  398. s3c2410_pm_debug_init();
  399. DBG("s3c2410_pm_enter(%d)\n", state);
  400. if (pm_cpu_prep == NULL || pm_cpu_sleep == NULL) {
  401. printk(KERN_ERR PFX "error: no cpu sleep functions set\n");
  402. return -EINVAL;
  403. }
  404. if (state != PM_SUSPEND_MEM) {
  405. printk(KERN_ERR PFX "error: only PM_SUSPEND_MEM supported\n");
  406. return -EINVAL;
  407. }
  408. /* check if we have anything to wake-up with... bad things seem
  409. * to happen if you suspend with no wakeup (system will often
  410. * require a full power-cycle)
  411. */
  412. if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
  413. !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
  414. printk(KERN_ERR PFX "No sources enabled for wake-up!\n");
  415. printk(KERN_ERR PFX "Aborting sleep\n");
  416. return -EINVAL;
  417. }
  418. /* prepare check area if configured */
  419. s3c2410_pm_check_prepare();
  420. /* store the physical address of the register recovery block */
  421. s3c2410_sleep_save_phys = virt_to_phys(regs_save);
  422. DBG("s3c2410_sleep_save_phys=0x%08lx\n", s3c2410_sleep_save_phys);
  423. /* save all necessary core registers not covered by the drivers */
  424. s3c2410_pm_do_save(gpio_save, ARRAY_SIZE(gpio_save));
  425. s3c2410_pm_do_save(core_save, ARRAY_SIZE(core_save));
  426. s3c2410_pm_do_save(uart_save, ARRAY_SIZE(uart_save));
  427. /* set the irq configuration for wake */
  428. s3c2410_pm_configure_extint();
  429. DBG("sleep: irq wakeup masks: %08lx,%08lx\n",
  430. s3c_irqwake_intmask, s3c_irqwake_eintmask);
  431. __raw_writel(s3c_irqwake_intmask, S3C2410_INTMSK);
  432. __raw_writel(s3c_irqwake_eintmask, S3C2410_EINTMASK);
  433. /* ack any outstanding external interrupts before we go to sleep */
  434. __raw_writel(__raw_readl(S3C2410_EINTPEND), S3C2410_EINTPEND);
  435. __raw_writel(__raw_readl(S3C2410_INTPND), S3C2410_INTPND);
  436. __raw_writel(__raw_readl(S3C2410_SRCPND), S3C2410_SRCPND);
  437. /* call cpu specific preperation */
  438. pm_cpu_prep();
  439. /* flush cache back to ram */
  440. flush_cache_all();
  441. s3c2410_pm_check_store();
  442. /* send the cpu to sleep... */
  443. __raw_writel(0x00, S3C2410_CLKCON); /* turn off clocks over sleep */
  444. /* s3c2410_cpu_save will also act as our return point from when
  445. * we resume as it saves its own register state, so use the return
  446. * code to differentiate return from save and return from sleep */
  447. if (s3c2410_cpu_save(regs_save) == 0) {
  448. flush_cache_all();
  449. pm_cpu_sleep();
  450. }
  451. /* restore the cpu state */
  452. cpu_init();
  453. /* restore the system state */
  454. s3c2410_pm_do_restore_core(core_save, ARRAY_SIZE(core_save));
  455. s3c2410_pm_do_restore(gpio_save, ARRAY_SIZE(gpio_save));
  456. s3c2410_pm_do_restore(uart_save, ARRAY_SIZE(uart_save));
  457. s3c2410_pm_debug_init();
  458. /* check what irq (if any) restored the system */
  459. DBG("post sleep: IRQs 0x%08x, 0x%08x\n",
  460. __raw_readl(S3C2410_SRCPND),
  461. __raw_readl(S3C2410_EINTPEND));
  462. s3c2410_pm_show_resume_irqs(IRQ_EINT0, __raw_readl(S3C2410_SRCPND),
  463. s3c_irqwake_intmask);
  464. s3c2410_pm_show_resume_irqs(IRQ_EINT4-4, __raw_readl(S3C2410_EINTPEND),
  465. s3c_irqwake_eintmask);
  466. DBG("post sleep, preparing to return\n");
  467. s3c2410_pm_check_restore();
  468. /* ok, let's return from sleep */
  469. DBG("S3C2410 PM Resume (post-restore)\n");
  470. return 0;
  471. }
  472. /*
  473. * Called after processes are frozen, but before we shut down devices.
  474. */
  475. static int s3c2410_pm_prepare(suspend_state_t state)
  476. {
  477. return 0;
  478. }
  479. /*
  480. * Called after devices are re-setup, but before processes are thawed.
  481. */
  482. static int s3c2410_pm_finish(suspend_state_t state)
  483. {
  484. return 0;
  485. }
  486. /*
  487. * Set to PM_DISK_FIRMWARE so we can quickly veto suspend-to-disk.
  488. */
  489. static struct pm_ops s3c2410_pm_ops = {
  490. .pm_disk_mode = PM_DISK_FIRMWARE,
  491. .prepare = s3c2410_pm_prepare,
  492. .enter = s3c2410_pm_enter,
  493. .finish = s3c2410_pm_finish,
  494. };
  495. /* s3c2410_pm_init
  496. *
  497. * Attach the power management functions. This should be called
  498. * from the board specific initialisation if the board supports
  499. * it.
  500. */
  501. int __init s3c2410_pm_init(void)
  502. {
  503. printk("S3C2410 Power Management, (c) 2004 Simtec Electronics\n");
  504. pm_set_ops(&s3c2410_pm_ops);
  505. return 0;
  506. }