Fix onda red. Unlike all other targets, it built the whole backlight driver in the...
[kugel-rb.git] / firmware / target / mips / ingenic_jz47xx / crt0.S
blob783773cf0cf27df1920d1bd652e39d38046698e6
1 /***************************************************************************
2  *             __________               __   ___.
3  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
4  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
5  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
6  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
7  *                     \/            \/     \/    \/            \/
8  * $Id$
9  *
10  * Copyright (C) 2008 by Maurus Cuelenaere
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18  * KIND, either express or implied.
19  *
20  ****************************************************************************/
23  * init.S
24  *
25  * Initialization code for JzRISC.
26  *
27  * Author: Seeger Chin
28  * e-mail: seeger.chin@gmail.com
29  *
30  * Copyright (C) 2006 Ingenic Semiconductor Inc.
31  * 
32  * This program is free software; you can redistribute it and/or modify
33  * it under the terms of the GNU General Public License version 2 as
34  * published by the Free Software Foundation.
35  *
36  */
38 #include "config.h"
39 #include "mips.h"
41     .text
43     .extern system_main
44     .extern main
45     .global _start
47     .section .init.text
48     .set    mips32
49     .set    noreorder
50     .set    noat
52 #ifdef BOOTLOADER
53     /* These will get filled in scramble */
54     .word 0 /* Unknown  */
55     .word 0 /* Filesize */
57     /* Relocate bootloader */
58     la     t0, (_loadaddress-0x400000)
59     la     t1, _loadaddress
60     la     t2, _bootend
61 _relocate_loop:
62     lw     t3, 0(t0)
63     sw     t3, 0(t1)
64     addiu  t1, 4
65     bne    t1, t2, _relocate_loop
66     addiu  t0, 4
67 #endif
69 _start:
70     la      ra, _start
71     /*
72       ----------------------------------------------------
73        Init CP0 registers.
74       ----------------------------------------------------
75     */
76     mtc0    zero, C0_WATCHLO
77     mtc0    zero, C0_WATCHHI
79     li      t0, (M_StatusBEV  | M_StatusIM7 | M_StatusIM6 \
80                 | M_StatusIM5 | M_StatusIM4 | M_StatusIM3 \
81                 | M_StatusIM2 | M_StatusERL)
82              /*
83                  BEV = Enable Boot Exception Vectors
84                  IMx = Interrupt mask
85                  ERL = Denotes error level
86              */
87     mtc0    t0, C0_STATUS
89     li      t0, M_CauseIV
90     mtc0    t0, C0_CAUSE
92     /*
93       ----------------------------------------------------
94        Init caches, assumes a 4way*128set*32byte I/D cache
95       ----------------------------------------------------
96     */
97     li      t0, 3            # enable cache for kseg0 accesses
98     mtc0    t0, C0_CONFIG    # CONFIG reg
99     la      t0, 0x80000000   # an idx op should use an unmappable address
100     ori     t1, t0, 0x4000   # 16kB cache
101     mtc0    zero, C0_TAGLO   # TAGLO reg
102     mtc0    zero, C0_TAGHI   # TAGHI reg
104 _cache_loop:
105     cache   0x8, 0(t0)       # index store icache tag
106     cache   0x9, 0(t0)       # index store dcache tag
107     bne     t0, t1, _cache_loop
108     addiu   t0, t0, 0x20     # 32 bytes per cache line
109     nop
111     /*
112       ----------------------------------------------------
113        Invalidate BTB
114       ----------------------------------------------------
115     */
116     mfc0    t0, C0_CONFIG
117     nop
118     ori     t0, 2
119     mtc0    t0, C0_CONFIG
120     nop
121     
122     /*
123       ----------------------------------------------------
124        Copy IRAM section
125         * copy IRAM first before BSS gets cleared, as both
126           have the same address
127       ----------------------------------------------------
128     */
129     la     t0, _iramcopy
130     la     t1, _iramstart
131     la     t2, _iramend
132 _iram_loop:
133     lw     t3, 0(t0)
134     sw     t3, 0(t1)
135     addiu  t1, 4
136     bne    t1, t2, _iram_loop
137     addiu  t0, 4
138     
139     /*
140       ----------------------------------------------------
141        Clear BSS section
142       ----------------------------------------------------
143     */
144     la     t0, _edata
145     la     t1, _end
146 _bss_loop:
147     sw     zero, 0(t0)
148     bne    t0, t1, _bss_loop
149     addiu  t0, 4
150     
151     /*
152       ----------------------------------------------------
153        Clear IBSS section
154       ----------------------------------------------------
155     */
156     la     t0, _iedata
157     la     t1, _iend
158 _ibss_loop:
159     sw     zero, 0(t0)
160     bne    t0, t1, _ibss_loop
161     addiu  t0, 4
163     /*
164       ----------------------------------------------------
165        Set up stack
166       ----------------------------------------------------
167     */
168     la      sp, stackend
169     la      t0, stackbegin
170     li      t1, 0xDEADBEEF
171     
172 _stack_loop:    
173     sw      t1, 0(t0)
174     bne     t0, sp, _stack_loop
175     addiu   t0, t0, 4
177     /*
178       ----------------------------------------------------
179        Jump to C code
180       ----------------------------------------------------
181     */
182     jal     system_main  /* Init clocks etc first */
183     nop
184     j       main
185     nop
187     
188     /*
189      * 0x0   - Simple TLB refill handler
190      * 0x100 - Cache error handler
191      * 0x180 - Exception/Interrupt handler
192      * 0x200 - Special Exception Interrupt handler (when IV is set in CP0_CAUSE)
193      */
194     
195     .section .vectors.1, "ax", %progbits
196     j      tlb_refill_handler
197     nop
198     
199     .section .vectors.2, "ax", %progbits
200     j      real_exception_handler
201     nop
202     
203     .section .vectors.3, "ax", %progbits
204     j      real_exception_handler
205     nop
206     
207     .section .vectors.4, "ax", %progbits
208     j      real_exception_handler
209     nop
211     .section .vectors, "ax", %progbits
212 real_exception_handler:
213     addiu   sp, -0x80
214     sw      ra,    0(sp)
215     sw      fp,    4(sp)
216     sw      gp,    8(sp)
217     sw      t9,  0xC(sp)
218     sw      t8, 0x10(sp)
219     sw      s7, 0x14(sp)
220     sw      s6, 0x18(sp)
221     sw      s5, 0x1C(sp)
222     sw      s4, 0x20(sp)
223     sw      s3, 0x24(sp)
224     sw      s2, 0x28(sp)
225     sw      s1, 0x2C(sp)
226     sw      s0, 0x30(sp)
227     sw      t7, 0x34(sp)
228     sw      t6, 0x38(sp)
229     sw      t5, 0x3C(sp)
230     sw      t4, 0x40(sp)
231     sw      t3, 0x44(sp)
232     sw      t2, 0x48(sp)
233     sw      t1, 0x4C(sp)
234     sw      t0, 0x50(sp)
235     sw      a3, 0x54(sp)
236     sw      a2, 0x58(sp)
237     sw      a1, 0x5C(sp)
238     sw      a0, 0x60(sp)
239     sw      v1, 0x64(sp)
240     sw      v0, 0x68(sp)
241     sw      $1, 0x6C(sp)
242     mflo    k0
243     nop
244     sw      k0, 0x70(sp)
245     mfhi    k0
246     nop
247     sw      k0, 0x74(sp)
248     mfc0    k0, C0_STATUS
249     sll     zero, 1
250     sll     zero, 1
251     sll     zero, 1
252     sll     zero, 1
253     sw      k0, 0x78(sp)
254     mfc0    k0, C0_EPC
255     sll     zero, 1
256     sll     zero, 1
257     sll     zero, 1
258     sll     zero, 1
259     sw      k0, 0x7C(sp)
260     
261     li      k1, M_CauseExcCode
262     mfc0    k0, C0_CAUSE
263     and     k0, k1
264     beq     zero, k0, _int
265     nop
266     j       _exception
267     nop
268     
269 _int:
270     jal     intr_handler
271     nop
272     j       _exception_return
273     
274 _exception:
275     move    a0, sp
276     mfc0    a1, C0_CAUSE
277     sll     zero, 1
278     sll     zero, 1
279     sll     zero, 1
280     sll     zero, 1
281     mfc0    a2, C0_EPC
282     sll     zero, 1
283     sll     zero, 1
284     sll     zero, 1
285     sll     zero, 1
286     jal     exception_handler
287     nop
289 _exception_return:
290     lw      ra,    0(sp)
291     lw      fp,    4(sp)
292     lw      gp,    8(sp)
293     lw      t9,  0xC(sp)
294     lw      t8, 0x10(sp)
295     lw      s7, 0x14(sp)
296     lw      s6, 0x18(sp)
297     lw      s5, 0x1C(sp)
298     lw      s4, 0x20(sp)
299     lw      s3, 0x24(sp)
300     lw      s2, 0x28(sp)
301     lw      s1, 0x2C(sp)
302     lw      s0, 0x30(sp)
303     lw      t7, 0x34(sp)
304     lw      t6, 0x38(sp)
305     lw      t5, 0x3C(sp)
306     lw      t4, 0x40(sp)
307     lw      t3, 0x44(sp)
308     lw      t2, 0x48(sp)
309     lw      t1, 0x4C(sp)
310     lw      t0, 0x50(sp)
311     lw      a3, 0x54(sp)
312     lw      a2, 0x58(sp)
313     lw      a1, 0x5C(sp)
314     lw      a0, 0x60(sp)
315     lw      v1, 0x64(sp)
316     lw      v0, 0x68(sp)
317     lw      $1, 0x6C(sp)
318     lw      k0, 0x70(sp)
319     mtlo    k0
320     nop
321     lw      k0, 0x74(sp)
322     mthi    k0
323     nop
324     lw      k0, 0x78(sp)
325     mtc0    k0, C0_STATUS
326     nop
327     sll     zero, 1
328     sll     zero, 1
329     sll     zero, 1
330     sll     zero, 1
331     lw      k0, 0x7C(sp)
332     mtc0    k0, C0_EPC
333     nop
334     sll     zero, 1
335     sll     zero, 1
336     sll     zero, 1
337     sll     zero, 1
338     addiu   sp, 0x80
339     eret
340     nop
341     .set reorder
342     .set at