OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [boot-loader-ethmac/] [start.S] - Blame information for rev 78

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 61 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  start.S                                                     //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  Assembly routines for boot-loader.                          //
10
//  As boot-loader is a stand-alone application, it needs a     //
11
//  simple start function written in assembly to call the       //
12
//  C code main() function.                                     //
13
//                                                              //
14
//  Author(s):                                                  //
15
//      - Conor Santifort, csantifort.amber@gmail.com           //
16
//                                                              //
17
//////////////////////////////////////////////////////////////////
18
//                                                              //
19
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
20
//                                                              //
21
// This source file may be used and distributed without         //
22
// restriction provided that this copyright statement is not    //
23
// removed from the file and that any derivative work contains  //
24
// the original copyright notice and the associated disclaimer. //
25
//                                                              //
26
// This source file is free software; you can redistribute it   //
27
// and/or modify it under the terms of the GNU Lesser General   //
28
// Public License as published by the Free Software Foundation; //
29
// either version 2.1 of the License, or (at your option) any   //
30
// later version.                                               //
31
//                                                              //
32
// This source is distributed in the hope that it will be       //
33
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
34
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
35
// PURPOSE.  See the GNU Lesser General Public License for more //
36
// details.                                                     //
37
//                                                              //
38
// You should have received a copy of the GNU Lesser General    //
39
// Public License along with this source; if not, download it   //
40
// from http://www.opencores.org/lgpl.shtml                     //
41
//                                                              //
42
----------------------------------------------------------------*/
43
 
44
#include "amber_registers.h"
45
#include "address_map.h"
46
 
47
/* Defined in vmlinux/include/asm-arm/setup.h */
48
#define ATAG_CORE       0x54410001
49
#define ATAG_MEM        0x54410002
50
#define ATAG_INITRD     0x54410005
51
#define ATAG_RAMDISK    0x54410004
52
#define ATAG_NONE       0x00000000
53
 
54
#define ATAG_CORE_SIZE    ((2*4 + 3*4) >> 2)
55
#define ATAG_MEM_SIZE     ((2*4 + 2*4) >> 2)
56
#define ATAG_INITRD_SIZE  ((2*4 + 2*4) >> 2)
57
#define ATAG_RAMDISK_SIZE ((2*4 + 3*4) >> 2)
58
 
59
 
60
/* from vmlinux/arch/arm/kernel/compat.c */
61
#define FLAG_READONLY   1
62
 
63
/* from the list in wmlinux/arch/arm/tools/mach-types */
64
#define MACH_TYPE_A5K   11
65
 
66
 
67
 
68
        .section .text
69 78 csantifort
        .globl  start
70
start:
71 61 csantifort
        /* 0x00 Reset Interrupt vector address */
72
        b       startup
73
 
74
        /* 0x04 Undefined Instruction Interrupt vector address */
75
        b       _testfail
76 78 csantifort
 
77 61 csantifort
        /* 0x08 SWI Interrupt vector address */
78
        b       _testfail
79 78 csantifort
 
80 61 csantifort
        /* 0x0c Prefetch abort Interrupt vector address */
81
        b       _testfail
82 78 csantifort
 
83 61 csantifort
        /* 0x10 Data abort Interrupt vector address */
84
        b       _testfail
85
        b       _testfail
86 78 csantifort
 
87 61 csantifort
        /* 0x18 IRQ vector address */
88
        b       service_irq
89 78 csantifort
 
90 61 csantifort
        /* 0x1c FIRQ vector address */
91
        b       _testfail
92
 
93 78 csantifort
 
94 61 csantifort
        .global _restart
95
_restart:
96
        @ jump to address 0 in irq mode
97
        mov     pc, #0x00000002
98
        nop
99
        nop
100
        nop
101
 
102
 
103
startup:
104
        /* copy program to exec space */
105
        mov     r0, #0
106
        ldr     r1, AdrExecBase
107
1:      ldm     r0!, {r2-r9}
108 78 csantifort
        stm     r1!, {r2-r9}
109 61 csantifort
        cmp     r0, #0x4000
110
        bne     1b
111
 
112
        /* Fix the interrupt jump pointers */
113 78 csantifort
        ldr     r0, AdrExecBase
114 61 csantifort
        mov     r1, r0, lsr #2
115
        mov     r2, #0
116 78 csantifort
 
117 61 csantifort
2:      ldr     r3, [r2]
118
        orr     r3, r3, r1
119
        str     r3, [r2], #4
120
        cmp     r2, #0x1c
121
        bne     2b
122 78 csantifort
 
123 61 csantifort
        /* Jump to 2f but offset from ExecBase not current location */
124 78 csantifort
3:      ldr     r0, AdrExecBase
125 61 csantifort
        ldr     r1, AdrJumpPoint
126
        orr     r0, r0, r1
127
        mov     pc, r0
128
 
129
_jump_point:
130
 
131
        /* Switch to IRQ Mode */
132
        mov     r0, #0x00000002
133 78 csantifort
        teqp    pc, r0
134 61 csantifort
        /* Set IRQ Mode stack pointer */
135
        ldr     sp, AdrIRQStack
136
 
137
        /* Switch to SVC mode and Unset interrupt mask bits */
138
        mov     r0, #0x00000003
139 78 csantifort
        teqp    pc, r0
140
 
141 61 csantifort
        @ Enable the cache
142
        @ set region 24 to be uncached. Used for packet buffers
143
        mov     r0, #0xfeffffff
144 78 csantifort
        mcr     15, 0, r0, cr3, cr0, 0   @ cacheable area
145 61 csantifort
        mov     r0, #1
146 78 csantifort
        mcr     15, 0, r0, cr2, cr0, 0   @ cache enable
147
 
148 61 csantifort
        @ init SP
149
        ldr     sp, AdrStack
150
 
151
        @ Set 32MB memory mode
152
        ldr     r0, AdrMemCtrl
153
        mov     r1, #1
154
        str     r1, [r0]
155 78 csantifort
 
156 61 csantifort
        .extern main
157
        bl      main
158 78 csantifort
 
159 61 csantifort
        @ jump to program at r0
160 78 csantifort
        .globl _jump_to_program
161 61 csantifort
_jump_to_program:
162
 
163 78 csantifort
 
164 61 csantifort
        @ ----------------------------------------------
165
        @ Copy ATAG structure to AdrBootParams
166
        @ ----------------------------------------------
167
        ldr     r1, AdrBootParams
168
        ldr     r2, AdrATAGBase
169
        ldr     r3, AdeEndATAG
170 78 csantifort
 
171 61 csantifort
1:      cmp     r2, r3
172
        beq     2f
173
        ldr     r4, [r2], #4
174
        str     r4, [r1], #4
175
        b       1b
176 78 csantifort
 
177 61 csantifort
        @ Set memc page tables
178 78 csantifort
2:      ldr     r2, AdrPageTabes
179 61 csantifort
        mov     r3, #0
180
        mov     r4, #40
181
3:      str     r3,[r2],#4
182
        subs    r4, r4, #1
183
        bne     3b
184 78 csantifort
 
185 61 csantifort
        @ ----------------------------------------------
186
        @ jump to start of program in svc mode with interrupts disabled
187
        @ ----------------------------------------------
188
        mov     r4, r0
189
        orr     r4, #0x0c000003
190 78 csantifort
        mov     r0, #0
191 61 csantifort
        mov     pc, r4
192
 
193 78 csantifort
 
194 61 csantifort
service_irq:
195 78 csantifort
        @ As this is an interrupt, need tp save all registers to the stack
196
        stmfd   sp!, {r0-r3, lr}
197 61 csantifort
 
198
        @ is it a timer interrupt ?
199
        ldr     r0, AdrInterruptStatus
200 78 csantifort
        ldr     r3, [r0]
201
        ands    r2, r3, #0x20
202 61 csantifort
        beq     1f  @ not timer int, jump
203 78 csantifort
        @ Remember that registers r0 to r2 can be changed by this function
204 61 csantifort
        .extern timer_interrupt
205 78 csantifort
        bl timer_interrupt
206
 
207 61 csantifort
        @ is it an ethernet interrupt ?
208 78 csantifort
1:      ands    r2, r3, #0x100
209
        beq     2f  @ not ethmac int, jump
210 61 csantifort
        .extern ethmac_interrupt
211 78 csantifort
        @ Remember that registers r0 to r2 can be changed by this function
212 61 csantifort
        bl ethmac_interrupt
213
 
214 78 csantifort
 
215 61 csantifort
2:      @ Restore all registers from the stack
216 78 csantifort
        ldmfd   sp!, {r0-r3, lr}
217
 
218 61 csantifort
        @ Jump straight back to normal execution
219
        subs    pc, lr, #4
220
 
221
 
222
 
223
/* _testfail: Used to terminate execution in Verilog simulations */
224
/* On the board just puts the processor into an infinite loop    */
225 78 csantifort
        .globl _testfail
226 61 csantifort
_testfail:
227
        ldr     r11, AdrTestStatus
228
        str     r0, [r11]
229
        b       _testfail
230
 
231 78 csantifort
 
232 61 csantifort
/* _testpass: Used to terminate execution in Verilog simulations */
233
/* On the board just puts the processor into an infinite loop    */
234 78 csantifort
        .globl _testpass
235
_testpass:
236 61 csantifort
        ldr     r11, AdrTestStatus
237
        mov     r10, #17
238
        str     r10, [r11]
239
        b       _testpass
240
 
241
 
242
 
243 78 csantifort
 
244 61 csantifort
/* _div: Integer division function */
245
        @ Divide r0 by r1
246
        @ Answer returned in r1
247
        .globl _div
248
        .globl __aeabi_idiv
249
__aeabi_idiv:
250
_div:
251
        stmdb   sp!, {r4, lr}
252
 
253
        @ set r4 to 1 if one of the two inputs is negative
254
        and     r2, r0, #0x80000000
255
        and     r3, r1, #0x80000000
256
        eor     r4, r2, r3
257
 
258
        @ Invert negative numbers
259
        tst     r0, #0x80000000
260
        mvnne   r0, r0
261 78 csantifort
        addne   r0, r0, #1
262 61 csantifort
 
263
        tst     r1, #0x80000000
264
        mvnne   r1, r1
265 78 csantifort
        addne   r1, r1, #1
266 61 csantifort
 
267
        @ divide r1 by r2, also use registers r0 and r4
268
        mov     r2, r1
269
        mov     r1, r0
270 78 csantifort
 
271 61 csantifort
        cmp      r2, #0
272
        beq      3f
273
 
274 78 csantifort
        @ In order to divide r1 by r2, the first thing we need to do is to shift r2
275
        @ left by the necessary number of places. The easiest method of doing this
276
        @ is simply by trial and error - shift until we discover that r2 has become
277 61 csantifort
        @ too big, then stop.
278
        mov      r0,#0     @ clear r0 to accumulate result
279
        mov      r3,#1     @ set bit 0 in r3, which will be
280
                           @ shifted left then right
281
 
282
1:      cmp      r3, #0    @ escape on error
283
        moveq    r3, #0x10000000
284
        beq      2f
285
        cmp      r2,r1
286
        movls    r2,r2,lsl#1
287
        movls    r3,r3,lsl#1
288
        bls      1b
289
        @ shift r2 left until it is about to be bigger than r1
290
        @ shift r3 left in parallel in order to flag how far we have to go
291
 
292
        @ r0 will be used to hold the result. The role of r3 is more complicated.
293 78 csantifort
        @ In effect, we are using r3 to mark where the right-hand end of r2 has got to
294
        @ - if we shift r2 three places left, this will be indicated by a value of %1000
295
        @ in r3. However, we also add it to r0 every time we manage a successful subtraction,
296
        @ since it marks the position of the digit currently being calculated in the answer.
297
        @ In the binary example (50 ÷ 10) above, we shifted the '10' two places left,
298
        @ so at the time of the first subtraction, r3 would have been %100, at the time
299
        @ of the second (which failed) it would have been %10, and at the time of the
300
        @ third %1. Adding it to r0 after each successful subtraction would have
301 61 csantifort
        @ given us, once again, the answer of %101!
302
 
303
        @ Now for the loop that actually does the work:
304
2:      cmp       r1,r2      @ carry set if r1>r2 (don't ask why)
305
        subcs     r1,r1,r2   @ subtract r2 from r1 if this would
306
                             @ give a positive answer
307
        addcs     r0,r0,r3   @ and add the current bit in r3 to
308
                             @ the accumulating answer in r0
309
 
310 78 csantifort
        @ In subtraction (a cmp instruction simulates a subtraction in
311
        @ order to set the flags), if r1 - r2 gives a positive answer and no 'borrow'
312
        @ is required, the carry flag is set. This is required in order to make SBC
313
        @ (Subtract with Carry) work properly when used to carry out a 64-bit subtraction,
314 61 csantifort
        @ but it is confusing!
315 78 csantifort
 
316
        @ In this case, we are turning it to our advantage. The carry flag is set to
317
        @ indicate that a successful subtraction is possible, i.e. one that doesn't
318
        @ generate a negative result, and the two following instructions are carried
319
        @ out only when the condition Carry Set applies. Note that the 'S' on the end
320
        @ of these instructions is part of the 'CS' condition code and does not mean
321 61 csantifort
        @ that they set the flags!
322 78 csantifort
 
323 61 csantifort
        movs      r3,r3,lsr #1    @ Shift r3 right into carry flag
324
        movcc     r2,r2,lsr #1    @ and if bit 0 of r3 was zero, also
325
                                  @ shift r2 right
326
        bcc       2b              @ If carry not clear, r3 has shifted
327
                                  @ back to where it started, and we
328
                                  @ can end
329 78 csantifort
 
330
        @ if one of the inputs is negetive then return a negative result
331 61 csantifort
        tst     r4, #0x80000000
332
        mvnne   r0, r0
333 78 csantifort
        addne   r0, r0, #1
334 61 csantifort
3:      ldmia   sp!, {r4, pc}^
335
 
336
 
337 78 csantifort
/* strcpy: String copy function
338 61 csantifort
    char * strcpy ( char * destination, const char * source );
339
    destination is returned
340 78 csantifort
*/
341 61 csantifort
        @ r0 points to destination
342 78 csantifort
        @ r1 points to source string which terminates with a 0
343 61 csantifort
        .globl strcpy
344
strcpy:
345
        stmdb   sp!, {r4-r6, lr}
346
        @ Use r6 to process the destination pointer.
347
        @ At the end of the function, r0 is returned, so need to preserve it
348
        mov     r6, r0
349
 
350
strcpy_main:
351
        @ unroll the loop 4 times
352
        ldrb    r3, [r1], #1
353
        strb    r3, [r6], #1
354
        cmp     r3, #0
355
        ldmeqia sp!, {r4-r6, pc}^
356 78 csantifort
 
357 61 csantifort
        ldrb    r3, [r1], #1
358
        strb    r3, [r6], #1
359
        cmp     r3, #0
360
        ldmeqia sp!, {r4-r6, pc}^
361 78 csantifort
 
362 61 csantifort
        ldrb    r3, [r1], #1
363
        strb    r3, [r6], #1
364
        cmp     r3, #0
365
        ldmeqia sp!, {r4-r6, pc}^
366 78 csantifort
 
367 61 csantifort
        ldrb    r3, [r1], #1
368
        strb    r3, [r6], #1
369
        cmp     r3, #0
370
        ldmeqia sp!, {r4-r6, pc}^
371 78 csantifort
 
372 61 csantifort
        b       strcpy_main
373
 
374
 
375
 
376
/* strncpy: String copy function */
377
        @ r0 points to destination
378
        @ r1 points to source string
379
        @ r2 is the number of bytes to copy
380
        .globl strncpy
381 78 csantifort
strncpy:
382 61 csantifort
        stmdb   sp!, {r4, lr}
383
        cmp     r2, #0
384
        beq     2f
385
        add     r4, r0, r2    @ set r4 to the address of the last byte copied
386
1:      ldrb    r3, [r1], #1
387
        strb    r3, [r0], #1
388
        cmp     r0,  r4
389
        bne     1b
390
2:      ldmia   sp!, {r4, pc}^
391
 
392
 
393
 
394
/* strncpy: String compare function */
395
        @ r0 points to first string
396
        @ r1 points to second string
397
        @ r2 is the number of bytes to compare
398
        @ return the difference if the strings don't match
399
        .globl strncmp
400
strncmp:
401
        stmdb   sp!, {r4, r5, r6, lr}
402 78 csantifort
 
403 61 csantifort
        @ check for 0 length
404
        cmp     r2, #0
405
        moveq   r0, #1
406
        beq     2f
407 78 csantifort
 
408 61 csantifort
        mov     r3, #0
409 78 csantifort
 
410 61 csantifort
1:      add     r3, r3,   #1
411
        ldrb    r4, [r0], #1
412
        ldrb    r5, [r1], #1
413 78 csantifort
 
414 61 csantifort
        subs    r6, r4, r5
415
        movne   r0, r6
416
        bne     2f
417 78 csantifort
 
418 61 csantifort
        cmp     r3, r2
419
        moveq   r0, #0
420
        beq     2f
421 78 csantifort
 
422 61 csantifort
        b       1b
423
2:      ldmia   sp!, {r4, r5, r6, pc}^
424
 
425
 
426
 
427
        .globl init_malloc
428
init_malloc:
429
        ldr     r0, AdrMallocBase
430
        ldr     r1, AdrMallocPointer
431
        str     r0, [r1]
432 78 csantifort
 
433 61 csantifort
        @ initialize the counter to 0
434
        ldr     r1, AdrMallocCount
435
        mov     r2, #0
436
        str     r2, [r1]
437 78 csantifort
 
438 61 csantifort
        mov     pc, lr
439
 
440
 
441
        /* void *malloc(size_t size); */
442
        .globl malloc
443
malloc:
444
        /* r0 contains the size of the object in bytes */
445
        ldr     r1, AdrMallocPointer
446
        ldr     r2, [r1]    /* r2 now containts the starting address of the next memory block to use */
447
        add     r3, r0, r2  /* r3 contains the address after the end of the new object */
448 78 csantifort
 
449 61 csantifort
        /* Round r3 up to the nearest 0x100 to keep memory aligned */
450
        tst     r3, #0xff
451
        beq     1f
452
        bic     r3, r3, #0xff
453
        add     r3, r3, #0x100
454 78 csantifort
 
455 61 csantifort
1:      str     r3, [r1]    /* Update the malloc pointer */
456
        mov     r0, r2      /* Return the address from before the pointer was updated */
457 78 csantifort
 
458 61 csantifort
        @ Update the block count
459
        ldr     r1, AdrMallocCount
460
        ldr     r2, [r1]
461
        add     r2, r2, #1
462
        str     r2, [r1]
463 78 csantifort
 
464 61 csantifort
        mov     pc, lr
465
 
466
 
467 78 csantifort
        .global serial_putchar_
468
serial_putchar_:
469
        ldr     r1, AdrUARTDR
470
        ldr     r3, AdrUARTFR
471
        @ Check the tx_full flag
472
1:      ldr     r2, [r3]
473
        and     r2, r2, #0x20
474
        cmp     r2, #0
475
        streqb  r0, [r1]
476
        moveqs  pc, lr          @ return
477
        bne     1b
478
 
479
 
480 61 csantifort
/* stack at top of ddr3 memory space */
481
AdrJumpPoint:               .word _jump_point
482
AdrExecBase:                .word ADR_EXEC_BASE
483
AdrStack:                   .word ADR_STACK
484
AdrIRQStack:                .word ADR_IRQ_STACK
485
AdrMallocPointer:           .word ADR_MALLOC_POINTER
486
AdrMallocCount:             .word ADR_MALLOC_COUNT
487
AdrMallocBase:              .word ADR_MALLOC_BASE
488
 
489
AdrMemCtrl:                 .word ADR_AMBER_TEST_MEM_CTRL
490
AdrTestStatus:              .word ADR_AMBER_TEST_STATUS
491
AdrInterruptStatus:         .word ADR_AMBER_IC_IRQ0_STATUS
492
 
493 78 csantifort
AdrUARTDR:                  .word ADR_AMBER_UART0_DR
494
AdrUARTFR:                  .word ADR_AMBER_UART0_FR
495
 
496 61 csantifort
                            .align 2
497
AdrATAGBase:                .word ATAGBase
498
AdeEndATAG:                 .word EndATAG
499
 
500
ATAGBase:                   .word ATAG_CORE_SIZE
501
                            .word ATAG_CORE
502
                            .word FLAG_READONLY     @ flags
503
                            .word 4096              @ page size
504
                            .word 0x0               @ rootdev
505 78 csantifort
 
506 61 csantifort
                            .word ATAG_MEM_SIZE
507
                            .word ATAG_MEM
508
                            .word 32*1024*1024      @ size - 32MB
509
                            .word 0x0               @ start
510
 
511
                            .word ATAG_RAMDISK_SIZE
512
                            .word ATAG_RAMDISK
513
                            .word 1                 @ flags: bit 0 = load, bit 1 = prompt
514
                            .word 0x000000d0        @ size in 1k blocks
515
                            .word 0x00800000        @ physical address of start of ramdisk
516
 
517
                            .word ATAG_INITRD_SIZE
518
                            .word ATAG_INITRD
519
                            .word 0x02800000        @ virtual address of start of initrd image
520
                            .word 0x00032000        @ size = 200k
521 78 csantifort
 
522 61 csantifort
                            .word ATAG_NONE
523
                            .word 0x0
524
EndATAG:                    .word 0x0
525
 
526
AdrBootParams:              .word 0x7c000
527
AdrPageTabes:               .word 0x3f01000

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.