blob: e85830b7a3d9863331794884e1ab7fbddd08afc5 [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
49 */
50
Anton Staaf5614e252015-03-24 14:33:33 -070051#include "check.h"
Anton Staafb2647882014-09-17 15:13:43 -070052#include "programmer.h"
53#include "spi.h"
Anton Staaf5614e252015-03-24 14:33:33 -070054#include "usb_device.h"
Anton Staafb2647882014-09-17 15:13:43 -070055
56#include <libusb.h>
Anton Staaf5614e252015-03-24 14:33:33 -070057#include <stdio.h>
Anton Staafb2647882014-09-17 15:13:43 -070058#include <stdlib.h>
Anton Staaf5614e252015-03-24 14:33:33 -070059#include <string.h>
Anton Staafb2647882014-09-17 15:13:43 -070060
Anton Staaf5614e252015-03-24 14:33:33 -070061#define GOOGLE_VID 0x18D1
62#define GOOGLE_RAIDEN_SPI_SUBCLASS 0x51
63#define GOOGLE_RAIDEN_SPI_PROTOCOL 0x01
Anton Staafb2647882014-09-17 15:13:43 -070064
Anton Staaf4589cd12015-03-23 13:36:44 -070065enum raiden_debug_spi_request {
Mary Ruthveneafafd82016-05-03 14:33:53 -070066 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
67 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
68 RAIDEN_DEBUG_SPI_REQ_ENABLE_AP = 0x0002,
69 RAIDEN_DEBUG_SPI_REQ_ENABLE_EC = 0x0003,
Anton Staaf4589cd12015-03-23 13:36:44 -070070};
71
Anton Staafd27536d2014-09-30 08:10:17 -070072#define PACKET_HEADER_SIZE 2
73#define MAX_PACKET_SIZE 64
Anton Staafb2647882014-09-17 15:13:43 -070074
75/*
76 * This timeout is so large because the Raiden SPI timeout is 800ms.
77 */
78#define TRANSFER_TIMEOUT_MS 1000
79
Anton Staaf5614e252015-03-24 14:33:33 -070080struct usb_device *device = NULL;
81uint8_t in_endpoint = 0;
82uint8_t out_endpoint = 0;
Anton Staafb2647882014-09-17 15:13:43 -070083
84static int send_command(unsigned int write_count,
85 unsigned int read_count,
86 const unsigned char *write_buffer,
87 unsigned char *read_buffer)
88{
89 uint8_t buffer[MAX_PACKET_SIZE];
90 int transferred;
91
92 if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
93 msg_perr("Raiden: invalid write_count of %d\n", write_count);
94 return SPI_INVALID_LENGTH;
95 }
96
97 if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
98 msg_perr("Raiden: invalid read_count of %d\n", read_count);
99 return SPI_INVALID_LENGTH;
100 }
101
102 buffer[0] = write_count;
103 buffer[1] = read_count;
104
105 memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count);
106
Anton Staaf5614e252015-03-24 14:33:33 -0700107 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
108 out_endpoint,
109 buffer,
110 write_count + PACKET_HEADER_SIZE,
111 &transferred,
112 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700113 "Raiden: OUT transfer failed\n"
114 " write_count = %d\n"
115 " read_count = %d\n",
116 write_count,
117 read_count);
118
119 if (transferred != write_count + PACKET_HEADER_SIZE) {
120 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
121 transferred, write_count + PACKET_HEADER_SIZE);
122 return 0x10001;
123 }
124
Anton Staaf5614e252015-03-24 14:33:33 -0700125 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
126 in_endpoint,
127 buffer,
128 read_count + PACKET_HEADER_SIZE,
129 &transferred,
130 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700131 "Raiden: IN transfer failed\n"
132 " write_count = %d\n"
133 " read_count = %d\n",
134 write_count,
135 read_count);
136
137 if (transferred != read_count + PACKET_HEADER_SIZE) {
138 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
139 transferred, read_count + PACKET_HEADER_SIZE);
140 return 0x10002;
141 }
142
143 memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
144
145 return buffer[0] | (buffer[1] << 8);
146}
147
148/*
149 * Unfortunately there doesn't seem to be a way to specify the maximum number
150 * of bytes that your SPI device can read/write, these values are the maximum
151 * data chunk size that flashrom will package up with an additional four bytes
152 * of command for the flash device, resulting in a 62 byte packet, that we then
153 * add two bytes to in either direction, making our way up to the 64 byte
154 * maximum USB packet size for the device.
155 *
156 * The largest command that flashrom generates is the byte program command, so
157 * we use that command header maximum size here. The definition of
158 * JEDEC_BYTE_PROGRAM_OUTSIZE includes enough space for a single byte of data
159 * to write, so we add one byte of space back.
160 */
161#define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
162 PACKET_HEADER_SIZE - \
163 JEDEC_BYTE_PROGRAM_OUTSIZE + \
164 1)
165
166static const struct spi_programmer spi_programmer_raiden_debug = {
167 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
168 .max_data_read = MAX_DATA_SIZE,
169 .max_data_write = MAX_DATA_SIZE,
170 .command = send_command,
171 .multicommand = default_spi_send_multicommand,
172 .read = default_spi_read,
173 .write_256 = default_spi_write_256,
174};
175
Anton Staaf5614e252015-03-24 14:33:33 -0700176static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
177 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700178{
Anton Staaf5614e252015-03-24 14:33:33 -0700179 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
180 direction) &&
181 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
182 LIBUSB_TRANSFER_TYPE_BULK));
183}
Anton Staafd27536d2014-09-30 08:10:17 -0700184
Souvik Ghosh5cdb7e52016-06-23 12:57:38 -0700185static int find_endpoints(void)
Anton Staaf5614e252015-03-24 14:33:33 -0700186{
187 int i;
188 int in_count = 0;
189 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700190
Anton Staaf5614e252015-03-24 14:33:33 -0700191 for (i = 0; i < device->interface_descriptor->bNumEndpoints; i++) {
192 struct libusb_endpoint_descriptor const *endpoint =
193 &device->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700194
Anton Staaf5614e252015-03-24 14:33:33 -0700195 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
196 in_count++;
197 in_endpoint = endpoint->bEndpointAddress;
198 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
199 out_count++;
200 out_endpoint = endpoint->bEndpointAddress;
201 }
202 }
203
204 if (in_count != 1 || out_count != 1) {
205 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
206 " found %d IN and %d OUT endpoints\n",
207 in_count,
208 out_count);
209 return 1;
210 }
211
212 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", in_endpoint);
213 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", out_endpoint);
214
215 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700216}
217
Anton Staaf4589cd12015-03-23 13:36:44 -0700218static int shutdown(void * data)
219{
Anton Staaf5614e252015-03-24 14:33:33 -0700220 CHECK(LIBUSB(libusb_control_transfer(
221 device->handle,
222 LIBUSB_ENDPOINT_OUT |
223 LIBUSB_REQUEST_TYPE_VENDOR |
224 LIBUSB_RECIPIENT_INTERFACE,
225 RAIDEN_DEBUG_SPI_REQ_DISABLE,
226 0,
227 device->interface_descriptor->bInterfaceNumber,
228 NULL,
229 0,
230 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700231 "Raiden: Failed to disable SPI bridge\n");
232
Anton Staaf5614e252015-03-24 14:33:33 -0700233 usb_device_free(device);
234
235 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700236 libusb_exit(NULL);
237
238 return 0;
239}
240
Anton Staafb2647882014-09-17 15:13:43 -0700241int raiden_debug_spi_init(void)
242{
Anton Staaf5614e252015-03-24 14:33:33 -0700243 struct usb_match match;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700244 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
245 char *target_str = extract_programmer_param("target");
246
247 if (target_str) {
248 if (!strcasecmp(target_str, "ap"))
249 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
250 else if (!strcasecmp(target_str, "ec"))
251 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
252 else {
253 msg_perr("Invalid target: %s\n", target_str);
254 free(target_str);
255 return 1;
256 }
257 }
258 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700259
Anton Staaf5614e252015-03-24 14:33:33 -0700260 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700261
Anton Staaf5614e252015-03-24 14:33:33 -0700262 usb_match_value_default(&match.vid, GOOGLE_VID);
263 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
264 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
265 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700266
Anton Staaf5614e252015-03-24 14:33:33 -0700267 CHECK(LIBUSB(libusb_init(NULL)), "Raiden: libusb_init failed\n");
268
269 CHECK(usb_device_find(&match, &device),
270 "Raiden: Failed to find devices\n");
271
272 if (device->next != NULL) {
273 struct usb_device *current;
274
275 msg_perr("Raiden: Found too many compatible devices\n");
276 msg_perr(" Use parameters to specify desired device\n");
277
278 for (current = device;
279 current != NULL;
280 current = usb_device_free(current))
281 usb_device_show(" ", current);
282
283 device = NULL;
284
Anton Staafb2647882014-09-17 15:13:43 -0700285 return 1;
286 }
287
Souvik Ghosh5cdb7e52016-06-23 12:57:38 -0700288 CHECK(find_endpoints(),
Anton Staaf5614e252015-03-24 14:33:33 -0700289 "Raiden: Failed to find valid endpoints\n");
Anton Staafb4661ee2014-10-21 11:24:36 -0700290
Anton Staaf5614e252015-03-24 14:33:33 -0700291 CHECK(usb_device_claim(device),
292 "Raiden: Failed to claim USB device\n");
Anton Staafb4661ee2014-10-21 11:24:36 -0700293
Anton Staaf5614e252015-03-24 14:33:33 -0700294 CHECK(LIBUSB(libusb_control_transfer(
295 device->handle,
296 LIBUSB_ENDPOINT_OUT |
297 LIBUSB_REQUEST_TYPE_VENDOR |
298 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700299 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700300 0,
301 device->interface_descriptor->bInterfaceNumber,
302 NULL,
303 0,
304 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700305 "Raiden: Failed to enable SPI bridge\n");
306
Anton Staafb2647882014-09-17 15:13:43 -0700307 register_spi_programmer(&spi_programmer_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700308 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700309
310 return 0;
311}