blob: 6e029c2b77a97945651f99ea753a8d0077b74ae6 [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"
29#include "spi.h"
Paul Fox05dfbe62009-06-16 21:08:06 +000030#include <ftdi.h>
31
Uwe Hermannc67d0372009-10-01 18:40:02 +000032/*
33 * The 'H' chips can run internally at either 12MHz or 60MHz.
34 * The non-H chips can only run at 12MHz.
35 */
Paul Fox05dfbe62009-06-16 21:08:06 +000036#define CLOCK_5X 1
37
Uwe Hermannc67d0372009-10-01 18:40:02 +000038/*
39 * In either case, the divisor is a simple integer clock divider.
40 * If CLOCK_5X is set, this divisor divides 30MHz, else it divides 6MHz.
41 */
42#define DIVIDE_BY 3 /* e.g. '3' will give either 10MHz or 2MHz SPI clock. */
Paul Fox05dfbe62009-06-16 21:08:06 +000043
Uwe Hermannc67d0372009-10-01 18:40:02 +000044#define BITMODE_BITBANG_NORMAL 1
45#define BITMODE_BITBANG_SPI 2
Paul Fox05dfbe62009-06-16 21:08:06 +000046
47static struct ftdi_context ftdic_context;
48
49int send_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size)
50{
51 int r;
52 r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
53 if (r < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +000054 msg_perr("ftdi_write_data: %d, %s\n", r,
Paul Fox05dfbe62009-06-16 21:08:06 +000055 ftdi_get_error_string(ftdic));
56 return 1;
57 }
58 return 0;
59}
60
61int get_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size)
62{
63 int r;
64 r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
65 if (r < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +000066 msg_perr("ftdi_read_data: %d, %s\n", r,
Paul Fox05dfbe62009-06-16 21:08:06 +000067 ftdi_get_error_string(ftdic));
68 return 1;
69 }
70 return 0;
71}
72
73int ft2232_spi_init(void)
74{
75 int f;
76 struct ftdi_context *ftdic = &ftdic_context;
77 unsigned char buf[512];
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000078 char *portpos = NULL;
79 int ft2232_type = FTDI_FT4232H;
80 enum ftdi_interface ft2232_interface = INTERFACE_B;
Paul Fox05dfbe62009-06-16 21:08:06 +000081
82 if (ftdi_init(ftdic) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +000083 msg_perr("ftdi_init failed\n");
Uwe Hermannc67d0372009-10-01 18:40:02 +000084 return EXIT_FAILURE; // TODO
Paul Fox05dfbe62009-06-16 21:08:06 +000085 }
86
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000087 if (programmer_param && !strlen(programmer_param)) {
88 free(programmer_param);
89 programmer_param = NULL;
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000090 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000091 if (programmer_param) {
92 if (strstr(programmer_param, "2232"))
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000093 ft2232_type = FTDI_FT2232H;
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000094 if (strstr(programmer_param, "4232"))
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000095 ft2232_type = FTDI_FT4232H;
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000096 portpos = strstr(programmer_param, "port=");
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000097 if (portpos) {
98 portpos += 5;
99 switch (toupper(*portpos)) {
100 case 'A':
101 ft2232_interface = INTERFACE_A;
102 break;
103 case 'B':
104 ft2232_interface = INTERFACE_B;
105 break;
106 default:
Sean Nelson34b5db72010-01-10 01:08:37 +0000107 msg_perr("Invalid interface specified, "
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000108 "using default.\n");
109 }
110 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000111 free(programmer_param);
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000112 }
Sean Nelson34b5db72010-01-10 01:08:37 +0000113 msg_pdbg("Using device type %s ",
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000114 (ft2232_type == FTDI_FT2232H) ? "2232H" : "4232H");
Sean Nelson34b5db72010-01-10 01:08:37 +0000115 msg_pdbg("interface %s\n",
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000116 (ft2232_interface == INTERFACE_A) ? "A" : "B");
117
118 f = ftdi_usb_open(ftdic, 0x0403, ft2232_type);
Paul Fox05dfbe62009-06-16 21:08:06 +0000119
120 if (f < 0 && f != -5) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000121 msg_perr("Unable to open FTDI device: %d (%s)\n", f,
Paul Fox05dfbe62009-06-16 21:08:06 +0000122 ftdi_get_error_string(ftdic));
Uwe Hermannc67d0372009-10-01 18:40:02 +0000123 exit(-1); // TODO
Paul Fox05dfbe62009-06-16 21:08:06 +0000124 }
125
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000126 if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000127 msg_perr("Unable to select interface: %s\n",
Paul Fox05dfbe62009-06-16 21:08:06 +0000128 ftdic->error_str);
129 }
130
131 if (ftdi_usb_reset(ftdic) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000132 msg_perr("Unable to reset FTDI device\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000133 }
134
135 if (ftdi_set_latency_timer(ftdic, 2) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000136 msg_perr("Unable to set latency timer\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000137 }
138
139 if (ftdi_write_data_set_chunksize(ftdic, 512)) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000140 msg_perr("Unable to set chunk size\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000141 }
142
Uwe Hermannc67d0372009-10-01 18:40:02 +0000143 if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000144 msg_perr("Unable to set bitmode to SPI\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000145 }
146
147#if CLOCK_5X
Sean Nelson34b5db72010-01-10 01:08:37 +0000148 msg_pdbg("Disable divide-by-5 front stage\n");
Uwe Hermannc67d0372009-10-01 18:40:02 +0000149 buf[0] = 0x8a; /* Disable divide-by-5. */
Paul Fox05dfbe62009-06-16 21:08:06 +0000150 if (send_buf(ftdic, buf, 1))
151 return -1;
152#define MPSSE_CLK 60.0
153
154#else
155
156#define MPSSE_CLK 12.0
157
158#endif
Sean Nelson34b5db72010-01-10 01:08:37 +0000159 msg_pdbg("Set clock divisor\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000160 buf[0] = 0x86; /* command "set divisor" */
161 /* valueL/valueH are (desired_divisor - 1) */
Uwe Hermannc67d0372009-10-01 18:40:02 +0000162 buf[1] = (DIVIDE_BY - 1) & 0xff;
163 buf[2] = ((DIVIDE_BY - 1) >> 8) & 0xff;
Paul Fox05dfbe62009-06-16 21:08:06 +0000164 if (send_buf(ftdic, buf, 3))
165 return -1;
166
Sean Nelson34b5db72010-01-10 01:08:37 +0000167 msg_pdbg("SPI clock is %fMHz\n",
Uwe Hermannc67d0372009-10-01 18:40:02 +0000168 (double)(MPSSE_CLK / (((DIVIDE_BY - 1) + 1) * 2)));
Paul Fox05dfbe62009-06-16 21:08:06 +0000169
Uwe Hermannc67d0372009-10-01 18:40:02 +0000170 /* Disconnect TDI/DO to TDO/DI for loopback. */
Sean Nelson34b5db72010-01-10 01:08:37 +0000171 msg_pdbg("No loopback of TDI/DO TDO/DI\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000172 buf[0] = 0x85;
173 if (send_buf(ftdic, buf, 1))
174 return -1;
175
Sean Nelson34b5db72010-01-10 01:08:37 +0000176 msg_pdbg("Set data bits\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000177 /* Set data bits low-byte command:
178 * value: 0x08 CS=high, DI=low, DO=low, SK=low
179 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
180 */
181#define CS_BIT 0x08
Paul Fox05dfbe62009-06-16 21:08:06 +0000182 buf[0] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000183 buf[1] = CS_BIT;
Paul Fox05dfbe62009-06-16 21:08:06 +0000184 buf[2] = 0x0b;
185 if (send_buf(ftdic, buf, 3))
186 return -1;
187
Sean Nelson34b5db72010-01-10 01:08:37 +0000188 // msg_pdbg("\nft2232 chosen\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000189
190 buses_supported = CHIP_BUSTYPE_SPI;
191 spi_controller = SPI_CONTROLLER_FT2232;
192
193 return 0;
194}
195
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000196int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Paul Fox05dfbe62009-06-16 21:08:06 +0000197 const unsigned char *writearr, unsigned char *readarr)
198{
199 struct ftdi_context *ftdic = &ftdic_context;
200 static unsigned char *buf = NULL;
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000201 /* failed is special. We use bitwise ops, but it is essentially bool. */
202 int i = 0, ret = 0, failed = 0;
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000203 int bufsize;
204 static int oldbufsize = 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000205
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000206 if (writecnt > 65536 || readcnt > 65536)
207 return SPI_INVALID_LENGTH;
208
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000209 /* buf is not used for the response from the chip. */
210 bufsize = max(writecnt + 9, 260 + 9);
211 /* Never shrink. realloc() calls are expensive. */
212 if (bufsize > oldbufsize) {
213 buf = realloc(buf, bufsize);
214 if (!buf) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000215 msg_perr("Out of memory!\n");
Carl-Daniel Hailfingerb7e01452009-11-25 16:58:17 +0000216 exit(1);
217 }
218 oldbufsize = bufsize;
Paul Fox05dfbe62009-06-16 21:08:06 +0000219 }
220
Uwe Hermannc67d0372009-10-01 18:40:02 +0000221 /*
222 * Minimize USB transfers by packing as many commands as possible
223 * together. If we're not expecting to read, we can assert CS#, write,
224 * and deassert CS# all in one shot. If reading, we do three separate
Stefan Reinauerab044b22009-09-16 08:26:59 +0000225 * operations.
226 */
Sean Nelson34b5db72010-01-10 01:08:37 +0000227 msg_pspew("Assert CS#\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000228 buf[i++] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000229 buf[i++] = 0 & ~CS_BIT; /* assertive */
Paul Fox05dfbe62009-06-16 21:08:06 +0000230 buf[i++] = 0x0b;
231
232 if (writecnt) {
233 buf[i++] = 0x11;
234 buf[i++] = (writecnt - 1) & 0xff;
235 buf[i++] = ((writecnt - 1) >> 8) & 0xff;
Uwe Hermannc67d0372009-10-01 18:40:02 +0000236 memcpy(buf + i, writearr, writecnt);
Paul Fox05dfbe62009-06-16 21:08:06 +0000237 i += writecnt;
238 }
239
Uwe Hermannc67d0372009-10-01 18:40:02 +0000240 /*
241 * Optionally terminate this batch of commands with a
Paul Fox05dfbe62009-06-16 21:08:06 +0000242 * read command, then do the fetch of the results.
243 */
244 if (readcnt) {
245 buf[i++] = 0x20;
246 buf[i++] = (readcnt - 1) & 0xff;
247 buf[i++] = ((readcnt - 1) >> 8) & 0xff;
248 ret = send_buf(ftdic, buf, i);
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000249 failed = ret;
250 /* We can't abort here, we still have to deassert CS#. */
251 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000252 msg_perr("send_buf failed before read: %i\n",
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000253 ret);
Paul Fox05dfbe62009-06-16 21:08:06 +0000254 i = 0;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000255 if (ret == 0) {
Uwe Hermannc67d0372009-10-01 18:40:02 +0000256 /*
257 * FIXME: This is unreliable. There's no guarantee that
258 * we read the response directly after sending the read
259 * command. We may be scheduled out etc.
Stefan Reinauerab044b22009-09-16 08:26:59 +0000260 */
261 ret = get_buf(ftdic, readarr, readcnt);
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000262 failed |= ret;
263 /* We can't abort here either. */
264 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000265 msg_perr("get_buf failed: %i\n", ret);
Stefan Reinauerab044b22009-09-16 08:26:59 +0000266 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000267 }
268
Sean Nelson34b5db72010-01-10 01:08:37 +0000269 msg_pspew("De-assert CS#\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000270 buf[i++] = SET_BITS_LOW;
Stefan Reinauerab044b22009-09-16 08:26:59 +0000271 buf[i++] = CS_BIT;
Paul Fox05dfbe62009-06-16 21:08:06 +0000272 buf[i++] = 0x0b;
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000273 ret = send_buf(ftdic, buf, i);
274 failed |= ret;
275 if (ret)
Sean Nelson34b5db72010-01-10 01:08:37 +0000276 msg_perr("send_buf failed at end: %i\n", ret);
Paul Fox05dfbe62009-06-16 21:08:06 +0000277
Carl-Daniel Hailfingera2441ce2009-11-22 01:33:40 +0000278 return failed ? -1 : 0;
Paul Fox05dfbe62009-06-16 21:08:06 +0000279}
280
281int ft2232_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
282{
283 /* Maximum read length is 64k bytes. */
284 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
285}
286
287int ft2232_spi_write_256(struct flashchip *flash, uint8_t *buf)
288{
289 int total_size = 1024 * flash->total_size;
290 int i;
291
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000292 spi_disable_blockprotect();
Uwe Hermannc67d0372009-10-01 18:40:02 +0000293 /* Erase first. */
Sean Nelson34b5db72010-01-10 01:08:37 +0000294 msg_pinfo("Erasing flash before programming... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000295 if (erase_flash(flash)) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000296 msg_perr("ERASE FAILED!\n");
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000297 return -1;
298 }
Sean Nelson34b5db72010-01-10 01:08:37 +0000299 msg_pinfo("done.\n");
300 msg_pdbg("total_size is %d\n", total_size);
Paul Fox05dfbe62009-06-16 21:08:06 +0000301 for (i = 0; i < total_size; i += 256) {
302 int l, r;
303 if (i + 256 <= total_size)
304 l = 256;
305 else
306 l = total_size - i;
307
Paul Fox05dfbe62009-06-16 21:08:06 +0000308 if ((r = spi_nbyte_program(i, &buf[i], l))) {
Sean Nelson34b5db72010-01-10 01:08:37 +0000309 msg_perr("%s: write fail %d\n", __func__, r);
Paul Fox05dfbe62009-06-16 21:08:06 +0000310 return 1;
311 }
Uwe Hermannc67d0372009-10-01 18:40:02 +0000312
Paul Fox05dfbe62009-06-16 21:08:06 +0000313 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
314 /* loop */;
315 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000316
317 return 0;
318}
319
Paul Fox05dfbe62009-06-16 21:08:06 +0000320#endif