hailfinger | f31da3d | 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> |
hailfinger | 39d159a | 2010-05-21 23:09:42 +0000 | [diff] [blame] | 5 | * Copyright (C) 2009, 2010 Carl-Daniel Hailfinger |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 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. |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
hailfinger | 90c7d54 | 2010-05-31 15:27:27 +0000 | [diff] [blame] | 17 | #if CONFIG_FT2232_SPI == 1 |
hailfinger | d9dcfbd | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 18 | |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 19 | #include <stdio.h> |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 20 | #include <strings.h> |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | #include <stdlib.h> |
hailfinger | e98628b | 2009-07-01 00:02:23 +0000 | [diff] [blame] | 23 | #include <ctype.h> |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 24 | #include "flash.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 25 | #include "programmer.h" |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 26 | #include "spi.h" |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 27 | #include <ftdi.h> |
| 28 | |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 29 | /* This is not defined in libftdi.h <0.20 (c7e4c09e68cfa6f5e112334aa1b3bb23401c8dc7 to be exact). |
Angel Pons | e815033 | 2021-04-17 17:07:34 +0200 | [diff] [blame] | 30 | * Some tests indicate that this is the only change that it is needed to support the FT232H in flashrom. */ |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 31 | #if !defined(HAVE_FT232H) |
| 32 | #define TYPE_232H 6 |
| 33 | #endif |
| 34 | |
uwe | d4ab19b | 2011-10-14 20:33:14 +0000 | [diff] [blame] | 35 | /* Please keep sorted by vendor ID, then device ID. */ |
| 36 | |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 37 | #define FTDI_VID 0x0403 |
| 38 | #define FTDI_FT2232H_PID 0x6010 |
| 39 | #define FTDI_FT4232H_PID 0x6011 |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 40 | #define FTDI_FT232H_PID 0x6014 |
uwe | d4ab19b | 2011-10-14 20:33:14 +0000 | [diff] [blame] | 41 | #define TIAO_TUMPA_PID 0x8a98 |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 42 | #define TIAO_TUMPA_LITE_PID 0x8a99 |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 43 | #define AMONTEC_JTAGKEY_PID 0xCFF8 |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 44 | |
uwe | 64e8d6f | 2011-10-20 23:14:10 +0000 | [diff] [blame] | 45 | #define GOEPEL_VID 0x096C |
| 46 | #define GOEPEL_PICOTAP_PID 0x1449 |
| 47 | |
hailfinger | ce67061 | 2010-11-10 03:26:57 +0000 | [diff] [blame] | 48 | #define FIC_VID 0x1457 |
| 49 | #define OPENMOKO_DBGBOARD_PID 0x5118 |
| 50 | |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 51 | #define OLIMEX_VID 0x15BA |
| 52 | #define OLIMEX_ARM_OCD_PID 0x0003 |
| 53 | #define OLIMEX_ARM_TINY_PID 0x0004 |
| 54 | #define OLIMEX_ARM_OCD_H_PID 0x002B |
| 55 | #define OLIMEX_ARM_TINY_H_PID 0x002A |
| 56 | |
Todd Broch | 7aff3df | 2011-08-23 13:41:53 -0700 | [diff] [blame] | 57 | #define GOOGLE_VID 0x18D1 |
| 58 | #define GOOGLE_SERVO_PID 0x5001 |
Todd Broch | 97f259f | 2012-04-03 15:52:14 -0700 | [diff] [blame] | 59 | #define GOOGLE_SERVO_V2_PID0 0x5002 |
| 60 | #define GOOGLE_SERVO_V2_PID1 0x5003 |
Todd Broch | 7aff3df | 2011-08-23 13:41:53 -0700 | [diff] [blame] | 61 | |
Nikolai Artemiev | c347a85 | 2020-04-29 12:17:08 +1000 | [diff] [blame] | 62 | const struct dev_entry devs_ft2232spi[] = { |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 63 | {FTDI_VID, FTDI_FT2232H_PID, OK, "FTDI", "FT2232H"}, |
| 64 | {FTDI_VID, FTDI_FT4232H_PID, OK, "FTDI", "FT4232H"}, |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 65 | {FTDI_VID, FTDI_FT232H_PID, OK, "FTDI", "FT232H"}, |
uwe | d4ab19b | 2011-10-14 20:33:14 +0000 | [diff] [blame] | 66 | {FTDI_VID, TIAO_TUMPA_PID, OK, "TIAO", "USB Multi-Protocol Adapter"}, |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 67 | {FTDI_VID, TIAO_TUMPA_LITE_PID, OK, "TIAO", "USB Multi-Protocol Adapter Lite"}, |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 68 | {FTDI_VID, AMONTEC_JTAGKEY_PID, OK, "Amontec", "JTAGkey"}, |
uwe | 64e8d6f | 2011-10-20 23:14:10 +0000 | [diff] [blame] | 69 | {GOEPEL_VID, GOEPEL_PICOTAP_PID, OK, "GOEPEL", "PicoTAP"}, |
Todd Broch | 7aff3df | 2011-08-23 13:41:53 -0700 | [diff] [blame] | 70 | {GOOGLE_VID, GOOGLE_SERVO_PID, OK, "Google", "Servo"}, |
Todd Broch | 97f259f | 2012-04-03 15:52:14 -0700 | [diff] [blame] | 71 | {GOOGLE_VID, GOOGLE_SERVO_V2_PID0, OK, "Google", "Servo V2 Legacy"}, |
| 72 | {GOOGLE_VID, GOOGLE_SERVO_V2_PID1, OK, "Google", "Servo V2"}, |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 73 | {FIC_VID, OPENMOKO_DBGBOARD_PID, OK, "FIC", "OpenMoko Neo1973 Debug board (V2+)"}, |
| 74 | {OLIMEX_VID, OLIMEX_ARM_OCD_PID, OK, "Olimex", "ARM-USB-OCD"}, |
| 75 | {OLIMEX_VID, OLIMEX_ARM_TINY_PID, OK, "Olimex", "ARM-USB-TINY"}, |
| 76 | {OLIMEX_VID, OLIMEX_ARM_OCD_H_PID, OK, "Olimex", "ARM-USB-OCD-H"}, |
| 77 | {OLIMEX_VID, OLIMEX_ARM_TINY_H_PID, OK, "Olimex", "ARM-USB-TINY-H"}, |
| 78 | |
Patrick Georgi | 8ddfee9 | 2017-03-20 14:54:28 +0100 | [diff] [blame] | 79 | {0}, |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 82 | #define DEFAULT_DIVISOR 2 |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 83 | |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 84 | #define BITMODE_BITBANG_NORMAL 1 |
| 85 | #define BITMODE_BITBANG_SPI 2 |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 86 | |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 87 | /* The variables pinlvl and pindir store the values for the "set data bits low byte" MPSSE command that |
Nikolai Artemiev | 0a63526 | 2020-02-11 13:49:52 +1100 | [diff] [blame] | 88 | * sets the initial state and the direction of the I/O pins. The pin offsets are as follows: |
sibradzic | 419c86f | 2020-02-04 18:24:58 +0900 | [diff] [blame] | 89 | * TCK/SK is bit 0. |
| 90 | * TDI/DO is bit 1. |
| 91 | * TDO/DI is bit 2. |
| 92 | * TMS/CS is bit 3. |
| 93 | * GPIOL0 is bit 4. |
| 94 | * GPIOL1 is bit 5. |
| 95 | * GPIOL2 is bit 6. |
| 96 | * GPIOL3 is bit 7. |
| 97 | * |
| 98 | * The pin signal direction bit offsets follow the same order; 0 means that |
| 99 | * pin at the matching bit index is an input, 1 means pin is an output. |
Nikolai Artemiev | 0a63526 | 2020-02-11 13:49:52 +1100 | [diff] [blame] | 100 | * |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 101 | * The default values (set below in ft2232_spi_init) are used for most devices: |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 102 | * value: 0x08 CS=high, DI=low, DO=low, SK=low |
| 103 | * dir: 0x0b CS=output, DI=input, DO=output, SK=output |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 104 | */ |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 105 | struct ft2232_data { |
| 106 | uint8_t pinlvl; |
| 107 | uint8_t pindir; |
| 108 | struct ftdi_context ftdic_context; |
| 109 | }; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 110 | |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 111 | static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type) |
| 112 | { |
| 113 | int i; |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 114 | for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) { |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 115 | if ((devs_ft2232spi[i].device_id == ft2232_type) |
| 116 | && (devs_ft2232spi[i].vendor_id == ft2232_vid)) |
| 117 | return devs_ft2232spi[i].device_name; |
| 118 | } |
| 119 | return "unknown device"; |
| 120 | } |
| 121 | |
| 122 | static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type) |
| 123 | { |
| 124 | int i; |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 125 | for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) { |
| 126 | if ((devs_ft2232spi[i].device_id == ft2232_type) |
| 127 | && (devs_ft2232spi[i].vendor_id == ft2232_vid)) |
| 128 | return devs_ft2232spi[i].vendor_name; |
| 129 | } |
| 130 | return "unknown vendor"; |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 131 | } |
| 132 | |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 133 | static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf, |
| 134 | int size) |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 135 | { |
| 136 | int r; |
| 137 | r = ftdi_write_data(ftdic, (unsigned char *) buf, size); |
| 138 | if (r < 0) { |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 139 | msg_perr("ftdi_write_data: %d, %s\n", r, |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 140 | ftdi_get_error_string(ftdic)); |
| 141 | return 1; |
| 142 | } |
| 143 | return 0; |
| 144 | } |
| 145 | |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 146 | static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf, |
| 147 | int size) |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 148 | { |
| 149 | int r; |
hailfinger | 694faa2 | 2010-11-10 03:10:41 +0000 | [diff] [blame] | 150 | |
| 151 | while (size > 0) { |
| 152 | r = ftdi_read_data(ftdic, (unsigned char *) buf, size); |
| 153 | if (r < 0) { |
| 154 | msg_perr("ftdi_read_data: %d, %s\n", r, |
| 155 | ftdi_get_error_string(ftdic)); |
| 156 | return 1; |
| 157 | } |
| 158 | buf += r; |
| 159 | size -= r; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 160 | } |
| 161 | return 0; |
| 162 | } |
| 163 | |
Alan Green | f6c6215 | 2021-01-08 09:36:45 +1100 | [diff] [blame] | 164 | static int ft2232_shutdown(void *data) |
| 165 | { |
| 166 | int f; |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 167 | struct ft2232_data *spi_data = (struct ft2232_data *) data; |
| 168 | struct ftdi_context *ftdic = &spi_data->ftdic_context; |
Alan Green | f6c6215 | 2021-01-08 09:36:45 +1100 | [diff] [blame] | 169 | unsigned char buf[3]; |
| 170 | |
| 171 | msg_pdbg("Releasing I/Os\n"); |
| 172 | buf[0] = SET_BITS_LOW; |
| 173 | buf[1] = 0; /* Output byte ignored */ |
| 174 | buf[2] = 0; /* Pin direction: all inputs */ |
| 175 | if (send_buf(ftdic, buf, 3)) { |
| 176 | msg_perr("Unable to set pins back inputs: (%s)\n", |
| 177 | ftdi_get_error_string(ftdic)); |
| 178 | } |
| 179 | |
| 180 | if ((f = ftdi_usb_close(ftdic)) < 0) { |
| 181 | msg_perr("Unable to close FTDI device: %d (%s)\n", f, |
| 182 | ftdi_get_error_string(ftdic)); |
| 183 | return f; |
| 184 | } |
| 185 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 186 | free(spi_data); |
Alan Green | f6c6215 | 2021-01-08 09:36:45 +1100 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 190 | /* Returns 0 upon success, a negative number upon errors. */ |
Nikolai Artemiev | 0a63526 | 2020-02-11 13:49:52 +1100 | [diff] [blame] | 191 | static int ft2232_spi_send_command(const struct flashctx *flash, |
| 192 | unsigned int writecnt, unsigned int readcnt, |
| 193 | const unsigned char *writearr, |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 194 | unsigned char *readarr) |
| 195 | { |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 196 | struct ft2232_data *spi_data = flash->mst->spi.data; |
| 197 | struct ftdi_context *ftdic = &spi_data->ftdic_context; |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 198 | static unsigned char *buf = NULL; |
| 199 | /* failed is special. We use bitwise ops, but it is essentially bool. */ |
| 200 | int i = 0, ret = 0, failed = 0; |
| 201 | size_t bufsize; |
| 202 | static size_t oldbufsize = 0; |
| 203 | |
| 204 | if (writecnt > 65536 || readcnt > 65536) |
| 205 | return SPI_INVALID_LENGTH; |
| 206 | |
| 207 | /* buf is not used for the response from the chip. */ |
| 208 | bufsize = max(writecnt + 9, 260 + 9); |
| 209 | /* Never shrink. realloc() calls are expensive. */ |
| 210 | if (!buf || bufsize > oldbufsize) { |
| 211 | buf = realloc(buf, bufsize); |
| 212 | if (!buf) { |
| 213 | msg_perr("Out of memory!\n"); |
| 214 | /* TODO: What to do with buf? */ |
| 215 | return SPI_GENERIC_ERROR; |
| 216 | } |
| 217 | oldbufsize = bufsize; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Minimize USB transfers by packing as many commands as possible |
| 222 | * together. If we're not expecting to read, we can assert CS#, write, |
| 223 | * and deassert CS# all in one shot. If reading, we do three separate |
| 224 | * operations. |
| 225 | */ |
| 226 | msg_pspew("Assert CS#\n"); |
| 227 | buf[i++] = SET_BITS_LOW; |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 228 | buf[i++] = ~ 0x08 & spi_data->pinlvl; /* assert CS (3rd) bit only */ |
| 229 | buf[i++] = spi_data->pindir; |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 230 | |
| 231 | if (writecnt) { |
| 232 | buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG; |
| 233 | buf[i++] = (writecnt - 1) & 0xff; |
| 234 | buf[i++] = ((writecnt - 1) >> 8) & 0xff; |
| 235 | memcpy(buf + i, writearr, writecnt); |
| 236 | i += writecnt; |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Optionally terminate this batch of commands with a |
| 241 | * read command, then do the fetch of the results. |
| 242 | */ |
| 243 | if (readcnt) { |
| 244 | buf[i++] = MPSSE_DO_READ; |
| 245 | buf[i++] = (readcnt - 1) & 0xff; |
| 246 | buf[i++] = ((readcnt - 1) >> 8) & 0xff; |
| 247 | ret = send_buf(ftdic, buf, i); |
| 248 | failed = ret; |
| 249 | /* We can't abort here, we still have to deassert CS#. */ |
| 250 | if (ret) |
| 251 | msg_perr("send_buf failed before read: %i\n", ret); |
| 252 | i = 0; |
| 253 | if (ret == 0) { |
| 254 | /* |
| 255 | * FIXME: This is unreliable. There's no guarantee that |
| 256 | * we read the response directly after sending the read |
| 257 | * command. We may be scheduled out etc. |
| 258 | */ |
| 259 | ret = get_buf(ftdic, readarr, readcnt); |
| 260 | failed |= ret; |
| 261 | /* We can't abort here either. */ |
| 262 | if (ret) |
| 263 | msg_perr("get_buf failed: %i\n", ret); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | msg_pspew("De-assert CS#\n"); |
| 268 | buf[i++] = SET_BITS_LOW; |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 269 | buf[i++] = spi_data->pinlvl; |
| 270 | buf[i++] = spi_data->pindir; |
Anastasia Klimchuk | ad79bd9 | 2021-02-15 15:04:20 +1100 | [diff] [blame] | 271 | ret = send_buf(ftdic, buf, i); |
| 272 | failed |= ret; |
| 273 | if (ret) |
| 274 | msg_perr("send_buf failed at end: %i\n", ret); |
| 275 | |
| 276 | return failed ? -1 : 0; |
| 277 | } |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 278 | |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 279 | static const struct spi_master spi_master_ft2232 = { |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 280 | .features = SPI_MASTER_4BA, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 281 | .max_data_read = 64 * 1024, |
| 282 | .max_data_write = 256, |
| 283 | .command = ft2232_spi_send_command, |
| 284 | .multicommand = default_spi_send_multicommand, |
| 285 | .read = default_spi_read, |
| 286 | .write_256 = default_spi_write_256, |
Edward O'Callaghan | eeaac6b | 2020-10-12 19:51:56 +1100 | [diff] [blame] | 287 | .write_aai = default_spi_write_aai, |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 288 | }; |
| 289 | |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 290 | /* Returns 0 upon success, a negative number upon errors. */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 291 | int ft2232_spi_init(void) |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 292 | { |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 293 | int ret = 0; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 294 | unsigned char buf[512]; |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 295 | int ft2232_vid = FTDI_VID; |
| 296 | int ft2232_type = FTDI_FT4232H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 297 | int channel_count = 4; /* Stores the number of channels of the device. */ |
| 298 | enum ftdi_interface ft2232_interface = INTERFACE_A; |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 299 | /* |
| 300 | * The 'H' chips can run with an internal clock of either 12 MHz or 60 MHz, |
| 301 | * but the non-H chips can only run at 12 MHz. We disable the divide-by-5 |
| 302 | * prescaler on 'H' chips so they run at 60MHz. |
| 303 | */ |
| 304 | uint8_t clock_5x = 1; |
| 305 | /* In addition to the prescaler mentioned above there is also another |
| 306 | * configurable one on all versions of the chips. Its divisor div can be |
| 307 | * set by a 16 bit value x according to the following formula: |
| 308 | * div = (1 + x) * 2 <-> x = div / 2 - 1 |
| 309 | * Hence the expressible divisors are all even numbers between 2 and |
| 310 | * 2^17 (=131072) resulting in SCK frequencies of 6 MHz down to about |
| 311 | * 92 Hz for 12 MHz inputs and 30 MHz down to about 458 Hz for 60 MHz |
| 312 | * inputs. |
| 313 | */ |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 314 | uint32_t divisor = DEFAULT_DIVISOR; |
| 315 | int f; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 316 | char *arg; |
hailfinger | 81f90f2 | 2010-11-10 03:18:41 +0000 | [diff] [blame] | 317 | double mpsse_clk; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 318 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 319 | uint8_t pinlvl = 0x08; |
| 320 | uint8_t pindir = 0x0b; |
| 321 | struct ftdi_context ftdic; |
| 322 | struct ft2232_data *spi_data; |
| 323 | |
hailfinger | ddeb4ac | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 324 | arg = extract_programmer_param("type"); |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 325 | if (arg) { |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 326 | if (!strcasecmp(arg, "2232H")) { |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 327 | ft2232_type = FTDI_FT2232H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 328 | channel_count = 2; |
| 329 | } else if (!strcasecmp(arg, "4232H")) { |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 330 | ft2232_type = FTDI_FT4232H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 331 | channel_count = 4; |
| 332 | } else if (!strcasecmp(arg, "232H")) { |
| 333 | ft2232_type = FTDI_FT232H_PID; |
| 334 | channel_count = 1; |
| 335 | } else if (!strcasecmp(arg, "jtagkey")) { |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 336 | ft2232_type = AMONTEC_JTAGKEY_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 337 | channel_count = 2; |
| 338 | /* JTAGkey(2) needs to enable its output via Bit4 / GPIOL0 |
| 339 | * value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low |
| 340 | * dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output */ |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 341 | pinlvl = 0x18; |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 342 | pindir = 0x1b; |
uwe | 64e8d6f | 2011-10-20 23:14:10 +0000 | [diff] [blame] | 343 | } else if (!strcasecmp(arg, "picotap")) { |
| 344 | ft2232_vid = GOEPEL_VID; |
| 345 | ft2232_type = GOEPEL_PICOTAP_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 346 | channel_count = 2; |
uwe | d4ab19b | 2011-10-14 20:33:14 +0000 | [diff] [blame] | 347 | } else if (!strcasecmp(arg, "tumpa")) { |
| 348 | /* Interface A is SPI1, B is SPI2. */ |
| 349 | ft2232_type = TIAO_TUMPA_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 350 | channel_count = 2; |
| 351 | } else if (!strcasecmp(arg, "tumpalite")) { |
| 352 | /* Only one channel is used on lite edition */ |
| 353 | ft2232_type = TIAO_TUMPA_LITE_PID; |
| 354 | channel_count = 1; |
stefanct | 90aefe8 | 2011-08-12 01:19:32 +0000 | [diff] [blame] | 355 | } else if (!strcasecmp(arg, "busblaster")) { |
| 356 | /* In its default configuration it is a jtagkey clone */ |
| 357 | ft2232_type = FTDI_FT2232H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 358 | channel_count = 2; |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 359 | pinlvl = 0x18; |
stefanct | 90aefe8 | 2011-08-12 01:19:32 +0000 | [diff] [blame] | 360 | pindir = 0x1b; |
hailfinger | ce67061 | 2010-11-10 03:26:57 +0000 | [diff] [blame] | 361 | } else if (!strcasecmp(arg, "openmoko")) { |
| 362 | ft2232_vid = FIC_VID; |
| 363 | ft2232_type = OPENMOKO_DBGBOARD_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 364 | channel_count = 2; |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 365 | } else if (!strcasecmp(arg, "arm-usb-ocd")) { |
| 366 | ft2232_vid = OLIMEX_VID; |
| 367 | ft2232_type = OLIMEX_ARM_OCD_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 368 | channel_count = 2; |
| 369 | /* arm-usb-ocd(-h) has an output buffer that needs to be enabled by pulling ADBUS4 low. |
| 370 | * value: 0x08 #OE=low, CS=high, DI=low, DO=low, SK=low |
| 371 | * dir: 0x1b #OE=output, CS=output, DI=input, DO=output, SK=output */ |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 372 | pinlvl = 0x08; |
Paul Fertser | 80ed25d | 2016-07-29 15:31:03 -0700 | [diff] [blame] | 373 | pindir = 0x1b; |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 374 | } else if (!strcasecmp(arg, "arm-usb-tiny")) { |
| 375 | ft2232_vid = OLIMEX_VID; |
| 376 | ft2232_type = OLIMEX_ARM_TINY_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 377 | channel_count = 2; |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 378 | } else if (!strcasecmp(arg, "arm-usb-ocd-h")) { |
| 379 | ft2232_vid = OLIMEX_VID; |
| 380 | ft2232_type = OLIMEX_ARM_OCD_H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 381 | channel_count = 2; |
| 382 | /* See arm-usb-ocd */ |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 383 | pinlvl = 0x08; |
Paul Fertser | 80ed25d | 2016-07-29 15:31:03 -0700 | [diff] [blame] | 384 | pindir = 0x1b; |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 385 | } else if (!strcasecmp(arg, "arm-usb-tiny-h")) { |
| 386 | ft2232_vid = OLIMEX_VID; |
| 387 | ft2232_type = OLIMEX_ARM_TINY_H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 388 | channel_count = 2; |
Edward O'Callaghan | b9a1dcf | 2020-03-02 17:14:14 +1100 | [diff] [blame] | 389 | } else if (!strcasecmp(arg, "google-servo")) { |
| 390 | ft2232_vid = GOOGLE_VID; |
| 391 | ft2232_type = GOOGLE_SERVO_PID; |
Edward O'Callaghan | b9a1dcf | 2020-03-02 17:14:14 +1100 | [diff] [blame] | 392 | } else if (!strcasecmp(arg, "google-servo-v2")) { |
| 393 | ft2232_vid = GOOGLE_VID; |
| 394 | ft2232_type = GOOGLE_SERVO_V2_PID1; |
Edward O'Callaghan | b9a1dcf | 2020-03-02 17:14:14 +1100 | [diff] [blame] | 395 | /* Default divisor is too fast, and chip ID fails */ |
| 396 | divisor = 6; |
| 397 | } else if (!strcasecmp(arg, "google-servo-v2-legacy")) { |
| 398 | ft2232_vid = GOOGLE_VID; |
| 399 | ft2232_type = GOOGLE_SERVO_V2_PID0; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 400 | } else if (!strcasecmp(arg, "flyswatter")) { |
| 401 | ft2232_type = FTDI_FT2232H_PID; |
| 402 | channel_count = 2; |
| 403 | /* Flyswatter and Flyswatter-2 require GPIO bits 0x80 |
| 404 | * and 0x40 to be driven low to enable output buffers */ |
| 405 | pindir = 0xcb; |
hailfinger | ce67061 | 2010-11-10 03:26:57 +0000 | [diff] [blame] | 406 | } else { |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 407 | msg_perr("Error: Invalid device type specified.\n"); |
| 408 | free(arg); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 409 | return -1; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | free(arg); |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 413 | |
hailfinger | ddeb4ac | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 414 | arg = extract_programmer_param("port"); |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 415 | if (arg) { |
hailfinger | ebad21f | 2010-10-08 12:40:09 +0000 | [diff] [blame] | 416 | switch (toupper((unsigned char)*arg)) { |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 417 | case 'A': |
| 418 | ft2232_interface = INTERFACE_A; |
| 419 | break; |
| 420 | case 'B': |
| 421 | ft2232_interface = INTERFACE_B; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 422 | if (channel_count < 2) |
| 423 | channel_count = -1; |
| 424 | break; |
| 425 | case 'C': |
| 426 | ft2232_interface = INTERFACE_C; |
| 427 | if (channel_count < 3) |
| 428 | channel_count = -1; |
| 429 | break; |
| 430 | case 'D': |
| 431 | ft2232_interface = INTERFACE_D; |
| 432 | if (channel_count < 4) |
| 433 | channel_count = -1; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 434 | break; |
| 435 | default: |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 436 | channel_count = -1; |
| 437 | break; |
| 438 | } |
| 439 | if (channel_count < 0 || strlen(arg) != 1) { |
| 440 | msg_perr("Error: Invalid channel/port/interface specified: \"%s\".\n", arg); |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 441 | free(arg); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 442 | return -2; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | free(arg); |
Nikolai Artemiev | 249cbab | 2020-02-07 11:53:15 +1100 | [diff] [blame] | 446 | |
Nikolai Artemiev | 249cbab | 2020-02-07 11:53:15 +1100 | [diff] [blame] | 447 | arg = extract_programmer_param("divisor"); |
| 448 | if (arg && strlen(arg)) { |
| 449 | unsigned int temp = 0; |
| 450 | char *endptr; |
| 451 | temp = strtoul(arg, &endptr, 10); |
| 452 | if (*endptr || temp < 2 || temp > 131072 || temp & 0x1) { |
| 453 | msg_perr("Error: Invalid SPI frequency divisor specified: \"%s\".\n" |
| 454 | "Valid are even values between 2 and 131072.\n", arg); |
| 455 | free(arg); |
| 456 | return -2; |
| 457 | } |
| 458 | divisor = (uint32_t)temp; |
| 459 | } |
| 460 | free(arg); |
| 461 | |
Alan Green | 2cb9f13 | 2021-01-18 18:41:25 +1100 | [diff] [blame] | 462 | /* Allows setting multiple GPIOL pins to high, for example: csgpiol=012 */ |
Sergey Alirzaev | 018a6df | 2018-08-01 16:39:17 +0300 | [diff] [blame] | 463 | arg = extract_programmer_param("csgpiol"); |
| 464 | if (arg) { |
sibradzic | 419c86f | 2020-02-04 18:24:58 +0900 | [diff] [blame] | 465 | unsigned int ngpios = strlen(arg); |
| 466 | for (unsigned int i = 0; i <= ngpios; i++) { |
| 467 | int temp = arg[i] - '0'; |
| 468 | if (ngpios == 0 || (ngpios != i && (temp < 0 || temp > 3))) { |
| 469 | msg_perr("Error: Invalid GPIOLs specified: \"%s\".\n" |
| 470 | "Valid values are numbers between 0 and 3. " |
| 471 | "Multiple GPIOLs can be specified.\n", arg); |
| 472 | free(arg); |
| 473 | return -2; |
| 474 | } else { |
| 475 | unsigned int pin = temp + 4; |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 476 | pinlvl |= 1 << pin; |
sibradzic | 419c86f | 2020-02-04 18:24:58 +0900 | [diff] [blame] | 477 | pindir |= 1 << pin; |
| 478 | } |
Sergey Alirzaev | 018a6df | 2018-08-01 16:39:17 +0300 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | free(arg); |
| 482 | |
Alan Green | 2cb9f13 | 2021-01-18 18:41:25 +1100 | [diff] [blame] | 483 | /* Allows setting GPIOL pins high, low or input (high-z) */ |
| 484 | arg = extract_programmer_param("gpiol"); |
| 485 | if (arg) { |
| 486 | int ok = 0; |
| 487 | if (strlen(arg) == 4) { |
| 488 | ok = 1; |
| 489 | for (int i = 0; i < 4; i++) { |
| 490 | unsigned int pin = i + 4; |
| 491 | switch (toupper(arg[i])) { |
| 492 | case 'H': |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 493 | pinlvl |= 1 << pin; |
Alan Green | 2cb9f13 | 2021-01-18 18:41:25 +1100 | [diff] [blame] | 494 | pindir |= 1 << pin; |
| 495 | break; |
| 496 | case 'L': |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 497 | pinlvl &= ~(1 << pin); |
Alan Green | 2cb9f13 | 2021-01-18 18:41:25 +1100 | [diff] [blame] | 498 | pindir |= 1 << pin; |
| 499 | break; |
| 500 | case 'Z': |
| 501 | pindir &= ~(1 << pin); |
| 502 | break; |
| 503 | case 'X': |
| 504 | break; |
| 505 | default: |
| 506 | ok = 0; |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | if (!ok) { |
| 511 | msg_perr("Error: Invalid GPIOLs specified: \"%s\".\n" |
| 512 | "Valid values are 4 character strings of H, L, Z and X.\n" |
| 513 | " H - Set GPIOL output high\n" |
| 514 | " L - Set GPIOL output low\n" |
| 515 | " Z - Set GPIOL as input (high impedance)\n" |
| 516 | " X - Leave as programmer default\n" |
| 517 | "Example: gpiol=LZXH drives GPIOL 0 low, and GPIOL 3 high, sets GPIOL 1\n" |
| 518 | "to an input and leaves GPIOL 2 set according to the programmer type.\n", arg); |
| 519 | free(arg); |
| 520 | return -2; |
| 521 | } |
| 522 | } |
| 523 | free(arg); |
| 524 | |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 525 | msg_pdbg("Using device type %s %s ", |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 526 | get_ft2232_vendorname(ft2232_vid, ft2232_type), |
| 527 | get_ft2232_devicename(ft2232_vid, ft2232_type)); |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 528 | msg_pdbg("channel %s.\n", |
| 529 | (ft2232_interface == INTERFACE_A) ? "A" : |
| 530 | (ft2232_interface == INTERFACE_B) ? "B" : |
| 531 | (ft2232_interface == INTERFACE_C) ? "C" : "D"); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 532 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 533 | if (ftdi_init(&ftdic) < 0) { |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 534 | msg_perr("ftdi_init failed.\n"); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 535 | return -3; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 536 | } |
| 537 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 538 | if (ftdi_set_interface(&ftdic, ft2232_interface) < 0) { |
| 539 | msg_perr("Unable to select channel (%s).\n", ftdi_get_error_string(&ftdic)); |
Todd Broch | cbaba9c | 2014-09-22 12:11:03 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Shik Chen | 5eb5624 | 2012-08-07 15:09:37 +0800 | [diff] [blame] | 542 | arg = extract_programmer_param("serial"); |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 543 | f = ftdi_usb_open_desc(&ftdic, ft2232_vid, ft2232_type, NULL, arg); |
Shik Chen | 5eb5624 | 2012-08-07 15:09:37 +0800 | [diff] [blame] | 544 | free(arg); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 545 | |
| 546 | if (f < 0 && f != -5) { |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 547 | msg_perr("Unable to open FTDI device: %d (%s)\n", f, |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 548 | ftdi_get_error_string(&ftdic)); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 549 | return -4; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 552 | if (ftdic.type != TYPE_2232H && ftdic.type != TYPE_4232H && ftdic.type != TYPE_232H) { |
| 553 | msg_pdbg("FTDI chip type %d is not high-speed.\n", ftdic.type); |
hailfinger | 81f90f2 | 2010-11-10 03:18:41 +0000 | [diff] [blame] | 554 | clock_5x = 0; |
| 555 | } |
| 556 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 557 | if (ftdi_usb_reset(&ftdic) < 0) { |
| 558 | msg_perr("Unable to reset FTDI device (%s).\n", ftdi_get_error_string(&ftdic)); |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 559 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 560 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 561 | if (ftdi_set_latency_timer(&ftdic, 2) < 0) { |
| 562 | msg_perr("Unable to set latency timer (%s).\n", ftdi_get_error_string(&ftdic)); |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 563 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 564 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 565 | if (ftdi_write_data_set_chunksize(&ftdic, 270)) { |
| 566 | msg_perr("Unable to set chunk size (%s).\n", ftdi_get_error_string(&ftdic)); |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 567 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 568 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 569 | if (ftdi_set_bitmode(&ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) { |
| 570 | msg_perr("Unable to set bitmode to SPI (%s).\n", ftdi_get_error_string(&ftdic)); |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 571 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 572 | |
hailfinger | 81f90f2 | 2010-11-10 03:18:41 +0000 | [diff] [blame] | 573 | if (clock_5x) { |
| 574 | msg_pdbg("Disable divide-by-5 front stage\n"); |
Nikolai Artemiev | a5c9cd7 | 2020-01-08 10:40:19 +1100 | [diff] [blame] | 575 | buf[0] = DIS_DIV_5; |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 576 | if (send_buf(&ftdic, buf, 1)) { |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 577 | ret = -5; |
| 578 | goto ftdi_err; |
| 579 | } |
hailfinger | 81f90f2 | 2010-11-10 03:18:41 +0000 | [diff] [blame] | 580 | mpsse_clk = 60.0; |
| 581 | } else { |
| 582 | mpsse_clk = 12.0; |
Todd Broch | 7774665 | 2012-04-19 09:52:49 -0700 | [diff] [blame] | 583 | } |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 584 | |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 585 | msg_pdbg("Set clock divisor\n"); |
Nikolai Artemiev | a5c9cd7 | 2020-01-08 10:40:19 +1100 | [diff] [blame] | 586 | buf[0] = TCK_DIVISOR; |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 587 | buf[1] = (divisor / 2 - 1) & 0xff; |
| 588 | buf[2] = ((divisor / 2 - 1) >> 8) & 0xff; |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 589 | if (send_buf(&ftdic, buf, 3)) { |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 590 | ret = -6; |
| 591 | goto ftdi_err; |
| 592 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 593 | |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 594 | msg_pdbg("MPSSE clock: %f MHz, divisor: %u, SPI clock: %f MHz\n", |
| 595 | mpsse_clk, divisor, (double)(mpsse_clk / divisor)); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 596 | |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 597 | /* Disconnect TDI/DO to TDO/DI for loopback. */ |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 598 | msg_pdbg("No loopback of TDI/DO TDO/DI\n"); |
Nikolai Artemiev | a5c9cd7 | 2020-01-08 10:40:19 +1100 | [diff] [blame] | 599 | buf[0] = LOOPBACK_END; |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 600 | if (send_buf(&ftdic, buf, 1)) { |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 601 | ret = -7; |
| 602 | goto ftdi_err; |
| 603 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 604 | |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 605 | msg_pdbg("Set data bits\n"); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 606 | buf[0] = SET_BITS_LOW; |
Alan Green | b390aaf | 2021-01-21 09:48:25 +1100 | [diff] [blame] | 607 | buf[1] = pinlvl; |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 608 | buf[2] = pindir; |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 609 | if (send_buf(&ftdic, buf, 3)) { |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 610 | ret = -8; |
| 611 | goto ftdi_err; |
| 612 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 613 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 614 | spi_data = calloc(1, sizeof(*spi_data)); |
| 615 | if (!spi_data) { |
| 616 | msg_perr("Unable to allocate space for SPI master data\n"); |
| 617 | return SPI_GENERIC_ERROR; |
| 618 | } |
| 619 | spi_data->pinlvl = pinlvl; |
| 620 | spi_data->pindir = pindir; |
| 621 | spi_data->ftdic_context = ftdic; |
| 622 | |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 623 | if (register_shutdown(ft2232_shutdown, spi_data)) { |
| 624 | free(spi_data); |
| 625 | goto ftdi_err; |
| 626 | } |
Nico Huber | 2ef004f | 2021-05-11 17:53:34 +0200 | [diff] [blame] | 627 | register_spi_master(&spi_master_ft2232, spi_data); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 628 | |
| 629 | return 0; |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 630 | |
| 631 | ftdi_err: |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 632 | if ((f = ftdi_usb_close(&ftdic)) < 0) { |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 633 | msg_perr("Unable to close FTDI device: %d (%s)\n", f, |
Anastasia Klimchuk | 40a8001 | 2021-04-12 10:05:57 +1000 | [diff] [blame] | 634 | ftdi_get_error_string(&ftdic)); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 635 | } |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 636 | return ret; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 637 | } |
| 638 | |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 639 | #endif |