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). |
| 30 | * Some tests indicate that his is the only change that it is needed to support the FT232H in flashrom. */ |
| 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 | |
Nikolai Artemiev | 0a63526 | 2020-02-11 13:49:52 +1100 | [diff] [blame] | 87 | /* The variables cs_bits and pindir store the values for the "set data bits low byte" MPSSE command that |
| 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 | * |
| 101 | * The default values (set below) 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 | */ |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 105 | static uint8_t cs_bits = 0x08; |
| 106 | static uint8_t pindir = 0x0b; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 107 | static struct ftdi_context ftdic_context; |
| 108 | |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 109 | static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type) |
| 110 | { |
| 111 | int i; |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 112 | for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) { |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 113 | if ((devs_ft2232spi[i].device_id == ft2232_type) |
| 114 | && (devs_ft2232spi[i].vendor_id == ft2232_vid)) |
| 115 | return devs_ft2232spi[i].device_name; |
| 116 | } |
| 117 | return "unknown device"; |
| 118 | } |
| 119 | |
| 120 | static const char *get_ft2232_vendorname(int ft2232_vid, int ft2232_type) |
| 121 | { |
| 122 | int i; |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 123 | for (i = 0; devs_ft2232spi[i].vendor_name != NULL; i++) { |
| 124 | if ((devs_ft2232spi[i].device_id == ft2232_type) |
| 125 | && (devs_ft2232spi[i].vendor_id == ft2232_vid)) |
| 126 | return devs_ft2232spi[i].vendor_name; |
| 127 | } |
| 128 | return "unknown vendor"; |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 129 | } |
| 130 | |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 131 | static int send_buf(struct ftdi_context *ftdic, const unsigned char *buf, |
| 132 | int size) |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 133 | { |
| 134 | int r; |
| 135 | r = ftdi_write_data(ftdic, (unsigned char *) buf, size); |
| 136 | if (r < 0) { |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 137 | msg_perr("ftdi_write_data: %d, %s\n", r, |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 138 | ftdi_get_error_string(ftdic)); |
| 139 | return 1; |
| 140 | } |
| 141 | return 0; |
| 142 | } |
| 143 | |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 144 | static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf, |
| 145 | int size) |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 146 | { |
| 147 | int r; |
hailfinger | 694faa2 | 2010-11-10 03:10:41 +0000 | [diff] [blame] | 148 | |
| 149 | while (size > 0) { |
| 150 | r = ftdi_read_data(ftdic, (unsigned char *) buf, size); |
| 151 | if (r < 0) { |
| 152 | msg_perr("ftdi_read_data: %d, %s\n", r, |
| 153 | ftdi_get_error_string(ftdic)); |
| 154 | return 1; |
| 155 | } |
| 156 | buf += r; |
| 157 | size -= r; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
Nikolai Artemiev | 0a63526 | 2020-02-11 13:49:52 +1100 | [diff] [blame] | 162 | static int ft2232_spi_send_command(const struct flashctx *flash, |
| 163 | unsigned int writecnt, unsigned int readcnt, |
| 164 | const unsigned char *writearr, |
| 165 | unsigned char *readarr); |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 166 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 167 | static const struct spi_master spi_master_ft2232 = { |
Edward O'Callaghan | a6673bd | 2019-06-24 15:22:28 +1000 | [diff] [blame] | 168 | .features = SPI_MASTER_4BA, |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 169 | .max_data_read = 64 * 1024, |
| 170 | .max_data_write = 256, |
| 171 | .command = ft2232_spi_send_command, |
| 172 | .multicommand = default_spi_send_multicommand, |
| 173 | .read = default_spi_read, |
| 174 | .write_256 = default_spi_write_256, |
mkarcher | d264e9e | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 177 | /* Returns 0 upon success, a negative number upon errors. */ |
David Hendricks | ac1d25c | 2016-08-09 17:00:58 -0700 | [diff] [blame] | 178 | int ft2232_spi_init(void) |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 179 | { |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 180 | int ret = 0; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 181 | struct ftdi_context *ftdic = &ftdic_context; |
| 182 | unsigned char buf[512]; |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 183 | int ft2232_vid = FTDI_VID; |
| 184 | int ft2232_type = FTDI_FT4232H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 185 | int channel_count = 4; /* Stores the number of channels of the device. */ |
| 186 | enum ftdi_interface ft2232_interface = INTERFACE_A; |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 187 | /* |
| 188 | * The 'H' chips can run with an internal clock of either 12 MHz or 60 MHz, |
| 189 | * but the non-H chips can only run at 12 MHz. We disable the divide-by-5 |
| 190 | * prescaler on 'H' chips so they run at 60MHz. |
| 191 | */ |
| 192 | uint8_t clock_5x = 1; |
| 193 | /* In addition to the prescaler mentioned above there is also another |
| 194 | * configurable one on all versions of the chips. Its divisor div can be |
| 195 | * set by a 16 bit value x according to the following formula: |
| 196 | * div = (1 + x) * 2 <-> x = div / 2 - 1 |
| 197 | * Hence the expressible divisors are all even numbers between 2 and |
| 198 | * 2^17 (=131072) resulting in SCK frequencies of 6 MHz down to about |
| 199 | * 92 Hz for 12 MHz inputs and 30 MHz down to about 458 Hz for 60 MHz |
| 200 | * inputs. |
| 201 | */ |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 202 | uint32_t divisor = DEFAULT_DIVISOR; |
| 203 | int f; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 204 | char *arg; |
hailfinger | 81f90f2 | 2010-11-10 03:18:41 +0000 | [diff] [blame] | 205 | double mpsse_clk; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 206 | |
hailfinger | ddeb4ac | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 207 | arg = extract_programmer_param("type"); |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 208 | if (arg) { |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 209 | if (!strcasecmp(arg, "2232H")) { |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 210 | ft2232_type = FTDI_FT2232H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 211 | channel_count = 2; |
| 212 | } else if (!strcasecmp(arg, "4232H")) { |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 213 | ft2232_type = FTDI_FT4232H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 214 | channel_count = 4; |
| 215 | } else if (!strcasecmp(arg, "232H")) { |
| 216 | ft2232_type = FTDI_FT232H_PID; |
| 217 | channel_count = 1; |
| 218 | } else if (!strcasecmp(arg, "jtagkey")) { |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 219 | ft2232_type = AMONTEC_JTAGKEY_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 220 | channel_count = 2; |
| 221 | /* JTAGkey(2) needs to enable its output via Bit4 / GPIOL0 |
| 222 | * value: 0x18 OE=high, CS=high, DI=low, DO=low, SK=low |
| 223 | * dir: 0x1b OE=output, CS=output, DI=input, DO=output, SK=output */ |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 224 | cs_bits = 0x18; |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 225 | pindir = 0x1b; |
uwe | 64e8d6f | 2011-10-20 23:14:10 +0000 | [diff] [blame] | 226 | } else if (!strcasecmp(arg, "picotap")) { |
| 227 | ft2232_vid = GOEPEL_VID; |
| 228 | ft2232_type = GOEPEL_PICOTAP_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 229 | channel_count = 2; |
uwe | d4ab19b | 2011-10-14 20:33:14 +0000 | [diff] [blame] | 230 | } else if (!strcasecmp(arg, "tumpa")) { |
| 231 | /* Interface A is SPI1, B is SPI2. */ |
| 232 | ft2232_type = TIAO_TUMPA_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 233 | channel_count = 2; |
| 234 | } else if (!strcasecmp(arg, "tumpalite")) { |
| 235 | /* Only one channel is used on lite edition */ |
| 236 | ft2232_type = TIAO_TUMPA_LITE_PID; |
| 237 | channel_count = 1; |
stefanct | 90aefe8 | 2011-08-12 01:19:32 +0000 | [diff] [blame] | 238 | } else if (!strcasecmp(arg, "busblaster")) { |
| 239 | /* In its default configuration it is a jtagkey clone */ |
| 240 | ft2232_type = FTDI_FT2232H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 241 | channel_count = 2; |
stefanct | 90aefe8 | 2011-08-12 01:19:32 +0000 | [diff] [blame] | 242 | cs_bits = 0x18; |
| 243 | pindir = 0x1b; |
hailfinger | ce67061 | 2010-11-10 03:26:57 +0000 | [diff] [blame] | 244 | } else if (!strcasecmp(arg, "openmoko")) { |
| 245 | ft2232_vid = FIC_VID; |
| 246 | ft2232_type = OPENMOKO_DBGBOARD_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 247 | channel_count = 2; |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 248 | } else if (!strcasecmp(arg, "arm-usb-ocd")) { |
| 249 | ft2232_vid = OLIMEX_VID; |
| 250 | ft2232_type = OLIMEX_ARM_OCD_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 251 | channel_count = 2; |
| 252 | /* arm-usb-ocd(-h) has an output buffer that needs to be enabled by pulling ADBUS4 low. |
| 253 | * value: 0x08 #OE=low, CS=high, DI=low, DO=low, SK=low |
| 254 | * dir: 0x1b #OE=output, CS=output, DI=input, DO=output, SK=output */ |
Paul Fertser | 80ed25d | 2016-07-29 15:31:03 -0700 | [diff] [blame] | 255 | cs_bits = 0x08; |
| 256 | pindir = 0x1b; |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 257 | } else if (!strcasecmp(arg, "arm-usb-tiny")) { |
| 258 | ft2232_vid = OLIMEX_VID; |
| 259 | ft2232_type = OLIMEX_ARM_TINY_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 260 | channel_count = 2; |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 261 | } else if (!strcasecmp(arg, "arm-usb-ocd-h")) { |
| 262 | ft2232_vid = OLIMEX_VID; |
| 263 | ft2232_type = OLIMEX_ARM_OCD_H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 264 | channel_count = 2; |
| 265 | /* See arm-usb-ocd */ |
Paul Fertser | 80ed25d | 2016-07-29 15:31:03 -0700 | [diff] [blame] | 266 | cs_bits = 0x08; |
| 267 | pindir = 0x1b; |
stefanct | 9348111 | 2011-06-11 12:21:37 +0000 | [diff] [blame] | 268 | } else if (!strcasecmp(arg, "arm-usb-tiny-h")) { |
| 269 | ft2232_vid = OLIMEX_VID; |
| 270 | ft2232_type = OLIMEX_ARM_TINY_H_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 271 | channel_count = 2; |
Edward O'Callaghan | b9a1dcf | 2020-03-02 17:14:14 +1100 | [diff] [blame] | 272 | } else if (!strcasecmp(arg, "google-servo")) { |
| 273 | ft2232_vid = GOOGLE_VID; |
| 274 | ft2232_type = GOOGLE_SERVO_PID; |
| 275 | channel_count = 2; |
| 276 | } else if (!strcasecmp(arg, "google-servo-v2")) { |
| 277 | ft2232_vid = GOOGLE_VID; |
| 278 | ft2232_type = GOOGLE_SERVO_V2_PID1; |
| 279 | channel_count = 2; |
| 280 | /* Default divisor is too fast, and chip ID fails */ |
| 281 | divisor = 6; |
| 282 | } else if (!strcasecmp(arg, "google-servo-v2-legacy")) { |
| 283 | ft2232_vid = GOOGLE_VID; |
| 284 | ft2232_type = GOOGLE_SERVO_V2_PID0; |
| 285 | channel_count = 2; |
| 286 | /* vv WARNING: deprecated vv */ |
Todd Broch | 7aff3df | 2011-08-23 13:41:53 -0700 | [diff] [blame] | 287 | } else if (!strcasecmp(arg, "servo")) { |
| 288 | ft2232_vid = GOOGLE_VID; |
| 289 | ft2232_type = GOOGLE_SERVO_PID; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 290 | channel_count = 2; |
Nicolas Boichat | d520d3b | 2020-05-18 08:26:20 +0800 | [diff] [blame] | 291 | msg_pwarn("Warning: Use 'google-servo' instead!\n"); |
Todd Broch | e1e13ec | 2012-01-17 09:32:13 -0800 | [diff] [blame] | 292 | } else if (!strcasecmp(arg, "servo-v2")) { |
| 293 | ft2232_vid = GOOGLE_VID; |
Todd Broch | 97f259f | 2012-04-03 15:52:14 -0700 | [diff] [blame] | 294 | ft2232_type = GOOGLE_SERVO_V2_PID1; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 295 | channel_count = 2; |
Nicolas Boichat | d520d3b | 2020-05-18 08:26:20 +0800 | [diff] [blame] | 296 | msg_pwarn("Warning: Use 'google-servo-v2' instead!\n"); |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 297 | /* Default divisor is too fast, and chip ID fails */ |
| 298 | divisor = 6; |
Todd Broch | 97f259f | 2012-04-03 15:52:14 -0700 | [diff] [blame] | 299 | } else if (!strcasecmp(arg, "servo-v2-legacy")) { |
| 300 | ft2232_vid = GOOGLE_VID; |
| 301 | ft2232_type = GOOGLE_SERVO_V2_PID0; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 302 | channel_count = 2; |
Nicolas Boichat | d520d3b | 2020-05-18 08:26:20 +0800 | [diff] [blame] | 303 | msg_pwarn("Warning: Use 'google-servo-v2-legacy' instead!\n"); |
Edward O'Callaghan | b9a1dcf | 2020-03-02 17:14:14 +1100 | [diff] [blame] | 304 | /* ^^ WARNING: deprecated ^^ */ |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 305 | } else if (!strcasecmp(arg, "flyswatter")) { |
| 306 | ft2232_type = FTDI_FT2232H_PID; |
| 307 | channel_count = 2; |
| 308 | /* Flyswatter and Flyswatter-2 require GPIO bits 0x80 |
| 309 | * and 0x40 to be driven low to enable output buffers */ |
| 310 | pindir = 0xcb; |
hailfinger | ce67061 | 2010-11-10 03:26:57 +0000 | [diff] [blame] | 311 | } else { |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 312 | msg_perr("Error: Invalid device type specified.\n"); |
| 313 | free(arg); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 314 | return -1; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | free(arg); |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 318 | |
hailfinger | ddeb4ac | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 319 | arg = extract_programmer_param("port"); |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 320 | if (arg) { |
hailfinger | ebad21f | 2010-10-08 12:40:09 +0000 | [diff] [blame] | 321 | switch (toupper((unsigned char)*arg)) { |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 322 | case 'A': |
| 323 | ft2232_interface = INTERFACE_A; |
| 324 | break; |
| 325 | case 'B': |
| 326 | ft2232_interface = INTERFACE_B; |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 327 | if (channel_count < 2) |
| 328 | channel_count = -1; |
| 329 | break; |
| 330 | case 'C': |
| 331 | ft2232_interface = INTERFACE_C; |
| 332 | if (channel_count < 3) |
| 333 | channel_count = -1; |
| 334 | break; |
| 335 | case 'D': |
| 336 | ft2232_interface = INTERFACE_D; |
| 337 | if (channel_count < 4) |
| 338 | channel_count = -1; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 339 | break; |
| 340 | default: |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 341 | channel_count = -1; |
| 342 | break; |
| 343 | } |
| 344 | if (channel_count < 0 || strlen(arg) != 1) { |
| 345 | msg_perr("Error: Invalid channel/port/interface specified: \"%s\".\n", arg); |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 346 | free(arg); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 347 | return -2; |
hailfinger | 1ef766d | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | free(arg); |
Nikolai Artemiev | 249cbab | 2020-02-07 11:53:15 +1100 | [diff] [blame] | 351 | |
Nikolai Artemiev | 249cbab | 2020-02-07 11:53:15 +1100 | [diff] [blame] | 352 | arg = extract_programmer_param("divisor"); |
| 353 | if (arg && strlen(arg)) { |
| 354 | unsigned int temp = 0; |
| 355 | char *endptr; |
| 356 | temp = strtoul(arg, &endptr, 10); |
| 357 | if (*endptr || temp < 2 || temp > 131072 || temp & 0x1) { |
| 358 | msg_perr("Error: Invalid SPI frequency divisor specified: \"%s\".\n" |
| 359 | "Valid are even values between 2 and 131072.\n", arg); |
| 360 | free(arg); |
| 361 | return -2; |
| 362 | } |
| 363 | divisor = (uint32_t)temp; |
| 364 | } |
| 365 | free(arg); |
| 366 | |
sibradzic | 419c86f | 2020-02-04 18:24:58 +0900 | [diff] [blame] | 367 | /* Allows setting multiple GPIOL states, for example: csgpiol=012 */ |
Sergey Alirzaev | 018a6df | 2018-08-01 16:39:17 +0300 | [diff] [blame] | 368 | arg = extract_programmer_param("csgpiol"); |
| 369 | if (arg) { |
sibradzic | 419c86f | 2020-02-04 18:24:58 +0900 | [diff] [blame] | 370 | unsigned int ngpios = strlen(arg); |
| 371 | for (unsigned int i = 0; i <= ngpios; i++) { |
| 372 | int temp = arg[i] - '0'; |
| 373 | if (ngpios == 0 || (ngpios != i && (temp < 0 || temp > 3))) { |
| 374 | msg_perr("Error: Invalid GPIOLs specified: \"%s\".\n" |
| 375 | "Valid values are numbers between 0 and 3. " |
| 376 | "Multiple GPIOLs can be specified.\n", arg); |
| 377 | free(arg); |
| 378 | return -2; |
| 379 | } else { |
| 380 | unsigned int pin = temp + 4; |
| 381 | cs_bits |= 1 << pin; |
| 382 | pindir |= 1 << pin; |
| 383 | } |
Sergey Alirzaev | 018a6df | 2018-08-01 16:39:17 +0300 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | free(arg); |
| 387 | |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 388 | msg_pdbg("Using device type %s %s ", |
uwe | 3dae687 | 2010-07-29 19:57:55 +0000 | [diff] [blame] | 389 | get_ft2232_vendorname(ft2232_vid, ft2232_type), |
| 390 | get_ft2232_devicename(ft2232_vid, ft2232_type)); |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 391 | msg_pdbg("channel %s.\n", |
| 392 | (ft2232_interface == INTERFACE_A) ? "A" : |
| 393 | (ft2232_interface == INTERFACE_B) ? "B" : |
| 394 | (ft2232_interface == INTERFACE_C) ? "C" : "D"); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 395 | |
| 396 | if (ftdi_init(ftdic) < 0) { |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 397 | msg_perr("ftdi_init failed.\n"); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 398 | return -3; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Todd Broch | cbaba9c | 2014-09-22 12:11:03 -0700 | [diff] [blame] | 401 | if (ftdi_set_interface(ftdic, ft2232_interface) < 0) { |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 402 | 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] | 403 | } |
| 404 | |
Shik Chen | 5eb5624 | 2012-08-07 15:09:37 +0800 | [diff] [blame] | 405 | arg = extract_programmer_param("serial"); |
| 406 | f = ftdi_usb_open_desc(ftdic, ft2232_vid, ft2232_type, NULL, arg); |
| 407 | free(arg); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 408 | |
| 409 | if (f < 0 && f != -5) { |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 410 | msg_perr("Unable to open FTDI device: %d (%s)\n", f, |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 411 | ftdi_get_error_string(ftdic)); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 412 | return -4; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Nikolai Artemiev | 0a3fb59 | 2020-01-09 11:37:46 +1100 | [diff] [blame] | 415 | if (ftdic->type != TYPE_2232H && ftdic->type != TYPE_4232H && ftdic->type != TYPE_232H) { |
| 416 | msg_pdbg("FTDI chip type %d is not high-speed.\n", ftdic->type); |
hailfinger | 81f90f2 | 2010-11-10 03:18:41 +0000 | [diff] [blame] | 417 | clock_5x = 0; |
| 418 | } |
| 419 | |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 420 | if (ftdi_usb_reset(ftdic) < 0) { |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 421 | 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] | 422 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 423 | |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 424 | if (ftdi_set_latency_timer(ftdic, 2) < 0) { |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 425 | 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] | 426 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 427 | |
el-coderon | 4fad488 | 2020-03-20 18:52:15 +0100 | [diff] [blame] | 428 | if (ftdi_write_data_set_chunksize(ftdic, 270)) { |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 429 | 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] | 430 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 431 | |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 432 | if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) { |
Edward O'Callaghan | 2068b92 | 2019-08-21 16:43:26 +1000 | [diff] [blame] | 433 | 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] | 434 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 435 | |
hailfinger | 81f90f2 | 2010-11-10 03:18:41 +0000 | [diff] [blame] | 436 | if (clock_5x) { |
| 437 | msg_pdbg("Disable divide-by-5 front stage\n"); |
Nikolai Artemiev | a5c9cd7 | 2020-01-08 10:40:19 +1100 | [diff] [blame] | 438 | buf[0] = DIS_DIV_5; |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 439 | if (send_buf(ftdic, buf, 1)) { |
| 440 | ret = -5; |
| 441 | goto ftdi_err; |
| 442 | } |
hailfinger | 81f90f2 | 2010-11-10 03:18:41 +0000 | [diff] [blame] | 443 | mpsse_clk = 60.0; |
| 444 | } else { |
| 445 | mpsse_clk = 12.0; |
Todd Broch | 7774665 | 2012-04-19 09:52:49 -0700 | [diff] [blame] | 446 | } |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 447 | |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 448 | msg_pdbg("Set clock divisor\n"); |
Nikolai Artemiev | a5c9cd7 | 2020-01-08 10:40:19 +1100 | [diff] [blame] | 449 | buf[0] = TCK_DIVISOR; |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 450 | buf[1] = (divisor / 2 - 1) & 0xff; |
| 451 | buf[2] = ((divisor / 2 - 1) >> 8) & 0xff; |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 452 | if (send_buf(ftdic, buf, 3)) { |
| 453 | ret = -6; |
| 454 | goto ftdi_err; |
| 455 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 456 | |
Nikolai Artemiev | 985f195 | 2020-01-20 16:33:30 +1100 | [diff] [blame] | 457 | msg_pdbg("MPSSE clock: %f MHz, divisor: %u, SPI clock: %f MHz\n", |
| 458 | mpsse_clk, divisor, (double)(mpsse_clk / divisor)); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 459 | |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 460 | /* Disconnect TDI/DO to TDO/DI for loopback. */ |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 461 | msg_pdbg("No loopback of TDI/DO TDO/DI\n"); |
Nikolai Artemiev | a5c9cd7 | 2020-01-08 10:40:19 +1100 | [diff] [blame] | 462 | buf[0] = LOOPBACK_END; |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 463 | if (send_buf(ftdic, buf, 1)) { |
| 464 | ret = -7; |
| 465 | goto ftdi_err; |
| 466 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 467 | |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 468 | msg_pdbg("Set data bits\n"); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 469 | buf[0] = SET_BITS_LOW; |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 470 | buf[1] = cs_bits; |
| 471 | buf[2] = pindir; |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 472 | if (send_buf(ftdic, buf, 3)) { |
| 473 | ret = -8; |
| 474 | goto ftdi_err; |
| 475 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 476 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 477 | register_spi_master(&spi_master_ft2232); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 478 | |
| 479 | return 0; |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 480 | |
| 481 | ftdi_err: |
| 482 | if ((f = ftdi_usb_close(ftdic)) < 0) { |
| 483 | msg_perr("Unable to close FTDI device: %d (%s)\n", f, |
| 484 | ftdi_get_error_string(ftdic)); |
| 485 | } |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 486 | return ret; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 487 | } |
| 488 | |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 489 | /* Returns 0 upon success, a negative number upon errors. */ |
Nikolai Artemiev | 0a63526 | 2020-02-11 13:49:52 +1100 | [diff] [blame] | 490 | static int ft2232_spi_send_command(const struct flashctx *flash, |
| 491 | unsigned int writecnt, unsigned int readcnt, |
| 492 | const unsigned char *writearr, |
| 493 | unsigned char *readarr) |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 494 | { |
| 495 | struct ftdi_context *ftdic = &ftdic_context; |
| 496 | static unsigned char *buf = NULL; |
hailfinger | 322be71 | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 497 | /* failed is special. We use bitwise ops, but it is essentially bool. */ |
hailfinger | b91c08c | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 498 | int i = 0, ret = 0, failed = 0; |
Angel Pons | 546ca74 | 2020-03-31 15:34:35 +0200 | [diff] [blame] | 499 | size_t bufsize; |
| 500 | static size_t oldbufsize = 0; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 501 | |
hailfinger | 9c290a7 | 2009-07-14 10:26:56 +0000 | [diff] [blame] | 502 | if (writecnt > 65536 || readcnt > 65536) |
| 503 | return SPI_INVALID_LENGTH; |
| 504 | |
hailfinger | 9060466 | 2009-11-25 16:58:17 +0000 | [diff] [blame] | 505 | /* buf is not used for the response from the chip. */ |
| 506 | bufsize = max(writecnt + 9, 260 + 9); |
| 507 | /* Never shrink. realloc() calls are expensive. */ |
Angel Pons | 546ca74 | 2020-03-31 15:34:35 +0200 | [diff] [blame] | 508 | if (!buf || bufsize > oldbufsize) { |
hailfinger | 9060466 | 2009-11-25 16:58:17 +0000 | [diff] [blame] | 509 | buf = realloc(buf, bufsize); |
| 510 | if (!buf) { |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 511 | msg_perr("Out of memory!\n"); |
uwe | 5619959 | 2011-07-21 09:18:18 +0000 | [diff] [blame] | 512 | /* TODO: What to do with buf? */ |
| 513 | return SPI_GENERIC_ERROR; |
hailfinger | 9060466 | 2009-11-25 16:58:17 +0000 | [diff] [blame] | 514 | } |
| 515 | oldbufsize = bufsize; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 516 | } |
| 517 | |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 518 | /* |
| 519 | * Minimize USB transfers by packing as many commands as possible |
| 520 | * together. If we're not expecting to read, we can assert CS#, write, |
| 521 | * and deassert CS# all in one shot. If reading, we do three separate |
stepan | a5807bf | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 522 | * operations. |
| 523 | */ |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 524 | msg_pspew("Assert CS#\n"); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 525 | buf[i++] = SET_BITS_LOW; |
sibradzic | 9ee0f20 | 2020-02-14 17:15:02 +0900 | [diff] [blame] | 526 | buf[i++] = ~ 0x08 & cs_bits; /* assert CS (3rd) bit only */ |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 527 | buf[i++] = pindir; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 528 | |
| 529 | if (writecnt) { |
Nikolai Artemiev | a5c9cd7 | 2020-01-08 10:40:19 +1100 | [diff] [blame] | 530 | buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 531 | buf[i++] = (writecnt - 1) & 0xff; |
| 532 | buf[i++] = ((writecnt - 1) >> 8) & 0xff; |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 533 | memcpy(buf + i, writearr, writecnt); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 534 | i += writecnt; |
| 535 | } |
| 536 | |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 537 | /* |
| 538 | * Optionally terminate this batch of commands with a |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 539 | * read command, then do the fetch of the results. |
| 540 | */ |
| 541 | if (readcnt) { |
Nikolai Artemiev | a5c9cd7 | 2020-01-08 10:40:19 +1100 | [diff] [blame] | 542 | buf[i++] = MPSSE_DO_READ; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 543 | buf[i++] = (readcnt - 1) & 0xff; |
| 544 | buf[i++] = ((readcnt - 1) >> 8) & 0xff; |
| 545 | ret = send_buf(ftdic, buf, i); |
hailfinger | 322be71 | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 546 | failed = ret; |
| 547 | /* We can't abort here, we still have to deassert CS#. */ |
| 548 | if (ret) |
uwe | 8d342eb | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 549 | msg_perr("send_buf failed before read: %i\n", ret); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 550 | i = 0; |
stepan | a5807bf | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 551 | if (ret == 0) { |
uwe | b34ec9f | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 552 | /* |
| 553 | * FIXME: This is unreliable. There's no guarantee that |
| 554 | * we read the response directly after sending the read |
| 555 | * command. We may be scheduled out etc. |
stepan | a5807bf | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 556 | */ |
| 557 | ret = get_buf(ftdic, readarr, readcnt); |
hailfinger | 322be71 | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 558 | failed |= ret; |
| 559 | /* We can't abort here either. */ |
| 560 | if (ret) |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 561 | msg_perr("get_buf failed: %i\n", ret); |
stepan | a5807bf | 2009-09-16 08:26:59 +0000 | [diff] [blame] | 562 | } |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 563 | } |
| 564 | |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 565 | msg_pspew("De-assert CS#\n"); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 566 | buf[i++] = SET_BITS_LOW; |
hailfinger | 888410e | 2010-07-29 15:54:53 +0000 | [diff] [blame] | 567 | buf[i++] = cs_bits; |
| 568 | buf[i++] = pindir; |
hailfinger | 322be71 | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 569 | ret = send_buf(ftdic, buf, i); |
| 570 | failed |= ret; |
| 571 | if (ret) |
snelson | 9393299 | 2010-01-10 01:08:37 +0000 | [diff] [blame] | 572 | msg_perr("send_buf failed at end: %i\n", ret); |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 573 | |
hailfinger | 322be71 | 2009-11-22 01:33:40 +0000 | [diff] [blame] | 574 | return failed ? -1 : 0; |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 575 | } |
| 576 | |
hailfinger | f31da3d | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 577 | #endif |