blob: 2529ac61d01f1fa0562b9a43939ca316d8a17333 [file] [log] [blame]
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
Mathias Krausedb7afc52010-11-09 23:30:43 +000020#include <stdio.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000021#include <string.h>
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000022#include <usb.h>
23#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000024#include "chipdrivers.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000026#include "spi.h"
27
28#define DEFAULT_TIMEOUT 3000
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000029static usb_dev_handle *dediprog_handle;
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +000030static int dediprog_firmwareversion;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +000031static int dediprog_endpoint;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000032
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000033#if 0
34/* Might be useful for other pieces of code as well. */
35static void print_hex(void *buf, size_t len)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000036{
37 size_t i;
38
39 for (i = 0; i < len; i++)
40 msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
41}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000042#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000043
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000044/* Might be useful for other USB devices as well. static for now. */
45static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000046{
47 struct usb_bus *bus;
48 struct usb_device *dev;
49
50 for (bus = usb_get_busses(); bus; bus = bus->next)
51 for (dev = bus->devices; dev; dev = dev->next)
52 if ((dev->descriptor.idVendor == vid) &&
53 (dev->descriptor.idProduct == pid))
54 return dev;
55
56 return NULL;
57}
58
59//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
60
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000061static int dediprog_set_spi_voltage(int millivolt)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000062{
63 int ret;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000064 uint16_t voltage_selector;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000065
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000066 switch (millivolt) {
67 case 0:
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000068 /* Admittedly this one is an assumption. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000069 voltage_selector = 0x0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000070 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000071 case 1800:
72 voltage_selector = 0x12;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000073 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000074 case 2500:
75 voltage_selector = 0x11;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000076 break;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000077 case 3500:
78 voltage_selector = 0x10;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000079 break;
80 default:
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000081 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000082 return 1;
83 }
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000084 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
85 millivolt % 1000);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000086
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000087 ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000088 if (ret != 0x0) {
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000089 msg_perr("Command Set SPI Voltage 0x%x failed!\n", voltage_selector);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000090 return 1;
91 }
92 return 0;
93}
94
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000095#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000096/* After dediprog_set_spi_speed, the original app always calls
97 * dediprog_set_spi_voltage(0) and then
98 * dediprog_check_devicestring() four times in a row.
99 * After that, dediprog_command_a() is called.
100 * This looks suspiciously like the microprocessor in the SF100 has to be
101 * restarted/reinitialized in case the speed changes.
102 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000103static int dediprog_set_spi_speed(uint16_t speed)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000104{
105 int ret;
106 unsigned int khz;
107
108 /* Case 1 and 2 are in weird order. Probably an organically "grown"
109 * interface.
110 * Base frequency is 24000 kHz, divisors are (in order)
111 * 1, 3, 2, 8, 11, 16, 32, 64.
112 */
113 switch (speed) {
114 case 0x0:
115 khz = 24000;
116 break;
117 case 0x1:
118 khz = 8000;
119 break;
120 case 0x2:
121 khz = 12000;
122 break;
123 case 0x3:
124 khz = 3000;
125 break;
126 case 0x4:
127 khz = 2180;
128 break;
129 case 0x5:
130 khz = 1500;
131 break;
132 case 0x6:
133 khz = 750;
134 break;
135 case 0x7:
136 khz = 375;
137 break;
138 default:
139 msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
140 return 1;
141 }
142 msg_pdbg("Setting SPI speed to %u kHz\n", khz);
143
144 ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
145 if (ret != 0x0) {
146 msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
147 return 1;
148 }
149 return 0;
150}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000151#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000152
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000153/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
154 * @start start address
155 * @len length
156 * @return 0 on success, 1 on failure
157 */
158static int dediprog_spi_bulk_read(struct flashchip *flash, uint8_t *buf,
159 int start, int len)
160{
161 int ret;
162 int i;
163 /* chunksize must be 512, other sizes will NOT work at all. */
164 const int chunksize = 0x200;
165 const int count = len / chunksize;
166 const char count_and_chunk[] = {count & 0xff,
167 (count >> 8) & 0xff,
168 chunksize & 0xff,
169 (chunksize >> 8) & 0xff};
170
171 if ((start % chunksize) || (len % chunksize)) {
172 msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
173 "at flashrom@flashrom.org\n", __func__, start, len);
174 return 1;
175 }
176
177 /* No idea if the hardware can handle empty reads, so chicken out. */
178 if (!len)
179 return 0;
180 /* Command Read SPI Bulk. No idea which read command is used on the
181 * SPI side.
182 */
183 ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
184 start / 0x10000, (char *)count_and_chunk,
185 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
186 if (ret != sizeof(count_and_chunk)) {
187 msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
188 usb_strerror());
189 return 1;
190 }
191
192 for (i = 0; i < count; i++) {
193 ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
194 (char *)buf + i * chunksize, chunksize,
195 DEFAULT_TIMEOUT);
196 if (ret != chunksize) {
197 msg_perr("SPI bulk read %i failed, expected %i, got %i "
198 "%s!\n", i, chunksize, ret, usb_strerror());
199 return 1;
200 }
201 }
202
203 return 0;
204}
205
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000206int dediprog_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
207{
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000208 int ret;
209 /* chunksize must be 512, other sizes will NOT work at all. */
210 const int chunksize = 0x200;
211 int residue = start % chunksize ? chunksize - start % chunksize : 0;
212 int bulklen;
213
214 if (residue) {
215 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
216 start, residue);
217 ret = spi_read_chunked(flash, buf, start, residue, 16);
218 if (ret)
219 return ret;
220 }
221
222 /* Round down. */
223 bulklen = (len - residue) / chunksize * chunksize;
224 ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
225 bulklen);
226 if (ret)
227 return ret;
228
229 len -= residue + bulklen;
230 if (len) {
231 msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
232 start, len);
233 ret = spi_read_chunked(flash, buf + residue + bulklen,
234 start + residue + bulklen, len, 16);
235 if (ret)
236 return ret;
237 }
238
239 return 0;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000240}
241
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000242int dediprog_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
243{
244 /* No idea about the real limit. Maybe 12, maybe more, maybe less. */
245 return spi_write_chunked(flash, buf, start, len, 12);
246}
247
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000248int dediprog_spi_send_command(unsigned int writecnt, unsigned int readcnt,
249 const unsigned char *writearr, unsigned char *readarr)
250{
251 int ret;
252
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000253 msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000254 /* Paranoid, but I don't want to be blamed if anything explodes. */
Carl-Daniel Hailfinger306b8182010-11-23 21:28:16 +0000255 if (writecnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000256 msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000257 return 1;
258 }
259 /* 16 byte reads should work. */
260 if (readcnt > 16) {
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000261 msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000262 return 1;
263 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000264
265 ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff, readcnt ? 0x1 : 0x0, (char *)writearr, writecnt, DEFAULT_TIMEOUT);
266 if (ret != writecnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000267 msg_perr("Send SPI failed, expected %i, got %i %s!\n",
268 writecnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000269 return 1;
270 }
271 if (!readcnt)
272 return 0;
273 memset(readarr, 0, readcnt);
274 ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000, (char *)readarr, readcnt, DEFAULT_TIMEOUT);
275 if (ret != readcnt) {
Carl-Daniel Hailfingereac65792010-01-22 02:53:30 +0000276 msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
277 readcnt, ret, usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000278 return 1;
279 }
280 return 0;
281}
282
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000283static int dediprog_check_devicestring(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000284{
285 int ret;
Mathias Krausedb7afc52010-11-09 23:30:43 +0000286 int fw[3];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000287 char buf[0x11];
288
289 /* Command Prepare Receive Device String. */
290 memset(buf, 0, sizeof(buf));
291 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf, 0x1, DEFAULT_TIMEOUT);
292 /* The char casting is needed to stop gcc complaining about an always true comparison. */
293 if ((ret != 0x1) || (buf[0] != (char)0xff)) {
294 msg_perr("Unexpected response to Command Prepare Receive Device"
295 " String!\n");
296 return 1;
297 }
298 /* Command Receive Device String. */
299 memset(buf, 0, sizeof(buf));
300 ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf, 0x10, DEFAULT_TIMEOUT);
301 if (ret != 0x10) {
302 msg_perr("Incomplete/failed Command Receive Device String!\n");
303 return 1;
304 }
305 buf[0x10] = '\0';
306 msg_pdbg("Found a %s\n", buf);
307 if (memcmp(buf, "SF100", 0x5)) {
308 msg_perr("Device not a SF100!\n");
309 return 1;
310 }
Mathias Krausedb7afc52010-11-09 23:30:43 +0000311 if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
312 msg_perr("Unexpected firmware version string!\n");
313 return 1;
314 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000315 /* Only these versions were tested. */
Mathias Krausedb7afc52010-11-09 23:30:43 +0000316 if (fw[0] < 2 || fw[0] > 5) {
317 msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
318 fw[1], fw[2]);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000319 return 1;
320 }
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000321 dediprog_firmwareversion = fw[0];
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000322 return 0;
323}
324
325/* Command A seems to be some sort of device init. It is either followed by
326 * dediprog_check_devicestring (often) or Command A (often) or
327 * Command F (once).
328 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000329static int dediprog_command_a(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000330{
331 int ret;
332 char buf[0x1];
333
334 memset(buf, 0, sizeof(buf));
335 ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf, 0x1, DEFAULT_TIMEOUT);
Mathias Krausedb7afc52010-11-09 23:30:43 +0000336 if (ret < 0) {
337 msg_perr("Command A failed (%s)!\n", usb_strerror());
338 return 1;
339 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000340 if ((ret != 0x1) || (buf[0] != 0x6f)) {
341 msg_perr("Unexpected response to Command A!\n");
342 return 1;
343 }
344 return 0;
345}
346
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000347#if 0
348/* Something.
349 * Present in eng_detect_blink.log with firmware 3.1.8
350 * Always preceded by Command Receive Device String
351 */
352static int dediprog_command_b(void)
353{
354 int ret;
355 char buf[0x3];
356
357 memset(buf, 0, sizeof(buf));
358 ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf, 0x3, DEFAULT_TIMEOUT);
359 if (ret < 0) {
360 msg_perr("Command B failed (%s)!\n", usb_strerror());
361 return 1;
362 }
363 if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
364 (buf[2] != 0xff)) {
365 msg_perr("Unexpected response to Command B!\n");
366 return 1;
367 }
368
369 return 0;
370}
371#endif
372
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000373/* Command C is only sent after dediprog_check_devicestring, but not after every
374 * invocation of dediprog_check_devicestring. It is only sent after the first
375 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
376 * I'm tempted to call this one start_SPI_engine or finish_init.
377 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000378static int dediprog_command_c(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000379{
380 int ret;
381
382 ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL, 0x0, DEFAULT_TIMEOUT);
383 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000384 msg_perr("Command C failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000385 return 1;
386 }
387 return 0;
388}
389
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000390#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000391/* Very strange. Seems to be a programmer keepalive or somesuch.
392 * Wait unsuccessfully for timeout ms to read one byte.
393 * Is usually called after setting voltage to 0.
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000394 * Present in all logs with Firmware 2.1.1 and 3.1.8
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000395 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000396static int dediprog_command_f(int timeout)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000397{
398 int ret;
399 char buf[0x1];
400
401 memset(buf, 0, sizeof(buf));
402 ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf, 0x1, timeout);
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000403 /* This check is most probably wrong. Command F always causes a timeout
404 * in the logs, so we should check for timeout instead of checking for
405 * success.
406 */
407 if (ret != 0x1) {
408 msg_perr("Command F failed (%s)!\n", usb_strerror());
409 return 1;
410 }
411 return 0;
412}
413
414/* Start/stop blinking?
415 * Present in eng_detect_blink.log with firmware 3.1.8
416 * Preceded by Command J
417 */
418static int dediprog_command_g(void)
419{
420 int ret;
421
422 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000423 if (ret != 0x0) {
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000424 msg_perr("Command G failed (%s)!\n", usb_strerror());
425 return 1;
426 }
427 return 0;
428}
429
430/* Something.
431 * Present in all logs with firmware 5.1.5
432 * Always preceded by Command Receive Device String
433 * Always followed by Command Set SPI Voltage nonzero
434 */
435static int dediprog_command_h(void)
436{
437 int ret;
438
439 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
440 if (ret != 0x0) {
441 msg_perr("Command H failed (%s)!\n", usb_strerror());
442 return 1;
443 }
444 return 0;
445}
446#endif
447
448/* Shutdown for firmware 5.x?
449 * This command swithces the "Pass" LED on.
450 * Present in all logs with firmware 5.1.5
451 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
452 * Always followed by Command Set SPI Voltage 0x0000
453 */
454static int dediprog_command_i(void)
455{
456 int ret;
457
458 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
459 if (ret != 0x0) {
460 msg_perr("Command I failed (%s)!\n", usb_strerror());
461 return 1;
462 }
463 return 0;
464}
465
466#if 0
467/* Start/stop blinking?
468 * Present in all logs with firmware 5.1.5
469 * Always preceded by Command Receive Device String on 5.1.5
470 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
471 * Present in eng_detect_blink.log with firmware 3.1.8
472 * Preceded by Command B in eng_detect_blink.log
473 * Followed by Command G in eng_detect_blink.log
474 */
475static int dediprog_command_j(void)
476{
477 int ret;
478
479 ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
480 if (ret != 0x0) {
481 msg_perr("Command J failed (%s)!\n", usb_strerror());
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000482 return 1;
483 }
484 return 0;
485}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000486#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000487
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000488static int parse_voltage(char *voltage)
489{
490 char *tmp = NULL;
491 int i;
492 int millivolt;
493 int fraction = 0;
494
495 if (!voltage || !strlen(voltage)) {
496 msg_perr("Empty voltage= specified.\n");
497 return -1;
498 }
499 millivolt = (int)strtol(voltage, &tmp, 0);
500 voltage = tmp;
501 /* Handle "," and "." as decimal point. Everything after it is assumed
502 * to be in decimal notation.
503 */
504 if ((*voltage == '.') || (*voltage == ',')) {
505 voltage++;
506 for (i = 0; i < 3; i++) {
507 fraction *= 10;
508 /* Don't advance if the current character is invalid,
509 * but continue multiplying.
510 */
511 if ((*voltage < '0') || (*voltage > '9'))
512 continue;
513 fraction += *voltage - '0';
514 voltage++;
515 }
516 /* Throw away remaining digits. */
517 voltage += strspn(voltage, "0123456789");
518 }
519 /* The remaining string must be empty or "mV" or "V". */
520 tolower_string(voltage);
521
522 /* No unit or "V". */
523 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
524 millivolt *= 1000;
525 millivolt += fraction;
526 } else if (!strncmp(voltage, "mv", 2) ||
527 !strncmp(voltage, "milliv", 6)) {
528 /* No adjustment. fraction is discarded. */
529 } else {
530 /* Garbage at the end of the string. */
531 msg_perr("Garbage voltage= specified.\n");
532 return -1;
533 }
534 return millivolt;
535}
536
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000537/* URB numbers refer to the first log ever captured. */
538int dediprog_init(void)
539{
540 struct usb_device *dev;
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000541 char *voltage;
542 int millivolt = 3500;
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000543 int ret;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000544
545 msg_pspew("%s\n", __func__);
546
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000547 voltage = extract_programmer_param("voltage");
548 if (voltage) {
549 millivolt = parse_voltage(voltage);
550 free(voltage);
551 if (millivolt < 0) {
552 return 1;
553 }
554 msg_pinfo("Setting voltage to %i mV\n", millivolt);
555 }
556
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000557 /* Here comes the USB stuff. */
558 usb_init();
559 usb_find_busses();
560 usb_find_devices();
561 dev = get_device_by_vid_pid(0x0483, 0xdada);
562 if (!dev) {
563 msg_perr("Could not find a Dediprog SF100 on USB!\n");
564 return 1;
565 }
566 msg_pdbg("Found USB device (%04x:%04x).\n",
567 dev->descriptor.idVendor,
568 dev->descriptor.idProduct);
569 dediprog_handle = usb_open(dev);
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000570 ret = usb_set_configuration(dediprog_handle, 1);
571 if (ret < 0) {
572 msg_perr("Could not set USB device configuration: %i %s\n",
573 ret, usb_strerror());
574 if (usb_close(dediprog_handle))
575 msg_perr("Could not close USB device!\n");
576 return 1;
577 }
578 ret = usb_claim_interface(dediprog_handle, 0);
579 if (ret < 0) {
580 msg_perr("Could not claim USB device interface %i: %i %s\n",
581 0, ret, usb_strerror());
582 if (usb_close(dediprog_handle))
583 msg_perr("Could not close USB device!\n");
584 return 1;
585 }
586 dediprog_endpoint = 2;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000587 /* URB 6. Command A. */
588 if (dediprog_command_a())
589 return 1;
590 /* URB 7. Command A. */
591 if (dediprog_command_a())
592 return 1;
593 /* URB 8. Command Prepare Receive Device String. */
594 /* URB 9. Command Receive Device String. */
595 if (dediprog_check_devicestring())
596 return 1;
597 /* URB 10. Command C. */
598 if (dediprog_command_c())
599 return 1;
600 /* URB 11. Command Set SPI Voltage. */
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000601 if (dediprog_set_spi_voltage(millivolt))
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000602 return 1;
603
604 buses_supported = CHIP_BUSTYPE_SPI;
605 spi_controller = SPI_CONTROLLER_DEDIPROG;
606
607 /* RE leftover, leave in until the driver is complete. */
608#if 0
609 /* Execute RDID by hand if you want to test it. */
610 dediprog_do_stuff();
611#endif
612
613 return 0;
614}
615
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000616#if 0
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000617/* Leftovers from reverse engineering. Keep for documentation purposes until
618 * completely understood.
619 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000620static int dediprog_do_stuff(void)
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000621{
622 char buf[0x4];
623 /* SPI command processing starts here. */
624
625 /* URB 12. Command Send SPI. */
626 /* URB 13. Command Receive SPI. */
627 memset(buf, 0, sizeof(buf));
628 /* JEDEC RDID */
629 msg_pdbg("Sending RDID\n");
630 buf[0] = JEDEC_RDID;
631 if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE, (unsigned char *)buf, (unsigned char *)buf))
632 return 1;
633 msg_pdbg("Receiving response: ");
634 print_hex(buf, JEDEC_RDID_INSIZE);
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000635 /* URB 14-27 are more SPI commands. */
636 /* URB 28. Command Set SPI Voltage. */
637 if (dediprog_set_spi_voltage(0x0))
638 return 1;
639 /* URB 29-38. Command F, unsuccessful wait. */
640 if (dediprog_command_f(544))
641 return 1;
642 /* URB 39. Command Set SPI Voltage. */
643 if (dediprog_set_spi_voltage(0x10))
644 return 1;
645 /* URB 40. Command Set SPI Speed. */
646 if (dediprog_set_spi_speed(0x2))
647 return 1;
648 /* URB 41 is just URB 28. */
649 /* URB 42,44,46,48,51,53 is just URB 8. */
650 /* URB 43,45,47,49,52,54 is just URB 9. */
651 /* URB 50 is just URB 6/7. */
652 /* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
653 /* URB 132,134 is just URB 6/7. */
654 /* URB 133 is just URB 29-38. */
655 /* URB 135 is just URB 8. */
656 /* URB 136 is just URB 9. */
657 /* URB 137 is just URB 11. */
658
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000659 /* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
660 /* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
661 /* Bulk transfer sizes for Command Start Bulk Read/Write are always
662 * 512 bytes, rest is filled with 0xff.
663 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000664
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000665 return 0;
666}
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000667#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000668
669int dediprog_shutdown(void)
670{
671 msg_pspew("%s\n", __func__);
672
Carl-Daniel Hailfingerff30d8a2011-01-20 21:05:15 +0000673 /* Shutdown on firmware 5.x */
674 if (dediprog_firmwareversion == 5)
675 if (dediprog_command_i())
676 return 1;
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000677 /* URB 28. Command Set SPI Voltage to 0. */
678 if (dediprog_set_spi_voltage(0x0))
679 return 1;
680
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000681 if (usb_release_interface(dediprog_handle, 0)) {
682 msg_perr("Could not release USB interface!\n");
683 return 1;
684 }
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000685 if (usb_close(dediprog_handle)) {
Carl-Daniel Hailfinger482e9742010-11-16 21:25:29 +0000686 msg_perr("Could not close USB device!\n");
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000687 return 1;
688 }
689 return 0;
690}