blob: 22053d25d42b82c595e291dec453e101a8621ccd [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;
186
187 /* Read the buttons early */
188
189 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs */
190 GPIO1_FUNCTION |= 0x00100062;
191 GPIO1_ENABLE &= ~0x00100062;
192
193 data = GPIO1_READ;
194 if ((data & 0x20) == 0)
195 on_button = true;
196
197 if ((data & 0x40) == 0)
198 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000199
200 power_init();
201 system_init();
202 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000203
204#ifdef HAVE_ADJUSTABLE_CPU_FREQ
205 /* Set up waitstates for the peripherals */
206 set_cpu_frequency(0); /* PLL off */
207#endif
208
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000209 backlight_init();
210 set_irq_level(0);
211 lcd_init();
212 font_init();
213 adc_init();
214 button_init();
215
216 lcd_setfont(FONT_SYSFIXED);
217
Linus Nielsen Feltzinga75f0e52005-07-09 09:04:37 +0000218 snprintf(buf, 256, "Rockboot version 3");
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000219 lcd_puts(0, line++, buf);
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 Feltzingd3971452005-01-28 12:51:10 +0000242 rc = ata_init();
243 if(rc)
244 {
245#ifdef HAVE_LCD_BITMAP
246 char str[32];
247 lcd_clear_display();
248 snprintf(str, 31, "ATA error: %d", rc);
249 lcd_puts(0, 1, str);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000250 lcd_update();
251 while(!(button_get(true) & BUTTON_REL));
252#endif
253 panicf("ata: %d", rc);
254 }
255
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000256 /* A hack to enter USB mode without using the USB thread */
257 if(usb_detect())
258 {
259 lcd_clear_display();
260 lcd_puts(0, 7, " Bootloader USB mode");
261 lcd_update();
262
263 ata_spin();
264 ata_enable(false);
265 usb_enable(true);
266 while(usb_detect())
267 {
268 ata_spin(); /* Prevent the drive from spinning down */
269 sleep(HZ);
270 }
271
272 system_reboot();
273 }
274
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000275 disk_init();
276
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000277 rc = disk_mount_all();
278 if (rc<=0)
279 {
280 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000281 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000282 while(button_get(true) != SYS_USB_CONNECTED) {};
283 }
284
285 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000286 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000287 i = load_firmware();
288 snprintf(buf, 256, "Result: %d", i);
289 lcd_puts(0, line++, buf);
290 lcd_update();
291
292 if(i == 0)
293 start_firmware();
294
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000295 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000296}
297
298/* These functions are present in the firmware library, but we reimplement
299 them here because the originals do a lot more than we want */
300
301void reset_poweroff_timer(void)
302{
303}
304
305void screen_dump(void)
306{
307}
308
309int dbg_ports(void)
310{
311 return 0;
312}
313
314void mpeg_stop(void)
315{
316}
317
318void usb_acknowledge(void)
319{
320}
321
322void usb_wait_for_disconnect(void)
323{
324}