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 | |
| 123 | /* |
| 124 | * This timeout is so large because the Raiden SPI timeout is 800ms. |
| 125 | */ |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 126 | #define TRANSFER_TIMEOUT_MS (1000) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 127 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 128 | struct usb_device *device = NULL; |
| 129 | uint8_t in_endpoint = 0; |
| 130 | uint8_t out_endpoint = 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 131 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 132 | typedef struct { |
| 133 | int8_t write_count; |
| 134 | /* -1 Indicates readback all on halfduplex compliant devices. */ |
| 135 | int8_t read_count; |
| 136 | uint8_t data[PAYLOAD_SIZE]; |
| 137 | } __attribute__((packed)) usb_spi_command_t; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 138 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 139 | typedef struct { |
| 140 | uint16_t status_code; |
| 141 | uint8_t data[PAYLOAD_SIZE]; |
| 142 | } __attribute__((packed)) usb_spi_response_t; |
| 143 | |
| 144 | static int write_command(const struct flashctx *flash, |
| 145 | unsigned int write_count, |
| 146 | unsigned int read_count, |
| 147 | const unsigned char *write_buffer, |
| 148 | unsigned char *read_buffer) |
| 149 | { |
| 150 | |
| 151 | int transferred; |
| 152 | int ret; |
| 153 | usb_spi_command_t command_packet; |
| 154 | |
| 155 | if (write_count > PAYLOAD_SIZE) { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 156 | msg_perr("Raiden: invalid write_count of %d\n", write_count); |
| 157 | return SPI_INVALID_LENGTH; |
| 158 | } |
| 159 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 160 | if (read_count > PAYLOAD_SIZE) { |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 161 | msg_perr("Raiden: invalid read_count of %d\n", read_count); |
| 162 | return SPI_INVALID_LENGTH; |
| 163 | } |
| 164 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 165 | command_packet.write_count = write_count; |
| 166 | command_packet.read_count = read_count; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 167 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 168 | memcpy(command_packet.data, write_buffer, write_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 169 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 170 | ret = LIBUSB(libusb_bulk_transfer(device->handle, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 171 | out_endpoint, |
| 172 | (void*)&command_packet, |
| 173 | write_count + PACKET_HEADER_SIZE, |
| 174 | &transferred, |
| 175 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 176 | if (ret != 0) { |
| 177 | msg_perr("Raiden: OUT transfer failed\n" |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 178 | " write_count = %d\n" |
| 179 | " read_count = %d\n", |
| 180 | write_count, read_count); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 181 | return ret; |
| 182 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 183 | |
| 184 | if (transferred != write_count + PACKET_HEADER_SIZE) { |
| 185 | msg_perr("Raiden: Write failure (wrote %d, expected %d)\n", |
| 186 | transferred, write_count + PACKET_HEADER_SIZE); |
| 187 | return 0x10001; |
| 188 | } |
| 189 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static int read_response(const struct flashctx *flash, |
| 194 | unsigned int write_count, |
| 195 | unsigned int read_count, |
| 196 | const unsigned char *write_buffer, |
| 197 | unsigned char *read_buffer) |
| 198 | { |
| 199 | |
| 200 | int transferred; |
| 201 | int ret; |
| 202 | usb_spi_response_t response_packet; |
| 203 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 204 | ret = LIBUSB(libusb_bulk_transfer(device->handle, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 205 | in_endpoint, |
| 206 | (void*)&response_packet, |
| 207 | read_count + PACKET_HEADER_SIZE, |
| 208 | &transferred, |
| 209 | TRANSFER_TIMEOUT_MS)); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 210 | if (ret != 0) { |
| 211 | msg_perr("Raiden: IN transfer failed\n" |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 212 | " write_count = %d\n" |
| 213 | " read_count = %d\n", |
| 214 | write_count, read_count); |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 215 | return ret; |
| 216 | } |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 217 | |
| 218 | if (transferred != read_count + PACKET_HEADER_SIZE) { |
| 219 | msg_perr("Raiden: Read failure (read %d, expected %d)\n", |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 220 | transferred, read_count + PACKET_HEADER_SIZE); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 221 | return 0x10002; |
| 222 | } |
| 223 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 224 | memcpy(read_buffer, response_packet.data, read_count); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 225 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 226 | return response_packet.status_code; |
| 227 | } |
| 228 | |
| 229 | static int send_command(const struct flashctx *flash, |
| 230 | unsigned int write_count, |
| 231 | unsigned int read_count, |
| 232 | const unsigned char *write_buffer, |
| 233 | unsigned char *read_buffer) |
| 234 | { |
| 235 | |
| 236 | int status = -1; |
| 237 | |
| 238 | status = write_command(flash, write_count, read_count, |
| 239 | write_buffer, read_buffer); |
| 240 | |
| 241 | if (status) { |
| 242 | return status; |
| 243 | } |
| 244 | |
| 245 | status = read_response(flash, write_count, read_count, |
| 246 | write_buffer, read_buffer); |
| 247 | |
| 248 | return status; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | /* |
| 252 | * Unfortunately there doesn't seem to be a way to specify the maximum number |
| 253 | * 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] | 254 | * 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] | 255 | * of command for the flash device, resulting in a 62 byte packet, that we then |
| 256 | * add two bytes to in either direction, making our way up to the 64 byte |
| 257 | * maximum USB packet size for the device. |
| 258 | * |
| 259 | * The largest command that flashrom generates is the byte program command, so |
Duncan Laurie | 537fd1d | 2018-10-05 10:53:20 -0700 | [diff] [blame] | 260 | * we use that command header maximum size here. |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 261 | */ |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 262 | #define MAX_DATA_SIZE (PAYLOAD_SIZE - JEDEC_BYTE_PROGRAM_OUTSIZE) |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 263 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 264 | static const struct spi_master spi_master_raiden_debug = { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 265 | .type = SPI_CONTROLLER_RAIDEN_DEBUG, |
| 266 | .features = SPI_MASTER_4BA, |
| 267 | .max_data_read = MAX_DATA_SIZE, |
| 268 | .max_data_write = MAX_DATA_SIZE, |
| 269 | .command = send_command, |
| 270 | .multicommand = default_spi_send_multicommand, |
| 271 | .read = default_spi_read, |
| 272 | .write_256 = default_spi_write_256, |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 273 | }; |
| 274 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 275 | static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor, |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 276 | enum libusb_endpoint_direction direction) |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 277 | { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 278 | return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == |
| 279 | direction) && |
| 280 | ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == |
| 281 | LIBUSB_TRANSFER_TYPE_BULK)); |
| 282 | } |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 283 | |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 284 | static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, |
| 285 | uint8_t *out_ep) |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 286 | { |
| 287 | int i; |
| 288 | int in_count = 0; |
| 289 | int out_count = 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 290 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 291 | for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) { |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 292 | struct libusb_endpoint_descriptor const *endpoint = |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 293 | &dev->interface_descriptor->endpoint[i]; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 294 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 295 | if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) { |
| 296 | in_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 297 | *in_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 298 | } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) { |
| 299 | out_count++; |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 300 | *out_ep = endpoint->bEndpointAddress; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | if (in_count != 1 || out_count != 1) { |
| 305 | msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n" |
| 306 | " found %d IN and %d OUT endpoints\n", |
| 307 | in_count, |
| 308 | out_count); |
| 309 | return 1; |
| 310 | } |
| 311 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 312 | msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep); |
| 313 | msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep); |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 314 | |
| 315 | return 0; |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 316 | } |
| 317 | |
David Hendricks | 93784b4 | 2016-08-09 17:00:38 -0700 | [diff] [blame] | 318 | static int shutdown(void * data) |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 319 | { |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 320 | int ret = LIBUSB(libusb_control_transfer( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 321 | device->handle, |
| 322 | LIBUSB_ENDPOINT_OUT | |
| 323 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 324 | LIBUSB_RECIPIENT_INTERFACE, |
| 325 | RAIDEN_DEBUG_SPI_REQ_DISABLE, |
| 326 | 0, |
| 327 | device->interface_descriptor->bInterfaceNumber, |
| 328 | NULL, |
| 329 | 0, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 330 | TRANSFER_TIMEOUT_MS)); |
| 331 | if (ret != 0) { |
| 332 | msg_perr("Raiden: Failed to disable SPI bridge\n"); |
| 333 | return ret; |
| 334 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 335 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 336 | usb_device_free(device); |
| 337 | |
| 338 | device = NULL; |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 339 | libusb_exit(NULL); |
| 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 344 | static int get_target() |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 345 | { |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 346 | int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 347 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 348 | char *target_str = extract_programmer_param("target"); |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 349 | if (target_str) { |
| 350 | if (!strcasecmp(target_str, "ap")) |
| 351 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP; |
| 352 | else if (!strcasecmp(target_str, "ec")) |
| 353 | request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC; |
| 354 | else { |
| 355 | msg_perr("Invalid target: %s\n", target_str); |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 356 | request_enable = -1; |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 357 | } |
| 358 | } |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 359 | free(target_str); |
Anton Staaf | d27536d | 2014-09-30 08:10:17 -0700 | [diff] [blame] | 360 | |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 361 | return request_enable; |
| 362 | } |
| 363 | |
| 364 | int raiden_debug_spi_init(void) |
| 365 | { |
| 366 | struct usb_match match; |
| 367 | char *serial = extract_programmer_param("serial"); |
| 368 | struct usb_device *current; |
| 369 | int found = 0; |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 370 | int ret; |
Edward O'Callaghan | 8d2f648 | 2019-11-19 15:42:44 +1100 | [diff] [blame] | 371 | |
| 372 | int request_enable = get_target(); |
| 373 | if (request_enable < 0) |
| 374 | return 1; |
| 375 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 376 | usb_match_init(&match); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 377 | |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 378 | usb_match_value_default(&match.vid, GOOGLE_VID); |
| 379 | usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC); |
| 380 | usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS); |
| 381 | usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 382 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 383 | ret = LIBUSB(libusb_init(NULL)); |
| 384 | if (ret != 0) { |
| 385 | msg_perr("Raiden: libusb_init failed\n"); |
| 386 | return ret; |
| 387 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 388 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 389 | ret = usb_device_find(&match, ¤t); |
| 390 | if (ret != 0) { |
| 391 | msg_perr("Raiden: Failed to find devices\n"); |
| 392 | return ret; |
| 393 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 394 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 395 | while (current) { |
| 396 | device = current; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 397 | |
Edward O'Callaghan | dabf7e8 | 2019-11-19 15:11:18 +1100 | [diff] [blame] | 398 | if (find_endpoints(device, &in_endpoint, &out_endpoint)) { |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 399 | msg_pdbg("Raiden: Failed to find valid endpoints on device"); |
| 400 | usb_device_show(" ", current); |
| 401 | goto loop_end; |
| 402 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 403 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 404 | if (usb_device_claim(device)) { |
| 405 | msg_pdbg("Raiden: Failed to claim USB device"); |
| 406 | usb_device_show(" ", current); |
| 407 | goto loop_end; |
| 408 | } |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 409 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 410 | if (!serial) { |
| 411 | found = 1; |
| 412 | goto loop_end; |
| 413 | } else { |
| 414 | unsigned char dev_serial[32]; |
| 415 | struct libusb_device_descriptor descriptor; |
| 416 | int rc; |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 417 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 418 | memset(dev_serial, 0, sizeof(dev_serial)); |
| 419 | |
| 420 | if (libusb_get_device_descriptor(device->device, &descriptor)) { |
| 421 | msg_pdbg("USB: Failed to get device descriptor.\n"); |
| 422 | goto loop_end; |
| 423 | } |
| 424 | |
| 425 | rc = libusb_get_string_descriptor_ascii(device->handle, |
| 426 | descriptor.iSerialNumber, |
| 427 | dev_serial, |
| 428 | sizeof(dev_serial)); |
| 429 | if (rc < 0) { |
| 430 | LIBUSB(rc); |
| 431 | } else { |
| 432 | if (strcmp(serial, (char *)dev_serial)) { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 433 | msg_pdbg("Raiden: Serial number %s did not match device", |
| 434 | serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 435 | usb_device_show(" ", current); |
| 436 | } else { |
Brian J. Nemec | da496dc | 2020-02-04 11:13:05 -0800 | [diff] [blame^] | 437 | msg_pinfo("Raiden: Serial number %s matched device", |
| 438 | serial); |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 439 | usb_device_show(" ", current); |
| 440 | found = 1; |
| 441 | } |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | loop_end: |
| 446 | if (found) |
| 447 | break; |
| 448 | else |
| 449 | current = usb_device_free(current); |
| 450 | } |
| 451 | |
| 452 | if (!device || !found) { |
| 453 | msg_perr("Raiden: No usable device found.\n"); |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 454 | return 1; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 455 | } |
| 456 | |
David Hendricks | 5c79a49 | 2016-06-14 20:56:36 -0700 | [diff] [blame] | 457 | /* free devices we don't care about */ |
| 458 | current = current->next; |
| 459 | while (current) |
| 460 | current = usb_device_free(current); |
Anton Staaf | b4661ee | 2014-10-21 11:24:36 -0700 | [diff] [blame] | 461 | |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 462 | ret = LIBUSB(libusb_control_transfer( |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 463 | device->handle, |
| 464 | LIBUSB_ENDPOINT_OUT | |
| 465 | LIBUSB_REQUEST_TYPE_VENDOR | |
| 466 | LIBUSB_RECIPIENT_INTERFACE, |
Mary Ruthven | eafafd8 | 2016-05-03 14:33:53 -0700 | [diff] [blame] | 467 | request_enable, |
Anton Staaf | 5614e25 | 2015-03-24 14:33:33 -0700 | [diff] [blame] | 468 | 0, |
| 469 | device->interface_descriptor->bInterfaceNumber, |
| 470 | NULL, |
| 471 | 0, |
Edward O'Callaghan | 5c81bf3 | 2020-01-29 14:26:55 +1100 | [diff] [blame] | 472 | TRANSFER_TIMEOUT_MS)); |
| 473 | if (ret != 0) { |
| 474 | msg_perr("Raiden: Failed to enable SPI bridge\n"); |
| 475 | return ret; |
| 476 | } |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 477 | |
Keith Short | 8453b55 | 2020-02-03 18:10:14 -0700 | [diff] [blame] | 478 | /* |
| 479 | * Allow for power to settle on the AP and EC flash devices. |
| 480 | * Load switches can have a 1-3 ms turn on time, and SPI flash devices |
| 481 | * can require up to 10 ms from power on to the first write. |
| 482 | */ |
| 483 | if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) || |
| 484 | (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC)) |
| 485 | usleep(50 * 1000); |
| 486 | |
Patrick Georgi | f4f1e2f | 2017-03-10 17:38:40 +0100 | [diff] [blame] | 487 | register_spi_master(&spi_master_raiden_debug); |
Anton Staaf | 4589cd1 | 2015-03-23 13:36:44 -0700 | [diff] [blame] | 488 | register_shutdown(shutdown, NULL); |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 489 | |
David Hendricks | 98b3c57 | 2016-11-30 01:50:08 +0000 | [diff] [blame] | 490 | return 0; |
Anton Staaf | b264788 | 2014-09-17 15:13:43 -0700 | [diff] [blame] | 491 | } |