time.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/irq.h>
  20. #include <linux/profile.h>
  21. #include <linux/delay.h>
  22. #include <asm/timex.h>
  23. #include <asm/platform.h>
  24. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  25. unsigned long ccount_per_jiffy; /* per 1/HZ */
  26. unsigned long nsec_per_ccount; /* nsec per ccount increment */
  27. #endif
  28. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  29. static struct irqaction timer_irqaction = {
  30. .handler = timer_interrupt,
  31. .flags = IRQF_DISABLED,
  32. .name = "timer",
  33. };
  34. void __init time_init(void)
  35. {
  36. /* The platform must provide a function to calibrate the processor
  37. * speed for the CALIBRATE.
  38. */
  39. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  40. printk("Calibrating CPU frequency ");
  41. platform_calibrate_ccount();
  42. printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
  43. (int)(ccount_per_jiffy/(10000/HZ))%100);
  44. #endif
  45. xtime.tv_nsec = 0;
  46. xtime.tv_sec = read_persistent_clock();
  47. set_normalized_timespec(&wall_to_monotonic,
  48. -xtime.tv_sec, -xtime.tv_nsec);
  49. /* Initialize the linux timer interrupt. */
  50. setup_irq(LINUX_TIMER_INT, &timer_irqaction);
  51. set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
  52. }
  53. int do_settimeofday(struct timespec *tv)
  54. {
  55. time_t wtm_sec, sec = tv->tv_sec;
  56. long wtm_nsec, nsec = tv->tv_nsec;
  57. unsigned long delta;
  58. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  59. return -EINVAL;
  60. write_seqlock_irq(&xtime_lock);
  61. /* This is revolting. We need to set "xtime" correctly. However, the
  62. * value in this location is the value at the most recent update of
  63. * wall time. Discover what correction gettimeofday() would have
  64. * made, and then undo it!
  65. */
  66. delta = CCOUNT_PER_JIFFY;
  67. delta += get_ccount() - get_linux_timer();
  68. nsec -= delta * NSEC_PER_CCOUNT;
  69. wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
  70. wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
  71. set_normalized_timespec(&xtime, sec, nsec);
  72. set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
  73. ntp_clear();
  74. write_sequnlock_irq(&xtime_lock);
  75. return 0;
  76. }
  77. EXPORT_SYMBOL(do_settimeofday);
  78. void do_gettimeofday(struct timeval *tv)
  79. {
  80. unsigned long flags;
  81. unsigned long volatile sec, usec, delta, seq;
  82. do {
  83. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  84. sec = xtime.tv_sec;
  85. usec = (xtime.tv_nsec / NSEC_PER_USEC);
  86. delta = get_linux_timer() - get_ccount();
  87. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  88. usec += (((unsigned long) CCOUNT_PER_JIFFY - delta)
  89. * (unsigned long) NSEC_PER_CCOUNT) / NSEC_PER_USEC;
  90. for (; usec >= 1000000; sec++, usec -= 1000000)
  91. ;
  92. tv->tv_sec = sec;
  93. tv->tv_usec = usec;
  94. }
  95. EXPORT_SYMBOL(do_gettimeofday);
  96. /*
  97. * The timer interrupt is called HZ times per second.
  98. */
  99. irqreturn_t timer_interrupt (int irq, void *dev_id)
  100. {
  101. unsigned long next;
  102. next = get_linux_timer();
  103. again:
  104. while ((signed long)(get_ccount() - next) > 0) {
  105. profile_tick(CPU_PROFILING);
  106. #ifndef CONFIG_SMP
  107. update_process_times(user_mode(get_irq_regs()));
  108. #endif
  109. write_seqlock(&xtime_lock);
  110. do_timer(1); /* Linux handler in kernel/timer.c */
  111. /* Note that writing CCOMPARE clears the interrupt. */
  112. next += CCOUNT_PER_JIFFY;
  113. set_linux_timer(next);
  114. write_sequnlock(&xtime_lock);
  115. }
  116. /* Allow platform to do something useful (Wdog). */
  117. platform_heartbeat();
  118. /* Make sure we didn't miss any tick... */
  119. if ((signed long)(get_ccount() - next) > 0)
  120. goto again;
  121. return IRQ_HANDLED;
  122. }
  123. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  124. void __cpuinit calibrate_delay(void)
  125. {
  126. loops_per_jiffy = CCOUNT_PER_JIFFY;
  127. printk("Calibrating delay loop (skipped)... "
  128. "%lu.%02lu BogoMIPS preset\n",
  129. loops_per_jiffy/(1000000/HZ),
  130. (loops_per_jiffy/(10000/HZ)) % 100);
  131. }
  132. #endif