blob: 005d33630fee56ea416b9457b03212df9ebdc2de [file] [log] [blame]
Linus Nielsen Feltzingc5df4f82007-02-23 09:30:09 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2006 by Greg White
11 *
Daniel Stenberg2acc0ac2008-06-28 18:10:04 +000012 * 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 Feltzingc5df4f82007-02-23 09:30:09 +000016 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
Marcoen Hirschberg0a068242006-08-12 08:27:48 +000021#include "config.h"
22
23#include <stdlib.h>
24#include <stdio.h>
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000025#include "inttypes.h"
26#include "string.h"
Marcoen Hirschberg0a068242006-08-12 08:27:48 +000027#include "cpu.h"
28#include "system.h"
29#include "lcd.h"
Michael Sevakis4ea4cdf2014-08-08 02:28:11 -040030#include "../kernel-internal.h"
Frank Gevaerts2f8a0082008-11-01 16:14:28 +000031#include "storage.h"
Michael Sevakis7d1a47c2013-08-05 22:02:45 -040032#include "file_internal.h"
Marcoen Hirschberg0a068242006-08-12 08:27:48 +000033#include "disk.h"
34#include "font.h"
35#include "adc.h"
36#include "backlight.h"
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000037#include "backlight-target.h"
38#include "button.h"
Marcoen Hirschberg0a068242006-08-12 08:27:48 +000039#include "panic.h"
40#include "power.h"
41#include "file.h"
Linus Nielsen Feltzing46597c92007-02-22 15:09:49 +000042#include "common.h"
Marcin Bukat0b296912012-03-04 15:34:29 +010043#include "rb-loader.h"
44#include "loader_strerror.h"
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000045#include "rbunicode.h"
46#include "usb.h"
Karl Kurbjun5a9a2b72007-10-23 03:29:15 +000047#include "mmu-arm.h"
Karl Kurbjunf32336e2008-11-12 05:11:18 +000048#include "rtc.h"
Rafaël Carré5d236b22010-05-27 09:41:46 +000049#include "version.h"
Marcoen Hirschberg29536762006-12-29 02:49:12 +000050
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000051#include <stdarg.h>
Marcoen Hirschberg29536762006-12-29 02:49:12 +000052
Karl Kurbjunf32336e2008-11-12 05:11:18 +000053void 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
Marcin Bukat89ba7e82015-01-09 00:22:40 +010065 backlight_hw_off();
Karl Kurbjunf32336e2008-11-12 05:11:18 +000066
67 power_off();
68}
69
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000070void main(void)
Marcoen Hirschberg29536762006-12-29 02:49:12 +000071{
Marcoen Hirschberg29536762006-12-29 02:49:12 +000072 unsigned char* loadbuffer;
73 int buffer_size;
Marcoen Hirschberg29536762006-12-29 02:49:12 +000074 int rc;
75 int(*kernel_entry)(void);
Marcoen Hirschberg0a068242006-08-12 08:27:48 +000076
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000077 system_init();
Michael Sevakiscb061082008-12-08 23:31:05 +000078 kernel_init(); /* Need the kernel to sleep */
79
80 enable_interrupt(IRQ_FIQ_STATUS);
81
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000082 lcd_init();
83 backlight_init();
Karl Kurbjunf32336e2008-11-12 05:11:18 +000084 button_init();
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000085 font_init();
Karl Kurbjunf32336e2008-11-12 05:11:18 +000086 adc_init();
Marcoen Hirschberg0a068242006-08-12 08:27:48 +000087
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +000088 lcd_setfont(FONT_SYSFIXED);
Karl Kurbjunf32336e2008-11-12 05:11:18 +000089
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 Hirschberg0a068242006-08-12 08:27:48 +0000130
Karl Kurbjunf32336e2008-11-12 05:11:18 +0000131 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 Kurbjun8a1fd8c2007-04-21 04:48:20 +0000146 usb_init();
147
148 /* Enter USB mode without USB thread */
Dave Chapman16723502007-09-04 08:03:07 +0000149 if(usb_detect() == USB_INSERTED)
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000150 {
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 Gevaerts2f8a0082008-11-01 16:14:28 +0000157 storage_enable(false);
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000158 sleep(HZ/20);
159 usb_enable(true);
160
Dave Chapman16723502007-09-04 08:03:07 +0000161 while (usb_detect() == USB_INSERTED)
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000162 sleep(HZ);
163
164 usb_enable(false);
165
166 reset_screen();
167 lcd_update();
Greg White3b65fc22007-01-17 18:31:40 +0000168 }
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000169
Karl Kurbjunf32336e2008-11-12 05:11:18 +0000170 reset_screen();
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000171
172 /* Show debug messages if button is pressed */
Karl Kurbjun75be9c32009-04-21 04:36:46 +0000173 if(button_read_device()&BUTTON_A)
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000174 verbose = true;
175
176 printf("Rockbox boot loader");
Michael Sevakis95a4c3a2014-08-28 10:26:45 -0400177 printf("Version %s", rbversion);
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000178
Karl Kurbjun75103352008-04-22 04:34:25 +0000179 sleep(50); /* ATA seems to error without this pause */
180
Frank Gevaerts2f8a0082008-11-01 16:14:28 +0000181 rc = storage_init();
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000182 if(rc)
183 {
184 reset_screen();
Rafaël Carré1ec82122010-06-23 05:08:36 +0000185 error(EATA, rc, true);
Greg White8b3c8792007-01-17 01:49:19 +0000186 }
Marcoen Hirschberg29536762006-12-29 02:49:12 +0000187
Michael Sevakis7d1a47c2013-08-05 22:02:45 -0400188 filesystem_init();
Marcoen Hirschberg29536762006-12-29 02:49:12 +0000189
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000190 rc = disk_mount_all();
191 if (rc<=0)
192 {
Rafaël Carré1ec82122010-06-23 05:08:36 +0000193 error(EDISK, rc, true);
Marcoen Hirschberg29536762006-12-29 02:49:12 +0000194 }
195
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000196 printf("Loading firmware");
Marcoen Hirschberg29536762006-12-29 02:49:12 +0000197
Michael Sevakiscb061082008-12-08 23:31:05 +0000198 /* Flush out anything pending first */
Michael Sevakis6a677072011-12-17 07:27:24 +0000199 commit_discard_idcache();
Michael Sevakiscb061082008-12-08 23:31:05 +0000200
Karl Kurbjun75103352008-04-22 04:34:25 +0000201 loadbuffer = (unsigned char*) 0x31000000;
202 buffer_size = (unsigned char*)0x31400000 - loadbuffer;
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000203
204 rc = load_firmware(loadbuffer, BOOTFILE, buffer_size);
Marcin Bukat0b296912012-03-04 15:34:29 +0100205 if(rc <= EFILE_EMPTY)
Rafaël Carré1ec82122010-06-23 05:08:36 +0000206 error(EBOOTFILE, rc, true);
Karl Kurbjun8a1fd8c2007-04-21 04:48:20 +0000207
Michael Sevakiscb061082008-12-08 23:31:05 +0000208 storage_close();
209 system_prepare_fw_start();
210
Marcin Bukat0b296912012-03-04 15:34:29 +0100211 commit_discard_idcache();
212 kernel_entry = (void*) loadbuffer;
213 rc = kernel_entry();
Michael Sevakiscb061082008-12-08 23:31:05 +0000214
215#if 0
216 /* Halt */
217 while (1)
218 core_idle();
219#else
220 /* Return and restart */
221#endif
Marcoen Hirschberg0a068242006-08-12 08:27:48 +0000222}
223