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/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 panicked.
  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_panicked, 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. 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. unsigned char reg_a, reg_c, xctrl_a;
  80. reg_c = CMOS_READ(RTC_INTR_FLAGS);
  81. reg_a = CMOS_READ(RTC_REG_A);
  82. CMOS_WRITE(reg_a | DS_REGA_DV0, RTC_REG_A);
  83. wbflush();
  84. xctrl_a = CMOS_READ(DS_B1_XCTRL4A);
  85. if ((xctrl_a & DS_XCTRL4A_IFS) || (reg_c & RTC_IRQF )) {
  86. /* Interrupt still being sent. */
  87. debounce_timer.expires = jiffies + 50;
  88. add_timer(&debounce_timer);
  89. /* clear interrupt source */
  90. CMOS_WRITE(xctrl_a & ~DS_XCTRL4A_IFS, DS_B1_XCTRL4A);
  91. CMOS_WRITE(reg_a & ~DS_REGA_DV0, RTC_REG_A);
  92. return;
  93. }
  94. CMOS_WRITE(reg_a & ~DS_REGA_DV0, RTC_REG_A);
  95. if (has_panicked)
  96. ip32_machine_restart(NULL);
  97. enable_irq(MACEISA_RTC_IRQ);
  98. }
  99. static inline void ip32_power_button(void)
  100. {
  101. if (has_panicked)
  102. return;
  103. if (shuting_down || kill_cad_pid(SIGINT, 1)) {
  104. /* No init process or button pressed twice. */
  105. ip32_machine_power_off();
  106. }
  107. shuting_down = 1;
  108. blink_timer.data = POWERDOWN_FREQ;
  109. blink_timeout(POWERDOWN_FREQ);
  110. init_timer(&power_timer);
  111. power_timer.function = power_timeout;
  112. power_timer.expires = jiffies + POWERDOWN_TIMEOUT * HZ;
  113. add_timer(&power_timer);
  114. }
  115. static irqreturn_t ip32_rtc_int(int irq, void *dev_id)
  116. {
  117. unsigned char reg_c;
  118. reg_c = CMOS_READ(RTC_INTR_FLAGS);
  119. if (!(reg_c & RTC_IRQF)) {
  120. printk(KERN_WARNING
  121. "%s: RTC IRQ without RTC_IRQF\n", __func__);
  122. }
  123. /* Wait until interrupt goes away */
  124. disable_irq_nosync(MACEISA_RTC_IRQ);
  125. init_timer(&debounce_timer);
  126. debounce_timer.function = debounce;
  127. debounce_timer.expires = jiffies + 50;
  128. add_timer(&debounce_timer);
  129. printk(KERN_DEBUG "Power button pressed\n");
  130. ip32_power_button();
  131. return IRQ_HANDLED;
  132. }
  133. static int panic_event(struct notifier_block *this, unsigned long event,
  134. void *ptr)
  135. {
  136. unsigned long led;
  137. if (has_panicked)
  138. return NOTIFY_DONE;
  139. has_panicked = 1;
  140. /* turn off the green LED */
  141. led = mace->perif.ctrl.misc | MACEISA_LED_GREEN;
  142. mace->perif.ctrl.misc = led;
  143. blink_timer.data = PANIC_FREQ;
  144. blink_timeout(PANIC_FREQ);
  145. return NOTIFY_DONE;
  146. }
  147. static struct notifier_block panic_block = {
  148. .notifier_call = panic_event,
  149. };
  150. static __init int ip32_reboot_setup(void)
  151. {
  152. /* turn on the green led only */
  153. unsigned long led = mace->perif.ctrl.misc;
  154. led |= MACEISA_LED_RED;
  155. led &= ~MACEISA_LED_GREEN;
  156. mace->perif.ctrl.misc = led;
  157. _machine_restart = ip32_machine_restart;
  158. _machine_halt = ip32_machine_halt;
  159. pm_power_off = ip32_machine_power_off;
  160. init_timer(&blink_timer);
  161. blink_timer.function = blink_timeout;
  162. atomic_notifier_chain_register(&panic_notifier_list, &panic_block);
  163. if (request_irq(MACEISA_RTC_IRQ, ip32_rtc_int, 0, "rtc", NULL))
  164. panic("Can't allocate MACEISA RTC IRQ");
  165. return 0;
  166. }
  167. subsys_initcall(ip32_reboot_setup);