RaaA: Add initial Pandora support
[maemo-rb.git] / rbutil / mkamsboot / dualboot / dualboot.S
blob242eefa815ba2b107d31465f7397c5bdd94a3fdc
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         b start     @ skip our data
56 /* These values are filled in by mkamsboot - don't move them from offset 0x4 */
58 uclunpack_end:   .word   0 /* End of the ucl_unpack function */
59 uclunpack_size:  .word   0 /* Size in bytes of the ucl_unpack function */
61 ucl_of_end:      .word   0 /* End of the ucl-compressed OF image */
62 ucl_of_size:     .word   0 /* Size in bytes of the compressed OF image */
64 ucl_rb_end:      .word   0 /* End of the ucl-compressed RB image */
65 ucl_rb_size:     .word   0 /* Size in bytes of the compressed RB image */
67 ucl_dest:        .word   0 /* End of our destination buffer (end of memory) */
69 start:
70         /* First copy the UCL unpack function to the end of RAM */
71         ldr     r0, uclunpack_end   /* Source */
72         ldr     r1, uclunpack_size  /* Source length */
73         sub     r2, r0, r1          /* Source start - 1*/
75         ldr     r3, ucl_dest        /* Destination end */
77 uclcopy:
78         ldrb    r4, [r0], #-1
79         strb    r4, [r3], #-1
80         cmp     r2, r0
81         bne     uclcopy
83         /* store the new destination buffer */
84         str     r3, ucl_dest
86         /* enable gpio clock */
87         ldr     r0, =CGU_PERI
88         ldr     r1, [r0]
89         orr     r1, r1, #(1<<16)
90         str     r1, [r0]
93 #ifdef SANSA_CLIPV2 /* TODO : remove this check when we'll have an USB driver */
94         ldr     r0, =GPIOA
95         mov     r1, #0
96         str     r1, [r0, #0x400]
97         ldr     r1, [r0, #(4*(1<<6))]
98         cmp     r1, #0
99         bne     boot_of
100 #endif
101         /* Here are model specific tests for dual boot */
102         /* All models but c200v2 use left button */
103         /* TODO: Left button for c200v2 too (needs DBOP code) */
105 #ifdef SANSA_CLIP
106 .set row, (1<<5) /* enable output on C5 */
107 .set col, (1<<0) /* read keyscan column B0 */
109         ldr     r0, =GPIOC
110         mov     r1, #row
111         str     r1, [r0, #0x400]
112         str     r1, [r0, #(4*row)]
114         ldr     r0, =GPIOB
115         mov     r1, #0
116         str     r1, [r0, #0x400]
117         ldr     r1, [r0, #(4*col)]
119         cmp     r1, #0
120         bne     boot_of
121 #elif defined(SANSA_CLIPV2)
122 .set row, (1<<4) /* enable output on D4 */
123 .set col, (1<<0) /* read keyscan column D0 */
125         ldr     r0, =GPIOD
126         mov     r1, #((1<<5)|(1<<4)|(1<<3)) /* all rows as output */
127         str     r1, [r0, #0x400]
129         /* all rows high */
130         mov     r1, #(1<<3)
131         str     r1, [r0, #(4*(1<<3))]
132         mov     r1, #(1<<4)
133         str     r1, [r0, #(4*(1<<4))]
134         mov     r1, #(1<<5)
135         str     r1, [r0, #(4*(1<<5))]
137         mov     r1, #0                      /* button row low */
138         str     r1, [r0, #(4*row)]
140         mov     r1, #5                      /* small delay */
141 1:      subs    r1, r1, #1
142         bne     1b
144         ldr     r1, [r0, #(4*col)]
146         cmp     r1, #0
147         beq     boot_of
148 #elif defined(SANSA_E200V2) || defined(SANSA_FUZE)
149         ldr     r0, =GPIOC
150         mov     r1, #0
151         str     r1, [r0, #0x400]
152         ldr     r1, [r0, #0x20]    /* read pin C3 */
154         cmp     r1, #0             /* C3 = #0 means button pressed */
155         beq     boot_of
157 #elif defined(SANSA_FUZEV2)
158         ldr     r0, =GPIOC
159         mov     r1, #0
160         str     r1, [r0, #0x400]
161         ldr     r1, [r0, #0x20]    /* read pin C3 */
163         cmp     r1, #0             /* C3 != #0 means button pressed */
164         bne     boot_of
166 #elif defined(SANSA_CLIPPLUS)
167         @ read pins
168         ldr     r0, =GPIOC
169         ldr     r1, [r0, #4*(1<<3)] @ read pin C3 "|<<"
171         ldr     r0, =GPIOA
172         ldr     r2, [r0, #4*(1<<1)] @ read pin A1 "Home"
174         orr     r2, r2, r1          @ c3 || A1
176         cmp     r2, #0              @ test input from pins
177         bne     boot_of             @ branch directly to OF if either pin high
180 #elif defined(SANSA_C200V2)
181 .set BUTTON_LEFT,      (1<< 2)
182 .set BUTTON_DOWN,      (1<< 3)
183 .set BUTTON_SELECT,    (1<< 4)
184 .set BUTTON_UP,        (1<< 5)
185 .set BUTTON_RIGHT,     (1<< 6)
186 .set BUTTON_HOLD,      (1<<12)
188         ldr     r0, =CGU_DBOP
189         mov     r1, #(1<<3)         @ DBOP freq = PCLK, clock enabled
190         str     r1, [r0]
192         @ AFSEL needs to be set for this to work
193         ldr     r2, =GPIOB
194         mov     r1, #0xc
195         str     r1, [r2, #0x420]    @ GPIOB_AFSEL
196         ldr     r2, =GPIOC
197         mov     r1, #0xff
198         str     r1, [r2, #0x420]    @ GPIOC_AFSEL
200         ldr     r0, =DBOP
201         @ TIMPOL doesn't matter here since we don't need
202         @ the control signals.
204         @ 16 bit data width
205         @ enable write
206         @ tri-state output
207         ldr     r1, =0x00091000
208         str     r1, [r0, #8]        @ DBOP_CTRL
210         ldr     r1, =0xf0ff         @ precharge
211         str     r1, [r0, #0x10]     @ DBOP_DOUT
213 2:      ldr     r1, [r0, #0xc]      @ DOBP_STAT
214         ands    r1, r1, #(1<<10)
215         beq     2b                  @ make sure fifo is empty
217         @ 16 bit data width
218         @ start read
219         @ tri-state output
220         @ strobe time 31
221         ldr     r1, =0x0008901f
222         str     r1, [r0, #8]        @ DBOP_CTRL
224 3:      ldr     r1, [r0, #0xc]      @ DOBP_STAT
225         ands    r1, r1, #(1<<16)
226         beq     3b                  @ wait for valid data
228         ldrh    r1, [r0, #0x14]     @ DBOP_DIN
230         tst     r1, #BUTTON_LEFT    @ boot of?
231         beq     boot_of
232 #elif defined(SANSA_M200V4)
233 .set row, (1<<5) /* enable output on A5 */
234 .set col, (1<<0) /* read keyscan column A0 */
236         ldr     r0, =GPIOA
237         mov     r1, #row
238         str     r1, [r0, #0x400]
239         str     r1, [r0, #(4*row)]
241         ldr     r2, [r0, #(4*col)]
243         /* check value read (1 means button pressed) */
244         cmp     r2, #0
245         bne     boot_of
246 #else
247         #error No target-specific key check defined!
248 #endif
250 #if defined(SANSA_CLIPPLUS) || defined(SANSA_FUZEV2)
251         /* Check for USB after buttons because I trust more the GPIO code than
252          * the i2c code.
253          * Also it seems we need to wait a bit before detecting USB connection
254          * on those models, but not on c200v2
255          */
258         ldr     r0, =CGU_PROC
259         mov     r1, #0
260         str     r1, [r0]    @ fclk = 24MHz
262         ldr     r0, =CGU_PERI
263         ldr     r1, [r0]
264         /* enable i2c audio master clock */
265         orr     r1, r1, #(1<<17)
266         /* pclk = fclk = 24MHz */
267         bic     r1, r1, #0x7f
268         str     r1, [r0]
270         ldr     r0, =I2C_BASE
271         /* disable i2c interrupts */
272         mov     r1, #0
273         str     r1, [r0, #I2C_IMR]
274         /* setup prescaler */
275         mov     r1, #I2C_PRESCALER_LOW
276         str     r1, [r0, #I2C_CPSR0]
277         mov     r1, #I2C_PRESCALER_HIGH
278         str     r1, [r0, #I2C_CPSR1]
279         /* setup i2c slave address */
280         mov     r1, #(AS3514_I2C_ADDR << 1)
281         str     r1, [r0, #I2C_SLAD0]
282         mov     r2, #0x51
283         str     r2, [r0, #I2C_CNTRL]
285         /* wait for not busy */
287         ldr     r1, [r0, #I2C_SR]
288         tst     r1, #1
289         bne 1b
291         /* wait a bit (~100ms) else detection fails */
292         mov     r1, #0x80000
293 1:      subs    r1, r1, #1
294         bne 1b
296         /* start read of irq_enrd0 */
297         mov     r1, #AS3514_IRQ_ENRD0
298         str     r1, [r0, #I2C_SADDR]
299         orr     r2, r2, #(1 << 1)
300         str     r2, [r0, #I2C_CNTRL]
301         mov     r1, #1
302         str     r1, [r0, #I2C_DACNT]
304         /* wait for transfer to finish */
306         ldr     r1, [r0, #I2C_DACNT]
307         cmp     r1, #0
308         bne     1b
310         /* load result and test USB_STATUS bit */
311         ldr     r1, [r0, #I2C_DATA]
312         tst     r1, #(1 << 3)
313         bne     boot_of
316 #endif
319         /* The dualboot button was not held, so we boot rockbox */
320         ldr     r0, ucl_rb_end      /* Address of compressed image */
321         ldr     r1, ucl_rb_size     /* Compressed size */
322         b       decompress
324 boot_of:
325         ldr     r0, ucl_of_end      /* Address of compressed image */
326         ldr     r1, ucl_of_size     /* Compressed size */
329 decompress:
330         /* At this point:                                              */
331         /* r0 = source_end for UCL image to copy                       */
332         /* r1 = size of UCL image to copy                              */
334         ldr     r3, ucl_dest
335         add     r5, r3, #2      /* r5 is entry point of copy of uclunpack */
336                                 /* function, plus one (for thumb mode */
338         sub     r4, r3, r1      /* r4 := destination_start - 1 */
340 fw_copy:
341         ldrb    r2, [r0], #-1
342         strb    r2, [r3], #-1
343         cmp     r3, r4          /* Stop when we reached dest_start-1 */
344         bne     fw_copy
346         /* Call the ucl decompress function, which will branch to 0x0 */
347         /* on completion */
348         add     r0, r3, #1      /* r0 := Start of compressed image */
349                                 /* r1 already contains compressed size */
350         mov     r2, #0          /* r2 := Destination for unpacking */
351         bx      r5              /* Branch to uclunpack, switching to thumb */
353         /* never reached : uclunpack will branch to the reset vector (0x0) */