Brian Nemec would like Brian Nemec to review this change.

View Change

raiden_debug_spi.c: Adds support for USB SPI protocol V2

Adds support for the USB SPI V2 protocol and documentation of it.
The protocol version number uses the bInterfaceProtocol field in
USB to identify which device to use, this enables us to support
both V1 and V2 with the same host.

The USB SPI V2 protocol adds the ability to perform multi-packet
USB SPI transfers. This results in fewer USB messages exchanged
and faster flashing speeds.

BUG=b:139058552
BRANCH=none
TEST=Manual testing of ServoMicro and Flashrom when performing
reads, writes, and verification of the EC firmware on Nami
with a USB SPI V1 protocol device
TEST=Manual testing of ServoMicro and Flashrom when performing
reads, writes, and verification of the EC firmware on Nami
with a USB SPI V2 protocol device
TEST=Builds

Signed-off-by: Brian J. Nemec <bnemec@chromium.com>
Change-Id: Ie356c63b521c0cc11a4946ffac128ec7139f0bec
---
M raiden_debug_spi.c
1 file changed, 470 insertions(+), 2 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/33/41533/1
diff --git a/raiden_debug_spi.c b/raiden_debug_spi.c
index 6cb32b3..5e07967 100644
--- a/raiden_debug_spi.c
+++ b/raiden_debug_spi.c
@@ -48,7 +48,10 @@
* include/usb_spi.h
* common/usb_spi.c
*
- * bInterfaceProtocol determines which protocol is used by the USB SPI device.
+ * 2 Versions of the protocol exist in deployed devices:
+ * The raiden_debug_spi.c interface used will switch between them automatically
+ * depending on the protocol version reported in the bInterfaceProtocol
+ *
*
*
* USB SPI Version 1:
@@ -117,8 +120,191 @@
* 0x20001-0x20063 Lower bits store the positive value representation
* of the libusb_error enum. See the libusb documentation:
* http://libusb.sourceforge.net/api-1.0/group__misc.html
+ *
+ *
+ *
+ * USB SPI Version 2:
+ *
+ * USB SPI version 2 adds support for larger SPI transfers and reduces the
+ * number of USB packets transferred. This improves performance when
+ * writing or reading large chunks of memory from a device. A packet ID
+ * field is used to distinguish the different packet types. Additional
+ * packets have been included to query the device for it's configuration
+ * allowing the interface to be used on platforms with different SPI
+ * limitations. It includes validation and a packet to recover from the
+ * situations where USB packets are lost.
+ *
+ *
+ * Example: USB SPI request with 128 byte write and 0 byte read.
+ *
+ * Packet #1 Host to Device:
+ * packet id = PACKET_ID_V2_COMMAND
+ * write count = 128
+ * read count = 0
+ * payload = First 58 bytes from the write buffer,
+ * start is byte 0
+ *
+ * Packet #2 Host to Device:
+ * packet id = PACKET_ID_V2_COMMAND_CONTINUE
+ * data index = 58
+ * payload = next 60 bytes from the write buffer,
+ * starting at byte 58
+ *
+ * Packet #3 Host to Device:
+ * packet id = PACKET_ID_V2_COMMAND_CONTINUE
+ * data index = 118
+ * payload = next 10 bytes from the write buffer,
+ * starting at byte 118
+ *
+ * Packet #4 Device to Host:
+ * packet id = PACKET_ID_V2_RESPONSE
+ * status code = status code from device
+ * payload = 0 bytes
+ *
+ * Command Packet (Host to Device):
+ *
+ * Start of USB SPI command, containing the number of bytes to write and
+ * read and a payload of bytes to write. If the payload is unable to fit
+ * in one USB packet, it contains the first 58 bytes.
+ *
+ * +----------------+------------------+-----------------+------------------------+
+ * | packet id : 2B | write count : 2B | read count : 2B | write payload : <= 58B |
+ * +----------------+------------------+-----------------+------------------------+
+ *
+ * packet id: 2 byte enum defined by packet_id_type
+ * Valid values packet id = PACKET_ID_V2_COMMAND
+ *
+ * write count: 2 byte, zero based count of bytes to write
+ *
+ * read count: 2 byte, zero based count of bytes to read
+ *
+ * write payload: Up to 62 bytes of data to write to SPI, the total
+ * length of all TX packets must match write count.
+ * Due to data alignment constraints, this must be an
+ * even number of bytes unless this is the final packet.
+ *
+ *
+ * Response Packet (Device to Host):
+ *
+ * Start of the USB SPI response, containing the status code and
+ * any bytes read in the payload. If read buffer can not fit in
+ * a single packet, it represents the first 60 bytes.
+ *
+ * +----------------+------------------+-----------------------+
+ * | packet id : 2B | status code : 2B | read payload : <= 60B |
+ * +----------------+------------------+-----------------------+
+ *
+ * packet id: 2 byte enum defined by packet_id_type
+ * Valid values packet id = PACKET_ID_V2_RESPONSE
+ *
+ * status code: 2 byte status code
+ * 0x0000: Success
+ * 0x0001: SPI timeout
+ * 0x0002: Busy, try again
+ * This can happen if someone else has acquired the shared memory
+ * buffer that the SPI driver uses as /dev/null
+ * 0x0003: Write count invalid. The byte limit is platform specific
+ * and is set during the configure USB SPI response.
+ * 0x0004: Read count invalid. The byte limit is platform specific
+ * and is set during the configure USB SPI response.
+ * 0x0005: The SPI bridge is disabled.
+ * 0x0006: The RX continue packet's data index is invalid. This
+ * can indicate a USB transfer failure to the device.
+ * 0x0007: The RX endpoint has received more data than write count.
+ * This can indicate a USB transfer failure to the device.
+ * 0x0008: An unexpected packet arrived that the device could not
+ * process.
+ * 0x8000: Unknown error mask
+ * The bottom 15 bits will contain the bottom 14 bits from the EC
+ * error code.
+ *
+ * read payload: Up to 62 bytes of data read from SPI, the total
+ * length of all RX packets must match read count
+ * unless an error status was returned. Due to data
+ * alignment constraints, this must be a even number
+ * of bytes unless this is the final packet.
+ *
+ *
+ * Continue Packet (Bidirectional):
+ *
+ * Continuation packet for the writes and read buffers. Both packets
+ * follow the same format, a data index counts the number of bytes
+ * previously transferred in the USB SPI transfer and a payload of bytes.
+ *
+ * +----------------+-----------------+-------------------------------+
+ * | packet id : 2B | data index : 2B | write / read payload : <= 60B |
+ * +----------------+-----------------+-------------------------------+
+ *
+ * packet id: 2 byte enum defined by packet_id_type
+ * The packet id has 2 values depending on direction:
+ * packet id = PACKET_ID_V2_COMMAND_CONTINUE indicates the
+ * packet is being transmitted from the host to the device
+ * packet id = PACKET_ID_V2_RESPONSE_CONTINUE indicates the
+ * packet is being transmitted from the device to the host.
+ *
+ * data index: The data index indicates the number of bytes in the
+ * read or write buffers have already been transmitted.
+ * It is used to validate that no packets have been dropped
+ * and that the prior packets have been correctly decoded.
+ * this value corresponds to the position in the destination
+ * destination to start copying the payload into.
+ *
+ * read and write payload:
+ * Contains up to 60 bytes of payload data to transfer to
+ * the SPI write buffer or from the SPI read buffer.
+ *
+ *
+ * Command Get Configuration Packet (Host to Device):
+ *
+ * Query the device to request it's USB SPI configuration indicating
+ * the number of bytes it can write and read.
+ *
+ * +----------------+
+ * | packet id : 2B |
+ * +----------------+
+ *
+ * packet id: 2 byte enum PACKET_ID_V2_COMMAND_GET_USB_SPI_CONFIG
+ *
+ * Response Configuration Packet (Device to Host):
+ *
+ * Response packet form the device to report the maximum write and
+ * read size supported by the device.
+ *
+ * +----------------+----------------------+---------------------+
+ * | packet id : 2B | max write count : 2B | max read count : 2B |
+ * +----------------+----------------------+---------------------+
+ *
+ * packet id: 2 byte enum PACKET_ID_V2_COMMAND_GET_USB_SPI_CONFIG
+ *
+ * max write count : 2 byte count of the maximum number of bytes
+ * the device can write to SPI in one transaction.
+ *
+ * max read count : 2 byte count of the maximum number of bytes
+ * the device can read from SPI in one transaction.
+ *
+ * Command Restart Response Packet (Host to Device):
+ *
+ * Command to restart the response transfer from the device. This enables
+ * the host to recover from a lost packet when reading the response
+ * without restarting the SPI transfer.
+ *
+ * +----------------+
+ * | packet id : 2B |
+ * +----------------+
+ *
+ * packet id: 2 byte enum PACKET_ID_V2_COMMAND_GET_USB_SPI_CONFIG
+ *
+ * send_command return codes have the following format:
+ *
+ * 0x00000: Status code success.
+ * 0x00001-0x0FFFF: Error code returned by the USB SPI device.
+ * 0x10001-0x1FFFF: USB SPI Host error codes
+ * 0x20001-0x20063 Lower bits store the positive value representation
+ * of the libusb_error enum. See the libusb documentation:
+ * http://libusb.sourceforge.net/api-1.0/group__misc.html
*/

+
#include "programmer.h"
#include "spi.h"
#include "usb_device.h"
@@ -138,6 +324,7 @@
#define GOOGLE_VID (0x18D1)
#define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51)
#define GOOGLE_RAIDEN_SPI_PROTOCOL_V1 (0x01)
+#define GOOGLE_RAIDEN_SPI_PROTOCOL_V2 (0x02)

enum {
/* The host failed to transfer the data with no libusb error. */
@@ -185,6 +372,7 @@
*/
#define WRITE_RETRY_ATTEMPTS (3)
#define READ_RETRY_ATTEMPTS (3)
+#define GET_CONFIG_RETRY_ATTEMPTS (3)
#define RETRY_INTERVAL_US (100 * 1000)

/*
@@ -200,6 +388,17 @@
* All of the USB SPI packets have size equal to the max USB packet size of 64B
*/
#define PAYLOAD_SIZE_V1 (62)
+#define PACKET_HEADER_SIZE (2)
+
+#define HOST_VERSION (1)
+
+#define PAYLOAD_SIZE_V2_START (58)
+
+#define PAYLOAD_SIZE_V2_RESPONSE (60)
+
+#define PAYLOAD_SIZE_V2_CONTINUE (60)
+
+#define PAYLOAD_SIZE_V2_ERROR (60)

#define SPI_TRANSFER_V1_MAX (PAYLOAD_SIZE_V1)

@@ -224,10 +423,86 @@
usb_spi_response_v1_t response;
} __attribute__((packed)) usb_spi_packet_v1_t;

+/*
+ * Version 2 protocol specific attributes
+ */
+
+enum packet_id_type {
+
+ /* Request USB SPI configuration data from device. */
+ PACKET_ID_V2_COMMAND_GET_USB_SPI_CONFIG = 0,
+ /* USB SPI configuration data from device. */
+ PACKET_ID_V2_RESPONSE_USB_SPI_CONFIG = 1,
+
+ /* Start a USB SPI transfer and deliver first packet of data to write. */
+ PACKET_ID_V2_COMMAND = 2,
+ /* Additional packets containing write payload. */
+ PACKET_ID_V2_COMMAND_CONTINUE = 3,
+
+ /*
+ * Request the device restart the response enabling us to recover from
+ * packet loss without another SPI transfer.
+ */
+ PACKET_ID_V2_COMMAND_RESTART_RESPONSE = 4,
+
+ /* First packet of USB SPI response with status code and read payload. */
+ PACKET_ID_V2_RESPONSE = 5,
+ /* Additional packets containing read payload. */
+ PACKET_ID_V2_RESPONSE_CONTINUE = 6,
+};
+
+typedef struct {
+ int16_t packet_id;
+} __attribute__((packed)) usb_spi_command_get_configuration_v2_t;
+
+typedef struct {
+ int16_t packet_id;
+ uint16_t max_write_count;
+ uint16_t max_read_count;
+} __attribute__((packed)) usb_spi_response_configuration_v2_t;
+
+typedef struct {
+ int16_t packet_id;
+ int16_t write_count;
+ /* -1 Indicates readback all on halfduplex compliant devices. */
+ int16_t read_count;
+ uint8_t data[PAYLOAD_SIZE_V2_START];
+} __attribute__((packed)) usb_spi_command_v2_t;
+
+typedef struct {
+ int16_t packet_id;
+ uint16_t status_code;
+ uint8_t data[PAYLOAD_SIZE_V2_RESPONSE];
+} __attribute__((packed)) usb_spi_response_v2_t;
+
+typedef struct {
+ int16_t packet_id;
+ uint16_t data_index;
+ uint8_t data[PAYLOAD_SIZE_V2_CONTINUE];
+} __attribute__((packed)) usb_spi_continue_v2_t;
+
+typedef struct {
+ int16_t packet_id;
+} __attribute__((packed)) usb_spi_command_restart_response_v2_t;
+
+typedef struct {
+ union {
+ int16_t packet_id;
+ usb_spi_command_get_configuration_v2_t command_get_config;
+ usb_spi_response_configuration_v2_t response_config;
+ usb_spi_command_restart_response_v2_t restart_response;
+ usb_spi_command_v2_t command;
+ usb_spi_response_v2_t response;
+ usb_spi_continue_v2_t command_continue;
+ usb_spi_continue_v2_t response_continue;
+ };
+} __attribute__((packed)) usb_spi_packet_v2_t;
+
typedef struct {
union {
uint8_t bytes[MAX_USB_PACKET_SIZE];
usb_spi_packet_v1_t packet_v1;
+ usb_spi_packet_v2_t packet_v2;
};
/*
* By storing the number of bytes in the header and knowing that the
@@ -427,6 +702,177 @@
return status;
}

+/*
+ * Version 2 Protocol
+ */
+
+int get_spi_size_v2(struct raiden_debug_spi_data *ctx_data)
+{
+ int status;
+ usb_spi_packet_ctx_t response_config;
+
+ usb_spi_packet_ctx_t command_get_config = {
+ .header_size = sizeof(usb_spi_command_get_configuration_v2_t),
+ .packet_size = sizeof(usb_spi_command_get_configuration_v2_t),
+ .packet_v2.packet_id = PACKET_ID_V2_COMMAND_GET_USB_SPI_CONFIG
+ };
+
+ for (int config_attempt = 0; config_attempt < GET_CONFIG_RETRY_ATTEMPTS;
+ config_attempt++) {
+
+ status = transmit_packet(ctx_data, &command_get_config);
+ if (status) {
+ msg_perr("Raiden: Failed to transmit get config\n"
+ " config attempt = %d\n"
+ " status = %d\n",
+ config_attempt + 1, status);
+ programmer_delay(RETRY_INTERVAL_US);
+ continue;
+ }
+
+ status = receive_packet(ctx_data, &response_config);
+ if (status) {
+ msg_perr("Raiden: Failed to receive packet\n"
+ " config attempt = %d\n"
+ " status = %d\n",
+ config_attempt + 1, status);
+ programmer_delay(RETRY_INTERVAL_US);
+ continue;
+ }
+
+ /*
+ * Perform validation on the packet received to verify it is a valid
+ * configuration. If it is, we are ready to perform transfers.
+ */
+ if ((response_config.packet_v2.packet_id ==
+ PACKET_ID_V2_RESPONSE_USB_SPI_CONFIG) ||
+ (response_config.packet_size ==
+ sizeof(usb_spi_response_configuration_v2_t))) {
+
+ /* Set the parameters from the configuration. */
+ ctx_data->max_spi_write_count =
+ response_config.packet_v2.response_config.max_write_count;
+ ctx_data->max_spi_read_count =
+ response_config.packet_v2.response_config.max_read_count;
+ return status;
+ }
+
+ msg_perr("Raiden: Packet is not a valid config\n"
+ " config attempt = %d\n"
+ " packet id = %d\n"
+ " packet size = %d\n",
+ config_attempt + 1,
+ response_config.packet_v2.packet_id,
+ response_config.packet_size);
+ programmer_delay(RETRY_INTERVAL_US);
+ }
+ return USB_SPI_HOST_INIT_FAILURE;
+}
+
+int restart_response_v2(const struct raiden_debug_spi_data *ctx_data)
+{
+ int status;
+
+ usb_spi_packet_ctx_t restart_response = {
+ .header_size = sizeof(usb_spi_command_restart_response_v2_t),
+ .packet_size = sizeof(usb_spi_command_restart_response_v2_t),
+ .packet_v2.packet_id = PACKET_ID_V2_COMMAND_RESTART_RESPONSE
+ };
+
+ status = transmit_packet(ctx_data, &restart_response);
+ return status;
+}
+
+
+int write_command_v2(const struct raiden_debug_spi_data * ctx_data,
+ usb_spi_transmit_ctx_t* write,
+ usb_spi_receive_ctx_t* read)
+{
+ int status;
+ usb_spi_packet_ctx_t continue_packet;
+
+ usb_spi_packet_ctx_t start_usb_spi_packet = {
+ .header_size = offsetof(usb_spi_command_v2_t, data),
+ .packet_v2.command.packet_id = PACKET_ID_V2_COMMAND,
+ .packet_v2.command.write_count = write->transmit_size,
+ .packet_v2.command.read_count = read->receive_size
+ };
+
+ fill_usb_packet(&start_usb_spi_packet, write);
+ status = transmit_packet(ctx_data, &start_usb_spi_packet);
+ if (status) {
+ return status;
+ }
+
+ while (write->transmit_index < write->transmit_size) {
+ /* Transmit any continue packets. */
+ continue_packet.header_size = offsetof(usb_spi_continue_v2_t, data);
+ continue_packet.packet_v2.command_continue.packet_id =
+ PACKET_ID_V2_COMMAND_CONTINUE;
+ continue_packet.packet_v2.command_continue.data_index =
+ write->transmit_index;
+
+ fill_usb_packet(&continue_packet, write);
+
+ status = transmit_packet(ctx_data, &continue_packet);
+ if (status) {
+ return status;
+ }
+ }
+ return status;
+}
+
+int read_response_v2(const struct raiden_debug_spi_data * ctx_data,
+ usb_spi_transmit_ctx_t* write,
+ usb_spi_receive_ctx_t* read)
+{
+ int status = -1;
+ usb_spi_packet_ctx_t response;
+
+ /* Receive the payload to the servo micro. */
+ while (read->receive_index < read->receive_size) {
+
+ status = receive_packet(ctx_data, &response);
+ if (status) {
+ /* Return the transfer error. */
+ return status;
+ }
+ if (response.packet_v2.packet_id == PACKET_ID_V2_RESPONSE) {
+ /*
+ * The host should only see this packet if an error occurs
+ * on the device or if it's the first response packet.
+ */
+ if (response.packet_v2.response.status_code) {
+ return response.packet_v2.response.status_code;
+ }
+ if (read->receive_index) {
+ msg_perr("Raiden: Unexpected packet id = %d",
+ response.packet_v2.response.packet_id);
+ return USB_SPI_HOST_RX_UNEXPECTED_PACKET;
+ }
+ response.header_size = offsetof(usb_spi_response_v2_t, data);
+ } if (response.packet_v2.packet_id ==
+ PACKET_ID_V2_RESPONSE_CONTINUE) {
+
+ /* We validate that no packets were missed. */
+ if (read->receive_index !=
+ response.packet_v2.response_continue.data_index) {
+ return USB_SPI_HOST_RX_BAD_DATA_INDEX;
+ }
+ response.header_size = offsetof(usb_spi_continue_v2_t, data);
+ } else {
+ msg_perr("Raiden: Unexpected packet id = %d",
+ response.packet_v2.packet_id);
+ return USB_SPI_HOST_RX_UNEXPECTED_PACKET;
+ }
+ status = read_usb_packet(read, &response);
+ if (status) {
+ return status;
+ }
+ }
+ return status;
+}
+
static int send_command(const struct flashctx *flash,
unsigned int write_count,
unsigned int read_count,
@@ -464,6 +910,8 @@
write_ctx.transmit_index = 0;
if (ctx_data->protocol_version == GOOGLE_RAIDEN_SPI_PROTOCOL_V1) {
status = write_command_v1(ctx_data, &write_ctx, &read_ctx);
+ } else {
+ status = write_command_v2(ctx_data, &write_ctx, &read_ctx);
}

if (status) {
@@ -490,6 +938,8 @@
read_ctx.receive_index = 0;
if (ctx_data->protocol_version == GOOGLE_RAIDEN_SPI_PROTOCOL_V1) {
status = read_response_v1(ctx_data, &write_ctx, &read_ctx);
+ } else {
+ status = read_response_v2(ctx_data, &write_ctx, &read_ctx);
}

if (status == 0) {
@@ -513,6 +963,15 @@
/* Reattempting will not result in a recovery. */
return status;
}
+ /*
+ * Protocol version 2 includes multi-packet messages, we can
+ * restart only the response operation without performing SPI
+ * transfer.
+ */
+ if (ctx_data->protocol_version ==
+ GOOGLE_RAIDEN_SPI_PROTOCOL_V2) {
+ restart_response_v2(ctx_data);
+ }
programmer_delay(RETRY_INTERVAL_US);
} else {
/* We were successful at performing the SPI transfer. */
@@ -579,6 +1038,7 @@

static int configure_protocol(struct spi_master *ctx_spi)
{
+ int status = 0;
struct raiden_debug_spi_data *ctx_data =
(struct raiden_debug_spi_data *)ctx_spi->data;

@@ -593,6 +1053,15 @@
ctx_data->max_spi_write_count= SPI_TRANSFER_V1_MAX;
ctx_data->max_spi_read_count = SPI_TRANSFER_V1_MAX;
break;
+ case GOOGLE_RAIDEN_SPI_PROTOCOL_V2:
+ /*
+ * Protocol V2 requires the host to query the device for
+ * it's maximum read and write sizes
+ */
+ status = get_spi_size_v2(ctx_data);
+ if (status) {
+ return status;
+ }
default:
msg_pdbg("Raiden: Unknown USB SPI protocol version = %d",
ctx_data->protocol_version);
@@ -692,7 +1161,6 @@
usb_match_value_default(&match.vid, GOOGLE_VID);
usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
- usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL_V1);

ret = LIBUSB(libusb_init(NULL));
if (ret != 0) {

To view, visit change 41533. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: Ie356c63b521c0cc11a4946ffac128ec7139f0bec
Gerrit-Change-Number: 41533
Gerrit-PatchSet: 1
Gerrit-Owner: Brian Nemec <bnemec@google.com>
Gerrit-Reviewer: Brian Nemec <bnemec@chromium.org>
Gerrit-MessageType: newchange