Path: blob/master/drivers/clocksource/tcb_clksrc.c
15109 views
#include <linux/init.h>1#include <linux/clocksource.h>2#include <linux/clockchips.h>3#include <linux/interrupt.h>4#include <linux/irq.h>56#include <linux/clk.h>7#include <linux/err.h>8#include <linux/ioport.h>9#include <linux/io.h>10#include <linux/platform_device.h>11#include <linux/atmel_tc.h>121314/*15* We're configured to use a specific TC block, one that's not hooked16* up to external hardware, to provide a time solution:17*18* - Two channels combine to create a free-running 32 bit counter19* with a base rate of 5+ MHz, packaged as a clocksource (with20* resolution better than 200 nsec).21*22* - The third channel may be used to provide a 16-bit clockevent23* source, used in either periodic or oneshot mode. This runs24* at 32 KiHZ, and can handle delays of up to two seconds.25*26* A boot clocksource and clockevent source are also currently needed,27* unless the relevant platforms (ARM/AT91, AVR32/AT32) are changed so28* this code can be used when init_timers() is called, well before most29* devices are set up. (Some low end AT91 parts, which can run uClinux,30* have only the timers in one TC block... they currently don't support31* the tclib code, because of that initialization issue.)32*33* REVISIT behavior during system suspend states... we should disable34* 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*/3839static void __iomem *tcaddr;4041static cycle_t tc_get_cycles(struct clocksource *cs)42{43unsigned long flags;44u32 lower, upper;4546raw_local_irq_save(flags);47do {48upper = __raw_readl(tcaddr + ATMEL_TC_REG(1, CV));49lower = __raw_readl(tcaddr + ATMEL_TC_REG(0, CV));50} while (upper != __raw_readl(tcaddr + ATMEL_TC_REG(1, CV)));5152raw_local_irq_restore(flags);53return (upper << 16) | lower;54}5556static struct clocksource clksrc = {57.name = "tcb_clksrc",58.rating = 200,59.read = tc_get_cycles,60.mask = CLOCKSOURCE_MASK(32),61.shift = 18,62.flags = CLOCK_SOURCE_IS_CONTINUOUS,63};6465#ifdef CONFIG_GENERIC_CLOCKEVENTS6667struct tc_clkevt_device {68struct clock_event_device clkevt;69struct clk *clk;70void __iomem *regs;71};7273static struct tc_clkevt_device *to_tc_clkevt(struct clock_event_device *clkevt)74{75return container_of(clkevt, struct tc_clkevt_device, clkevt);76}7778/* For now, we always use the 32K clock ... this optimizes for NO_HZ,79* because using one of the divided clocks would usually mean the80* tick rate can never be less than several dozen Hz (vs 0.5 Hz).81*82* A divided clock could be good for high resolution timers, since83* 30.5 usec resolution can seem "low".84*/85static u32 timer_clock;8687static void tc_mode(enum clock_event_mode m, struct clock_event_device *d)88{89struct tc_clkevt_device *tcd = to_tc_clkevt(d);90void __iomem *regs = tcd->regs;9192if (tcd->clkevt.mode == CLOCK_EVT_MODE_PERIODIC93|| 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));96clk_disable(tcd->clk);97}9899switch (m) {100101/* By not making the gentime core emulate periodic mode on top102* of oneshot, we get lower overhead and improved accuracy.103*/104case CLOCK_EVT_MODE_PERIODIC:105clk_enable(tcd->clk);106107/* slow clock, count up to RC, then irq and restart */108__raw_writel(timer_clock109| ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,110regs + ATMEL_TC_REG(2, CMR));111__raw_writel((32768 + HZ/2) / HZ, tcaddr + ATMEL_TC_REG(2, RC));112113/* Enable clock and interrupts on RC compare */114__raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));115116/* go go gadget! */117__raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,118regs + ATMEL_TC_REG(2, CCR));119break;120121case CLOCK_EVT_MODE_ONESHOT:122clk_enable(tcd->clk);123124/* slow clock, count up to RC, then irq and stop */125__raw_writel(timer_clock | ATMEL_TC_CPCSTOP126| ATMEL_TC_WAVE | ATMEL_TC_WAVESEL_UP_AUTO,127regs + ATMEL_TC_REG(2, CMR));128__raw_writel(ATMEL_TC_CPCS, regs + ATMEL_TC_REG(2, IER));129130/* set_next_event() configures and starts the timer */131break;132133default:134break;135}136}137138static int tc_next_event(unsigned long delta, struct clock_event_device *d)139{140__raw_writel(delta, tcaddr + ATMEL_TC_REG(2, RC));141142/* go go gadget! */143__raw_writel(ATMEL_TC_CLKEN | ATMEL_TC_SWTRG,144tcaddr + ATMEL_TC_REG(2, CCR));145return 0;146}147148static struct tc_clkevt_device clkevt = {149.clkevt = {150.name = "tc_clkevt",151.features = CLOCK_EVT_FEAT_PERIODIC152| CLOCK_EVT_FEAT_ONESHOT,153.shift = 32,154/* Should be lower than at91rm9200's system timer */155.rating = 125,156.set_next_event = tc_next_event,157.set_mode = tc_mode,158},159};160161static irqreturn_t ch2_irq(int irq, void *handle)162{163struct tc_clkevt_device *dev = handle;164unsigned int sr;165166sr = __raw_readl(dev->regs + ATMEL_TC_REG(2, SR));167if (sr & ATMEL_TC_CPCS) {168dev->clkevt.event_handler(&dev->clkevt);169return IRQ_HANDLED;170}171172return IRQ_NONE;173}174175static struct irqaction tc_irqaction = {176.name = "tc_clkevt",177.flags = IRQF_TIMER | IRQF_DISABLED,178.handler = ch2_irq,179};180181static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)182{183struct clk *t2_clk = tc->clk[2];184int irq = tc->irq[2];185186clkevt.regs = tc->regs;187clkevt.clk = t2_clk;188tc_irqaction.dev_id = &clkevt;189190timer_clock = clk32k_divisor_idx;191192clkevt.clkevt.mult = div_sc(32768, NSEC_PER_SEC, clkevt.clkevt.shift);193clkevt.clkevt.max_delta_ns194= clockevent_delta2ns(0xffff, &clkevt.clkevt);195clkevt.clkevt.min_delta_ns = clockevent_delta2ns(1, &clkevt.clkevt) + 1;196clkevt.clkevt.cpumask = cpumask_of(0);197198clockevents_register_device(&clkevt.clkevt);199200setup_irq(irq, &tc_irqaction);201}202203#else /* !CONFIG_GENERIC_CLOCKEVENTS */204205static void __init setup_clkevents(struct atmel_tc *tc, int clk32k_divisor_idx)206{207/* NOTHING */208}209210#endif211212static int __init tcb_clksrc_init(void)213{214static char bootinfo[] __initdata215= KERN_DEBUG "%s: tc%d at %d.%03d MHz\n";216217struct platform_device *pdev;218struct atmel_tc *tc;219struct clk *t0_clk;220u32 rate, divided_rate = 0;221int best_divisor_idx = -1;222int clk32k_divisor_idx = -1;223int i;224225tc = atmel_tc_alloc(CONFIG_ATMEL_TCB_CLKSRC_BLOCK, clksrc.name);226if (!tc) {227pr_debug("can't alloc TC for clocksource\n");228return -ENODEV;229}230tcaddr = tc->regs;231pdev = tc->pdev;232233t0_clk = tc->clk[0];234clk_enable(t0_clk);235236/* How fast will we be counting? Pick something over 5 MHz. */237rate = (u32) clk_get_rate(t0_clk);238for (i = 0; i < 5; i++) {239unsigned divisor = atmel_tc_divisors[i];240unsigned tmp;241242/* remember 32 KiHz clock for later */243if (!divisor) {244clk32k_divisor_idx = i;245continue;246}247248tmp = rate / divisor;249pr_debug("TC: %u / %-3u [%d] --> %u\n", rate, divisor, i, tmp);250if (best_divisor_idx > 0) {251if (tmp < 5 * 1000 * 1000)252continue;253}254divided_rate = tmp;255best_divisor_idx = i;256}257258clksrc.mult = clocksource_hz2mult(divided_rate, clksrc.shift);259260printk(bootinfo, clksrc.name, CONFIG_ATMEL_TCB_CLKSRC_BLOCK,261divided_rate / 1000000,262((divided_rate + 500000) % 1000000) / 1000);263264/* tclib will give us three clocks no matter what the265* underlying platform supports.266*/267clk_enable(tc->clk[1]);268269/* channel 0: waveform mode, input mclk/8, clock TIOA0 on overflow */270__raw_writel(best_divisor_idx /* likely divide-by-8 */271| ATMEL_TC_WAVE272| ATMEL_TC_WAVESEL_UP /* free-run */273| ATMEL_TC_ACPA_SET /* TIOA0 rises at 0 */274| ATMEL_TC_ACPC_CLEAR, /* (duty cycle 50%) */275tcaddr + ATMEL_TC_REG(0, CMR));276__raw_writel(0x0000, tcaddr + ATMEL_TC_REG(0, RA));277__raw_writel(0x8000, tcaddr + ATMEL_TC_REG(0, RC));278__raw_writel(0xff, tcaddr + ATMEL_TC_REG(0, IDR)); /* no irqs */279__raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(0, CCR));280281/* channel 1: waveform mode, input TIOA0 */282__raw_writel(ATMEL_TC_XC1 /* input: TIOA0 */283| ATMEL_TC_WAVE284| ATMEL_TC_WAVESEL_UP, /* free-run */285tcaddr + ATMEL_TC_REG(1, CMR));286__raw_writel(0xff, tcaddr + ATMEL_TC_REG(1, IDR)); /* no irqs */287__raw_writel(ATMEL_TC_CLKEN, tcaddr + ATMEL_TC_REG(1, CCR));288289/* chain channel 0 to channel 1, then reset all the timers */290__raw_writel(ATMEL_TC_TC1XC1S_TIOA0, tcaddr + ATMEL_TC_BMR);291__raw_writel(ATMEL_TC_SYNC, tcaddr + ATMEL_TC_BCR);292293/* and away we go! */294clocksource_register(&clksrc);295296/* channel 2: periodic and oneshot timer support */297setup_clkevents(tc, clk32k_divisor_idx);298299return 0;300}301arch_initcall(tcb_clksrc_init);302303304