/arch/sparc64/kernel/power.c

https://bitbucket.org/evzijst/gittest · C · 150 lines · 112 code · 22 blank · 16 comment · 19 complexity · 825365056897bbfd47e86c205a5b68c5 MD5 · raw file

  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/config.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/sched.h>
  11. #include <linux/signal.h>
  12. #include <linux/delay.h>
  13. #include <linux/interrupt.h>
  14. #include <asm/system.h>
  15. #include <asm/ebus.h>
  16. #include <asm/auxio.h>
  17. #define __KERNEL_SYSCALLS__
  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. EXPORT_SYMBOL(machine_power_off);
  61. #ifdef CONFIG_PCI
  62. static int powerd(void *__unused)
  63. {
  64. static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  65. char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
  66. DECLARE_WAITQUEUE(wait, current);
  67. daemonize("powerd");
  68. add_wait_queue(&powerd_wait, &wait);
  69. again:
  70. for (;;) {
  71. set_task_state(current, TASK_INTERRUPTIBLE);
  72. if (button_pressed)
  73. break;
  74. flush_signals(current);
  75. schedule();
  76. }
  77. __set_current_state(TASK_RUNNING);
  78. remove_wait_queue(&powerd_wait, &wait);
  79. /* Ok, down we go... */
  80. button_pressed = 0;
  81. if (execve("/sbin/shutdown", argv, envp) < 0) {
  82. printk("powerd: shutdown execution failed\n");
  83. add_wait_queue(&powerd_wait, &wait);
  84. goto again;
  85. }
  86. return 0;
  87. }
  88. static int __init has_button_interrupt(struct linux_ebus_device *edev)
  89. {
  90. if (edev->irqs[0] == PCI_IRQ_NONE)
  91. return 0;
  92. if (!prom_node_has_property(edev->prom_node, "button"))
  93. return 0;
  94. return 1;
  95. }
  96. void __init power_init(void)
  97. {
  98. struct linux_ebus *ebus;
  99. struct linux_ebus_device *edev;
  100. static int invoked;
  101. if (invoked)
  102. return;
  103. invoked = 1;
  104. for_each_ebus(ebus) {
  105. for_each_ebusdev(edev, ebus) {
  106. if (!strcmp(edev->prom_name, "power"))
  107. goto found;
  108. }
  109. }
  110. return;
  111. found:
  112. power_reg = ioremap(edev->resource[0].start, 0x4);
  113. printk("power: Control reg at %p ... ", power_reg);
  114. poweroff_method = machine_halt; /* able to use the standard halt */
  115. if (has_button_interrupt(edev)) {
  116. if (kernel_thread(powerd, NULL, CLONE_FS) < 0) {
  117. printk("Failed to start power daemon.\n");
  118. return;
  119. }
  120. printk("powerd running.\n");
  121. if (request_irq(edev->irqs[0],
  122. power_handler, SA_SHIRQ, "power", NULL) < 0)
  123. printk("power: Error, cannot register IRQ handler.\n");
  124. } else {
  125. printk("not using powerd.\n");
  126. }
  127. }
  128. #endif /* CONFIG_PCI */