Gigabeat S: Those odd calls to irq_handler can still happen rarely after executing...
[kugel-rb.git] / bootloader / mpio_hd200.c
blob68152f3c2dff9aa5f6619bb6aa6cdd9e09016bf9
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id:$
10 * Copyright (C) 2010 Marcin Bukat
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 "kernel.h"
31 #include "thread.h"
32 #include "storage.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"
45 #include "common.h"
47 #include <stdarg.h>
49 /* Maximum allowed firmware image size. 10MB is more than enough */
50 #define MAX_LOADSIZE (10*1024*1024)
52 #define DRAM_START 0x31000000
54 #define BOOTMENU_TIMEOUT (10*HZ)
55 #define BOOTMENU_OPTIONS 3
57 /* From common.c */
58 extern int line;
59 static const char *bootmenu_options[] = {
60 "Boot rockbox",
61 "Boot MPIO firmware",
62 "Shutdown"
65 enum option_t {
66 rockbox,
67 mpio_firmware,
68 shutdown
71 int usb_screen(void)
73 return 0;
76 char version[] = APPSVERSION;
78 bool _charger_inserted(void)
80 return (GPIO1_READ & (1<<14)) ? false : true;
83 bool _battery_full(void)
85 return (GPIO_READ & (1<<30)) ? true : false;
88 /* Reset the cookie for the crt0 crash check */
89 inline void __reset_cookie(void)
91 asm(" move.l #0,%d0");
92 asm(" move.l %d0,0x10017ffc");
95 void start_rockbox(void)
97 adc_close();
98 asm(" move.w #0x2700,%sr");
99 __reset_cookie();
100 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
101 asm(" movec.l %d0,%vbr");
102 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
103 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
104 asm(" jmp (%a0)");
107 void start_mpio_firmware(void)
109 asm(" move.w #0x2700,%sr");
110 __reset_cookie();
111 asm(" movec.l %d0,%vbr");
112 asm(" move.l 0,%sp");
113 asm(" jmp 8");
116 void __reset(void)
118 asm(" move.w #0x2700,%sr");
119 __reset_cookie();
120 asm(" movec.l %d0,%vbr");
121 asm(" move.l (0), %sp");
122 asm(" movea.l (4),%a0");
123 asm(" jmp (%a0)");
126 void __shutdown(void)
128 /* We need to gracefully spin down the disk to prevent clicks. */
129 if (ide_powered())
131 /* Make sure ATA has been initialized. */
132 storage_init();
134 /* And put the disk into sleep immediately. */
135 storage_sleepnow();
138 /* Backlight OFF */
139 _backlight_off();
140 __reset_cookie();
142 if (_charger_inserted())
144 /* reset instead of power_off() */
145 __reset();
147 else
149 power_off();
153 /* Print the battery voltage (and a warning message). */
154 void check_battery(void)
157 int battery_voltage, batt_int, batt_frac;
159 battery_voltage = battery_adc_voltage();
160 batt_int = battery_voltage / 1000;
161 batt_frac = (battery_voltage % 1000) / 10;
163 printf("Battery: %d.%02dV", batt_int, batt_frac);
165 if (battery_voltage <= 3500)
167 printf("WARNING! BATTERY LOW!!");
168 sleep(HZ*2);
174 void lcd_putstring_centered(const char *string)
176 int w,h;
177 font_getstringsize(string, &w, &h, FONT_SYSFIXED);
178 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, string);
181 void bootmenu(void)
183 int rc;
184 enum option_t i;
185 enum option_t option = rockbox;
186 int button;
187 const char select[] = "->";
188 long start_tick = current_tick;
190 /* backbone of menu */
191 /* run the loader */
192 printf("Rockbox boot loader");
193 printf("Ver: %s", version);
195 check_battery();
197 printf("");
198 printf("=========================");
200 line += BOOTMENU_OPTIONS+2; /* skip lines */
202 printf("=========================");
203 printf("");
204 printf(" [FF] [PREV] to move ");
205 printf(" [PLAY] to confirm ");
207 /* content of menu and keys handling */
208 while (TIME_BEFORE(current_tick,start_tick + BOOTMENU_TIMEOUT))
210 /* Draw the menu. */
211 line = 6; /* move below header */
213 for (i=0;i<BOOTMENU_OPTIONS;i++)
215 if (i != option)
216 printf(" %s",bootmenu_options[i]);
217 else
218 printf("%s %s",select,bootmenu_options[i]);
221 line = 15;
223 printf("Time left: %ds",(BOOTMENU_TIMEOUT -
224 (current_tick - start_tick))/HZ);
226 lcd_update();
228 button = button_get_w_tmo(HZ);
230 switch (button)
232 case BUTTON_PREV:
233 if (option > rockbox)
234 option--;
235 else
236 option = shutdown;
237 break;
239 case BUTTON_NEXT:
240 if (option < shutdown)
241 option++;
242 else
243 option = rockbox;
244 break;
246 case BUTTON_PLAY:
247 case (BUTTON_PLAY|BUTTON_REC):
248 reset_screen();
250 switch (option)
252 case rockbox:
253 rc = storage_init();
254 if(rc)
256 printf("ATA error: %d", rc);
257 sleep(HZ*5);
258 __shutdown();
261 disk_init();
263 rc = disk_mount_all();
264 if (rc<=0)
266 printf("No partition found");
267 sleep(HZ*5);
268 __shutdown();
271 printf("Loading firmware");
272 rc = load_firmware((unsigned char *)DRAM_START,
273 BOOTFILE, MAX_LOADSIZE);
274 printf("Result: %s", strerror(rc));
276 if (rc < EOK)
278 printf("Error!");
279 printf("Can't load " BOOTFILE ": ");
280 printf(strerror(rc));
281 sleep(HZ*5);
282 __shutdown();
284 else
286 start_rockbox();
289 break;
291 case mpio_firmware:
292 start_mpio_firmware();
293 break;
295 default:
296 __shutdown();
297 break;
301 /* timeout */
302 __shutdown();
305 void main(void)
307 /* messages */
308 const char usb_connect_msg[] = "Bootloader USB mode";
309 const char charging_msg[] = "Charging...";
310 const char complete_msg[] = "Charging complete";
311 const char hold_msg[] = "Hold switch on";
312 const char shutdown_msg[] = "Shutting down...";
314 /* helper variables for messages */
315 bool blink_toggle = false;
316 const char *msg;
318 bool on_button = false;
319 int button;
321 /* We want to read the buttons as early as possible, before the user
322 releases the ON button */
324 or_l( ((1<<24)|(1<<4)), &GPIO1_FUNCTION); /* main Hold & Play */
325 and_l( ~((1<<24)|(1<<4)), &GPIO1_ENABLE); /* HiZ */
327 if (GPIO1_READ & (1<<24))
328 on_button = true;
330 power_init();
332 system_init();
333 kernel_init();
335 set_cpu_frequency(CPUFREQ_NORMAL);
336 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS);
338 enable_irq();
339 lcd_init();
341 backlight_init();
342 font_init();
343 lcd_setfont(FONT_SYSFIXED);
345 adc_init();
346 button_init();
347 usb_init();
349 /* handle charging */
350 if( _charger_inserted())
352 or_l((1<<15),&GPIO_OUT);
354 cpu_idle_mode(true);
356 while( _charger_inserted() &&
357 usb_detect() != USB_INSERTED &&
358 !on_button)
360 button = button_get_w_tmo(HZ);
362 switch(button)
364 case BUTTON_ON:
365 on_button = true;
366 reset_screen();
367 break;
369 case BUTTON_NONE: /* Timeout */
371 if(!_battery_full())
373 /* To be replaced with a nice animation */
374 blink_toggle = !blink_toggle;
375 msg = charging_msg;
377 else
379 blink_toggle = true;
380 msg = complete_msg;
383 reset_screen();
384 if(blink_toggle)
385 lcd_putstring_centered(msg);
387 check_battery();
388 break;
392 cpu_idle_mode(false);
395 /* handle USB in bootloader */
396 if (usb_detect() == USB_INSERTED)
398 ide_power_enable(true);
399 sleep(HZ/20);
400 usb_enable(true);
401 cpu_idle_mode(true);
403 while (usb_detect() == USB_INSERTED)
405 line = 0;
407 reset_screen();
409 if(blink_toggle)
411 lcd_putstring_centered(usb_connect_msg);
414 check_battery();
415 blink_toggle = !blink_toggle;
417 storage_spin(); /* Prevent the drive from spinning down */
418 sleep(HZ);
421 cpu_idle_mode(false);
422 usb_enable(false);
424 sleep(HZ);
425 reset_screen();
426 lcd_update();
429 /* handle ON button press */
430 if (on_button)
432 if (button_hold() &&
433 !_charger_inserted() &&
434 usb_detect() != USB_INSERTED)
436 lcd_putstring_centered(hold_msg);
437 lcd_update();
438 sleep(HZ*3);
439 __shutdown();
443 else
445 lcd_putstring_centered(shutdown_msg);
446 lcd_update();
447 sleep(HZ*3);
448 __shutdown();
452 bootmenu();
455 /* These functions are present in the firmware library, but we reimplement
456 them here because the originals do a lot more than we want */
457 void screen_dump(void)