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. #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 <asm/sstate.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)
  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. sstate_poweroff();
  48. if (!serial_console || scons_pwroff) {
  49. #ifdef CONFIG_PCI
  50. if (power_reg) {
  51. /* Both register bits seem to have the
  52. * same effect, so until I figure out
  53. * what the difference is...
  54. */
  55. writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
  56. } else
  57. #endif /* CONFIG_PCI */
  58. if (poweroff_method != NULL) {
  59. poweroff_method();
  60. /* not reached */
  61. }
  62. }
  63. machine_halt();
  64. }
  65. void (*pm_power_off)(void) = machine_power_off;
  66. EXPORT_SYMBOL(pm_power_off);
  67. #ifdef CONFIG_PCI
  68. static int powerd(void *__unused)
  69. {
  70. static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  71. char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
  72. DECLARE_WAITQUEUE(wait, current);
  73. daemonize("powerd");
  74. add_wait_queue(&powerd_wait, &wait);
  75. again:
  76. for (;;) {
  77. set_task_state(current, TASK_INTERRUPTIBLE);
  78. if (button_pressed)
  79. break;
  80. flush_signals(current);
  81. schedule();
  82. }
  83. __set_current_state(TASK_RUNNING);
  84. remove_wait_queue(&powerd_wait, &wait);
  85. /* Ok, down we go... */
  86. button_pressed = 0;
  87. if (kernel_execve("/sbin/shutdown", argv, envp) < 0) {
  88. printk("powerd: shutdown execution failed\n");
  89. add_wait_queue(&powerd_wait, &wait);
  90. goto again;
  91. }
  92. return 0;
  93. }
  94. static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
  95. {
  96. if (irq == PCI_IRQ_NONE)
  97. return 0;
  98. if (!of_find_property(dp, "button", NULL))
  99. return 0;
  100. return 1;
  101. }
  102. static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
  103. {
  104. struct resource *res = &op->resource[0];
  105. unsigned int irq= op->irqs[0];
  106. power_reg = of_ioremap(res, 0, 0x4, "power");
  107. printk("%s: Control reg at %lx ... ",
  108. op->node->name, res->start);
  109. poweroff_method = machine_halt; /* able to use the standard halt */
  110. if (has_button_interrupt(irq, op->node)) {
  111. if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
  112. printk("Failed to start power daemon.\n");
  113. return 0;
  114. }
  115. printk("powerd running.\n");
  116. if (request_irq(irq,
  117. power_handler, 0, "power", NULL) < 0)
  118. printk("power: Error, cannot register IRQ handler.\n");
  119. } else {
  120. printk("not using powerd.\n");
  121. }
  122. return 0;
  123. }
  124. static struct of_device_id power_match[] = {
  125. {
  126. .name = "power",
  127. },
  128. {},
  129. };
  130. static struct of_platform_driver power_driver = {
  131. .name = "power",
  132. .match_table = power_match,
  133. .probe = power_probe,
  134. };
  135. void __init power_init(void)
  136. {
  137. of_register_driver(&power_driver, &of_bus_type);
  138. return;
  139. }
  140. #endif /* CONFIG_PCI */