rockchip_timer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Rockchip timer support
  3. *
  4. * Copyright (C) Daniel Lezcano <daniel.lezcano@linaro.org>
  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. #include <linux/clk.h>
  11. #include <linux/clockchips.h>
  12. #include <linux/init.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/sched_clock.h>
  15. #include <linux/slab.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #define TIMER_NAME "rk_timer"
  20. #define TIMER_LOAD_COUNT0 0x00
  21. #define TIMER_LOAD_COUNT1 0x04
  22. #define TIMER_CURRENT_VALUE0 0x08
  23. #define TIMER_CURRENT_VALUE1 0x0C
  24. #define TIMER_CONTROL_REG3288 0x10
  25. #define TIMER_CONTROL_REG3399 0x1c
  26. #define TIMER_INT_STATUS 0x18
  27. #define TIMER_DISABLE 0x0
  28. #define TIMER_ENABLE 0x1
  29. #define TIMER_MODE_FREE_RUNNING (0 << 1)
  30. #define TIMER_MODE_USER_DEFINED_COUNT (1 << 1)
  31. #define TIMER_INT_UNMASK (1 << 2)
  32. struct rk_timer {
  33. void __iomem *base;
  34. void __iomem *ctrl;
  35. struct clk *clk;
  36. struct clk *pclk;
  37. u32 freq;
  38. int irq;
  39. };
  40. struct rk_clkevt {
  41. struct clock_event_device ce;
  42. struct rk_timer timer;
  43. };
  44. static struct rk_clkevt *rk_clkevt;
  45. static struct rk_timer *rk_clksrc;
  46. static inline struct rk_timer *rk_timer(struct clock_event_device *ce)
  47. {
  48. return &container_of(ce, struct rk_clkevt, ce)->timer;
  49. }
  50. static inline void rk_timer_disable(struct rk_timer *timer)
  51. {
  52. writel_relaxed(TIMER_DISABLE, timer->ctrl);
  53. }
  54. static inline void rk_timer_enable(struct rk_timer *timer, u32 flags)
  55. {
  56. writel_relaxed(TIMER_ENABLE | flags, timer->ctrl);
  57. }
  58. static void rk_timer_update_counter(unsigned long cycles,
  59. struct rk_timer *timer)
  60. {
  61. writel_relaxed(cycles, timer->base + TIMER_LOAD_COUNT0);
  62. writel_relaxed(0, timer->base + TIMER_LOAD_COUNT1);
  63. }
  64. static void rk_timer_interrupt_clear(struct rk_timer *timer)
  65. {
  66. writel_relaxed(1, timer->base + TIMER_INT_STATUS);
  67. }
  68. static inline int rk_timer_set_next_event(unsigned long cycles,
  69. struct clock_event_device *ce)
  70. {
  71. struct rk_timer *timer = rk_timer(ce);
  72. rk_timer_disable(timer);
  73. rk_timer_update_counter(cycles, timer);
  74. rk_timer_enable(timer, TIMER_MODE_USER_DEFINED_COUNT |
  75. TIMER_INT_UNMASK);
  76. return 0;
  77. }
  78. static int rk_timer_shutdown(struct clock_event_device *ce)
  79. {
  80. struct rk_timer *timer = rk_timer(ce);
  81. rk_timer_disable(timer);
  82. return 0;
  83. }
  84. static int rk_timer_set_periodic(struct clock_event_device *ce)
  85. {
  86. struct rk_timer *timer = rk_timer(ce);
  87. rk_timer_disable(timer);
  88. rk_timer_update_counter(timer->freq / HZ - 1, timer);
  89. rk_timer_enable(timer, TIMER_MODE_FREE_RUNNING | TIMER_INT_UNMASK);
  90. return 0;
  91. }
  92. static irqreturn_t rk_timer_interrupt(int irq, void *dev_id)
  93. {
  94. struct clock_event_device *ce = dev_id;
  95. struct rk_timer *timer = rk_timer(ce);
  96. rk_timer_interrupt_clear(timer);
  97. if (clockevent_state_oneshot(ce))
  98. rk_timer_disable(timer);
  99. ce->event_handler(ce);
  100. return IRQ_HANDLED;
  101. }
  102. static u64 notrace rk_timer_sched_read(void)
  103. {
  104. return ~readl_relaxed(rk_clksrc->base + TIMER_CURRENT_VALUE0);
  105. }
  106. static int __init
  107. rk_timer_probe(struct rk_timer *timer, struct device_node *np)
  108. {
  109. struct clk *timer_clk;
  110. struct clk *pclk;
  111. int ret = -EINVAL, irq;
  112. u32 ctrl_reg = TIMER_CONTROL_REG3288;
  113. timer->base = of_iomap(np, 0);
  114. if (!timer->base) {
  115. pr_err("Failed to get base address for '%s'\n", TIMER_NAME);
  116. return -ENXIO;
  117. }
  118. if (of_device_is_compatible(np, "rockchip,rk3399-timer"))
  119. ctrl_reg = TIMER_CONTROL_REG3399;
  120. timer->ctrl = timer->base + ctrl_reg;
  121. pclk = of_clk_get_by_name(np, "pclk");
  122. if (IS_ERR(pclk)) {
  123. ret = PTR_ERR(pclk);
  124. pr_err("Failed to get pclk for '%s'\n", TIMER_NAME);
  125. goto out_unmap;
  126. }
  127. ret = clk_prepare_enable(pclk);
  128. if (ret) {
  129. pr_err("Failed to enable pclk for '%s'\n", TIMER_NAME);
  130. goto out_unmap;
  131. }
  132. timer->pclk = pclk;
  133. timer_clk = of_clk_get_by_name(np, "timer");
  134. if (IS_ERR(timer_clk)) {
  135. ret = PTR_ERR(timer_clk);
  136. pr_err("Failed to get timer clock for '%s'\n", TIMER_NAME);
  137. goto out_timer_clk;
  138. }
  139. ret = clk_prepare_enable(timer_clk);
  140. if (ret) {
  141. pr_err("Failed to enable timer clock\n");
  142. goto out_timer_clk;
  143. }
  144. timer->clk = timer_clk;
  145. timer->freq = clk_get_rate(timer_clk);
  146. irq = irq_of_parse_and_map(np, 0);
  147. if (!irq) {
  148. ret = -EINVAL;
  149. pr_err("Failed to map interrupts for '%s'\n", TIMER_NAME);
  150. goto out_irq;
  151. }
  152. timer->irq = irq;
  153. rk_timer_interrupt_clear(timer);
  154. rk_timer_disable(timer);
  155. return 0;
  156. out_irq:
  157. clk_disable_unprepare(timer_clk);
  158. out_timer_clk:
  159. clk_disable_unprepare(pclk);
  160. out_unmap:
  161. iounmap(timer->base);
  162. return ret;
  163. }
  164. static void __init rk_timer_cleanup(struct rk_timer *timer)
  165. {
  166. clk_disable_unprepare(timer->clk);
  167. clk_disable_unprepare(timer->pclk);
  168. iounmap(timer->base);
  169. }
  170. static int __init rk_clkevt_init(struct device_node *np)
  171. {
  172. struct clock_event_device *ce;
  173. int ret = -EINVAL;
  174. rk_clkevt = kzalloc(sizeof(struct rk_clkevt), GFP_KERNEL);
  175. if (!rk_clkevt) {
  176. ret = -ENOMEM;
  177. goto out;
  178. }
  179. ret = rk_timer_probe(&rk_clkevt->timer, np);
  180. if (ret)
  181. goto out_probe;
  182. ce = &rk_clkevt->ce;
  183. ce->name = TIMER_NAME;
  184. ce->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  185. CLOCK_EVT_FEAT_DYNIRQ;
  186. ce->set_next_event = rk_timer_set_next_event;
  187. ce->set_state_shutdown = rk_timer_shutdown;
  188. ce->set_state_periodic = rk_timer_set_periodic;
  189. ce->irq = rk_clkevt->timer.irq;
  190. ce->cpumask = cpu_possible_mask;
  191. ce->rating = 250;
  192. ret = request_irq(rk_clkevt->timer.irq, rk_timer_interrupt, IRQF_TIMER,
  193. TIMER_NAME, ce);
  194. if (ret) {
  195. pr_err("Failed to initialize '%s': %d\n",
  196. TIMER_NAME, ret);
  197. goto out_irq;
  198. }
  199. clockevents_config_and_register(&rk_clkevt->ce,
  200. rk_clkevt->timer.freq, 1, UINT_MAX);
  201. return 0;
  202. out_irq:
  203. rk_timer_cleanup(&rk_clkevt->timer);
  204. out_probe:
  205. kfree(rk_clkevt);
  206. out:
  207. /* Leave rk_clkevt not NULL to prevent future init */
  208. rk_clkevt = ERR_PTR(ret);
  209. return ret;
  210. }
  211. static int __init rk_clksrc_init(struct device_node *np)
  212. {
  213. int ret = -EINVAL;
  214. rk_clksrc = kzalloc(sizeof(struct rk_timer), GFP_KERNEL);
  215. if (!rk_clksrc) {
  216. ret = -ENOMEM;
  217. goto out;
  218. }
  219. ret = rk_timer_probe(rk_clksrc, np);
  220. if (ret)
  221. goto out_probe;
  222. rk_timer_update_counter(UINT_MAX, rk_clksrc);
  223. rk_timer_enable(rk_clksrc, 0);
  224. ret = clocksource_mmio_init(rk_clksrc->base + TIMER_CURRENT_VALUE0,
  225. TIMER_NAME, rk_clksrc->freq, 250, 32,
  226. clocksource_mmio_readl_down);
  227. if (ret) {
  228. pr_err("Failed to register clocksource\n");
  229. goto out_clocksource;
  230. }
  231. sched_clock_register(rk_timer_sched_read, 32, rk_clksrc->freq);
  232. return 0;
  233. out_clocksource:
  234. rk_timer_cleanup(rk_clksrc);
  235. out_probe:
  236. kfree(rk_clksrc);
  237. out:
  238. /* Leave rk_clksrc not NULL to prevent future init */
  239. rk_clksrc = ERR_PTR(ret);
  240. return ret;
  241. }
  242. static int __init rk_timer_init(struct device_node *np)
  243. {
  244. if (!rk_clkevt)
  245. return rk_clkevt_init(np);
  246. if (!rk_clksrc)
  247. return rk_clksrc_init(np);
  248. pr_err("Too many timer definitions for '%s'\n", TIMER_NAME);
  249. return -EINVAL;
  250. }
  251. TIMER_OF_DECLARE(rk3288_timer, "rockchip,rk3288-timer", rk_timer_init);
  252. TIMER_OF_DECLARE(rk3399_timer, "rockchip,rk3399-timer", rk_timer_init);