blob: 4febeda958a3fee5585683b4bb1eddbf7e7a06cb [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>
David S. Miller90bf8112006-01-09 13:59:12 -080017#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/auxio.h>
David S. Millerabbce6e2006-06-29 15:22:46 -070021#include <asm/prom.h>
22#include <asm/of_device.h>
23#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/unistd.h>
26
27/*
28 * sysctl - toggle power-off restriction for serial console
29 * systems in machine_power_off()
30 */
31int scons_pwroff = 1;
32
33#ifdef CONFIG_PCI
David S. Millerabbce6e2006-06-29 15:22:46 -070034#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035static void __iomem *power_reg;
36
37static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
38static int button_pressed;
39
40static irqreturn_t power_handler(int irq, void *dev_id, struct pt_regs *regs)
41{
42 if (button_pressed == 0) {
43 button_pressed = 1;
44 wake_up(&powerd_wait);
45 }
46
47 /* FIXME: Check registers for status... */
48 return IRQ_HANDLED;
49}
50#endif /* CONFIG_PCI */
51
52extern void machine_halt(void);
53extern void machine_alt_power_off(void);
54static void (*poweroff_method)(void) = machine_alt_power_off;
55
56void machine_power_off(void)
57{
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
David S. Miller90bf8112006-01-09 13:59:12 -080076void (*pm_power_off)(void) = machine_power_off;
77EXPORT_SYMBOL(pm_power_off);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#ifdef CONFIG_PCI
80static 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);
89again:
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;
102 if (execve("/sbin/shutdown", argv, envp) < 0) {
103 printk("powerd: shutdown execution failed\n");
104 add_wait_queue(&powerd_wait, &wait);
105 goto again;
106 }
107 return 0;
108}
109
David S. Miller690c8fd2006-06-22 19:12:03 -0700110static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
David S. Miller2256c132005-10-06 20:43:54 -0700112 if (irq == PCI_IRQ_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return 0;
David S. Miller690c8fd2006-06-22 19:12:03 -0700114 if (!of_find_property(dp, "button", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116
117 return 1;
118}
119
David S. Millerabbce6e2006-06-29 15:22:46 -0700120static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
David S. Millerabbce6e2006-06-29 15:22:46 -0700122 struct resource *res = &op->resource[0];
123 unsigned int irq= op->irqs[0];
David S. Millera2bd4fd2006-06-23 01:44:10 -0700124
David S. Millerabbce6e2006-06-29 15:22:46 -0700125 power_reg = of_ioremap(res, 0, 0x4, "power");
126
127 printk("%s: Control reg at %lx ... ",
128 op->node->name, res->start);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 poweroff_method = machine_halt; /* able to use the standard halt */
David S. Millera2bd4fd2006-06-23 01:44:10 -0700131
David S. Millerabbce6e2006-06-29 15:22:46 -0700132 if (has_button_interrupt(irq, op->node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
134 printk("Failed to start power daemon.\n");
David S. Millerabbce6e2006-06-29 15:22:46 -0700135 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137 printk("powerd running.\n");
138
David S. Miller2256c132005-10-06 20:43:54 -0700139 if (request_irq(irq,
David S. Miller00cde672006-06-29 14:39:11 -0700140 power_handler, 0, "power", NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 printk("power: Error, cannot register IRQ handler.\n");
142 } else {
143 printk("not using powerd.\n");
144 }
David S. Millerabbce6e2006-06-29 15:22:46 -0700145
146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
David S. Millera2bd4fd2006-06-23 01:44:10 -0700148
149static struct of_device_id power_match[] = {
150 {
151 .name = "power",
152 },
153 {},
154};
155
David S. Millerabbce6e2006-06-29 15:22:46 -0700156static struct of_platform_driver power_driver = {
David S. Millera2bd4fd2006-06-23 01:44:10 -0700157 .name = "power",
158 .match_table = power_match,
David S. Millerabbce6e2006-06-29 15:22:46 -0700159 .probe = power_probe,
David S. Millera2bd4fd2006-06-23 01:44:10 -0700160};
161
162void __init power_init(void)
163{
David S. Millerabbce6e2006-06-29 15:22:46 -0700164 of_register_driver(&power_driver, &of_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700165 return;
166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167#endif /* CONFIG_PCI */