timer-milbeaut.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Socionext Inc.
  4. */
  5. #include <linux/clk.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/irq.h>
  8. #include <linux/irqreturn.h>
  9. #include <linux/sched_clock.h>
  10. #include "timer-of.h"
  11. #define MLB_TMR_TMCSR_OFS 0x0
  12. #define MLB_TMR_TMR_OFS 0x4
  13. #define MLB_TMR_TMRLR1_OFS 0x8
  14. #define MLB_TMR_TMRLR2_OFS 0xc
  15. #define MLB_TMR_REGSZPCH 0x10
  16. #define MLB_TMR_TMCSR_OUTL BIT(5)
  17. #define MLB_TMR_TMCSR_RELD BIT(4)
  18. #define MLB_TMR_TMCSR_INTE BIT(3)
  19. #define MLB_TMR_TMCSR_UF BIT(2)
  20. #define MLB_TMR_TMCSR_CNTE BIT(1)
  21. #define MLB_TMR_TMCSR_TRG BIT(0)
  22. #define MLB_TMR_TMCSR_CSL_DIV2 0
  23. #define MLB_TMR_DIV_CNT 2
  24. #define MLB_TMR_SRC_CH 1
  25. #define MLB_TMR_EVT_CH 0
  26. #define MLB_TMR_SRC_CH_OFS (MLB_TMR_REGSZPCH * MLB_TMR_SRC_CH)
  27. #define MLB_TMR_EVT_CH_OFS (MLB_TMR_REGSZPCH * MLB_TMR_EVT_CH)
  28. #define MLB_TMR_SRC_TMCSR_OFS (MLB_TMR_SRC_CH_OFS + MLB_TMR_TMCSR_OFS)
  29. #define MLB_TMR_SRC_TMR_OFS (MLB_TMR_SRC_CH_OFS + MLB_TMR_TMR_OFS)
  30. #define MLB_TMR_SRC_TMRLR1_OFS (MLB_TMR_SRC_CH_OFS + MLB_TMR_TMRLR1_OFS)
  31. #define MLB_TMR_SRC_TMRLR2_OFS (MLB_TMR_SRC_CH_OFS + MLB_TMR_TMRLR2_OFS)
  32. #define MLB_TMR_EVT_TMCSR_OFS (MLB_TMR_EVT_CH_OFS + MLB_TMR_TMCSR_OFS)
  33. #define MLB_TMR_EVT_TMR_OFS (MLB_TMR_EVT_CH_OFS + MLB_TMR_TMR_OFS)
  34. #define MLB_TMR_EVT_TMRLR1_OFS (MLB_TMR_EVT_CH_OFS + MLB_TMR_TMRLR1_OFS)
  35. #define MLB_TMR_EVT_TMRLR2_OFS (MLB_TMR_EVT_CH_OFS + MLB_TMR_TMRLR2_OFS)
  36. #define MLB_TIMER_RATING 500
  37. #define MLB_TIMER_ONESHOT 0
  38. #define MLB_TIMER_PERIODIC 1
  39. static irqreturn_t mlb_timer_interrupt(int irq, void *dev_id)
  40. {
  41. struct clock_event_device *clk = dev_id;
  42. struct timer_of *to = to_timer_of(clk);
  43. u32 val;
  44. val = readl_relaxed(timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
  45. val &= ~MLB_TMR_TMCSR_UF;
  46. writel_relaxed(val, timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
  47. clk->event_handler(clk);
  48. return IRQ_HANDLED;
  49. }
  50. static void mlb_evt_timer_start(struct timer_of *to, bool periodic)
  51. {
  52. u32 val = MLB_TMR_TMCSR_CSL_DIV2;
  53. val |= MLB_TMR_TMCSR_CNTE | MLB_TMR_TMCSR_TRG | MLB_TMR_TMCSR_INTE;
  54. if (periodic)
  55. val |= MLB_TMR_TMCSR_RELD;
  56. writel_relaxed(val, timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
  57. }
  58. static void mlb_evt_timer_stop(struct timer_of *to)
  59. {
  60. u32 val = readl_relaxed(timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
  61. val &= ~MLB_TMR_TMCSR_CNTE;
  62. writel_relaxed(val, timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
  63. }
  64. static void mlb_evt_timer_register_count(struct timer_of *to, unsigned long cnt)
  65. {
  66. writel_relaxed(cnt, timer_of_base(to) + MLB_TMR_EVT_TMRLR1_OFS);
  67. }
  68. static int mlb_set_state_periodic(struct clock_event_device *clk)
  69. {
  70. struct timer_of *to = to_timer_of(clk);
  71. mlb_evt_timer_stop(to);
  72. mlb_evt_timer_register_count(to, to->of_clk.period);
  73. mlb_evt_timer_start(to, MLB_TIMER_PERIODIC);
  74. return 0;
  75. }
  76. static int mlb_set_state_oneshot(struct clock_event_device *clk)
  77. {
  78. struct timer_of *to = to_timer_of(clk);
  79. mlb_evt_timer_stop(to);
  80. mlb_evt_timer_start(to, MLB_TIMER_ONESHOT);
  81. return 0;
  82. }
  83. static int mlb_set_state_shutdown(struct clock_event_device *clk)
  84. {
  85. struct timer_of *to = to_timer_of(clk);
  86. mlb_evt_timer_stop(to);
  87. return 0;
  88. }
  89. static int mlb_clkevt_next_event(unsigned long event,
  90. struct clock_event_device *clk)
  91. {
  92. struct timer_of *to = to_timer_of(clk);
  93. mlb_evt_timer_stop(to);
  94. mlb_evt_timer_register_count(to, event);
  95. mlb_evt_timer_start(to, MLB_TIMER_ONESHOT);
  96. return 0;
  97. }
  98. static int mlb_config_clock_source(struct timer_of *to)
  99. {
  100. u32 val = MLB_TMR_TMCSR_CSL_DIV2;
  101. writel_relaxed(val, timer_of_base(to) + MLB_TMR_SRC_TMCSR_OFS);
  102. writel_relaxed(~0, timer_of_base(to) + MLB_TMR_SRC_TMRLR1_OFS);
  103. writel_relaxed(~0, timer_of_base(to) + MLB_TMR_SRC_TMRLR2_OFS);
  104. val |= MLB_TMR_TMCSR_RELD | MLB_TMR_TMCSR_CNTE | MLB_TMR_TMCSR_TRG;
  105. writel_relaxed(val, timer_of_base(to) + MLB_TMR_SRC_TMCSR_OFS);
  106. return 0;
  107. }
  108. static int mlb_config_clock_event(struct timer_of *to)
  109. {
  110. writel_relaxed(0, timer_of_base(to) + MLB_TMR_EVT_TMCSR_OFS);
  111. return 0;
  112. }
  113. static struct timer_of to = {
  114. .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
  115. .clkevt = {
  116. .name = "mlb-clkevt",
  117. .rating = MLB_TIMER_RATING,
  118. .cpumask = cpu_possible_mask,
  119. .features = CLOCK_EVT_FEAT_DYNIRQ | CLOCK_EVT_FEAT_ONESHOT,
  120. .set_state_oneshot = mlb_set_state_oneshot,
  121. .set_state_periodic = mlb_set_state_periodic,
  122. .set_state_shutdown = mlb_set_state_shutdown,
  123. .set_next_event = mlb_clkevt_next_event,
  124. },
  125. .of_irq = {
  126. .flags = IRQF_TIMER | IRQF_IRQPOLL,
  127. .handler = mlb_timer_interrupt,
  128. },
  129. };
  130. static u64 notrace mlb_timer_sched_read(void)
  131. {
  132. return ~readl_relaxed(timer_of_base(&to) + MLB_TMR_SRC_TMR_OFS);
  133. }
  134. static int __init mlb_timer_init(struct device_node *node)
  135. {
  136. int ret;
  137. unsigned long rate;
  138. ret = timer_of_init(node, &to);
  139. if (ret)
  140. return ret;
  141. rate = timer_of_rate(&to) / MLB_TMR_DIV_CNT;
  142. mlb_config_clock_source(&to);
  143. clocksource_mmio_init(timer_of_base(&to) + MLB_TMR_SRC_TMR_OFS,
  144. node->name, rate, MLB_TIMER_RATING, 32,
  145. clocksource_mmio_readl_down);
  146. sched_clock_register(mlb_timer_sched_read, 32, rate);
  147. mlb_config_clock_event(&to);
  148. clockevents_config_and_register(&to.clkevt, timer_of_rate(&to), 15,
  149. 0xffffffff);
  150. return 0;
  151. }
  152. TIMER_OF_DECLARE(mlb_peritimer, "socionext,milbeaut-timer",
  153. mlb_timer_init);