Fix red.
[kugel-rb.git] / rbutil / mkamsboot / dualboot.S
blob3b744f87dfcb6da1935028fa7d8ec54d80006a49
1 /***************************************************************************
2  *             __________               __   ___.
3  *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
4  *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
5  *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
6  *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
7  *                     \/            \/     \/    \/            \/
8  * $Id$
9  *
10  * Copyright (C) 2008 Rafaël Carré
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  ****************************************************************************/
22 .text
24 /* This is the size of the Clip's RAM, but there is nothing to be gained
25    (at the moment) by making use of the larger RAM of other targets */
27 .set DRAM_SIZE, 0x50000
29 .set GPIOA,     0xC80B0000
30 .set GPIOB,     0xC80C0000
31 .set GPIOC,     0xC80D0000
32 .set GPIOD,     0xC80E0000
33 .set CGU_PERI,  0xC80F0014
36 /* Vectors */
37         ldr   pc, =start
38 .word   0
39 .word   0
40 .word   0
41 .word   0
42 .word   0
43 .word   0
44 .word   0
46 /* These values are filled in by mkamsboot - don't move them from offset 0x20 */
47 uclunpack_end:   .word   0 /* End of the ucl_unpack function */
48 uclunpack_size:  .word   0 /* Size in bytes of the ucl_unpack function */
50 ucl_of_end:      .word   0 /* End of the ucl-compressed OF image */
51 ucl_of_size:     .word   0 /* Size in bytes of the compressed OF image */
53 ucl_rb_end:      .word   0 /* End of the ucl-compressed RB image */
54 ucl_rb_size:     .word   0 /* Size in bytes of the compressed RB image */
57 start:
58         /* First copy the UCL unpack function to the end of RAM */
59         ldr   r0, uclunpack_end     /* Source */
60         ldr   r1, uclunpack_size    /* Source length */
61         sub   r2, r0, r1            /* Source start - 1*/
63         ldr   r3, =(DRAM_SIZE-1)    /* Destination end */
65 uclcopy:
66         ldrb  r4, [r0], #-1
67         strb  r4, [r3], #-1
68         cmp   r2, r0
69         bne   uclcopy
71         add   r5, r3, #2            /* r5 is entry point of copy of uclunpack */
72                                     /* function, plus one (for thumb mode */
74         /* enable gpio clock */
75         ldr   r0, =CGU_PERI
76         ldr   r1, [r0]
77         orr   r1, r1, #(1<<16)
78         str   r1, [r0]
80         
81 #ifndef SANSA_M200V4  /* this doesnt work for m200 */         
82         /* we check A3 unconditionally of the model because it seems to be */
83         /* either hold, either usb on every model */
84         /* TODO: make it USB on all AMS Sansas for consistency, USB is safer too */
86         ldr   r0, =GPIOA
87         mov   r1, #0
88         str   r1, [r0, #0x400]
89 #ifdef SANSA_C200V2
90         ldr   r1, [r0, #0x8]    /* USB is A1 on C200 */
91 #elif defined(SANSA_CLIP)
92         ldr   r1, [r0, #0x100]    /* USB is A6 on Clip */
93 #else
94         ldr   r1, [r0, #0x20]    /* read pin A3 */
95 #endif
96         cmp   r1, #0
97         bne   boot_of
98 #endif
100         /* here are model specific tests, for dual boot without a computer */
102 #ifdef SANSA_CLIP
103         /* HOME button */
104 .set row, (1<<6) /* enable output on C6 */
105         ldr   r0, =GPIOC
106         mov   r1, #row
107         str   r1, [r0, #0x400]
108         str   r1, [r0, #(4*row)]
110 .set col, (1<<2) /* read keyscan column B2 */
111         ldr   r0, =GPIOB
112         mov   r1, #0
113         str   r1, [r0, #0x400]
114         ldr   r1, [r0, #(4*col)]
116         cmp   r1, #0
117         bne   boot_of
118 #elif defined(SANSA_E200V2) || defined(SANSA_FUZE)
119         /* LEFT button */
120         ldr   r0, =GPIOC
121         mov   r1, #0
122         str   r1, [r0, #0x400]
123         ldr   r1, [r0, #0x20]    /* read pin C3 */
125         cmp   r1, #0             /* C3 = #0 means button pressed */
126         beq   boot_of
127 #elif defined(SANSA_C200V2)
128         /* check for RIGHT on C6, should maybe changed to LEFT as soon as it
129          * known in which pin that is in order for consistency  */
130         ldr   r0, =GPIOC
131         mov   r1, #0
132         str   r1, [r0, #0x400]      /* set pin to output */
134         ldr   r1, [r0, #256]        /* 1<<(6+2) */
135         cmp   r1, #0                /* C6 low means button pressed */
136         beq   boot_of
137 #elif defined(SANSA_M200V4)
138         /* LEFT button */
139 .set row, (1<<5) /* enable output on A5 */
140         ldr   r0, =GPIOA
141         mov   r1, #row
142         str   r1, [r0, #0x400]
143         str   r1, [r0, #(4*row)]
145 .set col, (1<<0) /* read keyscan column A0 */
146         ldr   r2, [r0, #(4*col)]
148         /* check value read (1 means button pressed) */
149         cmp   r2, #0
150         bne   boot_of
151 #else
152         #error No target-specific key check defined!
153 #endif
155         /* No button was held, so we boot rockbox */
156         ldr   r0, ucl_rb_end   /* Address of compressed image */
157         ldr   r1, ucl_rb_size    /* Compressed size */
158         b     decompress
160 boot_of:
161         ldr   r0, ucl_of_end     /* Address of compressed image */
162         ldr   r1, ucl_of_size    /* Compressed size */
165 decompress:
166         /* At this point:                                              */
167         /* r5 = entry point (plus one for thumb) of uclunpack function */
168         /* r3 = destination_end for copy of UCL image                  */
169         /* r0 = source_end for UCL image to copy                       */
170         /* r1 = size of UCL image to copy                              */
172         sub   r4, r3, r1      /* r4 := destination_start - 1 */
173         
174 fw_copy:
175         ldrb  r2, [r0], #-1
176         strb  r2, [r3], #-1
177         cmp   r3, r4          /* Stop when we reached dest_start-1 */
178         bne   fw_copy
180         /* Call the ucl decompress function, which will branch to 0x0 */
181         /* on completion */
182         add   r0, r3, #1      /* r0 := Start of compressed image */
183                               /* r1 already contains compressed size */
184         mov   r2, #0          /* r2 := Destination for unpacking */
185         bx    r5              /* Branch to uclunpack, switching to thumb */
187         /* never reached */