timer-imx-tpm.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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, prev, now;
  73. prev = tpm_read_counter();
  74. next = prev + delta;
  75. writel(next, timer_base + TPM_C0V);
  76. now = tpm_read_counter();
  77. /*
  78. * Need to wait CNT increase at least 1 cycle to make sure
  79. * the C0V has been updated into HW.
  80. */
  81. if ((next & 0xffffffff) != readl(timer_base + TPM_C0V))
  82. while (now == tpm_read_counter())
  83. ;
  84. /*
  85. * NOTE: We observed in a very small probability, the bus fabric
  86. * contention between GPU and A7 may results a few cycles delay
  87. * of writing CNT registers which may cause the min_delta event got
  88. * missed, so we need add a ETIME check here in case it happened.
  89. */
  90. return (now - prev) >= delta ? -ETIME : 0;
  91. }
  92. static int tpm_set_state_oneshot(struct clock_event_device *evt)
  93. {
  94. tpm_timer_enable();
  95. return 0;
  96. }
  97. static int tpm_set_state_shutdown(struct clock_event_device *evt)
  98. {
  99. tpm_timer_disable();
  100. return 0;
  101. }
  102. static irqreturn_t tpm_timer_interrupt(int irq, void *dev_id)
  103. {
  104. struct clock_event_device *evt = dev_id;
  105. tpm_irq_acknowledge();
  106. evt->event_handler(evt);
  107. return IRQ_HANDLED;
  108. }
  109. static struct timer_of to_tpm = {
  110. .flags = TIMER_OF_IRQ | TIMER_OF_BASE | TIMER_OF_CLOCK,
  111. .clkevt = {
  112. .name = "i.MX TPM Timer",
  113. .rating = 200,
  114. .features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_DYNIRQ,
  115. .set_state_shutdown = tpm_set_state_shutdown,
  116. .set_state_oneshot = tpm_set_state_oneshot,
  117. .set_next_event = tpm_set_next_event,
  118. .cpumask = cpu_possible_mask,
  119. },
  120. .of_irq = {
  121. .handler = tpm_timer_interrupt,
  122. .flags = IRQF_TIMER,
  123. },
  124. .of_clk = {
  125. .name = "per",
  126. },
  127. };
  128. static int __init tpm_clocksource_init(void)
  129. {
  130. #if defined(CONFIG_ARM)
  131. tpm_delay_timer.read_current_timer = &tpm_read_current_timer;
  132. tpm_delay_timer.freq = timer_of_rate(&to_tpm) >> 3;
  133. register_current_timer_delay(&tpm_delay_timer);
  134. sched_clock_register(tpm_read_sched_clock, counter_width,
  135. timer_of_rate(&to_tpm) >> 3);
  136. #endif
  137. return clocksource_mmio_init(timer_base + TPM_CNT,
  138. "imx-tpm",
  139. timer_of_rate(&to_tpm) >> 3,
  140. to_tpm.clkevt.rating,
  141. counter_width,
  142. clocksource_mmio_readl_up);
  143. }
  144. static void __init tpm_clockevent_init(void)
  145. {
  146. clockevents_config_and_register(&to_tpm.clkevt,
  147. timer_of_rate(&to_tpm) >> 3,
  148. 300,
  149. GENMASK(counter_width - 1,
  150. 1));
  151. }
  152. static int __init tpm_timer_init(struct device_node *np)
  153. {
  154. struct clk *ipg;
  155. int ret;
  156. ipg = of_clk_get_by_name(np, "ipg");
  157. if (IS_ERR(ipg)) {
  158. pr_err("tpm: failed to get ipg clk\n");
  159. return -ENODEV;
  160. }
  161. /* enable clk before accessing registers */
  162. ret = clk_prepare_enable(ipg);
  163. if (ret) {
  164. pr_err("tpm: ipg clock enable failed (%d)\n", ret);
  165. clk_put(ipg);
  166. return ret;
  167. }
  168. ret = timer_of_init(np, &to_tpm);
  169. if (ret)
  170. return ret;
  171. timer_base = timer_of_base(&to_tpm);
  172. counter_width = (readl(timer_base + TPM_PARAM)
  173. & TPM_PARAM_WIDTH_MASK) >> TPM_PARAM_WIDTH_SHIFT;
  174. /* use rating 200 for 32-bit counter and 150 for 16-bit counter */
  175. to_tpm.clkevt.rating = counter_width == 0x20 ? 200 : 150;
  176. /*
  177. * Initialize tpm module to a known state
  178. * 1) Counter disabled
  179. * 2) TPM counter operates in up counting mode
  180. * 3) Timer Overflow Interrupt disabled
  181. * 4) Channel0 disabled
  182. * 5) DMA transfers disabled
  183. */
  184. /* make sure counter is disabled */
  185. writel(0, timer_base + TPM_SC);
  186. /* TOF is W1C */
  187. writel(TPM_SC_TOF_MASK, timer_base + TPM_SC);
  188. writel(0, timer_base + TPM_CNT);
  189. /* CHF is W1C */
  190. writel(TPM_C0SC_CHF_MASK, timer_base + TPM_C0SC);
  191. /*
  192. * increase per cnt,
  193. * div 8 for 32-bit counter and div 128 for 16-bit counter
  194. */
  195. writel(TPM_SC_CMOD_INC_PER_CNT |
  196. (counter_width == 0x20 ?
  197. TPM_SC_CMOD_DIV_DEFAULT : TPM_SC_CMOD_DIV_MAX),
  198. timer_base + TPM_SC);
  199. /* set MOD register to maximum for free running mode */
  200. writel(GENMASK(counter_width - 1, 0), timer_base + TPM_MOD);
  201. tpm_clockevent_init();
  202. return tpm_clocksource_init();
  203. }
  204. TIMER_OF_DECLARE(imx7ulp, "fsl,imx7ulp-tpm", tpm_timer_init);