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