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" |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame^] | 37 | #include "uda1380.h" |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 38 | |
Linus Nielsen Feltzing | 62c768c | 2005-07-09 07:46:42 +0000 | [diff] [blame] | 39 | #define DRAM_START 0x31000000 |
| 40 | |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 41 | #ifdef IRIVER_H100 |
| 42 | #define MODEL_NUMBER 1 |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 43 | #else |
| 44 | #define MODEL_NUMBER 0 |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 47 | int line = 0; |
| 48 | |
| 49 | int usb_screen(void) |
| 50 | { |
| 51 | return 0; |
| 52 | } |
| 53 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 54 | static void usb_enable(bool on) |
| 55 | { |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame^] | 56 | and_l(~0x01000000, &GPIO_OUT); /* GPIO24 is the Cypress chip power */ |
| 57 | or_l(0x01000000, &GPIO_ENABLE); |
| 58 | or_l(0x01000000, &GPIO_FUNCTION); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 59 | |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame^] | 60 | or_l(0x00000080, &GPIO1_FUNCTION); /* GPIO39 is the USB detect input */ |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 61 | |
| 62 | if(on) |
| 63 | { |
| 64 | /* Power on the Cypress chip */ |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame^] | 65 | or_l(0x01000000, &GPIO_OUT); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 66 | sleep(2); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | /* Power off the Cypress chip */ |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame^] | 71 | and_l(~0x01000000, &GPIO_OUT); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | bool usb_detect(void) |
| 76 | { |
| 77 | return (GPIO1_READ & 0x80)?true:false; |
| 78 | } |
| 79 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 80 | void start_iriver_fw(void) |
| 81 | { |
| 82 | asm(" move.w #0x2700,%sr"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 83 | /* Reset the cookie for the crt0 crash check */ |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 84 | asm(" move.l #0,%d0"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 85 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 86 | asm(" movec.l %d0,%vbr"); |
| 87 | asm(" move.l 0,%sp"); |
| 88 | asm(" lea.l 8,%a0"); |
| 89 | asm(" jmp (%a0)"); |
| 90 | } |
| 91 | |
| 92 | int load_firmware(void) |
| 93 | { |
| 94 | int fd; |
| 95 | int rc; |
| 96 | int len; |
| 97 | unsigned long chksum; |
Linus Nielsen Feltzing | a75f0e5 | 2005-07-09 09:04:37 +0000 | [diff] [blame] | 98 | char model[5]; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 99 | unsigned long sum; |
| 100 | int i; |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 101 | unsigned char *buf = (unsigned char *)DRAM_START; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 102 | char str[80]; |
| 103 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 104 | fd = open("/rockbox.iriver", O_RDONLY); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 105 | if(fd < 0) |
| 106 | return -1; |
| 107 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 108 | len = filesize(fd) - 8; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 109 | |
| 110 | snprintf(str, 80, "Length: %x", len); |
| 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_CRC, SEEK_SET); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 115 | |
| 116 | rc = read(fd, &chksum, 4); |
| 117 | if(rc < 4) |
| 118 | return -2; |
| 119 | |
| 120 | snprintf(str, 80, "Checksum: %x", chksum); |
| 121 | lcd_puts(0, line++, str); |
| 122 | lcd_update(); |
| 123 | |
Linus Nielsen Feltzing | a75f0e5 | 2005-07-09 09:04:37 +0000 | [diff] [blame] | 124 | rc = read(fd, model, 4); |
| 125 | if(rc < 4) |
| 126 | return -3; |
| 127 | |
| 128 | model[4] = 0; |
| 129 | |
| 130 | snprintf(str, 80, "Model name: %s", model); |
| 131 | lcd_puts(0, line++, str); |
| 132 | lcd_update(); |
| 133 | |
Linus Nielsen Feltzing | 2a77d3a | 2005-01-28 13:27:58 +0000 | [diff] [blame] | 134 | lseek(fd, FIRMWARE_OFFSET_FILE_DATA, SEEK_SET); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 135 | |
| 136 | rc = read(fd, buf, len); |
| 137 | if(rc < len) |
| 138 | return -4; |
| 139 | |
| 140 | close(fd); |
| 141 | |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 142 | sum = MODEL_NUMBER; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 143 | |
| 144 | for(i = 0;i < len;i++) { |
| 145 | sum += buf[i]; |
| 146 | } |
| 147 | |
| 148 | snprintf(str, 80, "Sum: %x", sum); |
| 149 | lcd_puts(0, line++, str); |
| 150 | lcd_update(); |
| 151 | |
| 152 | if(sum != chksum) |
| 153 | return -5; |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 158 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 159 | void start_firmware(void) |
| 160 | { |
| 161 | asm(" move.w #0x2700,%sr"); |
Linus Nielsen Feltzing | f1530d8 | 2005-02-04 18:24:58 +0000 | [diff] [blame] | 162 | /* Reset the cookie for the crt0 crash check */ |
| 163 | asm(" move.l #0,%d0"); |
| 164 | asm(" move.l %d0,0x10017ffc"); |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 165 | asm(" move.l %0,%%d0" :: "i"(DRAM_START)); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 166 | asm(" movec.l %d0,%vbr"); |
Linus Nielsen Feltzing | e82df4e | 2005-07-08 15:09:44 +0000 | [diff] [blame] | 167 | asm(" move.l %0,%%sp" :: "m"(*(int *)DRAM_START)); |
| 168 | asm(" move.l %0,%%a0" :: "m"(*(int *)(DRAM_START+4))); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 169 | asm(" jmp (%a0)"); |
| 170 | } |
| 171 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 172 | void main(void) |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 173 | { |
| 174 | int i; |
| 175 | int rc; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 176 | char buf[256]; |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 177 | bool rc_on_button = false; |
| 178 | bool on_button = false; |
| 179 | int data; |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 180 | int adc_battery, battery_voltage, batt_int, batt_frac; |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 181 | |
| 182 | /* Read the buttons early */ |
| 183 | |
| 184 | /* Set GPIO33, GPIO37, GPIO38 and GPIO52 as general purpose inputs */ |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame^] | 185 | or_l(0x00100062, &GPIO1_FUNCTION); |
| 186 | and_l(~0x00100062, &GPIO1_ENABLE); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 187 | |
| 188 | data = GPIO1_READ; |
| 189 | if ((data & 0x20) == 0) |
| 190 | on_button = true; |
| 191 | |
| 192 | if ((data & 0x40) == 0) |
| 193 | rc_on_button = true; |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 194 | |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 195 | /* Backlight ON */ |
| 196 | or_l(0x00020000, &GPIO1_ENABLE); |
| 197 | or_l(0x00020000, &GPIO1_FUNCTION); |
| 198 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 199 | power_init(); |
| 200 | system_init(); |
| 201 | kernel_init(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 202 | |
| 203 | #ifdef HAVE_ADJUSTABLE_CPU_FREQ |
| 204 | /* Set up waitstates for the peripherals */ |
| 205 | set_cpu_frequency(0); /* PLL off */ |
| 206 | #endif |
Linus Nielsen Feltzing | d2ca7fc | 2005-07-12 05:25:42 +0000 | [diff] [blame^] | 207 | |
| 208 | uda1380_reset(); |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 209 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 210 | backlight_init(); |
| 211 | set_irq_level(0); |
| 212 | lcd_init(); |
| 213 | font_init(); |
| 214 | adc_init(); |
| 215 | button_init(); |
| 216 | |
| 217 | lcd_setfont(FONT_SYSFIXED); |
| 218 | |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 219 | lcd_puts(0, line++, "Rockboot version CVS"); |
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 | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 242 | adc_battery = adc_read(ADC_BATTERY); |
| 243 | |
| 244 | battery_voltage = (adc_battery * BATTERY_SCALE_FACTOR) / 10000; |
| 245 | batt_int = battery_voltage / 100; |
| 246 | batt_frac = battery_voltage % 100; |
| 247 | |
| 248 | snprintf(buf, 32, "Batt: %d.%02dV", batt_int, batt_frac); |
| 249 | lcd_puts(0, line++, buf); |
| 250 | lcd_update(); |
| 251 | |
| 252 | if(battery_voltage <= 300) { |
| 253 | line++; |
| 254 | lcd_puts(0, line++, "WARNING! BATTERY LOW!!"); |
| 255 | lcd_update(); |
| 256 | sleep(HZ*2); |
| 257 | } |
| 258 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 259 | rc = ata_init(); |
| 260 | if(rc) |
| 261 | { |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 262 | char str[32]; |
| 263 | lcd_clear_display(); |
| 264 | snprintf(str, 31, "ATA error: %d", rc); |
Linus Nielsen Feltzing | dfa8ecb | 2005-07-11 14:01:45 +0000 | [diff] [blame] | 265 | lcd_puts(0, line++, str); |
| 266 | lcd_puts(0, line++, "Insert USB cable and press"); |
| 267 | lcd_puts(0, line++, "a button"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 268 | lcd_update(); |
| 269 | while(!(button_get(true) & BUTTON_REL)); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 270 | } |
| 271 | |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 272 | /* A hack to enter USB mode without using the USB thread */ |
| 273 | if(usb_detect()) |
| 274 | { |
| 275 | lcd_clear_display(); |
| 276 | lcd_puts(0, 7, " Bootloader USB mode"); |
| 277 | lcd_update(); |
| 278 | |
| 279 | ata_spin(); |
| 280 | ata_enable(false); |
| 281 | usb_enable(true); |
| 282 | while(usb_detect()) |
| 283 | { |
| 284 | ata_spin(); /* Prevent the drive from spinning down */ |
| 285 | sleep(HZ); |
| 286 | } |
| 287 | |
| 288 | system_reboot(); |
| 289 | } |
| 290 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 291 | disk_init(); |
| 292 | |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 293 | rc = disk_mount_all(); |
| 294 | if (rc<=0) |
| 295 | { |
| 296 | lcd_clear_display(); |
Linus Nielsen Feltzing | 8e958b5 | 2005-05-20 18:16:45 +0000 | [diff] [blame] | 297 | lcd_puts(0, 0, "No partition found"); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 298 | while(button_get(true) != SYS_USB_CONNECTED) {}; |
| 299 | } |
| 300 | |
| 301 | lcd_puts(0, line++, "Loading firmware"); |
Linus Nielsen Feltzing | 2a3856f | 2005-02-07 01:47:47 +0000 | [diff] [blame] | 302 | lcd_update(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 303 | i = load_firmware(); |
| 304 | snprintf(buf, 256, "Result: %d", i); |
| 305 | lcd_puts(0, line++, buf); |
| 306 | lcd_update(); |
| 307 | |
| 308 | if(i == 0) |
| 309 | start_firmware(); |
| 310 | |
Linus Nielsen Feltzing | b8a00d2 | 2005-02-10 21:55:48 +0000 | [diff] [blame] | 311 | start_iriver_fw(); |
Linus Nielsen Feltzing | d397145 | 2005-01-28 12:51:10 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* These functions are present in the firmware library, but we reimplement |
| 315 | them here because the originals do a lot more than we want */ |
| 316 | |
| 317 | void reset_poweroff_timer(void) |
| 318 | { |
| 319 | } |
| 320 | |
| 321 | void screen_dump(void) |
| 322 | { |
| 323 | } |
| 324 | |
| 325 | int dbg_ports(void) |
| 326 | { |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | void mpeg_stop(void) |
| 331 | { |
| 332 | } |
| 333 | |
| 334 | void usb_acknowledge(void) |
| 335 | { |
| 336 | } |
| 337 | |
| 338 | void usb_wait_for_disconnect(void) |
| 339 | { |
| 340 | } |