1 /*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Paul Fox <[email protected]>
5 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17 #include <stdbool.h>
18 #include <stdio.h>
19 #include <strings.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <ctype.h>
23 #include "flash.h"
24 #include "programmer.h"
25 #include "spi.h"
26 #include <ftdi.h>
27
28 /* This is not defined in libftdi.h <0.20 (c7e4c09e68cfa6f5e112334aa1b3bb23401c8dc7 to be exact).
29 * Some tests indicate that this is the only change that it is needed to support the FT232H in flashrom. */
30 #if !defined(HAVE_FT232H)
31 #define TYPE_232H 6
32 #endif
33
34 /* Please keep sorted by vendor ID, then device ID. */
35
36 #define FTDI_VID 0x0403
37 #define FTDI_FT2232H_PID 0x6010
38 #define FTDI_FT4232H_PID 0x6011
39 #define FTDI_FT232H_PID 0x6014
40 #define FTDI_FT4233H_PID 0x6041
41 #define TIAO_TUMPA_PID 0x8a98
42 #define TIAO_TUMPA_LITE_PID 0x8a99
43 #define KT_LINK_PID 0xbbe2
44 #define AMONTEC_JTAGKEY_PID 0xCFF8
45
46 #define GOEPEL_VID 0x096C
47 #define GOEPEL_PICOTAP_PID 0x1449
48
49 #define FIC_VID 0x1457
50 #define OPENMOKO_DBGBOARD_PID 0x5118
51
52 #define OLIMEX_VID 0x15BA
53 #define OLIMEX_ARM_OCD_PID 0x0003
54 #define OLIMEX_ARM_TINY_PID 0x0004
55 #define OLIMEX_ARM_OCD_H_PID 0x002B
56 #define OLIMEX_ARM_TINY_H_PID 0x002A
57
58 #define GOOGLE_VID 0x18D1
59 #define GOOGLE_SERVO_PID 0x5001
60 #define GOOGLE_SERVO_V2_PID0 0x5002
61 #define GOOGLE_SERVO_V2_PID1 0x5003
62
63 static const struct dev_entry devs_ft2232spi[] = {
64 {FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"},
65 {FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"},
66 {FTDI_VID, FTDI_FT232H_PID, OK, "FTDI", "FT232H"},
67 {FTDI_VID, FTDI_FT4233H_PID, OK, "FTDI", "FT4233H"},
68 {FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
69 {FTDI_VID, TIAO_TUMPA_LITE_PID, OK, "TIAO", "USB Multi-Protocol Adapter Lite"},
70 {FTDI_VID, KT_LINK_PID, OK, "Kristech", "KT-LINK"},
71 {FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
72 {GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"},
73 {GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"},
74 {GOOGLE_VID, GOOGLE_SERVO_V2_PID0, OK, "Google", "Servo V2 Legacy"},
75 {GOOGLE_VID, GOOGLE_SERVO_V2_PID1, OK, "Google", "Servo V2"},
76 {FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC", "OpenMoko Neo1973 Debug board (V2+)"},
77 {OLIMEX_VID, OLIMEX_ARM_OCD_PID, OK, "Olimex", "ARM-USB-OCD"},
78 {OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"},
79 {OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, OK, "Olimex", "ARM-USB-OCD-H"},
80 {OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, OK, "Olimex", "ARM-USB-TINY-H"},
81
82 {0},
83 };
84
85 #define FTDI_HW_BUFFER_SIZE 4096 /* in bytes */
86
87 #define DEFAULT_DIVISOR 2
88
89 #define BITMODE_BITBANG_NORMAL 1
90 #define BITMODE_BITBANG_SPI 2
91
92 /*
93 * The variables `cs_bits` and `pindir` store the values for the
94 * "set data bits low byte" MPSSE command that sets the initial
95 * state and the direction of the I/O pins. `cs_bits` pins default
96 * to high and will be toggled during SPI transactions. All other
97 * output pins will be kept low all the time. For some programmers,
98 * some reserved GPIOL* pins are used as outputs. Free GPIOL* pins
99 * are configured as inputs, while it's possible to use them either
100 * as generic gpios or as additional CS# signal(s) through the
101 * parameter(s) `gpiolX`. On exit, all pins will be reconfigured
102 * as inputs.
103 *
104 * The pin offsets are as follows:
105 * TCK/SK is bit 0.
106 * TDI/DO is bit 1.
107 * TDO/DI is bit 2.
108 * TMS/CS is bit 3.
109 * GPIOL0 is bit 4.
110 * GPIOL1 is bit 5.
111 * GPIOL2 is bit 6.
112 * GPIOL3 is bit 7.
113 *
114 * The default values (set below in ft2232_spi_init) are used for
115 * most devices:
116 * value: 0x08 CS=high, DI=low, DO=low, SK=low
117 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
118 */
119 struct ft2232_data {
120 uint8_t cs_bits;
121 uint8_t aux_bits;
122 uint8_t pindir;
123 struct ftdi_context ftdic_context;
124 };
125
get_ft2232_devicename(int ft2232_vid,int ft2232_type)126 static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
127 {
128 int i;
129 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
130 if ((devs_ft2232spi[i].device_id == ft2232_type)
131 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
132 return devs_ft2232spi[i].device_name;
133 }
134 return "unknown device";
135 }
136
get_ft2232_vendorname(int ft2232_vid,int ft2232_type)137 static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type)
138 {
139 int i;
140 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
141 if ((devs_ft2232spi[i].device_id == ft2232_type)
142 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
143 return devs_ft2232spi[i].vendor_name;
144 }
145 return "unknown vendor";
146 }
147
send_buf(struct ftdi_context * ftdic,const unsigned char * buf,int size)148 static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf,
149 int size)
150 {
151 int r;
152 r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
153 if (r < 0) {
154 msg_perr("ftdi_write_data: %d, %s\n", r,
155 ftdi_get_error_string(ftdic));
156 return 1;
157 }
158 return 0;
159 }
160
get_buf(struct ftdi_context * ftdic,const unsigned char * buf,int size)161 static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf,
162 int size)
163 {
164 int r;
165
166 while (size > 0) {
167 r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
168 if (r < 0) {
169 msg_perr("ftdi_read_data: %d, %s\n", r,
170 ftdi_get_error_string(ftdic));
171 return 1;
172 }
173 buf += r;
174 size -= r;
175 }
176 return 0;
177 }
178
ft2232_shutdown(void * data)179 static int ft2232_shutdown(void *data)
180 {
181 struct ft2232_data *spi_data = (struct ft2232_data *) data;
182 struct ftdi_context *ftdic = &spi_data->ftdic_context;
183 unsigned char buf[3];
184 int ret = 0;
185
186 msg_pdbg("Releasing I/Os\n");
187 buf[0] = SET_BITS_LOW;
188 buf[1] = 0; /* Output byte ignored */
189 buf[2] = 0; /* Pin direction: all inputs */
190 if (send_buf(ftdic, buf, 3)) {
191 msg_perr("Unable to set pins back to inputs.\n");
192 ret = 1;
193 }
194
195 const int close_ret = ftdi_usb_close(ftdic);
196 if (close_ret < 0) {
197 msg_perr("Unable to close FTDI device: %d (%s)\n", close_ret,
198 ftdi_get_error_string(ftdic));
199 ret = 1;
200 }
201
202 free(spi_data);
203 return ret;
204 }
205
ft2232_spi_command_fits(const struct spi_command * cmd,size_t buffer_size)206 static bool ft2232_spi_command_fits(const struct spi_command *cmd, size_t buffer_size)
207 {
208 const size_t cmd_len = 3; /* same length for any ft2232 command */
209 return
210 /* commands for CS# assertion and de-assertion: */
211 cmd_len + cmd_len
212 /* commands for either a write, a read or both: */
213 + (cmd->writecnt && cmd->readcnt ? cmd_len + cmd_len : cmd_len)
214 /* payload (only writecnt; readcnt concerns another buffer): */
215 + cmd->writecnt
216 <= buffer_size;
217 }
218
219 /* Returns 0 upon success, a negative number upon errors. */
ft2232_spi_send_multicommand(const struct flashctx * flash,struct spi_command * cmds)220 static int ft2232_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
221 {
222 struct ft2232_data *spi_data = flash->mst->spi.data;
223 struct ftdi_context *ftdic = &spi_data->ftdic_context;
224 static unsigned char buf[FTDI_HW_BUFFER_SIZE];
225 size_t i = 0;
226 int ret = 0;
227
228 /*
229 * Minimize FTDI-calls by packing as many commands as possible together.
230 */
231 for (; cmds->writecnt || cmds->readcnt; cmds++) {
232
233 if (cmds->writecnt > 65536 || cmds->readcnt > 65536)
234 return SPI_INVALID_LENGTH;
235
236 if (!ft2232_spi_command_fits(cmds, FTDI_HW_BUFFER_SIZE - i)) {
237 msg_perr("Command does not fit\n");
238 return SPI_GENERIC_ERROR;
239 }
240
241 msg_pspew("Assert CS#\n");
242 buf[i++] = SET_BITS_LOW;
243 /* assert CS# pins, keep aux_bits, all other output pins stay low */
244 buf[i++] = spi_data->aux_bits;
245 buf[i++] = spi_data->pindir;
246
247 /* WREN, OP(PROGRAM, ERASE), ADDR, DATA */
248 if (cmds->writecnt) {
249 buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG;
250 buf[i++] = (cmds->writecnt - 1) & 0xff;
251 buf[i++] = ((cmds->writecnt - 1) >> 8) & 0xff;
252 memcpy(buf + i, cmds->writearr, cmds->writecnt);
253 i += cmds->writecnt;
254 }
255
256 /* An optional read command */
257 if (cmds->readcnt) {
258 buf[i++] = MPSSE_DO_READ;
259 buf[i++] = (cmds->readcnt - 1) & 0xff;
260 buf[i++] = ((cmds->readcnt - 1) >> 8) & 0xff;
261 }
262
263 /* Add final de-assert CS# */
264 msg_pspew("De-assert CS#\n");
265 buf[i++] = SET_BITS_LOW;
266 buf[i++] = spi_data->cs_bits | spi_data->aux_bits;
267 buf[i++] = spi_data->pindir;
268
269 /* continue if there is no read-cmd and further cmds exist */
270 if (!cmds->readcnt &&
271 ((cmds + 1)->writecnt || (cmds + 1)->readcnt) &&
272 ft2232_spi_command_fits((cmds + 1), FTDI_HW_BUFFER_SIZE - i)) {
273 continue;
274 }
275
276 ret = send_buf(ftdic, buf, i);
277 i = 0;
278 if (ret) {
279 msg_perr("send_buf failed: %i\n", ret);
280 break;
281 }
282
283 if (cmds->readcnt) {
284 ret = get_buf(ftdic, cmds->readarr, cmds->readcnt);
285 if (ret) {
286 msg_perr("get_buf failed: %i\n", ret);
287 break;
288 }
289 }
290 }
291 return ret ? -1 : 0;
292 }
293
294 static const struct spi_master spi_master_ft2232 = {
295 .features = SPI_MASTER_4BA,
296 .max_data_read = 64 * 1024,
297 .max_data_write = 256,
298 .multicommand = ft2232_spi_send_multicommand,
299 .read = default_spi_read,
300 .write_256 = default_spi_write_256,
301 .shutdown = ft2232_shutdown,
302 };
303
304 /* Returns 0 upon success, a negative number upon errors. */
ft2232_spi_init(const struct programmer_cfg * cfg)305 static int ft2232_spi_init(const struct programmer_cfg *cfg)
306 {
307 int ret = 0;
308 unsigned char buf[512];
309 int ft2232_vid = FTDI_VID;
310 int ft2232_type = FTDI_FT4232H_PID;
311 int channel_count = 4; /* Stores the number of channels of the device. */
312 enum ftdi_interface ft2232_interface = INTERFACE_A;
313 /*
314 * The 'H' chips can run with an internal clock of either 12 MHz or 60 MHz,
315 * but the non-H chips can only run at 12 MHz. We disable the divide-by-5
316 * prescaler on 'H' chips so they run at 60MHz.
317 */
318 bool clock_5x = true;
319 /* In addition to the prescaler mentioned above there is also another
320 * configurable one on all versions of the chips. Its divisor div can be
321 * set by a 16 bit value x according to the following formula:
322 * div = (1 + x) * 2 <-> x = div / 2 - 1
323 * Hence the expressible divisors are all even numbers between 2 and
324 * 2^17 (=131072) resulting in SCK frequencies of 6 MHz down to about
325 * 92 Hz for 12 MHz inputs and 30 MHz down to about 458 Hz for 60 MHz
326 * inputs.
327 */
328 uint32_t divisor = DEFAULT_DIVISOR;
329 int f;
330 char *arg, *arg2;
331 double mpsse_clk;
332
333 uint8_t cs_bits = 0x08;
334 uint8_t aux_bits = 0x00;
335 uint8_t pindir = 0x0b;
336 uint8_t aux_bits_high = 0x00;
337 uint8_t pindir_high = 0x00;
338 struct ftdi_context ftdic;
339 struct ft2232_data *spi_data;
340
341 arg = extract_programmer_param_str(cfg, "type");
342 if (arg) {
343 if (!strcasecmp(arg, "2232H")) {
344 ft2232_type = FTDI_FT2232H_PID;
345 channel_count = 2;
346 } else if (!strcasecmp(arg, "4232H")) {
347 ft2232_type = FTDI_FT4232H_PID;
348 channel_count = 4;
349 } else if (!strcasecmp(arg, "232H")) {
350 ft2232_type = FTDI_FT232H_PID;
351 channel_count = 1;
352 } else if (!strcasecmp(arg, "4233H")) {
353 ft2232_type = FTDI_FT4233H_PID;
354 channel_count = 4;
355 } else if (!strcasecmp(arg, "jtagkey")) {
356 ft2232_type = AMONTEC_JTAGKEY_PID;
357 channel_count = 2;
358 /* JTAGkey(2) needs to enable its output via Bit4 / GPIOL0
359 * value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low
360 * dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output */
361 cs_bits = 0x18;
362 pindir = 0x1b;
363 } else if (!strcasecmp(arg, "picotap")) {
364 ft2232_vid = GOEPEL_VID;
365 ft2232_type = GOEPEL_PICOTAP_PID;
366 channel_count = 2;
367 } else if (!strcasecmp(arg, "tumpa")) {
368 /* Interface A is SPI1, B is SPI2. */
369 ft2232_type = TIAO_TUMPA_PID;
370 channel_count = 2;
371 } else if (!strcasecmp(arg, "tumpalite")) {
372 /* Only one channel is used on lite edition */
373 ft2232_type = TIAO_TUMPA_LITE_PID;
374 channel_count = 1;
375 } else if (!strcasecmp(arg, "busblaster")) {
376 /* In its default configuration it is a jtagkey clone */
377 ft2232_type = FTDI_FT2232H_PID;
378 channel_count = 2;
379 cs_bits = 0x18;
380 pindir = 0x1b;
381 } else if (!strcasecmp(arg, "openmoko")) {
382 ft2232_vid = FIC_VID;
383 ft2232_type = OPENMOKO_DBGBOARD_PID;
384 channel_count = 2;
385 } else if (!strcasecmp(arg, "arm-usb-ocd")) {
386 ft2232_vid = OLIMEX_VID;
387 ft2232_type = OLIMEX_ARM_OCD_PID;
388 channel_count = 2;
389 /* arm-usb-ocd(-h) has an output buffer that needs to be enabled by pulling ADBUS4 low.
390 * value: 0x08 #OE=low, CS=high, DI=low, DO=low, SK=low
391 * dir: 0x1b #OE=output, CS=output, DI=input, DO=output, SK=output */
392 cs_bits = 0x08;
393 pindir = 0x1b;
394 } else if (!strcasecmp(arg, "arm-usb-tiny")) {
395 ft2232_vid = OLIMEX_VID;
396 ft2232_type = OLIMEX_ARM_TINY_PID;
397 channel_count = 2;
398 } else if (!strcasecmp(arg, "arm-usb-ocd-h")) {
399 ft2232_vid = OLIMEX_VID;
400 ft2232_type = OLIMEX_ARM_OCD_H_PID;
401 channel_count = 2;
402 /* See arm-usb-ocd */
403 cs_bits = 0x08;
404 pindir = 0x1b;
405 } else if (!strcasecmp(arg, "arm-usb-tiny-h")) {
406 ft2232_vid = OLIMEX_VID;
407 ft2232_type = OLIMEX_ARM_TINY_H_PID;
408 channel_count = 2;
409 } else if (!strcasecmp(arg, "google-servo")) {
410 ft2232_vid = GOOGLE_VID;
411 ft2232_type = GOOGLE_SERVO_PID;
412 } else if (!strcasecmp(arg, "google-servo-v2")) {
413 ft2232_vid = GOOGLE_VID;
414 ft2232_type = GOOGLE_SERVO_V2_PID1;
415 /* Default divisor is too fast, and chip ID fails */
416 divisor = 6;
417 } else if (!strcasecmp(arg, "google-servo-v2-legacy")) {
418 ft2232_vid = GOOGLE_VID;
419 ft2232_type = GOOGLE_SERVO_V2_PID0;
420 } else if (!strcasecmp(arg, "flyswatter")) {
421 ft2232_type = FTDI_FT2232H_PID;
422 channel_count = 2;
423 /* Flyswatter and Flyswatter-2 require GPIO bits 0x80
424 * and 0x40 to be driven low to enable output buffers */
425 pindir = 0xcb;
426 } else if (!strcasecmp(arg, "kt-link")) {
427 ft2232_type = KT_LINK_PID;
428 /* port B is used as uart */
429 channel_count = 1;
430 /* Set GPIOL1 output high - route TMS and TDO through multiplexers */
431 aux_bits = 0x20;
432 pindir = 0x2b;
433 /* Set GPIOH4 output low - enable TMS output buffer */
434 /* Set GPIOH5 output low - enable TDI output buffer */
435 /* Set GPIOH6 output low - enable TCK output buffer */
436 pindir_high = 0x70;
437 } else {
438 msg_perr("Error: Invalid device type specified.\n");
439 free(arg);
440 return -1;
441 }
442 }
443 free(arg);
444
445 /* Remember reserved pins before pindir gets modified. */
446 const uint8_t rsv_bits = pindir & 0xf0;
447
448 arg = extract_programmer_param_str(cfg, "port");
449 if (arg) {
450 switch (toupper((unsigned char)*arg)) {
451 case 'A':
452 ft2232_interface = INTERFACE_A;
453 break;
454 case 'B':
455 ft2232_interface = INTERFACE_B;
456 if (channel_count < 2)
457 channel_count = -1;
458 break;
459 case 'C':
460 ft2232_interface = INTERFACE_C;
461 if (channel_count < 3)
462 channel_count = -1;
463 break;
464 case 'D':
465 ft2232_interface = INTERFACE_D;
466 if (channel_count < 4)
467 channel_count = -1;
468 break;
469 default:
470 channel_count = -1;
471 break;
472 }
473 if (channel_count < 0 || strlen(arg) != 1) {
474 msg_perr("Error: Invalid channel/port/interface specified: \"%s\".\n", arg);
475 free(arg);
476 return -2;
477 }
478 }
479 free(arg);
480
481 arg = extract_programmer_param_str(cfg, "divisor");
482 if (arg && strlen(arg)) {
483 unsigned int temp = 0;
484 char *endptr;
485 temp = strtoul(arg, &endptr, 10);
486 if (*endptr || temp < 2 || temp > 131072 || temp & 0x1) {
487 msg_perr("Error: Invalid SPI frequency divisor specified: \"%s\".\n"
488 "Valid are even values between 2 and 131072.\n", arg);
489 free(arg);
490 return -2;
491 }
492 divisor = (uint32_t)temp;
493 }
494 free(arg);
495
496 bool csgpiol_set = false;
497 arg = extract_programmer_param_str(cfg, "csgpiol");
498 if (arg) {
499 csgpiol_set = true;
500 msg_pwarn("Deprecation warning: `csgpiol` is deprecated and will be removed "
501 "in the future.\nUse `gpiolX=C` instead.\n");
502
503 char *endptr;
504 unsigned int temp = strtoul(arg, &endptr, 10);
505 if (*endptr || endptr == arg || temp > 3) {
506 msg_perr("Error: Invalid GPIOL specified: \"%s\".\n"
507 "Valid values are between 0 and 3.\n", arg);
508 free(arg);
509 return -2;
510 }
511
512 unsigned int pin = temp + 4;
513 if (rsv_bits & 1 << pin) {
514 msg_perr("Error: Invalid GPIOL specified: \"%s\".\n"
515 "The pin is reserved on this programmer.\n",
516 arg);
517 free(arg);
518 return -2;
519 }
520
521 cs_bits |= 1 << pin;
522 pindir |= 1 << pin;
523 }
524 free(arg);
525
526 /* gpiolX */
527 for (int pin = 0; pin < 4; pin++) {
528 char gpiol_param[7];
529 snprintf(gpiol_param, sizeof(gpiol_param), "gpiol%d", pin);
530 arg = extract_programmer_param_str(cfg, gpiol_param);
531
532 if (!arg)
533 continue;
534
535 if (csgpiol_set) {
536 msg_perr("Error: `csgpiol` and `gpiolX` are mutually exclusive.\n"
537 "Since `csgpiol` is deprecated and will be removed in the "
538 "future, use of `gpiolX=C` is recommended.\n");
539 free(arg);
540 return -2;
541 }
542
543 uint8_t bit = 1 << (pin + 4);
544 if (rsv_bits & bit) {
545 msg_perr("Error: Invalid GPIOL specified: \"gpiol%d=%s\".\n"
546 "Pin GPIOL%i is reserved on this programmer.\n",
547 pin, arg, pin);
548 free(arg);
549 return -2;
550 }
551
552 if (strlen(arg) != 1)
553 goto format_error;
554
555 switch (toupper(arg[0])) {
556 case 'H':
557 aux_bits |= bit;
558 pindir |= bit;
559 break;
560 case 'L':
561 pindir |= bit;
562 break;
563 case 'C':
564 cs_bits |= bit;
565 pindir |= bit;
566 break;
567 default:
568 goto format_error;
569 }
570
571 free(arg);
572 continue;
573
574 format_error:
575 msg_perr("Error: Invalid GPIOL specified: \"gpiol%d=%s\".\n"
576 "Valid values are H, L and C.\n"
577 " H - Set GPIOL output high\n"
578 " L - Set GPIOL output low\n"
579 " C - Use GPIOL as additional CS# output\n",
580 pin, arg);
581
582 free(arg);
583 return -2;
584 }
585
586 msg_pdbg("Using device type %s %s ",
587 get_ft2232_vendorname(ft2232_vid, ft2232_type),
588 get_ft2232_devicename(ft2232_vid, ft2232_type));
589 msg_pdbg("channel %s.\n",
590 (ft2232_interface == INTERFACE_A) ? "A" :
591 (ft2232_interface == INTERFACE_B) ? "B" :
592 (ft2232_interface == INTERFACE_C) ? "C" : "D");
593
594 if (ftdi_init(&ftdic) < 0) {
595 msg_perr("ftdi_init failed.\n");
596 return -3;
597 }
598
599 if (ftdi_set_interface(&ftdic, ft2232_interface) < 0) {
600 msg_perr("Unable to select channel (%s).\n", ftdi_get_error_string(&ftdic));
601 }
602
603 arg = extract_programmer_param_str(cfg, "serial");
604 arg2 = extract_programmer_param_str(cfg, "description");
605
606 f = ftdi_usb_open_desc(&ftdic, ft2232_vid, ft2232_type, arg2, arg);
607
608 free(arg);
609 free(arg2);
610
611 if (f < 0 && f != -5) {
612 msg_perr("Unable to open FTDI device: %d (%s)\n", f,
613 ftdi_get_error_string(&ftdic));
614 return -4;
615 }
616
617 if (ftdic.type != TYPE_2232H && ftdic.type != TYPE_4232H && ftdic.type != TYPE_232H) {
618 msg_pdbg("FTDI chip type %d is not high-speed.\n", ftdic.type);
619 clock_5x = false;
620 }
621
622 if (ftdi_usb_reset(&ftdic) < 0) {
623 msg_perr("Unable to reset FTDI device (%s).\n", ftdi_get_error_string(&ftdic));
624 }
625
626 if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
627 msg_perr("Unable to set latency timer (%s).\n", ftdi_get_error_string(&ftdic));
628 }
629
630 if (ftdi_set_bitmode(&ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
631 msg_perr("Unable to set bitmode to SPI (%s).\n", ftdi_get_error_string(&ftdic));
632 }
633
634 if (clock_5x) {
635 msg_pdbg("Disable divide-by-5 front stage\n");
636 buf[0] = DIS_DIV_5;
637 if (send_buf(&ftdic, buf, 1)) {
638 ret = -5;
639 goto ftdi_err;
640 }
641 mpsse_clk = 60.0;
642 } else {
643 mpsse_clk = 12.0;
644 }
645
646 msg_pdbg("Set clock divisor\n");
647 buf[0] = TCK_DIVISOR;
648 buf[1] = (divisor / 2 - 1) & 0xff;
649 buf[2] = ((divisor / 2 - 1) >> 8) & 0xff;
650 if (send_buf(&ftdic, buf, 3)) {
651 ret = -6;
652 goto ftdi_err;
653 }
654
655 msg_pdbg("MPSSE clock: %f MHz, divisor: %u, SPI clock: %f MHz\n",
656 mpsse_clk, divisor, (double)(mpsse_clk / divisor));
657
658 /* Disconnect TDI/DO to TDO/DI for loopback. */
659 msg_pdbg("No loopback of TDI/DO TDO/DI\n");
660 buf[0] = LOOPBACK_END;
661 if (send_buf(&ftdic, buf, 1)) {
662 ret = -7;
663 goto ftdi_err;
664 }
665
666 msg_pdbg("Set data bits\n");
667 buf[0] = SET_BITS_LOW;
668 buf[1] = cs_bits | aux_bits;
669 buf[2] = pindir;
670 if (send_buf(&ftdic, buf, 3)) {
671 ret = -8;
672 goto ftdi_err;
673 }
674
675 if (pindir_high) {
676 msg_pdbg("Set data bits HighByte\n");
677 buf[0] = SET_BITS_HIGH;
678 buf[1] = aux_bits_high;
679 buf[2] = pindir_high;
680 if (send_buf(&ftdic, buf, 3)) {
681 ret = -8;
682 goto ftdi_err;
683 }
684 }
685
686 spi_data = calloc(1, sizeof(*spi_data));
687 if (!spi_data) {
688 msg_perr("Unable to allocate space for SPI master data\n");
689 return SPI_GENERIC_ERROR;
690 }
691 spi_data->cs_bits = cs_bits;
692 spi_data->aux_bits = aux_bits;
693 spi_data->pindir = pindir;
694 spi_data->ftdic_context = ftdic;
695
696 return register_spi_master(&spi_master_ft2232, spi_data);
697
698 ftdi_err:
699 if ((f = ftdi_usb_close(&ftdic)) < 0) {
700 msg_perr("Unable to close FTDI device: %d (%s)\n", f,
701 ftdi_get_error_string(&ftdic));
702 }
703 return ret;
704 }
705
706 const struct programmer_entry programmer_ft2232_spi = {
707 .name = "ft2232_spi",
708 .type = USB,
709 .devs.dev = devs_ft2232spi,
710 .init = ft2232_spi_init,
711 };
712