h8300_tpu.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * H8S TPU Driver
  3. *
  4. * Copyright 2015 Yoshinori Sato <[email protected]>
  5. *
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/kernel.h>
  9. #include <linux/init.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/clk.h>
  12. #include <linux/io.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #define TCR 0x0
  17. #define TSR 0x5
  18. #define TCNT 0x6
  19. #define TCFV 0x10
  20. struct tpu_priv {
  21. struct clocksource cs;
  22. void __iomem *mapbase1;
  23. void __iomem *mapbase2;
  24. raw_spinlock_t lock;
  25. unsigned int cs_enabled;
  26. };
  27. static inline unsigned long read_tcnt32(struct tpu_priv *p)
  28. {
  29. unsigned long tcnt;
  30. tcnt = ioread16be(p->mapbase1 + TCNT) << 16;
  31. tcnt |= ioread16be(p->mapbase2 + TCNT);
  32. return tcnt;
  33. }
  34. static int tpu_get_counter(struct tpu_priv *p, unsigned long long *val)
  35. {
  36. unsigned long v1, v2, v3;
  37. int o1, o2;
  38. o1 = ioread8(p->mapbase1 + TSR) & TCFV;
  39. /* Make sure the timer value is stable. Stolen from acpi_pm.c */
  40. do {
  41. o2 = o1;
  42. v1 = read_tcnt32(p);
  43. v2 = read_tcnt32(p);
  44. v3 = read_tcnt32(p);
  45. o1 = ioread8(p->mapbase1 + TSR) & TCFV;
  46. } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3)
  47. || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2)));
  48. *val = v2;
  49. return o1;
  50. }
  51. static inline struct tpu_priv *cs_to_priv(struct clocksource *cs)
  52. {
  53. return container_of(cs, struct tpu_priv, cs);
  54. }
  55. static cycle_t tpu_clocksource_read(struct clocksource *cs)
  56. {
  57. struct tpu_priv *p = cs_to_priv(cs);
  58. unsigned long flags;
  59. unsigned long long value;
  60. raw_spin_lock_irqsave(&p->lock, flags);
  61. if (tpu_get_counter(p, &value))
  62. value += 0x100000000;
  63. raw_spin_unlock_irqrestore(&p->lock, flags);
  64. return value;
  65. }
  66. static int tpu_clocksource_enable(struct clocksource *cs)
  67. {
  68. struct tpu_priv *p = cs_to_priv(cs);
  69. WARN_ON(p->cs_enabled);
  70. iowrite16be(0, p->mapbase1 + TCNT);
  71. iowrite16be(0, p->mapbase2 + TCNT);
  72. iowrite8(0x0f, p->mapbase1 + TCR);
  73. iowrite8(0x03, p->mapbase2 + TCR);
  74. p->cs_enabled = true;
  75. return 0;
  76. }
  77. static void tpu_clocksource_disable(struct clocksource *cs)
  78. {
  79. struct tpu_priv *p = cs_to_priv(cs);
  80. WARN_ON(!p->cs_enabled);
  81. iowrite8(0, p->mapbase1 + TCR);
  82. iowrite8(0, p->mapbase2 + TCR);
  83. p->cs_enabled = false;
  84. }
  85. static struct tpu_priv tpu_priv = {
  86. .cs = {
  87. .name = "H8S_TPU",
  88. .rating = 200,
  89. .read = tpu_clocksource_read,
  90. .enable = tpu_clocksource_enable,
  91. .disable = tpu_clocksource_disable,
  92. .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8),
  93. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  94. },
  95. };
  96. #define CH_L 0
  97. #define CH_H 1
  98. static int __init h8300_tpu_init(struct device_node *node)
  99. {
  100. void __iomem *base[2];
  101. struct clk *clk;
  102. int ret = -ENXIO;
  103. clk = of_clk_get(node, 0);
  104. if (IS_ERR(clk)) {
  105. pr_err("failed to get clock for clocksource\n");
  106. return PTR_ERR(clk);
  107. }
  108. base[CH_L] = of_iomap(node, CH_L);
  109. if (!base[CH_L]) {
  110. pr_err("failed to map registers for clocksource\n");
  111. goto free_clk;
  112. }
  113. base[CH_H] = of_iomap(node, CH_H);
  114. if (!base[CH_H]) {
  115. pr_err("failed to map registers for clocksource\n");
  116. goto unmap_L;
  117. }
  118. tpu_priv.mapbase1 = base[CH_L];
  119. tpu_priv.mapbase2 = base[CH_H];
  120. return clocksource_register_hz(&tpu_priv.cs, clk_get_rate(clk) / 64);
  121. unmap_L:
  122. iounmap(base[CH_H]);
  123. free_clk:
  124. clk_put(clk);
  125. return ret;
  126. }
  127. CLOCKSOURCE_OF_DECLARE(h8300_tpu, "renesas,tpu", h8300_tpu_init);