time.c 11 KB

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