blob: 8e9c049c8b7c3d6c8b30cec0ca9831e5795af6a9 [file] [log] [blame]
SAN People73a59c12006-01-09 17:05:41 +00001/*
Andrew Victor8fc5ffa2006-06-29 16:06:33 +01002 * linux/arch/arm/mach-at91rm9200/at91rm9200_time.c
SAN People73a59c12006-01-09 17:05:41 +00003 *
4 * Copyright (C) 2003 SAN People
5 * Copyright (C) 2003 ATMEL
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
SAN People73a59c12006-01-09 17:05:41 +000022#include <linux/init.h>
23#include <linux/interrupt.h>
Thomas Gleixner07d265d2006-07-01 23:01:50 +010024#include <linux/irq.h>
SAN People73a59c12006-01-09 17:05:41 +000025#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/time.h>
28
29#include <asm/hardware.h>
30#include <asm/io.h>
SAN People73a59c12006-01-09 17:05:41 +000031#include <asm/mach/time.h>
32
Andrew Victor55d8bae2006-11-30 17:16:43 +010033#include <asm/arch/at91_st.h>
34
Andrew Victor963151f2006-06-19 15:23:41 +010035static unsigned long last_crtr;
36
SAN People73a59c12006-01-09 17:05:41 +000037/*
38 * The ST_CRTR is updated asynchronously to the master clock. It is therefore
39 * necessary to read it twice (with the same value) to ensure accuracy.
40 */
41static inline unsigned long read_CRTR(void) {
42 unsigned long x1, x2;
43
44 do {
45 x1 = at91_sys_read(AT91_ST_CRTR);
46 x2 = at91_sys_read(AT91_ST_CRTR);
47 } while (x1 != x2);
48
49 return x1;
50}
51
52/*
53 * Returns number of microseconds since last timer interrupt. Note that interrupts
54 * will have been disabled by do_gettimeofday()
55 * 'LATCH' is hwclock ticks (see CLOCK_TICK_RATE in timex.h) per jiffy.
56 * 'tick' is usecs per jiffy (linux/timex.h).
57 */
58static unsigned long at91rm9200_gettimeoffset(void)
59{
60 unsigned long elapsed;
61
Andrew Victor963151f2006-06-19 15:23:41 +010062 elapsed = (read_CRTR() - last_crtr) & AT91_ST_ALMV;
SAN People73a59c12006-01-09 17:05:41 +000063
64 return (unsigned long)(elapsed * (tick_nsec / 1000)) / LATCH;
65}
66
67/*
68 * IRQ handler for the timer.
69 */
Linus Torvalds0cd61b62006-10-06 10:53:39 -070070static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id)
SAN People73a59c12006-01-09 17:05:41 +000071{
SAN People73a59c12006-01-09 17:05:41 +000072 if (at91_sys_read(AT91_ST_SR) & AT91_ST_PITS) { /* This is a shared interrupt */
73 write_seqlock(&xtime_lock);
74
Andrew Victor963151f2006-06-19 15:23:41 +010075 while (((read_CRTR() - last_crtr) & AT91_ST_ALMV) >= LATCH) {
Linus Torvalds0cd61b62006-10-06 10:53:39 -070076 timer_tick();
Andrew Victor963151f2006-06-19 15:23:41 +010077 last_crtr = (last_crtr + LATCH) & AT91_ST_ALMV;
Andrew Victor39806802006-03-22 20:14:13 +000078 }
SAN People73a59c12006-01-09 17:05:41 +000079
80 write_sequnlock(&xtime_lock);
81
82 return IRQ_HANDLED;
83 }
84 else
85 return IRQ_NONE; /* not handled */
86}
87
88static struct irqaction at91rm9200_timer_irq = {
89 .name = "at91_tick",
Thomas Gleixner52e405e2006-07-03 02:20:05 +020090 .flags = IRQF_SHARED | IRQF_DISABLED | IRQF_TIMER,
SAN People73a59c12006-01-09 17:05:41 +000091 .handler = at91rm9200_timer_interrupt
92};
93
Andrew Victor2a6f9902006-06-19 15:26:50 +010094void at91rm9200_timer_reset(void)
95{
96 last_crtr = 0;
97
98 /* Real time counter incremented every 30.51758 microseconds */
99 at91_sys_write(AT91_ST_RTMR, 1);
100
101 /* Set Period Interval timer */
102 at91_sys_write(AT91_ST_PIMR, LATCH);
103
104 /* Enable Period Interval Timer interrupt */
105 at91_sys_write(AT91_ST_IER, AT91_ST_PITS);
106}
107
SAN People73a59c12006-01-09 17:05:41 +0000108/*
109 * Set up timer interrupt.
110 */
111void __init at91rm9200_timer_init(void)
112{
113 /* Disable all timer interrupts */
114 at91_sys_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS);
115 (void) at91_sys_read(AT91_ST_SR); /* Clear any pending interrupts */
116
Andrew Victor2a6f9902006-06-19 15:26:50 +0100117 /* Make IRQs happen for the system timer */
SAN People73a59c12006-01-09 17:05:41 +0000118 setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq);
119
SAN People73a59c12006-01-09 17:05:41 +0000120 /* Change the kernel's 'tick' value to 10009 usec. (the default is 10000) */
121 tick_usec = (LATCH * 1000000) / CLOCK_TICK_RATE;
122
Andrew Victor2a6f9902006-06-19 15:26:50 +0100123 /* Initialize and enable the timer interrupt */
124 at91rm9200_timer_reset();
SAN People73a59c12006-01-09 17:05:41 +0000125}
126
Andrew Victor2a6f9902006-06-19 15:26:50 +0100127#ifdef CONFIG_PM
128static void at91rm9200_timer_suspend(void)
129{
130 /* disable Period Interval Timer interrupt */
131 at91_sys_write(AT91_ST_IDR, AT91_ST_PITS);
132}
133#else
134#define at91rm9200_timer_suspend NULL
135#endif
136
SAN People73a59c12006-01-09 17:05:41 +0000137struct sys_timer at91rm9200_timer = {
138 .init = at91rm9200_timer_init,
139 .offset = at91rm9200_gettimeoffset,
Andrew Victor2a6f9902006-06-19 15:26:50 +0100140 .suspend = at91rm9200_timer_suspend,
141 .resume = at91rm9200_timer_reset,
SAN People73a59c12006-01-09 17:05:41 +0000142};
Andrew Victor2a6f9902006-06-19 15:26:50 +0100143