h8300_timer16.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * H8/300 16bit Timer driver
  3. *
  4. * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp>
  5. */
  6. #include <linux/errno.h>
  7. #include <linux/kernel.h>
  8. #include <linux/param.h>
  9. #include <linux/string.h>
  10. #include <linux/slab.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/init.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/clocksource.h>
  15. #include <linux/module.h>
  16. #include <linux/clk.h>
  17. #include <linux/io.h>
  18. #include <linux/of.h>
  19. #include <asm/segment.h>
  20. #include <asm/irq.h>
  21. #define TSTR 0
  22. #define TSNC 1
  23. #define TMDR 2
  24. #define TOLR 3
  25. #define TISRA 4
  26. #define TISRB 5
  27. #define TISRC 6
  28. #define TCR 0
  29. #define TIOR 1
  30. #define TCNT 2
  31. #define GRA 4
  32. #define GRB 6
  33. #define FLAG_REPROGRAM (1 << 0)
  34. #define FLAG_SKIPEVENT (1 << 1)
  35. #define FLAG_IRQCONTEXT (1 << 2)
  36. #define FLAG_STARTED (1 << 3)
  37. #define ONESHOT 0
  38. #define PERIODIC 1
  39. #define RELATIVE 0
  40. #define ABSOLUTE 1
  41. struct timer16_priv {
  42. struct platform_device *pdev;
  43. struct clocksource cs;
  44. struct irqaction irqaction;
  45. unsigned long total_cycles;
  46. unsigned long mapbase;
  47. unsigned long mapcommon;
  48. unsigned long flags;
  49. unsigned short gra;
  50. unsigned short cs_enabled;
  51. unsigned char enb;
  52. unsigned char imfa;
  53. unsigned char imiea;
  54. unsigned char ovf;
  55. raw_spinlock_t lock;
  56. struct clk *clk;
  57. };
  58. static unsigned long timer16_get_counter(struct timer16_priv *p)
  59. {
  60. unsigned long v1, v2, v3;
  61. int o1, o2;
  62. o1 = ctrl_inb(p->mapcommon + TISRC) & p->ovf;
  63. /* Make sure the timer value is stable. Stolen from acpi_pm.c */
  64. do {
  65. o2 = o1;
  66. v1 = ctrl_inw(p->mapbase + TCNT);
  67. v2 = ctrl_inw(p->mapbase + TCNT);
  68. v3 = ctrl_inw(p->mapbase + TCNT);
  69. o1 = ctrl_inb(p->mapcommon + TISRC) & p->ovf;
  70. } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
  71. || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
  72. v2 |= 0x10000;
  73. return v2;
  74. }
  75. static irqreturn_t timer16_interrupt(int irq, void *dev_id)
  76. {
  77. struct timer16_priv *p = (struct timer16_priv *)dev_id;
  78. ctrl_outb(ctrl_inb(p->mapcommon + TISRA) & ~p->imfa,
  79. p->mapcommon + TISRA);
  80. p->total_cycles += 0x10000;
  81. return IRQ_HANDLED;
  82. }
  83. static inline struct timer16_priv *cs_to_priv(struct clocksource *cs)
  84. {
  85. return container_of(cs, struct timer16_priv, cs);
  86. }
  87. static cycle_t timer16_clocksource_read(struct clocksource *cs)
  88. {
  89. struct timer16_priv *p = cs_to_priv(cs);
  90. unsigned long flags, raw;
  91. unsigned long value;
  92. raw_spin_lock_irqsave(&p->lock, flags);
  93. value = p->total_cycles;
  94. raw = timer16_get_counter(p);
  95. raw_spin_unlock_irqrestore(&p->lock, flags);
  96. return value + raw;
  97. }
  98. static int timer16_enable(struct clocksource *cs)
  99. {
  100. struct timer16_priv *p = cs_to_priv(cs);
  101. WARN_ON(p->cs_enabled);
  102. p->total_cycles = 0;
  103. ctrl_outw(0x0000, p->mapbase + TCNT);
  104. ctrl_outb(0x83, p->mapbase + TCR);
  105. ctrl_outb(ctrl_inb(p->mapcommon + TSTR) | p->enb,
  106. p->mapcommon + TSTR);
  107. p->cs_enabled = true;
  108. return 0;
  109. }
  110. static void timer16_disable(struct clocksource *cs)
  111. {
  112. struct timer16_priv *p = cs_to_priv(cs);
  113. WARN_ON(!p->cs_enabled);
  114. ctrl_outb(ctrl_inb(p->mapcommon + TSTR) & ~p->enb,
  115. p->mapcommon + TSTR);
  116. p->cs_enabled = false;
  117. }
  118. #define REG_CH 0
  119. #define REG_COMM 1
  120. static int timer16_setup(struct timer16_priv *p, struct platform_device *pdev)
  121. {
  122. struct resource *res[2];
  123. int ret, irq;
  124. unsigned int ch;
  125. memset(p, 0, sizeof(*p));
  126. p->pdev = pdev;
  127. res[REG_CH] = platform_get_resource(p->pdev,
  128. IORESOURCE_MEM, REG_CH);
  129. res[REG_COMM] = platform_get_resource(p->pdev,
  130. IORESOURCE_MEM, REG_COMM);
  131. if (!res[REG_CH] || !res[REG_COMM]) {
  132. dev_err(&p->pdev->dev, "failed to get I/O memory\n");
  133. return -ENXIO;
  134. }
  135. irq = platform_get_irq(p->pdev, 0);
  136. if (irq < 0) {
  137. dev_err(&p->pdev->dev, "failed to get irq\n");
  138. return irq;
  139. }
  140. p->clk = clk_get(&p->pdev->dev, "fck");
  141. if (IS_ERR(p->clk)) {
  142. dev_err(&p->pdev->dev, "can't get clk\n");
  143. return PTR_ERR(p->clk);
  144. }
  145. of_property_read_u32(p->pdev->dev.of_node, "renesas,channel", &ch);
  146. p->pdev = pdev;
  147. p->mapbase = res[REG_CH]->start;
  148. p->mapcommon = res[REG_COMM]->start;
  149. p->enb = 1 << ch;
  150. p->imfa = 1 << ch;
  151. p->imiea = 1 << (4 + ch);
  152. p->cs.name = pdev->name;
  153. p->cs.rating = 200;
  154. p->cs.read = timer16_clocksource_read;
  155. p->cs.enable = timer16_enable;
  156. p->cs.disable = timer16_disable;
  157. p->cs.mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8);
  158. p->cs.flags = CLOCK_SOURCE_IS_CONTINUOUS;
  159. ret = request_irq(irq, timer16_interrupt,
  160. IRQF_TIMER, pdev->name, p);
  161. if (ret < 0) {
  162. dev_err(&p->pdev->dev, "failed to request irq %d\n", irq);
  163. return ret;
  164. }
  165. clocksource_register_hz(&p->cs, clk_get_rate(p->clk) / 8);
  166. return 0;
  167. }
  168. static int timer16_probe(struct platform_device *pdev)
  169. {
  170. struct timer16_priv *p = platform_get_drvdata(pdev);
  171. if (p) {
  172. dev_info(&pdev->dev, "kept as earlytimer\n");
  173. return 0;
  174. }
  175. p = devm_kzalloc(&pdev->dev, sizeof(*p), GFP_KERNEL);
  176. if (!p)
  177. return -ENOMEM;
  178. return timer16_setup(p, pdev);
  179. }
  180. static int timer16_remove(struct platform_device *pdev)
  181. {
  182. return -EBUSY;
  183. }
  184. static const struct of_device_id timer16_of_table[] = {
  185. { .compatible = "renesas,16bit-timer" },
  186. { }
  187. };
  188. static struct platform_driver timer16_driver = {
  189. .probe = timer16_probe,
  190. .remove = timer16_remove,
  191. .driver = {
  192. .name = "h8300h-16timer",
  193. .of_match_table = of_match_ptr(timer16_of_table),
  194. }
  195. };
  196. static int __init timer16_init(void)
  197. {
  198. return platform_driver_register(&timer16_driver);
  199. }
  200. static void __exit timer16_exit(void)
  201. {
  202. platform_driver_unregister(&timer16_driver);
  203. }
  204. subsys_initcall(timer16_init);
  205. module_exit(timer16_exit);
  206. MODULE_AUTHOR("Yoshinori Sato");
  207. MODULE_DESCRIPTION("H8/300H 16bit Timer Driver");
  208. MODULE_LICENSE("GPL v2");