blob: c8711e9128f74bacc28f2f0576a0082e4d44af24 [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>
24#include <stdint.h>
25#include <string.h>
26#include <stdlib.h>
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000027#include <ctype.h>
Paul Fox05dfbe62009-06-16 21:08:06 +000028#include "flash.h"
Sean Nelson14ba6682010-02-26 05:48:29 +000029#include "chipdrivers.h"
Paul Fox05dfbe62009-06-16 21:08:06 +000030#include "spi.h"
Paul Fox05dfbe62009-06-16 21:08:06 +000031#include <ftdi.h>
32
Uwe Hermannc67d0372009-10-01 18:40:02 +000033/*
34 * The 'H' chips can run internally at either 12MHz or 60MHz.
35 * The non-H chips can only run at 12MHz.
36 */
Paul Fox05dfbe62009-06-16 21:08:06 +000037#define CLOCK_5X 1
38
Uwe Hermannc67d0372009-10-01 18:40:02 +000039/*
40 * In either case, the divisor is a simple integer clock divider.
41 * If CLOCK_5X is set, this divisor divides 30MHz, else it divides 6MHz.
42 */
43#define DIVIDE_BY 3 /* e.g. '3' will give either 10MHz or 2MHz SPI clock. */
Paul Fox05dfbe62009-06-16 21:08:06 +000044
Uwe Hermannc67d0372009-10-01 18:40:02 +000045#define BITMODE_BITBANG_NORMAL 1
46#define BITMODE_BITBANG_SPI 2
Paul Fox05dfbe62009-06-16 21:08:06 +000047
48static struct ftdi_context ftdic_context;
49
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000050static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size)
Paul Fox05dfbe62009-06-16 21:08:06 +000051{
52 int r;
53 r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
54 if (r < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +000055 msg_perr("ftdi_write_data: %d, %s\n", r,
Paul Fox05dfbe62009-06-16 21:08:06 +000056 ftdi_get_error_string(ftdic));
57 return 1;
58 }
59 return 0;
60}
61
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000062static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size)
Paul Fox05dfbe62009-06-16 21:08:06 +000063{
64 int r;
65 r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
66 if (r < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +000067 msg_perr("ftdi_read_data: %d, %s\n", r,
Paul Fox05dfbe62009-06-16 21:08:06 +000068 ftdi_get_error_string(ftdic));
69 return 1;
70 }
71 return 0;
72}
73
74int ft2232_spi_init(void)
75{
76 int f;
77 struct ftdi_context *ftdic = &ftdic_context;
78 unsigned char buf[512];
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000079 int ft2232_type = FTDI_FT4232H;
80 enum ftdi_interface ft2232_interface = INTERFACE_B;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000081 char *arg;
82
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000083 arg = extract_programmer_param("type");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000084 if (arg) {
85 if (!strcasecmp(arg, "2232H"))
86 ft2232_type = FTDI_FT2232H;
87 else if (!strcasecmp(arg, "4232H"))
88 ft2232_type = FTDI_FT4232H;
89 else {
90 msg_perr("Error: Invalid device type specified.\n");
91 free(arg);
92 return 1;
93 }
94 }
95 free(arg);
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000096 arg = extract_programmer_param("port");
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +000097 if (arg) {
98 switch (toupper(*arg)) {
99 case 'A':
100 ft2232_interface = INTERFACE_A;
101 break;
102 case 'B':
103 ft2232_interface = INTERFACE_B;
104 break;
105 default:
106 msg_perr("Error: Invalid port/interface specified.\n");
107 free(arg);
108 return 1;
109 }
110 }
111 free(arg);
112 msg_pdbg("Using device type %s ",
113 (ft2232_type == FTDI_FT2232H) ? "2232H" : "4232H");
114 msg_pdbg("interface %s\n",
115 (ft2232_interface == INTERFACE_A) ? "A" : "B");
Paul Fox05dfbe62009-06-16 21:08:06 +0000116
117 if (ftdi_init(ftdic) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000118 msg_perr("ftdi_init failed\n");
Uwe Hermannc67d0372009-10-01 18:40:02 +0000119 return EXIT_FAILURE; // TODO
Paul Fox05dfbe62009-06-16 21:08:06 +0000120 }
121
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000122 f = ftdi_usb_open(ftdic, 0x0403, ft2232_type);
Paul Fox05dfbe62009-06-16 21:08:06 +0000123
124 if (f < 0 && f != -5) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000125 msg_perr("Unable to open FTDI device: %d (%s)\n", f,
Paul Fox05dfbe62009-06-16 21:08:06 +0000126 ftdi_get_error_string(ftdic));
Uwe Hermannc67d0372009-10-01 18:40:02 +0000127 exit(-1); // TODO
Paul Fox05dfbe62009-06-16 21:08:06 +0000128 }
129
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000130 if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000131 msg_perr("Unable to select interface: %s\n",
Paul Fox05dfbe62009-06-16 21:08:06 +0000132 ftdic->error_str);
133 }
134
135 if (ftdi_usb_reset(ftdic) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000136 msg_perr("Unable to reset FTDI device\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000137 }
138
139 if (ftdi_set_latency_timer(ftdic, 2) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000140 msg_perr("Unable to set latency timer\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000141 }
142
143 if (ftdi_write_data_set_chunksize(ftdic, 512)) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000144 msg_perr("Unable to set chunk size\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000145 }
146
Uwe Hermannc67d0372009-10-01 18:40:02 +0000147 if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000148 msg_perr("Unable to set bitmode to SPI\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000149 }
150
151#if CLOCK_5X
Sean Nelson34b5db72010-01-10 01:08:37 +0000152 msg_pdbg("Disable divide-by-5 front stage\n");
Uwe Hermannc67d0372009-10-01 18:40:02 +0000153 buf[0] = 0x8a; /* Disable divide-by-5. */
Paul Fox05dfbe62009-06-16 21:08:06 +0000154 if (send_buf(ftdic, buf, 1))
155 return -1;
156#define MPSSE_CLK 60.0
157
158#else
159
160#define MPSSE_CLK 12.0
161
162#endif
Sean Nelson34b5db72010-01-10 01:08:37 +0000163 msg_pdbg("Set clock divisor\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000164 buf[0] = 0x86; /* command "set divisor" */
165 /* valueL/valueH are (desired_divisor - 1) */
Uwe Hermannc67d0372009-10-01 18:40:02 +0000166 buf[1] = (DIVIDE_BY - 1) & 0xff;
167 buf[2] = ((DIVIDE_BY - 1) >> 8) & 0xff;
Paul Fox05dfbe62009-06-16 21:08:06 +0000168 if (send_buf(ftdic, buf, 3))
169 return -1;
170
Sean Nelson34b5db72010-01-10 01:08:37 +0000171 msg_pdbg("SPI clock is %fMHz\n",
Uwe Hermannc67d0372009-10-01 18:40:02 +0000172 (double)(MPSSE_CLK / (((DIVIDE_BY - 1) + 1) * 2)));
Paul Fox05dfbe62009-06-16 21:08:06 +0000173
Uwe Hermannc67d0372009-10-01 18:40:02 +0000174 /* Disconnect TDI/DO to TDO/DI for loopback. */
Sean Nelson34b5db72010-01-10 01:08:37 +0000175 msg_pdbg("No loopback of TDI/DO TDO/DI\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000176 buf[0] = 0x85;
177 if (send_buf(ftdic, buf, 1))
178 return -1;
179
Sean Nelson34b5db72010-01-10 01:08:37 +0000180 msg_pdbg("Set data bits\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000181 /* Set data bits low-byte command:
182 * value: 0x08 CS=high, DI=low, DO=low, SK=low
183 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
184 */
185#define CS_BIT 0x08
Paul Fox05dfbe62009-06-16 21:08:06 +0000186 buf[0] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000187 buf[1] = CS_BIT;
Paul Fox05dfbe62009-06-16 21:08:06 +0000188 buf[2] = 0x0b;
189 if (send_buf(ftdic, buf, 3))
190 return -1;
191
Sean Nelson34b5db72010-01-10 01:08:37 +0000192 // msg_pdbg("\nft2232 chosen\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000193
194 buses_supported = CHIP_BUSTYPE_SPI;
195 spi_controller = SPI_CONTROLLER_FT2232;
196
197 return 0;
198}
199
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000200int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Paul Fox05dfbe62009-06-16 21:08:06 +0000201 const unsigned char *writearr, unsigned char *readarr)
202{
203 struct ftdi_context *ftdic = &ftdic_context;
204 static unsigned char *buf = NULL;
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000205 /* failed is special. We use bitwise ops, but it is essentially bool. */
206 int i = 0, ret = 0, failed = 0;
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000207 int bufsize;
208 static int oldbufsize = 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000209
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000210 if (writecnt > 65536 || readcnt > 65536)
211 return SPI_INVALID_LENGTH;
212
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000213 /* buf is not used for the response from the chip. */
214 bufsize = max(writecnt + 9, 260 + 9);
215 /* Never shrink. realloc() calls are expensive. */
216 if (bufsize > oldbufsize) {
217 buf = realloc(buf, bufsize);
218 if (!buf) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000219 msg_perr("Out of memory!\n");
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000220 exit(1);
221 }
222 oldbufsize = bufsize;
Paul Fox05dfbe62009-06-16 21:08:06 +0000223 }
224
Uwe Hermannc67d0372009-10-01 18:40:02 +0000225 /*
226 * Minimize USB transfers by packing as many commands as possible
227 * together. If we're not expecting to read, we can assert CS#, write,
228 * and deassert CS# all in one shot. If reading, we do three separate
Stefan Reinauerab044b22009-09-16 08:26:59 +0000229 * operations.
230 */
Sean Nelson34b5db72010-01-10 01:08:37 +0000231 msg_pspew("Assert CS#\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000232 buf[i++] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000233 buf[i++] = 0 & ~CS_BIT; /* assertive */
Paul Fox05dfbe62009-06-16 21:08:06 +0000234 buf[i++] = 0x0b;
235
236 if (writecnt) {
237 buf[i++] = 0x11;
238 buf[i++] = (writecnt - 1) & 0xff;
239 buf[i++] = ((writecnt - 1) >> 8) & 0xff;
Uwe Hermannc67d0372009-10-01 18:40:02 +0000240 memcpy(buf + i, writearr, writecnt);
Paul Fox05dfbe62009-06-16 21:08:06 +0000241 i += writecnt;
242 }
243
Uwe Hermannc67d0372009-10-01 18:40:02 +0000244 /*
245 * Optionally terminate this batch of commands with a
Paul Fox05dfbe62009-06-16 21:08:06 +0000246 * read command, then do the fetch of the results.
247 */
248 if (readcnt) {
249 buf[i++] = 0x20;
250 buf[i++] = (readcnt - 1) & 0xff;
251 buf[i++] = ((readcnt - 1) >> 8) & 0xff;
252 ret = send_buf(ftdic, buf, i);
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000253 failed = ret;
254 /* We can't abort here, we still have to deassert CS#. */
255 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000256 msg_perr("send_buf failed before read: %i\n",
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000257 ret);
Paul Fox05dfbe62009-06-16 21:08:06 +0000258 i = 0;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000259 if (ret == 0) {
Uwe Hermannc67d0372009-10-01 18:40:02 +0000260 /*
261 * FIXME: This is unreliable. There's no guarantee that
262 * we read the response directly after sending the read
263 * command. We may be scheduled out etc.
Stefan Reinauerab044b22009-09-16 08:26:59 +0000264 */
265 ret = get_buf(ftdic, readarr, readcnt);
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000266 failed |= ret;
267 /* We can't abort here either. */
268 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000269 msg_perr("get_buf failed: %i\n", ret);
Stefan Reinauerab044b22009-09-16 08:26:59 +0000270 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000271 }
272
Sean Nelson34b5db72010-01-10 01:08:37 +0000273 msg_pspew("De-assert CS#\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000274 buf[i++] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000275 buf[i++] = CS_BIT;
Paul Fox05dfbe62009-06-16 21:08:06 +0000276 buf[i++] = 0x0b;
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000277 ret = send_buf(ftdic, buf, i);
278 failed |= ret;
279 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000280 msg_perr("send_buf failed at end: %i\n", ret);
Paul Fox05dfbe62009-06-16 21:08:06 +0000281
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000282 return failed ? -1 : 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000283}
284
285int ft2232_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
286{
287 /* Maximum read length is 64k bytes. */
288 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
289}
290
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000291int ft2232_spi_write_256(struct flashchip *flash, uint8_t *buf, int start, int len)
Paul Fox05dfbe62009-06-16 21:08:06 +0000292{
Carl-Daniel Hailfinger9a795d82010-07-14 16:19:05 +0000293 return spi_write_chunked(flash, buf, start, len, 256);
Paul Fox05dfbe62009-06-16 21:08:06 +0000294}
295
Paul Fox05dfbe62009-06-16 21:08:06 +0000296#endif