Path: blob/master/arch/arm/mach-at91/at91rm9200_time.c
10817 views
/*1* linux/arch/arm/mach-at91/at91rm9200_time.c2*3* Copyright (C) 2003 SAN People4* Copyright (C) 2003 ATMEL5*6* This program is free software; you can redistribute it and/or modify7* it under the terms of the GNU General Public License as published by8* the Free Software Foundation; either version 2 of the License, or9* (at your option) any later version.10*11* This program is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14* GNU General Public License for more details.15*16* You should have received a copy of the GNU General Public License17* along with this program; if not, write to the Free Software18* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA19*/2021#include <linux/kernel.h>22#include <linux/interrupt.h>23#include <linux/irq.h>24#include <linux/clockchips.h>2526#include <asm/mach/time.h>2728#include <mach/at91_st.h>2930static unsigned long last_crtr;31static u32 irqmask;32static struct clock_event_device clkevt;3334/*35* The ST_CRTR is updated asynchronously to the master clock ... but36* the updates as seen by the CPU don't seem to be strictly monotonic.37* Waiting until we read the same value twice avoids glitching.38*/39static inline unsigned long read_CRTR(void)40{41unsigned long x1, x2;4243x1 = at91_sys_read(AT91_ST_CRTR);44do {45x2 = at91_sys_read(AT91_ST_CRTR);46if (x1 == x2)47break;48x1 = x2;49} while (1);50return x1;51}5253/*54* IRQ handler for the timer.55*/56static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)57{58u32 sr = at91_sys_read(AT91_ST_SR) & irqmask;5960/*61* irqs should be disabled here, but as the irq is shared they are only62* guaranteed to be off if the timer irq is registered first.63*/64WARN_ON_ONCE(!irqs_disabled());6566/* simulate "oneshot" timer with alarm */67if (sr & AT91_ST_ALMS) {68clkevt.event_handler(&clkevt);69return IRQ_HANDLED;70}7172/* periodic mode should handle delayed ticks */73if (sr & AT91_ST_PITS) {74u32 crtr = read_CRTR();7576while (((crtr - last_crtr) & AT91_ST_CRTV) >= LATCH) {77last_crtr += LATCH;78clkevt.event_handler(&clkevt);79}80return IRQ_HANDLED;81}8283/* this irq is shared ... */84return IRQ_NONE;85}8687static struct irqaction at91rm9200_timer_irq = {88.name = "at91_tick",89.flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,90.handler = at91rm9200_timer_interrupt91};9293static cycle_t read_clk32k(struct clocksource *cs)94{95return read_CRTR();96}9798static struct clocksource clk32k = {99.name = "32k_counter",100.rating = 150,101.read = read_clk32k,102.mask = CLOCKSOURCE_MASK(20),103.flags = CLOCK_SOURCE_IS_CONTINUOUS,104};105106static void107clkevt32k_mode(enum clock_event_mode mode, struct clock_event_device *dev)108{109/* Disable and flush pending timer interrupts */110at91_sys_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_ALMS);111(void) at91_sys_read(AT91_ST_SR);112113last_crtr = read_CRTR();114switch (mode) {115case CLOCK_EVT_MODE_PERIODIC:116/* PIT for periodic irqs; fixed rate of 1/HZ */117irqmask = AT91_ST_PITS;118at91_sys_write(AT91_ST_PIMR, LATCH);119break;120case CLOCK_EVT_MODE_ONESHOT:121/* ALM for oneshot irqs, set by next_event()122* before 32 seconds have passed123*/124irqmask = AT91_ST_ALMS;125at91_sys_write(AT91_ST_RTAR, last_crtr);126break;127case CLOCK_EVT_MODE_SHUTDOWN:128case CLOCK_EVT_MODE_UNUSED:129case CLOCK_EVT_MODE_RESUME:130irqmask = 0;131break;132}133at91_sys_write(AT91_ST_IER, irqmask);134}135136static int137clkevt32k_next_event(unsigned long delta, struct clock_event_device *dev)138{139u32 alm;140int status = 0;141142BUG_ON(delta < 2);143144/* The alarm IRQ uses absolute time (now+delta), not the relative145* time (delta) in our calling convention. Like all clockevents146* using such "match" hardware, we have a race to defend against.147*148* Our defense here is to have set up the clockevent device so the149* delta is at least two. That way we never end up writing RTAR150* with the value then held in CRTR ... which would mean the match151* wouldn't trigger until 32 seconds later, after CRTR wraps.152*/153alm = read_CRTR();154155/* Cancel any pending alarm; flush any pending IRQ */156at91_sys_write(AT91_ST_RTAR, alm);157(void) at91_sys_read(AT91_ST_SR);158159/* Schedule alarm by writing RTAR. */160alm += delta;161at91_sys_write(AT91_ST_RTAR, alm);162163return status;164}165166static struct clock_event_device clkevt = {167.name = "at91_tick",168.features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT,169.shift = 32,170.rating = 150,171.set_next_event = clkevt32k_next_event,172.set_mode = clkevt32k_mode,173};174175/*176* ST (system timer) module supports both clockevents and clocksource.177*/178void __init at91rm9200_timer_init(void)179{180/* Disable all timer interrupts, and clear any pending ones */181at91_sys_write(AT91_ST_IDR,182AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);183(void) at91_sys_read(AT91_ST_SR);184185/* Make IRQs happen for the system timer */186setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);187188/* The 32KiHz "Slow Clock" (tick every 30517.58 nanoseconds) is used189* directly for the clocksource and all clockevents, after adjusting190* its prescaler from the 1 Hz default.191*/192at91_sys_write(AT91_ST_RTMR, 1);193194/* Setup timer clockevent, with minimum of two ticks (important!!) */195clkevt.mult = div_sc(AT91_SLOW_CLOCK, NSEC_PER_SEC, clkevt.shift);196clkevt.max_delta_ns = clockevent_delta2ns(AT91_ST_ALMV, &clkevt);197clkevt.min_delta_ns = clockevent_delta2ns(2, &clkevt) + 1;198clkevt.cpumask = cpumask_of(0);199clockevents_register_device(&clkevt);200201/* register clocksource */202clocksource_register_hz(&clk32k, AT91_SLOW_CLOCK);203}204205struct sys_timer at91rm9200_timer = {206.init = at91rm9200_timer_init,207};208209210211