blob: 6ed31f9def7eb2aff31441e203ddd7b85bd1bd8a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
David Brownell4d243f92008-02-22 17:28:37 -08002#include <linux/init.h>
3#include <linux/clocksource.h>
4#include <linux/clockchips.h>
5#include <linux/interrupt.h>
6#include <linux/irq.h>
7
8#include <linux/clk.h>
9#include <linux/err.h>
10#include <linux/ioport.h>
11#include <linux/io.h>
Alexandre Belloni86232bf2019-04-26 23:47:11 +020012#include <linux/of_address.h>
13#include <linux/of_irq.h>
Alexandre Bellonif712a1e2019-04-26 23:47:12 +020014#include <linux/sched_clock.h>
Alexandre Belloni2a515e52017-05-12 20:22:51 +020015#include <linux/syscore_ops.h>
Alexandre Bellonic2c91362019-04-26 23:47:10 +020016#include <soc/at91/atmel_tcb.h>
David Brownell4d243f92008-02-22 17:28:37 -080017
18
19/*
20 * We're configured to use a specific TC block, one that's not hooked
21 * up to external hardware, to provide a time solution:
22 *
23 * - Two channels combine to create a free-running 32 bit counter
24 * with a base rate of 5+ MHz, packaged as a clocksource (with
25 * resolution better than 200 nsec).
Nicolas Ferre8e315a72012-01-19 18:44:49 +010026 * - Some chips support 32 bit counter. A single channel is used for
27 * this 32 bit free-running counter. the second channel is not used.
David Brownell4d243f92008-02-22 17:28:37 -080028 *
29 * - The third channel may be used to provide a 16-bit clockevent
30 * source, used in either periodic or oneshot mode. This runs
31 * at 32 KiHZ, and can handle delays of up to two seconds.
32 *
David Brownell4d243f92008-02-22 17:28:37 -080033 * REVISIT behavior during system suspend states... we should disable
34 * all clocks and save the power. Easily done for clockevent devices,
35 * but clocksources won't necessarily get the needed notifications.
36 * For deeper system sleep states, this will be mandatory...
37 */
38
39static void __iomem *tcaddr;
Alexandre Belloni2a515e52017-05-12 20:22:51 +020040static struct
41{
42 u32 cmr;
43 u32 imr;
44 u32 rc;
45 bool clken;
46} tcb_cache[3];
47static u32 bmr_cache;
David Brownell4d243f92008-02-22 17:28:37 -080048
Thomas Gleixnera5a1d1c2016-12-21 20:32:01 +010049static u64 tc_get_cycles(struct clocksource *cs)
David Brownell4d243f92008-02-22 17:28:37 -080050{
51 unsigned long flags;
52 u32 lower, upper;
53
54 raw_local_irq_save(flags);
55 do {
Alexandre Belloni6ec8be22017-06-23 17:03:31 +020056 upper = readl_relaxed(tcaddr + ATMEL_TC_REG(1, CV));
57 lower = readl_relaxed(tcaddr + ATMEL_TC_REG(0, CV));
58 } while (upper != readl_relaxed(tcaddr + ATMEL_TC_REG(1, CV)));
David Brownell4d243f92008-02-22 17:28:37 -080059
60 raw_local_irq_restore(flags);
61 return (upper << 16) | lower;
62}
63
David Engraf7b9f1d12017-01-11 14:50:59 +010064static u64 tc_get_cycles32(struct clocksource *cs)
65{
Alexandre Belloni6ec8be22017-06-23 17:03:31 +020066 return readl_relaxed(tcaddr + ATMEL_TC_REG(0, CV));
David Engraf7b9f1d12017-01-11 14:50:59 +010067}
68
kbuild test robot7ebe6812019-04-26 23:47:17 +020069static void tc_clksrc_suspend(struct clocksource *cs)
Alexandre Belloni2a515e52017-05-12 20:22:51 +020070{
71 int i;
72
73 for (i = 0; i < ARRAY_SIZE(tcb_cache); i++) {
74 tcb_cache[i].cmr = readl(tcaddr + ATMEL_TC_REG(i, CMR));
75 tcb_cache[i].imr = readl(tcaddr + ATMEL_TC_REG(i, IMR));
76 tcb_cache[i].rc = readl(tcaddr + ATMEL_TC_REG(i, RC));
77 tcb_cache[i].clken = !!(readl(tcaddr + ATMEL_TC_REG(i, SR)) &
78 ATMEL_TC_CLKSTA);
79 }
80
81 bmr_cache = readl(tcaddr + ATMEL_TC_BMR);
82}
83
kbuild test robot7ebe6812019-04-26 23:47:17 +020084static void tc_clksrc_resume(struct clocksource *cs)
Alexandre Belloni2a515e52017-05-12 20:22:51 +020085{
86 int i;
87
88 for (i = 0; i < ARRAY_SIZE(tcb_cache); i++) {
89 /* Restore registers for the channel, RA and RB are not used */
90 writel(tcb_cache[i].cmr, tcaddr + ATMEL_TC_REG(i, CMR));
91 writel(tcb_cache[i].rc, tcaddr + ATMEL_TC_REG(i, RC));
92 writel(0, tcaddr + ATMEL_TC_REG(i, RA));
93 writel(0, tcaddr + ATMEL_TC_REG(i, RB));
94 /* Disable all the interrupts */
95 writel(0xff, tcaddr + ATMEL_TC_REG(i, IDR));
96 /* Reenable interrupts that were enabled before suspending */
97 writel(tcb_cache[i].imr, tcaddr + ATMEL_TC_REG(i, IER));
98 /* Start the clock if it was used */
99 if (tcb_cache[i].clken)
100 writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(i, CCR));
101 }
102
103 /* Dual channel, chain channels */
104 writel(bmr_cache, tcaddr + ATMEL_TC_BMR);
105 /* Finally, trigger all the channels*/
106 writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
107}
108
David Brownell4d243f92008-02-22 17:28:37 -0800109static struct clocksource clksrc = {
David Brownell4d243f92008-02-22 17:28:37 -0800110 .rating = 200,
111 .read = tc_get_cycles,
112 .mask = CLOCKSOURCE_MASK(32),
David Brownell4d243f92008-02-22 17:28:37 -0800113 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
Alexandre Belloni2a515e52017-05-12 20:22:51 +0200114 .suspend = tc_clksrc_suspend,
115 .resume = tc_clksrc_resume,
David Brownell4d243f92008-02-22 17:28:37 -0800116};
117
Alexandre Bellonif712a1e2019-04-26 23:47:12 +0200118static u64 notrace tc_sched_clock_read(void)
119{
120 return tc_get_cycles(&clksrc);
121}
122
123static u64 notrace tc_sched_clock_read32(void)
124{
125 return tc_get_cycles32(&clksrc);
126}
127
David Brownell4d243f92008-02-22 17:28:37 -0800128#ifdef CONFIG_GENERIC_CLOCKEVENTS
129
130struct tc_clkevt_device {
131 struct clock_event_device clkevt;
132 struct clk *clk;
133 void __iomem *regs;
134};
135
136static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
137{
138 return container_of(clkevt, struct tc_clkevt_device, clkevt);
139}
140
141/* For now, we always use the 32K clock ... this optimizes for NO_HZ,
142 * because using one of the divided clocks would usually mean the
143 * tick rate can never be less than several dozen Hz (vs 0.5 Hz).
144 *
145 * A divided clock could be good for high resolution timers, since
146 * 30.5 usec resolution can seem "low".
147 */
148static u32 timer_clock;
149
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530150static int tc_shutdown(struct clock_event_device *d)
David Brownell4d243f92008-02-22 17:28:37 -0800151{
152 struct tc_clkevt_device *tcd = to_tc_clkevt(d);
153 void __iomem *regs = tcd->regs;
154
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200155 writel(0xff, regs + ATMEL_TC_REG(2, IDR));
156 writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
Alexandre Bellonif02b4b72016-01-15 11:34:21 +0100157 if (!clockevent_state_detached(d))
158 clk_disable(tcd->clk);
David Brownell4d243f92008-02-22 17:28:37 -0800159
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530160 return 0;
161}
162
163static int tc_set_oneshot(struct clock_event_device *d)
164{
165 struct tc_clkevt_device *tcd = to_tc_clkevt(d);
166 void __iomem *regs = tcd->regs;
167
168 if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
169 tc_shutdown(d);
170
171 clk_enable(tcd->clk);
172
173 /* slow clock, count up to RC, then irq and stop */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200174 writel(timer_clock | ATMEL_TC_CPCSTOP | ATMEL_TC_WAVE |
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530175 ATMEL_TC_WAVESEL_UP_AUTO, regs + ATMEL_TC_REG(2, CMR));
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200176 writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530177
178 /* set_next_event() configures and starts the timer */
179 return 0;
180}
181
182static int tc_set_periodic(struct clock_event_device *d)
183{
184 struct tc_clkevt_device *tcd = to_tc_clkevt(d);
185 void __iomem *regs = tcd->regs;
186
187 if (clockevent_state_oneshot(d) || clockevent_state_periodic(d))
188 tc_shutdown(d);
David Brownell4d243f92008-02-22 17:28:37 -0800189
190 /* By not making the gentime core emulate periodic mode on top
191 * of oneshot, we get lower overhead and improved accuracy.
192 */
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530193 clk_enable(tcd->clk);
David Brownell4d243f92008-02-22 17:28:37 -0800194
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530195 /* slow clock, count up to RC, then irq and restart */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200196 writel(timer_clock | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530197 regs + ATMEL_TC_REG(2, CMR));
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200198 writel((32768 + HZ / 2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));
David Brownell4d243f92008-02-22 17:28:37 -0800199
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530200 /* Enable clock and interrupts on RC compare */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200201 writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
David Brownell4d243f92008-02-22 17:28:37 -0800202
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530203 /* go go gadget! */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200204 writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG, regs +
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530205 ATMEL_TC_REG(2, CCR));
206 return 0;
David Brownell4d243f92008-02-22 17:28:37 -0800207}
208
209static int tc_next_event(unsigned long delta, struct clock_event_device *d)
210{
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200211 writel_relaxed(delta, tcaddr + ATMEL_TC_REG(2, RC));
David Brownell4d243f92008-02-22 17:28:37 -0800212
213 /* go go gadget! */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200214 writel_relaxed(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
David Brownell4d243f92008-02-22 17:28:37 -0800215 tcaddr + ATMEL_TC_REG(2, CCR));
216 return 0;
217}
218
219static struct tc_clkevt_device clkevt = {
220 .clkevt = {
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530221 .features = CLOCK_EVT_FEAT_PERIODIC |
222 CLOCK_EVT_FEAT_ONESHOT,
David Brownell4d243f92008-02-22 17:28:37 -0800223 /* Should be lower than at91rm9200's system timer */
Viresh Kumarcf4541c2015-06-18 16:24:38 +0530224 .rating = 125,
225 .set_next_event = tc_next_event,
226 .set_state_shutdown = tc_shutdown,
227 .set_state_periodic = tc_set_periodic,
228 .set_state_oneshot = tc_set_oneshot,
David Brownell4d243f92008-02-22 17:28:37 -0800229 },
230};
231
232static irqreturn_t ch2_irq(int irq, void *handle)
233{
234 struct tc_clkevt_device *dev = handle;
235 unsigned int sr;
236
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200237 sr = readl_relaxed(dev->regs + ATMEL_TC_REG(2, SR));
David Brownell4d243f92008-02-22 17:28:37 -0800238 if (sr & ATMEL_TC_CPCS) {
239 dev->clkevt.event_handler(&dev->clkevt);
240 return IRQ_HANDLED;
241 }
242
243 return IRQ_NONE;
244}
245
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200246static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
David Brownell4d243f92008-02-22 17:28:37 -0800247{
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200248 int ret;
David Brownell4d243f92008-02-22 17:28:37 -0800249 struct clk *t2_clk = tc->clk[2];
250 int irq = tc->irq[2];
251
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200252 ret = clk_prepare_enable(tc->slow_clk);
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200253 if (ret)
254 return ret;
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200255
256 /* try to enable t2 clk to avoid future errors in mode change */
257 ret = clk_prepare_enable(t2_clk);
258 if (ret) {
259 clk_disable_unprepare(tc->slow_clk);
260 return ret;
261 }
262
David Janderacbf6d22014-05-08 12:06:25 +0200263 clk_disable(t2_clk);
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200264
David Brownell4d243f92008-02-22 17:28:37 -0800265 clkevt.regs = tc->regs;
266 clkevt.clk = t2_clk;
David Brownell4d243f92008-02-22 17:28:37 -0800267
268 timer_clock = clk32k_divisor_idx;
269
Rusty Russell320ab2b2008-12-13 21:20:26 +1030270 clkevt.clkevt.cpumask = cpumask_of(0);
David Brownell4d243f92008-02-22 17:28:37 -0800271
Gaël PORTAYd07a1ec2014-09-06 19:52:37 +0200272 ret = request_irq(irq, ch2_irq, IRQF_TIMER, "tc_clkevt", &clkevt);
273 if (ret) {
Boris Brezilloneed9fb92015-08-16 11:23:45 +0200274 clk_unprepare(t2_clk);
Boris Brezillon7d8d05d2015-08-16 11:23:46 +0200275 clk_disable_unprepare(tc->slow_clk);
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200276 return ret;
Gaël PORTAYd07a1ec2014-09-06 19:52:37 +0200277 }
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200278
Shawn Guo77cc9822013-01-12 11:50:06 +0000279 clockevents_config_and_register(&clkevt.clkevt, 32768, 1, 0xffff);
Voss, Nikolaus1817dc02011-01-25 15:07:29 -0800280
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200281 return ret;
David Brownell4d243f92008-02-22 17:28:37 -0800282}
283
284#else /* !CONFIG_GENERIC_CLOCKEVENTS */
285
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200286static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
David Brownell4d243f92008-02-22 17:28:37 -0800287{
288 /* NOTHING */
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200289 return 0;
David Brownell4d243f92008-02-22 17:28:37 -0800290}
291
292#endif
293
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100294static void __init tcb_setup_dual_chan(struct atmel_tc *tc, int mck_divisor_idx)
295{
296 /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200297 writel(mck_divisor_idx /* likely divide-by-8 */
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100298 | ATMEL_TC_WAVE
299 | ATMEL_TC_WAVESEL_UP /* free-run */
300 | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */
301 | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */
302 tcaddr + ATMEL_TC_REG(0, CMR));
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200303 writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));
304 writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));
305 writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
306 writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100307
308 /* channel 1: waveform mode, input TIOA0 */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200309 writel(ATMEL_TC_XC1 /* input: TIOA0 */
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100310 | ATMEL_TC_WAVE
311 | ATMEL_TC_WAVESEL_UP, /* free-run */
312 tcaddr + ATMEL_TC_REG(1, CMR));
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200313 writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */
314 writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100315
316 /* chain channel 0 to channel 1*/
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200317 writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100318 /* then reset all the timers */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200319 writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100320}
321
322static void __init tcb_setup_single_chan(struct atmel_tc *tc, int mck_divisor_idx)
323{
324 /* channel 0: waveform mode, input mclk/8 */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200325 writel(mck_divisor_idx /* likely divide-by-8 */
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100326 | ATMEL_TC_WAVE
327 | ATMEL_TC_WAVESEL_UP, /* free-run */
328 tcaddr + ATMEL_TC_REG(0, CMR));
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200329 writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
330 writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100331
332 /* then reset all the timers */
Alexandre Belloni6ec8be22017-06-23 17:03:31 +0200333 writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100334}
335
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200336static const u8 atmel_tcb_divisors[5] = { 2, 8, 32, 128, 0, };
David Brownell4d243f92008-02-22 17:28:37 -0800337
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200338static const struct of_device_id atmel_tcb_of_match[] = {
339 { .compatible = "atmel,at91rm9200-tcb", .data = (void *)16, },
340 { .compatible = "atmel,at91sam9x5-tcb", .data = (void *)32, },
341 { /* sentinel */ }
342};
343
344static int __init tcb_clksrc_init(struct device_node *node)
345{
346 struct atmel_tc tc;
David Brownell3ee08ae2008-03-13 09:44:48 -0800347 struct clk *t0_clk;
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200348 const struct of_device_id *match;
Alexandre Bellonif712a1e2019-04-26 23:47:12 +0200349 u64 (*tc_sched_clock)(void);
David Brownell4d243f92008-02-22 17:28:37 -0800350 u32 rate, divided_rate = 0;
351 int best_divisor_idx = -1;
352 int clk32k_divisor_idx = -1;
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200353 int bits;
David Brownell4d243f92008-02-22 17:28:37 -0800354 int i;
Boris BREZILLON0e746ec2013-10-02 14:35:20 +0200355 int ret;
David Brownell4d243f92008-02-22 17:28:37 -0800356
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200357 /* Protect against multiple calls */
358 if (tcaddr)
359 return 0;
David Brownell4d243f92008-02-22 17:28:37 -0800360
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200361 tc.regs = of_iomap(node->parent, 0);
362 if (!tc.regs)
363 return -ENXIO;
364
365 t0_clk = of_clk_get_by_name(node->parent, "t0_clk");
366 if (IS_ERR(t0_clk))
367 return PTR_ERR(t0_clk);
368
369 tc.slow_clk = of_clk_get_by_name(node->parent, "slow_clk");
370 if (IS_ERR(tc.slow_clk))
371 return PTR_ERR(tc.slow_clk);
372
373 tc.clk[0] = t0_clk;
374 tc.clk[1] = of_clk_get_by_name(node->parent, "t1_clk");
375 if (IS_ERR(tc.clk[1]))
376 tc.clk[1] = t0_clk;
377 tc.clk[2] = of_clk_get_by_name(node->parent, "t2_clk");
378 if (IS_ERR(tc.clk[2]))
379 tc.clk[2] = t0_clk;
380
381 tc.irq[2] = of_irq_get(node->parent, 2);
382 if (tc.irq[2] <= 0) {
383 tc.irq[2] = of_irq_get(node->parent, 0);
384 if (tc.irq[2] <= 0)
385 return -EINVAL;
386 }
387
388 match = of_match_node(atmel_tcb_of_match, node->parent);
389 bits = (uintptr_t)match->data;
390
391 for (i = 0; i < ARRAY_SIZE(tc.irq); i++)
392 writel(ATMEL_TC_ALL_IRQ, tc.regs + ATMEL_TC_REG(i, IDR));
393
Boris BREZILLON0e746ec2013-10-02 14:35:20 +0200394 ret = clk_prepare_enable(t0_clk);
395 if (ret) {
396 pr_debug("can't enable T0 clk\n");
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200397 return ret;
Boris BREZILLON0e746ec2013-10-02 14:35:20 +0200398 }
David Brownell4d243f92008-02-22 17:28:37 -0800399
400 /* How fast will we be counting? Pick something over 5 MHz. */
401 rate = (u32) clk_get_rate(t0_clk);
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200402 for (i = 0; i < ARRAY_SIZE(atmel_tcb_divisors); i++) {
403 unsigned divisor = atmel_tcb_divisors[i];
David Brownell4d243f92008-02-22 17:28:37 -0800404 unsigned tmp;
405
406 /* remember 32 KiHz clock for later */
407 if (!divisor) {
408 clk32k_divisor_idx = i;
409 continue;
410 }
411
412 tmp = rate / divisor;
413 pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);
414 if (best_divisor_idx > 0) {
415 if (tmp < 5 * 1000 * 1000)
416 continue;
417 }
418 divided_rate = tmp;
419 best_divisor_idx = i;
420 }
421
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200422 clksrc.name = kbasename(node->parent->full_name);
423 clkevt.clkevt.name = kbasename(node->parent->full_name);
424 pr_debug("%s at %d.%03d MHz\n", clksrc.name, divided_rate / 1000000,
Romain Izard542f8242018-01-08 14:28:43 +0100425 ((divided_rate % 1000000) + 500) / 1000);
David Brownell4d243f92008-02-22 17:28:37 -0800426
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200427 tcaddr = tc.regs;
428
429 if (bits == 32) {
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100430 /* use apropriate function to read 32 bit counter */
431 clksrc.read = tc_get_cycles32;
432 /* setup ony channel 0 */
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200433 tcb_setup_single_chan(&tc, best_divisor_idx);
Alexandre Bellonif712a1e2019-04-26 23:47:12 +0200434 tc_sched_clock = tc_sched_clock_read32;
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100435 } else {
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200436 /* we have three clocks no matter what the
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100437 * underlying platform supports.
438 */
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200439 ret = clk_prepare_enable(tc.clk[1]);
Boris BREZILLON0e746ec2013-10-02 14:35:20 +0200440 if (ret) {
441 pr_debug("can't enable T1 clk\n");
442 goto err_disable_t0;
443 }
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100444 /* setup both channel 0 & 1 */
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200445 tcb_setup_dual_chan(&tc, best_divisor_idx);
Alexandre Bellonif712a1e2019-04-26 23:47:12 +0200446 tc_sched_clock = tc_sched_clock_read;
Nicolas Ferre8e315a72012-01-19 18:44:49 +0100447 }
David Brownell4d243f92008-02-22 17:28:37 -0800448
449 /* and away we go! */
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200450 ret = clocksource_register_hz(&clksrc, divided_rate);
451 if (ret)
452 goto err_disable_t1;
David Brownell4d243f92008-02-22 17:28:37 -0800453
454 /* channel 2: periodic and oneshot timer support */
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200455 ret = setup_clkevents(&tc, clk32k_divisor_idx);
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200456 if (ret)
457 goto err_unregister_clksrc;
David Brownell4d243f92008-02-22 17:28:37 -0800458
Alexandre Bellonif712a1e2019-04-26 23:47:12 +0200459 sched_clock_register(tc_sched_clock, 32, divided_rate);
460
David Brownell4d243f92008-02-22 17:28:37 -0800461 return 0;
Boris BREZILLON0e746ec2013-10-02 14:35:20 +0200462
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200463err_unregister_clksrc:
464 clocksource_unregister(&clksrc);
465
466err_disable_t1:
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200467 if (bits != 32)
468 clk_disable_unprepare(tc.clk[1]);
Boris BREZILLON5b3c11d2013-10-02 14:35:41 +0200469
Boris BREZILLON0e746ec2013-10-02 14:35:20 +0200470err_disable_t0:
471 clk_disable_unprepare(t0_clk);
472
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200473 tcaddr = NULL;
474
Boris BREZILLON0e746ec2013-10-02 14:35:20 +0200475 return ret;
David Brownell4d243f92008-02-22 17:28:37 -0800476}
Alexandre Belloni86232bf2019-04-26 23:47:11 +0200477TIMER_OF_DECLARE(atmel_tcb_clksrc, "atmel,tcb-timer", tcb_clksrc_init);