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