time.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  57. printk("Calibrating CPU frequency ");
  58. platform_calibrate_ccount();
  59. printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
  60. (int)(ccount_per_jiffy/(10000/HZ))%100);
  61. #endif
  62. ccount_clocksource.mult =
  63. clocksource_hz2mult(CCOUNT_PER_JIFFY * HZ,
  64. ccount_clocksource.shift);
  65. clocksource_register(&ccount_clocksource);
  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. /*
  71. * The timer interrupt is called HZ times per second.
  72. */
  73. irqreturn_t timer_interrupt (int irq, void *dev_id)
  74. {
  75. unsigned long next;
  76. next = get_linux_timer();
  77. again:
  78. while ((signed long)(get_ccount() - next) > 0) {
  79. profile_tick(CPU_PROFILING);
  80. #ifndef CONFIG_SMP
  81. update_process_times(user_mode(get_irq_regs()));
  82. #endif
  83. xtime_update(1); /* Linux handler in kernel/time/timekeeping */
  84. /* Note that writing CCOMPARE clears the interrupt. */
  85. next += CCOUNT_PER_JIFFY;
  86. set_linux_timer(next);
  87. }
  88. /* Allow platform to do something useful (Wdog). */
  89. platform_heartbeat();
  90. /* Make sure we didn't miss any tick... */
  91. if ((signed long)(get_ccount() - next) > 0)
  92. goto again;
  93. return IRQ_HANDLED;
  94. }
  95. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  96. void __cpuinit calibrate_delay(void)
  97. {
  98. loops_per_jiffy = CCOUNT_PER_JIFFY;
  99. printk("Calibrating delay loop (skipped)... "
  100. "%lu.%02lu BogoMIPS preset\n",
  101. loops_per_jiffy/(1000000/HZ),
  102. (loops_per_jiffy/(10000/HZ)) % 100);
  103. }
  104. #endif