Add description for the shortcuts plugin (FS#8829 by Alexander Levin).
[Rockbox.git] / bootloader / iriver_h1x0.c
blob4be92eb4ac8fb65a7441956dc61495ba02392214
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "inttypes.h"
24 #include "string.h"
25 #include "cpu.h"
26 #include "system.h"
27 #include "lcd.h"
28 #include "lcd-remote.h"
29 #include "scroll_engine.h"
30 #include "kernel.h"
31 #include "thread.h"
32 #include "ata.h"
33 #include "usb.h"
34 #include "disk.h"
35 #include "font.h"
36 #include "adc.h"
37 #include "backlight.h"
38 #include "backlight-target.h"
39 #include "button.h"
40 #include "panic.h"
41 #include "power.h"
42 #include "powermgmt.h"
43 #include "file.h"
44 #include "eeprom_settings.h"
45 #include "rbunicode.h"
46 #include "common.h"
48 #include <stdarg.h>
50 /* Maximum allowed firmware image size. 10MB is more than enough */
51 #define MAX_LOADSIZE (10*1024*1024)
53 #define DRAM_START 0x31000000
55 #ifdef HAVE_EEPROM_SETTINGS
56 static bool recovery_mode = false;
57 #endif
59 char version[] = APPSVERSION;
61 /* Reset the cookie for the crt0 crash check */
62 inline void __reset_cookie(void)
64 asm(" move.l #0,%d0");
65 asm(" move.l %d0,0x10017ffc");
68 void start_iriver_fw(void)
70 asm(" move.w #0x2700,%sr");
71 __reset_cookie();
72 asm(" movec.l %d0,%vbr");
73 asm(" move.l 0,%sp");
74 asm(" lea.l 8,%a0");
75 asm(" jmp (%a0)");
78 void start_firmware(void)
80 asm(" move.w #0x2700,%sr");
81 __reset_cookie();
82 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
83 asm(" movec.l %d0,%vbr");
84 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
85 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
86 asm(" jmp (%a0)");
89 #ifdef IRIVER_H100_SERIES
90 void start_flashed_romimage(void)
92 uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY;
93 int *reset_vector;
95 if (!detect_flashed_romimage())
96 return ;
98 reset_vector = (int *)(&src[sizeof(struct flash_header)+4]);
100 asm(" move.w #0x2700,%sr");
101 __reset_cookie();
103 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
104 asm(" movec.l %d0,%vbr");
105 asm(" move.l %0,%%sp" :: "m"(reset_vector[0]));
106 asm(" move.l %0,%%a0" :: "m"(reset_vector[1]));
107 asm(" jmp (%a0)");
109 /* Failure */
110 power_off();
113 void start_flashed_ramimage(void)
115 struct flash_header hdr;
116 unsigned char *buf = (unsigned char *)DRAM_START;
117 uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY;
119 if (!detect_flashed_ramimage())
120 return;
122 /* Load firmware from flash */
123 cpu_boost(true);
124 memcpy(&hdr, src, sizeof(struct flash_header));
125 src += sizeof(struct flash_header);
126 memcpy(buf, src, hdr.length);
127 cpu_boost(false);
129 start_firmware();
131 /* Failure */
132 power_off();
134 #endif /* IRIVER_H100_SERIES */
136 void shutdown(void)
138 printf("Shutting down...");
139 #ifdef HAVE_EEPROM_SETTINGS
140 /* Reset the rockbox crash check. */
141 firmware_settings.bl_version = 0;
142 eeprom_settings_store();
143 #endif
145 /* We need to gracefully spin down the disk to prevent clicks. */
146 if (ide_powered())
148 /* Make sure ATA has been initialized. */
149 ata_init();
151 /* And put the disk into sleep immediately. */
152 ata_sleepnow();
155 sleep(HZ*2);
157 /* Backlight OFF */
158 _backlight_off();
159 #ifdef HAVE_REMOTE_LCD
160 _remote_backlight_off();
161 #endif
163 __reset_cookie();
164 power_off();
167 /* Print the battery voltage (and a warning message). */
168 void check_battery(void)
170 int battery_voltage, batt_int, batt_frac;
172 battery_voltage = battery_adc_voltage();
173 batt_int = battery_voltage / 1000;
174 batt_frac = (battery_voltage % 1000) / 10;
176 printf("Batt: %d.%02dV", batt_int, batt_frac);
178 if (battery_voltage <= 310)
180 printf("WARNING! BATTERY LOW!!");
181 sleep(HZ*2);
185 #ifdef HAVE_EEPROM_SETTINGS
186 void initialize_eeprom(void)
188 if (detect_original_firmware())
189 return ;
191 if (!eeprom_settings_init())
193 recovery_mode = true;
194 return ;
197 /* If bootloader version has not been reset, disk might
198 * not be intact. */
199 if (firmware_settings.bl_version || !firmware_settings.disk_clean)
201 firmware_settings.disk_clean = false;
202 recovery_mode = true;
205 firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER;
206 eeprom_settings_store();
209 void try_flashboot(void)
211 if (!firmware_settings.initialized)
212 return ;
214 switch (firmware_settings.bootmethod)
216 case BOOT_DISK:
217 return;
219 case BOOT_ROM:
220 start_flashed_romimage();
221 recovery_mode = true;
222 break;
224 case BOOT_RAM:
225 start_flashed_ramimage();
226 recovery_mode = true;
227 break;
229 default:
230 recovery_mode = true;
231 return;
235 static const char *options[] = {
236 "Boot from disk",
237 "Boot RAM image",
238 "Boot ROM image",
239 "Shutdown"
242 #define FAILSAFE_OPTIONS 4
243 #define TIMEOUT (15*HZ)
244 void failsafe_menu(void)
246 long start_tick = current_tick;
247 int option = 3;
248 int button;
249 int defopt = -1;
250 char buf[32];
251 int i;
252 extern int line;
254 reset_screen();
255 printf("Bootloader %s", version);
256 check_battery();
257 printf("=========================");
258 line += FAILSAFE_OPTIONS;
259 printf("");
260 printf(" [NAVI] to confirm.");
261 printf(" [REC] to set as default.");
262 printf("");
264 if (firmware_settings.initialized)
266 defopt = firmware_settings.bootmethod;
267 if (defopt < 0 || defopt >= FAILSAFE_OPTIONS)
268 defopt = option;
271 while (current_tick - start_tick < TIMEOUT)
273 /* Draw the menu. */
274 line = 3;
275 for (i = 0; i < FAILSAFE_OPTIONS; i++)
277 char *def = "[DEF]";
278 char *arrow = "->";
280 if (i != defopt)
281 def = "";
282 if (i != option)
283 arrow = " ";
285 printf("%s %s %s", arrow, options[i], def);
288 snprintf(buf, sizeof(buf), "Time left: %ds",
289 (TIMEOUT - (current_tick - start_tick)) / HZ);
290 lcd_puts(0, 10, buf);
291 lcd_update();
292 button = button_get_w_tmo(HZ);
294 if (button == BUTTON_NONE || button & SYS_EVENT)
295 continue ;
297 start_tick = current_tick;
299 /* Ignore the ON/PLAY -button because it can cause trouble
300 with the RTC alarm mod. */
301 switch (button & ~(BUTTON_ON))
303 case BUTTON_UP:
304 case BUTTON_RC_REW:
305 if (option > 0)
306 option--;
307 break ;
309 case BUTTON_DOWN:
310 case BUTTON_RC_FF:
311 if (option < FAILSAFE_OPTIONS-1)
312 option++;
313 break ;
315 case BUTTON_SELECT:
316 case BUTTON_RC_ON:
317 goto execute;
319 case BUTTON_REC:
320 case BUTTON_RC_REC:
321 if (firmware_settings.initialized)
323 firmware_settings.bootmethod = option;
324 eeprom_settings_store();
325 defopt = option;
327 break ;
331 execute:
333 lcd_puts(0, 10, "Executing command...");
334 lcd_update();
335 sleep(HZ);
336 reset_screen();
338 switch (option)
340 case BOOT_DISK:
341 return ;
343 case BOOT_RAM:
344 start_flashed_ramimage();
345 printf("Image not found");
346 break;
348 case BOOT_ROM:
349 start_flashed_romimage();
350 printf("Image not found");
351 break;
354 shutdown();
356 #endif
358 /* get rid of a nasty humming sound during boot
359 -> RESET signal */
360 inline static void __uda1380_reset_hi(void)
362 #ifdef HAVE_UDA1380
363 or_l(1<<29, &GPIO_OUT);
364 or_l(1<<29, &GPIO_ENABLE);
365 or_l(1<<29, &GPIO_FUNCTION);
366 #endif
369 inline static void __uda1380_reset_lo(void)
371 #ifdef HAVE_UDA1380
372 and_l(~(1<<29), &GPIO_OUT);
373 #endif
376 void main(void)
378 int i;
379 int rc;
380 bool rc_on_button = false;
381 bool on_button = false;
382 bool rec_button = false;
383 bool hold_status = false;
384 int data;
385 extern int line; /* From common.c */
386 extern int remote_line; /* From common.c */
388 /* We want to read the buttons as early as possible, before the user
389 releases the ON button */
391 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
392 (The ON and Hold buttons on the main unit and the remote) */
393 or_l(0x00100062, &GPIO1_FUNCTION);
394 and_l(~0x00100062, &GPIO1_ENABLE);
396 data = GPIO1_READ;
397 if ((data & 0x20) == 0)
398 on_button = true;
400 if ((data & 0x40) == 0)
401 rc_on_button = true;
403 /* Set the default state of the hard drive power to OFF */
404 ide_power_enable(false);
406 power_init();
408 /* Turn off if neither ON button is pressed */
409 if (!(on_button || rc_on_button))
411 __reset_cookie();
412 power_off();
415 __uda1380_reset_hi();
417 /* Start with the main backlight OFF. */
418 _backlight_init();
419 _backlight_off();
421 /* Remote backlight ON */
422 #ifdef HAVE_REMOTE_LCD
423 _remote_backlight_on();
424 #endif
426 system_init();
427 kernel_init();
429 __uda1380_reset_lo();
431 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
432 /* Set up waitstates for the peripherals */
433 set_cpu_frequency(0); /* PLL off */
434 #ifdef CPU_COLDFIRE
435 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
436 #endif
437 #endif
438 enable_irq();
440 #ifdef HAVE_EEPROM_SETTINGS
441 initialize_eeprom();
442 #endif
444 usb_init();
445 /* A small delay after usb_init is necessary to read the I/O port correctly
446 (if ports are read _immediately_ after the init). */
447 /* sleep(1); */
449 adc_init();
450 button_init();
452 /* Only check remote hold status if remote power button was actually used. */
453 if (rc_on_button)
455 lcd_remote_init();
457 /* Allow the button driver to check the buttons */
458 sleep(HZ/50);
460 if (remote_button_hold())
461 hold_status = true;
464 /* Check main hold switch status too. */
465 if (on_button && button_hold())
467 hold_status = true;
470 /* Power on the hard drive early, to speed up the loading. */
471 if (!hold_status
472 # ifdef HAVE_EEPROM_SETTINGS
473 && !recovery_mode
474 # endif
477 ide_power_enable(true);
480 # ifdef HAVE_EEPROM_SETTINGS
481 if (!hold_status && (usb_detect() != USB_INSERTED) && !recovery_mode)
482 try_flashboot();
483 # endif
485 backlight_init();
488 lcd_init();
490 if (!rc_on_button)
491 lcd_remote_init();
493 /* Bootloader uses simplified backlight thread, so we need to enable
494 remote display here. */
495 if (remote_detect())
496 lcd_remote_on();
498 font_init();
500 lcd_setfont(FONT_SYSFIXED);
502 printf("Rockbox boot loader");
503 printf("Version %s", version);
505 /* No need to wait here more because lcd_init and others already do that. */
506 // sleep(HZ/50); /* Allow the button driver to check the buttons */
507 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
508 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
510 check_battery();
512 /* Don't start if the Hold button is active on the device you
513 are starting with */
514 if ((usb_detect() != USB_INSERTED) && (hold_status
515 #ifdef HAVE_EEPROM_SETTINGS
516 || recovery_mode
517 #endif
520 if (detect_original_firmware())
522 printf("Hold switch on");
523 shutdown();
526 #ifdef HAVE_EEPROM_SETTINGS
527 failsafe_menu();
528 #endif
531 /* Holding REC while starting runs the original firmware */
532 if (detect_original_firmware() && rec_button)
534 printf("Starting original firmware...");
535 start_iriver_fw();
538 /* A hack to enter USB mode without using the USB thread */
539 if(usb_detect() == USB_INSERTED)
541 const char msg[] = "Bootloader USB mode";
542 int w, h;
543 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
544 reset_screen();
545 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
546 lcd_update();
548 #ifdef HAVE_REMOTE_LCD
549 lcd_remote_puts(0, 3, msg);
550 lcd_remote_update();
551 #endif
553 #ifdef HAVE_EEPROM_SETTINGS
554 if (firmware_settings.initialized)
556 firmware_settings.disk_clean = false;
557 eeprom_settings_store();
559 #endif
560 ide_power_enable(true);
561 ata_enable(false);
562 sleep(HZ/20);
563 usb_enable(true);
564 cpu_idle_mode(true);
565 while (usb_detect() == USB_INSERTED)
567 /* Print the battery status. */
568 line = 0;
569 remote_line = 0;
570 check_battery();
572 ata_spin(); /* Prevent the drive from spinning down */
573 sleep(HZ);
575 /* Backlight OFF */
576 _backlight_off();
579 cpu_idle_mode(false);
580 usb_enable(false);
582 reset_screen();
583 lcd_update();
586 rc = ata_init();
587 if(rc)
589 reset_screen();
590 printf("ATA error: %d", rc);
591 printf("Insert USB cable and press");
592 printf("a button");
593 while(!(button_get(true) & BUTTON_REL));
597 disk_init();
599 rc = disk_mount_all();
600 if (rc<=0)
602 reset_screen();
603 printf("No partition found");
604 while(button_get(true) != SYS_USB_CONNECTED) {};
607 printf("Loading firmware");
608 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
609 if(i < 0)
610 printf("Error: %s", strerror(i));
612 if (i == EOK)
613 start_firmware();
615 if (!detect_original_firmware())
617 printf("No firmware found on disk");
618 sleep(HZ*2);
619 shutdown();
621 else {
622 sleep(HZ*2);
623 start_iriver_fw();
627 /* These functions are present in the firmware library, but we reimplement
628 them here because the originals do a lot more than we want */
629 void screen_dump(void)
633 int usb_screen(void)
635 return 0;
638 unsigned short *bidi_l2v(const unsigned char *str, int orientation)
640 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
641 unsigned short *target;
642 (void)orientation;
644 target = utf16_buf;
646 while (*str)
647 str = utf8decode(str, target++);
648 *target = 0;
649 return utf16_buf;