timers.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /***************************************************************************/
  2. /*
  3. * timers.c -- generic ColdFire hardware timer support.
  4. *
  5. * Copyright (C) 1999-2007, Greg Ungerer (gerg@snapgear.com)
  6. */
  7. /***************************************************************************/
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/sched.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/irq.h>
  13. #include <asm/io.h>
  14. #include <asm/traps.h>
  15. #include <asm/machdep.h>
  16. #include <asm/coldfire.h>
  17. #include <asm/mcftimer.h>
  18. #include <asm/mcfsim.h>
  19. /***************************************************************************/
  20. /*
  21. * By default use timer1 as the system clock timer.
  22. */
  23. #define TA(a) (MCF_MBAR + MCFTIMER_BASE1 + (a))
  24. /*
  25. * Default the timer and vector to use for ColdFire. Some ColdFire
  26. * CPU's and some boards may want different. Their sub-architecture
  27. * startup code (in config.c) can change these if they want.
  28. */
  29. unsigned int mcf_timervector = 29;
  30. unsigned int mcf_profilevector = 31;
  31. unsigned int mcf_timerlevel = 5;
  32. /*
  33. * These provide the underlying interrupt vector support.
  34. * Unfortunately it is a little different on each ColdFire.
  35. */
  36. extern void mcf_settimericr(int timer, int level);
  37. extern int mcf_timerirqpending(int timer);
  38. #if defined(CONFIG_M532x)
  39. #define __raw_readtrr __raw_readl
  40. #define __raw_writetrr __raw_writel
  41. #else
  42. #define __raw_readtrr __raw_readw
  43. #define __raw_writetrr __raw_writew
  44. #endif
  45. /***************************************************************************/
  46. static irqreturn_t hw_tick(int irq, void *dummy)
  47. {
  48. /* Reset the ColdFire timer */
  49. __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER));
  50. return arch_timer_interrupt(irq, dummy);
  51. }
  52. /***************************************************************************/
  53. static struct irqaction coldfire_timer_irq = {
  54. .name = "timer",
  55. .flags = IRQF_DISABLED | IRQF_TIMER,
  56. .handler = hw_tick,
  57. };
  58. /***************************************************************************/
  59. static int ticks_per_intr;
  60. void hw_timer_init(void)
  61. {
  62. setup_irq(mcf_timervector, &coldfire_timer_irq);
  63. __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
  64. ticks_per_intr = (MCF_BUSCLK / 16) / HZ;
  65. __raw_writetrr(ticks_per_intr - 1, TA(MCFTIMER_TRR));
  66. __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
  67. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
  68. mcf_settimericr(1, mcf_timerlevel);
  69. #ifdef CONFIG_HIGHPROFILE
  70. coldfire_profile_init();
  71. #endif
  72. }
  73. /***************************************************************************/
  74. unsigned long hw_timer_offset(void)
  75. {
  76. unsigned long tcn, offset;
  77. tcn = __raw_readw(TA(MCFTIMER_TCN));
  78. offset = ((tcn + 1) * (1000000 / HZ)) / ticks_per_intr;
  79. /* Check if we just wrapped the counters and maybe missed a tick */
  80. if ((offset < (1000000 / HZ / 2)) && mcf_timerirqpending(1))
  81. offset += 1000000 / HZ;
  82. return offset;
  83. }
  84. /***************************************************************************/
  85. #ifdef CONFIG_HIGHPROFILE
  86. /***************************************************************************/
  87. /*
  88. * By default use timer2 as the profiler clock timer.
  89. */
  90. #define PA(a) (MCF_MBAR + MCFTIMER_BASE2 + (a))
  91. /*
  92. * Choose a reasonably fast profile timer. Make it an odd value to
  93. * try and get good coverage of kernel operations.
  94. */
  95. #define PROFILEHZ 1013
  96. /*
  97. * Use the other timer to provide high accuracy profiling info.
  98. */
  99. irqreturn_t coldfire_profile_tick(int irq, void *dummy)
  100. {
  101. /* Reset ColdFire timer2 */
  102. __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
  103. if (current->pid)
  104. profile_tick(CPU_PROFILING, regs);
  105. return IRQ_HANDLED;
  106. }
  107. /***************************************************************************/
  108. void coldfire_profile_init(void)
  109. {
  110. printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n", PROFILEHZ);
  111. /* Set up TIMER 2 as high speed profile clock */
  112. __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
  113. __raw_writetrr(((MCF_CLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
  114. __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
  115. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
  116. request_irq(mcf_profilevector, coldfire_profile_tick,
  117. (IRQF_DISABLED | IRQ_FLG_FAST), "profile timer", NULL);
  118. mcf_settimericr(2, 7);
  119. }
  120. /***************************************************************************/
  121. #endif /* CONFIG_HIGHPROFILE */
  122. /***************************************************************************/