at91sam926x_time.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * linux/arch/arm/mach-at91/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. */
  28. static unsigned long at91sam926x_gettimeoffset(void)
  29. {
  30. unsigned long elapsed;
  31. unsigned long t = at91_sys_read(AT91_PIT_PIIR);
  32. elapsed = (PIT_PICNT(t) * LATCH) + PIT_CPIV(t); /* hardware clock cycles */
  33. return (unsigned long)(elapsed * jiffies_to_usecs(1)) / LATCH;
  34. }
  35. /*
  36. * IRQ handler for the timer.
  37. */
  38. static irqreturn_t at91sam926x_timer_interrupt(int irq, void *dev_id)
  39. {
  40. volatile long nr_ticks;
  41. if (at91_sys_read(AT91_PIT_SR) & AT91_PIT_PITS) { /* This is a shared interrupt */
  42. write_seqlock(&xtime_lock);
  43. /* Get number to ticks performed before interrupt and clear PIT interrupt */
  44. nr_ticks = PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
  45. do {
  46. timer_tick();
  47. nr_ticks--;
  48. } while (nr_ticks);
  49. write_sequnlock(&xtime_lock);
  50. return IRQ_HANDLED;
  51. } else
  52. return IRQ_NONE; /* not handled */
  53. }
  54. static struct irqaction at91sam926x_timer_irq = {
  55. .name = "at91_tick",
  56. .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
  57. .handler = at91sam926x_timer_interrupt
  58. };
  59. void at91sam926x_timer_reset(void)
  60. {
  61. /* Disable timer */
  62. at91_sys_write(AT91_PIT_MR, 0);
  63. /* Clear any pending interrupts */
  64. (void) at91_sys_read(AT91_PIT_PIVR);
  65. /* Set Period Interval timer and enable its interrupt */
  66. at91_sys_write(AT91_PIT_MR, (LATCH & AT91_PIT_PIV) | AT91_PIT_PITIEN | AT91_PIT_PITEN);
  67. }
  68. /*
  69. * Set up timer interrupt.
  70. */
  71. void __init at91sam926x_timer_init(void)
  72. {
  73. /* Initialize and enable the timer */
  74. at91sam926x_timer_reset();
  75. /* Make IRQs happen for the system timer. */
  76. setup_irq(AT91_ID_SYS, &at91sam926x_timer_irq);
  77. }
  78. #ifdef CONFIG_PM
  79. static void at91sam926x_timer_suspend(void)
  80. {
  81. /* Disable timer */
  82. at91_sys_write(AT91_PIT_MR, 0);
  83. }
  84. #else
  85. #define at91sam926x_timer_suspend NULL
  86. #endif
  87. struct sys_timer at91sam926x_timer = {
  88. .init = at91sam926x_timer_init,
  89. .offset = at91sam926x_gettimeoffset,
  90. .suspend = at91sam926x_timer_suspend,
  91. .resume = at91sam926x_timer_reset,
  92. };