timer-imx-tpm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright 2016 Freescale Semiconductor, Inc.
  4. // Copyright 2017 NXP
  5. #include <linux/clk.h>
  6. #include <linux/clockchips.h>
  7. #include <linux/clocksource.h>
  8. #include <linux/delay.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/sched_clock.h>
  11. #include "timer-of.h"
  12. #define TPM_PARAM 0x4
  13. #define TPM_PARAM_WIDTH_SHIFT 16
  14. #define TPM_PARAM_WIDTH_MASK (0xff << 16)
  15. #define TPM_SC 0x10
  16. #define TPM_SC_CMOD_INC_PER_CNT (0x1 << 3)
  17. #define TPM_SC_CMOD_DIV_DEFAULT 0x3
  18. #define TPM_SC_CMOD_DIV_MAX 0x7
  19. #define TPM_SC_TOF_MASK (0x1 << 7)
  20. #define TPM_CNT 0x14
  21. #define TPM_MOD 0x18
  22. #define TPM_STATUS 0x1c
  23. #define TPM_STATUS_CH0F BIT(0)
  24. #define TPM_C0SC 0x20
  25. #define TPM_C0SC_CHIE BIT(6)
  26. #define TPM_C0SC_MODE_SHIFT 2
  27. #define TPM_C0SC_MODE_MASK 0x3c
  28. #define TPM_C0SC_MODE_SW_COMPARE 0x4
  29. #define TPM_C0SC_CHF_MASK (0x1 << 7)
  30. #define TPM_C0V 0x24
  31. static int counter_width __ro_after_init;
  32. static void __iomem *timer_base __ro_after_init;
  33. static inline void tpm_timer_disable(void)
  34. {
  35. unsigned int val;
  36. /* channel disable */
  37. val = readl(timer_base + TPM_C0SC);
  38. val &= ~(TPM_C0SC_MODE_MASK | TPM_C0SC_CHIE);
  39. writel(val, timer_base + TPM_C0SC);
  40. }
  41. static inline void tpm_timer_enable(void)
  42. {
  43. unsigned int val;
  44. /* channel enabled in sw compare mode */
  45. val = readl(timer_base + TPM_C0SC);
  46. val |= (TPM_C0SC_MODE_SW_COMPARE << TPM_C0SC_MODE_SHIFT) |
  47. TPM_C0SC_CHIE;
  48. writel(val, timer_base + TPM_C0SC);
  49. }
  50. static inline void tpm_irq_acknowledge(void)
  51. {
  52. writel(TPM_STATUS_CH0F, timer_base + TPM_STATUS);
  53. }
  54. static inline unsigned long tpm_read_counter(void)
  55. {
  56. return readl(timer_base + TPM_CNT);
  57. }
  58. #if defined(CONFIG_ARM)
  59. static struct delay_timer tpm_delay_timer;
  60. static unsigned long tpm_read_current_timer(void)
  61. {
  62. return tpm_read_counter();
  63. }
  64. static u64 notrace tpm_read_sched_clock(void)
  65. {
  66. return tpm_read_counter();
  67. }
  68. #endif
  69. static int tpm_set_next_event(unsigned long delta,
  70. struct clock_event_device *evt)
  71. {
  72. unsigned long next, now;
  73. next = tpm_read_counter();
  74. next += delta;
  75. writel(next, timer_base + TPM_C0V);
  76. now = tpm_read_counter();
  77. /*
  78. * NOTE: We observed in a very small probability, the bus fabric
  79. * contention between GPU and A7 may results a few cycles delay
  80. * of writing CNT registers which may cause the min_delta event got
  81. * missed, so we need add a ETIME check here in case it happened.
  82. */
  83. return (int)(next - now) <= 0 ? -ETIME : 0;
  84. }
  85. static int tpm_set_state_oneshot(struct clock_event_device *evt)
  86. {
  87. tpm_timer_enable();
  88. return 0;
  89. }
  90. static int tpm_set_state_shutdown(struct clock_event_device *evt)
  91. {
  92. tpm_timer_disable();
  93. return 0;
  94. }
  95. static irqreturn_t tpm_timer_interrupt(int irq, void *dev_id)
  96. {
  97. struct clock_event_device *evt = dev_id;
  98. tpm_irq_acknowledge();
  99. evt->event_handler(evt);
  100. return IRQ_HANDLED;
  101. }
  102. static struct timer_of to_tpm = {
  103. .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
  104. .clkevt = {
  105. .name = "i.MX TPM Timer",
  106. .rating = 200,
  107. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ,
  108. .set_state_shutdown = tpm_set_state_shutdown,
  109. .set_state_oneshot = tpm_set_state_oneshot,
  110. .set_next_event = tpm_set_next_event,
  111. .cpumask = cpu_possible_mask,
  112. },
  113. .of_irq = {
  114. .handler = tpm_timer_interrupt,
  115. .flags = IRQF_TIMER,
  116. },
  117. .of_clk = {
  118. .name = "per",
  119. },
  120. };
  121. static int __init tpm_clocksource_init(void)
  122. {
  123. #if defined(CONFIG_ARM)
  124. tpm_delay_timer.read_current_timer = &tpm_read_current_timer;
  125. tpm_delay_timer.freq = timer_of_rate(&to_tpm) >> 3;
  126. register_current_timer_delay(&tpm_delay_timer);
  127. sched_clock_register(tpm_read_sched_clock, counter_width,
  128. timer_of_rate(&to_tpm) >> 3);
  129. #endif
  130. return clocksource_mmio_init(timer_base + TPM_CNT,
  131. "imx-tpm",
  132. timer_of_rate(&to_tpm) >> 3,
  133. to_tpm.clkevt.rating,
  134. counter_width,
  135. clocksource_mmio_readl_up);
  136. }
  137. static void __init tpm_clockevent_init(void)
  138. {
  139. clockevents_config_and_register(&to_tpm.clkevt,
  140. timer_of_rate(&to_tpm) >> 3,
  141. 300,
  142. GENMASK(counter_width - 1,
  143. 1));
  144. }
  145. static int __init tpm_timer_init(struct device_node *np)
  146. {
  147. struct clk *ipg;
  148. int ret;
  149. ipg = of_clk_get_by_name(np, "ipg");
  150. if (IS_ERR(ipg)) {
  151. pr_err("tpm: failed to get ipg clk\n");
  152. return -ENODEV;
  153. }
  154. /* enable clk before accessing registers */
  155. ret = clk_prepare_enable(ipg);
  156. if (ret) {
  157. pr_err("tpm: ipg clock enable failed (%d)\n", ret);
  158. clk_put(ipg);
  159. return ret;
  160. }
  161. ret = timer_of_init(np, &to_tpm);
  162. if (ret)
  163. return ret;
  164. timer_base = timer_of_base(&to_tpm);
  165. counter_width = (readl(timer_base + TPM_PARAM)
  166. & TPM_PARAM_WIDTH_MASK) >> TPM_PARAM_WIDTH_SHIFT;
  167. /* use rating 200 for 32-bit counter and 150 for 16-bit counter */
  168. to_tpm.clkevt.rating = counter_width == 0x20 ? 200 : 150;
  169. /*
  170. * Initialize tpm module to a known state
  171. * 1) Counter disabled
  172. * 2) TPM counter operates in up counting mode
  173. * 3) Timer Overflow Interrupt disabled
  174. * 4) Channel0 disabled
  175. * 5) DMA transfers disabled
  176. */
  177. /* make sure counter is disabled */
  178. writel(0, timer_base + TPM_SC);
  179. /* TOF is W1C */
  180. writel(TPM_SC_TOF_MASK, timer_base + TPM_SC);
  181. writel(0, timer_base + TPM_CNT);
  182. /* CHF is W1C */
  183. writel(TPM_C0SC_CHF_MASK, timer_base + TPM_C0SC);
  184. /*
  185. * increase per cnt,
  186. * div 8 for 32-bit counter and div 128 for 16-bit counter
  187. */
  188. writel(TPM_SC_CMOD_INC_PER_CNT |
  189. (counter_width == 0x20 ?
  190. TPM_SC_CMOD_DIV_DEFAULT : TPM_SC_CMOD_DIV_MAX),
  191. timer_base + TPM_SC);
  192. /* set MOD register to maximum for free running mode */
  193. writel(GENMASK(counter_width - 1, 0), timer_base + TPM_MOD);
  194. tpm_clockevent_init();
  195. return tpm_clocksource_init();
  196. }
  197. TIMER_OF_DECLARE(imx7ulp, "fsl,imx7ulp-tpm", tpm_timer_init);