rockchip_timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_irq.h>
  17. #define TIMER_NAME "rk_timer"
  18. #define TIMER_LOAD_COUNT0 0x00
  19. #define TIMER_LOAD_COUNT1 0x04
  20. #define TIMER_CONTROL_REG3288 0x10
  21. #define TIMER_CONTROL_REG3399 0x1c
  22. #define TIMER_INT_STATUS 0x18
  23. #define TIMER_DISABLE 0x0
  24. #define TIMER_ENABLE 0x1
  25. #define TIMER_MODE_FREE_RUNNING (0 << 1)
  26. #define TIMER_MODE_USER_DEFINED_COUNT (1 << 1)
  27. #define TIMER_INT_UNMASK (1 << 2)
  28. struct bc_timer {
  29. struct clock_event_device ce;
  30. void __iomem *base;
  31. void __iomem *ctrl;
  32. u32 freq;
  33. };
  34. static struct bc_timer bc_timer;
  35. static inline struct bc_timer *rk_timer(struct clock_event_device *ce)
  36. {
  37. return container_of(ce, struct bc_timer, ce);
  38. }
  39. static inline void __iomem *rk_base(struct clock_event_device *ce)
  40. {
  41. return rk_timer(ce)->base;
  42. }
  43. static inline void __iomem *rk_ctrl(struct clock_event_device *ce)
  44. {
  45. return rk_timer(ce)->ctrl;
  46. }
  47. static inline void rk_timer_disable(struct clock_event_device *ce)
  48. {
  49. writel_relaxed(TIMER_DISABLE, rk_ctrl(ce));
  50. }
  51. static inline void rk_timer_enable(struct clock_event_device *ce, u32 flags)
  52. {
  53. writel_relaxed(TIMER_ENABLE | TIMER_INT_UNMASK | flags,
  54. rk_ctrl(ce));
  55. }
  56. static void rk_timer_update_counter(unsigned long cycles,
  57. struct clock_event_device *ce)
  58. {
  59. writel_relaxed(cycles, rk_base(ce) + TIMER_LOAD_COUNT0);
  60. writel_relaxed(0, rk_base(ce) + TIMER_LOAD_COUNT1);
  61. }
  62. static void rk_timer_interrupt_clear(struct clock_event_device *ce)
  63. {
  64. writel_relaxed(1, rk_base(ce) + TIMER_INT_STATUS);
  65. }
  66. static inline int rk_timer_set_next_event(unsigned long cycles,
  67. struct clock_event_device *ce)
  68. {
  69. rk_timer_disable(ce);
  70. rk_timer_update_counter(cycles, ce);
  71. rk_timer_enable(ce, TIMER_MODE_USER_DEFINED_COUNT);
  72. return 0;
  73. }
  74. static int rk_timer_shutdown(struct clock_event_device *ce)
  75. {
  76. rk_timer_disable(ce);
  77. return 0;
  78. }
  79. static int rk_timer_set_periodic(struct clock_event_device *ce)
  80. {
  81. rk_timer_disable(ce);
  82. rk_timer_update_counter(rk_timer(ce)->freq / HZ - 1, ce);
  83. rk_timer_enable(ce, TIMER_MODE_FREE_RUNNING);
  84. return 0;
  85. }
  86. static irqreturn_t rk_timer_interrupt(int irq, void *dev_id)
  87. {
  88. struct clock_event_device *ce = dev_id;
  89. rk_timer_interrupt_clear(ce);
  90. if (clockevent_state_oneshot(ce))
  91. rk_timer_disable(ce);
  92. ce->event_handler(ce);
  93. return IRQ_HANDLED;
  94. }
  95. static int __init rk_timer_init(struct device_node *np, u32 ctrl_reg)
  96. {
  97. struct clock_event_device *ce = &bc_timer.ce;
  98. struct clk *timer_clk;
  99. struct clk *pclk;
  100. int ret = -EINVAL, irq;
  101. bc_timer.base = of_iomap(np, 0);
  102. if (!bc_timer.base) {
  103. pr_err("Failed to get base address for '%s'\n", TIMER_NAME);
  104. return -ENXIO;
  105. }
  106. bc_timer.ctrl = bc_timer.base + ctrl_reg;
  107. pclk = of_clk_get_by_name(np, "pclk");
  108. if (IS_ERR(pclk)) {
  109. ret = PTR_ERR(pclk);
  110. pr_err("Failed to get pclk for '%s'\n", TIMER_NAME);
  111. goto out_unmap;
  112. }
  113. ret = clk_prepare_enable(pclk);
  114. if (ret) {
  115. pr_err("Failed to enable pclk for '%s'\n", TIMER_NAME);
  116. goto out_unmap;
  117. }
  118. timer_clk = of_clk_get_by_name(np, "timer");
  119. if (IS_ERR(timer_clk)) {
  120. ret = PTR_ERR(timer_clk);
  121. pr_err("Failed to get timer clock for '%s'\n", TIMER_NAME);
  122. goto out_timer_clk;
  123. }
  124. ret = clk_prepare_enable(timer_clk);
  125. if (ret) {
  126. pr_err("Failed to enable timer clock\n");
  127. goto out_timer_clk;
  128. }
  129. bc_timer.freq = clk_get_rate(timer_clk);
  130. irq = irq_of_parse_and_map(np, 0);
  131. if (!irq) {
  132. ret = -EINVAL;
  133. pr_err("Failed to map interrupts for '%s'\n", TIMER_NAME);
  134. goto out_irq;
  135. }
  136. ce->name = TIMER_NAME;
  137. ce->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT |
  138. CLOCK_EVT_FEAT_DYNIRQ;
  139. ce->set_next_event = rk_timer_set_next_event;
  140. ce->set_state_shutdown = rk_timer_shutdown;
  141. ce->set_state_periodic = rk_timer_set_periodic;
  142. ce->irq = irq;
  143. ce->cpumask = cpu_possible_mask;
  144. ce->rating = 250;
  145. rk_timer_interrupt_clear(ce);
  146. rk_timer_disable(ce);
  147. ret = request_irq(irq, rk_timer_interrupt, IRQF_TIMER, TIMER_NAME, ce);
  148. if (ret) {
  149. pr_err("Failed to initialize '%s': %d\n", TIMER_NAME, ret);
  150. goto out_irq;
  151. }
  152. clockevents_config_and_register(ce, bc_timer.freq, 1, UINT_MAX);
  153. return 0;
  154. out_irq:
  155. clk_disable_unprepare(timer_clk);
  156. out_timer_clk:
  157. clk_disable_unprepare(pclk);
  158. out_unmap:
  159. iounmap(bc_timer.base);
  160. return ret;
  161. }
  162. static int __init rk3288_timer_init(struct device_node *np)
  163. {
  164. return rk_timer_init(np, TIMER_CONTROL_REG3288);
  165. }
  166. static int __init rk3399_timer_init(struct device_node *np)
  167. {
  168. return rk_timer_init(np, TIMER_CONTROL_REG3399);
  169. }
  170. CLOCKSOURCE_OF_DECLARE(rk3288_timer, "rockchip,rk3288-timer",
  171. rk3288_timer_init);
  172. CLOCKSOURCE_OF_DECLARE(rk3399_timer, "rockchip,rk3399-timer",
  173. rk3399_timer_init);