at91rm9200_time.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * linux/arch/arm/mach-at91/at91rm9200_time.c
  3. *
  4. * Copyright (C) 2003 SAN People
  5. * Copyright (C) 2003 ATMEL
  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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/irq.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/time.h>
  27. #include <asm/hardware.h>
  28. #include <asm/io.h>
  29. #include <asm/mach/time.h>
  30. #include <asm/arch/at91_st.h>
  31. static unsigned long last_crtr;
  32. /*
  33. * The ST_CRTR is updated asynchronously to the master clock. It is therefore
  34. * necessary to read it twice (with the same value) to ensure accuracy.
  35. */
  36. static inline unsigned long read_CRTR(void) {
  37. unsigned long x1, x2;
  38. do {
  39. x1 = at91_sys_read(AT91_ST_CRTR);
  40. x2 = at91_sys_read(AT91_ST_CRTR);
  41. } while (x1 != x2);
  42. return x1;
  43. }
  44. /*
  45. * Returns number of microseconds since last timer interrupt. Note that interrupts
  46. * will have been disabled by do_gettimeofday()
  47. * 'LATCH' is hwclock ticks (see CLOCK_TICK_RATE in timex.h) per jiffy.
  48. * 'tick' is usecs per jiffy (linux/timex.h).
  49. */
  50. static unsigned long at91rm9200_gettimeoffset(void)
  51. {
  52. unsigned long elapsed;
  53. elapsed = (read_CRTR() - last_crtr) & AT91_ST_ALMV;
  54. return (unsigned long)(elapsed * (tick_nsec / 1000)) / LATCH;
  55. }
  56. /*
  57. * IRQ handler for the timer.
  58. */
  59. static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
  60. {
  61. if (at91_sys_read(AT91_ST_SR) & AT91_ST_PITS) { /* This is a shared interrupt */
  62. write_seqlock(&xtime_lock);
  63. while (((read_CRTR() - last_crtr) & AT91_ST_ALMV) >= LATCH) {
  64. timer_tick();
  65. last_crtr = (last_crtr + LATCH) & AT91_ST_ALMV;
  66. }
  67. write_sequnlock(&xtime_lock);
  68. return IRQ_HANDLED;
  69. }
  70. else
  71. return IRQ_NONE; /* not handled */
  72. }
  73. static struct irqaction at91rm9200_timer_irq = {
  74. .name = "at91_tick",
  75. .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  76. .handler = at91rm9200_timer_interrupt
  77. };
  78. void at91rm9200_timer_reset(void)
  79. {
  80. last_crtr = 0;
  81. /* Real time counter incremented every 30.51758 microseconds */
  82. at91_sys_write(AT91_ST_RTMR, 1);
  83. /* Set Period Interval timer */
  84. at91_sys_write(AT91_ST_PIMR, LATCH);
  85. /* Clear any pending interrupts */
  86. (void) at91_sys_read(AT91_ST_SR);
  87. /* Enable Period Interval Timer interrupt */
  88. at91_sys_write(AT91_ST_IER, AT91_ST_PITS);
  89. }
  90. /*
  91. * Set up timer interrupt.
  92. */
  93. void __init at91rm9200_timer_init(void)
  94. {
  95. /* Disable all timer interrupts */
  96. at91_sys_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
  97. (void) at91_sys_read(AT91_ST_SR); /* Clear any pending interrupts */
  98. /* Make IRQs happen for the system timer */
  99. setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);
  100. /* Change the kernel's 'tick' value to 10009 usec. (the default is 10000) */
  101. tick_usec = (LATCH * 1000000) / CLOCK_TICK_RATE;
  102. /* Initialize and enable the timer interrupt */
  103. at91rm9200_timer_reset();
  104. }
  105. #ifdef CONFIG_PM
  106. static void at91rm9200_timer_suspend(void)
  107. {
  108. /* disable Period Interval Timer interrupt */
  109. at91_sys_write(AT91_ST_IDR, AT91_ST_PITS);
  110. }
  111. #else
  112. #define at91rm9200_timer_suspend NULL
  113. #endif
  114. struct sys_timer at91rm9200_timer = {
  115. .init = at91rm9200_timer_init,
  116. .offset = at91rm9200_gettimeoffset,
  117. .suspend = at91rm9200_timer_suspend,
  118. .resume = at91rm9200_timer_reset,
  119. };