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