time.c 5.0 KB

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