blob: 7fa60f961afad9f137ae3709b2fbfeaa02736e9b [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 Feltzingb68cb9b2005-12-03 00:54:59 +0000223 /* Power on the hard drive early, to speed up the loading.
224 Some H300 don't like this, so we only do it for the H100 */
225#ifndef IRIVER_H300_SERIES
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000226 if(!((on_button && button_hold()) ||
227 (rc_on_button && remote_button_hold()))) {
228 ide_power_enable(true);
229 }
Linus Nielsen Feltzingb68cb9b2005-12-03 00:54:59 +0000230#endif
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000231
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000232 system_init();
233 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000234
235#ifdef HAVE_ADJUSTABLE_CPU_FREQ
236 /* Set up waitstates for the peripherals */
237 set_cpu_frequency(0); /* PLL off */
238#endif
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000239
240 uda1380_reset();
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000241
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000242 backlight_init();
243 set_irq_level(0);
244 lcd_init();
245 font_init();
246 adc_init();
247 button_init();
248
249 lcd_setfont(FONT_SYSFIXED);
250
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000251 lcd_puts(0, line++, "Rockbox boot loader");
252 snprintf(buf, sizeof(buf), "Version %s", version);
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +0000253 lcd_puts(0, line++, buf);
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000254 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000255
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000256 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000257
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000258 /* Holding REC while starting runs the original firmware */
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000259 if(((button_status() & BUTTON_REC) == BUTTON_REC) ||
260 ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) {
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000261 lcd_puts(0, 8, "Starting original firmware...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000262 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000263 start_iriver_fw();
264 }
265
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000266 /* Don't start if the Hold button is active on the device you
267 are starting with */
268 if((on_button && button_hold()) ||
269 (rc_on_button && remote_button_hold())) {
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000270 lcd_puts(0, 8, "HOLD switch on, power off...");
271 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000272 sleep(HZ*2);
Linus Nielsen Feltzingbbe919b2005-11-19 01:33:28 +0000273
274 /* Backlight OFF */
275#ifdef HAVE_REMOTE_LCD
276#ifdef IRIVER_H300_SERIES
277 or_l(0x00000002, &GPIO1_OUT);
278#else
279 or_l(0x00000800, &GPIO_OUT);
280#endif
281#endif
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000282 /* Reset the cookie for the crt0 crash check */
283 asm(" move.l #0,%d0");
284 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000285 power_off();
286 }
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000287
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000288 adc_battery = adc_read(ADC_BATTERY);
289
290 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
291 batt_int = battery_voltage / 100;
292 batt_frac = battery_voltage % 100;
293
294 snprintf(buf, 32, "Batt: %d.%02dV", batt_int, batt_frac);
295 lcd_puts(0, line++, buf);
296 lcd_update();
297
298 if(battery_voltage <= 300) {
299 line++;
300 lcd_puts(0, line++, "WARNING! BATTERY LOW!!");
301 lcd_update();
302 sleep(HZ*2);
303 }
304
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000305 rc = ata_init();
306 if(rc)
307 {
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000308 char str[32];
309 lcd_clear_display();
310 snprintf(str, 31, "ATA error: %d", rc);
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000311 lcd_puts(0, line++, str);
312 lcd_puts(0, line++, "Insert USB cable and press");
313 lcd_puts(0, line++, "a button");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000314 lcd_update();
315 while(!(button_get(true) & BUTTON_REL));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000316 }
317
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000318 /* A hack to enter USB mode without using the USB thread */
319 if(usb_detect())
320 {
321 lcd_clear_display();
322 lcd_puts(0, 7, " Bootloader USB mode");
323 lcd_update();
324
325 ata_spin();
326 ata_enable(false);
327 usb_enable(true);
328 while(usb_detect())
329 {
330 ata_spin(); /* Prevent the drive from spinning down */
331 sleep(HZ);
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000332
333 /* Backlight OFF */
334 or_l(0x00020000, &GPIO1_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000335 }
336
Linus Nielsen Feltzing13103902005-07-19 11:03:00 +0000337 usb_enable(false);
338 ata_init(); /* Reinitialize ATA and continue booting */
339
340 lcd_clear_display();
341 line = 0;
342 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000343 }
344
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000345 disk_init();
346
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000347 rc = disk_mount_all();
348 if (rc<=0)
349 {
350 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000351 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000352 while(button_get(true) != SYS_USB_CONNECTED) {};
353 }
354
355 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000356 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000357 i = load_firmware();
358 snprintf(buf, 256, "Result: %d", i);
359 lcd_puts(0, line++, buf);
360 lcd_update();
361
362 if(i == 0)
363 start_firmware();
364
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000365 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000366}
367
368/* These functions are present in the firmware library, but we reimplement
369 them here because the originals do a lot more than we want */
370
371void reset_poweroff_timer(void)
372{
373}
374
375void screen_dump(void)
376{
377}
378
379int dbg_ports(void)
380{
381 return 0;
382}
383
384void mpeg_stop(void)
385{
386}
387
388void usb_acknowledge(void)
389{
390}
391
392void usb_wait_for_disconnect(void)
393{
394}
Linus Nielsen Feltzinge244f7f2005-09-14 09:44:45 +0000395
396void sys_poweroff(void)
397{
398}