time.c 8.6 KB

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