iop331-time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * arch/arm/mach-iop3xx/iop331-time.c
  3. *
  4. * Timer code for IOP331 based systems
  5. *
  6. * Author: Dave Jiang <dave.jiang@intel.com>
  7. *
  8. * Copyright 2003 Intel Corp.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/time.h>
  18. #include <linux/init.h>
  19. #include <linux/timex.h>
  20. #include <asm/hardware.h>
  21. #include <asm/io.h>
  22. #include <asm/irq.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/mach-types.h>
  25. #include <asm/mach/irq.h>
  26. #include <asm/mach/time.h>
  27. static inline unsigned long get_elapsed(void)
  28. {
  29. return LATCH - *IOP331_TU_TCR0;
  30. }
  31. static unsigned long iop331_gettimeoffset(void)
  32. {
  33. unsigned long elapsed, usec;
  34. u32 tisr1, tisr2;
  35. /*
  36. * If an interrupt was pending before we read the timer,
  37. * we've already wrapped. Factor this into the time.
  38. * If an interrupt was pending after we read the timer,
  39. * it may have wrapped between checking the interrupt
  40. * status and reading the timer. Re-read the timer to
  41. * be sure its value is after the wrap.
  42. */
  43. asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr1));
  44. elapsed = get_elapsed();
  45. asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr2));
  46. if(tisr1 & 1)
  47. elapsed += LATCH;
  48. else if (tisr2 & 1)
  49. elapsed = LATCH + get_elapsed();
  50. /*
  51. * Now convert them to usec.
  52. */
  53. usec = (unsigned long)(elapsed / (CLOCK_TICK_RATE/1000000));
  54. return usec;
  55. }
  56. static irqreturn_t
  57. iop331_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  58. {
  59. u32 tisr;
  60. write_seqlock(&xtime_lock);
  61. asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr));
  62. tisr |= 1;
  63. asm volatile("mcr p6, 0, %0, c6, c1, 0" : : "r" (tisr));
  64. timer_tick(regs);
  65. write_sequnlock(&xtime_lock);
  66. return IRQ_HANDLED;
  67. }
  68. static struct irqaction iop331_timer_irq = {
  69. .name = "IOP331 Timer Tick",
  70. .handler = iop331_timer_interrupt,
  71. .flags = SA_INTERRUPT | SA_TIMER,
  72. };
  73. static void __init iop331_timer_init(void)
  74. {
  75. u32 timer_ctl;
  76. setup_irq(IRQ_IOP331_TIMER0, &iop331_timer_irq);
  77. timer_ctl = IOP331_TMR_EN | IOP331_TMR_PRIVILEGED | IOP331_TMR_RELOAD |
  78. IOP331_TMR_RATIO_1_1;
  79. asm volatile("mcr p6, 0, %0, c4, c1, 0" : : "r" (LATCH));
  80. asm volatile("mcr p6, 0, %0, c0, c1, 0" : : "r" (timer_ctl));
  81. }
  82. struct sys_timer iop331_timer = {
  83. .init = iop331_timer_init,
  84. .offset = iop331_gettimeoffset,
  85. };