asm9260_timer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2014 Oleksij Rempel <linux@rempel-privat.de>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/init.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/sched.h>
  13. #include <linux/clk.h>
  14. #include <linux/clocksource.h>
  15. #include <linux/clockchips.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/bitops.h>
  21. #define DRIVER_NAME "asm9260-timer"
  22. /*
  23. * this device provide 4 offsets for each register:
  24. * 0x0 - plain read write mode
  25. * 0x4 - set mode, OR logic.
  26. * 0x8 - clr mode, XOR logic.
  27. * 0xc - togle mode.
  28. */
  29. #define SET_REG 4
  30. #define CLR_REG 8
  31. #define HW_IR 0x0000 /* RW. Interrupt */
  32. #define BM_IR_CR0 BIT(4)
  33. #define BM_IR_MR3 BIT(3)
  34. #define BM_IR_MR2 BIT(2)
  35. #define BM_IR_MR1 BIT(1)
  36. #define BM_IR_MR0 BIT(0)
  37. #define HW_TCR 0x0010 /* RW. Timer controller */
  38. /* BM_C*_RST
  39. * Timer Counter and the Prescale Counter are synchronously reset on the
  40. * next positive edge of PCLK. The counters remain reset until TCR[1] is
  41. * returned to zero. */
  42. #define BM_C3_RST BIT(7)
  43. #define BM_C2_RST BIT(6)
  44. #define BM_C1_RST BIT(5)
  45. #define BM_C0_RST BIT(4)
  46. /* BM_C*_EN
  47. * 1 - Timer Counter and Prescale Counter are enabled for counting
  48. * 0 - counters are disabled */
  49. #define BM_C3_EN BIT(3)
  50. #define BM_C2_EN BIT(2)
  51. #define BM_C1_EN BIT(1)
  52. #define BM_C0_EN BIT(0)
  53. #define HW_DIR 0x0020 /* RW. Direction? */
  54. /* 00 - count up
  55. * 01 - count down
  56. * 10 - ?? 2^n/2 */
  57. #define BM_DIR_COUNT_UP 0
  58. #define BM_DIR_COUNT_DOWN 1
  59. #define BM_DIR0_SHIFT 0
  60. #define BM_DIR1_SHIFT 4
  61. #define BM_DIR2_SHIFT 8
  62. #define BM_DIR3_SHIFT 12
  63. #define BM_DIR_DEFAULT (BM_DIR_COUNT_UP << BM_DIR0_SHIFT | \
  64. BM_DIR_COUNT_UP << BM_DIR1_SHIFT | \
  65. BM_DIR_COUNT_UP << BM_DIR2_SHIFT | \
  66. BM_DIR_COUNT_UP << BM_DIR3_SHIFT)
  67. #define HW_TC0 0x0030 /* RO. Timer counter 0 */
  68. /* HW_TC*. Timer counter owerflow (0xffff.ffff to 0x0000.0000) do not generate
  69. * interrupt. This registers can be used to detect overflow */
  70. #define HW_TC1 0x0040
  71. #define HW_TC2 0x0050
  72. #define HW_TC3 0x0060
  73. #define HW_PR 0x0070 /* RW. prescaler */
  74. #define BM_PR_DISABLE 0
  75. #define HW_PC 0x0080 /* RO. Prescaler counter */
  76. #define HW_MCR 0x0090 /* RW. Match control */
  77. /* enable interrupt on match */
  78. #define BM_MCR_INT_EN(n) (1 << (n * 3 + 0))
  79. /* enable TC reset on match */
  80. #define BM_MCR_RES_EN(n) (1 << (n * 3 + 1))
  81. /* enable stop TC on match */
  82. #define BM_MCR_STOP_EN(n) (1 << (n * 3 + 2))
  83. #define HW_MR0 0x00a0 /* RW. Match reg */
  84. #define HW_MR1 0x00b0
  85. #define HW_MR2 0x00C0
  86. #define HW_MR3 0x00D0
  87. #define HW_CTCR 0x0180 /* Counter control */
  88. #define BM_CTCR0_SHIFT 0
  89. #define BM_CTCR1_SHIFT 2
  90. #define BM_CTCR2_SHIFT 4
  91. #define BM_CTCR3_SHIFT 6
  92. #define BM_CTCR_TM 0 /* Timer mode. Every rising PCLK edge. */
  93. #define BM_CTCR_DEFAULT (BM_CTCR_TM << BM_CTCR0_SHIFT | \
  94. BM_CTCR_TM << BM_CTCR1_SHIFT | \
  95. BM_CTCR_TM << BM_CTCR2_SHIFT | \
  96. BM_CTCR_TM << BM_CTCR3_SHIFT)
  97. static struct asm9260_timer_priv {
  98. void __iomem *base;
  99. unsigned long ticks_per_jiffy;
  100. } priv;
  101. static int asm9260_timer_set_next_event(unsigned long delta,
  102. struct clock_event_device *evt)
  103. {
  104. /* configure match count for TC0 */
  105. writel_relaxed(delta, priv.base + HW_MR0);
  106. /* enable TC0 */
  107. writel_relaxed(BM_C0_EN, priv.base + HW_TCR + SET_REG);
  108. return 0;
  109. }
  110. static inline void __asm9260_timer_shutdown(struct clock_event_device *evt)
  111. {
  112. /* stop timer0 */
  113. writel_relaxed(BM_C0_EN, priv.base + HW_TCR + CLR_REG);
  114. }
  115. static int asm9260_timer_shutdown(struct clock_event_device *evt)
  116. {
  117. __asm9260_timer_shutdown(evt);
  118. return 0;
  119. }
  120. static int asm9260_timer_set_oneshot(struct clock_event_device *evt)
  121. {
  122. __asm9260_timer_shutdown(evt);
  123. /* enable reset and stop on match */
  124. writel_relaxed(BM_MCR_RES_EN(0) | BM_MCR_STOP_EN(0),
  125. priv.base + HW_MCR + SET_REG);
  126. return 0;
  127. }
  128. static int asm9260_timer_set_periodic(struct clock_event_device *evt)
  129. {
  130. __asm9260_timer_shutdown(evt);
  131. /* disable reset and stop on match */
  132. writel_relaxed(BM_MCR_RES_EN(0) | BM_MCR_STOP_EN(0),
  133. priv.base + HW_MCR + CLR_REG);
  134. /* configure match count for TC0 */
  135. writel_relaxed(priv.ticks_per_jiffy, priv.base + HW_MR0);
  136. /* enable TC0 */
  137. writel_relaxed(BM_C0_EN, priv.base + HW_TCR + SET_REG);
  138. return 0;
  139. }
  140. static struct clock_event_device event_dev = {
  141. .name = DRIVER_NAME,
  142. .rating = 200,
  143. .features = CLOCK_EVT_FEAT_PERIODIC |
  144. CLOCK_EVT_FEAT_ONESHOT,
  145. .set_next_event = asm9260_timer_set_next_event,
  146. .set_state_shutdown = asm9260_timer_shutdown,
  147. .set_state_periodic = asm9260_timer_set_periodic,
  148. .set_state_oneshot = asm9260_timer_set_oneshot,
  149. .tick_resume = asm9260_timer_shutdown,
  150. };
  151. static irqreturn_t asm9260_timer_interrupt(int irq, void *dev_id)
  152. {
  153. struct clock_event_device *evt = dev_id;
  154. evt->event_handler(evt);
  155. writel_relaxed(BM_IR_MR0, priv.base + HW_IR);
  156. return IRQ_HANDLED;
  157. }
  158. /*
  159. * ---------------------------------------------------------------------------
  160. * Timer initialization
  161. * ---------------------------------------------------------------------------
  162. */
  163. static int __init asm9260_timer_init(struct device_node *np)
  164. {
  165. int irq;
  166. struct clk *clk;
  167. int ret;
  168. unsigned long rate;
  169. priv.base = of_io_request_and_map(np, 0, np->name);
  170. if (IS_ERR(priv.base)) {
  171. pr_err("%s: unable to map resource\n", np->name);
  172. return PTR_ERR(priv.base);
  173. }
  174. clk = of_clk_get(np, 0);
  175. if (IS_ERR(clk)) {
  176. pr_err("Failed to get clk!\n");
  177. return PTR_ERR(clk);
  178. }
  179. ret = clk_prepare_enable(clk);
  180. if (ret) {
  181. pr_err("Failed to enable clk!\n");
  182. return ret;
  183. }
  184. irq = irq_of_parse_and_map(np, 0);
  185. ret = request_irq(irq, asm9260_timer_interrupt, IRQF_TIMER,
  186. DRIVER_NAME, &event_dev);
  187. if (ret) {
  188. pr_err("Failed to setup irq!\n");
  189. return ret;
  190. }
  191. /* set all timers for count-up */
  192. writel_relaxed(BM_DIR_DEFAULT, priv.base + HW_DIR);
  193. /* disable divider */
  194. writel_relaxed(BM_PR_DISABLE, priv.base + HW_PR);
  195. /* make sure all timers use every rising PCLK edge. */
  196. writel_relaxed(BM_CTCR_DEFAULT, priv.base + HW_CTCR);
  197. /* enable interrupt for TC0 and clean setting for all other lines */
  198. writel_relaxed(BM_MCR_INT_EN(0) , priv.base + HW_MCR);
  199. rate = clk_get_rate(clk);
  200. clocksource_mmio_init(priv.base + HW_TC1, DRIVER_NAME, rate,
  201. 200, 32, clocksource_mmio_readl_up);
  202. /* Seems like we can't use counter without match register even if
  203. * actions for MR are disabled. So, set MR to max value. */
  204. writel_relaxed(0xffffffff, priv.base + HW_MR1);
  205. /* enable TC1 */
  206. writel_relaxed(BM_C1_EN, priv.base + HW_TCR + SET_REG);
  207. priv.ticks_per_jiffy = DIV_ROUND_CLOSEST(rate, HZ);
  208. event_dev.cpumask = cpumask_of(0);
  209. clockevents_config_and_register(&event_dev, rate, 0x2c00, 0xfffffffe);
  210. return 0;
  211. }
  212. TIMER_OF_DECLARE(asm9260_timer, "alphascale,asm9260-timer",
  213. asm9260_timer_init);