time.c 11 KB

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