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 | |
Linus Nielsen Feltzing | 62c768c | 2005-07-09 07:46:42 +0000 | [diff] [blame] | 38 | #define DRAM_START 0x31000000 |
| 39 | |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 40 | #ifdef IRIVER_H100 |
| 41 | #define MODEL_NUMBER 1 |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 42 | #else |
| 43 | #define MODEL_NUMBER 0 |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 44 | #endif |
| 45 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 46 | int line = 0; |
| 47 | |
Linus Nielsen Feltzing | a75f0e5 | 2005-07-09 09:04:37 +0000 | [diff] [blame] | 48 | char *modelname[] = |
| 49 | { |
| 50 | "H120/140", |
| 51 | "H110/115", |
| 52 | "H300" |
| 53 | }; |
| 54 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 55 | int usb_screen(void) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 60 | static 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 | |
| 81 | bool usb_detect(void) |
| 82 | { |
| 83 | return (GPIO1_READ & 0x80)?true:false; |
| 84 | } |
| 85 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 86 | void start_iriver_fw(void) |
| 87 | { |
| 88 | asm(" move.w #0x2700,%sr"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 89 | /* Reset the cookie for the crt0 crash check */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 90 | asm(" move.l #0,%d0"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 91 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 92 | asm(" movec.l %d0,%vbr"); |
| 93 | asm(" move.l 0,%sp"); |
| 94 | asm(" lea.l 8,%a0"); |
| 95 | asm(" jmp (%a0)"); |
| 96 | } |
| 97 | |
| 98 | int load_firmware(void) |
| 99 | { |
| 100 | int fd; |
| 101 | int rc; |
| 102 | int len; |
| 103 | unsigned long chksum; |
Linus Nielsen Feltzing | a75f0e5 | 2005-07-09 09:04:37 +0000 | [diff] [blame] | 104 | char model[5]; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 105 | unsigned long sum; |
| 106 | int i; |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 107 | unsigned char *buf = (unsigned char *)DRAM_START; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 108 | char str[80]; |
| 109 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 110 | fd = open("/rockbox.iriver", O_RDONLY); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 111 | if(fd < 0) |
| 112 | return -1; |
| 113 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 114 | len = filesize(fd) - 8; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 115 | |
| 116 | snprintf(str, 80, "Length: %x", len); |
| 117 | lcd_puts(0, line++, str); |
| 118 | lcd_update(); |
| 119 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 120 | lseek(fd, FIRMWARE_OFFSET_FILE_CRC, SEEK_SET); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 121 | |
| 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 Feltzing | a75f0e5 | 2005-07-09 09:04:37 +0000 | [diff] [blame] | 130 | 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 Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 140 | lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 141 | |
| 142 | rc = read(fd, buf, len); |
| 143 | if(rc < len) |
| 144 | return -4; |
| 145 | |
| 146 | close(fd); |
| 147 | |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 148 | sum = MODEL_NUMBER; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 149 | |
| 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 Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 164 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 165 | void start_firmware(void) |
| 166 | { |
| 167 | asm(" move.w #0x2700,%sr"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 168 | /* Reset the cookie for the crt0 crash check */ |
| 169 | asm(" move.l #0,%d0"); |
| 170 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 171 | asm(" move.l %0,%%d0" :: "i"(DRAM_START)); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 172 | asm(" movec.l %d0,%vbr"); |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 173 | asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START)); |
| 174 | asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4))); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 175 | asm(" jmp (%a0)"); |
| 176 | } |
| 177 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 178 | void main(void) |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 179 | { |
| 180 | int i; |
| 181 | int rc; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 182 | char buf[256]; |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 183 | 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 Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 199 | |
| 200 | power_init(); |
| 201 | system_init(); |
| 202 | kernel_init(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 203 | |
| 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 Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 209 | 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 Feltzing | a75f0e5 | 2005-07-09 09:04:37 +0000 | [diff] [blame] | 218 | snprintf(buf, 256, "Rockboot version 3"); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 219 | lcd_puts(0, line++, buf); |
Linus Nielsen Feltzing | d1af08f | 2005-07-09 19:48:22 +0000 | [diff] [blame] | 220 | lcd_update(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 221 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 222 | sleep(HZ/50); /* Allow the button driver to check the buttons */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 223 | |
Linus Nielsen Feltzing | d1af08f | 2005-07-09 19:48:22 +0000 | [diff] [blame] | 224 | if(((button_status() & BUTTON_REC) == BUTTON_REC) || |
| 225 | ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) { |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 226 | lcd_puts(0, 8, "Starting original firmware..."); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 227 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 228 | start_iriver_fw(); |
| 229 | } |
| 230 | |
Linus Nielsen Feltzing | a39026a | 2005-07-08 15:36:12 +0000 | [diff] [blame] | 231 | if(on_button & button_hold() || |
| 232 | rc_on_button & remote_button_hold()) { |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 233 | lcd_puts(0, 8, "HOLD switch on, power off..."); |
| 234 | lcd_update(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 235 | sleep(HZ*2); |
Linus Nielsen Feltzing | d4ad14c | 2005-02-11 09:02:46 +0000 | [diff] [blame] | 236 | /* Reset the cookie for the crt0 crash check */ |
| 237 | asm(" move.l #0,%d0"); |
| 238 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 239 | power_off(); |
| 240 | } |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 241 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 242 | 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 Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 250 | lcd_update(); |
| 251 | while(!(button_get(true) & BUTTON_REL)); |
| 252 | #endif |
| 253 | panicf("ata: %d", rc); |
| 254 | } |
| 255 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 256 | /* 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 Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 275 | disk_init(); |
| 276 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 277 | rc = disk_mount_all(); |
| 278 | if (rc<=0) |
| 279 | { |
| 280 | lcd_clear_display(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 281 | lcd_puts(0, 0, "No partition found"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 282 | while(button_get(true) != SYS_USB_CONNECTED) {}; |
| 283 | } |
| 284 | |
| 285 | lcd_puts(0, line++, "Loading firmware"); |
Linus Nielsen Feltzing | 2a3856f | 2005-02-07 01:47:47 +0000 | [diff] [blame] | 286 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 287 | 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 Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 295 | start_iriver_fw(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 296 | } |
| 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 | |
| 301 | void reset_poweroff_timer(void) |
| 302 | { |
| 303 | } |
| 304 | |
| 305 | void screen_dump(void) |
| 306 | { |
| 307 | } |
| 308 | |
| 309 | int dbg_ports(void) |
| 310 | { |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | void mpeg_stop(void) |
| 315 | { |
| 316 | } |
| 317 | |
| 318 | void usb_acknowledge(void) |
| 319 | { |
| 320 | } |
| 321 | |
| 322 | void usb_wait_for_disconnect(void) |
| 323 | { |
| 324 | } |