time.c 5.1 KB

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