sltimers.c 3.8 KB

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