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 <linux/serial_core.h>
  37. #include <asm/cacheflush.h>
  38. #include <asm/hardware.h>
  39. #include <asm/io.h>
  40. #include <asm/arch/regs-serial.h>
  41. #include <asm/arch/regs-clock.h>
  42. #include <asm/arch/regs-gpio.h>
  43. #include <asm/arch/regs-mem.h>
  44. #include <asm/arch/regs-irq.h>
  45. #include <asm/mach/time.h>
  46. #include "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 sleep_save gpio_save[] = {
  74. SAVE_ITEM(S3C2410_GPACON),
  75. SAVE_ITEM(S3C2410_GPADAT),
  76. SAVE_ITEM(S3C2410_GPBCON),
  77. SAVE_ITEM(S3C2410_GPBDAT),
  78. SAVE_ITEM(S3C2410_GPBUP),
  79. SAVE_ITEM(S3C2410_GPCCON),
  80. SAVE_ITEM(S3C2410_GPCDAT),
  81. SAVE_ITEM(S3C2410_GPCUP),
  82. SAVE_ITEM(S3C2410_GPDCON),
  83. SAVE_ITEM(S3C2410_GPDDAT),
  84. SAVE_ITEM(S3C2410_GPDUP),
  85. SAVE_ITEM(S3C2410_GPECON),
  86. SAVE_ITEM(S3C2410_GPEDAT),
  87. SAVE_ITEM(S3C2410_GPEUP),
  88. SAVE_ITEM(S3C2410_GPFCON),
  89. SAVE_ITEM(S3C2410_GPFDAT),
  90. SAVE_ITEM(S3C2410_GPFUP),
  91. SAVE_ITEM(S3C2410_GPGCON),
  92. SAVE_ITEM(S3C2410_GPGDAT),
  93. SAVE_ITEM(S3C2410_GPGUP),
  94. SAVE_ITEM(S3C2410_GPHCON),
  95. SAVE_ITEM(S3C2410_GPHDAT),
  96. SAVE_ITEM(S3C2410_GPHUP),
  97. SAVE_ITEM(S3C2410_DCLKCON),
  98. };
  99. #ifdef CONFIG_S3C2410_PM_DEBUG
  100. #define SAVE_UART(va) \
  101. SAVE_ITEM((va) + S3C2410_ULCON), \
  102. SAVE_ITEM((va) + S3C2410_UCON), \
  103. SAVE_ITEM((va) + S3C2410_UFCON), \
  104. SAVE_ITEM((va) + S3C2410_UMCON), \
  105. SAVE_ITEM((va) + S3C2410_UBRDIV)
  106. static struct sleep_save uart_save[] = {
  107. SAVE_UART(S3C24XX_VA_UART0),
  108. SAVE_UART(S3C24XX_VA_UART1),
  109. #ifndef CONFIG_CPU_S3C2400
  110. SAVE_UART(S3C24XX_VA_UART2),
  111. #endif
  112. };
  113. /* debug
  114. *
  115. * we send the debug to printascii() to allow it to be seen if the
  116. * system never wakes up from the sleep
  117. */
  118. extern void printascii(const char *);
  119. void pm_dbg(const char *fmt, ...)
  120. {
  121. va_list va;
  122. char buff[256];
  123. va_start(va, fmt);
  124. vsprintf(buff, fmt, va);
  125. va_end(va);
  126. printascii(buff);
  127. }
  128. static void s3c2410_pm_debug_init(void)
  129. {
  130. unsigned long tmp = __raw_readl(S3C2410_CLKCON);
  131. /* re-start uart clocks */
  132. tmp |= S3C2410_CLKCON_UART0;
  133. tmp |= S3C2410_CLKCON_UART1;
  134. tmp |= S3C2410_CLKCON_UART2;
  135. __raw_writel(tmp, S3C2410_CLKCON);
  136. udelay(10);
  137. }
  138. #define DBG(fmt...) pm_dbg(fmt)
  139. #else
  140. #define DBG(fmt...) printk(KERN_DEBUG fmt)
  141. #define s3c2410_pm_debug_init() do { } while(0)
  142. static struct sleep_save uart_save[] = {};
  143. #endif
  144. #if defined(CONFIG_S3C2410_PM_CHECK) && CONFIG_S3C2410_PM_CHECK_CHUNKSIZE != 0
  145. /* suspend checking code...
  146. *
  147. * this next area does a set of crc checks over all the installed
  148. * memory, so the system can verify if the resume was ok.
  149. *
  150. * CONFIG_S3C2410_PM_CHECK_CHUNKSIZE defines the block-size for the CRC,
  151. * increasing it will mean that the area corrupted will be less easy to spot,
  152. * and reducing the size will cause the CRC save area to grow
  153. */
  154. #define CHECK_CHUNKSIZE (CONFIG_S3C2410_PM_CHECK_CHUNKSIZE * 1024)
  155. static u32 crc_size; /* size needed for the crc block */
  156. static u32 *crcs; /* allocated over suspend/resume */
  157. typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
  158. /* s3c2410_pm_run_res
  159. *
  160. * go thorugh the given resource list, and look for system ram
  161. */
  162. static void s3c2410_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg)
  163. {
  164. while (ptr != NULL) {
  165. if (ptr->child != NULL)
  166. s3c2410_pm_run_res(ptr->child, fn, arg);
  167. if ((ptr->flags & IORESOURCE_MEM) &&
  168. strcmp(ptr->name, "System RAM") == 0) {
  169. DBG("Found system RAM at %08lx..%08lx\n",
  170. ptr->start, ptr->end);
  171. arg = (fn)(ptr, arg);
  172. }
  173. ptr = ptr->sibling;
  174. }
  175. }
  176. static void s3c2410_pm_run_sysram(run_fn_t fn, u32 *arg)
  177. {
  178. s3c2410_pm_run_res(&iomem_resource, fn, arg);
  179. }
  180. static u32 *s3c2410_pm_countram(struct resource *res, u32 *val)
  181. {
  182. u32 size = (u32)(res->end - res->start)+1;
  183. size += CHECK_CHUNKSIZE-1;
  184. size /= CHECK_CHUNKSIZE;
  185. DBG("Area %08lx..%08lx, %d blocks\n", res->start, res->end, size);
  186. *val += size * sizeof(u32);
  187. return val;
  188. }
  189. /* s3c2410_pm_prepare_check
  190. *
  191. * prepare the necessary information for creating the CRCs. This
  192. * must be done before the final save, as it will require memory
  193. * allocating, and thus touching bits of the kernel we do not
  194. * know about.
  195. */
  196. static void s3c2410_pm_check_prepare(void)
  197. {
  198. crc_size = 0;
  199. s3c2410_pm_run_sysram(s3c2410_pm_countram, &crc_size);
  200. DBG("s3c2410_pm_prepare_check: %u checks needed\n", crc_size);
  201. crcs = kmalloc(crc_size+4, GFP_KERNEL);
  202. if (crcs == NULL)
  203. printk(KERN_ERR "Cannot allocated CRC save area\n");
  204. }
  205. static u32 *s3c2410_pm_makecheck(struct resource *res, u32 *val)
  206. {
  207. unsigned long addr, left;
  208. for (addr = res->start; addr < res->end;
  209. addr += CHECK_CHUNKSIZE) {
  210. left = res->end - addr;
  211. if (left > CHECK_CHUNKSIZE)
  212. left = CHECK_CHUNKSIZE;
  213. *val = crc32_le(~0, phys_to_virt(addr), left);
  214. val++;
  215. }
  216. return val;
  217. }
  218. /* s3c2410_pm_check_store
  219. *
  220. * compute the CRC values for the memory blocks before the final
  221. * sleep.
  222. */
  223. static void s3c2410_pm_check_store(void)
  224. {
  225. if (crcs != NULL)
  226. s3c2410_pm_run_sysram(s3c2410_pm_makecheck, crcs);
  227. }
  228. /* in_region
  229. *
  230. * return TRUE if the area defined by ptr..ptr+size contatins the
  231. * what..what+whatsz
  232. */
  233. static inline int in_region(void *ptr, int size, void *what, size_t whatsz)
  234. {
  235. if ((what+whatsz) < ptr)
  236. return 0;
  237. if (what > (ptr+size))
  238. return 0;
  239. return 1;
  240. }
  241. static u32 *s3c2410_pm_runcheck(struct resource *res, u32 *val)
  242. {
  243. void *save_at = phys_to_virt(s3c2410_sleep_save_phys);
  244. unsigned long addr;
  245. unsigned long left;
  246. void *ptr;
  247. u32 calc;
  248. for (addr = res->start; addr < res->end;
  249. addr += CHECK_CHUNKSIZE) {
  250. left = res->end - addr;
  251. if (left > CHECK_CHUNKSIZE)
  252. left = CHECK_CHUNKSIZE;
  253. ptr = phys_to_virt(addr);
  254. if (in_region(ptr, left, crcs, crc_size)) {
  255. DBG("skipping %08lx, has crc block in\n", addr);
  256. goto skip_check;
  257. }
  258. if (in_region(ptr, left, save_at, 32*4 )) {
  259. DBG("skipping %08lx, has save block in\n", addr);
  260. goto skip_check;
  261. }
  262. /* calculate and check the checksum */
  263. calc = crc32_le(~0, ptr, left);
  264. if (calc != *val) {
  265. printk(KERN_ERR PFX "Restore CRC error at "
  266. "%08lx (%08x vs %08x)\n", addr, calc, *val);
  267. DBG("Restore CRC error at %08lx (%08x vs %08x)\n",
  268. addr, calc, *val);
  269. }
  270. skip_check:
  271. val++;
  272. }
  273. return val;
  274. }
  275. /* s3c2410_pm_check_restore
  276. *
  277. * check the CRCs after the restore event and free the memory used
  278. * to hold them
  279. */
  280. static void s3c2410_pm_check_restore(void)
  281. {
  282. if (crcs != NULL) {
  283. s3c2410_pm_run_sysram(s3c2410_pm_runcheck, crcs);
  284. kfree(crcs);
  285. crcs = NULL;
  286. }
  287. }
  288. #else
  289. #define s3c2410_pm_check_prepare() do { } while(0)
  290. #define s3c2410_pm_check_restore() do { } while(0)
  291. #define s3c2410_pm_check_store() do { } while(0)
  292. #endif
  293. /* helper functions to save and restore register state */
  294. void s3c2410_pm_do_save(struct sleep_save *ptr, int count)
  295. {
  296. for (; count > 0; count--, ptr++) {
  297. ptr->val = __raw_readl(ptr->reg);
  298. DBG("saved %p value %08lx\n", ptr->reg, ptr->val);
  299. }
  300. }
  301. /* s3c2410_pm_do_restore
  302. *
  303. * restore the system from the given list of saved registers
  304. *
  305. * Note, we do not use DBG() in here, as the system may not have
  306. * restore the UARTs state yet
  307. */
  308. void s3c2410_pm_do_restore(struct sleep_save *ptr, int count)
  309. {
  310. for (; count > 0; count--, ptr++) {
  311. printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
  312. ptr->reg, ptr->val, __raw_readl(ptr->reg));
  313. __raw_writel(ptr->val, ptr->reg);
  314. }
  315. }
  316. /* s3c2410_pm_do_restore_core
  317. *
  318. * similar to s3c2410_pm_do_restore_core
  319. *
  320. * WARNING: Do not put any debug in here that may effect memory or use
  321. * peripherals, as things may be changing!
  322. */
  323. static void s3c2410_pm_do_restore_core(struct sleep_save *ptr, int count)
  324. {
  325. for (; count > 0; count--, ptr++) {
  326. __raw_writel(ptr->val, ptr->reg);
  327. }
  328. }
  329. /* s3c2410_pm_show_resume_irqs
  330. *
  331. * print any IRQs asserted at resume time (ie, we woke from)
  332. */
  333. static void s3c2410_pm_show_resume_irqs(int start, unsigned long which,
  334. unsigned long mask)
  335. {
  336. int i;
  337. which &= ~mask;
  338. for (i = 0; i <= 31; i++) {
  339. if ((which) & (1L<<i)) {
  340. DBG("IRQ %d asserted at resume\n", start+i);
  341. }
  342. }
  343. }
  344. /* s3c2410_pm_check_resume_pin
  345. *
  346. * check to see if the pin is configured correctly for sleep mode, and
  347. * make any necessary adjustments if it is not
  348. */
  349. static void s3c2410_pm_check_resume_pin(unsigned int pin, unsigned int irqoffs)
  350. {
  351. unsigned long irqstate;
  352. unsigned long pinstate;
  353. int irq = s3c2410_gpio_getirq(pin);
  354. if (irqoffs < 4)
  355. irqstate = s3c_irqwake_intmask & (1L<<irqoffs);
  356. else
  357. irqstate = s3c_irqwake_eintmask & (1L<<irqoffs);
  358. pinstate = s3c2410_gpio_getcfg(pin);
  359. if (!irqstate) {
  360. if (pinstate == S3C2410_GPIO_IRQ)
  361. DBG("Leaving IRQ %d (pin %d) enabled\n", irq, pin);
  362. } else {
  363. if (pinstate == S3C2410_GPIO_IRQ) {
  364. DBG("Disabling IRQ %d (pin %d)\n", irq, pin);
  365. s3c2410_gpio_cfgpin(pin, S3C2410_GPIO_INPUT);
  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. }