1 /*
2  * linux/include/asm-arm/arch-at91rm9200/time.h
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 #ifndef __ASM_ARCH_TIME_H
23 #define __ASM_ARCH_TIME_H
24 
25 #include <asm/system.h>
26 
27 extern unsigned long (*gettimeoffset)(void);
28 
29 /*
30  * Returns number of microseconds since last timer interrupt.  Note that interrupts
31  * will have been disabled by do_gettimeofday()
32  *  'LATCH' is hwclock ticks (see CLOCK_TICK_RATE in timex.h) per jiffy.
33  *  'tick' is usecs per jiffy (linux/timex.h).
34  */
at91rm9200_gettimeoffset(void)35 static unsigned long at91rm9200_gettimeoffset(void)
36 {
37 	unsigned long elapsed;
38 
39 	elapsed = (AT91_SYS->ST_CRTR - AT91_SYS->ST_RTAR) & AT91C_ST_ALMV;
40 
41 	return (unsigned long)(elapsed * tick) / LATCH;
42 }
43 
44 /*
45  * IRQ handler for the timer.
46  */
at91rm9200_timer_interrupt(int irq,void * dev_id,struct pt_regs * regs)47 static void at91rm9200_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
48 {
49 	if (AT91_SYS->ST_SR & AT91C_ST_PITS) {	/* This is a shared interrupt */
50 		do {
51 			do_timer(regs);
52 
53 			AT91_SYS->ST_RTAR = (AT91_SYS->ST_RTAR + LATCH) & AT91C_ST_ALMV;
54 
55 		} while (((AT91_SYS->ST_CRTR - AT91_SYS->ST_RTAR) & AT91C_ST_ALMV) >= LATCH);
56 
57 		do_profile(regs);
58 	}
59 }
60 
61 /*
62  * Set up timer interrupt.
63  */
setup_timer(void)64 static inline void setup_timer(void)
65 {
66 	/* Disable all timer interrupts */
67 	AT91_SYS->ST_IDR = AT91C_ST_PITS | AT91C_ST_WDOVF | AT91C_ST_RTTINC | AT91C_ST_ALMS;
68 	(void) AT91_SYS->ST_SR;		/* Clear any pending interrupts */
69 
70 	/*
71 	 * Make IRQs happen for the system timer.
72 	 */
73 	timer_irq.handler = at91rm9200_timer_interrupt;
74 	timer_irq.flags = SA_SHIRQ | SA_INTERRUPT;
75 	setup_arm_irq(AT91C_ID_SYS, &timer_irq);
76 	gettimeoffset = at91rm9200_gettimeoffset;
77 
78 	/* Set initial alarm to 0 */
79 	AT91_SYS->ST_RTAR = 0;
80 
81 	/* Real time counter incremented every 30.51758 microseconds */
82 	AT91_SYS->ST_RTMR = 1;
83 
84 	/* Set Period Interval timer */
85 	AT91_SYS->ST_PIMR = LATCH;
86 
87 	/* Change the kernel's 'tick' value to 10009 usec. (the default is 10000) */
88 	tick = (LATCH * 1000000) / CLOCK_TICK_RATE;
89 
90 	/* Enable Period Interval Timer interrupt */
91 	AT91_SYS->ST_IER = AT91C_ST_PITS;
92 }
93 
94 #endif
95