blob: cdd27b099de3d4ba953fcd646eb357949b9377c8 [file] [log] [blame]
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001/*
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 Krausedb7afc52010-11-09 23:30:43 +000020#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000021#include <string.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000022#include <usb.h>
23#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000024#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000026#include "spi.h"
27
Stefan Reinauer915b8402011-01-28 09:00:15 +000028#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000029#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000030static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +000031static int dediprog_firmwareversion;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000032static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000033
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000034#if 0
35/* Might be useful for other pieces of code as well. */
36static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000037{
38 size_t i;
39
40 for (i = 0; i < len; i++)
41 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
42}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000043#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000044
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000045/* Might be useful for other USB devices as well. static for now. */
46static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000047{
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 Reinauer915b8402011-01-28 09:00:15 +000062/* 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)
69static int current_led_status = -1;
70
71static int dediprog_set_leds(int leds)
72{
73 int ret, target_leds;
Uwe Hermann91f4afa2011-07-28 08:13:25 +000074
Stefan Reinauer915b8402011-01-28 09:00:15 +000075 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 Hermann91f4afa2011-07-28 08:13:25 +000095 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
96 NULL, 0x0, DEFAULT_TIMEOUT);
Stefan Reinauer915b8402011-01-28 09:00:15 +000097 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +000098 msg_perr("Command Set LED 0x%x failed (%s)!\n",
99 leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000100 return 1;
101 }
102
103 current_led_status = leds;
104
105 return 0;
106}
107
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000108static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000109{
110 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000111 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000112
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000113 switch (millivolt) {
114 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000115 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000116 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000117 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000118 case 1800:
119 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000120 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000121 case 2500:
122 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000123 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000124 case 3500:
125 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000126 break;
127 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000128 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000129 return 1;
130 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000131 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
132 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000133
Nico Huber4099a8a2012-06-16 00:02:27 +0000134 if (voltage_selector == 0) {
135 /* Wait some time as the original driver does. */
136 programmer_delay(200 * 1000);
137 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000138 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
139 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000140 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000141 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
142 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000143 return 1;
144 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000145 if (voltage_selector != 0) {
146 /* Wait some time as the original driver does. */
147 programmer_delay(200 * 1000);
148 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000149 return 0;
150}
151
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000152#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000153/* 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 Hailfingerad3cc552010-07-03 11:02:10 +0000160static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000161{
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 Hermann91f4afa2011-07-28 08:13:25 +0000201 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
202 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000203 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 Hailfingerad3cc552010-07-03 11:02:10 +0000209#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000210
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000211/* 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 Hailfinger63fd9022011-12-14 22:25:15 +0000216static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000217 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000218{
219 int ret;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000220 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000221 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000222 const unsigned int chunksize = 0x200;
223 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000224 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 Hailfinger63fd9022011-12-14 22:25:15 +0000264static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000265 unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000266{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000267 int ret;
268 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000269 const unsigned int chunksize = 0x200;
270 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
271 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000272
Stefan Reinauer915b8402011-01-28 09:00:15 +0000273 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
274
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000275 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 Reinauer915b8402011-01-28 09:00:15 +0000279 if (ret) {
280 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000281 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000282 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000283 }
284
285 /* Round down. */
286 bulklen = (len - residue) / chunksize * chunksize;
287 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
288 bulklen);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000289 if (ret) {
290 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000291 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000292 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000293
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 Reinauer915b8402011-01-28 09:00:15 +0000300 if (ret) {
301 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000302 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000303 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000304 }
305
Stefan Reinauer915b8402011-01-28 09:00:15 +0000306 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000307 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000308}
309
Paul Menzelac427b22012-02-16 21:07:07 +0000310/* Bulk write interface, will write multiple page_size byte chunks aligned to page_size bytes.
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000311 * @start start address
312 * @len length
313 * @return 0 on success, 1 on failure
314 */
315static 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 Hailfinger63fd9022011-12-14 22:25:15 +0000369static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000370 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000371{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000372 int ret;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000373 const unsigned int chunksize = flash->page_size;
374 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
375 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000376
377 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
378
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000379 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 Reinauer915b8402011-01-28 09:00:15 +0000389
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000390 /* 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 Reinauer915b8402011-01-28 09:00:15 +0000395 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000396 return ret;
397 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000398
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000399 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 Hailfinger306b8182010-11-23 21:28:16 +0000413}
414
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000415static 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 Hailfingerd38fac82010-01-19 11:15:48 +0000420{
421 int ret;
422
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000423 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000424 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000425 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000426 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000427 return 1;
428 }
429 /* 16 byte reads should work. */
430 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000431 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000432 return 1;
433 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000434
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000435 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
436 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
437 DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000438 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000439 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
440 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000441 return 1;
442 }
443 if (!readcnt)
444 return 0;
445 memset(readarr, 0, readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000446 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
447 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000448 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000449 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
450 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000451 return 1;
452 }
453 return 0;
454}
455
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000456static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000457{
458 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000459 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000460 char buf[0x11];
461
462 /* Command Prepare Receive Device String. */
463 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000464 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
465 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000466 /* 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 Hermann91f4afa2011-07-28 08:13:25 +0000474 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
475 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000476 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 Krausedb7afc52010-11-09 23:30:43 +0000486 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 Hailfingerd38fac82010-01-19 11:15:48 +0000490 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000491 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 Hailfingerd38fac82010-01-19 11:15:48 +0000494 return 1;
495 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000496 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000497 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 Hailfingerad3cc552010-07-03 11:02:10 +0000504static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000505{
506 int ret;
507 char buf[0x1];
508
509 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000510 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
511 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000512 if (ret < 0) {
513 msg_perr("Command A failed (%s)!\n", usb_strerror());
514 return 1;
515 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000516 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 Hailfingerff30d8a2011-01-20 21:05:15 +0000523#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 */
528static int dediprog_command_b(void)
529{
530 int ret;
531 char buf[0x3];
532
533 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000534 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
535 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000536 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 Hailfingerd38fac82010-01-19 11:15:48 +0000550/* 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 Hailfingerad3cc552010-07-03 11:02:10 +0000555static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000556{
557 int ret;
558
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000559 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
560 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000561 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000562 msg_perr("Command C failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000563 return 1;
564 }
565 return 0;
566}
567
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000568#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000569/* 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 Hailfingerff30d8a2011-01-20 21:05:15 +0000572 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000573 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000574static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000575{
576 int ret;
577 char buf[0x1];
578
579 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000580 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
581 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000582 /* 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 Hailfinger64204b52011-12-20 01:54:19 +0000592
593/* Start/stop blinking?
594 * Present in eng_detect_blink.log with firmware 3.1.8
595 * Preceded by Command J
596 */
597static 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 */
614static 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 */
631static 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 */
651static 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 Hailfingerad3cc552010-07-03 11:02:10 +0000662#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000663
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000664static int parse_voltage(char *voltage)
665{
666 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000667 int i;
668 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000669
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 Karcherb9dbe482011-05-11 17:07:07 +0000712static const struct spi_programmer spi_programmer_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000713 .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 Huber7bca1262012-06-15 22:28:12 +0000720 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000721};
722
David Hendricks8bb20212011-06-14 01:35:36 +0000723static int dediprog_shutdown(void *data)
724{
725 msg_pspew("%s\n", __func__);
726
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000727#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 Hendricks8bb20212011-06-14 01:35:36 +0000734 /* 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 Hailfingerd38fac82010-01-19 11:15:48 +0000749/* URB numbers refer to the first log ever captured. */
750int dediprog_init(void)
751{
752 struct usb_device *dev;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000753 char *voltage;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000754 int millivolt = 3500;
755 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000756
757 msg_pspew("%s\n", __func__);
758
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000759 voltage = extract_programmer_param("voltage");
760 if (voltage) {
761 millivolt = parse_voltage(voltage);
762 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000763 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000764 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000765 msg_pinfo("Setting voltage to %i mV\n", millivolt);
766 }
767
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000768 /* 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 Hermann91f4afa2011-07-28 08:13:25 +0000778 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000779 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000780 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 Reinauer915b8402011-01-28 09:00:15 +0000797
David Hendricks8bb20212011-06-14 01:35:36 +0000798 if (register_shutdown(dediprog_shutdown, NULL))
799 return 1;
800
Stefan Reinauer915b8402011-01-28 09:00:15 +0000801 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
802
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000803 /* URB 6. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000804 if (dediprog_command_a()) {
805 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000806 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000807 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000808 /* URB 7. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000809 if (dediprog_command_a()) {
810 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000811 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000812 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000813 /* URB 8. Command Prepare Receive Device String. */
814 /* URB 9. Command Receive Device String. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000815 if (dediprog_check_devicestring()) {
816 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000817 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000818 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000819 /* URB 10. Command C. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000820 if (dediprog_command_c()) {
821 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000822 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000823 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000824 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000825 if (dediprog_set_spi_voltage(millivolt)) {
826 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000827 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000828 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000829
Michael Karcherb9dbe482011-05-11 17:07:07 +0000830 register_spi_programmer(&spi_programmer_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000831
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 Reinauer915b8402011-01-28 09:00:15 +0000838 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
839
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000840 return 0;
841}
842
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000843#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000844/* Leftovers from reverse engineering. Keep for documentation purposes until
845 * completely understood.
846 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000847static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000848{
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 Hermann91f4afa2011-07-28 08:13:25 +0000858 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
859 (unsigned char *)buf, (unsigned char *)buf))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000860 return 1;
861 msg_pdbg("Receiving response: ");
862 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000863 /* 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 Hailfingerff30d8a2011-01-20 21:05:15 +0000887 /* 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 Hailfingerd38fac82010-01-19 11:15:48 +0000892
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000893 return 0;
894}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000895#endif