timers.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /***************************************************************************/
  2. /*
  3. * timers.c -- generic ColdFire hardware timer support.
  4. *
  5. * Copyright (C) 1999-2008, 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 <linux/profile.h>
  14. #include <linux/clocksource.h>
  15. #include <asm/io.h>
  16. #include <asm/traps.h>
  17. #include <asm/machdep.h>
  18. #include <asm/coldfire.h>
  19. #include <asm/mcftimer.h>
  20. #include <asm/mcfsim.h>
  21. /***************************************************************************/
  22. /*
  23. * By default use timer1 as the system clock timer.
  24. */
  25. #define FREQ (MCF_BUSCLK / 16)
  26. #define TA(a) (MCF_MBAR + MCFTIMER_BASE1 + (a))
  27. /*
  28. * Default the timer and vector to use for ColdFire. Some ColdFire
  29. * CPU's and some boards may want different. Their sub-architecture
  30. * startup code (in config.c) can change these if they want.
  31. */
  32. unsigned int mcf_timervector = 29;
  33. unsigned int mcf_profilevector = 31;
  34. unsigned int mcf_timerlevel = 5;
  35. /*
  36. * These provide the underlying interrupt vector support.
  37. * Unfortunately it is a little different on each ColdFire.
  38. */
  39. extern void mcf_settimericr(int timer, int level);
  40. void coldfire_profile_init(void);
  41. #if defined(CONFIG_M532x)
  42. #define __raw_readtrr __raw_readl
  43. #define __raw_writetrr __raw_writel
  44. #else
  45. #define __raw_readtrr __raw_readw
  46. #define __raw_writetrr __raw_writew
  47. #endif
  48. static u32 mcftmr_cycles_per_jiffy;
  49. static u32 mcftmr_cnt;
  50. /***************************************************************************/
  51. static irqreturn_t mcftmr_tick(int irq, void *dummy)
  52. {
  53. /* Reset the ColdFire timer */
  54. __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER));
  55. mcftmr_cnt += mcftmr_cycles_per_jiffy;
  56. return arch_timer_interrupt(irq, dummy);
  57. }
  58. /***************************************************************************/
  59. static struct irqaction mcftmr_timer_irq = {
  60. .name = "timer",
  61. .flags = IRQF_DISABLED | IRQF_TIMER,
  62. .handler = mcftmr_tick,
  63. };
  64. /***************************************************************************/
  65. static cycle_t mcftmr_read_clk(void)
  66. {
  67. unsigned long flags;
  68. u32 cycles;
  69. u16 tcn;
  70. local_irq_save(flags);
  71. tcn = __raw_readw(TA(MCFTIMER_TCN));
  72. cycles = mcftmr_cnt;
  73. local_irq_restore(flags);
  74. return cycles + tcn;
  75. }
  76. /***************************************************************************/
  77. static struct clocksource mcftmr_clk = {
  78. .name = "tmr",
  79. .rating = 250,
  80. .read = mcftmr_read_clk,
  81. .shift = 20,
  82. .mask = CLOCKSOURCE_MASK(32),
  83. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  84. };
  85. /***************************************************************************/
  86. void hw_timer_init(void)
  87. {
  88. setup_irq(mcf_timervector, &mcftmr_timer_irq);
  89. __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
  90. mcftmr_cycles_per_jiffy = FREQ / HZ;
  91. /*
  92. * The coldfire timer runs from 0 to TRR included, then 0
  93. * again and so on. It counts thus actually TRR + 1 steps
  94. * for 1 tick, not TRR. So if you want n cycles,
  95. * initialize TRR with n - 1.
  96. */
  97. __raw_writetrr(mcftmr_cycles_per_jiffy - 1, TA(MCFTIMER_TRR));
  98. __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
  99. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
  100. mcftmr_clk.mult = clocksource_hz2mult(FREQ, mcftmr_clk.shift);
  101. clocksource_register(&mcftmr_clk);
  102. mcf_settimericr(1, mcf_timerlevel);
  103. #ifdef CONFIG_HIGHPROFILE
  104. coldfire_profile_init();
  105. #endif
  106. }
  107. /***************************************************************************/
  108. #ifdef CONFIG_HIGHPROFILE
  109. /***************************************************************************/
  110. /*
  111. * By default use timer2 as the profiler clock timer.
  112. */
  113. #define PA(a) (MCF_MBAR + MCFTIMER_BASE2 + (a))
  114. /*
  115. * Choose a reasonably fast profile timer. Make it an odd value to
  116. * try and get good coverage of kernel operations.
  117. */
  118. #define PROFILEHZ 1013
  119. /*
  120. * Use the other timer to provide high accuracy profiling info.
  121. */
  122. irqreturn_t coldfire_profile_tick(int irq, void *dummy)
  123. {
  124. /* Reset ColdFire timer2 */
  125. __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
  126. if (current->pid)
  127. profile_tick(CPU_PROFILING);
  128. return IRQ_HANDLED;
  129. }
  130. /***************************************************************************/
  131. static struct irqaction coldfire_profile_irq = {
  132. .name = "profile timer",
  133. .flags = IRQF_DISABLED | IRQF_TIMER,
  134. .handler = coldfire_profile_tick,
  135. };
  136. void coldfire_profile_init(void)
  137. {
  138. printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n",
  139. PROFILEHZ);
  140. setup_irq(mcf_profilevector, &coldfire_profile_irq);
  141. /* Set up TIMER 2 as high speed profile clock */
  142. __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
  143. __raw_writetrr(((MCF_BUSCLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
  144. __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
  145. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
  146. mcf_settimericr(2, 7);
  147. }
  148. /***************************************************************************/
  149. #endif /* CONFIG_HIGHPROFILE */
  150. /***************************************************************************/