Fix rbutil building with gcc 4.3 on linux (FS#8757 by Dennis Schridde).
[Rockbox.git] / flash / bootloader / bootloader.c
blobd5ce509cce2dc502294cc5b2910f427c069deb75
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 * All files in this archive are subject to the GNU General Public License.
18 * See the file COPYING in the source tree root for full license agreement.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 ****************************************************************************/
25 #include "sh7034.h"
26 #include "bootloader.h"
29 #ifdef NO_ROM
30 /* start with the vector table */
31 UINT32 vectors[] __attribute__ ((section (".vectors"))) =
33 (UINT32)_main, /* entry point, the copy routine */
34 (UINT32)(end_stack - 1), /* initial stack pointer */
35 FLASH_BASE + 0x200, /* source of image in flash */
36 (UINT32)total_size, /* size of image */
37 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,
38 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,
39 0x03020080 /* mask and version (just as a suggestion) */
41 #else
42 /* our binary has to start with a vector to the entry point */
43 tpMain start_vector[] __attribute__ ((section (".startvector"))) = {main};
44 #endif
46 #ifdef NO_ROM /* some code which is only needed for the romless variant */
47 void _main(void)
49 UINT32* pSrc;
50 UINT32* pDest;
51 UINT32* pEnd;
53 asm volatile ("ldc %0,sr" : : "r"(0xF0)); // disable interrupts
54 asm volatile ("mov.l @%0,r15" : : "r"(4)); // load stack
55 asm volatile ("ldc %0,vbr" : : "r"(0)); // load vector base
57 /* copy everything to IRAM and continue there */
58 pSrc = begin_iramcopy;
59 pDest = begin_text;
60 pEnd = pDest + (begin_stack - begin_text);
64 *pDest++ = *pSrc++;
66 while (pDest < pEnd);
68 main(); /* jump to the real main() */
72 void BootInit(void)
74 /* inits from the boot ROM, whether they make sense or not */
75 PBDR &= 0xFFBF; /* LED off (0x131E) */
76 PBCR2 = 0; /* all GPIO */
77 PBIOR |= 0x0040; /* LED output */
78 PBIOR &= 0xFFF1; /* LCD lines input */
80 /* init DRAM like the boot ROM does */
81 PACR2 &= 0xFFFB;
82 PACR2 |= 0x0008;
83 CASCR = 0xAF;
84 BCR |= 0x8000;
85 WCR1 &= 0xFDFD;
86 DCR = 0x0E00;
87 RCR = 0x5AB0;
88 RTCOR = 0x9605;
89 RTCSR = 0xA518;
91 #endif /* #ifdef NO_ROM */
94 int main(void)
96 int nButton;
98 PlatformInit(); /* model-specific inits */
100 nButton = ButtonPressed();
102 if (nButton == 3)
103 { /* F3 means start monitor */
104 MiniMon();
106 else
108 tImage* pImage;
109 pImage = GetStartImage(nButton); /* which image */
110 DecompressStart(pImage); /* move into place and start it */
113 return 0; /* I guess we won't return ;-) */
117 /* init code that is specific to certain platform */
118 void PlatformInit(void)
120 #ifdef NO_ROM
121 BootInit(); /* if not started by boot ROM, we need to init what it did */
122 #endif
124 #if defined PLATFORM_PLAYER
125 BRR1 = 0x19; /* 14400 Baud for monitor */
126 PBDRL |= 0x10; /* set PB4 to 1 to power the hd early (and prepare for
127 * probing in case the charger is connected) */
128 PBIORL |= 0x10; /* make PB4 an output */
129 PACR2 &= 0xFFFC; /* GPIO for PA0 (charger detection, input by default) */
130 if (!(PADRL & 0x01)) /* charger plugged? */
131 { /* we need to probe whether the box is able to control hd power */
132 int i;
134 PBIORL &= ~0x10; /* set PB4 to input */
135 /* wait whether it goes low, max. ~1 ms */
136 for (i = 0; (PBDRL & 0x10) && i < 1000; i++);
138 if (~(PBDRL & 0x10)) /* pulled low -> power controllable */
139 PBDRL &= 0x10; /* set PB4 low */
140 else /* still floating high -> not controllable */
141 PBDRL |= 0x10; /* set PB4 high */
142 PBIORL |= 0x10; /* ..and output again */
144 #elif defined PLATFORM_RECORDER
145 BRR1 = 0x02; /* 115200 Baud for monitor */
146 if (ReadADC(7) > 0x100) /* charger plugged? */
147 { /* switch off the HD, else a flat battery may not start */
148 PACR2 &= 0xFBFF; /* GPIO for PA5 */
149 PAIOR |= 0x0020; /* make PA5 an output (low by default) */
151 #elif defined PLATFORM_FM
152 BRR1 = 0x02; /* 115200 Baud for monitor */
153 PBDR |= 0x0020; /* set PB5 to keep power (fixes the ON-holding problem) */
154 PBIOR |= 0x0020; /* make PB5 an output */
155 if (ReadADC(0) < 0x1FF) /* charger plugged? */
156 { /* switch off the HD, else a flat battery may not start */
157 PACR2 &= 0xFBFF; /* GPIO for PA5 */
158 PAIOR |= 0x0020; /* make PA5 an output (low by default) */
160 #elif defined PLATFORM_ONDIO
161 BRR1 = 0x19; /* 14400 Baud for monitor */
162 PBDR |= 0x0020; /* set PB5 to keep power (fixes the ON-holding problem) */
163 PBIOR |= 0x0020; /* make PB5 an output */
164 #endif
166 /* platform-independent inits */
167 DCR |= 0x1000; /* enable burst mode on DRAM */
168 BCR |= 0x2000; /* activate Warp mode (simultaneous internal and external
169 * mem access) */
173 /* Thinned out version of the UCL 2e decompression sourcecode
174 * Original (C) Markus F.X.J Oberhumer under GNU GPL license */
175 #define GETBIT(bb, src, ilen) \
176 (((bb = bb & 0x7f ? bb*2 : ((unsigned)src[ilen++]*2+1)) >> 8) & 1)
178 int ucl_nrv2e_decompress_8(
179 const UINT8 *src, UINT8 *dst, UINT32* dst_len)
181 UINT32 bb = 0;
182 unsigned ilen = 0, olen = 0, last_m_off = 1;
184 for (;;)
186 unsigned m_off, m_len;
188 while (GETBIT(bb,src,ilen))
190 dst[olen++] = src[ilen++];
192 m_off = 1;
193 for (;;)
195 m_off = m_off*2 + GETBIT(bb,src,ilen);
196 if (GETBIT(bb,src,ilen)) break;
197 m_off = (m_off-1)*2 + GETBIT(bb,src,ilen);
199 if (m_off == 2)
201 m_off = last_m_off;
202 m_len = GETBIT(bb,src,ilen);
204 else
206 m_off = (m_off-3)*256 + src[ilen++];
207 if (m_off == 0xffffffff)
208 break;
209 m_len = (m_off ^ 0xffffffff) & 1;
210 m_off >>= 1;
211 last_m_off = ++m_off;
213 if (m_len)
214 m_len = 1 + GETBIT(bb,src,ilen);
215 else if (GETBIT(bb,src,ilen))
216 m_len = 3 + GETBIT(bb,src,ilen);
217 else
219 m_len++;
220 do {
221 m_len = m_len*2 + GETBIT(bb,src,ilen);
222 } while (!GETBIT(bb,src,ilen));
223 m_len += 3;
225 m_len += (m_off > 0x500);
227 const UINT8 *m_pos;
228 m_pos = dst + olen - m_off;
229 dst[olen++] = *m_pos++;
230 do dst[olen++] = *m_pos++; while (--m_len > 0);
233 *dst_len = olen;
235 return ilen;
239 /* move the image into place and start it */
240 void DecompressStart(tImage* pImage)
242 UINT32* pSrc;
243 UINT32* pDest;
245 pSrc = pImage->image;
246 pDest = pImage->pDestination;
248 if (pSrc != pDest) /* if not linked to that flash address */
250 if (pImage->flags & IF_UCL_2E)
251 { /* UCL compressed, algorithm 2e */
252 UINT32 dst_len; /* dummy */
253 ucl_nrv2e_decompress_8((UINT8*)pSrc, (UINT8*)pDest, &dst_len);
255 else
256 { /* uncompressed, copy it */
257 UINT32 size = pImage->size;
258 UINT32* pEnd;
259 size = (size + 3) / 4; /* round up to 32bit-words */
260 pEnd = pDest + size;
264 *pDest++ = *pSrc++;
266 while (pDest < pEnd);
270 pImage->pExecute();
273 #ifdef USE_ADC
274 int ReadADC(int channel)
276 /* after channel 3, the ports wrap and get re-used */
277 volatile UINT16* pResult = (UINT16*)(ADDRAH_ADDR + 2 * (channel & 0x03));
278 int timeout = 266; /* conversion takes 266 clock cycles */
280 ADCSR = 0x20 | channel; /* start single conversion */
281 while (((ADCSR & 0x80) == 0) && (--timeout)); /* 6 instructions per round*/
283 return (timeout == 0) ? -1 : *pResult>>6;
285 #endif
288 /* This function is platform-dependent,
289 * until I figure out how to distinguish at runtime. */
290 int ButtonPressed(void) /* return 1,2,3 for F1,F2,F3, 0 if none pressed */
292 #ifdef USE_ADC
293 int value = ReadADC(CHANNEL);
295 if (value >= F1_LOWER && value <= F1_UPPER) /* in range */
296 return 1;
297 else if (value >= F2_LOWER && value <= F2_UPPER) /* in range */
298 return 2;
299 else if (value >= F3_LOWER && value <= F3_UPPER) /* in range */
300 return 3;
301 #else
302 int value = PCDR;
304 if (!(value & F1_MASK))
305 return 1;
306 else if (!(value & F2_MASK))
307 return 2;
308 else if (!(value & F3_MASK))
309 return 3;
310 #endif
312 return 0;
316 /* Determine the image to be started */
317 tImage* GetStartImage(int nPreferred)
319 tImage* pImage1;
320 tImage* pImage2 = NULL; /* default to not present */
321 UINT32 pos;
322 UINT32* pFlash = (UINT32*)FLASH_BASE;
324 /* determine the first image position */
325 pos = pFlash[2] + pFlash[3]; /* position + size of the bootloader
326 * = after it */
327 pos = (pos + 3) & ~3; /* be sure it's 32 bit aligned */
329 pImage1 = (tImage*)pos;
331 if (pImage1->size != 0)
332 { /* check for second image */
333 pos = (UINT32)(&pImage1->image) + pImage1->size;
334 pImage2 = (tImage*)pos;
336 /* does it make sense? (not in FF or 00 erazed space) */
337 if (pImage2->pDestination == (void*)0xFFFFFFFF
338 || pImage2->size == 0xFFFFFFFF
339 || pImage2->pExecute == (void*)0xFFFFFFFF
340 || pImage2->flags == 0xFFFFFFFF
341 || pImage2->pDestination == NULL)
342 /* size, execute and flags can legally be 0 */
344 pImage2 = NULL; /* invalidate */
348 if (pImage2 == NULL || nPreferred == 1)
349 { /* no second image or overridden: return the first */
350 return pImage1;
353 return pImage2; /* return second image */
356 /* diagnostic functions */
358 void SetLed(BOOL bOn)
360 if (bOn)
361 PBDR |= 0x0040;
362 else
363 PBDR &= ~0x0040;
367 void UartInit(void)
369 PBIOR &= 0xFBFF; /* input: RXD1 remote pin */
370 PBCR1 |= 0x00A0; /* set PB11+PB10 to UART */
371 PBCR1 &= 0xFFAF; /* clear bits 6, 4 -> UART */
372 SMR1 = 0x00; /* async format 8N1, baud generator input is CPU clock */
373 SCR1 = 0x30; /* transmit+receive enable */
374 PBCR1 &= 0x00FF; /* set bit 12...15 as GPIO */
375 SSR1 &= 0xBF; /* clear bit 6 (RDRF, receive data register full) */
379 UINT8 UartRead(void)
381 UINT8 byte;
382 while (!(SSR1 & SCI_RDRF)); /* wait for char to be available */
383 byte = RDR1;
384 SSR1 &= ~SCI_RDRF;
385 return byte;
389 void UartWrite(UINT8 byte)
391 while (!(SSR1 & SCI_TDRE)); /* wait for transmit buffer empty */
392 TDR1 = byte;
393 SSR1 &= ~SCI_TDRE;
397 /* include the mini monitor as a rescue feature, started with F3 */
398 void MiniMon(void)
400 UINT8 cmd;
401 UINT32 addr;
402 UINT32 size;
403 UINT32 content;
404 volatile UINT8* paddr = NULL;
405 volatile UINT8* pflash = NULL; /* flash base address */
407 UartInit();
409 while (1)
411 cmd = UartRead();
412 switch (cmd)
414 case BAUDRATE:
415 content = UartRead();
416 UartWrite(cmd); /* acknowledge by returning the command value */
417 while (!(SSR1 & SCI_TEND)); /* wait for empty shift register,
418 * before changing baudrate */
419 BRR1 = content;
420 break;
422 case ADDRESS:
423 addr = (UartRead() << 24) | (UartRead() << 16)
424 | (UartRead() << 8) | UartRead();
425 paddr = (UINT8*)addr;
426 pflash = (UINT8*)(addr & 0xFFF80000); /* round down to 512k align*/
427 UartWrite(cmd); /* acknowledge by returning the command value */
428 break;
430 case BYTE_READ:
431 content = *paddr++;
432 UartWrite(content); /* the content is the ack */
433 break;
435 case BYTE_WRITE:
436 content = UartRead();
437 *paddr++ = content;
438 UartWrite(cmd); /* acknowledge by returning the command value */
439 break;
441 case BYTE_READ16:
442 size = 16;
443 while (size--)
445 content = *paddr++;
446 UartWrite(content); /* the content is the ack */
448 break;
450 case BYTE_WRITE16:
451 size = 16;
452 while (size--)
454 content = UartRead();
455 *paddr++ = content;
457 UartWrite(cmd); /* acknowledge by returning the command value */
458 break;
460 case BYTE_FLASH:
461 content = UartRead();
462 pflash[0x5555] = 0xAA; /* set flash to command mode */
463 pflash[0x2AAA] = 0x55;
464 pflash[0x5555] = 0xA0; /* byte program command */
465 *paddr++ = content;
466 UartWrite(cmd); /* acknowledge by returning the command value */
467 break;
469 case BYTE_FLASH16:
470 size = 16;
471 while (size--)
473 content = UartRead();
474 pflash[0x5555] = 0xAA; /* set flash to command mode */
475 pflash[0x2AAA] = 0x55;
476 pflash[0x5555] = 0xA0; /* byte program command */
477 *paddr++ = content;
479 UartWrite(cmd); /* acknowledge by returning the command value */
480 break;
482 case HALFWORD_READ:
483 content = *(UINT16*)paddr;
484 paddr += 2;
485 UartWrite(content >> 8); /* highbyte */
486 UartWrite(content & 0xFF); /* lowbyte */
487 break;
489 case HALFWORD_WRITE:
490 content = UartRead() << 8 | UartRead();
491 *(UINT16*)paddr = content;
492 paddr += 2;
493 UartWrite(cmd); /* acknowledge by returning the command value */
494 break;
496 case EXECUTE:
498 tpFunc pFunc = (tpFunc)paddr;
499 pFunc();
500 UartWrite(cmd); /* acknowledge by returning the command value*/
502 break;
504 case VERSION:
505 UartWrite(1); /* return our version number */
506 break;
508 default:
510 SetLed(TRUE);
511 UartWrite(~cmd); /* error acknowledge */
514 } /* case */
515 } /* while (1) */