ip32-reset.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2001 Keith M Wesolowski
  7. * Copyright (C) 2001 Paul Mundt
  8. * Copyright (C) 2003 Guido Guenther <agx@sigxcpu.org>
  9. */
  10. #include <linux/compiler.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/panic_notifier.h>
  15. #include <linux/sched.h>
  16. #include <linux/sched/signal.h>
  17. #include <linux/notifier.h>
  18. #include <linux/delay.h>
  19. #include <linux/rtc/ds1685.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/pm.h>
  22. #include <asm/addrspace.h>
  23. #include <asm/irq.h>
  24. #include <asm/reboot.h>
  25. #include <asm/wbflush.h>
  26. #include <asm/ip32/mace.h>
  27. #include <asm/ip32/crime.h>
  28. #include <asm/ip32/ip32_ints.h>
  29. #include "ip32-common.h"
  30. #define POWERDOWN_TIMEOUT 120
  31. /*
  32. * Blink frequency during reboot grace period and when panicked.
  33. */
  34. #define POWERDOWN_FREQ (HZ / 4)
  35. #define PANIC_FREQ (HZ / 8)
  36. extern struct platform_device ip32_rtc_device;
  37. static struct timer_list power_timer, blink_timer;
  38. static unsigned long blink_timer_timeout;
  39. static int has_panicked, shutting_down;
  40. static __noreturn void ip32_poweroff(void *data)
  41. {
  42. void (*poweroff_func)(struct platform_device *) =
  43. symbol_get(ds1685_rtc_poweroff);
  44. #ifdef CONFIG_MODULES
  45. /* If the first __symbol_get failed, our module wasn't loaded. */
  46. if (!poweroff_func) {
  47. request_module("rtc-ds1685");
  48. poweroff_func = symbol_get(ds1685_rtc_poweroff);
  49. }
  50. #endif
  51. if (!poweroff_func)
  52. pr_emerg("RTC not available for power-off. Spinning forever ...\n");
  53. else {
  54. (*poweroff_func)((struct platform_device *)data);
  55. symbol_put(ds1685_rtc_poweroff);
  56. }
  57. unreachable();
  58. }
  59. static void ip32_machine_restart(char *cmd) __noreturn;
  60. static void ip32_machine_restart(char *cmd)
  61. {
  62. msleep(20);
  63. crime->control = CRIME_CONTROL_HARD_RESET;
  64. unreachable();
  65. }
  66. static void blink_timeout(struct timer_list *unused)
  67. {
  68. unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
  69. mace->perif.ctrl.misc = led;
  70. mod_timer(&blink_timer, jiffies + blink_timer_timeout);
  71. }
  72. static void ip32_machine_halt(void)
  73. {
  74. ip32_poweroff(&ip32_rtc_device);
  75. }
  76. static void power_timeout(struct timer_list *unused)
  77. {
  78. ip32_poweroff(&ip32_rtc_device);
  79. }
  80. void ip32_prepare_poweroff(void)
  81. {
  82. if (has_panicked)
  83. return;
  84. if (shutting_down || kill_cad_pid(SIGINT, 1)) {
  85. /* No init process or button pressed twice. */
  86. ip32_poweroff(&ip32_rtc_device);
  87. }
  88. shutting_down = 1;
  89. blink_timer_timeout = POWERDOWN_FREQ;
  90. blink_timeout(&blink_timer);
  91. timer_setup(&power_timer, power_timeout, 0);
  92. power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
  93. add_timer(&power_timer);
  94. }
  95. static int panic_event(struct notifier_block *this, unsigned long event,
  96. void *ptr)
  97. {
  98. unsigned long led;
  99. if (has_panicked)
  100. return NOTIFY_DONE;
  101. has_panicked = 1;
  102. /* turn off the green LED */
  103. led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
  104. mace->perif.ctrl.misc = led;
  105. blink_timer_timeout = PANIC_FREQ;
  106. blink_timeout(&blink_timer);
  107. return NOTIFY_DONE;
  108. }
  109. static struct notifier_block panic_block = {
  110. .notifier_call = panic_event,
  111. };
  112. static __init int ip32_reboot_setup(void)
  113. {
  114. /* turn on the green led only */
  115. unsigned long led = mace->perif.ctrl.misc;
  116. led |= MACEISA_LED_RED;
  117. led &= ~MACEISA_LED_GREEN;
  118. mace->perif.ctrl.misc = led;
  119. _machine_restart = ip32_machine_restart;
  120. _machine_halt = ip32_machine_halt;
  121. pm_power_off = ip32_machine_halt;
  122. timer_setup(&blink_timer, blink_timeout, 0);
  123. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  124. return 0;
  125. }
  126. subsys_initcall(ip32_reboot_setup);