Merge branch 'master'
[linux-2.6] / arch / sparc64 / kernel / power.c
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
7 #define __KERNEL_SYSCALLS__
8
9 #include <linux/config.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/sched.h>
14 #include <linux/signal.h>
15 #include <linux/delay.h>
16 #include <linux/interrupt.h>
17 #include <linux/pm.h>
18
19 #include <asm/system.h>
20 #include <asm/ebus.h>
21 #include <asm/isa.h>
22 #include <asm/auxio.h>
23
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
33 static void __iomem *power_reg;
34
35 static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
36 static int button_pressed;
37
38 static irqreturn_t power_handler(int irq, void *dev_id, struct pt_regs *regs)
39 {
40         if (button_pressed == 0) {
41                 button_pressed = 1;
42                 wake_up(&powerd_wait);
43         }
44
45         /* FIXME: Check registers for status... */
46         return IRQ_HANDLED;
47 }
48 #endif /* CONFIG_PCI */
49
50 extern void machine_halt(void);
51 extern void machine_alt_power_off(void);
52 static void (*poweroff_method)(void) = machine_alt_power_off;
53
54 void machine_power_off(void)
55 {
56         if (!serial_console || scons_pwroff) {
57 #ifdef CONFIG_PCI
58                 if (power_reg) {
59                         /* Both register bits seem to have the
60                          * same effect, so until I figure out
61                          * what the difference is...
62                          */
63                         writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
64                 } else
65 #endif /* CONFIG_PCI */
66                         if (poweroff_method != NULL) {
67                                 poweroff_method();
68                                 /* not reached */
69                         }
70         }
71         machine_halt();
72 }
73
74 void (*pm_power_off)(void) = machine_power_off;
75 EXPORT_SYMBOL(pm_power_off);
76
77 #ifdef CONFIG_PCI
78 static int powerd(void *__unused)
79 {
80         static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
81         char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
82         DECLARE_WAITQUEUE(wait, current);
83
84         daemonize("powerd");
85
86         add_wait_queue(&powerd_wait, &wait);
87 again:
88         for (;;) {
89                 set_task_state(current, TASK_INTERRUPTIBLE);
90                 if (button_pressed)
91                         break;
92                 flush_signals(current);
93                 schedule();
94         }
95         __set_current_state(TASK_RUNNING);
96         remove_wait_queue(&powerd_wait, &wait);
97
98         /* Ok, down we go... */
99         button_pressed = 0;
100         if (execve("/sbin/shutdown", argv, envp) < 0) {
101                 printk("powerd: shutdown execution failed\n");
102                 add_wait_queue(&powerd_wait, &wait);
103                 goto again;
104         }
105         return 0;
106 }
107
108 static int __init has_button_interrupt(unsigned int irq, int prom_node)
109 {
110         if (irq == PCI_IRQ_NONE)
111                 return 0;
112         if (!prom_node_has_property(prom_node, "button"))
113                 return 0;
114
115         return 1;
116 }
117
118 static int __init power_probe_ebus(struct resource **resp, unsigned int *irq_p, int *prom_node_p)
119 {
120         struct linux_ebus *ebus;
121         struct linux_ebus_device *edev;
122
123         for_each_ebus(ebus) {
124                 for_each_ebusdev(edev, ebus) {
125                         if (!strcmp(edev->prom_name, "power")) {
126                                 *resp = &edev->resource[0];
127                                 *irq_p = edev->irqs[0];
128                                 *prom_node_p = edev->prom_node;
129                                 return 0;
130                         }
131                 }
132         }
133         return -ENODEV;
134 }
135
136 static int __init power_probe_isa(struct resource **resp, unsigned int *irq_p, int *prom_node_p)
137 {
138         struct sparc_isa_bridge *isa_bus;
139         struct sparc_isa_device *isa_dev;
140
141         for_each_isa(isa_bus) {
142                 for_each_isadev(isa_dev, isa_bus) {
143                         if (!strcmp(isa_dev->prom_name, "power")) {
144                                 *resp = &isa_dev->resource;
145                                 *irq_p = isa_dev->irq;
146                                 *prom_node_p = isa_dev->prom_node;
147                                 return 0;
148                         }
149                 }
150         }
151         return -ENODEV;
152 }
153
154 void __init power_init(void)
155 {
156         struct resource *res = NULL;
157         unsigned int irq;
158         int prom_node;
159         static int invoked;
160
161         if (invoked)
162                 return;
163         invoked = 1;
164
165         if (!power_probe_ebus(&res, &irq, &prom_node))
166                 goto found;
167
168         if (!power_probe_isa(&res, &irq, &prom_node))
169                 goto found;
170
171         return;
172
173 found:
174         power_reg = ioremap(res->start, 0x4);
175         printk("power: Control reg at %p ... ", power_reg);
176         poweroff_method = machine_halt;  /* able to use the standard halt */
177         if (has_button_interrupt(irq, prom_node)) {
178                 if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
179                         printk("Failed to start power daemon.\n");
180                         return;
181                 }
182                 printk("powerd running.\n");
183
184                 if (request_irq(irq,
185                                 power_handler, SA_SHIRQ, "power", NULL) < 0)
186                         printk("power: Error, cannot register IRQ handler.\n");
187         } else {
188                 printk("not using powerd.\n");
189         }
190 }
191 #endif /* CONFIG_PCI */