4cbbc372e8e6e10a56208abc3432ccfbbc88ed84
[kugel-rb.git] / rbutil / mkamsboot / dualboot / dualboot.S
blob4cbbc372e8e6e10a56208abc3432ccfbbc88ed84
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)
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)
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         beq     boot_of
226 #elif defined(SANSA_FUZEV2)
227         ldr     r0, =GPIOC
228         mov     r1, #0
229         str     r1, [r0, #0x400]
230         ldr     r1, [r0, #0x20]    /* read pin C3 */
232         cmp     r1, #0             /* C3 != #0 means button pressed */
233         bne     boot_of
235 #elif defined(SANSA_CLIPPLUS)
236         @ read pins
237         ldr     r0, =GPIOC
238         ldr     r1, [r0, #4*(1<<3)] @ read pin C3 "|<<"
240         ldr     r0, =GPIOA
241         ldr     r2, [r0, #4*(1<<1)] @ read pin A1 "Home"
243         orr     r2, r2, r1          @ c3 || A1
245         cmp     r2, #0              @ test input from pins
246         bne     boot_of             @ branch directly to OF if either pin high
249 #elif defined(SANSA_C200V2)
250         /* check for RIGHT on C6, should changed to LEFT as soon as it
251          * known in which pin that is in order for consistency  */
252         ldr     r0, =GPIOC
253         mov     r1, #0
254         str     r1, [r0, #0x400]      /* set pin to output */
256         ldr     r1, [r0, #256]        /* 1<<(6+2) */
257         cmp     r1, #0                /* C6 low means button pressed */
258         beq     boot_of
259 #elif defined(SANSA_M200V4)
260 .set row, (1<<5) /* enable output on A5 */
261 .set col, (1<<0) /* read keyscan column A0 */
263         ldr     r0, =GPIOA
264         mov     r1, #row
265         str     r1, [r0, #0x400]
266         str     r1, [r0, #(4*row)]
268         ldr     r2, [r0, #(4*col)]
270         /* check value read (1 means button pressed) */
271         cmp     r2, #0
272         bne     boot_of
273 #else
274         #error No target-specific key check defined!
275 #endif
277 #if defined(SANSA_CLIPPLUS) || defined(SANSA_FUZEV2)
278         /* Check for USB after buttons because I trust more the GPIO code than
279          * the i2c code.
280          * Also it seems we need to wait a bit before detecting USB connection
281          * on those models, but not on c200v2
282          */
285         ldr     r0, =CGU_PROC
286         mov     r1, #0
287         str     r1, [r0]    @ fclk = 24MHz
289         ldr     r0, =CGU_PERI
290         ldr     r1, [r0]
291         /* enable i2c audio master clock */
292         orr     r1, r1, #(1<<17)
293         /* pclk = fclk = 24MHz */
294         bic     r1, r1, #0x7f
295         str     r1, [r0]
297         ldr     r0, =I2C_BASE
298         /* disable i2c interrupts */
299         mov     r1, #0
300         str     r1, [r0, #I2C_IMR]
301         /* setup prescaler */
302         mov     r1, #I2C_PRESCALER_LOW
303         str     r1, [r0, #I2C_CPSR0]
304         mov     r1, #I2C_PRESCALER_HIGH
305         str     r1, [r0, #I2C_CPSR1]
306         /* setup i2c slave address */
307         mov     r1, #(AS3514_I2C_ADDR << 1)
308         str     r1, [r0, #I2C_SLAD0]
309         mov     r2, #0x51
310         str     r2, [r0, #I2C_CNTRL]
312         /* wait for not busy */
314         ldr     r1, [r0, #I2C_SR]
315         tst     r1, #1
316         bne 1b
318         /* wait a bit (~100ms) else detection fails */
319         mov     r1, #0x80000
320 1:      subs    r1, r1, #1
321         bne 1b
323         /* start read of irq_enrd0 */
324         mov     r1, #AS3514_IRQ_ENRD0
325         str     r1, [r0, #I2C_SADDR]
326         orr     r2, r2, #(1 << 1)
327         str     r2, [r0, #I2C_CNTRL]
328         mov     r1, #1
329         str     r1, [r0, #I2C_DACNT]
331         /* wait for transfer to finish */
333         ldr     r1, [r0, #I2C_DACNT]
334         cmp     r1, #0
335         bne     1b
337         /* load result and test USB_STATUS bit */
338         ldr     r1, [r0, #I2C_DATA]
339         tst     r1, #(1 << 3)
340         bne     boot_of
343 #endif
346         /* The dualboot button was not held, so we boot rockbox */
347         ldr     r0, ucl_rb_end      /* Address of compressed image */
348         ldr     r1, ucl_rb_size     /* Compressed size */
349         b       decompress
351 boot_of:
352         ldr     r0, ucl_of_end      /* Address of compressed image */
353         ldr     r1, ucl_of_size     /* Compressed size */
356 decompress:
357         /* At this point:                                              */
358         /* r0 = source_end for UCL image to copy                       */
359         /* r1 = size of UCL image to copy                              */
361         ldr     r3, ucl_dest
362         add     r5, r3, #2      /* r5 is entry point of copy of uclunpack */
363                                 /* function, plus one (for thumb mode */
365         sub     r4, r3, r1      /* r4 := destination_start - 1 */
367 fw_copy:
368         ldrb    r2, [r0], #-1
369         strb    r2, [r3], #-1
370         cmp     r3, r4          /* Stop when we reached dest_start-1 */
371         bne     fw_copy
373         /* Call the ucl decompress function, which will branch to 0x0 */
374         /* on completion */
375         add     r0, r3, #1      /* r0 := Start of compressed image */
376                                 /* r1 already contains compressed size */
377         mov     r2, #0          /* r2 := Destination for unpacking */
378         bx      r5              /* Branch to uclunpack, switching to thumb */
380         /* never reached : uclunpack will branch to the reset vector (0x0) */