time.c 10 KB

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