670f44750d411fdddd251d55c44298c8b9f95da4
[kugel-rb.git] / rbutil / mkamsboot / dualboot / dualboot.S
blob670f44750d411fdddd251d55c44298c8b9f95da4
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 CGU_DBOP,  0xC80F0038
32 .set DBOP,      0xC8120000
33 .set I2C_BASE,  0xC8070000
34 .set I2C_DATA,  0x00
35 .set I2C_SLAD0, 0x04
36 .set I2C_CNTRL, 0x0c
37 .set I2C_DACNT, 0x10
38 .set I2C_CPSR0, 0x1c
39 .set I2C_CPSR1, 0x20
40 .set I2C_IMR,   0x24
41 .set I2C_SR,    0x30
42 .set I2C_SADDR, 0x44
43 .set AS3514_I2C_ADDR, 0x46
44 .set AS3514_IRQ_ENRD0, 0x25
45 .set PCLK, 24000000
46 .set I2C_CLK, 400000
47 .set I2C_PRESCALER, ((PCLK + I2C_CLK -1) / I2C_CLK)
48 .set I2C_PRESCALER_LOW, (I2C_PRESCALER & 0xff)
49 .set I2C_PRESCALER_HIGH, (I2C_PRESCALER >> 8)
50 #if I2C_PRESCALER_HIGH > 3
51 #error i2c prescaler too big!
52 #endif
54 /* Vectors */
56         ldr   pc, =start    /* reset vector */
57         /* next vectors are unused, halt cpu */
58 1:      b 1b
59 1:      b 1b
60 1:      b 1b
61 1:      b 1b
62 1:      b 1b
63 1:      b 1b
64 1:      b 1b
67 /* These values are filled in by mkamsboot - don't move them from offset 0x20 */
69 uclunpack_end:   .word   0 /* End of the ucl_unpack function */
70 uclunpack_size:  .word   0 /* Size in bytes of the ucl_unpack function */
72 ucl_of_end:      .word   0 /* End of the ucl-compressed OF image */
73 ucl_of_size:     .word   0 /* Size in bytes of the compressed OF image */
75 ucl_rb_end:      .word   0 /* End of the ucl-compressed RB image */
76 ucl_rb_size:     .word   0 /* Size in bytes of the compressed RB image */
78 ucl_dest:        .word   0 /* End of our destination buffer (end of memory) */
80 start:
81         /* First copy the UCL unpack function to the end of RAM */
82         ldr     r0, uclunpack_end   /* Source */
83         ldr     r1, uclunpack_size  /* Source length */
84         sub     r2, r0, r1          /* Source start - 1*/
86         ldr     r3, ucl_dest        /* Destination end */
88 uclcopy:
89         ldrb    r4, [r0], #-1
90         strb    r4, [r3], #-1
91         cmp     r2, r0
92         bne     uclcopy
94         /* store the new destination buffer */
95         str     r3, ucl_dest
97         /* enable gpio clock */
98         ldr     r0, =CGU_PERI
99         ldr     r1, [r0]
100         orr     r1, r1, #(1<<16)
101         str     r1, [r0]
104 /* TODO : M200V4 ? */
105 #if defined(SANSA_CLIP) || defined(SANSA_CLIPV2)
106 #define USB_PIN 6
107 #elif defined(SANSA_FUZE) || defined(SANSA_E200V2)
108 #define USB_PIN 3
109 #endif
111 #ifdef USB_PIN  /* TODO : remove this check when we'll have an USB driver */
112         ldr     r0, =GPIOA
113         mov     r1, #0
114         str     r1, [r0, #0x400]
115         ldr     r1, [r0, #(4*(1<<USB_PIN))]
116         cmp     r1, #0
117         bne     boot_of
118 #endif
119 #if defined(SANSA_C200V2)
120         /* Instead of checking the GPIO, check the audio master IRQ_ENRD0
121          * USB_STATUS bit on C200V2 */
123         ldr     r0, =CGU_PERI
124         ldr     r1, [r0]
125         /* enable i2c audio master clock */
126         orr     r1, r1, #(1<<17)
127         /* make sure 24MHz clk_main is selected */
128         bic     r1, r1, #0x7f
129         str     r1, [r0]
131         ldr     r0, =I2C_BASE
132         /* disable i2c interrupts */
133         mov     r1, #0
134         str     r1, [r0, #I2C_IMR]
135         /* setup prescaler */
136         mov     r1, #I2C_PRESCALER_LOW
137         str     r1, [r0, #I2C_CPSR0]
138         mov     r1, #I2C_PRESCALER_HIGH
139         str     r1, [r0, #I2C_CPSR1]
140         /* setup i2c slave address */
141         mov     r1, #(AS3514_I2C_ADDR << 1)
142         str     r1, [r0, #I2C_SLAD0]
143         mov     r2, #0x51
144         str     r2, [r0, #I2C_CNTRL]
146         /* wait for not busy */
148         ldr     r1, [r0, #I2C_SR]
149         tst     r1, #1
150         bne 1b
152         /* start read of irq_enrd0 */
153         mov     r1, #AS3514_IRQ_ENRD0
154         str     r1, [r0, #I2C_SADDR]
155         orr     r2, r2, #(1 << 1)
156         str     r2, [r0, #I2C_CNTRL]
157         mov     r1, #1
158         str     r1, [r0, #I2C_DACNT]
160         /* wait for transfer to finish */
162         ldr     r1, [r0, #I2C_DACNT]
163         cmp     r1, #0
164         bne     1b
166         /* load result and test USB_STATUS bit */
167         ldr     r1, [r0, #I2C_DATA]
168         tst     r1, #(1 << 3)
169         bne     boot_of
170 #endif
172         /* Here are model specific tests, for dual boot without a computer */
173         /* All models use left button */
174         /* /!\ Right button for c200v2 (left button is unkwown) */
176 #ifdef SANSA_CLIP
177 .set row, (1<<5) /* enable output on C5 */
178 .set col, (1<<0) /* read keyscan column B0 */
180         ldr     r0, =GPIOC
181         mov     r1, #row
182         str     r1, [r0, #0x400]
183         str     r1, [r0, #(4*row)]
185         ldr     r0, =GPIOB
186         mov     r1, #0
187         str     r1, [r0, #0x400]
188         ldr     r1, [r0, #(4*col)]
190         cmp     r1, #0
191         bne     boot_of
192 #elif defined(SANSA_CLIPV2)
193 .set row, (1<<4) /* enable output on D4 */
194 .set col, (1<<0) /* read keyscan column D0 */
196         ldr     r0, =GPIOD
197         mov     r1, #((1<<5)|(1<<4)|(1<<3)) /* all rows as output */
198         str     r1, [r0, #0x400]
200         /* all rows high */
201         mov     r1, #(1<<3)
202         str     r1, [r0, #(4*(1<<3))]
203         mov     r1, #(1<<4)
204         str     r1, [r0, #(4*(1<<4))]
205         mov     r1, #(1<<5)
206         str     r1, [r0, #(4*(1<<5))]
208         mov     r1, #0                      /* button row low */
209         str     r1, [r0, #(4*row)]
211         mov     r1, #5                      /* small delay */
212 1:      subs    r1, r1, #1
213         bne     1b
215         ldr     r1, [r0, #(4*col)]
217         cmp     r1, #0
218         beq     boot_of
219 #elif defined(SANSA_E200V2) || defined(SANSA_FUZE)
220         ldr     r0, =GPIOC
221         mov     r1, #0
222         str     r1, [r0, #0x400]
223         ldr     r1, [r0, #0x20]    /* read pin C3 */
225         cmp     r1, #0             /* C3 = #0 means button pressed */
226         beq     boot_of
228 #elif defined(SANSA_FUZEV2)
229         ldr     r0, =GPIOC
230         mov     r1, #0
231         str     r1, [r0, #0x400]
232         ldr     r1, [r0, #0x20]    /* read pin C3 */
234         cmp     r1, #0             /* C3 != #0 means button pressed */
235         bne     boot_of
237 #elif defined(SANSA_CLIPPLUS)
238         @ read pins
239         ldr     r0, =GPIOC
240         ldr     r1, [r0, #4*(1<<3)] @ read pin C3 "|<<"
242         ldr     r0, =GPIOA
243         ldr     r2, [r0, #4*(1<<1)] @ read pin A1 "Home"
245         orr     r2, r2, r1          @ c3 || A1
247         cmp     r2, #0              @ test input from pins
248         bne     boot_of             @ branch directly to OF if either pin high
251 #elif defined(SANSA_C200V2)
252 .set BUTTON_LEFT,      (1<< 2)
253 .set BUTTON_DOWN,      (1<< 3)
254 .set BUTTON_SELECT,    (1<< 4)
255 .set BUTTON_UP,        (1<< 5)
256 .set BUTTON_RIGHT,     (1<< 6)
257 .set BUTTON_HOLD,      (1<<12)
258 .set ENRD0_USB_STATUS, (1<< 3)
260         ldr     r0, =CGU_DBOP
261         mov     r1, #(1<<3)         @ DBOP freq = PCLK, clock enabled
262         str     r1, [r0]
264         @ AFSEL needs to be set for this to work
265         ldr     r2, =GPIOB
266         mov     r1, #0xc
267         str     r1, [r2, #0x420]    @ GPIOB_AFSEL
268         ldr     r2, =GPIOC
269         mov     r1, #0xff
270         str     r1, [r2, #0x420]    @ GPIOC_AFSEL
272         ldr     r0, =DBOP
273         @ TIMPOL doesn't matter here since we don't need
274         @ the control signals.
276         @ 16 bit data width
277         @ enable write
278         @ tri-state output
279         ldr     r1, =0x00091000
280         str     r1, [r0, #8]        @ DBOP_CTRL
282         ldr     r1, =0xf0ff         @ precharge
283         str     r1, [r0, #0x10]     @ DBOP_DOUT
285 2:      ldr     r1, [r0, #0xc]      @ DOBP_STAT
286         ands    r1, r1, #(1<<10)
287         beq     2b                  @ make sure fifo is empty
289         @ 16 bit data width
290         @ start read
291         @ tri-state output
292         @ strobe time 31
293         ldr     r1, =0x0008901f
294         str     r1, [r0, #8]        @ DBOP_CTRL
296 3:      ldr     r1, [r0, #0xc]      @ DOBP_STAT
297         ands    r1, r1, #(1<<16)
298         beq     3b                  @ wait for valid data
300         ldrh    r1, [r0, #0x14]     @ DBOP_DIN
302         tst     r1, #BUTTON_LEFT    @ boot of?
303         beq     boot_of
304 #elif defined(SANSA_M200V4)
305 .set row, (1<<5) /* enable output on A5 */
306 .set col, (1<<0) /* read keyscan column A0 */
308         ldr     r0, =GPIOA
309         mov     r1, #row
310         str     r1, [r0, #0x400]
311         str     r1, [r0, #(4*row)]
313         ldr     r2, [r0, #(4*col)]
315         /* check value read (1 means button pressed) */
316         cmp     r2, #0
317         bne     boot_of
318 #else
319         #error No target-specific key check defined!
320 #endif
322 #if defined(SANSA_CLIPPLUS) || defined(SANSA_FUZEV2)
323         /* Check for USB after buttons because I trust more the GPIO code than
324          * the i2c code.
325          * Also it seems we need to wait a bit before detecting USB connection
326          * on those models, but not on c200v2
327          */
330         ldr     r0, =CGU_PROC
331         mov     r1, #0
332         str     r1, [r0]    @ fclk = 24MHz
334         ldr     r0, =CGU_PERI
335         ldr     r1, [r0]
336         /* enable i2c audio master clock */
337         orr     r1, r1, #(1<<17)
338         /* pclk = fclk = 24MHz */
339         bic     r1, r1, #0x7f
340         str     r1, [r0]
342         ldr     r0, =I2C_BASE
343         /* disable i2c interrupts */
344         mov     r1, #0
345         str     r1, [r0, #I2C_IMR]
346         /* setup prescaler */
347         mov     r1, #I2C_PRESCALER_LOW
348         str     r1, [r0, #I2C_CPSR0]
349         mov     r1, #I2C_PRESCALER_HIGH
350         str     r1, [r0, #I2C_CPSR1]
351         /* setup i2c slave address */
352         mov     r1, #(AS3514_I2C_ADDR << 1)
353         str     r1, [r0, #I2C_SLAD0]
354         mov     r2, #0x51
355         str     r2, [r0, #I2C_CNTRL]
357         /* wait for not busy */
359         ldr     r1, [r0, #I2C_SR]
360         tst     r1, #1
361         bne 1b
363         /* wait a bit (~100ms) else detection fails */
364         mov     r1, #0x80000
365 1:      subs    r1, r1, #1
366         bne 1b
368         /* start read of irq_enrd0 */
369         mov     r1, #AS3514_IRQ_ENRD0
370         str     r1, [r0, #I2C_SADDR]
371         orr     r2, r2, #(1 << 1)
372         str     r2, [r0, #I2C_CNTRL]
373         mov     r1, #1
374         str     r1, [r0, #I2C_DACNT]
376         /* wait for transfer to finish */
378         ldr     r1, [r0, #I2C_DACNT]
379         cmp     r1, #0
380         bne     1b
382         /* load result and test USB_STATUS bit */
383         ldr     r1, [r0, #I2C_DATA]
384         tst     r1, #(1 << 3)
385         bne     boot_of
388 #endif
391         /* The dualboot button was not held, so we boot rockbox */
392         ldr     r0, ucl_rb_end      /* Address of compressed image */
393         ldr     r1, ucl_rb_size     /* Compressed size */
394         b       decompress
396 boot_of:
397         ldr     r0, ucl_of_end      /* Address of compressed image */
398         ldr     r1, ucl_of_size     /* Compressed size */
401 decompress:
402         /* At this point:                                              */
403         /* r0 = source_end for UCL image to copy                       */
404         /* r1 = size of UCL image to copy                              */
406         ldr     r3, ucl_dest
407         add     r5, r3, #2      /* r5 is entry point of copy of uclunpack */
408                                 /* function, plus one (for thumb mode */
410         sub     r4, r3, r1      /* r4 := destination_start - 1 */
412 fw_copy:
413         ldrb    r2, [r0], #-1
414         strb    r2, [r3], #-1
415         cmp     r3, r4          /* Stop when we reached dest_start-1 */
416         bne     fw_copy
418         /* Call the ucl decompress function, which will branch to 0x0 */
419         /* on completion */
420         add     r0, r3, #1      /* r0 := Start of compressed image */
421                                 /* r1 already contains compressed size */
422         mov     r2, #0          /* r2 := Destination for unpacking */
423         bx      r5              /* Branch to uclunpack, switching to thumb */
425         /* never reached : uclunpack will branch to the reset vector (0x0) */