timer-zevio.c 5.5 KB

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