blob: 6f1675dbe9cd4a1b631ea7c24ae90f42a08218bd [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. Nemeccea6bec2020-02-25 14:12:46 -0800206/*
207 * This function will return true when an error code can potentially recover
208 * if we attempt to write SPI data to the device or read from it. We know
209 * that some conditions are not recoverable in the current state so allows us
210 * to bypass the retry logic and terminate early.
211 */
212static bool retry_recovery(int error_code)
213{
214 if (error_code < 0x10000) {
Brian J. Nemec1a61f722020-05-04 20:58:06 -0700215 /*
216 * Handle error codes returned from the device. USB_SPI_TIMEOUT,
217 * USB_SPI_BUSY, and USB_SPI_WRITE_COUNT_INVALID have been observed
218 * during transfer errors to the device and can be recovered.
219 */
220 if (USB_SPI_READ_COUNT_INVALID <= error_code &&
221 error_code <= USB_SPI_DISABLED) {
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800222 return false;
223 }
224 } else if (usb_device_is_libusb_error(error_code)) {
225 /* Handle error codes returned from libusb. */
226 if (error_code == LIBUSB_ERROR(LIBUSB_ERROR_NO_DEVICE)) {
227 return false;
228 }
229 }
230 return true;
231}
232
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700233static const struct raiden_debug_spi_data *
234 get_raiden_data_from_context(const struct flashctx *flash)
235{
236 return (const struct raiden_debug_spi_data *)flash->mst->spi.data;
237}
238
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700239/*
240 * Version 1 Protocol: Responsible for constructing the packet to start
241 * a USB SPI transfer. Write and read counts and payloads to write from
242 * the write_buffer are transmitted to the device.
243 *
244 * @returns Returns status code with 0 on success.
245 */
246static int write_command_v1(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700247 unsigned int write_count,
248 unsigned int read_count,
249 const unsigned char *write_buffer,
250 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800251{
252
253 int transferred;
254 int ret;
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700255 struct usb_spi_command_v1 command_packet;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700256 const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800257
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800258 command_packet.write_count = write_count;
259 command_packet.read_count = read_count;
Anton Staafb2647882014-09-17 15:13:43 -0700260
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800261 memcpy(command_packet.data, write_buffer, write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700262
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700263 ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
264 ctx_data->out_ep,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700265 (void*)&command_packet,
266 write_count + PACKET_HEADER_SIZE,
267 &transferred,
268 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100269 if (ret != 0) {
270 msg_perr("Raiden: OUT transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800271 " write_count = %d\n"
272 " read_count = %d\n",
273 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100274 return ret;
275 }
Anton Staafb2647882014-09-17 15:13:43 -0700276
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700277 if ((unsigned) transferred != write_count + PACKET_HEADER_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700278 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
279 transferred, write_count + PACKET_HEADER_SIZE);
280 return 0x10001;
281 }
282
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800283 return 0;
284}
285
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700286/*
287 * Version 1 Protocol: Responsible for reading the response of the USB SPI
288 * transfer. Status codes from the transfer and any read payload are copied
289 * to the read_buffer.
290 *
291 * @returns Returns status code with 0 on success.
292 */
293static int read_response_v1(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700294 unsigned int write_count,
295 unsigned int read_count,
296 const unsigned char *write_buffer,
297 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800298{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800299 int transferred;
300 int ret;
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700301 struct usb_spi_response_v1 response_packet;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700302 const struct raiden_debug_spi_data * ctx_data = get_raiden_data_from_context(flash);
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800303
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700304 ret = LIBUSB(libusb_bulk_transfer(ctx_data->dev->handle,
305 ctx_data->in_ep,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700306 (void*)&response_packet,
307 read_count + PACKET_HEADER_SIZE,
308 &transferred,
309 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100310 if (ret != 0) {
311 msg_perr("Raiden: IN transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800312 " write_count = %d\n"
313 " read_count = %d\n",
314 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100315 return ret;
316 }
Anton Staafb2647882014-09-17 15:13:43 -0700317
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700318 if ((unsigned) transferred != read_count + PACKET_HEADER_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700319 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700320 transferred, read_count + PACKET_HEADER_SIZE);
Anton Staafb2647882014-09-17 15:13:43 -0700321 return 0x10002;
322 }
323
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800324 memcpy(read_buffer, response_packet.data, read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700325
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800326 return response_packet.status_code;
327}
328
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700329/*
330 * Version 1 Protocol: Sets up a USB SPI transfer, transmits data to the device,
331 * reads the status code and any payload from the device. This will also handle
332 * recovery if an error has occurred.
333 *
334 * @param flash Flash context storing SPI capabilities and USB device
335 * information.
336 * @param write_count Number of bytes to write
337 * @param read_count Number of bytes to read
338 * @param write_buffer Address of write buffer
339 * @param read_buffer Address of buffer to store read data
340 *
341 * @returns Returns status code with 0 on success.
342 */
343static int send_command_v1(const struct flashctx *flash,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700344 unsigned int write_count,
345 unsigned int read_count,
346 const unsigned char *write_buffer,
347 unsigned char *read_buffer)
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800348{
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800349 int status = -1;
350
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700351 if (write_count > PAYLOAD_SIZE_V1) {
352 msg_perr("Raiden: Invalid write count\n"
353 " write count = %u\n"
354 " max write = %d\n",
355 write_count, PAYLOAD_SIZE_V1);
356 return SPI_INVALID_LENGTH;
357 }
358
359 if (read_count > PAYLOAD_SIZE_V1) {
360 msg_perr("Raiden: Invalid read count\n"
361 " read count = %d\n"
362 " max read = %d\n",
363 read_count, PAYLOAD_SIZE_V1);
364 return SPI_INVALID_LENGTH;
365 }
366
367 for (unsigned int write_attempt = 0; write_attempt < WRITE_RETRY_ATTEMPTS;
Brian J. Nemec1118a582020-02-04 18:26:02 -0800368 write_attempt++) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800369
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700370 status = write_command_v1(flash, write_count, read_count,
Brian J. Nemec1118a582020-02-04 18:26:02 -0800371 write_buffer, read_buffer);
372
373 if (status) {
374 /* Write operation failed. */
375 msg_perr("Raiden: Write command failed\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700376 " write count = %u\n"
377 " read count = %u\n"
378 " write attempt = %u\n"
379 " status = 0x%05x\n",
380 write_count, read_count,
381 write_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800382 if (!retry_recovery(status)) {
383 /* Reattempting will not result in a recovery. */
384 return status;
385 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700386 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800387 continue;
388 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700389 for (unsigned int read_attempt = 0; read_attempt < READ_RETRY_ATTEMPTS;
390 read_attempt++) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800391
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700392 status = read_response_v1(flash, write_count, read_count,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700393 write_buffer, read_buffer);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800394
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700395 if (status) {
Brian J. Nemec1118a582020-02-04 18:26:02 -0800396 /* Read operation failed. */
397 msg_perr("Raiden: Read response failed\n"
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700398 " write count = %u\n"
399 " read count = %u\n"
400 " write attempt = %u\n"
401 " read attempt = %u\n"
402 " status = 0x%05x\n",
403 write_count, read_count,
404 write_attempt + 1, read_attempt + 1, status);
Brian J. Nemeccea6bec2020-02-25 14:12:46 -0800405 if (!retry_recovery(status)) {
406 /* Reattempting will not result in a recovery. */
407 return status;
408 }
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700409 programmer_delay(RETRY_INTERVAL_US);
Brian J. Nemec1118a582020-02-04 18:26:02 -0800410 } else {
411 /* We were successful at performing the SPI transfer. */
412 return status;
413 }
414 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800415 }
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800416 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700417}
418
419/*
420 * Unfortunately there doesn't seem to be a way to specify the maximum number
421 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700422 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700423 * of command for the flash device, resulting in a 62 byte packet, that we then
424 * add two bytes to in either direction, making our way up to the 64 byte
425 * maximum USB packet size for the device.
426 *
427 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700428 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700429 */
Brian J. Nemecb746f822020-07-22 02:57:56 -0700430#define MAX_DATA_SIZE (PAYLOAD_SIZE_V1 - JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700431
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700432static struct spi_master spi_master_raiden_debug = {
Brian J. Nemecb746f822020-07-22 02:57:56 -0700433 .features = SPI_MASTER_4BA,
434 .max_data_read = MAX_DATA_SIZE,
435 .max_data_write = MAX_DATA_SIZE,
Brian J. Nemecc5d69462020-07-22 03:12:15 -0700436 .command = send_command_v1,
Brian J. Nemecb746f822020-07-22 02:57:56 -0700437 .multicommand = default_spi_send_multicommand,
438 .read = default_spi_read,
439 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700440};
441
Anton Staaf5614e252015-03-24 14:33:33 -0700442static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800443 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700444{
Anton Staaf5614e252015-03-24 14:33:33 -0700445 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
446 direction) &&
447 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
448 LIBUSB_TRANSFER_TYPE_BULK));
449}
Anton Staafd27536d2014-09-30 08:10:17 -0700450
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700451static int find_endpoints(struct usb_device *dev, uint8_t *in_ep, uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700452{
453 int i;
454 int in_count = 0;
455 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700456
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100457 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700458 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100459 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700460
Anton Staaf5614e252015-03-24 14:33:33 -0700461 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
462 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100463 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700464 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
465 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100466 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700467 }
468 }
469
470 if (in_count != 1 || out_count != 1) {
471 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
472 " found %d IN and %d OUT endpoints\n",
473 in_count,
474 out_count);
475 return 1;
476 }
477
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100478 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
479 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700480
481 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700482}
483
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700484static int raiden_debug_spi_shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700485{
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700486 struct raiden_debug_spi_data * ctx_data =
487 (struct raiden_debug_spi_data *)data;
488
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100489 int ret = LIBUSB(libusb_control_transfer(
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700490 ctx_data->dev->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700491 LIBUSB_ENDPOINT_OUT |
492 LIBUSB_REQUEST_TYPE_VENDOR |
493 LIBUSB_RECIPIENT_INTERFACE,
494 RAIDEN_DEBUG_SPI_REQ_DISABLE,
495 0,
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700496 ctx_data->dev->interface_descriptor->bInterfaceNumber,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700497 NULL,
498 0,
499 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100500 if (ret != 0) {
501 msg_perr("Raiden: Failed to disable SPI bridge\n");
502 return ret;
503 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700504
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700505 usb_device_free(ctx_data->dev);
Anton Staaf4589cd12015-03-23 13:36:44 -0700506 libusb_exit(NULL);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700507 free(ctx_data);
Anton Staaf4589cd12015-03-23 13:36:44 -0700508
509 return 0;
510}
511
Edward O'Callaghandda1e542020-02-03 12:45:01 +1100512static int get_target(void)
Anton Staafb2647882014-09-17 15:13:43 -0700513{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700514 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700515
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100516 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700517 if (target_str) {
518 if (!strcasecmp(target_str, "ap"))
519 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
520 else if (!strcasecmp(target_str, "ec"))
521 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
522 else {
523 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100524 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700525 }
526 }
David Hendricks98b3c572016-11-30 01:50:08 +0000527 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700528
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100529 return request_enable;
530}
531
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700532static void free_dev_list(struct usb_device **dev_lst)
533{
534 struct usb_device *dev = *dev_lst;
535 /* free devices we don't care about */
536 dev = dev->next;
537 while (dev)
538 dev = usb_device_free(dev);
539}
540
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100541int raiden_debug_spi_init(void)
542{
543 struct usb_match match;
544 char *serial = extract_programmer_param("serial");
545 struct usb_device *current;
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700546 struct usb_device *device = NULL;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100547 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100548 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100549
550 int request_enable = get_target();
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700551 if (request_enable < 0) {
552 free(serial);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100553 return 1;
Brian J. Nemec7b5ad792020-07-23 03:35:54 -0700554 }
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100555
Anton Staaf5614e252015-03-24 14:33:33 -0700556 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700557
Anton Staaf5614e252015-03-24 14:33:33 -0700558 usb_match_value_default(&match.vid, GOOGLE_VID);
559 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
560 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
Brian J. Nemecb746f822020-07-22 02:57:56 -0700561 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL_V1);
Anton Staafb2647882014-09-17 15:13:43 -0700562
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100563 ret = LIBUSB(libusb_init(NULL));
564 if (ret != 0) {
565 msg_perr("Raiden: libusb_init failed\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700566 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100567 return ret;
568 }
Anton Staaf5614e252015-03-24 14:33:33 -0700569
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100570 ret = usb_device_find(&match, &current);
571 if (ret != 0) {
572 msg_perr("Raiden: Failed to find devices\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700573 free(serial);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100574 return ret;
575 }
Anton Staaf5614e252015-03-24 14:33:33 -0700576
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700577 uint8_t in_endpoint = 0;
578 uint8_t out_endpoint = 0;
David Hendricks5c79a492016-06-14 20:56:36 -0700579 while (current) {
580 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700581
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100582 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700583 msg_pdbg("Raiden: Failed to find valid endpoints on device");
584 usb_device_show(" ", current);
585 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700586 }
Anton Staaf5614e252015-03-24 14:33:33 -0700587
David Hendricks5c79a492016-06-14 20:56:36 -0700588 if (usb_device_claim(device)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700589 msg_pdbg("Raiden: Failed to claim USB device");
590 usb_device_show(" ", current);
591 goto loop_end;
David Hendricks5c79a492016-06-14 20:56:36 -0700592 }
Anton Staaf5614e252015-03-24 14:33:33 -0700593
David Hendricks5c79a492016-06-14 20:56:36 -0700594 if (!serial) {
595 found = 1;
596 goto loop_end;
597 } else {
598 unsigned char dev_serial[32];
599 struct libusb_device_descriptor descriptor;
600 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700601
David Hendricks5c79a492016-06-14 20:56:36 -0700602 memset(dev_serial, 0, sizeof(dev_serial));
603
604 if (libusb_get_device_descriptor(device->device, &descriptor)) {
605 msg_pdbg("USB: Failed to get device descriptor.\n");
606 goto loop_end;
607 }
608
609 rc = libusb_get_string_descriptor_ascii(device->handle,
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700610 descriptor.iSerialNumber,
611 dev_serial,
612 sizeof(dev_serial));
David Hendricks5c79a492016-06-14 20:56:36 -0700613 if (rc < 0) {
614 LIBUSB(rc);
615 } else {
616 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700617 msg_pdbg("Raiden: Serial number %s did not match device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700618 usb_device_show(" ", current);
619 } else {
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700620 msg_pinfo("Raiden: Serial number %s matched device", serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700621 usb_device_show(" ", current);
622 found = 1;
623 }
624 }
625 }
626
627loop_end:
628 if (found)
629 break;
630 else
631 current = usb_device_free(current);
632 }
633
634 if (!device || !found) {
635 msg_perr("Raiden: No usable device found.\n");
Brian J. Nemeca88d96e2020-07-23 03:22:57 -0700636 free(serial);
David Hendricks98b3c572016-11-30 01:50:08 +0000637 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700638 }
639
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700640 free_dev_list(&current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700641
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100642 ret = LIBUSB(libusb_control_transfer(
Brian J. Nemecec15cc42020-07-22 02:30:55 -0700643 device->handle,
644 LIBUSB_ENDPOINT_OUT |
645 LIBUSB_REQUEST_TYPE_VENDOR |
646 LIBUSB_RECIPIENT_INTERFACE,
647 request_enable,
648 0,
649 device->interface_descriptor->bInterfaceNumber,
650 NULL,
651 0,
652 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100653 if (ret != 0) {
654 msg_perr("Raiden: Failed to enable SPI bridge\n");
655 return ret;
656 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700657
Keith Short8453b552020-02-03 18:10:14 -0700658 /*
659 * Allow for power to settle on the AP and EC flash devices.
660 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
661 * can require up to 10 ms from power on to the first write.
662 */
663 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
664 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
665 usleep(50 * 1000);
666
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700667 struct raiden_debug_spi_data *data = calloc(1, sizeof(struct raiden_debug_spi_data));
668 if (!data) {
669 msg_perr("Unable to allocate space for extra SPI master data.\n");
670 return SPI_GENERIC_ERROR;
671 }
672
673 data->dev = device;
674 data->in_ep = in_endpoint;
675 data->out_ep = out_endpoint;
676
677 spi_master_raiden_debug.data = data;
678
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100679 register_spi_master(&spi_master_raiden_debug);
Brian J. Nemec721ac2c2020-07-23 03:30:53 -0700680 register_shutdown(raiden_debug_spi_shutdown, data);
Anton Staafb2647882014-09-17 15:13:43 -0700681
David Hendricks98b3c572016-11-30 01:50:08 +0000682 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700683}