blob: db4db175a7e5c87f2c4b17a38ec845de37f3584e [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
21#include <stdio.h>
22#include <stdint.h>
23#include <string.h>
24#include <stdlib.h>
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000025#include <ctype.h>
Paul Fox05dfbe62009-06-16 21:08:06 +000026#include "flash.h"
27#include "spi.h"
28
29#if FT2232_SPI_SUPPORT == 1
30
31#include <ftdi.h>
32
33/* the 'H' chips can run internally at either 12Mhz or 60Mhz.
34 * the non-H chips can only run at 12Mhz. */
35#define CLOCK_5X 1
36
37/* in either case, the divisor is a simple integer clock divider.
38 * if CLOCK_5X is set, this divisor divides 30Mhz, else it
39 * divides 6Mhz */
40#define DIVIDE_BY 3 // e.g. '3' will give either 10Mhz or 2Mhz spi clock
41
42
43static struct ftdi_context ftdic_context;
44
45int send_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size)
46{
47 int r;
48 r = ftdi_write_data(ftdic, (unsigned char *) buf, size);
49 if (r < 0) {
50 fprintf(stderr, "ftdi_write_data: %d, %s\n", r,
51 ftdi_get_error_string(ftdic));
52 return 1;
53 }
54 return 0;
55}
56
57int get_buf(struct ftdi_context *ftdic, const unsigned char *buf, int size)
58{
59 int r;
60 r = ftdi_read_data(ftdic, (unsigned char *) buf, size);
61 if (r < 0) {
62 fprintf(stderr, "ftdi_read_data: %d, %s\n", r,
63 ftdi_get_error_string(ftdic));
64 return 1;
65 }
66 return 0;
67}
68
69int ft2232_spi_init(void)
70{
71 int f;
72 struct ftdi_context *ftdic = &ftdic_context;
73 unsigned char buf[512];
74 unsigned char port_val = 0;
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000075 char *portpos = NULL;
76 int ft2232_type = FTDI_FT4232H;
77 enum ftdi_interface ft2232_interface = INTERFACE_B;
Paul Fox05dfbe62009-06-16 21:08:06 +000078
79 if (ftdi_init(ftdic) < 0) {
80 fprintf(stderr, "ftdi_init failed\n");
81 return EXIT_FAILURE;
82 }
83
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000084 if (programmer_param && !strlen(programmer_param)) {
85 free(programmer_param);
86 programmer_param = NULL;
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000087 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000088 if (programmer_param) {
89 if (strstr(programmer_param, "2232"))
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000090 ft2232_type = FTDI_FT2232H;
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000091 if (strstr(programmer_param, "4232"))
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000092 ft2232_type = FTDI_FT4232H;
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000093 portpos = strstr(programmer_param, "port=");
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +000094 if (portpos) {
95 portpos += 5;
96 switch (toupper(*portpos)) {
97 case 'A':
98 ft2232_interface = INTERFACE_A;
99 break;
100 case 'B':
101 ft2232_interface = INTERFACE_B;
102 break;
103 default:
104 fprintf(stderr, "Invalid interface specified, "
105 "using default.\n");
106 }
107 }
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +0000108 free(programmer_param);
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000109 }
110 printf_debug("Using device type %s ",
111 (ft2232_type == FTDI_FT2232H) ? "2232H" : "4232H");
112 printf_debug("interface %s\n",
113 (ft2232_interface == INTERFACE_A) ? "A" : "B");
114
115 f = ftdi_usb_open(ftdic, 0x0403, ft2232_type);
Paul Fox05dfbe62009-06-16 21:08:06 +0000116
117 if (f < 0 && f != -5) {
118 fprintf(stderr, "Unable to open ftdi device: %d (%s)\n", f,
119 ftdi_get_error_string(ftdic));
120 exit(-1);
121 }
122
Carl-Daniel Hailfingerfeea2722009-07-01 00:02:23 +0000123 if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
124 fprintf(stderr, "Unable to select interface: %s\n",
Paul Fox05dfbe62009-06-16 21:08:06 +0000125 ftdic->error_str);
126 }
127
128 if (ftdi_usb_reset(ftdic) < 0) {
129 fprintf(stderr, "Unable to reset ftdi device\n");
130 }
131
132 if (ftdi_set_latency_timer(ftdic, 2) < 0) {
133 fprintf(stderr, "Unable to set latency timer\n");
134 }
135
136 if (ftdi_write_data_set_chunksize(ftdic, 512)) {
137 fprintf(stderr, "Unable to set chunk size\n");
138 }
139
140 if (ftdi_set_bitmode(ftdic, 0x00, 2) < 0) {
141 fprintf(stderr, "Unable to set bitmode\n");
142 }
143
144#if CLOCK_5X
145 printf_debug("Disable divide-by-5 front stage\n");
146 buf[0] = 0x8a; /* disable divide-by-5 */
147 if (send_buf(ftdic, buf, 1))
148 return -1;
149#define MPSSE_CLK 60.0
150
151#else
152
153#define MPSSE_CLK 12.0
154
155#endif
156 printf_debug("Set clock divisor\n");
157 buf[0] = 0x86; /* command "set divisor" */
158 /* valueL/valueH are (desired_divisor - 1) */
159 buf[1] = (DIVIDE_BY-1) & 0xff;
160 buf[2] = ((DIVIDE_BY-1) >> 8) & 0xff;
161 if (send_buf(ftdic, buf, 3))
162 return -1;
163
164 printf("SPI clock is %fMHz\n",
165 (double)(MPSSE_CLK / (((DIVIDE_BY-1) + 1) * 2)));
166
167 /* Disconnect TDI/DO to TDO/DI for Loopback */
168 printf_debug("No loopback of tdi/do tdo/di\n");
169 buf[0] = 0x85;
170 if (send_buf(ftdic, buf, 1))
171 return -1;
172
173 printf_debug("Set data bits\n");
174 /* Set data bits low-byte command:
175 * value: 0x08 CS=high, DI=low, DO=low, SK=low
176 * dir: 0x0b CS=output, DI=input, DO=output, SK=output
177 */
178#define CS_BIT 0x08
179
180 buf[0] = SET_BITS_LOW;
181 buf[1] = (port_val = CS_BIT);
182 buf[2] = 0x0b;
183 if (send_buf(ftdic, buf, 3))
184 return -1;
185
186 printf_debug("\nft2232 chosen\n");
187
188 buses_supported = CHIP_BUSTYPE_SPI;
189 spi_controller = SPI_CONTROLLER_FT2232;
190
191 return 0;
192}
193
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000194int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Paul Fox05dfbe62009-06-16 21:08:06 +0000195 const unsigned char *writearr, unsigned char *readarr)
196{
197 struct ftdi_context *ftdic = &ftdic_context;
198 static unsigned char *buf = NULL;
199 unsigned char port_val = 0;
200 int i, ret = 0;
201
Carl-Daniel Hailfinger142e30f2009-07-14 10:26:56 +0000202 if (writecnt > 65536 || readcnt > 65536)
203 return SPI_INVALID_LENGTH;
204
Paul Fox05dfbe62009-06-16 21:08:06 +0000205 buf = realloc(buf, writecnt + readcnt + 100);
206 if (!buf) {
207 fprintf(stderr, "Out of memory!\n");
208 exit(1);
209 }
210
211 i = 0;
212
213 /* minimize USB transfers by packing as many commands
214 * as possible together. if we're not expecting to
215 * read, we can assert CS, write, and deassert CS all
216 * in one shot. if reading, we do three separate
217 * operations. */
218 printf_debug("Assert CS#\n");
219 buf[i++] = SET_BITS_LOW;
220 buf[i++] = (port_val &= ~CS_BIT);
221 buf[i++] = 0x0b;
222
223 if (writecnt) {
224 buf[i++] = 0x11;
225 buf[i++] = (writecnt - 1) & 0xff;
226 buf[i++] = ((writecnt - 1) >> 8) & 0xff;
227 memcpy(buf+i, writearr, writecnt);
228 i += writecnt;
229 }
230
231 /* optionally terminate this batch of commands with a
232 * read command, then do the fetch of the results.
233 */
234 if (readcnt) {
235 buf[i++] = 0x20;
236 buf[i++] = (readcnt - 1) & 0xff;
237 buf[i++] = ((readcnt - 1) >> 8) & 0xff;
238 ret = send_buf(ftdic, buf, i);
239 i = 0;
240 if (ret) goto deassert_cs;
241
242 /* FIXME: This is unreliable. There's no guarantee that we read
243 * the response directly after sending the read command.
244 * We may be scheduled out etc.
245 */
246 ret = get_buf(ftdic, readarr, readcnt);
247
248 }
249
Uwe Hermann1432a602009-06-28 23:26:37 +0000250deassert_cs:
Paul Fox05dfbe62009-06-16 21:08:06 +0000251 printf_debug("De-assert CS#\n");
252 buf[i++] = SET_BITS_LOW;
253 buf[i++] = (port_val |= CS_BIT);
254 buf[i++] = 0x0b;
255 if (send_buf(ftdic, buf, i))
256 return -1;
257
258 return ret;
259}
260
261int ft2232_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
262{
263 /* Maximum read length is 64k bytes. */
264 return spi_read_chunked(flash, buf, start, len, 64 * 1024);
265}
266
267int ft2232_spi_write_256(struct flashchip *flash, uint8_t *buf)
268{
269 int total_size = 1024 * flash->total_size;
270 int i;
271
Carl-Daniel Hailfinger116081a2009-08-10 02:29:21 +0000272 spi_disable_blockprotect();
273 /* Erase first */
274 printf("Erasing flash before programming... ");
275 if (flash->erase(flash)) {
276 fprintf(stderr, "ERASE FAILED!\n");
277 return -1;
278 }
279 printf("done.\n");
Paul Fox05dfbe62009-06-16 21:08:06 +0000280 printf_debug("total_size is %d\n", total_size);
281 for (i = 0; i < total_size; i += 256) {
282 int l, r;
283 if (i + 256 <= total_size)
284 l = 256;
285 else
286 l = total_size - i;
287
Paul Fox05dfbe62009-06-16 21:08:06 +0000288 if ((r = spi_nbyte_program(i, &buf[i], l))) {
289 fprintf(stderr, "%s: write fail %d\n", __FUNCTION__, r);
Paul Fox05dfbe62009-06-16 21:08:06 +0000290 return 1;
291 }
292
293 while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP)
294 /* loop */;
295 }
Paul Fox05dfbe62009-06-16 21:08:06 +0000296
297 return 0;
298}
299
300#else
301int ft2232_spi_init(void)
302{
303 fprintf(stderr, "FT2232 SPI support was not compiled in\n");
304 exit(1);
305}
306
Carl-Daniel Hailfingerd0478292009-07-10 21:08:55 +0000307int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt,
Paul Fox05dfbe62009-06-16 21:08:06 +0000308 const unsigned char *writearr, unsigned char *readarr)
309{
310 fprintf(stderr, "FT2232 SPI support was not compiled in\n");
311 exit(1);
312}
313
314int ft2232_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len)
315{
316 fprintf(stderr, "FT2232 SPI support was not compiled in\n");
317 exit(1);
318}
319
320int ft2232_spi_write_256(struct flashchip *flash, uint8_t *buf)
321{
322 fprintf(stderr, "FT2232 SPI support was not compiled in\n");
323 exit(1);
324}
325#endif