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