time.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * arch/arm/mach-iop13xx/time.c
  3. *
  4. * Timer code for IOP13xx (copied from IOP32x/IOP33x implementation)
  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/io.h>
  21. #include <asm/irq.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/mach/irq.h>
  24. #include <asm/mach/time.h>
  25. static unsigned long ticks_per_jiffy;
  26. static unsigned long ticks_per_usec;
  27. static unsigned long next_jiffy_time;
  28. static inline u32 read_tcr1(void)
  29. {
  30. u32 val;
  31. asm volatile("mrc p6, 0, %0, c3, c9, 0" : "=r" (val));
  32. return val;
  33. }
  34. unsigned long iop13xx_gettimeoffset(void)
  35. {
  36. unsigned long offset;
  37. u32 cp_flags;
  38. cp_flags = iop13xx_cp6_save();
  39. offset = next_jiffy_time - read_tcr1();
  40. iop13xx_cp6_restore(cp_flags);
  41. return offset / ticks_per_usec;
  42. }
  43. static irqreturn_t
  44. iop13xx_timer_interrupt(int irq, void *dev_id)
  45. {
  46. u32 cp_flags = iop13xx_cp6_save();
  47. write_seqlock(&xtime_lock);
  48. asm volatile("mcr p6, 0, %0, c6, c9, 0" : : "r" (1));
  49. while ((signed long)(next_jiffy_time - read_tcr1())
  50. >= ticks_per_jiffy) {
  51. timer_tick();
  52. next_jiffy_time -= ticks_per_jiffy;
  53. }
  54. write_sequnlock(&xtime_lock);
  55. iop13xx_cp6_restore(cp_flags);
  56. return IRQ_HANDLED;
  57. }
  58. static struct irqaction iop13xx_timer_irq = {
  59. .name = "IOP13XX Timer Tick",
  60. .handler = iop13xx_timer_interrupt,
  61. .flags = IRQF_DISABLED | IRQF_TIMER,
  62. };
  63. void __init iop13xx_init_time(unsigned long tick_rate)
  64. {
  65. u32 timer_ctl;
  66. u32 cp_flags;
  67. ticks_per_jiffy = (tick_rate + HZ/2) / HZ;
  68. ticks_per_usec = tick_rate / 1000000;
  69. next_jiffy_time = 0xffffffff;
  70. timer_ctl = IOP13XX_TMR_EN | IOP13XX_TMR_PRIVILEGED |
  71. IOP13XX_TMR_RELOAD | IOP13XX_TMR_RATIO_1_1;
  72. /*
  73. * We use timer 0 for our timer interrupt, and timer 1 as
  74. * monotonic counter for tracking missed jiffies.
  75. */
  76. cp_flags = iop13xx_cp6_save();
  77. asm volatile("mcr p6, 0, %0, c4, c9, 0" : : "r" (ticks_per_jiffy - 1));
  78. asm volatile("mcr p6, 0, %0, c0, c9, 0" : : "r" (timer_ctl));
  79. asm volatile("mcr p6, 0, %0, c5, c9, 0" : : "r" (0xffffffff));
  80. asm volatile("mcr p6, 0, %0, c1, c9, 0" : : "r" (timer_ctl));
  81. iop13xx_cp6_restore(cp_flags);
  82. setup_irq(IRQ_IOP13XX_TIMER0, &iop13xx_timer_irq);
  83. }