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