blob: e55466c77b61efaf0a8c13172e0dc100bcf3a2a1 [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/kernel.h>
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/sched.h>
13#include <linux/signal.h>
14#include <linux/delay.h>
15#include <linux/interrupt.h>
David S. Miller90bf8112006-01-09 13:59:12 -080016#include <linux/pm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/auxio.h>
David S. Millerabbce6e2006-06-29 15:22:46 -070020#include <asm/prom.h>
21#include <asm/of_device.h>
22#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/unistd.h>
25
26/*
27 * sysctl - toggle power-off restriction for serial console
28 * systems in machine_power_off()
29 */
30int scons_pwroff = 1;
31
32#ifdef CONFIG_PCI
David S. Millerabbce6e2006-06-29 15:22:46 -070033#include <linux/pci.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034static void __iomem *power_reg;
35
36static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
37static int button_pressed;
38
39static irqreturn_t power_handler(int irq, void *dev_id, struct pt_regs *regs)
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
51extern void machine_halt(void);
52extern void machine_alt_power_off(void);
53static void (*poweroff_method)(void) = machine_alt_power_off;
54
55void machine_power_off(void)
56{
57 if (!serial_console || scons_pwroff) {
58#ifdef CONFIG_PCI
59 if (power_reg) {
60 /* Both register bits seem to have the
61 * same effect, so until I figure out
62 * what the difference is...
63 */
64 writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
65 } else
66#endif /* CONFIG_PCI */
67 if (poweroff_method != NULL) {
68 poweroff_method();
69 /* not reached */
70 }
71 }
72 machine_halt();
73}
74
David S. Miller90bf8112006-01-09 13:59:12 -080075void (*pm_power_off)(void) = machine_power_off;
76EXPORT_SYMBOL(pm_power_off);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#ifdef CONFIG_PCI
79static int powerd(void *__unused)
80{
81 static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
82 char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
83 DECLARE_WAITQUEUE(wait, current);
84
85 daemonize("powerd");
86
87 add_wait_queue(&powerd_wait, &wait);
88again:
89 for (;;) {
90 set_task_state(current, TASK_INTERRUPTIBLE);
91 if (button_pressed)
92 break;
93 flush_signals(current);
94 schedule();
95 }
96 __set_current_state(TASK_RUNNING);
97 remove_wait_queue(&powerd_wait, &wait);
98
99 /* Ok, down we go... */
100 button_pressed = 0;
101 if (execve("/sbin/shutdown", argv, envp) < 0) {
102 printk("powerd: shutdown execution failed\n");
103 add_wait_queue(&powerd_wait, &wait);
104 goto again;
105 }
106 return 0;
107}
108
David S. Miller690c8fd2006-06-22 19:12:03 -0700109static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
David S. Miller2256c132005-10-06 20:43:54 -0700111 if (irq == PCI_IRQ_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return 0;
David S. Miller690c8fd2006-06-22 19:12:03 -0700113 if (!of_find_property(dp, "button", NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return 0;
115
116 return 1;
117}
118
David S. Millerabbce6e2006-06-29 15:22:46 -0700119static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
David S. Millerabbce6e2006-06-29 15:22:46 -0700121 struct resource *res = &op->resource[0];
122 unsigned int irq= op->irqs[0];
David S. Millera2bd4fd2006-06-23 01:44:10 -0700123
David S. Millerabbce6e2006-06-29 15:22:46 -0700124 power_reg = of_ioremap(res, 0, 0x4, "power");
125
126 printk("%s: Control reg at %lx ... ",
127 op->node->name, res->start);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 poweroff_method = machine_halt; /* able to use the standard halt */
David S. Millera2bd4fd2006-06-23 01:44:10 -0700130
David S. Millerabbce6e2006-06-29 15:22:46 -0700131 if (has_button_interrupt(irq, op->node)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
133 printk("Failed to start power daemon.\n");
David S. Millerabbce6e2006-06-29 15:22:46 -0700134 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136 printk("powerd running.\n");
137
David S. Miller2256c132005-10-06 20:43:54 -0700138 if (request_irq(irq,
David S. Miller00cde672006-06-29 14:39:11 -0700139 power_handler, 0, "power", NULL) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 printk("power: Error, cannot register IRQ handler.\n");
141 } else {
142 printk("not using powerd.\n");
143 }
David S. Millerabbce6e2006-06-29 15:22:46 -0700144
145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
David S. Millera2bd4fd2006-06-23 01:44:10 -0700147
148static struct of_device_id power_match[] = {
149 {
150 .name = "power",
151 },
152 {},
153};
154
David S. Millerabbce6e2006-06-29 15:22:46 -0700155static struct of_platform_driver power_driver = {
David S. Millera2bd4fd2006-06-23 01:44:10 -0700156 .name = "power",
157 .match_table = power_match,
David S. Millerabbce6e2006-06-29 15:22:46 -0700158 .probe = power_probe,
David S. Millera2bd4fd2006-06-23 01:44:10 -0700159};
160
161void __init power_init(void)
162{
David S. Millerabbce6e2006-06-29 15:22:46 -0700163 of_register_driver(&power_driver, &of_bus_type);
David S. Millera2bd4fd2006-06-23 01:44:10 -0700164 return;
165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166#endif /* CONFIG_PCI */