1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2003 by Jörg Hohensohn
12 * Second-level bootloader, with dual-boot feature by holding F1/Menu
13 * This is the image being descrambled and executed by the boot ROM.
14 * It's task is to copy Rockbox from Flash to DRAM.
15 * The image(s) in flash may optionally be compressed with UCL 2e
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
28 #include "bootloader.h"
31 static void PlatformInit(void);
32 static void DecompressStart(tImage
* pImage
);
34 static int ReadADC(int channel
);
36 static int ButtonPressed(void);
37 static tImage
* GetStartImage(int nPreferred
);
39 static void SetLed(BOOL bOn
);
40 static void UartInit(void);
41 static UINT8
UartRead(void);
42 static void UartWrite(UINT8 byte
);
43 static void MiniMon(void);
47 /* start with the vector table */
48 UINT32 vectors
[] __attribute__ ((section (".vectors"))) =
50 (UINT32
)_main
, /* entry point, the copy routine */
51 (UINT32
)(end_stack
- 1), /* initial stack pointer */
52 FLASH_BASE
+ 0x200, /* source of image in flash */
53 (UINT32
)total_size
, /* size of image */
54 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
55 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
56 0x03020080 /* mask and version (just as a suggestion) */
59 /* our binary has to start with a vector to the entry point */
60 tpMain start_vector
[] __attribute__ ((section (".startvector"))) = {main
};
63 #ifdef NO_ROM /* some code which is only needed for the romless variant */
70 asm volatile ("ldc %0,sr" : : "r"(0xF0)); // disable interrupts
71 asm volatile ("mov.l @%0,r15" : : "r"(4)); // load stack
72 asm volatile ("ldc %0,vbr" : : "r"(0)); // load vector base
74 /* copy everything to IRAM and continue there */
75 pSrc
= begin_iramcopy
;
77 pEnd
= pDest
+ (begin_stack
- begin_text
);
85 main(); /* jump to the real main() */
89 static void BootInit(void)
91 /* inits from the boot ROM, whether they make sense or not */
92 PBDR
&= 0xFFBF; /* LED off (0x131E) */
93 PBCR2
= 0; /* all GPIO */
94 PBIOR
|= 0x0040; /* LED output */
95 PBIOR
&= 0xFFF1; /* LCD lines input */
97 /* init DRAM like the boot ROM does */
108 #endif /* #ifdef NO_ROM */
115 PlatformInit(); /* model-specific inits */
117 nButton
= ButtonPressed();
120 { /* F3 means start monitor */
126 pImage
= GetStartImage(nButton
); /* which image */
127 DecompressStart(pImage
); /* move into place and start it */
130 return 0; /* I guess we won't return ;-) */
134 /* init code that is specific to certain platform */
135 static void PlatformInit(void)
138 BootInit(); /* if not started by boot ROM, we need to init what it did */
141 #if defined PLATFORM_PLAYER
142 BRR1
= 0x19; /* 14400 Baud for monitor */
143 PBDRL
|= 0x10; /* set PB4 to 1 to power the hd early (and prepare for
144 * probing in case the charger is connected) */
145 PBIORL
|= 0x10; /* make PB4 an output */
146 PACR2
&= 0xFFFC; /* GPIO for PA0 (charger detection, input by default) */
147 if (!(PADRL
& 0x01)) /* charger plugged? */
148 { /* we need to probe whether the box is able to control hd power */
151 PBIORL
&= ~0x10; /* set PB4 to input */
152 /* wait whether it goes low, max. ~1 ms */
153 for (i
= 0; (PBDRL
& 0x10) && i
< 1000; i
++);
155 if (~(PBDRL
& 0x10)) /* pulled low -> power controllable */
156 PBDRL
&= 0x10; /* set PB4 low */
157 else /* still floating high -> not controllable */
158 PBDRL
|= 0x10; /* set PB4 high */
159 PBIORL
|= 0x10; /* ..and output again */
161 #elif defined PLATFORM_RECORDER
162 BRR1
= 0x02; /* 115200 Baud for monitor */
163 if (ReadADC(7) > 0x100) /* charger plugged? */
164 { /* switch off the HD, else a flat battery may not start */
165 PACR2
&= 0xFBFF; /* GPIO for PA5 */
166 PAIOR
|= 0x0020; /* make PA5 an output (low by default) */
168 #elif defined PLATFORM_FM
169 BRR1
= 0x02; /* 115200 Baud for monitor */
170 PBDR
|= 0x0020; /* set PB5 to keep power (fixes the ON-holding problem) */
171 PBIOR
|= 0x0020; /* make PB5 an output */
172 if (ReadADC(0) < 0x1FF) /* charger plugged? */
173 { /* switch off the HD, else a flat battery may not start */
174 PACR2
&= 0xFBFF; /* GPIO for PA5 */
175 PAIOR
|= 0x0020; /* make PA5 an output (low by default) */
177 #elif defined PLATFORM_ONDIO
178 BRR1
= 0x19; /* 14400 Baud for monitor */
179 PBDR
|= 0x0020; /* set PB5 to keep power (fixes the ON-holding problem) */
180 PBIOR
|= 0x0020; /* make PB5 an output */
183 /* platform-independent inits */
184 DCR
|= 0x1000; /* enable burst mode on DRAM */
185 BCR
|= 0x2000; /* activate Warp mode (simultaneous internal and external
190 /* move the image into place and start it */
191 static void DecompressStart(tImage
* pImage
)
196 pSrc
= pImage
->image
;
197 pDest
= pImage
->pDestination
;
199 if (pSrc
!= pDest
) /* if not linked to that flash address */
201 if (pImage
->flags
& IF_UCL_2E
)
202 { /* UCL compressed, algorithm 2e */
203 UINT32 dst_len
; /* dummy */
204 ucl_nrv2e_decompress_8((UINT8
*)pSrc
, (UINT8
*)pDest
, &dst_len
);
207 { /* uncompressed, copy it */
208 UINT32 size
= pImage
->size
;
210 size
= (size
+ 3) / 4; /* round up to 32bit-words */
217 while (pDest
< pEnd
);
225 static int ReadADC(int channel
)
227 /* after channel 3, the ports wrap and get re-used */
228 volatile UINT16
* pResult
= (UINT16
*)(ADDRAH_ADDR
+ 2 * (channel
& 0x03));
229 int timeout
= 266; /* conversion takes 266 clock cycles */
231 ADCSR
= 0x20 | channel
; /* start single conversion */
232 while (((ADCSR
& 0x80) == 0) && (--timeout
)); /* 6 instructions per round*/
234 return (timeout
== 0) ? -1 : *pResult
>>6;
239 /* This function is platform-dependent,
240 * until I figure out how to distinguish at runtime. */
241 static int ButtonPressed(void) /* return 1,2,3 for F1,F2,F3, 0 if none pressed */
244 int value
= ReadADC(CHANNEL
);
246 if (value
>= F1_LOWER
&& value
<= F1_UPPER
) /* in range */
248 else if (value
>= F2_LOWER
&& value
<= F2_UPPER
) /* in range */
250 else if (value
>= F3_LOWER
&& value
<= F3_UPPER
) /* in range */
255 if (!(value
& F1_MASK
))
257 else if (!(value
& F2_MASK
))
259 else if (!(value
& F3_MASK
))
267 /* Determine the image to be started */
268 static tImage
* GetStartImage(int nPreferred
)
271 tImage
* pImage2
= NULL
; /* default to not present */
273 UINT32
* pFlash
= (UINT32
*)FLASH_BASE
;
275 /* determine the first image position */
276 pos
= pFlash
[2] + pFlash
[3]; /* position + size of the bootloader
278 pos
= (pos
+ 3) & ~3; /* be sure it's 32 bit aligned */
280 pImage1
= (tImage
*)pos
;
282 if (pImage1
->size
!= 0)
283 { /* check for second image */
284 pos
= (UINT32
)(&pImage1
->image
) + pImage1
->size
;
285 pImage2
= (tImage
*)pos
;
287 /* does it make sense? (not in FF or 00 erazed space) */
288 if (pImage2
->pDestination
== (void*)0xFFFFFFFF
289 || pImage2
->size
== 0xFFFFFFFF
290 || pImage2
->pExecute
== (void*)0xFFFFFFFF
291 || pImage2
->flags
== 0xFFFFFFFF
292 || pImage2
->pDestination
== NULL
)
293 /* size, execute and flags can legally be 0 */
295 pImage2
= NULL
; /* invalidate */
299 if (pImage2
== NULL
|| nPreferred
== 1)
300 { /* no second image or overridden: return the first */
304 return pImage2
; /* return second image */
307 /* diagnostic functions */
309 static void SetLed(BOOL bOn
)
318 static void UartInit(void)
320 PBIOR
&= 0xFBFF; /* input: RXD1 remote pin */
321 PBCR1
|= 0x00A0; /* set PB11+PB10 to UART */
322 PBCR1
&= 0xFFAF; /* clear bits 6, 4 -> UART */
323 SMR1
= 0x00; /* async format 8N1, baud generator input is CPU clock */
324 SCR1
= 0x30; /* transmit+receive enable */
325 PBCR1
&= 0x00FF; /* set bit 12...15 as GPIO */
326 SSR1
&= 0xBF; /* clear bit 6 (RDRF, receive data register full) */
330 static UINT8
UartRead(void)
333 while (!(SSR1
& SCI_RDRF
)); /* wait for char to be available */
340 static void UartWrite(UINT8 byte
)
342 while (!(SSR1
& SCI_TDRE
)); /* wait for transmit buffer empty */
348 /* include the mini monitor as a rescue feature, started with F3 */
349 static void MiniMon(void)
355 volatile UINT8
* paddr
= NULL
;
356 volatile UINT8
* pflash
= NULL
; /* flash base address */
366 content
= UartRead();
367 UartWrite(cmd
); /* acknowledge by returning the command value */
368 while (!(SSR1
& SCI_TEND
)); /* wait for empty shift register,
369 * before changing baudrate */
374 addr
= (UartRead() << 24) | (UartRead() << 16)
375 | (UartRead() << 8) | UartRead();
376 paddr
= (UINT8
*)addr
;
377 pflash
= (UINT8
*)(addr
& 0xFFF80000); /* round down to 512k align*/
378 UartWrite(cmd
); /* acknowledge by returning the command value */
383 UartWrite(content
); /* the content is the ack */
387 content
= UartRead();
389 UartWrite(cmd
); /* acknowledge by returning the command value */
397 UartWrite(content
); /* the content is the ack */
405 content
= UartRead();
408 UartWrite(cmd
); /* acknowledge by returning the command value */
412 content
= UartRead();
413 pflash
[0x5555] = 0xAA; /* set flash to command mode */
414 pflash
[0x2AAA] = 0x55;
415 pflash
[0x5555] = 0xA0; /* byte program command */
417 UartWrite(cmd
); /* acknowledge by returning the command value */
424 content
= UartRead();
425 pflash
[0x5555] = 0xAA; /* set flash to command mode */
426 pflash
[0x2AAA] = 0x55;
427 pflash
[0x5555] = 0xA0; /* byte program command */
430 UartWrite(cmd
); /* acknowledge by returning the command value */
434 content
= *(UINT16
*)paddr
;
436 UartWrite(content
>> 8); /* highbyte */
437 UartWrite(content
& 0xFF); /* lowbyte */
441 content
= UartRead() << 8 | UartRead();
442 *(UINT16
*)paddr
= content
;
444 UartWrite(cmd
); /* acknowledge by returning the command value */
449 tpFunc pFunc
= (tpFunc
)paddr
;
451 UartWrite(cmd
); /* acknowledge by returning the command value*/
456 UartWrite(1); /* return our version number */
462 UartWrite(~cmd
); /* error acknowledge */