h8300_timer8.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * linux/arch/h8300/kernel/cpu/timer/timer8.c
  3. *
  4. * Yoshinori Sato <ysato@users.sourcefoge.jp>
  5. *
  6. * 8bit Timer driver
  7. *
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/init.h>
  13. #include <linux/clockchips.h>
  14. #include <linux/clk.h>
  15. #include <linux/io.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_irq.h>
  19. #define _8TCR 0
  20. #define _8TCSR 2
  21. #define TCORA 4
  22. #define TCORB 6
  23. #define _8TCNT 8
  24. #define CMIEA 6
  25. #define CMFA 6
  26. #define FLAG_STARTED (1 << 3)
  27. #define SCALE 64
  28. #define bset(b, a) iowrite8(ioread8(a) | (1 << (b)), (a))
  29. #define bclr(b, a) iowrite8(ioread8(a) & ~(1 << (b)), (a))
  30. struct timer8_priv {
  31. struct clock_event_device ced;
  32. void __iomem *mapbase;
  33. unsigned long flags;
  34. unsigned int rate;
  35. };
  36. static irqreturn_t timer8_interrupt(int irq, void *dev_id)
  37. {
  38. struct timer8_priv *p = dev_id;
  39. if (clockevent_state_oneshot(&p->ced))
  40. iowrite16be(0x0000, p->mapbase + _8TCR);
  41. p->ced.event_handler(&p->ced);
  42. bclr(CMFA, p->mapbase + _8TCSR);
  43. return IRQ_HANDLED;
  44. }
  45. static void timer8_set_next(struct timer8_priv *p, unsigned long delta)
  46. {
  47. if (delta >= 0x10000)
  48. pr_warn("delta out of range\n");
  49. bclr(CMIEA, p->mapbase + _8TCR);
  50. iowrite16be(delta, p->mapbase + TCORA);
  51. iowrite16be(0x0000, p->mapbase + _8TCNT);
  52. bclr(CMFA, p->mapbase + _8TCSR);
  53. bset(CMIEA, p->mapbase + _8TCR);
  54. }
  55. static int timer8_enable(struct timer8_priv *p)
  56. {
  57. iowrite16be(0xffff, p->mapbase + TCORA);
  58. iowrite16be(0x0000, p->mapbase + _8TCNT);
  59. iowrite16be(0x0c02, p->mapbase + _8TCR);
  60. return 0;
  61. }
  62. static int timer8_start(struct timer8_priv *p)
  63. {
  64. int ret;
  65. if ((p->flags & FLAG_STARTED))
  66. return 0;
  67. ret = timer8_enable(p);
  68. if (!ret)
  69. p->flags |= FLAG_STARTED;
  70. return ret;
  71. }
  72. static void timer8_stop(struct timer8_priv *p)
  73. {
  74. iowrite16be(0x0000, p->mapbase + _8TCR);
  75. }
  76. static inline struct timer8_priv *ced_to_priv(struct clock_event_device *ced)
  77. {
  78. return container_of(ced, struct timer8_priv, ced);
  79. }
  80. static void timer8_clock_event_start(struct timer8_priv *p, unsigned long delta)
  81. {
  82. struct clock_event_device *ced = &p->ced;
  83. timer8_start(p);
  84. ced->shift = 32;
  85. ced->mult = div_sc(p->rate, NSEC_PER_SEC, ced->shift);
  86. ced->max_delta_ns = clockevent_delta2ns(0xffff, ced);
  87. ced->min_delta_ns = clockevent_delta2ns(0x0001, ced);
  88. timer8_set_next(p, delta);
  89. }
  90. static int timer8_clock_event_shutdown(struct clock_event_device *ced)
  91. {
  92. timer8_stop(ced_to_priv(ced));
  93. return 0;
  94. }
  95. static int timer8_clock_event_periodic(struct clock_event_device *ced)
  96. {
  97. struct timer8_priv *p = ced_to_priv(ced);
  98. pr_info("%s: used for periodic clock events\n", ced->name);
  99. timer8_stop(p);
  100. timer8_clock_event_start(p, (p->rate + HZ/2) / HZ);
  101. return 0;
  102. }
  103. static int timer8_clock_event_oneshot(struct clock_event_device *ced)
  104. {
  105. struct timer8_priv *p = ced_to_priv(ced);
  106. pr_info("%s: used for oneshot clock events\n", ced->name);
  107. timer8_stop(p);
  108. timer8_clock_event_start(p, 0x10000);
  109. return 0;
  110. }
  111. static int timer8_clock_event_next(unsigned long delta,
  112. struct clock_event_device *ced)
  113. {
  114. struct timer8_priv *p = ced_to_priv(ced);
  115. BUG_ON(!clockevent_state_oneshot(ced));
  116. timer8_set_next(p, delta - 1);
  117. return 0;
  118. }
  119. static struct timer8_priv timer8_priv = {
  120. .ced = {
  121. .name = "h8300_8timer",
  122. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  123. .rating = 200,
  124. .set_next_event = timer8_clock_event_next,
  125. .set_state_shutdown = timer8_clock_event_shutdown,
  126. .set_state_periodic = timer8_clock_event_periodic,
  127. .set_state_oneshot = timer8_clock_event_oneshot,
  128. },
  129. };
  130. static int __init h8300_8timer_init(struct device_node *node)
  131. {
  132. void __iomem *base;
  133. int irq, ret;
  134. struct clk *clk;
  135. clk = of_clk_get(node, 0);
  136. if (IS_ERR(clk)) {
  137. pr_err("failed to get clock for clockevent\n");
  138. return PTR_ERR(clk);
  139. }
  140. ret = ENXIO;
  141. base = of_iomap(node, 0);
  142. if (!base) {
  143. pr_err("failed to map registers for clockevent\n");
  144. goto free_clk;
  145. }
  146. ret = -EINVAL;
  147. irq = irq_of_parse_and_map(node, 0);
  148. if (!irq) {
  149. pr_err("failed to get irq for clockevent\n");
  150. goto unmap_reg;
  151. }
  152. timer8_priv.mapbase = base;
  153. timer8_priv.rate = clk_get_rate(clk) / SCALE;
  154. if (!timer8_priv.rate) {
  155. pr_err("Failed to get rate for the clocksource\n");
  156. goto unmap_reg;
  157. }
  158. if (request_irq(irq, timer8_interrupt, IRQF_TIMER,
  159. timer8_priv.ced.name, &timer8_priv) < 0) {
  160. pr_err("failed to request irq %d for clockevent\n", irq);
  161. goto unmap_reg;
  162. }
  163. clockevents_config_and_register(&timer8_priv.ced,
  164. timer8_priv.rate, 1, 0x0000ffff);
  165. return 0;
  166. unmap_reg:
  167. iounmap(base);
  168. free_clk:
  169. clk_put(clk);
  170. return ret;
  171. }
  172. CLOCKSOURCE_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init);