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