timer-imx-tpm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * Copyright 2016 Freescale Semiconductor, Inc.
  3. * Copyright 2017 NXP
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. */
  10. #include <linux/clk.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/clocksource.h>
  13. #include <linux/delay.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #include <linux/sched_clock.h>
  18. #define TPM_SC 0x10
  19. #define TPM_SC_CMOD_INC_PER_CNT (0x1 << 3)
  20. #define TPM_SC_CMOD_DIV_DEFAULT 0x3
  21. #define TPM_CNT 0x14
  22. #define TPM_MOD 0x18
  23. #define TPM_STATUS 0x1c
  24. #define TPM_STATUS_CH0F BIT(0)
  25. #define TPM_C0SC 0x20
  26. #define TPM_C0SC_CHIE BIT(6)
  27. #define TPM_C0SC_MODE_SHIFT 2
  28. #define TPM_C0SC_MODE_MASK 0x3c
  29. #define TPM_C0SC_MODE_SW_COMPARE 0x4
  30. #define TPM_C0V 0x24
  31. static void __iomem *timer_base;
  32. static struct clock_event_device clockevent_tpm;
  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 struct delay_timer tpm_delay_timer;
  55. static inline unsigned long tpm_read_counter(void)
  56. {
  57. return readl(timer_base + TPM_CNT);
  58. }
  59. static unsigned long tpm_read_current_timer(void)
  60. {
  61. return tpm_read_counter();
  62. }
  63. static u64 notrace tpm_read_sched_clock(void)
  64. {
  65. return tpm_read_counter();
  66. }
  67. static int __init tpm_clocksource_init(unsigned long rate)
  68. {
  69. tpm_delay_timer.read_current_timer = &tpm_read_current_timer;
  70. tpm_delay_timer.freq = rate;
  71. register_current_timer_delay(&tpm_delay_timer);
  72. sched_clock_register(tpm_read_sched_clock, 32, rate);
  73. return clocksource_mmio_init(timer_base + TPM_CNT, "imx-tpm",
  74. rate, 200, 32, clocksource_mmio_readl_up);
  75. }
  76. static int tpm_set_next_event(unsigned long delta,
  77. struct clock_event_device *evt)
  78. {
  79. unsigned long next, now;
  80. next = tpm_read_counter();
  81. next += delta;
  82. writel(next, timer_base + TPM_C0V);
  83. now = tpm_read_counter();
  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 (int)((next - now) <= 0) ? -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 clock_event_device clockevent_tpm = {
  110. .name = "i.MX7ULP TPM Timer",
  111. .features = CLOCK_EVT_FEAT_ONESHOT,
  112. .set_state_oneshot = tpm_set_state_oneshot,
  113. .set_next_event = tpm_set_next_event,
  114. .set_state_shutdown = tpm_set_state_shutdown,
  115. .rating = 200,
  116. };
  117. static int __init tpm_clockevent_init(unsigned long rate, int irq)
  118. {
  119. int ret;
  120. ret = request_irq(irq, tpm_timer_interrupt, IRQF_TIMER | IRQF_IRQPOLL,
  121. "i.MX7ULP TPM Timer", &clockevent_tpm);
  122. clockevent_tpm.cpumask = cpumask_of(0);
  123. clockevent_tpm.irq = irq;
  124. clockevents_config_and_register(&clockevent_tpm,
  125. rate, 300, 0xfffffffe);
  126. return ret;
  127. }
  128. static int __init tpm_timer_init(struct device_node *np)
  129. {
  130. struct clk *ipg, *per;
  131. int irq, ret;
  132. u32 rate;
  133. timer_base = of_iomap(np, 0);
  134. if (!timer_base) {
  135. pr_err("tpm: failed to get base address\n");
  136. return -ENXIO;
  137. }
  138. irq = irq_of_parse_and_map(np, 0);
  139. if (!irq) {
  140. pr_err("tpm: failed to get irq\n");
  141. ret = -ENOENT;
  142. goto err_iomap;
  143. }
  144. ipg = of_clk_get_by_name(np, "ipg");
  145. per = of_clk_get_by_name(np, "per");
  146. if (IS_ERR(ipg) || IS_ERR(per)) {
  147. pr_err("tpm: failed to get igp or per clk\n");
  148. ret = -ENODEV;
  149. goto err_clk_get;
  150. }
  151. /* enable clk before accessing registers */
  152. ret = clk_prepare_enable(ipg);
  153. if (ret) {
  154. pr_err("tpm: ipg clock enable failed (%d)\n", ret);
  155. goto err_clk_get;
  156. }
  157. ret = clk_prepare_enable(per);
  158. if (ret) {
  159. pr_err("tpm: per clock enable failed (%d)\n", ret);
  160. goto err_per_clk_enable;
  161. }
  162. /*
  163. * Initialize tpm module to a known state
  164. * 1) Counter disabled
  165. * 2) TPM counter operates in up counting mode
  166. * 3) Timer Overflow Interrupt disabled
  167. * 4) Channel0 disabled
  168. * 5) DMA transfers disabled
  169. */
  170. writel(0, timer_base + TPM_SC);
  171. writel(0, timer_base + TPM_CNT);
  172. writel(0, timer_base + TPM_C0SC);
  173. /* increase per cnt, div 8 by default */
  174. writel(TPM_SC_CMOD_INC_PER_CNT | TPM_SC_CMOD_DIV_DEFAULT,
  175. timer_base + TPM_SC);
  176. /* set MOD register to maximum for free running mode */
  177. writel(0xffffffff, timer_base + TPM_MOD);
  178. rate = clk_get_rate(per) >> 3;
  179. ret = tpm_clocksource_init(rate);
  180. if (ret)
  181. goto err_per_clk_enable;
  182. ret = tpm_clockevent_init(rate, irq);
  183. if (ret)
  184. goto err_per_clk_enable;
  185. return 0;
  186. err_per_clk_enable:
  187. clk_disable_unprepare(ipg);
  188. err_clk_get:
  189. clk_put(per);
  190. clk_put(ipg);
  191. err_iomap:
  192. iounmap(timer_base);
  193. return ret;
  194. }
  195. TIMER_OF_DECLARE(imx7ulp, "fsl,imx7ulp-tpm", tpm_timer_init);