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