timers.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0
  2. /***************************************************************************/
  3. /*
  4. * timers.c -- generic ColdFire hardware timer support.
  5. *
  6. * Copyright (C) 1999-2008, Greg Ungerer <[email protected]>
  7. */
  8. /***************************************************************************/
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/sched.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/irq.h>
  14. #include <linux/profile.h>
  15. #include <linux/clocksource.h>
  16. #include <asm/io.h>
  17. #include <asm/traps.h>
  18. #include <asm/machdep.h>
  19. #include <asm/coldfire.h>
  20. #include <asm/mcftimer.h>
  21. #include <asm/mcfsim.h>
  22. /***************************************************************************/
  23. /*
  24. * By default use timer1 as the system clock timer.
  25. */
  26. #define FREQ (MCF_BUSCLK / 16)
  27. #define TA(a) (MCFTIMER_BASE1 + (a))
  28. /*
  29. * These provide the underlying interrupt vector support.
  30. * Unfortunately it is a little different on each ColdFire.
  31. */
  32. void coldfire_profile_init(void);
  33. #if defined(CONFIG_M53xx) || defined(CONFIG_M5441x)
  34. #define __raw_readtrr __raw_readl
  35. #define __raw_writetrr __raw_writel
  36. #else
  37. #define __raw_readtrr __raw_readw
  38. #define __raw_writetrr __raw_writew
  39. #endif
  40. static u32 mcftmr_cycles_per_jiffy;
  41. static u32 mcftmr_cnt;
  42. /***************************************************************************/
  43. static void init_timer_irq(void)
  44. {
  45. #ifdef MCFSIM_ICR_AUTOVEC
  46. /* Timer1 is always used as system timer */
  47. writeb(MCFSIM_ICR_AUTOVEC | MCFSIM_ICR_LEVEL6 | MCFSIM_ICR_PRI3,
  48. MCFSIM_TIMER1ICR);
  49. mcf_mapirq2imr(MCF_IRQ_TIMER, MCFINTC_TIMER1);
  50. #ifdef CONFIG_HIGHPROFILE
  51. /* Timer2 is to be used as a high speed profile timer */
  52. writeb(MCFSIM_ICR_AUTOVEC | MCFSIM_ICR_LEVEL7 | MCFSIM_ICR_PRI3,
  53. MCFSIM_TIMER2ICR);
  54. mcf_mapirq2imr(MCF_IRQ_PROFILER, MCFINTC_TIMER2);
  55. #endif
  56. #endif /* MCFSIM_ICR_AUTOVEC */
  57. }
  58. /***************************************************************************/
  59. static irqreturn_t mcftmr_tick(int irq, void *dummy)
  60. {
  61. /* Reset the ColdFire timer */
  62. __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, TA(MCFTIMER_TER));
  63. mcftmr_cnt += mcftmr_cycles_per_jiffy;
  64. legacy_timer_tick(1);
  65. return IRQ_HANDLED;
  66. }
  67. /***************************************************************************/
  68. static u64 mcftmr_read_clk(struct clocksource *cs)
  69. {
  70. unsigned long flags;
  71. u32 cycles;
  72. u16 tcn;
  73. local_irq_save(flags);
  74. tcn = __raw_readw(TA(MCFTIMER_TCN));
  75. cycles = mcftmr_cnt;
  76. local_irq_restore(flags);
  77. return cycles + tcn;
  78. }
  79. /***************************************************************************/
  80. static struct clocksource mcftmr_clk = {
  81. .name = "tmr",
  82. .rating = 250,
  83. .read = mcftmr_read_clk,
  84. .mask = CLOCKSOURCE_MASK(32),
  85. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  86. };
  87. /***************************************************************************/
  88. void hw_timer_init(void)
  89. {
  90. int r;
  91. __raw_writew(MCFTIMER_TMR_DISABLE, TA(MCFTIMER_TMR));
  92. mcftmr_cycles_per_jiffy = FREQ / HZ;
  93. /*
  94. * The coldfire timer runs from 0 to TRR included, then 0
  95. * again and so on. It counts thus actually TRR + 1 steps
  96. * for 1 tick, not TRR. So if you want n cycles,
  97. * initialize TRR with n - 1.
  98. */
  99. __raw_writetrr(mcftmr_cycles_per_jiffy - 1, TA(MCFTIMER_TRR));
  100. __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
  101. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, TA(MCFTIMER_TMR));
  102. clocksource_register_hz(&mcftmr_clk, FREQ);
  103. init_timer_irq();
  104. r = request_irq(MCF_IRQ_TIMER, mcftmr_tick, IRQF_TIMER, "timer", NULL);
  105. if (r) {
  106. pr_err("Failed to request irq %d (timer): %pe\n", MCF_IRQ_TIMER,
  107. ERR_PTR(r));
  108. }
  109. #ifdef CONFIG_HIGHPROFILE
  110. coldfire_profile_init();
  111. #endif
  112. }
  113. /***************************************************************************/
  114. #ifdef CONFIG_HIGHPROFILE
  115. /***************************************************************************/
  116. /*
  117. * By default use timer2 as the profiler clock timer.
  118. */
  119. #define PA(a) (MCFTIMER_BASE2 + (a))
  120. /*
  121. * Choose a reasonably fast profile timer. Make it an odd value to
  122. * try and get good coverage of kernel operations.
  123. */
  124. #define PROFILEHZ 1013
  125. /*
  126. * Use the other timer to provide high accuracy profiling info.
  127. */
  128. irqreturn_t coldfire_profile_tick(int irq, void *dummy)
  129. {
  130. /* Reset ColdFire timer2 */
  131. __raw_writeb(MCFTIMER_TER_CAP | MCFTIMER_TER_REF, PA(MCFTIMER_TER));
  132. if (current->pid)
  133. profile_tick(CPU_PROFILING);
  134. return IRQ_HANDLED;
  135. }
  136. /***************************************************************************/
  137. void coldfire_profile_init(void)
  138. {
  139. int ret;
  140. printk(KERN_INFO "PROFILE: lodging TIMER2 @ %dHz as profile timer\n",
  141. PROFILEHZ);
  142. /* Set up TIMER 2 as high speed profile clock */
  143. __raw_writew(MCFTIMER_TMR_DISABLE, PA(MCFTIMER_TMR));
  144. __raw_writetrr(((MCF_BUSCLK / 16) / PROFILEHZ), PA(MCFTIMER_TRR));
  145. __raw_writew(MCFTIMER_TMR_ENORI | MCFTIMER_TMR_CLK16 |
  146. MCFTIMER_TMR_RESTART | MCFTIMER_TMR_ENABLE, PA(MCFTIMER_TMR));
  147. ret = request_irq(MCF_IRQ_PROFILER, coldfire_profile_tick, IRQF_TIMER,
  148. "profile timer", NULL);
  149. if (ret) {
  150. pr_err("Failed to request irq %d (profile timer): %pe\n",
  151. MCF_IRQ_PROFILER, ERR_PTR(ret));
  152. }
  153. }
  154. /***************************************************************************/
  155. #endif /* CONFIG_HIGHPROFILE */
  156. /***************************************************************************/