blob: 19fcdb6d07a74fab3e25fd33e48cbc9b70f33acb [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.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
hailfinger90c7d542010-05-31 15:27:27 +000021#if CONFIG_FT2232_SPI == 1
hailfingerd9dcfbd2009-08-19 13:27:58 +000022
hailfingerf31da3d2009-06-16 21:08:06 +000023#include <stdio.h>
hailfingerf31da3d2009-06-16 21:08:06 +000024#include <string.h>
25#include <stdlib.h>
hailfingere98628b2009-07-01 00:02:23 +000026#include <ctype.h>
hailfingerf31da3d2009-06-16 21:08:06 +000027#include "flash.h"
hailfinger428f6852010-07-27 22:41:39 +000028#include "programmer.h"
hailfingerf31da3d2009-06-16 21:08:06 +000029#include "spi.h"
hailfingerf31da3d2009-06-16 21:08:06 +000030#include <ftdi.h>
31
uwed4ab19b2011-10-14 20:33:14 +000032/* Please keep sorted by vendor ID, then device ID. */
33
uwe3dae6872010-07-29 19:57:55 +000034#define FTDI_VID 0x0403
35#define FTDI_FT2232H_PID 0x6010
36#define FTDI_FT4232H_PID 0x6011
uwed4ab19b2011-10-14 20:33:14 +000037#define TIAO_TUMPA_PID 0x8a98
uwe3dae6872010-07-29 19:57:55 +000038#define AMONTEC_JTAGKEY_PID 0xCFF8
hailfinger888410e2010-07-29 15:54:53 +000039
uwe64e8d6f2011-10-20 23:14:10 +000040#define GOEPEL_VID 0x096C
41#define GOEPEL_PICOTAP_PID 0x1449
42
hailfingerce670612010-11-10 03:26:57 +000043#define FIC_VID 0x1457
44#define OPENMOKO_DBGBOARD_PID 0x5118
45
stefanct93481112011-06-11 12:21:37 +000046#define OLIMEX_VID 0x15BA
47#define OLIMEX_ARM_OCD_PID 0x0003
48#define OLIMEX_ARM_TINY_PID 0x0004
49#define OLIMEX_ARM_OCD_H_PID 0x002B
50#define OLIMEX_ARM_TINY_H_PID 0x002A
51
Todd Broch7aff3df2011-08-23 13:41:53 -070052#define GOOGLE_VID 0x18D1
53#define GOOGLE_SERVO_PID 0x5001
Todd Broch97f259f2012-04-03 15:52:14 -070054#define GOOGLE_SERVO_V2_PID0 0x5002
55#define GOOGLE_SERVO_V2_PID1 0x5003
Todd Broch7aff3df2011-08-23 13:41:53 -070056
hailfinger888410e2010-07-29 15:54:53 +000057const struct usbdev_status devs_ft2232spi[] = {
uwe3dae6872010-07-29 19:57:55 +000058 {FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"},
59 {FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"},
uwed4ab19b2011-10-14 20:33:14 +000060 {FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
uwe3dae6872010-07-29 19:57:55 +000061 {FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
uwe64e8d6f2011-10-20 23:14:10 +000062 {GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"},
stefanct93481112011-06-11 12:21:37 +000063 {FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC",
hailfingerce670612010-11-10 03:26:57 +000064 "OpenMoko Neo1973 Debug board (V2+)"},
stefanct93481112011-06-11 12:21:37 +000065 {OLIMEX_VID, OLIMEX_ARM_OCD_PID, NT, "Olimex", "ARM-USB-OCD"},
66 {OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"},
67 {OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, NT, "Olimex", "ARM-USB-OCD-H"},
68 {OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, NT, "Olimex", "ARM-USB-TINY-H"},
Todd Broch7aff3df2011-08-23 13:41:53 -070069 {GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"},
Todd Broch97f259f2012-04-03 15:52:14 -070070 {GOOGLE_VID, GOOGLE_SERVO_V2_PID0, OK, "Google", "Servo V2 Legacy"},
71 {GOOGLE_VID, GOOGLE_SERVO_V2_PID1, OK, "Google", "Servo V2"},
uwe3dae6872010-07-29 19:57:55 +000072 {},
hailfinger888410e2010-07-29 15:54:53 +000073};
74
uweb34ec9f2009-10-01 18:40:02 +000075/*
76 * The 'H' chips can run internally at either 12MHz or 60MHz.
77 * The non-H chips can only run at 12MHz.
78 */
hailfinger81f90f22010-11-10 03:18:41 +000079static uint8_t clock_5x = 1;
hailfingerf31da3d2009-06-16 21:08:06 +000080
uweb34ec9f2009-10-01 18:40:02 +000081/*
82 * In either case, the divisor is a simple integer clock divider.
hailfinger81f90f22010-11-10 03:18:41 +000083 * If clock_5x is set, this divisor divides 30MHz, else it divides 6MHz.
uweb34ec9f2009-10-01 18:40:02 +000084 */
85#define DIVIDE_BY 3 /* e.g. '3' will give either 10MHz or 2MHz SPI clock. */
hailfingerf31da3d2009-06-16 21:08:06 +000086
uweb34ec9f2009-10-01 18:40:02 +000087#define BITMODE_BITBANG_NORMAL 1
88#define BITMODE_BITBANG_SPI 2
hailfingerf31da3d2009-06-16 21:08:06 +000089
hailfinger888410e2010-07-29 15:54:53 +000090/* Set data bits low-byte command:
91 * value: 0x08 CS=high, DI=low, DO=low, SK=low
92 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
93 *
94 * JTAGkey(2) needs to enable its output via Bit4 / GPIOL0
95 * value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low
96 * dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output
hailfinger888410e2010-07-29 15:54:53 +000097 */
uwe3dae6872010-07-29 19:57:55 +000098static uint8_t cs_bits = 0x08;
99static uint8_t pindir = 0x0b;
hailfingerf31da3d2009-06-16 21:08:06 +0000100static struct ftdi_context ftdic_context;
101
hailfinger888410e2010-07-29 15:54:53 +0000102static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
103{
104 int i;
uwe3dae6872010-07-29 19:57:55 +0000105 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
hailfinger888410e2010-07-29 15:54:53 +0000106 if ((devs_ft2232spi[i].device_id == ft2232_type)
107 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
108 return devs_ft2232spi[i].device_name;
109 }
110 return "unknown device";
111}
112
113static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type)
114{
115 int i;
uwe3dae6872010-07-29 19:57:55 +0000116 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
117 if ((devs_ft2232spi[i].device_id == ft2232_type)
118 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
119 return devs_ft2232spi[i].vendor_name;
120 }
121 return "unknown vendor";
hailfinger888410e2010-07-29 15:54:53 +0000122}
123
uwe3dae6872010-07-29 19:57:55 +0000124static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf,
125 int size)
hailfingerf31da3d2009-06-16 21:08:06 +0000126{
127 int r;
128 r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
129 if (r < 0) {
snelson93932992010-01-10 01:08:37 +0000130 msg_perr("ftdi_write_data: %d, %s\n", r,
hailfingerf31da3d2009-06-16 21:08:06 +0000131 ftdi_get_error_string(ftdic));
132 return 1;
133 }
134 return 0;
135}
136
uwe3dae6872010-07-29 19:57:55 +0000137static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf,
138 int size)
hailfingerf31da3d2009-06-16 21:08:06 +0000139{
140 int r;
hailfinger694faa22010-11-10 03:10:41 +0000141
142 while (size > 0) {
143 r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
144 if (r < 0) {
145 msg_perr("ftdi_read_data: %d, %s\n", r,
146 ftdi_get_error_string(ftdic));
147 return 1;
148 }
149 buf += r;
150 size -= r;
hailfingerf31da3d2009-06-16 21:08:06 +0000151 }
152 return 0;
153}
154
mkarcherd264e9e2011-05-11 17:07:07 +0000155static int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
156 const unsigned char *writearr, unsigned char *readarr);
157
158static const struct spi_programmer spi_programmer_ft2232 = {
uwe8d342eb2011-07-28 08:13:25 +0000159 .type = SPI_CONTROLLER_FT2232,
160 .max_data_read = 64 * 1024,
161 .max_data_write = 256,
162 .command = ft2232_spi_send_command,
163 .multicommand = default_spi_send_multicommand,
164 .read = default_spi_read,
165 .write_256 = default_spi_write_256,
mkarcherd264e9e2011-05-11 17:07:07 +0000166};
167
uwe56199592011-07-21 09:18:18 +0000168/* Returns 0 upon success, a negative number upon errors. */
hailfingerf31da3d2009-06-16 21:08:06 +0000169int ft2232_spi_init(void)
170{
uwe56199592011-07-21 09:18:18 +0000171 int f, ret = 0;
hailfingerf31da3d2009-06-16 21:08:06 +0000172 struct ftdi_context *ftdic = &ftdic_context;
173 unsigned char buf[512];
hailfinger888410e2010-07-29 15:54:53 +0000174 int ft2232_vid = FTDI_VID;
175 int ft2232_type = FTDI_FT4232H_PID;
hailfingere98628b2009-07-01 00:02:23 +0000176 enum ftdi_interface ft2232_interface = INTERFACE_B;
Todd Broch77746652012-04-19 09:52:49 -0700177 char *endp;
178 double spi_mhz = 0;
179 uint16_t divide_by = DIVIDE_BY;
hailfinger1ef766d2010-07-06 09:55:48 +0000180 char *arg;
hailfinger81f90f22010-11-10 03:18:41 +0000181 double mpsse_clk;
hailfinger1ef766d2010-07-06 09:55:48 +0000182
hailfingerddeb4ac2010-07-08 10:13:37 +0000183 arg = extract_programmer_param("type");
hailfinger1ef766d2010-07-06 09:55:48 +0000184 if (arg) {
185 if (!strcasecmp(arg, "2232H"))
hailfinger888410e2010-07-29 15:54:53 +0000186 ft2232_type = FTDI_FT2232H_PID;
hailfinger1ef766d2010-07-06 09:55:48 +0000187 else if (!strcasecmp(arg, "4232H"))
hailfinger888410e2010-07-29 15:54:53 +0000188 ft2232_type = FTDI_FT4232H_PID;
189 else if (!strcasecmp(arg, "jtagkey")) {
190 ft2232_type = AMONTEC_JTAGKEY_PID;
191 ft2232_interface = INTERFACE_A;
192 cs_bits = 0x18;
uwe3dae6872010-07-29 19:57:55 +0000193 pindir = 0x1b;
uwe64e8d6f2011-10-20 23:14:10 +0000194 } else if (!strcasecmp(arg, "picotap")) {
195 ft2232_vid = GOEPEL_VID;
196 ft2232_type = GOEPEL_PICOTAP_PID;
197 ft2232_interface = INTERFACE_A;
uwed4ab19b2011-10-14 20:33:14 +0000198 } else if (!strcasecmp(arg, "tumpa")) {
199 /* Interface A is SPI1, B is SPI2. */
200 ft2232_type = TIAO_TUMPA_PID;
201 ft2232_interface = INTERFACE_A;
stefanct90aefe82011-08-12 01:19:32 +0000202 } else if (!strcasecmp(arg, "busblaster")) {
203 /* In its default configuration it is a jtagkey clone */
204 ft2232_type = FTDI_FT2232H_PID;
205 ft2232_interface = INTERFACE_A;
206 cs_bits = 0x18;
207 pindir = 0x1b;
hailfingerce670612010-11-10 03:26:57 +0000208 } else if (!strcasecmp(arg, "openmoko")) {
209 ft2232_vid = FIC_VID;
210 ft2232_type = OPENMOKO_DBGBOARD_PID;
211 ft2232_interface = INTERFACE_A;
stefanct93481112011-06-11 12:21:37 +0000212 } else if (!strcasecmp(arg, "arm-usb-ocd")) {
213 ft2232_vid = OLIMEX_VID;
214 ft2232_type = OLIMEX_ARM_OCD_PID;
215 ft2232_interface = INTERFACE_A;
216 } else if (!strcasecmp(arg, "arm-usb-tiny")) {
217 ft2232_vid = OLIMEX_VID;
218 ft2232_type = OLIMEX_ARM_TINY_PID;
219 ft2232_interface = INTERFACE_A;
220 } else if (!strcasecmp(arg, "arm-usb-ocd-h")) {
221 ft2232_vid = OLIMEX_VID;
222 ft2232_type = OLIMEX_ARM_OCD_H_PID;
223 ft2232_interface = INTERFACE_A;
224 } else if (!strcasecmp(arg, "arm-usb-tiny-h")) {
225 ft2232_vid = OLIMEX_VID;
226 ft2232_type = OLIMEX_ARM_TINY_H_PID;
227 ft2232_interface = INTERFACE_A;
Todd Broch7aff3df2011-08-23 13:41:53 -0700228 } else if (!strcasecmp(arg, "servo")) {
229 ft2232_vid = GOOGLE_VID;
230 ft2232_type = GOOGLE_SERVO_PID;
231 ft2232_interface = INTERFACE_A;
Todd Broche1e13ec2012-01-17 09:32:13 -0800232 } else if (!strcasecmp(arg, "servo-v2")) {
233 ft2232_vid = GOOGLE_VID;
Todd Broch97f259f2012-04-03 15:52:14 -0700234 ft2232_type = GOOGLE_SERVO_V2_PID1;
235 ft2232_interface = INTERFACE_A;
236 } else if (!strcasecmp(arg, "servo-v2-legacy")) {
237 ft2232_vid = GOOGLE_VID;
238 ft2232_type = GOOGLE_SERVO_V2_PID0;
Todd Broche1e13ec2012-01-17 09:32:13 -0800239 ft2232_interface = INTERFACE_A;
hailfingerce670612010-11-10 03:26:57 +0000240 } else {
hailfinger1ef766d2010-07-06 09:55:48 +0000241 msg_perr("Error: Invalid device type specified.\n");
242 free(arg);
uwe56199592011-07-21 09:18:18 +0000243 return -1;
hailfinger1ef766d2010-07-06 09:55:48 +0000244 }
245 }
246 free(arg);
hailfingerddeb4ac2010-07-08 10:13:37 +0000247 arg = extract_programmer_param("port");
hailfinger1ef766d2010-07-06 09:55:48 +0000248 if (arg) {
hailfingerebad21f2010-10-08 12:40:09 +0000249 switch (toupper((unsigned char)*arg)) {
hailfinger1ef766d2010-07-06 09:55:48 +0000250 case 'A':
251 ft2232_interface = INTERFACE_A;
252 break;
253 case 'B':
254 ft2232_interface = INTERFACE_B;
255 break;
256 default:
257 msg_perr("Error: Invalid port/interface specified.\n");
258 free(arg);
uwe56199592011-07-21 09:18:18 +0000259 return -2;
hailfinger1ef766d2010-07-06 09:55:48 +0000260 }
261 }
262 free(arg);
Todd Broch77746652012-04-19 09:52:49 -0700263 arg = extract_programmer_param("spi_mhz");
264 if (arg) {
265 spi_mhz = strtod(arg, &endp);
266 if (arg == endp) {
267 msg_perr("%s: Invalid clock %s MHz. Will use "
268 "default\n", __func__, arg);
269 }
270 msg_pdbg("Clock %f MHz\n", spi_mhz);
271 }
272 free(arg);
hailfinger888410e2010-07-29 15:54:53 +0000273 msg_pdbg("Using device type %s %s ",
uwe3dae6872010-07-29 19:57:55 +0000274 get_ft2232_vendorname(ft2232_vid, ft2232_type),
275 get_ft2232_devicename(ft2232_vid, ft2232_type));
hailfinger1ef766d2010-07-06 09:55:48 +0000276 msg_pdbg("interface %s\n",
uwe3dae6872010-07-29 19:57:55 +0000277 (ft2232_interface == INTERFACE_A) ? "A" : "B");
hailfingerf31da3d2009-06-16 21:08:06 +0000278
279 if (ftdi_init(ftdic) < 0) {
snelson93932992010-01-10 01:08:37 +0000280 msg_perr("ftdi_init failed\n");
uwe56199592011-07-21 09:18:18 +0000281 return -3;
hailfingerf31da3d2009-06-16 21:08:06 +0000282 }
283
Todd Brochcbaba9c2014-09-22 12:11:03 -0700284 /* Must occur prior to ftdi_usb_open_* call */
285 if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
286 msg_perr("Unable to select interface: %s\n",
287 ftdic->error_str);
288 }
289
Shik Chen5eb56242012-08-07 15:09:37 +0800290 arg = extract_programmer_param("serial");
291 f = ftdi_usb_open_desc(ftdic, ft2232_vid, ft2232_type, NULL, arg);
292 free(arg);
hailfingerf31da3d2009-06-16 21:08:06 +0000293
294 if (f < 0 && f != -5) {
snelson93932992010-01-10 01:08:37 +0000295 msg_perr("Unable to open FTDI device: %d (%s)\n", f,
hailfingerf31da3d2009-06-16 21:08:06 +0000296 ftdi_get_error_string(ftdic));
uwe56199592011-07-21 09:18:18 +0000297 return -4;
hailfingerf31da3d2009-06-16 21:08:06 +0000298 }
299
hailfinger81f90f22010-11-10 03:18:41 +0000300 if (ftdic->type != TYPE_2232H && ftdic->type != TYPE_4232H) {
301 msg_pdbg("FTDI chip type %d is not high-speed\n",
302 ftdic->type);
303 clock_5x = 0;
304 }
305
hailfingerb91c08c2011-08-15 19:54:20 +0000306 if (ftdi_usb_reset(ftdic) < 0) {
snelson93932992010-01-10 01:08:37 +0000307 msg_perr("Unable to reset FTDI device\n");
hailfingerb91c08c2011-08-15 19:54:20 +0000308 }
hailfingerf31da3d2009-06-16 21:08:06 +0000309
hailfingerb91c08c2011-08-15 19:54:20 +0000310 if (ftdi_set_latency_timer(ftdic, 2) < 0) {
snelson93932992010-01-10 01:08:37 +0000311 msg_perr("Unable to set latency timer\n");
hailfingerb91c08c2011-08-15 19:54:20 +0000312 }
hailfingerf31da3d2009-06-16 21:08:06 +0000313
hailfingerb91c08c2011-08-15 19:54:20 +0000314 if (ftdi_write_data_set_chunksize(ftdic, 256)) {
snelson93932992010-01-10 01:08:37 +0000315 msg_perr("Unable to set chunk size\n");
hailfingerb91c08c2011-08-15 19:54:20 +0000316 }
hailfingerf31da3d2009-06-16 21:08:06 +0000317
hailfingerb91c08c2011-08-15 19:54:20 +0000318 if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
snelson93932992010-01-10 01:08:37 +0000319 msg_perr("Unable to set bitmode to SPI\n");
hailfingerb91c08c2011-08-15 19:54:20 +0000320 }
hailfingerf31da3d2009-06-16 21:08:06 +0000321
hailfinger81f90f22010-11-10 03:18:41 +0000322 if (clock_5x) {
323 msg_pdbg("Disable divide-by-5 front stage\n");
324 buf[0] = 0x8a; /* Disable divide-by-5. */
uwe56199592011-07-21 09:18:18 +0000325 if (send_buf(ftdic, buf, 1)) {
326 ret = -5;
327 goto ftdi_err;
328 }
hailfinger81f90f22010-11-10 03:18:41 +0000329 mpsse_clk = 60.0;
Todd Broch77746652012-04-19 09:52:49 -0700330 if (spi_mhz)
331 divide_by = (uint16_t)(mpsse_clk / (spi_mhz * 2));
hailfinger81f90f22010-11-10 03:18:41 +0000332 } else {
333 mpsse_clk = 12.0;
Todd Broch77746652012-04-19 09:52:49 -0700334 if (spi_mhz)
335 divide_by = (uint16_t)((mpsse_clk * 5) / (spi_mhz * 2));
336 }
337 if (divide_by < 1) {
338 msg_perr("Can't set SPI clock to %f MHz, will be %f MHz\n",
339 spi_mhz, mpsse_clk / 2);
340 divide_by = 1;
hailfinger81f90f22010-11-10 03:18:41 +0000341 }
hailfingerf31da3d2009-06-16 21:08:06 +0000342
snelson93932992010-01-10 01:08:37 +0000343 msg_pdbg("Set clock divisor\n");
hailfingerf31da3d2009-06-16 21:08:06 +0000344 buf[0] = 0x86; /* command "set divisor" */
345 /* valueL/valueH are (desired_divisor - 1) */
Todd Broch77746652012-04-19 09:52:49 -0700346 buf[1] = (divide_by - 1) & 0xff;
347 buf[2] = ((divide_by - 1) >> 8) & 0xff;
uwe56199592011-07-21 09:18:18 +0000348 if (send_buf(ftdic, buf, 3)) {
349 ret = -6;
350 goto ftdi_err;
351 }
hailfingerf31da3d2009-06-16 21:08:06 +0000352
hailfinger81f90f22010-11-10 03:18:41 +0000353 msg_pdbg("MPSSE clock: %f MHz divisor: %d "
Todd Broch77746652012-04-19 09:52:49 -0700354 "SPI clock: %f MHz\n", mpsse_clk, divide_by,
355 (double)(mpsse_clk / (((divide_by - 1) + 1) * 2)));
hailfingerf31da3d2009-06-16 21:08:06 +0000356
uweb34ec9f2009-10-01 18:40:02 +0000357 /* Disconnect TDI/DO to TDO/DI for loopback. */
snelson93932992010-01-10 01:08:37 +0000358 msg_pdbg("No loopback of TDI/DO TDO/DI\n");
hailfingerf31da3d2009-06-16 21:08:06 +0000359 buf[0] = 0x85;
uwe56199592011-07-21 09:18:18 +0000360 if (send_buf(ftdic, buf, 1)) {
361 ret = -7;
362 goto ftdi_err;
363 }
hailfingerf31da3d2009-06-16 21:08:06 +0000364
snelson93932992010-01-10 01:08:37 +0000365 msg_pdbg("Set data bits\n");
hailfingerf31da3d2009-06-16 21:08:06 +0000366 buf[0] = SET_BITS_LOW;
hailfinger888410e2010-07-29 15:54:53 +0000367 buf[1] = cs_bits;
368 buf[2] = pindir;
uwe56199592011-07-21 09:18:18 +0000369 if (send_buf(ftdic, buf, 3)) {
370 ret = -8;
371 goto ftdi_err;
372 }
hailfingerf31da3d2009-06-16 21:08:06 +0000373
snelson93932992010-01-10 01:08:37 +0000374 // msg_pdbg("\nft2232 chosen\n");
hailfingerf31da3d2009-06-16 21:08:06 +0000375
mkarcherd264e9e2011-05-11 17:07:07 +0000376 register_spi_programmer(&spi_programmer_ft2232);
hailfingerf31da3d2009-06-16 21:08:06 +0000377
378 return 0;
uwe56199592011-07-21 09:18:18 +0000379
380ftdi_err:
381 if ((f = ftdi_usb_close(ftdic)) < 0) {
382 msg_perr("Unable to close FTDI device: %d (%s)\n", f,
383 ftdi_get_error_string(ftdic));
384 }
385
386 return ret;
hailfingerf31da3d2009-06-16 21:08:06 +0000387}
388
uwe56199592011-07-21 09:18:18 +0000389/* Returns 0 upon success, a negative number upon errors. */
mkarcherd264e9e2011-05-11 17:07:07 +0000390static int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
hailfingerf31da3d2009-06-16 21:08:06 +0000391 const unsigned char *writearr, unsigned char *readarr)
392{
393 struct ftdi_context *ftdic = &ftdic_context;
394 static unsigned char *buf = NULL;
hailfinger322be712009-11-22 01:33:40 +0000395 /* failed is special. We use bitwise ops, but it is essentially bool. */
hailfingerb91c08c2011-08-15 19:54:20 +0000396 int i = 0, ret = 0, failed = 0;
397 int bufsize;
hailfinger90604662009-11-25 16:58:17 +0000398 static int oldbufsize = 0;
hailfingerf31da3d2009-06-16 21:08:06 +0000399
hailfinger9c290a72009-07-14 10:26:56 +0000400 if (writecnt > 65536 || readcnt > 65536)
401 return SPI_INVALID_LENGTH;
402
hailfinger90604662009-11-25 16:58:17 +0000403 /* buf is not used for the response from the chip. */
404 bufsize = max(writecnt + 9, 260 + 9);
405 /* Never shrink. realloc() calls are expensive. */
406 if (bufsize > oldbufsize) {
407 buf = realloc(buf, bufsize);
408 if (!buf) {
snelson93932992010-01-10 01:08:37 +0000409 msg_perr("Out of memory!\n");
uwe56199592011-07-21 09:18:18 +0000410 /* TODO: What to do with buf? */
411 return SPI_GENERIC_ERROR;
hailfinger90604662009-11-25 16:58:17 +0000412 }
413 oldbufsize = bufsize;
hailfingerf31da3d2009-06-16 21:08:06 +0000414 }
415
uweb34ec9f2009-10-01 18:40:02 +0000416 /*
417 * Minimize USB transfers by packing as many commands as possible
418 * together. If we're not expecting to read, we can assert CS#, write,
419 * and deassert CS# all in one shot. If reading, we do three separate
stepana5807bf2009-09-16 08:26:59 +0000420 * operations.
421 */
snelson93932992010-01-10 01:08:37 +0000422 msg_pspew("Assert CS#\n");
hailfingerf31da3d2009-06-16 21:08:06 +0000423 buf[i++] = SET_BITS_LOW;
hailfinger888410e2010-07-29 15:54:53 +0000424 buf[i++] = 0 & ~cs_bits; /* assertive */
425 buf[i++] = pindir;
hailfingerf31da3d2009-06-16 21:08:06 +0000426
427 if (writecnt) {
428 buf[i++] = 0x11;
429 buf[i++] = (writecnt - 1) & 0xff;
430 buf[i++] = ((writecnt - 1) >> 8) & 0xff;
uweb34ec9f2009-10-01 18:40:02 +0000431 memcpy(buf + i, writearr, writecnt);
hailfingerf31da3d2009-06-16 21:08:06 +0000432 i += writecnt;
433 }
434
uweb34ec9f2009-10-01 18:40:02 +0000435 /*
436 * Optionally terminate this batch of commands with a
hailfingerf31da3d2009-06-16 21:08:06 +0000437 * read command, then do the fetch of the results.
438 */
439 if (readcnt) {
440 buf[i++] = 0x20;
441 buf[i++] = (readcnt - 1) & 0xff;
442 buf[i++] = ((readcnt - 1) >> 8) & 0xff;
443 ret = send_buf(ftdic, buf, i);
hailfinger322be712009-11-22 01:33:40 +0000444 failed = ret;
445 /* We can't abort here, we still have to deassert CS#. */
446 if (ret)
uwe8d342eb2011-07-28 08:13:25 +0000447 msg_perr("send_buf failed before read: %i\n", ret);
hailfingerf31da3d2009-06-16 21:08:06 +0000448 i = 0;
stepana5807bf2009-09-16 08:26:59 +0000449 if (ret == 0) {
uweb34ec9f2009-10-01 18:40:02 +0000450 /*
451 * FIXME: This is unreliable. There's no guarantee that
452 * we read the response directly after sending the read
453 * command. We may be scheduled out etc.
stepana5807bf2009-09-16 08:26:59 +0000454 */
455 ret = get_buf(ftdic, readarr, readcnt);
hailfinger322be712009-11-22 01:33:40 +0000456 failed |= ret;
457 /* We can't abort here either. */
458 if (ret)
snelson93932992010-01-10 01:08:37 +0000459 msg_perr("get_buf failed: %i\n", ret);
stepana5807bf2009-09-16 08:26:59 +0000460 }
hailfingerf31da3d2009-06-16 21:08:06 +0000461 }
462
snelson93932992010-01-10 01:08:37 +0000463 msg_pspew("De-assert CS#\n");
hailfingerf31da3d2009-06-16 21:08:06 +0000464 buf[i++] = SET_BITS_LOW;
hailfinger888410e2010-07-29 15:54:53 +0000465 buf[i++] = cs_bits;
466 buf[i++] = pindir;
hailfinger322be712009-11-22 01:33:40 +0000467 ret = send_buf(ftdic, buf, i);
468 failed |= ret;
469 if (ret)
snelson93932992010-01-10 01:08:37 +0000470 msg_perr("send_buf failed at end: %i\n", ret);
hailfingerf31da3d2009-06-16 21:08:06 +0000471
hailfinger322be712009-11-22 01:33:40 +0000472 return failed ? -1 : 0;
hailfingerf31da3d2009-06-16 21:08:06 +0000473}
474
hailfinger888410e2010-07-29 15:54:53 +0000475void print_supported_usbdevs(const struct usbdev_status *devs)
476{
uwe3dae6872010-07-29 19:57:55 +0000477 int i;
hailfinger888410e2010-07-29 15:54:53 +0000478
hailfingerf79d1712010-10-06 23:48:34 +0000479 msg_pinfo("USB devices:\n");
uwe3dae6872010-07-29 19:57:55 +0000480 for (i = 0; devs[i].vendor_name != NULL; i++) {
481 msg_pinfo("%s %s [%04x:%04x]%s\n", devs[i].vendor_name,
482 devs[i].device_name, devs[i].vendor_id,
483 devs[i].device_id,
484 (devs[i].status == NT) ? " (untested)" : "");
485 }
hailfinger888410e2010-07-29 15:54:53 +0000486}
487
hailfingerf31da3d2009-06-16 21:08:06 +0000488#endif