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