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 ****************************************************************************/
38 #include "backlight.h"
47 /* Maximum allowed firmware image size. The largest known current
48 (December 2006) firmware is about 7.5MB (Apple's firmware for the ipod video)
49 so we set this to 8MB. */
50 #define MAX_LOADSIZE (8*1024*1024)
52 /* A buffer to load the Linux kernel or Rockbox into */
53 unsigned char *loadbuffer
= (unsigned char *)DRAM_START
;
55 /* Bootloader version */
56 char version
[] = APPSVERSION
;
58 #define IPOD_HW_REVISION (*((volatile unsigned long*)(0x00002084)))
60 /* We copy the hardware revision to the last four bytes of SDRAM and then
61 re-read it after we have re-mapped SDRAM to 0x0 in Rockbox */
62 #define TMP_IPOD_HW_REVISION (*((volatile unsigned long*)(0x11fffffc)))
66 #define BUTTON_RIGHT 3
70 #if CONFIG_KEYPAD == IPOD_4G_PAD && !defined(IPOD_MINI)
71 /* check if number of seconds has past */
72 int timer_check(int clock_start
, unsigned int usecs
)
74 if ((USEC_TIMER
- clock_start
) >= usecs
) {
81 static void ser_opto_keypad_cfg(int val
)
85 GPIOB_ENABLE
&=~ 0x80;
87 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
88 outl(val
, 0x7000c120);
89 outl(inl(0x7000c100) | 0x80000000, 0x7000c100);
91 GPIOB_OUTPUT_VAL
&=~ 0x10;
92 GPIOB_OUTPUT_EN
|= 0x10;
94 start_time
= USEC_TIMER
;
96 if ((inl(0x7000c104) & 0x80000000) == 0) {
99 } while (timer_check(start_time
, 1500) != 0);
101 outl(inl(0x7000c100) & ~0x80000000, 0x7000c100);
103 GPIOB_ENABLE
|= 0x80;
104 GPIOB_OUTPUT_VAL
|= 0x10;
105 GPIOB_OUTPUT_EN
&=~0x10;
107 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
108 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
111 int opto_keypad_read(void)
113 int loop_cnt
, had_io
= 0;
115 for (loop_cnt
= 5; loop_cnt
!= 0;)
119 unsigned int key_pad_val
;
121 ser_opto_keypad_cfg(0x8000023a);
123 start_time
= USEC_TIMER
;
125 if (inl(0x7000c104) & 0x4000000) {
133 } while (timer_check(start_time
, 1500) != 0);
135 key_pad_val
= inl(0x7000c140);
136 if ((key_pad_val
& ~0x7fff0000) != 0x8000023a) {
139 key_pad_val
= (key_pad_val
<< 11) >> 27;
143 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
144 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
146 if (key_pressed
!= 0) {
147 return key_pad_val
^ 0x1f;
155 static int key_pressed(void)
159 #if CONFIG_KEYPAD == IPOD_4G_PAD
160 #ifdef IPOD_MINI /* mini 1G only */
161 state
= GPIOA_INPUT_VAL
& 0x3f;
162 if ((state
& 0x10) == 0) return BUTTON_LEFT
;
163 if ((state
& 0x2) == 0) return BUTTON_MENU
;
164 if ((state
& 0x4) == 0) return BUTTON_PLAY
;
165 if ((state
& 0x8) == 0) return BUTTON_RIGHT
;
167 state
= opto_keypad_read();
168 if ((state
& 0x4) == 0) return BUTTON_LEFT
;
169 if ((state
& 0x10) == 0) return BUTTON_MENU
;
170 if ((state
& 0x8) == 0) return BUTTON_PLAY
;
171 if ((state
& 0x2) == 0) return BUTTON_RIGHT
;
173 #elif CONFIG_KEYPAD == IPOD_3G_PAD
174 state
= inb(0xcf000030);
175 if (((state
& 0x20) == 0)) return BUTTON_HOLD
; /* hold on */
176 if ((state
& 0x08) == 0) return BUTTON_LEFT
;
177 if ((state
& 0x10) == 0) return BUTTON_MENU
;
178 if ((state
& 0x04) == 0) return BUTTON_PLAY
;
179 if ((state
& 0x01) == 0) return BUTTON_RIGHT
;
184 /* This function is the same on all ipods */
185 bool button_hold(void)
187 return (GPIOA_INPUT_VAL
& 0x20)?false:true;
190 void fatal_error(void)
193 bool holdstatus
=false;
195 /* System font is 6 pixels wide */
196 #if LCD_WIDTH >= (30*6)
197 printf("Press MENU+SELECT to reboot");
198 printf("then SELECT+PLAY for disk mode");
200 printf("Press MENU+SELECT to");
201 printf("reboot then SELECT+PLAY");
202 printf("for disk mode");
206 if (button_hold() != holdstatus
) {
209 lcd_puts(0, line
, "Hold switch on!");
212 lcd_puts(0, line
, " ");
216 udelay(100000); /* 100ms */
229 bool button_was_held
;
230 struct partinfo
* pinfo
;
231 unsigned short* identify_info
;
233 /* Check the button hold status as soon as possible - to
234 give the user maximum chance to turn it off in order to
235 reset the settings in rockbox. */
236 button_was_held
= button_hold();
238 /* Turn on the backlight */
240 #if CONFIG_BACKLIGHT==BL_IPOD4G
241 /* brightness full */
242 outl(0x80000000 | (0xff << 16), 0x7000a010);
244 /* set port B03 on */
245 outl(((0x100 | 1) << 3), 0x6000d824);
247 #elif CONFIG_BACKLIGHT==BL_IPODMINI
248 /* set port B03 on */
249 outl(((0x100 | 1) << 3), 0x6000d824);
251 #elif CONFIG_BACKLIGHT==BL_IPODNANO
253 /* set port B03 on */
254 outl(((0x100 | 1) << 3), 0x6000d824);
256 /* set port L07 on */
257 GPIOL_OUTPUT_VAL
= ((0x100 | 1) << 7);
258 #elif CONFIG_BACKLIGHT==BL_IPOD3G
259 outl(inl(IPOD_LCD_BASE
) | 0x2, IPOD_LCD_BASE
);
262 TMP_IPOD_HW_REVISION
= IPOD_HW_REVISION
;
263 ipod_hw_rev
= IPOD_HW_REVISION
;
270 #ifdef HAVE_LCD_COLOR
271 lcd_set_foreground(LCD_WHITE
);
272 lcd_set_background(LCD_BLACK
);
277 /* ADC and button drivers are not yet implemented */
284 /* Enable bootloader messages */
285 if (btn
==BUTTON_RIGHT
)
288 lcd_setfont(FONT_SYSFIXED
);
290 printf("Rockbox boot loader");
291 printf("Version: %s", version
);
292 printf("IPOD version: 0x%08x", IPOD_HW_REVISION
);
296 identify_info
=ata_get_identify();
298 for (i
=0; i
< 20; i
++) {
299 ((unsigned short*)buf
)[i
]=htobe16(identify_info
[i
+27]);
302 for (i
=39; i
&& buf
[i
]==' '; i
--) {
307 printf("ATA: %d", i
);
311 rc
= disk_mount_all();
314 printf("No partition found");
318 pinfo
= disk_partinfo(1);
319 printf("Partition 1: 0x%02x %ld MB",
320 pinfo
->type
, pinfo
->size
/ 2048);
323 if (button_was_held
|| (btn
==BUTTON_MENU
)) {
324 /* If either the hold switch was on, or the Menu button was held, then
325 try the Apple firmware */
327 printf("Loading original firmware...");
329 /* First try an apple_os.ipod file on the FAT32 partition
330 (either in .rockbox or the root)
333 rc
=load_firmware(loadbuffer
, "apple_os.ipod", MAX_LOADSIZE
);
336 printf("apple_os.ipod loaded.");
337 return (void*)DRAM_START
;
338 } else if (rc
== EFILE_NOT_FOUND
) {
339 /* If apple_os.ipod doesn't exist, then check if there is an Apple
340 firmware image in RAM */
341 haveretailos
= (memcmp((void*)(DRAM_START
+0x20),"portalplayer",12)==0);
343 /* We have a copy of the retailos in RAM, lets just run it. */
344 return (void*)DRAM_START
;
346 } else if (rc
< EFILE_NOT_FOUND
) {
348 printf("Can't load apple_os.ipod:");
349 printf(strerror(rc
));
352 /* Everything failed - just loop forever */
353 printf("No RetailOS detected");
355 } else if (btn
==BUTTON_PLAY
) {
356 printf("Loading Linux...");
357 rc
=load_raw_firmware(loadbuffer
, "/linux.bin", MAX_LOADSIZE
);
360 printf("Can't load linux.bin:");
361 printf(strerror(rc
));
363 return (void*)DRAM_START
;
366 printf("Loading Rockbox...");
367 rc
=load_firmware(loadbuffer
, BOOTFILE
, MAX_LOADSIZE
);
370 printf("Can't load rockbox.ipod:");
371 printf(strerror(rc
));
373 printf("Rockbox loaded.");
374 return (void*)DRAM_START
;
378 /* If we get to here, then we haven't been able to load any firmware */
381 /* We never get here, but keep gcc happy */
385 /* These functions are present in the firmware library, but we reimplement
386 them here because the originals do a lot more than we want */
387 void usb_acknowledge(void)
391 void usb_wait_for_disconnect(void)