blob: 72b7e0bc5276cc295244b0d4bade01b92b28ec96 [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
38int line = 0;
39
40int usb_screen(void)
41{
42 return 0;
43}
44
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +000045static void usb_enable(bool on)
46{
47 GPIO_OUT &= ~0x01000000; /* GPIO24 is the Cypress chip power */
48 GPIO_ENABLE |= 0x01000000;
49 GPIO_FUNCTION |= 0x01000000;
50
51 GPIO1_FUNCTION |= 0x00000080; /* GPIO39 is the USB detect input */
52
53 if(on)
54 {
55 /* Power on the Cypress chip */
56 GPIO_OUT |= 0x01000000;
57 sleep(2);
58 }
59 else
60 {
61 /* Power off the Cypress chip */
62 GPIO_OUT &= ~0x01000000;
63 }
64}
65
66bool usb_detect(void)
67{
68 return (GPIO1_READ & 0x80)?true:false;
69}
70
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000071void start_iriver_fw(void)
72{
73 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000074 /* Reset the cookie for the crt0 crash check */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000075 asm(" move.l #0,%d0");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +000076 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000077 asm(" movec.l %d0,%vbr");
78 asm(" move.l 0,%sp");
79 asm(" lea.l 8,%a0");
80 asm(" jmp (%a0)");
81}
82
83int load_firmware(void)
84{
85 int fd;
86 int rc;
87 int len;
88 unsigned long chksum;
89 unsigned long sum;
90 int i;
Linus Nielsen Feltzing1c40d3c2005-04-20 06:48:17 +000091 unsigned char *buf = (unsigned char *)0x31000000;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000092 char str[80];
93
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +000094 fd = open("/rockbox.iriver", O_RDONLY);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000095 if(fd < 0)
96 return -1;
97
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +000098 len = filesize(fd) - 8;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +000099
100 snprintf(str, 80, "Length: %x", len);
101 lcd_puts(0, line++, str);
102 lcd_update();
103
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000104 lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000105
106 rc = read(fd, &chksum, 4);
107 if(rc < 4)
108 return -2;
109
110 snprintf(str, 80, "Checksum: %x", chksum);
111 lcd_puts(0, line++, str);
112 lcd_update();
113
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000114 lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000115
116 rc = read(fd, buf, len);
117 if(rc < len)
118 return -4;
119
120 close(fd);
121
122 sum = 0;
123
124 for(i = 0;i < len;i++) {
125 sum += buf[i];
126 }
127
128 snprintf(str, 80, "Sum: %x", sum);
129 lcd_puts(0, line++, str);
130 lcd_update();
131
132 if(sum != chksum)
133 return -5;
134
135 return 0;
136}
137
138void start_firmware(void)
139{
140 asm(" move.w #0x2700,%sr");
Linus Nielsen Feltzingf1530d82005-02-04 18:24:58 +0000141 /* Reset the cookie for the crt0 crash check */
142 asm(" move.l #0,%d0");
143 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000144 asm(" move.l #0x30000000,%d0");
145 asm(" movec.l %d0,%vbr");
146 asm(" move.l 0x30000000,%sp");
147 asm(" move.l 0x30000004,%a0");
148 asm(" jmp (%a0)");
149}
150
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000151void main(void)
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000152{
153 int i;
154 int rc;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000155 char buf[256];
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000156 bool rc_on_button = false;
157 bool on_button = false;
158 int data;
159
160 /* Read the buttons early */
161
162 /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs */
163 GPIO1_FUNCTION |= 0x00100062;
164 GPIO1_ENABLE &= ~0x00100062;
165
166 data = GPIO1_READ;
167 if ((data & 0x20) == 0)
168 on_button = true;
169
170 if ((data & 0x40) == 0)
171 rc_on_button = true;
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000172
173 power_init();
174 system_init();
175 kernel_init();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000176
177#ifdef HAVE_ADJUSTABLE_CPU_FREQ
178 /* Set up waitstates for the peripherals */
179 set_cpu_frequency(0); /* PLL off */
180#endif
181
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000182 backlight_init();
183 set_irq_level(0);
184 lcd_init();
185 font_init();
186 adc_init();
187 button_init();
188
189 lcd_setfont(FONT_SYSFIXED);
190
Linus Nielsen Feltzing794220c2005-06-02 21:40:52 +0000191 snprintf(buf, 256, "Rockboot version 2");
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000192 lcd_puts(0, line++, buf);
193
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000194 sleep(HZ/50); /* Allow the button driver to check the buttons */
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000195
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000196 if(button_status() & BUTTON_REC || rc_on_button) {
Linus Nielsen Feltzing2a77d3a2005-01-28 13:27:58 +0000197 lcd_puts(0, 8, "Starting original firmware...");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000198 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000199 start_iriver_fw();
200 }
201
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000202 if(on_button & button_hold()) {
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000203 lcd_puts(0, 8, "HOLD switch on, power off...");
204 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000205 sleep(HZ*2);
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000206 /* Reset the cookie for the crt0 crash check */
207 asm(" move.l #0,%d0");
208 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000209 power_off();
210 }
211#if 0
212 if((button_status() & BUTTON_RC_ON) & remote_button_hold()) {
213 lcd_puts(0, 8, "HOLD switch on, power off...");
214 lcd_update();
215 sleep(HZ/2);
216 power_off();
217 }
218#endif
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000219
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000220 rc = ata_init();
221 if(rc)
222 {
223#ifdef HAVE_LCD_BITMAP
224 char str[32];
225 lcd_clear_display();
226 snprintf(str, 31, "ATA error: %d", rc);
227 lcd_puts(0, 1, str);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000228 lcd_update();
229 while(!(button_get(true) & BUTTON_REL));
230#endif
231 panicf("ata: %d", rc);
232 }
233
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000234 /* A hack to enter USB mode without using the USB thread */
235 if(usb_detect())
236 {
237 lcd_clear_display();
238 lcd_puts(0, 7, " Bootloader USB mode");
239 lcd_update();
240
241 ata_spin();
242 ata_enable(false);
243 usb_enable(true);
244 while(usb_detect())
245 {
246 ata_spin(); /* Prevent the drive from spinning down */
247 sleep(HZ);
248 }
249
250 system_reboot();
251 }
252
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000253 disk_init();
254
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000255 rc = disk_mount_all();
256 if (rc<=0)
257 {
258 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000259 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000260 while(button_get(true) != SYS_USB_CONNECTED) {};
261 }
262
263 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000264 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000265 i = load_firmware();
266 snprintf(buf, 256, "Result: %d", i);
267 lcd_puts(0, line++, buf);
268 lcd_update();
269
270 if(i == 0)
271 start_firmware();
272
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000273 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000274}
275
276/* These functions are present in the firmware library, but we reimplement
277 them here because the originals do a lot more than we want */
278
279void reset_poweroff_timer(void)
280{
281}
282
283void screen_dump(void)
284{
285}
286
287int dbg_ports(void)
288{
289 return 0;
290}
291
292void mpeg_stop(void)
293{
294}
295
296void usb_acknowledge(void)
297{
298}
299
300void usb_wait_for_disconnect(void)
301{
302}