2 * MPC83xx suspend support
4 * Author: Scott Wood <scottwood@freescale.com>
6 * Copyright (c) 2006-2007 Freescale Semiconductor, Inc.
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.
14 #include <linux/types.h>
15 #include <linux/ioport.h>
16 #include <linux/interrupt.h>
17 #include <linux/wait.h>
18 #include <linux/sched/signal.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_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/of_platform.h>
26 #include <linux/export.h>
31 #include <asm/mpc6xx.h>
32 #include <asm/switch_to.h>
34 #include <sysdev/fsl_soc.h>
36 #define PMCCR1_NEXT_STATE 0x0C /* Next state for power management */
37 #define PMCCR1_NEXT_STATE_SHIFT 2
38 #define PMCCR1_CURR_STATE 0x03 /* Current state for power management*/
39 #define IMMR_SYSCR_OFFSET 0x100
40 #define IMMR_RCW_OFFSET 0x900
41 #define RCW_PCI_HOST 0x80000000
43 void mpc83xx_enter_deep_sleep(phys_addr_t immrbase
);
47 #define PMCCR_DLPEN 2 /* DDR SDRAM low power enable */
48 #define PMCCR_SLPEN 1 /* System low power enable */
52 /* All but PMCI are deep-sleep only */
53 #define PMCER_GPIO 0x100
54 #define PMCER_PCI 0x080
55 #define PMCER_USB 0x040
56 #define PMCER_ETSEC1 0x020
57 #define PMCER_ETSEC2 0x010
58 #define PMCER_TIMER 0x008
59 #define PMCER_INT1 0x004
60 #define PMCER_INT2 0x002
61 #define PMCER_PMCI 0x001
62 #define PMCER_ALL 0x1FF
66 #define PMCCR1_USE_STATE 0x80000000
67 #define PMCCR1_PME_EN 0x00000080
68 #define PMCCR1_ASSERT_PME 0x00000040
69 #define PMCCR1_POWER_OFF 0x00000020
80 struct mpc83xx_clock
{
86 struct mpc83xx_syscr
{
96 struct mpc83xx_saved
{
106 static struct platform_device
*pmc_dev
;
107 static int has_deep_sleep
, deep_sleeping
;
109 static struct mpc83xx_pmc __iomem
*pmc_regs
;
110 static struct mpc83xx_clock __iomem
*clock_regs
;
111 static struct mpc83xx_syscr __iomem
*syscr_regs
;
112 static struct mpc83xx_saved saved_regs
;
113 static int is_pci_agent
, wake_from_pci
;
114 static phys_addr_t immrbase
;
115 static int pci_pm_state
;
116 static DECLARE_WAIT_QUEUE_HEAD(agent_wq
);
118 int fsl_deep_sleep(void)
120 return deep_sleeping
;
122 EXPORT_SYMBOL(fsl_deep_sleep
);
124 static int mpc83xx_change_state(void)
127 u32 reg_cfg1
= in_be32(&pmc_regs
->config1
);
130 pci_pm_state
= (reg_cfg1
& PMCCR1_NEXT_STATE
) >>
131 PMCCR1_NEXT_STATE_SHIFT
;
132 curr_state
= reg_cfg1
& PMCCR1_CURR_STATE
;
134 if (curr_state
!= pci_pm_state
) {
135 reg_cfg1
&= ~PMCCR1_CURR_STATE
;
136 reg_cfg1
|= pci_pm_state
;
137 out_be32(&pmc_regs
->config1
, reg_cfg1
);
147 static irqreturn_t
pmc_irq_handler(int irq
, void *dev_id
)
149 u32 event
= in_be32(&pmc_regs
->event
);
152 if (mpc83xx_change_state())
156 out_be32(&pmc_regs
->event
, event
);
163 static void mpc83xx_suspend_restore_regs(void)
165 out_be32(&syscr_regs
->sicrl
, saved_regs
.sicrl
);
166 out_be32(&syscr_regs
->sicrh
, saved_regs
.sicrh
);
167 out_be32(&clock_regs
->sccr
, saved_regs
.sccr
);
170 static void mpc83xx_suspend_save_regs(void)
172 saved_regs
.sicrl
= in_be32(&syscr_regs
->sicrl
);
173 saved_regs
.sicrh
= in_be32(&syscr_regs
->sicrh
);
174 saved_regs
.sccr
= in_be32(&clock_regs
->sccr
);
177 static int mpc83xx_suspend_enter(suspend_state_t state
)
181 /* Don't go to sleep if there's a race where pci_pm_state changes
182 * between the agent thread checking it and the PM code disabling
186 if (pci_pm_state
!= (deep_sleeping
? 3 : 2))
189 out_be32(&pmc_regs
->config1
,
190 in_be32(&pmc_regs
->config1
) | PMCCR1_PME_EN
);
193 /* Put the system into low-power mode and the RAM
194 * into self-refresh mode once the core goes to
198 out_be32(&pmc_regs
->config
, PMCCR_SLPEN
| PMCCR_DLPEN
);
200 /* If it has deep sleep (i.e. it's an 831x or compatible),
201 * disable power to the core upon entering sleep mode. This will
202 * require going through the boot firmware upon a wakeup event.
206 mpc83xx_suspend_save_regs();
208 out_be32(&pmc_regs
->mask
, PMCER_ALL
);
210 out_be32(&pmc_regs
->config1
,
211 in_be32(&pmc_regs
->config1
) | PMCCR1_POWER_OFF
);
215 mpc83xx_enter_deep_sleep(immrbase
);
217 out_be32(&pmc_regs
->config1
,
218 in_be32(&pmc_regs
->config1
) & ~PMCCR1_POWER_OFF
);
220 out_be32(&pmc_regs
->mask
, PMCER_PMCI
);
222 mpc83xx_suspend_restore_regs();
224 out_be32(&pmc_regs
->mask
, PMCER_PMCI
);
226 mpc6xx_enter_standby();
232 out_be32(&pmc_regs
->config1
,
233 in_be32(&pmc_regs
->config1
) & ~PMCCR1_PME_EN
);
238 static void mpc83xx_suspend_end(void)
243 static int mpc83xx_suspend_valid(suspend_state_t state
)
245 return state
== PM_SUSPEND_STANDBY
|| state
== PM_SUSPEND_MEM
;
248 static int mpc83xx_suspend_begin(suspend_state_t state
)
251 case PM_SUSPEND_STANDBY
:
266 static int agent_thread_fn(void *data
)
269 wait_event_interruptible(agent_wq
, pci_pm_state
>= 2);
272 if (signal_pending(current
) || pci_pm_state
< 2)
275 /* With a preemptible kernel (or SMP), this could race with
276 * a userspace-driven suspend request. It's probably best
277 * to avoid mixing the two with such a configuration (or
278 * else fix it by adding a mutex to state_store that we can
284 pm_suspend(pci_pm_state
== 3 ? PM_SUSPEND_MEM
:
293 static void mpc83xx_set_agent(void)
295 out_be32(&pmc_regs
->config1
, PMCCR1_USE_STATE
);
296 out_be32(&pmc_regs
->mask
, PMCER_PMCI
);
298 kthread_run(agent_thread_fn
, NULL
, "PCI power mgt");
301 static int mpc83xx_is_pci_agent(void)
303 struct mpc83xx_rcw __iomem
*rcw_regs
;
306 rcw_regs
= ioremap(get_immrbase() + IMMR_RCW_OFFSET
,
307 sizeof(struct mpc83xx_rcw
));
312 ret
= !(in_be32(&rcw_regs
->rcwhr
) & RCW_PCI_HOST
);
318 static const struct platform_suspend_ops mpc83xx_suspend_ops
= {
319 .valid
= mpc83xx_suspend_valid
,
320 .begin
= mpc83xx_suspend_begin
,
321 .enter
= mpc83xx_suspend_enter
,
322 .end
= mpc83xx_suspend_end
,
325 static const struct of_device_id pmc_match
[];
326 static int pmc_probe(struct platform_device
*ofdev
)
328 const struct of_device_id
*match
;
329 struct device_node
*np
= ofdev
->dev
.of_node
;
331 const struct pmc_type
*type
;
334 match
= of_match_device(pmc_match
, &ofdev
->dev
);
340 if (!of_device_is_available(np
))
343 has_deep_sleep
= type
->has_deep_sleep
;
344 immrbase
= get_immrbase();
347 is_pci_agent
= mpc83xx_is_pci_agent();
348 if (is_pci_agent
< 0)
351 ret
= of_address_to_resource(np
, 0, &res
);
355 pmc_irq
= irq_of_parse_and_map(np
, 0);
357 ret
= request_irq(pmc_irq
, pmc_irq_handler
, IRQF_SHARED
,
364 pmc_regs
= ioremap(res
.start
, sizeof(*pmc_regs
));
371 ret
= of_address_to_resource(np
, 1, &res
);
377 clock_regs
= ioremap(res
.start
, sizeof(*clock_regs
));
384 if (has_deep_sleep
) {
385 syscr_regs
= ioremap(immrbase
+ IMMR_SYSCR_OFFSET
,
386 sizeof(*syscr_regs
));
396 suspend_set_ops(&mpc83xx_suspend_ops
);
405 free_irq(pmc_irq
, ofdev
);
410 static int pmc_remove(struct platform_device
*ofdev
)
415 static struct pmc_type pmc_types
[] = {
424 static const struct of_device_id pmc_match
[] = {
426 .compatible
= "fsl,mpc8313-pmc",
427 .data
= &pmc_types
[0],
430 .compatible
= "fsl,mpc8349-pmc",
431 .data
= &pmc_types
[1],
436 static struct platform_driver pmc_driver
= {
438 .name
= "mpc83xx-pmc",
439 .of_match_table
= pmc_match
,
445 builtin_platform_driver(pmc_driver
);