blob: 736b0a6f5f1363e87e4d933f5fde80956223007e [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 Feltzinge82df4e2005-07-08 15:09:44 +000038#ifdef IRIVER_H100
39#define MODEL_NUMBER 1
40#define DRAM_START 0x30000000
41#else
42#define MODEL_NUMBER 0
43#define DRAM_START 0x31000000
44#endif
45
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000046int line = 0;
47
48int usb_screen(void)
49{
50 return 0;
51}
52
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000053static void usb_enable(bool on)
54{
55 GPIO_OUT &= ~0x01000000; /* GPIO24 is the Cypress chip power */
56 GPIO_ENABLE |= 0x01000000;
57 GPIO_FUNCTION |= 0x01000000;
58
59 GPIO1_FUNCTION |= 0x00000080; /* GPIO39 is the USB detect input */
60
61 if(on)
62 {
63 /* Power on the Cypress chip */
64 GPIO_OUT |= 0x01000000;
65 sleep(2);
66 }
67 else
68 {
69 /* Power off the Cypress chip */
70 GPIO_OUT &= ~0x01000000;
71 }
72}
73
74bool usb_detect(void)
75{
76 return (GPIO1_READ & 0x80)?true:false;
77}
78
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000079void start_iriver_fw(void)
80{
81 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000082 /* Reset the cookie for the crt0 crash check */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000083 asm(" move.l #0,%d0");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000084 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000085 asm(" movec.l %d0,%vbr");
86 asm(" move.l 0,%sp");
87 asm(" lea.l 8,%a0");
88 asm(" jmp (%a0)");
89}
90
91int load_firmware(void)
92{
93 int fd;
94 int rc;
95 int len;
96 unsigned long chksum;
97 unsigned long sum;
98 int i;
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +000099 unsigned char *buf = (unsigned char *)DRAM_START;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000100 char str[80];
101
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000102 fd = open("/rockbox.iriver", O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000103 if(fd < 0)
104 return -1;
105
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000106 len = filesize(fd) - 8;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000107
108 snprintf(str, 80, "Length: %x", len);
109 lcd_puts(0, line++, str);
110 lcd_update();
111
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000112 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000113
114 rc = read(fd, &chksum, 4);
115 if(rc < 4)
116 return -2;
117
118 snprintf(str, 80, "Checksum: %x", chksum);
119 lcd_puts(0, line++, str);
120 lcd_update();
121
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000122 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000123
124 rc = read(fd, buf, len);
125 if(rc < len)
126 return -4;
127
128 close(fd);
129
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000130 sum = MODEL_NUMBER;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000131
132 for(i = 0;i < len;i++) {
133 sum += buf[i];
134 }
135
136 snprintf(str, 80, "Sum: %x", sum);
137 lcd_puts(0, line++, str);
138 lcd_update();
139
140 if(sum != chksum)
141 return -5;
142
143 return 0;
144}
145
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000146
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000147void start_firmware(void)
148{
149 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +0000150 /* Reset the cookie for the crt0 crash check */
151 asm(" move.l #0,%d0");
152 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000153 asm(" move.l %0,%%d0" :: "i"(DRAM_START));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000154 asm(" movec.l %d0,%vbr");
Linus Nielsen Feltzinge82df4e2005-07-08 15:09:44 +0000155 asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START));
156 asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4)));
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000157 asm(" jmp (%a0)");
158}
159
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000160void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000161{
162 int i;
163 int rc;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000164 char buf[256];
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000165 bool rc_on_button = false;
166 bool on_button = false;
167 int data;
168
169 /* Read the buttons early */
170
171 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs */
172 GPIO1_FUNCTION |= 0x00100062;
173 GPIO1_ENABLE &= ~0x00100062;
174
175 data = GPIO1_READ;
176 if ((data & 0x20) == 0)
177 on_button = true;
178
179 if ((data & 0x40) == 0)
180 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000181
182 power_init();
183 system_init();
184 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000185
186#ifdef HAVE_ADJUSTABLE_CPU_FREQ
187 /* Set up waitstates for the peripherals */
188 set_cpu_frequency(0); /* PLL off */
189#endif
190
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000191 backlight_init();
192 set_irq_level(0);
193 lcd_init();
194 font_init();
195 adc_init();
196 button_init();
197
198 lcd_setfont(FONT_SYSFIXED);
199
Linus Nielsen Feltzing794220c2005-06-02 21:40:52 +0000200 snprintf(buf, 256, "Rockboot version 2");
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000201 lcd_puts(0, line++, buf);
202
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000203 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000204
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000205 if(button_status() & BUTTON_REC || rc_on_button) {
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000206 lcd_puts(0, 8, "Starting original firmware...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000207 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000208 start_iriver_fw();
209 }
210
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000211 if(on_button & button_hold()) {
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000212 lcd_puts(0, 8, "HOLD switch on, power off...");
213 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000214 sleep(HZ*2);
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000215 /* Reset the cookie for the crt0 crash check */
216 asm(" move.l #0,%d0");
217 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000218 power_off();
219 }
220#if 0
221 if((button_status() & BUTTON_RC_ON) & remote_button_hold()) {
222 lcd_puts(0, 8, "HOLD switch on, power off...");
223 lcd_update();
224 sleep(HZ/2);
225 power_off();
226 }
227#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000228
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000229 rc = ata_init();
230 if(rc)
231 {
232#ifdef HAVE_LCD_BITMAP
233 char str[32];
234 lcd_clear_display();
235 snprintf(str, 31, "ATA error: %d", rc);
236 lcd_puts(0, 1, str);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000237 lcd_update();
238 while(!(button_get(true) & BUTTON_REL));
239#endif
240 panicf("ata: %d", rc);
241 }
242
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000243 /* A hack to enter USB mode without using the USB thread */
244 if(usb_detect())
245 {
246 lcd_clear_display();
247 lcd_puts(0, 7, " Bootloader USB mode");
248 lcd_update();
249
250 ata_spin();
251 ata_enable(false);
252 usb_enable(true);
253 while(usb_detect())
254 {
255 ata_spin(); /* Prevent the drive from spinning down */
256 sleep(HZ);
257 }
258
259 system_reboot();
260 }
261
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000262 disk_init();
263
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000264 rc = disk_mount_all();
265 if (rc<=0)
266 {
267 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000268 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000269 while(button_get(true) != SYS_USB_CONNECTED) {};
270 }
271
272 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000273 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000274 i = load_firmware();
275 snprintf(buf, 256, "Result: %d", i);
276 lcd_puts(0, line++, buf);
277 lcd_update();
278
279 if(i == 0)
280 start_firmware();
281
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000282 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000283}
284
285/* These functions are present in the firmware library, but we reimplement
286 them here because the originals do a lot more than we want */
287
288void reset_poweroff_timer(void)
289{
290}
291
292void screen_dump(void)
293{
294}
295
296int dbg_ports(void)
297{
298 return 0;
299}
300
301void mpeg_stop(void)
302{
303}
304
305void usb_acknowledge(void)
306{
307}
308
309void usb_wait_for_disconnect(void)
310{
311}