blob: 370e1a3a48c46ffb6bda0e7c4befab926e8d2da1 [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)
126#define GOOGLE_RAIDEN_SPI_PROTOCOL (0x01)
Anton Staafb2647882014-09-17 15:13:43 -0700127
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800128enum usb_spi_error {
129 USB_SPI_SUCCESS = 0x0000,
130 USB_SPI_TIMEOUT = 0x0001,
131 USB_SPI_BUSY = 0x0002,
132 USB_SPI_WRITE_COUNT_INVALID = 0x0003,
133 USB_SPI_READ_COUNT_INVALID = 0x0004,
134 USB_SPI_DISABLED = 0x0005,
135 USB_SPI_UNKNOWN_ERROR = 0x8000,
136};
137
Anton Staaf4589cd12015-03-23 13:36:44 -0700138enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700139 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
140 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
141 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
142 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -0700143};
144
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800145#define PACKET_HEADER_SIZE (2)
146#define MAX_PACKET_SIZE (64)
147#define PAYLOAD_SIZE (MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700148
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800149/*
150 * Servo Micro has an error where it is capable of acknowledging USB packets
151 * without loading it into the USB endpoint buffers or triggering interrupts.
152 * See crbug.com/952494. Retry mechanisms have been implemented to recover
153 * from these rare failures allowing the process to continue.
154 */
Brian J. Nemec1118a582020-02-04 18:26:02 -0800155#define WRITE_RETY_ATTEMPTS (3)
156#define READ_RETY_ATTEMPTS (3)
157#define RETY_INTERVAL_US (100 * 1000)
158
Anton Staafb2647882014-09-17 15:13:43 -0700159/*
160 * This timeout is so large because the Raiden SPI timeout is 800ms.
161 */
Edward O'Callaghanf1e6ef52020-03-03 13:57:15 +1100162#define TRANSFER_TIMEOUT_MS (200 + 800)
Anton Staafb2647882014-09-17 15:13:43 -0700163
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700164struct raiden_debug_spi_data {
165 struct usb_device *dev;
166 uint8_t in_ep;
167 uint8_t out_ep;
168};
Anton Staafb2647882014-09-17 15:13:43 -0700169
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800170typedef struct {
171 int8_t write_count;
172 /* -1 Indicates readback all on halfduplex compliant devices. */
173 int8_t read_count;
174 uint8_t data[PAYLOAD_SIZE];
175} __attribute__((packed)) usb_spi_command_t;
Anton Staafb2647882014-09-17 15:13:43 -0700176
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800177typedef struct {
178 uint16_t status_code;
179 uint8_t data[PAYLOAD_SIZE];
180} __attribute__((packed)) usb_spi_response_t;
181
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800182/*
183 * This function will return true when an error code can potentially recover
184 * if we attempt to write SPI data to the device or read from it. We know
185 * that some conditions are not recoverable in the current state so allows us
186 * to bypass the retry logic and terminate early.
187 */
188static bool retry_recovery(int error_code)
189{
190 if (error_code < 0x10000) {
Brian J. Nemec1a61f722020-05-04 20:58:06 -0700191 /*
192 * Handle error codes returned from the device. USB_SPI_TIMEOUT,
193 * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed
194 * during transfer errors to the device and can be recovered.
195 */
196 if (USB_SPI_READ_COUNT_INVALID <= error_code &&
197 error_code <= USB_SPI_DISABLED) {
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800198 return false;
199 }
200 } else if (usb_device_is_libusb_error(error_code)) {
201 /* Handle error codes returned from libusb. */
202 if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) {
203 return false;
204 }
205 }
206 return true;
207}
208
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700209static const struct raiden_debug_spi_data *
210 get_raiden_data_from_context(const struct flashctx *flash)
211{
212 return (const struct raiden_debug_spi_data *)flash->mst->spi.data;
213}
214
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800215static int write_command(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700216 unsigned int write_count,
217 unsigned int read_count,
218 const unsigned char *write_buffer,
219 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800220{
221
222 int transferred;
223 int ret;
224 usb_spi_command_t command_packet;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700225 const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800226
227 if (write_count > PAYLOAD_SIZE) {
Edward O'Callaghan8eb829a2020-03-03 13:55:08 +1100228 msg_perr("Raiden: Invalid write_count of %d\n", write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700229 return SPI_INVALID_LENGTH;
230 }
231
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800232 if (read_count > PAYLOAD_SIZE) {
Edward O'Callaghan8eb829a2020-03-03 13:55:08 +1100233 msg_perr("Raiden: Invalid read_count of %d\n", read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700234 return SPI_INVALID_LENGTH;
235 }
236
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800237 command_packet.write_count = write_count;
238 command_packet.read_count = read_count;
Anton Staafb2647882014-09-17 15:13:43 -0700239
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800240 memcpy(command_packet.data, write_buffer, write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700241
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700242 ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
243 ctx_data->out_ep,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700244 (void*)&command_packet,
245 write_count + PACKET_HEADER_SIZE,
246 &transferred,
247 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100248 if (ret != 0) {
249 msg_perr("Raiden: OUT transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800250 " write_count = %d\n"
251 " read_count = %d\n",
252 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100253 return ret;
254 }
Anton Staafb2647882014-09-17 15:13:43 -0700255
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700256 if ((unsigned) transferred != write_count + PACKET_HEADER_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700257 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
258 transferred, write_count + PACKET_HEADER_SIZE);
259 return 0x10001;
260 }
261
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800262 return 0;
263}
264
265static int read_response(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700266 unsigned int write_count,
267 unsigned int read_count,
268 const unsigned char *write_buffer,
269 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800270{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800271 int transferred;
272 int ret;
273 usb_spi_response_t response_packet;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700274 const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800275
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700276 ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
277 ctx_data->in_ep,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700278 (void*)&response_packet,
279 read_count + PACKET_HEADER_SIZE,
280 &transferred,
281 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100282 if (ret != 0) {
283 msg_perr("Raiden: IN transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800284 " write_count = %d\n"
285 " read_count = %d\n",
286 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100287 return ret;
288 }
Anton Staafb2647882014-09-17 15:13:43 -0700289
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700290 if ((unsigned) transferred != read_count + PACKET_HEADER_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700291 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700292 transferred, read_count + PACKET_HEADER_SIZE);
Anton Staafb2647882014-09-17 15:13:43 -0700293 return 0x10002;
294 }
295
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800296 memcpy(read_buffer, response_packet.data, read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700297
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800298 return response_packet.status_code;
299}
300
301static int send_command(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700302 unsigned int write_count,
303 unsigned int read_count,
304 const unsigned char *write_buffer,
305 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800306{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800307 int status = -1;
308
Brian J. Nemec1118a582020-02-04 18:26:02 -0800309 for (int write_attempt = 0; write_attempt < WRITE_RETY_ATTEMPTS;
310 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800311
Brian J. Nemec1118a582020-02-04 18:26:02 -0800312 status = write_command(flash, write_count, read_count,
313 write_buffer, read_buffer);
314
315 if (status) {
316 /* Write operation failed. */
317 msg_perr("Raiden: Write command failed\n"
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700318 "Write attempt = %d\n"
319 "status = %d\n",
320 write_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800321 if (!retry_recovery(status)) {
322 /* Reattempting will not result in a recovery. */
323 return status;
324 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800325 programmer_delay(RETY_INTERVAL_US);
326 continue;
327 }
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700328 for (int read_attempt = 0; read_attempt < READ_RETY_ATTEMPTS; read_attempt++) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800329
330 status = read_response(flash, write_count, read_count,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700331 write_buffer, read_buffer);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800332
333 if (status != 0) {
334 /* Read operation failed. */
335 msg_perr("Raiden: Read response failed\n"
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700336 "Write attempt = %d\n"
337 "Read attempt = %d\n"
338 "status = %d\n",
339 write_attempt + 1, read_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800340 if (!retry_recovery(status)) {
341 /* Reattempting will not result in a recovery. */
342 return status;
343 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800344 programmer_delay(RETY_INTERVAL_US);
345 } else {
346 /* We were successful at performing the SPI transfer. */
347 return status;
348 }
349 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800350 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800351 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700352}
353
354/*
355 * Unfortunately there doesn't seem to be a way to specify the maximum number
356 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700357 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700358 * of command for the flash device, resulting in a 62 byte packet, that we then
359 * add two bytes to in either direction, making our way up to the 64 byte
360 * maximum USB packet size for the device.
361 *
362 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700363 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700364 */
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800365#define MAX_DATA_SIZE (PAYLOAD_SIZE - JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700366
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700367static struct spi_master spi_master_raiden_debug = {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700368 .features = SPI_MASTER_4BA,
369 .max_data_read = MAX_DATA_SIZE,
370 .max_data_write = MAX_DATA_SIZE,
371 .command = send_command,
372 .multicommand = default_spi_send_multicommand,
373 .read = default_spi_read,
374 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700375};
376
Anton Staaf5614e252015-03-24 14:33:33 -0700377static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800378 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700379{
Anton Staaf5614e252015-03-24 14:33:33 -0700380 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
381 direction) &&
382 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
383 LIBUSB_TRANSFER_TYPE_BULK));
384}
Anton Staafd27536d2014-09-30 08:10:17 -0700385
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700386static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700387{
388 int i;
389 int in_count = 0;
390 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700391
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100392 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700393 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100394 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700395
Anton Staaf5614e252015-03-24 14:33:33 -0700396 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
397 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100398 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700399 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
400 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100401 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700402 }
403 }
404
405 if (in_count != 1 || out_count != 1) {
406 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
407 " found %d IN and %d OUT endpoints\n",
408 in_count,
409 out_count);
410 return 1;
411 }
412
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100413 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
414 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700415
416 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700417}
418
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700419static int raiden_debug_spi_shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700420{
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700421 struct raiden_debug_spi_data * ctx_data =
422 (struct raiden_debug_spi_data *)data;
423
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100424 int ret = LIBUSB(libusb_control_transfer(
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700425 ctx_data->dev->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700426 LIBUSB_ENDPOINT_OUT |
427 LIBUSB_REQUEST_TYPE_VENDOR |
428 LIBUSB_RECIPIENT_INTERFACE,
429 RAIDEN_DEBUG_SPI_REQ_DISABLE,
430 0,
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700431 ctx_data->dev->interface_descriptor->bInterfaceNumber,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700432 NULL,
433 0,
434 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100435 if (ret != 0) {
436 msg_perr("Raiden: Failed to disable SPI bridge\n");
437 return ret;
438 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700439
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700440 usb_device_free(ctx_data->dev);
Anton Staaf4589cd12015-03-23 13:36:44 -0700441 libusb_exit(NULL);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700442 free(ctx_data);
Anton Staaf4589cd12015-03-23 13:36:44 -0700443
444 return 0;
445}
446
Edward O'Callaghandda1e542020-02-03 12:45:01 +1100447static int get_target(void)
Anton Staafb2647882014-09-17 15:13:43 -0700448{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700449 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700450
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100451 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700452 if (target_str) {
453 if (!strcasecmp(target_str, "ap"))
454 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
455 else if (!strcasecmp(target_str, "ec"))
456 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
457 else {
458 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100459 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700460 }
461 }
David Hendricks98b3c572016-11-30 01:50:08 +0000462 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700463
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100464 return request_enable;
465}
466
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700467static void free_dev_list(struct usb_device **dev_lst)
468{
469 struct usb_device *dev = *dev_lst;
470 /* free devices we don't care about */
471 dev = dev->next;
472 while (dev)
473 dev = usb_device_free(dev);
474}
475
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100476int raiden_debug_spi_init(void)
477{
478 struct usb_match match;
479 char *serial = extract_programmer_param("serial");
480 struct usb_device *current;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700481 struct usb_device *device = NULL;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100482 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100483 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100484
485 int request_enable = get_target();
486 if (request_enable < 0)
487 return 1;
488
Anton Staaf5614e252015-03-24 14:33:33 -0700489 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700490
Anton Staaf5614e252015-03-24 14:33:33 -0700491 usb_match_value_default(&match.vid, GOOGLE_VID);
492 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
493 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
494 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700495
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100496 ret = LIBUSB(libusb_init(NULL));
497 if (ret != 0) {
498 msg_perr("Raiden: libusb_init failed\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700499 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100500 return ret;
501 }
Anton Staaf5614e252015-03-24 14:33:33 -0700502
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100503 ret = usb_device_find(&match, &current);
504 if (ret != 0) {
505 msg_perr("Raiden: Failed to find devices\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700506 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100507 return ret;
508 }
Anton Staaf5614e252015-03-24 14:33:33 -0700509
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700510 uint8_t in_endpoint = 0;
511 uint8_t out_endpoint = 0;
David Hendricks5c79a492016-06-14 20:56:36 -0700512 while (current) {
513 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700514
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100515 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700516 msg_pdbg("Raiden: Failed to find valid endpoints on device");
517 usb_device_show(" ", current);
518 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700519 }
Anton Staaf5614e252015-03-24 14:33:33 -0700520
David Hendricks5c79a492016-06-14 20:56:36 -0700521 if (usb_device_claim(device)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700522 msg_pdbg("Raiden: Failed to claim USB 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 (!serial) {
528 found = 1;
529 goto loop_end;
530 } else {
531 unsigned char dev_serial[32];
532 struct libusb_device_descriptor descriptor;
533 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700534
David Hendricks5c79a492016-06-14 20:56:36 -0700535 memset(dev_serial, 0, sizeof(dev_serial));
536
537 if (libusb_get_device_descriptor(device->device, &descriptor)) {
538 msg_pdbg("USB: Failed to get device descriptor.\n");
539 goto loop_end;
540 }
541
542 rc = libusb_get_string_descriptor_ascii(device->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700543 descriptor.iSerialNumber,
544 dev_serial,
545 sizeof(dev_serial));
David Hendricks5c79a492016-06-14 20:56:36 -0700546 if (rc < 0) {
547 LIBUSB(rc);
548 } else {
549 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700550 msg_pdbg("Raiden: Serial number %s did not match device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700551 usb_device_show(" ", current);
552 } else {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700553 msg_pinfo("Raiden: Serial number %s matched device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700554 usb_device_show(" ", current);
555 found = 1;
556 }
557 }
558 }
559
560loop_end:
561 if (found)
562 break;
563 else
564 current = usb_device_free(current);
565 }
566
567 if (!device || !found) {
568 msg_perr("Raiden: No usable device found.\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700569 free(serial);
David Hendricks98b3c572016-11-30 01:50:08 +0000570 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700571 }
572
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700573 free_dev_list(&current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700574
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100575 ret = LIBUSB(libusb_control_transfer(
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700576 device->handle,
577 LIBUSB_ENDPOINT_OUT |
578 LIBUSB_REQUEST_TYPE_VENDOR |
579 LIBUSB_RECIPIENT_INTERFACE,
580 request_enable,
581 0,
582 device->interface_descriptor->bInterfaceNumber,
583 NULL,
584 0,
585 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100586 if (ret != 0) {
587 msg_perr("Raiden: Failed to enable SPI bridge\n");
588 return ret;
589 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700590
Keith Short8453b552020-02-03 18:10:14 -0700591 /*
592 * Allow for power to settle on the AP and EC flash devices.
593 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
594 * can require up to 10 ms from power on to the first write.
595 */
596 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
597 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
598 usleep(50 * 1000);
599
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700600 struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data));
601 if (!data) {
602 msg_perr("Unable to allocate space for extra SPI master data.\n");
603 return SPI_GENERIC_ERROR;
604 }
605
606 data->dev = device;
607 data->in_ep = in_endpoint;
608 data->out_ep = out_endpoint;
609
610 spi_master_raiden_debug.data = data;
611
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100612 register_spi_master(&spi_master_raiden_debug);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700613 register_shutdown(raiden_debug_spi_shutdown, data);
Anton Staafb2647882014-09-17 15:13:43 -0700614
David Hendricks98b3c572016-11-30 01:50:08 +0000615 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700616}