blob: 4d8f87c6fda48dbbd5cb7445c486153e3e4a5970 [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 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070045 * The protocol for the USB-SPI bridge is implemented in the following files
46 * in that repository:
Anton Staafb2647882014-09-17 15:13:43 -070047 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070048 * chip/stm32/usb_spi.h
Anton Staafb2647882014-09-17 15:13:43 -070049 * chip/stm32/usb_spi.c
Brian J. Nemecda496dc2020-02-04 11:13:05 -080050 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070051 * bInterfaceProtocol determines which protocol is used by the USB SPI device.
Brian J. Nemecda496dc2020-02-04 11:13:05 -080052 *
Brian J. Nemecda496dc2020-02-04 11:13:05 -080053 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070054 * USB SPI Version 1:
55 *
56 * SPI transactions of up to 62B in each direction with every command having
57 * a response. The initial packet from host contains a 2B header indicating
58 * write and read counts with an optional payload length equal to the write
59 * count. The device will respond with a message that reports the 2B status
60 * code and an optional payload response length equal to read count.
61 *
62 *
63 * Message Packets:
64 *
65 * Command First Packet (Host to Device):
66 *
67 * USB SPI command, containing the number of bytes to write and read
68 * and a payload of bytes to write.
69 *
Brian J. Nemecda496dc2020-02-04 11:13:05 -080070 * +------------------+-----------------+------------------------+
71 * | write count : 1B | read count : 1B | write payload : <= 62B |
72 * +------------------+-----------------+------------------------+
73 *
74 * write count: 1 byte, zero based count of bytes to write
75 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070076 * read count: 1 byte, zero based count of bytes to read. Full duplex
77 * mode is enabled with UINT8_MAX
Brian J. Nemecda496dc2020-02-04 11:13:05 -080078 *
79 * write payload: Up to 62 bytes of data to write to SPI, the total
80 * length of all TX packets must match write count.
81 * Due to data alignment constraints, this must be an
82 * even number of bytes unless this is the final packet.
83 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -070084 * Response Packet (Device to Host):
85 *
86 * USB SPI response, containing the status code and any bytes of the
87 * read payload.
88 *
Brian J. Nemecda496dc2020-02-04 11:13:05 -080089 * +-------------+-----------------------+
90 * | status : 2B | read payload : <= 62B |
91 * +-------------+-----------------------+
92 *
93 * status: 2 byte status
94 * 0x0000: Success
95 * 0x0001: SPI timeout
96 * 0x0002: Busy, try again
97 * This can happen if someone else has acquired the shared memory
98 * buffer that the SPI driver uses as /dev/null
Brian J. Nemecc5d69462020-07-22 03:12:15 -070099 * 0x0003: Write count invalid (over 62 bytes)
100 * 0x0004: Read count invalid (over 62 bytes)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800101 * 0x0005: The SPI bridge is disabled.
102 * 0x8000: Unknown error mask
103 * The bottom 15 bits will contain the bottom 15 bits from the EC
104 * error code.
105 *
106 * read payload: Up to 62 bytes of data read from SPI, the total
107 * length of all RX packets must match read count
108 * unless an error status was returned. Due to data
109 * alignment constraints, this must be a even number
110 * of bytes unless this is the final packet.
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800111 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700112 *
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800113 * USB Error Codes:
114 *
115 * send_command return codes have the following format:
116 *
117 * 0x00000: Status code success.
118 * 0x00001-0x0FFFF: Error code returned by the USB SPI device.
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700119 * 0x10001-0x1FFFF: Error code returned by the USB SPI host.
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800120 * 0x20001-0x20063 Lower bits store the positive value representation
121 * of the libusb_error enum. See the libusb documentation:
122 * http://libusb.sourceforge.net/api-1.0/group__misc.html
Anton Staafb2647882014-09-17 15:13:43 -0700123 */
124
Anton Staafb2647882014-09-17 15:13:43 -0700125#include "programmer.h"
126#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -0700127#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -0700128
129#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700130#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -0700131#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700132#include <string.h>
Keith Short8453b552020-02-03 18:10:14 -0700133#include <unistd.h>
Anton Staafb2647882014-09-17 15:13:43 -0700134
Brian J. Nemecb42d6c12020-07-23 03:07:38 -0700135/* FIXME: Add some programmer IDs here */
136const struct dev_entry devs_raiden[] = {
137 {0},
138};
139
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800140#define GOOGLE_VID (0x18D1)
141#define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51)
Brian J. Nemecb746f822020-07-22 02:57:56 -0700142
143enum {
144 GOOGLE_RAIDEN_SPI_PROTOCOL_V1 = 0x01,
145 GOOGLE_RAIDEN_SPI_PROTOCOL_V2 = 0x02,
146};
Anton Staafb2647882014-09-17 15:13:43 -0700147
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700148enum {
149 /* The host failed to transfer the data with no libusb error. */
150 USB_SPI_HOST_TX_BAD_TRANSFER = 0x10001,
151 /* The number of bytes written did not match expected. */
152 USB_SPI_HOST_TX_WRITE_FAILURE = 0x10002,
153
154 /* We did not receive the expected USB packet. */
155 USB_SPI_HOST_RX_UNEXPECTED_PACKET = 0x11001,
156 /* We received a continue packet with an invalid data index. */
157 USB_SPI_HOST_RX_BAD_DATA_INDEX = 0x11002,
158 /* We received too much data. */
159 USB_SPI_HOST_RX_DATA_OVERFLOW = 0x11003,
160 /* The number of bytes read did not match expected. */
161 USB_SPI_HOST_RX_READ_FAILURE = 0x11004,
162
163 /* We were unable to configure the device. */
164 USB_SPI_HOST_INIT_FAILURE = 0x12001,
165};
166
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800167enum usb_spi_error {
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700168 USB_SPI_SUCCESS = 0x0000,
169 USB_SPI_TIMEOUT = 0x0001,
170 USB_SPI_BUSY = 0x0002,
171 USB_SPI_WRITE_COUNT_INVALID = 0x0003,
172 USB_SPI_READ_COUNT_INVALID = 0x0004,
173 USB_SPI_DISABLED = 0x0005,
174 USB_SPI_UNKNOWN_ERROR = 0x8000,
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800175};
176
Anton Staaf4589cd12015-03-23 13:36:44 -0700177enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700178 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
179 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
180 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
181 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -0700182};
183
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800184#define PACKET_HEADER_SIZE (2)
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700185#define USB_MAX_PACKET_SIZE (64)
186#define PAYLOAD_SIZE_V1 (USB_MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700187#define SPI_TRANSFER_V1_MAX (PAYLOAD_SIZE_V1)
Anton Staafb2647882014-09-17 15:13:43 -0700188
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800189/*
190 * Servo Micro has an error where it is capable of acknowledging USB packets
191 * without loading it into the USB endpoint buffers or triggering interrupts.
192 * See crbug.com/952494. Retry mechanisms have been implemented to recover
193 * from these rare failures allowing the process to continue.
194 */
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700195#define WRITE_RETRY_ATTEMPTS (3)
196#define READ_RETRY_ATTEMPTS (3)
197#define RETRY_INTERVAL_US (100 * 1000)
Brian J. Nemec1118a582020-02-04 18:26:02 -0800198
Anton Staafb2647882014-09-17 15:13:43 -0700199/*
200 * This timeout is so large because the Raiden SPI timeout is 800ms.
201 */
Edward O'Callaghanf1e6ef52020-03-03 13:57:15 +1100202#define TRANSFER_TIMEOUT_MS (200 + 800)
Anton Staafb2647882014-09-17 15:13:43 -0700203
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700204struct raiden_debug_spi_data {
205 struct usb_device *dev;
206 uint8_t in_ep;
207 uint8_t out_ep;
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700208 uint8_t protocol_version;
209 /*
210 * Note: Due to bugs, flashrom does not always treat the max_data_write
211 * and max_data_read counts as the maximum packet size. As a result, we
212 * have to store a local copy of the actual max packet sizes and validate
213 * against it when performing transfers.
214 */
215 uint16_t max_spi_write_count;
216 uint16_t max_spi_read_count;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700217};
Anton Staafb2647882014-09-17 15:13:43 -0700218
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700219/*
220 * Version 1 protocol specific attributes
221 */
Anton Staafb2647882014-09-17 15:13:43 -0700222
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700223struct usb_spi_command_v1 {
224 uint8_t write_count;
225 /* UINT8_MAX indicates full duplex mode on compliant devices. */
226 uint8_t read_count;
227 uint8_t data[PAYLOAD_SIZE_V1];
228} __attribute__((packed));
229
230struct usb_spi_response_v1 {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800231 uint16_t status_code;
Brian J. Nemecb746f822020-07-22 02:57:56 -0700232 uint8_t data[PAYLOAD_SIZE_V1];
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700233} __attribute__((packed));
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800234
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700235union usb_spi_packet_v1 {
236 struct usb_spi_command_v1 command;
237 struct usb_spi_response_v1 response;
238} __attribute__((packed));
239
240struct usb_spi_packet_ctx {
241 union {
242 uint8_t bytes[USB_MAX_PACKET_SIZE];
243 union usb_spi_packet_v1 packet_v1;
244 };
245 /*
246 * By storing the number of bytes in the header and knowing that the
247 * USB data packets are all 64B long, we are able to use the header
248 * size to store the offset of the buffer and it's size without
249 * duplicating variables that can go out of sync.
250 */
251 size_t header_size;
252 /* Number of bytes in the packet */
253 size_t packet_size;
254};
255
Brian J. Nemec1889a032020-07-22 03:16:45 -0700256struct usb_spi_transmit_ctx {
257 /* Buffer we are reading data from. */
258 const uint8_t *buffer;
259 /* Number of bytes in the transfer. */
260 size_t transmit_size;
261 /* Number of bytes transferred. */
262 size_t transmit_index;
263};
264
265struct usb_spi_receive_ctx {
266 /* Buffer we are writing data into. */
267 uint8_t *buffer;
268 /* Number of bytes in the transfer. */
269 size_t receive_size;
270 /* Number of bytes transferred. */
271 size_t receive_index;
272};
273
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800274/*
275 * This function will return true when an error code can potentially recover
276 * if we attempt to write SPI data to the device or read from it. We know
277 * that some conditions are not recoverable in the current state so allows us
278 * to bypass the retry logic and terminate early.
279 */
280static bool retry_recovery(int error_code)
281{
282 if (error_code < 0x10000) {
Brian J. Nemec1a61f722020-05-04 20:58:06 -0700283 /*
284 * Handle error codes returned from the device. USB_SPI_TIMEOUT,
285 * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed
286 * during transfer errors to the device and can be recovered.
287 */
288 if (USB_SPI_READ_COUNT_INVALID <= error_code &&
289 error_code <= USB_SPI_DISABLED) {
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800290 return false;
291 }
292 } else if (usb_device_is_libusb_error(error_code)) {
293 /* Handle error codes returned from libusb. */
294 if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) {
295 return false;
296 }
297 }
298 return true;
299}
300
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700301static struct raiden_debug_spi_data *
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700302 get_raiden_data_from_context(const struct flashctx *flash)
303{
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700304 return (struct raiden_debug_spi_data *)flash->mst->spi.data;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700305}
306
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700307/*
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700308 * Read data into the receive buffer.
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700309 *
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700310 * @param dst Destination receive context we are writing data to.
311 * @param src Source packet context we are reading data from.
312 *
313 * @returns status code 0 on success.
314 * USB_SPI_HOST_RX_DATA_OVERFLOW if the source packet is too
315 * large to fit in read buffer.
316 */
317static int read_usb_packet(struct usb_spi_receive_ctx *dst,
318 const struct usb_spi_packet_ctx *src)
319{
320 size_t max_read_length = dst->receive_size - dst->receive_index;
321 size_t bytes_in_buffer = src->packet_size - src->header_size;
322 const uint8_t *packet_buffer = src->bytes + src->header_size;
323
324 if (bytes_in_buffer > max_read_length) {
325 /*
326 * An error occurred, we should not receive more data than
327 * the buffer can support.
328 */
329 msg_perr("Raiden: Receive packet overflowed\n"
330 " bytes_in_buffer = %zu\n"
331 " max_read_length = %zu\n"
332 " receive_index = %zu\n"
333 " receive_size = %zu\n",
334 bytes_in_buffer, max_read_length,
335 dst->receive_size, dst->receive_index);
336 return USB_SPI_HOST_RX_DATA_OVERFLOW;
337 }
338 memcpy(dst->buffer + dst->receive_index, packet_buffer,
339 bytes_in_buffer);
340
341 dst->receive_index += bytes_in_buffer;
342 return 0;
343}
344
345/*
346 * Fill the USB packet with data from the transmit buffer.
347 *
348 * @param dst Destination packet context we are writing data to.
349 * @param src Source transmit context we are reading data from.
350 */
351static void fill_usb_packet(struct usb_spi_packet_ctx *dst,
352 struct usb_spi_transmit_ctx *src)
353{
354 size_t transmit_size = src->transmit_size - src->transmit_index;
355 size_t max_buffer_size = USB_MAX_PACKET_SIZE - dst->header_size;
356 uint8_t *packet_buffer = dst->bytes + dst->header_size;
357
358 if (transmit_size > max_buffer_size)
359 transmit_size = max_buffer_size;
360
361 memcpy(packet_buffer, src->buffer + src->transmit_index, transmit_size);
362
363 dst->packet_size = dst->header_size + transmit_size;
364 src->transmit_index += transmit_size;
365}
366
367/*
368 * Receive the data from the device USB endpoint and store in the packet.
369 *
370 * @param ctx_data Raiden SPI config.
371 * @param packet Destination packet used to store the endpoint data.
372 *
373 * @returns Returns status code with 0 on success.
374 */
375static int receive_packet(const struct raiden_debug_spi_data *ctx_data,
376 struct usb_spi_packet_ctx *packet)
377{
378 int received;
379 int status = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
380 ctx_data->in_ep,
381 packet->bytes,
382 USB_MAX_PACKET_SIZE,
383 &received,
384 TRANSFER_TIMEOUT_MS));
385 packet->packet_size = received;
386 if (status) {
387 msg_perr("Raiden: IN transfer failed\n"
388 " received = %d\n"
389 " status = 0x%05x\n",
390 received, status);
391 }
392 return status;
393}
394
395/*
396 * Transmit data from the packet to the device's USB endpoint.
397 *
398 * @param ctx_data Raiden SPI config.
399 * @param packet Source packet we will write to the endpoint data.
400 *
401 * @returns Returns status code with 0 on success.
402 */
403static int transmit_packet(const struct raiden_debug_spi_data *ctx_data,
404 struct usb_spi_packet_ctx *packet)
405{
406 int transferred;
407 int status = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
408 ctx_data->out_ep,
409 packet->bytes,
410 packet->packet_size,
411 &transferred,
412 TRANSFER_TIMEOUT_MS));
413 if (status || (size_t)transferred != packet->packet_size) {
414 if (!status) {
415 /* No error was reported, but we didn't transmit the data expected. */
416 status = USB_SPI_HOST_TX_BAD_TRANSFER;
417 }
418 msg_perr("Raiden: OUT transfer failed\n"
419 " transferred = %d\n"
420 " packet_size = %zu\n"
421 " status = 0x%05x\n",
422 transferred, packet->packet_size, status);
423
424 }
425 return status;
426}
427
428/*
429 * Version 1 protocol command to start a USB SPI transfer and write the payload.
430 *
431 * @param ctx_data Raiden SPI config.
Brian J. Nemec1889a032020-07-22 03:16:45 -0700432 * @param write Write context of data to transmit and write payload.
433 * @param read Read context of data to receive and read buffer.
434 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700435 * @returns Returns status code with 0 on success.
436 */
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700437static int write_command_v1(const struct raiden_debug_spi_data *ctx_data,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700438 struct usb_spi_transmit_ctx *write,
439 struct usb_spi_receive_ctx *read)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800440{
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700441 struct usb_spi_packet_ctx command = {
442 .header_size = offsetof(struct usb_spi_command_v1, data),
443 .packet_v1.command.write_count = write->transmit_size,
444 .packet_v1.command.read_count = read->receive_size
445 };
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800446
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700447 /* Reset the write context to the start. */
448 write->transmit_index = 0;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800449
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700450 fill_usb_packet(&command, write);
451 return transmit_packet(ctx_data, &command);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800452}
453
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700454/*
455 * Version 1 Protocol: Responsible for reading the response of the USB SPI
456 * transfer. Status codes from the transfer and any read payload are copied
457 * to the read_buffer.
458 *
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700459 * @param ctx_data Raiden SPI config.
Brian J. Nemec1889a032020-07-22 03:16:45 -0700460 * @param write Write context of data to transmit and write payload.
461 * @param read Read context of data to receive and read buffer.
462 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700463 * @returns Returns status code with 0 on success.
464 */
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700465static int read_response_v1(const struct raiden_debug_spi_data *ctx_data,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700466 struct usb_spi_transmit_ctx *write,
467 struct usb_spi_receive_ctx *read)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800468{
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700469 int status;
470 struct usb_spi_packet_ctx response;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800471
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700472 /* Reset the read context to the start. */
473 read->receive_index = 0;
474
475 status = receive_packet(ctx_data, &response);
476 if (status) {
477 /* Return the transfer error since the status_code is unreliable */
478 return status;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100479 }
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700480 if (response.packet_v1.response.status_code) {
481 return response.packet_v1.response.status_code;
Anton Staafb2647882014-09-17 15:13:43 -0700482 }
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700483 response.header_size = offsetof(struct usb_spi_response_v1, data);
Anton Staafb2647882014-09-17 15:13:43 -0700484
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700485 status = read_usb_packet(read, &response);
486 return status;
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800487}
488
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700489/*
490 * Version 1 Protocol: Sets up a USB SPI transfer, transmits data to the device,
491 * reads the status code and any payload from the device. This will also handle
492 * recovery if an error has occurred.
493 *
494 * @param flash Flash context storing SPI capabilities and USB device
495 * information.
496 * @param write_count Number of bytes to write
497 * @param read_count Number of bytes to read
498 * @param write_buffer Address of write buffer
499 * @param read_buffer Address of buffer to store read data
500 *
501 * @returns Returns status code with 0 on success.
502 */
503static int send_command_v1(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700504 unsigned int write_count,
505 unsigned int read_count,
506 const unsigned char *write_buffer,
507 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800508{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800509 int status = -1;
510
Brian J. Nemec1889a032020-07-22 03:16:45 -0700511 struct usb_spi_transmit_ctx write_ctx = {
512 .buffer = write_buffer,
513 .transmit_size = write_count
514 };
515 struct usb_spi_receive_ctx read_ctx = {
516 .buffer = read_buffer,
517 .receive_size = read_count
518 };
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700519 const struct raiden_debug_spi_data *ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemec1889a032020-07-22 03:16:45 -0700520
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700521 if (write_count > ctx_data->max_spi_write_count) {
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700522 msg_perr("Raiden: Invalid write count\n"
523 " write count = %u\n"
524 " max write = %d\n",
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700525 write_count, ctx_data->max_spi_write_count);
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700526 return SPI_INVALID_LENGTH;
527 }
528
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700529 if (read_count > ctx_data->max_spi_read_count) {
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700530 msg_perr("Raiden: Invalid read count\n"
531 " read count = %d\n"
532 " max read = %d\n",
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700533 read_count, ctx_data->max_spi_read_count);
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700534 return SPI_INVALID_LENGTH;
535 }
536
537 for (unsigned int write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS;
Brian J. Nemec1118a582020-02-04 18:26:02 -0800538 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800539
Brian J. Nemec1889a032020-07-22 03:16:45 -0700540
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700541 status = write_command_v1(ctx_data, &write_ctx, &read_ctx);
542
543 if (!status &&
544 (write_ctx.transmit_index != write_ctx.transmit_size)) {
545 /* No errors were reported, but write is incomplete. */
546 status = USB_SPI_HOST_TX_WRITE_FAILURE;
547 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800548
549 if (status) {
550 /* Write operation failed. */
551 msg_perr("Raiden: Write command failed\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700552 " write count = %u\n"
553 " read count = %u\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700554 " transmitted bytes = %zu\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700555 " write attempt = %u\n"
556 " status = 0x%05x\n",
Brian J. Nemec1889a032020-07-22 03:16:45 -0700557
558 write_count, read_count, write_ctx.transmit_index,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700559 write_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800560 if (!retry_recovery(status)) {
561 /* Reattempting will not result in a recovery. */
562 return status;
563 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700564 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800565 continue;
566 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700567 for (unsigned int read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS;
568 read_attempt++) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800569
Brian J. Nemec3ddd6832020-07-22 03:20:18 -0700570 status = read_response_v1(ctx_data, &write_ctx, &read_ctx);
571
572 if (!status) {
573 if (read_ctx.receive_size == read_ctx.receive_index) {
574 /* Successful transfer. */
575 return status;
576 } else {
577 /* Report the error from the failed read. */
578 status = USB_SPI_HOST_RX_READ_FAILURE;
579 }
580 }
Brian J. Nemec1118a582020-02-04 18:26:02 -0800581
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700582 if (status) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800583 /* Read operation failed. */
584 msg_perr("Raiden: Read response failed\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700585 " write count = %u\n"
586 " read count = %u\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700587 " received bytes = %zu\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700588 " write attempt = %u\n"
589 " read attempt = %u\n"
590 " status = 0x%05x\n",
Brian J. Nemec1889a032020-07-22 03:16:45 -0700591 write_count, read_count, read_ctx.receive_index,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700592 write_attempt + 1, read_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800593 if (!retry_recovery(status)) {
594 /* Reattempting will not result in a recovery. */
595 return status;
596 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700597 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800598 }
599 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800600 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800601 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700602}
603
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700604static const struct spi_master spi_master_raiden_debug = {
Brian J. Nemecb746f822020-07-22 02:57:56 -0700605 .features = SPI_MASTER_4BA,
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700606 .max_data_read = 0,
607 .max_data_write = 0,
608 .command = NULL,
Brian J. Nemecb746f822020-07-22 02:57:56 -0700609 .multicommand = default_spi_send_multicommand,
610 .read = default_spi_read,
611 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700612};
613
Anton Staaf5614e252015-03-24 14:33:33 -0700614static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800615 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700616{
Anton Staaf5614e252015-03-24 14:33:33 -0700617 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
618 direction) &&
619 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
620 LIBUSB_TRANSFER_TYPE_BULK));
621}
Anton Staafd27536d2014-09-30 08:10:17 -0700622
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700623static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700624{
625 int i;
626 int in_count = 0;
627 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700628
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100629 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700630 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100631 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700632
Anton Staaf5614e252015-03-24 14:33:33 -0700633 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
634 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100635 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700636 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
637 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100638 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700639 }
640 }
641
642 if (in_count != 1 || out_count != 1) {
643 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
644 " found %d IN and %d OUT endpoints\n",
645 in_count,
646 out_count);
647 return 1;
648 }
649
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100650 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
651 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700652
653 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700654}
655
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700656/*
657 * Configure the USB SPI master based on the device we are connected to.
658 * It will use the device's bInterfaceProtocol to identify which protocol
659 * is being used by the device USB SPI interface and if needed query the
660 * device for its capabilities.
661 *
662 * @param spi_config Raiden SPI config which will be modified.
663 *
664 * @returns Returns status code with 0 on success.
665 */
666static int configure_protocol(struct spi_master *spi_config)
667{
668 struct raiden_debug_spi_data *ctx_data =
669 (struct raiden_debug_spi_data *)spi_config->data;
670
671 ctx_data->protocol_version =
672 ctx_data->dev->interface_descriptor->bInterfaceProtocol;
673
674 switch (ctx_data->protocol_version) {
675 case GOOGLE_RAIDEN_SPI_PROTOCOL_V1:
676 /*
677 * Protocol V1 is supported by adjusting the max data
678 * read and write sizes which results in no continue packets.
679 */
680 spi_config->command = send_command_v1;
681 ctx_data->max_spi_write_count = SPI_TRANSFER_V1_MAX;
682 ctx_data->max_spi_read_count = SPI_TRANSFER_V1_MAX;
683 break;
684 default:
685 msg_pdbg("Raiden: Unknown USB SPI protocol version = %d",
686 ctx_data->protocol_version);
687 return USB_SPI_HOST_INIT_FAILURE;
688 }
689
690 /*
691 * Unfortunately there doesn't seem to be a way to specify the maximum number
692 * of bytes that your SPI device can read/write, these values are the maximum
693 * data chunk size that flashrom will package up with an additional five bytes
694 * of command for the flash device.
695 *
696 * The largest command that flashrom generates is the byte program command, so
697 * we use that command header maximum size here. If we didn't include the
698 * offset, flashrom may request a SPI transfer that is too large for the SPI
699 * device to support.
700 */
701 spi_config->max_data_write = ctx_data->max_spi_write_count -
702 JEDEC_BYTE_PROGRAM_OUTSIZE;
703 spi_config->max_data_read = ctx_data->max_spi_read_count -
704 JEDEC_BYTE_PROGRAM_OUTSIZE;
705
706 return 0;
707}
708
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700709static int raiden_debug_spi_shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700710{
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700711 struct spi_master *spi_config = data;
712 struct raiden_debug_spi_data *ctx_data =
713 (struct raiden_debug_spi_data *)spi_config->data;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700714
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100715 int ret = LIBUSB(libusb_control_transfer(
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700716 ctx_data->dev->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700717 LIBUSB_ENDPOINT_OUT |
718 LIBUSB_REQUEST_TYPE_VENDOR |
719 LIBUSB_RECIPIENT_INTERFACE,
720 RAIDEN_DEBUG_SPI_REQ_DISABLE,
721 0,
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700722 ctx_data->dev->interface_descriptor->bInterfaceNumber,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700723 NULL,
724 0,
725 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100726 if (ret != 0) {
727 msg_perr("Raiden: Failed to disable SPI bridge\n");
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700728 free(ctx_data);
729 free(spi_config);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100730 return ret;
731 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700732
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700733 usb_device_free(ctx_data->dev);
Anton Staaf4589cd12015-03-23 13:36:44 -0700734 libusb_exit(NULL);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700735 free(ctx_data);
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700736 free(spi_config);
Anton Staaf4589cd12015-03-23 13:36:44 -0700737
738 return 0;
739}
740
Edward O'Callaghandda1e542020-02-03 12:45:01 +1100741static int get_target(void)
Anton Staafb2647882014-09-17 15:13:43 -0700742{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700743 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700744
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100745 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700746 if (target_str) {
747 if (!strcasecmp(target_str, "ap"))
748 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
749 else if (!strcasecmp(target_str, "ec"))
750 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
751 else {
752 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100753 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700754 }
755 }
David Hendricks98b3c572016-11-30 01:50:08 +0000756 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700757
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100758 return request_enable;
759}
760
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700761static void free_dev_list(struct usb_device **dev_lst)
762{
763 struct usb_device *dev = *dev_lst;
764 /* free devices we don't care about */
765 dev = dev->next;
766 while (dev)
767 dev = usb_device_free(dev);
768}
769
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100770int raiden_debug_spi_init(void)
771{
772 struct usb_match match;
773 char *serial = extract_programmer_param("serial");
774 struct usb_device *current;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700775 struct usb_device *device = NULL;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100776 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100777 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100778
779 int request_enable = get_target();
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700780 if (request_enable < 0) {
781 free(serial);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100782 return 1;
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700783 }
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100784
Anton Staaf5614e252015-03-24 14:33:33 -0700785 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700786
Anton Staaf5614e252015-03-24 14:33:33 -0700787 usb_match_value_default(&match.vid, GOOGLE_VID);
788 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
789 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
Brian J. Nemecb746f822020-07-22 02:57:56 -0700790 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL_V1);
Anton Staafb2647882014-09-17 15:13:43 -0700791
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100792 ret = LIBUSB(libusb_init(NULL));
793 if (ret != 0) {
794 msg_perr("Raiden: libusb_init failed\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700795 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100796 return ret;
797 }
Anton Staaf5614e252015-03-24 14:33:33 -0700798
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100799 ret = usb_device_find(&match, &current);
800 if (ret != 0) {
801 msg_perr("Raiden: Failed to find devices\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700802 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100803 return ret;
804 }
Anton Staaf5614e252015-03-24 14:33:33 -0700805
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700806 uint8_t in_endpoint = 0;
807 uint8_t out_endpoint = 0;
David Hendricks5c79a492016-06-14 20:56:36 -0700808 while (current) {
809 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700810
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100811 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700812 msg_pdbg("Raiden: Failed to find valid endpoints on device");
813 usb_device_show(" ", current);
814 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700815 }
Anton Staaf5614e252015-03-24 14:33:33 -0700816
David Hendricks5c79a492016-06-14 20:56:36 -0700817 if (usb_device_claim(device)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700818 msg_pdbg("Raiden: Failed to claim USB device");
819 usb_device_show(" ", current);
820 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700821 }
Anton Staaf5614e252015-03-24 14:33:33 -0700822
David Hendricks5c79a492016-06-14 20:56:36 -0700823 if (!serial) {
824 found = 1;
825 goto loop_end;
826 } else {
827 unsigned char dev_serial[32];
828 struct libusb_device_descriptor descriptor;
829 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700830
David Hendricks5c79a492016-06-14 20:56:36 -0700831 memset(dev_serial, 0, sizeof(dev_serial));
832
833 if (libusb_get_device_descriptor(device->device, &descriptor)) {
834 msg_pdbg("USB: Failed to get device descriptor.\n");
835 goto loop_end;
836 }
837
838 rc = libusb_get_string_descriptor_ascii(device->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700839 descriptor.iSerialNumber,
840 dev_serial,
841 sizeof(dev_serial));
David Hendricks5c79a492016-06-14 20:56:36 -0700842 if (rc < 0) {
843 LIBUSB(rc);
844 } else {
845 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700846 msg_pdbg("Raiden: Serial number %s did not match device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700847 usb_device_show(" ", current);
848 } else {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700849 msg_pinfo("Raiden: Serial number %s matched device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700850 usb_device_show(" ", current);
851 found = 1;
852 }
853 }
854 }
855
856loop_end:
857 if (found)
858 break;
859 else
860 current = usb_device_free(current);
861 }
862
863 if (!device || !found) {
864 msg_perr("Raiden: No usable device found.\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700865 free(serial);
David Hendricks98b3c572016-11-30 01:50:08 +0000866 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700867 }
868
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700869 free_dev_list(&current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700870
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100871 ret = LIBUSB(libusb_control_transfer(
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700872 device->handle,
873 LIBUSB_ENDPOINT_OUT |
874 LIBUSB_REQUEST_TYPE_VENDOR |
875 LIBUSB_RECIPIENT_INTERFACE,
876 request_enable,
877 0,
878 device->interface_descriptor->bInterfaceNumber,
879 NULL,
880 0,
881 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100882 if (ret != 0) {
883 msg_perr("Raiden: Failed to enable SPI bridge\n");
884 return ret;
885 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700886
Keith Short8453b552020-02-03 18:10:14 -0700887 /*
888 * Allow for power to settle on the AP and EC flash devices.
889 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
890 * can require up to 10 ms from power on to the first write.
891 */
892 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
893 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
894 usleep(50 * 1000);
895
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700896 struct spi_master *spi_config = calloc(1, sizeof(struct spi_master));
897 if (!spi_config) {
898 msg_perr("Unable to allocate space for SPI master.\n");
899 return SPI_GENERIC_ERROR;
900 }
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700901 struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data));
902 if (!data) {
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700903 free(spi_config);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700904 msg_perr("Unable to allocate space for extra SPI master data.\n");
905 return SPI_GENERIC_ERROR;
906 }
907
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700908 memcpy(spi_config, &spi_master_raiden_debug, sizeof(struct spi_master));
909
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700910 data->dev = device;
911 data->in_ep = in_endpoint;
912 data->out_ep = out_endpoint;
913
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700914 spi_config->data = data;
915 /*
916 * The SPI master needs to be configured based on the device connected.
917 * Using the device protocol interrogation, we will set the limits on
918 * the write and read sizes and switch command functions.
919 */
920 ret = configure_protocol(spi_config);
921 if (ret) {
922 msg_perr("Raiden: Error configuring protocol\n"
923 " protocol = %u\n"
924 " status = 0x%05x\n",
925 data->dev->interface_descriptor->bInterfaceProtocol, ret);
926 free(data);
927 free(spi_config);
928 return SPI_GENERIC_ERROR;
929 }
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700930
Brian J. Nemec113c2b42020-07-22 03:28:55 -0700931 register_spi_master(spi_config);
932 register_shutdown(raiden_debug_spi_shutdown, spi_config);
Anton Staafb2647882014-09-17 15:13:43 -0700933
David Hendricks98b3c572016-11-30 01:50:08 +0000934 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700935}