renesas-ostm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Renesas Timer Support - OSTM
  4. *
  5. * Copyright (C) 2017 Renesas Electronics America, Inc.
  6. * Copyright (C) 2017 Chris Brandt
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clockchips.h>
  10. #include <linux/interrupt.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reset.h>
  13. #include <linux/sched_clock.h>
  14. #include <linux/slab.h>
  15. #include "timer-of.h"
  16. /*
  17. * The OSTM contains independent channels.
  18. * The first OSTM channel probed will be set up as a free running
  19. * clocksource. Additionally we will use this clocksource for the system
  20. * schedule timer sched_clock().
  21. *
  22. * The second (or more) channel probed will be set up as an interrupt
  23. * driven clock event.
  24. */
  25. static void __iomem *system_clock; /* For sched_clock() */
  26. /* OSTM REGISTERS */
  27. #define OSTM_CMP 0x000 /* RW,32 */
  28. #define OSTM_CNT 0x004 /* R,32 */
  29. #define OSTM_TE 0x010 /* R,8 */
  30. #define OSTM_TS 0x014 /* W,8 */
  31. #define OSTM_TT 0x018 /* W,8 */
  32. #define OSTM_CTL 0x020 /* RW,8 */
  33. #define TE 0x01
  34. #define TS 0x01
  35. #define TT 0x01
  36. #define CTL_PERIODIC 0x00
  37. #define CTL_ONESHOT 0x02
  38. #define CTL_FREERUN 0x02
  39. static void ostm_timer_stop(struct timer_of *to)
  40. {
  41. if (readb(timer_of_base(to) + OSTM_TE) & TE) {
  42. writeb(TT, timer_of_base(to) + OSTM_TT);
  43. /*
  44. * Read back the register simply to confirm the write operation
  45. * has completed since I/O writes can sometimes get queued by
  46. * the bus architecture.
  47. */
  48. while (readb(timer_of_base(to) + OSTM_TE) & TE)
  49. ;
  50. }
  51. }
  52. static int __init ostm_init_clksrc(struct timer_of *to)
  53. {
  54. ostm_timer_stop(to);
  55. writel(0, timer_of_base(to) + OSTM_CMP);
  56. writeb(CTL_FREERUN, timer_of_base(to) + OSTM_CTL);
  57. writeb(TS, timer_of_base(to) + OSTM_TS);
  58. return clocksource_mmio_init(timer_of_base(to) + OSTM_CNT,
  59. to->np->full_name, timer_of_rate(to), 300,
  60. 32, clocksource_mmio_readl_up);
  61. }
  62. static u64 notrace ostm_read_sched_clock(void)
  63. {
  64. return readl(system_clock);
  65. }
  66. static void __init ostm_init_sched_clock(struct timer_of *to)
  67. {
  68. system_clock = timer_of_base(to) + OSTM_CNT;
  69. sched_clock_register(ostm_read_sched_clock, 32, timer_of_rate(to));
  70. }
  71. static int ostm_clock_event_next(unsigned long delta,
  72. struct clock_event_device *ced)
  73. {
  74. struct timer_of *to = to_timer_of(ced);
  75. ostm_timer_stop(to);
  76. writel(delta, timer_of_base(to) + OSTM_CMP);
  77. writeb(CTL_ONESHOT, timer_of_base(to) + OSTM_CTL);
  78. writeb(TS, timer_of_base(to) + OSTM_TS);
  79. return 0;
  80. }
  81. static int ostm_shutdown(struct clock_event_device *ced)
  82. {
  83. struct timer_of *to = to_timer_of(ced);
  84. ostm_timer_stop(to);
  85. return 0;
  86. }
  87. static int ostm_set_periodic(struct clock_event_device *ced)
  88. {
  89. struct timer_of *to = to_timer_of(ced);
  90. if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
  91. ostm_timer_stop(to);
  92. writel(timer_of_period(to) - 1, timer_of_base(to) + OSTM_CMP);
  93. writeb(CTL_PERIODIC, timer_of_base(to) + OSTM_CTL);
  94. writeb(TS, timer_of_base(to) + OSTM_TS);
  95. return 0;
  96. }
  97. static int ostm_set_oneshot(struct clock_event_device *ced)
  98. {
  99. struct timer_of *to = to_timer_of(ced);
  100. ostm_timer_stop(to);
  101. return 0;
  102. }
  103. static irqreturn_t ostm_timer_interrupt(int irq, void *dev_id)
  104. {
  105. struct clock_event_device *ced = dev_id;
  106. if (clockevent_state_oneshot(ced))
  107. ostm_timer_stop(to_timer_of(ced));
  108. /* notify clockevent layer */
  109. if (ced->event_handler)
  110. ced->event_handler(ced);
  111. return IRQ_HANDLED;
  112. }
  113. static int __init ostm_init_clkevt(struct timer_of *to)
  114. {
  115. struct clock_event_device *ced = &to->clkevt;
  116. ced->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC;
  117. ced->set_state_shutdown = ostm_shutdown;
  118. ced->set_state_periodic = ostm_set_periodic;
  119. ced->set_state_oneshot = ostm_set_oneshot;
  120. ced->set_next_event = ostm_clock_event_next;
  121. ced->shift = 32;
  122. ced->rating = 300;
  123. ced->cpumask = cpumask_of(0);
  124. clockevents_config_and_register(ced, timer_of_rate(to), 0xf,
  125. 0xffffffff);
  126. return 0;
  127. }
  128. static int __init ostm_init(struct device_node *np)
  129. {
  130. struct reset_control *rstc;
  131. struct timer_of *to;
  132. int ret;
  133. to = kzalloc(sizeof(*to), GFP_KERNEL);
  134. if (!to)
  135. return -ENOMEM;
  136. rstc = of_reset_control_get_optional_exclusive(np, NULL);
  137. if (IS_ERR(rstc)) {
  138. ret = PTR_ERR(rstc);
  139. goto err_free;
  140. }
  141. reset_control_deassert(rstc);
  142. to->flags = TIMER_OF_BASE | TIMER_OF_CLOCK;
  143. if (system_clock) {
  144. /*
  145. * clock sources don't use interrupts, clock events do
  146. */
  147. to->flags |= TIMER_OF_IRQ;
  148. to->of_irq.flags = IRQF_TIMER | IRQF_IRQPOLL;
  149. to->of_irq.handler = ostm_timer_interrupt;
  150. }
  151. ret = timer_of_init(np, to);
  152. if (ret)
  153. goto err_reset;
  154. /*
  155. * First probed device will be used as system clocksource. Any
  156. * additional devices will be used as clock events.
  157. */
  158. if (!system_clock) {
  159. ret = ostm_init_clksrc(to);
  160. if (ret)
  161. goto err_cleanup;
  162. ostm_init_sched_clock(to);
  163. pr_info("%pOF: used for clocksource\n", np);
  164. } else {
  165. ret = ostm_init_clkevt(to);
  166. if (ret)
  167. goto err_cleanup;
  168. pr_info("%pOF: used for clock events\n", np);
  169. }
  170. return 0;
  171. err_cleanup:
  172. timer_of_cleanup(to);
  173. err_reset:
  174. reset_control_assert(rstc);
  175. reset_control_put(rstc);
  176. err_free:
  177. kfree(to);
  178. return ret;
  179. }
  180. TIMER_OF_DECLARE(ostm, "renesas,ostm", ostm_init);
  181. #ifdef CONFIG_ARCH_RZG2L
  182. static int __init ostm_probe(struct platform_device *pdev)
  183. {
  184. struct device *dev = &pdev->dev;
  185. return ostm_init(dev->of_node);
  186. }
  187. static const struct of_device_id ostm_of_table[] = {
  188. { .compatible = "renesas,ostm", },
  189. { /* sentinel */ }
  190. };
  191. static struct platform_driver ostm_device_driver = {
  192. .driver = {
  193. .name = "renesas_ostm",
  194. .of_match_table = of_match_ptr(ostm_of_table),
  195. .suppress_bind_attrs = true,
  196. },
  197. };
  198. builtin_platform_driver_probe(ostm_device_driver, ostm_probe);
  199. #endif