Linus Nielsen Feltzing | c5df4f8 | 2007-02-23 09:30:09 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * __________ __ ___. |
| 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
| 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
| 5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < |
| 6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ |
| 7 | * \/ \/ \/ \/ \/ |
| 8 | * $Id$ |
| 9 | * |
| 10 | * Copyright (C) 2006 by Greg White |
| 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 | c5df4f8 | 2007-02-23 09:30:09 +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 | ****************************************************************************/ |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | |
| 23 | #include <stdlib.h> |
| 24 | #include <stdio.h> |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 25 | #include "inttypes.h" |
| 26 | #include "string.h" |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 27 | #include "cpu.h" |
| 28 | #include "system.h" |
| 29 | #include "lcd.h" |
| 30 | #include "kernel.h" |
| 31 | #include "thread.h" |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 32 | #include "storage.h" |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 33 | #include "fat.h" |
| 34 | #include "disk.h" |
| 35 | #include "font.h" |
| 36 | #include "adc.h" |
| 37 | #include "backlight.h" |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 38 | #include "backlight-target.h" |
| 39 | #include "button.h" |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 40 | #include "panic.h" |
| 41 | #include "power.h" |
| 42 | #include "file.h" |
Linus Nielsen Feltzing | 46597c9 | 2007-02-22 15:09:49 +0000 | [diff] [blame] | 43 | #include "common.h" |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 44 | #include "rbunicode.h" |
| 45 | #include "usb.h" |
Karl Kurbjun | 5a9a2b7 | 2007-10-23 03:29:15 +0000 | [diff] [blame] | 46 | #include "mmu-arm.h" |
Karl Kurbjun | f32336e | 2008-11-12 05:11:18 +0000 | [diff] [blame] | 47 | #include "rtc.h" |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 48 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 49 | #include <stdarg.h> |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 50 | |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 51 | char version[] = APPSVERSION; |
| 52 | |
Karl Kurbjun | f32336e | 2008-11-12 05:11:18 +0000 | [diff] [blame] | 53 | void shutdown(void) |
| 54 | { |
| 55 | /* We need to gracefully spin down the disk to prevent clicks. */ |
| 56 | if (ide_powered()) |
| 57 | { |
| 58 | /* Make sure ATA has been initialized. */ |
| 59 | ata_init(); |
| 60 | |
| 61 | /* And put the disk into sleep immediately. */ |
| 62 | ata_sleepnow(); |
| 63 | } |
| 64 | |
| 65 | _backlight_off(); |
| 66 | |
| 67 | power_off(); |
| 68 | } |
| 69 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 70 | void main(void) |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 71 | { |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 72 | unsigned char* loadbuffer; |
| 73 | int buffer_size; |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 74 | int rc; |
| 75 | int(*kernel_entry)(void); |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 76 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 77 | system_init(); |
Michael Sevakis | cb06108 | 2008-12-08 23:31:05 +0000 | [diff] [blame] | 78 | kernel_init(); /* Need the kernel to sleep */ |
| 79 | |
| 80 | enable_interrupt(IRQ_FIQ_STATUS); |
| 81 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 82 | lcd_init(); |
| 83 | backlight_init(); |
Karl Kurbjun | f32336e | 2008-11-12 05:11:18 +0000 | [diff] [blame] | 84 | button_init(); |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 85 | font_init(); |
Karl Kurbjun | f32336e | 2008-11-12 05:11:18 +0000 | [diff] [blame] | 86 | adc_init(); |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 87 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 88 | lcd_setfont(FONT_SYSFIXED); |
Karl Kurbjun | f32336e | 2008-11-12 05:11:18 +0000 | [diff] [blame] | 89 | |
| 90 | /* These checks should only run if the bootloader is flashed */ |
| 91 | if(GSTATUS3&0x02) |
| 92 | { |
| 93 | GSTATUS3&=0xFFFFFFFD; |
| 94 | if(!(GPGDAT&BUTTON_POWER) && charger_inserted()) |
| 95 | { |
| 96 | while(!(GPGDAT&BUTTON_POWER) && charger_inserted()) |
| 97 | { |
| 98 | char msg[20]; |
| 99 | if(charging_state()) |
| 100 | { |
| 101 | snprintf(msg,sizeof(msg),"Charging"); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | snprintf(msg,sizeof(msg),"Charge Complete"); |
| 106 | } |
| 107 | reset_screen(); |
| 108 | lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2, |
| 109 | (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg); |
| 110 | lcd_update(); |
| 111 | |
| 112 | #if defined(HAVE_RTC_ALARM) |
| 113 | /* Check if the alarm went off while charging */ |
| 114 | if(rtc_check_alarm_flag()) |
| 115 | { |
| 116 | GSTATUS3=1; /* Normally this is set in crt0.s */ |
| 117 | break; |
| 118 | } |
| 119 | #endif |
| 120 | } |
| 121 | if(!(GPGDAT&BUTTON_POWER) |
| 122 | #if defined(HAVE_RTC_ALARM) |
| 123 | && !GSTATUS3 |
| 124 | #endif |
| 125 | ) |
| 126 | { |
| 127 | shutdown(); |
| 128 | } |
| 129 | } |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 130 | |
Karl Kurbjun | f32336e | 2008-11-12 05:11:18 +0000 | [diff] [blame] | 131 | if(button_hold()) |
| 132 | { |
| 133 | const char msg[] = "HOLD is enabled"; |
| 134 | reset_screen(); |
| 135 | lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2, |
| 136 | (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg); |
| 137 | lcd_update(); |
| 138 | |
| 139 | sleep(2*HZ); |
| 140 | |
| 141 | shutdown(); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | power_init(); |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 146 | usb_init(); |
| 147 | |
| 148 | /* Enter USB mode without USB thread */ |
Dave Chapman | 1672350 | 2007-09-04 08:03:07 +0000 | [diff] [blame] | 149 | if(usb_detect() == USB_INSERTED) |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 150 | { |
| 151 | const char msg[] = "Bootloader USB mode"; |
| 152 | reset_screen(); |
| 153 | lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2, |
| 154 | (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg); |
| 155 | lcd_update(); |
| 156 | |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 157 | storage_enable(false); |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 158 | sleep(HZ/20); |
| 159 | usb_enable(true); |
| 160 | |
Dave Chapman | 1672350 | 2007-09-04 08:03:07 +0000 | [diff] [blame] | 161 | while (usb_detect() == USB_INSERTED) |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 162 | sleep(HZ); |
| 163 | |
| 164 | usb_enable(false); |
| 165 | |
| 166 | reset_screen(); |
| 167 | lcd_update(); |
Greg White | 3b65fc2 | 2007-01-17 18:31:40 +0000 | [diff] [blame] | 168 | } |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 169 | |
Karl Kurbjun | f32336e | 2008-11-12 05:11:18 +0000 | [diff] [blame] | 170 | reset_screen(); |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 171 | |
| 172 | /* Show debug messages if button is pressed */ |
| 173 | if(button_read_device()) |
| 174 | verbose = true; |
| 175 | |
| 176 | printf("Rockbox boot loader"); |
| 177 | printf("Version %s", version); |
| 178 | |
Karl Kurbjun | 7510335 | 2008-04-22 04:34:25 +0000 | [diff] [blame] | 179 | sleep(50); /* ATA seems to error without this pause */ |
| 180 | |
Frank Gevaerts | 2f8a008 | 2008-11-01 16:14:28 +0000 | [diff] [blame] | 181 | rc = storage_init(); |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 182 | if(rc) |
| 183 | { |
| 184 | reset_screen(); |
| 185 | error(EATA, rc); |
Greg White | 8b3c879 | 2007-01-17 01:49:19 +0000 | [diff] [blame] | 186 | } |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 187 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 188 | disk_init(); |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 189 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 190 | rc = disk_mount_all(); |
| 191 | if (rc<=0) |
| 192 | { |
| 193 | error(EDISK,rc); |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 196 | printf("Loading firmware"); |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 197 | |
Michael Sevakis | cb06108 | 2008-12-08 23:31:05 +0000 | [diff] [blame] | 198 | /* Flush out anything pending first */ |
| 199 | invalidate_icache(); |
| 200 | |
Karl Kurbjun | 7510335 | 2008-04-22 04:34:25 +0000 | [diff] [blame] | 201 | loadbuffer = (unsigned char*) 0x31000000; |
| 202 | buffer_size = (unsigned char*)0x31400000 - loadbuffer; |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 203 | |
| 204 | rc = load_firmware(loadbuffer, BOOTFILE, buffer_size); |
| 205 | if(rc < 0) |
| 206 | error(EBOOTFILE, rc); |
| 207 | |
Michael Sevakis | cb06108 | 2008-12-08 23:31:05 +0000 | [diff] [blame] | 208 | storage_close(); |
| 209 | system_prepare_fw_start(); |
| 210 | |
Karl Kurbjun | 8a1fd8c | 2007-04-21 04:48:20 +0000 | [diff] [blame] | 211 | if (rc == EOK) |
| 212 | { |
Michael Sevakis | cb06108 | 2008-12-08 23:31:05 +0000 | [diff] [blame] | 213 | invalidate_icache(); |
Greg White | 355be50 | 2007-01-13 02:24:15 +0000 | [diff] [blame] | 214 | kernel_entry = (void*) loadbuffer; |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 215 | rc = kernel_entry(); |
Marcoen Hirschberg | 2953676 | 2006-12-29 02:49:12 +0000 | [diff] [blame] | 216 | } |
Michael Sevakis | cb06108 | 2008-12-08 23:31:05 +0000 | [diff] [blame] | 217 | |
| 218 | #if 0 |
| 219 | /* Halt */ |
| 220 | while (1) |
| 221 | core_idle(); |
| 222 | #else |
| 223 | /* Return and restart */ |
| 224 | #endif |
Marcoen Hirschberg | 0a06824 | 2006-08-12 08:27:48 +0000 | [diff] [blame] | 225 | } |
| 226 | |