source: trunk/dediprog.c @ 1477

Revision 1477, 23.2 KB checked in by hailfinger, 7 weeks ago (diff)

Speed up dediprog SPI page writes

All chips which use spi_chip_write_256 should be written at native
speed. Chips using spi_chip_write_1 or spi_chip_write_aai will
still be slow.

Thanks to Steven A. Falco for testing with a ST/Numonyx M25P16.
Thanks to David Hendricks for testing with a Winbond W25Q64.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@…>
Acked-by: Steven A. Falco <sfalco@…>

Line 
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{
38        size_t i;
39
40        for (i = 0; i < len; i++)
41                msg_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{
48        struct usb_bus *bus;
49        struct usb_device *dev;
50
51        for (bus = usb_get_busses(); bus; bus = bus->next)
52                for (dev = bus->devices; dev; dev = dev->next)
53                        if ((dev->descriptor.idVendor == vid) &&
54                            (dev->descriptor.idProduct == pid))
55                                return dev;
56
57        return 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{
73        int ret, target_leds;
74
75        if (leds < 0 || leds > 7)
76                leds = 0; // Bogus value, enable all LEDs
77
78        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)) {
89                target_leds = ((leds & ERROR_OFF) >> 2) | 
90                        ((leds & PASS_OFF) << 2);
91        } else {
92                target_leds = leds;
93        }
94
95        ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, target_leds,
96                              NULL, 0x0, DEFAULT_TIMEOUT);
97        if (ret != 0x0) {
98                msg_perr("Command Set LED 0x%x failed (%s)!\n",
99                         leds, usb_strerror());
100                return 1;
101        }
102
103        current_led_status = leds;
104
105        return 0;
106}
107
108static int dediprog_set_spi_voltage(int millivolt)
109{
110        int ret;
111        uint16_t voltage_selector;
112
113        switch (millivolt) {
114        case 0:
115                /* Admittedly this one is an assumption. */
116                voltage_selector = 0x0;
117                break;
118        case 1800:
119                voltage_selector = 0x12;
120                break;
121        case 2500:
122                voltage_selector = 0x11;
123                break;
124        case 3500:
125                voltage_selector = 0x10;
126                break;
127        default:
128                msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
129                return 1;
130        }
131        msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
132                 millivolt % 1000);
133
134        ret = usb_control_msg(dediprog_handle, 0x42, 0x9, voltage_selector,
135                              0xff, NULL, 0x0, DEFAULT_TIMEOUT);
136        if (ret != 0x0) {
137                msg_perr("Command Set SPI Voltage 0x%x failed!\n",
138                         voltage_selector);
139                return 1;
140        }
141        return 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{
154        int ret;
155        unsigned 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         */
162        switch (speed) {
163        case 0x0:
164                khz = 24000;
165                break;
166        case 0x1:
167                khz = 8000;
168                break;
169        case 0x2:
170                khz = 12000;
171                break;
172        case 0x3:
173                khz = 3000;
174                break;
175        case 0x4:
176                khz = 2180;
177                break;
178        case 0x5:
179                khz = 1500;
180                break;
181        case 0x6:
182                khz = 750;
183                break;
184        case 0x7:
185                khz = 375;
186                break;
187        default:
188                msg_perr("Unknown frequency selector 0x%x! Aborting.\n", speed);
189                return 1;
190        }
191        msg_pdbg("Setting SPI speed to %u kHz\n", khz);
192
193        ret = usb_control_msg(dediprog_handle, 0x42, 0x61, speed, 0xff, NULL,
194                              0x0, DEFAULT_TIMEOUT);
195        if (ret != 0x0) {
196                msg_perr("Command Set SPI Speed 0x%x failed!\n", speed);
197                return 1;
198        }
199        return 0;
200}
201#endif
202
203/* Bulk read interface, will read multiple 512 byte chunks aligned to 512 bytes.
204 * @start       start address
205 * @len         length
206 * @return      0 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{
211        int ret;
212        unsigned int i;
213        /* chunksize must be 512, other sizes will NOT work at all. */
214        const unsigned int chunksize = 0x200;
215        const unsigned int count = len / chunksize;
216        const char count_and_chunk[] = {count & 0xff,
217                                        (count >> 8) & 0xff,
218                                        chunksize & 0xff,
219                                        (chunksize >> 8) & 0xff};
220
221        if ((start % chunksize) || (len % chunksize)) {
222                msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
223                         "at flashrom@flashrom.org\n", __func__, start, len);
224                return 1;
225        }
226
227        /* No idea if the hardware can handle empty reads, so chicken out. */
228        if (!len)
229                return 0;
230        /* Command Read SPI Bulk. No idea which read command is used on the
231         * SPI side.
232         */
233        ret = usb_control_msg(dediprog_handle, 0x42, 0x20, start % 0x10000,
234                              start / 0x10000, (char *)count_and_chunk,
235                              sizeof(count_and_chunk), DEFAULT_TIMEOUT);
236        if (ret != sizeof(count_and_chunk)) {
237                msg_perr("Command Read SPI Bulk failed, %i %s!\n", ret,
238                         usb_strerror());
239                return 1;
240        }
241
242        for (i = 0; i < count; i++) {
243                ret = usb_bulk_read(dediprog_handle, 0x80 | dediprog_endpoint,
244                                    (char *)buf + i * chunksize, chunksize,
245                                    DEFAULT_TIMEOUT);
246                if (ret != chunksize) {
247                        msg_perr("SPI bulk read %i failed, expected %i, got %i "
248                                 "%s!\n", i, chunksize, ret, usb_strerror());
249                        return 1;
250                }
251        }
252
253        return 0;
254}
255
256static int dediprog_spi_read(struct flashctx *flash, uint8_t *buf,
257                             unsigned int start, unsigned int len)
258{
259        int ret;
260        /* chunksize must be 512, other sizes will NOT work at all. */
261        const unsigned int chunksize = 0x200;
262        unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
263        unsigned int bulklen;
264
265        dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
266
267        if (residue) {
268                msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
269                         start, residue);
270                ret = spi_read_chunked(flash, buf, start, residue, 16);
271                if (ret) {
272                        dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
273                        return ret;
274                }
275        }
276
277        /* Round down. */
278        bulklen = (len - residue) / chunksize * chunksize;
279        ret = dediprog_spi_bulk_read(flash, buf + residue, start + residue,
280                                     bulklen);
281        if (ret) {
282                dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
283                return ret;
284        }
285
286        len -= residue + bulklen;
287        if (len) {
288                msg_pdbg("Slow read for partial block from 0x%x, length 0x%x\n",
289                         start, len);
290                ret = spi_read_chunked(flash, buf + residue + bulklen,
291                                       start + residue + bulklen, len, 16);
292                if (ret) {
293                        dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
294                        return ret;
295                }
296        }
297
298        dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
299        return 0;
300}
301
302/* Bulk write interface, will read multiple page_size byte chunks aligned to page_size bytes.
303 * @start       start address
304 * @len         length
305 * @return      0 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{
310        int ret;
311        unsigned 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         */
316        const unsigned int chunksize = flash->page_size;
317        const unsigned int count = len / chunksize;
318        const char count_and_chunk[] = {count & 0xff,
319                                        (count >> 8) & 0xff,
320                                        chunksize & 0xff,
321                                        (chunksize >> 8) & 0xff};
322        char usbbuf[512];
323
324        if ((start % chunksize) || (len % chunksize)) {
325                msg_perr("%s: Unaligned start=%i, len=%i! Please report a bug "
326                         "at flashrom@flashrom.org\n", __func__, start, len);
327                return 1;
328        }
329
330        /* No idea if the hardware can handle empty writes, so chicken out. */
331        if (!len)
332                return 0;
333        /* Command Write SPI Bulk. No idea which write command is used on the
334         * SPI side.
335         */
336        ret = usb_control_msg(dediprog_handle, 0x42, 0x30, start % 0x10000,
337                              start / 0x10000, (char *)count_and_chunk,
338                              sizeof(count_and_chunk), DEFAULT_TIMEOUT);
339        if (ret != sizeof(count_and_chunk)) {
340                msg_perr("Command Write SPI Bulk failed, %i %s!\n", ret,
341                         usb_strerror());
342                return 1;
343        }
344
345        for (i = 0; i < count; i++) {
346                memset(usbbuf, 0xff, sizeof(usbbuf));
347                memcpy(usbbuf, buf + i * chunksize, chunksize);
348                ret = usb_bulk_write(dediprog_handle, dediprog_endpoint,
349                                    usbbuf, 512,
350                                    DEFAULT_TIMEOUT);
351                if (ret != 512) {
352                        msg_perr("SPI bulk write failed, expected %i, got %i "
353                                 "%s!\n", 512, ret, usb_strerror());
354                        return 1;
355                }
356        }
357
358        return 0;
359}
360
361static int dediprog_spi_write_256(struct flashctx *flash, uint8_t *buf,
362                                  unsigned int start, unsigned int len)
363{
364        int ret;
365        const unsigned int chunksize = flash->page_size;
366        unsigned int residue = start % chunksize ? chunksize - start % chunksize : 0;
367        unsigned int bulklen;
368
369        dediprog_set_leds(PASS_OFF|BUSY_ON|ERROR_OFF);
370
371        if (residue) {
372                msg_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. */
375                ret = spi_write_chunked(flash, buf, start, residue, 12);
376                if (ret) {
377                        dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
378                        return ret;
379                }
380        }
381
382        /* Round down. */
383        bulklen = (len - residue) / chunksize * chunksize;
384        ret = dediprog_spi_bulk_write(flash, buf + residue, start + residue,
385                                     bulklen);
386        if (ret) {
387                dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
388                return ret;
389        }
390
391        len -= residue + bulklen;
392        if (len) {
393                msg_pdbg("Slow write for partial block from 0x%x, length 0x%x\n",
394                         start, len);
395                ret = spi_write_chunked(flash, buf + residue + bulklen,
396                                        start + residue + bulklen, len, 12);
397                if (ret) {
398                        dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
399                        return ret;
400                }
401        }
402
403        dediprog_set_leds(PASS_ON|BUSY_OFF|ERROR_OFF);
404        return 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{
413        int ret;
414
415        msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
416        /* Paranoid, but I don't want to be blamed if anything explodes. */
417        if (writecnt > 16) {
418                msg_perr("Untested writecnt=%i, aborting.\n", writecnt);
419                return 1;
420        }
421        /* 16 byte reads should work. */
422        if (readcnt > 16) {
423                msg_perr("Untested readcnt=%i, aborting.\n", readcnt);
424                return 1;
425        }
426       
427        ret = usb_control_msg(dediprog_handle, 0x42, 0x1, 0xff,
428                              readcnt ? 0x1 : 0x0, (char *)writearr, writecnt,
429                              DEFAULT_TIMEOUT);
430        if (ret != writecnt) {
431                msg_perr("Send SPI failed, expected %i, got %i %s!\n",
432                         writecnt, ret, usb_strerror());
433                return 1;
434        }
435        if (!readcnt)
436                return 0;
437        memset(readarr, 0, readcnt);
438        ret = usb_control_msg(dediprog_handle, 0xc2, 0x01, 0xbb8, 0x0000,
439                             (char *)readarr, readcnt, DEFAULT_TIMEOUT);
440        if (ret != readcnt) {
441                msg_perr("Receive SPI failed, expected %i, got %i %s!\n",
442                         readcnt, ret, usb_strerror());
443                return 1;
444        }
445        return 0;
446}
447
448static int dediprog_check_devicestring(void)
449{
450        int ret;
451        int fw[3];
452        char buf[0x11];
453
454        /* Command Prepare Receive Device String. */
455        memset(buf, 0, sizeof(buf));
456        ret = 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. */
459        if ((ret != 0x1) || (buf[0] != (char)0xff)) {
460                msg_perr("Unexpected response to Command Prepare Receive Device"
461                         " String!\n");
462                return 1;
463        }
464        /* Command Receive Device String. */
465        memset(buf, 0, sizeof(buf));
466        ret = usb_control_msg(dediprog_handle, 0xc2, 0x8, 0xff, 0xff, buf,
467                              0x10, DEFAULT_TIMEOUT);
468        if (ret != 0x10) {
469                msg_perr("Incomplete/failed Command Receive Device String!\n");
470                return 1;
471        }
472        buf[0x10] = '\0';
473        msg_pdbg("Found a %s\n", buf);
474        if (memcmp(buf, "SF100", 0x5)) {
475                msg_perr("Device not a SF100!\n");
476                return 1;
477        }
478        if (sscanf(buf, "SF100 V:%d.%d.%d ", &fw[0], &fw[1], &fw[2]) != 3) {
479                msg_perr("Unexpected firmware version string!\n");
480                return 1;
481        }
482        /* Only these versions were tested. */
483        if (fw[0] < 2 || fw[0] > 5) {
484                msg_perr("Unexpected firmware version %d.%d.%d!\n", fw[0],
485                         fw[1], fw[2]);
486                return 1;
487        }
488        dediprog_firmwareversion = FIRMWARE_VERSION(fw[0], fw[1], fw[2]);
489        return 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{
498        int ret;
499        char buf[0x1];
500
501        memset(buf, 0, sizeof(buf));
502        ret = usb_control_msg(dediprog_handle, 0xc3, 0xb, 0x0, 0x0, buf,
503                              0x1, DEFAULT_TIMEOUT);
504        if (ret < 0) {
505                msg_perr("Command A failed (%s)!\n", usb_strerror());
506                return 1;
507        }
508        if ((ret != 0x1) || (buf[0] != 0x6f)) {
509                msg_perr("Unexpected response to Command A!\n");
510                return 1;
511        }
512        return 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{
522        int ret;
523        char buf[0x3];
524
525        memset(buf, 0, sizeof(buf));
526        ret = usb_control_msg(dediprog_handle, 0xc3, 0x7, 0x0, 0xef00, buf,
527                              0x3, DEFAULT_TIMEOUT);
528        if (ret < 0) {
529                msg_perr("Command B failed (%s)!\n", usb_strerror());
530                return 1;
531        }
532        if ((ret != 0x3) || (buf[0] != 0xff) || (buf[1] != 0xff) ||
533            (buf[2] != 0xff)) {
534                msg_perr("Unexpected response to Command B!\n");
535                return 1;
536        }
537
538        return 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{
549        int ret;
550
551        ret = usb_control_msg(dediprog_handle, 0x42, 0x4, 0x0, 0x0, NULL,
552                              0x0, DEFAULT_TIMEOUT);
553        if (ret != 0x0) {
554                msg_perr("Command C failed (%s)!\n", usb_strerror());
555                return 1;
556        }
557        return 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{
568        int ret;
569        char buf[0x1];
570
571        memset(buf, 0, sizeof(buf));
572        ret = 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         */
578        if (ret != 0x1) {
579                msg_perr("Command F failed (%s)!\n", usb_strerror());
580                return 1;
581        }
582        return 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{
591        int ret;
592
593        ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x03, NULL, 0x0, DEFAULT_TIMEOUT);
594        if (ret != 0x0) {
595                msg_perr("Command G failed (%s)!\n", usb_strerror());
596                return 1;
597        }
598        return 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{
608        int ret;
609
610        ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x05, NULL, 0x0, DEFAULT_TIMEOUT);
611        if (ret != 0x0) {
612                msg_perr("Command H failed (%s)!\n", usb_strerror());
613                return 1;
614        }
615        return 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{
625        int ret;
626
627        ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x06, NULL, 0x0, DEFAULT_TIMEOUT);
628        if (ret != 0x0) {
629                msg_perr("Command I failed (%s)!\n", usb_strerror());
630                return 1;
631        }
632        return 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{
645        int ret;
646
647        ret = usb_control_msg(dediprog_handle, 0x42, 0x07, 0x09, 0x07, NULL, 0x0, DEFAULT_TIMEOUT);
648        if (ret != 0x0) {
649                msg_perr("Command J failed (%s)!\n", usb_strerror());
650                return 1;
651        }
652        return 0;
653}
654#endif
655
656static int parse_voltage(char *voltage)
657{
658        char *tmp = NULL;
659        int i;
660        int millivolt = 0, fraction = 0;
661
662        if (!voltage || !strlen(voltage)) {
663                msg_perr("Empty voltage= specified.\n");
664                return -1;
665        }
666        millivolt = (int)strtol(voltage, &tmp, 0);
667        voltage = tmp;
668        /* Handle "," and "." as decimal point. Everything after it is assumed
669         * to be in decimal notation.
670         */
671        if ((*voltage == '.') || (*voltage == ',')) {
672                voltage++;
673                for (i = 0; i < 3; i++) {
674                        fraction *= 10;
675                        /* Don't advance if the current character is invalid,
676                         * but continue multiplying.
677                         */
678                        if ((*voltage < '0') || (*voltage > '9'))
679                                continue;
680                        fraction += *voltage - '0';
681                        voltage++;
682                }
683                /* Throw away remaining digits. */
684                voltage += strspn(voltage, "0123456789");
685        }
686        /* The remaining string must be empty or "mV" or "V". */
687        tolower_string(voltage);
688
689        /* No unit or "V". */
690        if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
691                millivolt *= 1000;
692                millivolt += 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. */
698                msg_perr("Garbage voltage= specified.\n");
699                return -1;
700        }
701        return 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{
716        msg_pspew("%s\n", __func__);
717
718#if 0
719        /* Shutdown on firmware 5.x */
720        if (dediprog_firmwareversion == 5)
721                if (dediprog_command_i())
722                        return 1;
723#endif
724
725        /* URB 28. Command Set SPI Voltage to 0. */
726        if (dediprog_set_spi_voltage(0x0))
727                return 1;
728
729        if (usb_release_interface(dediprog_handle, 0)) {
730                msg_perr("Could not release USB interface!\n");
731                return 1;
732        }
733        if (usb_close(dediprog_handle)) {
734                msg_perr("Could not close USB device!\n");
735                return 1;
736        }
737        return 0;
738}
739
740/* URB numbers refer to the first log ever captured. */
741int dediprog_init(void)
742{
743        struct usb_device *dev;
744        char *voltage;
745        int millivolt = 3500;
746        int ret;
747
748        msg_pspew("%s\n", __func__);
749
750        voltage = extract_programmer_param("voltage");
751        if (voltage) {
752                millivolt = parse_voltage(voltage);
753                free(voltage);
754                if (millivolt < 0)
755                        return 1;
756                msg_pinfo("Setting voltage to %i mV\n", millivolt);
757        }
758
759        /* Here comes the USB stuff. */
760        usb_init();
761        usb_find_busses();
762        usb_find_devices();
763        dev = get_device_by_vid_pid(0x0483, 0xdada);
764        if (!dev) {
765                msg_perr("Could not find a Dediprog SF100 on USB!\n");
766                return 1;
767        }
768        msg_pdbg("Found USB device (%04x:%04x).\n",
769                 dev->descriptor.idVendor, dev->descriptor.idProduct);
770        dediprog_handle = usb_open(dev);
771        ret = usb_set_configuration(dediprog_handle, 1);
772        if (ret < 0) {
773                msg_perr("Could not set USB device configuration: %i %s\n",
774                         ret, usb_strerror());
775                if (usb_close(dediprog_handle))
776                        msg_perr("Could not close USB device!\n");
777                return 1;
778        }
779        ret = usb_claim_interface(dediprog_handle, 0);
780        if (ret < 0) {
781                msg_perr("Could not claim USB device interface %i: %i %s\n",
782                         0, ret, usb_strerror());
783                if (usb_close(dediprog_handle))
784                        msg_perr("Could not close USB device!\n");
785                return 1;
786        }
787        dediprog_endpoint = 2;
788       
789        if (register_shutdown(dediprog_shutdown, NULL))
790                return 1;
791
792        dediprog_set_leds(PASS_ON|BUSY_ON|ERROR_ON);
793
794        /* URB 6. Command A. */
795        if (dediprog_command_a()) {
796                dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
797                return 1;
798        }
799        /* URB 7. Command A. */
800        if (dediprog_command_a()) {
801                dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
802                return 1;
803        }
804        /* URB 8. Command Prepare Receive Device String. */
805        /* URB 9. Command Receive Device String. */
806        if (dediprog_check_devicestring()) {
807                dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
808                return 1;
809        }
810        /* URB 10. Command C. */
811        if (dediprog_command_c()) {
812                dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
813                return 1;
814        }
815        /* URB 11. Command Set SPI Voltage. */
816        if (dediprog_set_spi_voltage(millivolt)) {
817                dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_ON);
818                return 1;
819        }
820
821        register_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. */
826        dediprog_do_stuff();
827#endif
828
829        dediprog_set_leds(PASS_OFF|BUSY_OFF|ERROR_OFF);
830
831        return 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{
840        char buf[0x4];
841        /* SPI command processing starts here. */
842
843        /* URB 12. Command Send SPI. */
844        /* URB 13. Command Receive SPI. */
845        memset(buf, 0, sizeof(buf));
846        /* JEDEC RDID */
847        msg_pdbg("Sending RDID\n");
848        buf[0] = JEDEC_RDID;
849        if (dediprog_spi_send_command(JEDEC_RDID_OUTSIZE, JEDEC_RDID_INSIZE,
850                                (unsigned char *)buf, (unsigned char *)buf))
851                return 1;
852        msg_pdbg("Receiving response: ");
853        print_hex(buf, JEDEC_RDID_INSIZE);
854        /* URB 14-27 are more SPI commands. */
855        /* URB 28. Command Set SPI Voltage. */
856        if (dediprog_set_spi_voltage(0x0))
857                return 1;
858        /* URB 29-38. Command F, unsuccessful wait. */
859        if (dediprog_command_f(544))
860                return 1;
861        /* URB 39. Command Set SPI Voltage. */
862        if (dediprog_set_spi_voltage(0x10))
863                return 1;
864        /* URB 40. Command Set SPI Speed. */
865        if (dediprog_set_spi_speed(0x2))
866                return 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
884        return 0;
885}
886#endif 
Note: See TracBrowser for help on using the repository browser.