time.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /* $Id: time.c,v 1.5 2004/09/29 06:12:46 starvik Exp $
  2. *
  3. * linux/arch/cris/arch-v10/kernel/time.c
  4. *
  5. * Copyright (C) 1991, 1992, 1995 Linus Torvalds
  6. * Copyright (C) 1999-2002 Axis Communications AB
  7. *
  8. */
  9. #include <linux/timex.h>
  10. #include <linux/time.h>
  11. #include <linux/jiffies.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/swap.h>
  14. #include <linux/sched.h>
  15. #include <linux/init.h>
  16. #include <asm/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. /* 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. extern void update_xtime_from_cmos(void);
  26. extern int set_rtc_mmss(unsigned long nowtime);
  27. extern int setup_irq(int, struct irqaction *);
  28. extern int have_rtc;
  29. unsigned long get_ns_in_jiffie(void)
  30. {
  31. unsigned char timer_count, t1;
  32. unsigned short presc_count;
  33. unsigned long ns;
  34. unsigned long flags;
  35. local_irq_save(flags);
  36. local_irq_disable();
  37. timer_count = *R_TIMER0_DATA;
  38. presc_count = *R_TIM_PRESC_STATUS;
  39. /* presc_count might be wrapped */
  40. t1 = *R_TIMER0_DATA;
  41. if (timer_count != t1){
  42. /* it wrapped, read prescaler again... */
  43. presc_count = *R_TIM_PRESC_STATUS;
  44. timer_count = t1;
  45. }
  46. local_irq_restore(flags);
  47. if (presc_count >= PRESCALE_VALUE/2 ){
  48. presc_count = PRESCALE_VALUE - presc_count + PRESCALE_VALUE/2;
  49. } else {
  50. presc_count = PRESCALE_VALUE - presc_count - PRESCALE_VALUE/2;
  51. }
  52. ns = ( (TIMER0_DIV - timer_count) * ((1000000000/HZ)/TIMER0_DIV )) +
  53. ( (presc_count) * (1000000000/PRESCALE_FREQ));
  54. return ns;
  55. }
  56. unsigned long do_slow_gettimeoffset(void)
  57. {
  58. unsigned long count, t1;
  59. unsigned long usec_count = 0;
  60. unsigned short presc_count;
  61. static unsigned long count_p = TIMER0_DIV;/* for the first call after boot */
  62. static unsigned long jiffies_p = 0;
  63. /*
  64. * cache volatile jiffies temporarily; we have IRQs turned off.
  65. */
  66. unsigned long jiffies_t;
  67. /* The timer interrupt comes from Etrax timer 0. In order to get
  68. * better precision, we check the current value. It might have
  69. * underflowed already though.
  70. */
  71. #ifndef CONFIG_SVINTO_SIM
  72. /* Not available in the xsim simulator. */
  73. count = *R_TIMER0_DATA;
  74. presc_count = *R_TIM_PRESC_STATUS;
  75. /* presc_count might be wrapped */
  76. t1 = *R_TIMER0_DATA;
  77. if (count != t1){
  78. /* it wrapped, read prescaler again... */
  79. presc_count = *R_TIM_PRESC_STATUS;
  80. count = t1;
  81. }
  82. #else
  83. count = 0;
  84. presc_count = 0;
  85. #endif
  86. jiffies_t = jiffies;
  87. /*
  88. * avoiding timer inconsistencies (they are rare, but they happen)...
  89. * there are one problem that must be avoided here:
  90. * 1. the timer counter underflows
  91. */
  92. if( jiffies_t == jiffies_p ) {
  93. if( count > count_p ) {
  94. /* Timer wrapped, use new count and prescale
  95. * increase the time corresponding to one jiffie
  96. */
  97. usec_count = 1000000/HZ;
  98. }
  99. } else
  100. jiffies_p = jiffies_t;
  101. count_p = count;
  102. if (presc_count >= PRESCALE_VALUE/2 ){
  103. presc_count = PRESCALE_VALUE - presc_count + PRESCALE_VALUE/2;
  104. } else {
  105. presc_count = PRESCALE_VALUE - presc_count - PRESCALE_VALUE/2;
  106. }
  107. /* Convert timer value to usec */
  108. usec_count += ( (TIMER0_DIV - count) * (1000000/HZ)/TIMER0_DIV ) +
  109. (( (presc_count) * (1000000000/PRESCALE_FREQ))/1000);
  110. return usec_count;
  111. }
  112. /* Excerpt from the Etrax100 HSDD about the built-in watchdog:
  113. *
  114. * 3.10.4 Watchdog timer
  115. * When the watchdog timer is started, it generates an NMI if the watchdog
  116. * isn't restarted or stopped within 0.1 s. If it still isn't restarted or
  117. * stopped after an additional 3.3 ms, the watchdog resets the chip.
  118. * The watchdog timer is stopped after reset. The watchdog timer is controlled
  119. * by the R_WATCHDOG register. The R_WATCHDOG register contains an enable bit
  120. * and a 3-bit key value. The effect of writing to the R_WATCHDOG register is
  121. * described in the table below:
  122. *
  123. * Watchdog Value written:
  124. * state: To enable: To key: Operation:
  125. * -------- ---------- ------- ----------
  126. * stopped 0 X No effect.
  127. * stopped 1 key_val Start watchdog with key = key_val.
  128. * started 0 ~key Stop watchdog
  129. * started 1 ~key Restart watchdog with key = ~key.
  130. * started X new_key_val Change key to new_key_val.
  131. *
  132. * Note: '~' is the bitwise NOT operator.
  133. *
  134. */
  135. /* right now, starting the watchdog is the same as resetting it */
  136. #define start_watchdog reset_watchdog
  137. #if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)
  138. static int watchdog_key = 0; /* arbitrary number */
  139. #endif
  140. /* number of pages to consider "out of memory". it is normal that the memory
  141. * is used though, so put this really low.
  142. */
  143. #define WATCHDOG_MIN_FREE_PAGES 8
  144. void
  145. reset_watchdog(void)
  146. {
  147. #if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)
  148. /* only keep watchdog happy as long as we have memory left! */
  149. if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
  150. /* reset the watchdog with the inverse of the old key */
  151. watchdog_key ^= 0x7; /* invert key, which is 3 bits */
  152. *R_WATCHDOG = IO_FIELD(R_WATCHDOG, key, watchdog_key) |
  153. IO_STATE(R_WATCHDOG, enable, start);
  154. }
  155. #endif
  156. }
  157. /* stop the watchdog - we still need the correct key */
  158. void
  159. stop_watchdog(void)
  160. {
  161. #if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)
  162. watchdog_key ^= 0x7; /* invert key, which is 3 bits */
  163. *R_WATCHDOG = IO_FIELD(R_WATCHDOG, key, watchdog_key) |
  164. IO_STATE(R_WATCHDOG, enable, stop);
  165. #endif
  166. }
  167. /* last time the cmos clock got updated */
  168. static long last_rtc_update = 0;
  169. /*
  170. * timer_interrupt() needs to keep up the real-time clock,
  171. * as well as call the "do_timer()" routine every clocktick
  172. */
  173. //static unsigned short myjiff; /* used by our debug routine print_timestamp */
  174. extern void cris_do_profile(struct pt_regs *regs);
  175. static inline irqreturn_t
  176. timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  177. {
  178. /* acknowledge the timer irq */
  179. #ifdef USE_CASCADE_TIMERS
  180. *R_TIMER_CTRL =
  181. IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |
  182. IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |
  183. IO_STATE( R_TIMER_CTRL, i1, clr) |
  184. IO_STATE( R_TIMER_CTRL, tm1, run) |
  185. IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |
  186. IO_STATE( R_TIMER_CTRL, i0, clr) |
  187. IO_STATE( R_TIMER_CTRL, tm0, run) |
  188. IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);
  189. #else
  190. *R_TIMER_CTRL = r_timer_ctrl_shadow |
  191. IO_STATE(R_TIMER_CTRL, i0, clr);
  192. #endif
  193. /* reset watchdog otherwise it resets us! */
  194. reset_watchdog();
  195. /* call the real timer interrupt handler */
  196. do_timer(1);
  197. cris_do_profile(regs); /* Save profiling information */
  198. /*
  199. * If we have an externally synchronized Linux clock, then update
  200. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  201. * called as close as possible to 500 ms before the new second starts.
  202. *
  203. * The division here is not time critical since it will run once in
  204. * 11 minutes
  205. */
  206. if (ntp_synced() &&
  207. xtime.tv_sec > last_rtc_update + 660 &&
  208. (xtime.tv_nsec / 1000) >= 500000 - (tick_nsec / 1000) / 2 &&
  209. (xtime.tv_nsec / 1000) <= 500000 + (tick_nsec / 1000) / 2) {
  210. if (set_rtc_mmss(xtime.tv_sec) == 0)
  211. last_rtc_update = xtime.tv_sec;
  212. else
  213. last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
  214. }
  215. return IRQ_HANDLED;
  216. }
  217. /* timer is IRQF_SHARED so drivers can add stuff to the timer irq chain
  218. * it needs to be IRQF_DISABLED to make the jiffies update work properly
  219. */
  220. static struct irqaction irq2 = { timer_interrupt, IRQF_SHARED | IRQF_DISABLED,
  221. CPU_MASK_NONE, "timer", NULL, NULL};
  222. void __init
  223. time_init(void)
  224. {
  225. /* probe for the RTC and read it if it exists
  226. * Before the RTC can be probed the loops_per_usec variable needs
  227. * to be initialized to make usleep work. A better value for
  228. * loops_per_usec is calculated by the kernel later once the
  229. * clock has started.
  230. */
  231. loops_per_usec = 50;
  232. if(RTC_INIT() < 0) {
  233. /* no RTC, start at 1980 */
  234. xtime.tv_sec = 0;
  235. xtime.tv_nsec = 0;
  236. have_rtc = 0;
  237. } else {
  238. /* get the current time */
  239. have_rtc = 1;
  240. update_xtime_from_cmos();
  241. }
  242. /*
  243. * Initialize wall_to_monotonic such that adding it to xtime will yield zero, the
  244. * tv_nsec field must be normalized (i.e., 0 <= nsec < NSEC_PER_SEC).
  245. */
  246. set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
  247. /* Setup the etrax timers
  248. * Base frequency is 25000 hz, divider 250 -> 100 HZ
  249. * In normal mode, we use timer0, so timer1 is free. In cascade
  250. * mode (which we sometimes use for debugging) both timers are used.
  251. * Remember that linux/timex.h contains #defines that rely on the
  252. * timer settings below (hz and divide factor) !!!
  253. */
  254. #ifdef USE_CASCADE_TIMERS
  255. *R_TIMER_CTRL =
  256. IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |
  257. IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |
  258. IO_STATE( R_TIMER_CTRL, i1, nop) |
  259. IO_STATE( R_TIMER_CTRL, tm1, stop_ld) |
  260. IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |
  261. IO_STATE( R_TIMER_CTRL, i0, nop) |
  262. IO_STATE( R_TIMER_CTRL, tm0, stop_ld) |
  263. IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);
  264. *R_TIMER_CTRL = r_timer_ctrl_shadow =
  265. IO_FIELD( R_TIMER_CTRL, timerdiv1, 0) |
  266. IO_FIELD( R_TIMER_CTRL, timerdiv0, 0) |
  267. IO_STATE( R_TIMER_CTRL, i1, nop) |
  268. IO_STATE( R_TIMER_CTRL, tm1, run) |
  269. IO_STATE( R_TIMER_CTRL, clksel1, cascade0) |
  270. IO_STATE( R_TIMER_CTRL, i0, nop) |
  271. IO_STATE( R_TIMER_CTRL, tm0, run) |
  272. IO_STATE( R_TIMER_CTRL, clksel0, c6250kHz);
  273. #else
  274. *R_TIMER_CTRL =
  275. IO_FIELD(R_TIMER_CTRL, timerdiv1, 192) |
  276. IO_FIELD(R_TIMER_CTRL, timerdiv0, TIMER0_DIV) |
  277. IO_STATE(R_TIMER_CTRL, i1, nop) |
  278. IO_STATE(R_TIMER_CTRL, tm1, stop_ld) |
  279. IO_STATE(R_TIMER_CTRL, clksel1, c19k2Hz) |
  280. IO_STATE(R_TIMER_CTRL, i0, nop) |
  281. IO_STATE(R_TIMER_CTRL, tm0, stop_ld) |
  282. IO_STATE(R_TIMER_CTRL, clksel0, flexible);
  283. *R_TIMER_CTRL = r_timer_ctrl_shadow =
  284. IO_FIELD(R_TIMER_CTRL, timerdiv1, 192) |
  285. IO_FIELD(R_TIMER_CTRL, timerdiv0, TIMER0_DIV) |
  286. IO_STATE(R_TIMER_CTRL, i1, nop) |
  287. IO_STATE(R_TIMER_CTRL, tm1, run) |
  288. IO_STATE(R_TIMER_CTRL, clksel1, c19k2Hz) |
  289. IO_STATE(R_TIMER_CTRL, i0, nop) |
  290. IO_STATE(R_TIMER_CTRL, tm0, run) |
  291. IO_STATE(R_TIMER_CTRL, clksel0, flexible);
  292. *R_TIMER_PRESCALE = PRESCALE_VALUE;
  293. #endif
  294. *R_IRQ_MASK0_SET =
  295. IO_STATE(R_IRQ_MASK0_SET, timer0, set); /* unmask the timer irq */
  296. /* now actually register the timer irq handler that calls timer_interrupt() */
  297. setup_irq(2, &irq2); /* irq 2 is the timer0 irq in etrax */
  298. /* enable watchdog if we should use one */
  299. #if defined(CONFIG_ETRAX_WATCHDOG) && !defined(CONFIG_SVINTO_SIM)
  300. printk("Enabling watchdog...\n");
  301. start_watchdog();
  302. /* If we use the hardware watchdog, we want to trap it as an NMI
  303. and dump registers before it resets us. For this to happen, we
  304. must set the "m" NMI enable flag (which once set, is unset only
  305. when an NMI is taken).
  306. The same goes for the external NMI, but that doesn't have any
  307. driver or infrastructure support yet. */
  308. asm ("setf m");
  309. *R_IRQ_MASK0_SET =
  310. IO_STATE(R_IRQ_MASK0_SET, watchdog_nmi, set);
  311. *R_VECT_MASK_SET =
  312. IO_STATE(R_VECT_MASK_SET, nmi, set);
  313. #endif
  314. }