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