iop321-time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * arch/arm/mach-iop3xx/iop321-time.c
  3. *
  4. * Timer code for IOP321 based systems
  5. *
  6. * Author: Deepak Saxena <dsaxena@mvista.com>
  7. *
  8. * Copyright 2002-2003 MontaVista Software Inc.
  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. #define IOP321_TIME_SYNC 0
  28. static inline unsigned long get_elapsed(void)
  29. {
  30. return LATCH - *IOP321_TU_TCR0;
  31. }
  32. static unsigned long iop321_gettimeoffset(void)
  33. {
  34. unsigned long elapsed, usec;
  35. u32 tisr1, tisr2;
  36. /*
  37. * If an interrupt was pending before we read the timer,
  38. * we've already wrapped. Factor this into the time.
  39. * If an interrupt was pending after we read the timer,
  40. * it may have wrapped between checking the interrupt
  41. * status and reading the timer. Re-read the timer to
  42. * be sure its value is after the wrap.
  43. */
  44. asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr1));
  45. elapsed = get_elapsed();
  46. asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr2));
  47. if(tisr1 & 1)
  48. elapsed += LATCH;
  49. else if (tisr2 & 1)
  50. elapsed = LATCH + get_elapsed();
  51. /*
  52. * Now convert them to usec.
  53. */
  54. usec = (unsigned long)(elapsed / (CLOCK_TICK_RATE/1000000));
  55. return usec;
  56. }
  57. static irqreturn_t
  58. iop321_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  59. {
  60. u32 tisr;
  61. write_seqlock(&xtime_lock);
  62. asm volatile("mrc p6, 0, %0, c6, c1, 0" : "=r" (tisr));
  63. tisr |= 1;
  64. asm volatile("mcr p6, 0, %0, c6, c1, 0" : : "r" (tisr));
  65. timer_tick(regs);
  66. write_sequnlock(&xtime_lock);
  67. return IRQ_HANDLED;
  68. }
  69. static struct irqaction iop321_timer_irq = {
  70. .name = "IOP321 Timer Tick",
  71. .handler = iop321_timer_interrupt,
  72. .flags = SA_INTERRUPT | SA_TIMER,
  73. };
  74. static void __init iop321_timer_init(void)
  75. {
  76. u32 timer_ctl;
  77. setup_irq(IRQ_IOP321_TIMER0, &iop321_timer_irq);
  78. timer_ctl = IOP321_TMR_EN | IOP321_TMR_PRIVILEGED | IOP321_TMR_RELOAD |
  79. IOP321_TMR_RATIO_1_1;
  80. asm volatile("mcr p6, 0, %0, c4, c1, 0" : : "r" (LATCH));
  81. asm volatile("mcr p6, 0, %0, c0, c1, 0" : : "r" (timer_ctl));
  82. }
  83. struct sys_timer iop321_timer = {
  84. .init = &iop321_timer_init,
  85. .offset = iop321_gettimeoffset,
  86. };