blob: b2e60b6a0c13a1e353b7bf7d6ed6127e725b87f2 [file] [log] [blame]
hailfingerdfb32a02010-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
hailfinger7a009082010-11-09 23:30:43 +000020#include <stdio.h>
hailfingerdfb32a02010-01-19 11:15:48 +000021#include <string.h>
hailfingerdfb32a02010-01-19 11:15:48 +000022#include <usb.h>
23#include "flash.h"
snelson8913d082010-02-26 05:48:29 +000024#include "chipdrivers.h"
hailfinger428f6852010-07-27 22:41:39 +000025#include "programmer.h"
hailfingerdfb32a02010-01-19 11:15:48 +000026#include "spi.h"
27
stepan4c06de72011-01-28 09:00:15 +000028#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
hailfingerdfb32a02010-01-19 11:15:48 +000029#define DEFAULT_TIMEOUT 3000
hailfinger1ff33dc2010-07-03 11:02:10 +000030static usb_dev_handle *dediprog_handle;
stepanff582cc2011-01-20 21:05:15 +000031static int dediprog_firmwareversion;
hailfingerfb6287d2010-11-16 21:25:29 +000032static int dediprog_endpoint;
hailfingerdfb32a02010-01-19 11:15:48 +000033
Simon Glasse53cc1d2015-01-08 05:48:51 -070034/* Set/clear LEDs on dediprog */
35enum {
36 LED_PASS = 1 << 0,
37 LED_BUSY = 1 << 1,
38 LED_ERROR = 1 << 2,
39 LED_ALL = 7,
40};
41
42static int current_led_status = -1;
43
hailfinger1ff33dc2010-07-03 11:02:10 +000044#if 0
45/* Might be useful for other pieces of code as well. */
46static void print_hex(void *buf, size_t len)
hailfingerdfb32a02010-01-19 11:15:48 +000047{
48 size_t i;
49
50 for (i = 0; i < len; i++)
51 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
52}
hailfinger1ff33dc2010-07-03 11:02:10 +000053#endif
hailfingerdfb32a02010-01-19 11:15:48 +000054
hailfinger1ff33dc2010-07-03 11:02:10 +000055/* Might be useful for other USB devices as well. static for now. */
56static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
hailfingerdfb32a02010-01-19 11:15:48 +000057{
58 struct usb_bus *bus;
59 struct usb_device *dev;
60
61 for (bus = usb_get_busses(); bus; bus = bus->next)
62 for (dev = bus->devices; dev; dev = dev->next)
63 if ((dev->descriptor.idVendor == vid) &&
64 (dev->descriptor.idProduct == pid))
65 return dev;
66
67 return NULL;
68}
69
70//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
71
stepan4c06de72011-01-28 09:00:15 +000072static int dediprog_set_leds(int leds)
73{
74 int ret, target_leds;
uwe8d342eb2011-07-28 08:13:25 +000075
Simon Glasse53cc1d2015-01-08 05:48:51 -070076 if (leds < 0 || leds > LED_ALL)
77 leds = LED_ALL;
stepan4c06de72011-01-28 09:00:15 +000078 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)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -070089 target_leds = ((leds & LED_ERROR) >> 2) |
90 ((leds & LED_PASS) << 2);
stepan4c06de72011-01-28 09:00:15 +000091 } else {
92 target_leds = leds;
93 }
94
Simon Glasse53cc1d2015-01-08 05:48:51 -070095 target_leds ^= 7;
uwe8d342eb2011-07-28 08:13:25 +000096 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
97 NULL, 0x0, DEFAULT_TIMEOUT);
stepan4c06de72011-01-28 09:00:15 +000098 if (ret != 0x0) {
uwe8d342eb2011-07-28 08:13:25 +000099 msg_perr("Command Set LED 0x%x failed (%s)!\n",
100 leds, usb_strerror());
stepan4c06de72011-01-28 09:00:15 +0000101 return 1;
102 }
103
104 current_led_status = leds;
105
106 return 0;
107}
108
hailfingerf76cc322010-11-09 22:00:31 +0000109static int dediprog_set_spi_voltage(int millivolt)
hailfingerdfb32a02010-01-19 11:15:48 +0000110{
111 int ret;
hailfingerf76cc322010-11-09 22:00:31 +0000112 uint16_t voltage_selector;
hailfingerdfb32a02010-01-19 11:15:48 +0000113
hailfingerf76cc322010-11-09 22:00:31 +0000114 switch (millivolt) {
115 case 0:
hailfingerdfb32a02010-01-19 11:15:48 +0000116 /* Admittedly this one is an assumption. */
hailfingerf76cc322010-11-09 22:00:31 +0000117 voltage_selector = 0x0;
hailfingerdfb32a02010-01-19 11:15:48 +0000118 break;
hailfingerf76cc322010-11-09 22:00:31 +0000119 case 1800:
120 voltage_selector = 0x12;
hailfingerdfb32a02010-01-19 11:15:48 +0000121 break;
hailfingerf76cc322010-11-09 22:00:31 +0000122 case 2500:
123 voltage_selector = 0x11;
hailfingerdfb32a02010-01-19 11:15:48 +0000124 break;
hailfingerf76cc322010-11-09 22:00:31 +0000125 case 3500:
126 voltage_selector = 0x10;
hailfingerdfb32a02010-01-19 11:15:48 +0000127 break;
128 default:
hailfingerf76cc322010-11-09 22:00:31 +0000129 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
hailfingerdfb32a02010-01-19 11:15:48 +0000130 return 1;
131 }
hailfingerf76cc322010-11-09 22:00:31 +0000132 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
133 millivolt % 1000);
hailfingerdfb32a02010-01-19 11:15:48 +0000134
uwe8d342eb2011-07-28 08:13:25 +0000135 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
136 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000137 if (ret != 0x0) {
uwe8d342eb2011-07-28 08:13:25 +0000138 msg_perr("Command Set SPI Voltage 0x%x failed!\n",
139 voltage_selector);
hailfingerdfb32a02010-01-19 11:15:48 +0000140 return 1;
141 }
142 return 0;
143}
144
hailfinger1ff33dc2010-07-03 11:02:10 +0000145#if 0
hailfingerdfb32a02010-01-19 11:15:48 +0000146/* After dediprog_set_spi_speed, the original app always calls
147 * dediprog_set_spi_voltage(0) and then
148 * dediprog_check_devicestring() four times in a row.
149 * After that, dediprog_command_a() is called.
150 * This looks suspiciously like the microprocessor in the SF100 has to be
151 * restarted/reinitialized in case the speed changes.
152 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000153static int dediprog_set_spi_speed(uint16_t speed)
hailfingerdfb32a02010-01-19 11:15:48 +0000154{
155 int ret;
156 unsigned int khz;
157
158 /* Case 1 and 2 are in weird order. Probably an organically "grown"
159 * interface.
160 * Base frequency is 24000 kHz, divisors are (in order)
161 * 1, 3, 2, 8, 11, 16, 32, 64.
162 */
163 switch (speed) {
164 case 0x0:
165 khz = 24000;
166 break;
167 case 0x1:
168 khz = 8000;
169 break;
170 case 0x2:
171 khz = 12000;
172 break;
173 case 0x3:
174 khz = 3000;
175 break;
176 case 0x4:
177 khz = 2180;
178 break;
179 case 0x5:
180 khz = 1500;
181 break;
182 case 0x6:
183 khz = 750;
184 break;
185 case 0x7:
186 khz = 375;
187 break;
188 default:
189 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
190 return 1;
191 }
192 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
193
uwe8d342eb2011-07-28 08:13:25 +0000194 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
195 0x0, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000196 if (ret != 0x0) {
197 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
198 return 1;
199 }
200 return 0;
201}
hailfinger1ff33dc2010-07-03 11:02:10 +0000202#endif
hailfingerdfb32a02010-01-19 11:15:48 +0000203
hailfingerfb6287d2010-11-16 21:25:29 +0000204/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
205 * @start start address
206 * @len length
207 * @return 0 on success, 1 on failure
208 */
209static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000210 unsigned int start, unsigned int len)
hailfingerfb6287d2010-11-16 21:25:29 +0000211{
212 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +0000213 unsigned int i;
hailfingerfb6287d2010-11-16 21:25:29 +0000214 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000215 const unsigned int chunksize = 0x200;
216 const unsigned int count = len / chunksize;
hailfingerfb6287d2010-11-16 21:25:29 +0000217 const char count_and_chunk[] = {count & 0xff,
218 (count >> 8) & 0xff,
219 chunksize & 0xff,
220 (chunksize >> 8) & 0xff};
221
222 if ((start % chunksize) || (len % chunksize)) {
223 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
224 "at flashrom@flashrom.org\n", __func__, start, len);
225 return 1;
226 }
227
228 /* No idea if the hardware can handle empty reads, so chicken out. */
229 if (!len)
230 return 0;
231 /* Command Read SPI Bulk. No idea which read command is used on the
232 * SPI side.
233 */
234 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
235 start / 0x10000, (char *)count_and_chunk,
236 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
237 if (ret != sizeof(count_and_chunk)) {
238 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
239 usb_strerror());
240 return 1;
241 }
242
243 for (i = 0; i < count; i++) {
244 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
245 (char *)buf + i * chunksize, chunksize,
246 DEFAULT_TIMEOUT);
247 if (ret != chunksize) {
248 msg_perr("SPI bulk read %i failed, expected %i, got %i "
249 "%s!\n", i, chunksize, ret, usb_strerror());
250 return 1;
251 }
252 }
253
254 return 0;
255}
256
stefanctc5eb8a92011-11-23 09:13:48 +0000257static int dediprog_spi_read(struct flashchip *flash, uint8_t *buf,
258 unsigned int start, unsigned int len)
hailfingerdfb32a02010-01-19 11:15:48 +0000259{
hailfingerfb6287d2010-11-16 21:25:29 +0000260 int ret;
261 /* chunksize must be 512, other sizes will NOT work at all. */
stefanctc5eb8a92011-11-23 09:13:48 +0000262 const unsigned int chunksize = 0x200;
263 unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
264 unsigned int bulklen;
hailfingerfb6287d2010-11-16 21:25:29 +0000265
Simon Glasse53cc1d2015-01-08 05:48:51 -0700266 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000267
hailfingerfb6287d2010-11-16 21:25:29 +0000268 if (residue) {
269 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
270 start, residue);
271 ret = spi_read_chunked(flash, buf, start, residue, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700272 if (ret)
273 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000274 }
275
276 /* Round down. */
277 bulklen = (len - residue) / chunksize * chunksize;
278 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
279 bulklen);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700280 if (ret)
281 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000282
283 len -= residue + bulklen;
284 if (len) {
285 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
286 start, len);
287 ret = spi_read_chunked(flash, buf + residue + bulklen,
288 start + residue + bulklen, len, 16);
Simon Glasse53cc1d2015-01-08 05:48:51 -0700289 if (ret)
290 goto err;
hailfingerfb6287d2010-11-16 21:25:29 +0000291 }
292
Simon Glasse53cc1d2015-01-08 05:48:51 -0700293 dediprog_set_leds(LED_PASS);
hailfingerfb6287d2010-11-16 21:25:29 +0000294 return 0;
Simon Glasse53cc1d2015-01-08 05:48:51 -0700295err:
296 dediprog_set_leds(LED_ERROR);
297 return ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000298}
299
uwe8d342eb2011-07-28 08:13:25 +0000300static int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf,
stefanctc5eb8a92011-11-23 09:13:48 +0000301 unsigned int start, unsigned int len)
hailfinger556e9c32010-11-23 21:28:16 +0000302{
stepan4c06de72011-01-28 09:00:15 +0000303 int ret;
304
Simon Glasse53cc1d2015-01-08 05:48:51 -0700305 dediprog_set_leds(LED_BUSY);
stepan4c06de72011-01-28 09:00:15 +0000306
hailfinger556e9c32010-11-23 21:28:16 +0000307 /* No idea about the real limit. Maybe 12, maybe more, maybe less. */
stepan4c06de72011-01-28 09:00:15 +0000308 ret = spi_write_chunked(flash, buf, start, len, 12);
309
310 if (ret)
Simon Glasse53cc1d2015-01-08 05:48:51 -0700311 dediprog_set_leds(LED_ERROR);
stepan4c06de72011-01-28 09:00:15 +0000312 else
Simon Glasse53cc1d2015-01-08 05:48:51 -0700313 dediprog_set_leds(LED_PASS);
stepan4c06de72011-01-28 09:00:15 +0000314
315 return ret;
hailfinger556e9c32010-11-23 21:28:16 +0000316}
317
mkarcherd264e9e2011-05-11 17:07:07 +0000318static int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerdfb32a02010-01-19 11:15:48 +0000319 const unsigned char *writearr, unsigned char *readarr)
320{
321 int ret;
322
hailfingeraf389cc2010-01-22 02:53:30 +0000323 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
hailfingerdfb32a02010-01-19 11:15:48 +0000324 /* Paranoid, but I don't want to be blamed if anything explodes. */
hailfinger556e9c32010-11-23 21:28:16 +0000325 if (writecnt > 16) {
hailfingerdfb32a02010-01-19 11:15:48 +0000326 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
hailfingeraf389cc2010-01-22 02:53:30 +0000327 return 1;
328 }
329 /* 16 byte reads should work. */
330 if (readcnt > 16) {
hailfingerdfb32a02010-01-19 11:15:48 +0000331 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
hailfingeraf389cc2010-01-22 02:53:30 +0000332 return 1;
333 }
hailfingerdfb32a02010-01-19 11:15:48 +0000334
uwe8d342eb2011-07-28 08:13:25 +0000335 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
336 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
337 DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000338 if (ret != writecnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000339 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
340 writecnt, ret, usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000341 return 1;
342 }
343 if (!readcnt)
344 return 0;
345 memset(readarr, 0, readcnt);
uwe8d342eb2011-07-28 08:13:25 +0000346 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
347 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000348 if (ret != readcnt) {
hailfingeraf389cc2010-01-22 02:53:30 +0000349 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
350 readcnt, ret, usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000351 return 1;
352 }
353 return 0;
354}
355
hailfinger1ff33dc2010-07-03 11:02:10 +0000356static int dediprog_check_devicestring(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000357{
358 int ret;
hailfinger7a009082010-11-09 23:30:43 +0000359 int fw[3];
hailfingerdfb32a02010-01-19 11:15:48 +0000360 char buf[0x11];
361
362 /* Command Prepare Receive Device String. */
363 memset(buf, 0, sizeof(buf));
uwe8d342eb2011-07-28 08:13:25 +0000364 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
365 0x1, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000366 /* The char casting is needed to stop gcc complaining about an always true comparison. */
367 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
368 msg_perr("Unexpected response to Command Prepare Receive Device"
369 " String!\n");
370 return 1;
371 }
372 /* Command Receive Device String. */
373 memset(buf, 0, sizeof(buf));
uwe8d342eb2011-07-28 08:13:25 +0000374 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
375 0x10, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000376 if (ret != 0x10) {
377 msg_perr("Incomplete/failed Command Receive Device String!\n");
378 return 1;
379 }
380 buf[0x10] = '\0';
381 msg_pdbg("Found a %s\n", buf);
382 if (memcmp(buf, "SF100", 0x5)) {
383 msg_perr("Device not a SF100!\n");
384 return 1;
385 }
hailfinger7a009082010-11-09 23:30:43 +0000386 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
387 msg_perr("Unexpected firmware version string!\n");
388 return 1;
389 }
hailfingerdfb32a02010-01-19 11:15:48 +0000390 /* Only these versions were tested. */
hailfinger7a009082010-11-09 23:30:43 +0000391 if (fw[0] < 2 || fw[0] > 5) {
392 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
393 fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000394 return 1;
395 }
stepan4c06de72011-01-28 09:00:15 +0000396 dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
hailfingerdfb32a02010-01-19 11:15:48 +0000397 return 0;
398}
399
400/* Command A seems to be some sort of device init. It is either followed by
401 * dediprog_check_devicestring (often) or Command A (often) or
402 * Command F (once).
403 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000404static int dediprog_command_a(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000405{
406 int ret;
407 char buf[0x1];
408
409 memset(buf, 0, sizeof(buf));
uwe8d342eb2011-07-28 08:13:25 +0000410 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
411 0x1, DEFAULT_TIMEOUT);
hailfinger7a009082010-11-09 23:30:43 +0000412 if (ret < 0) {
413 msg_perr("Command A failed (%s)!\n", usb_strerror());
414 return 1;
415 }
hailfingerdfb32a02010-01-19 11:15:48 +0000416 if ((ret != 0x1) || (buf[0] != 0x6f)) {
417 msg_perr("Unexpected response to Command A!\n");
418 return 1;
419 }
420 return 0;
421}
422
stepanff582cc2011-01-20 21:05:15 +0000423#if 0
424/* Something.
425 * Present in eng_detect_blink.log with firmware 3.1.8
426 * Always preceded by Command Receive Device String
427 */
428static int dediprog_command_b(void)
429{
430 int ret;
431 char buf[0x3];
432
433 memset(buf, 0, sizeof(buf));
uwe8d342eb2011-07-28 08:13:25 +0000434 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
435 0x3, DEFAULT_TIMEOUT);
stepanff582cc2011-01-20 21:05:15 +0000436 if (ret < 0) {
437 msg_perr("Command B failed (%s)!\n", usb_strerror());
438 return 1;
439 }
440 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
441 (buf[2] != 0xff)) {
442 msg_perr("Unexpected response to Command B!\n");
443 return 1;
444 }
445
446 return 0;
447}
448#endif
449
hailfingerdfb32a02010-01-19 11:15:48 +0000450/* Command C is only sent after dediprog_check_devicestring, but not after every
451 * invocation of dediprog_check_devicestring. It is only sent after the first
452 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
453 * I'm tempted to call this one start_SPI_engine or finish_init.
454 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000455static int dediprog_command_c(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000456{
457 int ret;
458
uwe8d342eb2011-07-28 08:13:25 +0000459 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
460 0x0, DEFAULT_TIMEOUT);
hailfingerdfb32a02010-01-19 11:15:48 +0000461 if (ret != 0x0) {
stepanff582cc2011-01-20 21:05:15 +0000462 msg_perr("Command C failed (%s)!\n", usb_strerror());
hailfingerdfb32a02010-01-19 11:15:48 +0000463 return 1;
464 }
465 return 0;
466}
467
hailfinger1ff33dc2010-07-03 11:02:10 +0000468#if 0
hailfingerdfb32a02010-01-19 11:15:48 +0000469/* Very strange. Seems to be a programmer keepalive or somesuch.
470 * Wait unsuccessfully for timeout ms to read one byte.
471 * Is usually called after setting voltage to 0.
stepanff582cc2011-01-20 21:05:15 +0000472 * Present in all logs with Firmware 2.1.1 and 3.1.8
hailfingerdfb32a02010-01-19 11:15:48 +0000473 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000474static int dediprog_command_f(int timeout)
hailfingerdfb32a02010-01-19 11:15:48 +0000475{
476 int ret;
477 char buf[0x1];
478
479 memset(buf, 0, sizeof(buf));
uwe8d342eb2011-07-28 08:13:25 +0000480 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
481 0x1, timeout);
stepanff582cc2011-01-20 21:05:15 +0000482 /* This check is most probably wrong. Command F always causes a timeout
483 * in the logs, so we should check for timeout instead of checking for
484 * success.
485 */
486 if (ret != 0x1) {
487 msg_perr("Command F failed (%s)!\n", usb_strerror());
488 return 1;
489 }
490 return 0;
491}
hailfinger1ff33dc2010-07-03 11:02:10 +0000492#endif
hailfingerdfb32a02010-01-19 11:15:48 +0000493
hailfingerf76cc322010-11-09 22:00:31 +0000494static int parse_voltage(char *voltage)
495{
496 char *tmp = NULL;
hailfingerb91c08c2011-08-15 19:54:20 +0000497 int i;
498 int millivolt = 0, fraction = 0;
hailfingerf76cc322010-11-09 22:00:31 +0000499
500 if (!voltage || !strlen(voltage)) {
501 msg_perr("Empty voltage= specified.\n");
502 return -1;
503 }
504 millivolt = (int)strtol(voltage, &tmp, 0);
505 voltage = tmp;
506 /* Handle "," and "." as decimal point. Everything after it is assumed
507 * to be in decimal notation.
508 */
509 if ((*voltage == '.') || (*voltage == ',')) {
510 voltage++;
511 for (i = 0; i < 3; i++) {
512 fraction *= 10;
513 /* Don't advance if the current character is invalid,
514 * but continue multiplying.
515 */
516 if ((*voltage < '0') || (*voltage > '9'))
517 continue;
518 fraction += *voltage - '0';
519 voltage++;
520 }
521 /* Throw away remaining digits. */
522 voltage += strspn(voltage, "0123456789");
523 }
524 /* The remaining string must be empty or "mV" or "V". */
525 tolower_string(voltage);
526
527 /* No unit or "V". */
528 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
529 millivolt *= 1000;
530 millivolt += fraction;
531 } else if (!strncmp(voltage, "mv", 2) ||
532 !strncmp(voltage, "milliv", 6)) {
533 /* No adjustment. fraction is discarded. */
534 } else {
535 /* Garbage at the end of the string. */
536 msg_perr("Garbage voltage= specified.\n");
537 return -1;
538 }
539 return millivolt;
540}
541
mkarcherd264e9e2011-05-11 17:07:07 +0000542static const struct spi_programmer spi_programmer_dediprog = {
uwe8d342eb2011-07-28 08:13:25 +0000543 .type = SPI_CONTROLLER_DEDIPROG,
544 .max_data_read = MAX_DATA_UNSPECIFIED,
545 .max_data_write = MAX_DATA_UNSPECIFIED,
546 .command = dediprog_spi_send_command,
547 .multicommand = default_spi_send_multicommand,
548 .read = dediprog_spi_read,
549 .write_256 = dediprog_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000550};
551
dhendrix0ffc2eb2011-06-14 01:35:36 +0000552static int dediprog_shutdown(void *data)
553{
554 msg_pspew("%s\n", __func__);
555
556 /* URB 28. Command Set SPI Voltage to 0. */
557 if (dediprog_set_spi_voltage(0x0))
558 return 1;
559
560 if (usb_release_interface(dediprog_handle, 0)) {
561 msg_perr("Could not release USB interface!\n");
562 return 1;
563 }
564 if (usb_close(dediprog_handle)) {
565 msg_perr("Could not close USB device!\n");
566 return 1;
567 }
568 return 0;
569}
570
hailfingerdfb32a02010-01-19 11:15:48 +0000571/* URB numbers refer to the first log ever captured. */
572int dediprog_init(void)
573{
574 struct usb_device *dev;
hailfingerf76cc322010-11-09 22:00:31 +0000575 char *voltage;
hailfingerb91c08c2011-08-15 19:54:20 +0000576 int millivolt = 3500;
577 int ret;
hailfingerdfb32a02010-01-19 11:15:48 +0000578
579 msg_pspew("%s\n", __func__);
580
hailfingerf76cc322010-11-09 22:00:31 +0000581 voltage = extract_programmer_param("voltage");
582 if (voltage) {
583 millivolt = parse_voltage(voltage);
584 free(voltage);
uwe8d342eb2011-07-28 08:13:25 +0000585 if (millivolt < 0)
hailfingerf76cc322010-11-09 22:00:31 +0000586 return 1;
hailfingerf76cc322010-11-09 22:00:31 +0000587 msg_pinfo("Setting voltage to %i mV\n", millivolt);
588 }
589
hailfingerdfb32a02010-01-19 11:15:48 +0000590 /* Here comes the USB stuff. */
591 usb_init();
592 usb_find_busses();
593 usb_find_devices();
594 dev = get_device_by_vid_pid(0x0483, 0xdada);
595 if (!dev) {
596 msg_perr("Could not find a Dediprog SF100 on USB!\n");
597 return 1;
598 }
599 msg_pdbg("Found USB device (%04x:%04x).\n",
uwe8d342eb2011-07-28 08:13:25 +0000600 dev->descriptor.idVendor, dev->descriptor.idProduct);
hailfingerdfb32a02010-01-19 11:15:48 +0000601 dediprog_handle = usb_open(dev);
hailfingerfb6287d2010-11-16 21:25:29 +0000602 ret = usb_set_configuration(dediprog_handle, 1);
603 if (ret < 0) {
604 msg_perr("Could not set USB device configuration: %i %s\n",
605 ret, usb_strerror());
606 if (usb_close(dediprog_handle))
607 msg_perr("Could not close USB device!\n");
608 return 1;
609 }
610 ret = usb_claim_interface(dediprog_handle, 0);
611 if (ret < 0) {
612 msg_perr("Could not claim USB device interface %i: %i %s\n",
613 0, ret, usb_strerror());
614 if (usb_close(dediprog_handle))
615 msg_perr("Could not close USB device!\n");
616 return 1;
617 }
618 dediprog_endpoint = 2;
stepan4c06de72011-01-28 09:00:15 +0000619
dhendrix0ffc2eb2011-06-14 01:35:36 +0000620 if (register_shutdown(dediprog_shutdown, NULL))
621 return 1;
622
Simon Glasse53cc1d2015-01-08 05:48:51 -0700623 dediprog_set_leds(LED_ALL);
stepan4c06de72011-01-28 09:00:15 +0000624
hailfingerdfb32a02010-01-19 11:15:48 +0000625 /* URB 6. Command A. */
stepan4c06de72011-01-28 09:00:15 +0000626 if (dediprog_command_a()) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700627 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000628 return 1;
stepan4c06de72011-01-28 09:00:15 +0000629 }
hailfingerdfb32a02010-01-19 11:15:48 +0000630 /* URB 7. Command A. */
stepan4c06de72011-01-28 09:00:15 +0000631 if (dediprog_command_a()) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700632 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000633 return 1;
stepan4c06de72011-01-28 09:00:15 +0000634 }
hailfingerdfb32a02010-01-19 11:15:48 +0000635 /* URB 8. Command Prepare Receive Device String. */
636 /* URB 9. Command Receive Device String. */
stepan4c06de72011-01-28 09:00:15 +0000637 if (dediprog_check_devicestring()) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700638 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000639 return 1;
stepan4c06de72011-01-28 09:00:15 +0000640 }
hailfingerdfb32a02010-01-19 11:15:48 +0000641 /* URB 10. Command C. */
stepan4c06de72011-01-28 09:00:15 +0000642 if (dediprog_command_c()) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700643 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000644 return 1;
stepan4c06de72011-01-28 09:00:15 +0000645 }
hailfingerdfb32a02010-01-19 11:15:48 +0000646 /* URB 11. Command Set SPI Voltage. */
stepan4c06de72011-01-28 09:00:15 +0000647 if (dediprog_set_spi_voltage(millivolt)) {
Simon Glasse53cc1d2015-01-08 05:48:51 -0700648 dediprog_set_leds(LED_ERROR);
hailfingerdfb32a02010-01-19 11:15:48 +0000649 return 1;
stepan4c06de72011-01-28 09:00:15 +0000650 }
hailfingerdfb32a02010-01-19 11:15:48 +0000651
mkarcherd264e9e2011-05-11 17:07:07 +0000652 register_spi_programmer(&spi_programmer_dediprog);
hailfingerdfb32a02010-01-19 11:15:48 +0000653
654 /* RE leftover, leave in until the driver is complete. */
655#if 0
656 /* Execute RDID by hand if you want to test it. */
657 dediprog_do_stuff();
658#endif
659
Simon Glasse53cc1d2015-01-08 05:48:51 -0700660 dediprog_set_leds(0);
stepan4c06de72011-01-28 09:00:15 +0000661
hailfingerdfb32a02010-01-19 11:15:48 +0000662 return 0;
663}
664
hailfinger1ff33dc2010-07-03 11:02:10 +0000665#if 0
hailfingerdfb32a02010-01-19 11:15:48 +0000666/* Leftovers from reverse engineering. Keep for documentation purposes until
667 * completely understood.
668 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000669static int dediprog_do_stuff(void)
hailfingerdfb32a02010-01-19 11:15:48 +0000670{
671 char buf[0x4];
672 /* SPI command processing starts here. */
673
674 /* URB 12. Command Send SPI. */
675 /* URB 13. Command Receive SPI. */
676 memset(buf, 0, sizeof(buf));
677 /* JEDEC RDID */
678 msg_pdbg("Sending RDID\n");
679 buf[0] = JEDEC_RDID;
uwe8d342eb2011-07-28 08:13:25 +0000680 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
681 (unsigned char *)buf, (unsigned char *)buf))
hailfingerdfb32a02010-01-19 11:15:48 +0000682 return 1;
683 msg_pdbg("Receiving response: ");
684 print_hex(buf, JEDEC_RDID_INSIZE);
hailfingerdfb32a02010-01-19 11:15:48 +0000685 /* URB 14-27 are more SPI commands. */
686 /* URB 28. Command Set SPI Voltage. */
687 if (dediprog_set_spi_voltage(0x0))
688 return 1;
689 /* URB 29-38. Command F, unsuccessful wait. */
690 if (dediprog_command_f(544))
691 return 1;
692 /* URB 39. Command Set SPI Voltage. */
693 if (dediprog_set_spi_voltage(0x10))
694 return 1;
695 /* URB 40. Command Set SPI Speed. */
696 if (dediprog_set_spi_speed(0x2))
697 return 1;
698 /* URB 41 is just URB 28. */
699 /* URB 42,44,46,48,51,53 is just URB 8. */
700 /* URB 43,45,47,49,52,54 is just URB 9. */
701 /* URB 50 is just URB 6/7. */
702 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
703 /* URB 132,134 is just URB 6/7. */
704 /* URB 133 is just URB 29-38. */
705 /* URB 135 is just URB 8. */
706 /* URB 136 is just URB 9. */
707 /* URB 137 is just URB 11. */
708
stepanff582cc2011-01-20 21:05:15 +0000709 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
710 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
711 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
712 * 512 bytes, rest is filled with 0xff.
713 */
hailfingerdfb32a02010-01-19 11:15:48 +0000714
hailfingerdfb32a02010-01-19 11:15:48 +0000715 return 0;
716}
hailfinger1ff33dc2010-07-03 11:02:10 +0000717#endif