blob: 2474e2fa61dec99799cb233f931f86deba8257b8 [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 *
45 * The protocol for the USB-SPI bridge is documented in the following file in
46 * that respository:
47 *
48 * chip/stm32/usb_spi.c
Brian J. Nemecda496dc2020-02-04 11:13:05 -080049 *
50 * Version 1:
51 * SPI transactions of up to 62B in each direction with every command having
52 * a response. The initial packet from host contains a 2B header indicating
53 * write and read counts with an optional payload length equal to the write
54 * count. The device will respond with a message that reports the 2B status
55 * code and an optional payload response length equal to read count.
56 *
57 * Message Format:
58 *
59 * Command Packet:
60 * +------------------+-----------------+------------------------+
61 * | write count : 1B | read count : 1B | write payload : <= 62B |
62 * +------------------+-----------------+------------------------+
63 *
64 * write count: 1 byte, zero based count of bytes to write
65 *
66 * read count: 1 byte, zero based count of bytes to read
67 *
68 * write payload: Up to 62 bytes of data to write to SPI, the total
69 * length of all TX packets must match write count.
70 * Due to data alignment constraints, this must be an
71 * even number of bytes unless this is the final packet.
72 *
73 * Response Packet:
74 * +-------------+-----------------------+
75 * | status : 2B | read payload : <= 62B |
76 * +-------------+-----------------------+
77 *
78 * status: 2 byte status
79 * 0x0000: Success
80 * 0x0001: SPI timeout
81 * 0x0002: Busy, try again
82 * This can happen if someone else has acquired the shared memory
83 * buffer that the SPI driver uses as /dev/null
84 * 0x0003: Write count invalid (V1 > 62B)
85 * 0x0004: Read count invalid (V1 > 62B)
86 * 0x0005: The SPI bridge is disabled.
87 * 0x8000: Unknown error mask
88 * The bottom 15 bits will contain the bottom 15 bits from the EC
89 * error code.
90 *
91 * read payload: Up to 62 bytes of data read from SPI, the total
92 * length of all RX packets must match read count
93 * unless an error status was returned. Due to data
94 * alignment constraints, this must be a even number
95 * of bytes unless this is the final packet.
Anton Staafb2647882014-09-17 15:13:43 -070096 */
97
Anton Staafb2647882014-09-17 15:13:43 -070098#include "programmer.h"
99#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -0700100#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -0700101
102#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700103#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -0700104#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -0700105#include <string.h>
Keith Short8453b552020-02-03 18:10:14 -0700106#include <unistd.h>
Anton Staafb2647882014-09-17 15:13:43 -0700107
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800108#define GOOGLE_VID (0x18D1)
109#define GOOGLE_RAIDEN_SPI_SUBCLASS (0x51)
110#define GOOGLE_RAIDEN_SPI_PROTOCOL (0x01)
Anton Staafb2647882014-09-17 15:13:43 -0700111
Anton Staaf4589cd12015-03-23 13:36:44 -0700112enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -0700113 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
114 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
115 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
116 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -0700117};
118
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800119#define PACKET_HEADER_SIZE (2)
120#define MAX_PACKET_SIZE (64)
121#define PAYLOAD_SIZE (MAX_PACKET_SIZE - PACKET_HEADER_SIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700122
123/*
124 * This timeout is so large because the Raiden SPI timeout is 800ms.
125 */
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800126#define TRANSFER_TIMEOUT_MS (1000)
Anton Staafb2647882014-09-17 15:13:43 -0700127
Anton Staaf5614e252015-03-24 14:33:33 -0700128struct usb_device *device = NULL;
129uint8_t in_endpoint = 0;
130uint8_t out_endpoint = 0;
Anton Staafb2647882014-09-17 15:13:43 -0700131
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800132typedef struct {
133 int8_t write_count;
134 /* -1 Indicates readback all on halfduplex compliant devices. */
135 int8_t read_count;
136 uint8_t data[PAYLOAD_SIZE];
137} __attribute__((packed)) usb_spi_command_t;
Anton Staafb2647882014-09-17 15:13:43 -0700138
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800139typedef struct {
140 uint16_t status_code;
141 uint8_t data[PAYLOAD_SIZE];
142} __attribute__((packed)) usb_spi_response_t;
143
144static int write_command(const struct flashctx *flash,
145 unsigned int write_count,
146 unsigned int read_count,
147 const unsigned char *write_buffer,
148 unsigned char *read_buffer)
149{
150
151 int transferred;
152 int ret;
153 usb_spi_command_t command_packet;
154
155 if (write_count > PAYLOAD_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700156 msg_perr("Raiden: invalid write_count of %d\n", write_count);
157 return SPI_INVALID_LENGTH;
158 }
159
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800160 if (read_count > PAYLOAD_SIZE) {
Anton Staafb2647882014-09-17 15:13:43 -0700161 msg_perr("Raiden: invalid read_count of %d\n", read_count);
162 return SPI_INVALID_LENGTH;
163 }
164
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800165 command_packet.write_count = write_count;
166 command_packet.read_count = read_count;
Anton Staafb2647882014-09-17 15:13:43 -0700167
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800168 memcpy(command_packet.data, write_buffer, write_count);
Anton Staafb2647882014-09-17 15:13:43 -0700169
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100170 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800171 out_endpoint,
172 (void*)&command_packet,
173 write_count + PACKET_HEADER_SIZE,
174 &transferred,
175 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100176 if (ret != 0) {
177 msg_perr("Raiden: OUT transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800178 " write_count = %d\n"
179 " read_count = %d\n",
180 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100181 return ret;
182 }
Anton Staafb2647882014-09-17 15:13:43 -0700183
184 if (transferred != write_count + PACKET_HEADER_SIZE) {
185 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
186 transferred, write_count + PACKET_HEADER_SIZE);
187 return 0x10001;
188 }
189
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800190 return 0;
191}
192
193static int read_response(const struct flashctx *flash,
194 unsigned int write_count,
195 unsigned int read_count,
196 const unsigned char *write_buffer,
197 unsigned char *read_buffer)
198{
199
200 int transferred;
201 int ret;
202 usb_spi_response_t response_packet;
203
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100204 ret = LIBUSB(libusb_bulk_transfer(device->handle,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800205 in_endpoint,
206 (void*)&response_packet,
207 read_count + PACKET_HEADER_SIZE,
208 &transferred,
209 TRANSFER_TIMEOUT_MS));
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100210 if (ret != 0) {
211 msg_perr("Raiden: IN transfer failed\n"
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800212 " write_count = %d\n"
213 " read_count = %d\n",
214 write_count, read_count);
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100215 return ret;
216 }
Anton Staafb2647882014-09-17 15:13:43 -0700217
218 if (transferred != read_count + PACKET_HEADER_SIZE) {
219 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800220 transferred, read_count + PACKET_HEADER_SIZE);
Anton Staafb2647882014-09-17 15:13:43 -0700221 return 0x10002;
222 }
223
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800224 memcpy(read_buffer, response_packet.data, read_count);
Anton Staafb2647882014-09-17 15:13:43 -0700225
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800226 return response_packet.status_code;
227}
228
229static int send_command(const struct flashctx *flash,
230 unsigned int write_count,
231 unsigned int read_count,
232 const unsigned char *write_buffer,
233 unsigned char *read_buffer)
234{
235
236 int status = -1;
237
238 status = write_command(flash, write_count, read_count,
239 write_buffer, read_buffer);
240
241 if (status) {
242 return status;
243 }
244
245 status = read_response(flash, write_count, read_count,
246 write_buffer, read_buffer);
247
248 return status;
Anton Staafb2647882014-09-17 15:13:43 -0700249}
250
251/*
252 * Unfortunately there doesn't seem to be a way to specify the maximum number
253 * of bytes that your SPI device can read/write, these values are the maximum
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700254 * data chunk size that flashrom will package up with an additional five bytes
Anton Staafb2647882014-09-17 15:13:43 -0700255 * of command for the flash device, resulting in a 62 byte packet, that we then
256 * add two bytes to in either direction, making our way up to the 64 byte
257 * maximum USB packet size for the device.
258 *
259 * The largest command that flashrom generates is the byte program command, so
Duncan Laurie537fd1d2018-10-05 10:53:20 -0700260 * we use that command header maximum size here.
Anton Staafb2647882014-09-17 15:13:43 -0700261 */
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800262#define MAX_DATA_SIZE (PAYLOAD_SIZE - JEDEC_BYTE_PROGRAM_OUTSIZE)
Anton Staafb2647882014-09-17 15:13:43 -0700263
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100264static const struct spi_master spi_master_raiden_debug = {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800265 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
266 .features = SPI_MASTER_4BA,
267 .max_data_read = MAX_DATA_SIZE,
268 .max_data_write = MAX_DATA_SIZE,
269 .command = send_command,
270 .multicommand = default_spi_send_multicommand,
271 .read = default_spi_read,
272 .write_256 = default_spi_write_256,
Anton Staafb2647882014-09-17 15:13:43 -0700273};
274
Anton Staaf5614e252015-03-24 14:33:33 -0700275static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800276 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700277{
Anton Staaf5614e252015-03-24 14:33:33 -0700278 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
279 direction) &&
280 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
281 LIBUSB_TRANSFER_TYPE_BULK));
282}
Anton Staafd27536d2014-09-30 08:10:17 -0700283
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800284static int find_endpoints(struct usb_device *dev, uint8_t *in_ep,
285 uint8_t *out_ep)
Anton Staaf5614e252015-03-24 14:33:33 -0700286{
287 int i;
288 int in_count = 0;
289 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700290
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100291 for (i = 0; i < dev->interface_descriptor->bNumEndpoints; i++) {
Anton Staaf5614e252015-03-24 14:33:33 -0700292 struct libusb_endpoint_descriptor const *endpoint =
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100293 &dev->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700294
Anton Staaf5614e252015-03-24 14:33:33 -0700295 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
296 in_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100297 *in_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700298 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
299 out_count++;
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100300 *out_ep = endpoint->bEndpointAddress;
Anton Staaf5614e252015-03-24 14:33:33 -0700301 }
302 }
303
304 if (in_count != 1 || out_count != 1) {
305 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
306 " found %d IN and %d OUT endpoints\n",
307 in_count,
308 out_count);
309 return 1;
310 }
311
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100312 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", *in_ep);
313 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", *out_ep);
Anton Staaf5614e252015-03-24 14:33:33 -0700314
315 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700316}
317
David Hendricks93784b42016-08-09 17:00:38 -0700318static int shutdown(void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700319{
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100320 int ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700321 device->handle,
322 LIBUSB_ENDPOINT_OUT |
323 LIBUSB_REQUEST_TYPE_VENDOR |
324 LIBUSB_RECIPIENT_INTERFACE,
325 RAIDEN_DEBUG_SPI_REQ_DISABLE,
326 0,
327 device->interface_descriptor->bInterfaceNumber,
328 NULL,
329 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100330 TRANSFER_TIMEOUT_MS));
331 if (ret != 0) {
332 msg_perr("Raiden: Failed to disable SPI bridge\n");
333 return ret;
334 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700335
Anton Staaf5614e252015-03-24 14:33:33 -0700336 usb_device_free(device);
337
338 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700339 libusb_exit(NULL);
340
341 return 0;
342}
343
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100344static int get_target()
Anton Staafb2647882014-09-17 15:13:43 -0700345{
Mary Ruthveneafafd82016-05-03 14:33:53 -0700346 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700347
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100348 char *target_str = extract_programmer_param("target");
Mary Ruthveneafafd82016-05-03 14:33:53 -0700349 if (target_str) {
350 if (!strcasecmp(target_str, "ap"))
351 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
352 else if (!strcasecmp(target_str, "ec"))
353 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
354 else {
355 msg_perr("Invalid target: %s\n", target_str);
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100356 request_enable = -1;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700357 }
358 }
David Hendricks98b3c572016-11-30 01:50:08 +0000359 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700360
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100361 return request_enable;
362}
363
364int raiden_debug_spi_init(void)
365{
366 struct usb_match match;
367 char *serial = extract_programmer_param("serial");
368 struct usb_device *current;
369 int found = 0;
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100370 int ret;
Edward O'Callaghan8d2f6482019-11-19 15:42:44 +1100371
372 int request_enable = get_target();
373 if (request_enable < 0)
374 return 1;
375
Anton Staaf5614e252015-03-24 14:33:33 -0700376 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700377
Anton Staaf5614e252015-03-24 14:33:33 -0700378 usb_match_value_default(&match.vid, GOOGLE_VID);
379 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
380 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
381 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700382
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100383 ret = LIBUSB(libusb_init(NULL));
384 if (ret != 0) {
385 msg_perr("Raiden: libusb_init failed\n");
386 return ret;
387 }
Anton Staaf5614e252015-03-24 14:33:33 -0700388
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100389 ret = usb_device_find(&match, &current);
390 if (ret != 0) {
391 msg_perr("Raiden: Failed to find devices\n");
392 return ret;
393 }
Anton Staaf5614e252015-03-24 14:33:33 -0700394
David Hendricks5c79a492016-06-14 20:56:36 -0700395 while (current) {
396 device = current;
Anton Staaf5614e252015-03-24 14:33:33 -0700397
Edward O'Callaghandabf7e82019-11-19 15:11:18 +1100398 if (find_endpoints(device, &in_endpoint, &out_endpoint)) {
David Hendricks5c79a492016-06-14 20:56:36 -0700399 msg_pdbg("Raiden: Failed to find valid endpoints on device");
400 usb_device_show(" ", current);
401 goto loop_end;
402 }
Anton Staaf5614e252015-03-24 14:33:33 -0700403
David Hendricks5c79a492016-06-14 20:56:36 -0700404 if (usb_device_claim(device)) {
405 msg_pdbg("Raiden: Failed to claim USB device");
406 usb_device_show(" ", current);
407 goto loop_end;
408 }
Anton Staaf5614e252015-03-24 14:33:33 -0700409
David Hendricks5c79a492016-06-14 20:56:36 -0700410 if (!serial) {
411 found = 1;
412 goto loop_end;
413 } else {
414 unsigned char dev_serial[32];
415 struct libusb_device_descriptor descriptor;
416 int rc;
Anton Staaf5614e252015-03-24 14:33:33 -0700417
David Hendricks5c79a492016-06-14 20:56:36 -0700418 memset(dev_serial, 0, sizeof(dev_serial));
419
420 if (libusb_get_device_descriptor(device->device, &descriptor)) {
421 msg_pdbg("USB: Failed to get device descriptor.\n");
422 goto loop_end;
423 }
424
425 rc = libusb_get_string_descriptor_ascii(device->handle,
426 descriptor.iSerialNumber,
427 dev_serial,
428 sizeof(dev_serial));
429 if (rc < 0) {
430 LIBUSB(rc);
431 } else {
432 if (strcmp(serial, (char *)dev_serial)) {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800433 msg_pdbg("Raiden: Serial number %s did not match device",
434 serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700435 usb_device_show(" ", current);
436 } else {
Brian J. Nemecda496dc2020-02-04 11:13:05 -0800437 msg_pinfo("Raiden: Serial number %s matched device",
438 serial);
David Hendricks5c79a492016-06-14 20:56:36 -0700439 usb_device_show(" ", current);
440 found = 1;
441 }
442 }
443 }
444
445loop_end:
446 if (found)
447 break;
448 else
449 current = usb_device_free(current);
450 }
451
452 if (!device || !found) {
453 msg_perr("Raiden: No usable device found.\n");
David Hendricks98b3c572016-11-30 01:50:08 +0000454 return 1;
Anton Staafb2647882014-09-17 15:13:43 -0700455 }
456
David Hendricks5c79a492016-06-14 20:56:36 -0700457 /* free devices we don't care about */
458 current = current->next;
459 while (current)
460 current = usb_device_free(current);
Anton Staafb4661ee2014-10-21 11:24:36 -0700461
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100462 ret = LIBUSB(libusb_control_transfer(
Anton Staaf5614e252015-03-24 14:33:33 -0700463 device->handle,
464 LIBUSB_ENDPOINT_OUT |
465 LIBUSB_REQUEST_TYPE_VENDOR |
466 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700467 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700468 0,
469 device->interface_descriptor->bInterfaceNumber,
470 NULL,
471 0,
Edward O'Callaghan5c81bf32020-01-29 14:26:55 +1100472 TRANSFER_TIMEOUT_MS));
473 if (ret != 0) {
474 msg_perr("Raiden: Failed to enable SPI bridge\n");
475 return ret;
476 }
Anton Staaf4589cd12015-03-23 13:36:44 -0700477
Keith Short8453b552020-02-03 18:10:14 -0700478 /*
479 * Allow for power to settle on the AP and EC flash devices.
480 * Load switches can have a 1-3 ms turn on time, and SPI flash devices
481 * can require up to 10 ms from power on to the first write.
482 */
483 if ((request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_AP) ||
484 (request_enable == RAIDEN_DEBUG_SPI_REQ_ENABLE_EC))
485 usleep(50 * 1000);
486
Patrick Georgif4f1e2f2017-03-10 17:38:40 +0100487 register_spi_master(&spi_master_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700488 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700489
David Hendricks98b3c572016-11-30 01:50:08 +0000490 return 0;
Anton Staafb2647882014-09-17 15:13:43 -0700491}