h8300_tpu.c 3.2 KB

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