Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 1 | /*************************************************************************** |
| 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 | |
| 38 | int line = 0; |
| 39 | |
| 40 | int usb_screen(void) |
| 41 | { |
| 42 | return 0; |
| 43 | } |
| 44 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 45 | static 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 | |
| 66 | bool usb_detect(void) |
| 67 | { |
| 68 | return (GPIO1_READ & 0x80)?true:false; |
| 69 | } |
| 70 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 71 | void start_iriver_fw(void) |
| 72 | { |
| 73 | asm(" move.w #0x2700,%sr"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 74 | /* Reset the cookie for the crt0 crash check */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 75 | asm(" move.l #0,%d0"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 76 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 77 | asm(" movec.l %d0,%vbr"); |
| 78 | asm(" move.l 0,%sp"); |
| 79 | asm(" lea.l 8,%a0"); |
| 80 | asm(" jmp (%a0)"); |
| 81 | } |
| 82 | |
| 83 | int 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 Feltzing | 1c40d3c | 2005-04-20 06:48:17 +0000 | [diff] [blame] | 91 | unsigned char *buf = (unsigned char *)0x31000000; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 92 | char str[80]; |
| 93 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 94 | fd = open("/rockbox.iriver", O_RDONLY); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 95 | if(fd < 0) |
| 96 | return -1; |
| 97 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 98 | len = filesize(fd) - 8; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 99 | |
| 100 | snprintf(str, 80, "Length: %x", len); |
| 101 | lcd_puts(0, line++, str); |
| 102 | lcd_update(); |
| 103 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 104 | lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 105 | |
| 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 Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 114 | lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 115 | |
| 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 | |
| 138 | void start_firmware(void) |
| 139 | { |
| 140 | asm(" move.w #0x2700,%sr"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 141 | /* Reset the cookie for the crt0 crash check */ |
| 142 | asm(" move.l #0,%d0"); |
| 143 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 144 | 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 Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 151 | void main(void) |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 152 | { |
| 153 | int i; |
| 154 | int rc; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 155 | char buf[256]; |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 156 | 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 Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 172 | |
| 173 | power_init(); |
| 174 | system_init(); |
| 175 | kernel_init(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 176 | |
| 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 Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 182 | 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 Feltzing | 794220c | 2005-06-02 21:40:52 +0000 | [diff] [blame] | 191 | snprintf(buf, 256, "Rockboot version 2"); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 192 | lcd_puts(0, line++, buf); |
| 193 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 194 | sleep(HZ/50); /* Allow the button driver to check the buttons */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 195 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 196 | if(button_status() & BUTTON_REC || rc_on_button) { |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 197 | lcd_puts(0, 8, "Starting original firmware..."); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 198 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 199 | start_iriver_fw(); |
| 200 | } |
| 201 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 202 | if(on_button & button_hold()) { |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 203 | lcd_puts(0, 8, "HOLD switch on, power off..."); |
| 204 | lcd_update(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 205 | sleep(HZ*2); |
Linus Nielsen Feltzing | d4ad14c | 2005-02-11 09:02:46 +0000 | [diff] [blame] | 206 | /* Reset the cookie for the crt0 crash check */ |
| 207 | asm(" move.l #0,%d0"); |
| 208 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 209 | 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 Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 219 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 220 | 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 Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 228 | lcd_update(); |
| 229 | while(!(button_get(true) & BUTTON_REL)); |
| 230 | #endif |
| 231 | panicf("ata: %d", rc); |
| 232 | } |
| 233 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 234 | /* 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 Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 253 | disk_init(); |
| 254 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 255 | rc = disk_mount_all(); |
| 256 | if (rc<=0) |
| 257 | { |
| 258 | lcd_clear_display(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 259 | lcd_puts(0, 0, "No partition found"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 260 | while(button_get(true) != SYS_USB_CONNECTED) {}; |
| 261 | } |
| 262 | |
| 263 | lcd_puts(0, line++, "Loading firmware"); |
Linus Nielsen Feltzing | 2a3856f | 2005-02-07 01:47:47 +0000 | [diff] [blame] | 264 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 265 | 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 Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 273 | start_iriver_fw(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 274 | } |
| 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 | |
| 279 | void reset_poweroff_timer(void) |
| 280 | { |
| 281 | } |
| 282 | |
| 283 | void screen_dump(void) |
| 284 | { |
| 285 | } |
| 286 | |
| 287 | int dbg_ports(void) |
| 288 | { |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | void mpeg_stop(void) |
| 293 | { |
| 294 | } |
| 295 | |
| 296 | void usb_acknowledge(void) |
| 297 | { |
| 298 | } |
| 299 | |
| 300 | void usb_wait_for_disconnect(void) |
| 301 | { |
| 302 | } |