time.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/sched.h>
  16. #include <linux/time.h>
  17. #include <linux/clocksource.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. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  27. unsigned long ccount_per_jiffy; /* per 1/HZ */
  28. unsigned long nsec_per_ccount; /* nsec per ccount increment */
  29. #endif
  30. static cycle_t ccount_read(void)
  31. {
  32. return (cycle_t)get_ccount();
  33. }
  34. static struct clocksource ccount_clocksource = {
  35. .name = "ccount",
  36. .rating = 200,
  37. .read = ccount_read,
  38. .mask = CLOCKSOURCE_MASK(32),
  39. /*
  40. * With a shift of 22 the lower limit of the cpu clock is
  41. * 1MHz, where NSEC_PER_CCOUNT is 1000 or a bit less than
  42. * 2^10: Since we have 32 bits and the multiplicator can
  43. * already take up as much as 10 bits, this leaves us with
  44. * remaining upper 22 bits.
  45. */
  46. .shift = 22,
  47. };
  48. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  49. static struct irqaction timer_irqaction = {
  50. .handler = timer_interrupt,
  51. .flags = IRQF_DISABLED,
  52. .name = "timer",
  53. };
  54. void __init time_init(void)
  55. {
  56. /* FIXME: xtime&wall_to_monotonic are set in timekeeping_init. */
  57. read_persistent_clock(&xtime);
  58. set_normalized_timespec(&wall_to_monotonic,
  59. -xtime.tv_sec, -xtime.tv_nsec);
  60. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  61. printk("Calibrating CPU frequency ");
  62. platform_calibrate_ccount();
  63. printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
  64. (int)(ccount_per_jiffy/(10000/HZ))%100);
  65. #endif
  66. ccount_clocksource.mult =
  67. clocksource_hz2mult(CCOUNT_PER_JIFFY * HZ,
  68. ccount_clocksource.shift);
  69. clocksource_register(&ccount_clocksource);
  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. /*
  75. * The timer interrupt is called HZ times per second.
  76. */
  77. irqreturn_t timer_interrupt (int irq, void *dev_id)
  78. {
  79. unsigned long next;
  80. next = get_linux_timer();
  81. again:
  82. while ((signed long)(get_ccount() - next) > 0) {
  83. profile_tick(CPU_PROFILING);
  84. #ifndef CONFIG_SMP
  85. update_process_times(user_mode(get_irq_regs()));
  86. #endif
  87. write_seqlock(&xtime_lock);
  88. do_timer(1); /* Linux handler in kernel/timer.c */
  89. /* Note that writing CCOMPARE clears the interrupt. */
  90. next += CCOUNT_PER_JIFFY;
  91. set_linux_timer(next);
  92. write_sequnlock(&xtime_lock);
  93. }
  94. /* Allow platform to do something useful (Wdog). */
  95. platform_heartbeat();
  96. /* Make sure we didn't miss any tick... */
  97. if ((signed long)(get_ccount() - next) > 0)
  98. goto again;
  99. return IRQ_HANDLED;
  100. }
  101. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  102. void __cpuinit calibrate_delay(void)
  103. {
  104. loops_per_jiffy = CCOUNT_PER_JIFFY;
  105. printk("Calibrating delay loop (skipped)... "
  106. "%lu.%02lu BogoMIPS preset\n",
  107. loops_per_jiffy/(1000000/HZ),
  108. (loops_per_jiffy/(10000/HZ)) % 100);
  109. }
  110. #endif