Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 1 | /* |
| 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 Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 21 | #if FT2232_SPI_SUPPORT == 1 |
| 22 | |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <stdint.h> |
| 25 | #include <string.h> |
| 26 | #include <stdlib.h> |
Carl-Daniel Hailfinger | feea272 | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 27 | #include <ctype.h> |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 28 | #include "flash.h" |
| 29 | #include "spi.h" |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 30 | #include <ftdi.h> |
| 31 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 32 | /* |
| 33 | * The 'H' chips can run internally at either 12MHz or 60MHz. |
| 34 | * The non-H chips can only run at 12MHz. |
| 35 | */ |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 36 | #define CLOCK_5X 1 |
| 37 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 38 | /* |
| 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 Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 43 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 44 | #define BITMODE_BITBANG_NORMAL 1 |
| 45 | #define BITMODE_BITBANG_SPI 2 |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 46 | |
| 47 | static struct ftdi_context ftdic_context; |
| 48 | |
| 49 | int 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) { |
| 54 | fprintf(stderr, "ftdi_write_data: %d, %s\n", r, |
| 55 | ftdi_get_error_string(ftdic)); |
| 56 | return 1; |
| 57 | } |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | int 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) { |
| 66 | fprintf(stderr, "ftdi_read_data: %d, %s\n", r, |
| 67 | ftdi_get_error_string(ftdic)); |
| 68 | return 1; |
| 69 | } |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | int ft2232_spi_init(void) |
| 74 | { |
| 75 | int f; |
| 76 | struct ftdi_context *ftdic = &ftdic_context; |
| 77 | unsigned char buf[512]; |
Carl-Daniel Hailfinger | feea272 | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 78 | char *portpos = NULL; |
| 79 | int ft2232_type = FTDI_FT4232H; |
| 80 | enum ftdi_interface ft2232_interface = INTERFACE_B; |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 81 | |
| 82 | if (ftdi_init(ftdic) < 0) { |
| 83 | fprintf(stderr, "ftdi_init failed\n"); |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 84 | return EXIT_FAILURE; // TODO |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 87 | if (programmer_param && !strlen(programmer_param)) { |
| 88 | free(programmer_param); |
| 89 | programmer_param = NULL; |
Carl-Daniel Hailfinger | feea272 | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 90 | } |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 91 | if (programmer_param) { |
| 92 | if (strstr(programmer_param, "2232")) |
Carl-Daniel Hailfinger | feea272 | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 93 | ft2232_type = FTDI_FT2232H; |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 94 | if (strstr(programmer_param, "4232")) |
Carl-Daniel Hailfinger | feea272 | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 95 | ft2232_type = FTDI_FT4232H; |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 96 | portpos = strstr(programmer_param, "port="); |
Carl-Daniel Hailfinger | feea272 | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 97 | 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: |
| 107 | fprintf(stderr, "Invalid interface specified, " |
| 108 | "using default.\n"); |
| 109 | } |
| 110 | } |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 111 | free(programmer_param); |
Carl-Daniel Hailfinger | feea272 | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 112 | } |
| 113 | printf_debug("Using device type %s ", |
| 114 | (ft2232_type == FTDI_FT2232H) ? "2232H" : "4232H"); |
| 115 | printf_debug("interface %s\n", |
| 116 | (ft2232_interface == INTERFACE_A) ? "A" : "B"); |
| 117 | |
| 118 | f = ftdi_usb_open(ftdic, 0x0403, ft2232_type); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 119 | |
| 120 | if (f < 0 && f != -5) { |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 121 | fprintf(stderr, "Unable to open FTDI device: %d (%s)\n", f, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 122 | ftdi_get_error_string(ftdic)); |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 123 | exit(-1); // TODO |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Carl-Daniel Hailfinger | feea272 | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 126 | if (ftdi_set_interface(ftdic, ft2232_interface) < 0) { |
| 127 | fprintf(stderr, "Unable to select interface: %s\n", |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 128 | ftdic->error_str); |
| 129 | } |
| 130 | |
| 131 | if (ftdi_usb_reset(ftdic) < 0) { |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 132 | fprintf(stderr, "Unable to reset FTDI device\n"); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | if (ftdi_set_latency_timer(ftdic, 2) < 0) { |
| 136 | fprintf(stderr, "Unable to set latency timer\n"); |
| 137 | } |
| 138 | |
| 139 | if (ftdi_write_data_set_chunksize(ftdic, 512)) { |
| 140 | fprintf(stderr, "Unable to set chunk size\n"); |
| 141 | } |
| 142 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 143 | if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) { |
| 144 | fprintf(stderr, "Unable to set bitmode to SPI\n"); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | #if CLOCK_5X |
| 148 | printf_debug("Disable divide-by-5 front stage\n"); |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 149 | buf[0] = 0x8a; /* Disable divide-by-5. */ |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 150 | 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 |
| 159 | printf_debug("Set clock divisor\n"); |
| 160 | buf[0] = 0x86; /* command "set divisor" */ |
| 161 | /* valueL/valueH are (desired_divisor - 1) */ |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 162 | buf[1] = (DIVIDE_BY - 1) & 0xff; |
| 163 | buf[2] = ((DIVIDE_BY - 1) >> 8) & 0xff; |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 164 | if (send_buf(ftdic, buf, 3)) |
| 165 | return -1; |
| 166 | |
| 167 | printf("SPI clock is %fMHz\n", |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 168 | (double)(MPSSE_CLK / (((DIVIDE_BY - 1) + 1) * 2))); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 169 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 170 | /* Disconnect TDI/DO to TDO/DI for loopback. */ |
| 171 | printf_debug("No loopback of TDI/DO TDO/DI\n"); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 172 | buf[0] = 0x85; |
| 173 | if (send_buf(ftdic, buf, 1)) |
| 174 | return -1; |
| 175 | |
| 176 | printf_debug("Set data bits\n"); |
| 177 | /* 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 Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 182 | buf[0] = SET_BITS_LOW; |
Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 183 | buf[1] = CS_BIT; |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 184 | buf[2] = 0x0b; |
| 185 | if (send_buf(ftdic, buf, 3)) |
| 186 | return -1; |
| 187 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 188 | // printf_debug("\nft2232 chosen\n"); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 189 | |
| 190 | buses_supported = CHIP_BUSTYPE_SPI; |
| 191 | spi_controller = SPI_CONTROLLER_FT2232; |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Carl-Daniel Hailfinger | d047829 | 2009-07-10 21:08:55 +0000 | [diff] [blame] | 196 | int ft2232_spi_send_command(unsigned int writecnt, unsigned int readcnt, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 197 | const unsigned char *writearr, unsigned char *readarr) |
| 198 | { |
| 199 | struct ftdi_context *ftdic = &ftdic_context; |
| 200 | static unsigned char *buf = NULL; |
Carl-Daniel Hailfinger | a2441ce | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 201 | /* failed is special. We use bitwise ops, but it is essentially bool. */ |
| 202 | int i = 0, ret = 0, failed = 0; |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 203 | |
Carl-Daniel Hailfinger | 142e30f | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 204 | if (writecnt > 65536 || readcnt > 65536) |
| 205 | return SPI_INVALID_LENGTH; |
| 206 | |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 207 | buf = realloc(buf, writecnt + readcnt + 100); |
| 208 | if (!buf) { |
| 209 | fprintf(stderr, "Out of memory!\n"); |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 210 | exit(1); // -1 |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 213 | /* |
| 214 | * Minimize USB transfers by packing as many commands as possible |
| 215 | * together. If we're not expecting to read, we can assert CS#, write, |
| 216 | * and deassert CS# all in one shot. If reading, we do three separate |
Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 217 | * operations. |
| 218 | */ |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 219 | printf_debug("Assert CS#\n"); |
| 220 | buf[i++] = SET_BITS_LOW; |
Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 221 | buf[i++] = 0 & ~CS_BIT; /* assertive */ |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 222 | buf[i++] = 0x0b; |
| 223 | |
| 224 | if (writecnt) { |
| 225 | buf[i++] = 0x11; |
| 226 | buf[i++] = (writecnt - 1) & 0xff; |
| 227 | buf[i++] = ((writecnt - 1) >> 8) & 0xff; |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 228 | memcpy(buf + i, writearr, writecnt); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 229 | i += writecnt; |
| 230 | } |
| 231 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 232 | /* |
| 233 | * Optionally terminate this batch of commands with a |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 234 | * read command, then do the fetch of the results. |
| 235 | */ |
| 236 | if (readcnt) { |
| 237 | buf[i++] = 0x20; |
| 238 | buf[i++] = (readcnt - 1) & 0xff; |
| 239 | buf[i++] = ((readcnt - 1) >> 8) & 0xff; |
| 240 | ret = send_buf(ftdic, buf, i); |
Carl-Daniel Hailfinger | a2441ce | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 241 | failed = ret; |
| 242 | /* We can't abort here, we still have to deassert CS#. */ |
| 243 | if (ret) |
| 244 | fprintf(stderr, "send_buf failed before read: %i\n", |
| 245 | ret); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 246 | i = 0; |
Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 247 | if (ret == 0) { |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 248 | /* |
| 249 | * FIXME: This is unreliable. There's no guarantee that |
| 250 | * we read the response directly after sending the read |
| 251 | * command. We may be scheduled out etc. |
Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 252 | */ |
| 253 | ret = get_buf(ftdic, readarr, readcnt); |
Carl-Daniel Hailfinger | a2441ce | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 254 | failed |= ret; |
| 255 | /* We can't abort here either. */ |
| 256 | if (ret) |
| 257 | fprintf(stderr, "get_buf failed: %i\n", ret); |
Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 258 | } |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 261 | printf_debug("De-assert CS#\n"); |
| 262 | buf[i++] = SET_BITS_LOW; |
Stefan Reinauer | ab044b2 | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 263 | buf[i++] = CS_BIT; |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 264 | buf[i++] = 0x0b; |
Carl-Daniel Hailfinger | a2441ce | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 265 | ret = send_buf(ftdic, buf, i); |
| 266 | failed |= ret; |
| 267 | if (ret) |
| 268 | fprintf(stderr, "send_buf failed at end: %i\n", ret); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 269 | |
Carl-Daniel Hailfinger | a2441ce | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 270 | return failed ? -1 : 0; |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | int ft2232_spi_read(struct flashchip *flash, uint8_t *buf, int start, int len) |
| 274 | { |
| 275 | /* Maximum read length is 64k bytes. */ |
| 276 | return spi_read_chunked(flash, buf, start, len, 64 * 1024); |
| 277 | } |
| 278 | |
| 279 | int ft2232_spi_write_256(struct flashchip *flash, uint8_t *buf) |
| 280 | { |
| 281 | int total_size = 1024 * flash->total_size; |
| 282 | int i; |
| 283 | |
Carl-Daniel Hailfinger | 116081a | 2009-08-10 02:29:21 +0000 | [diff] [blame] | 284 | spi_disable_blockprotect(); |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 285 | /* Erase first. */ |
Carl-Daniel Hailfinger | 116081a | 2009-08-10 02:29:21 +0000 | [diff] [blame] | 286 | printf("Erasing flash before programming... "); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 287 | if (erase_flash(flash)) { |
Carl-Daniel Hailfinger | 116081a | 2009-08-10 02:29:21 +0000 | [diff] [blame] | 288 | fprintf(stderr, "ERASE FAILED!\n"); |
| 289 | return -1; |
| 290 | } |
| 291 | printf("done.\n"); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 292 | printf_debug("total_size is %d\n", total_size); |
| 293 | for (i = 0; i < total_size; i += 256) { |
| 294 | int l, r; |
| 295 | if (i + 256 <= total_size) |
| 296 | l = 256; |
| 297 | else |
| 298 | l = total_size - i; |
| 299 | |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 300 | if ((r = spi_nbyte_program(i, &buf[i], l))) { |
Uwe Hermann | 04aa59a | 2009-09-02 22:09:00 +0000 | [diff] [blame] | 301 | fprintf(stderr, "%s: write fail %d\n", __func__, r); |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 302 | return 1; |
| 303 | } |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 304 | |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 305 | while (spi_read_status_register() & JEDEC_RDSR_BIT_WIP) |
| 306 | /* loop */; |
| 307 | } |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 312 | #endif |