flashrom 

flashrom Svn Source Tree

Root/trunk/ft2232_spi.c

1/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Paul Fox <pgf@laptop.org>
5 * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * 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
21#if CONFIG_FT2232_SPI == 1
22
23#include <stdio.h>
24#include <strings.h>
25#include <string.h>
26#include <stdlib.h>
27#include <ctype.h>
28#include "flash.h"
29#include "programmer.h"
30#include "spi.h"
31#include <ftdi.h>
32
33/* This is not defined in libftdi.h <0.20 (c7e4c09e68cfa6f5e112334aa1b3bb23401c8dc7 to be exact).
34 * Some tests indicate that his is the only change that it is needed to support the FT232H in flashrom. */
35#if !defined(HAVE_FT232H)
36#define TYPE_232H6
37#endif
38
39/* Please keep sorted by vendor ID, then device ID. */
40
41#define FTDI_VID0x0403
42#define FTDI_FT2232H_PID0x6010
43#define FTDI_FT4232H_PID0x6011
44#define FTDI_FT232H_PID0x6014
45#define TIAO_TUMPA_PID0x8a98
46#define TIAO_TUMPA_LITE_PID0x8a99
47#define AMONTEC_JTAGKEY_PID0xCFF8
48
49#define GOEPEL_VID0x096C
50#define GOEPEL_PICOTAP_PID0x1449
51
52#define FIC_VID0x1457
53#define OPENMOKO_DBGBOARD_PID0x5118
54
55#define OLIMEX_VID0x15BA
56#define OLIMEX_ARM_OCD_PID0x0003
57#define OLIMEX_ARM_TINY_PID0x0004
58#define OLIMEX_ARM_OCD_H_PID0x002B
59#define OLIMEX_ARM_TINY_H_PID0x002A
60
61#define GOOGLE_VID0x18D1
62#define GOOGLE_SERVO_PID0x5001
63#define GOOGLE_SERVO_V2_PID00x5002
64#define GOOGLE_SERVO_V2_PID10x5003
65
66const struct dev_entry devs_ft2232spi[] = {
67{FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"},
68{FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"},
69{FTDI_VID, FTDI_FT232H_PID, OK, "FTDI", "FT232H"},
70{FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"},
71{FTDI_VID, TIAO_TUMPA_LITE_PID, OK, "TIAO", "USB Multi-Protocol Adapter Lite"},
72{FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"},
73{GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"},
74{GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"},
75{GOOGLE_VID, GOOGLE_SERVO_V2_PID0, OK, "Google", "Servo V2 Legacy"},
76{GOOGLE_VID, GOOGLE_SERVO_V2_PID1, OK, "Google", "Servo V2"},
77{FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC", "OpenMoko Neo1973 Debug board (V2+)"},
78{OLIMEX_VID, OLIMEX_ARM_OCD_PID, OK, "Olimex", "ARM-USB-OCD"},
79{OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"},
80{OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, OK, "Olimex", "ARM-USB-OCD-H"},
81{OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, OK, "Olimex", "ARM-USB-TINY-H"},
82
83{0},
84};
85
86#define DEFAULT_DIVISOR 2
87
88#define BITMODE_BITBANG_NORMAL1
89#define BITMODE_BITBANG_SPI2
90
91/* The variables cs_bits and pindir store the values for the "set data bits low byte" MPSSE command that
92 * sets the initial state and the direction of the I/O pins. The pin offsets are as follows:
93 * SCK is bit 0.
94 * DO is bit 1.
95 * DI is bit 2.
96 * CS is bit 3.
97 *
98 * The default values (set below) are used for most devices:
99 * value: 0x08 CS=high, DI=low, DO=low, SK=low
100 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
101 */
102static uint8_t cs_bits = 0x08;
103static uint8_t pindir = 0x0b;
104static struct ftdi_context ftdic_context;
105
106static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
107{
108int i;
109for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
110if ((devs_ft2232spi[i].device_id == ft2232_type) && (devs_ft2232spi[i].vendor_id == ft2232_vid))
111return devs_ft2232spi[i].device_name;
112}
113return "unknown device";
114}
115
116static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type)
117{
118int i;
119for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) {
120if ((devs_ft2232spi[i].device_id == ft2232_type) && (devs_ft2232spi[i].vendor_id == ft2232_vid))
121return devs_ft2232spi[i].vendor_name;
122}
123return "unknown vendor";
124}
125
126static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf,
127 int size)
128{
129int r;
130r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
131if (r < 0) {
132msg_perr("ftdi_write_data: %d, %s\n", r, ftdi_get_error_string(ftdic));
133return 1;
134}
135return 0;
136}
137
138static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf,
139 int size)
140{
141int r;
142
143while (size > 0) {
144r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
145if (r < 0) {
146msg_perr("ftdi_read_data: %d, %s\n", r, ftdi_get_error_string(ftdic));
147return 1;
148}
149buf += r;
150size -= r;
151}
152return 0;
153}
154
155static int ft2232_spi_send_command(struct flashctx *flash,
156 unsigned int writecnt, unsigned int readcnt,
157 const unsigned char *writearr,
158 unsigned char *readarr);
159
160static const struct spi_master spi_master_ft2232 = {
161.type= SPI_CONTROLLER_FT2232,
162.max_data_read= 64 * 1024,
163.max_data_write= 256,
164.command= ft2232_spi_send_command,
165.multicommand= default_spi_send_multicommand,
166.read= default_spi_read,
167.write_256= default_spi_write_256,
168.write_aai= default_spi_write_aai,
169};
170
171/* Returns 0 upon success, a negative number upon errors. */
172int ft2232_spi_init(void)
173{
174int ret = 0;
175struct ftdi_context *ftdic = &ftdic_context;
176unsigned char buf[512];
177int ft2232_vid = FTDI_VID;
178int ft2232_type = FTDI_FT4232H_PID;
179int channel_count = 4; /* Stores the number of channels of the device. */
180enum ftdi_interface ft2232_interface = INTERFACE_A;
181/*
182 * The 'H' chips can run with an internal clock of either 12 MHz or 60 MHz,
183 * but the non-H chips can only run at 12 MHz. We enable the divide-by-5
184 * prescaler on the former to run on the same speed.
185 */
186uint8_t clock_5x = 1;
187/* In addition to the prescaler mentioned above there is also another
188 * configurable one on all versions of the chips. Its divisor div can be
189 * set by a 16 bit value x according to the following formula:
190 * div = (1 + x) * 2 <-> x = div / 2 - 1
191 * Hence the expressible divisors are all even numbers between 2 and
192 * 2^17 (=131072) resulting in SCK frequencies of 6 MHz down to about
193 * 92 Hz for 12 MHz inputs.
194 */
195uint32_t divisor = DEFAULT_DIVISOR;
196int f;
197char *arg;
198double mpsse_clk;
199
200arg = extract_programmer_param("type");
201if (arg) {
202if (!strcasecmp(arg, "2232H")) {
203ft2232_type = FTDI_FT2232H_PID;
204channel_count = 2;
205} else if (!strcasecmp(arg, "4232H")) {
206ft2232_type = FTDI_FT4232H_PID;
207channel_count = 4;
208} else if (!strcasecmp(arg, "232H")) {
209ft2232_type = FTDI_FT232H_PID;
210channel_count = 1;
211} else if (!strcasecmp(arg, "jtagkey")) {
212ft2232_type = AMONTEC_JTAGKEY_PID;
213channel_count = 2;
214/* JTAGkey(2) needs to enable its output via Bit4 / GPIOL0
215* value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low
216* dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output */
217cs_bits = 0x18;
218pindir = 0x1b;
219} else if (!strcasecmp(arg, "picotap")) {
220ft2232_vid = GOEPEL_VID;
221ft2232_type = GOEPEL_PICOTAP_PID;
222channel_count = 2;
223} else if (!strcasecmp(arg, "tumpa")) {
224/* Interface A is SPI1, B is SPI2. */
225ft2232_type = TIAO_TUMPA_PID;
226channel_count = 2;
227} else if (!strcasecmp(arg, "tumpalite")) {
228/* Only one channel is used on lite edition */
229ft2232_type = TIAO_TUMPA_LITE_PID;
230channel_count = 1;
231} else if (!strcasecmp(arg, "busblaster")) {
232/* In its default configuration it is a jtagkey clone */
233ft2232_type = FTDI_FT2232H_PID;
234channel_count = 2;
235cs_bits = 0x18;
236pindir = 0x1b;
237} else if (!strcasecmp(arg, "openmoko")) {
238ft2232_vid = FIC_VID;
239ft2232_type = OPENMOKO_DBGBOARD_PID;
240channel_count = 2;
241} else if (!strcasecmp(arg, "arm-usb-ocd")) {
242ft2232_vid = OLIMEX_VID;
243ft2232_type = OLIMEX_ARM_OCD_PID;
244channel_count = 2;
245/* arm-usb-ocd(-h) has an output buffer that needs to be enabled by pulling ADBUS4 low.
246* value: 0x08 #OE=low, CS=high, DI=low, DO=low, SK=low
247* dir: 0x1b #OE=output, CS=output, DI=input, DO=output, SK=output */
248cs_bits = 0x08;
249pindir = 0x1b;
250} else if (!strcasecmp(arg, "arm-usb-tiny")) {
251ft2232_vid = OLIMEX_VID;
252ft2232_type = OLIMEX_ARM_TINY_PID;
253channel_count = 2;
254} else if (!strcasecmp(arg, "arm-usb-ocd-h")) {
255ft2232_vid = OLIMEX_VID;
256ft2232_type = OLIMEX_ARM_OCD_H_PID;
257channel_count = 2;
258/* See arm-usb-ocd */
259cs_bits = 0x08;
260pindir = 0x1b;
261} else if (!strcasecmp(arg, "arm-usb-tiny-h")) {
262ft2232_vid = OLIMEX_VID;
263ft2232_type = OLIMEX_ARM_TINY_H_PID;
264channel_count = 2;
265} else if (!strcasecmp(arg, "google-servo")) {
266ft2232_vid = GOOGLE_VID;
267ft2232_type = GOOGLE_SERVO_PID;
268} else if (!strcasecmp(arg, "google-servo-v2")) {
269ft2232_vid = GOOGLE_VID;
270ft2232_type = GOOGLE_SERVO_V2_PID1;
271/* Default divisor is too fast, and chip ID fails */
272divisor = 6;
273} else if (!strcasecmp(arg, "google-servo-v2-legacy")) {
274ft2232_vid = GOOGLE_VID;
275ft2232_type = GOOGLE_SERVO_V2_PID0;
276} else {
277msg_perr("Error: Invalid device type specified.\n");
278free(arg);
279return -1;
280}
281}
282free(arg);
283
284arg = extract_programmer_param("port");
285if (arg) {
286switch (toupper((unsigned char)*arg)) {
287case 'A':
288ft2232_interface = INTERFACE_A;
289break;
290case 'B':
291ft2232_interface = INTERFACE_B;
292if (channel_count < 2)
293channel_count = -1;
294break;
295case 'C':
296ft2232_interface = INTERFACE_C;
297if (channel_count < 3)
298channel_count = -1;
299break;
300case 'D':
301ft2232_interface = INTERFACE_D;
302if (channel_count < 4)
303channel_count = -1;
304break;
305default:
306channel_count = -1;
307break;
308}
309if (channel_count < 0 || strlen(arg) != 1) {
310msg_perr("Error: Invalid channel/port/interface specified: \"%s\".\n", arg);
311free(arg);
312return -2;
313}
314}
315free(arg);
316
317arg = extract_programmer_param("divisor");
318if (arg && strlen(arg)) {
319unsigned int temp = 0;
320char *endptr;
321temp = strtoul(arg, &endptr, 10);
322if (*endptr || temp < 2 || temp > 131072 || temp & 0x1) {
323msg_perr("Error: Invalid SPI frequency divisor specified: \"%s\".\n"
324 "Valid are even values between 2 and 131072.\n", arg);
325free(arg);
326return -2;
327} else {
328divisor = (uint32_t)temp;
329}
330}
331free(arg);
332
333msg_pdbg("Using device type %s %s ",
334 get_ft2232_vendorname(ft2232_vid, ft2232_type),
335 get_ft2232_devicename(ft2232_vid, ft2232_type));
336msg_pdbg("channel %s.\n",
337 (ft2232_interface == INTERFACE_A) ? "A" :
338 (ft2232_interface == INTERFACE_B) ? "B" :
339 (ft2232_interface == INTERFACE_C) ? "C" : "D");
340
341if (ftdi_init(ftdic) < 0) {
342msg_perr("ftdi_init failed.\n");
343return -3;
344}
345
346if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
347msg_perr("Unable to select channel (%s).\n", ftdi_get_error_string(ftdic));
348}
349
350arg = extract_programmer_param("serial");
351f = ftdi_usb_open_desc(ftdic, ft2232_vid, ft2232_type, NULL, arg);
352free(arg);
353
354if (f < 0 && f != -5) {
355msg_perr("Unable to open FTDI device: %d (%s).\n", f, ftdi_get_error_string(ftdic));
356return -4;
357}
358
359if (ftdic->type != TYPE_2232H && ftdic->type != TYPE_4232H && ftdic->type != TYPE_232H) {
360msg_pdbg("FTDI chip type %d is not high-speed.\n", ftdic->type);
361clock_5x = 0;
362}
363
364if (ftdi_usb_reset(ftdic) < 0) {
365msg_perr("Unable to reset FTDI device (%s).\n", ftdi_get_error_string(ftdic));
366}
367
368if (ftdi_set_latency_timer(ftdic, 2) < 0) {
369msg_perr("Unable to set latency timer (%s).\n", ftdi_get_error_string(ftdic));
370}
371
372if (ftdi_write_data_set_chunksize(ftdic, 256)) {
373msg_perr("Unable to set chunk size (%s).\n", ftdi_get_error_string(ftdic));
374}
375
376if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
377msg_perr("Unable to set bitmode to SPI (%s).\n", ftdi_get_error_string(ftdic));
378}
379
380if (clock_5x) {
381msg_pdbg("Disable divide-by-5 front stage\n");
382buf[0] = 0x8a; /* Disable divide-by-5. DIS_DIV_5 in newer libftdi */
383if (send_buf(ftdic, buf, 1)) {
384ret = -5;
385goto ftdi_err;
386}
387mpsse_clk = 60.0;
388} else {
389mpsse_clk = 12.0;
390}
391
392msg_pdbg("Set clock divisor\n");
393buf[0] = TCK_DIVISOR;
394buf[1] = (divisor / 2 - 1) & 0xff;
395buf[2] = ((divisor / 2 - 1) >> 8) & 0xff;
396if (send_buf(ftdic, buf, 3)) {
397ret = -6;
398goto ftdi_err;
399}
400
401msg_pdbg("MPSSE clock: %f MHz, divisor: %u, SPI clock: %f MHz\n",
402 mpsse_clk, divisor, (double)(mpsse_clk / divisor));
403
404/* Disconnect TDI/DO to TDO/DI for loopback. */
405msg_pdbg("No loopback of TDI/DO TDO/DI\n");
406buf[0] = LOOPBACK_END;
407if (send_buf(ftdic, buf, 1)) {
408ret = -7;
409goto ftdi_err;
410}
411
412msg_pdbg("Set data bits\n");
413buf[0] = SET_BITS_LOW;
414buf[1] = cs_bits;
415buf[2] = pindir;
416if (send_buf(ftdic, buf, 3)) {
417ret = -8;
418goto ftdi_err;
419}
420
421register_spi_master(&spi_master_ft2232);
422
423return 0;
424
425ftdi_err:
426if ((f = ftdi_usb_close(ftdic)) < 0) {
427msg_perr("Unable to close FTDI device: %d (%s)\n", f, ftdi_get_error_string(ftdic));
428}
429return ret;
430}
431
432/* Returns 0 upon success, a negative number upon errors. */
433static int ft2232_spi_send_command(struct flashctx *flash,
434 unsigned int writecnt, unsigned int readcnt,
435 const unsigned char *writearr,
436 unsigned char *readarr)
437{
438struct ftdi_context *ftdic = &ftdic_context;
439static unsigned char *buf = NULL;
440/* failed is special. We use bitwise ops, but it is essentially bool. */
441int i = 0, ret = 0, failed = 0;
442int bufsize;
443static int oldbufsize = 0;
444
445if (writecnt > 65536 || readcnt > 65536)
446return SPI_INVALID_LENGTH;
447
448/* buf is not used for the response from the chip. */
449bufsize = max(writecnt + 9, 260 + 9);
450/* Never shrink. realloc() calls are expensive. */
451if (bufsize > oldbufsize) {
452buf = realloc(buf, bufsize);
453if (!buf) {
454msg_perr("Out of memory!\n");
455/* TODO: What to do with buf? */
456return SPI_GENERIC_ERROR;
457}
458oldbufsize = bufsize;
459}
460
461/*
462 * Minimize USB transfers by packing as many commands as possible
463 * together. If we're not expecting to read, we can assert CS#, write,
464 * and deassert CS# all in one shot. If reading, we do three separate
465 * operations.
466 */
467msg_pspew("Assert CS#\n");
468buf[i++] = SET_BITS_LOW;
469buf[i++] = 0 & ~cs_bits; /* assertive */
470buf[i++] = pindir;
471
472if (writecnt) {
473buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG;
474buf[i++] = (writecnt - 1) & 0xff;
475buf[i++] = ((writecnt - 1) >> 8) & 0xff;
476memcpy(buf + i, writearr, writecnt);
477i += writecnt;
478}
479
480/*
481 * Optionally terminate this batch of commands with a
482 * read command, then do the fetch of the results.
483 */
484if (readcnt) {
485buf[i++] = MPSSE_DO_READ;
486buf[i++] = (readcnt - 1) & 0xff;
487buf[i++] = ((readcnt - 1) >> 8) & 0xff;
488ret = send_buf(ftdic, buf, i);
489failed = ret;
490/* We can't abort here, we still have to deassert CS#. */
491if (ret)
492msg_perr("send_buf failed before read: %i\n", ret);
493i = 0;
494if (ret == 0) {
495/*
496 * FIXME: This is unreliable. There's no guarantee that
497 * we read the response directly after sending the read
498 * command. We may be scheduled out etc.
499 */
500ret = get_buf(ftdic, readarr, readcnt);
501failed |= ret;
502/* We can't abort here either. */
503if (ret)
504msg_perr("get_buf failed: %i\n", ret);
505}
506}
507
508msg_pspew("De-assert CS#\n");
509buf[i++] = SET_BITS_LOW;
510buf[i++] = cs_bits;
511buf[i++] = pindir;
512ret = send_buf(ftdic, buf, i);
513failed |= ret;
514if (ret)
515msg_perr("send_buf failed at end: %i\n", ret);
516
517return failed ? -1 : 0;
518}
519
520#endif

Archive Download this file

Revision: HEAD