blob: b0d25cc42f5b1d36c3a8406cd1e20fbabe63f928 [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"
29#include "disk.h"
30#include "font.h"
31#include "adc.h"
32#include "backlight.h"
33#include "button.h"
34#include "panic.h"
35#include "power.h"
36#include "file.h"
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000037#include "uda1380.h"
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000038
Linus Nielsen Feltzing62c768c2005-07-09 07:46:42 +000039#define DRAM_START 0x31000000
40
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000041int line = 0;
42
43int usb_screen(void)
44{
45 return 0;
46}
47
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +000048char version[] = APPSVERSION;
49
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000050static void usb_enable(bool on)
51{
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000052 and_l(~0x01000000, &GPIO_OUT); /* GPIO24 is the Cypress chip power */
53 or_l(0x01000000, &GPIO_ENABLE);
54 or_l(0x01000000, &GPIO_FUNCTION);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000055
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000056 or_l(0x00000080, &GPIO1_FUNCTION); /* GPIO39 is the USB detect input */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000057
58 if(on)
59 {
60 /* Power on the Cypress chip */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000061 or_l(0x01000000, &GPIO_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000062 sleep(2);
63 }
64 else
65 {
66 /* Power off the Cypress chip */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000067 and_l(~0x01000000, &GPIO_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000068 }
69}
70
71bool usb_detect(void)
72{
73 return (GPIO1_READ & 0x80)?true:false;
74}
75
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000076void start_iriver_fw(void)
77{
78 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000079 /* Reset the cookie for the crt0 crash check */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000080 asm(" move.l #0,%d0");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000081 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000082 asm(" movec.l %d0,%vbr");
83 asm(" move.l 0,%sp");
84 asm(" lea.l 8,%a0");
85 asm(" jmp (%a0)");
86}
87
88int load_firmware(void)
89{
90 int fd;
91 int rc;
92 int len;
93 unsigned long chksum;
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +000094 char model[5];
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000095 unsigned long sum;
96 int i;
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000097 unsigned char *buf = (unsigned char *)DRAM_START;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000098 char str[80];
99
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000100 fd = open("/.rockbox/" BOOTFILE, O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000101 if(fd < 0)
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000102 {
103 fd = open("/" BOOTFILE, O_RDONLY);
104 if(fd < 0)
105 return -1;
106 }
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000107
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000108 len = filesize(fd) - 8;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000109
110 snprintf(str, 80, "Length: %x", len);
111 lcd_puts(0, line++, str);
112 lcd_update();
113
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000114 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000115
116 rc = read(fd, &chksum, 4);
117 if(rc < 4)
118 return -2;
119
120 snprintf(str, 80, "Checksum: %x", chksum);
121 lcd_puts(0, line++, str);
122 lcd_update();
123
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000124 rc = read(fd, model, 4);
125 if(rc < 4)
126 return -3;
127
128 model[4] = 0;
129
130 snprintf(str, 80, "Model name: %s", model);
131 lcd_puts(0, line++, str);
132 lcd_update();
133
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000134 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000135
136 rc = read(fd, buf, len);
137 if(rc < len)
138 return -4;
139
140 close(fd);
141
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000142 sum = MODEL_NUMBER;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000143
144 for(i = 0;i < len;i++) {
145 sum += buf[i];
146 }
147
148 snprintf(str, 80, "Sum: %x", sum);
149 lcd_puts(0, line++, str);
150 lcd_update();
151
152 if(sum != chksum)
153 return -5;
154
155 return 0;
156}
157
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000158
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000159void start_firmware(void)
160{
161 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +0000162 /* Reset the cookie for the crt0 crash check */
163 asm(" move.l #0,%d0");
164 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000165 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000166 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000167 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
168 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000169 asm(" jmp (%a0)");
170}
171
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000172void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000173{
174 int i;
175 int rc;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000176 char buf[256];
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000177 bool rc_on_button = false;
178 bool on_button = false;
179 int data;
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000180 int adc_battery, battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000181
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000182 /* We want to read the buttons as early as possible, before the user
183 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000184
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000185 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
186 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000187 or_l(0x00100062, &GPIO1_FUNCTION);
188 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000189
190 data = GPIO1_READ;
191 if ((data & 0x20) == 0)
192 on_button = true;
193
194 if ((data & 0x40) == 0)
195 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000196
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000197 /* Set the default state of the hard drive power to OFF */
198 ide_power_enable(false);
199
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000200 power_init();
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000201
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000202 /* Turn off if neither ON button is pressed */
203 if(!(on_button || rc_on_button))
204 power_off();
205
206 /* Backlight ON */
207 or_l(0x00020000, &GPIO1_ENABLE);
208 or_l(0x00020000, &GPIO1_FUNCTION);
209 and_l(~0x00020000, &GPIO1_OUT);
210
211 /* Remote backlight ON */
212#ifdef HAVE_REMOTE_LCD
213#ifdef IRIVER_H300_SERIES
214 or_l(0x00000002, &GPIO1_ENABLE);
215 and_l(~0x00000002, &GPIO1_OUT);
216#else
217 or_l(0x00000800, &GPIO_ENABLE);
218 or_l(0x00000800, &GPIO_FUNCTION);
219 and_l(~0x00000800, &GPIO_OUT);
220#endif
221#endif
222
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000223 /* Power on the hard drive early, to speed up the loading */
224 if(!((on_button && button_hold()) ||
225 (rc_on_button && remote_button_hold()))) {
226 ide_power_enable(true);
227 }
228
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000229 system_init();
230 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000231
232#ifdef HAVE_ADJUSTABLE_CPU_FREQ
233 /* Set up waitstates for the peripherals */
234 set_cpu_frequency(0); /* PLL off */
235#endif
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000236
237 uda1380_reset();
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000238
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000239 backlight_init();
240 set_irq_level(0);
241 lcd_init();
242 font_init();
243 adc_init();
244 button_init();
245
246 lcd_setfont(FONT_SYSFIXED);
247
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000248 lcd_puts(0, line++, "Rockbox boot loader");
249 snprintf(buf, sizeof(buf), "Version %s", version);
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +0000250 lcd_puts(0, line++, buf);
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000251 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000252
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000253 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000254
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000255 /* Holding REC while starting runs the original firmware */
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000256 if(((button_status() & BUTTON_REC) == BUTTON_REC) ||
257 ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) {
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000258 lcd_puts(0, 8, "Starting original firmware...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000259 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000260 start_iriver_fw();
261 }
262
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000263 /* Don't start if the Hold button is active on the device you
264 are starting with */
265 if((on_button && button_hold()) ||
266 (rc_on_button && remote_button_hold())) {
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000267 lcd_puts(0, 8, "HOLD switch on, power off...");
268 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000269 sleep(HZ*2);
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000270
271 /* Backlight OFF */
272#ifdef HAVE_REMOTE_LCD
273#ifdef IRIVER_H300_SERIES
274 or_l(0x00000002, &GPIO1_OUT);
275#else
276 or_l(0x00000800, &GPIO_OUT);
277#endif
278#endif
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000279 /* Reset the cookie for the crt0 crash check */
280 asm(" move.l #0,%d0");
281 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000282 power_off();
283 }
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000284
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000285 adc_battery = adc_read(ADC_BATTERY);
286
287 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
288 batt_int = battery_voltage / 100;
289 batt_frac = battery_voltage % 100;
290
291 snprintf(buf, 32, "Batt: %d.%02dV", batt_int, batt_frac);
292 lcd_puts(0, line++, buf);
293 lcd_update();
294
295 if(battery_voltage <= 300) {
296 line++;
297 lcd_puts(0, line++, "WARNING! BATTERY LOW!!");
298 lcd_update();
299 sleep(HZ*2);
300 }
301
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000302 rc = ata_init();
303 if(rc)
304 {
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000305 char str[32];
306 lcd_clear_display();
307 snprintf(str, 31, "ATA error: %d", rc);
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000308 lcd_puts(0, line++, str);
309 lcd_puts(0, line++, "Insert USB cable and press");
310 lcd_puts(0, line++, "a button");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000311 lcd_update();
312 while(!(button_get(true) & BUTTON_REL));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000313 }
314
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000315 /* A hack to enter USB mode without using the USB thread */
316 if(usb_detect())
317 {
318 lcd_clear_display();
319 lcd_puts(0, 7, " Bootloader USB mode");
320 lcd_update();
321
322 ata_spin();
323 ata_enable(false);
324 usb_enable(true);
325 while(usb_detect())
326 {
327 ata_spin(); /* Prevent the drive from spinning down */
328 sleep(HZ);
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000329
330 /* Backlight OFF */
331 or_l(0x00020000, &GPIO1_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000332 }
333
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000334 usb_enable(false);
335 ata_init(); /* Reinitialize ATA and continue booting */
336
337 lcd_clear_display();
338 line = 0;
339 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000340 }
341
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000342 disk_init();
343
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000344 rc = disk_mount_all();
345 if (rc<=0)
346 {
347 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000348 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000349 while(button_get(true) != SYS_USB_CONNECTED) {};
350 }
351
352 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000353 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000354 i = load_firmware();
355 snprintf(buf, 256, "Result: %d", i);
356 lcd_puts(0, line++, buf);
357 lcd_update();
358
359 if(i == 0)
360 start_firmware();
361
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000362 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000363}
364
365/* These functions are present in the firmware library, but we reimplement
366 them here because the originals do a lot more than we want */
367
368void reset_poweroff_timer(void)
369{
370}
371
372void screen_dump(void)
373{
374}
375
376int dbg_ports(void)
377{
378 return 0;
379}
380
381void mpeg_stop(void)
382{
383}
384
385void usb_acknowledge(void)
386{
387}
388
389void usb_wait_for_disconnect(void)
390{
391}
Linus Nielsen Feltzinge244f7f2005-09-14 09:44:45 +0000392
393void sys_poweroff(void)
394{
395}