Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Carl-Daniel Hailfinger |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <ctype.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <usb.h> |
| 25 | #include "flash.h" |
| 26 | #include "spi.h" |
| 27 | |
| 28 | #define DEFAULT_TIMEOUT 3000 |
| 29 | usb_dev_handle *dediprog_handle; |
| 30 | |
| 31 | int dediprog_do_stuff(void); |
| 32 | |
| 33 | void print_hex(void *buf, size_t len) |
| 34 | { |
| 35 | size_t i; |
| 36 | |
| 37 | for (i = 0; i < len; i++) |
| 38 | msg_pdbg(" %02x", ((uint8_t *)buf)[i]); |
| 39 | } |
| 40 | |
| 41 | struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid) |
| 42 | { |
| 43 | struct usb_bus *bus; |
| 44 | struct usb_device *dev; |
| 45 | |
| 46 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 47 | for (dev = bus->devices; dev; dev = dev->next) |
| 48 | if ((dev->descriptor.idVendor == vid) && |
| 49 | (dev->descriptor.idProduct == pid)) |
| 50 | return dev; |
| 51 | |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | //int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); |
| 56 | |
| 57 | int dediprog_set_spi_voltage(uint16_t voltage) |
| 58 | { |
| 59 | int ret; |
| 60 | unsigned int mv; |
| 61 | |
| 62 | switch (voltage) { |
| 63 | case 0x0: |
| 64 | /* Admittedly this one is an assumption. */ |
| 65 | mv = 0; |
| 66 | break; |
| 67 | case 0x12: |
| 68 | mv = 1800; |
| 69 | break; |
| 70 | case 0x11: |
| 71 | mv = 2500; |
| 72 | break; |
| 73 | case 0x10: |
| 74 | mv = 3500; |
| 75 | break; |
| 76 | default: |
| 77 | msg_perr("Unknown voltage selector 0x%x! Aborting.\n", voltage); |
| 78 | return 1; |
| 79 | } |
| 80 | msg_pdbg("Setting SPI voltage to %u.%03u V\n", mv / 1000, mv % 1000); |
| 81 | |
| 82 | ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage, 0xff, NULL, 0x0, DEFAULT_TIMEOUT); |
| 83 | if (ret != 0x0) { |
| 84 | msg_perr("Command Set SPI Voltage 0x%x failed!\n", voltage); |
| 85 | return 1; |
| 86 | } |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /* After dediprog_set_spi_speed, the original app always calls |
| 91 | * dediprog_set_spi_voltage(0) and then |
| 92 | * dediprog_check_devicestring() four times in a row. |
| 93 | * After that, dediprog_command_a() is called. |
| 94 | * This looks suspiciously like the microprocessor in the SF100 has to be |
| 95 | * restarted/reinitialized in case the speed changes. |
| 96 | */ |
| 97 | int dediprog_set_spi_speed(uint16_t speed) |
| 98 | { |
| 99 | int ret; |
| 100 | unsigned int khz; |
| 101 | |
| 102 | /* Case 1 and 2 are in weird order. Probably an organically "grown" |
| 103 | * interface. |
| 104 | * Base frequency is 24000 kHz, divisors are (in order) |
| 105 | * 1, 3, 2, 8, 11, 16, 32, 64. |
| 106 | */ |
| 107 | switch (speed) { |
| 108 | case 0x0: |
| 109 | khz = 24000; |
| 110 | break; |
| 111 | case 0x1: |
| 112 | khz = 8000; |
| 113 | break; |
| 114 | case 0x2: |
| 115 | khz = 12000; |
| 116 | break; |
| 117 | case 0x3: |
| 118 | khz = 3000; |
| 119 | break; |
| 120 | case 0x4: |
| 121 | khz = 2180; |
| 122 | break; |
| 123 | case 0x5: |
| 124 | khz = 1500; |
| 125 | break; |
| 126 | case 0x6: |
| 127 | khz = 750; |
| 128 | break; |
| 129 | case 0x7: |
| 130 | khz = 375; |
| 131 | break; |
| 132 | default: |
| 133 | msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed); |
| 134 | return 1; |
| 135 | } |
| 136 | msg_pdbg("Setting SPI speed to %u kHz\n", khz); |
| 137 | |
| 138 | ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, 0x0, DEFAULT_TIMEOUT); |
| 139 | if (ret != 0x0) { |
| 140 | msg_perr("Command Set SPI Speed 0x%x failed!\n", speed); |
| 141 | return 1; |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 147 | { |
| 148 | /* Maximum read length is 4 bytes for now. */ |
| 149 | return spi_read_chunked(flash, buf, start, len, 4); |
| 150 | } |
| 151 | |
| 152 | int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 153 | const unsigned char *writearr, unsigned char *readarr) |
| 154 | { |
| 155 | int ret; |
| 156 | |
| 157 | /* Paranoid, but I don't want to be blamed if anything explodes. */ |
| 158 | if ((writecnt != 1) && (writecnt != 4)) |
| 159 | msg_perr("Untested writecnt=%i, aborting.\n", writecnt); |
| 160 | if (readcnt > 4) |
| 161 | msg_perr("Untested readcnt=%i, aborting.\n", readcnt); |
| 162 | if ((readcnt == 0) && (writecnt != 1)) |
| 163 | msg_perr("Untested writecnt=%i, readcnt=%i combination, " |
| 164 | "aborting.\n", writecnt, readcnt); |
| 165 | |
| 166 | ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT); |
| 167 | if (ret != writecnt) { |
| 168 | msg_perr("Command Send SPI failed, ret=%i, expected %i!\n", |
| 169 | ret, writecnt); |
| 170 | return 1; |
| 171 | } |
| 172 | if (!readcnt) |
| 173 | return 0; |
| 174 | memset(readarr, 0, readcnt); |
| 175 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT); |
| 176 | if (ret != readcnt) { |
| 177 | msg_perr("Command Receive SPI failed, ret=%i, expected %i!\n", |
| 178 | ret, readcnt); |
| 179 | return 1; |
| 180 | } |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | int dediprog_check_devicestring(void) |
| 185 | { |
| 186 | int ret; |
| 187 | char buf[0x11]; |
| 188 | |
| 189 | /* Command Prepare Receive Device String. */ |
| 190 | memset(buf, 0, sizeof(buf)); |
| 191 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT); |
| 192 | /* The char casting is needed to stop gcc complaining about an always true comparison. */ |
| 193 | if ((ret != 0x1) || (buf[0] != (char)0xff)) { |
| 194 | msg_perr("Unexpected response to Command Prepare Receive Device" |
| 195 | " String!\n"); |
| 196 | return 1; |
| 197 | } |
| 198 | /* Command Receive Device String. */ |
| 199 | memset(buf, 0, sizeof(buf)); |
| 200 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT); |
| 201 | if (ret != 0x10) { |
| 202 | msg_perr("Incomplete/failed Command Receive Device String!\n"); |
| 203 | return 1; |
| 204 | } |
| 205 | buf[0x10] = '\0'; |
| 206 | msg_pdbg("Found a %s\n", buf); |
| 207 | if (memcmp(buf, "SF100", 0x5)) { |
| 208 | msg_perr("Device not a SF100!\n"); |
| 209 | return 1; |
| 210 | } |
| 211 | /* Only these versions were tested. */ |
| 212 | if (memcmp(buf, "SF100 V:2.1.1 ", 0x10) && |
| 213 | memcmp(buf, "SF100 V:3.1.8 ", 0x10)) { |
| 214 | msg_perr("Unexpected firmware version!\n"); |
| 215 | return 1; |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | /* Command A seems to be some sort of device init. It is either followed by |
| 221 | * dediprog_check_devicestring (often) or Command A (often) or |
| 222 | * Command F (once). |
| 223 | */ |
| 224 | int dediprog_command_a(void) |
| 225 | { |
| 226 | int ret; |
| 227 | char buf[0x1]; |
| 228 | |
| 229 | memset(buf, 0, sizeof(buf)); |
| 230 | ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT); |
| 231 | if ((ret != 0x1) || (buf[0] != 0x6f)) { |
| 232 | msg_perr("Unexpected response to Command A!\n"); |
| 233 | return 1; |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* Command C is only sent after dediprog_check_devicestring, but not after every |
| 239 | * invocation of dediprog_check_devicestring. It is only sent after the first |
| 240 | * dediprog_command_a(); dediprog_check_devicestring() sequence in each session. |
| 241 | * I'm tempted to call this one start_SPI_engine or finish_init. |
| 242 | */ |
| 243 | int dediprog_command_c(void) |
| 244 | { |
| 245 | int ret; |
| 246 | |
| 247 | ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT); |
| 248 | if (ret != 0x0) { |
| 249 | msg_perr("Unexpected response to Command C!\n"); |
| 250 | return 1; |
| 251 | } |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /* Very strange. Seems to be a programmer keepalive or somesuch. |
| 256 | * Wait unsuccessfully for timeout ms to read one byte. |
| 257 | * Is usually called after setting voltage to 0. |
| 258 | */ |
| 259 | int dediprog_command_f(int timeout) |
| 260 | { |
| 261 | int ret; |
| 262 | char buf[0x1]; |
| 263 | |
| 264 | memset(buf, 0, sizeof(buf)); |
| 265 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout); |
| 266 | if (ret != 0x0) { |
| 267 | msg_perr("Unexpected response to Command F!\n"); |
| 268 | return 1; |
| 269 | } |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | /* URB numbers refer to the first log ever captured. */ |
| 274 | int dediprog_init(void) |
| 275 | { |
| 276 | struct usb_device *dev; |
| 277 | |
| 278 | msg_pspew("%s\n", __func__); |
| 279 | |
| 280 | /* Here comes the USB stuff. */ |
| 281 | usb_init(); |
| 282 | usb_find_busses(); |
| 283 | usb_find_devices(); |
| 284 | dev = get_device_by_vid_pid(0x0483, 0xdada); |
| 285 | if (!dev) { |
| 286 | msg_perr("Could not find a Dediprog SF100 on USB!\n"); |
| 287 | return 1; |
| 288 | } |
| 289 | msg_pdbg("Found USB device (%04x:%04x).\n", |
| 290 | dev->descriptor.idVendor, |
| 291 | dev->descriptor.idProduct); |
| 292 | dediprog_handle = usb_open(dev); |
| 293 | /* URB 6. Command A. */ |
| 294 | if (dediprog_command_a()) |
| 295 | return 1; |
| 296 | /* URB 7. Command A. */ |
| 297 | if (dediprog_command_a()) |
| 298 | return 1; |
| 299 | /* URB 8. Command Prepare Receive Device String. */ |
| 300 | /* URB 9. Command Receive Device String. */ |
| 301 | if (dediprog_check_devicestring()) |
| 302 | return 1; |
| 303 | /* URB 10. Command C. */ |
| 304 | if (dediprog_command_c()) |
| 305 | return 1; |
| 306 | /* URB 11. Command Set SPI Voltage. */ |
| 307 | if (dediprog_set_spi_voltage(0x10)) |
| 308 | return 1; |
| 309 | |
| 310 | buses_supported = CHIP_BUSTYPE_SPI; |
| 311 | spi_controller = SPI_CONTROLLER_DEDIPROG; |
| 312 | |
| 313 | /* RE leftover, leave in until the driver is complete. */ |
| 314 | #if 0 |
| 315 | /* Execute RDID by hand if you want to test it. */ |
| 316 | dediprog_do_stuff(); |
| 317 | #endif |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /* Leftovers from reverse engineering. Keep for documentation purposes until |
| 323 | * completely understood. |
| 324 | */ |
| 325 | int dediprog_do_stuff(void) |
| 326 | { |
| 327 | char buf[0x4]; |
| 328 | /* SPI command processing starts here. */ |
| 329 | |
| 330 | /* URB 12. Command Send SPI. */ |
| 331 | /* URB 13. Command Receive SPI. */ |
| 332 | memset(buf, 0, sizeof(buf)); |
| 333 | /* JEDEC RDID */ |
| 334 | msg_pdbg("Sending RDID\n"); |
| 335 | buf[0] = JEDEC_RDID; |
| 336 | if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf)) |
| 337 | return 1; |
| 338 | msg_pdbg("Receiving response: "); |
| 339 | print_hex(buf, JEDEC_RDID_INSIZE); |
| 340 | #if 0 |
| 341 | /* URB 14-27 are more SPI commands. */ |
| 342 | /* URB 28. Command Set SPI Voltage. */ |
| 343 | if (dediprog_set_spi_voltage(0x0)) |
| 344 | return 1; |
| 345 | /* URB 29-38. Command F, unsuccessful wait. */ |
| 346 | if (dediprog_command_f(544)) |
| 347 | return 1; |
| 348 | /* URB 39. Command Set SPI Voltage. */ |
| 349 | if (dediprog_set_spi_voltage(0x10)) |
| 350 | return 1; |
| 351 | /* URB 40. Command Set SPI Speed. */ |
| 352 | if (dediprog_set_spi_speed(0x2)) |
| 353 | return 1; |
| 354 | /* URB 41 is just URB 28. */ |
| 355 | /* URB 42,44,46,48,51,53 is just URB 8. */ |
| 356 | /* URB 43,45,47,49,52,54 is just URB 9. */ |
| 357 | /* URB 50 is just URB 6/7. */ |
| 358 | /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/ |
| 359 | /* URB 132,134 is just URB 6/7. */ |
| 360 | /* URB 133 is just URB 29-38. */ |
| 361 | /* URB 135 is just URB 8. */ |
| 362 | /* URB 136 is just URB 9. */ |
| 363 | /* URB 137 is just URB 11. */ |
| 364 | |
| 365 | /* Command I is probably Start Bulk Read. Data is u16 blockcount, u16 blocksize. */ |
| 366 | /* Command J is probably Start Bulk Write. Data is u16 blockcount, u16 blocksize. */ |
| 367 | /* Bulk transfer sizes for Command I/J are always 512 bytes, rest is filled with 0xff. */ |
| 368 | #endif |
| 369 | |
| 370 | msg_pinfo("All probes will fail because this driver is not hooked up " |
| 371 | "to the SPI infrastructure yet."); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | int dediprog_shutdown(void) |
| 376 | { |
| 377 | msg_pspew("%s\n", __func__); |
| 378 | |
| 379 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 380 | if (dediprog_set_spi_voltage(0x0)) |
| 381 | return 1; |
| 382 | |
| 383 | if (usb_close(dediprog_handle)) { |
| 384 | msg_perr("Couldn't close USB device!\n"); |
| 385 | return 1; |
| 386 | } |
| 387 | return 0; |
| 388 | } |