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