asm9260_timer.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 void asm9260_timer_set_mode(enum clock_event_mode mode,
  111. struct clock_event_device *evt)
  112. {
  113. /* stop timer0 */
  114. writel_relaxed(BM_C0_EN, priv.base + HW_TCR + CLR_REG);
  115. switch (mode) {
  116. case CLOCK_EVT_MODE_PERIODIC:
  117. /* disable reset and stop on match */
  118. writel_relaxed(BM_MCR_RES_EN(0) | BM_MCR_STOP_EN(0),
  119. priv.base + HW_MCR + CLR_REG);
  120. /* configure match count for TC0 */
  121. writel_relaxed(priv.ticks_per_jiffy, priv.base + HW_MR0);
  122. /* enable TC0 */
  123. writel_relaxed(BM_C0_EN, priv.base + HW_TCR + SET_REG);
  124. break;
  125. case CLOCK_EVT_MODE_ONESHOT:
  126. /* enable reset and stop on match */
  127. writel_relaxed(BM_MCR_RES_EN(0) | BM_MCR_STOP_EN(0),
  128. priv.base + HW_MCR + SET_REG);
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. static struct clock_event_device event_dev = {
  135. .name = DRIVER_NAME,
  136. .rating = 200,
  137. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  138. .set_next_event = asm9260_timer_set_next_event,
  139. .set_mode = asm9260_timer_set_mode,
  140. };
  141. static irqreturn_t asm9260_timer_interrupt(int irq, void *dev_id)
  142. {
  143. struct clock_event_device *evt = dev_id;
  144. evt->event_handler(evt);
  145. writel_relaxed(BM_IR_MR0, priv.base + HW_IR);
  146. return IRQ_HANDLED;
  147. }
  148. /*
  149. * ---------------------------------------------------------------------------
  150. * Timer initialization
  151. * ---------------------------------------------------------------------------
  152. */
  153. static void __init asm9260_timer_init(struct device_node *np)
  154. {
  155. int irq;
  156. struct clk *clk;
  157. int ret;
  158. unsigned long rate;
  159. priv.base = of_io_request_and_map(np, 0, np->name);
  160. if (IS_ERR(priv.base))
  161. panic("%s: unable to map resource", np->name);
  162. clk = of_clk_get(np, 0);
  163. ret = clk_prepare_enable(clk);
  164. if (ret)
  165. panic("Failed to enable clk!\n");
  166. irq = irq_of_parse_and_map(np, 0);
  167. ret = request_irq(irq, asm9260_timer_interrupt, IRQF_TIMER,
  168. DRIVER_NAME, &event_dev);
  169. if (ret)
  170. panic("Failed to setup irq!\n");
  171. /* set all timers for count-up */
  172. writel_relaxed(BM_DIR_DEFAULT, priv.base + HW_DIR);
  173. /* disable divider */
  174. writel_relaxed(BM_PR_DISABLE, priv.base + HW_PR);
  175. /* make sure all timers use every rising PCLK edge. */
  176. writel_relaxed(BM_CTCR_DEFAULT, priv.base + HW_CTCR);
  177. /* enable interrupt for TC0 and clean setting for all other lines */
  178. writel_relaxed(BM_MCR_INT_EN(0) , priv.base + HW_MCR);
  179. rate = clk_get_rate(clk);
  180. clocksource_mmio_init(priv.base + HW_TC1, DRIVER_NAME, rate,
  181. 200, 32, clocksource_mmio_readl_up);
  182. /* Seems like we can't use counter without match register even if
  183. * actions for MR are disabled. So, set MR to max value. */
  184. writel_relaxed(0xffffffff, priv.base + HW_MR1);
  185. /* enable TC1 */
  186. writel_relaxed(BM_C1_EN, priv.base + HW_TCR + SET_REG);
  187. priv.ticks_per_jiffy = DIV_ROUND_CLOSEST(rate, HZ);
  188. event_dev.cpumask = cpumask_of(0);
  189. clockevents_config_and_register(&event_dev, rate, 0x2c00, 0xfffffffe);
  190. }
  191. CLOCKSOURCE_OF_DECLARE(asm9260_timer, "alphascale,asm9260-timer",
  192. asm9260_timer_init);