blob: 424849916e27bf08f1fe991f9e484d1941286266 [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
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000020#include "platform.h"
21
Mathias Krausedb7afc52010-11-09 23:30:43 +000022#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000023#include <string.h>
Stefan Taunere34e3e82013-01-01 00:06:51 +000024#include <limits.h>
Nathan Laredo21541a62012-12-24 22:07:36 +000025#include <errno.h>
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000026
27#if IS_WINDOWS
28#include <lusb0_usb.h>
29#else
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000030#include <usb.h>
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000031#endif
32
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000033#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000034#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000035#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000036#include "spi.h"
37
Stefan Reinauer915b8402011-01-28 09:00:15 +000038#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000039#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000040static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +000041static int dediprog_firmwareversion;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000042static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000043
Nico Hubera4b14f72012-06-19 12:06:53 +000044#define DEDI_SPI_CMD_PAGEWRITE 0x1
45#define DEDI_SPI_CMD_AAIWRITE 0x4
46
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000047#if 0
48/* Might be useful for other pieces of code as well. */
49static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000050{
51 size_t i;
52
53 for (i = 0; i < len; i++)
54 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
55}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000056#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000057
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000058/* Might be useful for other USB devices as well. static for now. */
Nathan Laredo21541a62012-12-24 22:07:36 +000059/* device parameter allows user to specify one device of multiple installed */
60static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid, unsigned int device)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000061{
62 struct usb_bus *bus;
63 struct usb_device *dev;
64
65 for (bus = usb_get_busses(); bus; bus = bus->next)
66 for (dev = bus->devices; dev; dev = dev->next)
67 if ((dev->descriptor.idVendor == vid) &&
Nathan Laredo21541a62012-12-24 22:07:36 +000068 (dev->descriptor.idProduct == pid)) {
69 if (device == 0)
70 return dev;
71 device--;
72 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000073
74 return NULL;
75}
76
77//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
78
Stefan Reinauer915b8402011-01-28 09:00:15 +000079/* Set/clear LEDs on dediprog */
80#define PASS_ON (0 << 0)
81#define PASS_OFF (1 << 0)
82#define BUSY_ON (0 << 1)
83#define BUSY_OFF (1 << 1)
84#define ERROR_ON (0 << 2)
85#define ERROR_OFF (1 << 2)
86static int current_led_status = -1;
87
88static int dediprog_set_leds(int leds)
89{
90 int ret, target_leds;
Uwe Hermann91f4afa2011-07-28 08:13:25 +000091
Stefan Reinauer915b8402011-01-28 09:00:15 +000092 if (leds < 0 || leds > 7)
93 leds = 0; // Bogus value, enable all LEDs
94
95 if (leds == current_led_status)
96 return 0;
97
98 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
99 * two LEDs, and they were reversed. So map them around if
100 * we have an old device. On those devices the LEDs map as
101 * follows:
102 * bit 2 == 0: green light is on.
103 * bit 0 == 0: red light is on.
104 */
105 if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
106 target_leds = ((leds & ERROR_OFF) >> 2) |
107 ((leds & PASS_OFF) << 2);
108 } else {
109 target_leds = leds;
110 }
111
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000112 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
113 NULL, 0x0, DEFAULT_TIMEOUT);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000114 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000115 msg_perr("Command Set LED 0x%x failed (%s)!\n",
116 leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000117 return 1;
118 }
119
120 current_led_status = leds;
121
122 return 0;
123}
124
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000125static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000126{
127 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000128 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000129
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000130 switch (millivolt) {
131 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000132 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000133 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000134 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000135 case 1800:
136 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000137 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000138 case 2500:
139 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000140 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000141 case 3500:
142 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000143 break;
144 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000145 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000146 return 1;
147 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000148 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
149 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000150
Nico Huber4099a8a2012-06-16 00:02:27 +0000151 if (voltage_selector == 0) {
152 /* Wait some time as the original driver does. */
153 programmer_delay(200 * 1000);
154 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000155 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
156 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000157 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000158 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
159 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000160 return 1;
161 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000162 if (voltage_selector != 0) {
163 /* Wait some time as the original driver does. */
164 programmer_delay(200 * 1000);
165 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000166 return 0;
167}
168
Nico Huber77fa67d2013-02-20 18:03:36 +0000169struct dediprog_spispeeds {
170 const char *const name;
171 const int speed;
172};
173
174static const struct dediprog_spispeeds spispeeds[] = {
175 { "24M", 0x0 },
176 { "12M", 0x2 },
177 { "8M", 0x1 },
178 { "3M", 0x3 },
179 { "2.18M", 0x4 },
180 { "1.5M", 0x5 },
181 { "750k", 0x6 },
182 { "375k", 0x7 },
183 { NULL, 0x0 },
184};
185
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000186/* After dediprog_set_spi_speed, the original app always calls
187 * dediprog_set_spi_voltage(0) and then
188 * dediprog_check_devicestring() four times in a row.
189 * After that, dediprog_command_a() is called.
190 * This looks suspiciously like the microprocessor in the SF100 has to be
191 * restarted/reinitialized in case the speed changes.
192 */
Nico Huber77fa67d2013-02-20 18:03:36 +0000193static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000194{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000195 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
196 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
197 return 0;
198 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000199
Patrick Georgiefe2d432013-05-23 21:47:46 +0000200 msg_pdbg("SPI speed is %s Hz\n", spispeeds[spispeed_idx].name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000201
Patrick Georgiefe2d432013-05-23 21:47:46 +0000202 int ret = usb_control_msg(dediprog_handle, 0x42, 0x61, spispeeds[spispeed_idx].speed, 0xff,
203 NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000204 if (ret != 0x0) {
Nico Huber77fa67d2013-02-20 18:03:36 +0000205 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeeds[spispeed_idx].speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000206 return 1;
207 }
208 return 0;
209}
210
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
Nico Hubera4b14f72012-06-19 12:06:53 +0000310/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
311 * @chunksize length of data chunks, only 256 supported by now
312 * @start start address
313 * @len length
314 * @dedi_spi_cmd dediprog specific write command for spi bus
315 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000316 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000317static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000318 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000319{
320 int ret;
321 unsigned int i;
322 /* USB transfer size must be 512, other sizes will NOT work at all.
323 * chunksize is the real data size per USB bulk transfer. The remaining
324 * space in a USB bulk transfer must be filled with 0xff padding.
325 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000326 const unsigned int count = len / chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000327 const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000328 char usbbuf[512];
329
Nico Hubera4b14f72012-06-19 12:06:53 +0000330 /*
331 * We should change this check to
332 * chunksize > 512
333 * once we know how to handle different chunk sizes.
334 */
335 if (chunksize != 256) {
336 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
337 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
338 return 1;
339 }
340
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000341 if ((start % chunksize) || (len % chunksize)) {
342 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
343 "at flashrom@flashrom.org\n", __func__, start, len);
344 return 1;
345 }
346
347 /* No idea if the hardware can handle empty writes, so chicken out. */
348 if (!len)
349 return 0;
350 /* Command Write SPI Bulk. No idea which write command is used on the
351 * SPI side.
352 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000353 ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000, start / 0x10000,
354 (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT);
355 if (ret != sizeof(count_and_cmd)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000356 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
357 usb_strerror());
358 return 1;
359 }
360
361 for (i = 0; i < count; i++) {
362 memset(usbbuf, 0xff, sizeof(usbbuf));
363 memcpy(usbbuf, buf + i * chunksize, chunksize);
364 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
365 usbbuf, 512,
366 DEFAULT_TIMEOUT);
367 if (ret != 512) {
368 msg_perr("SPI bulk write failed, expected %i, got %i "
369 "%s!\n", 512, ret, usb_strerror());
370 return 1;
371 }
372 }
373
374 return 0;
375}
376
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000377static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000378 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000379{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000380 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000381 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000382 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
383 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000384
385 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
386
Nico Hubera4b14f72012-06-19 12:06:53 +0000387 if (chunksize != 256) {
388 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
389 "we don't know how dediprog\nhandles them.\n");
390 /* Write everything like it was residue. */
391 residue = len;
392 }
393
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000394 if (residue) {
395 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
396 start, residue);
397 /* No idea about the real limit. Maybe 12, maybe more. */
398 ret = spi_write_chunked(flash, buf, start, residue, 12);
399 if (ret) {
400 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
401 return ret;
402 }
403 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000404
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000405 /* Round down. */
406 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000407 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000408 if (ret) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000409 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000410 return ret;
411 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000412
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000413 len -= residue + bulklen;
414 if (len) {
415 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
416 start, len);
417 ret = spi_write_chunked(flash, buf + residue + bulklen,
418 start + residue + bulklen, len, 12);
419 if (ret) {
420 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
421 return ret;
422 }
423 }
424
425 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
426 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000427}
428
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000429static int dediprog_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Nico Hubera4b14f72012-06-19 12:06:53 +0000430{
431 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_PAGEWRITE);
432}
433
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000434static int dediprog_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len)
Nico Hubera4b14f72012-06-19 12:06:53 +0000435{
436 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_AAIWRITE);
437}
438
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000439static int dediprog_spi_send_command(struct flashctx *flash,
440 unsigned int writecnt,
441 unsigned int readcnt,
442 const unsigned char *writearr,
443 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000444{
445 int ret;
446
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000447 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000448 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000449 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000450 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000451 return 1;
452 }
453 /* 16 byte reads should work. */
454 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000455 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000456 return 1;
457 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000458
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000459 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
460 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
461 DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000462 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000463 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
464 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000465 return 1;
466 }
467 if (!readcnt)
468 return 0;
469 memset(readarr, 0, readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000470 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
471 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000472 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000473 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
474 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000475 return 1;
476 }
477 return 0;
478}
479
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000480static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000481{
482 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000483 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000484 char buf[0x11];
485
486 /* Command Prepare Receive Device String. */
487 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000488 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
489 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000490 /* The char casting is needed to stop gcc complaining about an always true comparison. */
491 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
492 msg_perr("Unexpected response to Command Prepare Receive Device"
493 " String!\n");
494 return 1;
495 }
496 /* Command Receive Device String. */
497 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000498 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
499 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000500 if (ret != 0x10) {
501 msg_perr("Incomplete/failed Command Receive Device String!\n");
502 return 1;
503 }
504 buf[0x10] = '\0';
505 msg_pdbg("Found a %s\n", buf);
506 if (memcmp(buf, "SF100", 0x5)) {
507 msg_perr("Device not a SF100!\n");
508 return 1;
509 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000510 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
511 msg_perr("Unexpected firmware version string!\n");
512 return 1;
513 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000514 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000515 if (fw[0] < 2 || fw[0] > 5) {
516 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
517 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000518 return 1;
519 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000520 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000521 return 0;
522}
523
524/* Command A seems to be some sort of device init. It is either followed by
525 * dediprog_check_devicestring (often) or Command A (often) or
526 * Command F (once).
527 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000528static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000529{
530 int ret;
531 char buf[0x1];
532
533 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000534 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
535 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000536 if (ret < 0) {
537 msg_perr("Command A failed (%s)!\n", usb_strerror());
538 return 1;
539 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000540 if ((ret != 0x1) || (buf[0] != 0x6f)) {
541 msg_perr("Unexpected response to Command A!\n");
542 return 1;
543 }
544 return 0;
545}
546
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000547#if 0
548/* Something.
549 * Present in eng_detect_blink.log with firmware 3.1.8
550 * Always preceded by Command Receive Device String
551 */
552static int dediprog_command_b(void)
553{
554 int ret;
555 char buf[0x3];
556
557 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000558 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
559 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000560 if (ret < 0) {
561 msg_perr("Command B failed (%s)!\n", usb_strerror());
562 return 1;
563 }
564 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
565 (buf[2] != 0xff)) {
566 msg_perr("Unexpected response to Command B!\n");
567 return 1;
568 }
569
570 return 0;
571}
572#endif
573
Stefan Taunere659d2d2013-05-03 21:58:28 +0000574/* Command Chip Select is only sent after dediprog_check_devicestring, but not after every
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000575 * invocation of dediprog_check_devicestring. It is only sent after the first
576 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
Stefan Taunere659d2d2013-05-03 21:58:28 +0000577 * Bit #1 of the value changes the chip select: 0 is target 1, 1 is target 2 and parameter target can be 1 or 2
578 * respectively. We don't know how to encode "3, Socket" and "0, reference card" yet. On SF100 the vendor
579 * software "DpCmd 6.0.4.06" selects target 2 when requesting 3 (which is unavailable on that hardware).
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000580 */
Stefan Taunere659d2d2013-05-03 21:58:28 +0000581static int dediprog_chip_select(int target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000582{
583 int ret;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000584 uint16_t value = ((target - 1) & 1) << 1;
585 msg_pdbg("Selecting target chip %i\n", target);
586 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, value, 0x0, NULL,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000587 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000588 if (ret != 0x0) {
Stefan Taunere659d2d2013-05-03 21:58:28 +0000589 msg_perr("Command Chip Select failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000590 return 1;
591 }
592 return 0;
593}
594
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000595#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000596/* Very strange. Seems to be a programmer keepalive or somesuch.
597 * Wait unsuccessfully for timeout ms to read one byte.
598 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000599 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000600 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000601static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000602{
603 int ret;
604 char buf[0x1];
605
606 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000607 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
608 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000609 /* This check is most probably wrong. Command F always causes a timeout
610 * in the logs, so we should check for timeout instead of checking for
611 * success.
612 */
613 if (ret != 0x1) {
614 msg_perr("Command F failed (%s)!\n", usb_strerror());
615 return 1;
616 }
617 return 0;
618}
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000619
620/* Start/stop blinking?
621 * Present in eng_detect_blink.log with firmware 3.1.8
622 * Preceded by Command J
623 */
624static int dediprog_command_g(void)
625{
626 int ret;
627
628 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
629 if (ret != 0x0) {
630 msg_perr("Command G failed (%s)!\n", usb_strerror());
631 return 1;
632 }
633 return 0;
634}
635
636/* Something.
637 * Present in all logs with firmware 5.1.5
638 * Always preceded by Command Receive Device String
639 * Always followed by Command Set SPI Voltage nonzero
640 */
641static int dediprog_command_h(void)
642{
643 int ret;
644
645 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
646 if (ret != 0x0) {
647 msg_perr("Command H failed (%s)!\n", usb_strerror());
648 return 1;
649 }
650 return 0;
651}
652
653/* Shutdown for firmware 5.x?
654 * Present in all logs with firmware 5.1.5
655 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
656 * Always followed by Command Set SPI Voltage 0x0000
657 */
658static int dediprog_command_i(void)
659{
660 int ret;
661
662 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
663 if (ret != 0x0) {
664 msg_perr("Command I failed (%s)!\n", usb_strerror());
665 return 1;
666 }
667 return 0;
668}
669
670/* Start/stop blinking?
671 * Present in all logs with firmware 5.1.5
672 * Always preceded by Command Receive Device String on 5.1.5
673 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
674 * Present in eng_detect_blink.log with firmware 3.1.8
675 * Preceded by Command B in eng_detect_blink.log
676 * Followed by Command G in eng_detect_blink.log
677 */
678static int dediprog_command_j(void)
679{
680 int ret;
681
682 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
683 if (ret != 0x0) {
684 msg_perr("Command J failed (%s)!\n", usb_strerror());
685 return 1;
686 }
687 return 0;
688}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000689#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000690
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000691static int parse_voltage(char *voltage)
692{
693 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000694 int i;
695 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000696
697 if (!voltage || !strlen(voltage)) {
698 msg_perr("Empty voltage= specified.\n");
699 return -1;
700 }
701 millivolt = (int)strtol(voltage, &tmp, 0);
702 voltage = tmp;
703 /* Handle "," and "." as decimal point. Everything after it is assumed
704 * to be in decimal notation.
705 */
706 if ((*voltage == '.') || (*voltage == ',')) {
707 voltage++;
708 for (i = 0; i < 3; i++) {
709 fraction *= 10;
710 /* Don't advance if the current character is invalid,
711 * but continue multiplying.
712 */
713 if ((*voltage < '0') || (*voltage > '9'))
714 continue;
715 fraction += *voltage - '0';
716 voltage++;
717 }
718 /* Throw away remaining digits. */
719 voltage += strspn(voltage, "0123456789");
720 }
721 /* The remaining string must be empty or "mV" or "V". */
722 tolower_string(voltage);
723
724 /* No unit or "V". */
725 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
726 millivolt *= 1000;
727 millivolt += fraction;
728 } else if (!strncmp(voltage, "mv", 2) ||
729 !strncmp(voltage, "milliv", 6)) {
730 /* No adjustment. fraction is discarded. */
731 } else {
732 /* Garbage at the end of the string. */
733 msg_perr("Garbage voltage= specified.\n");
734 return -1;
735 }
736 return millivolt;
737}
738
Stefan Taunere659d2d2013-05-03 21:58:28 +0000739static int dediprog_setup(long target)
Nico Huber77fa67d2013-02-20 18:03:36 +0000740{
741 /* URB 6. Command A. */
742 if (dediprog_command_a()) {
743 return 1;
744 }
745 /* URB 7. Command A. */
746 if (dediprog_command_a()) {
747 return 1;
748 }
749 /* URB 8. Command Prepare Receive Device String. */
750 /* URB 9. Command Receive Device String. */
751 if (dediprog_check_devicestring()) {
752 return 1;
753 }
Stefan Taunere659d2d2013-05-03 21:58:28 +0000754 /* URB 10. Command Chip Select */
755 if (dediprog_chip_select(target)) {
Nico Huber77fa67d2013-02-20 18:03:36 +0000756 return 1;
757 }
758 return 0;
759}
760
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000761static const struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000762 .type = SPI_CONTROLLER_DEDIPROG,
763 .max_data_read = MAX_DATA_UNSPECIFIED,
764 .max_data_write = MAX_DATA_UNSPECIFIED,
765 .command = dediprog_spi_send_command,
766 .multicommand = default_spi_send_multicommand,
767 .read = dediprog_spi_read,
768 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000769 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000770};
771
David Hendricks8bb20212011-06-14 01:35:36 +0000772static int dediprog_shutdown(void *data)
773{
774 msg_pspew("%s\n", __func__);
775
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000776#if 0
777 /* Shutdown on firmware 5.x */
778 if (dediprog_firmwareversion == 5)
779 if (dediprog_command_i())
780 return 1;
781#endif
782
David Hendricks8bb20212011-06-14 01:35:36 +0000783 /* URB 28. Command Set SPI Voltage to 0. */
784 if (dediprog_set_spi_voltage(0x0))
785 return 1;
786
787 if (usb_release_interface(dediprog_handle, 0)) {
788 msg_perr("Could not release USB interface!\n");
789 return 1;
790 }
791 if (usb_close(dediprog_handle)) {
792 msg_perr("Could not close USB device!\n");
793 return 1;
794 }
795 return 0;
796}
797
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000798/* URB numbers refer to the first log ever captured. */
799int dediprog_init(void)
800{
801 struct usb_device *dev;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000802 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +0000803 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000804 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000805 long usedevice = 0;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000806 long target = 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000807 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000808
809 msg_pspew("%s\n", __func__);
810
Nico Huber77fa67d2013-02-20 18:03:36 +0000811 spispeed = extract_programmer_param("spispeed");
812 if (spispeed) {
813 for (i = 0; spispeeds[i].name; ++i) {
814 if (!strcasecmp(spispeeds[i].name, spispeed)) {
815 spispeed_idx = i;
816 break;
817 }
818 }
819 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000820 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +0000821 free(spispeed);
822 return 1;
823 }
824 free(spispeed);
825 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000826 voltage = extract_programmer_param("voltage");
827 if (voltage) {
828 millivolt = parse_voltage(voltage);
829 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000830 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000831 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000832 msg_pinfo("Setting voltage to %i mV\n", millivolt);
833 }
834
Nathan Laredo21541a62012-12-24 22:07:36 +0000835 device = extract_programmer_param("device");
836 if (device) {
837 char *dev_suffix;
838 errno = 0;
839 usedevice = strtol(device, &dev_suffix, 10);
840 if (errno != 0 || device == dev_suffix) {
841 msg_perr("Error: Could not convert 'device'.\n");
842 free(device);
843 return 1;
844 }
845 if (usedevice < 0 || usedevice > UINT_MAX) {
846 msg_perr("Error: Value for 'device' is out of range.\n");
847 free(device);
848 return 1;
849 }
850 if (strlen(dev_suffix) > 0) {
851 msg_perr("Error: Garbage following 'device' value.\n");
852 free(device);
853 return 1;
854 }
855 msg_pinfo("Using device %li.\n", usedevice);
856 }
857 free(device);
858
Stefan Taunere659d2d2013-05-03 21:58:28 +0000859 target_str = extract_programmer_param("target");
860 if (target_str) {
861 char *target_suffix;
862 errno = 0;
863 target = strtol(target_str, &target_suffix, 10);
864 if (errno != 0 || target_str == target_suffix) {
865 msg_perr("Error: Could not convert 'target'.\n");
866 free(target_str);
867 return 1;
868 }
869 if (target < 1 || target > 2) {
870 msg_perr("Error: Value for 'target' is out of range.\n");
871 free(target_str);
872 return 1;
873 }
874 if (strlen(target_suffix) > 0) {
875 msg_perr("Error: Garbage following 'target' value.\n");
876 free(target_str);
877 return 1;
878 }
879 msg_pinfo("Using target %li.\n", target);
880 }
881 free(target_str);
882
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000883 /* Here comes the USB stuff. */
884 usb_init();
885 usb_find_busses();
886 usb_find_devices();
Nathan Laredo21541a62012-12-24 22:07:36 +0000887 dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000888 if (!dev) {
889 msg_perr("Could not find a Dediprog SF100 on USB!\n");
890 return 1;
891 }
892 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000893 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000894 dediprog_handle = usb_open(dev);
David Woodhoused2a7e872013-07-30 09:34:44 +0000895 if (!dediprog_handle) {
896 msg_perr("Could not open USB device: %s\n", usb_strerror());
897 return 1;
898 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000899 ret = usb_set_configuration(dediprog_handle, 1);
900 if (ret < 0) {
901 msg_perr("Could not set USB device configuration: %i %s\n",
902 ret, usb_strerror());
903 if (usb_close(dediprog_handle))
904 msg_perr("Could not close USB device!\n");
905 return 1;
906 }
907 ret = usb_claim_interface(dediprog_handle, 0);
908 if (ret < 0) {
909 msg_perr("Could not claim USB device interface %i: %i %s\n",
910 0, ret, usb_strerror());
911 if (usb_close(dediprog_handle))
912 msg_perr("Could not close USB device!\n");
913 return 1;
914 }
915 dediprog_endpoint = 2;
Nico Huber77fa67d2013-02-20 18:03:36 +0000916
David Hendricks8bb20212011-06-14 01:35:36 +0000917 if (register_shutdown(dediprog_shutdown, NULL))
918 return 1;
919
Stefan Reinauer915b8402011-01-28 09:00:15 +0000920 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
921
Nico Huber77fa67d2013-02-20 18:03:36 +0000922 /* Perform basic setup. */
Stefan Taunere659d2d2013-05-03 21:58:28 +0000923 if (dediprog_setup(target)) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000924 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000925 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000926 }
Nico Huber77fa67d2013-02-20 18:03:36 +0000927
928 /* After setting voltage and speed, perform setup again. */
Stefan Taunere659d2d2013-05-03 21:58:28 +0000929 if (dediprog_set_spi_voltage(0) || dediprog_set_spi_speed(spispeed_idx) || dediprog_setup(target)) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000930 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000931 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000932 }
Nico Huber77fa67d2013-02-20 18:03:36 +0000933
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000934 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000935 if (dediprog_set_spi_voltage(millivolt)) {
936 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000937 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000938 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000939
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000940 register_spi_master(&spi_master_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000941
942 /* RE leftover, leave in until the driver is complete. */
943#if 0
944 /* Execute RDID by hand if you want to test it. */
945 dediprog_do_stuff();
946#endif
947
Stefan Reinauer915b8402011-01-28 09:00:15 +0000948 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
949
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000950 return 0;
951}
952
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000953#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000954/* Leftovers from reverse engineering. Keep for documentation purposes until
955 * completely understood.
956 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000957static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000958{
959 char buf[0x4];
960 /* SPI command processing starts here. */
961
962 /* URB 12. Command Send SPI. */
963 /* URB 13. Command Receive SPI. */
964 memset(buf, 0, sizeof(buf));
965 /* JEDEC RDID */
966 msg_pdbg("Sending RDID\n");
967 buf[0] = JEDEC_RDID;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000968 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
969 (unsigned char *)buf, (unsigned char *)buf))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000970 return 1;
971 msg_pdbg("Receiving response: ");
972 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000973 /* URB 14-27 are more SPI commands. */
974 /* URB 28. Command Set SPI Voltage. */
975 if (dediprog_set_spi_voltage(0x0))
976 return 1;
977 /* URB 29-38. Command F, unsuccessful wait. */
978 if (dediprog_command_f(544))
979 return 1;
980 /* URB 39. Command Set SPI Voltage. */
981 if (dediprog_set_spi_voltage(0x10))
982 return 1;
983 /* URB 40. Command Set SPI Speed. */
984 if (dediprog_set_spi_speed(0x2))
985 return 1;
986 /* URB 41 is just URB 28. */
987 /* URB 42,44,46,48,51,53 is just URB 8. */
988 /* URB 43,45,47,49,52,54 is just URB 9. */
989 /* URB 50 is just URB 6/7. */
990 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
991 /* URB 132,134 is just URB 6/7. */
992 /* URB 133 is just URB 29-38. */
993 /* URB 135 is just URB 8. */
994 /* URB 136 is just URB 9. */
995 /* URB 137 is just URB 11. */
996
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000997 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
998 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
999 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
1000 * 512 bytes, rest is filled with 0xff.
1001 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001002
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001003 return 0;
1004}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001005#endif