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 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 28 | #define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 29 | #define DEFAULT_TIMEOUT 3000 |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 30 | static usb_dev_handle *dediprog_handle; |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 31 | static int dediprog_firmwareversion; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 32 | static int dediprog_endpoint; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 33 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 34 | #define DEDI_SPI_CMD_PAGEWRITE 0x1 |
| 35 | #define DEDI_SPI_CMD_AAIWRITE 0x4 |
| 36 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 37 | #if 0 |
| 38 | /* Might be useful for other pieces of code as well. */ |
| 39 | static void print_hex(void *buf, size_t len) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 40 | { |
| 41 | size_t i; |
| 42 | |
| 43 | for (i = 0; i < len; i++) |
| 44 | msg_pdbg(" %02x", ((uint8_t *)buf)[i]); |
| 45 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 46 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 47 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 48 | /* Might be useful for other USB devices as well. static for now. */ |
| 49 | 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] | 50 | { |
| 51 | struct usb_bus *bus; |
| 52 | struct usb_device *dev; |
| 53 | |
| 54 | for (bus = usb_get_busses(); bus; bus = bus->next) |
| 55 | for (dev = bus->devices; dev; dev = dev->next) |
| 56 | if ((dev->descriptor.idVendor == vid) && |
| 57 | (dev->descriptor.idProduct == pid)) |
| 58 | return dev; |
| 59 | |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | //int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout); |
| 64 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 65 | /* Set/clear LEDs on dediprog */ |
| 66 | #define PASS_ON (0 << 0) |
| 67 | #define PASS_OFF (1 << 0) |
| 68 | #define BUSY_ON (0 << 1) |
| 69 | #define BUSY_OFF (1 << 1) |
| 70 | #define ERROR_ON (0 << 2) |
| 71 | #define ERROR_OFF (1 << 2) |
| 72 | static int current_led_status = -1; |
| 73 | |
| 74 | static int dediprog_set_leds(int leds) |
| 75 | { |
| 76 | int ret, target_leds; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 77 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 78 | if (leds < 0 || leds > 7) |
| 79 | leds = 0; // Bogus value, enable all LEDs |
| 80 | |
| 81 | if (leds == current_led_status) |
| 82 | return 0; |
| 83 | |
| 84 | /* Older Dediprogs with 2.x.x and 3.x.x firmware only had |
| 85 | * two LEDs, and they were reversed. So map them around if |
| 86 | * we have an old device. On those devices the LEDs map as |
| 87 | * follows: |
| 88 | * bit 2 == 0: green light is on. |
| 89 | * bit 0 == 0: red light is on. |
| 90 | */ |
| 91 | if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) { |
| 92 | target_leds = ((leds & ERROR_OFF) >> 2) | |
| 93 | ((leds & PASS_OFF) << 2); |
| 94 | } else { |
| 95 | target_leds = leds; |
| 96 | } |
| 97 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 98 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds, |
| 99 | NULL, 0x0, DEFAULT_TIMEOUT); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 100 | if (ret != 0x0) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 101 | msg_perr("Command Set LED 0x%x failed (%s)!\n", |
| 102 | leds, usb_strerror()); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | current_led_status = leds; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 111 | static int dediprog_set_spi_voltage(int millivolt) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 112 | { |
| 113 | int ret; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 114 | uint16_t voltage_selector; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 115 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 116 | switch (millivolt) { |
| 117 | case 0: |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 118 | /* Admittedly this one is an assumption. */ |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 119 | voltage_selector = 0x0; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 120 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 121 | case 1800: |
| 122 | voltage_selector = 0x12; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 123 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 124 | case 2500: |
| 125 | voltage_selector = 0x11; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 126 | break; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 127 | case 3500: |
| 128 | voltage_selector = 0x10; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 129 | break; |
| 130 | default: |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 131 | msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 132 | return 1; |
| 133 | } |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 134 | msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000, |
| 135 | millivolt % 1000); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 136 | |
Nico Huber | 4099a8a | 2012-06-16 00:02:27 +0000 | [diff] [blame] | 137 | if (voltage_selector == 0) { |
| 138 | /* Wait some time as the original driver does. */ |
| 139 | programmer_delay(200 * 1000); |
| 140 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 141 | ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector, |
| 142 | 0xff, NULL, 0x0, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 143 | if (ret != 0x0) { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 144 | msg_perr("Command Set SPI Voltage 0x%x failed!\n", |
| 145 | voltage_selector); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 146 | return 1; |
| 147 | } |
Nico Huber | 4099a8a | 2012-06-16 00:02:27 +0000 | [diff] [blame] | 148 | if (voltage_selector != 0) { |
| 149 | /* Wait some time as the original driver does. */ |
| 150 | programmer_delay(200 * 1000); |
| 151 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 152 | return 0; |
| 153 | } |
| 154 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 155 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 156 | /* After dediprog_set_spi_speed, the original app always calls |
| 157 | * dediprog_set_spi_voltage(0) and then |
| 158 | * dediprog_check_devicestring() four times in a row. |
| 159 | * After that, dediprog_command_a() is called. |
| 160 | * This looks suspiciously like the microprocessor in the SF100 has to be |
| 161 | * restarted/reinitialized in case the speed changes. |
| 162 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 163 | static int dediprog_set_spi_speed(uint16_t speed) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 164 | { |
| 165 | int ret; |
| 166 | unsigned int khz; |
| 167 | |
| 168 | /* Case 1 and 2 are in weird order. Probably an organically "grown" |
| 169 | * interface. |
| 170 | * Base frequency is 24000 kHz, divisors are (in order) |
| 171 | * 1, 3, 2, 8, 11, 16, 32, 64. |
| 172 | */ |
| 173 | switch (speed) { |
| 174 | case 0x0: |
| 175 | khz = 24000; |
| 176 | break; |
| 177 | case 0x1: |
| 178 | khz = 8000; |
| 179 | break; |
| 180 | case 0x2: |
| 181 | khz = 12000; |
| 182 | break; |
| 183 | case 0x3: |
| 184 | khz = 3000; |
| 185 | break; |
| 186 | case 0x4: |
| 187 | khz = 2180; |
| 188 | break; |
| 189 | case 0x5: |
| 190 | khz = 1500; |
| 191 | break; |
| 192 | case 0x6: |
| 193 | khz = 750; |
| 194 | break; |
| 195 | case 0x7: |
| 196 | khz = 375; |
| 197 | break; |
| 198 | default: |
| 199 | msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed); |
| 200 | return 1; |
| 201 | } |
| 202 | msg_pdbg("Setting SPI speed to %u kHz\n", khz); |
| 203 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 204 | ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, |
| 205 | 0x0, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 206 | if (ret != 0x0) { |
| 207 | msg_perr("Command Set SPI Speed 0x%x failed!\n", speed); |
| 208 | return 1; |
| 209 | } |
| 210 | return 0; |
| 211 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 212 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 213 | |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 214 | /* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes. |
| 215 | * @start start address |
| 216 | * @len length |
| 217 | * @return 0 on success, 1 on failure |
| 218 | */ |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 219 | static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 220 | unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 221 | { |
| 222 | int ret; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 223 | unsigned int i; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 224 | /* chunksize must be 512, other sizes will NOT work at all. */ |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 225 | const unsigned int chunksize = 0x200; |
| 226 | const unsigned int count = len / chunksize; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 227 | const char count_and_chunk[] = {count & 0xff, |
| 228 | (count >> 8) & 0xff, |
| 229 | chunksize & 0xff, |
| 230 | (chunksize >> 8) & 0xff}; |
| 231 | |
| 232 | if ((start % chunksize) || (len % chunksize)) { |
| 233 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 234 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | /* No idea if the hardware can handle empty reads, so chicken out. */ |
| 239 | if (!len) |
| 240 | return 0; |
| 241 | /* Command Read SPI Bulk. No idea which read command is used on the |
| 242 | * SPI side. |
| 243 | */ |
| 244 | ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000, |
| 245 | start / 0x10000, (char *)count_and_chunk, |
| 246 | sizeof(count_and_chunk), DEFAULT_TIMEOUT); |
| 247 | if (ret != sizeof(count_and_chunk)) { |
| 248 | msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret, |
| 249 | usb_strerror()); |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | for (i = 0; i < count; i++) { |
| 254 | ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint, |
| 255 | (char *)buf + i * chunksize, chunksize, |
| 256 | DEFAULT_TIMEOUT); |
| 257 | if (ret != chunksize) { |
| 258 | msg_perr("SPI bulk read %i failed, expected %i, got %i " |
| 259 | "%s!\n", i, chunksize, ret, usb_strerror()); |
| 260 | return 1; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 267 | static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 268 | unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 269 | { |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 270 | int ret; |
| 271 | /* chunksize must be 512, other sizes will NOT work at all. */ |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 272 | const unsigned int chunksize = 0x200; |
| 273 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 274 | unsigned int bulklen; |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 275 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 276 | dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF); |
| 277 | |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 278 | if (residue) { |
| 279 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 280 | start, residue); |
| 281 | ret = spi_read_chunked(flash, buf, start, residue, 16); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 282 | if (ret) { |
| 283 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 284 | return ret; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 285 | } |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /* Round down. */ |
| 289 | bulklen = (len - residue) / chunksize * chunksize; |
| 290 | ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue, |
| 291 | bulklen); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 292 | if (ret) { |
| 293 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 294 | return ret; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 295 | } |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 296 | |
| 297 | len -= residue + bulklen; |
| 298 | if (len) { |
| 299 | msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n", |
| 300 | start, len); |
| 301 | ret = spi_read_chunked(flash, buf + residue + bulklen, |
| 302 | start + residue + bulklen, len, 16); |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 303 | if (ret) { |
| 304 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 305 | return ret; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 306 | } |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 309 | dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 310 | return 0; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 313 | /* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes. |
| 314 | * @chunksize length of data chunks, only 256 supported by now |
| 315 | * @start start address |
| 316 | * @len length |
| 317 | * @dedi_spi_cmd dediprog specific write command for spi bus |
| 318 | * @return 0 on success, 1 on failure |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 319 | */ |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 320 | static int dediprog_spi_bulk_write(struct flashctx *flash, uint8_t *buf, unsigned int chunksize, |
| 321 | unsigned int start, unsigned int len, uint8_t dedi_spi_cmd) |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 322 | { |
| 323 | int ret; |
| 324 | unsigned int i; |
| 325 | /* USB transfer size must be 512, other sizes will NOT work at all. |
| 326 | * chunksize is the real data size per USB bulk transfer. The remaining |
| 327 | * space in a USB bulk transfer must be filled with 0xff padding. |
| 328 | */ |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 329 | const unsigned int count = len / chunksize; |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 330 | const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd}; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 331 | char usbbuf[512]; |
| 332 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 333 | /* |
| 334 | * We should change this check to |
| 335 | * chunksize > 512 |
| 336 | * once we know how to handle different chunk sizes. |
| 337 | */ |
| 338 | if (chunksize != 256) { |
| 339 | msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n" |
| 340 | "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize); |
| 341 | return 1; |
| 342 | } |
| 343 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 344 | if ((start % chunksize) || (len % chunksize)) { |
| 345 | msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug " |
| 346 | "at flashrom@flashrom.org\n", __func__, start, len); |
| 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | /* No idea if the hardware can handle empty writes, so chicken out. */ |
| 351 | if (!len) |
| 352 | return 0; |
| 353 | /* Command Write SPI Bulk. No idea which write command is used on the |
| 354 | * SPI side. |
| 355 | */ |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 356 | ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000, start / 0x10000, |
| 357 | (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT); |
| 358 | if (ret != sizeof(count_and_cmd)) { |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 359 | msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret, |
| 360 | usb_strerror()); |
| 361 | return 1; |
| 362 | } |
| 363 | |
| 364 | for (i = 0; i < count; i++) { |
| 365 | memset(usbbuf, 0xff, sizeof(usbbuf)); |
| 366 | memcpy(usbbuf, buf + i * chunksize, chunksize); |
| 367 | ret = usb_bulk_write(dediprog_handle, dediprog_endpoint, |
| 368 | usbbuf, 512, |
| 369 | DEFAULT_TIMEOUT); |
| 370 | if (ret != 512) { |
| 371 | msg_perr("SPI bulk write failed, expected %i, got %i " |
| 372 | "%s!\n", 512, ret, usb_strerror()); |
| 373 | return 1; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 380 | static int dediprog_spi_write(struct flashctx *flash, uint8_t *buf, |
| 381 | unsigned int start, unsigned int len, uint8_t dedi_spi_cmd) |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 382 | { |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 383 | int ret; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 384 | const unsigned int chunksize = flash->chip->page_size; |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 385 | unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0; |
| 386 | unsigned int bulklen; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 387 | |
| 388 | dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF); |
| 389 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 390 | if (chunksize != 256) { |
| 391 | msg_pdbg("Page sizes other than 256 bytes are unsupported as " |
| 392 | "we don't know how dediprog\nhandles them.\n"); |
| 393 | /* Write everything like it was residue. */ |
| 394 | residue = len; |
| 395 | } |
| 396 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 397 | if (residue) { |
| 398 | msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n", |
| 399 | start, residue); |
| 400 | /* No idea about the real limit. Maybe 12, maybe more. */ |
| 401 | ret = spi_write_chunked(flash, buf, start, residue, 12); |
| 402 | if (ret) { |
| 403 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
| 404 | return ret; |
| 405 | } |
| 406 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 407 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 408 | /* Round down. */ |
| 409 | bulklen = (len - residue) / chunksize * chunksize; |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 410 | ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 411 | if (ret) { |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 412 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 413 | return ret; |
| 414 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 415 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 416 | len -= residue + bulklen; |
| 417 | if (len) { |
| 418 | msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n", |
| 419 | start, len); |
| 420 | ret = spi_write_chunked(flash, buf + residue + bulklen, |
| 421 | start + residue + bulklen, len, 12); |
| 422 | if (ret) { |
| 423 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
| 424 | return ret; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF); |
| 429 | return 0; |
Carl-Daniel Hailfinger | 306b818 | 2010-11-23 21:28:16 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 432 | static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
| 433 | { |
| 434 | return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_PAGEWRITE); |
| 435 | } |
| 436 | |
| 437 | static int dediprog_spi_write_aai(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) |
| 438 | { |
| 439 | return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_AAIWRITE); |
| 440 | } |
| 441 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 442 | static int dediprog_spi_send_command(struct flashctx *flash, |
| 443 | unsigned int writecnt, |
| 444 | unsigned int readcnt, |
| 445 | const unsigned char *writearr, |
| 446 | unsigned char *readarr) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 447 | { |
| 448 | int ret; |
| 449 | |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 450 | msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 451 | /* 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] | 452 | if (writecnt > 16) { |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 453 | msg_perr("Untested writecnt=%i, aborting.\n", writecnt); |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 454 | return 1; |
| 455 | } |
| 456 | /* 16 byte reads should work. */ |
| 457 | if (readcnt > 16) { |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 458 | msg_perr("Untested readcnt=%i, aborting.\n", readcnt); |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 459 | return 1; |
| 460 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 461 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 462 | ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, |
| 463 | readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, |
| 464 | DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 465 | if (ret != writecnt) { |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 466 | msg_perr("Send SPI failed, expected %i, got %i %s!\n", |
| 467 | writecnt, ret, usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 468 | return 1; |
| 469 | } |
| 470 | if (!readcnt) |
| 471 | return 0; |
| 472 | memset(readarr, 0, readcnt); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 473 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, |
| 474 | (char *)readarr, readcnt, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 475 | if (ret != readcnt) { |
Carl-Daniel Hailfinger | eac6579 | 2010-01-22 02:53:30 +0000 | [diff] [blame] | 476 | msg_perr("Receive SPI failed, expected %i, got %i %s!\n", |
| 477 | readcnt, ret, usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 478 | return 1; |
| 479 | } |
| 480 | return 0; |
| 481 | } |
| 482 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 483 | static int dediprog_check_devicestring(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 484 | { |
| 485 | int ret; |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 486 | int fw[3]; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 487 | char buf[0x11]; |
| 488 | |
| 489 | /* Command Prepare Receive Device String. */ |
| 490 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 491 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, |
| 492 | 0x1, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 493 | /* The char casting is needed to stop gcc complaining about an always true comparison. */ |
| 494 | if ((ret != 0x1) || (buf[0] != (char)0xff)) { |
| 495 | msg_perr("Unexpected response to Command Prepare Receive Device" |
| 496 | " String!\n"); |
| 497 | return 1; |
| 498 | } |
| 499 | /* Command Receive Device String. */ |
| 500 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 501 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, |
| 502 | 0x10, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 503 | if (ret != 0x10) { |
| 504 | msg_perr("Incomplete/failed Command Receive Device String!\n"); |
| 505 | return 1; |
| 506 | } |
| 507 | buf[0x10] = '\0'; |
| 508 | msg_pdbg("Found a %s\n", buf); |
| 509 | if (memcmp(buf, "SF100", 0x5)) { |
| 510 | msg_perr("Device not a SF100!\n"); |
| 511 | return 1; |
| 512 | } |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 513 | if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) { |
| 514 | msg_perr("Unexpected firmware version string!\n"); |
| 515 | return 1; |
| 516 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 517 | /* Only these versions were tested. */ |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 518 | if (fw[0] < 2 || fw[0] > 5) { |
| 519 | msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0], |
| 520 | fw[1], fw[2]); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 521 | return 1; |
| 522 | } |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 523 | dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* Command A seems to be some sort of device init. It is either followed by |
| 528 | * dediprog_check_devicestring (often) or Command A (often) or |
| 529 | * Command F (once). |
| 530 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 531 | static int dediprog_command_a(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 532 | { |
| 533 | int ret; |
| 534 | char buf[0x1]; |
| 535 | |
| 536 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 537 | ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, |
| 538 | 0x1, DEFAULT_TIMEOUT); |
Mathias Krause | db7afc5 | 2010-11-09 23:30:43 +0000 | [diff] [blame] | 539 | if (ret < 0) { |
| 540 | msg_perr("Command A failed (%s)!\n", usb_strerror()); |
| 541 | return 1; |
| 542 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 543 | if ((ret != 0x1) || (buf[0] != 0x6f)) { |
| 544 | msg_perr("Unexpected response to Command A!\n"); |
| 545 | return 1; |
| 546 | } |
| 547 | return 0; |
| 548 | } |
| 549 | |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 550 | #if 0 |
| 551 | /* Something. |
| 552 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 553 | * Always preceded by Command Receive Device String |
| 554 | */ |
| 555 | static int dediprog_command_b(void) |
| 556 | { |
| 557 | int ret; |
| 558 | char buf[0x3]; |
| 559 | |
| 560 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 561 | ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf, |
| 562 | 0x3, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 563 | if (ret < 0) { |
| 564 | msg_perr("Command B failed (%s)!\n", usb_strerror()); |
| 565 | return 1; |
| 566 | } |
| 567 | if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) || |
| 568 | (buf[2] != 0xff)) { |
| 569 | msg_perr("Unexpected response to Command B!\n"); |
| 570 | return 1; |
| 571 | } |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | #endif |
| 576 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 577 | /* Command C is only sent after dediprog_check_devicestring, but not after every |
| 578 | * invocation of dediprog_check_devicestring. It is only sent after the first |
| 579 | * dediprog_command_a(); dediprog_check_devicestring() sequence in each session. |
| 580 | * I'm tempted to call this one start_SPI_engine or finish_init. |
| 581 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 582 | static int dediprog_command_c(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 583 | { |
| 584 | int ret; |
| 585 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 586 | ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, |
| 587 | 0x0, DEFAULT_TIMEOUT); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 588 | if (ret != 0x0) { |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 589 | msg_perr("Command C failed (%s)!\n", usb_strerror()); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 590 | return 1; |
| 591 | } |
| 592 | return 0; |
| 593 | } |
| 594 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 595 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 596 | /* Very strange. Seems to be a programmer keepalive or somesuch. |
| 597 | * Wait unsuccessfully for timeout ms to read one byte. |
| 598 | * Is usually called after setting voltage to 0. |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 599 | * Present in all logs with Firmware 2.1.1 and 3.1.8 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 600 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 601 | static int dediprog_command_f(int timeout) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 602 | { |
| 603 | int ret; |
| 604 | char buf[0x1]; |
| 605 | |
| 606 | memset(buf, 0, sizeof(buf)); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 607 | ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, |
| 608 | 0x1, timeout); |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 609 | /* This check is most probably wrong. Command F always causes a timeout |
| 610 | * in the logs, so we should check for timeout instead of checking for |
| 611 | * success. |
| 612 | */ |
| 613 | if (ret != 0x1) { |
| 614 | msg_perr("Command F failed (%s)!\n", usb_strerror()); |
| 615 | return 1; |
| 616 | } |
| 617 | return 0; |
| 618 | } |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 619 | |
| 620 | /* Start/stop blinking? |
| 621 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 622 | * Preceded by Command J |
| 623 | */ |
| 624 | static int dediprog_command_g(void) |
| 625 | { |
| 626 | int ret; |
| 627 | |
| 628 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT); |
| 629 | if (ret != 0x0) { |
| 630 | msg_perr("Command G failed (%s)!\n", usb_strerror()); |
| 631 | return 1; |
| 632 | } |
| 633 | return 0; |
| 634 | } |
| 635 | |
| 636 | /* Something. |
| 637 | * Present in all logs with firmware 5.1.5 |
| 638 | * Always preceded by Command Receive Device String |
| 639 | * Always followed by Command Set SPI Voltage nonzero |
| 640 | */ |
| 641 | static int dediprog_command_h(void) |
| 642 | { |
| 643 | int ret; |
| 644 | |
| 645 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT); |
| 646 | if (ret != 0x0) { |
| 647 | msg_perr("Command H failed (%s)!\n", usb_strerror()); |
| 648 | return 1; |
| 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | /* Shutdown for firmware 5.x? |
| 654 | * Present in all logs with firmware 5.1.5 |
| 655 | * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI) |
| 656 | * Always followed by Command Set SPI Voltage 0x0000 |
| 657 | */ |
| 658 | static int dediprog_command_i(void) |
| 659 | { |
| 660 | int ret; |
| 661 | |
| 662 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT); |
| 663 | if (ret != 0x0) { |
| 664 | msg_perr("Command I failed (%s)!\n", usb_strerror()); |
| 665 | return 1; |
| 666 | } |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | /* Start/stop blinking? |
| 671 | * Present in all logs with firmware 5.1.5 |
| 672 | * Always preceded by Command Receive Device String on 5.1.5 |
| 673 | * Always followed by Command Set SPI Voltage nonzero on 5.1.5 |
| 674 | * Present in eng_detect_blink.log with firmware 3.1.8 |
| 675 | * Preceded by Command B in eng_detect_blink.log |
| 676 | * Followed by Command G in eng_detect_blink.log |
| 677 | */ |
| 678 | static int dediprog_command_j(void) |
| 679 | { |
| 680 | int ret; |
| 681 | |
| 682 | ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT); |
| 683 | if (ret != 0x0) { |
| 684 | msg_perr("Command J failed (%s)!\n", usb_strerror()); |
| 685 | return 1; |
| 686 | } |
| 687 | return 0; |
| 688 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 689 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 690 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 691 | static int parse_voltage(char *voltage) |
| 692 | { |
| 693 | char *tmp = NULL; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 694 | int i; |
| 695 | int millivolt = 0, fraction = 0; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 696 | |
| 697 | if (!voltage || !strlen(voltage)) { |
| 698 | msg_perr("Empty voltage= specified.\n"); |
| 699 | return -1; |
| 700 | } |
| 701 | millivolt = (int)strtol(voltage, &tmp, 0); |
| 702 | voltage = tmp; |
| 703 | /* Handle "," and "." as decimal point. Everything after it is assumed |
| 704 | * to be in decimal notation. |
| 705 | */ |
| 706 | if ((*voltage == '.') || (*voltage == ',')) { |
| 707 | voltage++; |
| 708 | for (i = 0; i < 3; i++) { |
| 709 | fraction *= 10; |
| 710 | /* Don't advance if the current character is invalid, |
| 711 | * but continue multiplying. |
| 712 | */ |
| 713 | if ((*voltage < '0') || (*voltage > '9')) |
| 714 | continue; |
| 715 | fraction += *voltage - '0'; |
| 716 | voltage++; |
| 717 | } |
| 718 | /* Throw away remaining digits. */ |
| 719 | voltage += strspn(voltage, "0123456789"); |
| 720 | } |
| 721 | /* The remaining string must be empty or "mV" or "V". */ |
| 722 | tolower_string(voltage); |
| 723 | |
| 724 | /* No unit or "V". */ |
| 725 | if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) { |
| 726 | millivolt *= 1000; |
| 727 | millivolt += fraction; |
| 728 | } else if (!strncmp(voltage, "mv", 2) || |
| 729 | !strncmp(voltage, "milliv", 6)) { |
| 730 | /* No adjustment. fraction is discarded. */ |
| 731 | } else { |
| 732 | /* Garbage at the end of the string. */ |
| 733 | msg_perr("Garbage voltage= specified.\n"); |
| 734 | return -1; |
| 735 | } |
| 736 | return millivolt; |
| 737 | } |
| 738 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 739 | static const struct spi_programmer spi_programmer_dediprog = { |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 740 | .type = SPI_CONTROLLER_DEDIPROG, |
| 741 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 742 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 743 | .command = dediprog_spi_send_command, |
| 744 | .multicommand = default_spi_send_multicommand, |
| 745 | .read = dediprog_spi_read, |
| 746 | .write_256 = dediprog_spi_write_256, |
Nico Huber | a4b14f7 | 2012-06-19 12:06:53 +0000 | [diff] [blame] | 747 | .write_aai = dediprog_spi_write_aai, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 748 | }; |
| 749 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 750 | static int dediprog_shutdown(void *data) |
| 751 | { |
| 752 | msg_pspew("%s\n", __func__); |
| 753 | |
Carl-Daniel Hailfinger | 64204b5 | 2011-12-20 01:54:19 +0000 | [diff] [blame] | 754 | #if 0 |
| 755 | /* Shutdown on firmware 5.x */ |
| 756 | if (dediprog_firmwareversion == 5) |
| 757 | if (dediprog_command_i()) |
| 758 | return 1; |
| 759 | #endif |
| 760 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 761 | /* URB 28. Command Set SPI Voltage to 0. */ |
| 762 | if (dediprog_set_spi_voltage(0x0)) |
| 763 | return 1; |
| 764 | |
| 765 | if (usb_release_interface(dediprog_handle, 0)) { |
| 766 | msg_perr("Could not release USB interface!\n"); |
| 767 | return 1; |
| 768 | } |
| 769 | if (usb_close(dediprog_handle)) { |
| 770 | msg_perr("Could not close USB device!\n"); |
| 771 | return 1; |
| 772 | } |
| 773 | return 0; |
| 774 | } |
| 775 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 776 | /* URB numbers refer to the first log ever captured. */ |
| 777 | int dediprog_init(void) |
| 778 | { |
| 779 | struct usb_device *dev; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 780 | char *voltage; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 781 | int millivolt = 3500; |
| 782 | int ret; |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 783 | |
| 784 | msg_pspew("%s\n", __func__); |
| 785 | |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 786 | voltage = extract_programmer_param("voltage"); |
| 787 | if (voltage) { |
| 788 | millivolt = parse_voltage(voltage); |
| 789 | free(voltage); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 790 | if (millivolt < 0) |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 791 | return 1; |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 792 | msg_pinfo("Setting voltage to %i mV\n", millivolt); |
| 793 | } |
| 794 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 795 | /* Here comes the USB stuff. */ |
| 796 | usb_init(); |
| 797 | usb_find_busses(); |
| 798 | usb_find_devices(); |
| 799 | dev = get_device_by_vid_pid(0x0483, 0xdada); |
| 800 | if (!dev) { |
| 801 | msg_perr("Could not find a Dediprog SF100 on USB!\n"); |
| 802 | return 1; |
| 803 | } |
| 804 | msg_pdbg("Found USB device (%04x:%04x).\n", |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 805 | dev->descriptor.idVendor, dev->descriptor.idProduct); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 806 | dediprog_handle = usb_open(dev); |
Carl-Daniel Hailfinger | 482e974 | 2010-11-16 21:25:29 +0000 | [diff] [blame] | 807 | ret = usb_set_configuration(dediprog_handle, 1); |
| 808 | if (ret < 0) { |
| 809 | msg_perr("Could not set USB device configuration: %i %s\n", |
| 810 | ret, usb_strerror()); |
| 811 | if (usb_close(dediprog_handle)) |
| 812 | msg_perr("Could not close USB device!\n"); |
| 813 | return 1; |
| 814 | } |
| 815 | ret = usb_claim_interface(dediprog_handle, 0); |
| 816 | if (ret < 0) { |
| 817 | msg_perr("Could not claim USB device interface %i: %i %s\n", |
| 818 | 0, ret, usb_strerror()); |
| 819 | if (usb_close(dediprog_handle)) |
| 820 | msg_perr("Could not close USB device!\n"); |
| 821 | return 1; |
| 822 | } |
| 823 | dediprog_endpoint = 2; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 824 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 825 | if (register_shutdown(dediprog_shutdown, NULL)) |
| 826 | return 1; |
| 827 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 828 | dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON); |
| 829 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 830 | /* URB 6. Command A. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 831 | if (dediprog_command_a()) { |
| 832 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 833 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 834 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 835 | /* URB 7. Command A. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 836 | if (dediprog_command_a()) { |
| 837 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 838 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 839 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 840 | /* URB 8. Command Prepare Receive Device String. */ |
| 841 | /* URB 9. Command Receive Device String. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 842 | if (dediprog_check_devicestring()) { |
| 843 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 844 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 845 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 846 | /* URB 10. Command C. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 847 | if (dediprog_command_c()) { |
| 848 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 849 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 850 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 851 | /* URB 11. Command Set SPI Voltage. */ |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 852 | if (dediprog_set_spi_voltage(millivolt)) { |
| 853 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 854 | return 1; |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 855 | } |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 856 | |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 857 | register_spi_programmer(&spi_programmer_dediprog); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 858 | |
| 859 | /* RE leftover, leave in until the driver is complete. */ |
| 860 | #if 0 |
| 861 | /* Execute RDID by hand if you want to test it. */ |
| 862 | dediprog_do_stuff(); |
| 863 | #endif |
| 864 | |
Stefan Reinauer | 915b840 | 2011-01-28 09:00:15 +0000 | [diff] [blame] | 865 | dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF); |
| 866 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 867 | return 0; |
| 868 | } |
| 869 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 870 | #if 0 |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 871 | /* Leftovers from reverse engineering. Keep for documentation purposes until |
| 872 | * completely understood. |
| 873 | */ |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 874 | static int dediprog_do_stuff(void) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 875 | { |
| 876 | char buf[0x4]; |
| 877 | /* SPI command processing starts here. */ |
| 878 | |
| 879 | /* URB 12. Command Send SPI. */ |
| 880 | /* URB 13. Command Receive SPI. */ |
| 881 | memset(buf, 0, sizeof(buf)); |
| 882 | /* JEDEC RDID */ |
| 883 | msg_pdbg("Sending RDID\n"); |
| 884 | buf[0] = JEDEC_RDID; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 885 | if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, |
| 886 | (unsigned char *)buf, (unsigned char *)buf)) |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 887 | return 1; |
| 888 | msg_pdbg("Receiving response: "); |
| 889 | print_hex(buf, JEDEC_RDID_INSIZE); |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 890 | /* URB 14-27 are more SPI commands. */ |
| 891 | /* URB 28. Command Set SPI Voltage. */ |
| 892 | if (dediprog_set_spi_voltage(0x0)) |
| 893 | return 1; |
| 894 | /* URB 29-38. Command F, unsuccessful wait. */ |
| 895 | if (dediprog_command_f(544)) |
| 896 | return 1; |
| 897 | /* URB 39. Command Set SPI Voltage. */ |
| 898 | if (dediprog_set_spi_voltage(0x10)) |
| 899 | return 1; |
| 900 | /* URB 40. Command Set SPI Speed. */ |
| 901 | if (dediprog_set_spi_speed(0x2)) |
| 902 | return 1; |
| 903 | /* URB 41 is just URB 28. */ |
| 904 | /* URB 42,44,46,48,51,53 is just URB 8. */ |
| 905 | /* URB 43,45,47,49,52,54 is just URB 9. */ |
| 906 | /* URB 50 is just URB 6/7. */ |
| 907 | /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/ |
| 908 | /* URB 132,134 is just URB 6/7. */ |
| 909 | /* URB 133 is just URB 29-38. */ |
| 910 | /* URB 135 is just URB 8. */ |
| 911 | /* URB 136 is just URB 9. */ |
| 912 | /* URB 137 is just URB 11. */ |
| 913 | |
Carl-Daniel Hailfinger | ff30d8a | 2011-01-20 21:05:15 +0000 | [diff] [blame] | 914 | /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */ |
| 915 | /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */ |
| 916 | /* Bulk transfer sizes for Command Start Bulk Read/Write are always |
| 917 | * 512 bytes, rest is filled with 0xff. |
| 918 | */ |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 919 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 920 | return 0; |
| 921 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 922 | #endif |