Support "eject" on OS X.
[maemo-rb.git] / bootloader / iriver_h300.c
blobb6d7da20fa252a426ed2d684fc06a8ab4831d46a
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
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 "pcf50606.h"
47 #include "common.h"
48 #include "rbunicode.h"
49 #include "isp1362.h"
50 #include "version.h"
52 #include <stdarg.h>
54 /* Maximum allowed firmware image size. 10MB is more than enough */
55 #define MAX_LOADSIZE (10*1024*1024)
57 #define DRAM_START 0x31000000
59 /* Reset the cookie for the crt0 crash check */
60 inline void __reset_cookie(void)
62 asm(" move.l #0,%d0");
63 asm(" move.l %d0,0x10017ffc");
66 void start_iriver_fw(void)
68 asm(" move.w #0x2700,%sr");
69 __reset_cookie();
70 asm(" movec.l %d0,%vbr");
71 asm(" move.l 0,%sp");
72 asm(" lea.l 8,%a0");
73 asm(" jmp (%a0)");
76 void start_firmware(void)
78 asm(" move.w #0x2700,%sr");
79 __reset_cookie();
80 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
81 asm(" movec.l %d0,%vbr");
82 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
83 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
84 asm(" jmp (%a0)");
87 void shutdown(void)
89 printf("Shutting down...");
91 /* We need to gracefully spin down the disk to prevent clicks. */
92 if (ide_powered())
94 /* Make sure ATA has been initialized. */
95 storage_init();
97 /* And put the disk into sleep immediately. */
98 storage_sleepnow();
101 sleep(HZ*2);
103 _backlight_off();
104 _remote_backlight_off();
106 __reset_cookie();
107 power_off();
110 /* Print the battery voltage (and a warning message). */
111 void check_battery(void)
113 int battery_voltage, batt_int, batt_frac;
115 battery_voltage = _battery_voltage();
116 batt_int = battery_voltage / 1000;
117 batt_frac = (battery_voltage % 1000) / 10;
119 printf("Batt: %d.%02dV", batt_int, batt_frac);
121 if (battery_voltage <= 310)
123 printf("WARNING! BATTERY LOW!!");
124 sleep(HZ*2);
128 /* From the pcf50606 driver */
129 extern unsigned char pcf50606_intregs[3];
131 /* From common.c */
132 extern int line;
133 extern int remote_line;
135 void main(void)
137 int i;
138 int rc;
139 bool rc_on_button = false;
140 bool on_button = false;
141 bool rec_button = false;
142 bool hold_status = false;
143 int data;
144 bool rtc_alarm;
145 int button;
146 int mask;
148 /* We want to read the buttons as early as possible, before the user
149 releases the ON button */
151 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
152 (The ON and Hold buttons on the main unit and the remote) */
153 or_l(0x00100062, &GPIO1_FUNCTION);
154 and_l(~0x00100062, &GPIO1_ENABLE);
156 data = GPIO1_READ;
157 if ((data & 0x20) == 0)
158 on_button = true;
160 if ((data & 0x40) == 0)
161 rc_on_button = true;
163 /* Set the default state of the hard drive power to OFF */
164 ide_power_enable(false);
166 power_init();
168 /* Check the interrupt registers if it was an RTC alarm */
169 rtc_alarm = (pcf50606_intregs[0] & 0x80)?true:false;
171 /* Turn off if we believe the start was accidental */
172 if(!(rtc_alarm || on_button || rc_on_button ||
173 (usb_detect() == USB_INSERTED) || charger_inserted())) {
174 __reset_cookie();
175 power_off();
178 /* get rid of a nasty humming sound during boot */
179 mask = disable_irq_save();
180 pcf50606_write(0x3b, 0x00); /* GPOOD2 high Z */
181 pcf50606_write(0x3b, 0x07); /* GPOOD2 low */
182 restore_irq(mask);
184 /* Start with the main backlight OFF. */
185 _backlight_init();
186 _backlight_off();
188 _remote_backlight_on();
190 system_init();
191 kernel_init();
193 /* Set up waitstates for the peripherals */
194 set_cpu_frequency(0); /* PLL off */
195 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
196 enable_irq();
198 isp1362_init();
200 adc_init();
201 button_init();
203 backlight_init();
205 lcd_init();
206 lcd_remote_init();
207 font_init();
209 lcd_setfont(FONT_SYSFIXED);
211 printf("Rockbox boot loader");
212 printf("Version " RBVERSION);
214 sleep(HZ/50); /* Allow the button driver to check the buttons */
215 rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC)
216 || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC);
218 check_battery();
220 if(rtc_alarm)
221 printf("RTC alarm detected");
223 /* Don't start if the Hold button is active on the device you
224 are starting with */
225 if ((on_button && button_hold()) ||
226 (rc_on_button && remote_button_hold()))
228 hold_status = true;
230 if (hold_status && !rtc_alarm && (usb_detect() != USB_INSERTED) &&
231 !charger_inserted())
233 if (detect_original_firmware())
235 printf("Hold switch on");
236 shutdown();
240 /* Holding REC while starting runs the original firmware */
241 if (detect_original_firmware() && rec_button)
243 printf("Starting original firmware...");
244 start_iriver_fw();
247 if(charger_inserted())
249 const char charging_msg[] = "Charging...";
250 const char complete_msg[] = "Charging complete";
251 const char *msg;
252 int w, h;
253 bool blink_toggle = false;
254 bool request_start = false;
256 cpu_idle_mode(true);
258 while(charger_inserted() && !request_start)
260 button = button_get_w_tmo(HZ);
262 switch(button)
264 case BUTTON_ON:
265 request_start = true;
266 break;
268 case BUTTON_NONE: /* Timeout */
270 if(charging_state())
272 /* To be replaced with a nice animation */
273 blink_toggle = !blink_toggle;
274 msg = charging_msg;
276 else
278 blink_toggle = true;
279 msg = complete_msg;
282 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
283 reset_screen();
284 if(blink_toggle)
285 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
287 check_battery();
288 break;
291 if(usb_detect() == USB_INSERTED)
292 request_start = true;
294 if(!request_start)
296 __reset_cookie();
297 power_off();
300 cpu_idle_mode(false);
303 usb_init();
305 /* A hack to enter USB mode without using the USB thread */
306 if(usb_detect() == USB_INSERTED)
308 const char msg[] = "Bootloader USB mode";
309 int w, h;
310 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
311 reset_screen();
312 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
313 lcd_update();
315 lcd_remote_puts(0, 3, msg);
316 lcd_remote_update();
318 ide_power_enable(true);
319 storage_enable(false);
320 sleep(HZ/20);
321 usb_enable(true);
322 cpu_idle_mode(true);
323 while (usb_detect() == USB_INSERTED)
325 /* Print the battery status. */
326 line = 0;
327 remote_line = 0;
328 check_battery();
330 storage_spin(); /* Prevent the drive from spinning down */
331 sleep(HZ);
334 cpu_idle_mode(false);
335 usb_enable(false);
337 reset_screen();
338 lcd_update();
341 rc = storage_init();
342 if(rc)
344 reset_screen();
345 printf("ATA error: %d", rc);
346 printf("Insert USB cable and press");
347 printf("a button");
348 while(!(button_get(true) & BUTTON_REL));
352 disk_init();
354 rc = disk_mount_all();
355 if (rc<=0)
357 reset_screen();
358 printf("No partition found");
359 while(button_get(true) != SYS_USB_CONNECTED) {};
362 printf("Loading firmware");
363 i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE);
364 if(i < 0)
365 printf("Error: %s", strerror(i));
367 if (i == EOK)
368 start_firmware();
370 if (!detect_original_firmware())
372 printf("No firmware found on disk");
373 sleep(HZ*2);
374 shutdown();
376 else {
377 sleep(HZ*2);
378 start_iriver_fw();
382 /* These functions are present in the firmware library, but we reimplement
383 them here because the originals do a lot more than we want */
384 void screen_dump(void)
388 int usb_screen(void)
390 return 0;
393 unsigned short *bidi_l2v(const unsigned char *str, int orientation)
395 static unsigned short utf16_buf[SCROLL_LINE_SIZE];
396 unsigned short *target;
397 (void)orientation;
399 target = utf16_buf;
401 while (*str)
402 str = utf8decode(str, target++);
403 *target = 0;
404 return utf16_buf;