Merge commit 'v2.6.28-rc6' into core/debug
[linux-2.6] / arch / arm / mach-pnx4008 / pm.c
1 /*
2  * arch/arm/mach-pnx4008/pm.c
3  *
4  * Power Management driver for PNX4008
5  *
6  * Authors: Vitaly Wool, Dmitry Chigirev <source@mvista.com>
7  *
8  * 2005 (c) MontaVista Software, Inc. This file is licensed under
9  * the terms of the GNU General Public License version 2. This program
10  * is licensed "as is" without any warranty of any kind, whether express
11  * or implied.
12  */
13
14 #include <linux/pm.h>
15 #include <linux/rtc.h>
16 #include <linux/sched.h>
17 #include <linux/proc_fs.h>
18 #include <linux/suspend.h>
19 #include <linux/delay.h>
20 #include <linux/clk.h>
21 #include <linux/io.h>
22
23 #include <asm/cacheflush.h>
24 #include <mach/pm.h>
25 #include <mach/clock.h>
26
27 #define SRAM_VA IO_ADDRESS(PNX4008_IRAM_BASE)
28
29 static void *saved_sram;
30
31 static struct clk *pll4_clk;
32
33 static inline void pnx4008_standby(void)
34 {
35         void (*pnx4008_cpu_standby_ptr) (void);
36
37         local_irq_disable();
38         local_fiq_disable();
39
40         clk_disable(pll4_clk);
41
42         /*saving portion of SRAM to be used by suspend function. */
43         memcpy(saved_sram, (void *)SRAM_VA, pnx4008_cpu_standby_sz);
44
45         /*make sure SRAM copy gets physically written into SDRAM.
46            SDRAM will be placed into self-refresh during power down */
47         flush_cache_all();
48
49         /*copy suspend function into SRAM */
50         memcpy((void *)SRAM_VA, pnx4008_cpu_standby, pnx4008_cpu_standby_sz);
51
52         /*do suspend */
53         pnx4008_cpu_standby_ptr = (void *)SRAM_VA;
54         pnx4008_cpu_standby_ptr();
55
56         /*restoring portion of SRAM that was used by suspend function */
57         memcpy((void *)SRAM_VA, saved_sram, pnx4008_cpu_standby_sz);
58
59         clk_enable(pll4_clk);
60
61         local_fiq_enable();
62         local_irq_enable();
63 }
64
65 static inline void pnx4008_suspend(void)
66 {
67         void (*pnx4008_cpu_suspend_ptr) (void);
68
69         local_irq_disable();
70         local_fiq_disable();
71
72         clk_disable(pll4_clk);
73
74         __raw_writel(0xffffffff, START_INT_RSR_REG(SE_PIN_BASE_INT));
75         __raw_writel(0xffffffff, START_INT_RSR_REG(SE_INT_BASE_INT));
76
77         /*saving portion of SRAM to be used by suspend function. */
78         memcpy(saved_sram, (void *)SRAM_VA, pnx4008_cpu_suspend_sz);
79
80         /*make sure SRAM copy gets physically written into SDRAM.
81            SDRAM will be placed into self-refresh during power down */
82         flush_cache_all();
83
84         /*copy suspend function into SRAM */
85         memcpy((void *)SRAM_VA, pnx4008_cpu_suspend, pnx4008_cpu_suspend_sz);
86
87         /*do suspend */
88         pnx4008_cpu_suspend_ptr = (void *)SRAM_VA;
89         pnx4008_cpu_suspend_ptr();
90
91         /*restoring portion of SRAM that was used by suspend function */
92         memcpy((void *)SRAM_VA, saved_sram, pnx4008_cpu_suspend_sz);
93
94         clk_enable(pll4_clk);
95
96         local_fiq_enable();
97         local_irq_enable();
98 }
99
100 static int pnx4008_pm_enter(suspend_state_t state)
101 {
102         switch (state) {
103         case PM_SUSPEND_STANDBY:
104                 pnx4008_standby();
105                 break;
106         case PM_SUSPEND_MEM:
107                 pnx4008_suspend();
108                 break;
109         }
110         return 0;
111 }
112
113 static int pnx4008_pm_valid(suspend_state_t state)
114 {
115         return (state == PM_SUSPEND_STANDBY) ||
116                (state == PM_SUSPEND_MEM);
117 }
118
119 static struct platform_suspend_ops pnx4008_pm_ops = {
120         .enter = pnx4008_pm_enter,
121         .valid = pnx4008_pm_valid,
122 };
123
124 static int __init pnx4008_pm_init(void)
125 {
126         u32 sram_size_to_allocate;
127
128         pll4_clk = clk_get(0, "ck_pll4");
129         if (IS_ERR(pll4_clk)) {
130                 printk(KERN_ERR
131                        "PM Suspend cannot acquire ARM(PLL4) clock control\n");
132                 return PTR_ERR(pll4_clk);
133         }
134
135         if (pnx4008_cpu_standby_sz > pnx4008_cpu_suspend_sz)
136                 sram_size_to_allocate = pnx4008_cpu_standby_sz;
137         else
138                 sram_size_to_allocate = pnx4008_cpu_suspend_sz;
139
140         saved_sram = kmalloc(sram_size_to_allocate, GFP_ATOMIC);
141         if (!saved_sram) {
142                 printk(KERN_ERR
143                        "PM Suspend: cannot allocate memory to save portion of SRAM\n");
144                 clk_put(pll4_clk);
145                 return -ENOMEM;
146         }
147
148         suspend_set_ops(&pnx4008_pm_ops);
149         return 0;
150 }
151
152 late_initcall(pnx4008_pm_init);