renesas-ostm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/of_address.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/clk.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/sched_clock.h>
  14. #include <linux/slab.h>
  15. /*
  16. * The OSTM contains independent channels.
  17. * The first OSTM channel probed will be set up as a free running
  18. * clocksource. Additionally we will use this clocksource for the system
  19. * schedule timer sched_clock().
  20. *
  21. * The second (or more) channel probed will be set up as an interrupt
  22. * driven clock event.
  23. */
  24. struct ostm_device {
  25. void __iomem *base;
  26. unsigned long ticks_per_jiffy;
  27. struct clock_event_device ced;
  28. };
  29. static void __iomem *system_clock; /* For sched_clock() */
  30. /* OSTM REGISTERS */
  31. #define OSTM_CMP 0x000 /* RW,32 */
  32. #define OSTM_CNT 0x004 /* R,32 */
  33. #define OSTM_TE 0x010 /* R,8 */
  34. #define OSTM_TS 0x014 /* W,8 */
  35. #define OSTM_TT 0x018 /* W,8 */
  36. #define OSTM_CTL 0x020 /* RW,8 */
  37. #define TE 0x01
  38. #define TS 0x01
  39. #define TT 0x01
  40. #define CTL_PERIODIC 0x00
  41. #define CTL_ONESHOT 0x02
  42. #define CTL_FREERUN 0x02
  43. static struct ostm_device *ced_to_ostm(struct clock_event_device *ced)
  44. {
  45. return container_of(ced, struct ostm_device, ced);
  46. }
  47. static void ostm_timer_stop(struct ostm_device *ostm)
  48. {
  49. if (readb(ostm->base + OSTM_TE) & TE) {
  50. writeb(TT, ostm->base + OSTM_TT);
  51. /*
  52. * Read back the register simply to confirm the write operation
  53. * has completed since I/O writes can sometimes get queued by
  54. * the bus architecture.
  55. */
  56. while (readb(ostm->base + OSTM_TE) & TE)
  57. ;
  58. }
  59. }
  60. static int __init ostm_init_clksrc(struct ostm_device *ostm, unsigned long rate)
  61. {
  62. /*
  63. * irq not used (clock sources don't use interrupts)
  64. */
  65. ostm_timer_stop(ostm);
  66. writel(0, ostm->base + OSTM_CMP);
  67. writeb(CTL_FREERUN, ostm->base + OSTM_CTL);
  68. writeb(TS, ostm->base + OSTM_TS);
  69. return clocksource_mmio_init(ostm->base + OSTM_CNT,
  70. "ostm", rate,
  71. 300, 32, clocksource_mmio_readl_up);
  72. }
  73. static u64 notrace ostm_read_sched_clock(void)
  74. {
  75. return readl(system_clock);
  76. }
  77. static void __init ostm_init_sched_clock(struct ostm_device *ostm,
  78. unsigned long rate)
  79. {
  80. system_clock = ostm->base + OSTM_CNT;
  81. sched_clock_register(ostm_read_sched_clock, 32, rate);
  82. }
  83. static int ostm_clock_event_next(unsigned long delta,
  84. struct clock_event_device *ced)
  85. {
  86. struct ostm_device *ostm = ced_to_ostm(ced);
  87. ostm_timer_stop(ostm);
  88. writel(delta, ostm->base + OSTM_CMP);
  89. writeb(CTL_ONESHOT, ostm->base + OSTM_CTL);
  90. writeb(TS, ostm->base + OSTM_TS);
  91. return 0;
  92. }
  93. static int ostm_shutdown(struct clock_event_device *ced)
  94. {
  95. struct ostm_device *ostm = ced_to_ostm(ced);
  96. ostm_timer_stop(ostm);
  97. return 0;
  98. }
  99. static int ostm_set_periodic(struct clock_event_device *ced)
  100. {
  101. struct ostm_device *ostm = ced_to_ostm(ced);
  102. if (clockevent_state_oneshot(ced) || clockevent_state_periodic(ced))
  103. ostm_timer_stop(ostm);
  104. writel(ostm->ticks_per_jiffy - 1, ostm->base + OSTM_CMP);
  105. writeb(CTL_PERIODIC, ostm->base + OSTM_CTL);
  106. writeb(TS, ostm->base + OSTM_TS);
  107. return 0;
  108. }
  109. static int ostm_set_oneshot(struct clock_event_device *ced)
  110. {
  111. struct ostm_device *ostm = ced_to_ostm(ced);
  112. ostm_timer_stop(ostm);
  113. return 0;
  114. }
  115. static irqreturn_t ostm_timer_interrupt(int irq, void *dev_id)
  116. {
  117. struct ostm_device *ostm = dev_id;
  118. if (clockevent_state_oneshot(&ostm->ced))
  119. ostm_timer_stop(ostm);
  120. /* notify clockevent layer */
  121. if (ostm->ced.event_handler)
  122. ostm->ced.event_handler(&ostm->ced);
  123. return IRQ_HANDLED;
  124. }
  125. static int __init ostm_init_clkevt(struct ostm_device *ostm, int irq,
  126. unsigned long rate)
  127. {
  128. struct clock_event_device *ced = &ostm->ced;
  129. int ret = -ENXIO;
  130. ret = request_irq(irq, ostm_timer_interrupt,
  131. IRQF_TIMER | IRQF_IRQPOLL,
  132. "ostm", ostm);
  133. if (ret) {
  134. pr_err("ostm: failed to request irq\n");
  135. return ret;
  136. }
  137. ced->name = "ostm";
  138. ced->features = CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_PERIODIC;
  139. ced->set_state_shutdown = ostm_shutdown;
  140. ced->set_state_periodic = ostm_set_periodic;
  141. ced->set_state_oneshot = ostm_set_oneshot;
  142. ced->set_next_event = ostm_clock_event_next;
  143. ced->shift = 32;
  144. ced->rating = 300;
  145. ced->cpumask = cpumask_of(0);
  146. clockevents_config_and_register(ced, rate, 0xf, 0xffffffff);
  147. return 0;
  148. }
  149. static int __init ostm_init(struct device_node *np)
  150. {
  151. struct ostm_device *ostm;
  152. int ret = -EFAULT;
  153. struct clk *ostm_clk = NULL;
  154. int irq;
  155. unsigned long rate;
  156. ostm = kzalloc(sizeof(*ostm), GFP_KERNEL);
  157. if (!ostm)
  158. return -ENOMEM;
  159. ostm->base = of_iomap(np, 0);
  160. if (!ostm->base) {
  161. pr_err("ostm: failed to remap I/O memory\n");
  162. goto err;
  163. }
  164. irq = irq_of_parse_and_map(np, 0);
  165. if (irq < 0) {
  166. pr_err("ostm: Failed to get irq\n");
  167. goto err;
  168. }
  169. ostm_clk = of_clk_get(np, 0);
  170. if (IS_ERR(ostm_clk)) {
  171. pr_err("ostm: Failed to get clock\n");
  172. ostm_clk = NULL;
  173. goto err;
  174. }
  175. ret = clk_prepare_enable(ostm_clk);
  176. if (ret) {
  177. pr_err("ostm: Failed to enable clock\n");
  178. goto err;
  179. }
  180. rate = clk_get_rate(ostm_clk);
  181. ostm->ticks_per_jiffy = (rate + HZ / 2) / HZ;
  182. /*
  183. * First probed device will be used as system clocksource. Any
  184. * additional devices will be used as clock events.
  185. */
  186. if (!system_clock) {
  187. ret = ostm_init_clksrc(ostm, rate);
  188. if (!ret) {
  189. ostm_init_sched_clock(ostm, rate);
  190. pr_info("ostm: used for clocksource\n");
  191. }
  192. } else {
  193. ret = ostm_init_clkevt(ostm, irq, rate);
  194. if (!ret)
  195. pr_info("ostm: used for clock events\n");
  196. }
  197. err:
  198. if (ret) {
  199. clk_disable_unprepare(ostm_clk);
  200. iounmap(ostm->base);
  201. kfree(ostm);
  202. return ret;
  203. }
  204. return 0;
  205. }
  206. TIMER_OF_DECLARE(ostm, "renesas,ostm", ostm_init);