Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2007 by Linus Nielsen Feltzing |
| 11 | * |
| 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. |
| 14 | * |
| 15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 16 | * KIND, either express or implied. |
| 17 | * |
| 18 | ****************************************************************************/ |
| 19 | #include "config.h" |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> |
| 23 | #include "inttypes.h" |
| 24 | #include "string.h" |
| 25 | #include "cpu.h" |
| 26 | #include "system.h" |
| 27 | #include "lcd.h" |
| 28 | #include "lcd-remote.h" |
| 29 | #include "kernel.h" |
| 30 | #include "thread.h" |
| 31 | #include "ata.h" |
| 32 | #include "usb.h" |
| 33 | #include "disk.h" |
| 34 | #include "font.h" |
| 35 | #include "adc.h" |
| 36 | #include "backlight.h" |
| 37 | #include "backlight-target.h" |
| 38 | #include "button.h" |
| 39 | #include "panic.h" |
| 40 | #include "power.h" |
| 41 | #include "file.h" |
| 42 | #include "uda1380.h" |
| 43 | #include "pcf50606.h" |
| 44 | #include "common.h" |
| 45 | #include "rbunicode.h" |
Linus Nielsen Feltzing | 8448d3b | 2007-03-02 13:04:57 +0000 | [diff] [blame] | 46 | #include "isp1362.h" |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 47 | |
| 48 | #include <stdarg.h> |
| 49 | |
| 50 | /* Maximum allowed firmware image size. 10MB is more than enough */ |
| 51 | #define MAX_LOADSIZE (10*1024*1024) |
| 52 | |
| 53 | #define DRAM_START 0x31000000 |
| 54 | |
| 55 | char version[] = APPSVERSION; |
| 56 | |
| 57 | /* Reset the cookie for the crt0 crash check */ |
| 58 | inline void __reset_cookie(void) |
| 59 | { |
| 60 | asm(" move.l #0,%d0"); |
| 61 | asm(" move.l %d0,0x10017ffc"); |
| 62 | } |
| 63 | |
| 64 | void start_iriver_fw(void) |
| 65 | { |
| 66 | asm(" move.w #0x2700,%sr"); |
| 67 | __reset_cookie(); |
| 68 | asm(" movec.l %d0,%vbr"); |
| 69 | asm(" move.l 0,%sp"); |
| 70 | asm(" lea.l 8,%a0"); |
| 71 | asm(" jmp (%a0)"); |
| 72 | } |
| 73 | |
| 74 | void start_firmware(void) |
| 75 | { |
| 76 | asm(" move.w #0x2700,%sr"); |
| 77 | __reset_cookie(); |
| 78 | asm(" move.l %0,%%d0" :: "i"(DRAM_START)); |
| 79 | asm(" movec.l %d0,%vbr"); |
| 80 | asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START)); |
| 81 | asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4))); |
| 82 | asm(" jmp (%a0)"); |
| 83 | } |
| 84 | |
| 85 | void shutdown(void) |
| 86 | { |
| 87 | printf("Shutting down..."); |
| 88 | |
| 89 | /* We need to gracefully spin down the disk to prevent clicks. */ |
| 90 | if (ide_powered()) |
| 91 | { |
| 92 | /* Make sure ATA has been initialized. */ |
| 93 | ata_init(); |
| 94 | |
| 95 | /* And put the disk into sleep immediately. */ |
| 96 | ata_sleepnow(); |
| 97 | } |
| 98 | |
| 99 | sleep(HZ*2); |
| 100 | |
| 101 | __backlight_off(); |
| 102 | __remote_backlight_off(); |
| 103 | |
| 104 | __reset_cookie(); |
| 105 | power_off(); |
| 106 | } |
| 107 | |
| 108 | /* Print the battery voltage (and a warning message). */ |
| 109 | void check_battery(void) |
| 110 | { |
| 111 | int adc_battery, battery_voltage, batt_int, batt_frac; |
| 112 | |
| 113 | adc_battery = adc_read(ADC_BATTERY); |
| 114 | |
| 115 | battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000; |
| 116 | batt_int = battery_voltage / 100; |
| 117 | batt_frac = battery_voltage % 100; |
| 118 | |
| 119 | printf("Batt: %d.%02dV", batt_int, batt_frac); |
| 120 | |
| 121 | if (battery_voltage <= 310) |
| 122 | { |
| 123 | printf("WARNING! BATTERY LOW!!"); |
| 124 | sleep(HZ*2); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* From the pcf50606 driver */ |
| 129 | extern unsigned char pcf50606_intregs[3]; |
| 130 | |
| 131 | /* From common.c */ |
| 132 | extern int line; |
| 133 | extern int remote_line; |
| 134 | |
| 135 | void main(void) |
| 136 | { |
| 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 | |
| 147 | /* We want to read the buttons as early as possible, before the user |
| 148 | releases the ON button */ |
| 149 | |
| 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); |
| 154 | |
| 155 | data = GPIO1_READ; |
| 156 | if ((data & 0x20) == 0) |
| 157 | on_button = true; |
| 158 | |
| 159 | if ((data & 0x40) == 0) |
| 160 | rc_on_button = true; |
| 161 | |
| 162 | /* Set the default state of the hard drive power to OFF */ |
| 163 | ide_power_enable(false); |
| 164 | |
| 165 | power_init(); |
| 166 | |
| 167 | /* Check the interrupt registers if it was an RTC alarm */ |
| 168 | rtc_alarm = (pcf50606_intregs[0] & 0x80)?true:false; |
| 169 | |
| 170 | /* Turn off if we believe the start was accidental */ |
| 171 | if(!(rtc_alarm || on_button || rc_on_button || |
| 172 | usb_detect() || charger_inserted())) { |
| 173 | __reset_cookie(); |
| 174 | power_off(); |
| 175 | } |
| 176 | |
| 177 | audiohw_reset(); |
| 178 | |
| 179 | /* Start with the main backlight OFF. */ |
| 180 | __backlight_init(); |
| 181 | __backlight_off(); |
| 182 | |
| 183 | __remote_backlight_on(); |
| 184 | |
| 185 | system_init(); |
| 186 | kernel_init(); |
| 187 | |
| 188 | /* Set up waitstates for the peripherals */ |
| 189 | set_cpu_frequency(0); /* PLL off */ |
| 190 | coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS); |
| 191 | set_irq_level(0); |
| 192 | |
Linus Nielsen Feltzing | 8448d3b | 2007-03-02 13:04:57 +0000 | [diff] [blame] | 193 | isp1362_init(); |
| 194 | |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 195 | adc_init(); |
| 196 | button_init(); |
| 197 | |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 198 | backlight_init(); |
| 199 | |
| 200 | lcd_init(); |
| 201 | lcd_remote_init(); |
| 202 | font_init(); |
| 203 | |
| 204 | lcd_setfont(FONT_SYSFIXED); |
| 205 | |
| 206 | printf("Rockbox boot loader"); |
| 207 | printf("Version %s", version); |
| 208 | |
| 209 | sleep(HZ/50); /* Allow the button driver to check the buttons */ |
| 210 | rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC) |
| 211 | || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC); |
| 212 | |
| 213 | check_battery(); |
| 214 | |
Steve Bavin | 88f249d | 2007-03-07 11:52:17 +0000 | [diff] [blame] | 215 | if(rtc_alarm) |
| 216 | printf("RTC alarm detected"); |
| 217 | |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 218 | /* Don't start if the Hold button is active on the device you |
| 219 | are starting with */ |
Steve Bavin | 88f249d | 2007-03-07 11:52:17 +0000 | [diff] [blame] | 220 | if ((on_button && button_hold()) || |
| 221 | (rc_on_button && remote_button_hold())) |
| 222 | { |
| 223 | hold_status = true; |
| 224 | } |
| 225 | if (hold_status && !rtc_alarm && !usb_detect() && !charger_inserted()) |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 226 | { |
| 227 | if (detect_original_firmware()) |
| 228 | { |
| 229 | printf("Hold switch on"); |
| 230 | shutdown(); |
| 231 | } |
| 232 | } |
| 233 | |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 234 | /* Holding REC while starting runs the original firmware */ |
| 235 | if (detect_original_firmware() && rec_button) |
| 236 | { |
| 237 | printf("Starting original firmware..."); |
| 238 | start_iriver_fw(); |
| 239 | } |
| 240 | |
| 241 | if(charger_inserted()) |
| 242 | { |
| 243 | const char charging_msg[] = "Charging..."; |
| 244 | const char complete_msg[] = "Charging complete"; |
| 245 | const char *msg; |
| 246 | int w, h; |
| 247 | bool blink_toggle = false; |
| 248 | bool request_start = false; |
| 249 | |
Linus Nielsen Feltzing | 8448d3b | 2007-03-02 13:04:57 +0000 | [diff] [blame] | 250 | cpu_idle_mode(true); |
| 251 | |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 252 | while(charger_inserted() && !request_start) |
| 253 | { |
| 254 | button = button_get_w_tmo(HZ); |
| 255 | |
| 256 | switch(button) |
| 257 | { |
| 258 | case BUTTON_ON: |
| 259 | request_start = true; |
| 260 | break; |
| 261 | |
| 262 | case BUTTON_NONE: /* Timeout */ |
| 263 | |
| 264 | if(charging_state()) |
| 265 | { |
| 266 | /* To be replaced with a nice animation */ |
| 267 | blink_toggle = !blink_toggle; |
| 268 | msg = charging_msg; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | blink_toggle = true; |
| 273 | msg = complete_msg; |
| 274 | } |
| 275 | |
| 276 | font_getstringsize(msg, &w, &h, FONT_SYSFIXED); |
| 277 | reset_screen(); |
| 278 | if(blink_toggle) |
| 279 | lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg); |
| 280 | |
| 281 | check_battery(); |
| 282 | break; |
| 283 | } |
| 284 | |
| 285 | if(usb_detect()) |
| 286 | request_start = true; |
| 287 | } |
| 288 | if(!request_start) |
| 289 | { |
| 290 | __reset_cookie(); |
| 291 | power_off(); |
| 292 | } |
Linus Nielsen Feltzing | 8448d3b | 2007-03-02 13:04:57 +0000 | [diff] [blame] | 293 | |
| 294 | cpu_idle_mode(false); |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | usb_init(); |
| 298 | |
| 299 | /* A hack to enter USB mode without using the USB thread */ |
| 300 | if(usb_detect()) |
| 301 | { |
| 302 | const char msg[] = "Bootloader USB mode"; |
| 303 | int w, h; |
| 304 | font_getstringsize(msg, &w, &h, FONT_SYSFIXED); |
| 305 | reset_screen(); |
| 306 | lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg); |
| 307 | lcd_update(); |
| 308 | |
| 309 | lcd_remote_puts(0, 3, msg); |
| 310 | lcd_remote_update(); |
| 311 | |
| 312 | ide_power_enable(true); |
| 313 | ata_enable(false); |
| 314 | sleep(HZ/20); |
| 315 | usb_enable(true); |
| 316 | cpu_idle_mode(true); |
| 317 | while (usb_detect()) |
| 318 | { |
| 319 | /* Print the battery status. */ |
| 320 | line = 0; |
| 321 | remote_line = 0; |
| 322 | check_battery(); |
| 323 | |
| 324 | ata_spin(); /* Prevent the drive from spinning down */ |
| 325 | sleep(HZ); |
| 326 | } |
| 327 | |
| 328 | cpu_idle_mode(false); |
| 329 | usb_enable(false); |
| 330 | |
| 331 | reset_screen(); |
| 332 | lcd_update(); |
| 333 | } |
| 334 | |
| 335 | rc = ata_init(); |
| 336 | if(rc) |
| 337 | { |
| 338 | reset_screen(); |
| 339 | printf("ATA error: %d", rc); |
| 340 | printf("Insert USB cable and press"); |
| 341 | printf("a button"); |
| 342 | while(!(button_get(true) & BUTTON_REL)); |
| 343 | } |
| 344 | |
| 345 | |
| 346 | disk_init(); |
| 347 | |
| 348 | rc = disk_mount_all(); |
| 349 | if (rc<=0) |
| 350 | { |
| 351 | reset_screen(); |
| 352 | printf("No partition found"); |
| 353 | while(button_get(true) != SYS_USB_CONNECTED) {}; |
| 354 | } |
| 355 | |
| 356 | printf("Loading firmware"); |
| 357 | i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE); |
| 358 | if(i < 0) |
Steve Bavin | 0e16bd3 | 2007-06-18 06:52:29 +0000 | [diff] [blame] | 359 | printf("Error: %s", strerror(i)); |
Linus Nielsen Feltzing | ff17905 | 2007-03-02 11:28:23 +0000 | [diff] [blame] | 360 | |
| 361 | if (i == EOK) |
| 362 | start_firmware(); |
| 363 | |
| 364 | if (!detect_original_firmware()) |
| 365 | { |
| 366 | printf("No firmware found on disk"); |
| 367 | sleep(HZ*2); |
| 368 | shutdown(); |
| 369 | } |
| 370 | else { |
| 371 | sleep(HZ*2); |
| 372 | start_iriver_fw(); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | /* These functions are present in the firmware library, but we reimplement |
| 377 | them here because the originals do a lot more than we want */ |
| 378 | void screen_dump(void) |
| 379 | { |
| 380 | } |
| 381 | |
| 382 | int usb_screen(void) |
| 383 | { |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | unsigned short *bidi_l2v(const unsigned char *str, int orientation) |
| 388 | { |
| 389 | static unsigned short utf16_buf[SCROLL_LINE_SIZE]; |
| 390 | unsigned short *target; |
| 391 | (void)orientation; |
| 392 | |
| 393 | target = utf16_buf; |
| 394 | |
| 395 | while (*str) |
| 396 | str = utf8decode(str, target++); |
| 397 | *target = 0; |
| 398 | return utf16_buf; |
| 399 | } |