Add forgotten file (git was supposed to rename!).
[kugel-rb.git] / bootloader / gigabeat.c
blob81c069469dbbb5f95f0244fb8bcebd9a34864720
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"
49 #include <stdarg.h>
51 char version[] = APPSVERSION;
53 void shutdown(void)
55 /* We need to gracefully spin down the disk to prevent clicks. */
56 if (ide_powered())
58 /* Make sure ATA has been initialized. */
59 ata_init();
61 /* And put the disk into sleep immediately. */
62 ata_sleepnow();
65 _backlight_off();
67 power_off();
70 void main(void)
72 unsigned char* loadbuffer;
73 int buffer_size;
74 int rc;
75 int(*kernel_entry)(void);
77 system_init();
78 kernel_init(); /* Need the kernel to sleep */
80 enable_interrupt(IRQ_FIQ_STATUS);
82 lcd_init();
83 backlight_init();
84 button_init();
85 font_init();
86 adc_init();
88 lcd_setfont(FONT_SYSFIXED);
90 /* These checks should only run if the bootloader is flashed */
91 if(GSTATUS3&0x02)
93 GSTATUS3&=0xFFFFFFFD;
94 if(!(GPGDAT&BUTTON_POWER) && charger_inserted())
96 while(!(GPGDAT&BUTTON_POWER) && charger_inserted())
98 char msg[20];
99 if(charging_state())
101 snprintf(msg,sizeof(msg),"Charging");
103 else
105 snprintf(msg,sizeof(msg),"Charge Complete");
107 reset_screen();
108 lcd_putsxy( (LCD_WIDTH - (SYSFONT_WIDTH * strlen(msg))) / 2,
109 (LCD_HEIGHT - SYSFONT_HEIGHT) / 2, msg);
110 lcd_update();
112 #if defined(HAVE_RTC_ALARM)
113 /* Check if the alarm went off while charging */
114 if(rtc_check_alarm_flag())
116 GSTATUS3=1; /* Normally this is set in crt0.s */
117 break;
119 #endif
121 if(!(GPGDAT&BUTTON_POWER)
122 #if defined(HAVE_RTC_ALARM)
123 && !GSTATUS3
124 #endif
127 shutdown();
131 if(button_hold())
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();
139 sleep(2*HZ);
141 shutdown();
145 power_init();
146 usb_init();
148 /* Enter USB mode without USB thread */
149 if(usb_detect() == USB_INSERTED)
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();
157 storage_enable(false);
158 sleep(HZ/20);
159 usb_enable(true);
161 while (usb_detect() == USB_INSERTED)
162 sleep(HZ);
164 usb_enable(false);
166 reset_screen();
167 lcd_update();
170 reset_screen();
172 /* Show debug messages if button is pressed */
173 if(button_read_device()&BUTTON_A)
174 verbose = true;
176 printf("Rockbox boot loader");
177 printf("Version %s", version);
179 sleep(50); /* ATA seems to error without this pause */
181 rc = storage_init();
182 if(rc)
184 reset_screen();
185 error(EATA, rc);
188 disk_init();
190 rc = disk_mount_all();
191 if (rc<=0)
193 error(EDISK,rc);
196 printf("Loading firmware");
198 /* Flush out anything pending first */
199 cpucache_invalidate();
201 loadbuffer = (unsigned char*) 0x31000000;
202 buffer_size = (unsigned char*)0x31400000 - loadbuffer;
204 rc = load_firmware(loadbuffer, BOOTFILE, buffer_size);
205 if(rc < 0)
206 error(EBOOTFILE, rc);
208 storage_close();
209 system_prepare_fw_start();
211 if (rc == EOK)
213 cpucache_invalidate();
214 kernel_entry = (void*) loadbuffer;
215 rc = kernel_entry();
218 #if 0
219 /* Halt */
220 while (1)
221 core_idle();
222 #else
223 /* Return and restart */
224 #endif