blob: 533104c7907d970994d3363eaa79b2126e327d8c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* $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
David S. Millerbb49bcd2005-07-10 16:49:28 -07007#define __KERNEL_SYSCALLS__
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#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
18#include <asm/system.h>
19#include <asm/ebus.h>
20#include <asm/auxio.h>
21
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/unistd.h>
23
24/*
25 * sysctl - toggle power-off restriction for serial console
26 * systems in machine_power_off()
27 */
28int scons_pwroff = 1;
29
30#ifdef CONFIG_PCI
31static void __iomem *power_reg;
32
33static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
34static int button_pressed;
35
36static irqreturn_t power_handler(int irq, void *dev_id, struct pt_regs *regs)
37{
38 if (button_pressed == 0) {
39 button_pressed = 1;
40 wake_up(&powerd_wait);
41 }
42
43 /* FIXME: Check registers for status... */
44 return IRQ_HANDLED;
45}
46#endif /* CONFIG_PCI */
47
48extern void machine_halt(void);
49extern void machine_alt_power_off(void);
50static void (*poweroff_method)(void) = machine_alt_power_off;
51
52void machine_power_off(void)
53{
54 if (!serial_console || scons_pwroff) {
55#ifdef CONFIG_PCI
56 if (power_reg) {
57 /* Both register bits seem to have the
58 * same effect, so until I figure out
59 * what the difference is...
60 */
61 writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
62 } else
63#endif /* CONFIG_PCI */
64 if (poweroff_method != NULL) {
65 poweroff_method();
66 /* not reached */
67 }
68 }
69 machine_halt();
70}
71
72EXPORT_SYMBOL(machine_power_off);
73
74#ifdef CONFIG_PCI
75static int powerd(void *__unused)
76{
77 static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
78 char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
79 DECLARE_WAITQUEUE(wait, current);
80
81 daemonize("powerd");
82
83 add_wait_queue(&powerd_wait, &wait);
84again:
85 for (;;) {
86 set_task_state(current, TASK_INTERRUPTIBLE);
87 if (button_pressed)
88 break;
89 flush_signals(current);
90 schedule();
91 }
92 __set_current_state(TASK_RUNNING);
93 remove_wait_queue(&powerd_wait, &wait);
94
95 /* Ok, down we go... */
96 button_pressed = 0;
97 if (execve("/sbin/shutdown", argv, envp) < 0) {
98 printk("powerd: shutdown execution failed\n");
99 add_wait_queue(&powerd_wait, &wait);
100 goto again;
101 }
102 return 0;
103}
104
105static int __init has_button_interrupt(struct linux_ebus_device *edev)
106{
107 if (edev->irqs[0] == PCI_IRQ_NONE)
108 return 0;
109 if (!prom_node_has_property(edev->prom_node, "button"))
110 return 0;
111
112 return 1;
113}
114
115void __init power_init(void)
116{
117 struct linux_ebus *ebus;
118 struct linux_ebus_device *edev;
119 static int invoked;
120
121 if (invoked)
122 return;
123 invoked = 1;
124
125 for_each_ebus(ebus) {
126 for_each_ebusdev(edev, ebus) {
127 if (!strcmp(edev->prom_name, "power"))
128 goto found;
129 }
130 }
131 return;
132
133found:
134 power_reg = ioremap(edev->resource[0].start, 0x4);
135 printk("power: Control reg at %p ... ", power_reg);
136 poweroff_method = machine_halt; /* able to use the standard halt */
137 if (has_button_interrupt(edev)) {
138 if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
139 printk("Failed to start power daemon.\n");
140 return;
141 }
142 printk("powerd running.\n");
143
144 if (request_irq(edev->irqs[0],
145 power_handler, SA_SHIRQ, "power", NULL) < 0)
146 printk("power: Error, cannot register IRQ handler.\n");
147 } else {
148 printk("not using powerd.\n");
149 }
150}
151#endif /* CONFIG_PCI */