time.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * linux/arch/arm/mach-imx/time.c
  3. *
  4. * Copyright (C) 2000-2001 Deep Blue Solutions
  5. * Copyright (C) 2002 Shane Nay (shane@minirl.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/sched.h>
  14. #include <linux/init.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/time.h>
  17. #include <asm/hardware.h>
  18. #include <asm/io.h>
  19. #include <asm/leds.h>
  20. #include <asm/irq.h>
  21. #include <asm/mach/time.h>
  22. /* Use timer 1 as system timer */
  23. #define TIMER_BASE IMX_TIM1_BASE
  24. /*
  25. * Returns number of us since last clock interrupt. Note that interrupts
  26. * will have been disabled by do_gettimeoffset()
  27. */
  28. static unsigned long imx_gettimeoffset(void)
  29. {
  30. unsigned long ticks;
  31. /*
  32. * Get the current number of ticks. Note that there is a race
  33. * condition between us reading the timer and checking for
  34. * an interrupt. We get around this by ensuring that the
  35. * counter has not reloaded between our two reads.
  36. */
  37. ticks = IMX_TCN(TIMER_BASE);
  38. /*
  39. * Interrupt pending? If so, we've reloaded once already.
  40. */
  41. if (IMX_TSTAT(TIMER_BASE) & TSTAT_COMP)
  42. ticks += LATCH;
  43. /*
  44. * Convert the ticks to usecs
  45. */
  46. return (1000000 / CLK32) * ticks;
  47. }
  48. /*
  49. * IRQ handler for the timer
  50. */
  51. static irqreturn_t
  52. imx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  53. {
  54. write_seqlock(&xtime_lock);
  55. /* clear the interrupt */
  56. if (IMX_TSTAT(TIMER_BASE))
  57. IMX_TSTAT(TIMER_BASE) = 0;
  58. timer_tick(regs);
  59. write_sequnlock(&xtime_lock);
  60. return IRQ_HANDLED;
  61. }
  62. static struct irqaction imx_timer_irq = {
  63. .name = "i.MX Timer Tick",
  64. .flags = SA_INTERRUPT | SA_TIMER,
  65. .handler = imx_timer_interrupt,
  66. };
  67. /*
  68. * Set up timer interrupt, and return the current time in seconds.
  69. */
  70. static void __init imx_timer_init(void)
  71. {
  72. /*
  73. * Initialise to a known state (all timers off, and timing reset)
  74. */
  75. IMX_TCTL(TIMER_BASE) = 0;
  76. IMX_TPRER(TIMER_BASE) = 0;
  77. IMX_TCMP(TIMER_BASE) = LATCH - 1;
  78. IMX_TCTL(TIMER_BASE) = TCTL_CLK_32 | TCTL_IRQEN | TCTL_TEN;
  79. /*
  80. * Make irqs happen for the system timer
  81. */
  82. setup_irq(TIM1_INT, &imx_timer_irq);
  83. }
  84. struct sys_timer imx_timer = {
  85. .init = imx_timer_init,
  86. .offset = imx_gettimeoffset,
  87. };