power.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #define __KERNEL_SYSCALLS__
  7. #include <linux/config.h>
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/signal.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/pm.h>
  16. #include <asm/system.h>
  17. #include <asm/auxio.h>
  18. #include <asm/prom.h>
  19. #include <asm/of_device.h>
  20. #include <asm/io.h>
  21. #include <linux/unistd.h>
  22. /*
  23. * sysctl - toggle power-off restriction for serial console
  24. * systems in machine_power_off()
  25. */
  26. int scons_pwroff = 1;
  27. #ifdef CONFIG_PCI
  28. #include <linux/pci.h>
  29. static void __iomem *power_reg;
  30. static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
  31. static int button_pressed;
  32. static irqreturn_t power_handler(int irq, void *dev_id, struct pt_regs *regs)
  33. {
  34. if (button_pressed == 0) {
  35. button_pressed = 1;
  36. wake_up(&powerd_wait);
  37. }
  38. /* FIXME: Check registers for status... */
  39. return IRQ_HANDLED;
  40. }
  41. #endif /* CONFIG_PCI */
  42. extern void machine_halt(void);
  43. extern void machine_alt_power_off(void);
  44. static void (*poweroff_method)(void) = machine_alt_power_off;
  45. void machine_power_off(void)
  46. {
  47. if (!serial_console || scons_pwroff) {
  48. #ifdef CONFIG_PCI
  49. if (power_reg) {
  50. /* Both register bits seem to have the
  51. * same effect, so until I figure out
  52. * what the difference is...
  53. */
  54. writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
  55. } else
  56. #endif /* CONFIG_PCI */
  57. if (poweroff_method != NULL) {
  58. poweroff_method();
  59. /* not reached */
  60. }
  61. }
  62. machine_halt();
  63. }
  64. void (*pm_power_off)(void) = machine_power_off;
  65. EXPORT_SYMBOL(pm_power_off);
  66. #ifdef CONFIG_PCI
  67. static int powerd(void *__unused)
  68. {
  69. static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  70. char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
  71. DECLARE_WAITQUEUE(wait, current);
  72. daemonize("powerd");
  73. add_wait_queue(&powerd_wait, &wait);
  74. again:
  75. for (;;) {
  76. set_task_state(current, TASK_INTERRUPTIBLE);
  77. if (button_pressed)
  78. break;
  79. flush_signals(current);
  80. schedule();
  81. }
  82. __set_current_state(TASK_RUNNING);
  83. remove_wait_queue(&powerd_wait, &wait);
  84. /* Ok, down we go... */
  85. button_pressed = 0;
  86. if (execve("/sbin/shutdown", argv, envp) < 0) {
  87. printk("powerd: shutdown execution failed\n");
  88. add_wait_queue(&powerd_wait, &wait);
  89. goto again;
  90. }
  91. return 0;
  92. }
  93. static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
  94. {
  95. if (irq == PCI_IRQ_NONE)
  96. return 0;
  97. if (!of_find_property(dp, "button", NULL))
  98. return 0;
  99. return 1;
  100. }
  101. static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
  102. {
  103. struct resource *res = &op->resource[0];
  104. unsigned int irq= op->irqs[0];
  105. power_reg = of_ioremap(res, 0, 0x4, "power");
  106. printk("%s: Control reg at %lx ... ",
  107. op->node->name, res->start);
  108. poweroff_method = machine_halt; /* able to use the standard halt */
  109. if (has_button_interrupt(irq, op->node)) {
  110. if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
  111. printk("Failed to start power daemon.\n");
  112. return 0;
  113. }
  114. printk("powerd running.\n");
  115. if (request_irq(irq,
  116. power_handler, 0, "power", NULL) < 0)
  117. printk("power: Error, cannot register IRQ handler.\n");
  118. } else {
  119. printk("not using powerd.\n");
  120. }
  121. return 0;
  122. }
  123. static struct of_device_id power_match[] = {
  124. {
  125. .name = "power",
  126. },
  127. {},
  128. };
  129. static struct of_platform_driver power_driver = {
  130. .name = "power",
  131. .match_table = power_match,
  132. .probe = power_probe,
  133. };
  134. void __init power_init(void)
  135. {
  136. of_register_driver(&power_driver, &of_bus_type);
  137. return;
  138. }
  139. #endif /* CONFIG_PCI */