blob: 0a65e501e1fab4a5ad3a21879b553be9b78cc5e5 [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. Nemeccea6bec2020-02-25 14:12:46 -0800148enum usb_spi_error {
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700149 USB_SPI_SUCCESS = 0x0000,
150 USB_SPI_TIMEOUT = 0x0001,
151 USB_SPI_BUSY = 0x0002,
152 USB_SPI_WRITE_COUNT_INVALID = 0x0003,
153 USB_SPI_READ_COUNT_INVALID = 0x0004,
154 USB_SPI_DISABLED = 0x0005,
155 USB_SPI_UNKNOWN_ERROR = 0x8000,
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800156};
157
Anton Staaf4589cd12015-03-23 13:36:44 -0700158enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700159 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
160 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
161 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
162 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -0700163};
164
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800165#define PACKET_HEADER_SIZE (2)
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700166#define USB_MAX_PACKET_SIZE (64)
167#define PAYLOAD_SIZE_V1 (USB_MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700168
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800169/*
170 * Servo Micro has an error where it is capable of acknowledging USB packets
171 * without loading it into the USB endpoint buffers or triggering interrupts.
172 * See crbug.com/952494. Retry mechanisms have been implemented to recover
173 * from these rare failures allowing the process to continue.
174 */
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700175#define WRITE_RETRY_ATTEMPTS (3)
176#define READ_RETRY_ATTEMPTS (3)
177#define RETRY_INTERVAL_US (100 * 1000)
Brian J. Nemec1118a582020-02-04 18:26:02 -0800178
Anton Staafb2647882014-09-17 15:13:43 -0700179/*
180 * This timeout is so large because the Raiden SPI timeout is 800ms.
181 */
Edward O'Callaghanf1e6ef52020-03-03 13:57:15 +1100182#define TRANSFER_TIMEOUT_MS (200 + 800)
Anton Staafb2647882014-09-17 15:13:43 -0700183
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700184struct raiden_debug_spi_data {
185 struct usb_device *dev;
186 uint8_t in_ep;
187 uint8_t out_ep;
188};
Anton Staafb2647882014-09-17 15:13:43 -0700189
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700190/*
191 * Version 1 protocol specific attributes
192 */
Anton Staafb2647882014-09-17 15:13:43 -0700193
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700194struct usb_spi_command_v1 {
195 uint8_t write_count;
196 /* UINT8_MAX indicates full duplex mode on compliant devices. */
197 uint8_t read_count;
198 uint8_t data[PAYLOAD_SIZE_V1];
199} __attribute__((packed));
200
201struct usb_spi_response_v1 {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800202 uint16_t status_code;
Brian J. Nemecb746f822020-07-22 02:57:56 -0700203 uint8_t data[PAYLOAD_SIZE_V1];
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700204} __attribute__((packed));
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800205
Brian J. Nemec1889a032020-07-22 03:16:45 -0700206struct usb_spi_transmit_ctx {
207 /* Buffer we are reading data from. */
208 const uint8_t *buffer;
209 /* Number of bytes in the transfer. */
210 size_t transmit_size;
211 /* Number of bytes transferred. */
212 size_t transmit_index;
213};
214
215struct usb_spi_receive_ctx {
216 /* Buffer we are writing data into. */
217 uint8_t *buffer;
218 /* Number of bytes in the transfer. */
219 size_t receive_size;
220 /* Number of bytes transferred. */
221 size_t receive_index;
222};
223
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800224/*
225 * This function will return true when an error code can potentially recover
226 * if we attempt to write SPI data to the device or read from it. We know
227 * that some conditions are not recoverable in the current state so allows us
228 * to bypass the retry logic and terminate early.
229 */
230static bool retry_recovery(int error_code)
231{
232 if (error_code < 0x10000) {
Brian J. Nemec1a61f722020-05-04 20:58:06 -0700233 /*
234 * Handle error codes returned from the device. USB_SPI_TIMEOUT,
235 * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed
236 * during transfer errors to the device and can be recovered.
237 */
238 if (USB_SPI_READ_COUNT_INVALID <= error_code &&
239 error_code <= USB_SPI_DISABLED) {
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800240 return false;
241 }
242 } else if (usb_device_is_libusb_error(error_code)) {
243 /* Handle error codes returned from libusb. */
244 if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) {
245 return false;
246 }
247 }
248 return true;
249}
250
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700251static const struct raiden_debug_spi_data *
252 get_raiden_data_from_context(const struct flashctx *flash)
253{
254 return (const struct raiden_debug_spi_data *)flash->mst->spi.data;
255}
256
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700257/*
258 * Version 1 Protocol: Responsible for constructing the packet to start
259 * a USB SPI transfer. Write and read counts and payloads to write from
260 * the write_buffer are transmitted to the device.
261 *
Brian J. Nemec1889a032020-07-22 03:16:45 -0700262 * @param flash Flash context storing SPI capabilities and USB device
263 * information.
264 * @param write Write context of data to transmit and write payload.
265 * @param read Read context of data to receive and read buffer.
266 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700267 * @returns Returns status code with 0 on success.
268 */
269static int write_command_v1(const struct flashctx *flash,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700270 struct usb_spi_transmit_ctx *write,
271 struct usb_spi_receive_ctx *read)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800272{
273
274 int transferred;
275 int ret;
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700276 struct usb_spi_command_v1 command_packet;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700277 const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800278
Brian J. Nemec1889a032020-07-22 03:16:45 -0700279 command_packet.write_count = write->transmit_size;
280 command_packet.read_count = read->receive_size;
Anton Staafb2647882014-09-17 15:13:43 -0700281
Brian J. Nemec1889a032020-07-22 03:16:45 -0700282 memcpy(command_packet.data, write->buffer, write->transmit_size);
Anton Staafb2647882014-09-17 15:13:43 -0700283
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700284 ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
285 ctx_data->out_ep,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700286 (void*)&command_packet,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700287 write->transmit_size + PACKET_HEADER_SIZE,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700288 &transferred,
289 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100290 if (ret != 0) {
291 msg_perr("Raiden: OUT transfer failed\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700292 " write_count = %zu\n"
293 " read_count = %zu\n",
294 write->transmit_size, read->receive_size);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100295 return ret;
296 }
Anton Staafb2647882014-09-17 15:13:43 -0700297
Brian J. Nemec1889a032020-07-22 03:16:45 -0700298 if ((unsigned) transferred != write->transmit_size + PACKET_HEADER_SIZE) {
299 msg_perr("Raiden: Write failure (wrote %d, expected %zu)\n",
300 transferred, write->transmit_size + PACKET_HEADER_SIZE);
Anton Staafb2647882014-09-17 15:13:43 -0700301 return 0x10001;
302 }
303
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800304 return 0;
305}
306
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700307/*
308 * Version 1 Protocol: Responsible for reading the response of the USB SPI
309 * transfer. Status codes from the transfer and any read payload are copied
310 * to the read_buffer.
311 *
Brian J. Nemec1889a032020-07-22 03:16:45 -0700312 * @param flash Flash context storing SPI capabilities and USB device
313 * information.
314 * @param write Write context of data to transmit and write payload.
315 * @param read Read context of data to receive and read buffer.
316 *
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700317 * @returns Returns status code with 0 on success.
318 */
319static int read_response_v1(const struct flashctx *flash,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700320 struct usb_spi_transmit_ctx *write,
321 struct usb_spi_receive_ctx *read)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800322{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800323 int transferred;
324 int ret;
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700325 struct usb_spi_response_v1 response_packet;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700326 const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800327
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700328 ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
329 ctx_data->in_ep,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700330 (void*)&response_packet,
Brian J. Nemec1889a032020-07-22 03:16:45 -0700331 read->receive_size + PACKET_HEADER_SIZE,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700332 &transferred,
333 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100334 if (ret != 0) {
335 msg_perr("Raiden: IN transfer failed\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700336 " write_count = %zu\n"
337 " read_count = %zu\n",
338 write->transmit_size, read->receive_size);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100339 return ret;
340 }
Anton Staafb2647882014-09-17 15:13:43 -0700341
Brian J. Nemec1889a032020-07-22 03:16:45 -0700342 if ((unsigned) transferred != read->receive_size + PACKET_HEADER_SIZE) {
343 msg_perr("Raiden: Read failure (read %d, expected %zu)\n",
344 transferred, read->receive_size + PACKET_HEADER_SIZE);
Anton Staafb2647882014-09-17 15:13:43 -0700345 return 0x10002;
346 }
347
Brian J. Nemec1889a032020-07-22 03:16:45 -0700348 memcpy(read->buffer, response_packet.data, read->receive_size);
Anton Staafb2647882014-09-17 15:13:43 -0700349
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800350 return response_packet.status_code;
351}
352
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700353/*
354 * Version 1 Protocol: Sets up a USB SPI transfer, transmits data to the device,
355 * reads the status code and any payload from the device. This will also handle
356 * recovery if an error has occurred.
357 *
358 * @param flash Flash context storing SPI capabilities and USB device
359 * information.
360 * @param write_count Number of bytes to write
361 * @param read_count Number of bytes to read
362 * @param write_buffer Address of write buffer
363 * @param read_buffer Address of buffer to store read data
364 *
365 * @returns Returns status code with 0 on success.
366 */
367static int send_command_v1(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700368 unsigned int write_count,
369 unsigned int read_count,
370 const unsigned char *write_buffer,
371 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800372{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800373 int status = -1;
374
Brian J. Nemec1889a032020-07-22 03:16:45 -0700375 struct usb_spi_transmit_ctx write_ctx = {
376 .buffer = write_buffer,
377 .transmit_size = write_count
378 };
379 struct usb_spi_receive_ctx read_ctx = {
380 .buffer = read_buffer,
381 .receive_size = read_count
382 };
383
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700384 if (write_count > PAYLOAD_SIZE_V1) {
385 msg_perr("Raiden: Invalid write count\n"
386 " write count = %u\n"
387 " max write = %d\n",
388 write_count, PAYLOAD_SIZE_V1);
389 return SPI_INVALID_LENGTH;
390 }
391
392 if (read_count > PAYLOAD_SIZE_V1) {
393 msg_perr("Raiden: Invalid read count\n"
394 " read count = %d\n"
395 " max read = %d\n",
396 read_count, PAYLOAD_SIZE_V1);
397 return SPI_INVALID_LENGTH;
398 }
399
400 for (unsigned int write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS;
Brian J. Nemec1118a582020-02-04 18:26:02 -0800401 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800402
Brian J. Nemec1889a032020-07-22 03:16:45 -0700403
404 status = write_command_v1(flash, &write_ctx, &read_ctx);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800405
406 if (status) {
407 /* Write operation failed. */
408 msg_perr("Raiden: Write command failed\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700409 " write count = %u\n"
410 " read count = %u\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700411 " transmitted bytes = %zu\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700412 " write attempt = %u\n"
413 " status = 0x%05x\n",
Brian J. Nemec1889a032020-07-22 03:16:45 -0700414
415 write_count, read_count, write_ctx.transmit_index,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700416 write_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800417 if (!retry_recovery(status)) {
418 /* Reattempting will not result in a recovery. */
419 return status;
420 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700421 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800422 continue;
423 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700424 for (unsigned int read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS;
425 read_attempt++) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800426
Brian J. Nemec1889a032020-07-22 03:16:45 -0700427 status = read_response_v1(flash, &write_ctx, &read_ctx);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800428
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700429 if (status) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800430 /* Read operation failed. */
431 msg_perr("Raiden: Read response failed\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700432 " write count = %u\n"
433 " read count = %u\n"
Brian J. Nemec1889a032020-07-22 03:16:45 -0700434 " received bytes = %zu\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700435 " write attempt = %u\n"
436 " read attempt = %u\n"
437 " status = 0x%05x\n",
Brian J. Nemec1889a032020-07-22 03:16:45 -0700438 write_count, read_count, read_ctx.receive_index,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700439 write_attempt + 1, read_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800440 if (!retry_recovery(status)) {
441 /* Reattempting will not result in a recovery. */
442 return status;
443 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700444 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800445 } else {
446 /* We were successful at performing the SPI transfer. */
447 return status;
448 }
449 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800450 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800451 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700452}
453
454/*
455 * Unfortunately there doesn't seem to be a way to specify the maximum number
456 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700457 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700458 * of command for the flash device, resulting in a 62 byte packet, that we then
459 * add two bytes to in either direction, making our way up to the 64 byte
460 * maximum USB packet size for the device.
461 *
462 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700463 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700464 */
Brian J. Nemecb746f822020-07-22 02:57:56 -0700465#define MAX_DATA_SIZE (PAYLOAD_SIZE_V1 - JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700466
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700467static struct spi_master spi_master_raiden_debug = {
Brian J. Nemecb746f822020-07-22 02:57:56 -0700468 .features = SPI_MASTER_4BA,
469 .max_data_read = MAX_DATA_SIZE,
470 .max_data_write = MAX_DATA_SIZE,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700471 .command = send_command_v1,
Brian J. Nemecb746f822020-07-22 02:57:56 -0700472 .multicommand = default_spi_send_multicommand,
473 .read = default_spi_read,
474 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700475};
476
Anton Staaf5614e252015-03-24 14:33:33 -0700477static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800478 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700479{
Anton Staaf5614e252015-03-24 14:33:33 -0700480 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
481 direction) &&
482 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
483 LIBUSB_TRANSFER_TYPE_BULK));
484}
Anton Staafd27536d2014-09-30 08:10:17 -0700485
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700486static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700487{
488 int i;
489 int in_count = 0;
490 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700491
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100492 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700493 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100494 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700495
Anton Staaf5614e252015-03-24 14:33:33 -0700496 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
497 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100498 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700499 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
500 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100501 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700502 }
503 }
504
505 if (in_count != 1 || out_count != 1) {
506 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
507 " found %d IN and %d OUT endpoints\n",
508 in_count,
509 out_count);
510 return 1;
511 }
512
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100513 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
514 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700515
516 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700517}
518
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700519static int raiden_debug_spi_shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700520{
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700521 struct raiden_debug_spi_data * ctx_data =
522 (struct raiden_debug_spi_data *)data;
523
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100524 int ret = LIBUSB(libusb_control_transfer(
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700525 ctx_data->dev->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700526 LIBUSB_ENDPOINT_OUT |
527 LIBUSB_REQUEST_TYPE_VENDOR |
528 LIBUSB_RECIPIENT_INTERFACE,
529 RAIDEN_DEBUG_SPI_REQ_DISABLE,
530 0,
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700531 ctx_data->dev->interface_descriptor->bInterfaceNumber,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700532 NULL,
533 0,
534 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100535 if (ret != 0) {
536 msg_perr("Raiden: Failed to disable SPI bridge\n");
537 return ret;
538 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700539
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700540 usb_device_free(ctx_data->dev);
Anton Staaf4589cd12015-03-23 13:36:44 -0700541 libusb_exit(NULL);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700542 free(ctx_data);
Anton Staaf4589cd12015-03-23 13:36:44 -0700543
544 return 0;
545}
546
Edward O'Callaghandda1e542020-02-03 12:45:01 +1100547static int get_target(void)
Anton Staafb2647882014-09-17 15:13:43 -0700548{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700549 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700550
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100551 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700552 if (target_str) {
553 if (!strcasecmp(target_str, "ap"))
554 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
555 else if (!strcasecmp(target_str, "ec"))
556 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
557 else {
558 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100559 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700560 }
561 }
David Hendricks98b3c572016-11-30 01:50:08 +0000562 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700563
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100564 return request_enable;
565}
566
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700567static void free_dev_list(struct usb_device **dev_lst)
568{
569 struct usb_device *dev = *dev_lst;
570 /* free devices we don't care about */
571 dev = dev->next;
572 while (dev)
573 dev = usb_device_free(dev);
574}
575
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100576int raiden_debug_spi_init(void)
577{
578 struct usb_match match;
579 char *serial = extract_programmer_param("serial");
580 struct usb_device *current;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700581 struct usb_device *device = NULL;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100582 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100583 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100584
585 int request_enable = get_target();
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700586 if (request_enable < 0) {
587 free(serial);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100588 return 1;
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700589 }
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100590
Anton Staaf5614e252015-03-24 14:33:33 -0700591 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700592
Anton Staaf5614e252015-03-24 14:33:33 -0700593 usb_match_value_default(&match.vid, GOOGLE_VID);
594 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
595 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
Brian J. Nemecb746f822020-07-22 02:57:56 -0700596 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL_V1);
Anton Staafb2647882014-09-17 15:13:43 -0700597
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100598 ret = LIBUSB(libusb_init(NULL));
599 if (ret != 0) {
600 msg_perr("Raiden: libusb_init failed\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700601 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100602 return ret;
603 }
Anton Staaf5614e252015-03-24 14:33:33 -0700604
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100605 ret = usb_device_find(&match, &current);
606 if (ret != 0) {
607 msg_perr("Raiden: Failed to find devices\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700608 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100609 return ret;
610 }
Anton Staaf5614e252015-03-24 14:33:33 -0700611
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700612 uint8_t in_endpoint = 0;
613 uint8_t out_endpoint = 0;
David Hendricks5c79a492016-06-14 20:56:36 -0700614 while (current) {
615 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700616
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100617 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700618 msg_pdbg("Raiden: Failed to find valid endpoints on device");
619 usb_device_show(" ", current);
620 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700621 }
Anton Staaf5614e252015-03-24 14:33:33 -0700622
David Hendricks5c79a492016-06-14 20:56:36 -0700623 if (usb_device_claim(device)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700624 msg_pdbg("Raiden: Failed to claim USB device");
625 usb_device_show(" ", current);
626 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700627 }
Anton Staaf5614e252015-03-24 14:33:33 -0700628
David Hendricks5c79a492016-06-14 20:56:36 -0700629 if (!serial) {
630 found = 1;
631 goto loop_end;
632 } else {
633 unsigned char dev_serial[32];
634 struct libusb_device_descriptor descriptor;
635 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700636
David Hendricks5c79a492016-06-14 20:56:36 -0700637 memset(dev_serial, 0, sizeof(dev_serial));
638
639 if (libusb_get_device_descriptor(device->device, &descriptor)) {
640 msg_pdbg("USB: Failed to get device descriptor.\n");
641 goto loop_end;
642 }
643
644 rc = libusb_get_string_descriptor_ascii(device->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700645 descriptor.iSerialNumber,
646 dev_serial,
647 sizeof(dev_serial));
David Hendricks5c79a492016-06-14 20:56:36 -0700648 if (rc < 0) {
649 LIBUSB(rc);
650 } else {
651 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700652 msg_pdbg("Raiden: Serial number %s did not match device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700653 usb_device_show(" ", current);
654 } else {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700655 msg_pinfo("Raiden: Serial number %s matched device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700656 usb_device_show(" ", current);
657 found = 1;
658 }
659 }
660 }
661
662loop_end:
663 if (found)
664 break;
665 else
666 current = usb_device_free(current);
667 }
668
669 if (!device || !found) {
670 msg_perr("Raiden: No usable device found.\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700671 free(serial);
David Hendricks98b3c572016-11-30 01:50:08 +0000672 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700673 }
674
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700675 free_dev_list(&current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700676
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100677 ret = LIBUSB(libusb_control_transfer(
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700678 device->handle,
679 LIBUSB_ENDPOINT_OUT |
680 LIBUSB_REQUEST_TYPE_VENDOR |
681 LIBUSB_RECIPIENT_INTERFACE,
682 request_enable,
683 0,
684 device->interface_descriptor->bInterfaceNumber,
685 NULL,
686 0,
687 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100688 if (ret != 0) {
689 msg_perr("Raiden: Failed to enable SPI bridge\n");
690 return ret;
691 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700692
Keith Short8453b552020-02-03 18:10:14 -0700693 /*
694 * Allow for power to settle on the AP and EC flash devices.
695 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
696 * can require up to 10 ms from power on to the first write.
697 */
698 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
699 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
700 usleep(50 * 1000);
701
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700702 struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data));
703 if (!data) {
704 msg_perr("Unable to allocate space for extra SPI master data.\n");
705 return SPI_GENERIC_ERROR;
706 }
707
708 data->dev = device;
709 data->in_ep = in_endpoint;
710 data->out_ep = out_endpoint;
711
712 spi_master_raiden_debug.data = data;
713
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100714 register_spi_master(&spi_master_raiden_debug);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700715 register_shutdown(raiden_debug_spi_shutdown, data);
Anton Staafb2647882014-09-17 15:13:43 -0700716
David Hendricks98b3c572016-11-30 01:50:08 +0000717 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700718}