zevio-timer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * linux/drivers/clocksource/zevio-timer.c
  3. *
  4. * Copyright (C) 2013 Daniel Tang <tangrs@tangrs.id.au>
  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. */
  11. #include <linux/io.h>
  12. #include <linux/irq.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/clk.h>
  17. #include <linux/clockchips.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/slab.h>
  21. #define IO_CURRENT_VAL 0x00
  22. #define IO_DIVIDER 0x04
  23. #define IO_CONTROL 0x08
  24. #define IO_TIMER1 0x00
  25. #define IO_TIMER2 0x0C
  26. #define IO_MATCH_BEGIN 0x18
  27. #define IO_MATCH(x) (IO_MATCH_BEGIN + ((x) << 2))
  28. #define IO_INTR_STS 0x00
  29. #define IO_INTR_ACK 0x00
  30. #define IO_INTR_MSK 0x04
  31. #define CNTL_STOP_TIMER (1 << 4)
  32. #define CNTL_RUN_TIMER (0 << 4)
  33. #define CNTL_INC (1 << 3)
  34. #define CNTL_DEC (0 << 3)
  35. #define CNTL_TOZERO 0
  36. #define CNTL_MATCH(x) ((x) + 1)
  37. #define CNTL_FOREVER 7
  38. /* There are 6 match registers but we only use one. */
  39. #define TIMER_MATCH 0
  40. #define TIMER_INTR_MSK (1 << (TIMER_MATCH))
  41. #define TIMER_INTR_ALL 0x3F
  42. struct zevio_timer {
  43. void __iomem *base;
  44. void __iomem *timer1, *timer2;
  45. void __iomem *interrupt_regs;
  46. struct clk *clk;
  47. struct clock_event_device clkevt;
  48. struct irqaction clkevt_irq;
  49. char clocksource_name[64];
  50. char clockevent_name[64];
  51. };
  52. static int zevio_timer_set_event(unsigned long delta,
  53. struct clock_event_device *dev)
  54. {
  55. struct zevio_timer *timer = container_of(dev, struct zevio_timer,
  56. clkevt);
  57. writel(delta, timer->timer1 + IO_CURRENT_VAL);
  58. writel(CNTL_RUN_TIMER | CNTL_DEC | CNTL_MATCH(TIMER_MATCH),
  59. timer->timer1 + IO_CONTROL);
  60. return 0;
  61. }
  62. static void zevio_timer_set_mode(enum clock_event_mode mode,
  63. struct clock_event_device *dev)
  64. {
  65. struct zevio_timer *timer = container_of(dev, struct zevio_timer,
  66. clkevt);
  67. switch (mode) {
  68. case CLOCK_EVT_MODE_RESUME:
  69. case CLOCK_EVT_MODE_ONESHOT:
  70. /* Enable timer interrupts */
  71. writel(TIMER_INTR_MSK, timer->interrupt_regs + IO_INTR_MSK);
  72. writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
  73. break;
  74. case CLOCK_EVT_MODE_SHUTDOWN:
  75. case CLOCK_EVT_MODE_UNUSED:
  76. /* Disable timer interrupts */
  77. writel(0, timer->interrupt_regs + IO_INTR_MSK);
  78. writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
  79. /* Stop timer */
  80. writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
  81. break;
  82. case CLOCK_EVT_MODE_PERIODIC:
  83. default:
  84. /* Unsupported */
  85. break;
  86. }
  87. }
  88. static irqreturn_t zevio_timer_interrupt(int irq, void *dev_id)
  89. {
  90. struct zevio_timer *timer = dev_id;
  91. u32 intr;
  92. intr = readl(timer->interrupt_regs + IO_INTR_ACK);
  93. if (!(intr & TIMER_INTR_MSK))
  94. return IRQ_NONE;
  95. writel(TIMER_INTR_MSK, timer->interrupt_regs + IO_INTR_ACK);
  96. writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
  97. if (timer->clkevt.event_handler)
  98. timer->clkevt.event_handler(&timer->clkevt);
  99. return IRQ_HANDLED;
  100. }
  101. static int __init zevio_timer_add(struct device_node *node)
  102. {
  103. struct zevio_timer *timer;
  104. struct resource res;
  105. int irqnr, ret;
  106. timer = kzalloc(sizeof(*timer), GFP_KERNEL);
  107. if (!timer)
  108. return -ENOMEM;
  109. timer->base = of_iomap(node, 0);
  110. if (!timer->base) {
  111. ret = -EINVAL;
  112. goto error_free;
  113. }
  114. timer->timer1 = timer->base + IO_TIMER1;
  115. timer->timer2 = timer->base + IO_TIMER2;
  116. timer->clk = of_clk_get(node, 0);
  117. if (IS_ERR(timer->clk)) {
  118. ret = PTR_ERR(timer->clk);
  119. pr_err("Timer clock not found! (error %d)\n", ret);
  120. goto error_unmap;
  121. }
  122. timer->interrupt_regs = of_iomap(node, 1);
  123. irqnr = irq_of_parse_and_map(node, 0);
  124. of_address_to_resource(node, 0, &res);
  125. scnprintf(timer->clocksource_name, sizeof(timer->clocksource_name),
  126. "%llx.%s_clocksource",
  127. (unsigned long long)res.start, node->name);
  128. scnprintf(timer->clockevent_name, sizeof(timer->clockevent_name),
  129. "%llx.%s_clockevent",
  130. (unsigned long long)res.start, node->name);
  131. if (timer->interrupt_regs && irqnr) {
  132. timer->clkevt.name = timer->clockevent_name;
  133. timer->clkevt.set_next_event = zevio_timer_set_event;
  134. timer->clkevt.set_mode = zevio_timer_set_mode;
  135. timer->clkevt.rating = 200;
  136. timer->clkevt.cpumask = cpu_all_mask;
  137. timer->clkevt.features = CLOCK_EVT_FEAT_ONESHOT;
  138. timer->clkevt.irq = irqnr;
  139. writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
  140. writel(0, timer->timer1 + IO_DIVIDER);
  141. /* Start with timer interrupts disabled */
  142. writel(0, timer->interrupt_regs + IO_INTR_MSK);
  143. writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
  144. /* Interrupt to occur when timer value matches 0 */
  145. writel(0, timer->base + IO_MATCH(TIMER_MATCH));
  146. timer->clkevt_irq.name = timer->clockevent_name;
  147. timer->clkevt_irq.handler = zevio_timer_interrupt;
  148. timer->clkevt_irq.dev_id = timer;
  149. timer->clkevt_irq.flags = IRQF_TIMER | IRQF_IRQPOLL;
  150. setup_irq(irqnr, &timer->clkevt_irq);
  151. clockevents_config_and_register(&timer->clkevt,
  152. clk_get_rate(timer->clk), 0x0001, 0xffff);
  153. pr_info("Added %s as clockevent\n", timer->clockevent_name);
  154. }
  155. writel(CNTL_STOP_TIMER, timer->timer2 + IO_CONTROL);
  156. writel(0, timer->timer2 + IO_CURRENT_VAL);
  157. writel(0, timer->timer2 + IO_DIVIDER);
  158. writel(CNTL_RUN_TIMER | CNTL_FOREVER | CNTL_INC,
  159. timer->timer2 + IO_CONTROL);
  160. clocksource_mmio_init(timer->timer2 + IO_CURRENT_VAL,
  161. timer->clocksource_name,
  162. clk_get_rate(timer->clk),
  163. 200, 16,
  164. clocksource_mmio_readw_up);
  165. pr_info("Added %s as clocksource\n", timer->clocksource_name);
  166. return 0;
  167. error_unmap:
  168. iounmap(timer->base);
  169. error_free:
  170. kfree(timer);
  171. return ret;
  172. }
  173. static void __init zevio_timer_init(struct device_node *node)
  174. {
  175. BUG_ON(zevio_timer_add(node));
  176. }
  177. CLOCKSOURCE_OF_DECLARE(zevio_timer, "lsi,zevio-timer", zevio_timer_init);