Clip+: boot to OF if USB is connected
[kugel-rb.git] / rbutil / mkamsboot / dualboot / dualboot.S
blob309aa67742a2f05d1226e136e833016f6d57ac6c
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_PROC,  0xC80F0010
30 .set CGU_PERI,  0xC80F0014
31 .set I2C_BASE,  0xC8070000
32 .set I2C_DATA,  0x00
33 .set I2C_SLAD0, 0x04
34 .set I2C_CNTRL, 0x0c
35 .set I2C_DACNT, 0x10
36 .set I2C_CPSR0, 0x1c
37 .set I2C_CPSR1, 0x20
38 .set I2C_IMR,   0x24
39 .set I2C_SR,    0x30
40 .set I2C_SADDR, 0x44
41 .set AS3514_I2C_ADDR, 0x46
42 .set AS3514_IRQ_ENRD0, 0x25
43 .set PCLK, 24000000
44 .set I2C_CLK, 400000
45 .set I2C_PRESCALER, ((PCLK + I2C_CLK -1) / I2C_CLK)
46 .set I2C_PRESCALER_LOW, (I2C_PRESCALER & 0xff)
47 .set I2C_PRESCALER_HIGH, (I2C_PRESCALER >> 8)
48 #if I2C_PRESCALER_HIGH > 3
49 #error i2c prescaler too big!
50 #endif
52 /* Vectors */
54         ldr   pc, =start    /* reset vector */
55         /* next vectors are unused, halt cpu */
56 1:      b 1b
57 1:      b 1b
58 1:      b 1b
59 1:      b 1b
60 1:      b 1b
61 1:      b 1b
62 1:      b 1b
65 /* These values are filled in by mkamsboot - don't move them from offset 0x20 */
67 uclunpack_end:   .word   0 /* End of the ucl_unpack function */
68 uclunpack_size:  .word   0 /* Size in bytes of the ucl_unpack function */
70 ucl_of_end:      .word   0 /* End of the ucl-compressed OF image */
71 ucl_of_size:     .word   0 /* Size in bytes of the compressed OF image */
73 ucl_rb_end:      .word   0 /* End of the ucl-compressed RB image */
74 ucl_rb_size:     .word   0 /* Size in bytes of the compressed RB image */
76 ucl_dest:        .word   0 /* End of our destination buffer (end of memory) */
78 start:
79         /* First copy the UCL unpack function to the end of RAM */
80         ldr     r0, uclunpack_end   /* Source */
81         ldr     r1, uclunpack_size  /* Source length */
82         sub     r2, r0, r1          /* Source start - 1*/
84         ldr     r3, ucl_dest        /* Destination end */
86 uclcopy:
87         ldrb    r4, [r0], #-1
88         strb    r4, [r3], #-1
89         cmp     r2, r0
90         bne     uclcopy
92         /* store the new destination buffer */
93         str     r3, ucl_dest
95         /* enable gpio clock */
96         ldr     r0, =CGU_PERI
97         ldr     r1, [r0]
98         orr     r1, r1, #(1<<16)
99         str     r1, [r0]
102 /* TODO : M200V4 ? */
103 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
104 #define USB_PIN 6
105 #elif defined(SANSA_FUZE) || defined(SANSA_E200V2) || defined(SANSA_FUZEV2)
106 #define USB_PIN 3
107 #endif
109 #ifdef USB_PIN  /* TODO : remove this check when we'll have an USB driver */
110         ldr     r0, =GPIOA
111         mov     r1, #0
112         str     r1, [r0, #0x400]
113         ldr     r1, [r0, #(4*(1<<USB_PIN))]
114         cmp     r1, #0
115         bne     boot_of
116 #endif
117 #if defined(SANSA_C200V2)
118         /* Instead of checking the GPIO, check the audio master IRQ_ENRD0
119          * USB_STATUS bit on C200V2 */
121         ldr     r0, =CGU_PERI
122         ldr     r1, [r0]
123         /* enable i2c audio master clock */
124         orr     r1, r1, #(1<<17)
125         /* make sure 24MHz clk_main is selected */
126         bic     r1, r1, #0x7f
127         str     r1, [r0]
129         ldr     r0, =I2C_BASE
130         /* disable i2c interrupts */
131         mov     r1, #0
132         str     r1, [r0, #I2C_IMR]
133         /* setup prescaler */
134         mov     r1, #I2C_PRESCALER_LOW
135         str     r1, [r0, #I2C_CPSR0]
136         mov     r1, #I2C_PRESCALER_HIGH
137         str     r1, [r0, #I2C_CPSR1]
138         /* setup i2c slave address */
139         mov     r1, #(AS3514_I2C_ADDR << 1)
140         str     r1, [r0, #I2C_SLAD0]
141         mov     r2, #0x51
142         str     r2, [r0, #I2C_CNTRL]
144         /* wait for not busy */
146         ldr     r1, [r0, #I2C_SR]
147         tst     r1, #1
148         bne 1b
150         /* start read of irq_enrd0 */
151         mov     r1, #AS3514_IRQ_ENRD0
152         str     r1, [r0, #I2C_SADDR]
153         orr     r2, r2, #(1 << 1)
154         str     r2, [r0, #I2C_CNTRL]
155         mov     r1, #1
156         str     r1, [r0, #I2C_DACNT]
158         /* wait for transfer to finish */
160         ldr     r1, [r0, #I2C_DACNT]
161         cmp     r1, #0
162         bne     1b
164         /* load result and test USB_STATUS bit */
165         ldr     r1, [r0, #I2C_DATA]
166         tst     r1, #(1 << 3)
167         bne     boot_of
168 #endif
170         /* Here are model specific tests, for dual boot without a computer */
171         /* All models use left button */
172         /* /!\ Right button for c200v2 (left button is unkwown) */
174 #ifdef SANSA_CLIP
175 .set row, (1<<5) /* enable output on C5 */
176 .set col, (1<<0) /* read keyscan column B0 */
178         ldr     r0, =GPIOC
179         mov     r1, #row
180         str     r1, [r0, #0x400]
181         str     r1, [r0, #(4*row)]
183         ldr     r0, =GPIOB
184         mov     r1, #0
185         str     r1, [r0, #0x400]
186         ldr     r1, [r0, #(4*col)]
188         cmp     r1, #0
189         bne     boot_of
190 #elif defined(SANSA_CLIPV2)
191 .set row, (1<<4) /* enable output on D4 */
192 .set col, (1<<0) /* read keyscan column D0 */
194         ldr     r0, =GPIOD
195         mov     r1, #((1<<5)|(1<<4)|(1<<3)) /* all rows as output */
196         str     r1, [r0, #0x400]
198         /* all rows high */
199         mov     r1, #(1<<3)
200         str     r1, [r0, #(4*(1<<3))]
201         mov     r1, #(1<<4)
202         str     r1, [r0, #(4*(1<<4))]
203         mov     r1, #(1<<5)
204         str     r1, [r0, #(4*(1<<5))]
206         mov     r1, #0                      /* button row low */
207         str     r1, [r0, #(4*row)]
209         mov     r1, #5                      /* small delay */
210 1:      subs    r1, r1, #1
211         bne     1b
213         ldr     r1, [r0, #(4*col)]
215         cmp     r1, #0
216         beq     boot_of
217 #elif defined(SANSA_E200V2) || defined(SANSA_FUZE) || defined(SANSA_FUZEV2)
218         ldr     r0, =GPIOC
219         mov     r1, #0
220         str     r1, [r0, #0x400]
221         ldr     r1, [r0, #0x20]    /* read pin C3 */
223         cmp     r1, #0             /* C3 = #0 means button pressed */
224 #ifdef SANSA_FUZEV2
225         /* the logic is reversed on the fuzev2 */
226         bne     boot_of
227 #else
228         beq     boot_of
229 #endif /* SANSA_FUZEV2 */
231 #elif defined(SANSA_CLIPPLUS)
232         @ read pins
233         ldr     r0, =GPIOC
234         ldr     r1, [r0, #4*(1<<3)] @ read pin C3 "|<<"
236         ldr     r0, =GPIOA
237         ldr     r2, [r0, #4*(1<<1)] @ read pin A1 "Home"
239         orr     r2, r2, r1          @ c3 || A1
241         cmp     r2, #0              @ test input from pins
242         bne     boot_of             @ branch directly to OF if either pin high
245         @ check USB connection
247         ldr     r0, =CGU_PROC
248         mov     r1, #0
249         str     r1, [r0]    @ fclk = 24MHz
251         ldr     r0, =CGU_PERI
252         ldr     r1, [r0]
253         /* enable i2c audio master clock */
254         orr     r1, r1, #(1<<17)
255         /* pclk = fclk = 24MHz */
256         bic     r1, r1, #0x7f
257         str     r1, [r0]
259         ldr     r0, =I2C_BASE
260         /* disable i2c interrupts */
261         mov     r1, #0
262         str     r1, [r0, #I2C_IMR]
263         /* setup prescaler */
264         mov     r1, #I2C_PRESCALER_LOW
265         str     r1, [r0, #I2C_CPSR0]
266         mov     r1, #I2C_PRESCALER_HIGH
267         str     r1, [r0, #I2C_CPSR1]
268         /* setup i2c slave address */
269         mov     r1, #(AS3514_I2C_ADDR << 1)
270         str     r1, [r0, #I2C_SLAD0]
271         mov     r2, #0x51
272         str     r2, [r0, #I2C_CNTRL]
274         /* wait for not busy */
276         ldr     r1, [r0, #I2C_SR]
277         tst     r1, #1
278         bne 1b
280         /* wait a bit (~100ms) else detection fails */
281         mov     r1, #0x80000
282 1:      subs    r1, r1, #1
283         bne 1b
285         /* start read of irq_enrd0 */
286         mov     r1, #AS3514_IRQ_ENRD0
287         str     r1, [r0, #I2C_SADDR]
288         orr     r2, r2, #(1 << 1)
289         str     r2, [r0, #I2C_CNTRL]
290         mov     r1, #1
291         str     r1, [r0, #I2C_DACNT]
293         /* wait for transfer to finish */
295         ldr     r1, [r0, #I2C_DACNT]
296         cmp     r1, #0
297         bne     1b
299         /* load result and test USB_STATUS bit */
300         ldr     r1, [r0, #I2C_DATA]
301         tst     r1, #(1 << 3)
302         bne     boot_of
305 #elif defined(SANSA_C200V2)
306         /* check for RIGHT on C6, should changed to LEFT as soon as it
307          * known in which pin that is in order for consistency  */
308         ldr     r0, =GPIOC
309         mov     r1, #0
310         str     r1, [r0, #0x400]      /* set pin to output */
312         ldr     r1, [r0, #256]        /* 1<<(6+2) */
313         cmp     r1, #0                /* C6 low means button pressed */
314         beq     boot_of
315 #elif defined(SANSA_M200V4)
316 .set row, (1<<5) /* enable output on A5 */
317 .set col, (1<<0) /* read keyscan column A0 */
319         ldr     r0, =GPIOA
320         mov     r1, #row
321         str     r1, [r0, #0x400]
322         str     r1, [r0, #(4*row)]
324         ldr     r2, [r0, #(4*col)]
326         /* check value read (1 means button pressed) */
327         cmp     r2, #0
328         bne     boot_of
329 #else
330         #error No target-specific key check defined!
331 #endif
334         /* The dualboot button was not held, so we boot rockbox */
335         ldr     r0, ucl_rb_end      /* Address of compressed image */
336         ldr     r1, ucl_rb_size     /* Compressed size */
337         b       decompress
339 boot_of:
340         ldr     r0, ucl_of_end      /* Address of compressed image */
341         ldr     r1, ucl_of_size     /* Compressed size */
344 decompress:
345         /* At this point:                                              */
346         /* r0 = source_end for UCL image to copy                       */
347         /* r1 = size of UCL image to copy                              */
349         ldr     r3, ucl_dest
350         add     r5, r3, #2      /* r5 is entry point of copy of uclunpack */
351                                 /* function, plus one (for thumb mode */
353         sub     r4, r3, r1      /* r4 := destination_start - 1 */
355 fw_copy:
356         ldrb    r2, [r0], #-1
357         strb    r2, [r3], #-1
358         cmp     r3, r4          /* Stop when we reached dest_start-1 */
359         bne     fw_copy
361         /* Call the ucl decompress function, which will branch to 0x0 */
362         /* on completion */
363         add     r0, r3, #1      /* r0 := Start of compressed image */
364                                 /* r1 already contains compressed size */
365         mov     r2, #0          /* r2 := Destination for unpacking */
366         bx      r5              /* Branch to uclunpack, switching to thumb */
368         /* never reached : uclunpack will branch to the reset vector (0x0) */