at91rm9200: Reset update
[oweals/u-boot.git] / cpu / arm920t / at91rm9200 / interrupts.c
1 /*
2  * (C) Copyright 2002
3  * Lineo, Inc. <www.lineo.com>
4  * Bernhard Kuhn <bkuhn@lineo.com>
5  *
6  * (C) Copyright 2002
7  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8  * Marius Groeger <mgroeger@sysgo.de>
9  *
10  * (C) Copyright 2002
11  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
12  * Alex Zuepke <azu@sysgo.de>
13  *
14  * See file CREDITS for list of people who contributed to this
15  * project.
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
30  * MA 02111-1307 USA
31  */
32
33 #include <common.h>
34 /*#include <asm/io.h>*/
35 #include <asm/arch/hardware.h>
36 /*#include <asm/proc/ptrace.h>*/
37
38 /* the number of clocks per CONFIG_SYS_HZ */
39 #define TIMER_LOAD_VAL (CONFIG_SYS_HZ_CLOCK/CONFIG_SYS_HZ)
40
41 /* macro to read the 16 bit timer */
42 #define READ_TIMER (tmr->TC_CV & 0x0000ffff)
43 AT91PS_TC tmr;
44
45 static ulong timestamp;
46 static ulong lastinc;
47
48 void board_reset(void) __attribute__((__weak__));
49
50 int interrupt_init (void)
51 {
52         tmr = AT91C_BASE_TC0;
53
54         /* enables TC1.0 clock */
55         *AT91C_PMC_PCER = 1 << AT91C_ID_TC0;  /* enable clock */
56
57         *AT91C_TCB0_BCR = 0;
58         *AT91C_TCB0_BMR = AT91C_TCB_TC0XC0S_NONE | AT91C_TCB_TC1XC1S_NONE | AT91C_TCB_TC2XC2S_NONE;
59         tmr->TC_CCR = AT91C_TC_CLKDIS;
60 #define AT91C_TC_CMR_CPCTRG (1 << 14)
61         /* set to MCLK/2 and restart the timer when the vlaue in TC_RC is reached */
62         tmr->TC_CMR = AT91C_TC_TIMER_DIV1_CLOCK | AT91C_TC_CMR_CPCTRG;
63
64         tmr->TC_IDR = ~0ul;
65         tmr->TC_RC = TIMER_LOAD_VAL;
66         lastinc = 0;
67         tmr->TC_CCR = AT91C_TC_SWTRG | AT91C_TC_CLKEN;
68         timestamp = 0;
69
70         return (0);
71 }
72
73 /*
74  * timer without interrupts
75  */
76
77 void reset_timer (void)
78 {
79         reset_timer_masked ();
80 }
81
82 ulong get_timer (ulong base)
83 {
84         return get_timer_masked () - base;
85 }
86
87 void set_timer (ulong t)
88 {
89         timestamp = t;
90 }
91
92 void udelay (unsigned long usec)
93 {
94         udelay_masked(usec);
95 }
96
97 void reset_timer_masked (void)
98 {
99         /* reset time */
100         lastinc = READ_TIMER;
101         timestamp = 0;
102 }
103
104 ulong get_timer_raw (void)
105 {
106         ulong now = READ_TIMER;
107
108         if (now >= lastinc) {
109                 /* normal mode */
110                 timestamp += now - lastinc;
111         } else {
112                 /* we have an overflow ... */
113                 timestamp += now + TIMER_LOAD_VAL - lastinc;
114         }
115         lastinc = now;
116
117         return timestamp;
118 }
119
120 ulong get_timer_masked (void)
121 {
122         return get_timer_raw()/TIMER_LOAD_VAL;
123 }
124
125 void udelay_masked (unsigned long usec)
126 {
127         ulong tmo;
128         ulong endtime;
129         signed long diff;
130
131         tmo = CONFIG_SYS_HZ_CLOCK / 1000;
132         tmo *= usec;
133         tmo /= 1000;
134
135         endtime = get_timer_raw () + tmo;
136
137         do {
138                 ulong now = get_timer_raw ();
139                 diff = endtime - now;
140         } while (diff >= 0);
141 }
142
143 /*
144  * This function is derived from PowerPC code (read timebase as long long).
145  * On ARM it just returns the timer value.
146  */
147 unsigned long long get_ticks(void)
148 {
149         return get_timer(0);
150 }
151
152 /*
153  * This function is derived from PowerPC code (timebase clock frequency).
154  * On ARM it returns the number of timer ticks per second.
155  */
156 ulong get_tbclk (void)
157 {
158         ulong tbclk;
159
160         tbclk = CONFIG_SYS_HZ;
161         return tbclk;
162 }
163
164 /*
165  * Reset the cpu by setting up the watchdog timer and let him time out
166  * or toggle a GPIO pin on the AT91RM9200DK board
167  */
168 void reset_cpu (ulong ignored)
169 {
170
171 #if defined(CONFIG_AT91RM9200_USART)
172         /*shutdown the console to avoid strange chars during reset */
173         serial_exit();
174 #endif
175
176         if (board_reset)
177                 board_reset();
178
179         /* this is the way Linux does it */
180
181         /* FIXME:
182          * These defines should be moved into
183          * include/asm-arm/arch-at91rm9200/AT91RM9200.h
184          * as soon as the whitespace fix gets applied.
185          */
186         #define AT91C_ST_RSTEN (0x1 << 16)
187         #define AT91C_ST_EXTEN (0x1 << 17)
188         #define AT91C_ST_WDRST (0x1 <<  0)
189         #define ST_WDMR *((unsigned long *)0xfffffd08)  /* watchdog mode register */
190         #define ST_CR *((unsigned long *)0xfffffd00)    /* system clock control register */
191
192         ST_WDMR = AT91C_ST_RSTEN | AT91C_ST_EXTEN | 1 ;
193         ST_CR = AT91C_ST_WDRST;
194
195         while (1);
196         /* Never reached */
197 }