blob: a7985cda405b7e602ebb97b99ff4b55a89cf8d96 [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"
37
Linus Nielsen Feltzing62c768c2005-07-09 07:46:42 +000038#define DRAM_START 0x31000000
39
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000040#ifdef IRIVER_H100
41#define MODEL_NUMBER 1
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000042#else
43#define MODEL_NUMBER 0
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000044#endif
45
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000046int line = 0;
47
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +000048char *modelname[] =
49{
50 "H120/140",
51 "H110/115",
52 "H300"
53};
54
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000055int usb_screen(void)
56{
57 return 0;
58}
59
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000060static void usb_enable(bool on)
61{
62 GPIO_OUT &= ~0x01000000; /* GPIO24 is the Cypress chip power */
63 GPIO_ENABLE |= 0x01000000;
64 GPIO_FUNCTION |= 0x01000000;
65
66 GPIO1_FUNCTION |= 0x00000080; /* GPIO39 is the USB detect input */
67
68 if(on)
69 {
70 /* Power on the Cypress chip */
71 GPIO_OUT |= 0x01000000;
72 sleep(2);
73 }
74 else
75 {
76 /* Power off the Cypress chip */
77 GPIO_OUT &= ~0x01000000;
78 }
79}
80
81bool usb_detect(void)
82{
83 return (GPIO1_READ & 0x80)?true:false;
84}
85
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000086void start_iriver_fw(void)
87{
88 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000089 /* Reset the cookie for the crt0 crash check */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000090 asm(" move.l #0,%d0");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000091 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000092 asm(" movec.l %d0,%vbr");
93 asm(" move.l 0,%sp");
94 asm(" lea.l 8,%a0");
95 asm(" jmp (%a0)");
96}
97
98int load_firmware(void)
99{
100 int fd;
101 int rc;
102 int len;
103 unsigned long chksum;
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000104 char model[5];
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000105 unsigned long sum;
106 int i;
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000107 unsigned char *buf = (unsigned char *)DRAM_START;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000108 char str[80];
109
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000110 fd = open("/rockbox.iriver", O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000111 if(fd < 0)
112 return -1;
113
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000114 len = filesize(fd) - 8;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000115
116 snprintf(str, 80, "Length: %x", len);
117 lcd_puts(0, line++, str);
118 lcd_update();
119
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000120 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000121
122 rc = read(fd, &chksum, 4);
123 if(rc < 4)
124 return -2;
125
126 snprintf(str, 80, "Checksum: %x", chksum);
127 lcd_puts(0, line++, str);
128 lcd_update();
129
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000130 rc = read(fd, model, 4);
131 if(rc < 4)
132 return -3;
133
134 model[4] = 0;
135
136 snprintf(str, 80, "Model name: %s", model);
137 lcd_puts(0, line++, str);
138 lcd_update();
139
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000140 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000141
142 rc = read(fd, buf, len);
143 if(rc < len)
144 return -4;
145
146 close(fd);
147
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000148 sum = MODEL_NUMBER;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000149
150 for(i = 0;i < len;i++) {
151 sum += buf[i];
152 }
153
154 snprintf(str, 80, "Sum: %x", sum);
155 lcd_puts(0, line++, str);
156 lcd_update();
157
158 if(sum != chksum)
159 return -5;
160
161 return 0;
162}
163
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000164
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000165void start_firmware(void)
166{
167 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +0000168 /* Reset the cookie for the crt0 crash check */
169 asm(" move.l #0,%d0");
170 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000171 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000172 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000173 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
174 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000175 asm(" jmp (%a0)");
176}
177
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000178void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000179{
180 int i;
181 int rc;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000182 char buf[256];
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000183 bool rc_on_button = false;
184 bool on_button = false;
185 int data;
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000186 int adc_battery, battery_voltage, batt_int, batt_frac;
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000187
188 /* Read the buttons early */
189
190 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs */
191 GPIO1_FUNCTION |= 0x00100062;
192 GPIO1_ENABLE &= ~0x00100062;
193
194 data = GPIO1_READ;
195 if ((data & 0x20) == 0)
196 on_button = true;
197
198 if ((data & 0x40) == 0)
199 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000200
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000201 /* Backlight ON */
202 or_l(0x00020000, &GPIO1_ENABLE);
203 or_l(0x00020000, &GPIO1_FUNCTION);
204
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000205 power_init();
206 system_init();
207 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000208
209#ifdef HAVE_ADJUSTABLE_CPU_FREQ
210 /* Set up waitstates for the peripherals */
211 set_cpu_frequency(0); /* PLL off */
212#endif
213
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000214 /* UDA1380 RESET */
215 GPIO_OUT |= (1<<29);
216 GPIO_ENABLE |= (1<<29);
217 GPIO_FUNCTION |= (1<<29);
218 sleep(HZ/100);
219 GPIO_OUT &= ~(1<<29);
220
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000221 backlight_init();
222 set_irq_level(0);
223 lcd_init();
224 font_init();
225 adc_init();
226 button_init();
227
228 lcd_setfont(FONT_SYSFIXED);
229
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000230 lcd_puts(0, line++, "Rockboot version CVS");
Linus Nielsen Feltzingd1af08f2005-07-09 19:48:22 +0000231 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000232
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000233 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000234
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 Feltzinga39026a2005-07-08 15:36:12 +0000242 if(on_button & button_hold() ||
243 rc_on_button & remote_button_hold()) {
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000244 lcd_puts(0, 8, "HOLD switch on, power off...");
245 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000246 sleep(HZ*2);
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000247 /* Reset the cookie for the crt0 crash check */
248 asm(" move.l #0,%d0");
249 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000250 power_off();
251 }
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000252
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000253 adc_battery = adc_read(ADC_BATTERY);
254
255 battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000;
256 batt_int = battery_voltage / 100;
257 batt_frac = battery_voltage % 100;
258
259 snprintf(buf, 32, "Batt: %d.%02dV", batt_int, batt_frac);
260 lcd_puts(0, line++, buf);
261 lcd_update();
262
263 if(battery_voltage <= 300) {
264 line++;
265 lcd_puts(0, line++, "WARNING! BATTERY LOW!!");
266 lcd_update();
267 sleep(HZ*2);
268 }
269
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000270 rc = ata_init();
271 if(rc)
272 {
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000273 char str[32];
274 lcd_clear_display();
275 snprintf(str, 31, "ATA error: %d", rc);
Linus Nielsen Feltzingdfa8ecb2005-07-11 14:01:45 +0000276 lcd_puts(0, line++, str);
277 lcd_puts(0, line++, "Insert USB cable and press");
278 lcd_puts(0, line++, "a button");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000279 lcd_update();
280 while(!(button_get(true) & BUTTON_REL));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000281 }
282
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000283 /* A hack to enter USB mode without using the USB thread */
284 if(usb_detect())
285 {
286 lcd_clear_display();
287 lcd_puts(0, 7, " Bootloader USB mode");
288 lcd_update();
289
290 ata_spin();
291 ata_enable(false);
292 usb_enable(true);
293 while(usb_detect())
294 {
295 ata_spin(); /* Prevent the drive from spinning down */
296 sleep(HZ);
297 }
298
299 system_reboot();
300 }
301
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000302 disk_init();
303
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000304 rc = disk_mount_all();
305 if (rc<=0)
306 {
307 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000308 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000309 while(button_get(true) != SYS_USB_CONNECTED) {};
310 }
311
312 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000313 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000314 i = load_firmware();
315 snprintf(buf, 256, "Result: %d", i);
316 lcd_puts(0, line++, buf);
317 lcd_update();
318
319 if(i == 0)
320 start_firmware();
321
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000322 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000323}
324
325/* These functions are present in the firmware library, but we reimplement
326 them here because the originals do a lot more than we want */
327
328void reset_poweroff_timer(void)
329{
330}
331
332void screen_dump(void)
333{
334}
335
336int dbg_ports(void)
337{
338 return 0;
339}
340
341void mpeg_stop(void)
342{
343}
344
345void usb_acknowledge(void)
346{
347}
348
349void usb_wait_for_disconnect(void)
350{
351}