blob: 5cc995c8551aeace67daebbd10e42205f1b96875 [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
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 Feltzinga39026a2005-07-08 15:36:12 +0000205 if(button_status() & BUTTON_REC) {
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 Feltzinga39026a2005-07-08 15:36:12 +0000211 if(on_button & button_hold() ||
212 rc_on_button & remote_button_hold()) {
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000213 lcd_puts(0, 8, "HOLD switch on, power off...");
214 lcd_update();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000215 sleep(HZ*2);
Linus Nielsen Feltzingd4ad14c2005-02-11 09:02:46 +0000216 /* Reset the cookie for the crt0 crash check */
217 asm(" move.l #0,%d0");
218 asm(" move.l %d0,0x10017ffc");
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000219 power_off();
220 }
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000221
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000222 rc = ata_init();
223 if(rc)
224 {
225#ifdef HAVE_LCD_BITMAP
226 char str[32];
227 lcd_clear_display();
228 snprintf(str, 31, "ATA error: %d", rc);
229 lcd_puts(0, 1, str);
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000230 lcd_update();
231 while(!(button_get(true) & BUTTON_REL));
232#endif
233 panicf("ata: %d", rc);
234 }
235
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000236 /* A hack to enter USB mode without using the USB thread */
237 if(usb_detect())
238 {
239 lcd_clear_display();
240 lcd_puts(0, 7, " Bootloader USB mode");
241 lcd_update();
242
243 ata_spin();
244 ata_enable(false);
245 usb_enable(true);
246 while(usb_detect())
247 {
248 ata_spin(); /* Prevent the drive from spinning down */
249 sleep(HZ);
250 }
251
252 system_reboot();
253 }
254
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000255 disk_init();
256
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000257 rc = disk_mount_all();
258 if (rc<=0)
259 {
260 lcd_clear_display();
Linus Nielsen Feltzing8e958b52005-05-20 18:16:45 +0000261 lcd_puts(0, 0, "No partition found");
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000262 while(button_get(true) != SYS_USB_CONNECTED) {};
263 }
264
265 lcd_puts(0, line++, "Loading firmware");
Linus Nielsen Feltzing2a3856f2005-02-07 01:47:47 +0000266 lcd_update();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000267 i = load_firmware();
268 snprintf(buf, 256, "Result: %d", i);
269 lcd_puts(0, line++, buf);
270 lcd_update();
271
272 if(i == 0)
273 start_firmware();
274
Linus Nielsen Feltzingb8a00d22005-02-10 21:55:48 +0000275 start_iriver_fw();
Linus Nielsen Feltzingd3971452005-01-28 12:51:10 +0000276}
277
278/* These functions are present in the firmware library, but we reimplement
279 them here because the originals do a lot more than we want */
280
281void reset_poweroff_timer(void)
282{
283}
284
285void screen_dump(void)
286{
287}
288
289int dbg_ports(void)
290{
291 return 0;
292}
293
294void mpeg_stop(void)
295{
296}
297
298void usb_acknowledge(void)
299{
300}
301
302void usb_wait_for_disconnect(void)
303{
304}