blob: c250815a6ef8e2f7563325d8313283d96231a050 [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 Feltzinge82df4e2005-07-08 15:09:44 +000041#ifdef IRIVER_H100
42#define MODEL_NUMBER 1
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000043#else
44#define MODEL_NUMBER 0
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000045#endif
46
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000047int line = 0;
48
49int usb_screen(void)
50{
51 return 0;
52}
53
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000054static void usb_enable(bool on)
55{
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000056 and_l(~0x01000000, &GPIO_OUT); /* GPIO24 is the Cypress chip power */
57 or_l(0x01000000, &GPIO_ENABLE);
58 or_l(0x01000000, &GPIO_FUNCTION);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000059
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000060 or_l(0x00000080, &GPIO1_FUNCTION); /* GPIO39 is the USB detect input */
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000061
62 if(on)
63 {
64 /* Power on the Cypress chip */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000065 or_l(0x01000000, &GPIO_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000066 sleep(2);
67 }
68 else
69 {
70 /* Power off the Cypress chip */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +000071 and_l(~0x01000000, &GPIO_OUT);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000072 }
73}
74
75bool usb_detect(void)
76{
77 return (GPIO1_READ & 0x80)?true:false;
78}
79
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000080void start_iriver_fw(void)
81{
82 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000083 /* Reset the cookie for the crt0 crash check */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000084 asm(" move.l #0,%d0");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000085 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000086 asm(" movec.l %d0,%vbr");
87 asm(" move.l 0,%sp");
88 asm(" lea.l 8,%a0");
89 asm(" jmp (%a0)");
90}
91
92int load_firmware(void)
93{
94 int fd;
95 int rc;
96 int len;
97 unsigned long chksum;
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +000098 char model[5];
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000099 unsigned long sum;
100 int i;
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000101 unsigned char *buf = (unsigned char *)DRAM_START;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000102 char str[80];
103
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000104 fd = open("/rockbox.iriver", O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000105 if(fd < 0)
106 return -1;
107
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
182 /* Read the buttons early */
183
184 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs */
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000185 or_l(0x00100062, &GPIO1_FUNCTION);
186 and_l(~0x00100062, &GPIO1_ENABLE);
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000187
188 data = GPIO1_READ;
189 if ((data & 0x20) == 0)
190 on_button = true;
191
192 if ((data & 0x40) == 0)
193 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000194
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000195 /* Backlight ON */
196 or_l(0x00020000, &GPIO1_ENABLE);
197 or_l(0x00020000, &GPIO1_FUNCTION);
198
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000199 power_init();
200 system_init();
201 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000202
203#ifdef HAVE_ADJUSTABLE_CPU_FREQ
204 /* Set up waitstates for the peripherals */
205 set_cpu_frequency(0); /* PLL off */
206#endif
Linus Nielsen Feltzingd2ca7fc2005-07-12 05:25:42 +0000207
208 uda1380_reset();
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000209
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000210 backlight_init();
211 set_irq_level(0);
212 lcd_init();
213 font_init();
214 adc_init();
215 button_init();
216
217 lcd_setfont(FONT_SYSFIXED);
218
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000219 lcd_puts(0, line++, "Rockboot version CVS");
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000220 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000221
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000222 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000223
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000224 if(((button_status() & BUTTON_REC) == BUTTON_REC) ||
225 ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) {
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000226 lcd_puts(0, 8, "Starting original firmware...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000227 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000228 start_iriver_fw();
229 }
230
Linus Nielsen Feltzinga39026a2005-07-08 15:36:12 +0000231 if(on_button & button_hold() ||
232 rc_on_button & remote_button_hold()) {
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000233 lcd_puts(0, 8, "HOLD switch on, power off...");
234 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000235 sleep(HZ*2);
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000236 /* Reset the cookie for the crt0 crash check */
237 asm(" move.l #0,%d0");
238 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000239 power_off();
240 }
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000241
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000242 adc_battery = adc_read(ADC_BATTERY);
243
244 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
245 batt_int = battery_voltage / 100;
246 batt_frac = battery_voltage % 100;
247
248 snprintf(buf, 32, "Batt: %d.%02dV", batt_int, batt_frac);
249 lcd_puts(0, line++, buf);
250 lcd_update();
251
252 if(battery_voltage <= 300) {
253 line++;
254 lcd_puts(0, line++, "WARNING! BATTERY LOW!!");
255 lcd_update();
256 sleep(HZ*2);
257 }
258
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000259 rc = ata_init();
260 if(rc)
261 {
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000262 char str[32];
263 lcd_clear_display();
264 snprintf(str, 31, "ATA error: %d", rc);
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000265 lcd_puts(0, line++, str);
266 lcd_puts(0, line++, "Insert USB cable and press");
267 lcd_puts(0, line++, "a button");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000268 lcd_update();
269 while(!(button_get(true) & BUTTON_REL));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000270 }
271
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000272 /* A hack to enter USB mode without using the USB thread */
273 if(usb_detect())
274 {
275 lcd_clear_display();
276 lcd_puts(0, 7, " Bootloader USB mode");
277 lcd_update();
278
279 ata_spin();
280 ata_enable(false);
281 usb_enable(true);
282 while(usb_detect())
283 {
284 ata_spin(); /* Prevent the drive from spinning down */
285 sleep(HZ);
286 }
287
288 system_reboot();
289 }
290
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000291 disk_init();
292
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000293 rc = disk_mount_all();
294 if (rc<=0)
295 {
296 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000297 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000298 while(button_get(true) != SYS_USB_CONNECTED) {};
299 }
300
301 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000302 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000303 i = load_firmware();
304 snprintf(buf, 256, "Result: %d", i);
305 lcd_puts(0, line++, buf);
306 lcd_update();
307
308 if(i == 0)
309 start_firmware();
310
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000311 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000312}
313
314/* These functions are present in the firmware library, but we reimplement
315 them here because the originals do a lot more than we want */
316
317void reset_poweroff_timer(void)
318{
319}
320
321void screen_dump(void)
322{
323}
324
325int dbg_ports(void)
326{
327 return 0;
328}
329
330void mpeg_stop(void)
331{
332}
333
334void usb_acknowledge(void)
335{
336}
337
338void usb_wait_for_disconnect(void)
339{
340}