power.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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/ebus.h>
  18. #include <asm/isa.h>
  19. #include <asm/auxio.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. static void __iomem *power_reg;
  28. static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
  29. static int button_pressed;
  30. static irqreturn_t power_handler(int irq, void *dev_id, struct pt_regs *regs)
  31. {
  32. if (button_pressed == 0) {
  33. button_pressed = 1;
  34. wake_up(&powerd_wait);
  35. }
  36. /* FIXME: Check registers for status... */
  37. return IRQ_HANDLED;
  38. }
  39. #endif /* CONFIG_PCI */
  40. extern void machine_halt(void);
  41. extern void machine_alt_power_off(void);
  42. static void (*poweroff_method)(void) = machine_alt_power_off;
  43. void machine_power_off(void)
  44. {
  45. if (!serial_console || scons_pwroff) {
  46. #ifdef CONFIG_PCI
  47. if (power_reg) {
  48. /* Both register bits seem to have the
  49. * same effect, so until I figure out
  50. * what the difference is...
  51. */
  52. writel(AUXIO_PCIO_CPWR_OFF | AUXIO_PCIO_SPWR_OFF, power_reg);
  53. } else
  54. #endif /* CONFIG_PCI */
  55. if (poweroff_method != NULL) {
  56. poweroff_method();
  57. /* not reached */
  58. }
  59. }
  60. machine_halt();
  61. }
  62. void (*pm_power_off)(void) = machine_power_off;
  63. EXPORT_SYMBOL(pm_power_off);
  64. #ifdef CONFIG_PCI
  65. static int powerd(void *__unused)
  66. {
  67. static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  68. char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
  69. DECLARE_WAITQUEUE(wait, current);
  70. daemonize("powerd");
  71. add_wait_queue(&powerd_wait, &wait);
  72. again:
  73. for (;;) {
  74. set_task_state(current, TASK_INTERRUPTIBLE);
  75. if (button_pressed)
  76. break;
  77. flush_signals(current);
  78. schedule();
  79. }
  80. __set_current_state(TASK_RUNNING);
  81. remove_wait_queue(&powerd_wait, &wait);
  82. /* Ok, down we go... */
  83. button_pressed = 0;
  84. if (execve("/sbin/shutdown", argv, envp) < 0) {
  85. printk("powerd: shutdown execution failed\n");
  86. add_wait_queue(&powerd_wait, &wait);
  87. goto again;
  88. }
  89. return 0;
  90. }
  91. static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
  92. {
  93. if (irq == PCI_IRQ_NONE)
  94. return 0;
  95. if (!of_find_property(dp, "button", NULL))
  96. return 0;
  97. return 1;
  98. }
  99. static void __devinit power_probe_common(struct of_device *dev, struct resource *res, unsigned int irq)
  100. {
  101. power_reg = ioremap(res->start, 0x4);
  102. printk("power: Control reg at %p ... ", power_reg);
  103. poweroff_method = machine_halt; /* able to use the standard halt */
  104. if (has_button_interrupt(irq, dev->node)) {
  105. if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
  106. printk("Failed to start power daemon.\n");
  107. return;
  108. }
  109. printk("powerd running.\n");
  110. if (request_irq(irq,
  111. power_handler, SA_SHIRQ, "power", NULL) < 0)
  112. printk("power: Error, cannot register IRQ handler.\n");
  113. } else {
  114. printk("not using powerd.\n");
  115. }
  116. }
  117. static struct of_device_id power_match[] = {
  118. {
  119. .name = "power",
  120. },
  121. {},
  122. };
  123. static int __devinit ebus_power_probe(struct of_device *dev, const struct of_device_id *match)
  124. {
  125. struct linux_ebus_device *edev = to_ebus_device(&dev->dev);
  126. struct resource *res = &edev->resource[0];
  127. unsigned int irq = edev->irqs[0];
  128. power_probe_common(dev, res,irq);
  129. return 0;
  130. }
  131. static struct of_platform_driver ebus_power_driver = {
  132. .name = "power",
  133. .match_table = power_match,
  134. .probe = ebus_power_probe,
  135. };
  136. static int __devinit isa_power_probe(struct of_device *dev, const struct of_device_id *match)
  137. {
  138. struct sparc_isa_device *idev = to_isa_device(&dev->dev);
  139. struct resource *res = &idev->resource;
  140. unsigned int irq = idev->irq;
  141. power_probe_common(dev, res,irq);
  142. return 0;
  143. }
  144. static struct of_platform_driver isa_power_driver = {
  145. .name = "power",
  146. .match_table = power_match,
  147. .probe = isa_power_probe,
  148. };
  149. void __init power_init(void)
  150. {
  151. of_register_driver(&ebus_power_driver, &ebus_bus_type);
  152. of_register_driver(&isa_power_driver, &isa_bus_type);
  153. return;
  154. }
  155. #endif /* CONFIG_PCI */