b0c261936f3d7a8b240229231e518cd402319109
[platform/kernel/linux-rpi.git] / arch / powerpc / platforms / 83xx / suspend.c
1 /*
2  * MPC83xx suspend support
3  *
4  * Author: Scott Wood <scottwood@freescale.com>
5  *
6  * Copyright (c) 2006-2007 Freescale Semiconductor, Inc.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published
10  * by the Free Software Foundation.
11  */
12
13 #include <linux/init.h>
14 #include <linux/pm.h>
15 #include <linux/types.h>
16 #include <linux/ioport.h>
17 #include <linux/interrupt.h>
18 #include <linux/wait.h>
19 #include <linux/kthread.h>
20 #include <linux/freezer.h>
21 #include <linux/suspend.h>
22 #include <linux/fsl_devices.h>
23 #include <linux/of_platform.h>
24
25 #include <asm/reg.h>
26 #include <asm/io.h>
27 #include <asm/time.h>
28 #include <asm/mpc6xx.h>
29
30 #include <sysdev/fsl_soc.h>
31
32 #define PMCCR1_NEXT_STATE       0x0C /* Next state for power management */
33 #define PMCCR1_NEXT_STATE_SHIFT 2
34 #define PMCCR1_CURR_STATE       0x03 /* Current state for power management*/
35 #define IMMR_RCW_OFFSET         0x900
36 #define RCW_PCI_HOST            0x80000000
37
38 void mpc83xx_enter_deep_sleep(phys_addr_t immrbase);
39
40 struct mpc83xx_pmc {
41         u32 config;
42 #define PMCCR_DLPEN 2 /* DDR SDRAM low power enable */
43 #define PMCCR_SLPEN 1 /* System low power enable */
44
45         u32 event;
46         u32 mask;
47 /* All but PMCI are deep-sleep only */
48 #define PMCER_GPIO   0x100
49 #define PMCER_PCI    0x080
50 #define PMCER_USB    0x040
51 #define PMCER_ETSEC1 0x020
52 #define PMCER_ETSEC2 0x010
53 #define PMCER_TIMER  0x008
54 #define PMCER_INT1   0x004
55 #define PMCER_INT2   0x002
56 #define PMCER_PMCI   0x001
57 #define PMCER_ALL    0x1FF
58
59         /* deep-sleep only */
60         u32 config1;
61 #define PMCCR1_USE_STATE  0x80000000
62 #define PMCCR1_PME_EN     0x00000080
63 #define PMCCR1_ASSERT_PME 0x00000040
64 #define PMCCR1_POWER_OFF  0x00000020
65
66         /* deep-sleep only */
67         u32 config2;
68 };
69
70 struct mpc83xx_rcw {
71         u32 rcwlr;
72         u32 rcwhr;
73 };
74
75 struct mpc83xx_clock {
76         u32 spmr;
77         u32 occr;
78         u32 sccr;
79 };
80
81 struct pmc_type {
82         int has_deep_sleep;
83 };
84
85 static struct of_device *pmc_dev;
86 static int has_deep_sleep, deep_sleeping;
87 static int pmc_irq;
88 static struct mpc83xx_pmc __iomem *pmc_regs;
89 static struct mpc83xx_clock __iomem *clock_regs;
90 static int is_pci_agent, wake_from_pci;
91 static phys_addr_t immrbase;
92 static int pci_pm_state;
93 static DECLARE_WAIT_QUEUE_HEAD(agent_wq);
94
95 int fsl_deep_sleep(void)
96 {
97         return deep_sleeping;
98 }
99 EXPORT_SYMBOL(fsl_deep_sleep);
100
101 static int mpc83xx_change_state(void)
102 {
103         u32 curr_state;
104         u32 reg_cfg1 = in_be32(&pmc_regs->config1);
105
106         if (is_pci_agent) {
107                 pci_pm_state = (reg_cfg1 & PMCCR1_NEXT_STATE) >>
108                                PMCCR1_NEXT_STATE_SHIFT;
109                 curr_state = reg_cfg1 & PMCCR1_CURR_STATE;
110
111                 if (curr_state != pci_pm_state) {
112                         reg_cfg1 &= ~PMCCR1_CURR_STATE;
113                         reg_cfg1 |= pci_pm_state;
114                         out_be32(&pmc_regs->config1, reg_cfg1);
115
116                         wake_up(&agent_wq);
117                         return 1;
118                 }
119         }
120
121         return 0;
122 }
123
124 static irqreturn_t pmc_irq_handler(int irq, void *dev_id)
125 {
126         u32 event = in_be32(&pmc_regs->event);
127         int ret = IRQ_NONE;
128
129         if (mpc83xx_change_state())
130                 ret = IRQ_HANDLED;
131
132         if (event) {
133                 out_be32(&pmc_regs->event, event);
134                 ret = IRQ_HANDLED;
135         }
136
137         return ret;
138 }
139
140 static int mpc83xx_suspend_enter(suspend_state_t state)
141 {
142         int ret = -EAGAIN;
143
144         /* Don't go to sleep if there's a race where pci_pm_state changes
145          * between the agent thread checking it and the PM code disabling
146          * interrupts.
147          */
148         if (wake_from_pci) {
149                 if (pci_pm_state != (deep_sleeping ? 3 : 2))
150                         goto out;
151
152                 out_be32(&pmc_regs->config1,
153                          in_be32(&pmc_regs->config1) | PMCCR1_PME_EN);
154         }
155
156         /* Put the system into low-power mode and the RAM
157          * into self-refresh mode once the core goes to
158          * sleep.
159          */
160
161         out_be32(&pmc_regs->config, PMCCR_SLPEN | PMCCR_DLPEN);
162
163         /* If it has deep sleep (i.e. it's an 831x or compatible),
164          * disable power to the core upon entering sleep mode.  This will
165          * require going through the boot firmware upon a wakeup event.
166          */
167
168         if (deep_sleeping) {
169                 out_be32(&pmc_regs->mask, PMCER_ALL);
170
171                 out_be32(&pmc_regs->config1,
172                          in_be32(&pmc_regs->config1) | PMCCR1_POWER_OFF);
173
174                 enable_kernel_fp();
175
176                 mpc83xx_enter_deep_sleep(immrbase);
177
178                 out_be32(&pmc_regs->config1,
179                          in_be32(&pmc_regs->config1) & ~PMCCR1_POWER_OFF);
180
181                 out_be32(&pmc_regs->mask, PMCER_PMCI);
182         } else {
183                 out_be32(&pmc_regs->mask, PMCER_PMCI);
184
185                 mpc6xx_enter_standby();
186         }
187
188         ret = 0;
189
190 out:
191         out_be32(&pmc_regs->config1,
192                  in_be32(&pmc_regs->config1) & ~PMCCR1_PME_EN);
193
194         return ret;
195 }
196
197 static void mpc83xx_suspend_end(void)
198 {
199         deep_sleeping = 0;
200 }
201
202 static int mpc83xx_suspend_valid(suspend_state_t state)
203 {
204         return state == PM_SUSPEND_STANDBY || state == PM_SUSPEND_MEM;
205 }
206
207 static int mpc83xx_suspend_begin(suspend_state_t state)
208 {
209         switch (state) {
210                 case PM_SUSPEND_STANDBY:
211                         deep_sleeping = 0;
212                         return 0;
213
214                 case PM_SUSPEND_MEM:
215                         if (has_deep_sleep)
216                                 deep_sleeping = 1;
217
218                         return 0;
219
220                 default:
221                         return -EINVAL;
222         }
223 }
224
225 static int agent_thread_fn(void *data)
226 {
227         while (1) {
228                 wait_event_interruptible(agent_wq, pci_pm_state >= 2);
229                 try_to_freeze();
230
231                 if (signal_pending(current) || pci_pm_state < 2)
232                         continue;
233
234                 /* With a preemptible kernel (or SMP), this could race with
235                  * a userspace-driven suspend request.  It's probably best
236                  * to avoid mixing the two with such a configuration (or
237                  * else fix it by adding a mutex to state_store that we can
238                  * synchronize with).
239                  */
240
241                 wake_from_pci = 1;
242
243                 pm_suspend(pci_pm_state == 3 ? PM_SUSPEND_MEM :
244                                                PM_SUSPEND_STANDBY);
245
246                 wake_from_pci = 0;
247         }
248
249         return 0;
250 }
251
252 static void mpc83xx_set_agent(void)
253 {
254         out_be32(&pmc_regs->config1, PMCCR1_USE_STATE);
255         out_be32(&pmc_regs->mask, PMCER_PMCI);
256
257         kthread_run(agent_thread_fn, NULL, "PCI power mgt");
258 }
259
260 static int mpc83xx_is_pci_agent(void)
261 {
262         struct mpc83xx_rcw __iomem *rcw_regs;
263         int ret;
264
265         rcw_regs = ioremap(get_immrbase() + IMMR_RCW_OFFSET,
266                            sizeof(struct mpc83xx_rcw));
267
268         if (!rcw_regs)
269                 return -ENOMEM;
270
271         ret = !(in_be32(&rcw_regs->rcwhr) & RCW_PCI_HOST);
272
273         iounmap(rcw_regs);
274         return ret;
275 }
276
277 static struct platform_suspend_ops mpc83xx_suspend_ops = {
278         .valid = mpc83xx_suspend_valid,
279         .begin = mpc83xx_suspend_begin,
280         .enter = mpc83xx_suspend_enter,
281         .end = mpc83xx_suspend_end,
282 };
283
284 static int pmc_probe(struct of_device *ofdev,
285                      const struct of_device_id *match)
286 {
287         struct device_node *np = ofdev->node;
288         struct resource res;
289         struct pmc_type *type = match->data;
290         int ret = 0;
291
292         if (!of_device_is_available(np))
293                 return -ENODEV;
294
295         has_deep_sleep = type->has_deep_sleep;
296         immrbase = get_immrbase();
297         pmc_dev = ofdev;
298
299         is_pci_agent = mpc83xx_is_pci_agent();
300         if (is_pci_agent < 0)
301                 return is_pci_agent;
302
303         ret = of_address_to_resource(np, 0, &res);
304         if (ret)
305                 return -ENODEV;
306
307         pmc_irq = irq_of_parse_and_map(np, 0);
308         if (pmc_irq != NO_IRQ) {
309                 ret = request_irq(pmc_irq, pmc_irq_handler, IRQF_SHARED,
310                                   "pmc", ofdev);
311
312                 if (ret)
313                         return -EBUSY;
314         }
315
316         pmc_regs = ioremap(res.start, sizeof(struct mpc83xx_pmc));
317
318         if (!pmc_regs) {
319                 ret = -ENOMEM;
320                 goto out;
321         }
322
323         ret = of_address_to_resource(np, 1, &res);
324         if (ret) {
325                 ret = -ENODEV;
326                 goto out_pmc;
327         }
328
329         clock_regs = ioremap(res.start, sizeof(struct mpc83xx_pmc));
330
331         if (!clock_regs) {
332                 ret = -ENOMEM;
333                 goto out_pmc;
334         }
335
336         if (is_pci_agent)
337                 mpc83xx_set_agent();
338
339         suspend_set_ops(&mpc83xx_suspend_ops);
340         return 0;
341
342 out_pmc:
343         iounmap(pmc_regs);
344 out:
345         if (pmc_irq != NO_IRQ)
346                 free_irq(pmc_irq, ofdev);
347
348         return ret;
349 }
350
351 static int pmc_remove(struct of_device *ofdev)
352 {
353         return -EPERM;
354 };
355
356 static struct pmc_type pmc_types[] = {
357         {
358                 .has_deep_sleep = 1,
359         },
360         {
361                 .has_deep_sleep = 0,
362         }
363 };
364
365 static struct of_device_id pmc_match[] = {
366         {
367                 .compatible = "fsl,mpc8313-pmc",
368                 .data = &pmc_types[0],
369         },
370         {
371                 .compatible = "fsl,mpc8349-pmc",
372                 .data = &pmc_types[1],
373         },
374         {}
375 };
376
377 static struct of_platform_driver pmc_driver = {
378         .name = "mpc83xx-pmc",
379         .match_table = pmc_match,
380         .probe = pmc_probe,
381         .remove = pmc_remove
382 };
383
384 static int pmc_init(void)
385 {
386         return of_register_platform_driver(&pmc_driver);
387 }
388
389 module_init(pmc_init);