Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2005 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> |
Miika Pekkarinen | e1eb91b | 2006-08-09 12:04:13 +0000 | [diff] [blame] | 23 | #include "inttypes.h" |
| 24 | #include "string.h" |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 25 | #include "cpu.h" |
| 26 | #include "system.h" |
| 27 | #include "lcd.h" |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 28 | #include "lcd-remote.h" |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 29 | #include "kernel.h" |
| 30 | #include "thread.h" |
| 31 | #include "ata.h" |
Linus Nielsen Feltzing | 01917ec | 2005-12-06 12:12:29 +0000 | [diff] [blame] | 32 | #include "usb.h" |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 33 | #include "disk.h" |
| 34 | #include "font.h" |
| 35 | #include "adc.h" |
| 36 | #include "backlight.h" |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 37 | #include "backlight-target.h" |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 38 | #include "button.h" |
| 39 | #include "panic.h" |
| 40 | #include "power.h" |
| 41 | #include "file.h" |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame] | 42 | #include "uda1380.h" |
Miika Pekkarinen | e1eb91b | 2006-08-09 12:04:13 +0000 | [diff] [blame] | 43 | #include "eeprom_settings.h" |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 44 | |
Linus Nielsen Feltzing | ce6527b | 2006-03-22 11:45:33 +0000 | [diff] [blame] | 45 | #include "pcf50606.h" |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 46 | #include "common.h" |
Linus Nielsen Feltzing | ce6527b | 2006-03-22 11:45:33 +0000 | [diff] [blame] | 47 | |
| 48 | #include <stdarg.h> |
| 49 | |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 50 | /* Maximum allowed firmware image size. 10MB is more than enough */ |
| 51 | #define MAX_LOADSIZE (10*1024*1024) |
| 52 | |
Linus Nielsen Feltzing | 62c768c | 2005-07-09 07:46:42 +0000 | [diff] [blame] | 53 | #define DRAM_START 0x31000000 |
| 54 | |
Miika Pekkarinen | 886c302 | 2007-01-13 08:16:02 +0000 | [diff] [blame] | 55 | #ifdef HAVE_EEPROM_SETTINGS |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 56 | static bool recovery_mode = false; |
Miika Pekkarinen | 886c302 | 2007-01-13 08:16:02 +0000 | [diff] [blame] | 57 | #endif |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 58 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 59 | int usb_screen(void) |
| 60 | { |
| 61 | return 0; |
| 62 | } |
| 63 | |
Linus Nielsen Feltzing | a2e8cf7 | 2005-07-15 07:42:25 +0000 | [diff] [blame] | 64 | char version[] = APPSVERSION; |
| 65 | |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 66 | /* Reset the cookie for the crt0 crash check */ |
| 67 | inline void __reset_cookie(void) |
| 68 | { |
| 69 | asm(" move.l #0,%d0"); |
| 70 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | ce6527b | 2006-03-22 11:45:33 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 73 | void start_iriver_fw(void) |
| 74 | { |
| 75 | asm(" move.w #0x2700,%sr"); |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 76 | __reset_cookie(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 77 | asm(" movec.l %d0,%vbr"); |
| 78 | asm(" move.l 0,%sp"); |
| 79 | asm(" lea.l 8,%a0"); |
| 80 | asm(" jmp (%a0)"); |
| 81 | } |
| 82 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 83 | void start_firmware(void) |
| 84 | { |
| 85 | asm(" move.w #0x2700,%sr"); |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 86 | __reset_cookie(); |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 87 | asm(" move.l %0,%%d0" :: "i"(DRAM_START)); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 88 | asm(" movec.l %d0,%vbr"); |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 89 | asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START)); |
| 90 | asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4))); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 91 | asm(" jmp (%a0)"); |
| 92 | } |
| 93 | |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 94 | #ifdef IRIVER_H100_SERIES |
| 95 | void start_flashed_romimage(void) |
| 96 | { |
| 97 | uint8_t *src = (uint8_t *)FLASH_ROMIMAGE_ENTRY; |
| 98 | int *reset_vector; |
| 99 | |
| 100 | if (!detect_flashed_romimage()) |
| 101 | return ; |
| 102 | |
| 103 | reset_vector = (int *)(&src[sizeof(struct flash_header)+4]); |
| 104 | |
| 105 | asm(" move.w #0x2700,%sr"); |
| 106 | __reset_cookie(); |
| 107 | |
| 108 | asm(" move.l %0,%%d0" :: "i"(DRAM_START)); |
| 109 | asm(" movec.l %d0,%vbr"); |
| 110 | asm(" move.l %0,%%sp" :: "m"(reset_vector[0])); |
| 111 | asm(" move.l %0,%%a0" :: "m"(reset_vector[1])); |
| 112 | asm(" jmp (%a0)"); |
| 113 | |
| 114 | /* Failure */ |
| 115 | power_off(); |
| 116 | } |
| 117 | |
| 118 | void start_flashed_ramimage(void) |
| 119 | { |
| 120 | struct flash_header hdr; |
| 121 | unsigned char *buf = (unsigned char *)DRAM_START; |
| 122 | uint8_t *src = (uint8_t *)FLASH_RAMIMAGE_ENTRY; |
| 123 | |
| 124 | if (!detect_flashed_ramimage()) |
| 125 | return; |
| 126 | |
| 127 | /* Load firmware from flash */ |
| 128 | cpu_boost(true); |
| 129 | memcpy(&hdr, src, sizeof(struct flash_header)); |
| 130 | src += sizeof(struct flash_header); |
| 131 | memcpy(buf, src, hdr.length); |
| 132 | cpu_boost(false); |
| 133 | |
| 134 | start_firmware(); |
| 135 | |
| 136 | /* Failure */ |
| 137 | power_off(); |
| 138 | } |
| 139 | #endif /* IRIVER_H100_SERIES */ |
| 140 | |
| 141 | void shutdown(void) |
| 142 | { |
| 143 | printf("Shutting down..."); |
| 144 | #ifdef HAVE_EEPROM_SETTINGS |
| 145 | /* Reset the rockbox crash check. */ |
| 146 | firmware_settings.bl_version = 0; |
| 147 | eeprom_settings_store(); |
| 148 | #endif |
| 149 | |
| 150 | /* We need to gracefully spin down the disk to prevent clicks. */ |
| 151 | if (ide_powered()) |
| 152 | { |
| 153 | /* Make sure ATA has been initialized. */ |
| 154 | ata_init(); |
| 155 | |
| 156 | /* And put the disk into sleep immediately. */ |
| 157 | ata_sleepnow(); |
| 158 | } |
| 159 | |
| 160 | sleep(HZ*2); |
| 161 | |
| 162 | /* Backlight OFF */ |
| 163 | __backlight_off(); |
| 164 | #ifdef HAVE_REMOTE_LCD |
| 165 | __remote_backlight_off(); |
| 166 | #endif |
| 167 | |
| 168 | __reset_cookie(); |
| 169 | power_off(); |
| 170 | } |
| 171 | |
| 172 | /* Print the battery voltage (and a warning message). */ |
| 173 | void check_battery(void) |
| 174 | { |
| 175 | int adc_battery, battery_voltage, batt_int, batt_frac; |
| 176 | |
| 177 | adc_battery = adc_read(ADC_BATTERY); |
| 178 | |
| 179 | battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000; |
| 180 | batt_int = battery_voltage / 100; |
| 181 | batt_frac = battery_voltage % 100; |
| 182 | |
| 183 | printf("Batt: %d.%02dV", batt_int, batt_frac); |
| 184 | |
| 185 | if (battery_voltage <= 310) |
| 186 | { |
| 187 | printf("WARNING! BATTERY LOW!!"); |
| 188 | sleep(HZ*2); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | #ifdef HAVE_EEPROM_SETTINGS |
| 193 | void initialize_eeprom(void) |
| 194 | { |
| 195 | if (detect_original_firmware()) |
| 196 | return ; |
| 197 | |
| 198 | if (!eeprom_settings_init()) |
| 199 | { |
| 200 | recovery_mode = true; |
| 201 | return ; |
| 202 | } |
| 203 | |
| 204 | /* If bootloader version has not been reset, disk might |
| 205 | * not be intact. */ |
| 206 | if (firmware_settings.bl_version || !firmware_settings.disk_clean) |
| 207 | { |
| 208 | firmware_settings.disk_clean = false; |
| 209 | recovery_mode = true; |
| 210 | } |
| 211 | |
| 212 | firmware_settings.bl_version = EEPROM_SETTINGS_BL_MINVER; |
| 213 | eeprom_settings_store(); |
| 214 | } |
| 215 | |
| 216 | void try_flashboot(void) |
| 217 | { |
| 218 | if (!firmware_settings.initialized) |
| 219 | return ; |
| 220 | |
| 221 | switch (firmware_settings.bootmethod) |
| 222 | { |
| 223 | case BOOT_DISK: |
| 224 | return; |
| 225 | |
| 226 | case BOOT_ROM: |
| 227 | start_flashed_romimage(); |
| 228 | recovery_mode = true; |
| 229 | break; |
| 230 | |
| 231 | case BOOT_RAM: |
| 232 | start_flashed_ramimage(); |
| 233 | recovery_mode = true; |
| 234 | break; |
| 235 | |
| 236 | default: |
| 237 | recovery_mode = true; |
| 238 | return; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | static const char *options[] = { |
| 243 | "Boot from disk", |
| 244 | "Boot RAM image", |
| 245 | "Boot ROM image", |
| 246 | "Shutdown" |
| 247 | }; |
| 248 | |
| 249 | #define FAILSAFE_OPTIONS 4 |
| 250 | void failsafe_menu(void) |
| 251 | { |
| 252 | int timeout = 15; |
| 253 | int option = 3; |
| 254 | int button; |
| 255 | int defopt = -1; |
| 256 | char buf[32]; |
| 257 | int i; |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 258 | extern int line; |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 259 | |
| 260 | reset_screen(); |
| 261 | printf("Bootloader %s", version); |
| 262 | check_battery(); |
| 263 | printf("========================="); |
| 264 | line += FAILSAFE_OPTIONS; |
| 265 | printf(""); |
| 266 | printf(" [NAVI] to confirm."); |
| 267 | printf(" [REC] to set as default."); |
| 268 | printf(""); |
| 269 | |
| 270 | if (firmware_settings.initialized) |
| 271 | { |
| 272 | defopt = firmware_settings.bootmethod; |
| 273 | if (defopt < 0 || defopt >= FAILSAFE_OPTIONS) |
| 274 | defopt = option; |
| 275 | } |
| 276 | |
| 277 | while (timeout > 0) |
| 278 | { |
| 279 | /* Draw the menu. */ |
| 280 | line = 3; |
| 281 | for (i = 0; i < FAILSAFE_OPTIONS; i++) |
| 282 | { |
| 283 | char *def = "[DEF]"; |
| 284 | char *arrow = "->"; |
| 285 | |
| 286 | if (i != defopt) |
| 287 | def = ""; |
| 288 | if (i != option) |
| 289 | arrow = " "; |
| 290 | |
| 291 | printf("%s %s %s", arrow, options[i], def); |
| 292 | } |
| 293 | |
| 294 | snprintf(buf, sizeof(buf), "Time left: %ds", timeout); |
| 295 | lcd_puts(0, 10, buf); |
| 296 | lcd_update(); |
| 297 | button = button_get_w_tmo(HZ); |
| 298 | |
| 299 | if (button == BUTTON_NONE) |
| 300 | { |
| 301 | timeout--; |
| 302 | continue ; |
| 303 | } |
| 304 | |
| 305 | timeout = 15; |
| 306 | /* Ignore the ON/PLAY -button because it can cause trouble |
| 307 | with the RTC alarm mod. */ |
| 308 | switch (button & ~(BUTTON_ON)) |
| 309 | { |
| 310 | case BUTTON_UP: |
| 311 | case BUTTON_RC_REW: |
| 312 | if (option > 0) |
| 313 | option--; |
| 314 | break ; |
| 315 | |
| 316 | case BUTTON_DOWN: |
| 317 | case BUTTON_RC_FF: |
| 318 | if (option < FAILSAFE_OPTIONS-1) |
| 319 | option++; |
| 320 | break ; |
| 321 | |
| 322 | case BUTTON_SELECT: |
| 323 | case BUTTON_RC_ON: |
| 324 | timeout = 0; |
| 325 | break ; |
| 326 | |
| 327 | case BUTTON_REC: |
| 328 | case BUTTON_RC_REC: |
| 329 | if (firmware_settings.initialized) |
| 330 | { |
| 331 | firmware_settings.bootmethod = option; |
| 332 | eeprom_settings_store(); |
| 333 | defopt = option; |
| 334 | } |
| 335 | break ; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | lcd_puts(0, 10, "Executing command..."); |
| 340 | lcd_update(); |
| 341 | sleep(HZ); |
| 342 | reset_screen(); |
| 343 | |
| 344 | switch (option) |
| 345 | { |
| 346 | case BOOT_DISK: |
| 347 | return ; |
| 348 | |
| 349 | case BOOT_RAM: |
| 350 | start_flashed_ramimage(); |
| 351 | printf("Image not found"); |
| 352 | break; |
| 353 | |
| 354 | case BOOT_ROM: |
| 355 | start_flashed_romimage(); |
| 356 | printf("Image not found"); |
| 357 | break; |
| 358 | } |
| 359 | |
| 360 | shutdown(); |
| 361 | } |
| 362 | #endif |
| 363 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 364 | void main(void) |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 365 | { |
| 366 | int i; |
| 367 | int rc; |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 368 | bool rc_on_button = false; |
| 369 | bool on_button = false; |
Miika Pekkarinen | e1eb91b | 2006-08-09 12:04:13 +0000 | [diff] [blame] | 370 | bool rec_button = false; |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 371 | bool hold_status = false; |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 372 | int data; |
Linus Nielsen Feltzing | 343c428 | 2007-02-28 08:40:37 +0000 | [diff] [blame^] | 373 | extern int line; /* From common.c */ |
| 374 | extern int remote_line; /* From common.c */ |
Linus Nielsen Feltzing | ec549ec | 2007-02-22 21:38:59 +0000 | [diff] [blame] | 375 | |
Linus Nielsen Feltzing | e628b5c | 2005-07-19 08:08:33 +0000 | [diff] [blame] | 376 | /* We want to read the buttons as early as possible, before the user |
| 377 | releases the ON button */ |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 378 | |
Linus Nielsen Feltzing | e628b5c | 2005-07-19 08:08:33 +0000 | [diff] [blame] | 379 | /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs |
| 380 | (The ON and Hold buttons on the main unit and the remote) */ |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame] | 381 | or_l(0x00100062, &GPIO1_FUNCTION); |
| 382 | and_l(~0x00100062, &GPIO1_ENABLE); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 383 | |
| 384 | data = GPIO1_READ; |
| 385 | if ((data & 0x20) == 0) |
| 386 | on_button = true; |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 387 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 388 | if ((data & 0x40) == 0) |
| 389 | rc_on_button = true; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 390 | |
Linus Nielsen Feltzing | e628b5c | 2005-07-19 08:08:33 +0000 | [diff] [blame] | 391 | /* Set the default state of the hard drive power to OFF */ |
| 392 | ide_power_enable(false); |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 393 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 394 | power_init(); |
Linus Nielsen Feltzing | e628b5c | 2005-07-19 08:08:33 +0000 | [diff] [blame] | 395 | |
Linus Nielsen Feltzing | bbe919b | 2005-11-19 01:33:28 +0000 | [diff] [blame] | 396 | /* Turn off if neither ON button is pressed */ |
Linus Nielsen Feltzing | 54da924 | 2006-01-10 23:11:57 +0000 | [diff] [blame] | 397 | if(!(on_button || rc_on_button || usb_detect())) |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 398 | { |
| 399 | __reset_cookie(); |
Linus Nielsen Feltzing | bbe919b | 2005-11-19 01:33:28 +0000 | [diff] [blame] | 400 | power_off(); |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 401 | } |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 402 | |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 403 | /* Start with the main backlight OFF. */ |
| 404 | __backlight_init(); |
| 405 | __backlight_off(); |
| 406 | |
Linus Nielsen Feltzing | bbe919b | 2005-11-19 01:33:28 +0000 | [diff] [blame] | 407 | /* Remote backlight ON */ |
| 408 | #ifdef HAVE_REMOTE_LCD |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 409 | __remote_backlight_on(); |
Linus Nielsen Feltzing | b68cb9b | 2005-12-03 00:54:59 +0000 | [diff] [blame] | 410 | #endif |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 411 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 412 | system_init(); |
| 413 | kernel_init(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 414 | |
| 415 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ |
| 416 | /* Set up waitstates for the peripherals */ |
| 417 | set_cpu_frequency(0); /* PLL off */ |
Michael Sevakis | 0f5cb94 | 2006-11-06 18:07:30 +0000 | [diff] [blame] | 418 | #ifdef CPU_COLDFIRE |
| 419 | coldfire_set_pllcr_audio_bits(DEFAULT_PLLCR_AUDIO_BITS); |
| 420 | #endif |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 421 | #endif |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 422 | set_irq_level(0); |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame] | 423 | |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 424 | #ifdef HAVE_EEPROM_SETTINGS |
| 425 | initialize_eeprom(); |
| 426 | #endif |
| 427 | |
| 428 | adc_init(); |
| 429 | button_init(); |
| 430 | |
| 431 | if ((on_button && button_hold()) || |
| 432 | (rc_on_button && remote_button_hold())) |
| 433 | { |
| 434 | hold_status = true; |
| 435 | } |
| 436 | |
| 437 | /* Power on the hard drive early, to speed up the loading. |
| 438 | Some H300 don't like this, so we only do it for the H100 */ |
| 439 | #ifndef IRIVER_H300_SERIES |
Miika Pekkarinen | 20171c1 | 2007-01-13 09:08:18 +0000 | [diff] [blame] | 440 | if (!hold_status |
| 441 | # ifdef HAVE_EEPROM_SETTINGS |
| 442 | && !recovery_mode |
| 443 | # endif |
| 444 | ) |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 445 | { |
| 446 | ide_power_enable(true); |
| 447 | } |
| 448 | |
Miika Pekkarinen | 20171c1 | 2007-01-13 09:08:18 +0000 | [diff] [blame] | 449 | # ifdef EEPROM_SETTINGS |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 450 | if (!hold_status && !usb_detect() && !recovery_mode) |
| 451 | try_flashboot(); |
Miika Pekkarinen | 20171c1 | 2007-01-13 09:08:18 +0000 | [diff] [blame] | 452 | # endif |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 453 | #endif |
| 454 | |
| 455 | backlight_init(); |
Linus Nielsen Feltzing | 2fcd1b0 | 2006-02-04 23:09:56 +0000 | [diff] [blame] | 456 | #ifdef HAVE_UDA1380 |
Marcoen Hirschberg | 6c2e1b8 | 2006-12-06 10:36:20 +0000 | [diff] [blame] | 457 | audiohw_reset(); |
Linus Nielsen Feltzing | 2fcd1b0 | 2006-02-04 23:09:56 +0000 | [diff] [blame] | 458 | #endif |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 459 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 460 | lcd_init(); |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 461 | #ifdef HAVE_REMOTE_LCD |
| 462 | lcd_remote_init(); |
| 463 | #endif |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 464 | font_init(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 465 | |
| 466 | lcd_setfont(FONT_SYSFIXED); |
| 467 | |
Linus Nielsen Feltzing | ce6527b | 2006-03-22 11:45:33 +0000 | [diff] [blame] | 468 | printf("Rockbox boot loader"); |
| 469 | printf("Version %s", version); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 470 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 471 | sleep(HZ/50); /* Allow the button driver to check the buttons */ |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 472 | rec_button = ((button_status() & BUTTON_REC) == BUTTON_REC) |
Miika Pekkarinen | e1eb91b | 2006-08-09 12:04:13 +0000 | [diff] [blame] | 473 | || ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC); |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 474 | |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 475 | check_battery(); |
| 476 | |
Miika Pekkarinen | 22860a9 | 2006-08-11 19:59:16 +0000 | [diff] [blame] | 477 | /* Don't start if the Hold button is active on the device you |
| 478 | are starting with */ |
Miika Pekkarinen | 20171c1 | 2007-01-13 09:08:18 +0000 | [diff] [blame] | 479 | if (!usb_detect() && (hold_status |
| 480 | #ifdef HAVE_EEPROM_SETTINGS |
| 481 | || recovery_mode |
| 482 | #endif |
| 483 | )) |
Miika Pekkarinen | 22860a9 | 2006-08-11 19:59:16 +0000 | [diff] [blame] | 484 | { |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 485 | if (detect_original_firmware()) |
| 486 | { |
| 487 | printf("Hold switch on"); |
| 488 | shutdown(); |
| 489 | } |
| 490 | |
Miika Pekkarinen | 20171c1 | 2007-01-13 09:08:18 +0000 | [diff] [blame] | 491 | #ifdef HAVE_EEPROM_SETTINGS |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 492 | failsafe_menu(); |
Miika Pekkarinen | 22860a9 | 2006-08-11 19:59:16 +0000 | [diff] [blame] | 493 | #endif |
Miika Pekkarinen | 22860a9 | 2006-08-11 19:59:16 +0000 | [diff] [blame] | 494 | } |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 495 | |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 496 | /* Holding REC while starting runs the original firmware */ |
| 497 | if (detect_original_firmware() && rec_button) |
Miika Pekkarinen | e1eb91b | 2006-08-09 12:04:13 +0000 | [diff] [blame] | 498 | { |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 499 | printf("Starting original firmware..."); |
| 500 | start_iriver_fw(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Linus Nielsen Feltzing | 01917ec | 2005-12-06 12:12:29 +0000 | [diff] [blame] | 503 | usb_init(); |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 504 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 505 | /* A hack to enter USB mode without using the USB thread */ |
| 506 | if(usb_detect()) |
| 507 | { |
Linus Nielsen Feltzing | 01917ec | 2005-12-06 12:12:29 +0000 | [diff] [blame] | 508 | const char msg[] = "Bootloader USB mode"; |
| 509 | int w, h; |
| 510 | font_getstringsize(msg, &w, &h, FONT_SYSFIXED); |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 511 | reset_screen(); |
Linus Nielsen Feltzing | 01917ec | 2005-12-06 12:12:29 +0000 | [diff] [blame] | 512 | lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 513 | lcd_update(); |
| 514 | |
Linus Nielsen Feltzing | 343c428 | 2007-02-28 08:40:37 +0000 | [diff] [blame^] | 515 | #ifdef HAVE_REMOTE_LCD |
| 516 | lcd_remote_puts(0, 3, msg); |
| 517 | lcd_remote_update(); |
| 518 | #endif |
| 519 | |
Peter D'Hoye | c4a59a2 | 2006-08-15 22:54:06 +0000 | [diff] [blame] | 520 | #ifdef HAVE_EEPROM_SETTINGS |
Miika Pekkarinen | e1eb91b | 2006-08-09 12:04:13 +0000 | [diff] [blame] | 521 | if (firmware_settings.initialized) |
| 522 | { |
| 523 | firmware_settings.disk_clean = false; |
| 524 | eeprom_settings_store(); |
| 525 | } |
| 526 | #endif |
Linus Nielsen Feltzing | 237ff9e | 2007-02-28 00:06:35 +0000 | [diff] [blame] | 527 | ide_power_enable(true); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 528 | ata_enable(false); |
Linus Nielsen Feltzing | 237ff9e | 2007-02-28 00:06:35 +0000 | [diff] [blame] | 529 | sleep(HZ/20); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 530 | usb_enable(true); |
Linus Nielsen Feltzing | 01917ec | 2005-12-06 12:12:29 +0000 | [diff] [blame] | 531 | cpu_idle_mode(true); |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 532 | while (usb_detect()) |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 533 | { |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 534 | /* Print the battery status. */ |
| 535 | line = 0; |
Linus Nielsen Feltzing | 343c428 | 2007-02-28 08:40:37 +0000 | [diff] [blame^] | 536 | remote_line = 0; |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 537 | check_battery(); |
| 538 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 539 | ata_spin(); /* Prevent the drive from spinning down */ |
| 540 | sleep(HZ); |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 541 | |
Linus Nielsen Feltzing | e628b5c | 2005-07-19 08:08:33 +0000 | [diff] [blame] | 542 | /* Backlight OFF */ |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 543 | __backlight_off(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Linus Nielsen Feltzing | 01917ec | 2005-12-06 12:12:29 +0000 | [diff] [blame] | 546 | cpu_idle_mode(false); |
Linus Nielsen Feltzing | 1310390 | 2005-07-19 11:03:00 +0000 | [diff] [blame] | 547 | usb_enable(false); |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 548 | |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 549 | reset_screen(); |
Linus Nielsen Feltzing | 1310390 | 2005-07-19 11:03:00 +0000 | [diff] [blame] | 550 | lcd_update(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 551 | } |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 552 | |
Linus Nielsen Feltzing | 237ff9e | 2007-02-28 00:06:35 +0000 | [diff] [blame] | 553 | rc = ata_init(); |
| 554 | if(rc) |
| 555 | { |
| 556 | reset_screen(); |
| 557 | printf("ATA error: %d", rc); |
| 558 | printf("Insert USB cable and press"); |
| 559 | printf("a button"); |
| 560 | while(!(button_get(true) & BUTTON_REL)); |
| 561 | } |
| 562 | |
| 563 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 564 | disk_init(); |
| 565 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 566 | rc = disk_mount_all(); |
| 567 | if (rc<=0) |
| 568 | { |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 569 | reset_screen(); |
Linus Nielsen Feltzing | ce6527b | 2006-03-22 11:45:33 +0000 | [diff] [blame] | 570 | printf("No partition found"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 571 | while(button_get(true) != SYS_USB_CONNECTED) {}; |
| 572 | } |
| 573 | |
Linus Nielsen Feltzing | ce6527b | 2006-03-22 11:45:33 +0000 | [diff] [blame] | 574 | printf("Loading firmware"); |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 575 | i = load_firmware((unsigned char *)DRAM_START, BOOTFILE, MAX_LOADSIZE); |
Linus Nielsen Feltzing | 343c428 | 2007-02-28 08:40:37 +0000 | [diff] [blame^] | 576 | if(i < 0) |
| 577 | printf("Error: %d", strerror(i)); |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 578 | |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 579 | if (i == EOK) |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 580 | start_firmware(); |
Michael Sevakis | 9e8fe0e | 2006-10-30 11:33:38 +0000 | [diff] [blame] | 581 | |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 582 | if (!detect_original_firmware()) |
Miika Pekkarinen | 973ee5d | 2006-09-04 16:06:11 +0000 | [diff] [blame] | 583 | { |
| 584 | printf("No firmware found on disk"); |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 585 | sleep(HZ*2); |
Miika Pekkarinen | 35b0c3f | 2007-01-12 20:45:37 +0000 | [diff] [blame] | 586 | shutdown(); |
Miika Pekkarinen | 973ee5d | 2006-09-04 16:06:11 +0000 | [diff] [blame] | 587 | } |
Linus Nielsen Feltzing | 343c428 | 2007-02-28 08:40:37 +0000 | [diff] [blame^] | 588 | else { |
| 589 | sleep(HZ*2); |
Miika Pekkarinen | 973ee5d | 2006-09-04 16:06:11 +0000 | [diff] [blame] | 590 | start_iriver_fw(); |
Linus Nielsen Feltzing | 343c428 | 2007-02-28 08:40:37 +0000 | [diff] [blame^] | 591 | } |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* These functions are present in the firmware library, but we reimplement |
| 595 | them here because the originals do a lot more than we want */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 596 | void screen_dump(void) |
| 597 | { |
| 598 | } |