h8300_timer16.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * H8/300 16bit Timer driver
  4. *
  5. * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
  6. */
  7. #include <linux/interrupt.h>
  8. #include <linux/init.h>
  9. #include <linux/clocksource.h>
  10. #include <linux/clk.h>
  11. #include <linux/io.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #define TSTR 0
  16. #define TISRC 6
  17. #define TCR 0
  18. #define TCNT 2
  19. #define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
  20. #define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
  21. struct timer16_priv {
  22. struct clocksource cs;
  23. unsigned long total_cycles;
  24. void __iomem *mapbase;
  25. void __iomem *mapcommon;
  26. unsigned short cs_enabled;
  27. unsigned char enb;
  28. unsigned char ovf;
  29. unsigned char ovie;
  30. };
  31. static unsigned long timer16_get_counter(struct timer16_priv *p)
  32. {
  33. unsigned short v1, v2, v3;
  34. unsigned char o1, o2;
  35. o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
  36. /* Make sure the timer value is stable. Stolen from acpi_pm.c */
  37. do {
  38. o2 = o1;
  39. v1 = ioread16be(p->mapbase + TCNT);
  40. v2 = ioread16be(p->mapbase + TCNT);
  41. v3 = ioread16be(p->mapbase + TCNT);
  42. o1 = ioread8(p->mapcommon + TISRC) & p->ovf;
  43. } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
  44. || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
  45. if (likely(!o1))
  46. return v2;
  47. else
  48. return v2 + 0x10000;
  49. }
  50. static irqreturn_t timer16_interrupt(int irq, void *dev_id)
  51. {
  52. struct timer16_priv *p = (struct timer16_priv *)dev_id;
  53. bclr(p->ovf, p->mapcommon + TISRC);
  54. p->total_cycles += 0x10000;
  55. return IRQ_HANDLED;
  56. }
  57. static inline struct timer16_priv *cs_to_priv(struct clocksource *cs)
  58. {
  59. return container_of(cs, struct timer16_priv, cs);
  60. }
  61. static u64 timer16_clocksource_read(struct clocksource *cs)
  62. {
  63. struct timer16_priv *p = cs_to_priv(cs);
  64. unsigned long raw, value;
  65. value = p->total_cycles;
  66. raw = timer16_get_counter(p);
  67. return value + raw;
  68. }
  69. static int timer16_enable(struct clocksource *cs)
  70. {
  71. struct timer16_priv *p = cs_to_priv(cs);
  72. WARN_ON(p->cs_enabled);
  73. p->total_cycles = 0;
  74. iowrite16be(0x0000, p->mapbase + TCNT);
  75. iowrite8(0x83, p->mapbase + TCR);
  76. bset(p->ovie, p->mapcommon + TISRC);
  77. bset(p->enb, p->mapcommon + TSTR);
  78. p->cs_enabled = true;
  79. return 0;
  80. }
  81. static void timer16_disable(struct clocksource *cs)
  82. {
  83. struct timer16_priv *p = cs_to_priv(cs);
  84. WARN_ON(!p->cs_enabled);
  85. bclr(p->ovie, p->mapcommon + TISRC);
  86. bclr(p->enb, p->mapcommon + TSTR);
  87. p->cs_enabled = false;
  88. }
  89. static struct timer16_priv timer16_priv = {
  90. .cs = {
  91. .name = "h8300_16timer",
  92. .rating = 200,
  93. .read = timer16_clocksource_read,
  94. .enable = timer16_enable,
  95. .disable = timer16_disable,
  96. .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
  97. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  98. },
  99. };
  100. #define REG_CH 0
  101. #define REG_COMM 1
  102. static int __init h8300_16timer_init(struct device_node *node)
  103. {
  104. void __iomem *base[2];
  105. int ret, irq;
  106. unsigned int ch;
  107. struct clk *clk;
  108. clk = of_clk_get(node, 0);
  109. if (IS_ERR(clk)) {
  110. pr_err("failed to get clock for clocksource\n");
  111. return PTR_ERR(clk);
  112. }
  113. ret = -ENXIO;
  114. base[REG_CH] = of_iomap(node, 0);
  115. if (!base[REG_CH]) {
  116. pr_err("failed to map registers for clocksource\n");
  117. goto free_clk;
  118. }
  119. base[REG_COMM] = of_iomap(node, 1);
  120. if (!base[REG_COMM]) {
  121. pr_err("failed to map registers for clocksource\n");
  122. goto unmap_ch;
  123. }
  124. ret = -EINVAL;
  125. irq = irq_of_parse_and_map(node, 0);
  126. if (!irq) {
  127. pr_err("failed to get irq for clockevent\n");
  128. goto unmap_comm;
  129. }
  130. of_property_read_u32(node, "renesas,channel", &ch);
  131. timer16_priv.mapbase = base[REG_CH];
  132. timer16_priv.mapcommon = base[REG_COMM];
  133. timer16_priv.enb = ch;
  134. timer16_priv.ovf = ch;
  135. timer16_priv.ovie = 4 + ch;
  136. ret = request_irq(irq, timer16_interrupt,
  137. IRQF_TIMER, timer16_priv.cs.name, &timer16_priv);
  138. if (ret < 0) {
  139. pr_err("failed to request irq %d of clocksource\n", irq);
  140. goto unmap_comm;
  141. }
  142. clocksource_register_hz(&timer16_priv.cs,
  143. clk_get_rate(clk) / 8);
  144. return 0;
  145. unmap_comm:
  146. iounmap(base[REG_COMM]);
  147. unmap_ch:
  148. iounmap(base[REG_CH]);
  149. free_clk:
  150. clk_put(clk);
  151. return ret;
  152. }
  153. TIMER_OF_DECLARE(h8300_16bit, "renesas,16bit-timer",
  154. h8300_16timer_init);