time.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. static irqreturn_t timer_interrupt(int irq, void *dev_id);
  41. static struct irqaction timer_irqaction = {
  42. .handler = timer_interrupt,
  43. .flags = IRQF_DISABLED,
  44. .name = "timer",
  45. };
  46. void __init time_init(void)
  47. {
  48. #ifdef CONFIG_XTENSA_CALIBRATE_CCOUNT
  49. printk("Calibrating CPU frequency ");
  50. platform_calibrate_ccount();
  51. printk("%d.%02d MHz\n", (int)ccount_per_jiffy/(1000000/HZ),
  52. (int)(ccount_per_jiffy/(10000/HZ))%100);
  53. #endif
  54. clocksource_register_hz(&ccount_clocksource, CCOUNT_PER_JIFFY * HZ);
  55. /* Initialize the linux timer interrupt. */
  56. setup_irq(LINUX_TIMER_INT, &timer_irqaction);
  57. set_linux_timer(get_ccount() + CCOUNT_PER_JIFFY);
  58. }
  59. /*
  60. * The timer interrupt is called HZ times per second.
  61. */
  62. irqreturn_t timer_interrupt (int irq, void *dev_id)
  63. {
  64. unsigned long next;
  65. next = get_linux_timer();
  66. again:
  67. while ((signed long)(get_ccount() - next) > 0) {
  68. profile_tick(CPU_PROFILING);
  69. #ifndef CONFIG_SMP
  70. update_process_times(user_mode(get_irq_regs()));
  71. #endif
  72. xtime_update(1); /* Linux handler in kernel/time/timekeeping */
  73. /* Note that writing CCOMPARE clears the interrupt. */
  74. next += CCOUNT_PER_JIFFY;
  75. set_linux_timer(next);
  76. }
  77. /* Allow platform to do something useful (Wdog). */
  78. platform_heartbeat();
  79. /* Make sure we didn't miss any tick... */
  80. if ((signed long)(get_ccount() - next) > 0)
  81. goto again;
  82. return IRQ_HANDLED;
  83. }
  84. #ifndef CONFIG_GENERIC_CALIBRATE_DELAY
  85. void __cpuinit calibrate_delay(void)
  86. {
  87. loops_per_jiffy = CCOUNT_PER_JIFFY;
  88. printk("Calibrating delay loop (skipped)... "
  89. "%lu.%02lu BogoMIPS preset\n",
  90. loops_per_jiffy/(1000000/HZ),
  91. (loops_per_jiffy/(10000/HZ)) % 100);
  92. }
  93. #endif