ip32-reset.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/notifier.h>
  14. #include <linux/delay.h>
  15. #include <linux/ds17287rtc.h>
  16. #include <linux/interrupt.h>
  17. #include <asm/addrspace.h>
  18. #include <asm/irq.h>
  19. #include <asm/reboot.h>
  20. #include <asm/system.h>
  21. #include <asm/wbflush.h>
  22. #include <asm/ip32/mace.h>
  23. #include <asm/ip32/crime.h>
  24. #include <asm/ip32/ip32_ints.h>
  25. #define POWERDOWN_TIMEOUT 120
  26. /*
  27. * Blink frequency during reboot grace period and when paniced.
  28. */
  29. #define POWERDOWN_FREQ (HZ / 4)
  30. #define PANIC_FREQ (HZ / 8)
  31. static struct timer_list power_timer, blink_timer, debounce_timer;
  32. static int has_paniced, shuting_down;
  33. static void ip32_machine_restart(char *command) __attribute__((noreturn));
  34. static void ip32_machine_halt(void) __attribute__((noreturn));
  35. static void ip32_machine_power_off(void) __attribute__((noreturn));
  36. static void ip32_machine_restart(char *cmd)
  37. {
  38. crime->control = CRIME_CONTROL_HARD_RESET;
  39. while (1);
  40. }
  41. static inline void ip32_machine_halt(void)
  42. {
  43. ip32_machine_power_off();
  44. }
  45. static void ip32_machine_power_off(void)
  46. {
  47. volatile unsigned char reg_a, xctrl_a, xctrl_b;
  48. disable_irq(MACEISA_RTC_IRQ);
  49. reg_a = CMOS_READ(RTC_REG_A);
  50. /* setup for kickstart & wake-up (DS12287 Ref. Man. p. 19) */
  51. reg_a &= ~DS_REGA_DV2;
  52. reg_a |= DS_REGA_DV1;
  53. CMOS_WRITE(reg_a | DS_REGA_DV0, RTC_REG_A);
  54. wbflush();
  55. xctrl_b = CMOS_READ(DS_B1_XCTRL4B)
  56. | DS_XCTRL4B_ABE | DS_XCTRL4B_KFE;
  57. CMOS_WRITE(xctrl_b, DS_B1_XCTRL4B);
  58. xctrl_a = CMOS_READ(DS_B1_XCTRL4A) & ~DS_XCTRL4A_IFS;
  59. CMOS_WRITE(xctrl_a, DS_B1_XCTRL4A);
  60. wbflush();
  61. /* adios amigos... */
  62. CMOS_WRITE(xctrl_a | DS_XCTRL4A_PAB, DS_B1_XCTRL4A);
  63. CMOS_WRITE(reg_a, RTC_REG_A);
  64. wbflush();
  65. while (1);
  66. }
  67. static void power_timeout(unsigned long data)
  68. {
  69. ip32_machine_power_off();
  70. }
  71. static void blink_timeout(unsigned long data)
  72. {
  73. unsigned long led = mace->perif.ctrl.misc ^ MACEISA_LED_RED;
  74. mace->perif.ctrl.misc = led;
  75. mod_timer(&blink_timer, jiffies + data);
  76. }
  77. static void debounce(unsigned long data)
  78. {
  79. volatile unsigned char reg_a, reg_c, xctrl_a;
  80. reg_c = CMOS_READ(RTC_INTR_FLAGS);
  81. CMOS_WRITE(reg_a | DS_REGA_DV0, RTC_REG_A);
  82. wbflush();
  83. xctrl_a = CMOS_READ(DS_B1_XCTRL4A);
  84. if ((xctrl_a & DS_XCTRL4A_IFS) || (reg_c & RTC_IRQF )) {
  85. /* Interrupt still being sent. */
  86. debounce_timer.expires = jiffies + 50;
  87. add_timer(&debounce_timer);
  88. /* clear interrupt source */
  89. CMOS_WRITE(xctrl_a & ~DS_XCTRL4A_IFS, DS_B1_XCTRL4A);
  90. CMOS_WRITE(reg_a & ~DS_REGA_DV0, RTC_REG_A);
  91. return;
  92. }
  93. CMOS_WRITE(reg_a & ~DS_REGA_DV0, RTC_REG_A);
  94. if (has_paniced)
  95. ip32_machine_restart(NULL);
  96. enable_irq(MACEISA_RTC_IRQ);
  97. }
  98. static inline void ip32_power_button(void)
  99. {
  100. if (has_paniced)
  101. return;
  102. if (shuting_down || kill_proc(1, SIGINT, 1)) {
  103. /* No init process or button pressed twice. */
  104. ip32_machine_power_off();
  105. }
  106. shuting_down = 1;
  107. blink_timer.data = POWERDOWN_FREQ;
  108. blink_timeout(POWERDOWN_FREQ);
  109. init_timer(&power_timer);
  110. power_timer.function = power_timeout;
  111. power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
  112. add_timer(&power_timer);
  113. }
  114. static irqreturn_t ip32_rtc_int(int irq, void *dev_id, struct pt_regs *regs)
  115. {
  116. volatile unsigned char reg_c;
  117. reg_c = CMOS_READ(RTC_INTR_FLAGS);
  118. if (!(reg_c & RTC_IRQF)) {
  119. printk(KERN_WARNING
  120. "%s: RTC IRQ without RTC_IRQF\n", __FUNCTION__);
  121. }
  122. /* Wait until interrupt goes away */
  123. disable_irq(MACEISA_RTC_IRQ);
  124. init_timer(&debounce_timer);
  125. debounce_timer.function = debounce;
  126. debounce_timer.expires = jiffies + 50;
  127. add_timer(&debounce_timer);
  128. printk(KERN_DEBUG "Power button pressed\n");
  129. ip32_power_button();
  130. return IRQ_HANDLED;
  131. }
  132. static int panic_event(struct notifier_block *this, unsigned long event,
  133. void *ptr)
  134. {
  135. unsigned long led;
  136. if (has_paniced)
  137. return NOTIFY_DONE;
  138. has_paniced = 1;
  139. /* turn off the green LED */
  140. led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
  141. mace->perif.ctrl.misc = led;
  142. blink_timer.data = PANIC_FREQ;
  143. blink_timeout(PANIC_FREQ);
  144. return NOTIFY_DONE;
  145. }
  146. static struct notifier_block panic_block = {
  147. .notifier_call = panic_event,
  148. };
  149. static __init int ip32_reboot_setup(void)
  150. {
  151. /* turn on the green led only */
  152. unsigned long led = mace->perif.ctrl.misc;
  153. led |= MACEISA_LED_RED;
  154. led &= ~MACEISA_LED_GREEN;
  155. mace->perif.ctrl.misc = led;
  156. _machine_restart = ip32_machine_restart;
  157. _machine_halt = ip32_machine_halt;
  158. _machine_power_off = ip32_machine_power_off;
  159. init_timer(&blink_timer);
  160. blink_timer.function = blink_timeout;
  161. notifier_chain_register(&panic_notifier_list, &panic_block);
  162. request_irq(MACEISA_RTC_IRQ, ip32_rtc_int, 0, "rtc", NULL);
  163. return 0;
  164. }
  165. subsys_initcall(ip32_reboot_setup);