1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2007 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 ****************************************************************************/
28 #include "lcd-remote.h"
29 #include "scroll_engine.h"
37 #include "backlight.h"
38 #include "backlight-target.h"
42 #include "powermgmt.h"
46 #include "rbunicode.h"
51 /* Maximum allowed firmware image size. 10MB is more than enough */
52 #define MAX_LOADSIZE (10*1024*1024)
54 #define DRAM_START 0x31000000
56 char version
[] = APPSVERSION
;
58 /* Reset the cookie for the crt0 crash check */
59 inline void __reset_cookie(void)
61 asm(" move.l #0,%d0");
62 asm(" move.l %d0,0x10017ffc");
65 void start_iriver_fw(void)
67 asm(" move.w #0x2700,%sr");
69 asm(" movec.l %d0,%vbr");
75 void start_firmware(void)
77 asm(" move.w #0x2700,%sr");
79 asm(" move.l %0,%%d0" :: "i"(DRAM_START
));
80 asm(" movec.l %d0,%vbr");
81 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START
));
82 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START
+4)));
88 printf("Shutting down...");
90 /* We need to gracefully spin down the disk to prevent clicks. */
93 /* Make sure ATA has been initialized. */
96 /* And put the disk into sleep immediately. */
103 _remote_backlight_off();
109 /* Print the battery voltage (and a warning message). */
110 void check_battery(void)
112 int battery_voltage
, batt_int
, batt_frac
;
114 battery_voltage
= battery_adc_voltage();
115 batt_int
= battery_voltage
/ 1000;
116 batt_frac
= (battery_voltage
% 1000) / 10;
118 printf("Batt: %d.%02dV", batt_int
, batt_frac
);
120 if (battery_voltage
<= 310)
122 printf("WARNING! BATTERY LOW!!");
127 /* From the pcf50606 driver */
128 extern unsigned char pcf50606_intregs
[3];
132 extern int remote_line
;
138 bool rc_on_button
= false;
139 bool on_button
= false;
140 bool rec_button
= false;
141 bool hold_status
= false;
147 /* We want to read the buttons as early as possible, before the user
148 releases the ON button */
150 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
151 (The ON and Hold buttons on the main unit and the remote) */
152 or_l(0x00100062, &GPIO1_FUNCTION
);
153 and_l(~0x00100062, &GPIO1_ENABLE
);
156 if ((data
& 0x20) == 0)
159 if ((data
& 0x40) == 0)
162 /* Set the default state of the hard drive power to OFF */
163 ide_power_enable(false);
167 /* Check the interrupt registers if it was an RTC alarm */
168 rtc_alarm
= (pcf50606_intregs
[0] & 0x80)?true:false;
170 /* Turn off if we believe the start was accidental */
171 if(!(rtc_alarm
|| on_button
|| rc_on_button
||
172 (usb_detect() == USB_INSERTED
) || charger_inserted())) {
177 /* get rid of a nasty humming sound during boot */
178 mask
= disable_irq_save();
179 pcf50606_write(0x3b, 0x00); /* GPOOD2 high Z */
180 pcf50606_write(0x3b, 0x07); /* GPOOD2 low */
183 /* Start with the main backlight OFF. */
187 _remote_backlight_on();
192 /* Set up waitstates for the peripherals */
193 set_cpu_frequency(0); /* PLL off */
194 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS
);
208 lcd_setfont(FONT_SYSFIXED
);
210 printf("Rockbox boot loader");
211 printf("Version %s", version
);
213 sleep(HZ
/50); /* Allow the button driver to check the buttons */
214 rec_button
= ((button_status() & BUTTON_REC
) == BUTTON_REC
)
215 || ((button_status() & BUTTON_RC_REC
) == BUTTON_RC_REC
);
220 printf("RTC alarm detected");
222 /* Don't start if the Hold button is active on the device you
224 if ((on_button
&& button_hold()) ||
225 (rc_on_button
&& remote_button_hold()))
229 if (hold_status
&& !rtc_alarm
&& (usb_detect() != USB_INSERTED
) &&
232 if (detect_original_firmware())
234 printf("Hold switch on");
239 /* Holding REC while starting runs the original firmware */
240 if (detect_original_firmware() && rec_button
)
242 printf("Starting original firmware...");
246 if(charger_inserted())
248 const char charging_msg
[] = "Charging...";
249 const char complete_msg
[] = "Charging complete";
252 bool blink_toggle
= false;
253 bool request_start
= false;
257 while(charger_inserted() && !request_start
)
259 button
= button_get_w_tmo(HZ
);
264 request_start
= true;
267 case BUTTON_NONE
: /* Timeout */
271 /* To be replaced with a nice animation */
272 blink_toggle
= !blink_toggle
;
281 font_getstringsize(msg
, &w
, &h
, FONT_SYSFIXED
);
284 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, msg
);
290 if(usb_detect() == USB_INSERTED
)
291 request_start
= true;
299 cpu_idle_mode(false);
304 /* A hack to enter USB mode without using the USB thread */
305 if(usb_detect() == USB_INSERTED
)
307 const char msg
[] = "Bootloader USB mode";
309 font_getstringsize(msg
, &w
, &h
, FONT_SYSFIXED
);
311 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, msg
);
314 lcd_remote_puts(0, 3, msg
);
317 ide_power_enable(true);
322 while (usb_detect() == USB_INSERTED
)
324 /* Print the battery status. */
329 ata_spin(); /* Prevent the drive from spinning down */
333 cpu_idle_mode(false);
344 printf("ATA error: %d", rc
);
345 printf("Insert USB cable and press");
347 while(!(button_get(true) & BUTTON_REL
));
353 rc
= disk_mount_all();
357 printf("No partition found");
358 while(button_get(true) != SYS_USB_CONNECTED
) {};
361 printf("Loading firmware");
362 i
= load_firmware((unsigned char *)DRAM_START
, BOOTFILE
, MAX_LOADSIZE
);
364 printf("Error: %s", strerror(i
));
369 if (!detect_original_firmware())
371 printf("No firmware found on disk");
381 /* These functions are present in the firmware library, but we reimplement
382 them here because the originals do a lot more than we want */
383 void screen_dump(void)
392 unsigned short *bidi_l2v(const unsigned char *str
, int orientation
)
394 static unsigned short utf16_buf
[SCROLL_LINE_SIZE
];
395 unsigned short *target
;
401 str
= utf8decode(str
, target
++);