time.c 10 KB

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