blob: 6072cd26eebb5c7d755123314103de2891d3b8ca [file] [log] [blame]
hailfingerf31da3d2009-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>
hailfinger39d159a2010-05-21 23:09:42 +00005 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
hailfingerf31da3d2009-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.
hailfingerf31da3d2009-06-16 21:08:06 +000015 */
16
hailfinger90c7d542010-05-31 15:27:27 +000017#if CONFIG_FT2232_SPI == 1
hailfingerd9dcfbd2009-08-19 13:27:58 +000018
hailfingerf31da3d2009-06-16 21:08:06 +000019#include <stdio.h>
Edward O'Callaghan2068b922019-08-21 16:43:26 +100020#include <strings.h>
hailfingerf31da3d2009-06-16 21:08:06 +000021#include <string.h>
22#include <stdlib.h>
hailfingere98628b2009-07-01 00:02:23 +000023#include <ctype.h>
hailfingerf31da3d2009-06-16 21:08:06 +000024#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000025#include "programmer.h"
hailfingerf31da3d2009-06-16 21:08:06 +000026#include "spi.h"
hailfingerf31da3d2009-06-16 21:08:06 +000027#include <ftdi.h>
28
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +110029/* This is not defined in libftdi.h <0.20 (c7e4c09e68cfa6f5e112334aa1b3bb23401c8dc7 to be exact).
Angel Ponse8150332021-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. */
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +110031#if !defined(HAVE_FT232H)
32#define TYPE_232H 6
33#endif
34
uwed4ab19b2011-10-14 20:33:14 +000035/* Please keep sorted by vendor ID, then device ID. */
36
uwe3dae6872010-07-29 19:57:55 +000037#define FTDI_VID 0x0403
38#define FTDI_FT2232H_PID 0x6010
39#define FTDI_FT4232H_PID 0x6011
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +110040#define FTDI_FT232H_PID 0x6014
uwed4ab19b2011-10-14 20:33:14 +000041#define TIAO_TUMPA_PID 0x8a98
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +110042#define TIAO_TUMPA_LITE_PID 0x8a99
uwe3dae6872010-07-29 19:57:55 +000043#define AMONTEC_JTAGKEY_PID 0xCFF8
hailfinger888410e2010-07-29 15:54:53 +000044
uwe64e8d6f2011-10-20 23:14:10 +000045#define GOEPEL_VID 0x096C
46#define GOEPEL_PICOTAP_PID 0x1449
47
hailfingerce670612010-11-10 03:26:57 +000048#define FIC_VID 0x1457
49#define OPENMOKO_DBGBOARD_PID 0x5118
50
stefanct93481112011-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 Broch7aff3df2011-08-23 13:41:53 -070057#define GOOGLE_VID 0x18D1
58#define GOOGLE_SERVO_PID 0x5001
Todd Broch97f259f2012-04-03 15:52:14 -070059#define GOOGLE_SERVO_V2_PID0 0x5002
60#define GOOGLE_SERVO_V2_PID1 0x5003
Todd Broch7aff3df2011-08-23 13:41:53 -070061
Nikolai Artemievc347a852020-04-29 12:17:08 +100062const struct dev_entry devs_ft2232spi[] = {
uwe3dae6872010-07-29 19:57:55 +000063 {FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"},
64 {FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"},
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +110065 {FTDI_VID, FTDI_FT232H_PID, OK, "FTDI", "FT232H"},
uwed4ab19b2011-10-14 20:33:14 +000066 {FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +110067 {FTDI_VID, TIAO_TUMPA_LITE_PID, OK, "TIAO", "USB Multi-Protocol Adapter Lite"},
uwe3dae6872010-07-29 19:57:55 +000068 {FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
uwe64e8d6f2011-10-20 23:14:10 +000069 {GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"},
Todd Broch7aff3df2011-08-23 13:41:53 -070070 {GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"},
Todd Broch97f259f2012-04-03 15:52:14 -070071 {GOOGLE_VID, GOOGLE_SERVO_V2_PID0, OK, "Google", "Servo V2 Legacy"},
72 {GOOGLE_VID, GOOGLE_SERVO_V2_PID1, OK, "Google", "Servo V2"},
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +110073 {FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC", "OpenMoko Neo1973 Debug board (V2+)"},
74 {OLIMEX_VID, OLIMEX_ARM_OCD_PID, OK, "Olimex", "ARM-USB-OCD"},
75 {OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"},
76 {OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, OK, "Olimex", "ARM-USB-OCD-H"},
77 {OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, OK, "Olimex", "ARM-USB-TINY-H"},
78
Patrick Georgi8ddfee92017-03-20 14:54:28 +010079 {0},
hailfinger888410e2010-07-29 15:54:53 +000080};
81
Nikolai Artemiev985f1952020-01-20 16:33:30 +110082#define DEFAULT_DIVISOR 2
hailfingerf31da3d2009-06-16 21:08:06 +000083
uweb34ec9f2009-10-01 18:40:02 +000084#define BITMODE_BITBANG_NORMAL 1
85#define BITMODE_BITBANG_SPI 2
hailfingerf31da3d2009-06-16 21:08:06 +000086
Alan Greenb390aaf2021-01-21 09:48:25 +110087/* The variables pinlvl and pindir store the values for the "set data bits low byte" MPSSE command that
Nikolai Artemiev0a635262020-02-11 13:49:52 +110088 * sets the initial state and the direction of the I/O pins. The pin offsets are as follows:
sibradzic419c86f2020-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.
Nikolai Artemiev0a635262020-02-11 13:49:52 +1100100 *
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000101 * The default values (set below in ft2232_spi_init) are used for most devices:
hailfinger888410e2010-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
hailfinger888410e2010-07-29 15:54:53 +0000104 */
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000105struct ft2232_data {
106 uint8_t pinlvl;
107 uint8_t pindir;
108 struct ftdi_context ftdic_context;
109};
hailfingerf31da3d2009-06-16 21:08:06 +0000110
hailfinger888410e2010-07-29 15:54:53 +0000111static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
112{
113 int i;
uwe3dae6872010-07-29 19:57:55 +0000114 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
hailfinger888410e2010-07-29 15:54:53 +0000115 if ((devs_ft2232spi[i].device_id == ft2232_type)
116 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
117 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;
uwe3dae6872010-07-29 19:57:55 +0000125 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
126 if ((devs_ft2232spi[i].device_id == ft2232_type)
127 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
128 return devs_ft2232spi[i].vendor_name;
129 }
130 return "unknown vendor";
hailfinger888410e2010-07-29 15:54:53 +0000131}
132
uwe3dae6872010-07-29 19:57:55 +0000133static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf,
134 int size)
hailfingerf31da3d2009-06-16 21:08:06 +0000135{
136 int r;
137 r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
138 if (r < 0) {
snelson93932992010-01-10 01:08:37 +0000139 msg_perr("ftdi_write_data: %d, %s\n", r,
hailfingerf31da3d2009-06-16 21:08:06 +0000140 ftdi_get_error_string(ftdic));
141 return 1;
142 }
143 return 0;
144}
145
uwe3dae6872010-07-29 19:57:55 +0000146static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf,
147 int size)
hailfingerf31da3d2009-06-16 21:08:06 +0000148{
149 int r;
hailfinger694faa22010-11-10 03:10:41 +0000150
151 while (size > 0) {
152 r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
153 if (r < 0) {
154 msg_perr("ftdi_read_data: %d, %s\n", r,
155 ftdi_get_error_string(ftdic));
156 return 1;
157 }
158 buf += r;
159 size -= r;
hailfingerf31da3d2009-06-16 21:08:06 +0000160 }
161 return 0;
162}
163
Alan Greenf6c62152021-01-08 09:36:45 +1100164static int ft2232_shutdown(void *data)
165{
166 int f;
Anastasia Klimchuk40a80012021-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 Greenf6c62152021-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 Klimchuk40a80012021-04-12 10:05:57 +1000186 free(spi_data);
Alan Greenf6c62152021-01-08 09:36:45 +1100187 return 0;
188}
189
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100190/* Returns 0 upon success, a negative number upon errors. */
Nikolai Artemiev0a635262020-02-11 13:49:52 +1100191static int ft2232_spi_send_command(const struct flashctx *flash,
192 unsigned int writecnt, unsigned int readcnt,
193 const unsigned char *writearr,
Anastasia Klimchukad79bd92021-02-15 15:04:20 +1100194 unsigned char *readarr)
195{
Anastasia Klimchuk40a80012021-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 Klimchukad79bd92021-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 Klimchuk40a80012021-04-12 10:05:57 +1000228 buf[i++] = ~ 0x08 & spi_data->pinlvl; /* assert CS (3rd) bit only */
229 buf[i++] = spi_data->pindir;
Anastasia Klimchukad79bd92021-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 Klimchuk40a80012021-04-12 10:05:57 +1000269 buf[i++] = spi_data->pinlvl;
270 buf[i++] = spi_data->pindir;
Anastasia Klimchukad79bd92021-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}
mkarcherd264e9e2011-05-11 17:07:07 +0000278
Nico Huber2ef004f2021-05-11 17:53:34 +0200279static const struct spi_master spi_master_ft2232 = {
Edward O'Callaghana6673bd2019-06-24 15:22:28 +1000280 .features = SPI_MASTER_4BA,
uwe8d342eb2011-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,
Edward O'Callaghaneeaac6b2020-10-12 19:51:56 +1100287 .write_aai = default_spi_write_aai,
mkarcherd264e9e2011-05-11 17:07:07 +0000288};
289
uwe56199592011-07-21 09:18:18 +0000290/* Returns 0 upon success, a negative number upon errors. */
David Hendricksac1d25c2016-08-09 17:00:58 -0700291int ft2232_spi_init(void)
hailfingerf31da3d2009-06-16 21:08:06 +0000292{
Nikolai Artemiev985f1952020-01-20 16:33:30 +1100293 int ret = 0;
hailfingerf31da3d2009-06-16 21:08:06 +0000294 unsigned char buf[512];
hailfinger888410e2010-07-29 15:54:53 +0000295 int ft2232_vid = FTDI_VID;
296 int ft2232_type = FTDI_FT4232H_PID;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100297 int channel_count = 4; /* Stores the number of channels of the device. */
298 enum ftdi_interface ft2232_interface = INTERFACE_A;
Nikolai Artemiev985f1952020-01-20 16:33:30 +1100299 /*
300 * The 'H' chips can run with an internal clock of either 12 MHz or 60 MHz,
301 * 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.
303 */
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
311 * 92 Hz for 12 MHz inputs and 30 MHz down to about 458 Hz for 60 MHz
312 * inputs.
313 */
Nikolai Artemiev985f1952020-01-20 16:33:30 +1100314 uint32_t divisor = DEFAULT_DIVISOR;
315 int f;
hailfinger1ef766d2010-07-06 09:55:48 +0000316 char *arg;
hailfinger81f90f22010-11-10 03:18:41 +0000317 double mpsse_clk;
hailfinger1ef766d2010-07-06 09:55:48 +0000318
Anastasia Klimchuk40a80012021-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
hailfingerddeb4ac2010-07-08 10:13:37 +0000324 arg = extract_programmer_param("type");
hailfinger1ef766d2010-07-06 09:55:48 +0000325 if (arg) {
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100326 if (!strcasecmp(arg, "2232H")) {
hailfinger888410e2010-07-29 15:54:53 +0000327 ft2232_type = FTDI_FT2232H_PID;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100328 channel_count = 2;
329 } else if (!strcasecmp(arg, "4232H")) {
hailfinger888410e2010-07-29 15:54:53 +0000330 ft2232_type = FTDI_FT4232H_PID;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100331 channel_count = 4;
332 } else if (!strcasecmp(arg, "232H")) {
333 ft2232_type = FTDI_FT232H_PID;
334 channel_count = 1;
335 } else if (!strcasecmp(arg, "jtagkey")) {
hailfinger888410e2010-07-29 15:54:53 +0000336 ft2232_type = AMONTEC_JTAGKEY_PID;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100337 channel_count = 2;
338 /* 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 Greenb390aaf2021-01-21 09:48:25 +1100341 pinlvl = 0x18;
uwe3dae6872010-07-29 19:57:55 +0000342 pindir = 0x1b;
uwe64e8d6f2011-10-20 23:14:10 +0000343 } else if (!strcasecmp(arg, "picotap")) {
344 ft2232_vid = GOEPEL_VID;
345 ft2232_type = GOEPEL_PICOTAP_PID;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100346 channel_count = 2;
uwed4ab19b2011-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;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100350 channel_count = 2;
351 } 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;
stefanct90aefe82011-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;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100358 channel_count = 2;
Alan Greenb390aaf2021-01-21 09:48:25 +1100359 pinlvl = 0x18;
stefanct90aefe82011-08-12 01:19:32 +0000360 pindir = 0x1b;
hailfingerce670612010-11-10 03:26:57 +0000361 } else if (!strcasecmp(arg, "openmoko")) {
362 ft2232_vid = FIC_VID;
363 ft2232_type = OPENMOKO_DBGBOARD_PID;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100364 channel_count = 2;
stefanct93481112011-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;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100368 channel_count = 2;
369 /* 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 Greenb390aaf2021-01-21 09:48:25 +1100372 pinlvl = 0x08;
Paul Fertser80ed25d2016-07-29 15:31:03 -0700373 pindir = 0x1b;
stefanct93481112011-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;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100377 channel_count = 2;
stefanct93481112011-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;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100381 channel_count = 2;
382 /* See arm-usb-ocd */
Alan Greenb390aaf2021-01-21 09:48:25 +1100383 pinlvl = 0x08;
Paul Fertser80ed25d2016-07-29 15:31:03 -0700384 pindir = 0x1b;
stefanct93481112011-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;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100388 channel_count = 2;
Edward O'Callaghanb9a1dcf2020-03-02 17:14:14 +1100389 } else if (!strcasecmp(arg, "google-servo")) {
390 ft2232_vid = GOOGLE_VID;
391 ft2232_type = GOOGLE_SERVO_PID;
Edward O'Callaghanb9a1dcf2020-03-02 17:14:14 +1100392 } else if (!strcasecmp(arg, "google-servo-v2")) {
393 ft2232_vid = GOOGLE_VID;
394 ft2232_type = GOOGLE_SERVO_V2_PID1;
Edward O'Callaghanb9a1dcf2020-03-02 17:14:14 +1100395 /* 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;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100400 } 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;
hailfingerce670612010-11-10 03:26:57 +0000406 } else {
hailfinger1ef766d2010-07-06 09:55:48 +0000407 msg_perr("Error: Invalid device type specified.\n");
408 free(arg);
uwe56199592011-07-21 09:18:18 +0000409 return -1;
hailfinger1ef766d2010-07-06 09:55:48 +0000410 }
411 }
412 free(arg);
Edward O'Callaghan2068b922019-08-21 16:43:26 +1000413
hailfingerddeb4ac2010-07-08 10:13:37 +0000414 arg = extract_programmer_param("port");
hailfinger1ef766d2010-07-06 09:55:48 +0000415 if (arg) {
hailfingerebad21f2010-10-08 12:40:09 +0000416 switch (toupper((unsigned char)*arg)) {
hailfinger1ef766d2010-07-06 09:55:48 +0000417 case 'A':
418 ft2232_interface = INTERFACE_A;
419 break;
420 case 'B':
421 ft2232_interface = INTERFACE_B;
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100422 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;
hailfinger1ef766d2010-07-06 09:55:48 +0000434 break;
435 default:
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100436 channel_count = -1;
437 break;
438 }
439 if (channel_count < 0 || strlen(arg) != 1) {
440 msg_perr("Error: Invalid channel/port/interface specified: \"%s\".\n", arg);
hailfinger1ef766d2010-07-06 09:55:48 +0000441 free(arg);
uwe56199592011-07-21 09:18:18 +0000442 return -2;
hailfinger1ef766d2010-07-06 09:55:48 +0000443 }
444 }
445 free(arg);
Nikolai Artemiev249cbab2020-02-07 11:53:15 +1100446
Nikolai Artemiev249cbab2020-02-07 11:53:15 +1100447 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;
457 }
458 divisor = (uint32_t)temp;
459 }
460 free(arg);
461
Alan Green2cb9f132021-01-18 18:41:25 +1100462 /* Allows setting multiple GPIOL pins to high, for example: csgpiol=012 */
Sergey Alirzaev018a6df2018-08-01 16:39:17 +0300463 arg = extract_programmer_param("csgpiol");
464 if (arg) {
sibradzic419c86f2020-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 Greenb390aaf2021-01-21 09:48:25 +1100476 pinlvl |= 1 << pin;
sibradzic419c86f2020-02-04 18:24:58 +0900477 pindir |= 1 << pin;
478 }
Sergey Alirzaev018a6df2018-08-01 16:39:17 +0300479 }
480 }
481 free(arg);
482
Alan Green2cb9f132021-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 Greenb390aaf2021-01-21 09:48:25 +1100493 pinlvl |= 1 << pin;
Alan Green2cb9f132021-01-18 18:41:25 +1100494 pindir |= 1 << pin;
495 break;
496 case 'L':
Alan Greenb390aaf2021-01-21 09:48:25 +1100497 pinlvl &= ~(1 << pin);
Alan Green2cb9f132021-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
hailfinger888410e2010-07-29 15:54:53 +0000525 msg_pdbg("Using device type %s %s ",
uwe3dae6872010-07-29 19:57:55 +0000526 get_ft2232_vendorname(ft2232_vid, ft2232_type),
527 get_ft2232_devicename(ft2232_vid, ft2232_type));
Nikolai Artemiev0a3fb592020-01-09 11:37:46 +1100528 msg_pdbg("channel %s.\n",
529 (ft2232_interface == INTERFACE_A) ? "A" :
530 (ft2232_interface == INTERFACE_B) ? "B" :
531 (ft2232_interface == INTERFACE_C) ? "C" : "D");
hailfingerf31da3d2009-06-16 21:08:06 +0000532
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000533 if (ftdi_init(&ftdic) < 0) {
Edward O'Callaghan2068b922019-08-21 16:43:26 +1000534 msg_perr("ftdi_init failed.\n");
uwe56199592011-07-21 09:18:18 +0000535 return -3;
hailfingerf31da3d2009-06-16 21:08:06 +0000536 }
537
Anastasia Klimchuk40a80012021-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));
Todd Brochcbaba9c2014-09-22 12:11:03 -0700540 }
541
Shik Chen5eb56242012-08-07 15:09:37 +0800542 arg = extract_programmer_param("serial");
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000543 f = ftdi_usb_open_desc(&ftdic, ft2232_vid, ft2232_type, NULL, arg);
Shik Chen5eb56242012-08-07 15:09:37 +0800544 free(arg);
hailfingerf31da3d2009-06-16 21:08:06 +0000545
546 if (f < 0 && f != -5) {
snelson93932992010-01-10 01:08:37 +0000547 msg_perr("Unable to open FTDI device: %d (%s)\n", f,
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000548 ftdi_get_error_string(&ftdic));
uwe56199592011-07-21 09:18:18 +0000549 return -4;
hailfingerf31da3d2009-06-16 21:08:06 +0000550 }
551
Anastasia Klimchuk40a80012021-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);
hailfinger81f90f22010-11-10 03:18:41 +0000554 clock_5x = 0;
555 }
556
Anastasia Klimchuk40a80012021-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));
hailfingerb91c08c2011-08-15 19:54:20 +0000559 }
hailfingerf31da3d2009-06-16 21:08:06 +0000560
Anastasia Klimchuk40a80012021-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));
hailfingerb91c08c2011-08-15 19:54:20 +0000563 }
hailfingerf31da3d2009-06-16 21:08:06 +0000564
Anastasia Klimchuk40a80012021-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));
hailfingerb91c08c2011-08-15 19:54:20 +0000567 }
hailfingerf31da3d2009-06-16 21:08:06 +0000568
Anastasia Klimchuk40a80012021-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));
hailfingerb91c08c2011-08-15 19:54:20 +0000571 }
hailfingerf31da3d2009-06-16 21:08:06 +0000572
hailfinger81f90f22010-11-10 03:18:41 +0000573 if (clock_5x) {
574 msg_pdbg("Disable divide-by-5 front stage\n");
Nikolai Artemieva5c9cd72020-01-08 10:40:19 +1100575 buf[0] = DIS_DIV_5;
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000576 if (send_buf(&ftdic, buf, 1)) {
uwe56199592011-07-21 09:18:18 +0000577 ret = -5;
578 goto ftdi_err;
579 }
hailfinger81f90f22010-11-10 03:18:41 +0000580 mpsse_clk = 60.0;
581 } else {
582 mpsse_clk = 12.0;
Todd Broch77746652012-04-19 09:52:49 -0700583 }
Nikolai Artemiev985f1952020-01-20 16:33:30 +1100584
snelson93932992010-01-10 01:08:37 +0000585 msg_pdbg("Set clock divisor\n");
Nikolai Artemieva5c9cd72020-01-08 10:40:19 +1100586 buf[0] = TCK_DIVISOR;
Nikolai Artemiev985f1952020-01-20 16:33:30 +1100587 buf[1] = (divisor / 2 - 1) & 0xff;
588 buf[2] = ((divisor / 2 - 1) >> 8) & 0xff;
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000589 if (send_buf(&ftdic, buf, 3)) {
uwe56199592011-07-21 09:18:18 +0000590 ret = -6;
591 goto ftdi_err;
592 }
hailfingerf31da3d2009-06-16 21:08:06 +0000593
Nikolai Artemiev985f1952020-01-20 16:33:30 +1100594 msg_pdbg("MPSSE clock: %f MHz, divisor: %u, SPI clock: %f MHz\n",
595 mpsse_clk, divisor, (double)(mpsse_clk / divisor));
hailfingerf31da3d2009-06-16 21:08:06 +0000596
uweb34ec9f2009-10-01 18:40:02 +0000597 /* Disconnect TDI/DO to TDO/DI for loopback. */
snelson93932992010-01-10 01:08:37 +0000598 msg_pdbg("No loopback of TDI/DO TDO/DI\n");
Nikolai Artemieva5c9cd72020-01-08 10:40:19 +1100599 buf[0] = LOOPBACK_END;
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000600 if (send_buf(&ftdic, buf, 1)) {
uwe56199592011-07-21 09:18:18 +0000601 ret = -7;
602 goto ftdi_err;
603 }
hailfingerf31da3d2009-06-16 21:08:06 +0000604
snelson93932992010-01-10 01:08:37 +0000605 msg_pdbg("Set data bits\n");
hailfingerf31da3d2009-06-16 21:08:06 +0000606 buf[0] = SET_BITS_LOW;
Alan Greenb390aaf2021-01-21 09:48:25 +1100607 buf[1] = pinlvl;
hailfinger888410e2010-07-29 15:54:53 +0000608 buf[2] = pindir;
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000609 if (send_buf(&ftdic, buf, 3)) {
uwe56199592011-07-21 09:18:18 +0000610 ret = -8;
611 goto ftdi_err;
612 }
hailfingerf31da3d2009-06-16 21:08:06 +0000613
Anastasia Klimchuk40a80012021-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 Klimchuk40a80012021-04-12 10:05:57 +1000623 if (register_shutdown(ft2232_shutdown, spi_data)) {
624 free(spi_data);
625 goto ftdi_err;
626 }
Nico Huber2ef004f2021-05-11 17:53:34 +0200627 register_spi_master(&spi_master_ft2232, spi_data);
hailfingerf31da3d2009-06-16 21:08:06 +0000628
629 return 0;
uwe56199592011-07-21 09:18:18 +0000630
631ftdi_err:
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000632 if ((f = ftdi_usb_close(&ftdic)) < 0) {
uwe56199592011-07-21 09:18:18 +0000633 msg_perr("Unable to close FTDI device: %d (%s)\n", f,
Anastasia Klimchuk40a80012021-04-12 10:05:57 +1000634 ftdi_get_error_string(&ftdic));
uwe56199592011-07-21 09:18:18 +0000635 }
uwe56199592011-07-21 09:18:18 +0000636 return ret;
hailfingerf31da3d2009-06-16 21:08:06 +0000637}
638
hailfingerf31da3d2009-06-16 21:08:06 +0000639#endif