Workaround for crashing shutdown sequence
[maemo-rb.git] / bootloader / gigabeat.c
blobcfba4bb38d73087a8c3f18006455c4cc974ecac8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Greg White
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
21 #include "config.h"
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 "fat.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 "file.h"
43 #include "common.h"
44 #include "rbunicode.h"
45 #include "usb.h"
46 #include "mmu-arm.h"
47 #include "rtc.h"
48 #include "version.h"
50 #include <stdarg.h>
52 void shutdown(void)
54 /* We need to gracefully spin down the disk to prevent clicks. */
55 if (ide_powered())
57 /* Make sure ATA has been initialized. */
58 ata_init();
60 /* And put the disk into sleep immediately. */
61 ata_sleepnow();
64 _backlight_off();
66 power_off();
69 void main(void)
71 unsigned char* loadbuffer;
72 int buffer_size;
73 int rc;
74 int(*kernel_entry)(void);
76 system_init();
77 kernel_init(); /* Need the kernel to sleep */
79 enable_interrupt(IRQ_FIQ_STATUS);
81 lcd_init();
82 backlight_init();
83 button_init();
84 font_init();
85 adc_init();
87 lcd_setfont(FONT_SYSFIXED);
89 /* These checks should only run if the bootloader is flashed */
90 if(GSTATUS3&0x02)
92 GSTATUS3&=0xFFFFFFFD;
93 if(!(GPGDAT&BUTTON_POWER) && charger_inserted())
95 while(!(GPGDAT&BUTTON_POWER) && charger_inserted())
97 char msg[20];
98 if(charging_state())
100 snprintf(msg,sizeof(msg),"Charging");
102 else
104 snprintf(msg,sizeof(msg),"Charge Complete");
106 reset_screen();
107 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
108 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
109 lcd_update();
111 #if defined(HAVE_RTC_ALARM)
112 /* Check if the alarm went off while charging */
113 if(rtc_check_alarm_flag())
115 GSTATUS3=1; /* Normally this is set in crt0.s */
116 break;
118 #endif
120 if(!(GPGDAT&BUTTON_POWER)
121 #if defined(HAVE_RTC_ALARM)
122 && !GSTATUS3
123 #endif
126 shutdown();
130 if(button_hold())
132 const char msg[] = "HOLD is enabled";
133 reset_screen();
134 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
135 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
136 lcd_update();
138 sleep(2*HZ);
140 shutdown();
144 power_init();
145 usb_init();
147 /* Enter USB mode without USB thread */
148 if(usb_detect() == USB_INSERTED)
150 const char msg[] = "Bootloader USB mode";
151 reset_screen();
152 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
153 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
154 lcd_update();
156 storage_enable(false);
157 sleep(HZ/20);
158 usb_enable(true);
160 while (usb_detect() == USB_INSERTED)
161 sleep(HZ);
163 usb_enable(false);
165 reset_screen();
166 lcd_update();
169 reset_screen();
171 /* Show debug messages if button is pressed */
172 if(button_read_device()&BUTTON_A)
173 verbose = true;
175 printf("Rockbox boot loader");
176 printf("Version " RBVERSION);
178 sleep(50); /* ATA seems to error without this pause */
180 rc = storage_init();
181 if(rc)
183 reset_screen();
184 error(EATA, rc, true);
187 disk_init();
189 rc = disk_mount_all();
190 if (rc<=0)
192 error(EDISK, rc, true);
195 printf("Loading firmware");
197 /* Flush out anything pending first */
198 cpucache_invalidate();
200 loadbuffer = (unsigned char*) 0x31000000;
201 buffer_size = (unsigned char*)0x31400000 - loadbuffer;
203 rc = load_firmware(loadbuffer, BOOTFILE, buffer_size);
204 if(rc < 0)
205 error(EBOOTFILE, rc, true);
207 storage_close();
208 system_prepare_fw_start();
210 if (rc == EOK)
212 cpucache_invalidate();
213 kernel_entry = (void*) loadbuffer;
214 rc = kernel_entry();
217 #if 0
218 /* Halt */
219 while (1)
220 core_idle();
221 #else
222 /* Return and restart */
223 #endif