blob: 8d4c1e27aa0129074a756a1e941fa9a1bec8afe7 [file] [log] [blame]
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2005 by Linus Nielsen Feltzing
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19#include "config.h"
20
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"
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +000029#include "usb.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000030#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"
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000038#include "uda1380.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000039
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000040#include "pcf50606.h"
41
42#include <stdarg.h>
43
Linus Nielsen Feltzing62c768c2005-07-09 07:46:42 +000044#define DRAM_START 0x31000000
45
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000046int line = 0;
47
48int usb_screen(void)
49{
50 return 0;
51}
52
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +000053char version[] = APPSVERSION;
54
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +000055char printfbuf[256];
56
57void printf(const char *format, ...)
58{
59 int len;
60 unsigned char *ptr;
61 va_list ap;
62 va_start(ap, format);
63
64 ptr = printfbuf;
65 len = vsnprintf(ptr, sizeof(printfbuf), format, ap);
66 va_end(ap);
67
68 lcd_puts(0, line++, ptr);
69 lcd_update();
70 if(line >= 16)
71 line = 0;
72}
73
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000074void start_iriver_fw(void)
75{
76 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000077 /* Reset the cookie for the crt0 crash check */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000078 asm(" move.l #0,%d0");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000079 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000080 asm(" movec.l %d0,%vbr");
81 asm(" move.l 0,%sp");
82 asm(" lea.l 8,%a0");
83 asm(" jmp (%a0)");
84}
85
86int load_firmware(void)
87{
88 int fd;
89 int rc;
90 int len;
91 unsigned long chksum;
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +000092 char model[5];
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000093 unsigned long sum;
94 int i;
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000095 unsigned char *buf = (unsigned char *)DRAM_START;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000096
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +000097 fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000098 if(fd < 0)
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +000099 {
100 fd = open("/" BOOTFILE, O_RDONLY);
101 if(fd < 0)
102 return -1;
103 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000104
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000105 len = filesize(fd) - 8;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000106
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000107 printf("Length: %x", len);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000108 lcd_update();
109
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000110 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000111
112 rc = read(fd, &chksum, 4);
113 if(rc < 4)
114 return -2;
115
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000116 printf("Checksum: %x", chksum);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000117 lcd_update();
118
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000119 rc = read(fd, model, 4);
120 if(rc < 4)
121 return -3;
122
123 model[4] = 0;
124
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000125 printf("Model name: %s", model);
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000126 lcd_update();
127
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000128 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000129
130 rc = read(fd, buf, len);
131 if(rc < len)
132 return -4;
133
134 close(fd);
135
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000136 sum = MODEL_NUMBER;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000137
138 for(i = 0;i < len;i++) {
139 sum += buf[i];
140 }
141
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000142 printf("Sum: %x", sum);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000143 lcd_update();
144
145 if(sum != chksum)
146 return -5;
147
148 return 0;
149}
150
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000151
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000152void start_firmware(void)
153{
154 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +0000155 /* Reset the cookie for the crt0 crash check */
156 asm(" move.l #0,%d0");
157 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000158 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000159 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000160 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
161 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000162 asm(" jmp (%a0)");
163}
164
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000165void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000166{
167 int i;
168 int rc;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000169 bool rc_on_button = false;
170 bool on_button = false;
171 int data;
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000172 int adc_battery, battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000173
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000174#ifdef IAUDIO_X5
175 (void)rc_on_button;
176 (void)on_button;
177 (void)data;
178 power_init();
179
180 system_init();
181 kernel_init();
182
183 set_cpu_frequency(CPUFREQ_NORMAL);
184
185 set_irq_level(0);
186 lcd_init();
187 font_init();
188 adc_init();
189 button_init();
190
191 printf("Rockbox boot loader");
192 printf("Version %s", version);
193 lcd_update();
194
195 adc_battery = adc_read(ADC_BATTERY);
196
197 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
198 batt_int = battery_voltage / 100;
199 batt_frac = battery_voltage % 100;
200
201 printf("Batt: %d.%02dV", batt_int, batt_frac);
202 lcd_update();
203
204 rc = ata_init();
205 if(rc)
206 {
207 printf("ATA error: %d", rc);
208 sleep(HZ*5);
209 power_off();
210 }
211
212 disk_init();
213
214 rc = disk_mount_all();
215 if (rc<=0)
216 {
217 printf("No partition found");
218 sleep(HZ*5);
219 power_off();
220 }
221
222 printf("Loading firmware");
223 lcd_update();
224 i = load_firmware();
225 printf("Result: %d", i);
226 lcd_update();
227
228 if(i == 0)
229 start_firmware();
230
231 power_off();
232
233#else
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000234 /* We want to read the buttons as early as possible, before the user
235 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000236
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000237 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
238 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000239 or_l(0x00100062, &GPIO1_FUNCTION);
240 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000241
242 data = GPIO1_READ;
243 if ((data & 0x20) == 0)
244 on_button = true;
245
246 if ((data & 0x40) == 0)
247 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000248
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000249 /* Set the default state of the hard drive power to OFF */
250 ide_power_enable(false);
251
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000252 power_init();
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000253
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000254 /* Turn off if neither ON button is pressed */
Linus Nielsen Feltzing54da9242006-01-10 23:11:57 +0000255 if(!(on_button || rc_on_button || usb_detect()))
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000256 power_off();
257
258 /* Backlight ON */
259 or_l(0x00020000, &GPIO1_ENABLE);
260 or_l(0x00020000, &GPIO1_FUNCTION);
261 and_l(~0x00020000, &GPIO1_OUT);
262
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
274
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000275 /* 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
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000278 if(!((on_button && button_hold()) ||
279 (rc_on_button && remote_button_hold()))) {
280 ide_power_enable(true);
281 }
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000282#endif
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000283
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000284 system_init();
285 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000286
287#ifdef HAVE_ADJUSTABLE_CPU_FREQ
288 /* Set up waitstates for the peripherals */
289 set_cpu_frequency(0); /* PLL off */
290#endif
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000291
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000292#ifdef HAVE_UDA1380
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000293 uda1380_reset();
Linus Nielsen Feltzing2fcd1b02006-02-04 23:09:56 +0000294#endif
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000295
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000296 backlight_init();
297 set_irq_level(0);
298 lcd_init();
299 font_init();
300 adc_init();
301 button_init();
302
303 lcd_setfont(FONT_SYSFIXED);
304
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000305 printf("Rockbox boot loader");
306 printf("Version %s", version);
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000307 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000308
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000309 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000310
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000311 /* Holding REC while starting runs the original firmware */
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000312 if(((button_status() & BUTTON_REC) == BUTTON_REC) ||
313 ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) {
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000314 printf("Starting original firmware...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000315 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000316 start_iriver_fw();
317 }
318
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000319 /* Don't start if the Hold button is active on the device you
320 are starting with */
Linus Nielsen Feltzing54da9242006-01-10 23:11:57 +0000321 if(!usb_detect() && ((on_button && button_hold()) ||
322 (rc_on_button && remote_button_hold()))) {
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000323 printf("HOLD switch on, power off...");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000324 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000325 sleep(HZ*2);
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000326
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
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000335 /* Reset the cookie for the crt0 crash check */
336 asm(" move.l #0,%d0");
337 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000338 power_off();
339 }
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000340
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000341 usb_init();
342
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000343 adc_battery = adc_read(ADC_BATTERY);
344
345 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
346 batt_int = battery_voltage / 100;
347 batt_frac = battery_voltage % 100;
348
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000349 printf("Batt: %d.%02dV", batt_int, batt_frac);
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000350 lcd_update();
351
352 if(battery_voltage <= 300) {
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000353 printf("WARNING! BATTERY LOW!!");
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000354 lcd_update();
355 sleep(HZ*2);
356 }
357
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000358 rc = ata_init();
359 if(rc)
360 {
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000361 lcd_clear_display();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000362 printf("ATA error: %d", rc);
363 printf("Insert USB cable and press");
364 printf("a button");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000365 lcd_update();
366 while(!(button_get(true) & BUTTON_REL));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000367 }
368
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000369 /* A hack to enter USB mode without using the USB thread */
370 if(usb_detect())
371 {
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000372 const char msg[] = "Bootloader USB mode";
373 int w, h;
374 font_getstringsize(msg, &w, &h, FONT_SYSFIXED);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000375 lcd_clear_display();
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000376 lcd_putsxy((LCD_WIDTH-w)/2, (LCD_HEIGHT-h)/2, msg);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000377 lcd_update();
378
Linus Nielsen Feltzing780cfff2006-01-11 15:37:08 +0000379#ifdef IRIVER_H300_SERIES
380 sleep(HZ);
381#endif
382
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000383 ata_spin();
384 ata_enable(false);
385 usb_enable(true);
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000386 cpu_idle_mode(true);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000387 while(usb_detect())
388 {
389 ata_spin(); /* Prevent the drive from spinning down */
390 sleep(HZ);
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000391
392 /* Backlight OFF */
393 or_l(0x00020000, &GPIO1_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000394 }
395
Linus Nielsen Feltzing01917ec2005-12-06 12:12:29 +0000396 cpu_idle_mode(false);
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000397 usb_enable(false);
398 ata_init(); /* Reinitialize ATA and continue booting */
399
400 lcd_clear_display();
401 line = 0;
402 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000403 }
404
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000405 disk_init();
406
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000407 rc = disk_mount_all();
408 if (rc<=0)
409 {
410 lcd_clear_display();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000411 printf("No partition found");
412 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000413 while(button_get(true) != SYS_USB_CONNECTED) {};
414 }
415
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000416 printf("Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000417 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000418 i = load_firmware();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000419 printf("Result: %d", i);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000420 lcd_update();
421
422 if(i == 0)
423 start_firmware();
424
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000425 start_iriver_fw();
Linus Nielsen Feltzingce6527b2006-03-22 11:45:33 +0000426#endif /* IAUDIO_X5 */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000427}
428
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 */
431
432void reset_poweroff_timer(void)
433{
434}
435
436void screen_dump(void)
437{
438}
439
440int dbg_ports(void)
441{
442 return 0;
443}
444
445void mpeg_stop(void)
446{
447}
448
Linus Nielsen Feltzinge244f7f2005-09-14 09:44:45 +0000449void sys_poweroff(void)
450{
451}