1/*
2 *  linux/include/asm-arm/arch-mx1ads/time.h
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 * Copyright (C) 2002 Shane Nay (shane@minirl.com)
19 */
20#include <asm/system.h>
21
22/*
23 * Where is the timer (VA)?
24 */
25#define TIMER0_VA_BASE (IO_ADDRESS(MX1ADS_TIM1_BASE)+0x00000000)
26#define TIMER1_VA_BASE (IO_ADDRESS(MX1ADS_TIM2_BASE)+0x00000000)
27
28/*
29 * How long is the timer interval?
30 *
31 * Note-
32 * Clocking is not accurate enough.  Need to change the input
33 * to CLKOUT, and fix what those values are.  However,
34 * first need to evaluate what a reasonable value is
35 * as several other things depend upon that clock.
36 *
37 */
38
39#define TIMER_RELOAD	(328)
40
41#define TICKS2USECS(x)	((x) * 30)
42
43#define TIM_32KHZ       0x08
44#define TIM_INTEN       0x10
45#define TIM_ENAB        0x01
46
47/*
48 * What does it look like?
49 */
50typedef struct TimerStruct {
51	unsigned long TimerControl;
52	unsigned long TimerPrescaler;
53	unsigned long TimerCompare;
54	unsigned long TimerCapture;
55	unsigned long TimerCounter;
56	unsigned long TimerClear;	/* Clear Status */
57} TimerStruct_t;
58
59extern unsigned long (*gettimeoffset) (void);
60
61/*
62 * Returns number of ms since last clock interrupt.  Note that interrupts
63 * will have been disabled by do_gettimeoffset()
64 */
65static unsigned long
66mx1ads_gettimeoffset(void)
67{
68	volatile TimerStruct_t *timer1 = (TimerStruct_t *) TIMER1_VA_BASE;
69	unsigned long ticks, status;
70
71	/*
72	 * Get the current number of ticks.  Note that there is a race
73	 * condition between us reading the timer and checking for
74	 * an interrupt.  We get around this by ensuring that the
75	 * counter has not reloaded between our two reads.
76	 */
77	ticks = timer1->TimerCounter;
78
79	/*
80	 * Interrupt pending?  If so, we've reloaded once already.
81	 */
82	if (timer1->TimerClear & 1)
83		ticks += TIMER_RELOAD;
84
85	/*
86	 * Convert the ticks to usecs
87	 */
88	return TICKS2USECS(ticks);
89}
90
91/*
92 * IRQ handler for the timer
93 */
94static void
95mx1ads_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
96{
97	volatile TimerStruct_t *timer1 =
98	    (volatile TimerStruct_t *) TIMER1_VA_BASE;
99	// ...clear the interrupt
100	if (timer1->TimerClear) {
101		timer1->TimerClear = 0x0;
102	}
103
104	do_timer(regs);
105	do_profile(regs);
106}
107
108/*
109 * Set up timer interrupt, and return the current time in seconds.
110 */
111static inline void
112setup_timer(void)
113{
114	volatile TimerStruct_t *timer0 =
115	    (volatile TimerStruct_t *) TIMER0_VA_BASE;
116	volatile TimerStruct_t *timer1 =
117	    (volatile TimerStruct_t *) TIMER1_VA_BASE;
118
119
120	timer_irq.handler = mx1ads_timer_interrupt;
121
122	/*
123	 * Initialise to a known state (all timers off, and timing reset)
124	 */
125	timer0->TimerControl = 0;
126	timer1->TimerControl = 0;
127	timer0->TimerPrescaler = 0;
128	timer1->TimerPrescaler = 0;
129
130	timer1->TimerCompare = 328;
131	timer1->TimerControl = (TIM_32KHZ | TIM_INTEN | TIM_ENAB);
132
133	/*
134	 * Make irqs happen for the system timer
135	 */
136	setup_arm_irq(TIM2_INT, &timer_irq);
137	gettimeoffset = mx1ads_gettimeoffset;
138}
139