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