Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* $Id: power.c,v 1.10 2001/12/11 01:57:16 davem Exp $ |
2 | * power.c: Power management driver. | |
3 | * | |
4 | * Copyright (C) 1999 David S. Miller (davem@redhat.com) | |
5 | */ | |
6 | ||
1da177e4 LT |
7 | #include <linux/kernel.h> |
8 | #include <linux/module.h> | |
9 | #include <linux/init.h> | |
10 | #include <linux/sched.h> | |
11 | #include <linux/signal.h> | |
12 | #include <linux/delay.h> | |
13 | #include <linux/interrupt.h> | |
90bf8116 | 14 | #include <linux/pm.h> |
67608567 | 15 | #include <linux/syscalls.h> |
1da177e4 LT |
16 | |
17 | #include <asm/system.h> | |
1da177e4 | 18 | #include <asm/auxio.h> |
abbce6e2 DM |
19 | #include <asm/prom.h> |
20 | #include <asm/of_device.h> | |
21 | #include <asm/io.h> | |
22d6a1cb | 22 | #include <asm/sstate.h> |
1da177e4 | 23 | |
1da177e4 LT |
24 | #include <linux/unistd.h> |
25 | ||
26 | /* | |
27 | * sysctl - toggle power-off restriction for serial console | |
28 | * systems in machine_power_off() | |
29 | */ | |
30 | int scons_pwroff = 1; | |
31 | ||
32 | #ifdef CONFIG_PCI | |
abbce6e2 | 33 | #include <linux/pci.h> |
1da177e4 LT |
34 | static void __iomem *power_reg; |
35 | ||
36 | static DECLARE_WAIT_QUEUE_HEAD(powerd_wait); | |
37 | static int button_pressed; | |
38 | ||
6d24c8dc | 39 | static irqreturn_t power_handler(int irq, void *dev_id) |
1da177e4 LT |
40 | { |
41 | if (button_pressed == 0) { | |
42 | button_pressed = 1; | |
43 | wake_up(&powerd_wait); | |
44 | } | |
45 | ||
46 | /* FIXME: Check registers for status... */ | |
47 | return IRQ_HANDLED; | |
48 | } | |
49 | #endif /* CONFIG_PCI */ | |
50 | ||
51 | extern void machine_halt(void); | |
52 | extern void machine_alt_power_off(void); | |
53 | static void (*poweroff_method)(void) = machine_alt_power_off; | |
54 | ||
55 | void machine_power_off(void) | |
56 | { | |
22d6a1cb | 57 | sstate_poweroff(); |
1da177e4 LT |
58 | if (!serial_console || scons_pwroff) { |
59 | #ifdef CONFIG_PCI | |
60 | if (power_reg) { | |
61 | /* Both register bits seem to have the | |
62 | * same effect, so until I figure out | |
63 | * what the difference is... | |
64 | */ | |
65 | writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg); | |
66 | } else | |
67 | #endif /* CONFIG_PCI */ | |
68 | if (poweroff_method != NULL) { | |
69 | poweroff_method(); | |
70 | /* not reached */ | |
71 | } | |
72 | } | |
73 | machine_halt(); | |
74 | } | |
75 | ||
90bf8116 DM |
76 | void (*pm_power_off)(void) = machine_power_off; |
77 | EXPORT_SYMBOL(pm_power_off); | |
78 | ||
1da177e4 LT |
79 | #ifdef CONFIG_PCI |
80 | static int powerd(void *__unused) | |
81 | { | |
82 | static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL }; | |
83 | char *argv[] = { "/sbin/shutdown", "-h", "now", NULL }; | |
84 | DECLARE_WAITQUEUE(wait, current); | |
85 | ||
86 | daemonize("powerd"); | |
87 | ||
88 | add_wait_queue(&powerd_wait, &wait); | |
89 | again: | |
90 | for (;;) { | |
91 | set_task_state(current, TASK_INTERRUPTIBLE); | |
92 | if (button_pressed) | |
93 | break; | |
94 | flush_signals(current); | |
95 | schedule(); | |
96 | } | |
97 | __set_current_state(TASK_RUNNING); | |
98 | remove_wait_queue(&powerd_wait, &wait); | |
99 | ||
100 | /* Ok, down we go... */ | |
101 | button_pressed = 0; | |
67608567 | 102 | if (kernel_execve("/sbin/shutdown", argv, envp) < 0) { |
1da177e4 LT |
103 | printk("powerd: shutdown execution failed\n"); |
104 | add_wait_queue(&powerd_wait, &wait); | |
105 | goto again; | |
106 | } | |
107 | return 0; | |
108 | } | |
109 | ||
690c8fd3 | 110 | static int __init has_button_interrupt(unsigned int irq, struct device_node *dp) |
1da177e4 | 111 | { |
2256c13b | 112 | if (irq == PCI_IRQ_NONE) |
1da177e4 | 113 | return 0; |
690c8fd3 | 114 | if (!of_find_property(dp, "button", NULL)) |
1da177e4 LT |
115 | return 0; |
116 | ||
117 | return 1; | |
118 | } | |
119 | ||
abbce6e2 | 120 | static int __devinit power_probe(struct of_device *op, const struct of_device_id *match) |
1da177e4 | 121 | { |
abbce6e2 DM |
122 | struct resource *res = &op->resource[0]; |
123 | unsigned int irq= op->irqs[0]; | |
a2bd4fd1 | 124 | |
abbce6e2 DM |
125 | power_reg = of_ioremap(res, 0, 0x4, "power"); |
126 | ||
127 | printk("%s: Control reg at %lx ... ", | |
128 | op->node->name, res->start); | |
a2bd4fd1 | 129 | |
1da177e4 | 130 | poweroff_method = machine_halt; /* able to use the standard halt */ |
a2bd4fd1 | 131 | |
abbce6e2 | 132 | if (has_button_interrupt(irq, op->node)) { |
1da177e4 LT |
133 | if (kernel_thread(powerd, NULL, CLONE_FS) < 0) { |
134 | printk("Failed to start power daemon.\n"); | |
abbce6e2 | 135 | return 0; |
1da177e4 LT |
136 | } |
137 | printk("powerd running.\n"); | |
138 | ||
2256c13b | 139 | if (request_irq(irq, |
00cde674 | 140 | power_handler, 0, "power", NULL) < 0) |
1da177e4 LT |
141 | printk("power: Error, cannot register IRQ handler.\n"); |
142 | } else { | |
143 | printk("not using powerd.\n"); | |
144 | } | |
abbce6e2 DM |
145 | |
146 | return 0; | |
1da177e4 | 147 | } |
a2bd4fd1 DM |
148 | |
149 | static struct of_device_id power_match[] = { | |
150 | { | |
151 | .name = "power", | |
152 | }, | |
153 | {}, | |
154 | }; | |
155 | ||
abbce6e2 | 156 | static struct of_platform_driver power_driver = { |
a2bd4fd1 DM |
157 | .name = "power", |
158 | .match_table = power_match, | |
abbce6e2 | 159 | .probe = power_probe, |
a2bd4fd1 DM |
160 | }; |
161 | ||
162 | void __init power_init(void) | |
163 | { | |
abbce6e2 | 164 | of_register_driver(&power_driver, &of_bus_type); |
a2bd4fd1 DM |
165 | return; |
166 | } | |
1da177e4 | 167 | #endif /* CONFIG_PCI */ |