blob: a589fe76d9158d1eedac0ff04ae657ed38f22e53 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* linux/arch/arm/mach-s3c2410/pm.c
2 *
3 * Copyright (c) 2004 Simtec Electronics
4 * Ben Dooks <ben@simtec.co.uk>
5 *
6 * S3C2410 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 * Modifications:
29 * 10-Mar-2005 LCVR Changed S3C2410_VA_UART to S3C24XX_VA_UART
30*/
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/init.h>
33#include <linux/suspend.h>
34#include <linux/errno.h>
35#include <linux/time.h>
36#include <linux/interrupt.h>
37#include <linux/crc32.h>
38#include <linux/ioport.h>
39#include <linux/delay.h>
40
41#include <asm/hardware.h>
42#include <asm/io.h>
43
44#include <asm/arch/regs-serial.h>
45#include <asm/arch/regs-clock.h>
46#include <asm/arch/regs-gpio.h>
47#include <asm/arch/regs-mem.h>
48#include <asm/arch/regs-irq.h>
49
50#include <asm/mach/time.h>
51
52#include "pm.h"
53
54/* for external use */
55
56unsigned long s3c_pm_flags;
57
58/* cache functions from arch/arm/mm/proc-arm920.S */
59
Ben Dooks4833acb2006-06-18 16:21:51 +010060#ifndef CONFIG_CPU_DCACHE_WRITETHROUGH
Linus Torvalds1da177e2005-04-16 15:20:36 -070061extern void arm920_flush_kern_cache_all(void);
Ben Dooks4833acb2006-06-18 16:21:51 +010062#else
63static void arm920_flush_kern_cache_all(void) { }
64#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66#define PFX "s3c24xx-pm: "
67
68static struct sleep_save core_save[] = {
69 SAVE_ITEM(S3C2410_LOCKTIME),
70 SAVE_ITEM(S3C2410_CLKCON),
71
72 /* we restore the timings here, with the proviso that the board
73 * brings the system up in an slower, or equal frequency setting
74 * to the original system.
75 *
76 * if we cannot guarantee this, then things are going to go very
77 * wrong here, as we modify the refresh and both pll settings.
78 */
79
80 SAVE_ITEM(S3C2410_BWSCON),
81 SAVE_ITEM(S3C2410_BANKCON0),
82 SAVE_ITEM(S3C2410_BANKCON1),
83 SAVE_ITEM(S3C2410_BANKCON2),
84 SAVE_ITEM(S3C2410_BANKCON3),
85 SAVE_ITEM(S3C2410_BANKCON4),
86 SAVE_ITEM(S3C2410_BANKCON5),
87
88 SAVE_ITEM(S3C2410_CLKDIVN),
89 SAVE_ITEM(S3C2410_MPLLCON),
90 SAVE_ITEM(S3C2410_UPLLCON),
91 SAVE_ITEM(S3C2410_CLKSLOW),
92 SAVE_ITEM(S3C2410_REFRESH),
93};
94
95/* this lot should be really saved by the IRQ code */
96static struct sleep_save irq_save[] = {
97 SAVE_ITEM(S3C2410_EXTINT0),
98 SAVE_ITEM(S3C2410_EXTINT1),
99 SAVE_ITEM(S3C2410_EXTINT2),
100 SAVE_ITEM(S3C2410_EINFLT0),
101 SAVE_ITEM(S3C2410_EINFLT1),
102 SAVE_ITEM(S3C2410_EINFLT2),
103 SAVE_ITEM(S3C2410_EINFLT3),
104 SAVE_ITEM(S3C2410_EINTMASK),
105 SAVE_ITEM(S3C2410_INTMSK)
106};
107
108static struct sleep_save gpio_save[] = {
109 SAVE_ITEM(S3C2410_GPACON),
110 SAVE_ITEM(S3C2410_GPADAT),
111
112 SAVE_ITEM(S3C2410_GPBCON),
113 SAVE_ITEM(S3C2410_GPBDAT),
114 SAVE_ITEM(S3C2410_GPBUP),
115
116 SAVE_ITEM(S3C2410_GPCCON),
117 SAVE_ITEM(S3C2410_GPCDAT),
118 SAVE_ITEM(S3C2410_GPCUP),
119
120 SAVE_ITEM(S3C2410_GPDCON),
121 SAVE_ITEM(S3C2410_GPDDAT),
122 SAVE_ITEM(S3C2410_GPDUP),
123
124 SAVE_ITEM(S3C2410_GPECON),
125 SAVE_ITEM(S3C2410_GPEDAT),
126 SAVE_ITEM(S3C2410_GPEUP),
127
128 SAVE_ITEM(S3C2410_GPFCON),
129 SAVE_ITEM(S3C2410_GPFDAT),
130 SAVE_ITEM(S3C2410_GPFUP),
131
132 SAVE_ITEM(S3C2410_GPGCON),
133 SAVE_ITEM(S3C2410_GPGDAT),
134 SAVE_ITEM(S3C2410_GPGUP),
135
136 SAVE_ITEM(S3C2410_GPHCON),
137 SAVE_ITEM(S3C2410_GPHDAT),
138 SAVE_ITEM(S3C2410_GPHUP),
139
140 SAVE_ITEM(S3C2410_DCLKCON),
141};
142
143#ifdef CONFIG_S3C2410_PM_DEBUG
144
145#define SAVE_UART(va) \
146 SAVE_ITEM((va) + S3C2410_ULCON), \
147 SAVE_ITEM((va) + S3C2410_UCON), \
148 SAVE_ITEM((va) + S3C2410_UFCON), \
149 SAVE_ITEM((va) + S3C2410_UMCON), \
150 SAVE_ITEM((va) + S3C2410_UBRDIV)
151
152static struct sleep_save uart_save[] = {
153 SAVE_UART(S3C24XX_VA_UART0),
154 SAVE_UART(S3C24XX_VA_UART1),
155#ifndef CONFIG_CPU_S3C2400
156 SAVE_UART(S3C24XX_VA_UART2),
157#endif
158};
159
160/* debug
161 *
162 * we send the debug to printascii() to allow it to be seen if the
163 * system never wakes up from the sleep
164*/
165
166extern void printascii(const char *);
167
168static void pm_dbg(const char *fmt, ...)
169{
170 va_list va;
171 char buff[256];
172
173 va_start(va, fmt);
174 vsprintf(buff, fmt, va);
175 va_end(va);
176
177 printascii(buff);
178}
179
180static void s3c2410_pm_debug_init(void)
181{
182 unsigned long tmp = __raw_readl(S3C2410_CLKCON);
183
184 /* re-start uart clocks */
185 tmp |= S3C2410_CLKCON_UART0;
186 tmp |= S3C2410_CLKCON_UART1;
187 tmp |= S3C2410_CLKCON_UART2;
188
189 __raw_writel(tmp, S3C2410_CLKCON);
190 udelay(10);
191}
192
193#define DBG(fmt...) pm_dbg(fmt)
194#else
195#define DBG(fmt...) printk(KERN_DEBUG fmt)
196
197#define s3c2410_pm_debug_init() do { } while(0)
198
199static struct sleep_save uart_save[] = {};
200#endif
201
202#if defined(CONFIG_S3C2410_PM_CHECK) && CONFIG_S3C2410_PM_CHECK_CHUNKSIZE != 0
203
204/* suspend checking code...
205 *
206 * this next area does a set of crc checks over all the installed
207 * memory, so the system can verify if the resume was ok.
208 *
209 * CONFIG_S3C2410_PM_CHECK_CHUNKSIZE defines the block-size for the CRC,
210 * increasing it will mean that the area corrupted will be less easy to spot,
211 * and reducing the size will cause the CRC save area to grow
212*/
213
214#define CHECK_CHUNKSIZE (CONFIG_S3C2410_PM_CHECK_CHUNKSIZE * 1024)
215
216static u32 crc_size; /* size needed for the crc block */
217static u32 *crcs; /* allocated over suspend/resume */
218
219typedef u32 *(run_fn_t)(struct resource *ptr, u32 *arg);
220
221/* s3c2410_pm_run_res
222 *
223 * go thorugh the given resource list, and look for system ram
224*/
225
226static void s3c2410_pm_run_res(struct resource *ptr, run_fn_t fn, u32 *arg)
227{
228 while (ptr != NULL) {
229 if (ptr->child != NULL)
230 s3c2410_pm_run_res(ptr->child, fn, arg);
231
232 if ((ptr->flags & IORESOURCE_MEM) &&
233 strcmp(ptr->name, "System RAM") == 0) {
234 DBG("Found system RAM at %08lx..%08lx\n",
235 ptr->start, ptr->end);
236 arg = (fn)(ptr, arg);
237 }
238
239 ptr = ptr->sibling;
240 }
241}
242
243static void s3c2410_pm_run_sysram(run_fn_t fn, u32 *arg)
244{
245 s3c2410_pm_run_res(&iomem_resource, fn, arg);
246}
247
248static u32 *s3c2410_pm_countram(struct resource *res, u32 *val)
249{
250 u32 size = (u32)(res->end - res->start)+1;
251
252 size += CHECK_CHUNKSIZE-1;
253 size /= CHECK_CHUNKSIZE;
254
255 DBG("Area %08lx..%08lx, %d blocks\n", res->start, res->end, size);
256
257 *val += size * sizeof(u32);
258 return val;
259}
260
261/* s3c2410_pm_prepare_check
262 *
263 * prepare the necessary information for creating the CRCs. This
264 * must be done before the final save, as it will require memory
265 * allocating, and thus touching bits of the kernel we do not
266 * know about.
267*/
268
269static void s3c2410_pm_check_prepare(void)
270{
271 crc_size = 0;
272
273 s3c2410_pm_run_sysram(s3c2410_pm_countram, &crc_size);
274
275 DBG("s3c2410_pm_prepare_check: %u checks needed\n", crc_size);
276
277 crcs = kmalloc(crc_size+4, GFP_KERNEL);
278 if (crcs == NULL)
279 printk(KERN_ERR "Cannot allocated CRC save area\n");
280}
281
282static u32 *s3c2410_pm_makecheck(struct resource *res, u32 *val)
283{
284 unsigned long addr, left;
285
286 for (addr = res->start; addr < res->end;
287 addr += CHECK_CHUNKSIZE) {
288 left = res->end - addr;
289
290 if (left > CHECK_CHUNKSIZE)
291 left = CHECK_CHUNKSIZE;
292
293 *val = crc32_le(~0, phys_to_virt(addr), left);
294 val++;
295 }
296
297 return val;
298}
299
300/* s3c2410_pm_check_store
301 *
302 * compute the CRC values for the memory blocks before the final
303 * sleep.
304*/
305
306static void s3c2410_pm_check_store(void)
307{
308 if (crcs != NULL)
309 s3c2410_pm_run_sysram(s3c2410_pm_makecheck, crcs);
310}
311
312/* in_region
313 *
314 * return TRUE if the area defined by ptr..ptr+size contatins the
315 * what..what+whatsz
316*/
317
318static inline int in_region(void *ptr, int size, void *what, size_t whatsz)
319{
320 if ((what+whatsz) < ptr)
321 return 0;
322
323 if (what > (ptr+size))
324 return 0;
325
326 return 1;
327}
328
329static u32 *s3c2410_pm_runcheck(struct resource *res, u32 *val)
330{
331 void *save_at = phys_to_virt(s3c2410_sleep_save_phys);
332 unsigned long addr;
333 unsigned long left;
334 void *ptr;
335 u32 calc;
336
337 for (addr = res->start; addr < res->end;
338 addr += CHECK_CHUNKSIZE) {
339 left = res->end - addr;
340
341 if (left > CHECK_CHUNKSIZE)
342 left = CHECK_CHUNKSIZE;
343
344 ptr = phys_to_virt(addr);
345
346 if (in_region(ptr, left, crcs, crc_size)) {
347 DBG("skipping %08lx, has crc block in\n", addr);
348 goto skip_check;
349 }
350
351 if (in_region(ptr, left, save_at, 32*4 )) {
352 DBG("skipping %08lx, has save block in\n", addr);
353 goto skip_check;
354 }
355
356 /* calculate and check the checksum */
357
358 calc = crc32_le(~0, ptr, left);
359 if (calc != *val) {
360 printk(KERN_ERR PFX "Restore CRC error at "
361 "%08lx (%08x vs %08x)\n", addr, calc, *val);
362
363 DBG("Restore CRC error at %08lx (%08x vs %08x)\n",
364 addr, calc, *val);
365 }
366
367 skip_check:
368 val++;
369 }
370
371 return val;
372}
373
374/* s3c2410_pm_check_restore
375 *
376 * check the CRCs after the restore event and free the memory used
377 * to hold them
378*/
379
380static void s3c2410_pm_check_restore(void)
381{
382 if (crcs != NULL) {
383 s3c2410_pm_run_sysram(s3c2410_pm_runcheck, crcs);
384 kfree(crcs);
385 crcs = NULL;
386 }
387}
388
389#else
390
391#define s3c2410_pm_check_prepare() do { } while(0)
392#define s3c2410_pm_check_restore() do { } while(0)
393#define s3c2410_pm_check_store() do { } while(0)
394#endif
395
396/* helper functions to save and restore register state */
397
398void s3c2410_pm_do_save(struct sleep_save *ptr, int count)
399{
400 for (; count > 0; count--, ptr++) {
401 ptr->val = __raw_readl(ptr->reg);
402 DBG("saved %p value %08lx\n", ptr->reg, ptr->val);
403 }
404}
405
406/* s3c2410_pm_do_restore
407 *
408 * restore the system from the given list of saved registers
409 *
410 * Note, we do not use DBG() in here, as the system may not have
411 * restore the UARTs state yet
412*/
413
414void s3c2410_pm_do_restore(struct sleep_save *ptr, int count)
415{
416 for (; count > 0; count--, ptr++) {
417 printk(KERN_DEBUG "restore %p (restore %08lx, was %08x)\n",
418 ptr->reg, ptr->val, __raw_readl(ptr->reg));
419
420 __raw_writel(ptr->val, ptr->reg);
421 }
422}
423
424/* s3c2410_pm_do_restore_core
425 *
426 * similar to s3c2410_pm_do_restore_core
427 *
428 * WARNING: Do not put any debug in here that may effect memory or use
429 * peripherals, as things may be changing!
430*/
431
432static void s3c2410_pm_do_restore_core(struct sleep_save *ptr, int count)
433{
434 for (; count > 0; count--, ptr++) {
435 __raw_writel(ptr->val, ptr->reg);
436 }
437}
438
439/* s3c2410_pm_show_resume_irqs
440 *
441 * print any IRQs asserted at resume time (ie, we woke from)
442*/
443
444static void s3c2410_pm_show_resume_irqs(int start, unsigned long which,
445 unsigned long mask)
446{
447 int i;
448
449 which &= ~mask;
450
451 for (i = 0; i <= 31; i++) {
452 if ((which) & (1L<<i)) {
453 DBG("IRQ %d asserted at resume\n", start+i);
454 }
455 }
456}
457
458/* s3c2410_pm_check_resume_pin
459 *
460 * check to see if the pin is configured correctly for sleep mode, and
461 * make any necessary adjustments if it is not
462*/
463
464static void s3c2410_pm_check_resume_pin(unsigned int pin, unsigned int irqoffs)
465{
466 unsigned long irqstate;
467 unsigned long pinstate;
468 int irq = s3c2410_gpio_getirq(pin);
469
470 if (irqoffs < 4)
471 irqstate = s3c_irqwake_intmask & (1L<<irqoffs);
472 else
473 irqstate = s3c_irqwake_eintmask & (1L<<irqoffs);
474
475 pinstate = s3c2410_gpio_getcfg(pin);
476 pinstate >>= S3C2410_GPIO_OFFSET(pin)*2;
477
478 if (!irqstate) {
479 if (pinstate == 0x02)
480 DBG("Leaving IRQ %d (pin %d) enabled\n", irq, pin);
481 } else {
482 if (pinstate == 0x02) {
483 DBG("Disabling IRQ %d (pin %d)\n", irq, pin);
484 s3c2410_gpio_cfgpin(pin, 0x00);
485 }
486 }
487}
488
489/* s3c2410_pm_configure_extint
490 *
491 * configure all external interrupt pins
492*/
493
494static void s3c2410_pm_configure_extint(void)
495{
496 int pin;
497
498 /* for each of the external interrupts (EINT0..EINT15) we
499 * need to check wether it is an external interrupt source,
500 * and then configure it as an input if it is not
501 */
502
503 for (pin = S3C2410_GPF0; pin <= S3C2410_GPF7; pin++) {
504 s3c2410_pm_check_resume_pin(pin, pin - S3C2410_GPF0);
505 }
506
507 for (pin = S3C2410_GPG0; pin <= S3C2410_GPG7; pin++) {
508 s3c2410_pm_check_resume_pin(pin, (pin - S3C2410_GPG0)+8);
509 }
510}
511
512#define any_allowed(mask, allow) (((mask) & (allow)) != (allow))
513
514/* s3c2410_pm_enter
515 *
516 * central control for sleep/resume process
517*/
518
519static int s3c2410_pm_enter(suspend_state_t state)
520{
521 unsigned long regs_save[16];
522 unsigned long tmp;
523
524 /* ensure the debug is initialised (if enabled) */
525
526 s3c2410_pm_debug_init();
527
528 DBG("s3c2410_pm_enter(%d)\n", state);
529
530 if (state != PM_SUSPEND_MEM) {
531 printk(KERN_ERR PFX "error: only PM_SUSPEND_MEM supported\n");
532 return -EINVAL;
533 }
534
535 /* check if we have anything to wake-up with... bad things seem
536 * to happen if you suspend with no wakeup (system will often
537 * require a full power-cycle)
538 */
539
540 if (!any_allowed(s3c_irqwake_intmask, s3c_irqwake_intallow) &&
541 !any_allowed(s3c_irqwake_eintmask, s3c_irqwake_eintallow)) {
542 printk(KERN_ERR PFX "No sources enabled for wake-up!\n");
543 printk(KERN_ERR PFX "Aborting sleep\n");
544 return -EINVAL;
545 }
546
547 /* prepare check area if configured */
548
549 s3c2410_pm_check_prepare();
550
551 /* store the physical address of the register recovery block */
552
553 s3c2410_sleep_save_phys = virt_to_phys(regs_save);
554
555 DBG("s3c2410_sleep_save_phys=0x%08lx\n", s3c2410_sleep_save_phys);
556
557 /* ensure at least GESTATUS3 has the resume address */
558
559 __raw_writel(virt_to_phys(s3c2410_cpu_resume), S3C2410_GSTATUS3);
560
561 DBG("GSTATUS3 0x%08x\n", __raw_readl(S3C2410_GSTATUS3));
562 DBG("GSTATUS4 0x%08x\n", __raw_readl(S3C2410_GSTATUS4));
563
564 /* save all necessary core registers not covered by the drivers */
565
566 s3c2410_pm_do_save(gpio_save, ARRAY_SIZE(gpio_save));
567 s3c2410_pm_do_save(irq_save, ARRAY_SIZE(irq_save));
568 s3c2410_pm_do_save(core_save, ARRAY_SIZE(core_save));
569 s3c2410_pm_do_save(uart_save, ARRAY_SIZE(uart_save));
570
571 /* set the irq configuration for wake */
572
573 s3c2410_pm_configure_extint();
574
575 DBG("sleep: irq wakeup masks: %08lx,%08lx\n",
576 s3c_irqwake_intmask, s3c_irqwake_eintmask);
577
578 __raw_writel(s3c_irqwake_intmask, S3C2410_INTMSK);
579 __raw_writel(s3c_irqwake_eintmask, S3C2410_EINTMASK);
580
581 /* ack any outstanding external interrupts before we go to sleep */
582
583 __raw_writel(__raw_readl(S3C2410_EINTPEND), S3C2410_EINTPEND);
584
585 /* flush cache back to ram */
586
587 arm920_flush_kern_cache_all();
588
589 s3c2410_pm_check_store();
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /* send the cpu to sleep... */
592
593 __raw_writel(0x00, S3C2410_CLKCON); /* turn off clocks over sleep */
594
595 s3c2410_cpu_suspend(regs_save);
596
Ben Dooks9bed07d2005-09-03 19:39:26 +0100597 /* restore the cpu state */
598
599 cpu_init();
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /* unset the return-from-sleep flag, to ensure reset */
602
603 tmp = __raw_readl(S3C2410_GSTATUS2);
604 tmp &= S3C2410_GSTATUS2_OFFRESET;
605 __raw_writel(tmp, S3C2410_GSTATUS2);
606
607 /* restore the system state */
608
609 s3c2410_pm_do_restore_core(core_save, ARRAY_SIZE(core_save));
610 s3c2410_pm_do_restore(gpio_save, ARRAY_SIZE(gpio_save));
611 s3c2410_pm_do_restore(irq_save, ARRAY_SIZE(irq_save));
612 s3c2410_pm_do_restore(uart_save, ARRAY_SIZE(uart_save));
613
614 s3c2410_pm_debug_init();
615
616 /* check what irq (if any) restored the system */
617
618 DBG("post sleep: IRQs 0x%08x, 0x%08x\n",
619 __raw_readl(S3C2410_SRCPND),
620 __raw_readl(S3C2410_EINTPEND));
621
622 s3c2410_pm_show_resume_irqs(IRQ_EINT0, __raw_readl(S3C2410_SRCPND),
623 s3c_irqwake_intmask);
624
625 s3c2410_pm_show_resume_irqs(IRQ_EINT4-4, __raw_readl(S3C2410_EINTPEND),
626 s3c_irqwake_eintmask);
627
628 DBG("post sleep, preparing to return\n");
629
630 s3c2410_pm_check_restore();
631
632 /* ok, let's return from sleep */
633
634 DBG("S3C2410 PM Resume (post-restore)\n");
635 return 0;
636}
637
638/*
639 * Called after processes are frozen, but before we shut down devices.
640 */
641static int s3c2410_pm_prepare(suspend_state_t state)
642{
643 return 0;
644}
645
646/*
647 * Called after devices are re-setup, but before processes are thawed.
648 */
649static int s3c2410_pm_finish(suspend_state_t state)
650{
651 return 0;
652}
653
654/*
655 * Set to PM_DISK_FIRMWARE so we can quickly veto suspend-to-disk.
656 */
657static struct pm_ops s3c2410_pm_ops = {
658 .pm_disk_mode = PM_DISK_FIRMWARE,
659 .prepare = s3c2410_pm_prepare,
660 .enter = s3c2410_pm_enter,
661 .finish = s3c2410_pm_finish,
662};
663
664/* s3c2410_pm_init
665 *
666 * Attach the power management functions. This should be called
667 * from the board specific initialisation if the board supports
668 * it.
669*/
670
671int __init s3c2410_pm_init(void)
672{
673 printk("S3C2410 Power Management, (c) 2004 Simtec Electronics\n");
674
675 pm_set_ops(&s3c2410_pm_ops);
676 return 0;
677}