Initialuse user font table for checkwps. This is normally done from the settings...
[kugel-rb.git] / bootloader / iriver_h1x0.c
blob83e545540b1eeb6f9252069e6b8ebc7cf71111ed
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include "inttypes.h"
26 #include "string.h"
27 #include "cpu.h"
28 #include "system.h"
29 #include "lcd.h"
30 #include "lcd-remote.h"
31 #include "scroll_engine.h"
32 #include "kernel.h"
33 #include "thread.h"
34 #include "storage.h"
35 #include "usb.h"
36 #include "disk.h"
37 #include "font.h"
38 #include "adc.h"
39 #include "backlight.h"
40 #include "backlight-target.h"
41 #include "button.h"
42 #include "panic.h"
43 #include "power.h"
44 #include "powermgmt.h"
45 #include "file.h"
46 #include "eeprom_settings.h"
47 #include "rbunicode.h"
48 #include "common.h"
50 #include <stdarg.h>
52 /* Maximum allowed firmware image size. 10MB is more than enough */
53 #define MAX_LOADSIZE (10*1024*1024)
55 #define DRAM_START 0x31000000
57 #ifdef HAVE_EEPROM_SETTINGS
58 static bool recovery_mode = false;
59 #endif
61 char version[] = APPSVERSION;
63 /* Reset the cookie for the crt0 crash check */
64 inline void __reset_cookie(void)
66 asm(" move.l #0,%d0");
67 asm(" move.l %d0,0x10017ffc");
70 void start_iriver_fw(void)
72 asm(" move.w #0x2700,%sr");
73 __reset_cookie();
74 asm(" movec.l %d0,%vbr");
75 asm(" move.l 0,%sp");
76 asm(" lea.l 8,%a0");
77 asm(" jmp (%a0)");
80 void start_firmware(void)
82 asm(" move.w #0x2700,%sr");
83 __reset_cookie();
84 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
85 asm(" movec.l %d0,%vbr");
86 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
87 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
88 asm(" jmp (%a0)");
91 #ifdef IRIVER_H100_SERIES
92 void start_flashed_romimage(void)
94 uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY;
95 int *reset_vector;
97 if (!detect_flashed_romimage())
98 return ;
100 reset_vector = (int *)(&src[sizeof(struct flash_header)+4]);
102 asm(" move.w #0x2700,%sr");
103 __reset_cookie();
105 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
106 asm(" movec.l %d0,%vbr");
107 asm(" move.l %0,%%sp" :: "m"(reset_vector[0]));
108 asm(" move.l %0,%%a0" :: "m"(reset_vector[1]));
109 asm(" jmp (%a0)");
111 /* Failure */
112 power_off();
115 void start_flashed_ramimage(void)
117 struct flash_header hdr;
118 unsigned char *buf = (unsigned char *)DRAM_START;
119 uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY;
121 if (!detect_flashed_ramimage())
122 return;
124 /* Load firmware from flash */
125 cpu_boost(true);
126 memcpy(&hdr, src, sizeof(struct flash_header));
127 src += sizeof(struct flash_header);
128 memcpy(buf, src, hdr.length);
129 cpu_boost(false);
131 start_firmware();
133 /* Failure */
134 power_off();
136 #endif /* IRIVER_H100_SERIES */
138 void shutdown(void)
140 printf("Shutting down...");
141 #ifdef HAVE_EEPROM_SETTINGS
142 /* Reset the rockbox crash check. */
143 firmware_settings.bl_version = 0;
144 eeprom_settings_store();
145 #endif
147 /* We need to gracefully spin down the disk to prevent clicks. */
148 if (ide_powered())
150 /* Make sure ATA has been initialized. */
151 storage_init();
153 /* And put the disk into sleep immediately. */
154 storage_sleepnow();
157 sleep(HZ*2);
159 /* Backlight OFF */
160 _backlight_off();
161 #ifdef HAVE_REMOTE_LCD
162 _remote_backlight_off();
163 #endif
165 __reset_cookie();
166 power_off();
169 /* Print the battery voltage (and a warning message). */
170 void check_battery(void)
172 int battery_voltage, batt_int, batt_frac;
174 battery_voltage = battery_adc_voltage();
175 batt_int = battery_voltage / 1000;
176 batt_frac = (battery_voltage % 1000) / 10;
178 printf("Batt: %d.%02dV", batt_int, batt_frac);
180 if (battery_voltage <= 310)
182 printf("WARNING! BATTERY LOW!!");
183 sleep(HZ*2);
187 #ifdef HAVE_EEPROM_SETTINGS
188 void initialize_eeprom(void)
190 if (detect_original_firmware())
191 return ;
193 if (!eeprom_settings_init())
195 recovery_mode = true;
196 return ;
199 /* If bootloader version has not been reset, disk might
200 * not be intact. */
201 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
203 firmware_settings.disk_clean = false;
204 recovery_mode = true;
207 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
208 eeprom_settings_store();
211 void try_flashboot(void)
213 if (!firmware_settings.initialized)
214 return ;
216 switch (firmware_settings.bootmethod)
218 case BOOT_DISK:
219 return;
221 case BOOT_ROM:
222 start_flashed_romimage();
223 recovery_mode = true;
224 break;
226 case BOOT_RAM:
227 start_flashed_ramimage();
228 recovery_mode = true;
229 break;
231 default:
232 recovery_mode = true;
233 return;
237 static const char *options[] = {
238 "Boot from disk",
239 "Boot RAM image",
240 "Boot ROM image",
241 "Shutdown"
244 #define FAILSAFE_OPTIONS 4
245 #define TIMEOUT (15*HZ)
246 void failsafe_menu(void)
248 long start_tick = current_tick;
249 int option = 3;
250 int button;
251 int defopt = -1;
252 char buf[32];
253 int i;
254 extern int line;
256 reset_screen();
257 printf("Bootloader %s", version);
258 check_battery();
259 printf("=========================");
260 line += FAILSAFE_OPTIONS;
261 printf("");
262 printf(" [NAVI] to confirm.");
263 printf(" [REC] to set as default.");
264 printf("");
266 if (firmware_settings.initialized)
268 defopt = firmware_settings.bootmethod;
269 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
270 defopt = option;
273 while (current_tick - start_tick < TIMEOUT)
275 /* Draw the menu. */
276 line = 3;
277 for (i = 0; i < FAILSAFE_OPTIONS; i++)
279 char *def = "[DEF]";
280 char *arrow = "->";
282 if (i != defopt)
283 def = "";
284 if (i != option)
285 arrow = " ";
287 printf("%s %s %s", arrow, options[i], def);
290 snprintf(buf, sizeof(buf), "Time left: %ds",
291 (TIMEOUT - (current_tick - start_tick)) / HZ);
292 lcd_puts(0, 10, buf);
293 lcd_update();
294 button = button_get_w_tmo(HZ);
296 if (button == BUTTON_NONE || button & SYS_EVENT)
297 continue ;
299 start_tick = current_tick;
301 /* Ignore the ON/PLAY -button because it can cause trouble
302 with the RTC alarm mod. */
303 switch (button & ~(BUTTON_ON))
305 case BUTTON_UP:
306 case BUTTON_RC_REW:
307 if (option > 0)
308 option--;
309 break ;
311 case BUTTON_DOWN:
312 case BUTTON_RC_FF:
313 if (option < FAILSAFE_OPTIONS-1)
314 option++;
315 break ;
317 case BUTTON_SELECT:
318 case BUTTON_RC_ON:
319 goto execute;
321 case BUTTON_REC:
322 case BUTTON_RC_REC:
323 if (firmware_settings.initialized)
325 firmware_settings.bootmethod = option;
326 eeprom_settings_store();
327 defopt = option;
329 break ;
333 execute:
335 lcd_puts(0, 10, "Executing command...");
336 lcd_update();
337 sleep(HZ);
338 reset_screen();
340 switch (option)
342 case BOOT_DISK:
343 return ;
345 case BOOT_RAM:
346 start_flashed_ramimage();
347 printf("Image not found");
348 break;
350 case BOOT_ROM:
351 start_flashed_romimage();
352 printf("Image not found");
353 break;
356 shutdown();
358 #endif
360 /* get rid of a nasty humming sound during boot
361 -> RESET signal */
362 inline static void __uda1380_reset_hi(void)
364 #ifdef HAVE_UDA1380
365 or_l(1<<29, &GPIO_OUT);
366 or_l(1<<29, &GPIO_ENABLE);
367 or_l(1<<29, &GPIO_FUNCTION);
368 #endif
371 inline static void __uda1380_reset_lo(void)
373 #ifdef HAVE_UDA1380
374 and_l(~(1<<29), &GPIO_OUT);
375 #endif
378 void main(void)
380 int i;
381 int rc;
382 bool rc_on_button = false;
383 bool on_button = false;
384 bool rec_button = false;
385 bool hold_status = false;
386 int data;
387 extern int line; /* From common.c */
388 extern int remote_line; /* From common.c */
390 /* We want to read the buttons as early as possible, before the user
391 releases the ON button */
393 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
394 (The ON and Hold buttons on the main unit and the remote) */
395 or_l(0x00100062, &GPIO1_FUNCTION);
396 and_l(~0x00100062, &GPIO1_ENABLE);
398 data = GPIO1_READ;
399 if ((data & 0x20) == 0)
400 on_button = true;
402 if ((data & 0x40) == 0)
403 rc_on_button = true;
405 /* Set the default state of the hard drive power to OFF */
406 ide_power_enable(false);
408 power_init();
410 /* Turn off if neither ON button is pressed */
411 if (!(on_button || rc_on_button))
413 __reset_cookie();
414 power_off();
417 __uda1380_reset_hi();
419 /* Start with the main backlight OFF. */
420 _backlight_init();
421 _backlight_off();
423 /* Remote backlight ON */
424 #ifdef HAVE_REMOTE_LCD
425 _remote_backlight_on();
426 #endif
428 system_init();
429 kernel_init();
431 __uda1380_reset_lo();
433 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
434 /* Set up waitstates for the peripherals */
435 set_cpu_frequency(0); /* PLL off */
436 #ifdef CPU_COLDFIRE
437 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
438 #endif
439 #endif
440 enable_irq();
442 #ifdef HAVE_EEPROM_SETTINGS
443 initialize_eeprom();
444 #endif
446 usb_init();
447 /* A small delay after usb_init is necessary to read the I/O port correctly
448 (if ports are read _immediately_ after the init). */
449 /* sleep(1); */
451 adc_init();
452 button_init();
454 /* Only check remote hold status if remote power button was actually used. */
455 if (rc_on_button)
457 lcd_remote_init();
459 /* Allow the button driver to check the buttons */
460 sleep(HZ/50);
462 if (remote_button_hold())
463 hold_status = true;
466 /* Check main hold switch status too. */
467 if (on_button && button_hold())
469 hold_status = true;
472 /* Power on the hard drive early, to speed up the loading. */
473 if (!hold_status
474 # ifdef HAVE_EEPROM_SETTINGS
475 && !recovery_mode
476 # endif
479 ide_power_enable(true);
482 # ifdef HAVE_EEPROM_SETTINGS
483 if (!hold_status && (usb_detect() != USB_INSERTED) && !recovery_mode)
484 try_flashboot();
485 # endif
487 backlight_init();
490 lcd_init();
492 if (!rc_on_button)
493 lcd_remote_init();
495 /* Bootloader uses simplified backlight thread, so we need to enable
496 remote display here. */
497 if (remote_detect())
498 lcd_remote_on();
500 font_init();
502 lcd_setfont(FONT_SYSFIXED);
504 printf("Rockbox boot loader");
505 printf("Version %s", version);
507 /* No need to wait here more because lcd_init and others already do that. */
508 // sleep(HZ/50); /* Allow the button driver to check the buttons */
509 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
510 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
512 check_battery();
514 /* Don't start if the Hold button is active on the device you
515 are starting with */
516 if ((usb_detect() != USB_INSERTED) && (hold_status
517 #ifdef HAVE_EEPROM_SETTINGS
518 || recovery_mode
519 #endif
522 if (detect_original_firmware())
524 printf("Hold switch on");
525 shutdown();
528 #ifdef HAVE_EEPROM_SETTINGS
529 failsafe_menu();
530 #endif
533 /* Holding REC while starting runs the original firmware */
534 if (detect_original_firmware() && rec_button)
536 printf("Starting original firmware...");
537 start_iriver_fw();
540 /* A hack to enter USB mode without using the USB thread */
541 if(usb_detect() == USB_INSERTED)
543 const char msg[] = "Bootloader USB mode";
544 int w, h;
545 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
546 reset_screen();
547 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
548 lcd_update();
550 #ifdef HAVE_REMOTE_LCD
551 lcd_remote_puts(0, 3, msg);
552 lcd_remote_update();
553 #endif
555 #ifdef HAVE_EEPROM_SETTINGS
556 if (firmware_settings.initialized)
558 firmware_settings.disk_clean = false;
559 eeprom_settings_store();
561 #endif
562 ide_power_enable(true);
563 storage_enable(false);
564 sleep(HZ/20);
565 usb_enable(true);
566 cpu_idle_mode(true);
567 while (usb_detect() == USB_INSERTED)
569 /* Print the battery status. */
570 line = 0;
571 remote_line = 0;
572 check_battery();
574 storage_spin(); /* Prevent the drive from spinning down */
575 sleep(HZ);
577 /* Backlight OFF */
578 _backlight_off();
581 cpu_idle_mode(false);
582 usb_enable(false);
584 reset_screen();
585 lcd_update();
588 rc = storage_init();
589 if(rc)
591 reset_screen();
592 printf("ATA error: %d", rc);
593 printf("Insert USB cable and press");
594 printf("a button");
595 while(!(button_get(true) & BUTTON_REL));
599 disk_init();
601 rc = disk_mount_all();
602 if (rc<=0)
604 reset_screen();
605 printf("No partition found");
606 while(button_get(true) != SYS_USB_CONNECTED) {};
609 printf("Loading firmware");
610 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
611 if(i < 0)
612 printf("Error: %s", strerror(i));
614 if (i == EOK)
615 start_firmware();
617 if (!detect_original_firmware())
619 printf("No firmware found on disk");
620 sleep(HZ*2);
621 shutdown();
623 else {
624 sleep(HZ*2);
625 start_iriver_fw();
629 /* These functions are present in the firmware library, but we reimplement
630 them here because the originals do a lot more than we want */
631 void screen_dump(void)
635 int usb_screen(void)
637 return 0;
640 unsigned short *bidi_l2v(const unsigned char *str, int orientation)
642 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
643 unsigned short *target;
644 (void)orientation;
646 target = utf16_buf;
648 while (*str)
649 str = utf8decode(str, target++);
650 *target = 0;
651 return utf16_buf;