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