blob: 7a52718b62a61dd9ae13f87f2330b6197a414a3e [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.
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
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000021#if CONFIG_FT2232_SPI == 1
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +000022
Paul Fox05dfbe62009-06-16 21:08:06 +000023#include <stdio.h>
Paul Fox05dfbe62009-06-16 21:08:06 +000024#include <string.h>
25#include <stdlib.h>
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000026#include <ctype.h>
Paul Fox05dfbe62009-06-16 21:08:06 +000027#include "flash.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000028#include "programmer.h"
Paul Fox05dfbe62009-06-16 21:08:06 +000029#include "spi.h"
Paul Fox05dfbe62009-06-16 21:08:06 +000030#include <ftdi.h>
31
Uwe Hermann836b26a2011-10-14 20:33:14 +000032/* Please keep sorted by vendor ID, then device ID. */
33
Uwe Hermann70145292010-07-29 19:57:55 +000034#define FTDI_VID 0x0403
35#define FTDI_FT2232H_PID 0x6010
36#define FTDI_FT4232H_PID 0x6011
Uwe Hermann836b26a2011-10-14 20:33:14 +000037#define TIAO_TUMPA_PID 0x8a98
Uwe Hermann70145292010-07-29 19:57:55 +000038#define AMONTEC_JTAGKEY_PID 0xCFF8
Jörg Fischer6529b9f2010-07-29 15:54:53 +000039
Alex Badeacaf2d422010-11-10 03:26:57 +000040#define FIC_VID 0x1457
41#define OPENMOKO_DBGBOARD_PID 0x5118
42
Pete Batardc0207062011-06-11 12:21:37 +000043#define OLIMEX_VID 0x15BA
44#define OLIMEX_ARM_OCD_PID 0x0003
45#define OLIMEX_ARM_TINY_PID 0x0004
46#define OLIMEX_ARM_OCD_H_PID 0x002B
47#define OLIMEX_ARM_TINY_H_PID 0x002A
48
Jörg Fischer6529b9f2010-07-29 15:54:53 +000049const struct usbdev_status devs_ft2232spi[] = {
Uwe Hermann70145292010-07-29 19:57:55 +000050 {FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"},
51 {FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"},
Uwe Hermann836b26a2011-10-14 20:33:14 +000052 {FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
Uwe Hermann70145292010-07-29 19:57:55 +000053 {FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
Pete Batardc0207062011-06-11 12:21:37 +000054 {FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC",
Alex Badeacaf2d422010-11-10 03:26:57 +000055 "OpenMoko Neo1973 Debug board (V2+)"},
Pete Batardc0207062011-06-11 12:21:37 +000056 {OLIMEX_VID, OLIMEX_ARM_OCD_PID, NT, "Olimex", "ARM-USB-OCD"},
57 {OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"},
58 {OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, NT, "Olimex", "ARM-USB-OCD-H"},
59 {OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, NT, "Olimex", "ARM-USB-TINY-H"},
Uwe Hermann70145292010-07-29 19:57:55 +000060 {},
Jörg Fischer6529b9f2010-07-29 15:54:53 +000061};
62
Uwe Hermannc67d0372009-10-01 18:40:02 +000063/*
64 * The 'H' chips can run internally at either 12MHz or 60MHz.
65 * The non-H chips can only run at 12MHz.
66 */
Alex Badea0b94d052010-11-10 03:18:41 +000067static uint8_t clock_5x = 1;
Paul Fox05dfbe62009-06-16 21:08:06 +000068
Uwe Hermannc67d0372009-10-01 18:40:02 +000069/*
70 * In either case, the divisor is a simple integer clock divider.
Alex Badea0b94d052010-11-10 03:18:41 +000071 * If clock_5x is set, this divisor divides 30MHz, else it divides 6MHz.
Uwe Hermannc67d0372009-10-01 18:40:02 +000072 */
73#define DIVIDE_BY 3 /* e.g. '3' will give either 10MHz or 2MHz SPI clock. */
Paul Fox05dfbe62009-06-16 21:08:06 +000074
Uwe Hermannc67d0372009-10-01 18:40:02 +000075#define BITMODE_BITBANG_NORMAL 1
76#define BITMODE_BITBANG_SPI 2
Paul Fox05dfbe62009-06-16 21:08:06 +000077
Jörg Fischer6529b9f2010-07-29 15:54:53 +000078/* Set data bits low-byte command:
79 * value: 0x08 CS=high, DI=low, DO=low, SK=low
80 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
81 *
82 * JTAGkey(2) needs to enable its output via Bit4 / GPIOL0
83 * value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low
84 * dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output
Jörg Fischer6529b9f2010-07-29 15:54:53 +000085 */
Uwe Hermann70145292010-07-29 19:57:55 +000086static uint8_t cs_bits = 0x08;
87static uint8_t pindir = 0x0b;
Paul Fox05dfbe62009-06-16 21:08:06 +000088static struct ftdi_context ftdic_context;
89
Jörg Fischer6529b9f2010-07-29 15:54:53 +000090static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
91{
92 int i;
Uwe Hermann70145292010-07-29 19:57:55 +000093 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
Jörg Fischer6529b9f2010-07-29 15:54:53 +000094 if ((devs_ft2232spi[i].device_id == ft2232_type)
95 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
96 return devs_ft2232spi[i].device_name;
97 }
98 return "unknown device";
99}
100
101static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type)
102{
103 int i;
Uwe Hermann70145292010-07-29 19:57:55 +0000104 for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
105 if ((devs_ft2232spi[i].device_id == ft2232_type)
106 && (devs_ft2232spi[i].vendor_id == ft2232_vid))
107 return devs_ft2232spi[i].vendor_name;
108 }
109 return "unknown vendor";
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000110}
111
Uwe Hermann70145292010-07-29 19:57:55 +0000112static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf,
113 int size)
Paul Fox05dfbe62009-06-16 21:08:06 +0000114{
115 int r;
116 r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
117 if (r < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000118 msg_perr("ftdi_write_data: %d, %s\n", r,
Paul Fox05dfbe62009-06-16 21:08:06 +0000119 ftdi_get_error_string(ftdic));
120 return 1;
121 }
122 return 0;
123}
124
Uwe Hermann70145292010-07-29 19:57:55 +0000125static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf,
126 int size)
Paul Fox05dfbe62009-06-16 21:08:06 +0000127{
128 int r;
Alex Badeab9556e02010-11-10 03:10:41 +0000129
130 while (size > 0) {
131 r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
132 if (r < 0) {
133 msg_perr("ftdi_read_data: %d, %s\n", r,
134 ftdi_get_error_string(ftdic));
135 return 1;
136 }
137 buf += r;
138 size -= r;
Paul Fox05dfbe62009-06-16 21:08:06 +0000139 }
140 return 0;
141}
142
Michael Karcherb9dbe482011-05-11 17:07:07 +0000143static int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
144 const unsigned char *writearr, unsigned char *readarr);
145
146static const struct spi_programmer spi_programmer_ft2232 = {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000147 .type = SPI_CONTROLLER_FT2232,
148 .max_data_read = 64 * 1024,
149 .max_data_write = 256,
150 .command = ft2232_spi_send_command,
151 .multicommand = default_spi_send_multicommand,
152 .read = default_spi_read,
153 .write_256 = default_spi_write_256,
Michael Karcherb9dbe482011-05-11 17:07:07 +0000154};
155
Uwe Hermann274a20d2011-07-21 09:18:18 +0000156/* Returns 0 upon success, a negative number upon errors. */
Paul Fox05dfbe62009-06-16 21:08:06 +0000157int ft2232_spi_init(void)
158{
Uwe Hermann274a20d2011-07-21 09:18:18 +0000159 int f, ret = 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000160 struct ftdi_context *ftdic = &ftdic_context;
161 unsigned char buf[512];
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000162 int ft2232_vid = FTDI_VID;
163 int ft2232_type = FTDI_FT4232H_PID;
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000164 enum ftdi_interface ft2232_interface = INTERFACE_B;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000165 char *arg;
Alex Badea0b94d052010-11-10 03:18:41 +0000166 double mpsse_clk;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000167
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000168 arg = extract_programmer_param("type");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000169 if (arg) {
170 if (!strcasecmp(arg, "2232H"))
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000171 ft2232_type = FTDI_FT2232H_PID;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000172 else if (!strcasecmp(arg, "4232H"))
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000173 ft2232_type = FTDI_FT4232H_PID;
174 else if (!strcasecmp(arg, "jtagkey")) {
175 ft2232_type = AMONTEC_JTAGKEY_PID;
176 ft2232_interface = INTERFACE_A;
177 cs_bits = 0x18;
Uwe Hermann70145292010-07-29 19:57:55 +0000178 pindir = 0x1b;
Uwe Hermann836b26a2011-10-14 20:33:14 +0000179 } else if (!strcasecmp(arg, "tumpa")) {
180 /* Interface A is SPI1, B is SPI2. */
181 ft2232_type = TIAO_TUMPA_PID;
182 ft2232_interface = INTERFACE_A;
Steve Markgraf0528b7f2011-08-12 01:19:32 +0000183 } else if (!strcasecmp(arg, "busblaster")) {
184 /* In its default configuration it is a jtagkey clone */
185 ft2232_type = FTDI_FT2232H_PID;
186 ft2232_interface = INTERFACE_A;
187 cs_bits = 0x18;
188 pindir = 0x1b;
Alex Badeacaf2d422010-11-10 03:26:57 +0000189 } else if (!strcasecmp(arg, "openmoko")) {
190 ft2232_vid = FIC_VID;
191 ft2232_type = OPENMOKO_DBGBOARD_PID;
192 ft2232_interface = INTERFACE_A;
Pete Batardc0207062011-06-11 12:21:37 +0000193 } else if (!strcasecmp(arg, "arm-usb-ocd")) {
194 ft2232_vid = OLIMEX_VID;
195 ft2232_type = OLIMEX_ARM_OCD_PID;
196 ft2232_interface = INTERFACE_A;
197 } else if (!strcasecmp(arg, "arm-usb-tiny")) {
198 ft2232_vid = OLIMEX_VID;
199 ft2232_type = OLIMEX_ARM_TINY_PID;
200 ft2232_interface = INTERFACE_A;
201 } else if (!strcasecmp(arg, "arm-usb-ocd-h")) {
202 ft2232_vid = OLIMEX_VID;
203 ft2232_type = OLIMEX_ARM_OCD_H_PID;
204 ft2232_interface = INTERFACE_A;
205 } else if (!strcasecmp(arg, "arm-usb-tiny-h")) {
206 ft2232_vid = OLIMEX_VID;
207 ft2232_type = OLIMEX_ARM_TINY_H_PID;
208 ft2232_interface = INTERFACE_A;
Alex Badeacaf2d422010-11-10 03:26:57 +0000209 } else {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000210 msg_perr("Error: Invalid device type specified.\n");
211 free(arg);
Uwe Hermann274a20d2011-07-21 09:18:18 +0000212 return -1;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000213 }
214 }
215 free(arg);
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000216 arg = extract_programmer_param("port");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000217 if (arg) {
Carl-Daniel Hailfinger9e3a6c42010-10-08 12:40:09 +0000218 switch (toupper((unsigned char)*arg)) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000219 case 'A':
220 ft2232_interface = INTERFACE_A;
221 break;
222 case 'B':
223 ft2232_interface = INTERFACE_B;
224 break;
225 default:
226 msg_perr("Error: Invalid port/interface specified.\n");
227 free(arg);
Uwe Hermann274a20d2011-07-21 09:18:18 +0000228 return -2;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000229 }
230 }
231 free(arg);
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000232 msg_pdbg("Using device type %s %s ",
Uwe Hermann70145292010-07-29 19:57:55 +0000233 get_ft2232_vendorname(ft2232_vid, ft2232_type),
234 get_ft2232_devicename(ft2232_vid, ft2232_type));
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000235 msg_pdbg("interface %s\n",
Uwe Hermann70145292010-07-29 19:57:55 +0000236 (ft2232_interface == INTERFACE_A) ? "A" : "B");
Paul Fox05dfbe62009-06-16 21:08:06 +0000237
238 if (ftdi_init(ftdic) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000239 msg_perr("ftdi_init failed\n");
Uwe Hermann274a20d2011-07-21 09:18:18 +0000240 return -3;
Paul Fox05dfbe62009-06-16 21:08:06 +0000241 }
242
Alex Badeab1b459c2010-11-10 03:22:39 +0000243 f = ftdi_usb_open(ftdic, ft2232_vid, ft2232_type);
Paul Fox05dfbe62009-06-16 21:08:06 +0000244
245 if (f < 0 && f != -5) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000246 msg_perr("Unable to open FTDI device: %d (%s)\n", f,
Paul Fox05dfbe62009-06-16 21:08:06 +0000247 ftdi_get_error_string(ftdic));
Uwe Hermann274a20d2011-07-21 09:18:18 +0000248 return -4;
Paul Fox05dfbe62009-06-16 21:08:06 +0000249 }
250
Alex Badea0b94d052010-11-10 03:18:41 +0000251 if (ftdic->type != TYPE_2232H && ftdic->type != TYPE_4232H) {
252 msg_pdbg("FTDI chip type %d is not high-speed\n",
253 ftdic->type);
254 clock_5x = 0;
255 }
256
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000257 if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000258 msg_perr("Unable to select interface: %s\n",
Paul Fox05dfbe62009-06-16 21:08:06 +0000259 ftdic->error_str);
260 }
261
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000262 if (ftdi_usb_reset(ftdic) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000263 msg_perr("Unable to reset FTDI device\n");
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000264 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000265
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000266 if (ftdi_set_latency_timer(ftdic, 2) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000267 msg_perr("Unable to set latency timer\n");
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000268 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000269
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000270 if (ftdi_write_data_set_chunksize(ftdic, 256)) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000271 msg_perr("Unable to set chunk size\n");
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000272 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000273
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000274 if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000275 msg_perr("Unable to set bitmode to SPI\n");
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000276 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000277
Alex Badea0b94d052010-11-10 03:18:41 +0000278 if (clock_5x) {
279 msg_pdbg("Disable divide-by-5 front stage\n");
280 buf[0] = 0x8a; /* Disable divide-by-5. */
Uwe Hermann274a20d2011-07-21 09:18:18 +0000281 if (send_buf(ftdic, buf, 1)) {
282 ret = -5;
283 goto ftdi_err;
284 }
Alex Badea0b94d052010-11-10 03:18:41 +0000285 mpsse_clk = 60.0;
286 } else {
287 mpsse_clk = 12.0;
288 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000289
Sean Nelson34b5db72010-01-10 01:08:37 +0000290 msg_pdbg("Set clock divisor\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000291 buf[0] = 0x86; /* command "set divisor" */
292 /* valueL/valueH are (desired_divisor - 1) */
Uwe Hermannc67d0372009-10-01 18:40:02 +0000293 buf[1] = (DIVIDE_BY - 1) & 0xff;
294 buf[2] = ((DIVIDE_BY - 1) >> 8) & 0xff;
Uwe Hermann274a20d2011-07-21 09:18:18 +0000295 if (send_buf(ftdic, buf, 3)) {
296 ret = -6;
297 goto ftdi_err;
298 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000299
Alex Badea0b94d052010-11-10 03:18:41 +0000300 msg_pdbg("MPSSE clock: %f MHz divisor: %d "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000301 "SPI clock: %f MHz\n", mpsse_clk, DIVIDE_BY,
Alex Badea0b94d052010-11-10 03:18:41 +0000302 (double)(mpsse_clk / (((DIVIDE_BY - 1) + 1) * 2)));
Paul Fox05dfbe62009-06-16 21:08:06 +0000303
Uwe Hermannc67d0372009-10-01 18:40:02 +0000304 /* Disconnect TDI/DO to TDO/DI for loopback. */
Sean Nelson34b5db72010-01-10 01:08:37 +0000305 msg_pdbg("No loopback of TDI/DO TDO/DI\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000306 buf[0] = 0x85;
Uwe Hermann274a20d2011-07-21 09:18:18 +0000307 if (send_buf(ftdic, buf, 1)) {
308 ret = -7;
309 goto ftdi_err;
310 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000311
Sean Nelson34b5db72010-01-10 01:08:37 +0000312 msg_pdbg("Set data bits\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000313 buf[0] = SET_BITS_LOW;
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000314 buf[1] = cs_bits;
315 buf[2] = pindir;
Uwe Hermann274a20d2011-07-21 09:18:18 +0000316 if (send_buf(ftdic, buf, 3)) {
317 ret = -8;
318 goto ftdi_err;
319 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000320
Sean Nelson34b5db72010-01-10 01:08:37 +0000321 // msg_pdbg("\nft2232 chosen\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000322
Michael Karcherb9dbe482011-05-11 17:07:07 +0000323 register_spi_programmer(&spi_programmer_ft2232);
Paul Fox05dfbe62009-06-16 21:08:06 +0000324
325 return 0;
Uwe Hermann274a20d2011-07-21 09:18:18 +0000326
327ftdi_err:
328 if ((f = ftdi_usb_close(ftdic)) < 0) {
329 msg_perr("Unable to close FTDI device: %d (%s)\n", f,
330 ftdi_get_error_string(ftdic));
331 }
332
333 return ret;
Paul Fox05dfbe62009-06-16 21:08:06 +0000334}
335
Uwe Hermann274a20d2011-07-21 09:18:18 +0000336/* Returns 0 upon success, a negative number upon errors. */
Michael Karcherb9dbe482011-05-11 17:07:07 +0000337static int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Paul Fox05dfbe62009-06-16 21:08:06 +0000338 const unsigned char *writearr, unsigned char *readarr)
339{
340 struct ftdi_context *ftdic = &ftdic_context;
341 static unsigned char *buf = NULL;
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000342 /* failed is special. We use bitwise ops, but it is essentially bool. */
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000343 int i = 0, ret = 0, failed = 0;
344 int bufsize;
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000345 static int oldbufsize = 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000346
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000347 if (writecnt > 65536 || readcnt > 65536)
348 return SPI_INVALID_LENGTH;
349
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000350 /* buf is not used for the response from the chip. */
351 bufsize = max(writecnt + 9, 260 + 9);
352 /* Never shrink. realloc() calls are expensive. */
353 if (bufsize > oldbufsize) {
354 buf = realloc(buf, bufsize);
355 if (!buf) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000356 msg_perr("Out of memory!\n");
Uwe Hermann274a20d2011-07-21 09:18:18 +0000357 /* TODO: What to do with buf? */
358 return SPI_GENERIC_ERROR;
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000359 }
360 oldbufsize = bufsize;
Paul Fox05dfbe62009-06-16 21:08:06 +0000361 }
362
Uwe Hermannc67d0372009-10-01 18:40:02 +0000363 /*
364 * Minimize USB transfers by packing as many commands as possible
365 * together. If we're not expecting to read, we can assert CS#, write,
366 * and deassert CS# all in one shot. If reading, we do three separate
Stefan Reinauerab044b22009-09-16 08:26:59 +0000367 * operations.
368 */
Sean Nelson34b5db72010-01-10 01:08:37 +0000369 msg_pspew("Assert CS#\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000370 buf[i++] = SET_BITS_LOW;
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000371 buf[i++] = 0 & ~cs_bits; /* assertive */
372 buf[i++] = pindir;
Paul Fox05dfbe62009-06-16 21:08:06 +0000373
374 if (writecnt) {
375 buf[i++] = 0x11;
376 buf[i++] = (writecnt - 1) & 0xff;
377 buf[i++] = ((writecnt - 1) >> 8) & 0xff;
Uwe Hermannc67d0372009-10-01 18:40:02 +0000378 memcpy(buf + i, writearr, writecnt);
Paul Fox05dfbe62009-06-16 21:08:06 +0000379 i += writecnt;
380 }
381
Uwe Hermannc67d0372009-10-01 18:40:02 +0000382 /*
383 * Optionally terminate this batch of commands with a
Paul Fox05dfbe62009-06-16 21:08:06 +0000384 * read command, then do the fetch of the results.
385 */
386 if (readcnt) {
387 buf[i++] = 0x20;
388 buf[i++] = (readcnt - 1) & 0xff;
389 buf[i++] = ((readcnt - 1) >> 8) & 0xff;
390 ret = send_buf(ftdic, buf, i);
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000391 failed = ret;
392 /* We can't abort here, we still have to deassert CS#. */
393 if (ret)
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000394 msg_perr("send_buf failed before read: %i\n", ret);
Paul Fox05dfbe62009-06-16 21:08:06 +0000395 i = 0;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000396 if (ret == 0) {
Uwe Hermannc67d0372009-10-01 18:40:02 +0000397 /*
398 * FIXME: This is unreliable. There's no guarantee that
399 * we read the response directly after sending the read
400 * command. We may be scheduled out etc.
Stefan Reinauerab044b22009-09-16 08:26:59 +0000401 */
402 ret = get_buf(ftdic, readarr, readcnt);
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000403 failed |= ret;
404 /* We can't abort here either. */
405 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000406 msg_perr("get_buf failed: %i\n", ret);
Stefan Reinauerab044b22009-09-16 08:26:59 +0000407 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000408 }
409
Sean Nelson34b5db72010-01-10 01:08:37 +0000410 msg_pspew("De-assert CS#\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000411 buf[i++] = SET_BITS_LOW;
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000412 buf[i++] = cs_bits;
413 buf[i++] = pindir;
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000414 ret = send_buf(ftdic, buf, i);
415 failed |= ret;
416 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000417 msg_perr("send_buf failed at end: %i\n", ret);
Paul Fox05dfbe62009-06-16 21:08:06 +0000418
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000419 return failed ? -1 : 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000420}
421
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000422void print_supported_usbdevs(const struct usbdev_status *devs)
423{
Uwe Hermann70145292010-07-29 19:57:55 +0000424 int i;
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000425
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +0000426 msg_pinfo("USB devices:\n");
Uwe Hermann70145292010-07-29 19:57:55 +0000427 for (i = 0; devs[i].vendor_name != NULL; i++) {
428 msg_pinfo("%s %s [%04x:%04x]%s\n", devs[i].vendor_name,
429 devs[i].device_name, devs[i].vendor_id,
430 devs[i].device_id,
431 (devs[i].status == NT) ? " (untested)" : "");
432 }
Jörg Fischer6529b9f2010-07-29 15:54:53 +0000433}
434
Paul Fox05dfbe62009-06-16 21:08:06 +0000435#endif