at91sam926x_time.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * linux/arch/arm/mach-at91rm9200/at91sam926x_time.c
  3. *
  4. * Copyright (C) 2005-2006 M. Amine SAYA, ATMEL Rousset, France
  5. * Revision 2005 M. Nicolas Diremdjian, ATMEL Rousset, France
  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/init.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/kernel.h>
  15. #include <linux/sched.h>
  16. #include <linux/time.h>
  17. #include <asm/hardware.h>
  18. #include <asm/io.h>
  19. #include <asm/mach/time.h>
  20. #include <asm/arch/at91_pit.h>
  21. #define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
  22. #define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
  23. /*
  24. * Returns number of microseconds since last timer interrupt. Note that interrupts
  25. * will have been disabled by do_gettimeofday()
  26. * 'LATCH' is hwclock ticks (see CLOCK_TICK_RATE in timex.h) per jiffy.
  27. * 'tick' is usecs per jiffy (linux/timex.h).
  28. */
  29. static unsigned long at91sam926x_gettimeoffset(void)
  30. {
  31. unsigned long elapsed;
  32. unsigned long t = at91_sys_read(AT91_PIT_PIIR);
  33. elapsed = (PIT_PICNT(t) * LATCH) + PIT_CPIV(t); /* hardware clock cycles */
  34. return (unsigned long)(elapsed * 1000000) / LATCH;
  35. }
  36. /*
  37. * IRQ handler for the timer.
  38. */
  39. static irqreturn_t at91sam926x_timer_interrupt(int irq, void *dev_id)
  40. {
  41. volatile long nr_ticks;
  42. if (at91_sys_read(AT91_PIT_SR) & AT91_PIT_PITS) { /* This is a shared interrupt */
  43. write_seqlock(&xtime_lock);
  44. /* Get number to ticks performed before interrupt and clear PIT interrupt */
  45. nr_ticks = PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
  46. do {
  47. timer_tick();
  48. nr_ticks--;
  49. } while (nr_ticks);
  50. write_sequnlock(&xtime_lock);
  51. return IRQ_HANDLED;
  52. } else
  53. return IRQ_NONE; /* not handled */
  54. }
  55. static struct irqaction at91sam926x_timer_irq = {
  56. .name = "at91_tick",
  57. .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER,
  58. .handler = at91sam926x_timer_interrupt
  59. };
  60. void at91sam926x_timer_reset(void)
  61. {
  62. /* Disable timer */
  63. at91_sys_write(AT91_PIT_MR, 0);
  64. /* Clear any pending interrupts */
  65. (void) at91_sys_read(AT91_PIT_PIVR);
  66. /* Set Period Interval timer and enable its interrupt */
  67. at91_sys_write(AT91_PIT_MR, (LATCH & AT91_PIT_PIV) | AT91_PIT_PITIEN | AT91_PIT_PITEN);
  68. }
  69. /*
  70. * Set up timer interrupt.
  71. */
  72. void __init at91sam926x_timer_init(void)
  73. {
  74. /* Initialize and enable the timer */
  75. at91sam926x_timer_reset();
  76. /* Make IRQs happen for the system timer. */
  77. setup_irq(AT91_ID_SYS, &at91sam926x_timer_irq);
  78. }
  79. #ifdef CONFIG_PM
  80. static void at91sam926x_timer_suspend(void)
  81. {
  82. /* Disable timer */
  83. at91_sys_write(AT91_PIT_MR, 0);
  84. }
  85. #else
  86. #define at91sam926x_timer_suspend NULL
  87. #endif
  88. struct sys_timer at91sam926x_timer = {
  89. .init = at91sam926x_timer_init,
  90. .offset = at91sam926x_gettimeoffset,
  91. .suspend = at91sam926x_timer_suspend,
  92. .resume = at91sam926x_timer_reset,
  93. };