blob: 9b46b51c9267e78efcb4ed9a914b43a3ec528014 [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. Nemecda496dc2020-02-04 11:13:05 -0800119#define GOOGLE_VID (0x18D1)
120#define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51)
121#define GOOGLE_RAIDEN_SPI_PROTOCOL (0x01)
Anton Staafb2647882014-09-17 15:13:43 -0700122
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800123enum usb_spi_error {
124 USB_SPI_SUCCESS = 0x0000,
125 USB_SPI_TIMEOUT = 0x0001,
126 USB_SPI_BUSY = 0x0002,
127 USB_SPI_WRITE_COUNT_INVALID = 0x0003,
128 USB_SPI_READ_COUNT_INVALID = 0x0004,
129 USB_SPI_DISABLED = 0x0005,
130 USB_SPI_UNKNOWN_ERROR = 0x8000,
131};
132
Anton Staaf4589cd12015-03-23 13:36:44 -0700133enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700134 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
135 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
136 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
137 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -0700138};
139
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800140#define PACKET_HEADER_SIZE (2)
141#define MAX_PACKET_SIZE (64)
142#define PAYLOAD_SIZE (MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700143
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800144/*
145 * Servo Micro has an error where it is capable of acknowledging USB packets
146 * without loading it into the USB endpoint buffers or triggering interrupts.
147 * See crbug.com/952494. Retry mechanisms have been implemented to recover
148 * from these rare failures allowing the process to continue.
149 */
Brian J. Nemec1118a582020-02-04 18:26:02 -0800150#define WRITE_RETY_ATTEMPTS (3)
151#define READ_RETY_ATTEMPTS (3)
152#define RETY_INTERVAL_US (100 * 1000)
153
Anton Staafb2647882014-09-17 15:13:43 -0700154/*
155 * This timeout is so large because the Raiden SPI timeout is 800ms.
156 */
Edward O'Callaghanf1e6ef52020-03-03 13:57:15 +1100157#define TRANSFER_TIMEOUT_MS (200 + 800)
Anton Staafb2647882014-09-17 15:13:43 -0700158
Anton Staaf5614e252015-03-24 14:33:33 -0700159struct usb_device *device = NULL;
160uint8_t in_endpoint = 0;
161uint8_t out_endpoint = 0;
Anton Staafb2647882014-09-17 15:13:43 -0700162
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800163typedef struct {
164 int8_t write_count;
165 /* -1 Indicates readback all on halfduplex compliant devices. */
166 int8_t read_count;
167 uint8_t data[PAYLOAD_SIZE];
168} __attribute__((packed)) usb_spi_command_t;
Anton Staafb2647882014-09-17 15:13:43 -0700169
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800170typedef struct {
171 uint16_t status_code;
172 uint8_t data[PAYLOAD_SIZE];
173} __attribute__((packed)) usb_spi_response_t;
174
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800175/*
176 * This function will return true when an error code can potentially recover
177 * if we attempt to write SPI data to the device or read from it. We know
178 * that some conditions are not recoverable in the current state so allows us
179 * to bypass the retry logic and terminate early.
180 */
181static bool retry_recovery(int error_code)
182{
183 if (error_code < 0x10000) {
184 /* Handle error codes returned from the device. */
185 if (USB_SPI_WRITE_COUNT_INVALID <= error_code &&
186 error_code <= USB_SPI_DISABLED) {
187 return false;
188 }
189 } else if (usb_device_is_libusb_error(error_code)) {
190 /* Handle error codes returned from libusb. */
191 if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) {
192 return false;
193 }
194 }
195 return true;
196}
197
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800198static int write_command(const struct flashctx *flash,
199 unsigned int write_count,
200 unsigned int read_count,
201 const unsigned char *write_buffer,
202 unsigned char *read_buffer)
203{
204
205 int transferred;
206 int ret;
207 usb_spi_command_t command_packet;
208
209 if (write_count > PAYLOAD_SIZE) {
Edward O'Callaghan8eb829a2020-03-03 13:55:08 +1100210 msg_perr("Raiden: Invalid write_count of %d\n", write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700211 return SPI_INVALID_LENGTH;
212 }
213
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800214 if (read_count > PAYLOAD_SIZE) {
Edward O'Callaghan8eb829a2020-03-03 13:55:08 +1100215 msg_perr("Raiden: Invalid read_count of %d\n", read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700216 return SPI_INVALID_LENGTH;
217 }
218
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800219 command_packet.write_count = write_count;
220 command_packet.read_count = read_count;
Anton Staafb2647882014-09-17 15:13:43 -0700221
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800222 memcpy(command_packet.data, write_buffer, write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700223
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100224 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800225 out_endpoint,
226 (void*)&command_packet,
227 write_count + PACKET_HEADER_SIZE,
228 &transferred,
229 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100230 if (ret != 0) {
231 msg_perr("Raiden: OUT transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800232 " write_count = %d\n"
233 " read_count = %d\n",
234 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100235 return ret;
236 }
Anton Staafb2647882014-09-17 15:13:43 -0700237
238 if (transferred != write_count + PACKET_HEADER_SIZE) {
239 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
240 transferred, write_count + PACKET_HEADER_SIZE);
241 return 0x10001;
242 }
243
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800244 return 0;
245}
246
247static int read_response(const struct flashctx *flash,
248 unsigned int write_count,
249 unsigned int read_count,
250 const unsigned char *write_buffer,
251 unsigned char *read_buffer)
252{
253
254 int transferred;
255 int ret;
256 usb_spi_response_t response_packet;
257
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100258 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800259 in_endpoint,
260 (void*)&response_packet,
261 read_count + PACKET_HEADER_SIZE,
262 &transferred,
263 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100264 if (ret != 0) {
265 msg_perr("Raiden: IN transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800266 " write_count = %d\n"
267 " read_count = %d\n",
268 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100269 return ret;
270 }
Anton Staafb2647882014-09-17 15:13:43 -0700271
272 if (transferred != read_count + PACKET_HEADER_SIZE) {
273 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800274 transferred, read_count + PACKET_HEADER_SIZE);
Anton Staafb2647882014-09-17 15:13:43 -0700275 return 0x10002;
276 }
277
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800278 memcpy(read_buffer, response_packet.data, read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700279
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800280 return response_packet.status_code;
281}
282
283static int send_command(const struct flashctx *flash,
284 unsigned int write_count,
285 unsigned int read_count,
286 const unsigned char *write_buffer,
287 unsigned char *read_buffer)
288{
289
290 int status = -1;
291
Brian J. Nemec1118a582020-02-04 18:26:02 -0800292 for (int write_attempt = 0; write_attempt < WRITE_RETY_ATTEMPTS;
293 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800294
Brian J. Nemec1118a582020-02-04 18:26:02 -0800295 status = write_command(flash, write_count, read_count,
296 write_buffer, read_buffer);
297
298 if (status) {
299 /* Write operation failed. */
300 msg_perr("Raiden: Write command failed\n"
301 "Write attempt = %d\n"
302 "status = %d\n",
303 write_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800304 if (!retry_recovery(status)) {
305 /* Reattempting will not result in a recovery. */
306 return status;
307 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800308 programmer_delay(RETY_INTERVAL_US);
309 continue;
310 }
311 for (int read_attempt = 0; read_attempt < READ_RETY_ATTEMPTS;
312 read_attempt++) {
313
314 status = read_response(flash, write_count, read_count,
315 write_buffer, read_buffer);
316
317 if (status != 0) {
318 /* Read operation failed. */
319 msg_perr("Raiden: Read response failed\n"
320 "Write attempt = %d\n"
321 "Read attempt = %d\n"
322 "status = %d\n",
323 write_attempt + 1, read_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800324 if (!retry_recovery(status)) {
325 /* Reattempting will not result in a recovery. */
326 return status;
327 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800328 programmer_delay(RETY_INTERVAL_US);
329 } else {
330 /* We were successful at performing the SPI transfer. */
331 return status;
332 }
333 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800334 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800335 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700336}
337
338/*
339 * Unfortunately there doesn't seem to be a way to specify the maximum number
340 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700341 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700342 * of command for the flash device, resulting in a 62 byte packet, that we then
343 * add two bytes to in either direction, making our way up to the 64 byte
344 * maximum USB packet size for the device.
345 *
346 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700347 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700348 */
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800349#define MAX_DATA_SIZE (PAYLOAD_SIZE - JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700350
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100351static const struct spi_master spi_master_raiden_debug = {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800352 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
353 .features = SPI_MASTER_4BA,
354 .max_data_read = MAX_DATA_SIZE,
355 .max_data_write = MAX_DATA_SIZE,
356 .command = send_command,
357 .multicommand = default_spi_send_multicommand,
358 .read = default_spi_read,
359 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700360};
361
Anton Staaf5614e252015-03-24 14:33:33 -0700362static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800363 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700364{
Anton Staaf5614e252015-03-24 14:33:33 -0700365 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
366 direction) &&
367 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
368 LIBUSB_TRANSFER_TYPE_BULK));
369}
Anton Staafd27536d2014-09-30 08:10:17 -0700370
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800371static int find_endpoints(struct usb_device *dev, uint8_t *in_ep,
372 uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700373{
374 int i;
375 int in_count = 0;
376 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700377
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100378 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700379 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100380 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700381
Anton Staaf5614e252015-03-24 14:33:33 -0700382 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
383 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100384 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700385 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
386 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100387 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700388 }
389 }
390
391 if (in_count != 1 || out_count != 1) {
392 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
393 " found %d IN and %d OUT endpoints\n",
394 in_count,
395 out_count);
396 return 1;
397 }
398
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100399 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
400 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700401
402 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700403}
404
David Hendricks93784b42016-08-09 17:00:38 -0700405static int shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700406{
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100407 int ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700408 device->handle,
409 LIBUSB_ENDPOINT_OUT |
410 LIBUSB_REQUEST_TYPE_VENDOR |
411 LIBUSB_RECIPIENT_INTERFACE,
412 RAIDEN_DEBUG_SPI_REQ_DISABLE,
413 0,
414 device->interface_descriptor->bInterfaceNumber,
415 NULL,
416 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100417 TRANSFER_TIMEOUT_MS));
418 if (ret != 0) {
419 msg_perr("Raiden: Failed to disable SPI bridge\n");
420 return ret;
421 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700422
Anton Staaf5614e252015-03-24 14:33:33 -0700423 usb_device_free(device);
424
425 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700426 libusb_exit(NULL);
427
428 return 0;
429}
430
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100431static int get_target()
Anton Staafb2647882014-09-17 15:13:43 -0700432{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700433 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700434
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100435 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700436 if (target_str) {
437 if (!strcasecmp(target_str, "ap"))
438 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
439 else if (!strcasecmp(target_str, "ec"))
440 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
441 else {
442 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100443 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700444 }
445 }
David Hendricks98b3c572016-11-30 01:50:08 +0000446 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700447
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100448 return request_enable;
449}
450
451int raiden_debug_spi_init(void)
452{
453 struct usb_match match;
454 char *serial = extract_programmer_param("serial");
455 struct usb_device *current;
456 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100457 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100458
459 int request_enable = get_target();
460 if (request_enable < 0)
461 return 1;
462
Anton Staaf5614e252015-03-24 14:33:33 -0700463 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700464
Anton Staaf5614e252015-03-24 14:33:33 -0700465 usb_match_value_default(&match.vid, GOOGLE_VID);
466 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
467 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
468 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700469
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100470 ret = LIBUSB(libusb_init(NULL));
471 if (ret != 0) {
472 msg_perr("Raiden: libusb_init failed\n");
473 return ret;
474 }
Anton Staaf5614e252015-03-24 14:33:33 -0700475
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100476 ret = usb_device_find(&match, &current);
477 if (ret != 0) {
478 msg_perr("Raiden: Failed to find devices\n");
479 return ret;
480 }
Anton Staaf5614e252015-03-24 14:33:33 -0700481
David Hendricks5c79a492016-06-14 20:56:36 -0700482 while (current) {
483 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700484
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100485 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
David Hendricks5c79a492016-06-14 20:56:36 -0700486 msg_pdbg("Raiden: Failed to find valid endpoints on device");
487 usb_device_show(" ", current);
488 goto loop_end;
489 }
Anton Staaf5614e252015-03-24 14:33:33 -0700490
David Hendricks5c79a492016-06-14 20:56:36 -0700491 if (usb_device_claim(device)) {
492 msg_pdbg("Raiden: Failed to claim USB device");
493 usb_device_show(" ", current);
494 goto loop_end;
495 }
Anton Staaf5614e252015-03-24 14:33:33 -0700496
David Hendricks5c79a492016-06-14 20:56:36 -0700497 if (!serial) {
498 found = 1;
499 goto loop_end;
500 } else {
501 unsigned char dev_serial[32];
502 struct libusb_device_descriptor descriptor;
503 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700504
David Hendricks5c79a492016-06-14 20:56:36 -0700505 memset(dev_serial, 0, sizeof(dev_serial));
506
507 if (libusb_get_device_descriptor(device->device, &descriptor)) {
508 msg_pdbg("USB: Failed to get device descriptor.\n");
509 goto loop_end;
510 }
511
512 rc = libusb_get_string_descriptor_ascii(device->handle,
513 descriptor.iSerialNumber,
514 dev_serial,
515 sizeof(dev_serial));
516 if (rc < 0) {
517 LIBUSB(rc);
518 } else {
519 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800520 msg_pdbg("Raiden: Serial number %s did not match device",
521 serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700522 usb_device_show(" ", current);
523 } else {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800524 msg_pinfo("Raiden: Serial number %s matched device",
525 serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700526 usb_device_show(" ", current);
527 found = 1;
528 }
529 }
530 }
531
532loop_end:
533 if (found)
534 break;
535 else
536 current = usb_device_free(current);
537 }
538
539 if (!device || !found) {
540 msg_perr("Raiden: No usable device found.\n");
David Hendricks98b3c572016-11-30 01:50:08 +0000541 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700542 }
543
David Hendricks5c79a492016-06-14 20:56:36 -0700544 /* free devices we don't care about */
545 current = current->next;
546 while (current)
547 current = usb_device_free(current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700548
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100549 ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700550 device->handle,
551 LIBUSB_ENDPOINT_OUT |
552 LIBUSB_REQUEST_TYPE_VENDOR |
553 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700554 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700555 0,
556 device->interface_descriptor->bInterfaceNumber,
557 NULL,
558 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100559 TRANSFER_TIMEOUT_MS));
560 if (ret != 0) {
561 msg_perr("Raiden: Failed to enable SPI bridge\n");
562 return ret;
563 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700564
Keith Short8453b552020-02-03 18:10:14 -0700565 /*
566 * Allow for power to settle on the AP and EC flash devices.
567 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
568 * can require up to 10 ms from power on to the first write.
569 */
570 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
571 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
572 usleep(50 * 1000);
573
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100574 register_spi_master(&spi_master_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700575 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700576
David Hendricks98b3c572016-11-30 01:50:08 +0000577 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700578}