ip32-reset.c 4.9 KB

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