Correct typo in comments
[Rockbox.git] / bootloader / ipod.c
blob2728529dce640eec96cdc3efb76268c58ca8b738
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 "cpu.h"
28 #include "system.h"
29 #include "lcd.h"
30 #include "kernel.h"
31 #include "thread.h"
32 #include "ata.h"
33 #include "fat.h"
34 #include "disk.h"
35 #include "font.h"
36 #include "adc.h"
37 #include "backlight.h"
38 #include "panic.h"
39 #include "power.h"
40 #include "file.h"
42 #if (CONFIG_CPU == PP5020)
43 #define DRAM_START 0x10000000
44 #else
45 #define IPOD_LCD_BASE 0xc0001000
46 #define DRAM_START 0x28000000
47 #endif
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)))
54 #define BUTTON_LEFT 1
55 #define BUTTON_MENU 2
56 #define BUTTON_RIGHT 3
57 #define BUTTON_PLAY 4
58 #define BUTTON_HOLD 5
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; /* '' */
67 unsigned id; /* */
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 */
76 } image_t;
78 extern image_t boot_table[];
80 int line=0;
82 static void memmove16(void *dest, const void *src, unsigned count)
84 struct bufstr {
85 unsigned _buf[4];
86 } *d, *s;
88 if (src >= dest) {
89 count = (count + 15) >> 4;
90 d = (struct bufstr *) dest;
91 s = (struct bufstr *) src;
92 while (count--)
93 *d++ = *s++;
94 } else {
95 count = (count + 15) >> 4;
96 d = (struct bufstr *)(dest + (count <<4));
97 s = (struct bufstr *)(src + (count <<4));
98 while (count--)
99 *--d = *--s;
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) {
108 return 1;
109 } else {
110 return 0;
114 static void ser_opto_keypad_cfg(int val)
116 int start_time;
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;
128 do {
129 if ((inl(0x7000c104) & 0x80000000) == 0) {
130 break;
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;)
150 int key_pressed = 0;
151 int start_time;
152 unsigned int key_pad_val;
154 ser_opto_keypad_cfg(0x8000023a);
156 start_time = USEC_TIMER;
157 do {
158 if (inl(0x7000c104) & 0x4000000) {
159 had_io = 1;
160 break;
163 if (had_io != 0) {
164 break;
166 } while (timer_check(start_time, 1500) != 0);
168 key_pad_val = inl(0x7000c140);
169 if ((key_pad_val & ~0x7fff0000) != 0x8000023a) {
170 loop_cnt--;
171 } else {
172 key_pad_val = (key_pad_val << 11) >> 27;
173 key_pressed = 1;
176 outl(inl(0x7000c100) | 0x60000000, 0x7000c100);
177 outl(inl(0x7000c104) | 0xc000000, 0x7000c104);
179 if (key_pressed != 0) {
180 return key_pad_val ^ 0x1f;
184 return 0;
186 #endif
188 static int key_pressed(void)
190 unsigned char state;
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;
205 #endif
206 return 0;
209 int load_rockbox(unsigned char* buf)
211 int fd;
212 int rc;
213 int len;
214 unsigned long chksum;
215 char model[5];
216 unsigned long sum;
217 int i;
218 char str[80];
220 fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
221 if(fd < 0)
223 fd = open("/" BOOTFILE, O_RDONLY);
224 if(fd < 0)
225 return -1;
228 len = filesize(fd) - 8;
230 if (len > MAX_LOADSIZE)
231 return -6;
233 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
235 rc = read(fd, &chksum, 4);
236 chksum=betoh32(chksum); /* Rockbox checksums are big-endian */
237 if(rc < 4)
238 return -2;
240 rc = read(fd, model, 4);
241 if(rc < 4)
242 return -3;
244 model[4] = 0;
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);
250 lcd_update();
252 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
254 rc = read(fd, buf, len);
255 if(rc < len)
256 return -4;
258 close(fd);
260 sum = MODEL_NUMBER;
262 for(i = 0;i < len;i++) {
263 sum += buf[i];
266 snprintf(str, 80, "Sum: %x", sum);
267 lcd_puts(0, line++, str);
268 lcd_update();
270 if(sum != chksum)
271 return -5;
273 return len;
277 int load_linux(unsigned char* buf) {
278 int fd;
279 int rc;
280 int len;
281 char str[80];
283 fd=open("/linux.bin",O_RDONLY);
284 if (fd < 0)
285 return -1;
287 len=filesize(fd);
288 if (len > MAX_LOADSIZE)
289 return -6;
291 rc=read(fd,buf,len);
293 if (rc < len)
294 return -4;
296 snprintf(str, 80, "Loaded Linux: %d bytes", len);
297 lcd_puts(0, line++, str);
298 lcd_update();
300 return len;
304 /* A buffer to load the Linux kernel or Rockbox into */
305 unsigned char loadbuffer[MAX_LOADSIZE];
307 void* main(void)
309 char buf[256];
310 int imageno=0;
311 int i;
312 int rc;
313 int padding = 0x4400;
314 image_t *tblp = boot_table;
315 void* entry;
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);
341 #endif
343 TMP_IPOD_HW_REVISION = IPOD_HW_REVISION;
344 ipod_hw_rev = IPOD_HW_REVISION;
346 system_init();
347 kernel_init();
348 lcd_init();
349 font_init();
351 #if 0
352 /* ADC and button drivers are not yet implemented */
353 adc_init();
354 button_init();
355 #endif
357 line=0;
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);
366 lcd_update();
368 i=ata_init();
369 if (i==0) {
370 identify_info=ata_get_identify();
371 /* Show model */
372 for (i=0; i < 20; i++) {
373 ((unsigned short*)buf)[i]=htobe16(identify_info[i+27]);
375 buf[40]=0;
376 for (i=39; i && buf[i]==' '; i--) {
377 buf[i]=0;
379 lcd_puts(0, line++, buf);
380 lcd_update();
381 } else {
382 snprintf(buf, sizeof(buf), "ATA: %d", i);
383 lcd_puts(0, line++, buf);
384 lcd_update();
387 disk_init();
388 rc = disk_mount_all();
389 if (rc<=0)
391 lcd_puts(0, line++, "No partition found");
392 lcd_update();
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);
400 lcd_update();
402 /* Check for a keypress */
403 i=key_pressed();
405 if ((i!=BUTTON_MENU) && (i!=BUTTON_PLAY)) {
406 lcd_puts(0, line, "Loading Rockbox...");
407 lcd_update();
408 rc=load_rockbox(loadbuffer);
409 if (rc < 0) {
410 snprintf(buf, sizeof(buf), "Rockbox error: %d",rc);
411 lcd_puts(0, line++, buf);
412 lcd_update();
413 } else {
414 lcd_puts(0, line++, "Rockbox loaded.");
415 lcd_update();
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. */
420 asm volatile(
421 "mov r0, #0x10000000 \n"
422 "mov pc, r0 \n"
425 /* We don't get here, but keep the compiler happy. */
426 return (void*)0;
430 if (i==BUTTON_PLAY) {
431 lcd_puts(0, line, "Loading Linux...");
432 lcd_update();
433 rc=load_linux(loadbuffer);
434 if (rc < 0) {
435 snprintf(buf, sizeof(buf), "Linux error: %d",rc);
436 lcd_puts(0, line++, buf);
437 lcd_update();
438 } else {
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...");
446 lcd_update();
448 /* Pause for 5 seconds so we can see what's happened */
449 // udelay(5000000);
451 entry = tblp->addr + tblp->entryOffset;
452 if (imageno || ((int)tblp->addr & 0xffffff) != 0) {
453 memmove16(tblp->addr, tblp->addr + tblp->devOffset - padding,
454 tblp->len);
457 /* Return the start address in loaded image */
458 return entry;
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)
468 int dbg_ports(void)
470 return 0;
473 void mpeg_stop(void)
477 void usb_acknowledge(void)
481 void usb_wait_for_disconnect(void)
485 void sys_poweroff(void)