timers.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /***************************************************************************/
  2. /*
  3. * timers.c -- generic ColdFire hardware timer support.
  4. *
  5. * Copyright (C) 1999-2006, 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 <asm/io.h>
  14. #include <asm/irq.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 int ticks_per_intr;
  54. void coldfire_timer_init(irq_handler_t handler)
  55. {
  56. __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
  57. ticks_per_intr = (MCF_BUSCLK / 16) / HZ;
  58. __raw_writetrr(ticks_per_intr - 1, TA(MCFTIMER_TRR));
  59. __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
  60. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
  61. request_irq(mcf_timervector, handler, IRQF_DISABLED, "timer", NULL);
  62. mcf_settimericr(1, mcf_timerlevel);
  63. #ifdef CONFIG_HIGHPROFILE
  64. coldfire_profile_init();
  65. #endif
  66. }
  67. /***************************************************************************/
  68. unsigned long coldfire_timer_offset(void)
  69. {
  70. unsigned long tcn, offset;
  71. tcn = __raw_readw(TA(MCFTIMER_TCN));
  72. offset = ((tcn + 1) * (1000000 / HZ)) / ticks_per_intr;
  73. /* Check if we just wrapped the counters and maybe missed a tick */
  74. if ((offset < (1000000 / HZ / 2)) && mcf_timerirqpending(1))
  75. offset += 1000000 / HZ;
  76. return offset;
  77. }
  78. /***************************************************************************/
  79. #ifdef CONFIG_HIGHPROFILE
  80. /***************************************************************************/
  81. /*
  82. * By default use timer2 as the profiler clock timer.
  83. */
  84. #define PA(a) (MCF_MBAR + MCFTIMER_BASE2 + (a))
  85. /*
  86. * Choose a reasonably fast profile timer. Make it an odd value to
  87. * try and get good coverage of kernel operations.
  88. */
  89. #define PROFILEHZ 1013
  90. /*
  91. * Use the other timer to provide high accuracy profiling info.
  92. */
  93. irqreturn_t coldfire_profile_tick(int irq, void *dummy)
  94. {
  95. /* Reset ColdFire timer2 */
  96. __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
  97. if (current->pid)
  98. profile_tick(CPU_PROFILING, regs);
  99. return IRQ_HANDLED;
  100. }
  101. /***************************************************************************/
  102. void coldfire_profile_init(void)
  103. {
  104. printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n", PROFILEHZ);
  105. /* Set up TIMER 2 as high speed profile clock */
  106. __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
  107. __raw_writetrr(((MCF_CLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
  108. __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
  109. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
  110. request_irq(mcf_profilevector, coldfire_profile_tick,
  111. (IRQF_DISABLED | IRQ_FLG_FAST), "profile timer", NULL);
  112. mcf_settimericr(2, 7);
  113. }
  114. /***************************************************************************/
  115. #endif /* CONFIG_HIGHPROFILE */
  116. /***************************************************************************/