Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
Marcin Bukat | 082c7d3 | 2010-10-22 12:28:43 +0000 | [diff] [blame] | 8 | * $Id$ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 9 | * |
| 10 | * Copyright (C) 2010 Marcin Bukat |
| 11 | * |
| 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. |
| 16 | * |
| 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 18 | * KIND, either express or implied. |
| 19 | * |
| 20 | ****************************************************************************/ |
| 21 | #include "config.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
| 25 | #include "inttypes.h" |
| 26 | #include "string.h" |
| 27 | #include "cpu.h" |
| 28 | #include "system.h" |
| 29 | #include "lcd.h" |
| 30 | #include "kernel.h" |
| 31 | #include "thread.h" |
| 32 | #include "storage.h" |
| 33 | #include "usb.h" |
| 34 | #include "disk.h" |
| 35 | #include "font.h" |
| 36 | #include "adc.h" |
| 37 | #include "backlight.h" |
| 38 | #include "backlight-target.h" |
| 39 | #include "button.h" |
| 40 | #include "panic.h" |
| 41 | #include "power.h" |
| 42 | #include "powermgmt.h" |
| 43 | #include "file.h" |
| 44 | |
| 45 | #include "common.h" |
Rafaël Carré | 5d236b2 | 2010-05-27 09:41:46 +0000 | [diff] [blame] | 46 | #include "version.h" |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 47 | |
| 48 | #include <stdarg.h> |
| 49 | |
| 50 | /* Maximum allowed firmware image size. 10MB is more than enough */ |
| 51 | #define MAX_LOADSIZE (10*1024*1024) |
| 52 | |
| 53 | #define DRAM_START 0x31000000 |
| 54 | |
| 55 | #define BOOTMENU_TIMEOUT (10*HZ) |
| 56 | #define BOOTMENU_OPTIONS 3 |
| 57 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 58 | #define EVENT_NONE 0x00 |
| 59 | #define EVENT_ON 0x01 |
| 60 | #define EVENT_AC 0x02 |
| 61 | #define EVENT_USB 0x04 |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 62 | #define EVENT_RTC 0x08 |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 63 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 64 | /* From common.c */ |
| 65 | extern int line; |
| 66 | static const char *bootmenu_options[] = { |
| 67 | "Boot rockbox", |
| 68 | "Boot MPIO firmware", |
| 69 | "Shutdown" |
| 70 | }; |
| 71 | |
| 72 | enum option_t { |
| 73 | rockbox, |
| 74 | mpio_firmware, |
| 75 | shutdown |
| 76 | }; |
| 77 | |
| 78 | int usb_screen(void) |
| 79 | { |
| 80 | return 0; |
| 81 | } |
| 82 | |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 83 | /* return true if charger is present */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 84 | static inline bool _charger_inserted(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 85 | { |
| 86 | return (GPIO1_READ & (1<<14)) ? false : true; |
| 87 | } |
| 88 | |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 89 | /* returns true if end of charge condition is reached */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 90 | static inline bool _battery_full(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 91 | { |
| 92 | return (GPIO_READ & (1<<30)) ? true : false; |
| 93 | } |
| 94 | |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 95 | #ifdef MPIO_HD300 |
| 96 | /* returns true if startup is due to RTC alarm */ |
| 97 | static inline bool _rtc_alarm(void) |
| 98 | { |
| 99 | if ( (GPIO1_READ & (1<<4)) && (GPIO1_READ & (1<<5)) ) |
| 100 | return false; |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | #endif |
| 105 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 106 | /* Reset the cookie for the crt0 crash check */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 107 | static inline void __reset_cookie(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 108 | { |
| 109 | asm(" move.l #0,%d0"); |
| 110 | asm(" move.l %d0,0x10017ffc"); |
| 111 | } |
| 112 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 113 | static void start_rockbox(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 114 | { |
| 115 | adc_close(); |
| 116 | asm(" move.w #0x2700,%sr"); |
| 117 | __reset_cookie(); |
| 118 | asm(" move.l %0,%%d0" :: "i"(DRAM_START)); |
| 119 | asm(" movec.l %d0,%vbr"); |
| 120 | asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START)); |
| 121 | asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4))); |
| 122 | asm(" jmp (%a0)"); |
| 123 | } |
| 124 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 125 | static void start_mpio_firmware(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 126 | { |
| 127 | asm(" move.w #0x2700,%sr"); |
| 128 | __reset_cookie(); |
| 129 | asm(" movec.l %d0,%vbr"); |
| 130 | asm(" move.l 0,%sp"); |
| 131 | asm(" jmp 8"); |
| 132 | } |
| 133 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 134 | static void __shutdown(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 135 | { |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 136 | if (_charger_inserted()) |
| 137 | /* if AC power do nothing */ |
| 138 | return; |
| 139 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 140 | /* We need to gracefully spin down the disk to prevent clicks. */ |
| 141 | if (ide_powered()) |
| 142 | { |
| 143 | /* Make sure ATA has been initialized. */ |
| 144 | storage_init(); |
| 145 | |
| 146 | /* And put the disk into sleep immediately. */ |
| 147 | storage_sleepnow(); |
| 148 | } |
| 149 | |
| 150 | /* Backlight OFF */ |
| 151 | _backlight_off(); |
| 152 | __reset_cookie(); |
| 153 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 154 | power_off(); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /* Print the battery voltage (and a warning message). */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 158 | static void check_battery(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 159 | { |
| 160 | |
| 161 | int battery_voltage, batt_int, batt_frac; |
| 162 | |
| 163 | battery_voltage = battery_adc_voltage(); |
| 164 | batt_int = battery_voltage / 1000; |
| 165 | batt_frac = (battery_voltage % 1000) / 10; |
| 166 | |
| 167 | printf("Battery: %d.%02dV", batt_int, batt_frac); |
| 168 | |
| 169 | if (battery_voltage <= 3500) |
| 170 | { |
| 171 | printf("WARNING! BATTERY LOW!!"); |
| 172 | sleep(HZ*2); |
| 173 | } |
| 174 | |
| 175 | } |
| 176 | |
| 177 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 178 | static void lcd_putstring_centered(const char *string) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 179 | { |
| 180 | int w,h; |
| 181 | font_getstringsize(string, &w, &h, FONT_SYSFIXED); |
| 182 | lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, string); |
| 183 | } |
| 184 | |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 185 | /* This function initializes ATA driver, mounts partitions, |
| 186 | * loads rockbox image from disk to ram and finally |
| 187 | * jumps to entry point in ram |
| 188 | */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 189 | static void rb_boot(void) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 190 | { |
| 191 | int rc; |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 192 | |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 193 | /* boost to speedup rb image loading */ |
| 194 | cpu_boost(true); |
| 195 | |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 196 | reset_screen(); |
Marcin Bukat | 15a0a3c | 2010-11-28 23:10:09 +0000 | [diff] [blame] | 197 | printf("Rockbox boot loader"); |
| 198 | printf("Version " RBVERSION); |
| 199 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 200 | rc = storage_init(); |
| 201 | if(rc) |
| 202 | { |
| 203 | printf("ATA error: %d", rc); |
| 204 | sleep(HZ*5); |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | disk_init(); |
| 209 | |
| 210 | rc = disk_mount_all(); |
| 211 | if (rc<=0) |
| 212 | { |
| 213 | printf("No partition found"); |
| 214 | sleep(HZ*5); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | printf("Loading firmware"); |
| 219 | |
| 220 | rc = load_firmware((unsigned char *)DRAM_START, |
| 221 | BOOTFILE, MAX_LOADSIZE); |
| 222 | |
| 223 | if (rc < EOK) |
| 224 | { |
| 225 | printf("Error!"); |
| 226 | printf("Can't load " BOOTFILE ": "); |
| 227 | printf("Result: %s", strerror(rc)); |
| 228 | sleep(HZ*5); |
| 229 | return; |
| 230 | } |
| 231 | |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 232 | cpu_boost(false); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 233 | start_rockbox(); |
| 234 | } |
| 235 | |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 236 | /* This function prints small bootmenu where |
| 237 | * you can choose to boot OF, rockbox or just shutdown |
| 238 | */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 239 | static void bootmenu(void) |
| 240 | { |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 241 | enum option_t i; |
| 242 | enum option_t option = rockbox; |
| 243 | int button; |
| 244 | const char select[] = "->"; |
| 245 | long start_tick = current_tick; |
| 246 | |
| 247 | /* backbone of menu */ |
| 248 | /* run the loader */ |
| 249 | printf("Rockbox boot loader"); |
Rafaël Carré | 5d236b2 | 2010-05-27 09:41:46 +0000 | [diff] [blame] | 250 | printf("Ver: " RBVERSION); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 251 | |
| 252 | check_battery(); |
| 253 | |
| 254 | printf(""); |
| 255 | printf("========================="); |
| 256 | |
| 257 | line += BOOTMENU_OPTIONS+2; /* skip lines */ |
| 258 | |
| 259 | printf("========================="); |
| 260 | printf(""); |
Marcin Bukat | 650a294 | 2010-11-02 10:51:04 +0000 | [diff] [blame] | 261 | printf(" [FF] [REW] to move "); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 262 | printf(" [PLAY] to confirm "); |
| 263 | |
| 264 | /* content of menu and keys handling */ |
| 265 | while (TIME_BEFORE(current_tick,start_tick + BOOTMENU_TIMEOUT)) |
| 266 | { |
| 267 | /* Draw the menu. */ |
| 268 | line = 6; /* move below header */ |
| 269 | |
| 270 | for (i=0;i<BOOTMENU_OPTIONS;i++) |
| 271 | { |
| 272 | if (i != option) |
| 273 | printf(" %s",bootmenu_options[i]); |
| 274 | else |
| 275 | printf("%s %s",select,bootmenu_options[i]); |
| 276 | } |
| 277 | |
| 278 | line = 15; |
| 279 | |
| 280 | printf("Time left: %ds",(BOOTMENU_TIMEOUT - |
| 281 | (current_tick - start_tick))/HZ); |
| 282 | |
| 283 | lcd_update(); |
| 284 | |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 285 | button = BUTTON_NONE; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 286 | button = button_get_w_tmo(HZ); |
| 287 | |
| 288 | switch (button) |
| 289 | { |
Marcin Bukat | 650a294 | 2010-11-02 10:51:04 +0000 | [diff] [blame] | 290 | case BUTTON_REW: |
Marcin Bukat | 9a24892 | 2010-11-26 23:28:08 +0000 | [diff] [blame] | 291 | #ifdef MPIO_HD200 |
Marcin Bukat | 650a294 | 2010-11-02 10:51:04 +0000 | [diff] [blame] | 292 | case BUTTON_RC_REW: |
Marcin Bukat | 9a24892 | 2010-11-26 23:28:08 +0000 | [diff] [blame] | 293 | #endif |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 294 | if (option > rockbox) |
| 295 | option--; |
| 296 | else |
| 297 | option = shutdown; |
| 298 | break; |
| 299 | |
Marcin Bukat | 650a294 | 2010-11-02 10:51:04 +0000 | [diff] [blame] | 300 | case BUTTON_FF: |
Marcin Bukat | 9a24892 | 2010-11-26 23:28:08 +0000 | [diff] [blame] | 301 | #ifdef MPIO_HD200 |
Marcin Bukat | 650a294 | 2010-11-02 10:51:04 +0000 | [diff] [blame] | 302 | case BUTTON_RC_FF: |
Marcin Bukat | 9a24892 | 2010-11-26 23:28:08 +0000 | [diff] [blame] | 303 | #endif |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 304 | if (option < shutdown) |
| 305 | option++; |
| 306 | else |
| 307 | option = rockbox; |
| 308 | break; |
| 309 | |
| 310 | case BUTTON_PLAY: |
Marcin Bukat | 9a24892 | 2010-11-26 23:28:08 +0000 | [diff] [blame] | 311 | #ifdef MPIO_HD200 |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 312 | case BUTTON_RC_PLAY: |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 313 | case (BUTTON_PLAY|BUTTON_REC): |
Marcin Bukat | 9a24892 | 2010-11-26 23:28:08 +0000 | [diff] [blame] | 314 | #endif |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 315 | reset_screen(); |
| 316 | |
| 317 | switch (option) |
| 318 | { |
| 319 | case rockbox: |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 320 | rb_boot(); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 321 | break; |
| 322 | |
| 323 | case mpio_firmware: |
| 324 | start_mpio_firmware(); |
| 325 | break; |
| 326 | |
| 327 | default: |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 328 | __shutdown(); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | /* timeout */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | void main(void) |
| 337 | { |
| 338 | /* messages */ |
| 339 | const char usb_connect_msg[] = "Bootloader USB mode"; |
| 340 | const char charging_msg[] = "Charging..."; |
| 341 | const char complete_msg[] = "Charging complete"; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 342 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 343 | /* helper variable for messages */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 344 | bool blink_toggle = false; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 345 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 346 | int button; |
Marcin Bukat | 67feb81 | 2011-01-03 13:11:48 +0000 | [diff] [blame^] | 347 | |
| 348 | /* hold status variables |
| 349 | * this two must have different |
| 350 | * values in the begining |
| 351 | */ |
| 352 | bool hold = false; |
| 353 | bool last_hold = true; |
| 354 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 355 | unsigned int event = EVENT_NONE; |
| 356 | unsigned int last_event = EVENT_NONE; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 357 | |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 358 | /* this is default mode after power_init() */ |
| 359 | bool high_current_charging = true; |
| 360 | |
Marcin Bukat | 38edf67 | 2010-10-22 12:11:58 +0000 | [diff] [blame] | 361 | /* setup GPIOs related to power functions */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 362 | power_init(); |
| 363 | |
| 364 | system_init(); |
| 365 | kernel_init(); |
| 366 | |
Marcin Bukat | 38edf67 | 2010-10-22 12:11:58 +0000 | [diff] [blame] | 367 | /* run at 45MHz */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 368 | set_cpu_frequency(CPUFREQ_NORMAL); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 369 | |
Marcin Bukat | 38edf67 | 2010-10-22 12:11:58 +0000 | [diff] [blame] | 370 | /* IRQs are needed by button driver */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 371 | enable_irq(); |
Marcin Bukat | 38edf67 | 2010-10-22 12:11:58 +0000 | [diff] [blame] | 372 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 373 | lcd_init(); |
| 374 | |
Marcin Bukat | 67feb81 | 2011-01-03 13:11:48 +0000 | [diff] [blame^] | 375 | /* setup font system */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 376 | font_init(); |
| 377 | lcd_setfont(FONT_SYSFIXED); |
Marcin Bukat | 38edf67 | 2010-10-22 12:11:58 +0000 | [diff] [blame] | 378 | |
Marcin Bukat | 67feb81 | 2011-01-03 13:11:48 +0000 | [diff] [blame^] | 379 | /* buttons reading init */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 380 | adc_init(); |
| 381 | button_init(); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 382 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 383 | usb_init(); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 384 | cpu_idle_mode(true); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 385 | |
Marcin Bukat | 67feb81 | 2011-01-03 13:11:48 +0000 | [diff] [blame^] | 386 | /* lowlevel init only */ |
| 387 | _backlight_init(); |
| 388 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 389 | /* Handle wakeup event. Possibilities are: |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 390 | * RTC alarm (HD300) |
| 391 | * ON button (PLAY or RC_PLAY on HD200) |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 392 | * USB insert |
| 393 | * AC charger plug |
| 394 | */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 395 | while(1) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 396 | { |
Marcin Bukat | 67feb81 | 2011-01-03 13:11:48 +0000 | [diff] [blame^] | 397 | /* check hold status */ |
| 398 | hold = button_hold(); |
| 399 | |
| 400 | /* backlight handling |
| 401 | * change only on hold toggle */ |
| 402 | if ( hold != last_hold ) |
| 403 | { |
| 404 | if ( hold ) |
| 405 | _backlight_hw_off(); |
| 406 | else |
| 407 | _backlight_hw_on(); |
| 408 | |
| 409 | last_hold = hold; |
| 410 | } |
| 411 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 412 | /* read buttons */ |
| 413 | event = EVENT_NONE; |
| 414 | button = button_get_w_tmo(HZ); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 415 | |
Marcin Bukat | 9a24892 | 2010-11-26 23:28:08 +0000 | [diff] [blame] | 416 | if ( (button & BUTTON_PLAY) |
| 417 | #ifdef MPIO_HD200 |
| 418 | || (button & BUTTON_RC_PLAY) |
| 419 | #endif |
| 420 | ) |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 421 | event |= EVENT_ON; |
| 422 | |
| 423 | if ( usb_detect() == USB_INSERTED ) |
| 424 | event |= EVENT_USB; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 425 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 426 | if ( _charger_inserted() ) |
| 427 | event |= EVENT_AC; |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 428 | #ifdef MPIO_HD300 |
| 429 | if ( _rtc_alarm() ) |
| 430 | event |= EVENT_RTC; |
| 431 | #endif |
Marcin Bukat | 67feb81 | 2011-01-03 13:11:48 +0000 | [diff] [blame^] | 432 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 433 | reset_screen(); |
| 434 | switch (event) |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 435 | { |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 436 | #ifdef MPIO_HD300 |
| 437 | case EVENT_RTC: |
| 438 | case (EVENT_RTC | EVENT_ON): |
| 439 | /* start regardles of buttons state */ |
| 440 | rb_boot(); |
| 441 | break; |
| 442 | #endif |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 443 | case EVENT_ON: |
| 444 | case (EVENT_ON | EVENT_AC): |
| 445 | /* hold is handled in button driver */ |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 446 | cpu_idle_mode(false); |
| 447 | ide_power_enable(true); |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 448 | |
Marcin Bukat | a73102f | 2010-12-06 10:24:29 +0000 | [diff] [blame] | 449 | if (button & BUTTON_REC) |
| 450 | bootmenu(); |
| 451 | else |
| 452 | rb_boot(); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 453 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 454 | break; |
| 455 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 456 | case EVENT_AC: |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 457 | /* AC plug in */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 458 | if (!(last_event & EVENT_AC)) |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 459 | { |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 460 | /* reset charging circuit */ |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 461 | and_l(~(1<<23), &GPIO_ENABLE); |
| 462 | } |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 463 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 464 | /* USB unplug */ |
| 465 | if (last_event & EVENT_USB) |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 466 | { |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 467 | usb_enable(false); |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 468 | sleep(HZ); |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 469 | ide_power_enable(false); |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 470 | sleep(HZ); |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 471 | } |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 472 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 473 | if(!_battery_full()) |
| 474 | { |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 475 | if (blink_toggle) |
| 476 | lcd_putstring_centered(charging_msg); |
| 477 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 478 | blink_toggle = !blink_toggle; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 479 | } |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 480 | else /* end of charge condition */ |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 481 | { |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 482 | /* put LTC1733 into shutdown mode */ |
| 483 | or_l((1<<23), &GPIO_ENABLE); |
| 484 | |
| 485 | if (high_current_charging) |
| 486 | { |
| 487 | /* switch to low current mode */ |
| 488 | and_l(~(1<<15), &GPIO_OUT); |
| 489 | |
| 490 | /* reset charging circuit */ |
| 491 | and_l(~(1<<23), &GPIO_ENABLE); |
| 492 | |
| 493 | high_current_charging = false; |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | lcd_putstring_centered(complete_msg); |
| 498 | } |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 499 | } |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 500 | check_battery(); |
| 501 | break; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 502 | |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 503 | case EVENT_USB: |
| 504 | case (EVENT_USB | EVENT_AC): |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 505 | /* AC plug in while in USB mode */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 506 | if (!(last_event & EVENT_AC)) |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 507 | { |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 508 | /* reset charger circuit */ |
Marcin Bukat | cd87971 | 2010-06-14 10:42:45 +0000 | [diff] [blame] | 509 | and_l(~(1<<23), &GPIO_ENABLE); |
| 510 | } |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 511 | |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 512 | /* USB plug in */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 513 | if (!(last_event & EVENT_USB)) |
| 514 | { |
| 515 | /* init USB */ |
| 516 | ide_power_enable(true); |
| 517 | sleep(HZ/20); |
Marcin Bukat | 0358e7b | 2010-12-09 11:31:08 +0000 | [diff] [blame] | 518 | ata_enable(false); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 519 | usb_enable(true); |
| 520 | } |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 521 | |
| 522 | /* display blinking USB indicator */ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 523 | line = 0; |
| 524 | |
| 525 | if (blink_toggle) |
| 526 | lcd_putstring_centered(usb_connect_msg); |
| 527 | |
| 528 | check_battery(); |
| 529 | blink_toggle = !blink_toggle; |
| 530 | storage_spin(); |
| 531 | break; |
| 532 | |
| 533 | default: |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 534 | /* USB unplug */ |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 535 | if (last_event & EVENT_USB) |
| 536 | { |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 537 | /* disable USB */ |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 538 | usb_enable(false); |
Marcin Bukat | 0358e7b | 2010-12-09 11:31:08 +0000 | [diff] [blame] | 539 | ata_enable(true); |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 540 | sleep(HZ); |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 541 | ide_power_enable(false); |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 542 | sleep(HZ); |
Marcin Bukat | 57484bd | 2010-07-06 17:30:10 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Marcin Bukat | 78d54fa | 2010-07-09 09:12:51 +0000 | [diff] [blame] | 545 | /* spurious wakeup ?*/ |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 546 | __shutdown(); |
| 547 | break; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 548 | } |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 549 | lcd_update(); |
Marcin Bukat | 69fa42d | 2010-05-04 11:16:17 +0000 | [diff] [blame] | 550 | last_event = event; |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Marcin Bukat | 28d54c6 | 2010-04-26 21:40:16 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | /* These functions are present in the firmware library, but we reimplement |
| 556 | them here because the originals do a lot more than we want */ |
| 557 | void screen_dump(void) |
| 558 | { |
| 559 | } |