sltimers.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /***************************************************************************/
  2. /*
  3. * sltimers.c -- generic ColdFire slice timer support.
  4. *
  5. * Copyright (C) 2009-2010, Philippe De Muyter <phdm@macqel.be>
  6. * based on
  7. * timers.c -- generic ColdFire hardware timer support.
  8. * Copyright (C) 1999-2008, Greg Ungerer <gerg@snapgear.com>
  9. */
  10. /***************************************************************************/
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/sched.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/profile.h>
  17. #include <linux/clocksource.h>
  18. #include <asm/io.h>
  19. #include <asm/traps.h>
  20. #include <asm/machdep.h>
  21. #include <asm/coldfire.h>
  22. #include <asm/mcfslt.h>
  23. #include <asm/mcfsim.h>
  24. /***************************************************************************/
  25. #ifdef CONFIG_HIGHPROFILE
  26. /*
  27. * By default use Slice Timer 1 as the profiler clock timer.
  28. */
  29. #define PA(a) (MCFSLT_TIMER1 + (a))
  30. /*
  31. * Choose a reasonably fast profile timer. Make it an odd value to
  32. * try and get good coverage of kernel operations.
  33. */
  34. #define PROFILEHZ 1013
  35. irqreturn_t mcfslt_profile_tick(int irq, void *dummy)
  36. {
  37. /* Reset Slice Timer 1 */
  38. __raw_writel(MCFSLT_SSR_BE | MCFSLT_SSR_TE, PA(MCFSLT_SSR));
  39. if (current->pid)
  40. profile_tick(CPU_PROFILING);
  41. return IRQ_HANDLED;
  42. }
  43. static struct irqaction mcfslt_profile_irq = {
  44. .name = "profile timer",
  45. .flags = IRQF_TIMER,
  46. .handler = mcfslt_profile_tick,
  47. };
  48. void mcfslt_profile_init(void)
  49. {
  50. printk(KERN_INFO "PROFILE: lodging TIMER 1 @ %dHz as profile timer\n",
  51. PROFILEHZ);
  52. setup_irq(MCF_IRQ_PROFILER, &mcfslt_profile_irq);
  53. /* Set up TIMER 2 as high speed profile clock */
  54. __raw_writel(MCF_BUSCLK / PROFILEHZ - 1, PA(MCFSLT_STCNT));
  55. __raw_writel(MCFSLT_SCR_RUN | MCFSLT_SCR_IEN | MCFSLT_SCR_TEN,
  56. PA(MCFSLT_SCR));
  57. }
  58. #endif /* CONFIG_HIGHPROFILE */
  59. /***************************************************************************/
  60. /*
  61. * By default use Slice Timer 0 as the system clock timer.
  62. */
  63. #define TA(a) (MCFSLT_TIMER0 + (a))
  64. static u32 mcfslt_cycles_per_jiffy;
  65. static u32 mcfslt_cnt;
  66. static irq_handler_t timer_interrupt;
  67. static irqreturn_t mcfslt_tick(int irq, void *dummy)
  68. {
  69. /* Reset Slice Timer 0 */
  70. __raw_writel(MCFSLT_SSR_BE | MCFSLT_SSR_TE, TA(MCFSLT_SSR));
  71. mcfslt_cnt += mcfslt_cycles_per_jiffy;
  72. return timer_interrupt(irq, dummy);
  73. }
  74. static struct irqaction mcfslt_timer_irq = {
  75. .name = "timer",
  76. .flags = IRQF_TIMER,
  77. .handler = mcfslt_tick,
  78. };
  79. static cycle_t mcfslt_read_clk(struct clocksource *cs)
  80. {
  81. unsigned long flags;
  82. u32 cycles, scnt;
  83. local_irq_save(flags);
  84. scnt = __raw_readl(TA(MCFSLT_SCNT));
  85. cycles = mcfslt_cnt;
  86. if (__raw_readl(TA(MCFSLT_SSR)) & MCFSLT_SSR_TE) {
  87. cycles += mcfslt_cycles_per_jiffy;
  88. scnt = __raw_readl(TA(MCFSLT_SCNT));
  89. }
  90. local_irq_restore(flags);
  91. /* subtract because slice timers count down */
  92. return cycles + ((mcfslt_cycles_per_jiffy - 1) - scnt);
  93. }
  94. static struct clocksource mcfslt_clk = {
  95. .name = "slt",
  96. .rating = 250,
  97. .read = mcfslt_read_clk,
  98. .mask = CLOCKSOURCE_MASK(32),
  99. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  100. };
  101. void hw_timer_init(irq_handler_t handler)
  102. {
  103. mcfslt_cycles_per_jiffy = MCF_BUSCLK / HZ;
  104. /*
  105. * The coldfire slice timer (SLT) runs from STCNT to 0 included,
  106. * then STCNT again and so on. It counts thus actually
  107. * STCNT + 1 steps for 1 tick, not STCNT. So if you want
  108. * n cycles, initialize STCNT with n - 1.
  109. */
  110. __raw_writel(mcfslt_cycles_per_jiffy - 1, TA(MCFSLT_STCNT));
  111. __raw_writel(MCFSLT_SCR_RUN | MCFSLT_SCR_IEN | MCFSLT_SCR_TEN,
  112. TA(MCFSLT_SCR));
  113. /* initialize mcfslt_cnt knowing that slice timers count down */
  114. mcfslt_cnt = mcfslt_cycles_per_jiffy;
  115. timer_interrupt = handler;
  116. setup_irq(MCF_IRQ_TIMER, &mcfslt_timer_irq);
  117. clocksource_register_hz(&mcfslt_clk, MCF_BUSCLK);
  118. #ifdef CONFIG_HIGHPROFILE
  119. mcfslt_profile_init();
  120. #endif
  121. }