Commit | Line | Data |
---|---|---|
73a59c1c SP |
1 | /* |
2 | * linux/arch/arm/mach-at91rm9200/time.c | |
3 | * | |
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 | ||
22 | #include <linux/config.h> | |
23 | #include <linux/init.h> | |
24 | #include <linux/interrupt.h> | |
25 | #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> | |
31 | #include <asm/irq.h> | |
32 | #include <asm/mach/time.h> | |
33 | ||
34 | /* | |
35 | * The ST_CRTR is updated asynchronously to the master clock. It is therefore | |
36 | * necessary to read it twice (with the same value) to ensure accuracy. | |
37 | */ | |
38 | static inline unsigned long read_CRTR(void) { | |
39 | unsigned long x1, x2; | |
40 | ||
41 | do { | |
42 | x1 = at91_sys_read(AT91_ST_CRTR); | |
43 | x2 = at91_sys_read(AT91_ST_CRTR); | |
44 | } while (x1 != x2); | |
45 | ||
46 | return x1; | |
47 | } | |
48 | ||
49 | /* | |
50 | * Returns number of microseconds since last timer interrupt. Note that interrupts | |
51 | * will have been disabled by do_gettimeofday() | |
52 | * 'LATCH' is hwclock ticks (see CLOCK_TICK_RATE in timex.h) per jiffy. | |
53 | * 'tick' is usecs per jiffy (linux/timex.h). | |
54 | */ | |
55 | static unsigned long at91rm9200_gettimeoffset(void) | |
56 | { | |
57 | unsigned long elapsed; | |
58 | ||
59 | elapsed = (read_CRTR() - at91_sys_read(AT91_ST_RTAR)) & AT91_ST_ALMV; | |
60 | ||
61 | return (unsigned long)(elapsed * (tick_nsec / 1000)) / LATCH; | |
62 | } | |
63 | ||
64 | /* | |
65 | * IRQ handler for the timer. | |
66 | */ | |
67 | static irqreturn_t at91rm9200_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs) | |
68 | { | |
69 | unsigned long rtar; | |
70 | ||
71 | if (at91_sys_read(AT91_ST_SR) & AT91_ST_PITS) { /* This is a shared interrupt */ | |
72 | write_seqlock(&xtime_lock); | |
73 | ||
39806805 | 74 | while (((read_CRTR() - at91_sys_read(AT91_ST_RTAR)) & AT91_ST_ALMV) >= LATCH) { |
73a59c1c SP |
75 | timer_tick(regs); |
76 | rtar = (at91_sys_read(AT91_ST_RTAR) + LATCH) & AT91_ST_ALMV; | |
77 | at91_sys_write(AT91_ST_RTAR, rtar); | |
39806805 | 78 | } |
73a59c1c SP |
79 | |
80 | write_sequnlock(&xtime_lock); | |
81 | ||
82 | return IRQ_HANDLED; | |
83 | } | |
84 | else | |
85 | return IRQ_NONE; /* not handled */ | |
86 | } | |
87 | ||
88 | static struct irqaction at91rm9200_timer_irq = { | |
89 | .name = "at91_tick", | |
90 | .flags = SA_SHIRQ | SA_INTERRUPT, | |
91 | .handler = at91rm9200_timer_interrupt | |
92 | }; | |
93 | ||
94 | /* | |
95 | * Set up timer interrupt. | |
96 | */ | |
97 | void __init at91rm9200_timer_init(void) | |
98 | { | |
99 | /* Disable all timer interrupts */ | |
100 | at91_sys_write(AT91_ST_IDR, AT91_ST_PITS | AT91_ST_WDOVF | AT91_ST_RTTINC | AT91_ST_ALMS); | |
101 | (void) at91_sys_read(AT91_ST_SR); /* Clear any pending interrupts */ | |
102 | ||
103 | /* | |
104 | * Make IRQs happen for the system timer. | |
105 | */ | |
106 | setup_irq(AT91_ID_SYS, &at91rm9200_timer_irq); | |
107 | ||
108 | /* Set initial alarm to 0 */ | |
109 | at91_sys_write(AT91_ST_RTAR, 0); | |
110 | ||
111 | /* Real time counter incremented every 30.51758 microseconds */ | |
112 | at91_sys_write(AT91_ST_RTMR, 1); | |
113 | ||
114 | /* Set Period Interval timer */ | |
115 | at91_sys_write(AT91_ST_PIMR, LATCH); | |
116 | ||
117 | /* Change the kernel's 'tick' value to 10009 usec. (the default is 10000) */ | |
118 | tick_usec = (LATCH * 1000000) / CLOCK_TICK_RATE; | |
119 | ||
120 | /* Enable Period Interval Timer interrupt */ | |
121 | at91_sys_write(AT91_ST_IER, AT91_ST_PITS); | |
122 | } | |
123 | ||
124 | struct sys_timer at91rm9200_timer = { | |
125 | .init = at91rm9200_timer_init, | |
126 | .offset = at91rm9200_gettimeoffset, | |
127 | }; |