Sansa Fuzev2: Add mkamsboot and dualboot support. The bootloader doesn't work, but...
[kugel-rb.git] / rbutil / mkamsboot / dualboot / dualboot.S
blob84d2fcee4d490eebafddf2bece97e9bfa1d06d46
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 /* AS3525 hardware registers */
25 .set GPIOA,     0xC80B0000
26 .set GPIOB,     0xC80C0000
27 .set GPIOC,     0xC80D0000
28 .set GPIOD,     0xC80E0000
29 .set CGU_PERI,  0xC80F0014
32 /* Vectors */
34         ldr   pc, =start    /* reset vector */
35         /* next vectors are unused */
36         .word   0
37         .word   0
38         .word   0
39         .word   0
40         .word   0
41         .word   0
42         .word   0
44 /* These values are filled in by mkamsboot - don't move them from offset 0x20 */
46 uclunpack_end:   .word   0 /* End of the ucl_unpack function */
47 uclunpack_size:  .word   0 /* Size in bytes of the ucl_unpack function */
49 ucl_of_end:      .word   0 /* End of the ucl-compressed OF image */
50 ucl_of_size:     .word   0 /* Size in bytes of the compressed OF image */
52 ucl_rb_end:      .word   0 /* End of the ucl-compressed RB image */
53 ucl_rb_size:     .word   0 /* Size in bytes of the compressed RB image */
55 ucl_dest:        .word   0 /* End of our destination buffer (end of memory) */
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, ucl_dest        /* Destination end */
65 uclcopy:
66         ldrb    r4, [r0], #-1
67         strb    r4, [r3], #-1
68         cmp     r2, r0
69         bne     uclcopy
71         /* store the new destination buffer */
72         str     r3, ucl_dest
74         /* enable gpio clock */
75         ldr     r0, =CGU_PERI
76         ldr     r1, [r0]
77         orr     r1, r1, #(1<<16)
78         str     r1, [r0]
81 /* TODO : M200V4 ? */
82 #if defined(SANSA_C200V2)
83 #define USB_PIN 1   /* FIXME: not correct on some c200v2s */
84 #elif defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
85 #define USB_PIN 6
86 #elif defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2)
87 #define USB_PIN 3
88 #endif
90 #ifdef USB_PIN  /* TODO : remove this check when we'll have an USB driver */
91         ldr     r0, =GPIOA
92         mov     r1, #0
93         str     r1, [r0, #0x400]
94         ldr     r1, [r0, #(4*(1<<USB_PIN))]
95         cmp     r1, #0
96         bne     boot_of
97 #endif
99         /* Here are model specific tests, for dual boot without a computer */
100         /* All models use left button */
101         /* /!\ Right button for c200v2 (left button is unkwown) */
103 #ifdef SANSA_CLIP
104 .set row, (1<<5) /* enable output on C5 */
105 .set col, (1<<0) /* read keyscan column B0 */
107         ldr     r0, =GPIOC
108         mov     r1, #row
109         str     r1, [r0, #0x400]
110         str     r1, [r0, #(4*row)]
112         ldr     r0, =GPIOB
113         mov     r1, #0
114         str     r1, [r0, #0x400]
115         ldr     r1, [r0, #(4*col)]
117         cmp     r1, #0
118         bne     boot_of
119 #elif defined(SANSA_CLIPV2)
120 .set row, (1<<4) /* enable output on D4 */
121 .set col, (1<<0) /* read keyscan column D0 */
123         ldr     r0, =GPIOD
124         mov     r1, #((1<<5)|(1<<4)|(1<<3)) /* all rows as output */
125         str     r1, [r0, #0x400]
127         /* all rows high */
128         mov     r1, #(1<<3)
129         str     r1, [r0, #(4*(1<<3))]
130         mov     r1, #(1<<4)
131         str     r1, [r0, #(4*(1<<4))]
132         mov     r1, #(1<<5)
133         str     r1, [r0, #(4*(1<<5))]
135         mov     r1, #0                      /* button row low */
136         str     r1, [r0, #(4*row)]
138         mov     r1, #5                      /* small delay */
139 1:      subs    r1, r1, #1
140         bne     1b
142         ldr     r1, [r0, #(4*col)]
144         cmp     r1, #0
145         beq     boot_of
146 #elif defined(SANSA_E200V2) || defined(SANSA_FUZE) || defined(SANSA_FUZEV2)
147         ldr     r0, =GPIOC
148         mov     r1, #0
149         str     r1, [r0, #0x400]
150         ldr     r1, [r0, #0x20]    /* read pin C3 */
152         cmp     r1, #0             /* C3 = #0 means button pressed */
153 #ifdef SANSA_FUZEV2
154         /* the logic is reversed on the fuzev2 */
155         bne     boot_of
156 #else
157         beq     boot_of
158 #endif /* SANSA_FUZEV2 */
160 #elif defined(SANSA_CLIPPLUS)
161         @ read pins
162         ldr     r0, =GPIOC
163         ldr     r1, [r0, #4*(1<<3)] @ read pin C3 "|<<"
165         ldr     r0, =GPIOA
166         ldr     r2, [r0, #4*(1<<1)] @ read pin A1 "Home"
168         orr     r2, r2, r1          @ c3 || A1
170         cmp     r2, #0              @ test input from pins
171         bne     boot_of             @ branch directly to OF if either pin high
172 #elif defined(SANSA_C200V2)
173         /* check for RIGHT on C6, should changed to LEFT as soon as it
174          * known in which pin that is in order for consistency  */
175         ldr     r0, =GPIOC
176         mov     r1, #0
177         str     r1, [r0, #0x400]      /* set pin to output */
179         ldr     r1, [r0, #256]        /* 1<<(6+2) */
180         cmp     r1, #0                /* C6 low means button pressed */
181         beq     boot_of
182 #elif defined(SANSA_M200V4)
183 .set row, (1<<5) /* enable output on A5 */
184 .set col, (1<<0) /* read keyscan column A0 */
186         ldr     r0, =GPIOA
187         mov     r1, #row
188         str     r1, [r0, #0x400]
189         str     r1, [r0, #(4*row)]
191         ldr     r2, [r0, #(4*col)]
193         /* check value read (1 means button pressed) */
194         cmp     r2, #0
195         bne     boot_of
196 #else
197         #error No target-specific key check defined!
198 #endif
201         /* The dualboot button was not held, so we boot rockbox */
202         ldr     r0, ucl_rb_end      /* Address of compressed image */
203         ldr     r1, ucl_rb_size     /* Compressed size */
204         b       decompress
206 boot_of:
207         ldr     r0, ucl_of_end      /* Address of compressed image */
208         ldr     r1, ucl_of_size     /* Compressed size */
211 decompress:
212         /* At this point:                                              */
213         /* r0 = source_end for UCL image to copy                       */
214         /* r1 = size of UCL image to copy                              */
216         ldr     r3, ucl_dest
217         add     r5, r3, #2      /* r5 is entry point of copy of uclunpack */
218                                 /* function, plus one (for thumb mode */
220         sub     r4, r3, r1      /* r4 := destination_start - 1 */
222 fw_copy:
223         ldrb    r2, [r0], #-1
224         strb    r2, [r3], #-1
225         cmp     r3, r4          /* Stop when we reached dest_start-1 */
226         bne     fw_copy
228         /* Call the ucl decompress function, which will branch to 0x0 */
229         /* on completion */
230         add     r0, r3, #1      /* r0 := Start of compressed image */
231                                 /* r1 already contains compressed size */
232         mov     r2, #0          /* r2 := Destination for unpacking */
233         bx      r5              /* Branch to uclunpack, switching to thumb */
235         /* never reached : uclunpack will branch to the reset vector (0x0) */