1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
37 #include "backlight.h"
38 #include "backlight-target.h"
42 #include "powermgmt.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 #define BOOTMENU_TIMEOUT (10*HZ)
56 #define BOOTMENU_OPTIONS 3
58 #define EVENT_NONE 0x00
61 #define EVENT_USB 0x04
65 static const char *bootmenu_options
[] = {
82 static inline bool _charger_inserted(void)
84 return (GPIO1_READ
& (1<<14)) ? false : true;
87 static inline bool _battery_full(void)
89 return (GPIO_READ
& (1<<30)) ? true : false;
92 /* Reset the cookie for the crt0 crash check */
93 static inline void __reset_cookie(void)
95 asm(" move.l #0,%d0");
96 asm(" move.l %d0,0x10017ffc");
99 static void start_rockbox(void)
102 asm(" move.w #0x2700,%sr");
104 asm(" move.l %0,%%d0" :: "i"(DRAM_START
));
105 asm(" movec.l %d0,%vbr");
106 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START
));
107 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START
+4)));
111 static void start_mpio_firmware(void)
113 asm(" move.w #0x2700,%sr");
115 asm(" movec.l %d0,%vbr");
116 asm(" move.l 0,%sp");
120 static void __shutdown(void)
122 if (_charger_inserted())
123 /* if AC power do nothing */
126 /* We need to gracefully spin down the disk to prevent clicks. */
129 /* Make sure ATA has been initialized. */
132 /* And put the disk into sleep immediately. */
143 /* Print the battery voltage (and a warning message). */
144 static void check_battery(void)
147 int battery_voltage
, batt_int
, batt_frac
;
149 battery_voltage
= battery_adc_voltage();
150 batt_int
= battery_voltage
/ 1000;
151 batt_frac
= (battery_voltage
% 1000) / 10;
153 printf("Battery: %d.%02dV", batt_int
, batt_frac
);
155 if (battery_voltage
<= 3500)
157 printf("WARNING! BATTERY LOW!!");
164 static void lcd_putstring_centered(const char *string
)
167 font_getstringsize(string
, &w
, &h
, FONT_SYSFIXED
);
168 lcd_putsxy((LCD_WIDTH
-w
)/2, (LCD_HEIGHT
-h
)/2, string
);
171 static void rb_boot(void)
175 /* boost to speedup rb image loading */
181 printf("ATA error: %d", rc
);
188 rc
= disk_mount_all();
191 printf("No partition found");
196 printf("Loading firmware");
198 rc
= load_firmware((unsigned char *)DRAM_START
,
199 BOOTFILE
, MAX_LOADSIZE
);
204 printf("Can't load " BOOTFILE
": ");
205 printf("Result: %s", strerror(rc
));
214 static void bootmenu(void)
217 enum option_t option
= rockbox
;
219 const char select
[] = "->";
220 long start_tick
= current_tick
;
222 /* backbone of menu */
224 printf("Rockbox boot loader");
225 printf("Ver: " RBVERSION
);
230 printf("=========================");
232 line
+= BOOTMENU_OPTIONS
+2; /* skip lines */
234 printf("=========================");
236 printf(" [FF] [PREV] to move ");
237 printf(" [PLAY] to confirm ");
239 /* content of menu and keys handling */
240 while (TIME_BEFORE(current_tick
,start_tick
+ BOOTMENU_TIMEOUT
))
243 line
= 6; /* move below header */
245 for (i
=0;i
<BOOTMENU_OPTIONS
;i
++)
248 printf(" %s",bootmenu_options
[i
]);
250 printf("%s %s",select
,bootmenu_options
[i
]);
255 printf("Time left: %ds",(BOOTMENU_TIMEOUT
-
256 (current_tick
- start_tick
))/HZ
);
260 button
= BUTTON_NONE
;
261 button
= button_get_w_tmo(HZ
);
267 if (option
> rockbox
)
275 if (option
< shutdown
)
283 case (BUTTON_PLAY
|BUTTON_REC
):
293 start_mpio_firmware();
308 const char usb_connect_msg
[] = "Bootloader USB mode";
309 const char charging_msg
[] = "Charging...";
310 const char complete_msg
[] = "Charging complete";
312 /* helper variable for messages */
313 bool blink_toggle
= false;
316 unsigned int event
= EVENT_NONE
;
317 unsigned int last_event
= EVENT_NONE
;
319 /* this is default mode after power_init() */
320 bool high_current_charging
= true;
327 set_cpu_frequency(CPUFREQ_NORMAL
);
328 coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS
);
333 /* only lowlevel functions no queue init */
337 /* setup font system*/
339 lcd_setfont(FONT_SYSFIXED
);
341 /* buttons reading */
348 /* Handle wakeup event. Possibilities are:
358 button
= button_get_w_tmo(HZ
);
360 if ( (button
& BUTTON_PLAY
) || (button
& BUTTON_RC_PLAY
) )
363 if ( usb_detect() == USB_INSERTED
)
366 if ( _charger_inserted() )
373 case (EVENT_ON
| EVENT_AC
):
374 /* hold is handled in button driver */
375 cpu_idle_mode(false);
376 ide_power_enable(true);
378 if (button
== (BUTTON_PLAY
|BUTTON_REC
))
387 if (!(last_event
& EVENT_AC
))
389 /* reset charging circuit */
390 and_l(~(1<<23), &GPIO_ENABLE
);
394 if (last_event
& EVENT_USB
)
398 ide_power_enable(false);
405 lcd_putstring_centered(charging_msg
);
407 blink_toggle
= !blink_toggle
;
409 else /* end of charge condition */
411 /* put LTC1733 into shutdown mode */
412 or_l((1<<23), &GPIO_ENABLE
);
414 if (high_current_charging
)
416 /* switch to low current mode */
417 and_l(~(1<<15), &GPIO_OUT
);
419 /* reset charging circuit */
420 and_l(~(1<<23), &GPIO_ENABLE
);
422 high_current_charging
= false;
426 lcd_putstring_centered(complete_msg
);
433 case (EVENT_USB
| EVENT_AC
):
434 /* AC plug in while in USB mode */
435 if (!(last_event
& EVENT_AC
))
437 /* reset charger circuit */
438 and_l(~(1<<23), &GPIO_ENABLE
);
442 if (!(last_event
& EVENT_USB
))
445 ide_power_enable(true);
450 /* display blinking USB indicator */
454 lcd_putstring_centered(usb_connect_msg
);
457 blink_toggle
= !blink_toggle
;
463 if (last_event
& EVENT_USB
)
468 ide_power_enable(false);
472 /* spurious wakeup ?*/
482 /* These functions are present in the firmware library, but we reimplement
483 them here because the originals do a lot more than we want */
484 void screen_dump(void)