time.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * arch/xtensa/kernel/time.c
  3. *
  4. * Timer and clock support.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. *
  10. * Copyright (C) 2005 Tensilica Inc.
  11. *
  12. * Chris Zankel <chris@zankel.net>
  13. */
  14. #include <linux/errno.h>
  15. #include <linux/time.h>
  16. #include <linux/timex.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/irq.h>
  21. #include <linux/profile.h>
  22. #include <linux/delay.h>
  23. #include <asm/timex.h>
  24. #include <asm/platform.h>
  25. DEFINE_SPINLOCK(rtc_lock);
  26. EXPORT_SYMBOL(rtc_lock);
  27. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  28. unsigned long ccount_per_jiffy; /* per 1/HZ */
  29. unsigned long nsec_per_ccount; /* nsec per ccount increment */
  30. #endif
  31. static long last_rtc_update = 0;
  32. /*
  33. * Scheduler clock - returns current tim in nanosec units.
  34. */
  35. unsigned long long sched_clock(void)
  36. {
  37. return (unsigned long long)jiffies * (1000000000 / HZ);
  38. }
  39. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  40. static struct irqaction timer_irqaction = {
  41. .handler = timer_interrupt,
  42. .flags = IRQF_DISABLED,
  43. .name = "timer",
  44. };
  45. void __init time_init(void)
  46. {
  47. time_t sec_o, sec_n = 0;
  48. /* The platform must provide a function to calibrate the processor
  49. * speed for the CALIBRATE.
  50. */
  51. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  52. printk("Calibrating CPU frequency ");
  53. platform_calibrate_ccount();
  54. printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
  55. (int)(ccount_per_jiffy/(10000/HZ))%100);
  56. #endif
  57. /* Set time from RTC (if provided) */
  58. if (platform_get_rtc_time(&sec_o) == 0)
  59. while (platform_get_rtc_time(&sec_n))
  60. if (sec_o != sec_n)
  61. break;
  62. xtime.tv_nsec = 0;
  63. last_rtc_update = xtime.tv_sec = sec_n;
  64. set_normalized_timespec(&wall_to_monotonic,
  65. -xtime.tv_sec, -xtime.tv_nsec);
  66. /* Initialize the linux timer interrupt. */
  67. setup_irq(LINUX_TIMER_INT, &timer_irqaction);
  68. set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
  69. }
  70. int do_settimeofday(struct timespec *tv)
  71. {
  72. time_t wtm_sec, sec = tv->tv_sec;
  73. long wtm_nsec, nsec = tv->tv_nsec;
  74. unsigned long delta;
  75. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  76. return -EINVAL;
  77. write_seqlock_irq(&xtime_lock);
  78. /* This is revolting. We need to set "xtime" correctly. However, the
  79. * value in this location is the value at the most recent update of
  80. * wall time. Discover what correction gettimeofday() would have
  81. * made, and then undo it!
  82. */
  83. delta = CCOUNT_PER_JIFFY;
  84. delta += get_ccount() - get_linux_timer();
  85. nsec -= delta * NSEC_PER_CCOUNT;
  86. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  87. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  88. set_normalized_timespec(&xtime, sec, nsec);
  89. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  90. ntp_clear();
  91. write_sequnlock_irq(&xtime_lock);
  92. return 0;
  93. }
  94. EXPORT_SYMBOL(do_settimeofday);
  95. void do_gettimeofday(struct timeval *tv)
  96. {
  97. unsigned long flags;
  98. unsigned long volatile sec, usec, delta, seq;
  99. do {
  100. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  101. sec = xtime.tv_sec;
  102. usec = (xtime.tv_nsec / NSEC_PER_USEC);
  103. delta = get_linux_timer() - get_ccount();
  104. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  105. usec += (((unsigned long) CCOUNT_PER_JIFFY - delta)
  106. * (unsigned long) NSEC_PER_CCOUNT) / NSEC_PER_USEC;
  107. for (; usec >= 1000000; sec++, usec -= 1000000)
  108. ;
  109. tv->tv_sec = sec;
  110. tv->tv_usec = usec;
  111. }
  112. EXPORT_SYMBOL(do_gettimeofday);
  113. /*
  114. * The timer interrupt is called HZ times per second.
  115. */
  116. irqreturn_t timer_interrupt (int irq, void *dev_id)
  117. {
  118. unsigned long next;
  119. next = get_linux_timer();
  120. again:
  121. while ((signed long)(get_ccount() - next) > 0) {
  122. profile_tick(CPU_PROFILING);
  123. #ifndef CONFIG_SMP
  124. update_process_times(user_mode(get_irq_regs()));
  125. #endif
  126. write_seqlock(&xtime_lock);
  127. do_timer(1); /* Linux handler in kernel/timer.c */
  128. /* Note that writing CCOMPARE clears the interrupt. */
  129. next += CCOUNT_PER_JIFFY;
  130. set_linux_timer(next);
  131. if (ntp_synced() &&
  132. xtime.tv_sec - last_rtc_update >= 659 &&
  133. abs((xtime.tv_nsec/1000)-(1000000-1000000/HZ))<5000000/HZ) {
  134. if (platform_set_rtc_time(xtime.tv_sec+1) == 0)
  135. last_rtc_update = xtime.tv_sec+1;
  136. else
  137. /* Do it again in 60 s */
  138. last_rtc_update += 60;
  139. }
  140. write_sequnlock(&xtime_lock);
  141. }
  142. /* Allow platform to do something useful (Wdog). */
  143. platform_heartbeat();
  144. /* Make sure we didn't miss any tick... */
  145. if ((signed long)(get_ccount() - next) > 0)
  146. goto again;
  147. return IRQ_HANDLED;
  148. }
  149. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  150. void __cpuinit calibrate_delay(void)
  151. {
  152. loops_per_jiffy = CCOUNT_PER_JIFFY;
  153. printk("Calibrating delay loop (skipped)... "
  154. "%lu.%02lu BogoMIPS preset\n",
  155. loops_per_jiffy/(1000000/HZ),
  156. (loops_per_jiffy/(10000/HZ)) % 100);
  157. }
  158. #endif