time.c 2.3 KB

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