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