blob: fd8049e5e67d6a4e981d00c20d1de4f8bb01b6b2 [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 Feltzing2a77d3a2005-01-28 13:27:58 +0000100 fd = open("/rockbox.iriver", O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000101 if(fd < 0)
102 return -1;
103
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000104 len = filesize(fd) - 8;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000105
106 snprintf(str, 80, "Length: %x", len);
107 lcd_puts(0, line++, str);
108 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
116 snprintf(str, 80, "Checksum: %x", chksum);
117 lcd_puts(0, line++, str);
118 lcd_update();
119
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000120 rc = read(fd, model, 4);
121 if(rc < 4)
122 return -3;
123
124 model[4] = 0;
125
126 snprintf(str, 80, "Model name: %s", model);
127 lcd_puts(0, line++, str);
128 lcd_update();
129
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000130 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000131
132 rc = read(fd, buf, len);
133 if(rc < len)
134 return -4;
135
136 close(fd);
137
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000138 sum = MODEL_NUMBER;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000139
140 for(i = 0;i < len;i++) {
141 sum += buf[i];
142 }
143
144 snprintf(str, 80, "Sum: %x", sum);
145 lcd_puts(0, line++, str);
146 lcd_update();
147
148 if(sum != chksum)
149 return -5;
150
151 return 0;
152}
153
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000154
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000155void start_firmware(void)
156{
157 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +0000158 /* Reset the cookie for the crt0 crash check */
159 asm(" move.l #0,%d0");
160 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000161 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000162 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000163 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
164 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000165 asm(" jmp (%a0)");
166}
167
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000168void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000169{
170 int i;
171 int rc;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000172 char buf[256];
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000173 bool rc_on_button = false;
174 bool on_button = false;
175 int data;
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000176 int adc_battery, battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000177
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000178 /* We want to read the buttons as early as possible, before the user
179 releases the ON button */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000180
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000181 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs
182 (The ON and Hold buttons on the main unit and the remote) */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000183 or_l(0x00100062, &GPIO1_FUNCTION);
184 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000185
186 data = GPIO1_READ;
187 if ((data & 0x20) == 0)
188 on_button = true;
189
190 if ((data & 0x40) == 0)
191 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000192
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000193 /* Backlight ON */
194 or_l(0x00020000, &GPIO1_ENABLE);
195 or_l(0x00020000, &GPIO1_FUNCTION);
196
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
202 /* Power on the hard drive early, to speed up the loading */
203 if(!((on_button && button_hold()) ||
204 (rc_on_button && remote_button_hold()))) {
205 ide_power_enable(true);
206 }
207
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000208 system_init();
209 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000210
211#ifdef HAVE_ADJUSTABLE_CPU_FREQ
212 /* Set up waitstates for the peripherals */
213 set_cpu_frequency(0); /* PLL off */
214#endif
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000215
216 uda1380_reset();
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000217
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000218 backlight_init();
219 set_irq_level(0);
220 lcd_init();
221 font_init();
222 adc_init();
223 button_init();
224
225 lcd_setfont(FONT_SYSFIXED);
226
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000227 lcd_puts(0, line++, "Rockbox boot loader");
228 snprintf(buf, sizeof(buf), "Version %s", version);
Linus Nielsen Feltzinga2e8cf72005-07-15 07:42:25 +0000229 lcd_puts(0, line++, buf);
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000230 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000231
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000232 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000233
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000234 /* Holding REC while starting runs the original firmware */
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000235 if(((button_status() & BUTTON_REC) == BUTTON_REC) ||
236 ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) {
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000237 lcd_puts(0, 8, "Starting original firmware...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000238 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000239 start_iriver_fw();
240 }
241
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000242 /* Don't start if the Hold button is active on the device you
243 are starting with */
244 if((on_button && button_hold()) ||
245 (rc_on_button && remote_button_hold())) {
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000246 lcd_puts(0, 8, "HOLD switch on, power off...");
247 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000248 sleep(HZ*2);
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000249 /* Reset the cookie for the crt0 crash check */
250 asm(" move.l #0,%d0");
251 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000252 power_off();
253 }
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000254
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000255 adc_battery = adc_read(ADC_BATTERY);
256
257 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
258 batt_int = battery_voltage / 100;
259 batt_frac = battery_voltage % 100;
260
261 snprintf(buf, 32, "Batt: %d.%02dV", batt_int, batt_frac);
262 lcd_puts(0, line++, buf);
263 lcd_update();
264
265 if(battery_voltage <= 300) {
266 line++;
267 lcd_puts(0, line++, "WARNING! BATTERY LOW!!");
268 lcd_update();
269 sleep(HZ*2);
270 }
271
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000272 rc = ata_init();
273 if(rc)
274 {
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000275 char str[32];
276 lcd_clear_display();
277 snprintf(str, 31, "ATA error: %d", rc);
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000278 lcd_puts(0, line++, str);
279 lcd_puts(0, line++, "Insert USB cable and press");
280 lcd_puts(0, line++, "a button");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000281 lcd_update();
282 while(!(button_get(true) & BUTTON_REL));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000283 }
284
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000285 /* A hack to enter USB mode without using the USB thread */
286 if(usb_detect())
287 {
288 lcd_clear_display();
289 lcd_puts(0, 7, " Bootloader USB mode");
290 lcd_update();
291
292 ata_spin();
293 ata_enable(false);
294 usb_enable(true);
295 while(usb_detect())
296 {
297 ata_spin(); /* Prevent the drive from spinning down */
298 sleep(HZ);
Linus Nielsen Feltzinge628b5c2005-07-19 08:08:33 +0000299
300 /* Backlight OFF */
301 or_l(0x00020000, &GPIO1_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000302 }
303
304 system_reboot();
305 }
306
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000307 disk_init();
308
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000309 rc = disk_mount_all();
310 if (rc<=0)
311 {
312 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000313 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000314 while(button_get(true) != SYS_USB_CONNECTED) {};
315 }
316
317 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000318 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000319 i = load_firmware();
320 snprintf(buf, 256, "Result: %d", i);
321 lcd_puts(0, line++, buf);
322 lcd_update();
323
324 if(i == 0)
325 start_firmware();
326
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000327 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000328}
329
330/* These functions are present in the firmware library, but we reimplement
331 them here because the originals do a lot more than we want */
332
333void reset_poweroff_timer(void)
334{
335}
336
337void screen_dump(void)
338{
339}
340
341int dbg_ports(void)
342{
343 return 0;
344}
345
346void mpeg_stop(void)
347{
348}
349
350void usb_acknowledge(void)
351{
352}
353
354void usb_wait_for_disconnect(void)
355{
356}