Path: blob/master/arch/arm/mach-at91/at91sam926x_time.c
10817 views
/*1* at91sam926x_time.c - Periodic Interval Timer (PIT) for at91sam926x2*3* Copyright (C) 2005-2006 M. Amine SAYA, ATMEL Rousset, France4* Revision 2005 M. Nicolas Diremdjian, ATMEL Rousset, France5* Converted to ClockSource/ClockEvents by David Brownell.6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License version 2 as9* published by the Free Software Foundation.10*/11#include <linux/interrupt.h>12#include <linux/irq.h>13#include <linux/kernel.h>14#include <linux/clk.h>15#include <linux/clockchips.h>1617#include <asm/mach/time.h>1819#include <mach/at91_pit.h>202122#define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)23#define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)2425static u32 pit_cycle; /* write-once */26static u32 pit_cnt; /* access only w/system irq blocked */272829/*30* Clocksource: just a monotonic counter of MCK/16 cycles.31* We don't care whether or not PIT irqs are enabled.32*/33static cycle_t read_pit_clk(struct clocksource *cs)34{35unsigned long flags;36u32 elapsed;37u32 t;3839raw_local_irq_save(flags);40elapsed = pit_cnt;41t = at91_sys_read(AT91_PIT_PIIR);42raw_local_irq_restore(flags);4344elapsed += PIT_PICNT(t) * pit_cycle;45elapsed += PIT_CPIV(t);46return elapsed;47}4849static struct clocksource pit_clk = {50.name = "pit",51.rating = 175,52.read = read_pit_clk,53.flags = CLOCK_SOURCE_IS_CONTINUOUS,54};555657/*58* Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)59*/60static void61pit_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)62{63switch (mode) {64case CLOCK_EVT_MODE_PERIODIC:65/* update clocksource counter */66pit_cnt += pit_cycle * PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));67at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN68| AT91_PIT_PITIEN);69break;70case CLOCK_EVT_MODE_ONESHOT:71BUG();72/* FALLTHROUGH */73case CLOCK_EVT_MODE_SHUTDOWN:74case CLOCK_EVT_MODE_UNUSED:75/* disable irq, leaving the clocksource active */76at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);77break;78case CLOCK_EVT_MODE_RESUME:79break;80}81}8283static struct clock_event_device pit_clkevt = {84.name = "pit",85.features = CLOCK_EVT_FEAT_PERIODIC,86.shift = 32,87.rating = 100,88.set_mode = pit_clkevt_mode,89};909192/*93* IRQ handler for the timer.94*/95static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)96{97/*98* irqs should be disabled here, but as the irq is shared they are only99* guaranteed to be off if the timer irq is registered first.100*/101WARN_ON_ONCE(!irqs_disabled());102103/* The PIT interrupt may be disabled, and is shared */104if ((pit_clkevt.mode == CLOCK_EVT_MODE_PERIODIC)105&& (at91_sys_read(AT91_PIT_SR) & AT91_PIT_PITS)) {106unsigned nr_ticks;107108/* Get number of ticks performed before irq, and ack it */109nr_ticks = PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));110do {111pit_cnt += pit_cycle;112pit_clkevt.event_handler(&pit_clkevt);113nr_ticks--;114} while (nr_ticks);115116return IRQ_HANDLED;117}118119return IRQ_NONE;120}121122static struct irqaction at91sam926x_pit_irq = {123.name = "at91_tick",124.flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,125.handler = at91sam926x_pit_interrupt126};127128static void at91sam926x_pit_reset(void)129{130/* Disable timer and irqs */131at91_sys_write(AT91_PIT_MR, 0);132133/* Clear any pending interrupts, wait for PIT to stop counting */134while (PIT_CPIV(at91_sys_read(AT91_PIT_PIVR)) != 0)135cpu_relax();136137/* Start PIT but don't enable IRQ */138at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);139}140141/*142* Set up both clocksource and clockevent support.143*/144static void __init at91sam926x_pit_init(void)145{146unsigned long pit_rate;147unsigned bits;148149/*150* Use our actual MCK to figure out how many MCK/16 ticks per151* 1/HZ period (instead of a compile-time constant LATCH).152*/153pit_rate = clk_get_rate(clk_get(NULL, "mck")) / 16;154pit_cycle = (pit_rate + HZ/2) / HZ;155WARN_ON(((pit_cycle - 1) & ~AT91_PIT_PIV) != 0);156157/* Initialize and enable the timer */158at91sam926x_pit_reset();159160/*161* Register clocksource. The high order bits of PIV are unused,162* so this isn't a 32-bit counter unless we get clockevent irqs.163*/164bits = 12 /* PICNT */ + ilog2(pit_cycle) /* PIV */;165pit_clk.mask = CLOCKSOURCE_MASK(bits);166clocksource_register_hz(&pit_clk, pit_rate);167168/* Set up irq handler */169setup_irq(AT91_ID_SYS, &at91sam926x_pit_irq);170171/* Set up and register clockevents */172pit_clkevt.mult = div_sc(pit_rate, NSEC_PER_SEC, pit_clkevt.shift);173pit_clkevt.cpumask = cpumask_of(0);174clockevents_register_device(&pit_clkevt);175}176177static void at91sam926x_pit_suspend(void)178{179/* Disable timer */180at91_sys_write(AT91_PIT_MR, 0);181}182183struct sys_timer at91sam926x_timer = {184.init = at91sam926x_pit_init,185.suspend = at91sam926x_pit_suspend,186.resume = at91sam926x_pit_reset,187};188189190