1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 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 ****************************************************************************/
30 #include "lcd-remote.h"
31 #include "scroll_engine.h"
39 #include "backlight.h"
40 #include "backlight-target.h"
44 #include "powermgmt.h"
48 #include "rbunicode.h"
53 /* Maximum allowed firmware image size. 10MB is more than enough */
54 #define MAX_LOADSIZE (10*1024*1024)
56 #define DRAM_START 0x31000000
58 char version
[] = APPSVERSION
;
60 /* Reset the cookie for the crt0 crash check */
61 inline void __reset_cookie(void)
63 asm(" move.l #0,%d0");
64 asm(" move.l %d0,0x10017ffc");
67 void start_iriver_fw(void)
69 asm(" move.w #0x2700,%sr");
71 asm(" movec.l %d0,%vbr");
77 void start_firmware(void)
79 asm(" move.w #0x2700,%sr");
81 asm(" move.l %0,%%d0" :: "i"(DRAM_START
));
82 asm(" movec.l %d0,%vbr");
83 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START
));
84 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START
+4)));
90 printf("Shutting down...");
92 /* We need to gracefully spin down the disk to prevent clicks. */
95 /* Make sure ATA has been initialized. */
98 /* And put the disk into sleep immediately. */
105 _remote_backlight_off();
111 /* Print the battery voltage (and a warning message). */
112 void check_battery(void)
114 int battery_voltage
, batt_int
, batt_frac
;
116 battery_voltage
= battery_adc_voltage();
117 batt_int
= battery_voltage
/ 1000;
118 batt_frac
= (battery_voltage
% 1000) / 10;
120 printf("Batt: %d.%02dV", batt_int
, batt_frac
);
122 if (battery_voltage
<= 310)
124 printf("WARNING! BATTERY LOW!!");
129 /* From the pcf50606 driver */
130 extern unsigned char pcf50606_intregs
[3];
134 extern int remote_line
;
140 bool rc_on_button
= false;
141 bool on_button
= false;
142 bool rec_button
= false;
143 bool hold_status
= false;
149 /* We want to read the buttons as early as possible, before the user
150 releases the ON button */
152 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
153 (The ON and Hold buttons on the main unit and the remote) */
154 or_l(0x00100062, &GPIO1_FUNCTION
);
155 and_l(~0x00100062, &GPIO1_ENABLE
);
158 if ((data
& 0x20) == 0)
161 if ((data
& 0x40) == 0)
164 /* Set the default state of the hard drive power to OFF */
165 ide_power_enable(false);
169 /* Check the interrupt registers if it was an RTC alarm */
170 rtc_alarm
= (pcf50606_intregs
[0] & 0x80)?true:false;
172 /* Turn off if we believe the start was accidental */
173 if(!(rtc_alarm
|| on_button
|| rc_on_button
||
174 (usb_detect() == USB_INSERTED
) || charger_inserted())) {
179 /* get rid of a nasty humming sound during boot */
180 mask
= disable_irq_save();
181 pcf50606_write(0x3b, 0x00); /* GPOOD2 high Z */
182 pcf50606_write(0x3b, 0x07); /* GPOOD2 low */
185 /* Start with the main backlight OFF. */
189 _remote_backlight_on();
194 /* Set up waitstates for the peripherals */
195 set_cpu_frequency(0); /* PLL off */
196 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS
);
210 lcd_setfont(FONT_SYSFIXED
);
212 printf("Rockbox boot loader");
213 printf("Version %s", version
);
215 sleep(HZ
/50); /* Allow the button driver to check the buttons */
216 rec_button
= ((button_status() & BUTTON_REC
) == BUTTON_REC
)
217 || ((button_status() & BUTTON_RC_REC
) == BUTTON_RC_REC
);
222 printf("RTC alarm detected");
224 /* Don't start if the Hold button is active on the device you
226 if ((on_button
&& button_hold()) ||
227 (rc_on_button
&& remote_button_hold()))
231 if (hold_status
&& !rtc_alarm
&& (usb_detect() != USB_INSERTED
) &&
234 if (detect_original_firmware())
236 printf("Hold switch on");
241 /* Holding REC while starting runs the original firmware */
242 if (detect_original_firmware() && rec_button
)
244 printf("Starting original firmware...");
248 if(charger_inserted())
250 const char charging_msg
[] = "Charging...";
251 const char complete_msg
[] = "Charging complete";
254 bool blink_toggle
= false;
255 bool request_start
= false;
259 while(charger_inserted() && !request_start
)
261 button
= button_get_w_tmo(HZ
);
266 request_start
= true;
269 case BUTTON_NONE
: /* Timeout */
273 /* To be replaced with a nice animation */
274 blink_toggle
= !blink_toggle
;
283 font_getstringsize(msg
, &w
, &h
, FONT_SYSFIXED
);
286 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, msg
);
292 if(usb_detect() == USB_INSERTED
)
293 request_start
= true;
301 cpu_idle_mode(false);
306 /* A hack to enter USB mode without using the USB thread */
307 if(usb_detect() == USB_INSERTED
)
309 const char msg
[] = "Bootloader USB mode";
311 font_getstringsize(msg
, &w
, &h
, FONT_SYSFIXED
);
313 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, msg
);
316 lcd_remote_puts(0, 3, msg
);
319 ide_power_enable(true);
320 storage_enable(false);
324 while (usb_detect() == USB_INSERTED
)
326 /* Print the battery status. */
331 storage_spin(); /* Prevent the drive from spinning down */
335 cpu_idle_mode(false);
346 printf("ATA error: %d", rc
);
347 printf("Insert USB cable and press");
349 while(!(button_get(true) & BUTTON_REL
));
355 rc
= disk_mount_all();
359 printf("No partition found");
360 while(button_get(true) != SYS_USB_CONNECTED
) {};
363 printf("Loading firmware");
364 i
= load_firmware((unsigned char *)DRAM_START
, BOOTFILE
, MAX_LOADSIZE
);
366 printf("Error: %s", strerror(i
));
371 if (!detect_original_firmware())
373 printf("No firmware found on disk");
383 /* These functions are present in the firmware library, but we reimplement
384 them here because the originals do a lot more than we want */
385 void screen_dump(void)
394 unsigned short *bidi_l2v(const unsigned char *str
, int orientation
)
396 static unsigned short utf16_buf
[SCROLL_LINE_SIZE
];
397 unsigned short *target
;
403 str
= utf8decode(str
, target
++);