aboutsummaryrefslogtreecommitdiffstats
path: root/raiden_debug_spi.c
diff options
context:
space:
mode:
authorEdward O'Callaghan <quasisec@google.com>2020-03-05 14:51:36 +1100
committerEdward O'Callaghan <quasisec@chromium.org>2020-03-11 06:23:05 +0000
commite8c0ee7c7eb93225de1bb0d16d8bbeda4f3ed542 (patch)
treebb4c39d066f898ac7a919dfedf19ac8996774a2b /raiden_debug_spi.c
parent4847496062bf57f1738ce1523e77d6ad229d12c6 (diff)
downloadflashrom-e8c0ee7c7eb93225de1bb0d16d8bbeda4f3ed542.tar.gz
flashrom-e8c0ee7c7eb93225de1bb0d16d8bbeda4f3ed542.tar.bz2
flashrom-e8c0ee7c7eb93225de1bb0d16d8bbeda4f3ed542.zip
raiden_debug_spi.c: Clean up RW ops into sep paths
- The USB SPI interface has been split up into write and read stages. - The packet packing has been transitioned from array based to a struct. This was based off the downstream commit: https://chromium-review.googlesource.com/c/chromiumos/third_party/flashrom/+/2036508 Change-Id: Id3a2a544c1c7e1d969a5157977b8a1c7af18371b Signed-off-by: Edward O'Callaghan <quasisec@google.com> Reviewed-on: https://review.coreboot.org/c/flashrom/+/39309 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
Diffstat (limited to 'raiden_debug_spi.c')
-rw-r--r--raiden_debug_spi.c96
1 files changed, 71 insertions, 25 deletions
diff --git a/raiden_debug_spi.c b/raiden_debug_spi.c
index 08db5830..08f8928b 100644
--- a/raiden_debug_spi.c
+++ b/raiden_debug_spi.c
@@ -128,8 +128,9 @@ enum raiden_debug_spi_request {
RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
};
-#define PACKET_HEADER_SIZE 2
-#define MAX_PACKET_SIZE 64
+#define PACKET_HEADER_SIZE (2)
+#define MAX_PACKET_SIZE (64)
+#define PAYLOAD_SIZE (MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
/*
* This timeout is so large because the Raiden SPI timeout is 800ms.
@@ -140,37 +141,50 @@ struct usb_device *device = NULL;
uint8_t in_endpoint = 0;
uint8_t out_endpoint = 0;
-static int send_command(struct flashctx *flash,
- unsigned int write_count,
- unsigned int read_count,
- const unsigned char *write_buffer,
- unsigned char *read_buffer)
+typedef struct {
+ int8_t write_count;
+ /* -1 Indicates readback all on halfduplex compliant devices. */
+ int8_t read_count;
+ uint8_t data[PAYLOAD_SIZE];
+} __attribute__((packed)) usb_spi_command_t;
+
+typedef struct {
+ uint16_t status_code;
+ uint8_t data[PAYLOAD_SIZE];
+} __attribute__((packed)) usb_spi_response_t;
+
+static int write_command(const struct flashctx *flash,
+ unsigned int write_count,
+ unsigned int read_count,
+ const unsigned char *write_buffer,
+ unsigned char *read_buffer)
{
- uint8_t buffer[MAX_PACKET_SIZE];
- int transferred;
+
+ int transferred;
int ret;
+ usb_spi_command_t command_packet;
- if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
+ if (write_count > PAYLOAD_SIZE) {
msg_perr("Raiden: Invalid write_count of %d\n", write_count);
return SPI_INVALID_LENGTH;
}
- if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
+ if (read_count > PAYLOAD_SIZE) {
msg_perr("Raiden: Invalid read_count of %d\n", read_count);
return SPI_INVALID_LENGTH;
}
- buffer[0] = write_count;
- buffer[1] = read_count;
+ command_packet.write_count = write_count;
+ command_packet.read_count = read_count;
- memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count);
+ memcpy(command_packet.data, write_buffer, write_count);
ret = LIBUSB(libusb_bulk_transfer(device->handle,
- out_endpoint,
- buffer,
- write_count + PACKET_HEADER_SIZE,
- &transferred,
- TRANSFER_TIMEOUT_MS));
+ out_endpoint,
+ (void*)&command_packet,
+ write_count + PACKET_HEADER_SIZE,
+ &transferred,
+ TRANSFER_TIMEOUT_MS));
if (ret != 0) {
msg_perr("Raiden: OUT transfer failed\n"
" write_count = %d\n"
@@ -185,9 +199,22 @@ static int send_command(struct flashctx *flash,
return 0x10001;
}
+ return 0;
+}
+
+static int read_response(const struct flashctx *flash,
+ unsigned int write_count,
+ unsigned int read_count,
+ const unsigned char *write_buffer,
+ unsigned char *read_buffer)
+{
+ int transferred;
+ int ret;
+ usb_spi_response_t response_packet;
+
ret = LIBUSB(libusb_bulk_transfer(device->handle,
in_endpoint,
- buffer,
+ (void*)&response_packet,
read_count + PACKET_HEADER_SIZE,
&transferred,
TRANSFER_TIMEOUT_MS));
@@ -205,9 +232,30 @@ static int send_command(struct flashctx *flash,
return 0x10002;
}
- memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
+ memcpy(read_buffer, response_packet.data, read_count);
+
+ return response_packet.status_code;
+}
+
+static int send_command(struct flashctx *flash,
+ unsigned int write_count,
+ unsigned int read_count,
+ const unsigned char *write_buffer,
+ unsigned char *read_buffer)
+{
+ int status = -1;
+
+ status = write_command(flash, write_count, read_count,
+ write_buffer, read_buffer);
+
+ if (status) {
+ return status;
+ }
+
+ status = read_response(flash, write_count, read_count,
+ write_buffer, read_buffer);
- return buffer[0] | (buffer[1] << 8);
+ return status;
}
/*
@@ -221,9 +269,7 @@ static int send_command(struct flashctx *flash,
* The largest command that flashrom generates is the byte program command, so
* we use that command header maximum size here.
*/
-#define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
- PACKET_HEADER_SIZE - \
- JEDEC_BYTE_PROGRAM_OUTSIZE)
+#define MAX_DATA_SIZE (PAYLOAD_SIZE - JEDEC_BYTE_PROGRAM_OUTSIZE)
static const struct spi_master spi_master_raiden_debug = {
.features = SPI_MASTER_4BA,