blob: 97b4867fb4e3449816e06b06428268a796ce8363 [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>
5 * Copyright (C) 2009 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
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +000021#if FT2232_SPI_SUPPORT == 1
22
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
50int send_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size)
51{
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
62int get_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size)
63{
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 char *portpos = NULL;
80 int ft2232_type = FTDI_FT4232H;
81 enum ftdi_interface ft2232_interface = INTERFACE_B;
Paul Fox05dfbe62009-06-16 21:08:06 +000082
83 if (ftdi_init(ftdic) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +000084 msg_perr("ftdi_init failed\n");
Uwe Hermannc67d0372009-10-01 18:40:02 +000085 return EXIT_FAILURE; // TODO
Paul Fox05dfbe62009-06-16 21:08:06 +000086 }
87
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000088 if (programmer_param && !strlen(programmer_param)) {
89 free(programmer_param);
90 programmer_param = NULL;
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000091 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000092 if (programmer_param) {
93 if (strstr(programmer_param, "2232"))
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000094 ft2232_type = FTDI_FT2232H;
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000095 if (strstr(programmer_param, "4232"))
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000096 ft2232_type = FTDI_FT4232H;
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000097 portpos = strstr(programmer_param, "port=");
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000098 if (portpos) {
99 portpos += 5;
100 switch (toupper(*portpos)) {
101 case 'A':
102 ft2232_interface = INTERFACE_A;
103 break;
104 case 'B':
105 ft2232_interface = INTERFACE_B;
106 break;
107 default:
Sean Nelson34b5db72010-01-10 01:08:37 +0000108 msg_perr("Invalid interface specified, "
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000109 "using default.\n");
110 }
111 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000112 free(programmer_param);
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000113 }
Sean Nelson34b5db72010-01-10 01:08:37 +0000114 msg_pdbg("Using device type %s ",
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000115 (ft2232_type == FTDI_FT2232H) ? "2232H" : "4232H");
Sean Nelson34b5db72010-01-10 01:08:37 +0000116 msg_pdbg("interface %s\n",
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000117 (ft2232_interface == INTERFACE_A) ? "A" : "B");
118
119 f = ftdi_usb_open(ftdic, 0x0403, ft2232_type);
Paul Fox05dfbe62009-06-16 21:08:06 +0000120
121 if (f < 0 && f != -5) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000122 msg_perr("Unable to open FTDI device: %d (%s)\n", f,
Paul Fox05dfbe62009-06-16 21:08:06 +0000123 ftdi_get_error_string(ftdic));
Uwe Hermannc67d0372009-10-01 18:40:02 +0000124 exit(-1); // TODO
Paul Fox05dfbe62009-06-16 21:08:06 +0000125 }
126
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000127 if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000128 msg_perr("Unable to select interface: %s\n",
Paul Fox05dfbe62009-06-16 21:08:06 +0000129 ftdic->error_str);
130 }
131
132 if (ftdi_usb_reset(ftdic) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000133 msg_perr("Unable to reset FTDI device\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000134 }
135
136 if (ftdi_set_latency_timer(ftdic, 2) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000137 msg_perr("Unable to set latency timer\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000138 }
139
140 if (ftdi_write_data_set_chunksize(ftdic, 512)) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000141 msg_perr("Unable to set chunk size\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000142 }
143
Uwe Hermannc67d0372009-10-01 18:40:02 +0000144 if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000145 msg_perr("Unable to set bitmode to SPI\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000146 }
147
148#if CLOCK_5X
Sean Nelson34b5db72010-01-10 01:08:37 +0000149 msg_pdbg("Disable divide-by-5 front stage\n");
Uwe Hermannc67d0372009-10-01 18:40:02 +0000150 buf[0] = 0x8a; /* Disable divide-by-5. */
Paul Fox05dfbe62009-06-16 21:08:06 +0000151 if (send_buf(ftdic, buf, 1))
152 return -1;
153#define MPSSE_CLK 60.0
154
155#else
156
157#define MPSSE_CLK 12.0
158
159#endif
Sean Nelson34b5db72010-01-10 01:08:37 +0000160 msg_pdbg("Set clock divisor\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000161 buf[0] = 0x86; /* command "set divisor" */
162 /* valueL/valueH are (desired_divisor - 1) */
Uwe Hermannc67d0372009-10-01 18:40:02 +0000163 buf[1] = (DIVIDE_BY - 1) & 0xff;
164 buf[2] = ((DIVIDE_BY - 1) >> 8) & 0xff;
Paul Fox05dfbe62009-06-16 21:08:06 +0000165 if (send_buf(ftdic, buf, 3))
166 return -1;
167
Sean Nelson34b5db72010-01-10 01:08:37 +0000168 msg_pdbg("SPI clock is %fMHz\n",
Uwe Hermannc67d0372009-10-01 18:40:02 +0000169 (double)(MPSSE_CLK / (((DIVIDE_BY - 1) + 1) * 2)));
Paul Fox05dfbe62009-06-16 21:08:06 +0000170
Uwe Hermannc67d0372009-10-01 18:40:02 +0000171 /* Disconnect TDI/DO to TDO/DI for loopback. */
Sean Nelson34b5db72010-01-10 01:08:37 +0000172 msg_pdbg("No loopback of TDI/DO TDO/DI\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000173 buf[0] = 0x85;
174 if (send_buf(ftdic, buf, 1))
175 return -1;
176
Sean Nelson34b5db72010-01-10 01:08:37 +0000177 msg_pdbg("Set data bits\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000178 /* Set data bits low-byte command:
179 * value: 0x08 CS=high, DI=low, DO=low, SK=low
180 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
181 */
182#define CS_BIT 0x08
Paul Fox05dfbe62009-06-16 21:08:06 +0000183 buf[0] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000184 buf[1] = CS_BIT;
Paul Fox05dfbe62009-06-16 21:08:06 +0000185 buf[2] = 0x0b;
186 if (send_buf(ftdic, buf, 3))
187 return -1;
188
Sean Nelson34b5db72010-01-10 01:08:37 +0000189 // msg_pdbg("\nft2232 chosen\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000190
191 buses_supported = CHIP_BUSTYPE_SPI;
192 spi_controller = SPI_CONTROLLER_FT2232;
193
194 return 0;
195}
196
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000197int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Paul Fox05dfbe62009-06-16 21:08:06 +0000198 const unsigned char *writearr, unsigned char *readarr)
199{
200 struct ftdi_context *ftdic = &ftdic_context;
201 static unsigned char *buf = NULL;
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000202 /* failed is special. We use bitwise ops, but it is essentially bool. */
203 int i = 0, ret = 0, failed = 0;
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000204 int bufsize;
205 static int oldbufsize = 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000206
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000207 if (writecnt > 65536 || readcnt > 65536)
208 return SPI_INVALID_LENGTH;
209
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000210 /* buf is not used for the response from the chip. */
211 bufsize = max(writecnt + 9, 260 + 9);
212 /* Never shrink. realloc() calls are expensive. */
213 if (bufsize > oldbufsize) {
214 buf = realloc(buf, bufsize);
215 if (!buf) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000216 msg_perr("Out of memory!\n");
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000217 exit(1);
218 }
219 oldbufsize = bufsize;
Paul Fox05dfbe62009-06-16 21:08:06 +0000220 }
221
Uwe Hermannc67d0372009-10-01 18:40:02 +0000222 /*
223 * Minimize USB transfers by packing as many commands as possible
224 * together. If we're not expecting to read, we can assert CS#, write,
225 * and deassert CS# all in one shot. If reading, we do three separate
Stefan Reinauerab044b22009-09-16 08:26:59 +0000226 * operations.
227 */
Sean Nelson34b5db72010-01-10 01:08:37 +0000228 msg_pspew("Assert CS#\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000229 buf[i++] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000230 buf[i++] = 0 & ~CS_BIT; /* assertive */
Paul Fox05dfbe62009-06-16 21:08:06 +0000231 buf[i++] = 0x0b;
232
233 if (writecnt) {
234 buf[i++] = 0x11;
235 buf[i++] = (writecnt - 1) & 0xff;
236 buf[i++] = ((writecnt - 1) >> 8) & 0xff;
Uwe Hermannc67d0372009-10-01 18:40:02 +0000237 memcpy(buf + i, writearr, writecnt);
Paul Fox05dfbe62009-06-16 21:08:06 +0000238 i += writecnt;
239 }
240
Uwe Hermannc67d0372009-10-01 18:40:02 +0000241 /*
242 * Optionally terminate this batch of commands with a
Paul Fox05dfbe62009-06-16 21:08:06 +0000243 * read command, then do the fetch of the results.
244 */
245 if (readcnt) {
246 buf[i++] = 0x20;
247 buf[i++] = (readcnt - 1) & 0xff;
248 buf[i++] = ((readcnt - 1) >> 8) & 0xff;
249 ret = send_buf(ftdic, buf, i);
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000250 failed = ret;
251 /* We can't abort here, we still have to deassert CS#. */
252 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000253 msg_perr("send_buf failed before read: %i\n",
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000254 ret);
Paul Fox05dfbe62009-06-16 21:08:06 +0000255 i = 0;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000256 if (ret == 0) {
Uwe Hermannc67d0372009-10-01 18:40:02 +0000257 /*
258 * FIXME: This is unreliable. There's no guarantee that
259 * we read the response directly after sending the read
260 * command. We may be scheduled out etc.
Stefan Reinauerab044b22009-09-16 08:26:59 +0000261 */
262 ret = get_buf(ftdic, readarr, readcnt);
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000263 failed |= ret;
264 /* We can't abort here either. */
265 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000266 msg_perr("get_buf failed: %i\n", ret);
Stefan Reinauerab044b22009-09-16 08:26:59 +0000267 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000268 }
269
Sean Nelson34b5db72010-01-10 01:08:37 +0000270 msg_pspew("De-assert CS#\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000271 buf[i++] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000272 buf[i++] = CS_BIT;
Paul Fox05dfbe62009-06-16 21:08:06 +0000273 buf[i++] = 0x0b;
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000274 ret = send_buf(ftdic, buf, i);
275 failed |= ret;
276 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000277 msg_perr("send_buf failed at end: %i\n", ret);
Paul Fox05dfbe62009-06-16 21:08:06 +0000278
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000279 return failed ? -1 : 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000280}
281
282int ft2232_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
283{
284 /* Maximum read length is 64k bytes. */
285 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
286}
287
288int ft2232_spi_write_256(struct flashchip *flash, uint8_t *buf)
289{
290 int total_size = 1024 * flash->total_size;
291 int i;
292
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000293 spi_disable_blockprotect();
Uwe Hermannc67d0372009-10-01 18:40:02 +0000294 /* Erase first. */
Sean Nelson34b5db72010-01-10 01:08:37 +0000295 msg_pinfo("Erasing flash before programming... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000296 if (erase_flash(flash)) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000297 msg_perr("ERASE FAILED!\n");
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000298 return -1;
299 }
Sean Nelson34b5db72010-01-10 01:08:37 +0000300 msg_pinfo("done.\n");
301 msg_pdbg("total_size is %d\n", total_size);
Paul Fox05dfbe62009-06-16 21:08:06 +0000302 for (i = 0; i < total_size; i += 256) {
303 int l, r;
304 if (i + 256 <= total_size)
305 l = 256;
306 else
307 l = total_size - i;
308
Paul Fox05dfbe62009-06-16 21:08:06 +0000309 if ((r = spi_nbyte_program(i, &buf[i], l))) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000310 msg_perr("%s: write fail %d\n", __func__, r);
Paul Fox05dfbe62009-06-16 21:08:06 +0000311 return 1;
312 }
Uwe Hermannc67d0372009-10-01 18:40:02 +0000313
Paul Fox05dfbe62009-06-16 21:08:06 +0000314 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
315 /* loop */;
316 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000317
318 return 0;
319}
320
Paul Fox05dfbe62009-06-16 21:08:06 +0000321#endif