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