moxart_timer.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * MOXA ART SoCs timer handling.
  3. *
  4. * Copyright (C) 2013 Jonas Jensen
  5. *
  6. * Jonas Jensen <jonas.jensen@gmail.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqreturn.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/io.h>
  21. #include <linux/clocksource.h>
  22. #include <linux/bitops.h>
  23. #include <linux/slab.h>
  24. #define TIMER1_BASE 0x00
  25. #define TIMER2_BASE 0x10
  26. #define TIMER3_BASE 0x20
  27. #define REG_COUNT 0x0 /* writable */
  28. #define REG_LOAD 0x4
  29. #define REG_MATCH1 0x8
  30. #define REG_MATCH2 0xC
  31. #define TIMER_CR 0x30
  32. #define TIMER_INTR_STATE 0x34
  33. #define TIMER_INTR_MASK 0x38
  34. /*
  35. * Moxart TIMER_CR flags:
  36. *
  37. * MOXART_CR_*_CLOCK 0: PCLK, 1: EXT1CLK
  38. * MOXART_CR_*_INT overflow interrupt enable bit
  39. */
  40. #define MOXART_CR_1_ENABLE BIT(0)
  41. #define MOXART_CR_1_CLOCK BIT(1)
  42. #define MOXART_CR_1_INT BIT(2)
  43. #define MOXART_CR_2_ENABLE BIT(3)
  44. #define MOXART_CR_2_CLOCK BIT(4)
  45. #define MOXART_CR_2_INT BIT(5)
  46. #define MOXART_CR_3_ENABLE BIT(6)
  47. #define MOXART_CR_3_CLOCK BIT(7)
  48. #define MOXART_CR_3_INT BIT(8)
  49. #define MOXART_CR_COUNT_UP BIT(9)
  50. #define MOXART_TIMER1_ENABLE (MOXART_CR_2_ENABLE | MOXART_CR_1_ENABLE)
  51. #define MOXART_TIMER1_DISABLE (MOXART_CR_2_ENABLE)
  52. /*
  53. * The ASpeed variant of the IP block has a different layout
  54. * for the control register
  55. */
  56. #define ASPEED_CR_1_ENABLE BIT(0)
  57. #define ASPEED_CR_1_CLOCK BIT(1)
  58. #define ASPEED_CR_1_INT BIT(2)
  59. #define ASPEED_CR_2_ENABLE BIT(4)
  60. #define ASPEED_CR_2_CLOCK BIT(5)
  61. #define ASPEED_CR_2_INT BIT(6)
  62. #define ASPEED_CR_3_ENABLE BIT(8)
  63. #define ASPEED_CR_3_CLOCK BIT(9)
  64. #define ASPEED_CR_3_INT BIT(10)
  65. #define ASPEED_TIMER1_ENABLE (ASPEED_CR_2_ENABLE | ASPEED_CR_1_ENABLE)
  66. #define ASPEED_TIMER1_DISABLE (ASPEED_CR_2_ENABLE)
  67. struct moxart_timer {
  68. void __iomem *base;
  69. unsigned int t1_disable_val;
  70. unsigned int t1_enable_val;
  71. unsigned int count_per_tick;
  72. struct clock_event_device clkevt;
  73. };
  74. static inline struct moxart_timer *to_moxart(struct clock_event_device *evt)
  75. {
  76. return container_of(evt, struct moxart_timer, clkevt);
  77. }
  78. static inline void moxart_disable(struct clock_event_device *evt)
  79. {
  80. struct moxart_timer *timer = to_moxart(evt);
  81. writel(timer->t1_disable_val, timer->base + TIMER_CR);
  82. }
  83. static inline void moxart_enable(struct clock_event_device *evt)
  84. {
  85. struct moxart_timer *timer = to_moxart(evt);
  86. writel(timer->t1_enable_val, timer->base + TIMER_CR);
  87. }
  88. static int moxart_shutdown(struct clock_event_device *evt)
  89. {
  90. moxart_disable(evt);
  91. return 0;
  92. }
  93. static int moxart_set_oneshot(struct clock_event_device *evt)
  94. {
  95. moxart_disable(evt);
  96. writel(~0, to_moxart(evt)->base + TIMER1_BASE + REG_LOAD);
  97. return 0;
  98. }
  99. static int moxart_set_periodic(struct clock_event_device *evt)
  100. {
  101. struct moxart_timer *timer = to_moxart(evt);
  102. moxart_disable(evt);
  103. writel(timer->count_per_tick, timer->base + TIMER1_BASE + REG_LOAD);
  104. writel(0, timer->base + TIMER1_BASE + REG_MATCH1);
  105. moxart_enable(evt);
  106. return 0;
  107. }
  108. static int moxart_clkevt_next_event(unsigned long cycles,
  109. struct clock_event_device *evt)
  110. {
  111. struct moxart_timer *timer = to_moxart(evt);
  112. u32 u;
  113. moxart_disable(evt);
  114. u = readl(timer->base + TIMER1_BASE + REG_COUNT) - cycles;
  115. writel(u, timer->base + TIMER1_BASE + REG_MATCH1);
  116. moxart_enable(evt);
  117. return 0;
  118. }
  119. static irqreturn_t moxart_timer_interrupt(int irq, void *dev_id)
  120. {
  121. struct clock_event_device *evt = dev_id;
  122. evt->event_handler(evt);
  123. return IRQ_HANDLED;
  124. }
  125. static int __init moxart_timer_init(struct device_node *node)
  126. {
  127. int ret, irq;
  128. unsigned long pclk;
  129. struct clk *clk;
  130. struct moxart_timer *timer;
  131. timer = kzalloc(sizeof(*timer), GFP_KERNEL);
  132. if (!timer)
  133. return -ENOMEM;
  134. timer->base = of_iomap(node, 0);
  135. if (!timer->base) {
  136. pr_err("%s: of_iomap failed\n", node->full_name);
  137. return -ENXIO;
  138. }
  139. irq = irq_of_parse_and_map(node, 0);
  140. if (irq <= 0) {
  141. pr_err("%s: irq_of_parse_and_map failed\n", node->full_name);
  142. return -EINVAL;
  143. }
  144. clk = of_clk_get(node, 0);
  145. if (IS_ERR(clk)) {
  146. pr_err("%s: of_clk_get failed\n", node->full_name);
  147. return PTR_ERR(clk);
  148. }
  149. pclk = clk_get_rate(clk);
  150. if (of_device_is_compatible(node, "moxa,moxart-timer")) {
  151. timer->t1_enable_val = MOXART_TIMER1_ENABLE;
  152. timer->t1_disable_val = MOXART_TIMER1_DISABLE;
  153. } else if (of_device_is_compatible(node, "aspeed,ast2400-timer")) {
  154. timer->t1_enable_val = ASPEED_TIMER1_ENABLE;
  155. timer->t1_disable_val = ASPEED_TIMER1_DISABLE;
  156. } else {
  157. pr_err("%s: unknown platform\n", node->full_name);
  158. return -EINVAL;
  159. }
  160. timer->count_per_tick = DIV_ROUND_CLOSEST(pclk, HZ);
  161. timer->clkevt.name = node->name;
  162. timer->clkevt.rating = 200;
  163. timer->clkevt.features = CLOCK_EVT_FEAT_PERIODIC |
  164. CLOCK_EVT_FEAT_ONESHOT;
  165. timer->clkevt.set_state_shutdown = moxart_shutdown;
  166. timer->clkevt.set_state_periodic = moxart_set_periodic;
  167. timer->clkevt.set_state_oneshot = moxart_set_oneshot;
  168. timer->clkevt.tick_resume = moxart_set_oneshot;
  169. timer->clkevt.set_next_event = moxart_clkevt_next_event;
  170. timer->clkevt.cpumask = cpumask_of(0);
  171. timer->clkevt.irq = irq;
  172. ret = clocksource_mmio_init(timer->base + TIMER2_BASE + REG_COUNT,
  173. "moxart_timer", pclk, 200, 32,
  174. clocksource_mmio_readl_down);
  175. if (ret) {
  176. pr_err("%s: clocksource_mmio_init failed\n", node->full_name);
  177. return ret;
  178. }
  179. ret = request_irq(irq, moxart_timer_interrupt, IRQF_TIMER,
  180. node->name, &timer->clkevt);
  181. if (ret) {
  182. pr_err("%s: setup_irq failed\n", node->full_name);
  183. return ret;
  184. }
  185. /* Clear match registers */
  186. writel(0, timer->base + TIMER1_BASE + REG_MATCH1);
  187. writel(0, timer->base + TIMER1_BASE + REG_MATCH2);
  188. writel(0, timer->base + TIMER2_BASE + REG_MATCH1);
  189. writel(0, timer->base + TIMER2_BASE + REG_MATCH2);
  190. /*
  191. * Start timer 2 rolling as our main wall clock source, keep timer 1
  192. * disabled
  193. */
  194. writel(0, timer->base + TIMER_CR);
  195. writel(~0, timer->base + TIMER2_BASE + REG_LOAD);
  196. writel(timer->t1_disable_val, timer->base + TIMER_CR);
  197. /*
  198. * documentation is not publicly available:
  199. * min_delta / max_delta obtained by trial-and-error,
  200. * max_delta 0xfffffffe should be ok because count
  201. * register size is u32
  202. */
  203. clockevents_config_and_register(&timer->clkevt, pclk, 0x4, 0xfffffffe);
  204. return 0;
  205. }
  206. CLOCKSOURCE_OF_DECLARE(moxart, "moxa,moxart-timer", moxart_timer_init);
  207. CLOCKSOURCE_OF_DECLARE(aspeed, "aspeed,ast2400-timer", moxart_timer_init);