Browse link added
[kugel-rb.git] / bootloader / main.c
blob8d4c1e27aa0129074a756a1e941fa9a1bec8afe7
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include "cpu.h"
24 #include "system.h"
25 #include "lcd.h"
26 #include "kernel.h"
27 #include "thread.h"
28 #include "ata.h"
29 #include "usb.h"
30 #include "disk.h"
31 #include "font.h"
32 #include "adc.h"
33 #include "backlight.h"
34 #include "button.h"
35 #include "panic.h"
36 #include "power.h"
37 #include "file.h"
38 #include "uda1380.h"
40 #include "pcf50606.h"
42 #include <stdarg.h>
44 #define DRAM_START 0x31000000
46 int line = 0;
48 int usb_screen(void)
50 return 0;
53 char version[] = APPSVERSION;
55 char printfbuf[256];
57 void printf(const char *format, ...)
59 int len;
60 unsigned char *ptr;
61 va_list ap;
62 va_start(ap, format);
64 ptr = printfbuf;
65 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
66 va_end(ap);
68 lcd_puts(0, line++, ptr);
69 lcd_update();
70 if(line >= 16)
71 line = 0;
74 void start_iriver_fw(void)
76 asm(" move.w #0x2700,%sr");
77 /* Reset the cookie for the crt0 crash check */
78 asm(" move.l #0,%d0");
79 asm(" move.l %d0,0x10017ffc");
80 asm(" movec.l %d0,%vbr");
81 asm(" move.l 0,%sp");
82 asm(" lea.l 8,%a0");
83 asm(" jmp (%a0)");
86 int load_firmware(void)
88 int fd;
89 int rc;
90 int len;
91 unsigned long chksum;
92 char model[5];
93 unsigned long sum;
94 int i;
95 unsigned char *buf = (unsigned char *)DRAM_START;
97 fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
98 if(fd < 0)
100 fd = open("/" BOOTFILE, O_RDONLY);
101 if(fd < 0)
102 return -1;
105 len = filesize(fd) - 8;
107 printf("Length: %x", len);
108 lcd_update();
110 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
112 rc = read(fd, &chksum, 4);
113 if(rc < 4)
114 return -2;
116 printf("Checksum: %x", chksum);
117 lcd_update();
119 rc = read(fd, model, 4);
120 if(rc < 4)
121 return -3;
123 model[4] = 0;
125 printf("Model name: %s", model);
126 lcd_update();
128 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
130 rc = read(fd, buf, len);
131 if(rc < len)
132 return -4;
134 close(fd);
136 sum = MODEL_NUMBER;
138 for(i = 0;i < len;i++) {
139 sum += buf[i];
142 printf("Sum: %x", sum);
143 lcd_update();
145 if(sum != chksum)
146 return -5;
148 return 0;
152 void start_firmware(void)
154 asm(" move.w #0x2700,%sr");
155 /* Reset the cookie for the crt0 crash check */
156 asm(" move.l #0,%d0");
157 asm(" move.l %d0,0x10017ffc");
158 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
159 asm(" movec.l %d0,%vbr");
160 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
161 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
162 asm(" jmp (%a0)");
165 void main(void)
167 int i;
168 int rc;
169 bool rc_on_button = false;
170 bool on_button = false;
171 int data;
172 int adc_battery, battery_voltage, batt_int, batt_frac;
174 #ifdef IAUDIO_X5
175 (void)rc_on_button;
176 (void)on_button;
177 (void)data;
178 power_init();
180 system_init();
181 kernel_init();
183 set_cpu_frequency(CPUFREQ_NORMAL);
185 set_irq_level(0);
186 lcd_init();
187 font_init();
188 adc_init();
189 button_init();
191 printf("Rockbox boot loader");
192 printf("Version %s", version);
193 lcd_update();
195 adc_battery = adc_read(ADC_BATTERY);
197 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
198 batt_int = battery_voltage / 100;
199 batt_frac = battery_voltage % 100;
201 printf("Batt: %d.%02dV", batt_int, batt_frac);
202 lcd_update();
204 rc = ata_init();
205 if(rc)
207 printf("ATA error: %d", rc);
208 sleep(HZ*5);
209 power_off();
212 disk_init();
214 rc = disk_mount_all();
215 if (rc<=0)
217 printf("No partition found");
218 sleep(HZ*5);
219 power_off();
222 printf("Loading firmware");
223 lcd_update();
224 i = load_firmware();
225 printf("Result: %d", i);
226 lcd_update();
228 if(i == 0)
229 start_firmware();
231 power_off();
233 #else
234 /* We want to read the buttons as early as possible, before the user
235 releases the ON button */
237 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
238 (The ON and Hold buttons on the main unit and the remote) */
239 or_l(0x00100062, &GPIO1_FUNCTION);
240 and_l(~0x00100062, &GPIO1_ENABLE);
242 data = GPIO1_READ;
243 if ((data & 0x20) == 0)
244 on_button = true;
246 if ((data & 0x40) == 0)
247 rc_on_button = true;
249 /* Set the default state of the hard drive power to OFF */
250 ide_power_enable(false);
252 power_init();
254 /* Turn off if neither ON button is pressed */
255 if(!(on_button || rc_on_button || usb_detect()))
256 power_off();
258 /* Backlight ON */
259 or_l(0x00020000, &GPIO1_ENABLE);
260 or_l(0x00020000, &GPIO1_FUNCTION);
261 and_l(~0x00020000, &GPIO1_OUT);
263 /* Remote backlight ON */
264 #ifdef HAVE_REMOTE_LCD
265 #ifdef IRIVER_H300_SERIES
266 or_l(0x00000002, &GPIO1_ENABLE);
267 and_l(~0x00000002, &GPIO1_OUT);
268 #else
269 or_l(0x00000800, &GPIO_ENABLE);
270 or_l(0x00000800, &GPIO_FUNCTION);
271 and_l(~0x00000800, &GPIO_OUT);
272 #endif
273 #endif
275 /* Power on the hard drive early, to speed up the loading.
276 Some H300 don't like this, so we only do it for the H100 */
277 #ifndef IRIVER_H300_SERIES
278 if(!((on_button && button_hold()) ||
279 (rc_on_button && remote_button_hold()))) {
280 ide_power_enable(true);
282 #endif
284 system_init();
285 kernel_init();
287 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
288 /* Set up waitstates for the peripherals */
289 set_cpu_frequency(0); /* PLL off */
290 #endif
292 #ifdef HAVE_UDA1380
293 uda1380_reset();
294 #endif
296 backlight_init();
297 set_irq_level(0);
298 lcd_init();
299 font_init();
300 adc_init();
301 button_init();
303 lcd_setfont(FONT_SYSFIXED);
305 printf("Rockbox boot loader");
306 printf("Version %s", version);
307 lcd_update();
309 sleep(HZ/50); /* Allow the button driver to check the buttons */
311 /* Holding REC while starting runs the original firmware */
312 if(((button_status() & BUTTON_REC) == BUTTON_REC) ||
313 ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) {
314 printf("Starting original firmware...");
315 lcd_update();
316 start_iriver_fw();
319 /* Don't start if the Hold button is active on the device you
320 are starting with */
321 if(!usb_detect() && ((on_button && button_hold()) ||
322 (rc_on_button && remote_button_hold()))) {
323 printf("HOLD switch on, power off...");
324 lcd_update();
325 sleep(HZ*2);
327 /* Backlight OFF */
328 #ifdef HAVE_REMOTE_LCD
329 #ifdef IRIVER_H300_SERIES
330 or_l(0x00000002, &GPIO1_OUT);
331 #else
332 or_l(0x00000800, &GPIO_OUT);
333 #endif
334 #endif
335 /* Reset the cookie for the crt0 crash check */
336 asm(" move.l #0,%d0");
337 asm(" move.l %d0,0x10017ffc");
338 power_off();
341 usb_init();
343 adc_battery = adc_read(ADC_BATTERY);
345 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
346 batt_int = battery_voltage / 100;
347 batt_frac = battery_voltage % 100;
349 printf("Batt: %d.%02dV", batt_int, batt_frac);
350 lcd_update();
352 if(battery_voltage <= 300) {
353 printf("WARNING! BATTERY LOW!!");
354 lcd_update();
355 sleep(HZ*2);
358 rc = ata_init();
359 if(rc)
361 lcd_clear_display();
362 printf("ATA error: %d", rc);
363 printf("Insert USB cable and press");
364 printf("a button");
365 lcd_update();
366 while(!(button_get(true) & BUTTON_REL));
369 /* A hack to enter USB mode without using the USB thread */
370 if(usb_detect())
372 const char msg[] = "Bootloader USB mode";
373 int w, h;
374 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
375 lcd_clear_display();
376 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
377 lcd_update();
379 #ifdef IRIVER_H300_SERIES
380 sleep(HZ);
381 #endif
383 ata_spin();
384 ata_enable(false);
385 usb_enable(true);
386 cpu_idle_mode(true);
387 while(usb_detect())
389 ata_spin(); /* Prevent the drive from spinning down */
390 sleep(HZ);
392 /* Backlight OFF */
393 or_l(0x00020000, &GPIO1_OUT);
396 cpu_idle_mode(false);
397 usb_enable(false);
398 ata_init(); /* Reinitialize ATA and continue booting */
400 lcd_clear_display();
401 line = 0;
402 lcd_update();
405 disk_init();
407 rc = disk_mount_all();
408 if (rc<=0)
410 lcd_clear_display();
411 printf("No partition found");
412 lcd_update();
413 while(button_get(true) != SYS_USB_CONNECTED) {};
416 printf("Loading firmware");
417 lcd_update();
418 i = load_firmware();
419 printf("Result: %d", i);
420 lcd_update();
422 if(i == 0)
423 start_firmware();
425 start_iriver_fw();
426 #endif /* IAUDIO_X5 */
429 /* These functions are present in the firmware library, but we reimplement
430 them here because the originals do a lot more than we want */
432 void reset_poweroff_timer(void)
436 void screen_dump(void)
440 int dbg_ports(void)
442 return 0;
445 void mpeg_stop(void)
449 void sys_poweroff(void)