1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 by Dave Chapman
12 * Based on Rockbox iriver bootloader by Linus Nielsen Feltzing
13 * and the ipodlinux bootloader by Daniel Palffy and Bernard Leach
15 * All files in this archive are subject to the GNU General Public License.
16 * See the file COPYING in the source tree root for full license agreement.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
37 #include "backlight.h"
42 #if (CONFIG_CPU == PP5020)
43 #define DRAM_START 0x10000000
45 #define IPOD_LCD_BASE 0xc0001000
46 #define DRAM_START 0x28000000
48 #define IPOD_HW_REVISION (*((volatile unsigned long*)(0x00002084)))
50 /* We copy the hardware revision to the last four bytes of SDRAM and then
51 re-read it after we have re-mapped SDRAM to 0x0 in Rockbox */
52 #define TMP_IPOD_HW_REVISION (*((volatile unsigned long*)(0x11fffffc)))
56 #define BUTTON_RIGHT 3
60 /* Size of the buffer to store the loaded Rockbox/Linux image */
61 #define MAX_LOADSIZE (4*1024*1024)
63 char version
[] = APPSVERSION
;
65 typedef struct _image
{
66 unsigned type
; /* '' */
68 unsigned pad1
; /* 0000 0000 */
69 unsigned devOffset
; /* byte offset of start of image code */
70 unsigned len
; /* length in bytes of image */
71 void *addr
; /* load address */
72 unsigned entryOffset
; /* execution start within image */
73 unsigned chksum
; /* checksum for image */
74 unsigned vers
; /* image version */
75 unsigned loadAddr
; /* load address for image */
78 extern image_t boot_table
[];
82 static void memmove16(void *dest
, const void *src
, unsigned count
)
89 count
= (count
+ 15) >> 4;
90 d
= (struct bufstr
*) dest
;
91 s
= (struct bufstr
*) src
;
95 count
= (count
+ 15) >> 4;
96 d
= (struct bufstr
*)(dest
+ (count
<<4));
97 s
= (struct bufstr
*)(src
+ (count
<<4));
103 #if CONFIG_KEYPAD == IPOD_4G_PAD
104 /* check if number of seconds has past */
105 int timer_check(int clock_start
, unsigned int usecs
)
107 if ((USEC_TIMER
- clock_start
) >= usecs
) {
114 static void ser_opto_keypad_cfg(int val
)
118 outl(inl(0x6000d004) & ~0x80, 0x6000d004);
120 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
121 outl(val
, 0x7000c120);
122 outl(inl(0x7000c100) | 0x80000000, 0x7000c100);
124 outl(inl(0x6000d024) & ~0x10, 0x6000d024);
125 outl(inl(0x6000d014) | 0x10, 0x6000d014);
127 start_time
= USEC_TIMER
;
129 if ((inl(0x7000c104) & 0x80000000) == 0) {
132 } while (timer_check(start_time
, 1500) != 0);
134 outl(inl(0x7000c100) & ~0x80000000, 0x7000c100);
136 outl(inl(0x6000d004) | 0x80, 0x6000d004);
137 outl(inl(0x6000d024) | 0x10, 0x6000d024);
138 outl(inl(0x6000d014) & ~0x10, 0x6000d014);
140 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
141 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
144 int opto_keypad_read(void)
146 int loop_cnt
, had_io
= 0;
148 for (loop_cnt
= 5; loop_cnt
!= 0;)
152 unsigned int key_pad_val
;
154 ser_opto_keypad_cfg(0x8000023a);
156 start_time
= USEC_TIMER
;
158 if (inl(0x7000c104) & 0x4000000) {
166 } while (timer_check(start_time
, 1500) != 0);
168 key_pad_val
= inl(0x7000c140);
169 if ((key_pad_val
& ~0x7fff0000) != 0x8000023a) {
172 key_pad_val
= (key_pad_val
<< 11) >> 27;
176 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
177 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
179 if (key_pressed
!= 0) {
180 return key_pad_val
^ 0x1f;
188 static int key_pressed(void)
192 #if CONFIG_KEYPAD == IPOD_4G_PAD
193 state
= opto_keypad_read();
194 if ((state
& 0x4) == 0) return BUTTON_LEFT
;
195 if ((state
& 0x10) == 0) return BUTTON_MENU
;
196 if ((state
& 0x8) == 0) return BUTTON_PLAY
;
197 if ((state
& 0x2) == 0) return BUTTON_RIGHT
;
198 #elif CONFIG_KEYPAD == IPOD_3G_PAD
199 state
= inb(0xcf000030);
200 if (((state
& 0x20) == 0)) return BUTTON_HOLD
; /* hold on */
201 if ((state
& 0x08) == 0) return BUTTON_LEFT
;
202 if ((state
& 0x10) == 0) return BUTTON_MENU
;
203 if ((state
& 0x04) == 0) return BUTTON_PLAY
;
204 if ((state
& 0x01) == 0) return BUTTON_RIGHT
;
209 int load_rockbox(unsigned char* buf
)
214 unsigned long chksum
;
220 fd
= open("/.rockbox/" BOOTFILE
, O_RDONLY
);
223 fd
= open("/" BOOTFILE
, O_RDONLY
);
228 len
= filesize(fd
) - 8;
230 if (len
> MAX_LOADSIZE
)
233 lseek(fd
, FIRMWARE_OFFSET_FILE_CRC
, SEEK_SET
);
235 rc
= read(fd
, &chksum
, 4);
236 chksum
=betoh32(chksum
); /* Rockbox checksums are big-endian */
240 rc
= read(fd
, model
, 4);
246 snprintf(str
, 80, "Model: %s", model
);
247 lcd_puts(0, line
++, str
);
248 snprintf(str
, 80, "Checksum: %x", chksum
);
249 lcd_puts(0, line
++, str
);
252 lseek(fd
, FIRMWARE_OFFSET_FILE_DATA
, SEEK_SET
);
254 rc
= read(fd
, buf
, len
);
262 for(i
= 0;i
< len
;i
++) {
266 snprintf(str
, 80, "Sum: %x", sum
);
267 lcd_puts(0, line
++, str
);
277 int load_linux(unsigned char* buf
) {
283 fd
=open("/linux.bin",O_RDONLY
);
288 if (len
> MAX_LOADSIZE
)
296 snprintf(str
, 80, "Loaded Linux: %d bytes", len
);
297 lcd_puts(0, line
++, str
);
304 /* A buffer to load the Linux kernel or Rockbox into */
305 unsigned char loadbuffer
[MAX_LOADSIZE
];
313 int padding
= 0x4400;
314 image_t
*tblp
= boot_table
;
316 struct partinfo
* pinfo
;
317 unsigned short* identify_info
;
319 /* Turn on the backlight */
321 #if CONFIG_BACKLIGHT==BL_IPOD4G
322 /* brightness full */
323 outl(0x80000000 | (0xff << 16), 0x7000a010);
325 /* set port B03 on */
326 outl(((0x100 | 1) << 3), 0x6000d824);
328 #elif CONFIG_BACKLIGHT==BL_IPODMINI
329 /* set port B03 on */
330 outl(((0x100 | 1) << 3), 0x6000d824);
332 #elif CONFIG_BACKLIGHT==BL_IPODNANO
334 /* set port B03 on */
335 outl(((0x100 | 1) << 3), 0x6000d824);
337 /* set port L07 on */
338 outl(((0x100 | 1) << 7), 0x6000d12c);
339 #elif CONFIG_BACKLIGHT==BL_IPOD3G
340 outl(inl(IPOD_LCD_BASE
) | 0x2, IPOD_LCD_BASE
);
343 TMP_IPOD_HW_REVISION
= IPOD_HW_REVISION
;
344 ipod_hw_rev
= IPOD_HW_REVISION
;
352 /* ADC and button drivers are not yet implemented */
359 lcd_setfont(FONT_SYSFIXED
);
361 lcd_puts(0, line
++, "Rockbox boot loader");
362 snprintf(buf
, sizeof(buf
), "Version: 20%s", version
);
363 lcd_puts(0, line
++, buf
);
364 snprintf(buf
, sizeof(buf
), "IPOD version: 0x%08x", IPOD_HW_REVISION
);
365 lcd_puts(0, line
++, buf
);
370 identify_info
=ata_get_identify();
372 for (i
=0; i
< 20; i
++) {
373 ((unsigned short*)buf
)[i
]=htobe16(identify_info
[i
+27]);
376 for (i
=39; i
&& buf
[i
]==' '; i
--) {
379 lcd_puts(0, line
++, buf
);
382 snprintf(buf
, sizeof(buf
), "ATA: %d", i
);
383 lcd_puts(0, line
++, buf
);
388 rc
= disk_mount_all();
391 lcd_puts(0, line
++, "No partition found");
393 // while(button_get(true) != SYS_USB_CONNECTED) {};
396 pinfo
= disk_partinfo(1);
397 snprintf(buf
, sizeof(buf
), "Partition 1: 0x%02x %ld MB",
398 pinfo
->type
, pinfo
->size
/ 2048);
399 lcd_puts(0, line
++, buf
);
402 /* Check for a keypress */
405 if ((i
!=BUTTON_MENU
) && (i
!=BUTTON_PLAY
)) {
406 lcd_puts(0, line
, "Loading Rockbox...");
408 rc
=load_rockbox(loadbuffer
);
410 snprintf(buf
, sizeof(buf
), "Rockbox error: %d",rc
);
411 lcd_puts(0, line
++, buf
);
414 lcd_puts(0, line
++, "Rockbox loaded.");
416 memcpy((void*)DRAM_START
,loadbuffer
,rc
);
418 /* Transfer execution directly to Rockbox - we don't want
419 to run the rest of the bootloader startup code. */
421 "mov r0, #0x10000000 \n"
425 /* We don't get here, but keep the compiler happy. */
430 if (i
==BUTTON_PLAY
) {
431 lcd_puts(0, line
, "Loading Linux...");
433 rc
=load_linux(loadbuffer
);
435 snprintf(buf
, sizeof(buf
), "Linux error: %d",rc
);
436 lcd_puts(0, line
++, buf
);
439 memcpy((void*)DRAM_START
,loadbuffer
,rc
);
440 return (void*)DRAM_START
;
444 /* If everything else failed, try the original firmware */
445 lcd_puts(0, line
, "Loading original firmware...");
448 /* Pause for 5 seconds so we can see what's happened */
451 entry
= tblp
->addr
+ tblp
->entryOffset
;
452 if (imageno
|| ((int)tblp
->addr
& 0xffffff) != 0) {
453 memmove16(tblp
->addr
, tblp
->addr
+ tblp
->devOffset
- padding
,
457 /* Return the start address in loaded image */
461 /* These functions are present in the firmware library, but we reimplement
462 them here because the originals do a lot more than we want */
464 void reset_poweroff_timer(void)
477 void usb_acknowledge(void)
481 void usb_wait_for_disconnect(void)
485 void sys_poweroff(void)