rockchip_timer.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_REG 0x10
  21. #define TIMER_INT_STATUS 0x18
  22. #define TIMER_DISABLE 0x0
  23. #define TIMER_ENABLE 0x1
  24. #define TIMER_MODE_FREE_RUNNING (0 << 1)
  25. #define TIMER_MODE_USER_DEFINED_COUNT (1 << 1)
  26. #define TIMER_INT_UNMASK (1 << 2)
  27. struct bc_timer {
  28. struct clock_event_device ce;
  29. void __iomem *base;
  30. u32 freq;
  31. };
  32. static struct bc_timer bc_timer;
  33. static inline struct bc_timer *rk_timer(struct clock_event_device *ce)
  34. {
  35. return container_of(ce, struct bc_timer, ce);
  36. }
  37. static inline void __iomem *rk_base(struct clock_event_device *ce)
  38. {
  39. return rk_timer(ce)->base;
  40. }
  41. static inline void rk_timer_disable(struct clock_event_device *ce)
  42. {
  43. writel_relaxed(TIMER_DISABLE, rk_base(ce) + TIMER_CONTROL_REG);
  44. dsb();
  45. }
  46. static inline void rk_timer_enable(struct clock_event_device *ce, u32 flags)
  47. {
  48. writel_relaxed(TIMER_ENABLE | TIMER_INT_UNMASK | flags,
  49. rk_base(ce) + TIMER_CONTROL_REG);
  50. dsb();
  51. }
  52. static void rk_timer_update_counter(unsigned long cycles,
  53. struct clock_event_device *ce)
  54. {
  55. writel_relaxed(cycles, rk_base(ce) + TIMER_LOAD_COUNT0);
  56. writel_relaxed(0, rk_base(ce) + TIMER_LOAD_COUNT1);
  57. dsb();
  58. }
  59. static void rk_timer_interrupt_clear(struct clock_event_device *ce)
  60. {
  61. writel_relaxed(1, rk_base(ce) + TIMER_INT_STATUS);
  62. dsb();
  63. }
  64. static inline int rk_timer_set_next_event(unsigned long cycles,
  65. struct clock_event_device *ce)
  66. {
  67. rk_timer_disable(ce);
  68. rk_timer_update_counter(cycles, ce);
  69. rk_timer_enable(ce, TIMER_MODE_USER_DEFINED_COUNT);
  70. return 0;
  71. }
  72. static inline void rk_timer_set_mode(enum clock_event_mode mode,
  73. struct clock_event_device *ce)
  74. {
  75. switch (mode) {
  76. case CLOCK_EVT_MODE_PERIODIC:
  77. rk_timer_disable(ce);
  78. rk_timer_update_counter(rk_timer(ce)->freq / HZ - 1, ce);
  79. rk_timer_enable(ce, TIMER_MODE_FREE_RUNNING);
  80. break;
  81. case CLOCK_EVT_MODE_ONESHOT:
  82. case CLOCK_EVT_MODE_RESUME:
  83. break;
  84. case CLOCK_EVT_MODE_UNUSED:
  85. case CLOCK_EVT_MODE_SHUTDOWN:
  86. rk_timer_disable(ce);
  87. break;
  88. }
  89. }
  90. static irqreturn_t rk_timer_interrupt(int irq, void *dev_id)
  91. {
  92. struct clock_event_device *ce = dev_id;
  93. rk_timer_interrupt_clear(ce);
  94. if (ce->mode == CLOCK_EVT_MODE_ONESHOT)
  95. rk_timer_disable(ce);
  96. ce->event_handler(ce);
  97. return IRQ_HANDLED;
  98. }
  99. static void __init rk_timer_init(struct device_node *np)
  100. {
  101. struct clock_event_device *ce = &bc_timer.ce;
  102. struct clk *timer_clk;
  103. struct clk *pclk;
  104. int ret, irq;
  105. bc_timer.base = of_iomap(np, 0);
  106. if (!bc_timer.base) {
  107. pr_err("Failed to get base address for '%s'\n", TIMER_NAME);
  108. return;
  109. }
  110. pclk = of_clk_get_by_name(np, "pclk");
  111. if (IS_ERR(pclk)) {
  112. pr_err("Failed to get pclk for '%s'\n", TIMER_NAME);
  113. return;
  114. }
  115. if (clk_prepare_enable(pclk)) {
  116. pr_err("Failed to enable pclk for '%s'\n", TIMER_NAME);
  117. return;
  118. }
  119. timer_clk = of_clk_get_by_name(np, "timer");
  120. if (IS_ERR(timer_clk)) {
  121. pr_err("Failed to get timer clock for '%s'\n", TIMER_NAME);
  122. return;
  123. }
  124. if (clk_prepare_enable(timer_clk)) {
  125. pr_err("Failed to enable timer clock\n");
  126. return;
  127. }
  128. bc_timer.freq = clk_get_rate(timer_clk);
  129. irq = irq_of_parse_and_map(np, 0);
  130. if (irq == NO_IRQ) {
  131. pr_err("Failed to map interrupts for '%s'\n", TIMER_NAME);
  132. return;
  133. }
  134. ce->name = TIMER_NAME;
  135. ce->features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT;
  136. ce->set_next_event = rk_timer_set_next_event;
  137. ce->set_mode = rk_timer_set_mode;
  138. ce->irq = irq;
  139. ce->cpumask = cpumask_of(0);
  140. ce->rating = 250;
  141. rk_timer_interrupt_clear(ce);
  142. rk_timer_disable(ce);
  143. ret = request_irq(irq, rk_timer_interrupt, IRQF_TIMER, TIMER_NAME, ce);
  144. if (ret) {
  145. pr_err("Failed to initialize '%s': %d\n", TIMER_NAME, ret);
  146. return;
  147. }
  148. clockevents_config_and_register(ce, bc_timer.freq, 1, UINT_MAX);
  149. }
  150. CLOCKSOURCE_OF_DECLARE(rk_timer, "rockchip,rk3288-timer", rk_timer_init);