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