timers.c 4.3 KB

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