Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * H8/300 16bit Timer driver |
| 3 | * |
| 4 | * Copyright 2015 Yoshinori Sato <ysato@users.sourcefoge.jp> |
| 5 | */ |
| 6 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 7 | #include <linux/interrupt.h> |
| 8 | #include <linux/init.h> |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 9 | #include <linux/clocksource.h> |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 10 | #include <linux/clk.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/of.h> |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 13 | #include <linux/of_address.h> |
| 14 | #include <linux/of_irq.h> |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 15 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 16 | #define TSTR 0 |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 17 | #define TISRC 6 |
| 18 | |
| 19 | #define TCR 0 |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 20 | #define TCNT 2 |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 21 | |
| 22 | struct timer16_priv { |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 23 | struct clocksource cs; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 24 | unsigned long total_cycles; |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame] | 25 | void __iomem *mapbase; |
| 26 | void __iomem *mapcommon; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 27 | unsigned short cs_enabled; |
| 28 | unsigned char enb; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 29 | unsigned char ovf; |
Yoshinori Sato | 2a0ff877 | 2015-12-05 02:48:14 +0900 | [diff] [blame^] | 30 | unsigned char ovie; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 31 | struct clk *clk; |
| 32 | }; |
| 33 | |
| 34 | static unsigned long timer16_get_counter(struct timer16_priv *p) |
| 35 | { |
| 36 | unsigned long v1, v2, v3; |
| 37 | int o1, o2; |
| 38 | |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame] | 39 | o1 = readb(p->mapcommon + TISRC) & p->ovf; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 40 | |
| 41 | /* Make sure the timer value is stable. Stolen from acpi_pm.c */ |
| 42 | do { |
| 43 | o2 = o1; |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame] | 44 | v1 = readw(p->mapbase + TCNT); |
| 45 | v2 = readw(p->mapbase + TCNT); |
| 46 | v3 = readw(p->mapbase + TCNT); |
| 47 | o1 = readb(p->mapcommon + TISRC) & p->ovf; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 48 | } while (unlikely((o1 != o2) || (v1 > v2 && v1 < v3) |
| 49 | || (v2 > v3 && v2 < v1) || (v3 > v1 && v3 < v2))); |
| 50 | |
| 51 | v2 |= 0x10000; |
| 52 | return v2; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | static irqreturn_t timer16_interrupt(int irq, void *dev_id) |
| 57 | { |
| 58 | struct timer16_priv *p = (struct timer16_priv *)dev_id; |
| 59 | |
Yoshinori Sato | 2a0ff877 | 2015-12-05 02:48:14 +0900 | [diff] [blame^] | 60 | writeb(readb(p->mapcommon + TISRC) & ~p->ovf, |
| 61 | p->mapcommon + TISRC); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 62 | p->total_cycles += 0x10000; |
| 63 | |
| 64 | return IRQ_HANDLED; |
| 65 | } |
| 66 | |
| 67 | static inline struct timer16_priv *cs_to_priv(struct clocksource *cs) |
| 68 | { |
| 69 | return container_of(cs, struct timer16_priv, cs); |
| 70 | } |
| 71 | |
| 72 | static cycle_t timer16_clocksource_read(struct clocksource *cs) |
| 73 | { |
| 74 | struct timer16_priv *p = cs_to_priv(cs); |
Daniel Lezcano | 05de7ed | 2015-11-09 10:55:30 +0100 | [diff] [blame] | 75 | unsigned long raw, value; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 76 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 77 | value = p->total_cycles; |
| 78 | raw = timer16_get_counter(p); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 79 | |
| 80 | return value + raw; |
| 81 | } |
| 82 | |
| 83 | static int timer16_enable(struct clocksource *cs) |
| 84 | { |
| 85 | struct timer16_priv *p = cs_to_priv(cs); |
| 86 | |
| 87 | WARN_ON(p->cs_enabled); |
| 88 | |
| 89 | p->total_cycles = 0; |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame] | 90 | writew(0x0000, p->mapbase + TCNT); |
| 91 | writeb(0x83, p->mapbase + TCR); |
| 92 | writeb(readb(p->mapcommon + TSTR) | p->enb, |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 93 | p->mapcommon + TSTR); |
Yoshinori Sato | 2a0ff877 | 2015-12-05 02:48:14 +0900 | [diff] [blame^] | 94 | writeb(readb(p->mapcommon + TISRC) | p->ovie, |
| 95 | p->mapcommon + TSTR); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 96 | |
| 97 | p->cs_enabled = true; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static void timer16_disable(struct clocksource *cs) |
| 102 | { |
| 103 | struct timer16_priv *p = cs_to_priv(cs); |
| 104 | |
| 105 | WARN_ON(!p->cs_enabled); |
| 106 | |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame] | 107 | writeb(readb(p->mapcommon + TSTR) & ~p->enb, |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 108 | p->mapcommon + TSTR); |
| 109 | |
| 110 | p->cs_enabled = false; |
| 111 | } |
| 112 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 113 | static struct timer16_priv timer16_priv = { |
| 114 | .cs = { |
| 115 | .name = "h8300_16timer", |
| 116 | .rating = 200, |
| 117 | .read = timer16_clocksource_read, |
| 118 | .enable = timer16_enable, |
| 119 | .disable = timer16_disable, |
| 120 | .mask = CLOCKSOURCE_MASK(sizeof(unsigned long) * 8), |
| 121 | .flags = CLOCK_SOURCE_IS_CONTINUOUS, |
| 122 | }, |
| 123 | }; |
| 124 | |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 125 | #define REG_CH 0 |
| 126 | #define REG_COMM 1 |
| 127 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 128 | static void __init h8300_16timer_init(struct device_node *node) |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 129 | { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 130 | void __iomem *base[2]; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 131 | int ret, irq; |
| 132 | unsigned int ch; |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 133 | struct clk *clk; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 134 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 135 | clk = of_clk_get(node, 0); |
| 136 | if (IS_ERR(clk)) { |
| 137 | pr_err("failed to get clock for clocksource\n"); |
| 138 | return; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 139 | } |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 140 | |
| 141 | base[REG_CH] = of_iomap(node, 0); |
| 142 | if (!base[REG_CH]) { |
| 143 | pr_err("failed to map registers for clocksource\n"); |
| 144 | goto free_clk; |
| 145 | } |
| 146 | |
| 147 | base[REG_COMM] = of_iomap(node, 1); |
| 148 | if (!base[REG_COMM]) { |
| 149 | pr_err("failed to map registers for clocksource\n"); |
| 150 | goto unmap_ch; |
| 151 | } |
| 152 | |
| 153 | irq = irq_of_parse_and_map(node, 0); |
Daniel Lezcano | 5019c90 | 2015-11-09 10:52:35 +0100 | [diff] [blame] | 154 | if (!irq) { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 155 | pr_err("failed to get irq for clockevent\n"); |
| 156 | goto unmap_comm; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 157 | } |
| 158 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 159 | of_property_read_u32(node, "renesas,channel", &ch); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 160 | |
Daniel Lezcano | 7516051 | 2015-11-08 22:55:12 +0100 | [diff] [blame] | 161 | timer16_priv.mapbase = base[REG_CH]; |
| 162 | timer16_priv.mapcommon = base[REG_COMM]; |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 163 | timer16_priv.enb = 1 << ch; |
Yoshinori Sato | 2a0ff877 | 2015-12-05 02:48:14 +0900 | [diff] [blame^] | 164 | timer16_priv.ovf = 1 << ch; |
| 165 | timer16_priv.ovie = 1 << (4 + ch); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 166 | |
| 167 | ret = request_irq(irq, timer16_interrupt, |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 168 | IRQF_TIMER, timer16_priv.cs.name, &timer16_priv); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 169 | if (ret < 0) { |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 170 | pr_err("failed to request irq %d of clocksource\n", irq); |
| 171 | goto unmap_comm; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 172 | } |
| 173 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 174 | clocksource_register_hz(&timer16_priv.cs, |
| 175 | clk_get_rate(timer16_priv.clk) / 8); |
| 176 | return; |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 177 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 178 | unmap_comm: |
| 179 | iounmap(base[REG_COMM]); |
| 180 | unmap_ch: |
| 181 | iounmap(base[REG_CH]); |
| 182 | free_clk: |
| 183 | clk_put(clk); |
Yoshinori Sato | 618b902 | 2015-01-28 02:52:42 +0900 | [diff] [blame] | 184 | } |
| 185 | |
Yoshinori Sato | 4633f4c | 2015-11-07 01:31:44 +0900 | [diff] [blame] | 186 | CLOCKSOURCE_OF_DECLARE(h8300_16bit, "renesas,16bit-timer", h8300_16timer_init); |