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; |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 186 | int adc_battery, battery_voltage, batt_int, batt_frac; |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 187 | |
| 188 | /* Read the buttons early */ |
| 189 | |
| 190 | /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs */ |
| 191 | GPIO1_FUNCTION |= 0x00100062; |
| 192 | GPIO1_ENABLE &= ~0x00100062; |
| 193 | |
| 194 | data = GPIO1_READ; |
| 195 | if ((data & 0x20) == 0) |
| 196 | on_button = true; |
| 197 | |
| 198 | if ((data & 0x40) == 0) |
| 199 | rc_on_button = true; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 200 | |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 201 | /* Backlight ON */ |
| 202 | or_l(0x00020000, &GPIO1_ENABLE); |
| 203 | or_l(0x00020000, &GPIO1_FUNCTION); |
| 204 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 205 | power_init(); |
| 206 | system_init(); |
| 207 | kernel_init(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 208 | |
| 209 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ |
| 210 | /* Set up waitstates for the peripherals */ |
| 211 | set_cpu_frequency(0); /* PLL off */ |
| 212 | #endif |
| 213 | |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 214 | /* UDA1380 RESET */ |
| 215 | GPIO_OUT |= (1<<29); |
| 216 | GPIO_ENABLE |= (1<<29); |
| 217 | GPIO_FUNCTION |= (1<<29); |
| 218 | sleep(HZ/100); |
| 219 | GPIO_OUT &= ~(1<<29); |
| 220 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 221 | backlight_init(); |
| 222 | set_irq_level(0); |
| 223 | lcd_init(); |
| 224 | font_init(); |
| 225 | adc_init(); |
| 226 | button_init(); |
| 227 | |
| 228 | lcd_setfont(FONT_SYSFIXED); |
| 229 | |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 230 | lcd_puts(0, line++, "Rockboot version CVS"); |
Linus Nielsen Feltzing | d1af08f | 2005-07-09 19:48:22 +0000 | [diff] [blame] | 231 | lcd_update(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 232 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 233 | sleep(HZ/50); /* Allow the button driver to check the buttons */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 234 | |
Linus Nielsen Feltzing | d1af08f | 2005-07-09 19:48:22 +0000 | [diff] [blame] | 235 | if(((button_status() & BUTTON_REC) == BUTTON_REC) || |
| 236 | ((button_status() & BUTTON_RC_REC) == BUTTON_RC_REC)) { |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 237 | lcd_puts(0, 8, "Starting original firmware..."); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 238 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 239 | start_iriver_fw(); |
| 240 | } |
| 241 | |
Linus Nielsen Feltzing | a39026a | 2005-07-08 15:36:12 +0000 | [diff] [blame] | 242 | if(on_button & button_hold() || |
| 243 | rc_on_button & remote_button_hold()) { |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 244 | lcd_puts(0, 8, "HOLD switch on, power off..."); |
| 245 | lcd_update(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 246 | sleep(HZ*2); |
Linus Nielsen Feltzing | d4ad14c | 2005-02-11 09:02:46 +0000 | [diff] [blame] | 247 | /* Reset the cookie for the crt0 crash check */ |
| 248 | asm(" move.l #0,%d0"); |
| 249 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 250 | power_off(); |
| 251 | } |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 252 | |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 253 | adc_battery = adc_read(ADC_BATTERY); |
| 254 | |
| 255 | battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000; |
| 256 | batt_int = battery_voltage / 100; |
| 257 | batt_frac = battery_voltage % 100; |
| 258 | |
| 259 | snprintf(buf, 32, "Batt: %d.%02dV", batt_int, batt_frac); |
| 260 | lcd_puts(0, line++, buf); |
| 261 | lcd_update(); |
| 262 | |
| 263 | if(battery_voltage <= 300) { |
| 264 | line++; |
| 265 | lcd_puts(0, line++, "WARNING! BATTERY LOW!!"); |
| 266 | lcd_update(); |
| 267 | sleep(HZ*2); |
| 268 | } |
| 269 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 270 | rc = ata_init(); |
| 271 | if(rc) |
| 272 | { |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 273 | char str[32]; |
| 274 | lcd_clear_display(); |
| 275 | snprintf(str, 31, "ATA error: %d", rc); |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 276 | lcd_puts(0, line++, str); |
| 277 | lcd_puts(0, line++, "Insert USB cable and press"); |
| 278 | lcd_puts(0, line++, "a button"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 279 | lcd_update(); |
| 280 | while(!(button_get(true) & BUTTON_REL)); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 283 | /* A hack to enter USB mode without using the USB thread */ |
| 284 | if(usb_detect()) |
| 285 | { |
| 286 | lcd_clear_display(); |
| 287 | lcd_puts(0, 7, " Bootloader USB mode"); |
| 288 | lcd_update(); |
| 289 | |
| 290 | ata_spin(); |
| 291 | ata_enable(false); |
| 292 | usb_enable(true); |
| 293 | while(usb_detect()) |
| 294 | { |
| 295 | ata_spin(); /* Prevent the drive from spinning down */ |
| 296 | sleep(HZ); |
| 297 | } |
| 298 | |
| 299 | system_reboot(); |
| 300 | } |
| 301 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 302 | disk_init(); |
| 303 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 304 | rc = disk_mount_all(); |
| 305 | if (rc<=0) |
| 306 | { |
| 307 | lcd_clear_display(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 308 | lcd_puts(0, 0, "No partition found"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 309 | while(button_get(true) != SYS_USB_CONNECTED) {}; |
| 310 | } |
| 311 | |
| 312 | lcd_puts(0, line++, "Loading firmware"); |
Linus Nielsen Feltzing | 2a3856f | 2005-02-07 01:47:47 +0000 | [diff] [blame] | 313 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 314 | i = load_firmware(); |
| 315 | snprintf(buf, 256, "Result: %d", i); |
| 316 | lcd_puts(0, line++, buf); |
| 317 | lcd_update(); |
| 318 | |
| 319 | if(i == 0) |
| 320 | start_firmware(); |
| 321 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 322 | start_iriver_fw(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* These functions are present in the firmware library, but we reimplement |
| 326 | them here because the originals do a lot more than we want */ |
| 327 | |
| 328 | void reset_poweroff_timer(void) |
| 329 | { |
| 330 | } |
| 331 | |
| 332 | void screen_dump(void) |
| 333 | { |
| 334 | } |
| 335 | |
| 336 | int dbg_ports(void) |
| 337 | { |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | void mpeg_stop(void) |
| 342 | { |
| 343 | } |
| 344 | |
| 345 | void usb_acknowledge(void) |
| 346 | { |
| 347 | } |
| 348 | |
| 349 | void usb_wait_for_disconnect(void) |
| 350 | { |
| 351 | } |