blob: 851dbaeca1a6887b52fa51ee5c197d9fa6d1cee9 [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
Stefan Tauner5c316f92015-02-08 21:57:52 +000022#include <stdlib.h>
Mathias Krausedb7afc52010-11-09 23:30:43 +000023#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000024#include <string.h>
Stefan Taunere34e3e82013-01-01 00:06:51 +000025#include <limits.h>
Nathan Laredo21541a62012-12-24 22:07:36 +000026#include <errno.h>
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000027
28#if IS_WINDOWS
29#include <lusb0_usb.h>
30#else
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000031#include <usb.h>
Stefan Tauner0cbd8c22015-01-26 22:06:04 +000032#endif
33
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000034#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000035#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000036#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000037#include "spi.h"
38
Stefan Reinauer915b8402011-01-28 09:00:15 +000039#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000040#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000041static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +000042static int dediprog_firmwareversion;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000043static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000044
Nico Hubera4b14f72012-06-19 12:06:53 +000045#define DEDI_SPI_CMD_PAGEWRITE 0x1
46#define DEDI_SPI_CMD_AAIWRITE 0x4
47
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000048#if 0
49/* Might be useful for other pieces of code as well. */
50static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000051{
52 size_t i;
53
54 for (i = 0; i < len; i++)
55 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
56}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000057#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000058
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000059/* Might be useful for other USB devices as well. static for now. */
Nathan Laredo21541a62012-12-24 22:07:36 +000060/* device parameter allows user to specify one device of multiple installed */
61static 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 +000062{
63 struct usb_bus *bus;
64 struct usb_device *dev;
65
66 for (bus = usb_get_busses(); bus; bus = bus->next)
67 for (dev = bus->devices; dev; dev = dev->next)
68 if ((dev->descriptor.idVendor == vid) &&
Nathan Laredo21541a62012-12-24 22:07:36 +000069 (dev->descriptor.idProduct == pid)) {
70 if (device == 0)
71 return dev;
72 device--;
73 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000074
75 return NULL;
76}
77
78//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
79
Stefan Reinauer915b8402011-01-28 09:00:15 +000080/* Set/clear LEDs on dediprog */
81#define PASS_ON (0 << 0)
82#define PASS_OFF (1 << 0)
83#define BUSY_ON (0 << 1)
84#define BUSY_OFF (1 << 1)
85#define ERROR_ON (0 << 2)
86#define ERROR_OFF (1 << 2)
87static int current_led_status = -1;
88
89static int dediprog_set_leds(int leds)
90{
91 int ret, target_leds;
Uwe Hermann91f4afa2011-07-28 08:13:25 +000092
Stefan Reinauer915b8402011-01-28 09:00:15 +000093 if (leds < 0 || leds > 7)
94 leds = 0; // Bogus value, enable all LEDs
95
96 if (leds == current_led_status)
97 return 0;
98
99 /* Older Dediprogs with 2.x.x and 3.x.x firmware only had
100 * two LEDs, and they were reversed. So map them around if
101 * we have an old device. On those devices the LEDs map as
102 * follows:
103 * bit 2 == 0: green light is on.
104 * bit 0 == 0: red light is on.
105 */
106 if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
107 target_leds = ((leds & ERROR_OFF) >> 2) |
108 ((leds & PASS_OFF) << 2);
109 } else {
110 target_leds = leds;
111 }
112
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000113 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
114 NULL, 0x0, DEFAULT_TIMEOUT);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000115 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000116 msg_perr("Command Set LED 0x%x failed (%s)!\n",
117 leds, usb_strerror());
Stefan Reinauer915b8402011-01-28 09:00:15 +0000118 return 1;
119 }
120
121 current_led_status = leds;
122
123 return 0;
124}
125
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000126static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000127{
128 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000129 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000130
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000131 switch (millivolt) {
132 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000133 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000134 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000135 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000136 case 1800:
137 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000138 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000139 case 2500:
140 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000141 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000142 case 3500:
143 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000144 break;
145 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000146 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000147 return 1;
148 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000149 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
150 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000151
Nico Huber4099a8a2012-06-16 00:02:27 +0000152 if (voltage_selector == 0) {
153 /* Wait some time as the original driver does. */
154 programmer_delay(200 * 1000);
155 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000156 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
157 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000158 if (ret != 0x0) {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000159 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
160 voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000161 return 1;
162 }
Nico Huber4099a8a2012-06-16 00:02:27 +0000163 if (voltage_selector != 0) {
164 /* Wait some time as the original driver does. */
165 programmer_delay(200 * 1000);
166 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000167 return 0;
168}
169
Nico Huber77fa67d2013-02-20 18:03:36 +0000170struct dediprog_spispeeds {
171 const char *const name;
172 const int speed;
173};
174
175static const struct dediprog_spispeeds spispeeds[] = {
176 { "24M", 0x0 },
177 { "12M", 0x2 },
178 { "8M", 0x1 },
179 { "3M", 0x3 },
180 { "2.18M", 0x4 },
181 { "1.5M", 0x5 },
182 { "750k", 0x6 },
183 { "375k", 0x7 },
184 { NULL, 0x0 },
185};
186
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000187/* After dediprog_set_spi_speed, the original app always calls
188 * dediprog_set_spi_voltage(0) and then
189 * dediprog_check_devicestring() four times in a row.
190 * After that, dediprog_command_a() is called.
191 * This looks suspiciously like the microprocessor in the SF100 has to be
192 * restarted/reinitialized in case the speed changes.
193 */
Nico Huber77fa67d2013-02-20 18:03:36 +0000194static int dediprog_set_spi_speed(unsigned int spispeed_idx)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000195{
Patrick Georgiefe2d432013-05-23 21:47:46 +0000196 if (dediprog_firmwareversion < FIRMWARE_VERSION(5, 0, 0)) {
197 msg_pwarn("Skipping to set SPI speed because firmware is too old.\n");
198 return 0;
199 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000200
Patrick Georgiefe2d432013-05-23 21:47:46 +0000201 msg_pdbg("SPI speed is %s Hz\n", spispeeds[spispeed_idx].name);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000202
Patrick Georgiefe2d432013-05-23 21:47:46 +0000203 int ret = usb_control_msg(dediprog_handle, 0x42, 0x61, spispeeds[spispeed_idx].speed, 0xff,
204 NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000205 if (ret != 0x0) {
Nico Huber77fa67d2013-02-20 18:03:36 +0000206 msg_perr("Command Set SPI Speed 0x%x failed!\n", spispeeds[spispeed_idx].speed);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000207 return 1;
208 }
209 return 0;
210}
211
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000212/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
213 * @start start address
214 * @len length
215 * @return 0 on success, 1 on failure
216 */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000217static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000218 unsigned int start, unsigned int len)
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000219{
220 int ret;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000221 unsigned int i;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000222 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000223 const unsigned int chunksize = 0x200;
224 const unsigned int count = len / chunksize;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000225 const char count_and_chunk[] = {count & 0xff,
226 (count >> 8) & 0xff,
227 chunksize & 0xff,
228 (chunksize >> 8) & 0xff};
229
230 if ((start % chunksize) || (len % chunksize)) {
231 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
232 "at flashrom@flashrom.org\n", __func__, start, len);
233 return 1;
234 }
235
236 /* No idea if the hardware can handle empty reads, so chicken out. */
237 if (!len)
238 return 0;
239 /* Command Read SPI Bulk. No idea which read command is used on the
240 * SPI side.
241 */
242 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
243 start / 0x10000, (char *)count_and_chunk,
244 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
245 if (ret != sizeof(count_and_chunk)) {
246 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
247 usb_strerror());
248 return 1;
249 }
250
251 for (i = 0; i < count; i++) {
252 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
253 (char *)buf + i * chunksize, chunksize,
254 DEFAULT_TIMEOUT);
255 if (ret != chunksize) {
256 msg_perr("SPI bulk read %i failed, expected %i, got %i "
257 "%s!\n", i, chunksize, ret, usb_strerror());
258 return 1;
259 }
260 }
261
262 return 0;
263}
264
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000265static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000266 unsigned int start, unsigned int len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000267{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000268 int ret;
269 /* chunksize must be 512, other sizes will NOT work at all. */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000270 const unsigned int chunksize = 0x200;
271 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
272 unsigned int bulklen;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000273
Stefan Reinauer915b8402011-01-28 09:00:15 +0000274 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
275
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000276 if (residue) {
277 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
278 start, residue);
279 ret = spi_read_chunked(flash, buf, start, residue, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000280 if (ret) {
281 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000282 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000283 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000284 }
285
286 /* Round down. */
287 bulklen = (len - residue) / chunksize * chunksize;
288 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
289 bulklen);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000290 if (ret) {
291 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000292 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000293 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000294
295 len -= residue + bulklen;
296 if (len) {
297 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
298 start, len);
299 ret = spi_read_chunked(flash, buf + residue + bulklen,
300 start + residue + bulklen, len, 16);
Stefan Reinauer915b8402011-01-28 09:00:15 +0000301 if (ret) {
302 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000303 return ret;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000304 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000305 }
306
Stefan Reinauer915b8402011-01-28 09:00:15 +0000307 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000308 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000309}
310
Nico Hubera4b14f72012-06-19 12:06:53 +0000311/* Bulk write interface, will write multiple chunksize byte chunks aligned to chunksize bytes.
312 * @chunksize length of data chunks, only 256 supported by now
313 * @start start address
314 * @len length
315 * @dedi_spi_cmd dediprog specific write command for spi bus
316 * @return 0 on success, 1 on failure
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000317 */
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000318static int dediprog_spi_bulk_write(struct flashctx *flash, const uint8_t *buf, unsigned int chunksize,
Nico Hubera4b14f72012-06-19 12:06:53 +0000319 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000320{
321 int ret;
322 unsigned int i;
323 /* USB transfer size must be 512, other sizes will NOT work at all.
324 * chunksize is the real data size per USB bulk transfer. The remaining
325 * space in a USB bulk transfer must be filled with 0xff padding.
326 */
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000327 const unsigned int count = len / chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000328 const char count_and_cmd[] = {count & 0xff, (count >> 8) & 0xff, 0x00, dedi_spi_cmd};
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000329 char usbbuf[512];
330
Nico Hubera4b14f72012-06-19 12:06:53 +0000331 /*
332 * We should change this check to
333 * chunksize > 512
334 * once we know how to handle different chunk sizes.
335 */
336 if (chunksize != 256) {
337 msg_perr("%s: Chunk sizes other than 256 bytes are unsupported, chunksize=%u!\n"
338 "Please report a bug at flashrom@flashrom.org\n", __func__, chunksize);
339 return 1;
340 }
341
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000342 if ((start % chunksize) || (len % chunksize)) {
343 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
344 "at flashrom@flashrom.org\n", __func__, start, len);
345 return 1;
346 }
347
348 /* No idea if the hardware can handle empty writes, so chicken out. */
349 if (!len)
350 return 0;
351 /* Command Write SPI Bulk. No idea which write command is used on the
352 * SPI side.
353 */
Nico Hubera4b14f72012-06-19 12:06:53 +0000354 ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000, start / 0x10000,
355 (char *)count_and_cmd, sizeof(count_and_cmd), DEFAULT_TIMEOUT);
356 if (ret != sizeof(count_and_cmd)) {
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000357 msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
358 usb_strerror());
359 return 1;
360 }
361
362 for (i = 0; i < count; i++) {
363 memset(usbbuf, 0xff, sizeof(usbbuf));
364 memcpy(usbbuf, buf + i * chunksize, chunksize);
365 ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
366 usbbuf, 512,
367 DEFAULT_TIMEOUT);
368 if (ret != 512) {
369 msg_perr("SPI bulk write failed, expected %i, got %i "
370 "%s!\n", 512, ret, usb_strerror());
371 return 1;
372 }
373 }
374
375 return 0;
376}
377
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000378static int dediprog_spi_write(struct flashctx *flash, const uint8_t *buf,
Nico Hubera4b14f72012-06-19 12:06:53 +0000379 unsigned int start, unsigned int len, uint8_t dedi_spi_cmd)
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000380{
Stefan Reinauer915b8402011-01-28 09:00:15 +0000381 int ret;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000382 const unsigned int chunksize = flash->chip->page_size;
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000383 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
384 unsigned int bulklen;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000385
386 dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
387
Nico Hubera4b14f72012-06-19 12:06:53 +0000388 if (chunksize != 256) {
389 msg_pdbg("Page sizes other than 256 bytes are unsupported as "
390 "we don't know how dediprog\nhandles them.\n");
391 /* Write everything like it was residue. */
392 residue = len;
393 }
394
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000395 if (residue) {
396 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
397 start, residue);
398 /* No idea about the real limit. Maybe 12, maybe more. */
399 ret = spi_write_chunked(flash, buf, start, residue, 12);
400 if (ret) {
401 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
402 return ret;
403 }
404 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000405
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000406 /* Round down. */
407 bulklen = (len - residue) / chunksize * chunksize;
Nico Hubera4b14f72012-06-19 12:06:53 +0000408 ret = dediprog_spi_bulk_write(flash, buf + residue, chunksize, start + residue, bulklen, dedi_spi_cmd);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000409 if (ret) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000410 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000411 return ret;
412 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000413
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000414 len -= residue + bulklen;
415 if (len) {
416 msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
417 start, len);
418 ret = spi_write_chunked(flash, buf + residue + bulklen,
419 start + residue + bulklen, len, 12);
420 if (ret) {
421 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
422 return ret;
423 }
424 }
425
426 dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
427 return 0;
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000428}
429
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000430static 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 +0000431{
432 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_PAGEWRITE);
433}
434
Stefan Taunerffb0cf62014-05-25 07:47:47 +0000435static 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 +0000436{
437 return dediprog_spi_write(flash, buf, start, len, DEDI_SPI_CMD_AAIWRITE);
438}
439
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000440static int dediprog_spi_send_command(struct flashctx *flash,
441 unsigned int writecnt,
442 unsigned int readcnt,
443 const unsigned char *writearr,
444 unsigned char *readarr)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000445{
446 int ret;
447
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000448 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000449 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000450 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000451 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000452 return 1;
453 }
454 /* 16 byte reads should work. */
455 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000456 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000457 return 1;
458 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000459
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000460 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
461 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
462 DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000463 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000464 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
465 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000466 return 1;
467 }
468 if (!readcnt)
469 return 0;
470 memset(readarr, 0, readcnt);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000471 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
472 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000473 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000474 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
475 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000476 return 1;
477 }
478 return 0;
479}
480
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000481static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000482{
483 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000484 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000485 char buf[0x11];
486
487 /* Command Prepare Receive Device String. */
488 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000489 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
490 0x1, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000491 /* The char casting is needed to stop gcc complaining about an always true comparison. */
492 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
493 msg_perr("Unexpected response to Command Prepare Receive Device"
494 " String!\n");
495 return 1;
496 }
497 /* Command Receive Device String. */
498 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000499 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
500 0x10, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000501 if (ret != 0x10) {
502 msg_perr("Incomplete/failed Command Receive Device String!\n");
503 return 1;
504 }
505 buf[0x10] = '\0';
506 msg_pdbg("Found a %s\n", buf);
507 if (memcmp(buf, "SF100", 0x5)) {
508 msg_perr("Device not a SF100!\n");
509 return 1;
510 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000511 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
512 msg_perr("Unexpected firmware version string!\n");
513 return 1;
514 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000515 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000516 if (fw[0] < 2 || fw[0] > 5) {
517 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
518 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000519 return 1;
520 }
Stefan Reinauer915b8402011-01-28 09:00:15 +0000521 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000522 return 0;
523}
524
525/* Command A seems to be some sort of device init. It is either followed by
526 * dediprog_check_devicestring (often) or Command A (often) or
527 * Command F (once).
528 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000529static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000530{
531 int ret;
532 char buf[0x1];
533
534 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000535 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
536 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000537 if (ret < 0) {
538 msg_perr("Command A failed (%s)!\n", usb_strerror());
539 return 1;
540 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000541 if ((ret != 0x1) || (buf[0] != 0x6f)) {
542 msg_perr("Unexpected response to Command A!\n");
543 return 1;
544 }
545 return 0;
546}
547
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000548#if 0
549/* Something.
550 * Present in eng_detect_blink.log with firmware 3.1.8
551 * Always preceded by Command Receive Device String
552 */
553static int dediprog_command_b(void)
554{
555 int ret;
556 char buf[0x3];
557
558 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000559 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
560 0x3, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000561 if (ret < 0) {
562 msg_perr("Command B failed (%s)!\n", usb_strerror());
563 return 1;
564 }
565 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
566 (buf[2] != 0xff)) {
567 msg_perr("Unexpected response to Command B!\n");
568 return 1;
569 }
570
571 return 0;
572}
573#endif
574
Stefan Taunere659d2d2013-05-03 21:58:28 +0000575/* Command Chip Select is only sent after dediprog_check_devicestring, but not after every
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000576 * invocation of dediprog_check_devicestring. It is only sent after the first
577 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
Stefan Taunere659d2d2013-05-03 21:58:28 +0000578 * 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
579 * respectively. We don't know how to encode "3, Socket" and "0, reference card" yet. On SF100 the vendor
580 * 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 +0000581 */
Stefan Taunere659d2d2013-05-03 21:58:28 +0000582static int dediprog_chip_select(int target)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000583{
584 int ret;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000585 uint16_t value = ((target - 1) & 1) << 1;
586 msg_pdbg("Selecting target chip %i\n", target);
587 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, value, 0x0, NULL,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000588 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000589 if (ret != 0x0) {
Stefan Taunere659d2d2013-05-03 21:58:28 +0000590 msg_perr("Command Chip Select failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000591 return 1;
592 }
593 return 0;
594}
595
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000596#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000597/* Very strange. Seems to be a programmer keepalive or somesuch.
598 * Wait unsuccessfully for timeout ms to read one byte.
599 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000600 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000601 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000602static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000603{
604 int ret;
605 char buf[0x1];
606
607 memset(buf, 0, sizeof(buf));
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000608 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
609 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000610 /* This check is most probably wrong. Command F always causes a timeout
611 * in the logs, so we should check for timeout instead of checking for
612 * success.
613 */
614 if (ret != 0x1) {
615 msg_perr("Command F failed (%s)!\n", usb_strerror());
616 return 1;
617 }
618 return 0;
619}
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000620
621/* Start/stop blinking?
622 * Present in eng_detect_blink.log with firmware 3.1.8
623 * Preceded by Command J
624 */
625static int dediprog_command_g(void)
626{
627 int ret;
628
629 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
630 if (ret != 0x0) {
631 msg_perr("Command G failed (%s)!\n", usb_strerror());
632 return 1;
633 }
634 return 0;
635}
636
637/* Something.
638 * Present in all logs with firmware 5.1.5
639 * Always preceded by Command Receive Device String
640 * Always followed by Command Set SPI Voltage nonzero
641 */
642static int dediprog_command_h(void)
643{
644 int ret;
645
646 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
647 if (ret != 0x0) {
648 msg_perr("Command H failed (%s)!\n", usb_strerror());
649 return 1;
650 }
651 return 0;
652}
653
654/* Shutdown for firmware 5.x?
655 * Present in all logs with firmware 5.1.5
656 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
657 * Always followed by Command Set SPI Voltage 0x0000
658 */
659static int dediprog_command_i(void)
660{
661 int ret;
662
663 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
664 if (ret != 0x0) {
665 msg_perr("Command I failed (%s)!\n", usb_strerror());
666 return 1;
667 }
668 return 0;
669}
670
671/* Start/stop blinking?
672 * Present in all logs with firmware 5.1.5
673 * Always preceded by Command Receive Device String on 5.1.5
674 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
675 * Present in eng_detect_blink.log with firmware 3.1.8
676 * Preceded by Command B in eng_detect_blink.log
677 * Followed by Command G in eng_detect_blink.log
678 */
679static int dediprog_command_j(void)
680{
681 int ret;
682
683 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
684 if (ret != 0x0) {
685 msg_perr("Command J failed (%s)!\n", usb_strerror());
686 return 1;
687 }
688 return 0;
689}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000690#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000691
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000692static int parse_voltage(char *voltage)
693{
694 char *tmp = NULL;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000695 int i;
696 int millivolt = 0, fraction = 0;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000697
698 if (!voltage || !strlen(voltage)) {
699 msg_perr("Empty voltage= specified.\n");
700 return -1;
701 }
702 millivolt = (int)strtol(voltage, &tmp, 0);
703 voltage = tmp;
704 /* Handle "," and "." as decimal point. Everything after it is assumed
705 * to be in decimal notation.
706 */
707 if ((*voltage == '.') || (*voltage == ',')) {
708 voltage++;
709 for (i = 0; i < 3; i++) {
710 fraction *= 10;
711 /* Don't advance if the current character is invalid,
712 * but continue multiplying.
713 */
714 if ((*voltage < '0') || (*voltage > '9'))
715 continue;
716 fraction += *voltage - '0';
717 voltage++;
718 }
719 /* Throw away remaining digits. */
720 voltage += strspn(voltage, "0123456789");
721 }
722 /* The remaining string must be empty or "mV" or "V". */
723 tolower_string(voltage);
724
725 /* No unit or "V". */
726 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
727 millivolt *= 1000;
728 millivolt += fraction;
729 } else if (!strncmp(voltage, "mv", 2) ||
730 !strncmp(voltage, "milliv", 6)) {
731 /* No adjustment. fraction is discarded. */
732 } else {
733 /* Garbage at the end of the string. */
734 msg_perr("Garbage voltage= specified.\n");
735 return -1;
736 }
737 return millivolt;
738}
739
Stefan Taunere659d2d2013-05-03 21:58:28 +0000740static int dediprog_setup(long target)
Nico Huber77fa67d2013-02-20 18:03:36 +0000741{
742 /* URB 6. Command A. */
743 if (dediprog_command_a()) {
744 return 1;
745 }
746 /* URB 7. Command A. */
747 if (dediprog_command_a()) {
748 return 1;
749 }
750 /* URB 8. Command Prepare Receive Device String. */
751 /* URB 9. Command Receive Device String. */
752 if (dediprog_check_devicestring()) {
753 return 1;
754 }
Stefan Taunere659d2d2013-05-03 21:58:28 +0000755 /* URB 10. Command Chip Select */
756 if (dediprog_chip_select(target)) {
Nico Huber77fa67d2013-02-20 18:03:36 +0000757 return 1;
758 }
759 return 0;
760}
761
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000762static const struct spi_master spi_master_dediprog = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000763 .type = SPI_CONTROLLER_DEDIPROG,
764 .max_data_read = MAX_DATA_UNSPECIFIED,
765 .max_data_write = MAX_DATA_UNSPECIFIED,
766 .command = dediprog_spi_send_command,
767 .multicommand = default_spi_send_multicommand,
768 .read = dediprog_spi_read,
769 .write_256 = dediprog_spi_write_256,
Nico Hubera4b14f72012-06-19 12:06:53 +0000770 .write_aai = dediprog_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000771};
772
David Hendricks8bb20212011-06-14 01:35:36 +0000773static int dediprog_shutdown(void *data)
774{
775 msg_pspew("%s\n", __func__);
776
Carl-Daniel Hailfinger64204b52011-12-20 01:54:19 +0000777#if 0
778 /* Shutdown on firmware 5.x */
779 if (dediprog_firmwareversion == 5)
780 if (dediprog_command_i())
781 return 1;
782#endif
783
David Hendricks8bb20212011-06-14 01:35:36 +0000784 /* URB 28. Command Set SPI Voltage to 0. */
785 if (dediprog_set_spi_voltage(0x0))
786 return 1;
787
788 if (usb_release_interface(dediprog_handle, 0)) {
789 msg_perr("Could not release USB interface!\n");
790 return 1;
791 }
792 if (usb_close(dediprog_handle)) {
793 msg_perr("Could not close USB device!\n");
794 return 1;
795 }
796 return 0;
797}
798
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000799/* URB numbers refer to the first log ever captured. */
800int dediprog_init(void)
801{
802 struct usb_device *dev;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000803 char *voltage, *device, *spispeed, *target_str;
Patrick Georgiefe2d432013-05-23 21:47:46 +0000804 int spispeed_idx = 1;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000805 int millivolt = 3500;
Nathan Laredo21541a62012-12-24 22:07:36 +0000806 long usedevice = 0;
Stefan Taunere659d2d2013-05-03 21:58:28 +0000807 long target = 1;
Nico Huber77fa67d2013-02-20 18:03:36 +0000808 int i, ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000809
810 msg_pspew("%s\n", __func__);
811
Nico Huber77fa67d2013-02-20 18:03:36 +0000812 spispeed = extract_programmer_param("spispeed");
813 if (spispeed) {
814 for (i = 0; spispeeds[i].name; ++i) {
815 if (!strcasecmp(spispeeds[i].name, spispeed)) {
816 spispeed_idx = i;
817 break;
818 }
819 }
820 if (!spispeeds[i].name) {
Patrick Georgiefe2d432013-05-23 21:47:46 +0000821 msg_perr("Error: Invalid spispeed value: '%s'.\n", spispeed);
Nico Huber77fa67d2013-02-20 18:03:36 +0000822 free(spispeed);
823 return 1;
824 }
825 free(spispeed);
826 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000827 voltage = extract_programmer_param("voltage");
828 if (voltage) {
829 millivolt = parse_voltage(voltage);
830 free(voltage);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000831 if (millivolt < 0)
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000832 return 1;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000833 msg_pinfo("Setting voltage to %i mV\n", millivolt);
834 }
835
Nathan Laredo21541a62012-12-24 22:07:36 +0000836 device = extract_programmer_param("device");
837 if (device) {
838 char *dev_suffix;
839 errno = 0;
840 usedevice = strtol(device, &dev_suffix, 10);
841 if (errno != 0 || device == dev_suffix) {
842 msg_perr("Error: Could not convert 'device'.\n");
843 free(device);
844 return 1;
845 }
846 if (usedevice < 0 || usedevice > UINT_MAX) {
847 msg_perr("Error: Value for 'device' is out of range.\n");
848 free(device);
849 return 1;
850 }
851 if (strlen(dev_suffix) > 0) {
852 msg_perr("Error: Garbage following 'device' value.\n");
853 free(device);
854 return 1;
855 }
856 msg_pinfo("Using device %li.\n", usedevice);
857 }
858 free(device);
859
Stefan Taunere659d2d2013-05-03 21:58:28 +0000860 target_str = extract_programmer_param("target");
861 if (target_str) {
862 char *target_suffix;
863 errno = 0;
864 target = strtol(target_str, &target_suffix, 10);
865 if (errno != 0 || target_str == target_suffix) {
866 msg_perr("Error: Could not convert 'target'.\n");
867 free(target_str);
868 return 1;
869 }
870 if (target < 1 || target > 2) {
871 msg_perr("Error: Value for 'target' is out of range.\n");
872 free(target_str);
873 return 1;
874 }
875 if (strlen(target_suffix) > 0) {
876 msg_perr("Error: Garbage following 'target' value.\n");
877 free(target_str);
878 return 1;
879 }
880 msg_pinfo("Using target %li.\n", target);
881 }
882 free(target_str);
883
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000884 /* Here comes the USB stuff. */
885 usb_init();
886 usb_find_busses();
887 usb_find_devices();
Nathan Laredo21541a62012-12-24 22:07:36 +0000888 dev = get_device_by_vid_pid(0x0483, 0xdada, (unsigned int) usedevice);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000889 if (!dev) {
890 msg_perr("Could not find a Dediprog SF100 on USB!\n");
891 return 1;
892 }
893 msg_pdbg("Found USB device (%04x:%04x).\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000894 dev->descriptor.idVendor, dev->descriptor.idProduct);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000895 dediprog_handle = usb_open(dev);
David Woodhoused2a7e872013-07-30 09:34:44 +0000896 if (!dediprog_handle) {
897 msg_perr("Could not open USB device: %s\n", usb_strerror());
898 return 1;
899 }
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000900 ret = usb_set_configuration(dediprog_handle, 1);
901 if (ret < 0) {
902 msg_perr("Could not set USB device configuration: %i %s\n",
903 ret, usb_strerror());
904 if (usb_close(dediprog_handle))
905 msg_perr("Could not close USB device!\n");
906 return 1;
907 }
908 ret = usb_claim_interface(dediprog_handle, 0);
909 if (ret < 0) {
910 msg_perr("Could not claim USB device interface %i: %i %s\n",
911 0, ret, usb_strerror());
912 if (usb_close(dediprog_handle))
913 msg_perr("Could not close USB device!\n");
914 return 1;
915 }
916 dediprog_endpoint = 2;
Nico Huber77fa67d2013-02-20 18:03:36 +0000917
David Hendricks8bb20212011-06-14 01:35:36 +0000918 if (register_shutdown(dediprog_shutdown, NULL))
919 return 1;
920
Stefan Reinauer915b8402011-01-28 09:00:15 +0000921 dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
922
Nico Huber77fa67d2013-02-20 18:03:36 +0000923 /* Perform basic setup. */
Stefan Taunere659d2d2013-05-03 21:58:28 +0000924 if (dediprog_setup(target)) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000925 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000926 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000927 }
Nico Huber77fa67d2013-02-20 18:03:36 +0000928
929 /* After setting voltage and speed, perform setup again. */
Stefan Taunere659d2d2013-05-03 21:58:28 +0000930 if (dediprog_set_spi_voltage(0) || dediprog_set_spi_speed(spispeed_idx) || dediprog_setup(target)) {
Stefan Reinauer915b8402011-01-28 09:00:15 +0000931 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000932 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000933 }
Nico Huber77fa67d2013-02-20 18:03:36 +0000934
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000935 /* URB 11. Command Set SPI Voltage. */
Stefan Reinauer915b8402011-01-28 09:00:15 +0000936 if (dediprog_set_spi_voltage(millivolt)) {
937 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000938 return 1;
Stefan Reinauer915b8402011-01-28 09:00:15 +0000939 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000940
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000941 register_spi_master(&spi_master_dediprog);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000942
943 /* RE leftover, leave in until the driver is complete. */
944#if 0
945 /* Execute RDID by hand if you want to test it. */
946 dediprog_do_stuff();
947#endif
948
Stefan Reinauer915b8402011-01-28 09:00:15 +0000949 dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
950
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000951 return 0;
952}
953
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000954#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000955/* Leftovers from reverse engineering. Keep for documentation purposes until
956 * completely understood.
957 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000958static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000959{
960 char buf[0x4];
961 /* SPI command processing starts here. */
962
963 /* URB 12. Command Send SPI. */
964 /* URB 13. Command Receive SPI. */
965 memset(buf, 0, sizeof(buf));
966 /* JEDEC RDID */
967 msg_pdbg("Sending RDID\n");
968 buf[0] = JEDEC_RDID;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000969 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
970 (unsigned char *)buf, (unsigned char *)buf))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000971 return 1;
972 msg_pdbg("Receiving response: ");
973 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000974 /* URB 14-27 are more SPI commands. */
975 /* URB 28. Command Set SPI Voltage. */
976 if (dediprog_set_spi_voltage(0x0))
977 return 1;
978 /* URB 29-38. Command F, unsuccessful wait. */
979 if (dediprog_command_f(544))
980 return 1;
981 /* URB 39. Command Set SPI Voltage. */
982 if (dediprog_set_spi_voltage(0x10))
983 return 1;
984 /* URB 40. Command Set SPI Speed. */
985 if (dediprog_set_spi_speed(0x2))
986 return 1;
987 /* URB 41 is just URB 28. */
988 /* URB 42,44,46,48,51,53 is just URB 8. */
989 /* URB 43,45,47,49,52,54 is just URB 9. */
990 /* URB 50 is just URB 6/7. */
991 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
992 /* URB 132,134 is just URB 6/7. */
993 /* URB 133 is just URB 29-38. */
994 /* URB 135 is just URB 8. */
995 /* URB 136 is just URB 9. */
996 /* URB 137 is just URB 11. */
997
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000998 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
999 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
1000 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
1001 * 512 bytes, rest is filled with 0xff.
1002 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001003
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001004 return 0;
1005}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001006#endif