Fix timer code for ARM systems: make sure that udelay() does not
[platform/kernel/u-boot.git] / cpu / at91rm9200 / cpu.c
1 /*
2  * (C) Copyright 2002
3  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
4  * Marius Groeger <mgroeger@sysgo.de>
5  *
6  * (C) Copyright 2002
7  * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8  * Alex Zuepke <azu@sysgo.de>
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 /*
30  * CPU specific code
31  */
32
33 #include <common.h>
34 #include <command.h>
35 #include <asm/io.h>
36 #include <asm/arch/hardware.h>
37
38 #if !defined(CONFIG_DBGU) && !defined(CONFIG_USART0) && !defined(CONFIG_USART1)
39 #error must define one of CONFIG_DBGU or CONFIG_USART0 or CONFIG_USART1
40 #endif
41
42 /* read co-processor 15, register #1 (control register) */
43 static unsigned long read_p15_c1(void)
44 {
45     unsigned long value;
46
47     __asm__ __volatile__(
48         "mrc     p15, 0, %0, c1, c0, 0   @ read control reg\n"
49         : "=r" (value)
50         :
51         : "memory");
52     /*printf("p15/c1 is = %08lx\n", value); */
53     return value;
54 }
55
56 /* write to co-processor 15, register #1 (control register) */
57 static void write_p15_c1(unsigned long value)
58 {
59     /*printf("write %08lx to p15/c1\n", value); */
60     __asm__ __volatile__(
61         "mcr     p15, 0, %0, c1, c0, 0   @ write it back\n"
62         : "=r" (value)
63         :
64         : "memory");
65
66     read_p15_c1();
67 }
68
69 static void cp_delay(void)
70 {
71     volatile int i;
72
73     /* copro seems to need some delay between reading and writing */
74     for (i=0; i<100; i++);
75 }
76 /* See also ARM Ref. Man. */
77 #define C1_MMU          (1<<0)  /* mmu off/on */
78 #define C1_ALIGN        (1<<1)  /* alignment faults off/on */
79 #define C1_IDC          (1<<2)  /* icache and/or dcache off/on */
80 #define C1_WRITE_BUFFER (1<<3)  /* write buffer off/on */
81 #define C1_BIG_ENDIAN   (1<<7)  /* big endian off/on */
82 #define C1_SYS_PROT     (1<<8)  /* system protection */
83 #define C1_ROM_PROT     (1<<9)  /* ROM protection */
84 #define C1_HIGH_VECTORS (1<<13) /* location of vectors: low/high addresses */
85
86 int cpu_init(void)
87 {
88         /*
89          * setup up stacks if necessary
90          */
91 #ifdef CONFIG_USE_IRQ
92         DECLARE_GLOBAL_DATA_PTR;
93
94         IRQ_STACK_START = _armboot_start - CFG_MALLOC_LEN - CFG_GBL_DATA_SIZE - 4;
95         FIQ_STACK_START = IRQ_STACK_START - CONFIG_STACKSIZE_IRQ;
96 #endif
97         return 0;
98 }
99
100 int cleanup_before_linux(void)
101 {
102     /*
103      * this function is called just before we call linux
104      * it prepares the processor for linux
105      *
106      * we turn off caches etc ...
107      * and we set the CPU-speed to 73 MHz - see start.S for details
108      */
109
110     disable_interrupts();
111     return 0;
112 }
113
114 int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
115 {
116
117 #ifdef CFG_SOFT_RESET
118     disable_interrupts();
119     reset_cpu(0);
120 #else
121 #ifdef CONFIG_DBGU
122    AT91PS_USART us = (AT91PS_USART) AT91C_BASE_DBGU;
123 #endif
124 #ifdef CONFIG_USART0
125    AT91PS_USART us = AT91C_BASE_US0;
126 #endif
127 #ifdef CONFIG_USART1
128    AT91PS_USART us = AT91C_BASE_US1;
129 #endif
130    AT91PS_PIO pio = AT91C_BASE_PIOA;
131
132    /*shutdown the console to avoid strange chars during reset */
133    us->US_CR = (AT91C_US_RSTRX | AT91C_US_RSTTX);
134
135 #ifdef CONFIG_AT91RM9200DK
136    /* Clear PA19 to trigger the hard reset */
137    pio->PIO_CODR = 0x00080000;
138    pio->PIO_OER  = 0x00080000;
139    pio->PIO_PER  = 0x00080000;
140 #endif
141 #ifdef CONFIG_CMC_PU2
142 /* this is the way Linux does it */
143 #define AT91C_ST_RSTEN (0x1 << 16)
144 #define AT91C_ST_EXTEN (0x1 << 17)
145 #define AT91C_ST_WDRST (0x1 <<  0)
146 /* watchdog mode register */
147 #define ST_WDMR *((unsigned long *)0xfffffd08)
148 /* system clock control register */
149 #define ST_CR *((unsigned long *)0xfffffd00)
150         ST_WDMR = AT91C_ST_RSTEN | AT91C_ST_EXTEN | 1 ;
151         ST_CR = AT91C_ST_WDRST;
152    /* Never reached */
153 #endif
154 #endif
155    return 0;
156 }
157
158 void icache_enable(void)
159 {
160     ulong reg;
161     reg = read_p15_c1();
162     cp_delay();
163     write_p15_c1(reg | C1_IDC);
164 }
165
166 void icache_disable(void)
167 {
168     ulong reg;
169     reg = read_p15_c1();
170     cp_delay();
171     write_p15_c1(reg & ~C1_IDC);
172 }
173
174 int icache_status(void)
175 {
176     return (read_p15_c1() & C1_IDC) != 0;
177     return 0;
178 }
179
180 void dcache_enable(void)
181 {
182     ulong reg;
183     reg = read_p15_c1();
184     cp_delay();
185     write_p15_c1(reg | C1_IDC);
186 }
187
188 void dcache_disable(void)
189 {
190     ulong reg;
191     reg = read_p15_c1();
192     cp_delay();
193     write_p15_c1(reg & ~C1_IDC);
194 }
195
196 int dcache_status(void)
197 {
198     return (read_p15_c1() & C1_IDC) != 0;
199     return 0;
200 }