Reserve more bytes to display unicode in the recording trigger settings menu. Fixes...
[Rockbox.git] / bootloader / ipod.c
blob680331cd3ebe42f1f3a2eabf976e5234bcb26d25
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include "cpu.h"
29 #include "system.h"
30 #include "lcd.h"
31 #include "kernel.h"
32 #include "thread.h"
33 #include "ata.h"
34 #include "fat.h"
35 #include "disk.h"
36 #include "font.h"
37 #include "adc.h"
38 #include "backlight-target.h"
39 #include "panic.h"
40 #include "power.h"
41 #include "file.h"
42 #include "common.h"
44 #define XSC(X) #X
45 #define SC(X) XSC(X)
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)))
64 #define BUTTON_LEFT 1
65 #define BUTTON_MENU 2
66 #define BUTTON_RIGHT 3
67 #define BUTTON_PLAY 4
68 #define BUTTON_HOLD 5
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) {
75 return 1;
76 } else {
77 return 0;
81 static void ser_opto_keypad_cfg(int val)
83 int start_time;
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;
95 do {
96 if ((inl(0x7000c104) & 0x80000000) == 0) {
97 break;
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;)
117 int key_pressed = 0;
118 int start_time;
119 unsigned int key_pad_val;
121 ser_opto_keypad_cfg(0x8000023a);
123 start_time = USEC_TIMER;
124 do {
125 if (inl(0x7000c104) & 0x4000000) {
126 had_io = 1;
127 break;
130 if (had_io != 0) {
131 break;
133 } while (timer_check(start_time, 1500) != 0);
135 key_pad_val = inl(0x7000c140);
136 if ((key_pad_val & ~0x7fff0000) != 0x8000023a) {
137 loop_cnt--;
138 } else {
139 key_pad_val = (key_pad_val << 11) >> 27;
140 key_pressed = 1;
143 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
144 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
146 if (key_pressed != 0) {
147 return key_pad_val ^ 0x1f;
151 return 0;
153 #endif
155 static int key_pressed(void)
157 unsigned char state;
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;
166 #else
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;
172 #endif
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;
180 #endif
181 return 0;
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)
192 extern int line;
193 bool holdstatus=false;
195 /* System font is 6 pixels wide */
196 #if LCD_WIDTH >= (30*6)
197 printf("Hold MENU+SELECT to reboot");
198 printf("then SELECT+PLAY for disk mode");
199 #else
200 printf("Hold MENU+SELECT to");
201 printf("reboot then SELECT+PLAY");
202 printf("for disk mode");
203 #endif
204 lcd_update();
206 while (1) {
207 if (button_hold() != holdstatus) {
208 if (button_hold()) {
209 holdstatus=true;
210 lcd_puts(0, line, "Hold switch on!");
211 } else {
212 holdstatus=false;
213 lcd_puts(0, line, " ");
215 lcd_update();
217 udelay(100000); /* 100ms */
223 void* main(void)
225 char buf[256];
226 int i;
227 int btn;
228 int rc;
229 bool haveretailos;
230 bool button_was_held;
231 struct partinfo* pinfo;
232 unsigned short* identify_info;
234 /* Check the button hold status as soon as possible - to
235 give the user maximum chance to turn it off in order to
236 reset the settings in rockbox. */
237 button_was_held = button_hold();
239 /* Turn on the backlight */
241 __backlight_on();
243 TMP_IPOD_HW_REVISION = IPOD_HW_REVISION;
244 ipod_hw_rev = IPOD_HW_REVISION;
246 system_init();
247 kernel_init();
248 lcd_init();
249 font_init();
251 #ifdef HAVE_LCD_COLOR
252 lcd_set_foreground(LCD_WHITE);
253 lcd_set_background(LCD_BLACK);
254 lcd_clear_display();
255 #endif
257 #if 0
258 /* ADC and button drivers are not yet implemented */
259 adc_init();
260 button_init();
261 #endif
263 btn=key_pressed();
265 /* Enable bootloader messages */
266 if (btn==BUTTON_RIGHT)
267 verbose = true;
269 lcd_setfont(FONT_SYSFIXED);
271 printf("Rockbox boot loader");
272 printf("Version: %s", version);
273 printf("IPOD version: 0x%08x", IPOD_HW_REVISION);
275 i=ata_init();
276 if (i==0) {
277 identify_info=ata_get_identify();
278 /* Show model */
279 for (i=0; i < 20; i++) {
280 ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
282 buf[40]=0;
283 for (i=39; i && buf[i]==' '; i--) {
284 buf[i]=0;
286 printf(buf);
287 } else {
288 printf("ATA: %d", i);
291 disk_init();
292 rc = disk_mount_all();
293 if (rc<=0)
295 printf("No partition found");
296 fatal_error();
299 pinfo = disk_partinfo(1);
300 printf("Partition 1: 0x%02x %ld MB",
301 pinfo->type, pinfo->size / 2048);
304 if (button_was_held || (btn==BUTTON_MENU)) {
305 /* If either the hold switch was on, or the Menu button was held, then
306 try the Apple firmware */
308 printf("Loading original firmware...");
310 /* First try an apple_os.ipod file on the FAT32 partition
311 (either in .rockbox or the root)
314 rc=load_firmware(loadbuffer, "apple_os.ipod", MAX_LOADSIZE);
316 if (rc == EOK) {
317 printf("apple_os.ipod loaded.");
318 return (void*)DRAM_START;
319 } else if (rc == EFILE_NOT_FOUND) {
320 /* If apple_os.ipod doesn't exist, then check if there is an Apple
321 firmware image in RAM */
322 haveretailos = (memcmp((void*)(DRAM_START+0x20),"portalplayer",12)==0);
323 if (haveretailos) {
324 /* We have a copy of the retailos in RAM, lets just run it. */
325 return (void*)DRAM_START;
327 } else if (rc < EFILE_NOT_FOUND) {
328 printf("Error!");
329 printf("Can't load apple_os.ipod:");
330 printf(strerror(rc));
333 /* Everything failed - just loop forever */
334 printf("No RetailOS detected");
336 } else if (btn==BUTTON_PLAY) {
337 printf("Loading Linux...");
338 rc=load_raw_firmware(loadbuffer, "/linux.bin", MAX_LOADSIZE);
339 if (rc < EOK) {
340 printf("Error!");
341 printf("Can't load linux.bin:");
342 printf(strerror(rc));
343 } else {
344 return (void*)DRAM_START;
346 } else {
347 printf("Loading Rockbox...");
348 rc=load_firmware(loadbuffer, BOOTFILE, MAX_LOADSIZE);
349 if (rc < EOK) {
350 printf("Error!");
351 printf("Can't load rockbox.ipod:");
352 printf(strerror(rc));
353 } else {
354 printf("Rockbox loaded.");
355 return (void*)DRAM_START;
359 /* If we get to here, then we haven't been able to load any firmware */
360 fatal_error();
362 /* We never get here, but keep gcc happy */
363 return (void*)0;
366 /* These functions are present in the firmware library, but we reimplement
367 them here because the originals do a lot more than we want */
368 void usb_acknowledge(void)
372 void usb_wait_for_disconnect(void)