time.c 2.7 KB

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