blob: c081e9ba2013fecfb8868a98e6032439428c1b14 [file] [log] [blame]
Anton Staafb2647882014-09-17 15:13:43 -07001/*
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. Nemecda496dc2020-02-04 11:13:05 -080049 *
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.
Brian J. Nemeccea6bec2020-02-25 14:12:46 -080096 *
97 * USB Error Codes:
98 *
99 * send_command return codes have the following format:
100 *
101 * 0x00000: Status code success.
102 * 0x00001-0x0FFFF: Error code returned by the USB SPI device.
103 * 0x10001-0x1FFFF: The host has determined an error has occurred.
104 * 0x20001-0x20063 Lower bits store the positive value representation
105 * of the libusb_error enum. See the libusb documentation:
106 * http://libusb.sourceforge.net/api-1.0/group__misc.html
Anton Staafb2647882014-09-17 15:13:43 -0700107 */
108
Anton Staafb2647882014-09-17 15:13:43 -0700109#include "programmer.h"
110#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -0700111#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -0700112
113#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700114#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -0700115#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700116#include <string.h>
Keith Short8453b552020-02-03 18:10:14 -0700117#include <unistd.h>
Anton Staafb2647882014-09-17 15:13:43 -0700118
Brian J. Nemecb42d6c12020-07-23 03:07:38 -0700119/* FIXME: Add some programmer IDs here */
120const struct dev_entry devs_raiden[] = {
121 {0},
122};
123
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800124#define GOOGLE_VID (0x18D1)
125#define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51)
Brian J. Nemecb746f822020-07-22 02:57:56 -0700126
127enum {
128 GOOGLE_RAIDEN_SPI_PROTOCOL_V1 = 0x01,
129 GOOGLE_RAIDEN_SPI_PROTOCOL_V2 = 0x02,
130};
Anton Staafb2647882014-09-17 15:13:43 -0700131
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800132enum usb_spi_error {
133 USB_SPI_SUCCESS = 0x0000,
134 USB_SPI_TIMEOUT = 0x0001,
135 USB_SPI_BUSY = 0x0002,
136 USB_SPI_WRITE_COUNT_INVALID = 0x0003,
137 USB_SPI_READ_COUNT_INVALID = 0x0004,
138 USB_SPI_DISABLED = 0x0005,
139 USB_SPI_UNKNOWN_ERROR = 0x8000,
140};
141
Anton Staaf4589cd12015-03-23 13:36:44 -0700142enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700143 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
144 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
145 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
146 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -0700147};
148
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800149#define PACKET_HEADER_SIZE (2)
150#define MAX_PACKET_SIZE (64)
Brian J. Nemecb746f822020-07-22 02:57:56 -0700151#define PAYLOAD_SIZE_V1 (MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700152
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800153/*
154 * Servo Micro has an error where it is capable of acknowledging USB packets
155 * without loading it into the USB endpoint buffers or triggering interrupts.
156 * See crbug.com/952494. Retry mechanisms have been implemented to recover
157 * from these rare failures allowing the process to continue.
158 */
Brian J. Nemec1118a582020-02-04 18:26:02 -0800159#define WRITE_RETY_ATTEMPTS (3)
160#define READ_RETY_ATTEMPTS (3)
161#define RETY_INTERVAL_US (100 * 1000)
162
Anton Staafb2647882014-09-17 15:13:43 -0700163/*
164 * This timeout is so large because the Raiden SPI timeout is 800ms.
165 */
Edward O'Callaghanf1e6ef52020-03-03 13:57:15 +1100166#define TRANSFER_TIMEOUT_MS (200 + 800)
Anton Staafb2647882014-09-17 15:13:43 -0700167
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700168struct raiden_debug_spi_data {
169 struct usb_device *dev;
170 uint8_t in_ep;
171 uint8_t out_ep;
172};
Anton Staafb2647882014-09-17 15:13:43 -0700173
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800174typedef struct {
175 int8_t write_count;
176 /* -1 Indicates readback all on halfduplex compliant devices. */
177 int8_t read_count;
Brian J. Nemecb746f822020-07-22 02:57:56 -0700178 uint8_t data[PAYLOAD_SIZE_V1];
179} __attribute__((packed)) usb_spi_command_v1_t;
Anton Staafb2647882014-09-17 15:13:43 -0700180
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800181typedef struct {
182 uint16_t status_code;
Brian J. Nemecb746f822020-07-22 02:57:56 -0700183 uint8_t data[PAYLOAD_SIZE_V1];
184} __attribute__((packed)) usb_spi_response_v1_t;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800185
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800186/*
187 * This function will return true when an error code can potentially recover
188 * if we attempt to write SPI data to the device or read from it. We know
189 * that some conditions are not recoverable in the current state so allows us
190 * to bypass the retry logic and terminate early.
191 */
192static bool retry_recovery(int error_code)
193{
194 if (error_code < 0x10000) {
Brian J. Nemec1a61f722020-05-04 20:58:06 -0700195 /*
196 * Handle error codes returned from the device. USB_SPI_TIMEOUT,
197 * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed
198 * during transfer errors to the device and can be recovered.
199 */
200 if (USB_SPI_READ_COUNT_INVALID <= error_code &&
201 error_code <= USB_SPI_DISABLED) {
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800202 return false;
203 }
204 } else if (usb_device_is_libusb_error(error_code)) {
205 /* Handle error codes returned from libusb. */
206 if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) {
207 return false;
208 }
209 }
210 return true;
211}
212
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700213static const struct raiden_debug_spi_data *
214 get_raiden_data_from_context(const struct flashctx *flash)
215{
216 return (const struct raiden_debug_spi_data *)flash->mst->spi.data;
217}
218
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800219static int write_command(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700220 unsigned int write_count,
221 unsigned int read_count,
222 const unsigned char *write_buffer,
223 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800224{
225
226 int transferred;
227 int ret;
Brian J. Nemecb746f822020-07-22 02:57:56 -0700228 usb_spi_command_v1_t command_packet;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700229 const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800230
Brian J. Nemecb746f822020-07-22 02:57:56 -0700231 if (write_count > PAYLOAD_SIZE_V1) {
Edward O'Callaghan8eb829a2020-03-03 13:55:08 +1100232 msg_perr("Raiden: Invalid write_count of %d\n", write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700233 return SPI_INVALID_LENGTH;
234 }
235
Brian J. Nemecb746f822020-07-22 02:57:56 -0700236 if (read_count > PAYLOAD_SIZE_V1) {
Edward O'Callaghan8eb829a2020-03-03 13:55:08 +1100237 msg_perr("Raiden: Invalid read_count of %d\n", read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700238 return SPI_INVALID_LENGTH;
239 }
240
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800241 command_packet.write_count = write_count;
242 command_packet.read_count = read_count;
Anton Staafb2647882014-09-17 15:13:43 -0700243
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800244 memcpy(command_packet.data, write_buffer, write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700245
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700246 ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
247 ctx_data->out_ep,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700248 (void*)&command_packet,
249 write_count + PACKET_HEADER_SIZE,
250 &transferred,
251 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100252 if (ret != 0) {
253 msg_perr("Raiden: OUT transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800254 " write_count = %d\n"
255 " read_count = %d\n",
256 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100257 return ret;
258 }
Anton Staafb2647882014-09-17 15:13:43 -0700259
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700260 if ((unsigned) transferred != write_count + PACKET_HEADER_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700261 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
262 transferred, write_count + PACKET_HEADER_SIZE);
263 return 0x10001;
264 }
265
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800266 return 0;
267}
268
269static int read_response(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700270 unsigned int write_count,
271 unsigned int read_count,
272 const unsigned char *write_buffer,
273 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800274{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800275 int transferred;
276 int ret;
Brian J. Nemecb746f822020-07-22 02:57:56 -0700277 usb_spi_response_v1_t response_packet;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700278 const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800279
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700280 ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
281 ctx_data->in_ep,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700282 (void*)&response_packet,
283 read_count + PACKET_HEADER_SIZE,
284 &transferred,
285 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100286 if (ret != 0) {
287 msg_perr("Raiden: IN transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800288 " write_count = %d\n"
289 " read_count = %d\n",
290 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100291 return ret;
292 }
Anton Staafb2647882014-09-17 15:13:43 -0700293
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700294 if ((unsigned) transferred != read_count + PACKET_HEADER_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700295 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700296 transferred, read_count + PACKET_HEADER_SIZE);
Anton Staafb2647882014-09-17 15:13:43 -0700297 return 0x10002;
298 }
299
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800300 memcpy(read_buffer, response_packet.data, read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700301
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800302 return response_packet.status_code;
303}
304
305static int send_command(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700306 unsigned int write_count,
307 unsigned int read_count,
308 const unsigned char *write_buffer,
309 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800310{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800311 int status = -1;
312
Brian J. Nemec1118a582020-02-04 18:26:02 -0800313 for (int write_attempt = 0; write_attempt < WRITE_RETY_ATTEMPTS;
314 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800315
Brian J. Nemec1118a582020-02-04 18:26:02 -0800316 status = write_command(flash, write_count, read_count,
317 write_buffer, read_buffer);
318
319 if (status) {
320 /* Write operation failed. */
321 msg_perr("Raiden: Write command failed\n"
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700322 "Write attempt = %d\n"
323 "status = %d\n",
324 write_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800325 if (!retry_recovery(status)) {
326 /* Reattempting will not result in a recovery. */
327 return status;
328 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800329 programmer_delay(RETY_INTERVAL_US);
330 continue;
331 }
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700332 for (int read_attempt = 0; read_attempt < READ_RETY_ATTEMPTS; read_attempt++) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800333
334 status = read_response(flash, write_count, read_count,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700335 write_buffer, read_buffer);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800336
337 if (status != 0) {
338 /* Read operation failed. */
339 msg_perr("Raiden: Read response failed\n"
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700340 "Write attempt = %d\n"
341 "Read attempt = %d\n"
342 "status = %d\n",
343 write_attempt + 1, read_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800344 if (!retry_recovery(status)) {
345 /* Reattempting will not result in a recovery. */
346 return status;
347 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800348 programmer_delay(RETY_INTERVAL_US);
349 } else {
350 /* We were successful at performing the SPI transfer. */
351 return status;
352 }
353 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800354 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800355 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700356}
357
358/*
359 * Unfortunately there doesn't seem to be a way to specify the maximum number
360 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700361 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700362 * of command for the flash device, resulting in a 62 byte packet, that we then
363 * add two bytes to in either direction, making our way up to the 64 byte
364 * maximum USB packet size for the device.
365 *
366 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700367 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700368 */
Brian J. Nemecb746f822020-07-22 02:57:56 -0700369#define MAX_DATA_SIZE (PAYLOAD_SIZE_V1 - JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700370
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700371static struct spi_master spi_master_raiden_debug = {
Brian J. Nemecb746f822020-07-22 02:57:56 -0700372 .features = SPI_MASTER_4BA,
373 .max_data_read = MAX_DATA_SIZE,
374 .max_data_write = MAX_DATA_SIZE,
375 .command = send_command,
376 .multicommand = default_spi_send_multicommand,
377 .read = default_spi_read,
378 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700379};
380
Anton Staaf5614e252015-03-24 14:33:33 -0700381static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800382 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700383{
Anton Staaf5614e252015-03-24 14:33:33 -0700384 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
385 direction) &&
386 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
387 LIBUSB_TRANSFER_TYPE_BULK));
388}
Anton Staafd27536d2014-09-30 08:10:17 -0700389
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700390static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700391{
392 int i;
393 int in_count = 0;
394 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700395
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100396 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700397 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100398 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700399
Anton Staaf5614e252015-03-24 14:33:33 -0700400 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
401 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100402 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700403 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
404 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100405 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700406 }
407 }
408
409 if (in_count != 1 || out_count != 1) {
410 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
411 " found %d IN and %d OUT endpoints\n",
412 in_count,
413 out_count);
414 return 1;
415 }
416
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100417 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
418 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700419
420 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700421}
422
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700423static int raiden_debug_spi_shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700424{
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700425 struct raiden_debug_spi_data * ctx_data =
426 (struct raiden_debug_spi_data *)data;
427
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100428 int ret = LIBUSB(libusb_control_transfer(
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700429 ctx_data->dev->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700430 LIBUSB_ENDPOINT_OUT |
431 LIBUSB_REQUEST_TYPE_VENDOR |
432 LIBUSB_RECIPIENT_INTERFACE,
433 RAIDEN_DEBUG_SPI_REQ_DISABLE,
434 0,
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700435 ctx_data->dev->interface_descriptor->bInterfaceNumber,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700436 NULL,
437 0,
438 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100439 if (ret != 0) {
440 msg_perr("Raiden: Failed to disable SPI bridge\n");
441 return ret;
442 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700443
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700444 usb_device_free(ctx_data->dev);
Anton Staaf4589cd12015-03-23 13:36:44 -0700445 libusb_exit(NULL);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700446 free(ctx_data);
Anton Staaf4589cd12015-03-23 13:36:44 -0700447
448 return 0;
449}
450
Edward O'Callaghandda1e542020-02-03 12:45:01 +1100451static int get_target(void)
Anton Staafb2647882014-09-17 15:13:43 -0700452{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700453 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700454
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100455 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700456 if (target_str) {
457 if (!strcasecmp(target_str, "ap"))
458 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
459 else if (!strcasecmp(target_str, "ec"))
460 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
461 else {
462 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100463 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700464 }
465 }
David Hendricks98b3c572016-11-30 01:50:08 +0000466 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700467
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100468 return request_enable;
469}
470
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700471static void free_dev_list(struct usb_device **dev_lst)
472{
473 struct usb_device *dev = *dev_lst;
474 /* free devices we don't care about */
475 dev = dev->next;
476 while (dev)
477 dev = usb_device_free(dev);
478}
479
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100480int raiden_debug_spi_init(void)
481{
482 struct usb_match match;
483 char *serial = extract_programmer_param("serial");
484 struct usb_device *current;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700485 struct usb_device *device = NULL;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100486 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100487 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100488
489 int request_enable = get_target();
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700490 if (request_enable < 0) {
491 free(serial);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100492 return 1;
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700493 }
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100494
Anton Staaf5614e252015-03-24 14:33:33 -0700495 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700496
Anton Staaf5614e252015-03-24 14:33:33 -0700497 usb_match_value_default(&match.vid, GOOGLE_VID);
498 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
499 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
Brian J. Nemecb746f822020-07-22 02:57:56 -0700500 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL_V1);
Anton Staafb2647882014-09-17 15:13:43 -0700501
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100502 ret = LIBUSB(libusb_init(NULL));
503 if (ret != 0) {
504 msg_perr("Raiden: libusb_init failed\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700505 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100506 return ret;
507 }
Anton Staaf5614e252015-03-24 14:33:33 -0700508
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100509 ret = usb_device_find(&match, &current);
510 if (ret != 0) {
511 msg_perr("Raiden: Failed to find devices\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700512 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100513 return ret;
514 }
Anton Staaf5614e252015-03-24 14:33:33 -0700515
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700516 uint8_t in_endpoint = 0;
517 uint8_t out_endpoint = 0;
David Hendricks5c79a492016-06-14 20:56:36 -0700518 while (current) {
519 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700520
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100521 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700522 msg_pdbg("Raiden: Failed to find valid endpoints on device");
523 usb_device_show(" ", current);
524 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700525 }
Anton Staaf5614e252015-03-24 14:33:33 -0700526
David Hendricks5c79a492016-06-14 20:56:36 -0700527 if (usb_device_claim(device)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700528 msg_pdbg("Raiden: Failed to claim USB device");
529 usb_device_show(" ", current);
530 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700531 }
Anton Staaf5614e252015-03-24 14:33:33 -0700532
David Hendricks5c79a492016-06-14 20:56:36 -0700533 if (!serial) {
534 found = 1;
535 goto loop_end;
536 } else {
537 unsigned char dev_serial[32];
538 struct libusb_device_descriptor descriptor;
539 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700540
David Hendricks5c79a492016-06-14 20:56:36 -0700541 memset(dev_serial, 0, sizeof(dev_serial));
542
543 if (libusb_get_device_descriptor(device->device, &descriptor)) {
544 msg_pdbg("USB: Failed to get device descriptor.\n");
545 goto loop_end;
546 }
547
548 rc = libusb_get_string_descriptor_ascii(device->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700549 descriptor.iSerialNumber,
550 dev_serial,
551 sizeof(dev_serial));
David Hendricks5c79a492016-06-14 20:56:36 -0700552 if (rc < 0) {
553 LIBUSB(rc);
554 } else {
555 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700556 msg_pdbg("Raiden: Serial number %s did not match device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700557 usb_device_show(" ", current);
558 } else {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700559 msg_pinfo("Raiden: Serial number %s matched device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700560 usb_device_show(" ", current);
561 found = 1;
562 }
563 }
564 }
565
566loop_end:
567 if (found)
568 break;
569 else
570 current = usb_device_free(current);
571 }
572
573 if (!device || !found) {
574 msg_perr("Raiden: No usable device found.\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700575 free(serial);
David Hendricks98b3c572016-11-30 01:50:08 +0000576 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700577 }
578
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700579 free_dev_list(&current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700580
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100581 ret = LIBUSB(libusb_control_transfer(
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700582 device->handle,
583 LIBUSB_ENDPOINT_OUT |
584 LIBUSB_REQUEST_TYPE_VENDOR |
585 LIBUSB_RECIPIENT_INTERFACE,
586 request_enable,
587 0,
588 device->interface_descriptor->bInterfaceNumber,
589 NULL,
590 0,
591 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100592 if (ret != 0) {
593 msg_perr("Raiden: Failed to enable SPI bridge\n");
594 return ret;
595 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700596
Keith Short8453b552020-02-03 18:10:14 -0700597 /*
598 * Allow for power to settle on the AP and EC flash devices.
599 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
600 * can require up to 10 ms from power on to the first write.
601 */
602 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
603 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
604 usleep(50 * 1000);
605
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700606 struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data));
607 if (!data) {
608 msg_perr("Unable to allocate space for extra SPI master data.\n");
609 return SPI_GENERIC_ERROR;
610 }
611
612 data->dev = device;
613 data->in_ep = in_endpoint;
614 data->out_ep = out_endpoint;
615
616 spi_master_raiden_debug.data = data;
617
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100618 register_spi_master(&spi_master_raiden_debug);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700619 register_shutdown(raiden_debug_spi_shutdown, data);
Anton Staafb2647882014-09-17 15:13:43 -0700620
David Hendricks98b3c572016-11-30 01:50:08 +0000621 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700622}