zevio-timer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 int zevio_timer_shutdown(struct clock_event_device *dev)
  63. {
  64. struct zevio_timer *timer = container_of(dev, struct zevio_timer,
  65. clkevt);
  66. /* Disable timer interrupts */
  67. writel(0, timer->interrupt_regs + IO_INTR_MSK);
  68. writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
  69. /* Stop timer */
  70. writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
  71. return 0;
  72. }
  73. static int zevio_timer_set_oneshot(struct clock_event_device *dev)
  74. {
  75. struct zevio_timer *timer = container_of(dev, struct zevio_timer,
  76. clkevt);
  77. /* Enable timer interrupts */
  78. writel(TIMER_INTR_MSK, timer->interrupt_regs + IO_INTR_MSK);
  79. writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
  80. return 0;
  81. }
  82. static irqreturn_t zevio_timer_interrupt(int irq, void *dev_id)
  83. {
  84. struct zevio_timer *timer = dev_id;
  85. u32 intr;
  86. intr = readl(timer->interrupt_regs + IO_INTR_ACK);
  87. if (!(intr & TIMER_INTR_MSK))
  88. return IRQ_NONE;
  89. writel(TIMER_INTR_MSK, timer->interrupt_regs + IO_INTR_ACK);
  90. writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
  91. if (timer->clkevt.event_handler)
  92. timer->clkevt.event_handler(&timer->clkevt);
  93. return IRQ_HANDLED;
  94. }
  95. static int __init zevio_timer_add(struct device_node *node)
  96. {
  97. struct zevio_timer *timer;
  98. struct resource res;
  99. int irqnr, ret;
  100. timer = kzalloc(sizeof(*timer), GFP_KERNEL);
  101. if (!timer)
  102. return -ENOMEM;
  103. timer->base = of_iomap(node, 0);
  104. if (!timer->base) {
  105. ret = -EINVAL;
  106. goto error_free;
  107. }
  108. timer->timer1 = timer->base + IO_TIMER1;
  109. timer->timer2 = timer->base + IO_TIMER2;
  110. timer->clk = of_clk_get(node, 0);
  111. if (IS_ERR(timer->clk)) {
  112. ret = PTR_ERR(timer->clk);
  113. pr_err("Timer clock not found! (error %d)\n", ret);
  114. goto error_unmap;
  115. }
  116. timer->interrupt_regs = of_iomap(node, 1);
  117. irqnr = irq_of_parse_and_map(node, 0);
  118. of_address_to_resource(node, 0, &res);
  119. scnprintf(timer->clocksource_name, sizeof(timer->clocksource_name),
  120. "%llx.%s_clocksource",
  121. (unsigned long long)res.start, node->name);
  122. scnprintf(timer->clockevent_name, sizeof(timer->clockevent_name),
  123. "%llx.%s_clockevent",
  124. (unsigned long long)res.start, node->name);
  125. if (timer->interrupt_regs && irqnr) {
  126. timer->clkevt.name = timer->clockevent_name;
  127. timer->clkevt.set_next_event = zevio_timer_set_event;
  128. timer->clkevt.set_state_shutdown = zevio_timer_shutdown;
  129. timer->clkevt.set_state_oneshot = zevio_timer_set_oneshot;
  130. timer->clkevt.tick_resume = zevio_timer_set_oneshot;
  131. timer->clkevt.rating = 200;
  132. timer->clkevt.cpumask = cpu_all_mask;
  133. timer->clkevt.features = CLOCK_EVT_FEAT_ONESHOT;
  134. timer->clkevt.irq = irqnr;
  135. writel(CNTL_STOP_TIMER, timer->timer1 + IO_CONTROL);
  136. writel(0, timer->timer1 + IO_DIVIDER);
  137. /* Start with timer interrupts disabled */
  138. writel(0, timer->interrupt_regs + IO_INTR_MSK);
  139. writel(TIMER_INTR_ALL, timer->interrupt_regs + IO_INTR_ACK);
  140. /* Interrupt to occur when timer value matches 0 */
  141. writel(0, timer->base + IO_MATCH(TIMER_MATCH));
  142. timer->clkevt_irq.name = timer->clockevent_name;
  143. timer->clkevt_irq.handler = zevio_timer_interrupt;
  144. timer->clkevt_irq.dev_id = timer;
  145. timer->clkevt_irq.flags = IRQF_TIMER | IRQF_IRQPOLL;
  146. setup_irq(irqnr, &timer->clkevt_irq);
  147. clockevents_config_and_register(&timer->clkevt,
  148. clk_get_rate(timer->clk), 0x0001, 0xffff);
  149. pr_info("Added %s as clockevent\n", timer->clockevent_name);
  150. }
  151. writel(CNTL_STOP_TIMER, timer->timer2 + IO_CONTROL);
  152. writel(0, timer->timer2 + IO_CURRENT_VAL);
  153. writel(0, timer->timer2 + IO_DIVIDER);
  154. writel(CNTL_RUN_TIMER | CNTL_FOREVER | CNTL_INC,
  155. timer->timer2 + IO_CONTROL);
  156. clocksource_mmio_init(timer->timer2 + IO_CURRENT_VAL,
  157. timer->clocksource_name,
  158. clk_get_rate(timer->clk),
  159. 200, 16,
  160. clocksource_mmio_readw_up);
  161. pr_info("Added %s as clocksource\n", timer->clocksource_name);
  162. return 0;
  163. error_unmap:
  164. iounmap(timer->base);
  165. error_free:
  166. kfree(timer);
  167. return ret;
  168. }
  169. static int __init zevio_timer_init(struct device_node *node)
  170. {
  171. return zevio_timer_add(node);
  172. }
  173. CLOCKSOURCE_OF_DECLARE(zevio_timer, "lsi,zevio-timer", zevio_timer_init);