mps2-timer.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2015 ARM Limited
  3. *
  4. * Author: Vladimir Murzin <vladimir.murzin@arm.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  12. #include <linux/clk.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/clocksource.h>
  15. #include <linux/err.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/irq.h>
  19. #include <linux/of_address.h>
  20. #include <linux/of.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/sched_clock.h>
  23. #include <linux/slab.h>
  24. #define TIMER_CTRL 0x0
  25. #define TIMER_CTRL_ENABLE BIT(0)
  26. #define TIMER_CTRL_IE BIT(3)
  27. #define TIMER_VALUE 0x4
  28. #define TIMER_RELOAD 0x8
  29. #define TIMER_INT 0xc
  30. struct clockevent_mps2 {
  31. void __iomem *reg;
  32. u32 clock_count_per_tick;
  33. struct clock_event_device clkevt;
  34. };
  35. static void __iomem *sched_clock_base;
  36. static u64 notrace mps2_sched_read(void)
  37. {
  38. return ~readl_relaxed(sched_clock_base + TIMER_VALUE);
  39. }
  40. static inline struct clockevent_mps2 *to_mps2_clkevt(struct clock_event_device *c)
  41. {
  42. return container_of(c, struct clockevent_mps2, clkevt);
  43. }
  44. static void clockevent_mps2_writel(u32 val, struct clock_event_device *c, u32 offset)
  45. {
  46. writel_relaxed(val, to_mps2_clkevt(c)->reg + offset);
  47. }
  48. static int mps2_timer_shutdown(struct clock_event_device *ce)
  49. {
  50. clockevent_mps2_writel(0, ce, TIMER_RELOAD);
  51. clockevent_mps2_writel(0, ce, TIMER_CTRL);
  52. return 0;
  53. }
  54. static int mps2_timer_set_next_event(unsigned long next, struct clock_event_device *ce)
  55. {
  56. clockevent_mps2_writel(next, ce, TIMER_VALUE);
  57. clockevent_mps2_writel(TIMER_CTRL_IE | TIMER_CTRL_ENABLE, ce, TIMER_CTRL);
  58. return 0;
  59. }
  60. static int mps2_timer_set_periodic(struct clock_event_device *ce)
  61. {
  62. u32 clock_count_per_tick = to_mps2_clkevt(ce)->clock_count_per_tick;
  63. clockevent_mps2_writel(clock_count_per_tick, ce, TIMER_RELOAD);
  64. clockevent_mps2_writel(clock_count_per_tick, ce, TIMER_VALUE);
  65. clockevent_mps2_writel(TIMER_CTRL_IE | TIMER_CTRL_ENABLE, ce, TIMER_CTRL);
  66. return 0;
  67. }
  68. static irqreturn_t mps2_timer_interrupt(int irq, void *dev_id)
  69. {
  70. struct clockevent_mps2 *ce = dev_id;
  71. u32 status = readl_relaxed(ce->reg + TIMER_INT);
  72. if (!status) {
  73. pr_warn("spurious interrupt\n");
  74. return IRQ_NONE;
  75. }
  76. writel_relaxed(1, ce->reg + TIMER_INT);
  77. ce->clkevt.event_handler(&ce->clkevt);
  78. return IRQ_HANDLED;
  79. }
  80. static int __init mps2_clockevent_init(struct device_node *np)
  81. {
  82. void __iomem *base;
  83. struct clk *clk = NULL;
  84. struct clockevent_mps2 *ce;
  85. u32 rate;
  86. int irq, ret;
  87. const char *name = "mps2-clkevt";
  88. ret = of_property_read_u32(np, "clock-frequency", &rate);
  89. if (ret) {
  90. clk = of_clk_get(np, 0);
  91. if (IS_ERR(clk)) {
  92. ret = PTR_ERR(clk);
  93. pr_err("failed to get clock for clockevent: %d\n", ret);
  94. goto out;
  95. }
  96. ret = clk_prepare_enable(clk);
  97. if (ret) {
  98. pr_err("failed to enable clock for clockevent: %d\n", ret);
  99. goto out_clk_put;
  100. }
  101. rate = clk_get_rate(clk);
  102. }
  103. base = of_iomap(np, 0);
  104. if (!base) {
  105. ret = -EADDRNOTAVAIL;
  106. pr_err("failed to map register for clockevent: %d\n", ret);
  107. goto out_clk_disable;
  108. }
  109. irq = irq_of_parse_and_map(np, 0);
  110. if (!irq) {
  111. ret = -ENOENT;
  112. pr_err("failed to get irq for clockevent: %d\n", ret);
  113. goto out_iounmap;
  114. }
  115. ce = kzalloc(sizeof(*ce), GFP_KERNEL);
  116. if (!ce) {
  117. ret = -ENOMEM;
  118. goto out_iounmap;
  119. }
  120. ce->reg = base;
  121. ce->clock_count_per_tick = DIV_ROUND_CLOSEST(rate, HZ);
  122. ce->clkevt.irq = irq;
  123. ce->clkevt.name = name;
  124. ce->clkevt.rating = 200;
  125. ce->clkevt.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  126. ce->clkevt.cpumask = cpu_possible_mask;
  127. ce->clkevt.set_state_shutdown = mps2_timer_shutdown,
  128. ce->clkevt.set_state_periodic = mps2_timer_set_periodic,
  129. ce->clkevt.set_state_oneshot = mps2_timer_shutdown,
  130. ce->clkevt.set_next_event = mps2_timer_set_next_event;
  131. /* Ensure timer is disabled */
  132. writel_relaxed(0, base + TIMER_CTRL);
  133. ret = request_irq(irq, mps2_timer_interrupt, IRQF_TIMER, name, ce);
  134. if (ret) {
  135. pr_err("failed to request irq for clockevent: %d\n", ret);
  136. goto out_kfree;
  137. }
  138. clockevents_config_and_register(&ce->clkevt, rate, 0xf, 0xffffffff);
  139. return 0;
  140. out_kfree:
  141. kfree(ce);
  142. out_iounmap:
  143. iounmap(base);
  144. out_clk_disable:
  145. /* clk_{disable, unprepare, put}() can handle NULL as a parameter */
  146. clk_disable_unprepare(clk);
  147. out_clk_put:
  148. clk_put(clk);
  149. out:
  150. return ret;
  151. }
  152. static int __init mps2_clocksource_init(struct device_node *np)
  153. {
  154. void __iomem *base;
  155. struct clk *clk = NULL;
  156. u32 rate;
  157. int ret;
  158. const char *name = "mps2-clksrc";
  159. ret = of_property_read_u32(np, "clock-frequency", &rate);
  160. if (ret) {
  161. clk = of_clk_get(np, 0);
  162. if (IS_ERR(clk)) {
  163. ret = PTR_ERR(clk);
  164. pr_err("failed to get clock for clocksource: %d\n", ret);
  165. goto out;
  166. }
  167. ret = clk_prepare_enable(clk);
  168. if (ret) {
  169. pr_err("failed to enable clock for clocksource: %d\n", ret);
  170. goto out_clk_put;
  171. }
  172. rate = clk_get_rate(clk);
  173. }
  174. base = of_iomap(np, 0);
  175. if (!base) {
  176. ret = -EADDRNOTAVAIL;
  177. pr_err("failed to map register for clocksource: %d\n", ret);
  178. goto out_clk_disable;
  179. }
  180. /* Ensure timer is disabled */
  181. writel_relaxed(0, base + TIMER_CTRL);
  182. /* ... and set it up as free-running clocksource */
  183. writel_relaxed(0xffffffff, base + TIMER_VALUE);
  184. writel_relaxed(0xffffffff, base + TIMER_RELOAD);
  185. writel_relaxed(TIMER_CTRL_ENABLE, base + TIMER_CTRL);
  186. ret = clocksource_mmio_init(base + TIMER_VALUE, name,
  187. rate, 200, 32,
  188. clocksource_mmio_readl_down);
  189. if (ret) {
  190. pr_err("failed to init clocksource: %d\n", ret);
  191. goto out_iounmap;
  192. }
  193. sched_clock_base = base;
  194. sched_clock_register(mps2_sched_read, 32, rate);
  195. return 0;
  196. out_iounmap:
  197. iounmap(base);
  198. out_clk_disable:
  199. /* clk_{disable, unprepare, put}() can handle NULL as a parameter */
  200. clk_disable_unprepare(clk);
  201. out_clk_put:
  202. clk_put(clk);
  203. out:
  204. return ret;
  205. }
  206. static int __init mps2_timer_init(struct device_node *np)
  207. {
  208. static int has_clocksource, has_clockevent;
  209. int ret;
  210. if (!has_clocksource) {
  211. ret = mps2_clocksource_init(np);
  212. if (!ret) {
  213. has_clocksource = 1;
  214. return 0;
  215. }
  216. }
  217. if (!has_clockevent) {
  218. ret = mps2_clockevent_init(np);
  219. if (!ret) {
  220. has_clockevent = 1;
  221. return 0;
  222. }
  223. }
  224. return 0;
  225. }
  226. CLOCKSOURCE_OF_DECLARE(mps2_timer, "arm,mps2-timer", mps2_timer_init);