Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright 2014, Google Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions are |
| 9 | * met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following disclaimer |
| 15 | * in the documentation and/or other materials provided with the |
| 16 | * distribution. |
| 17 | * * Neither the name of Google Inc. nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived from |
| 19 | * this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | * Alternatively, this software may be distributed under the terms of the |
| 34 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 35 | * Software Foundation. |
| 36 | */ |
| 37 | |
| 38 | /* |
| 39 | * This SPI flash programming interface is designed to talk to a Chromium OS |
| 40 | * device over a Raiden USB connection. The USB connection is routed to a |
| 41 | * microcontroller running an image compiled from: |
| 42 | * |
| 43 | * https://chromium.googlesource.com/chromiumos/platform/ec |
| 44 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 45 | * The protocol for the USB-SPI bridge is implemented in the following files |
| 46 | * in that repository: |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 47 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 48 | * chip/stm32/usb_spi.h |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 49 | * chip/stm32/usb_spi.c |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 50 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 51 | * bInterfaceProtocol determines which protocol is used by the USB SPI device. |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 52 | * |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 53 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 54 | * USB SPI Version 1: |
| 55 | * |
| 56 | * SPI transactions of up to 62B in each direction with every command having |
| 57 | * a response. The initial packet from host contains a 2B header indicating |
| 58 | * write and read counts with an optional payload length equal to the write |
| 59 | * count. The device will respond with a message that reports the 2B status |
| 60 | * code and an optional payload response length equal to read count. |
| 61 | * |
| 62 | * |
| 63 | * Message Packets: |
| 64 | * |
| 65 | * Command First Packet (Host to Device): |
| 66 | * |
| 67 | * USB SPI command, containing the number of bytes to write and read |
| 68 | * and a payload of bytes to write. |
| 69 | * |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 70 | * +------------------+-----------------+------------------------+ |
| 71 | * | write count : 1B | read count : 1B | write payload : <= 62B | |
| 72 | * +------------------+-----------------+------------------------+ |
| 73 | * |
| 74 | * write count: 1 byte, zero based count of bytes to write |
| 75 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 76 | * read count: 1 byte, zero based count of bytes to read. Full duplex |
| 77 | * mode is enabled with UINT8_MAX |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 78 | * |
| 79 | * write payload: Up to 62 bytes of data to write to SPI, the total |
| 80 | * length of all TX packets must match write count. |
| 81 | * Due to data alignment constraints, this must be an |
| 82 | * even number of bytes unless this is the final packet. |
| 83 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 84 | * Response Packet (Device to Host): |
| 85 | * |
| 86 | * USB SPI response, containing the status code and any bytes of the |
| 87 | * read payload. |
| 88 | * |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 89 | * +-------------+-----------------------+ |
| 90 | * | status : 2B | read payload : <= 62B | |
| 91 | * +-------------+-----------------------+ |
| 92 | * |
| 93 | * status: 2 byte status |
| 94 | * 0x0000: Success |
| 95 | * 0x0001: SPI timeout |
| 96 | * 0x0002: Busy, try again |
| 97 | * This can happen if someone else has acquired the shared memory |
| 98 | * buffer that the SPI driver uses as /dev/null |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 99 | * 0x0003: Write count invalid (over 62 bytes) |
| 100 | * 0x0004: Read count invalid (over 62 bytes) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 101 | * 0x0005: The SPI bridge is disabled. |
| 102 | * 0x8000: Unknown error mask |
| 103 | * The bottom 15 bits will contain the bottom 15 bits from the EC |
| 104 | * error code. |
| 105 | * |
| 106 | * read payload: Up to 62 bytes of data read from SPI, the total |
| 107 | * length of all RX packets must match read count |
| 108 | * unless an error status was returned. Due to data |
| 109 | * alignment constraints, this must be a even number |
| 110 | * of bytes unless this is the final packet. |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 111 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 112 | * |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 113 | * USB Error Codes: |
| 114 | * |
| 115 | * send_command return codes have the following format: |
| 116 | * |
| 117 | * 0x00000: Status code success. |
| 118 | * 0x00001-0x0FFFF: Error code returned by the USB SPI device. |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 119 | * 0x10001-0x1FFFF: Error code returned by the USB SPI host. |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 120 | * 0x20001-0x20063 Lower bits store the positive value representation |
| 121 | * of the libusb_error enum. See the libusb documentation: |
| 122 | * http://libusb.sourceforge.net/api-1.0/group__misc.html |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 123 | */ |
| 124 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 125 | #include "programmer.h" |
| 126 | #include "spi.h" |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 127 | #include "usb_device.h" |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 128 | |
| 129 | #include <libusb.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 130 | #include <stdio.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 131 | #include <stdlib.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 132 | #include <string.h> |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 133 | #include <unistd.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 134 | |
Brian J. Nemec | b42d6c1 | 2020-07-23 03:07:38 -0700 | [diff] [blame] | 135 | /* FIXME: Add some programmer IDs here */ |
| 136 | const struct dev_entry devs_raiden[] = { |
| 137 | {0}, |
| 138 | }; |
| 139 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 140 | #define GOOGLE_VID (0x18D1) |
| 141 | #define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51) |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 142 | |
| 143 | enum { |
| 144 | GOOGLE_RAIDEN_SPI_PROTOCOL_V1 = 0x01, |
| 145 | GOOGLE_RAIDEN_SPI_PROTOCOL_V2 = 0x02, |
| 146 | }; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 147 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 148 | enum { |
| 149 | /* The host failed to transfer the data with no libusb error. */ |
| 150 | USB_SPI_HOST_TX_BAD_TRANSFER = 0x10001, |
| 151 | /* The number of bytes written did not match expected. */ |
| 152 | USB_SPI_HOST_TX_WRITE_FAILURE = 0x10002, |
| 153 | |
| 154 | /* We did not receive the expected USB packet. */ |
| 155 | USB_SPI_HOST_RX_UNEXPECTED_PACKET = 0x11001, |
| 156 | /* We received a continue packet with an invalid data index. */ |
| 157 | USB_SPI_HOST_RX_BAD_DATA_INDEX = 0x11002, |
| 158 | /* We received too much data. */ |
| 159 | USB_SPI_HOST_RX_DATA_OVERFLOW = 0x11003, |
| 160 | /* The number of bytes read did not match expected. */ |
| 161 | USB_SPI_HOST_RX_READ_FAILURE = 0x11004, |
| 162 | |
| 163 | /* We were unable to configure the device. */ |
| 164 | USB_SPI_HOST_INIT_FAILURE = 0x12001, |
| 165 | }; |
| 166 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 167 | enum usb_spi_error { |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 168 | USB_SPI_SUCCESS = 0x0000, |
| 169 | USB_SPI_TIMEOUT = 0x0001, |
| 170 | USB_SPI_BUSY = 0x0002, |
| 171 | USB_SPI_WRITE_COUNT_INVALID = 0x0003, |
| 172 | USB_SPI_READ_COUNT_INVALID = 0x0004, |
| 173 | USB_SPI_DISABLED = 0x0005, |
| 174 | USB_SPI_UNKNOWN_ERROR = 0x8000, |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 177 | enum raiden_debug_spi_request { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 178 | RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000, |
| 179 | RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001, |
| 180 | RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002, |
| 181 | RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003, |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 182 | }; |
| 183 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 184 | #define PACKET_HEADER_SIZE (2) |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 185 | #define USB_MAX_PACKET_SIZE (64) |
| 186 | #define PAYLOAD_SIZE_V1 (USB_MAX_PACKET_SIZE - PACKET_HEADER_SIZE) |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 187 | #define SPI_TRANSFER_V1_MAX (PAYLOAD_SIZE_V1) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 188 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 189 | /* |
| 190 | * Servo Micro has an error where it is capable of acknowledging USB packets |
| 191 | * without loading it into the USB endpoint buffers or triggering interrupts. |
| 192 | * See crbug.com/952494. Retry mechanisms have been implemented to recover |
| 193 | * from these rare failures allowing the process to continue. |
| 194 | */ |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 195 | #define WRITE_RETRY_ATTEMPTS (3) |
| 196 | #define READ_RETRY_ATTEMPTS (3) |
| 197 | #define RETRY_INTERVAL_US (100 * 1000) |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 198 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 199 | /* |
| 200 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 201 | */ |
Edward O'Callaghan | f1e6ef5 | 2020-03-03 13:57:15 +1100 | [diff] [blame] | 202 | #define TRANSFER_TIMEOUT_MS (200 + 800) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 203 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 204 | struct raiden_debug_spi_data { |
| 205 | struct usb_device *dev; |
| 206 | uint8_t in_ep; |
| 207 | uint8_t out_ep; |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 208 | uint8_t protocol_version; |
| 209 | /* |
| 210 | * Note: Due to bugs, flashrom does not always treat the max_data_write |
| 211 | * and max_data_read counts as the maximum packet size. As a result, we |
| 212 | * have to store a local copy of the actual max packet sizes and validate |
| 213 | * against it when performing transfers. |
| 214 | */ |
| 215 | uint16_t max_spi_write_count; |
| 216 | uint16_t max_spi_read_count; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 217 | }; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 218 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 219 | /* |
| 220 | * Version 1 protocol specific attributes |
| 221 | */ |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 222 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 223 | struct usb_spi_command_v1 { |
| 224 | uint8_t write_count; |
| 225 | /* UINT8_MAX indicates full duplex mode on compliant devices. */ |
| 226 | uint8_t read_count; |
| 227 | uint8_t data[PAYLOAD_SIZE_V1]; |
| 228 | } __attribute__((packed)); |
| 229 | |
| 230 | struct usb_spi_response_v1 { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 231 | uint16_t status_code; |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 232 | uint8_t data[PAYLOAD_SIZE_V1]; |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 233 | } __attribute__((packed)); |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 234 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 235 | union usb_spi_packet_v1 { |
| 236 | struct usb_spi_command_v1 command; |
| 237 | struct usb_spi_response_v1 response; |
| 238 | } __attribute__((packed)); |
| 239 | |
| 240 | struct usb_spi_packet_ctx { |
| 241 | union { |
| 242 | uint8_t bytes[USB_MAX_PACKET_SIZE]; |
| 243 | union usb_spi_packet_v1 packet_v1; |
| 244 | }; |
| 245 | /* |
| 246 | * By storing the number of bytes in the header and knowing that the |
| 247 | * USB data packets are all 64B long, we are able to use the header |
| 248 | * size to store the offset of the buffer and it's size without |
| 249 | * duplicating variables that can go out of sync. |
| 250 | */ |
| 251 | size_t header_size; |
| 252 | /* Number of bytes in the packet */ |
| 253 | size_t packet_size; |
| 254 | }; |
| 255 | |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 256 | struct usb_spi_transmit_ctx { |
| 257 | /* Buffer we are reading data from. */ |
| 258 | const uint8_t *buffer; |
| 259 | /* Number of bytes in the transfer. */ |
| 260 | size_t transmit_size; |
| 261 | /* Number of bytes transferred. */ |
| 262 | size_t transmit_index; |
| 263 | }; |
| 264 | |
| 265 | struct usb_spi_receive_ctx { |
| 266 | /* Buffer we are writing data into. */ |
| 267 | uint8_t *buffer; |
| 268 | /* Number of bytes in the transfer. */ |
| 269 | size_t receive_size; |
| 270 | /* Number of bytes transferred. */ |
| 271 | size_t receive_index; |
| 272 | }; |
| 273 | |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 274 | /* |
| 275 | * This function will return true when an error code can potentially recover |
| 276 | * if we attempt to write SPI data to the device or read from it. We know |
| 277 | * that some conditions are not recoverable in the current state so allows us |
| 278 | * to bypass the retry logic and terminate early. |
| 279 | */ |
| 280 | static bool retry_recovery(int error_code) |
| 281 | { |
| 282 | if (error_code < 0x10000) { |
Brian J. Nemec | 1a61f72 | 2020-05-04 20:58:06 -0700 | [diff] [blame] | 283 | /* |
| 284 | * Handle error codes returned from the device. USB_SPI_TIMEOUT, |
| 285 | * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed |
| 286 | * during transfer errors to the device and can be recovered. |
| 287 | */ |
| 288 | if (USB_SPI_READ_COUNT_INVALID <= error_code && |
| 289 | error_code <= USB_SPI_DISABLED) { |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 290 | return false; |
| 291 | } |
| 292 | } else if (usb_device_is_libusb_error(error_code)) { |
| 293 | /* Handle error codes returned from libusb. */ |
| 294 | if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) { |
| 295 | return false; |
| 296 | } |
| 297 | } |
| 298 | return true; |
| 299 | } |
| 300 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 301 | static struct raiden_debug_spi_data * |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 302 | get_raiden_data_from_context(const struct flashctx *flash) |
| 303 | { |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 304 | return (struct raiden_debug_spi_data *)flash->mst->spi.data; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 307 | /* |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 308 | * Read data into the receive buffer. |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 309 | * |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 310 | * @param dst Destination receive context we are writing data to. |
| 311 | * @param src Source packet context we are reading data from. |
| 312 | * |
| 313 | * @returns status code 0 on success. |
| 314 | * USB_SPI_HOST_RX_DATA_OVERFLOW if the source packet is too |
| 315 | * large to fit in read buffer. |
| 316 | */ |
| 317 | static int read_usb_packet(struct usb_spi_receive_ctx *dst, |
| 318 | const struct usb_spi_packet_ctx *src) |
| 319 | { |
| 320 | size_t max_read_length = dst->receive_size - dst->receive_index; |
| 321 | size_t bytes_in_buffer = src->packet_size - src->header_size; |
| 322 | const uint8_t *packet_buffer = src->bytes + src->header_size; |
| 323 | |
| 324 | if (bytes_in_buffer > max_read_length) { |
| 325 | /* |
| 326 | * An error occurred, we should not receive more data than |
| 327 | * the buffer can support. |
| 328 | */ |
| 329 | msg_perr("Raiden: Receive packet overflowed\n" |
| 330 | " bytes_in_buffer = %zu\n" |
| 331 | " max_read_length = %zu\n" |
| 332 | " receive_index = %zu\n" |
| 333 | " receive_size = %zu\n", |
| 334 | bytes_in_buffer, max_read_length, |
| 335 | dst->receive_size, dst->receive_index); |
| 336 | return USB_SPI_HOST_RX_DATA_OVERFLOW; |
| 337 | } |
| 338 | memcpy(dst->buffer + dst->receive_index, packet_buffer, |
| 339 | bytes_in_buffer); |
| 340 | |
| 341 | dst->receive_index += bytes_in_buffer; |
| 342 | return 0; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Fill the USB packet with data from the transmit buffer. |
| 347 | * |
| 348 | * @param dst Destination packet context we are writing data to. |
| 349 | * @param src Source transmit context we are reading data from. |
| 350 | */ |
| 351 | static void fill_usb_packet(struct usb_spi_packet_ctx *dst, |
| 352 | struct usb_spi_transmit_ctx *src) |
| 353 | { |
| 354 | size_t transmit_size = src->transmit_size - src->transmit_index; |
| 355 | size_t max_buffer_size = USB_MAX_PACKET_SIZE - dst->header_size; |
| 356 | uint8_t *packet_buffer = dst->bytes + dst->header_size; |
| 357 | |
| 358 | if (transmit_size > max_buffer_size) |
| 359 | transmit_size = max_buffer_size; |
| 360 | |
| 361 | memcpy(packet_buffer, src->buffer + src->transmit_index, transmit_size); |
| 362 | |
| 363 | dst->packet_size = dst->header_size + transmit_size; |
| 364 | src->transmit_index += transmit_size; |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Receive the data from the device USB endpoint and store in the packet. |
| 369 | * |
| 370 | * @param ctx_data Raiden SPI config. |
| 371 | * @param packet Destination packet used to store the endpoint data. |
| 372 | * |
| 373 | * @returns Returns status code with 0 on success. |
| 374 | */ |
| 375 | static int receive_packet(const struct raiden_debug_spi_data *ctx_data, |
| 376 | struct usb_spi_packet_ctx *packet) |
| 377 | { |
| 378 | int received; |
| 379 | int status = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle, |
| 380 | ctx_data->in_ep, |
| 381 | packet->bytes, |
| 382 | USB_MAX_PACKET_SIZE, |
| 383 | &received, |
| 384 | TRANSFER_TIMEOUT_MS)); |
| 385 | packet->packet_size = received; |
| 386 | if (status) { |
| 387 | msg_perr("Raiden: IN transfer failed\n" |
| 388 | " received = %d\n" |
| 389 | " status = 0x%05x\n", |
| 390 | received, status); |
| 391 | } |
| 392 | return status; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Transmit data from the packet to the device's USB endpoint. |
| 397 | * |
| 398 | * @param ctx_data Raiden SPI config. |
| 399 | * @param packet Source packet we will write to the endpoint data. |
| 400 | * |
| 401 | * @returns Returns status code with 0 on success. |
| 402 | */ |
| 403 | static int transmit_packet(const struct raiden_debug_spi_data *ctx_data, |
| 404 | struct usb_spi_packet_ctx *packet) |
| 405 | { |
| 406 | int transferred; |
| 407 | int status = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle, |
| 408 | ctx_data->out_ep, |
| 409 | packet->bytes, |
| 410 | packet->packet_size, |
| 411 | &transferred, |
| 412 | TRANSFER_TIMEOUT_MS)); |
| 413 | if (status || (size_t)transferred != packet->packet_size) { |
| 414 | if (!status) { |
| 415 | /* No error was reported, but we didn't transmit the data expected. */ |
| 416 | status = USB_SPI_HOST_TX_BAD_TRANSFER; |
| 417 | } |
| 418 | msg_perr("Raiden: OUT transfer failed\n" |
| 419 | " transferred = %d\n" |
| 420 | " packet_size = %zu\n" |
| 421 | " status = 0x%05x\n", |
| 422 | transferred, packet->packet_size, status); |
| 423 | |
| 424 | } |
| 425 | return status; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Version 1 protocol command to start a USB SPI transfer and write the payload. |
| 430 | * |
| 431 | * @param ctx_data Raiden SPI config. |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 432 | * @param write Write context of data to transmit and write payload. |
| 433 | * @param read Read context of data to receive and read buffer. |
| 434 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 435 | * @returns Returns status code with 0 on success. |
| 436 | */ |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 437 | static int write_command_v1(const struct raiden_debug_spi_data *ctx_data, |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 438 | struct usb_spi_transmit_ctx *write, |
| 439 | struct usb_spi_receive_ctx *read) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 440 | { |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 441 | struct usb_spi_packet_ctx command = { |
| 442 | .header_size = offsetof(struct usb_spi_command_v1, data), |
| 443 | .packet_v1.command.write_count = write->transmit_size, |
| 444 | .packet_v1.command.read_count = read->receive_size |
| 445 | }; |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 446 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 447 | /* Reset the write context to the start. */ |
| 448 | write->transmit_index = 0; |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 449 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 450 | fill_usb_packet(&command, write); |
| 451 | return transmit_packet(ctx_data, &command); |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 454 | /* |
| 455 | * Version 1 Protocol: Responsible for reading the response of the USB SPI |
| 456 | * transfer. Status codes from the transfer and any read payload are copied |
| 457 | * to the read_buffer. |
| 458 | * |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 459 | * @param ctx_data Raiden SPI config. |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 460 | * @param write Write context of data to transmit and write payload. |
| 461 | * @param read Read context of data to receive and read buffer. |
| 462 | * |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 463 | * @returns Returns status code with 0 on success. |
| 464 | */ |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 465 | static int read_response_v1(const struct raiden_debug_spi_data *ctx_data, |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 466 | struct usb_spi_transmit_ctx *write, |
| 467 | struct usb_spi_receive_ctx *read) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 468 | { |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 469 | int status; |
| 470 | struct usb_spi_packet_ctx response; |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 471 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 472 | /* Reset the read context to the start. */ |
| 473 | read->receive_index = 0; |
| 474 | |
| 475 | status = receive_packet(ctx_data, &response); |
| 476 | if (status) { |
| 477 | /* Return the transfer error since the status_code is unreliable */ |
| 478 | return status; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 479 | } |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 480 | if (response.packet_v1.response.status_code) { |
| 481 | return response.packet_v1.response.status_code; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 482 | } |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 483 | response.header_size = offsetof(struct usb_spi_response_v1, data); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 484 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 485 | status = read_usb_packet(read, &response); |
| 486 | return status; |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 489 | /* |
| 490 | * Version 1 Protocol: Sets up a USB SPI transfer, transmits data to the device, |
| 491 | * reads the status code and any payload from the device. This will also handle |
| 492 | * recovery if an error has occurred. |
| 493 | * |
| 494 | * @param flash Flash context storing SPI capabilities and USB device |
| 495 | * information. |
| 496 | * @param write_count Number of bytes to write |
| 497 | * @param read_count Number of bytes to read |
| 498 | * @param write_buffer Address of write buffer |
| 499 | * @param read_buffer Address of buffer to store read data |
| 500 | * |
| 501 | * @returns Returns status code with 0 on success. |
| 502 | */ |
| 503 | static int send_command_v1(const struct flashctx *flash, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 504 | unsigned int write_count, |
| 505 | unsigned int read_count, |
| 506 | const unsigned char *write_buffer, |
| 507 | unsigned char *read_buffer) |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 508 | { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 509 | int status = -1; |
| 510 | |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 511 | struct usb_spi_transmit_ctx write_ctx = { |
| 512 | .buffer = write_buffer, |
| 513 | .transmit_size = write_count |
| 514 | }; |
| 515 | struct usb_spi_receive_ctx read_ctx = { |
| 516 | .buffer = read_buffer, |
| 517 | .receive_size = read_count |
| 518 | }; |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 519 | const struct raiden_debug_spi_data *ctx_data = get_raiden_data_from_context(flash); |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 520 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 521 | if (write_count > ctx_data->max_spi_write_count) { |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 522 | msg_perr("Raiden: Invalid write count\n" |
| 523 | " write count = %u\n" |
| 524 | " max write = %d\n", |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 525 | write_count, ctx_data->max_spi_write_count); |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 526 | return SPI_INVALID_LENGTH; |
| 527 | } |
| 528 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 529 | if (read_count > ctx_data->max_spi_read_count) { |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 530 | msg_perr("Raiden: Invalid read count\n" |
| 531 | " read count = %d\n" |
| 532 | " max read = %d\n", |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 533 | read_count, ctx_data->max_spi_read_count); |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 534 | return SPI_INVALID_LENGTH; |
| 535 | } |
| 536 | |
| 537 | for (unsigned int write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS; |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 538 | write_attempt++) { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 539 | |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 540 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 541 | status = write_command_v1(ctx_data, &write_ctx, &read_ctx); |
| 542 | |
| 543 | if (!status && |
| 544 | (write_ctx.transmit_index != write_ctx.transmit_size)) { |
| 545 | /* No errors were reported, but write is incomplete. */ |
| 546 | status = USB_SPI_HOST_TX_WRITE_FAILURE; |
| 547 | } |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 548 | |
| 549 | if (status) { |
| 550 | /* Write operation failed. */ |
| 551 | msg_perr("Raiden: Write command failed\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 552 | " write count = %u\n" |
| 553 | " read count = %u\n" |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 554 | " transmitted bytes = %zu\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 555 | " write attempt = %u\n" |
| 556 | " status = 0x%05x\n", |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 557 | |
| 558 | write_count, read_count, write_ctx.transmit_index, |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 559 | write_attempt + 1, status); |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 560 | if (!retry_recovery(status)) { |
| 561 | /* Reattempting will not result in a recovery. */ |
| 562 | return status; |
| 563 | } |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 564 | programmer_delay(RETRY_INTERVAL_US); |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 565 | continue; |
| 566 | } |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 567 | for (unsigned int read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS; |
| 568 | read_attempt++) { |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 569 | |
Brian J. Nemec | 3ddd683 | 2020-07-22 03:20:18 -0700 | [diff] [blame] | 570 | status = read_response_v1(ctx_data, &write_ctx, &read_ctx); |
| 571 | |
| 572 | if (!status) { |
| 573 | if (read_ctx.receive_size == read_ctx.receive_index) { |
| 574 | /* Successful transfer. */ |
| 575 | return status; |
| 576 | } else { |
| 577 | /* Report the error from the failed read. */ |
| 578 | status = USB_SPI_HOST_RX_READ_FAILURE; |
| 579 | } |
| 580 | } |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 581 | |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 582 | if (status) { |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 583 | /* Read operation failed. */ |
| 584 | msg_perr("Raiden: Read response failed\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 585 | " write count = %u\n" |
| 586 | " read count = %u\n" |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 587 | " received bytes = %zu\n" |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 588 | " write attempt = %u\n" |
| 589 | " read attempt = %u\n" |
| 590 | " status = 0x%05x\n", |
Brian J. Nemec | 1889a03 | 2020-07-22 03:16:45 -0700 | [diff] [blame] | 591 | write_count, read_count, read_ctx.receive_index, |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 592 | write_attempt + 1, read_attempt + 1, status); |
Brian J. Nemec | cea6bec | 2020-02-25 14:12:46 -0800 | [diff] [blame] | 593 | if (!retry_recovery(status)) { |
| 594 | /* Reattempting will not result in a recovery. */ |
| 595 | return status; |
| 596 | } |
Brian J. Nemec | c5d6946 | 2020-07-22 03:12:15 -0700 | [diff] [blame] | 597 | programmer_delay(RETRY_INTERVAL_US); |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame] | 598 | } |
| 599 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 600 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 601 | return status; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 604 | static const struct spi_master spi_master_raiden_debug = { |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 605 | .features = SPI_MASTER_4BA, |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 606 | .max_data_read = 0, |
| 607 | .max_data_write = 0, |
| 608 | .command = NULL, |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 609 | .multicommand = default_spi_send_multicommand, |
| 610 | .read = default_spi_read, |
| 611 | .write_256 = default_spi_write_256, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 612 | }; |
| 613 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 614 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 615 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 616 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 617 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 618 | direction) && |
| 619 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 620 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 621 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 622 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 623 | static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep) |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 624 | { |
| 625 | int i; |
| 626 | int in_count = 0; |
| 627 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 628 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 629 | for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 630 | struct libusb_endpoint_descriptor const *endpoint = |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 631 | &dev->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 632 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 633 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 634 | in_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 635 | *in_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 636 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 637 | out_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 638 | *out_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
| 642 | if (in_count != 1 || out_count != 1) { |
| 643 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 644 | " found %d IN and %d OUT endpoints\n", |
| 645 | in_count, |
| 646 | out_count); |
| 647 | return 1; |
| 648 | } |
| 649 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 650 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep); |
| 651 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep); |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 652 | |
| 653 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 654 | } |
| 655 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 656 | /* |
| 657 | * Configure the USB SPI master based on the device we are connected to. |
| 658 | * It will use the device's bInterfaceProtocol to identify which protocol |
| 659 | * is being used by the device USB SPI interface and if needed query the |
| 660 | * device for its capabilities. |
| 661 | * |
| 662 | * @param spi_config Raiden SPI config which will be modified. |
| 663 | * |
| 664 | * @returns Returns status code with 0 on success. |
| 665 | */ |
| 666 | static int configure_protocol(struct spi_master *spi_config) |
| 667 | { |
| 668 | struct raiden_debug_spi_data *ctx_data = |
| 669 | (struct raiden_debug_spi_data *)spi_config->data; |
| 670 | |
| 671 | ctx_data->protocol_version = |
| 672 | ctx_data->dev->interface_descriptor->bInterfaceProtocol; |
| 673 | |
| 674 | switch (ctx_data->protocol_version) { |
| 675 | case GOOGLE_RAIDEN_SPI_PROTOCOL_V1: |
| 676 | /* |
| 677 | * Protocol V1 is supported by adjusting the max data |
| 678 | * read and write sizes which results in no continue packets. |
| 679 | */ |
| 680 | spi_config->command = send_command_v1; |
| 681 | ctx_data->max_spi_write_count = SPI_TRANSFER_V1_MAX; |
| 682 | ctx_data->max_spi_read_count = SPI_TRANSFER_V1_MAX; |
| 683 | break; |
| 684 | default: |
| 685 | msg_pdbg("Raiden: Unknown USB SPI protocol version = %d", |
| 686 | ctx_data->protocol_version); |
| 687 | return USB_SPI_HOST_INIT_FAILURE; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 692 | * of bytes that your SPI device can read/write, these values are the maximum |
| 693 | * data chunk size that flashrom will package up with an additional five bytes |
| 694 | * of command for the flash device. |
| 695 | * |
| 696 | * The largest command that flashrom generates is the byte program command, so |
| 697 | * we use that command header maximum size here. If we didn't include the |
| 698 | * offset, flashrom may request a SPI transfer that is too large for the SPI |
| 699 | * device to support. |
| 700 | */ |
| 701 | spi_config->max_data_write = ctx_data->max_spi_write_count - |
| 702 | JEDEC_BYTE_PROGRAM_OUTSIZE; |
| 703 | spi_config->max_data_read = ctx_data->max_spi_read_count - |
| 704 | JEDEC_BYTE_PROGRAM_OUTSIZE; |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 709 | static int raiden_debug_spi_shutdown(void * data) |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 710 | { |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 711 | struct spi_master *spi_config = data; |
| 712 | struct raiden_debug_spi_data *ctx_data = |
| 713 | (struct raiden_debug_spi_data *)spi_config->data; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 714 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 715 | int ret = LIBUSB(libusb_control_transfer( |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 716 | ctx_data->dev->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 717 | LIBUSB_ENDPOINT_OUT | |
| 718 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 719 | LIBUSB_RECIPIENT_INTERFACE, |
| 720 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 721 | 0, |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 722 | ctx_data->dev->interface_descriptor->bInterfaceNumber, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 723 | NULL, |
| 724 | 0, |
| 725 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 726 | if (ret != 0) { |
| 727 | msg_perr("Raiden: Failed to disable SPI bridge\n"); |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 728 | free(ctx_data); |
| 729 | free(spi_config); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 730 | return ret; |
| 731 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 732 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 733 | usb_device_free(ctx_data->dev); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 734 | libusb_exit(NULL); |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 735 | free(ctx_data); |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 736 | free(spi_config); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 737 | |
| 738 | return 0; |
| 739 | } |
| 740 | |
Edward O'Callaghan | dda1e54 | 2020-02-03 12:45:01 +1100 | [diff] [blame] | 741 | static int get_target(void) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 742 | { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 743 | int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 744 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 745 | char *target_str = extract_programmer_param("target"); |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 746 | if (target_str) { |
| 747 | if (!strcasecmp(target_str, "ap")) |
| 748 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP; |
| 749 | else if (!strcasecmp(target_str, "ec")) |
| 750 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC; |
| 751 | else { |
| 752 | msg_perr("Invalid target: %s\n", target_str); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 753 | request_enable = -1; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 756 | free(target_str); |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 757 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 758 | return request_enable; |
| 759 | } |
| 760 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 761 | static void free_dev_list(struct usb_device **dev_lst) |
| 762 | { |
| 763 | struct usb_device *dev = *dev_lst; |
| 764 | /* free devices we don't care about */ |
| 765 | dev = dev->next; |
| 766 | while (dev) |
| 767 | dev = usb_device_free(dev); |
| 768 | } |
| 769 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 770 | int raiden_debug_spi_init(void) |
| 771 | { |
| 772 | struct usb_match match; |
| 773 | char *serial = extract_programmer_param("serial"); |
| 774 | struct usb_device *current; |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 775 | struct usb_device *device = NULL; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 776 | int found = 0; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 777 | int ret; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 778 | |
| 779 | int request_enable = get_target(); |
Brian J. Nemec | 7b5ad79 | 2020-07-23 03:35:54 -0700 | [diff] [blame] | 780 | if (request_enable < 0) { |
| 781 | free(serial); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 782 | return 1; |
Brian J. Nemec | 7b5ad79 | 2020-07-23 03:35:54 -0700 | [diff] [blame] | 783 | } |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 784 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 785 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 786 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 787 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 788 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 789 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
Brian J. Nemec | b746f82 | 2020-07-22 02:57:56 -0700 | [diff] [blame] | 790 | usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL_V1); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 791 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 792 | ret = LIBUSB(libusb_init(NULL)); |
| 793 | if (ret != 0) { |
| 794 | msg_perr("Raiden: libusb_init failed\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 795 | free(serial); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 796 | return ret; |
| 797 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 798 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 799 | ret = usb_device_find(&match, ¤t); |
| 800 | if (ret != 0) { |
| 801 | msg_perr("Raiden: Failed to find devices\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 802 | free(serial); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 803 | return ret; |
| 804 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 805 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 806 | uint8_t in_endpoint = 0; |
| 807 | uint8_t out_endpoint = 0; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 808 | while (current) { |
| 809 | device = current; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 810 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 811 | if (find_endpoints(device, &in_endpoint, &out_endpoint)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 812 | msg_pdbg("Raiden: Failed to find valid endpoints on device"); |
| 813 | usb_device_show(" ", current); |
| 814 | goto loop_end; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 815 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 816 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 817 | if (usb_device_claim(device)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 818 | msg_pdbg("Raiden: Failed to claim USB device"); |
| 819 | usb_device_show(" ", current); |
| 820 | goto loop_end; |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 821 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 822 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 823 | if (!serial) { |
| 824 | found = 1; |
| 825 | goto loop_end; |
| 826 | } else { |
| 827 | unsigned char dev_serial[32]; |
| 828 | struct libusb_device_descriptor descriptor; |
| 829 | int rc; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 830 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 831 | memset(dev_serial, 0, sizeof(dev_serial)); |
| 832 | |
| 833 | if (libusb_get_device_descriptor(device->device, &descriptor)) { |
| 834 | msg_pdbg("USB: Failed to get device descriptor.\n"); |
| 835 | goto loop_end; |
| 836 | } |
| 837 | |
| 838 | rc = libusb_get_string_descriptor_ascii(device->handle, |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 839 | descriptor.iSerialNumber, |
| 840 | dev_serial, |
| 841 | sizeof(dev_serial)); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 842 | if (rc < 0) { |
| 843 | LIBUSB(rc); |
| 844 | } else { |
| 845 | if (strcmp(serial, (char *)dev_serial)) { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 846 | msg_pdbg("Raiden: Serial number %s did not match device", serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 847 | usb_device_show(" ", current); |
| 848 | } else { |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 849 | msg_pinfo("Raiden: Serial number %s matched device", serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 850 | usb_device_show(" ", current); |
| 851 | found = 1; |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | loop_end: |
| 857 | if (found) |
| 858 | break; |
| 859 | else |
| 860 | current = usb_device_free(current); |
| 861 | } |
| 862 | |
| 863 | if (!device || !found) { |
| 864 | msg_perr("Raiden: No usable device found.\n"); |
Brian J. Nemec | a88d96e | 2020-07-23 03:22:57 -0700 | [diff] [blame] | 865 | free(serial); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 866 | return 1; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 867 | } |
| 868 | |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 869 | free_dev_list(¤t); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 870 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 871 | ret = LIBUSB(libusb_control_transfer( |
Brian J. Nemec | ec15cc4 | 2020-07-22 02:30:55 -0700 | [diff] [blame] | 872 | device->handle, |
| 873 | LIBUSB_ENDPOINT_OUT | |
| 874 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 875 | LIBUSB_RECIPIENT_INTERFACE, |
| 876 | request_enable, |
| 877 | 0, |
| 878 | device->interface_descriptor->bInterfaceNumber, |
| 879 | NULL, |
| 880 | 0, |
| 881 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 882 | if (ret != 0) { |
| 883 | msg_perr("Raiden: Failed to enable SPI bridge\n"); |
| 884 | return ret; |
| 885 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 886 | |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 887 | /* |
| 888 | * Allow for power to settle on the AP and EC flash devices. |
| 889 | * Load switches can have a 1-3 ms turn on time, and SPI flash devices |
| 890 | * can require up to 10 ms from power on to the first write. |
| 891 | */ |
| 892 | if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) || |
| 893 | (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC)) |
| 894 | usleep(50 * 1000); |
| 895 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 896 | struct spi_master *spi_config = calloc(1, sizeof(struct spi_master)); |
| 897 | if (!spi_config) { |
| 898 | msg_perr("Unable to allocate space for SPI master.\n"); |
| 899 | return SPI_GENERIC_ERROR; |
| 900 | } |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 901 | struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data)); |
| 902 | if (!data) { |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 903 | free(spi_config); |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 904 | msg_perr("Unable to allocate space for extra SPI master data.\n"); |
| 905 | return SPI_GENERIC_ERROR; |
| 906 | } |
| 907 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 908 | memcpy(spi_config, &spi_master_raiden_debug, sizeof(struct spi_master)); |
| 909 | |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 910 | data->dev = device; |
| 911 | data->in_ep = in_endpoint; |
| 912 | data->out_ep = out_endpoint; |
| 913 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 914 | spi_config->data = data; |
| 915 | /* |
| 916 | * The SPI master needs to be configured based on the device connected. |
| 917 | * Using the device protocol interrogation, we will set the limits on |
| 918 | * the write and read sizes and switch command functions. |
| 919 | */ |
| 920 | ret = configure_protocol(spi_config); |
| 921 | if (ret) { |
| 922 | msg_perr("Raiden: Error configuring protocol\n" |
| 923 | " protocol = %u\n" |
| 924 | " status = 0x%05x\n", |
| 925 | data->dev->interface_descriptor->bInterfaceProtocol, ret); |
| 926 | free(data); |
| 927 | free(spi_config); |
| 928 | return SPI_GENERIC_ERROR; |
| 929 | } |
Brian J. Nemec | 721ac2c | 2020-07-23 03:30:53 -0700 | [diff] [blame] | 930 | |
Brian J. Nemec | 113c2b4 | 2020-07-22 03:28:55 -0700 | [diff] [blame^] | 931 | register_spi_master(spi_config); |
| 932 | register_shutdown(raiden_debug_spi_shutdown, spi_config); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 933 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 934 | return 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 935 | } |