h8300_timer8.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. timer8_start(p);
  83. timer8_set_next(p, delta);
  84. }
  85. static int timer8_clock_event_shutdown(struct clock_event_device *ced)
  86. {
  87. timer8_stop(ced_to_priv(ced));
  88. return 0;
  89. }
  90. static int timer8_clock_event_periodic(struct clock_event_device *ced)
  91. {
  92. struct timer8_priv *p = ced_to_priv(ced);
  93. pr_info("%s: used for periodic clock events\n", ced->name);
  94. timer8_stop(p);
  95. timer8_clock_event_start(p, (p->rate + HZ/2) / HZ);
  96. return 0;
  97. }
  98. static int timer8_clock_event_oneshot(struct clock_event_device *ced)
  99. {
  100. struct timer8_priv *p = ced_to_priv(ced);
  101. pr_info("%s: used for oneshot clock events\n", ced->name);
  102. timer8_stop(p);
  103. timer8_clock_event_start(p, 0x10000);
  104. return 0;
  105. }
  106. static int timer8_clock_event_next(unsigned long delta,
  107. struct clock_event_device *ced)
  108. {
  109. struct timer8_priv *p = ced_to_priv(ced);
  110. BUG_ON(!clockevent_state_oneshot(ced));
  111. timer8_set_next(p, delta - 1);
  112. return 0;
  113. }
  114. static struct timer8_priv timer8_priv = {
  115. .ced = {
  116. .name = "h8300_8timer",
  117. .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,
  118. .rating = 200,
  119. .set_next_event = timer8_clock_event_next,
  120. .set_state_shutdown = timer8_clock_event_shutdown,
  121. .set_state_periodic = timer8_clock_event_periodic,
  122. .set_state_oneshot = timer8_clock_event_oneshot,
  123. },
  124. };
  125. static int __init h8300_8timer_init(struct device_node *node)
  126. {
  127. void __iomem *base;
  128. int irq, ret;
  129. struct clk *clk;
  130. clk = of_clk_get(node, 0);
  131. if (IS_ERR(clk)) {
  132. pr_err("failed to get clock for clockevent\n");
  133. return PTR_ERR(clk);
  134. }
  135. ret = ENXIO;
  136. base = of_iomap(node, 0);
  137. if (!base) {
  138. pr_err("failed to map registers for clockevent\n");
  139. goto free_clk;
  140. }
  141. ret = -EINVAL;
  142. irq = irq_of_parse_and_map(node, 0);
  143. if (!irq) {
  144. pr_err("failed to get irq for clockevent\n");
  145. goto unmap_reg;
  146. }
  147. timer8_priv.mapbase = base;
  148. timer8_priv.rate = clk_get_rate(clk) / SCALE;
  149. if (!timer8_priv.rate) {
  150. pr_err("Failed to get rate for the clocksource\n");
  151. goto unmap_reg;
  152. }
  153. if (request_irq(irq, timer8_interrupt, IRQF_TIMER,
  154. timer8_priv.ced.name, &timer8_priv) < 0) {
  155. pr_err("failed to request irq %d for clockevent\n", irq);
  156. goto unmap_reg;
  157. }
  158. clockevents_config_and_register(&timer8_priv.ced,
  159. timer8_priv.rate, 1, 0x0000ffff);
  160. return 0;
  161. unmap_reg:
  162. iounmap(base);
  163. free_clk:
  164. clk_put(clk);
  165. return ret;
  166. }
  167. TIMER_OF_DECLARE(h8300_8bit, "renesas,8bit-timer", h8300_8timer_init);