Merge tag 'dmaengine-fix-4.4-rc6' of git://git.infradead.org/users/vkoul/slave-dma
[deliverable/linux.git] / drivers / clocksource / tcb_clksrc.c
CommitLineData
4d243f92
DB
1#include <linux/init.h>
2#include <linux/clocksource.h>
3#include <linux/clockchips.h>
4#include <linux/interrupt.h>
5#include <linux/irq.h>
6
7#include <linux/clk.h>
8#include <linux/err.h>
9#include <linux/ioport.h>
10#include <linux/io.h>
11#include <linux/platform_device.h>
12#include <linux/atmel_tc.h>
13
14
15/*
16 * We're configured to use a specific TC block, one that's not hooked
17 * up to external hardware, to provide a time solution:
18 *
19 * - Two channels combine to create a free-running 32 bit counter
20 * with a base rate of 5+ MHz, packaged as a clocksource (with
21 * resolution better than 200 nsec).
8e315a7b
NF
22 * - Some chips support 32 bit counter. A single channel is used for
23 * this 32 bit free-running counter. the second channel is not used.
4d243f92
DB
24 *
25 * - The third channel may be used to provide a 16-bit clockevent
26 * source, used in either periodic or oneshot mode. This runs
27 * at 32 KiHZ, and can handle delays of up to two seconds.
28 *
29 * A boot clocksource and clockevent source are also currently needed,
30 * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
31 * this code can be used when init_timers() is called, well before most
32 * devices are set up. (Some low end AT91 parts, which can run uClinux,
33 * have only the timers in one TC block... they currently don't support
34 * the tclib code, because of that initialization issue.)
35 *
36 * REVISIT behavior during system suspend states... we should disable
37 * all clocks and save the power. Easily done for clockevent devices,
38 * but clocksources won't necessarily get the needed notifications.
39 * For deeper system sleep states, this will be mandatory...
40 */
41
42static void __iomem *tcaddr;
43
8e19608e 44static cycle_t tc_get_cycles(struct clocksource *cs)
4d243f92
DB
45{
46 unsigned long flags;
47 u32 lower, upper;
48
49 raw_local_irq_save(flags);
50 do {
51 upper = __raw_readl(tcaddr + ATMEL_TC_REG(1, CV));
52 lower = __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));
53 } while (upper != __raw_readl(tcaddr + ATMEL_TC_REG(1, CV)));
54
55 raw_local_irq_restore(flags);
56 return (upper << 16) | lower;
57}
58
8e315a7b
NF
59static cycle_t tc_get_cycles32(struct clocksource *cs)
60{
61 return __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));
62}
63
4d243f92
DB
64static struct clocksource clksrc = {
65 .name = "tcb_clksrc",
66 .rating = 200,
67 .read = tc_get_cycles,
68 .mask = CLOCKSOURCE_MASK(32),
4d243f92
DB
69 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
70};
71
72#ifdef CONFIG_GENERIC_CLOCKEVENTS
73
74struct tc_clkevt_device {
75 struct clock_event_device clkevt;
76 struct clk *clk;
77 void __iomem *regs;
78};
79
80static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
81{
82 return container_of(clkevt, struct tc_clkevt_device, clkevt);
83}
84
85/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
86 * because using one of the divided clocks would usually mean the
87 * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
88 *
89 * A divided clock could be good for high resolution timers, since
90 * 30.5 usec resolution can seem "low".
91 */
92static u32 timer_clock;
93
cf4541c1 94static int tc_shutdown(struct clock_event_device *d)
4d243f92
DB
95{
96 struct tc_clkevt_device *tcd = to_tc_clkevt(d);
97 void __iomem *regs = tcd->regs;
98
cf4541c1
VK
99 __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
100 __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
101 clk_disable(tcd->clk);
4d243f92 102
cf4541c1
VK
103 return 0;
104}
4d243f92 105
cf4541c1
VK
106static int tc_set_oneshot(struct clock_event_device *d)
107{
108 struct tc_clkevt_device *tcd = to_tc_clkevt(d);
109 void __iomem *regs = tcd->regs;
4d243f92 110
cf4541c1
VK
111 if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
112 tc_shutdown(d);
4d243f92 113
cf4541c1 114 clk_enable(tcd->clk);
4d243f92 115
cf4541c1
VK
116 /* slow clock, count up to RC, then irq and stop */
117 __raw_writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
118 ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
119 __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
4d243f92 120
cf4541c1
VK
121 /* set_next_event() configures and starts the timer */
122 return 0;
123}
4d243f92 124
cf4541c1
VK
125static int tc_set_periodic(struct clock_event_device *d)
126{
127 struct tc_clkevt_device *tcd = to_tc_clkevt(d);
128 void __iomem *regs = tcd->regs;
4d243f92 129
cf4541c1
VK
130 if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
131 tc_shutdown(d);
4d243f92 132
cf4541c1
VK
133 /* By not making the gentime core emulate periodic mode on top
134 * of oneshot, we get lower overhead and improved accuracy.
135 */
136 clk_enable(tcd->clk);
137
138 /* slow clock, count up to RC, then irq and restart */
139 __raw_writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
140 regs + ATMEL_TC_REG(2, CMR));
141 __raw_writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
142
143 /* Enable clock and interrupts on RC compare */
144 __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
145
146 /* go go gadget! */
147 __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG, regs +
148 ATMEL_TC_REG(2, CCR));
149 return 0;
4d243f92
DB
150}
151
152static int tc_next_event(unsigned long delta, struct clock_event_device *d)
153{
154 __raw_writel(delta, tcaddr + ATMEL_TC_REG(2, RC));
155
156 /* go go gadget! */
157 __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
158 tcaddr + ATMEL_TC_REG(2, CCR));
159 return 0;
160}
161
162static struct tc_clkevt_device clkevt = {
163 .clkevt = {
cf4541c1
VK
164 .name = "tc_clkevt",
165 .features = CLOCK_EVT_FEAT_PERIODIC |
166 CLOCK_EVT_FEAT_ONESHOT,
4d243f92 167 /* Should be lower than at91rm9200's system timer */
cf4541c1
VK
168 .rating = 125,
169 .set_next_event = tc_next_event,
170 .set_state_shutdown = tc_shutdown,
171 .set_state_periodic = tc_set_periodic,
172 .set_state_oneshot = tc_set_oneshot,
4d243f92
DB
173 },
174};
175
176static irqreturn_t ch2_irq(int irq, void *handle)
177{
178 struct tc_clkevt_device *dev = handle;
179 unsigned int sr;
180
181 sr = __raw_readl(dev->regs + ATMEL_TC_REG(2, SR));
182 if (sr & ATMEL_TC_CPCS) {
183 dev->clkevt.event_handler(&dev->clkevt);
184 return IRQ_HANDLED;
185 }
186
187 return IRQ_NONE;
188}
189
5b3c11da 190static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
4d243f92 191{
5b3c11da 192 int ret;
4d243f92
DB
193 struct clk *t2_clk = tc->clk[2];
194 int irq = tc->irq[2];
195
7d8d05d1
BB
196 ret = clk_prepare_enable(tc->slow_clk);
197 if (ret)
198 return ret;
199
5b3c11da
BB
200 /* try to enable t2 clk to avoid future errors in mode change */
201 ret = clk_prepare_enable(t2_clk);
7d8d05d1
BB
202 if (ret) {
203 clk_disable_unprepare(tc->slow_clk);
5b3c11da 204 return ret;
7d8d05d1
BB
205 }
206
acbf6d21 207 clk_disable(t2_clk);
5b3c11da 208
4d243f92
DB
209 clkevt.regs = tc->regs;
210 clkevt.clk = t2_clk;
4d243f92
DB
211
212 timer_clock = clk32k_divisor_idx;
213
320ab2b0 214 clkevt.clkevt.cpumask = cpumask_of(0);
4d243f92 215
d07a1ecd
GP
216 ret = request_irq(irq, ch2_irq, IRQF_TIMER, "tc_clkevt", &clkevt);
217 if (ret) {
eed9fb9d 218 clk_unprepare(t2_clk);
7d8d05d1 219 clk_disable_unprepare(tc->slow_clk);
5b3c11da 220 return ret;
d07a1ecd 221 }
5b3c11da 222
77cc982f 223 clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
1817dc03 224
5b3c11da 225 return ret;
4d243f92
DB
226}
227
228#else /* !CONFIG_GENERIC_CLOCKEVENTS */
229
5b3c11da 230static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
4d243f92
DB
231{
232 /* NOTHING */
5b3c11da 233 return 0;
4d243f92
DB
234}
235
236#endif
237
8e315a7b
NF
238static void __init tcb_setup_dual_chan(struct atmel_tc *tc, int mck_divisor_idx)
239{
240 /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */
241 __raw_writel(mck_divisor_idx /* likely divide-by-8 */
242 | ATMEL_TC_WAVE
243 | ATMEL_TC_WAVESEL_UP /* free-run */
244 | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */
245 | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */
246 tcaddr + ATMEL_TC_REG(0, CMR));
247 __raw_writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));
248 __raw_writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));
249 __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
250 __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
251
252 /* channel 1: waveform mode, input TIOA0 */
253 __raw_writel(ATMEL_TC_XC1 /* input: TIOA0 */
254 | ATMEL_TC_WAVE
255 | ATMEL_TC_WAVESEL_UP, /* free-run */
256 tcaddr + ATMEL_TC_REG(1, CMR));
257 __raw_writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */
258 __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));
259
260 /* chain channel 0 to channel 1*/
261 __raw_writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);
262 /* then reset all the timers */
263 __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
264}
265
266static void __init tcb_setup_single_chan(struct atmel_tc *tc, int mck_divisor_idx)
267{
268 /* channel 0: waveform mode, input mclk/8 */
269 __raw_writel(mck_divisor_idx /* likely divide-by-8 */
270 | ATMEL_TC_WAVE
271 | ATMEL_TC_WAVESEL_UP, /* free-run */
272 tcaddr + ATMEL_TC_REG(0, CMR));
273 __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
274 __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
275
276 /* then reset all the timers */
277 __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
278}
279
4d243f92
DB
280static int __init tcb_clksrc_init(void)
281{
282 static char bootinfo[] __initdata
283 = KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";
284
285 struct platform_device *pdev;
286 struct atmel_tc *tc;
3ee08aea 287 struct clk *t0_clk;
4d243f92
DB
288 u32 rate, divided_rate = 0;
289 int best_divisor_idx = -1;
290 int clk32k_divisor_idx = -1;
291 int i;
0e746ec5 292 int ret;
4d243f92 293
4930d247 294 tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK);
4d243f92
DB
295 if (!tc) {
296 pr_debug("can't alloc TC for clocksource\n");
297 return -ENODEV;
298 }
299 tcaddr = tc->regs;
300 pdev = tc->pdev;
301
302 t0_clk = tc->clk[0];
0e746ec5
BB
303 ret = clk_prepare_enable(t0_clk);
304 if (ret) {
305 pr_debug("can't enable T0 clk\n");
306 goto err_free_tc;
307 }
4d243f92
DB
308
309 /* How fast will we be counting? Pick something over 5 MHz. */
310 rate = (u32) clk_get_rate(t0_clk);
311 for (i = 0; i < 5; i++) {
312 unsigned divisor = atmel_tc_divisors[i];
313 unsigned tmp;
314
315 /* remember 32 KiHz clock for later */
316 if (!divisor) {
317 clk32k_divisor_idx = i;
318 continue;
319 }
320
321 tmp = rate / divisor;
322 pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);
323 if (best_divisor_idx > 0) {
324 if (tmp < 5 * 1000 * 1000)
325 continue;
326 }
327 divided_rate = tmp;
328 best_divisor_idx = i;
329 }
330
4d243f92
DB
331
332 printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,
333 divided_rate / 1000000,
334 ((divided_rate + 500000) % 1000000) / 1000);
335
8e315a7b
NF
336 if (tc->tcb_config && tc->tcb_config->counter_width == 32) {
337 /* use apropriate function to read 32 bit counter */
338 clksrc.read = tc_get_cycles32;
339 /* setup ony channel 0 */
340 tcb_setup_single_chan(tc, best_divisor_idx);
341 } else {
342 /* tclib will give us three clocks no matter what the
343 * underlying platform supports.
344 */
0e746ec5
BB
345 ret = clk_prepare_enable(tc->clk[1]);
346 if (ret) {
347 pr_debug("can't enable T1 clk\n");
348 goto err_disable_t0;
349 }
8e315a7b
NF
350 /* setup both channel 0 & 1 */
351 tcb_setup_dual_chan(tc, best_divisor_idx);
352 }
4d243f92
DB
353
354 /* and away we go! */
5b3c11da
BB
355 ret = clocksource_register_hz(&clksrc, divided_rate);
356 if (ret)
357 goto err_disable_t1;
4d243f92
DB
358
359 /* channel 2: periodic and oneshot timer support */
5b3c11da
BB
360 ret = setup_clkevents(tc, clk32k_divisor_idx);
361 if (ret)
362 goto err_unregister_clksrc;
4d243f92
DB
363
364 return 0;
0e746ec5 365
5b3c11da
BB
366err_unregister_clksrc:
367 clocksource_unregister(&clksrc);
368
369err_disable_t1:
370 if (!tc->tcb_config || tc->tcb_config->counter_width != 32)
371 clk_disable_unprepare(tc->clk[1]);
372
0e746ec5
BB
373err_disable_t0:
374 clk_disable_unprepare(t0_clk);
375
376err_free_tc:
377 atmel_tc_free(tc);
378 return ret;
4d243f92
DB
379}
380arch_initcall(tcb_clksrc_init);
This page took 0.602361 seconds and 5 git commands to generate.