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 | * |
| 45 | * The protocol for the USB-SPI bridge is documented in the following file in |
| 46 | * that respository: |
| 47 | * |
| 48 | * chip/stm32/usb_spi.c |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 49 | * |
| 50 | * Version 1: |
| 51 | * SPI transactions of up to 62B in each direction with every command having |
| 52 | * a response. The initial packet from host contains a 2B header indicating |
| 53 | * write and read counts with an optional payload length equal to the write |
| 54 | * count. The device will respond with a message that reports the 2B status |
| 55 | * code and an optional payload response length equal to read count. |
| 56 | * |
| 57 | * Message Format: |
| 58 | * |
| 59 | * Command Packet: |
| 60 | * +------------------+-----------------+------------------------+ |
| 61 | * | write count : 1B | read count : 1B | write payload : <= 62B | |
| 62 | * +------------------+-----------------+------------------------+ |
| 63 | * |
| 64 | * write count: 1 byte, zero based count of bytes to write |
| 65 | * |
| 66 | * read count: 1 byte, zero based count of bytes to read |
| 67 | * |
| 68 | * write payload: Up to 62 bytes of data to write to SPI, the total |
| 69 | * length of all TX packets must match write count. |
| 70 | * Due to data alignment constraints, this must be an |
| 71 | * even number of bytes unless this is the final packet. |
| 72 | * |
| 73 | * Response Packet: |
| 74 | * +-------------+-----------------------+ |
| 75 | * | status : 2B | read payload : <= 62B | |
| 76 | * +-------------+-----------------------+ |
| 77 | * |
| 78 | * status: 2 byte status |
| 79 | * 0x0000: Success |
| 80 | * 0x0001: SPI timeout |
| 81 | * 0x0002: Busy, try again |
| 82 | * This can happen if someone else has acquired the shared memory |
| 83 | * buffer that the SPI driver uses as /dev/null |
| 84 | * 0x0003: Write count invalid (V1 > 62B) |
| 85 | * 0x0004: Read count invalid (V1 > 62B) |
| 86 | * 0x0005: The SPI bridge is disabled. |
| 87 | * 0x8000: Unknown error mask |
| 88 | * The bottom 15 bits will contain the bottom 15 bits from the EC |
| 89 | * error code. |
| 90 | * |
| 91 | * read payload: Up to 62 bytes of data read from SPI, the total |
| 92 | * length of all RX packets must match read count |
| 93 | * unless an error status was returned. Due to data |
| 94 | * alignment constraints, this must be a even number |
| 95 | * of bytes unless this is the final packet. |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 96 | */ |
| 97 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 98 | #include "programmer.h" |
| 99 | #include "spi.h" |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 100 | #include "usb_device.h" |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 101 | |
| 102 | #include <libusb.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 103 | #include <stdio.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 104 | #include <stdlib.h> |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 105 | #include <string.h> |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 106 | #include <unistd.h> |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 107 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 108 | #define GOOGLE_VID (0x18D1) |
| 109 | #define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51) |
| 110 | #define GOOGLE_RAIDEN_SPI_PROTOCOL (0x01) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 111 | |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 112 | enum raiden_debug_spi_request { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 113 | RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000, |
| 114 | RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001, |
| 115 | RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002, |
| 116 | RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003, |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 119 | #define PACKET_HEADER_SIZE (2) |
| 120 | #define MAX_PACKET_SIZE (64) |
| 121 | #define PAYLOAD_SIZE (MAX_PACKET_SIZE - PACKET_HEADER_SIZE) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 122 | |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame^] | 123 | #define WRITE_RETY_ATTEMPTS (3) |
| 124 | #define READ_RETY_ATTEMPTS (3) |
| 125 | #define RETY_INTERVAL_US (100 * 1000) |
| 126 | |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 127 | /* |
| 128 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 129 | */ |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 130 | #define TRANSFER_TIMEOUT_MS (1000) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 131 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 132 | struct usb_device *device = NULL; |
| 133 | uint8_t in_endpoint = 0; |
| 134 | uint8_t out_endpoint = 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 135 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 136 | typedef struct { |
| 137 | int8_t write_count; |
| 138 | /* -1 Indicates readback all on halfduplex compliant devices. */ |
| 139 | int8_t read_count; |
| 140 | uint8_t data[PAYLOAD_SIZE]; |
| 141 | } __attribute__((packed)) usb_spi_command_t; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 142 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 143 | typedef struct { |
| 144 | uint16_t status_code; |
| 145 | uint8_t data[PAYLOAD_SIZE]; |
| 146 | } __attribute__((packed)) usb_spi_response_t; |
| 147 | |
| 148 | static int write_command(const struct flashctx *flash, |
| 149 | unsigned int write_count, |
| 150 | unsigned int read_count, |
| 151 | const unsigned char *write_buffer, |
| 152 | unsigned char *read_buffer) |
| 153 | { |
| 154 | |
| 155 | int transferred; |
| 156 | int ret; |
| 157 | usb_spi_command_t command_packet; |
| 158 | |
| 159 | if (write_count > PAYLOAD_SIZE) { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 160 | msg_perr("Raiden: invalid write_count of %d\n", write_count); |
| 161 | return SPI_INVALID_LENGTH; |
| 162 | } |
| 163 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 164 | if (read_count > PAYLOAD_SIZE) { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 165 | msg_perr("Raiden: invalid read_count of %d\n", read_count); |
| 166 | return SPI_INVALID_LENGTH; |
| 167 | } |
| 168 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 169 | command_packet.write_count = write_count; |
| 170 | command_packet.read_count = read_count; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 171 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 172 | memcpy(command_packet.data, write_buffer, write_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 173 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 174 | ret = LIBUSB(libusb_bulk_transfer(device->handle, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 175 | out_endpoint, |
| 176 | (void*)&command_packet, |
| 177 | write_count + PACKET_HEADER_SIZE, |
| 178 | &transferred, |
| 179 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 180 | if (ret != 0) { |
| 181 | msg_perr("Raiden: OUT transfer failed\n" |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 182 | " write_count = %d\n" |
| 183 | " read_count = %d\n", |
| 184 | write_count, read_count); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 185 | return ret; |
| 186 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 187 | |
| 188 | if (transferred != write_count + PACKET_HEADER_SIZE) { |
| 189 | msg_perr("Raiden: Write failure (wrote %d, expected %d)\n", |
| 190 | transferred, write_count + PACKET_HEADER_SIZE); |
| 191 | return 0x10001; |
| 192 | } |
| 193 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int read_response(const struct flashctx *flash, |
| 198 | unsigned int write_count, |
| 199 | unsigned int read_count, |
| 200 | const unsigned char *write_buffer, |
| 201 | unsigned char *read_buffer) |
| 202 | { |
| 203 | |
| 204 | int transferred; |
| 205 | int ret; |
| 206 | usb_spi_response_t response_packet; |
| 207 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 208 | ret = LIBUSB(libusb_bulk_transfer(device->handle, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 209 | in_endpoint, |
| 210 | (void*)&response_packet, |
| 211 | read_count + PACKET_HEADER_SIZE, |
| 212 | &transferred, |
| 213 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 214 | if (ret != 0) { |
| 215 | msg_perr("Raiden: IN transfer failed\n" |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 216 | " write_count = %d\n" |
| 217 | " read_count = %d\n", |
| 218 | write_count, read_count); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 219 | return ret; |
| 220 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 221 | |
| 222 | if (transferred != read_count + PACKET_HEADER_SIZE) { |
| 223 | msg_perr("Raiden: Read failure (read %d, expected %d)\n", |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 224 | transferred, read_count + PACKET_HEADER_SIZE); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 225 | return 0x10002; |
| 226 | } |
| 227 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 228 | memcpy(read_buffer, response_packet.data, read_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 229 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 230 | return response_packet.status_code; |
| 231 | } |
| 232 | |
| 233 | static int send_command(const struct flashctx *flash, |
| 234 | unsigned int write_count, |
| 235 | unsigned int read_count, |
| 236 | const unsigned char *write_buffer, |
| 237 | unsigned char *read_buffer) |
| 238 | { |
| 239 | |
| 240 | int status = -1; |
| 241 | |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame^] | 242 | for (int write_attempt = 0; write_attempt < WRITE_RETY_ATTEMPTS; |
| 243 | write_attempt++) { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 244 | |
Brian J. Nemec | 1118a58 | 2020-02-04 18:26:02 -0800 | [diff] [blame^] | 245 | status = write_command(flash, write_count, read_count, |
| 246 | write_buffer, read_buffer); |
| 247 | |
| 248 | if (status) { |
| 249 | /* Write operation failed. */ |
| 250 | msg_perr("Raiden: Write command failed\n" |
| 251 | "Write attempt = %d\n" |
| 252 | "status = %d\n", |
| 253 | write_attempt + 1, status); |
| 254 | programmer_delay(RETY_INTERVAL_US); |
| 255 | continue; |
| 256 | } |
| 257 | for (int read_attempt = 0; read_attempt < READ_RETY_ATTEMPTS; |
| 258 | read_attempt++) { |
| 259 | |
| 260 | status = read_response(flash, write_count, read_count, |
| 261 | write_buffer, read_buffer); |
| 262 | |
| 263 | if (status != 0) { |
| 264 | /* Read operation failed. */ |
| 265 | msg_perr("Raiden: Read response failed\n" |
| 266 | "Write attempt = %d\n" |
| 267 | "Read attempt = %d\n" |
| 268 | "status = %d\n", |
| 269 | write_attempt + 1, read_attempt + 1, status); |
| 270 | programmer_delay(RETY_INTERVAL_US); |
| 271 | } else { |
| 272 | /* We were successful at performing the SPI transfer. */ |
| 273 | return status; |
| 274 | } |
| 275 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 276 | } |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 277 | return status; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 282 | * of bytes that your SPI device can read/write, these values are the maximum |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 283 | * data chunk size that flashrom will package up with an additional five bytes |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 284 | * of command for the flash device, resulting in a 62 byte packet, that we then |
| 285 | * add two bytes to in either direction, making our way up to the 64 byte |
| 286 | * maximum USB packet size for the device. |
| 287 | * |
| 288 | * The largest command that flashrom generates is the byte program command, so |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 289 | * we use that command header maximum size here. |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 290 | */ |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 291 | #define MAX_DATA_SIZE (PAYLOAD_SIZE - JEDEC_BYTE_PROGRAM_OUTSIZE) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 292 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 293 | static const struct spi_master spi_master_raiden_debug = { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 294 | .type = SPI_CONTROLLER_RAIDEN_DEBUG, |
| 295 | .features = SPI_MASTER_4BA, |
| 296 | .max_data_read = MAX_DATA_SIZE, |
| 297 | .max_data_write = MAX_DATA_SIZE, |
| 298 | .command = send_command, |
| 299 | .multicommand = default_spi_send_multicommand, |
| 300 | .read = default_spi_read, |
| 301 | .write_256 = default_spi_write_256, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 302 | }; |
| 303 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 304 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 305 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 306 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 307 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 308 | direction) && |
| 309 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 310 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 311 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 312 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 313 | static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, |
| 314 | uint8_t *out_ep) |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 315 | { |
| 316 | int i; |
| 317 | int in_count = 0; |
| 318 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 319 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 320 | for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 321 | struct libusb_endpoint_descriptor const *endpoint = |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 322 | &dev->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 323 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 324 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 325 | in_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 326 | *in_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 327 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 328 | out_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 329 | *out_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | if (in_count != 1 || out_count != 1) { |
| 334 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 335 | " found %d IN and %d OUT endpoints\n", |
| 336 | in_count, |
| 337 | out_count); |
| 338 | return 1; |
| 339 | } |
| 340 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 341 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep); |
| 342 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep); |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 343 | |
| 344 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 345 | } |
| 346 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 347 | static int shutdown(void * data) |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 348 | { |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 349 | int ret = LIBUSB(libusb_control_transfer( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 350 | device->handle, |
| 351 | LIBUSB_ENDPOINT_OUT | |
| 352 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 353 | LIBUSB_RECIPIENT_INTERFACE, |
| 354 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 355 | 0, |
| 356 | device->interface_descriptor->bInterfaceNumber, |
| 357 | NULL, |
| 358 | 0, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 359 | TRANSFER_TIMEOUT_MS)); |
| 360 | if (ret != 0) { |
| 361 | msg_perr("Raiden: Failed to disable SPI bridge\n"); |
| 362 | return ret; |
| 363 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 364 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 365 | usb_device_free(device); |
| 366 | |
| 367 | device = NULL; |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 368 | libusb_exit(NULL); |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 373 | static int get_target() |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 374 | { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 375 | int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 376 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 377 | char *target_str = extract_programmer_param("target"); |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 378 | if (target_str) { |
| 379 | if (!strcasecmp(target_str, "ap")) |
| 380 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP; |
| 381 | else if (!strcasecmp(target_str, "ec")) |
| 382 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC; |
| 383 | else { |
| 384 | msg_perr("Invalid target: %s\n", target_str); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 385 | request_enable = -1; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 386 | } |
| 387 | } |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 388 | free(target_str); |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 389 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 390 | return request_enable; |
| 391 | } |
| 392 | |
| 393 | int raiden_debug_spi_init(void) |
| 394 | { |
| 395 | struct usb_match match; |
| 396 | char *serial = extract_programmer_param("serial"); |
| 397 | struct usb_device *current; |
| 398 | int found = 0; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 399 | int ret; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 400 | |
| 401 | int request_enable = get_target(); |
| 402 | if (request_enable < 0) |
| 403 | return 1; |
| 404 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 405 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 406 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 407 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 408 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 409 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
| 410 | usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 411 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 412 | ret = LIBUSB(libusb_init(NULL)); |
| 413 | if (ret != 0) { |
| 414 | msg_perr("Raiden: libusb_init failed\n"); |
| 415 | return ret; |
| 416 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 417 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 418 | ret = usb_device_find(&match, ¤t); |
| 419 | if (ret != 0) { |
| 420 | msg_perr("Raiden: Failed to find devices\n"); |
| 421 | return ret; |
| 422 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 423 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 424 | while (current) { |
| 425 | device = current; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 426 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 427 | if (find_endpoints(device, &in_endpoint, &out_endpoint)) { |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 428 | msg_pdbg("Raiden: Failed to find valid endpoints on device"); |
| 429 | usb_device_show(" ", current); |
| 430 | goto loop_end; |
| 431 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 432 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 433 | if (usb_device_claim(device)) { |
| 434 | msg_pdbg("Raiden: Failed to claim USB device"); |
| 435 | usb_device_show(" ", current); |
| 436 | goto loop_end; |
| 437 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 438 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 439 | if (!serial) { |
| 440 | found = 1; |
| 441 | goto loop_end; |
| 442 | } else { |
| 443 | unsigned char dev_serial[32]; |
| 444 | struct libusb_device_descriptor descriptor; |
| 445 | int rc; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 446 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 447 | memset(dev_serial, 0, sizeof(dev_serial)); |
| 448 | |
| 449 | if (libusb_get_device_descriptor(device->device, &descriptor)) { |
| 450 | msg_pdbg("USB: Failed to get device descriptor.\n"); |
| 451 | goto loop_end; |
| 452 | } |
| 453 | |
| 454 | rc = libusb_get_string_descriptor_ascii(device->handle, |
| 455 | descriptor.iSerialNumber, |
| 456 | dev_serial, |
| 457 | sizeof(dev_serial)); |
| 458 | if (rc < 0) { |
| 459 | LIBUSB(rc); |
| 460 | } else { |
| 461 | if (strcmp(serial, (char *)dev_serial)) { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 462 | msg_pdbg("Raiden: Serial number %s did not match device", |
| 463 | serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 464 | usb_device_show(" ", current); |
| 465 | } else { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame] | 466 | msg_pinfo("Raiden: Serial number %s matched device", |
| 467 | serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 468 | usb_device_show(" ", current); |
| 469 | found = 1; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | loop_end: |
| 475 | if (found) |
| 476 | break; |
| 477 | else |
| 478 | current = usb_device_free(current); |
| 479 | } |
| 480 | |
| 481 | if (!device || !found) { |
| 482 | msg_perr("Raiden: No usable device found.\n"); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 483 | return 1; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 484 | } |
| 485 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 486 | /* free devices we don't care about */ |
| 487 | current = current->next; |
| 488 | while (current) |
| 489 | current = usb_device_free(current); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 490 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 491 | ret = LIBUSB(libusb_control_transfer( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 492 | device->handle, |
| 493 | LIBUSB_ENDPOINT_OUT | |
| 494 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 495 | LIBUSB_RECIPIENT_INTERFACE, |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 496 | request_enable, |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 497 | 0, |
| 498 | device->interface_descriptor->bInterfaceNumber, |
| 499 | NULL, |
| 500 | 0, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 501 | TRANSFER_TIMEOUT_MS)); |
| 502 | if (ret != 0) { |
| 503 | msg_perr("Raiden: Failed to enable SPI bridge\n"); |
| 504 | return ret; |
| 505 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 506 | |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 507 | /* |
| 508 | * Allow for power to settle on the AP and EC flash devices. |
| 509 | * Load switches can have a 1-3 ms turn on time, and SPI flash devices |
| 510 | * can require up to 10 ms from power on to the first write. |
| 511 | */ |
| 512 | if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) || |
| 513 | (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC)) |
| 514 | usleep(50 * 1000); |
| 515 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 516 | register_spi_master(&spi_master_raiden_debug); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 517 | register_shutdown(shutdown, NULL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 518 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 519 | return 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 520 | } |