time.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * linux/arch/cris/arch-v10/kernel/time.c
  3. *
  4. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  5. * Copyright (C) 1999-2002 Axis Communications AB
  6. *
  7. */
  8. #include <linux/timex.h>
  9. #include <linux/time.h>
  10. #include <linux/jiffies.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/swap.h>
  13. #include <linux/sched.h>
  14. #include <linux/init.h>
  15. #include <linux/mm.h>
  16. #include <asm/types.h>
  17. #include <asm/signal.h>
  18. #include <asm/io.h>
  19. #include <asm/delay.h>
  20. #include <asm/irq_regs.h>
  21. /* define this if you need to use print_timestamp */
  22. /* it will make jiffies at 96 hz instead of 100 hz though */
  23. #undef USE_CASCADE_TIMERS
  24. unsigned long get_ns_in_jiffie(void)
  25. {
  26. unsigned char timer_count, t1;
  27. unsigned short presc_count;
  28. unsigned long ns;
  29. unsigned long flags;
  30. local_irq_save(flags);
  31. timer_count = *R_TIMER0_DATA;
  32. presc_count = *R_TIM_PRESC_STATUS;
  33. /* presc_count might be wrapped */
  34. t1 = *R_TIMER0_DATA;
  35. if (timer_count != t1){
  36. /* it wrapped, read prescaler again... */
  37. presc_count = *R_TIM_PRESC_STATUS;
  38. timer_count = t1;
  39. }
  40. local_irq_restore(flags);
  41. if (presc_count >= PRESCALE_VALUE/2 ){
  42. presc_count = PRESCALE_VALUE - presc_count + PRESCALE_VALUE/2;
  43. } else {
  44. presc_count = PRESCALE_VALUE - presc_count - PRESCALE_VALUE/2;
  45. }
  46. ns = ( (TIMER0_DIV - timer_count) * ((1000000000/HZ)/TIMER0_DIV )) +
  47. ( (presc_count) * (1000000000/PRESCALE_FREQ));
  48. return ns;
  49. }
  50. static u32 cris_v10_gettimeoffset(void)
  51. {
  52. u32 count;
  53. /* The timer interrupt comes from Etrax timer 0. In order to get
  54. * better precision, we check the current value. It might have
  55. * underflowed already though.
  56. */
  57. count = *R_TIMER0_DATA;
  58. /* Convert timer value to nsec */
  59. return (TIMER0_DIV - count) * (NSEC_PER_SEC/HZ)/TIMER0_DIV;
  60. }
  61. /* Excerpt from the Etrax100 HSDD about the built-in watchdog:
  62. *
  63. * 3.10.4 Watchdog timer
  64. * When the watchdog timer is started, it generates an NMI if the watchdog
  65. * isn't restarted or stopped within 0.1 s. If it still isn't restarted or
  66. * stopped after an additional 3.3 ms, the watchdog resets the chip.
  67. * The watchdog timer is stopped after reset. The watchdog timer is controlled
  68. * by the R_WATCHDOG register. The R_WATCHDOG register contains an enable bit
  69. * and a 3-bit key value. The effect of writing to the R_WATCHDOG register is
  70. * described in the table below:
  71. *
  72. * Watchdog Value written:
  73. * state: To enable: To key: Operation:
  74. * -------- ---------- ------- ----------
  75. * stopped 0 X No effect.
  76. * stopped 1 key_val Start watchdog with key = key_val.
  77. * started 0 ~key Stop watchdog
  78. * started 1 ~key Restart watchdog with key = ~key.
  79. * started X new_key_val Change key to new_key_val.
  80. *
  81. * Note: '~' is the bitwise NOT operator.
  82. *
  83. */
  84. /* right now, starting the watchdog is the same as resetting it */
  85. #define start_watchdog reset_watchdog
  86. #ifdef CONFIG_ETRAX_WATCHDOG
  87. static int watchdog_key = 0; /* arbitrary number */
  88. #endif
  89. /* number of pages to consider "out of memory". it is normal that the memory
  90. * is used though, so put this really low.
  91. */
  92. #define WATCHDOG_MIN_FREE_PAGES 8
  93. void reset_watchdog(void)
  94. {
  95. #if defined(CONFIG_ETRAX_WATCHDOG)
  96. /* only keep watchdog happy as long as we have memory left! */
  97. if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
  98. /* reset the watchdog with the inverse of the old key */
  99. watchdog_key ^= 0x7; /* invert key, which is 3 bits */
  100. *R_WATCHDOG = IO_FIELD(R_WATCHDOG, key, watchdog_key) |
  101. IO_STATE(R_WATCHDOG, enable, start);
  102. }
  103. #endif
  104. }
  105. /* stop the watchdog - we still need the correct key */
  106. void stop_watchdog(void)
  107. {
  108. #ifdef CONFIG_ETRAX_WATCHDOG
  109. watchdog_key ^= 0x7; /* invert key, which is 3 bits */
  110. *R_WATCHDOG = IO_FIELD(R_WATCHDOG, key, watchdog_key) |
  111. IO_STATE(R_WATCHDOG, enable, stop);
  112. #endif
  113. }
  114. extern void cris_do_profile(struct pt_regs *regs);
  115. /*
  116. * timer_interrupt() needs to keep up the real-time clock,
  117. * as well as call the "xtime_update()" routine every clocktick
  118. */
  119. static inline irqreturn_t timer_interrupt(int irq, void *dev_id)
  120. {
  121. struct pt_regs *regs = get_irq_regs();
  122. /* acknowledge the timer irq */
  123. #ifdef USE_CASCADE_TIMERS
  124. *R_TIMER_CTRL =
  125. IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |
  126. IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |
  127. IO_STATE( R_TIMER_CTRL, i1, clr) |
  128. IO_STATE( R_TIMER_CTRL, tm1, run) |
  129. IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |
  130. IO_STATE( R_TIMER_CTRL, i0, clr) |
  131. IO_STATE( R_TIMER_CTRL, tm0, run) |
  132. IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);
  133. #else
  134. *R_TIMER_CTRL = r_timer_ctrl_shadow | IO_STATE(R_TIMER_CTRL, i0, clr);
  135. #endif
  136. /* reset watchdog otherwise it resets us! */
  137. reset_watchdog();
  138. /* Update statistics. */
  139. update_process_times(user_mode(regs));
  140. /* call the real timer interrupt handler */
  141. xtime_update(1);
  142. cris_do_profile(regs); /* Save profiling information */
  143. return IRQ_HANDLED;
  144. }
  145. /* timer is IRQF_SHARED so drivers can add stuff to the timer irq chain */
  146. static struct irqaction irq2 = {
  147. .handler = timer_interrupt,
  148. .flags = IRQF_SHARED,
  149. .name = "timer",
  150. };
  151. void __init time_init(void)
  152. {
  153. arch_gettimeoffset = cris_v10_gettimeoffset;
  154. /* probe for the RTC and read it if it exists
  155. * Before the RTC can be probed the loops_per_usec variable needs
  156. * to be initialized to make usleep work. A better value for
  157. * loops_per_usec is calculated by the kernel later once the
  158. * clock has started.
  159. */
  160. loops_per_usec = 50;
  161. /* Setup the etrax timers
  162. * Base frequency is 25000 hz, divider 250 -> 100 HZ
  163. * In normal mode, we use timer0, so timer1 is free. In cascade
  164. * mode (which we sometimes use for debugging) both timers are used.
  165. * Remember that linux/timex.h contains #defines that rely on the
  166. * timer settings below (hz and divide factor) !!!
  167. */
  168. #ifdef USE_CASCADE_TIMERS
  169. *R_TIMER_CTRL =
  170. IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |
  171. IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |
  172. IO_STATE( R_TIMER_CTRL, i1, nop) |
  173. IO_STATE( R_TIMER_CTRL, tm1, stop_ld) |
  174. IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |
  175. IO_STATE( R_TIMER_CTRL, i0, nop) |
  176. IO_STATE( R_TIMER_CTRL, tm0, stop_ld) |
  177. IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);
  178. *R_TIMER_CTRL = r_timer_ctrl_shadow =
  179. IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |
  180. IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |
  181. IO_STATE( R_TIMER_CTRL, i1, nop) |
  182. IO_STATE( R_TIMER_CTRL, tm1, run) |
  183. IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |
  184. IO_STATE( R_TIMER_CTRL, i0, nop) |
  185. IO_STATE( R_TIMER_CTRL, tm0, run) |
  186. IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);
  187. #else
  188. *R_TIMER_CTRL =
  189. IO_FIELD(R_TIMER_CTRL, timerdiv1, 192) |
  190. IO_FIELD(R_TIMER_CTRL, timerdiv0, TIMER0_DIV) |
  191. IO_STATE(R_TIMER_CTRL, i1, nop) |
  192. IO_STATE(R_TIMER_CTRL, tm1, stop_ld) |
  193. IO_STATE(R_TIMER_CTRL, clksel1, c19k2Hz) |
  194. IO_STATE(R_TIMER_CTRL, i0, nop) |
  195. IO_STATE(R_TIMER_CTRL, tm0, stop_ld) |
  196. IO_STATE(R_TIMER_CTRL, clksel0, flexible);
  197. *R_TIMER_CTRL = r_timer_ctrl_shadow =
  198. IO_FIELD(R_TIMER_CTRL, timerdiv1, 192) |
  199. IO_FIELD(R_TIMER_CTRL, timerdiv0, TIMER0_DIV) |
  200. IO_STATE(R_TIMER_CTRL, i1, nop) |
  201. IO_STATE(R_TIMER_CTRL, tm1, run) |
  202. IO_STATE(R_TIMER_CTRL, clksel1, c19k2Hz) |
  203. IO_STATE(R_TIMER_CTRL, i0, nop) |
  204. IO_STATE(R_TIMER_CTRL, tm0, run) |
  205. IO_STATE(R_TIMER_CTRL, clksel0, flexible);
  206. *R_TIMER_PRESCALE = PRESCALE_VALUE;
  207. #endif
  208. /* unmask the timer irq */
  209. *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, timer0, set);
  210. /* now actually register the irq handler that calls timer_interrupt() */
  211. setup_irq(2, &irq2); /* irq 2 is the timer0 irq in etrax */
  212. /* enable watchdog if we should use one */
  213. #if defined(CONFIG_ETRAX_WATCHDOG)
  214. printk("Enabling watchdog...\n");
  215. start_watchdog();
  216. /* If we use the hardware watchdog, we want to trap it as an NMI
  217. and dump registers before it resets us. For this to happen, we
  218. must set the "m" NMI enable flag (which once set, is unset only
  219. when an NMI is taken).
  220. The same goes for the external NMI, but that doesn't have any
  221. driver or infrastructure support yet. */
  222. asm ("setf m");
  223. *R_IRQ_MASK0_SET = IO_STATE(R_IRQ_MASK0_SET, watchdog_nmi, set);
  224. *R_VECT_MASK_SET = IO_STATE(R_VECT_MASK_SET, nmi, set);
  225. #endif
  226. }