blob: 6072cd26eebb5c7d755123314103de2891d3b8ca [file] [log] [blame]
Paul Fox05dfbe62009-06-16 21:08:06 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
Carl-Daniel Hailfinger5824fbf2010-05-21 23:09:42 +00005 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
Paul Fox05dfbe62009-06-16 21:08:06 +00006 *
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.
Paul Fox05dfbe62009-06-16 21:08:06 +000015 */
16
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000017#if CONFIG_FT2232_SPI == 1
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +000018
Paul Fox05dfbe62009-06-16 21:08:06 +000019#include <stdio.h>
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000020#include <strings.h>
Paul Fox05dfbe62009-06-16 21:08:06 +000021#include <string.h>
22#include <stdlib.h>
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000023#include <ctype.h>
Paul Fox05dfbe62009-06-16 21:08:06 +000024#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000025#include "programmer.h"
Paul Fox05dfbe62009-06-16 21:08:06 +000026#include "spi.h"
Paul Fox05dfbe62009-06-16 21:08:06 +000027#include <ftdi.h>
28
Ilya A. Volynets-Evenbakh2c714ab2012-09-26 00:47:09 +000029/* This is not defined in libftdi.h <0.20 (c7e4c09e68cfa6f5e112334aa1b3bb23401c8dc7 to be exact).
Angel Pons236c4ab2021-04-17 17:07:34 +020030 * Some tests indicate that this is the only change that it is needed to support the FT232H in flashrom. */
Ilya A. Volynets-Evenbakh2c714ab2012-09-26 00:47:09 +000031#if !defined(HAVE_FT232H)
32#define TYPE_232H 6
33#endif
34
Uwe Hermann836b26a2011-10-14 20:33:14 +000035/* Please keep sorted by vendor ID, then device ID. */
36
Uwe Hermann70145292010-07-29 19:57:55 +000037#define FTDI_VID 0x0403
38#define FTDI_FT2232H_PID 0x6010
39#define FTDI_FT4232H_PID 0x6011
Ilya A. Volynets-Evenbakh2c714ab2012-09-26 00:47:09 +000040#define FTDI_FT232H_PID 0x6014
Uwe Hermann836b26a2011-10-14 20:33:14 +000041#define TIAO_TUMPA_PID 0x8a98
Stefan Taunerb66ed842014-04-27 05:07:35 +000042#define TIAO_TUMPA_LITE_PID 0x8a99
Uwe Hermann70145292010-07-29 19:57:55 +000043#define AMONTEC_JTAGKEY_PID 0xCFF8
Jörg Fischer6529b9f2010-07-29 15:54:53 +000044
Samir Ibradžić7189a5f2011-10-20 23:14:10 +000045#define GOEPEL_VID 0x096C
46#define GOEPEL_PICOTAP_PID 0x1449
47
Alex Badeacaf2d422010-11-10 03:26:57 +000048#define FIC_VID 0x1457
49#define OPENMOKO_DBGBOARD_PID 0x5118
50
Pete Batardc0207062011-06-11 12:21:37 +000051#define OLIMEX_VID 0x15BA
52#define OLIMEX_ARM_OCD_PID 0x0003
53#define OLIMEX_ARM_TINY_PID 0x0004
54#define OLIMEX_ARM_OCD_H_PID 0x002B
55#define OLIMEX_ARM_TINY_H_PID 0x002A
56
Todd Broch6800c952016-02-14 15:46:00 +000057#define GOOGLE_VID 0x18D1
58#define GOOGLE_SERVO_PID 0x5001
59#define GOOGLE_SERVO_V2_PID0 0x5002
60#define GOOGLE_SERVO_V2_PID1 0x5003
61
Stefan Tauner4b24a2d2012-12-27 18:40:36 +000062const struct dev_entry devs_ft2232spi[] = {
Uwe Hermann70145292010-07-29 19:57:55 +000063 {FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"},
64 {FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"},
Ilya A. Volynets-Evenbakh2c714ab2012-09-26 00:47:09 +000065 {FTDI_VID, FTDI_FT232H_PID, OK, "FTDI", "FT232H"},
Uwe Hermann836b26a2011-10-14 20:33:14 +000066 {FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
Stefan Taunerb66ed842014-04-27 05:07:35 +000067 {FTDI_VID, TIAO_TUMPA_LITE_PID, OK, "TIAO", "USB Multi-Protocol Adapter Lite"},
Uwe Hermann70145292010-07-29 19:57:55 +000068 {FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
Samir Ibradžić7189a5f2011-10-20 23:14:10 +000069 {GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"},
Todd Broch6800c952016-02-14 15:46:00 +000070 {GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"},
71 {GOOGLE_VID, GOOGLE_SERVO_V2_PID0, OK, "Google", "Servo V2 Legacy"},
72 {GOOGLE_VID, GOOGLE_SERVO_V2_PID1, OK, "Google", "Servo V2"},
Stefan Tauner3e515e22012-09-26 00:48:16 +000073 {FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC", "OpenMoko Neo1973 Debug board (V2+)"},
Stefan Tauner74dc73f2015-03-01 22:04:38 +000074 {OLIMEX_VID, OLIMEX_ARM_OCD_PID, OK, "Olimex", "ARM-USB-OCD"},
Pete Batardc0207062011-06-11 12:21:37 +000075 {OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"},
Stefan Tauner6697f712014-08-06 15:09:15 +000076 {OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, OK, "Olimex", "ARM-USB-OCD-H"},
Stefan Taunerc2eec2c2014-05-03 21:33:01 +000077 {OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, OK, "Olimex", "ARM-USB-TINY-H"},
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +000078
79 {0},
Jörg Fischer6529b9f2010-07-29 15:54:53 +000080};
81
Samir Ibradžićb482c6d2012-05-15 22:58:19 +000082#define DEFAULT_DIVISOR 2
Paul Fox05dfbe62009-06-16 21:08:06 +000083
Uwe Hermannc67d0372009-10-01 18:40:02 +000084#define BITMODE_BITBANG_NORMAL 1
85#define BITMODE_BITBANG_SPI 2
Paul Fox05dfbe62009-06-16 21:08:06 +000086
Alan Green18007962021-01-21 09:48:25 +110087/* The variables pinlvl and pindir store the values for the "set data bits low byte" MPSSE command that
Antony Pavlovb4dd40c2015-01-25 03:52:47 +000088 * sets the initial state and the direction of the I/O pins. The pin offsets are as follows:
sibradzicba6575d2020-02-04 18:24:58 +090089 * TCK/SK is bit 0.
90 * TDI/DO is bit 1.
91 * TDO/DI is bit 2.
92 * TMS/CS is bit 3.
93 * GPIOL0 is bit 4.
94 * GPIOL1 is bit 5.
95 * GPIOL2 is bit 6.
96 * GPIOL3 is bit 7.
97 *
98 * The pin signal direction bit offsets follow the same order; 0 means that
99 * pin at the matching bit index is an input, 1 means pin is an output.
Antony Pavlovb4dd40c2015-01-25 03:52:47 +0000100 *
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000101 * The default values (set below in ft2232_spi_init) are used for most devices:
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000102 * value: 0x08 CS=high, DI=low, DO=low, SK=low
103 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000104 */
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000105struct ft2232_data {
106 uint8_t pinlvl;
107 uint8_t pindir;
108 struct ftdi_context ftdic_context;
109};
Paul Fox05dfbe62009-06-16 21:08:06 +0000110
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000111static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
112{
113 int i;
Uwe Hermann70145292010-07-29 19:57:55 +0000114 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000115 if ((devs_ft2232spi[i].device_id == ft2232_type)
116 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000117 return devs_ft2232spi[i].device_name;
118 }
119 return "unknown device";
120}
121
122static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type)
123{
124 int i;
Uwe Hermann70145292010-07-29 19:57:55 +0000125 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000126 if ((devs_ft2232spi[i].device_id == ft2232_type)
127 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
Uwe Hermann70145292010-07-29 19:57:55 +0000128 return devs_ft2232spi[i].vendor_name;
129 }
130 return "unknown vendor";
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000131}
132
Uwe Hermann70145292010-07-29 19:57:55 +0000133static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf,
134 int size)
Paul Fox05dfbe62009-06-16 21:08:06 +0000135{
136 int r;
137 r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
138 if (r < 0) {
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000139 msg_perr("ftdi_write_data: %d, %s\n", r,
140 ftdi_get_error_string(ftdic));
Paul Fox05dfbe62009-06-16 21:08:06 +0000141 return 1;
142 }
143 return 0;
144}
145
Uwe Hermann70145292010-07-29 19:57:55 +0000146static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf,
147 int size)
Paul Fox05dfbe62009-06-16 21:08:06 +0000148{
149 int r;
Alex Badeab9556e02010-11-10 03:10:41 +0000150
151 while (size > 0) {
152 r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
153 if (r < 0) {
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000154 msg_perr("ftdi_read_data: %d, %s\n", r,
155 ftdi_get_error_string(ftdic));
Alex Badeab9556e02010-11-10 03:10:41 +0000156 return 1;
157 }
158 buf += r;
159 size -= r;
Paul Fox05dfbe62009-06-16 21:08:06 +0000160 }
161 return 0;
162}
163
Alan Green25a5bce2021-01-08 09:36:45 +1100164static int ft2232_shutdown(void *data)
165{
166 int f;
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000167 struct ft2232_data *spi_data = (struct ft2232_data *) data;
168 struct ftdi_context *ftdic = &spi_data->ftdic_context;
Alan Green25a5bce2021-01-08 09:36:45 +1100169 unsigned char buf[3];
170
171 msg_pdbg("Releasing I/Os\n");
172 buf[0] = SET_BITS_LOW;
173 buf[1] = 0; /* Output byte ignored */
174 buf[2] = 0; /* Pin direction: all inputs */
175 if (send_buf(ftdic, buf, 3)) {
176 msg_perr("Unable to set pins back inputs: (%s)\n",
177 ftdi_get_error_string(ftdic));
178 }
179
180 if ((f = ftdi_usb_close(ftdic)) < 0) {
181 msg_perr("Unable to close FTDI device: %d (%s)\n", f,
182 ftdi_get_error_string(ftdic));
183 return f;
184 }
185
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000186 free(spi_data);
Alan Green25a5bce2021-01-08 09:36:45 +1100187 return 0;
188}
189
Anastasia Klimchukf1391c72021-02-15 15:04:20 +1100190/* Returns 0 upon success, a negative number upon errors. */
Edward O'Callaghane4ddc362020-04-12 17:27:53 +1000191static int ft2232_spi_send_command(const struct flashctx *flash,
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000192 unsigned int writecnt, unsigned int readcnt,
193 const unsigned char *writearr,
Anastasia Klimchukf1391c72021-02-15 15:04:20 +1100194 unsigned char *readarr)
195{
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000196 struct ft2232_data *spi_data = flash->mst->spi.data;
197 struct ftdi_context *ftdic = &spi_data->ftdic_context;
Anastasia Klimchukf1391c72021-02-15 15:04:20 +1100198 static unsigned char *buf = NULL;
199 /* failed is special. We use bitwise ops, but it is essentially bool. */
200 int i = 0, ret = 0, failed = 0;
201 size_t bufsize;
202 static size_t oldbufsize = 0;
203
204 if (writecnt > 65536 || readcnt > 65536)
205 return SPI_INVALID_LENGTH;
206
207 /* buf is not used for the response from the chip. */
208 bufsize = max(writecnt + 9, 260 + 9);
209 /* Never shrink. realloc() calls are expensive. */
210 if (!buf || bufsize > oldbufsize) {
211 buf = realloc(buf, bufsize);
212 if (!buf) {
213 msg_perr("Out of memory!\n");
214 /* TODO: What to do with buf? */
215 return SPI_GENERIC_ERROR;
216 }
217 oldbufsize = bufsize;
218 }
219
220 /*
221 * Minimize USB transfers by packing as many commands as possible
222 * together. If we're not expecting to read, we can assert CS#, write,
223 * and deassert CS# all in one shot. If reading, we do three separate
224 * operations.
225 */
226 msg_pspew("Assert CS#\n");
227 buf[i++] = SET_BITS_LOW;
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000228 buf[i++] = ~ 0x08 & spi_data->pinlvl; /* assert CS (3rd) bit only */
229 buf[i++] = spi_data->pindir;
Anastasia Klimchukf1391c72021-02-15 15:04:20 +1100230
231 if (writecnt) {
232 buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG;
233 buf[i++] = (writecnt - 1) & 0xff;
234 buf[i++] = ((writecnt - 1) >> 8) & 0xff;
235 memcpy(buf + i, writearr, writecnt);
236 i += writecnt;
237 }
238
239 /*
240 * Optionally terminate this batch of commands with a
241 * read command, then do the fetch of the results.
242 */
243 if (readcnt) {
244 buf[i++] = MPSSE_DO_READ;
245 buf[i++] = (readcnt - 1) & 0xff;
246 buf[i++] = ((readcnt - 1) >> 8) & 0xff;
247 ret = send_buf(ftdic, buf, i);
248 failed = ret;
249 /* We can't abort here, we still have to deassert CS#. */
250 if (ret)
251 msg_perr("send_buf failed before read: %i\n", ret);
252 i = 0;
253 if (ret == 0) {
254 /*
255 * FIXME: This is unreliable. There's no guarantee that
256 * we read the response directly after sending the read
257 * command. We may be scheduled out etc.
258 */
259 ret = get_buf(ftdic, readarr, readcnt);
260 failed |= ret;
261 /* We can't abort here either. */
262 if (ret)
263 msg_perr("get_buf failed: %i\n", ret);
264 }
265 }
266
267 msg_pspew("De-assert CS#\n");
268 buf[i++] = SET_BITS_LOW;
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000269 buf[i++] = spi_data->pinlvl;
270 buf[i++] = spi_data->pindir;
Anastasia Klimchukf1391c72021-02-15 15:04:20 +1100271 ret = send_buf(ftdic, buf, i);
272 failed |= ret;
273 if (ret)
274 msg_perr("send_buf failed at end: %i\n", ret);
275
276 return failed ? -1 : 0;
277}
Michael Karcherb9dbe482011-05-11 17:07:07 +0000278
Nico Huber90739d12021-05-11 17:53:34 +0200279static const struct spi_master spi_master_ft2232 = {
Nico Huber1cf407b2017-11-10 20:18:23 +0100280 .features = SPI_MASTER_4BA,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000281 .max_data_read = 64 * 1024,
282 .max_data_write = 256,
283 .command = ft2232_spi_send_command,
284 .multicommand = default_spi_send_multicommand,
285 .read = default_spi_read,
286 .write_256 = default_spi_write_256,
Nico Huber7bca1262012-06-15 22:28:12 +0000287 .write_aai = default_spi_write_aai,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000288};
289
Uwe Hermann274a20d2011-07-21 09:18:18 +0000290/* Returns 0 upon success, a negative number upon errors. */
Paul Fox05dfbe62009-06-16 21:08:06 +0000291int ft2232_spi_init(void)
292{
Samir Ibradžićb482c6d2012-05-15 22:58:19 +0000293 int ret = 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000294 unsigned char buf[512];
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000295 int ft2232_vid = FTDI_VID;
296 int ft2232_type = FTDI_FT4232H_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000297 int channel_count = 4; /* Stores the number of channels of the device. */
298 enum ftdi_interface ft2232_interface = INTERFACE_A;
Samir Ibradžićb482c6d2012-05-15 22:58:19 +0000299 /*
300 * The 'H' chips can run with an internal clock of either 12 MHz or 60 MHz,
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000301 * but the non-H chips can only run at 12 MHz. We disable the divide-by-5
302 * prescaler on 'H' chips so they run at 60MHz.
Samir Ibradžićb482c6d2012-05-15 22:58:19 +0000303 */
304 uint8_t clock_5x = 1;
305 /* In addition to the prescaler mentioned above there is also another
306 * configurable one on all versions of the chips. Its divisor div can be
307 * set by a 16 bit value x according to the following formula:
308 * div = (1 + x) * 2 <-> x = div / 2 - 1
309 * Hence the expressible divisors are all even numbers between 2 and
310 * 2^17 (=131072) resulting in SCK frequencies of 6 MHz down to about
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000311 * 92 Hz for 12 MHz inputs and 30 MHz down to about 458 Hz for 60 MHz
312 * inputs.
Samir Ibradžićb482c6d2012-05-15 22:58:19 +0000313 */
314 uint32_t divisor = DEFAULT_DIVISOR;
315 int f;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000316 char *arg;
Alex Badea0b94d052010-11-10 03:18:41 +0000317 double mpsse_clk;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000318
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000319 uint8_t pinlvl = 0x08;
320 uint8_t pindir = 0x0b;
321 struct ftdi_context ftdic;
322 struct ft2232_data *spi_data;
323
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000324 arg = extract_programmer_param("type");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000325 if (arg) {
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000326 if (!strcasecmp(arg, "2232H")) {
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000327 ft2232_type = FTDI_FT2232H_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000328 channel_count = 2;
329 } else if (!strcasecmp(arg, "4232H")) {
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000330 ft2232_type = FTDI_FT4232H_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000331 channel_count = 4;
Ilya A. Volynets-Evenbakh2c714ab2012-09-26 00:47:09 +0000332 } else if (!strcasecmp(arg, "232H")) {
333 ft2232_type = FTDI_FT232H_PID;
334 channel_count = 1;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000335 } else if (!strcasecmp(arg, "jtagkey")) {
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000336 ft2232_type = AMONTEC_JTAGKEY_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000337 channel_count = 2;
Antony Pavlovb4dd40c2015-01-25 03:52:47 +0000338 /* JTAGkey(2) needs to enable its output via Bit4 / GPIOL0
339 * value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low
340 * dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output */
Alan Green18007962021-01-21 09:48:25 +1100341 pinlvl = 0x18;
Uwe Hermann70145292010-07-29 19:57:55 +0000342 pindir = 0x1b;
Samir Ibradžić7189a5f2011-10-20 23:14:10 +0000343 } else if (!strcasecmp(arg, "picotap")) {
344 ft2232_vid = GOEPEL_VID;
345 ft2232_type = GOEPEL_PICOTAP_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000346 channel_count = 2;
Uwe Hermann836b26a2011-10-14 20:33:14 +0000347 } else if (!strcasecmp(arg, "tumpa")) {
348 /* Interface A is SPI1, B is SPI2. */
349 ft2232_type = TIAO_TUMPA_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000350 channel_count = 2;
Stefan Taunerb66ed842014-04-27 05:07:35 +0000351 } else if (!strcasecmp(arg, "tumpalite")) {
352 /* Only one channel is used on lite edition */
353 ft2232_type = TIAO_TUMPA_LITE_PID;
354 channel_count = 1;
Steve Markgraf0528b7f2011-08-12 01:19:32 +0000355 } else if (!strcasecmp(arg, "busblaster")) {
356 /* In its default configuration it is a jtagkey clone */
357 ft2232_type = FTDI_FT2232H_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000358 channel_count = 2;
Alan Green18007962021-01-21 09:48:25 +1100359 pinlvl = 0x18;
Steve Markgraf0528b7f2011-08-12 01:19:32 +0000360 pindir = 0x1b;
Alex Badeacaf2d422010-11-10 03:26:57 +0000361 } else if (!strcasecmp(arg, "openmoko")) {
362 ft2232_vid = FIC_VID;
363 ft2232_type = OPENMOKO_DBGBOARD_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000364 channel_count = 2;
Pete Batardc0207062011-06-11 12:21:37 +0000365 } else if (!strcasecmp(arg, "arm-usb-ocd")) {
366 ft2232_vid = OLIMEX_VID;
367 ft2232_type = OLIMEX_ARM_OCD_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000368 channel_count = 2;
Antony Pavlovb4dd40c2015-01-25 03:52:47 +0000369 /* arm-usb-ocd(-h) has an output buffer that needs to be enabled by pulling ADBUS4 low.
370 * value: 0x08 #OE=low, CS=high, DI=low, DO=low, SK=low
371 * dir: 0x1b #OE=output, CS=output, DI=input, DO=output, SK=output */
Alan Green18007962021-01-21 09:48:25 +1100372 pinlvl = 0x08;
Paul Fertser3cf335e2011-12-20 02:08:14 +0000373 pindir = 0x1b;
Pete Batardc0207062011-06-11 12:21:37 +0000374 } else if (!strcasecmp(arg, "arm-usb-tiny")) {
375 ft2232_vid = OLIMEX_VID;
376 ft2232_type = OLIMEX_ARM_TINY_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000377 channel_count = 2;
Pete Batardc0207062011-06-11 12:21:37 +0000378 } else if (!strcasecmp(arg, "arm-usb-ocd-h")) {
379 ft2232_vid = OLIMEX_VID;
380 ft2232_type = OLIMEX_ARM_OCD_H_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000381 channel_count = 2;
Antony Pavlovb4dd40c2015-01-25 03:52:47 +0000382 /* See arm-usb-ocd */
Alan Green18007962021-01-21 09:48:25 +1100383 pinlvl = 0x08;
Paul Fertser3cf335e2011-12-20 02:08:14 +0000384 pindir = 0x1b;
Pete Batardc0207062011-06-11 12:21:37 +0000385 } else if (!strcasecmp(arg, "arm-usb-tiny-h")) {
386 ft2232_vid = OLIMEX_VID;
387 ft2232_type = OLIMEX_ARM_TINY_H_PID;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000388 channel_count = 2;
Todd Broch6800c952016-02-14 15:46:00 +0000389 } else if (!strcasecmp(arg, "google-servo")) {
390 ft2232_vid = GOOGLE_VID;
391 ft2232_type = GOOGLE_SERVO_PID;
392 } else if (!strcasecmp(arg, "google-servo-v2")) {
393 ft2232_vid = GOOGLE_VID;
394 ft2232_type = GOOGLE_SERVO_V2_PID1;
395 /* Default divisor is too fast, and chip ID fails */
396 divisor = 6;
397 } else if (!strcasecmp(arg, "google-servo-v2-legacy")) {
398 ft2232_vid = GOOGLE_VID;
399 ft2232_type = GOOGLE_SERVO_V2_PID0;
Russ Dill7bd31a42019-10-30 00:40:43 -0700400 } else if (!strcasecmp(arg, "flyswatter")) {
401 ft2232_type = FTDI_FT2232H_PID;
402 channel_count = 2;
403 /* Flyswatter and Flyswatter-2 require GPIO bits 0x80
404 * and 0x40 to be driven low to enable output buffers */
405 pindir = 0xcb;
Alex Badeacaf2d422010-11-10 03:26:57 +0000406 } else {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000407 msg_perr("Error: Invalid device type specified.\n");
408 free(arg);
Uwe Hermann274a20d2011-07-21 09:18:18 +0000409 return -1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000410 }
411 }
412 free(arg);
Stefan Tauner3e515e22012-09-26 00:48:16 +0000413
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000414 arg = extract_programmer_param("port");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000415 if (arg) {
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000416 switch (toupper((unsigned char)*arg)) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000417 case 'A':
418 ft2232_interface = INTERFACE_A;
419 break;
420 case 'B':
421 ft2232_interface = INTERFACE_B;
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000422 if (channel_count < 2)
423 channel_count = -1;
424 break;
425 case 'C':
426 ft2232_interface = INTERFACE_C;
427 if (channel_count < 3)
428 channel_count = -1;
429 break;
430 case 'D':
431 ft2232_interface = INTERFACE_D;
432 if (channel_count < 4)
433 channel_count = -1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000434 break;
435 default:
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000436 channel_count = -1;
437 break;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000438 }
Stefan Taunerd7d423b2012-10-20 09:13:16 +0000439 if (channel_count < 0 || strlen(arg) != 1) {
440 msg_perr("Error: Invalid channel/port/interface specified: \"%s\".\n", arg);
441 free(arg);
442 return -2;
443 }
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000444 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000445 free(arg);
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000446
Samir Ibradžićb482c6d2012-05-15 22:58:19 +0000447 arg = extract_programmer_param("divisor");
448 if (arg && strlen(arg)) {
449 unsigned int temp = 0;
450 char *endptr;
451 temp = strtoul(arg, &endptr, 10);
452 if (*endptr || temp < 2 || temp > 131072 || temp & 0x1) {
453 msg_perr("Error: Invalid SPI frequency divisor specified: \"%s\".\n"
454 "Valid are even values between 2 and 131072.\n", arg);
455 free(arg);
456 return -2;
Samir Ibradžićb482c6d2012-05-15 22:58:19 +0000457 }
Elyes HAOUAS712ba3a2019-06-09 17:11:49 +0200458 divisor = (uint32_t)temp;
Samir Ibradžićb482c6d2012-05-15 22:58:19 +0000459 }
460 free(arg);
Stefan Tauner3e515e22012-09-26 00:48:16 +0000461
Alan Green32078442021-01-18 18:41:25 +1100462 /* Allows setting multiple GPIOL pins to high, for example: csgpiol=012 */
Sergey Alirzaev4acc3f32018-08-01 16:39:17 +0300463 arg = extract_programmer_param("csgpiol");
464 if (arg) {
sibradzicba6575d2020-02-04 18:24:58 +0900465 unsigned int ngpios = strlen(arg);
466 for (unsigned int i = 0; i <= ngpios; i++) {
467 int temp = arg[i] - '0';
468 if (ngpios == 0 || (ngpios != i && (temp < 0 || temp > 3))) {
469 msg_perr("Error: Invalid GPIOLs specified: \"%s\".\n"
470 "Valid values are numbers between 0 and 3. "
471 "Multiple GPIOLs can be specified.\n", arg);
472 free(arg);
473 return -2;
474 } else {
475 unsigned int pin = temp + 4;
Alan Green18007962021-01-21 09:48:25 +1100476 pinlvl |= 1 << pin;
sibradzicba6575d2020-02-04 18:24:58 +0900477 pindir |= 1 << pin;
478 }
Sergey Alirzaev4acc3f32018-08-01 16:39:17 +0300479 }
480 }
481 free(arg);
482
Alan Green32078442021-01-18 18:41:25 +1100483 /* Allows setting GPIOL pins high, low or input (high-z) */
484 arg = extract_programmer_param("gpiol");
485 if (arg) {
486 int ok = 0;
487 if (strlen(arg) == 4) {
488 ok = 1;
489 for (int i = 0; i < 4; i++) {
490 unsigned int pin = i + 4;
491 switch (toupper(arg[i])) {
492 case 'H':
Alan Green18007962021-01-21 09:48:25 +1100493 pinlvl |= 1 << pin;
Alan Green32078442021-01-18 18:41:25 +1100494 pindir |= 1 << pin;
495 break;
496 case 'L':
Alan Green18007962021-01-21 09:48:25 +1100497 pinlvl &= ~(1 << pin);
Alan Green32078442021-01-18 18:41:25 +1100498 pindir |= 1 << pin;
499 break;
500 case 'Z':
501 pindir &= ~(1 << pin);
502 break;
503 case 'X':
504 break;
505 default:
506 ok = 0;
507 }
508 }
509 }
510 if (!ok) {
511 msg_perr("Error: Invalid GPIOLs specified: \"%s\".\n"
512 "Valid values are 4 character strings of H, L, Z and X.\n"
513 " H - Set GPIOL output high\n"
514 " L - Set GPIOL output low\n"
515 " Z - Set GPIOL as input (high impedance)\n"
516 " X - Leave as programmer default\n"
517 "Example: gpiol=LZXH drives GPIOL 0 low, and GPIOL 3 high, sets GPIOL 1\n"
518 "to an input and leaves GPIOL 2 set according to the programmer type.\n", arg);
519 free(arg);
520 return -2;
521 }
522 }
523 free(arg);
524
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000525 msg_pdbg("Using device type %s %s ",
Uwe Hermann70145292010-07-29 19:57:55 +0000526 get_ft2232_vendorname(ft2232_vid, ft2232_type),
527 get_ft2232_devicename(ft2232_vid, ft2232_type));
Stefan Tauner3e515e22012-09-26 00:48:16 +0000528 msg_pdbg("channel %s.\n",
Stefan Taunerfbc71ac2012-09-26 00:46:02 +0000529 (ft2232_interface == INTERFACE_A) ? "A" :
530 (ft2232_interface == INTERFACE_B) ? "B" :
531 (ft2232_interface == INTERFACE_C) ? "C" : "D");
Paul Fox05dfbe62009-06-16 21:08:06 +0000532
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000533 if (ftdi_init(&ftdic) < 0) {
Stefan Tauner3e515e22012-09-26 00:48:16 +0000534 msg_perr("ftdi_init failed.\n");
Uwe Hermann274a20d2011-07-21 09:18:18 +0000535 return -3;
Paul Fox05dfbe62009-06-16 21:08:06 +0000536 }
537
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000538 if (ftdi_set_interface(&ftdic, ft2232_interface) < 0) {
539 msg_perr("Unable to select channel (%s).\n", ftdi_get_error_string(&ftdic));
Ilya A. Volynets-Evenbakh7c36d522012-08-14 01:32:46 +0000540 }
541
Shik Chen14fbc4b2012-09-17 00:40:54 +0000542 arg = extract_programmer_param("serial");
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000543 f = ftdi_usb_open_desc(&ftdic, ft2232_vid, ft2232_type, NULL, arg);
Shik Chen14fbc4b2012-09-17 00:40:54 +0000544 free(arg);
Paul Fox05dfbe62009-06-16 21:08:06 +0000545
546 if (f < 0 && f != -5) {
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000547 msg_perr("Unable to open FTDI device: %d (%s)\n", f,
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000548 ftdi_get_error_string(&ftdic));
Uwe Hermann274a20d2011-07-21 09:18:18 +0000549 return -4;
Paul Fox05dfbe62009-06-16 21:08:06 +0000550 }
551
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000552 if (ftdic.type != TYPE_2232H && ftdic.type != TYPE_4232H && ftdic.type != TYPE_232H) {
553 msg_pdbg("FTDI chip type %d is not high-speed.\n", ftdic.type);
Alex Badea0b94d052010-11-10 03:18:41 +0000554 clock_5x = 0;
555 }
556
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000557 if (ftdi_usb_reset(&ftdic) < 0) {
558 msg_perr("Unable to reset FTDI device (%s).\n", ftdi_get_error_string(&ftdic));
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000559 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000560
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000561 if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
562 msg_perr("Unable to set latency timer (%s).\n", ftdi_get_error_string(&ftdic));
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000563 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000564
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000565 if (ftdi_write_data_set_chunksize(&ftdic, 270)) {
566 msg_perr("Unable to set chunk size (%s).\n", ftdi_get_error_string(&ftdic));
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000567 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000568
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000569 if (ftdi_set_bitmode(&ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
570 msg_perr("Unable to set bitmode to SPI (%s).\n", ftdi_get_error_string(&ftdic));
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000571 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000572
Alex Badea0b94d052010-11-10 03:18:41 +0000573 if (clock_5x) {
574 msg_pdbg("Disable divide-by-5 front stage\n");
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000575 buf[0] = DIS_DIV_5;
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000576 if (send_buf(&ftdic, buf, 1)) {
Uwe Hermann274a20d2011-07-21 09:18:18 +0000577 ret = -5;
578 goto ftdi_err;
579 }
Alex Badea0b94d052010-11-10 03:18:41 +0000580 mpsse_clk = 60.0;
581 } else {
582 mpsse_clk = 12.0;
583 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000584
Sean Nelson34b5db72010-01-10 01:08:37 +0000585 msg_pdbg("Set clock divisor\n");
Antony Pavlovb4dd40c2015-01-25 03:52:47 +0000586 buf[0] = TCK_DIVISOR;
Ilya A. Volynets-Evenbakh3464d052012-06-14 13:08:33 +0000587 buf[1] = (divisor / 2 - 1) & 0xff;
588 buf[2] = ((divisor / 2 - 1) >> 8) & 0xff;
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000589 if (send_buf(&ftdic, buf, 3)) {
Uwe Hermann274a20d2011-07-21 09:18:18 +0000590 ret = -6;
591 goto ftdi_err;
592 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000593
Samir Ibradžićb482c6d2012-05-15 22:58:19 +0000594 msg_pdbg("MPSSE clock: %f MHz, divisor: %u, SPI clock: %f MHz\n",
595 mpsse_clk, divisor, (double)(mpsse_clk / divisor));
Paul Fox05dfbe62009-06-16 21:08:06 +0000596
Uwe Hermannc67d0372009-10-01 18:40:02 +0000597 /* Disconnect TDI/DO to TDO/DI for loopback. */
Sean Nelson34b5db72010-01-10 01:08:37 +0000598 msg_pdbg("No loopback of TDI/DO TDO/DI\n");
Antony Pavlovb4dd40c2015-01-25 03:52:47 +0000599 buf[0] = LOOPBACK_END;
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000600 if (send_buf(&ftdic, buf, 1)) {
Uwe Hermann274a20d2011-07-21 09:18:18 +0000601 ret = -7;
602 goto ftdi_err;
603 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000604
Sean Nelson34b5db72010-01-10 01:08:37 +0000605 msg_pdbg("Set data bits\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000606 buf[0] = SET_BITS_LOW;
Alan Green18007962021-01-21 09:48:25 +1100607 buf[1] = pinlvl;
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000608 buf[2] = pindir;
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000609 if (send_buf(&ftdic, buf, 3)) {
Uwe Hermann274a20d2011-07-21 09:18:18 +0000610 ret = -8;
611 goto ftdi_err;
612 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000613
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000614 spi_data = calloc(1, sizeof(*spi_data));
615 if (!spi_data) {
616 msg_perr("Unable to allocate space for SPI master data\n");
617 return SPI_GENERIC_ERROR;
618 }
619 spi_data->pinlvl = pinlvl;
620 spi_data->pindir = pindir;
621 spi_data->ftdic_context = ftdic;
622
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000623 if (register_shutdown(ft2232_shutdown, spi_data)) {
624 free(spi_data);
625 goto ftdi_err;
626 }
Nico Huber90739d12021-05-11 17:53:34 +0200627 register_spi_master(&spi_master_ft2232, spi_data);
Paul Fox05dfbe62009-06-16 21:08:06 +0000628
629 return 0;
Uwe Hermann274a20d2011-07-21 09:18:18 +0000630
631ftdi_err:
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000632 if ((f = ftdi_usb_close(&ftdic)) < 0) {
Nikolai Artemiev9e440ca2020-06-24 18:53:03 +1000633 msg_perr("Unable to close FTDI device: %d (%s)\n", f,
Anastasia Klimchuk9dad8c22021-04-12 10:05:57 +1000634 ftdi_get_error_string(&ftdic));
Uwe Hermann274a20d2011-07-21 09:18:18 +0000635 }
Uwe Hermann274a20d2011-07-21 09:18:18 +0000636 return ret;
Paul Fox05dfbe62009-06-16 21:08:06 +0000637}
638
Paul Fox05dfbe62009-06-16 21:08:06 +0000639#endif