time.c 9.4 KB

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