Flashrom

Flashrom Svn Source Tree

Root/trunk/dediprog.c

1/*
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
20#include <stdio.h>
21#include <string.h>
22#include <usb.h>
23#include "flash.h"
24#include "chipdrivers.h"
25#include "programmer.h"
26#include "spi.h"
27
28#define FIRMWARE_VERSION(x,y,z) ((x << 16) | (y << 8) | z)
29#define DEFAULT_TIMEOUT 3000
30static usb_dev_handle *dediprog_handle;
31static int dediprog_firmwareversion;
32static int dediprog_endpoint;
33
34#if 0
35/* Might be useful for other pieces of code as well. */
36static void print_hex(void *buf, size_t len)
37{
38size_t i;
39
40for (i = 0; i < len; i++)
41msg_pdbg(" %02x", ((uint8_t *)buf)[i]);
42}
43#endif
44
45/* Might be useful for other USB devices as well. static for now. */
46static struct usb_device *get_device_by_vid_pid(uint16_t vid, uint16_t pid)
47{
48struct usb_bus *bus;
49struct usb_device *dev;
50
51for (bus = usb_get_busses(); bus; bus = bus->next)
52for (dev = bus->devices; dev; dev = dev->next)
53if ((dev->descriptor.idVendor == vid) &&
54 (dev->descriptor.idProduct == pid))
55return dev;
56
57return NULL;
58}
59
60//int usb_control_msg(usb_dev_handle *dev, int requesttype, int request, int value, int index, char *bytes, int size, int timeout);
61
62/* Set/clear LEDs on dediprog */
63#define PASS_ON(0 << 0)
64#define PASS_OFF(1 << 0)
65#define BUSY_ON(0 << 1)
66#define BUSY_OFF(1 << 1)
67#define ERROR_ON(0 << 2)
68#define ERROR_OFF(1 << 2)
69static int current_led_status = -1;
70
71static int dediprog_set_leds(int leds)
72{
73int ret, target_leds;
74
75if (leds < 0 || leds > 7)
76leds = 0; // Bogus value, enable all LEDs
77
78if (leds == current_led_status)
79return 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 */
88if (dediprog_firmwareversion < FIRMWARE_VERSION(5,0,0)) {
89target_leds = ((leds & ERROR_OFF) >> 2) |
90((leds & PASS_OFF) << 2);
91} else {
92target_leds = leds;
93}
94
95ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
96 NULL, 0x0, DEFAULT_TIMEOUT);
97if (ret != 0x0) {
98msg_perr("Command Set LED 0x%x failed (%s)!\n",
99 leds, usb_strerror());
100return 1;
101}
102
103current_led_status = leds;
104
105return 0;
106}
107
108static int dediprog_set_spi_voltage(int millivolt)
109{
110int ret;
111uint16_t voltage_selector;
112
113switch (millivolt) {
114case 0:
115/* Admittedly this one is an assumption. */
116voltage_selector = 0x0;
117break;
118case 1800:
119voltage_selector = 0x12;
120break;
121case 2500:
122voltage_selector = 0x11;
123break;
124case 3500:
125voltage_selector = 0x10;
126break;
127default:
128msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
129return 1;
130}
131msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
132 millivolt % 1000);
133
134ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
135 0xff, NULL, 0x0, DEFAULT_TIMEOUT);
136if (ret != 0x0) {
137msg_perr("Command Set SPI Voltage 0x%x failed!\n",
138 voltage_selector);
139return 1;
140}
141return 0;
142}
143
144#if 0
145/* After dediprog_set_spi_speed, the original app always calls
146 * dediprog_set_spi_voltage(0) and then
147 * dediprog_check_devicestring() four times in a row.
148 * After that, dediprog_command_a() is called.
149 * This looks suspiciously like the microprocessor in the SF100 has to be
150 * restarted/reinitialized in case the speed changes.
151 */
152static int dediprog_set_spi_speed(uint16_t speed)
153{
154int ret;
155unsigned int khz;
156
157/* Case 1 and 2 are in weird order. Probably an organically "grown"
158 * interface.
159 * Base frequency is 24000 kHz, divisors are (in order)
160 * 1, 3, 2, 8, 11, 16, 32, 64.
161 */
162switch (speed) {
163case 0x0:
164khz = 24000;
165break;
166case 0x1:
167khz = 8000;
168break;
169case 0x2:
170khz = 12000;
171break;
172case 0x3:
173khz = 3000;
174break;
175case 0x4:
176khz = 2180;
177break;
178case 0x5:
179khz = 1500;
180break;
181case 0x6:
182khz = 750;
183break;
184case 0x7:
185khz = 375;
186break;
187default:
188msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
189return 1;
190}
191msg_pdbg("Setting SPI speed to %u kHz\n", khz);
192
193ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
194 0x0, DEFAULT_TIMEOUT);
195if (ret != 0x0) {
196msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
197return 1;
198}
199return 0;
200}
201#endif
202
203/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
204 * @startstart address
205 * @lenlength
206 * @return0 on success, 1 on failure
207 */
208static int dediprog_spi_bulk_read(struct flashctx *flash, uint8_t *buf,
209 unsigned int start, unsigned int len)
210{
211int ret;
212unsigned int i;
213/* chunksize must be 512, other sizes will NOT work at all. */
214const unsigned int chunksize = 0x200;
215const unsigned int count = len / chunksize;
216const char count_and_chunk[] = {count & 0xff,
217(count >> 8) & 0xff,
218chunksize & 0xff,
219(chunksize >> 8) & 0xff};
220
221if ((start % chunksize) || (len % chunksize)) {
222msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
223 "at flashrom@flashrom.org\n", __func__, start, len);
224return 1;
225}
226
227/* No idea if the hardware can handle empty reads, so chicken out. */
228if (!len)
229return 0;
230/* Command Read SPI Bulk. No idea which read command is used on the
231 * SPI side.
232 */
233ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
234 start / 0x10000, (char *)count_and_chunk,
235 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
236if (ret != sizeof(count_and_chunk)) {
237msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
238 usb_strerror());
239return 1;
240}
241
242for (i = 0; i < count; i++) {
243ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
244 (char *)buf + i * chunksize, chunksize,
245 DEFAULT_TIMEOUT);
246if (ret != chunksize) {
247msg_perr("SPI bulk read %i failed, expected %i, got %i "
248 "%s!\n", i, chunksize, ret, usb_strerror());
249return 1;
250}
251}
252
253return 0;
254}
255
256static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
257 unsigned int start, unsigned int len)
258{
259int ret;
260/* chunksize must be 512, other sizes will NOT work at all. */
261const unsigned int chunksize = 0x200;
262unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
263unsigned int bulklen;
264
265dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
266
267if (residue) {
268msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
269 start, residue);
270ret = spi_read_chunked(flash, buf, start, residue, 16);
271if (ret) {
272dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
273return ret;
274}
275}
276
277/* Round down. */
278bulklen = (len - residue) / chunksize * chunksize;
279ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
280 bulklen);
281if (ret) {
282dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
283return ret;
284}
285
286len -= residue + bulklen;
287if (len) {
288msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
289 start, len);
290ret = spi_read_chunked(flash, buf + residue + bulklen,
291 start + residue + bulklen, len, 16);
292if (ret) {
293dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
294return ret;
295}
296}
297
298dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
299return 0;
300}
301
302/* Bulk write interface, will read multiple page_size byte chunks aligned to page_size bytes.
303 * @startstart address
304 * @lenlength
305 * @return0 on success, 1 on failure
306 */
307static int dediprog_spi_bulk_write(struct flashctx *flash, uint8_t *buf,
308 unsigned int start, unsigned int len)
309{
310int ret;
311unsigned int i;
312/* USB transfer size must be 512, other sizes will NOT work at all.
313 * chunksize is the real data size per USB bulk transfer. The remaining
314 * space in a USB bulk transfer must be filled with 0xff padding.
315 */
316const unsigned int chunksize = flash->page_size;
317const unsigned int count = len / chunksize;
318const char count_and_chunk[] = {count & 0xff,
319(count >> 8) & 0xff,
320chunksize & 0xff,
321(chunksize >> 8) & 0xff};
322char usbbuf[512];
323
324if ((start % chunksize) || (len % chunksize)) {
325msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
326 "at flashrom@flashrom.org\n", __func__, start, len);
327return 1;
328}
329
330/* No idea if the hardware can handle empty writes, so chicken out. */
331if (!len)
332return 0;
333/* Command Write SPI Bulk. No idea which write command is used on the
334 * SPI side.
335 */
336ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000,
337 start / 0x10000, (char *)count_and_chunk,
338 sizeof(count_and_chunk), DEFAULT_TIMEOUT);
339if (ret != sizeof(count_and_chunk)) {
340msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
341 usb_strerror());
342return 1;
343}
344
345for (i = 0; i < count; i++) {
346memset(usbbuf, 0xff, sizeof(usbbuf));
347memcpy(usbbuf, buf + i * chunksize, chunksize);
348ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
349 usbbuf, 512,
350 DEFAULT_TIMEOUT);
351if (ret != 512) {
352msg_perr("SPI bulk write failed, expected %i, got %i "
353 "%s!\n", 512, ret, usb_strerror());
354return 1;
355}
356}
357
358return 0;
359}
360
361static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf,
362 unsigned int start, unsigned int len)
363{
364int ret;
365const unsigned int chunksize = flash->page_size;
366unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
367unsigned int bulklen;
368
369dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
370
371if (residue) {
372msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
373 start, residue);
374/* No idea about the real limit. Maybe 12, maybe more. */
375ret = spi_write_chunked(flash, buf, start, residue, 12);
376if (ret) {
377dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
378return ret;
379}
380}
381
382/* Round down. */
383bulklen = (len - residue) / chunksize * chunksize;
384ret = dediprog_spi_bulk_write(flash, buf + residue, start + residue,
385 bulklen);
386if (ret) {
387dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
388return ret;
389}
390
391len -= residue + bulklen;
392if (len) {
393msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
394 start, len);
395ret = spi_write_chunked(flash, buf + residue + bulklen,
396 start + residue + bulklen, len, 12);
397if (ret) {
398dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
399return ret;
400}
401}
402
403dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
404return 0;
405}
406
407static int dediprog_spi_send_command(struct flashctx *flash,
408 unsigned int writecnt,
409 unsigned int readcnt,
410 const unsigned char *writearr,
411 unsigned char *readarr)
412{
413int ret;
414
415msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
416/* Paranoid, but I don't want to be blamed if anything explodes. */
417if (writecnt > 16) {
418msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
419return 1;
420}
421/* 16 byte reads should work. */
422if (readcnt > 16) {
423msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
424return 1;
425}
426
427ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
428 readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
429 DEFAULT_TIMEOUT);
430if (ret != writecnt) {
431msg_perr("Send SPI failed, expected %i, got %i %s!\n",
432 writecnt, ret, usb_strerror());
433return 1;
434}
435if (!readcnt)
436return 0;
437memset(readarr, 0, readcnt);
438ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
439 (char *)readarr, readcnt, DEFAULT_TIMEOUT);
440if (ret != readcnt) {
441msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
442 readcnt, ret, usb_strerror());
443return 1;
444}
445return 0;
446}
447
448static int dediprog_check_devicestring(void)
449{
450int ret;
451int fw[3];
452char buf[0x11];
453
454/* Command Prepare Receive Device String. */
455memset(buf, 0, sizeof(buf));
456ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef03, buf,
457 0x1, DEFAULT_TIMEOUT);
458/* The char casting is needed to stop gcc complaining about an always true comparison. */
459if ((ret != 0x1) || (buf[0] != (char)0xff)) {
460msg_perr("Unexpected response to Command Prepare Receive Device"
461 " String!\n");
462return 1;
463}
464/* Command Receive Device String. */
465memset(buf, 0, sizeof(buf));
466ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
467 0x10, DEFAULT_TIMEOUT);
468if (ret != 0x10) {
469msg_perr("Incomplete/failed Command Receive Device String!\n");
470return 1;
471}
472buf[0x10] = '\0';
473msg_pdbg("Found a %s\n", buf);
474if (memcmp(buf, "SF100", 0x5)) {
475msg_perr("Device not a SF100!\n");
476return 1;
477}
478if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
479msg_perr("Unexpected firmware version string!\n");
480return 1;
481}
482/* Only these versions were tested. */
483if (fw[0] < 2 || fw[0] > 5) {
484msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
485 fw[1], fw[2]);
486return 1;
487}
488dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
489return 0;
490}
491
492/* Command A seems to be some sort of device init. It is either followed by
493 * dediprog_check_devicestring (often) or Command A (often) or
494 * Command F (once).
495 */
496static int dediprog_command_a(void)
497{
498int ret;
499char buf[0x1];
500
501memset(buf, 0, sizeof(buf));
502ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
503 0x1, DEFAULT_TIMEOUT);
504if (ret < 0) {
505msg_perr("Command A failed (%s)!\n", usb_strerror());
506return 1;
507}
508if ((ret != 0x1) || (buf[0] != 0x6f)) {
509msg_perr("Unexpected response to Command A!\n");
510return 1;
511}
512return 0;
513}
514
515#if 0
516/* Something.
517 * Present in eng_detect_blink.log with firmware 3.1.8
518 * Always preceded by Command Receive Device String
519 */
520static int dediprog_command_b(void)
521{
522int ret;
523char buf[0x3];
524
525memset(buf, 0, sizeof(buf));
526ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
527 0x3, DEFAULT_TIMEOUT);
528if (ret < 0) {
529msg_perr("Command B failed (%s)!\n", usb_strerror());
530return 1;
531}
532if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
533 (buf[2] != 0xff)) {
534msg_perr("Unexpected response to Command B!\n");
535return 1;
536}
537
538return 0;
539}
540#endif
541
542/* Command C is only sent after dediprog_check_devicestring, but not after every
543 * invocation of dediprog_check_devicestring. It is only sent after the first
544 * dediprog_command_a(); dediprog_check_devicestring() sequence in each session.
545 * I'm tempted to call this one start_SPI_engine or finish_init.
546 */
547static int dediprog_command_c(void)
548{
549int ret;
550
551ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
552 0x0, DEFAULT_TIMEOUT);
553if (ret != 0x0) {
554msg_perr("Command C failed (%s)!\n", usb_strerror());
555return 1;
556}
557return 0;
558}
559
560#if 0
561/* Very strange. Seems to be a programmer keepalive or somesuch.
562 * Wait unsuccessfully for timeout ms to read one byte.
563 * Is usually called after setting voltage to 0.
564 * Present in all logs with Firmware 2.1.1 and 3.1.8
565 */
566static int dediprog_command_f(int timeout)
567{
568int ret;
569char buf[0x1];
570
571memset(buf, 0, sizeof(buf));
572ret = usb_control_msg(dediprog_handle, 0xc2, 0x11, 0xff, 0xff, buf,
573 0x1, timeout);
574/* This check is most probably wrong. Command F always causes a timeout
575 * in the logs, so we should check for timeout instead of checking for
576 * success.
577 */
578if (ret != 0x1) {
579msg_perr("Command F failed (%s)!\n", usb_strerror());
580return 1;
581}
582return 0;
583}
584
585/* Start/stop blinking?
586 * Present in eng_detect_blink.log with firmware 3.1.8
587 * Preceded by Command J
588 */
589static int dediprog_command_g(void)
590{
591int ret;
592
593ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
594if (ret != 0x0) {
595msg_perr("Command G failed (%s)!\n", usb_strerror());
596return 1;
597}
598return 0;
599}
600
601/* Something.
602 * Present in all logs with firmware 5.1.5
603 * Always preceded by Command Receive Device String
604 * Always followed by Command Set SPI Voltage nonzero
605 */
606static int dediprog_command_h(void)
607{
608int ret;
609
610ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
611if (ret != 0x0) {
612msg_perr("Command H failed (%s)!\n", usb_strerror());
613return 1;
614}
615return 0;
616}
617
618/* Shutdown for firmware 5.x?
619 * Present in all logs with firmware 5.1.5
620 * Often preceded by a SPI operation (Command Read SPI Bulk or Receive SPI)
621 * Always followed by Command Set SPI Voltage 0x0000
622 */
623static int dediprog_command_i(void)
624{
625int ret;
626
627ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
628if (ret != 0x0) {
629msg_perr("Command I failed (%s)!\n", usb_strerror());
630return 1;
631}
632return 0;
633}
634
635/* Start/stop blinking?
636 * Present in all logs with firmware 5.1.5
637 * Always preceded by Command Receive Device String on 5.1.5
638 * Always followed by Command Set SPI Voltage nonzero on 5.1.5
639 * Present in eng_detect_blink.log with firmware 3.1.8
640 * Preceded by Command B in eng_detect_blink.log
641 * Followed by Command G in eng_detect_blink.log
642 */
643static int dediprog_command_j(void)
644{
645int ret;
646
647ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
648if (ret != 0x0) {
649msg_perr("Command J failed (%s)!\n", usb_strerror());
650return 1;
651}
652return 0;
653}
654#endif
655
656static int parse_voltage(char *voltage)
657{
658char *tmp = NULL;
659int i;
660int millivolt = 0, fraction = 0;
661
662if (!voltage || !strlen(voltage)) {
663msg_perr("Empty voltage= specified.\n");
664return -1;
665}
666millivolt = (int)strtol(voltage, &tmp, 0);
667voltage = tmp;
668/* Handle "," and "." as decimal point. Everything after it is assumed
669 * to be in decimal notation.
670 */
671if ((*voltage == '.') || (*voltage == ',')) {
672voltage++;
673for (i = 0; i < 3; i++) {
674fraction *= 10;
675/* Don't advance if the current character is invalid,
676 * but continue multiplying.
677 */
678if ((*voltage < '0') || (*voltage > '9'))
679continue;
680fraction += *voltage - '0';
681voltage++;
682}
683/* Throw away remaining digits. */
684voltage += strspn(voltage, "0123456789");
685}
686/* The remaining string must be empty or "mV" or "V". */
687tolower_string(voltage);
688
689/* No unit or "V". */
690if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
691millivolt *= 1000;
692millivolt += fraction;
693} else if (!strncmp(voltage, "mv", 2) ||
694 !strncmp(voltage, "milliv", 6)) {
695/* No adjustment. fraction is discarded. */
696} else {
697/* Garbage at the end of the string. */
698msg_perr("Garbage voltage= specified.\n");
699return -1;
700}
701return millivolt;
702}
703
704static const struct spi_programmer spi_programmer_dediprog = {
705.type= SPI_CONTROLLER_DEDIPROG,
706.max_data_read= MAX_DATA_UNSPECIFIED,
707.max_data_write= MAX_DATA_UNSPECIFIED,
708.command= dediprog_spi_send_command,
709.multicommand= default_spi_send_multicommand,
710.read= dediprog_spi_read,
711.write_256= dediprog_spi_write_256,
712};
713
714static int dediprog_shutdown(void *data)
715{
716msg_pspew("%s\n", __func__);
717
718#if 0
719/* Shutdown on firmware 5.x */
720if (dediprog_firmwareversion == 5)
721if (dediprog_command_i())
722return 1;
723#endif
724
725/* URB 28. Command Set SPI Voltage to 0. */
726if (dediprog_set_spi_voltage(0x0))
727return 1;
728
729if (usb_release_interface(dediprog_handle, 0)) {
730msg_perr("Could not release USB interface!\n");
731return 1;
732}
733if (usb_close(dediprog_handle)) {
734msg_perr("Could not close USB device!\n");
735return 1;
736}
737return 0;
738}
739
740/* URB numbers refer to the first log ever captured. */
741int dediprog_init(void)
742{
743struct usb_device *dev;
744char *voltage;
745int millivolt = 3500;
746int ret;
747
748msg_pspew("%s\n", __func__);
749
750voltage = extract_programmer_param("voltage");
751if (voltage) {
752millivolt = parse_voltage(voltage);
753free(voltage);
754if (millivolt < 0)
755return 1;
756msg_pinfo("Setting voltage to %i mV\n", millivolt);
757}
758
759/* Here comes the USB stuff. */
760usb_init();
761usb_find_busses();
762usb_find_devices();
763dev = get_device_by_vid_pid(0x0483, 0xdada);
764if (!dev) {
765msg_perr("Could not find a Dediprog SF100 on USB!\n");
766return 1;
767}
768msg_pdbg("Found USB device (%04x:%04x).\n",
769 dev->descriptor.idVendor, dev->descriptor.idProduct);
770dediprog_handle = usb_open(dev);
771ret = usb_set_configuration(dediprog_handle, 1);
772if (ret < 0) {
773msg_perr("Could not set USB device configuration: %i %s\n",
774 ret, usb_strerror());
775if (usb_close(dediprog_handle))
776msg_perr("Could not close USB device!\n");
777return 1;
778}
779ret = usb_claim_interface(dediprog_handle, 0);
780if (ret < 0) {
781msg_perr("Could not claim USB device interface %i: %i %s\n",
782 0, ret, usb_strerror());
783if (usb_close(dediprog_handle))
784msg_perr("Could not close USB device!\n");
785return 1;
786}
787dediprog_endpoint = 2;
788
789if (register_shutdown(dediprog_shutdown, NULL))
790return 1;
791
792dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
793
794/* URB 6. Command A. */
795if (dediprog_command_a()) {
796dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
797return 1;
798}
799/* URB 7. Command A. */
800if (dediprog_command_a()) {
801dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
802return 1;
803}
804/* URB 8. Command Prepare Receive Device String. */
805/* URB 9. Command Receive Device String. */
806if (dediprog_check_devicestring()) {
807dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
808return 1;
809}
810/* URB 10. Command C. */
811if (dediprog_command_c()) {
812dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
813return 1;
814}
815/* URB 11. Command Set SPI Voltage. */
816if (dediprog_set_spi_voltage(millivolt)) {
817dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
818return 1;
819}
820
821register_spi_programmer(&spi_programmer_dediprog);
822
823/* RE leftover, leave in until the driver is complete. */
824#if 0
825/* Execute RDID by hand if you want to test it. */
826dediprog_do_stuff();
827#endif
828
829dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
830
831return 0;
832}
833
834#if 0
835/* Leftovers from reverse engineering. Keep for documentation purposes until
836 * completely understood.
837 */
838static int dediprog_do_stuff(void)
839{
840char buf[0x4];
841/* SPI command processing starts here. */
842
843/* URB 12. Command Send SPI. */
844/* URB 13. Command Receive SPI. */
845memset(buf, 0, sizeof(buf));
846/* JEDEC RDID */
847msg_pdbg("Sending RDID\n");
848buf[0] = JEDEC_RDID;
849if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
850(unsigned char *)buf, (unsigned char *)buf))
851return 1;
852msg_pdbg("Receiving response: ");
853print_hex(buf, JEDEC_RDID_INSIZE);
854/* URB 14-27 are more SPI commands. */
855/* URB 28. Command Set SPI Voltage. */
856if (dediprog_set_spi_voltage(0x0))
857return 1;
858/* URB 29-38. Command F, unsuccessful wait. */
859if (dediprog_command_f(544))
860return 1;
861/* URB 39. Command Set SPI Voltage. */
862if (dediprog_set_spi_voltage(0x10))
863return 1;
864/* URB 40. Command Set SPI Speed. */
865if (dediprog_set_spi_speed(0x2))
866return 1;
867/* URB 41 is just URB 28. */
868/* URB 42,44,46,48,51,53 is just URB 8. */
869/* URB 43,45,47,49,52,54 is just URB 9. */
870/* URB 50 is just URB 6/7. */
871/* URB 55-131 is just URB 29-38. (wait unsuccessfully for 4695 (maybe 4751) ms)*/
872/* URB 132,134 is just URB 6/7. */
873/* URB 133 is just URB 29-38. */
874/* URB 135 is just URB 8. */
875/* URB 136 is just URB 9. */
876/* URB 137 is just URB 11. */
877
878/* Command Start Bulk Read. Data is u16 blockcount, u16 blocksize. */
879/* Command Start Bulk Write. Data is u16 blockcount, u16 blocksize. */
880/* Bulk transfer sizes for Command Start Bulk Read/Write are always
881 * 512 bytes, rest is filled with 0xff.
882 */
883
884return 0;
885}
886#endif
887

Archive Download this file

Revision: HEAD