blob: 4ba85499fa979b8503b2e86ab9aa5c2786266b1c [file] [log] [blame]
Andrew Victor1a0ed732006-12-01 09:04:47 +01001/*
Andrew Victorad48ce72008-04-16 20:43:49 +01002 * at91sam926x_time.c - Periodic Interval Timer (PIT) for at91sam926x
Andrew Victor1a0ed732006-12-01 09:04:47 +01003 *
4 * Copyright (C) 2005-2006 M. Amine SAYA, ATMEL Rousset, France
5 * Revision 2005 M. Nicolas Diremdjian, ATMEL Rousset, France
Andrew Victorad48ce72008-04-16 20:43:49 +01006 * Converted to ClockSource/ClockEvents by David Brownell.
Andrew Victor1a0ed732006-12-01 09:04:47 +01007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
Andrew Victor1a0ed732006-12-01 09:04:47 +010012#include <linux/interrupt.h>
13#include <linux/irq.h>
14#include <linux/kernel.h>
Andrew Victorad48ce72008-04-16 20:43:49 +010015#include <linux/clk.h>
16#include <linux/clockchips.h>
Andrew Victor1a0ed732006-12-01 09:04:47 +010017
Andrew Victor1a0ed732006-12-01 09:04:47 +010018#include <asm/mach/time.h>
19
Russell Kinga09e64f2008-08-05 16:14:15 +010020#include <mach/at91_pit.h>
Andrew Victor1a0ed732006-12-01 09:04:47 +010021
22
23#define PIT_CPIV(x) ((x) & AT91_PIT_CPIV)
24#define PIT_PICNT(x) (((x) & AT91_PIT_PICNT) >> 20)
25
Andrew Victorad48ce72008-04-16 20:43:49 +010026static u32 pit_cycle; /* write-once */
27static u32 pit_cnt; /* access only w/system irq blocked */
28
29
Andrew Victor1a0ed732006-12-01 09:04:47 +010030/*
Andrew Victorad48ce72008-04-16 20:43:49 +010031 * Clocksource: just a monotonic counter of MCK/16 cycles.
32 * We don't care whether or not PIT irqs are enabled.
Andrew Victor1a0ed732006-12-01 09:04:47 +010033 */
Magnus Damm8e196082009-04-21 12:24:00 -070034static cycle_t read_pit_clk(struct clocksource *cs)
Andrew Victor1a0ed732006-12-01 09:04:47 +010035{
Andrew Victorad48ce72008-04-16 20:43:49 +010036 unsigned long flags;
37 u32 elapsed;
38 u32 t;
Andrew Victor1a0ed732006-12-01 09:04:47 +010039
Andrew Victorad48ce72008-04-16 20:43:49 +010040 raw_local_irq_save(flags);
41 elapsed = pit_cnt;
42 t = at91_sys_read(AT91_PIT_PIIR);
43 raw_local_irq_restore(flags);
Andrew Victor1a0ed732006-12-01 09:04:47 +010044
Andrew Victorad48ce72008-04-16 20:43:49 +010045 elapsed += PIT_PICNT(t) * pit_cycle;
46 elapsed += PIT_CPIV(t);
47 return elapsed;
Andrew Victor1a0ed732006-12-01 09:04:47 +010048}
49
Andrew Victorad48ce72008-04-16 20:43:49 +010050static struct clocksource pit_clk = {
51 .name = "pit",
52 .rating = 175,
53 .read = read_pit_clk,
Andrew Victorad48ce72008-04-16 20:43:49 +010054 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
55};
56
57
58/*
59 * Clockevent device: interrupts every 1/HZ (== pit_cycles * MCK/16)
60 */
61static void
62pit_clkevt_mode(enum clock_event_mode mode, struct clock_event_device *dev)
63{
Andrew Victorad48ce72008-04-16 20:43:49 +010064 switch (mode) {
65 case CLOCK_EVT_MODE_PERIODIC:
Uwe Kleine-König501d7032009-09-21 09:30:09 +020066 /* update clocksource counter */
Andrew Victorad48ce72008-04-16 20:43:49 +010067 pit_cnt += pit_cycle * PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
68 at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN
69 | AT91_PIT_PITIEN);
Andrew Victorad48ce72008-04-16 20:43:49 +010070 break;
71 case CLOCK_EVT_MODE_ONESHOT:
72 BUG();
73 /* FALLTHROUGH */
74 case CLOCK_EVT_MODE_SHUTDOWN:
75 case CLOCK_EVT_MODE_UNUSED:
76 /* disable irq, leaving the clocksource active */
77 at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);
78 break;
79 case CLOCK_EVT_MODE_RESUME:
80 break;
81 }
82}
83
84static struct clock_event_device pit_clkevt = {
85 .name = "pit",
86 .features = CLOCK_EVT_FEAT_PERIODIC,
87 .shift = 32,
88 .rating = 100,
Andrew Victorad48ce72008-04-16 20:43:49 +010089 .set_mode = pit_clkevt_mode,
90};
91
92
Andrew Victor1a0ed732006-12-01 09:04:47 +010093/*
94 * IRQ handler for the timer.
95 */
Andrew Victorad48ce72008-04-16 20:43:49 +010096static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
Andrew Victor1a0ed732006-12-01 09:04:47 +010097{
Uwe Kleine-König501d7032009-09-21 09:30:09 +020098 /*
99 * irqs should be disabled here, but as the irq is shared they are only
100 * guaranteed to be off if the timer irq is registered first.
101 */
102 WARN_ON_ONCE(!irqs_disabled());
Andrew Victor1a0ed732006-12-01 09:04:47 +0100103
Andrew Victorad48ce72008-04-16 20:43:49 +0100104 /* The PIT interrupt may be disabled, and is shared */
105 if ((pit_clkevt.mode == CLOCK_EVT_MODE_PERIODIC)
106 && (at91_sys_read(AT91_PIT_SR) & AT91_PIT_PITS)) {
107 unsigned nr_ticks;
108
109 /* Get number of ticks performed before irq, and ack it */
Andrew Victor1a0ed732006-12-01 09:04:47 +0100110 nr_ticks = PIT_PICNT(at91_sys_read(AT91_PIT_PIVR));
111 do {
Andrew Victorad48ce72008-04-16 20:43:49 +0100112 pit_cnt += pit_cycle;
113 pit_clkevt.event_handler(&pit_clkevt);
Andrew Victor1a0ed732006-12-01 09:04:47 +0100114 nr_ticks--;
115 } while (nr_ticks);
116
Andrew Victor1a0ed732006-12-01 09:04:47 +0100117 return IRQ_HANDLED;
Andrew Victorad48ce72008-04-16 20:43:49 +0100118 }
119
120 return IRQ_NONE;
Andrew Victor1a0ed732006-12-01 09:04:47 +0100121}
122
Andrew Victorad48ce72008-04-16 20:43:49 +0100123static struct irqaction at91sam926x_pit_irq = {
Andrew Victor1a0ed732006-12-01 09:04:47 +0100124 .name = "at91_tick",
Bernhard Walleb30faba2007-05-08 00:35:39 -0700125 .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER | IRQF_IRQPOLL,
Andrew Victorad48ce72008-04-16 20:43:49 +0100126 .handler = at91sam926x_pit_interrupt
Andrew Victor1a0ed732006-12-01 09:04:47 +0100127};
128
Andrew Victorad48ce72008-04-16 20:43:49 +0100129static void at91sam926x_pit_reset(void)
Andrew Victor1a0ed732006-12-01 09:04:47 +0100130{
Andrew Victorad48ce72008-04-16 20:43:49 +0100131 /* Disable timer and irqs */
Andrew Victor1a0ed732006-12-01 09:04:47 +0100132 at91_sys_write(AT91_PIT_MR, 0);
133
Andrew Victorad48ce72008-04-16 20:43:49 +0100134 /* Clear any pending interrupts, wait for PIT to stop counting */
135 while (PIT_CPIV(at91_sys_read(AT91_PIT_PIVR)) != 0)
136 cpu_relax();
Andrew Victor1a0ed732006-12-01 09:04:47 +0100137
Andrew Victorad48ce72008-04-16 20:43:49 +0100138 /* Start PIT but don't enable IRQ */
139 at91_sys_write(AT91_PIT_MR, (pit_cycle - 1) | AT91_PIT_PITEN);
Andrew Victor1a0ed732006-12-01 09:04:47 +0100140}
141
142/*
Andrew Victorad48ce72008-04-16 20:43:49 +0100143 * Set up both clocksource and clockevent support.
Andrew Victor1a0ed732006-12-01 09:04:47 +0100144 */
Andrew Victorad48ce72008-04-16 20:43:49 +0100145static void __init at91sam926x_pit_init(void)
Andrew Victor1a0ed732006-12-01 09:04:47 +0100146{
Andrew Victorad48ce72008-04-16 20:43:49 +0100147 unsigned long pit_rate;
148 unsigned bits;
Andrew Victor1a0ed732006-12-01 09:04:47 +0100149
Andrew Victorad48ce72008-04-16 20:43:49 +0100150 /*
151 * Use our actual MCK to figure out how many MCK/16 ticks per
152 * 1/HZ period (instead of a compile-time constant LATCH).
153 */
154 pit_rate = clk_get_rate(clk_get(NULL, "mck")) / 16;
155 pit_cycle = (pit_rate + HZ/2) / HZ;
156 WARN_ON(((pit_cycle - 1) & ~AT91_PIT_PIV) != 0);
157
158 /* Initialize and enable the timer */
159 at91sam926x_pit_reset();
160
161 /*
162 * Register clocksource. The high order bits of PIV are unused,
163 * so this isn't a 32-bit counter unless we get clockevent irqs.
164 */
Andrew Victorad48ce72008-04-16 20:43:49 +0100165 bits = 12 /* PICNT */ + ilog2(pit_cycle) /* PIV */;
166 pit_clk.mask = CLOCKSOURCE_MASK(bits);
Russell King132b1632010-12-13 13:14:55 +0000167 clocksource_register_hz(&pit_clk, pit_rate);
Andrew Victorad48ce72008-04-16 20:43:49 +0100168
169 /* Set up irq handler */
170 setup_irq(AT91_ID_SYS, &at91sam926x_pit_irq);
171
172 /* Set up and register clockevents */
173 pit_clkevt.mult = div_sc(pit_rate, NSEC_PER_SEC, pit_clkevt.shift);
Rusty Russell320ab2b2008-12-13 21:20:26 +1030174 pit_clkevt.cpumask = cpumask_of(0);
Andrew Victorad48ce72008-04-16 20:43:49 +0100175 clockevents_register_device(&pit_clkevt);
Andrew Victor1a0ed732006-12-01 09:04:47 +0100176}
177
Andrew Victorad48ce72008-04-16 20:43:49 +0100178static void at91sam926x_pit_suspend(void)
Andrew Victor1a0ed732006-12-01 09:04:47 +0100179{
180 /* Disable timer */
181 at91_sys_write(AT91_PIT_MR, 0);
182}
Andrew Victor1a0ed732006-12-01 09:04:47 +0100183
184struct sys_timer at91sam926x_timer = {
Andrew Victorad48ce72008-04-16 20:43:49 +0100185 .init = at91sam926x_pit_init,
186 .suspend = at91sam926x_pit_suspend,
187 .resume = at91sam926x_pit_reset,
Andrew Victor1a0ed732006-12-01 09:04:47 +0100188};