Add SAM7 I2C driver.
[bertos.git] / bertos / cpu / arm / hw / crtat91sam7_rom.S
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \version $Id: $
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * \brief AT91SAM7S256 CRT, adapted from NUt/OS, see license below.
38  */
39
40 /*
41  * Copyright (C) 2005-2007 by egnite Software GmbH. All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  *
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  * 3. Neither the name of the copyright holders nor the names of
53  *    contributors may be used to endorse or promote products derived
54  *    from this software without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
57  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
58  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
59  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
60  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
61  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
62  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
63  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
64  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
65  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
66  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  * For additional information see http://www.ethernut.de/
70  *
71  */
72
73 #include "hw_cpu.h"
74 #include <cpu/detect.h>
75
76
77 #if CLOCK_FREQ != 48023000L
78 #error Clock registers set for 48MHz operation, revise following code if you want a different clock.
79 #endif
80
81
82 #if CPU_ARM_AT91SAM7S256 || CPU_ARM_AT91SAM7X256
83         /**
84         * With a 18.420MHz cristal, master clock is:
85         * (((18.420 * PLL_MUL_VAL + 1) / PLL_DIV_VAL) / AT91MCK_PRES) = 48.023MHz
86         */
87         #define PLL_MUL_VAL  72  /**< Real multiplier value is PLL_MUL_VAL + 1! */
88         #define PLL_DIV_VAL  14
89         #define AT91MCK_PRES PMC_PRES_CLK_2
90
91         /**
92         * Register I/O adresses.
93         * \{
94         */
95         #define MC_BASE             0xFFFFFF00
96         #define MC_FMR_OFF          0x00000060
97         #define MC_FWS_2R3W         0x00000100
98
99         #define AIC_BASE            0xFFFFF000
100         #define AIC_EOICR_OFF       0x00000130
101         #define AIC_IDCR_OFF        0x00000124
102
103         #define WDT_BASE            0xFFFFFD40
104         #define WDT_MR_OFF          0x00000004
105         #define WDT_WDDIS            (1 << 15)
106
107         #define PMC_BASE            0xFFFFFC00
108         #define PMC_SR_OFF          0x00000068
109         #define PMC_MCKR_OFF        0x00000030
110         #define PMC_MOSCS             (1 << 0)
111         #define PMC_LOCK              (1 << 2)
112         #define PMC_MCKRDY            (1 << 3)
113         #define PMC_CSS_PLL_CLK     0x00000003
114         #define PMC_PRES_CLK_2      0x00000004
115
116         #define CKGR_MOR_OFF        0x00000020
117         #define CKGR_PLLR_OFF       0x0000002C
118         #define CKGR_MOSCEN           (1 << 0)
119         #define CKGR_MUL_SHIFT              16
120         #define CKGR_PLLCOUNT_SHIFT          8
121
122         #define RSTC_MR             0xFFFFFD08
123         #define RSTC_KEY            0xA5000000
124         #define RSTC_URSTEN           (1 << 0)
125
126         #define ARM_MODE_FIQ              0x11
127         #define ARM_MODE_IRQ              0x12
128         #define ARM_MODE_SVC              0x13
129         #define ARM_MODE_ABORT            0x17
130         #define ARM_MODE_UNDEF            0x1B
131
132 #else
133         #error No register I/O definition for selected ARM CPU
134 #endif
135 /*\}*/
136
137 /*
138  * Section 0: Vector table and reset entry.
139  */
140         .section .vectors,"ax",%progbits
141
142         .global __vectors
143 __vectors:
144         ldr     pc, [pc, #24]   /* Reset */
145         ldr     pc, [pc, #24]   /* Undefined instruction */
146         ldr     pc, [pc, #24]   /* Software interrupt */
147         ldr     pc, [pc, #24]   /* Prefetch abort */
148         ldr     pc, [pc, #24]   /* Data abort */
149         ldr     pc, [pc, #24]   /* Reserved */
150
151         /*
152          * On IRQ the PC will be loaded from AIC_IVR, which
153          * provides the address previously set in AIC_SVR.
154          * The interrupt routine will be called in ARM_MODE_IRQ
155          * with IRQ disabled and FIQ unchanged.
156          */
157         ldr     pc, [pc, #-0xF20]   /* Interrupt request, auto vectoring. */
158         ldr     pc, [pc, #-0xF20]   /* Fast interrupt request, auto vectoring. */
159
160         .word   _init
161         .word   __undef
162         .word   __swi
163         .word   __prefetch_abort
164         .word   __data_abort
165
166         .weak   __undef
167         .set    __undef, __xcpt_dummy_undef
168         .weak   __swi
169         .set    __swi, __xcpt_dummy_swi
170         .weak   __prefetch_abort
171         .set    __prefetch_abort, __xcpt_dummy_pref
172         .weak   __data_abort
173         .set    __data_abort, __xcpt_dummy_dab
174
175 /**        .global __xcpt_dummy*/
176 __xcpt_dummy_undef:
177         b       __xcpt_dummy_undef
178
179 __xcpt_dummy_swi:
180         b       __xcpt_dummy_swi
181
182 __xcpt_dummy_pref:
183         b       __xcpt_dummy_pref
184
185 __xcpt_dummy_dab:
186         b       __xcpt_dummy_dab
187
188
189         .ltorg
190 /*
191  * Hardware initialization.
192  */
193         .section .init, "ax", %progbits
194         .globl  _init
195 _init:
196         /*
197          * Use 2 cycles for flash access.
198          */
199         ldr     r1, =MC_BASE
200         mov     r0, #MC_FWS_2R3W
201         str     r0, [r1, #MC_FMR_OFF]
202
203         /*
204          * Disable all interrupts. Useful for debugging w/o target reset.
205          */
206         ldr     r1, =AIC_BASE
207         mvn     r0, #0
208         str     r0, [r1, #AIC_EOICR_OFF]
209         str     r0, [r1, #AIC_IDCR_OFF]
210
211         /*
212          * The watchdog is enabled after processor reset. Disable it.
213          */
214         ldr     r1, =WDT_BASE
215         ldr     r0, =WDT_WDDIS
216         str     r0, [r1, #WDT_MR_OFF]
217
218         /*
219          * Enable the main oscillator. Set startup time of 6 * 8 slow
220          * clock cycles and wait until oscillator is stabilized.
221          */
222         ldr     r1, =PMC_BASE
223         mov     r0, #(6 << 8)
224         orr     r0, r0, #CKGR_MOSCEN
225         str     r0, [r1, #CKGR_MOR_OFF]
226 wait_moscs:
227         ldr     r0, [r1, #PMC_SR_OFF]
228         tst     r0, #PMC_MOSCS
229         beq     wait_moscs
230
231         /*
232          * Set PLL:
233          * PLLfreq = crystal / divider * (multiplier + 1)
234          * Wait 28 clock cycles until PLL is locked.
235          */
236         ldr     r0, =((PLL_MUL_VAL << CKGR_MUL_SHIFT) | (28 << CKGR_PLLCOUNT_SHIFT) | PLL_DIV_VAL)
237
238         str     r0, [r1, #CKGR_PLLR_OFF]
239 wait_lock:
240         ldr     r0, [r1, #PMC_SR_OFF]
241         tst     r0, #PMC_LOCK
242         beq     wait_lock
243
244         /*
245          * Set master clock prescaler.
246          */
247         mov     r0, #AT91MCK_PRES
248         str     r0, [r1, #PMC_MCKR_OFF]
249 wait_presrdy:
250         ldr     r0, [r1, #PMC_SR_OFF]
251         tst     r0, #PMC_MCKRDY
252         beq     wait_presrdy
253
254         /*
255          * Switch to PLL clock. Trying to set this together with the
256          * prescaler fails (see datasheets).
257          */
258         ldr     r0, [r1, #PMC_MCKR_OFF]
259         orr     r0, r0, #PMC_CSS_PLL_CLK
260         str     r0, [r1, #PMC_MCKR_OFF]
261 wait_pllsel:
262         ldr     r0, [r1, #PMC_SR_OFF]
263         tst     r0, #PMC_MCKRDY
264         beq     wait_pllsel
265
266         /*
267          * Enable external reset key.
268          */
269         ldr     r0, =(RSTC_KEY | RSTC_URSTEN)
270         ldr     r1, =RSTC_MR
271         str     r0, [r1, #0]
272
273         /*
274          * Set exception stack pointers
275          */
276         ldr     r0, =__stack_fiq_end
277         msr     CPSR_c, #ARM_MODE_FIQ | 0xC0
278         mov     r13, r0
279         ldr     r0, =__stack_irq_end
280         msr     CPSR_c, #ARM_MODE_IRQ | 0xC0
281         mov     r13, r0
282         ldr     r0, =__stack_abt_end
283         msr     CPSR_c, #ARM_MODE_ABORT | 0xC0
284         mov     r13, r0
285         ldr     r0, =__stack_und_end
286         msr     CPSR_c, #ARM_MODE_UNDEF | 0xC0
287         mov     r13, r0
288         ldr     r0, =__stack_svc_end
289         msr     CPSR_c, #ARM_MODE_SVC | 0xC0
290         mov     r13, r0
291
292         /*
293          * Clear .bss
294          */
295         ldr     r1, =__bss_start
296         ldr     r2, =__bss_end
297         ldr     r3, =0
298
299 _40:
300         cmp     r1, r2
301         strne   r3, [r1], #+4
302         bne     _40
303
304         /*
305          * Relocate .data section (Copy from ROM to RAM).
306          */
307         ldr     r1, =__etext
308         ldr     r2, =__data_start
309         ldr     r3, =__data_end
310
311 _41:
312         cmp     r2, r3
313         ldrlo   r0, [r1], #4
314         strlo   r0, [r2], #4
315         blo     _41
316
317         /*
318          * Initialize user stack pointer.
319          */
320         ldr     r13, =__stack_end
321
322         /*
323          * Jump to main
324          */
325         ldr     r0, =main
326         bx      r0
327
328 End:
329         b       End
330
331         .ltorg