blob: f5d08ff4dfa0209c2c480b8db5ca16a50328cc15 [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
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000134 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
135 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000136 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000137 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
138 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000139 return 1;
140 }
141 return 0;
142}
143
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000144#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000145/* After dediprog_set_spi_speed, the original app always calls
146 * dediprog_set_spi_voltage(0) and then
147 * dediprog_check_devicestring() four times in a row.
148 * After that, dediprog_command_a() is called.
149 * This looks suspiciously like the microprocessor in the SF100 has to be
150 * restarted/reinitialized in case the speed changes.
151 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000152static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000153{
154 int ret;
155 unsigned int khz;
156
157 /* Case 1 and 2 are in weird order. Probably an organically "grown"
158 * interface.
159 * Base frequency is 24000 kHz, divisors are (in order)
160 * 1, 3, 2, 8, 11, 16, 32, 64.
161 */
162 switch (speed) {
163 case 0x0:
164 khz = 24000;
165 break;
166 case 0x1:
167 khz = 8000;
168 break;
169 case 0x2:
170 khz = 12000;
171 break;
172 case 0x3:
173 khz = 3000;
174 break;
175 case 0x4:
176 khz = 2180;
177 break;
178 case 0x5:
179 khz = 1500;
180 break;
181 case 0x6:
182 khz = 750;
183 break;
184 case 0x7:
185 khz = 375;
186 break;
187 default:
188 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
189 return 1;
190 }
191 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
192
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000193 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
194 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000195 if (ret != 0x0) {
196 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
197 return 1;
198 }
199 return 0;
200}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000201#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000202
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000203/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
204 * @start start address
205 * @len length
206 * @return 0 on success, 1 on failure
207 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000208static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000209 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000210{
211 int ret;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000212 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000213 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000214 const unsigned int chunksize = 0x200;
215 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000216 const char count_and_chunk[] = {count & 0xff,
217 (count >> 8) & 0xff,
218 chunksize & 0xff,
219 (chunksize >> 8) & 0xff};
220
221 if ((start % chunksize) || (len % chunksize)) {
222 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
223 "at flashrom@flashrom.org\n", __func__, start, len);
224 return 1;
225 }
226
227 /* No idea if the hardware can handle empty reads, so chicken out. */
228 if (!len)
229 return 0;
230 /* Command Read SPI Bulk. No idea which read command is used on the
231 * SPI side.
232 */
233 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
234 start / 0x10000, (char *)count_and_chunk,
235 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
236 if (ret != sizeof(count_and_chunk)) {
237 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
238 usb_strerror());
239 return 1;
240 }
241
242 for (i = 0; i < count; i++) {
243 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
244 (char *)buf + i * chunksize, chunksize,
245 DEFAULT_TIMEOUT);
246 if (ret != chunksize) {
247 msg_perr("SPI bulk read %i failed, expected %i, got %i "
248 "%s!\n", i, chunksize, ret, usb_strerror());
249 return 1;
250 }
251 }
252
253 return 0;
254}
255
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000256static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000257 unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000258{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000259 int ret;
260 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000261 const unsigned int chunksize = 0x200;
262 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
263 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000264
Stefan Reinauer915b8402011-01-28 09:00:15 +0000265 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
266
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000267 if (residue) {
268 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
269 start, residue);
270 ret = spi_read_chunked(flash, buf, start, residue, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000271 if (ret) {
272 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000273 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000274 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000275 }
276
277 /* Round down. */
278 bulklen = (len - residue) / chunksize * chunksize;
279 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
280 bulklen);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000281 if (ret) {
282 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000283 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000284 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000285
286 len -= residue + bulklen;
287 if (len) {
288 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
289 start, len);
290 ret = spi_read_chunked(flash, buf + residue + bulklen,
291 start + residue + bulklen, len, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000292 if (ret) {
293 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000294 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000295 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000296 }
297
Stefan Reinauer915b8402011-01-28 09:00:15 +0000298 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000299 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000300}
301
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000302/* Bulk write interface, will read multiple page_size byte chunks aligned to page_size bytes.
303 * @start start address
304 * @len length
305 * @return 0 on success, 1 on failure
306 */
307static int dediprog_spi_bulk_write(struct flashctx *flash, uint8_t *buf,
308 unsigned int start, unsigned int len)
309{
310 int ret;
311 unsigned int i;
312 /* USB transfer size must be 512, other sizes will NOT work at all.
313 * chunksize is the real data size per USB bulk transfer. The remaining
314 * space in a USB bulk transfer must be filled with 0xff padding.
315 */
316 const unsigned int chunksize = flash->page_size;
317 const unsigned int count = len / chunksize;
318 const char count_and_chunk[] = {count & 0xff,
319 (count >> 8) & 0xff,
320 chunksize & 0xff,
321 (chunksize >> 8) & 0xff};
322 char usbbuf[512];
323
324 if ((start % chunksize) || (len % chunksize)) {
325 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
326 "at flashrom@flashrom.org\n", __func__, start, len);
327 return 1;
328 }
329
330 /* No idea if the hardware can handle empty writes, so chicken out. */
331 if (!len)
332 return 0;
333 /* Command Write SPI Bulk. No idea which write command is used on the
334 * SPI side.
335 */
336 ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000,
337 start / 0x10000, (char *)count_and_chunk,
338 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
339 if (ret != sizeof(count_and_chunk)) {
340 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
341 usb_strerror());
342 return 1;
343 }
344
345 for (i = 0; i < count; i++) {
346 memset(usbbuf, 0xff, sizeof(usbbuf));
347 memcpy(usbbuf, buf + i * chunksize, chunksize);
348 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
349 usbbuf, 512,
350 DEFAULT_TIMEOUT);
351 if (ret != 512) {
352 msg_perr("SPI bulk write failed, expected %i, got %i "
353 "%s!\n", 512, ret, usb_strerror());
354 return 1;
355 }
356 }
357
358 return 0;
359}
360
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000361static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000362 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000363{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000364 int ret;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000365 const unsigned int chunksize = flash->page_size;
366 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
367 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000368
369 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
370
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000371 if (residue) {
372 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
373 start, residue);
374 /* No idea about the real limit. Maybe 12, maybe more. */
375 ret = spi_write_chunked(flash, buf, start, residue, 12);
376 if (ret) {
377 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
378 return ret;
379 }
380 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000381
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000382 /* Round down. */
383 bulklen = (len - residue) / chunksize * chunksize;
384 ret = dediprog_spi_bulk_write(flash, buf + residue, start + residue,
385 bulklen);
386 if (ret) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000387 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000388 return ret;
389 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000390
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000391 len -= residue + bulklen;
392 if (len) {
393 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
394 start, len);
395 ret = spi_write_chunked(flash, buf + residue + bulklen,
396 start + residue + bulklen, len, 12);
397 if (ret) {
398 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
399 return ret;
400 }
401 }
402
403 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
404 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000405}
406
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000407static int dediprog_spi_send_command(struct flashctx *flash,
408 unsigned int writecnt,
409 unsigned int readcnt,
410 const unsigned char *writearr,
411 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000412{
413 int ret;
414
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000415 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000416 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000417 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000418 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000419 return 1;
420 }
421 /* 16 byte reads should work. */
422 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000423 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000424 return 1;
425 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000426
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000427 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
428 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
429 DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000430 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000431 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
432 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000433 return 1;
434 }
435 if (!readcnt)
436 return 0;
437 memset(readarr, 0, readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000438 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
439 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000440 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000441 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
442 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000443 return 1;
444 }
445 return 0;
446}
447
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000448static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000449{
450 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000451 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000452 char buf[0x11];
453
454 /* Command Prepare Receive Device String. */
455 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000456 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
457 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000458 /* The char casting is needed to stop gcc complaining about an always true comparison. */
459 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
460 msg_perr("Unexpected response to Command Prepare Receive Device"
461 " String!\n");
462 return 1;
463 }
464 /* Command Receive Device String. */
465 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000466 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
467 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000468 if (ret != 0x10) {
469 msg_perr("Incomplete/failed Command Receive Device String!\n");
470 return 1;
471 }
472 buf[0x10] = '\0';
473 msg_pdbg("Found a %s\n", buf);
474 if (memcmp(buf, "SF100", 0x5)) {
475 msg_perr("Device not a SF100!\n");
476 return 1;
477 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000478 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
479 msg_perr("Unexpected firmware version string!\n");
480 return 1;
481 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000482 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000483 if (fw[0] < 2 || fw[0] > 5) {
484 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
485 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000486 return 1;
487 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000488 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000489 return 0;
490}
491
492/* Command A seems to be some sort of device init. It is either followed by
493 * dediprog_check_devicestring (often) or Command A (often) or
494 * Command F (once).
495 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000496static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000497{
498 int ret;
499 char buf[0x1];
500
501 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000502 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
503 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000504 if (ret < 0) {
505 msg_perr("Command A failed (%s)!\n", usb_strerror());
506 return 1;
507 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000508 if ((ret != 0x1) || (buf[0] != 0x6f)) {
509 msg_perr("Unexpected response to Command A!\n");
510 return 1;
511 }
512 return 0;
513}
514
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000515#if 0
516/* Something.
517 * Present in eng_detect_blink.log with firmware 3.1.8
518 * Always preceded by Command Receive Device String
519 */
520static int dediprog_command_b(void)
521{
522 int ret;
523 char buf[0x3];
524
525 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000526 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
527 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000528 if (ret < 0) {
529 msg_perr("Command B failed (%s)!\n", usb_strerror());
530 return 1;
531 }
532 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
533 (buf[2] != 0xff)) {
534 msg_perr("Unexpected response to Command B!\n");
535 return 1;
536 }
537
538 return 0;
539}
540#endif
541
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000542/* Command C is only sent after dediprog_check_devicestring, but not after every
543 * invocation of dediprog_check_devicestring. It is only sent after the first
544 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
545 * I'm tempted to call this one start_SPI_engine or finish_init.
546 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000547static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000548{
549 int ret;
550
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000551 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
552 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000553 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000554 msg_perr("Command C failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000555 return 1;
556 }
557 return 0;
558}
559
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000560#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000561/* Very strange. Seems to be a programmer keepalive or somesuch.
562 * Wait unsuccessfully for timeout ms to read one byte.
563 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000564 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000565 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000566static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000567{
568 int ret;
569 char buf[0x1];
570
571 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000572 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
573 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000574 /* This check is most probably wrong. Command F always causes a timeout
575 * in the logs, so we should check for timeout instead of checking for
576 * success.
577 */
578 if (ret != 0x1) {
579 msg_perr("Command F failed (%s)!\n", usb_strerror());
580 return 1;
581 }
582 return 0;
583}
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000584
585/* Start/stop blinking?
586 * Present in eng_detect_blink.log with firmware 3.1.8
587 * Preceded by Command J
588 */
589static int dediprog_command_g(void)
590{
591 int ret;
592
593 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
594 if (ret != 0x0) {
595 msg_perr("Command G failed (%s)!\n", usb_strerror());
596 return 1;
597 }
598 return 0;
599}
600
601/* Something.
602 * Present in all logs with firmware 5.1.5
603 * Always preceded by Command Receive Device String
604 * Always followed by Command Set SPI Voltage nonzero
605 */
606static int dediprog_command_h(void)
607{
608 int ret;
609
610 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
611 if (ret != 0x0) {
612 msg_perr("Command H failed (%s)!\n", usb_strerror());
613 return 1;
614 }
615 return 0;
616}
617
618/* Shutdown for firmware 5.x?
619 * Present in all logs with firmware 5.1.5
620 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
621 * Always followed by Command Set SPI Voltage 0x0000
622 */
623static int dediprog_command_i(void)
624{
625 int ret;
626
627 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
628 if (ret != 0x0) {
629 msg_perr("Command I failed (%s)!\n", usb_strerror());
630 return 1;
631 }
632 return 0;
633}
634
635/* Start/stop blinking?
636 * Present in all logs with firmware 5.1.5
637 * Always preceded by Command Receive Device String on 5.1.5
638 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
639 * Present in eng_detect_blink.log with firmware 3.1.8
640 * Preceded by Command B in eng_detect_blink.log
641 * Followed by Command G in eng_detect_blink.log
642 */
643static int dediprog_command_j(void)
644{
645 int ret;
646
647 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
648 if (ret != 0x0) {
649 msg_perr("Command J failed (%s)!\n", usb_strerror());
650 return 1;
651 }
652 return 0;
653}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000654#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000655
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000656static int parse_voltage(char *voltage)
657{
658 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000659 int i;
660 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000661
662 if (!voltage || !strlen(voltage)) {
663 msg_perr("Empty voltage= specified.\n");
664 return -1;
665 }
666 millivolt = (int)strtol(voltage, &tmp, 0);
667 voltage = tmp;
668 /* Handle "," and "." as decimal point. Everything after it is assumed
669 * to be in decimal notation.
670 */
671 if ((*voltage == '.') || (*voltage == ',')) {
672 voltage++;
673 for (i = 0; i < 3; i++) {
674 fraction *= 10;
675 /* Don't advance if the current character is invalid,
676 * but continue multiplying.
677 */
678 if ((*voltage < '0') || (*voltage > '9'))
679 continue;
680 fraction += *voltage - '0';
681 voltage++;
682 }
683 /* Throw away remaining digits. */
684 voltage += strspn(voltage, "0123456789");
685 }
686 /* The remaining string must be empty or "mV" or "V". */
687 tolower_string(voltage);
688
689 /* No unit or "V". */
690 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
691 millivolt *= 1000;
692 millivolt += fraction;
693 } else if (!strncmp(voltage, "mv", 2) ||
694 !strncmp(voltage, "milliv", 6)) {
695 /* No adjustment. fraction is discarded. */
696 } else {
697 /* Garbage at the end of the string. */
698 msg_perr("Garbage voltage= specified.\n");
699 return -1;
700 }
701 return millivolt;
702}
703
Michael Karcherb9dbe482011-05-11 17:07:07 +0000704static const struct spi_programmer spi_programmer_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000705 .type = SPI_CONTROLLER_DEDIPROG,
706 .max_data_read = MAX_DATA_UNSPECIFIED,
707 .max_data_write = MAX_DATA_UNSPECIFIED,
708 .command = dediprog_spi_send_command,
709 .multicommand = default_spi_send_multicommand,
710 .read = dediprog_spi_read,
711 .write_256 = dediprog_spi_write_256,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000712};
713
David Hendricks8bb20212011-06-14 01:35:36 +0000714static int dediprog_shutdown(void *data)
715{
716 msg_pspew("%s\n", __func__);
717
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000718#if 0
719 /* Shutdown on firmware 5.x */
720 if (dediprog_firmwareversion == 5)
721 if (dediprog_command_i())
722 return 1;
723#endif
724
David Hendricks8bb20212011-06-14 01:35:36 +0000725 /* URB 28. Command Set SPI Voltage to 0. */
726 if (dediprog_set_spi_voltage(0x0))
727 return 1;
728
729 if (usb_release_interface(dediprog_handle, 0)) {
730 msg_perr("Could not release USB interface!\n");
731 return 1;
732 }
733 if (usb_close(dediprog_handle)) {
734 msg_perr("Could not close USB device!\n");
735 return 1;
736 }
737 return 0;
738}
739
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000740/* URB numbers refer to the first log ever captured. */
741int dediprog_init(void)
742{
743 struct usb_device *dev;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000744 char *voltage;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000745 int millivolt = 3500;
746 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000747
748 msg_pspew("%s\n", __func__);
749
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000750 voltage = extract_programmer_param("voltage");
751 if (voltage) {
752 millivolt = parse_voltage(voltage);
753 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000754 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000755 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000756 msg_pinfo("Setting voltage to %i mV\n", millivolt);
757 }
758
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000759 /* Here comes the USB stuff. */
760 usb_init();
761 usb_find_busses();
762 usb_find_devices();
763 dev = get_device_by_vid_pid(0x0483, 0xdada);
764 if (!dev) {
765 msg_perr("Could not find a Dediprog SF100 on USB!\n");
766 return 1;
767 }
768 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000769 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000770 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000771 ret = usb_set_configuration(dediprog_handle, 1);
772 if (ret < 0) {
773 msg_perr("Could not set USB device configuration: %i %s\n",
774 ret, usb_strerror());
775 if (usb_close(dediprog_handle))
776 msg_perr("Could not close USB device!\n");
777 return 1;
778 }
779 ret = usb_claim_interface(dediprog_handle, 0);
780 if (ret < 0) {
781 msg_perr("Could not claim USB device interface %i: %i %s\n",
782 0, ret, usb_strerror());
783 if (usb_close(dediprog_handle))
784 msg_perr("Could not close USB device!\n");
785 return 1;
786 }
787 dediprog_endpoint = 2;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000788
David Hendricks8bb20212011-06-14 01:35:36 +0000789 if (register_shutdown(dediprog_shutdown, NULL))
790 return 1;
791
Stefan Reinauer915b8402011-01-28 09:00:15 +0000792 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
793
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000794 /* URB 6. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000795 if (dediprog_command_a()) {
796 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000797 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000798 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000799 /* URB 7. Command A. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000800 if (dediprog_command_a()) {
801 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000802 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000803 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000804 /* URB 8. Command Prepare Receive Device String. */
805 /* URB 9. Command Receive Device String. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000806 if (dediprog_check_devicestring()) {
807 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000808 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000809 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000810 /* URB 10. Command C. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000811 if (dediprog_command_c()) {
812 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000813 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000814 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000815 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000816 if (dediprog_set_spi_voltage(millivolt)) {
817 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000818 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000819 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000820
Michael Karcherb9dbe482011-05-11 17:07:07 +0000821 register_spi_programmer(&spi_programmer_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000822
823 /* RE leftover, leave in until the driver is complete. */
824#if 0
825 /* Execute RDID by hand if you want to test it. */
826 dediprog_do_stuff();
827#endif
828
Stefan Reinauer915b8402011-01-28 09:00:15 +0000829 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
830
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000831 return 0;
832}
833
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000834#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000835/* Leftovers from reverse engineering. Keep for documentation purposes until
836 * completely understood.
837 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000838static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000839{
840 char buf[0x4];
841 /* SPI command processing starts here. */
842
843 /* URB 12. Command Send SPI. */
844 /* URB 13. Command Receive SPI. */
845 memset(buf, 0, sizeof(buf));
846 /* JEDEC RDID */
847 msg_pdbg("Sending RDID\n");
848 buf[0] = JEDEC_RDID;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000849 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
850 (unsigned char *)buf, (unsigned char *)buf))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000851 return 1;
852 msg_pdbg("Receiving response: ");
853 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000854 /* URB 14-27 are more SPI commands. */
855 /* URB 28. Command Set SPI Voltage. */
856 if (dediprog_set_spi_voltage(0x0))
857 return 1;
858 /* URB 29-38. Command F, unsuccessful wait. */
859 if (dediprog_command_f(544))
860 return 1;
861 /* URB 39. Command Set SPI Voltage. */
862 if (dediprog_set_spi_voltage(0x10))
863 return 1;
864 /* URB 40. Command Set SPI Speed. */
865 if (dediprog_set_spi_speed(0x2))
866 return 1;
867 /* URB 41 is just URB 28. */
868 /* URB 42,44,46,48,51,53 is just URB 8. */
869 /* URB 43,45,47,49,52,54 is just URB 9. */
870 /* URB 50 is just URB 6/7. */
871 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
872 /* URB 132,134 is just URB 6/7. */
873 /* URB 133 is just URB 29-38. */
874 /* URB 135 is just URB 8. */
875 /* URB 136 is just URB 9. */
876 /* URB 137 is just URB 11. */
877
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000878 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
879 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
880 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
881 * 512 bytes, rest is filled with 0xff.
882 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000883
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000884 return 0;
885}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000886#endif