power.c 3.3 KB

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