power.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* power.c: Power management driver.
  2. *
  3. * Copyright (C) 1999, 2007 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/sched.h>
  9. #include <linux/signal.h>
  10. #include <linux/delay.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/pm.h>
  13. #include <linux/syscalls.h>
  14. #include <asm/system.h>
  15. #include <asm/auxio.h>
  16. #include <asm/prom.h>
  17. #include <asm/of_device.h>
  18. #include <asm/io.h>
  19. #include <asm/power.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. static void __iomem *power_reg;
  28. static DECLARE_WAIT_QUEUE_HEAD(powerd_wait);
  29. static int button_pressed;
  30. void wake_up_powerd(void)
  31. {
  32. if (button_pressed == 0) {
  33. button_pressed = 1;
  34. wake_up(&powerd_wait);
  35. }
  36. }
  37. static irqreturn_t power_handler(int irq, void *dev_id)
  38. {
  39. wake_up_powerd();
  40. /* FIXME: Check registers for status... */
  41. return IRQ_HANDLED;
  42. }
  43. extern void machine_halt(void);
  44. extern void machine_alt_power_off(void);
  45. static void (*poweroff_method)(void) = machine_alt_power_off;
  46. void machine_power_off(void)
  47. {
  48. sstate_poweroff();
  49. if (!serial_console || scons_pwroff) {
  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. if (poweroff_method != NULL) {
  58. poweroff_method();
  59. /* not reached */
  60. }
  61. }
  62. }
  63. machine_halt();
  64. }
  65. void (*pm_power_off)(void) = machine_power_off;
  66. EXPORT_SYMBOL(pm_power_off);
  67. static int powerd(void *__unused)
  68. {
  69. static char *envp[] = { "HOME=/", "TERM=linux", "PATH=/sbin:/usr/sbin:/bin:/usr/bin", NULL };
  70. char *argv[] = { "/sbin/shutdown", "-h", "now", NULL };
  71. DECLARE_WAITQUEUE(wait, current);
  72. daemonize("powerd");
  73. add_wait_queue(&powerd_wait, &wait);
  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(KERN_ERR "powerd: shutdown execution failed\n");
  87. machine_power_off();
  88. }
  89. return 0;
  90. }
  91. int start_powerd(void)
  92. {
  93. int err;
  94. err = kernel_thread(powerd, NULL, CLONE_FS);
  95. if (err < 0)
  96. printk(KERN_ERR "power: Failed to start power daemon.\n");
  97. else
  98. printk(KERN_INFO "power: powerd running.\n");
  99. return err;
  100. }
  101. static int __init has_button_interrupt(unsigned int irq, struct device_node *dp)
  102. {
  103. if (irq == 0xffffffff)
  104. return 0;
  105. if (!of_find_property(dp, "button", NULL))
  106. return 0;
  107. return 1;
  108. }
  109. static int __devinit power_probe(struct of_device *op, const struct of_device_id *match)
  110. {
  111. struct resource *res = &op->resource[0];
  112. unsigned int irq= op->irqs[0];
  113. power_reg = of_ioremap(res, 0, 0x4, "power");
  114. printk("%s: Control reg at %lx ... ",
  115. op->node->name, res->start);
  116. poweroff_method = machine_halt; /* able to use the standard halt */
  117. if (has_button_interrupt(irq, op->node)) {
  118. if (start_powerd() < 0)
  119. return 0;
  120. if (request_irq(irq,
  121. power_handler, 0, "power", NULL) < 0)
  122. printk(KERN_ERR "power: Cannot setup IRQ handler.\n");
  123. } else {
  124. printk(KERN_INFO "power: Not using powerd.\n");
  125. }
  126. return 0;
  127. }
  128. static struct of_device_id power_match[] = {
  129. {
  130. .name = "power",
  131. },
  132. {},
  133. };
  134. static struct of_platform_driver power_driver = {
  135. .name = "power",
  136. .match_table = power_match,
  137. .probe = power_probe,
  138. };
  139. void __init power_init(void)
  140. {
  141. of_register_driver(&power_driver, &of_bus_type);
  142. return;
  143. }