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 | |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 21 | #include <string.h> |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 22 | #include <usb.h> |
| 23 | #include "flash.h" |
Sean Nelson | 14ba668 | 2010-02-26 05:48:29 +0000 | [diff] [blame] | 24 | #include "chipdrivers.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 25 | #include "programmer.h" |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 26 | #include "spi.h" |
| 27 | |
| 28 | #define DEFAULT_TIMEOUT 3000 |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 29 | static usb_dev_handle *dediprog_handle; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 30 | static int dediprog_endpoint; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 31 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 32 | #if 0 |
| 33 | /* Might be useful for other pieces of code as well. */ |
| 34 | static void print_hex(void *buf, size_t len) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 35 | { |
| 36 | size_t i; |
| 37 | |
| 38 | for (i = 0; i < len; i++) |
| 39 | msg_pdbg(" %02x", ((uint8_t *)buf)[i]); |
| 40 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 41 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 42 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 43 | /* Might be useful for other USB devices as well. static for now. */ |
| 44 | static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 45 | { |
| 46 | struct usb_bus *bus; |
| 47 | struct usb_device *dev; |
| 48 | |
| 49 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 50 | for (dev = bus->devices; dev; dev = dev->next) |
| 51 | if ((dev->descriptor.idVendor == vid) && |
| 52 | (dev->descriptor.idProduct == pid)) |
| 53 | return dev; |
| 54 | |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | //int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); |
| 59 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 60 | static int dediprog_set_spi_voltage(int millivolt) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 61 | { |
| 62 | int ret; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 63 | uint16_t voltage_selector; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 64 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 65 | switch (millivolt) { |
| 66 | case 0: |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 67 | /* Admittedly this one is an assumption. */ |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 68 | voltage_selector = 0x0; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 69 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 70 | case 1800: |
| 71 | voltage_selector = 0x12; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 72 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 73 | case 2500: |
| 74 | voltage_selector = 0x11; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 75 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 76 | case 3500: |
| 77 | voltage_selector = 0x10; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 78 | break; |
| 79 | default: |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 80 | msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 81 | return 1; |
| 82 | } |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 83 | msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000, |
| 84 | millivolt % 1000); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 85 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 86 | ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector, 0xff, NULL, 0x0, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 87 | if (ret != 0x0) { |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 88 | msg_perr("Command Set SPI Voltage 0x%x failed!\n", voltage_selector); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 89 | return 1; |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 94 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 95 | /* After dediprog_set_spi_speed, the original app always calls |
| 96 | * dediprog_set_spi_voltage(0) and then |
| 97 | * dediprog_check_devicestring() four times in a row. |
| 98 | * After that, dediprog_command_a() is called. |
| 99 | * This looks suspiciously like the microprocessor in the SF100 has to be |
| 100 | * restarted/reinitialized in case the speed changes. |
| 101 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 102 | static int dediprog_set_spi_speed(uint16_t speed) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 103 | { |
| 104 | int ret; |
| 105 | unsigned int khz; |
| 106 | |
| 107 | /* Case 1 and 2 are in weird order. Probably an organically "grown" |
| 108 | * interface. |
| 109 | * Base frequency is 24000 kHz, divisors are (in order) |
| 110 | * 1, 3, 2, 8, 11, 16, 32, 64. |
| 111 | */ |
| 112 | switch (speed) { |
| 113 | case 0x0: |
| 114 | khz = 24000; |
| 115 | break; |
| 116 | case 0x1: |
| 117 | khz = 8000; |
| 118 | break; |
| 119 | case 0x2: |
| 120 | khz = 12000; |
| 121 | break; |
| 122 | case 0x3: |
| 123 | khz = 3000; |
| 124 | break; |
| 125 | case 0x4: |
| 126 | khz = 2180; |
| 127 | break; |
| 128 | case 0x5: |
| 129 | khz = 1500; |
| 130 | break; |
| 131 | case 0x6: |
| 132 | khz = 750; |
| 133 | break; |
| 134 | case 0x7: |
| 135 | khz = 375; |
| 136 | break; |
| 137 | default: |
| 138 | msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed); |
| 139 | return 1; |
| 140 | } |
| 141 | msg_pdbg("Setting SPI speed to %u kHz\n", khz); |
| 142 | |
| 143 | ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, 0x0, DEFAULT_TIMEOUT); |
| 144 | if (ret != 0x0) { |
| 145 | msg_perr("Command Set SPI Speed 0x%x failed!\n", speed); |
| 146 | return 1; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 150 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 151 | |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 152 | /* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes. |
| 153 | * @start start address |
| 154 | * @len length |
| 155 | * @return 0 on success, 1 on failure |
| 156 | */ |
| 157 | static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf, |
| 158 | int start, int len) |
| 159 | { |
| 160 | int ret; |
| 161 | int i; |
| 162 | /* chunksize must be 512, other sizes will NOT work at all. */ |
| 163 | const int chunksize = 0x200; |
| 164 | const int count = len / chunksize; |
| 165 | const char count_and_chunk[] = {count & 0xff, |
| 166 | (count >> 8) & 0xff, |
| 167 | chunksize & 0xff, |
| 168 | (chunksize >> 8) & 0xff}; |
| 169 | |
| 170 | if ((start % chunksize) || (len % chunksize)) { |
| 171 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 172 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 173 | return 1; |
| 174 | } |
| 175 | |
| 176 | /* No idea if the hardware can handle empty reads, so chicken out. */ |
| 177 | if (!len) |
| 178 | return 0; |
| 179 | /* Command Read SPI Bulk. No idea which read command is used on the |
| 180 | * SPI side. |
| 181 | */ |
| 182 | ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000, |
| 183 | start / 0x10000, (char *)count_and_chunk, |
| 184 | sizeof(count_and_chunk), DEFAULT_TIMEOUT); |
| 185 | if (ret != sizeof(count_and_chunk)) { |
| 186 | msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, |
| 187 | usb_strerror()); |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | for (i = 0; i < count; i++) { |
| 192 | ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint, |
| 193 | (char *)buf + i * chunksize, chunksize, |
| 194 | DEFAULT_TIMEOUT); |
| 195 | if (ret != chunksize) { |
| 196 | msg_perr("SPI bulk read %i failed, expected %i, got %i " |
| 197 | "%s!\n", i, chunksize, ret, usb_strerror()); |
| 198 | return 1; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 205 | int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 206 | { |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 207 | int ret; |
| 208 | /* chunksize must be 512, other sizes will NOT work at all. */ |
| 209 | const int chunksize = 0x200; |
| 210 | int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 211 | int bulklen; |
| 212 | |
| 213 | if (residue) { |
| 214 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 215 | start, residue); |
| 216 | ret = spi_read_chunked(flash, buf, start, residue, 16); |
| 217 | if (ret) |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | /* Round down. */ |
| 222 | bulklen = (len - residue) / chunksize * chunksize; |
| 223 | ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, |
| 224 | bulklen); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | |
| 228 | len -= residue + bulklen; |
| 229 | if (len) { |
| 230 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 231 | start, len); |
| 232 | ret = spi_read_chunked(flash, buf + residue + bulklen, |
| 233 | start + residue + bulklen, len, 16); |
| 234 | if (ret) |
| 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | return 0; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 241 | int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 242 | { |
| 243 | /* No idea about the real limit. Maybe 12, maybe more, maybe less. */ |
| 244 | return spi_write_chunked(flash, buf, start, len, 12); |
| 245 | } |
| 246 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 247 | int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
| 248 | const unsigned char *writearr, unsigned char *readarr) |
| 249 | { |
| 250 | int ret; |
| 251 | |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 252 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 253 | /* Paranoid, but I don't want to be blamed if anything explodes. */ |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 254 | if (writecnt > 16) { |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 255 | msg_perr("Untested writecnt=%i, aborting.\n", writecnt); |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 256 | return 1; |
| 257 | } |
| 258 | /* 16 byte reads should work. */ |
| 259 | if (readcnt > 16) { |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 260 | msg_perr("Untested readcnt=%i, aborting.\n", readcnt); |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 261 | return 1; |
| 262 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 263 | |
| 264 | ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT); |
| 265 | if (ret != writecnt) { |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 266 | msg_perr("Send SPI failed, expected %i, got %i %s!\n", |
| 267 | writecnt, ret, usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | if (!readcnt) |
| 271 | return 0; |
| 272 | memset(readarr, 0, readcnt); |
| 273 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT); |
| 274 | if (ret != readcnt) { |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 275 | msg_perr("Receive SPI failed, expected %i, got %i %s!\n", |
| 276 | readcnt, ret, usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 277 | return 1; |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 282 | static int dediprog_check_devicestring(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 283 | { |
| 284 | int ret; |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 285 | int fw[3]; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 286 | char buf[0x11]; |
| 287 | |
| 288 | /* Command Prepare Receive Device String. */ |
| 289 | memset(buf, 0, sizeof(buf)); |
| 290 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT); |
| 291 | /* The char casting is needed to stop gcc complaining about an always true comparison. */ |
| 292 | if ((ret != 0x1) || (buf[0] != (char)0xff)) { |
| 293 | msg_perr("Unexpected response to Command Prepare Receive Device" |
| 294 | " String!\n"); |
| 295 | return 1; |
| 296 | } |
| 297 | /* Command Receive Device String. */ |
| 298 | memset(buf, 0, sizeof(buf)); |
| 299 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT); |
| 300 | if (ret != 0x10) { |
| 301 | msg_perr("Incomplete/failed Command Receive Device String!\n"); |
| 302 | return 1; |
| 303 | } |
| 304 | buf[0x10] = '\0'; |
| 305 | msg_pdbg("Found a %s\n", buf); |
| 306 | if (memcmp(buf, "SF100", 0x5)) { |
| 307 | msg_perr("Device not a SF100!\n"); |
| 308 | return 1; |
| 309 | } |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 310 | if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) { |
| 311 | msg_perr("Unexpected firmware version string!\n"); |
| 312 | return 1; |
| 313 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 314 | /* Only these versions were tested. */ |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 315 | if (fw[0] < 2 || fw[0] > 5) { |
| 316 | msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], |
| 317 | fw[1], fw[2]); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 318 | return 1; |
| 319 | } |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /* Command A seems to be some sort of device init. It is either followed by |
| 324 | * dediprog_check_devicestring (often) or Command A (often) or |
| 325 | * Command F (once). |
| 326 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 327 | static int dediprog_command_a(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 328 | { |
| 329 | int ret; |
| 330 | char buf[0x1]; |
| 331 | |
| 332 | memset(buf, 0, sizeof(buf)); |
| 333 | ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT); |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 334 | if (ret < 0) { |
| 335 | msg_perr("Command A failed (%s)!\n", usb_strerror()); |
| 336 | return 1; |
| 337 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 338 | if ((ret != 0x1) || (buf[0] != 0x6f)) { |
| 339 | msg_perr("Unexpected response to Command A!\n"); |
| 340 | return 1; |
| 341 | } |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* Command C is only sent after dediprog_check_devicestring, but not after every |
| 346 | * invocation of dediprog_check_devicestring. It is only sent after the first |
| 347 | * dediprog_command_a(); dediprog_check_devicestring() sequence in each session. |
| 348 | * I'm tempted to call this one start_SPI_engine or finish_init. |
| 349 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 350 | static int dediprog_command_c(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 351 | { |
| 352 | int ret; |
| 353 | |
| 354 | ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT); |
| 355 | if (ret != 0x0) { |
| 356 | msg_perr("Unexpected response to Command C!\n"); |
| 357 | return 1; |
| 358 | } |
| 359 | return 0; |
| 360 | } |
| 361 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 362 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 363 | /* Very strange. Seems to be a programmer keepalive or somesuch. |
| 364 | * Wait unsuccessfully for timeout ms to read one byte. |
| 365 | * Is usually called after setting voltage to 0. |
| 366 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 367 | static int dediprog_command_f(int timeout) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 368 | { |
| 369 | int ret; |
| 370 | char buf[0x1]; |
| 371 | |
| 372 | memset(buf, 0, sizeof(buf)); |
| 373 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout); |
| 374 | if (ret != 0x0) { |
| 375 | msg_perr("Unexpected response to Command F!\n"); |
| 376 | return 1; |
| 377 | } |
| 378 | return 0; |
| 379 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 380 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 381 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 382 | static int parse_voltage(char *voltage) |
| 383 | { |
| 384 | char *tmp = NULL; |
| 385 | int i; |
| 386 | int millivolt; |
| 387 | int fraction = 0; |
| 388 | |
| 389 | if (!voltage || !strlen(voltage)) { |
| 390 | msg_perr("Empty voltage= specified.\n"); |
| 391 | return -1; |
| 392 | } |
| 393 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 394 | voltage = tmp; |
| 395 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 396 | * to be in decimal notation. |
| 397 | */ |
| 398 | if ((*voltage == '.') || (*voltage == ',')) { |
| 399 | voltage++; |
| 400 | for (i = 0; i < 3; i++) { |
| 401 | fraction *= 10; |
| 402 | /* Don't advance if the current character is invalid, |
| 403 | * but continue multiplying. |
| 404 | */ |
| 405 | if ((*voltage < '0') || (*voltage > '9')) |
| 406 | continue; |
| 407 | fraction += *voltage - '0'; |
| 408 | voltage++; |
| 409 | } |
| 410 | /* Throw away remaining digits. */ |
| 411 | voltage += strspn(voltage, "0123456789"); |
| 412 | } |
| 413 | /* The remaining string must be empty or "mV" or "V". */ |
| 414 | tolower_string(voltage); |
| 415 | |
| 416 | /* No unit or "V". */ |
| 417 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 418 | millivolt *= 1000; |
| 419 | millivolt += fraction; |
| 420 | } else if (!strncmp(voltage, "mv", 2) || |
| 421 | !strncmp(voltage, "milliv", 6)) { |
| 422 | /* No adjustment. fraction is discarded. */ |
| 423 | } else { |
| 424 | /* Garbage at the end of the string. */ |
| 425 | msg_perr("Garbage voltage= specified.\n"); |
| 426 | return -1; |
| 427 | } |
| 428 | return millivolt; |
| 429 | } |
| 430 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 431 | /* URB numbers refer to the first log ever captured. */ |
| 432 | int dediprog_init(void) |
| 433 | { |
| 434 | struct usb_device *dev; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 435 | char *voltage; |
| 436 | int millivolt = 3500; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 437 | int ret; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 438 | |
| 439 | msg_pspew("%s\n", __func__); |
| 440 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 441 | voltage = extract_programmer_param("voltage"); |
| 442 | if (voltage) { |
| 443 | millivolt = parse_voltage(voltage); |
| 444 | free(voltage); |
| 445 | if (millivolt < 0) { |
| 446 | return 1; |
| 447 | } |
| 448 | msg_pinfo("Setting voltage to %i mV\n", millivolt); |
| 449 | } |
| 450 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 451 | /* Here comes the USB stuff. */ |
| 452 | usb_init(); |
| 453 | usb_find_busses(); |
| 454 | usb_find_devices(); |
| 455 | dev = get_device_by_vid_pid(0x0483, 0xdada); |
| 456 | if (!dev) { |
| 457 | msg_perr("Could not find a Dediprog SF100 on USB!\n"); |
| 458 | return 1; |
| 459 | } |
| 460 | msg_pdbg("Found USB device (%04x:%04x).\n", |
| 461 | dev->descriptor.idVendor, |
| 462 | dev->descriptor.idProduct); |
| 463 | dediprog_handle = usb_open(dev); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 464 | ret = usb_set_configuration(dediprog_handle, 1); |
| 465 | if (ret < 0) { |
| 466 | msg_perr("Could not set USB device configuration: %i %s\n", |
| 467 | ret, usb_strerror()); |
| 468 | if (usb_close(dediprog_handle)) |
| 469 | msg_perr("Could not close USB device!\n"); |
| 470 | return 1; |
| 471 | } |
| 472 | ret = usb_claim_interface(dediprog_handle, 0); |
| 473 | if (ret < 0) { |
| 474 | msg_perr("Could not claim USB device interface %i: %i %s\n", |
| 475 | 0, ret, usb_strerror()); |
| 476 | if (usb_close(dediprog_handle)) |
| 477 | msg_perr("Could not close USB device!\n"); |
| 478 | return 1; |
| 479 | } |
| 480 | dediprog_endpoint = 2; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 481 | /* URB 6. Command A. */ |
| 482 | if (dediprog_command_a()) |
| 483 | return 1; |
| 484 | /* URB 7. Command A. */ |
| 485 | if (dediprog_command_a()) |
| 486 | return 1; |
| 487 | /* URB 8. Command Prepare Receive Device String. */ |
| 488 | /* URB 9. Command Receive Device String. */ |
| 489 | if (dediprog_check_devicestring()) |
| 490 | return 1; |
| 491 | /* URB 10. Command C. */ |
| 492 | if (dediprog_command_c()) |
| 493 | return 1; |
| 494 | /* URB 11. Command Set SPI Voltage. */ |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 495 | if (dediprog_set_spi_voltage(millivolt)) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 496 | return 1; |
| 497 | |
| 498 | buses_supported = CHIP_BUSTYPE_SPI; |
| 499 | spi_controller = SPI_CONTROLLER_DEDIPROG; |
| 500 | |
| 501 | /* RE leftover, leave in until the driver is complete. */ |
| 502 | #if 0 |
| 503 | /* Execute RDID by hand if you want to test it. */ |
| 504 | dediprog_do_stuff(); |
| 505 | #endif |
| 506 | |
| 507 | return 0; |
| 508 | } |
| 509 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 510 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 511 | /* Leftovers from reverse engineering. Keep for documentation purposes until |
| 512 | * completely understood. |
| 513 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 514 | static int dediprog_do_stuff(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 515 | { |
| 516 | char buf[0x4]; |
| 517 | /* SPI command processing starts here. */ |
| 518 | |
| 519 | /* URB 12. Command Send SPI. */ |
| 520 | /* URB 13. Command Receive SPI. */ |
| 521 | memset(buf, 0, sizeof(buf)); |
| 522 | /* JEDEC RDID */ |
| 523 | msg_pdbg("Sending RDID\n"); |
| 524 | buf[0] = JEDEC_RDID; |
| 525 | if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf)) |
| 526 | return 1; |
| 527 | msg_pdbg("Receiving response: "); |
| 528 | print_hex(buf, JEDEC_RDID_INSIZE); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 529 | /* URB 14-27 are more SPI commands. */ |
| 530 | /* URB 28. Command Set SPI Voltage. */ |
| 531 | if (dediprog_set_spi_voltage(0x0)) |
| 532 | return 1; |
| 533 | /* URB 29-38. Command F, unsuccessful wait. */ |
| 534 | if (dediprog_command_f(544)) |
| 535 | return 1; |
| 536 | /* URB 39. Command Set SPI Voltage. */ |
| 537 | if (dediprog_set_spi_voltage(0x10)) |
| 538 | return 1; |
| 539 | /* URB 40. Command Set SPI Speed. */ |
| 540 | if (dediprog_set_spi_speed(0x2)) |
| 541 | return 1; |
| 542 | /* URB 41 is just URB 28. */ |
| 543 | /* URB 42,44,46,48,51,53 is just URB 8. */ |
| 544 | /* URB 43,45,47,49,52,54 is just URB 9. */ |
| 545 | /* URB 50 is just URB 6/7. */ |
| 546 | /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/ |
| 547 | /* URB 132,134 is just URB 6/7. */ |
| 548 | /* URB 133 is just URB 29-38. */ |
| 549 | /* URB 135 is just URB 8. */ |
| 550 | /* URB 136 is just URB 9. */ |
| 551 | /* URB 137 is just URB 11. */ |
| 552 | |
| 553 | /* Command I is probably Start Bulk Read. Data is u16 blockcount, u16 blocksize. */ |
| 554 | /* Command J is probably Start Bulk Write. Data is u16 blockcount, u16 blocksize. */ |
| 555 | /* Bulk transfer sizes for Command I/J are always 512 bytes, rest is filled with 0xff. */ |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 556 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 557 | return 0; |
| 558 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 559 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 560 | |
| 561 | int dediprog_shutdown(void) |
| 562 | { |
| 563 | msg_pspew("%s\n", __func__); |
| 564 | |
| 565 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 566 | if (dediprog_set_spi_voltage(0x0)) |
| 567 | return 1; |
| 568 | |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 569 | if (usb_release_interface(dediprog_handle, 0)) { |
| 570 | msg_perr("Could not release USB interface!\n"); |
| 571 | return 1; |
| 572 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 573 | if (usb_close(dediprog_handle)) { |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 574 | msg_perr("Could not close USB device!\n"); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 575 | return 1; |
| 576 | } |
| 577 | return 0; |
| 578 | } |