renesas-ostm.c 6.0 KB

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