1 #include <linux/init.h>
2 #include <linux/clocksource.h>
3 #include <linux/clockchips.h>
4 #include <linux/interrupt.h>
9 #include <linux/ioport.h>
11 #include <linux/platform_device.h>
12 #include <linux/atmel_tc.h>
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:
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).
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.
25 * - The third channel may be used to provide a 16-bit clockevent
26 * source, used in either periodic or oneshot mode.
28 * A boot clocksource and clockevent source are also currently needed,
29 * unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so
30 * this code can be used when init_timers() is called, well before most
31 * devices are set up. (Some low end AT91 parts, which can run uClinux,
32 * have only the timers in one TC block... they currently don't support
33 * the tclib code, because of that initialization issue.)
35 * REVISIT behavior during system suspend states... we should disable
36 * all clocks and save the power. Easily done for clockevent devices,
37 * but clocksources won't necessarily get the needed notifications.
38 * For deeper system sleep states, this will be mandatory...
41 static void __iomem *tcaddr;
43 static cycle_t tc_get_cycles(struct clocksource *cs)
48 raw_local_irq_save(flags);
50 upper = __raw_readl(tcaddr + ATMEL_TC_REG(1, CV));
51 lower = __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));
52 } while (upper != __raw_readl(tcaddr + ATMEL_TC_REG(1, CV)));
54 raw_local_irq_restore(flags);
55 return (upper << 16) | lower;
58 static cycle_t tc_get_cycles32(struct clocksource *cs)
60 return __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));
63 static struct clocksource clksrc = {
66 .read = tc_get_cycles,
67 .mask = CLOCKSOURCE_MASK(32),
68 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
71 #ifdef CONFIG_GENERIC_CLOCKEVENTS
73 struct tc_clkevt_device {
74 struct clock_event_device clkevt;
80 static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)
82 return container_of(clkevt, struct tc_clkevt_device, clkevt);
85 static u32 timer_clock;
87 static void tc_mode(enum clock_event_mode m, struct clock_event_device *d)
89 struct tc_clkevt_device *tcd = to_tc_clkevt(d);
90 void __iomem *regs = tcd->regs;
92 if (tcd->clkevt.mode == CLOCK_EVT_MODE_PERIODIC
93 || tcd->clkevt.mode == CLOCK_EVT_MODE_ONESHOT) {
94 __raw_writel(0xff, regs + ATMEL_TC_REG(2, IDR));
95 __raw_writel(ATMEL_TC_CLKDIS, regs + ATMEL_TC_REG(2, CCR));
96 clk_disable(tcd->clk);
101 /* By not making the gentime core emulate periodic mode on top
102 * of oneshot, we get lower overhead and improved accuracy.
104 case CLOCK_EVT_MODE_PERIODIC:
105 clk_enable(tcd->clk);
107 /* count up to RC, then irq and restart */
108 __raw_writel(timer_clock
109 | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
110 regs + ATMEL_TC_REG(2, CMR));
111 __raw_writel((tcd->freq + HZ / 2) / HZ,
112 tcaddr + ATMEL_TC_REG(2, RC));
114 /* Enable clock and interrupts on RC compare */
115 __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
118 __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
119 regs + ATMEL_TC_REG(2, CCR));
122 case CLOCK_EVT_MODE_ONESHOT:
123 clk_enable(tcd->clk);
125 /* count up to RC, then irq and stop */
126 __raw_writel(timer_clock | ATMEL_TC_CPCSTOP
127 | ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,
128 regs + ATMEL_TC_REG(2, CMR));
129 __raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));
131 /* set_next_event() configures and starts the timer */
139 static int tc_next_event(unsigned long delta, struct clock_event_device *d)
141 __raw_writel(delta, tcaddr + ATMEL_TC_REG(2, RC));
144 __raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,
145 tcaddr + ATMEL_TC_REG(2, CCR));
149 static struct tc_clkevt_device clkevt = {
152 .features = CLOCK_EVT_FEAT_PERIODIC
153 | CLOCK_EVT_FEAT_ONESHOT,
154 #ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
155 /* Should be lower than at91rm9200's system timer */
160 .set_next_event = tc_next_event,
165 static irqreturn_t ch2_irq(int irq, void *handle)
167 struct tc_clkevt_device *dev = handle;
170 sr = __raw_readl(dev->regs + ATMEL_TC_REG(2, SR));
171 if (sr & ATMEL_TC_CPCS) {
172 dev->clkevt.event_handler(&dev->clkevt);
179 static int __init setup_clkevents(struct atmel_tc *tc, int divisor_idx)
181 unsigned divisor = atmel_tc_divisors[divisor_idx];
183 struct clk *t2_clk = tc->clk[2];
184 int irq = tc->irq[2];
186 /* try to enable t2 clk to avoid future errors in mode change */
187 ret = clk_prepare_enable(t2_clk);
192 clkevt.regs = tc->regs;
195 timer_clock = divisor_idx;
199 clkevt.freq = clk_get_rate(t2_clk) / divisor;
201 clkevt.clkevt.cpumask = cpumask_of(0);
203 ret = request_irq(irq, ch2_irq, IRQF_TIMER, "tc_clkevt", &clkevt);
205 clk_disable_unprepare(t2_clk);
209 clockevents_config_and_register(&clkevt.clkevt, clkevt.freq, 1, 0xffff);
214 #else /* !CONFIG_GENERIC_CLOCKEVENTS */
216 static int __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)
224 static void __init tcb_setup_dual_chan(struct atmel_tc *tc, int mck_divisor_idx)
226 /* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */
227 __raw_writel(mck_divisor_idx /* likely divide-by-8 */
229 | ATMEL_TC_WAVESEL_UP /* free-run */
230 | ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */
231 | ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */
232 tcaddr + ATMEL_TC_REG(0, CMR));
233 __raw_writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));
234 __raw_writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));
235 __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
236 __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
238 /* channel 1: waveform mode, input TIOA0 */
239 __raw_writel(ATMEL_TC_XC1 /* input: TIOA0 */
241 | ATMEL_TC_WAVESEL_UP, /* free-run */
242 tcaddr + ATMEL_TC_REG(1, CMR));
243 __raw_writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */
244 __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));
246 /* chain channel 0 to channel 1*/
247 __raw_writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);
248 /* then reset all the timers */
249 __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
252 static void __init tcb_setup_single_chan(struct atmel_tc *tc, int mck_divisor_idx)
254 /* channel 0: waveform mode, input mclk/8 */
255 __raw_writel(mck_divisor_idx /* likely divide-by-8 */
257 | ATMEL_TC_WAVESEL_UP, /* free-run */
258 tcaddr + ATMEL_TC_REG(0, CMR));
259 __raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */
260 __raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));
262 /* then reset all the timers */
263 __raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);
266 static int __init tcb_clksrc_init(void)
268 static char bootinfo[] __initdata
269 = KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";
271 struct platform_device *pdev;
274 u32 rate, divided_rate = 0;
275 int best_divisor_idx = -1;
276 int clk32k_divisor_idx = -1;
280 tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK);
282 pr_debug("can't alloc TC for clocksource\n");
289 ret = clk_prepare_enable(t0_clk);
291 pr_debug("can't enable T0 clk\n");
295 /* How fast will we be counting? Pick something over 5 MHz. */
296 rate = (u32) clk_get_rate(t0_clk);
297 for (i = 0; i < 5; i++) {
298 unsigned divisor = atmel_tc_divisors[i];
301 /* remember 32 KiHz clock for later */
303 clk32k_divisor_idx = i;
307 tmp = rate / divisor;
308 pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);
309 if (best_divisor_idx > 0) {
310 if (tmp < 5 * 1000 * 1000)
314 best_divisor_idx = i;
318 printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,
319 divided_rate / 1000000,
320 ((divided_rate + 500000) % 1000000) / 1000);
322 if (tc->tcb_config && tc->tcb_config->counter_width == 32) {
323 /* use apropriate function to read 32 bit counter */
324 clksrc.read = tc_get_cycles32;
325 /* setup ony channel 0 */
326 tcb_setup_single_chan(tc, best_divisor_idx);
328 /* tclib will give us three clocks no matter what the
329 * underlying platform supports.
331 ret = clk_prepare_enable(tc->clk[1]);
333 pr_debug("can't enable T1 clk\n");
336 /* setup both channel 0 & 1 */
337 tcb_setup_dual_chan(tc, best_divisor_idx);
340 /* and away we go! */
341 ret = clocksource_register_hz(&clksrc, divided_rate);
345 /* channel 2: periodic and oneshot timer support */
346 #ifdef CONFIG_ATMEL_TCB_CLKSRC_USE_SLOW_CLOCK
347 ret = setup_clkevents(tc, clk32k_divisor_idx);
349 ret = setup_clkevents(tc, best_divisor_idx);
352 goto err_unregister_clksrc;
356 err_unregister_clksrc:
357 clocksource_unregister(&clksrc);
360 if (!tc->tcb_config || tc->tcb_config->counter_width != 32)
361 clk_disable_unprepare(tc->clk[1]);
364 clk_disable_unprepare(t0_clk);
370 arch_initcall(tcb_clksrc_init);