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