power.c 3.6 KB

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