time.c 8.5 KB

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