time.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* $Id: time.c,v 1.19 2005/04/29 05:40:09 starvik Exp $
  2. *
  3. * linux/arch/cris/arch-v32/kernel/time.c
  4. *
  5. * Copyright (C) 2003 Axis Communications AB
  6. *
  7. */
  8. #include <linux/config.h>
  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 <linux/threads.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.h>
  23. #include <asm/arch/hwregs/reg_map.h>
  24. #include <asm/arch/hwregs/reg_rdwr.h>
  25. #include <asm/arch/hwregs/timer_defs.h>
  26. #include <asm/arch/hwregs/intr_vect_defs.h>
  27. /* Watchdog defines */
  28. #define ETRAX_WD_KEY_MASK 0x7F /* key is 7 bit */
  29. #define ETRAX_WD_HZ 763 /* watchdog counts at 763 Hz */
  30. #define ETRAX_WD_CNT ((2*ETRAX_WD_HZ)/HZ + 1) /* Number of 763 counts before watchdog bites */
  31. unsigned long timer_regs[NR_CPUS] =
  32. {
  33. regi_timer,
  34. #ifdef CONFIG_SMP
  35. regi_timer2
  36. #endif
  37. };
  38. extern void update_xtime_from_cmos(void);
  39. extern int set_rtc_mmss(unsigned long nowtime);
  40. extern int setup_irq(int, struct irqaction *);
  41. extern int have_rtc;
  42. unsigned long get_ns_in_jiffie(void)
  43. {
  44. reg_timer_r_tmr0_data data;
  45. unsigned long ns;
  46. data = REG_RD(timer, regi_timer, r_tmr0_data);
  47. ns = (TIMER0_DIV - data) * 10;
  48. return ns;
  49. }
  50. unsigned long do_slow_gettimeoffset(void)
  51. {
  52. unsigned long count;
  53. unsigned long usec_count = 0;
  54. static unsigned long count_p = TIMER0_DIV;/* for the first call after boot */
  55. static unsigned long jiffies_p = 0;
  56. /*
  57. * cache volatile jiffies temporarily; we have IRQs turned off.
  58. */
  59. unsigned long jiffies_t;
  60. /* The timer interrupt comes from Etrax timer 0. In order to get
  61. * better precision, we check the current value. It might have
  62. * underflowed already though.
  63. */
  64. count = REG_RD(timer, regi_timer, r_tmr0_data);
  65. jiffies_t = jiffies;
  66. /*
  67. * avoiding timer inconsistencies (they are rare, but they happen)...
  68. * there are one problem that must be avoided here:
  69. * 1. the timer counter underflows
  70. */
  71. if( jiffies_t == jiffies_p ) {
  72. if( count > count_p ) {
  73. /* Timer wrapped, use new count and prescale
  74. * increase the time corresponding to one jiffie
  75. */
  76. usec_count = 1000000/HZ;
  77. }
  78. } else
  79. jiffies_p = jiffies_t;
  80. count_p = count;
  81. /* Convert timer value to usec */
  82. /* 100 MHz timer, divide by 100 to get usec */
  83. usec_count += (TIMER0_DIV - count) / 100;
  84. return usec_count;
  85. }
  86. /* From timer MDS describing the hardware watchdog:
  87. * 4.3.1 Watchdog Operation
  88. * The watchdog timer is an 8-bit timer with a configurable start value.
  89. * Once started the whatchdog counts downwards with a frequency of 763 Hz
  90. * (100/131072 MHz). When the watchdog counts down to 1, it generates an
  91. * NMI (Non Maskable Interrupt), and when it counts down to 0, it resets the
  92. * chip.
  93. */
  94. /* This gives us 1.3 ms to do something useful when the NMI comes */
  95. /* right now, starting the watchdog is the same as resetting it */
  96. #define start_watchdog reset_watchdog
  97. #if defined(CONFIG_ETRAX_WATCHDOG)
  98. static short int watchdog_key = 42; /* arbitrary 7 bit number */
  99. #endif
  100. /* number of pages to consider "out of memory". it is normal that the memory
  101. * is used though, so put this really low.
  102. */
  103. #define WATCHDOG_MIN_FREE_PAGES 8
  104. void
  105. reset_watchdog(void)
  106. {
  107. #if defined(CONFIG_ETRAX_WATCHDOG)
  108. reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
  109. /* only keep watchdog happy as long as we have memory left! */
  110. if(nr_free_pages() > WATCHDOG_MIN_FREE_PAGES) {
  111. /* reset the watchdog with the inverse of the old key */
  112. watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
  113. wd_ctrl.cnt = ETRAX_WD_CNT;
  114. wd_ctrl.cmd = regk_timer_start;
  115. wd_ctrl.key = watchdog_key;
  116. REG_WR(timer, regi_timer, rw_wd_ctrl, wd_ctrl);
  117. }
  118. #endif
  119. }
  120. /* stop the watchdog - we still need the correct key */
  121. void
  122. stop_watchdog(void)
  123. {
  124. #if defined(CONFIG_ETRAX_WATCHDOG)
  125. reg_timer_rw_wd_ctrl wd_ctrl = { 0 };
  126. watchdog_key ^= ETRAX_WD_KEY_MASK; /* invert key, which is 7 bits */
  127. wd_ctrl.cnt = ETRAX_WD_CNT;
  128. wd_ctrl.cmd = regk_timer_stop;
  129. wd_ctrl.key = watchdog_key;
  130. REG_WR(timer, regi_timer, rw_wd_ctrl, wd_ctrl);
  131. #endif
  132. }
  133. extern void show_registers(struct pt_regs *regs);
  134. void
  135. handle_watchdog_bite(struct pt_regs* regs)
  136. {
  137. #if defined(CONFIG_ETRAX_WATCHDOG)
  138. extern int cause_of_death;
  139. raw_printk("Watchdog bite\n");
  140. /* Check if forced restart or unexpected watchdog */
  141. if (cause_of_death == 0xbedead) {
  142. while(1);
  143. }
  144. /* Unexpected watchdog, stop the watchdog and dump registers*/
  145. stop_watchdog();
  146. raw_printk("Oops: bitten by watchdog\n");
  147. show_registers(regs);
  148. #ifndef CONFIG_ETRAX_WATCHDOG_NICE_DOGGY
  149. reset_watchdog();
  150. #endif
  151. while(1) /* nothing */;
  152. #endif
  153. }
  154. /* last time the cmos clock got updated */
  155. static long last_rtc_update = 0;
  156. /*
  157. * timer_interrupt() needs to keep up the real-time clock,
  158. * as well as call the "do_timer()" routine every clocktick
  159. */
  160. //static unsigned short myjiff; /* used by our debug routine print_timestamp */
  161. extern void cris_do_profile(struct pt_regs *regs);
  162. static inline irqreturn_t
  163. timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  164. {
  165. int cpu = smp_processor_id();
  166. reg_timer_r_masked_intr masked_intr;
  167. reg_timer_rw_ack_intr ack_intr = { 0 };
  168. /* Check if the timer interrupt is for us (a tmr0 int) */
  169. masked_intr = REG_RD(timer, timer_regs[cpu], r_masked_intr);
  170. if (!masked_intr.tmr0)
  171. return IRQ_NONE;
  172. /* acknowledge the timer irq */
  173. ack_intr.tmr0 = 1;
  174. REG_WR(timer, timer_regs[cpu], rw_ack_intr, ack_intr);
  175. /* reset watchdog otherwise it resets us! */
  176. reset_watchdog();
  177. /* Update statistics. */
  178. update_process_times(user_mode(regs));
  179. cris_do_profile(regs); /* Save profiling information */
  180. /* The master CPU is responsible for the time keeping. */
  181. if (cpu != 0)
  182. return IRQ_HANDLED;
  183. /* call the real timer interrupt handler */
  184. do_timer(regs);
  185. /*
  186. * If we have an externally synchronized Linux clock, then update
  187. * CMOS clock accordingly every ~11 minutes. Set_rtc_mmss() has to be
  188. * called as close as possible to 500 ms before the new second starts.
  189. *
  190. * The division here is not time critical since it will run once in
  191. * 11 minutes
  192. */
  193. if ((time_status & STA_UNSYNC) == 0 &&
  194. xtime.tv_sec > last_rtc_update + 660 &&
  195. (xtime.tv_nsec / 1000) >= 500000 - (tick_nsec / 1000) / 2 &&
  196. (xtime.tv_nsec / 1000) <= 500000 + (tick_nsec / 1000) / 2) {
  197. if (set_rtc_mmss(xtime.tv_sec) == 0)
  198. last_rtc_update = xtime.tv_sec;
  199. else
  200. last_rtc_update = xtime.tv_sec - 600; /* do it again in 60 s */
  201. }
  202. return IRQ_HANDLED;
  203. }
  204. /* timer is SA_SHIRQ so drivers can add stuff to the timer irq chain
  205. * it needs to be SA_INTERRUPT to make the jiffies update work properly
  206. */
  207. static struct irqaction irq_timer = { timer_interrupt, SA_SHIRQ | SA_INTERRUPT,
  208. CPU_MASK_NONE, "timer", NULL, NULL};
  209. void __init
  210. cris_timer_init(void)
  211. {
  212. int cpu = smp_processor_id();
  213. reg_timer_rw_tmr0_ctrl tmr0_ctrl = { 0 };
  214. reg_timer_rw_tmr0_div tmr0_div = TIMER0_DIV;
  215. reg_timer_rw_intr_mask timer_intr_mask;
  216. /* Setup the etrax timers
  217. * Base frequency is 100MHz, divider 1000000 -> 100 HZ
  218. * We use timer0, so timer1 is free.
  219. * The trig timer is used by the fasttimer API if enabled.
  220. */
  221. tmr0_ctrl.op = regk_timer_ld;
  222. tmr0_ctrl.freq = regk_timer_f100;
  223. REG_WR(timer, timer_regs[cpu], rw_tmr0_div, tmr0_div);
  224. REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Load */
  225. tmr0_ctrl.op = regk_timer_run;
  226. REG_WR(timer, timer_regs[cpu], rw_tmr0_ctrl, tmr0_ctrl); /* Start */
  227. /* enable the timer irq */
  228. timer_intr_mask = REG_RD(timer, timer_regs[cpu], rw_intr_mask);
  229. timer_intr_mask.tmr0 = 1;
  230. REG_WR(timer, timer_regs[cpu], rw_intr_mask, timer_intr_mask);
  231. }
  232. void __init
  233. time_init(void)
  234. {
  235. reg_intr_vect_rw_mask intr_mask;
  236. /* probe for the RTC and read it if it exists
  237. * Before the RTC can be probed the loops_per_usec variable needs
  238. * to be initialized to make usleep work. A better value for
  239. * loops_per_usec is calculated by the kernel later once the
  240. * clock has started.
  241. */
  242. loops_per_usec = 50;
  243. if(RTC_INIT() < 0) {
  244. /* no RTC, start at 1980 */
  245. xtime.tv_sec = 0;
  246. xtime.tv_nsec = 0;
  247. have_rtc = 0;
  248. } else {
  249. /* get the current time */
  250. have_rtc = 1;
  251. update_xtime_from_cmos();
  252. }
  253. /*
  254. * Initialize wall_to_monotonic such that adding it to xtime will yield zero, the
  255. * tv_nsec field must be normalized (i.e., 0 <= nsec < NSEC_PER_SEC).
  256. */
  257. set_normalized_timespec(&wall_to_monotonic, -xtime.tv_sec, -xtime.tv_nsec);
  258. /* Start CPU local timer */
  259. cris_timer_init();
  260. /* enable the timer irq in global config */
  261. intr_mask = REG_RD(intr_vect, regi_irq, rw_mask);
  262. intr_mask.timer = 1;
  263. REG_WR(intr_vect, regi_irq, rw_mask, intr_mask);
  264. /* now actually register the timer irq handler that calls timer_interrupt() */
  265. setup_irq(TIMER_INTR_VECT, &irq_timer);
  266. /* enable watchdog if we should use one */
  267. #if defined(CONFIG_ETRAX_WATCHDOG)
  268. printk("Enabling watchdog...\n");
  269. start_watchdog();
  270. /* If we use the hardware watchdog, we want to trap it as an NMI
  271. and dump registers before it resets us. For this to happen, we
  272. must set the "m" NMI enable flag (which once set, is unset only
  273. when an NMI is taken).
  274. The same goes for the external NMI, but that doesn't have any
  275. driver or infrastructure support yet. */
  276. {
  277. unsigned long flags;
  278. local_save_flags(flags);
  279. flags |= (1<<30); /* NMI M flag is at bit 30 */
  280. local_irq_restore(flags);
  281. }
  282. #endif
  283. }