ip32-reset.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <[email protected]>
  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. #define POWERDOWN_TIMEOUT 120
  30. /*
  31. * Blink frequency during reboot grace period and when panicked.
  32. */
  33. #define POWERDOWN_FREQ (HZ / 4)
  34. #define PANIC_FREQ (HZ / 8)
  35. extern struct platform_device ip32_rtc_device;
  36. static struct timer_list power_timer, blink_timer;
  37. static unsigned long blink_timer_timeout;
  38. static int has_panicked, shutting_down;
  39. static __noreturn void ip32_poweroff(void *data)
  40. {
  41. void (*poweroff_func)(struct platform_device *) =
  42. symbol_get(ds1685_rtc_poweroff);
  43. #ifdef CONFIG_MODULES
  44. /* If the first __symbol_get failed, our module wasn't loaded. */
  45. if (!poweroff_func) {
  46. request_module("rtc-ds1685");
  47. poweroff_func = symbol_get(ds1685_rtc_poweroff);
  48. }
  49. #endif
  50. if (!poweroff_func)
  51. pr_emerg("RTC not available for power-off. Spinning forever ...\n");
  52. else {
  53. (*poweroff_func)((struct platform_device *)data);
  54. symbol_put(ds1685_rtc_poweroff);
  55. }
  56. unreachable();
  57. }
  58. static void ip32_machine_restart(char *cmd) __noreturn;
  59. static void ip32_machine_restart(char *cmd)
  60. {
  61. msleep(20);
  62. crime->control = CRIME_CONTROL_HARD_RESET;
  63. unreachable();
  64. }
  65. static void blink_timeout(struct timer_list *unused)
  66. {
  67. unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
  68. mace->perif.ctrl.misc = led;
  69. mod_timer(&blink_timer, jiffies + blink_timer_timeout);
  70. }
  71. static void ip32_machine_halt(void)
  72. {
  73. ip32_poweroff(&ip32_rtc_device);
  74. }
  75. static void power_timeout(struct timer_list *unused)
  76. {
  77. ip32_poweroff(&ip32_rtc_device);
  78. }
  79. void ip32_prepare_poweroff(void)
  80. {
  81. if (has_panicked)
  82. return;
  83. if (shutting_down || kill_cad_pid(SIGINT, 1)) {
  84. /* No init process or button pressed twice. */
  85. ip32_poweroff(&ip32_rtc_device);
  86. }
  87. shutting_down = 1;
  88. blink_timer_timeout = POWERDOWN_FREQ;
  89. blink_timeout(&blink_timer);
  90. timer_setup(&power_timer, power_timeout, 0);
  91. power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
  92. add_timer(&power_timer);
  93. }
  94. static int panic_event(struct notifier_block *this, unsigned long event,
  95. void *ptr)
  96. {
  97. unsigned long led;
  98. if (has_panicked)
  99. return NOTIFY_DONE;
  100. has_panicked = 1;
  101. /* turn off the green LED */
  102. led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
  103. mace->perif.ctrl.misc = led;
  104. blink_timer_timeout = PANIC_FREQ;
  105. blink_timeout(&blink_timer);
  106. return NOTIFY_DONE;
  107. }
  108. static struct notifier_block panic_block = {
  109. .notifier_call = panic_event,
  110. };
  111. static __init int ip32_reboot_setup(void)
  112. {
  113. /* turn on the green led only */
  114. unsigned long led = mace->perif.ctrl.misc;
  115. led |= MACEISA_LED_RED;
  116. led &= ~MACEISA_LED_GREEN;
  117. mace->perif.ctrl.misc = led;
  118. _machine_restart = ip32_machine_restart;
  119. _machine_halt = ip32_machine_halt;
  120. pm_power_off = ip32_machine_halt;
  121. timer_setup(&blink_timer, blink_timeout, 0);
  122. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  123. return 0;
  124. }
  125. subsys_initcall(ip32_reboot_setup);