blob: e8bece0ca763e47fdf3de0d66fb0d64776f7f4c7 [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 {
66 RAIDEN_DEBUG_SPI_REQ_ENABLE = 0x0000,
67 RAIDEN_DEBUG_SPI_REQ_DISABLE = 0x0001,
68};
69
Anton Staafd27536d2014-09-30 08:10:17 -070070#define PACKET_HEADER_SIZE 2
71#define MAX_PACKET_SIZE 64
Anton Staafb2647882014-09-17 15:13:43 -070072
73/*
74 * This timeout is so large because the Raiden SPI timeout is 800ms.
75 */
76#define TRANSFER_TIMEOUT_MS 1000
77
Anton Staaf5614e252015-03-24 14:33:33 -070078struct usb_device *device = NULL;
79uint8_t in_endpoint = 0;
80uint8_t out_endpoint = 0;
Anton Staafb2647882014-09-17 15:13:43 -070081
82static int send_command(unsigned int write_count,
83 unsigned int read_count,
84 const unsigned char *write_buffer,
85 unsigned char *read_buffer)
86{
87 uint8_t buffer[MAX_PACKET_SIZE];
88 int transferred;
89
90 if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
91 msg_perr("Raiden: invalid write_count of %d\n", write_count);
92 return SPI_INVALID_LENGTH;
93 }
94
95 if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
96 msg_perr("Raiden: invalid read_count of %d\n", read_count);
97 return SPI_INVALID_LENGTH;
98 }
99
100 buffer[0] = write_count;
101 buffer[1] = read_count;
102
103 memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count);
104
Anton Staaf5614e252015-03-24 14:33:33 -0700105 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
106 out_endpoint,
107 buffer,
108 write_count + PACKET_HEADER_SIZE,
109 &transferred,
110 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700111 "Raiden: OUT transfer failed\n"
112 " write_count = %d\n"
113 " read_count = %d\n",
114 write_count,
115 read_count);
116
117 if (transferred != write_count + PACKET_HEADER_SIZE) {
118 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
119 transferred, write_count + PACKET_HEADER_SIZE);
120 return 0x10001;
121 }
122
Anton Staaf5614e252015-03-24 14:33:33 -0700123 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
124 in_endpoint,
125 buffer,
126 read_count + PACKET_HEADER_SIZE,
127 &transferred,
128 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700129 "Raiden: IN transfer failed\n"
130 " write_count = %d\n"
131 " read_count = %d\n",
132 write_count,
133 read_count);
134
135 if (transferred != read_count + PACKET_HEADER_SIZE) {
136 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
137 transferred, read_count + PACKET_HEADER_SIZE);
138 return 0x10002;
139 }
140
141 memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
142
143 return buffer[0] | (buffer[1] << 8);
144}
145
146/*
147 * Unfortunately there doesn't seem to be a way to specify the maximum number
148 * of bytes that your SPI device can read/write, these values are the maximum
149 * data chunk size that flashrom will package up with an additional four bytes
150 * of command for the flash device, resulting in a 62 byte packet, that we then
151 * add two bytes to in either direction, making our way up to the 64 byte
152 * maximum USB packet size for the device.
153 *
154 * The largest command that flashrom generates is the byte program command, so
155 * we use that command header maximum size here. The definition of
156 * JEDEC_BYTE_PROGRAM_OUTSIZE includes enough space for a single byte of data
157 * to write, so we add one byte of space back.
158 */
159#define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
160 PACKET_HEADER_SIZE - \
161 JEDEC_BYTE_PROGRAM_OUTSIZE + \
162 1)
163
164static const struct spi_programmer spi_programmer_raiden_debug = {
165 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
166 .max_data_read = MAX_DATA_SIZE,
167 .max_data_write = MAX_DATA_SIZE,
168 .command = send_command,
169 .multicommand = default_spi_send_multicommand,
170 .read = default_spi_read,
171 .write_256 = default_spi_write_256,
172};
173
Anton Staaf5614e252015-03-24 14:33:33 -0700174static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
175 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700176{
Anton Staaf5614e252015-03-24 14:33:33 -0700177 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
178 direction) &&
179 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
180 LIBUSB_TRANSFER_TYPE_BULK));
181}
Anton Staafd27536d2014-09-30 08:10:17 -0700182
Anton Staaf5614e252015-03-24 14:33:33 -0700183static int find_endpoints(struct usb_device *device)
184{
185 int i;
186 int in_count = 0;
187 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700188
Anton Staaf5614e252015-03-24 14:33:33 -0700189 for (i = 0; i < device->interface_descriptor->bNumEndpoints; i++) {
190 struct libusb_endpoint_descriptor const *endpoint =
191 &device->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700192
Anton Staaf5614e252015-03-24 14:33:33 -0700193 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
194 in_count++;
195 in_endpoint = endpoint->bEndpointAddress;
196 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
197 out_count++;
198 out_endpoint = endpoint->bEndpointAddress;
199 }
200 }
201
202 if (in_count != 1 || out_count != 1) {
203 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
204 " found %d IN and %d OUT endpoints\n",
205 in_count,
206 out_count);
207 return 1;
208 }
209
210 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", in_endpoint);
211 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", out_endpoint);
212
213 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700214}
215
Anton Staaf4589cd12015-03-23 13:36:44 -0700216static int shutdown(void * data)
217{
Anton Staaf5614e252015-03-24 14:33:33 -0700218 CHECK(LIBUSB(libusb_control_transfer(
219 device->handle,
220 LIBUSB_ENDPOINT_OUT |
221 LIBUSB_REQUEST_TYPE_VENDOR |
222 LIBUSB_RECIPIENT_INTERFACE,
223 RAIDEN_DEBUG_SPI_REQ_DISABLE,
224 0,
225 device->interface_descriptor->bInterfaceNumber,
226 NULL,
227 0,
228 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700229 "Raiden: Failed to disable SPI bridge\n");
230
Anton Staaf5614e252015-03-24 14:33:33 -0700231 usb_device_free(device);
232
233 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700234 libusb_exit(NULL);
235
236 return 0;
237}
238
Anton Staafb2647882014-09-17 15:13:43 -0700239int raiden_debug_spi_init(void)
240{
Anton Staaf5614e252015-03-24 14:33:33 -0700241 struct usb_match match;
Anton Staafd27536d2014-09-30 08:10:17 -0700242
Anton Staaf5614e252015-03-24 14:33:33 -0700243 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700244
Anton Staaf5614e252015-03-24 14:33:33 -0700245 usb_match_value_default(&match.vid, GOOGLE_VID);
246 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
247 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
248 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700249
Anton Staaf5614e252015-03-24 14:33:33 -0700250 CHECK(LIBUSB(libusb_init(NULL)), "Raiden: libusb_init failed\n");
251
252 CHECK(usb_device_find(&match, &device),
253 "Raiden: Failed to find devices\n");
254
255 if (device->next != NULL) {
256 struct usb_device *current;
257
258 msg_perr("Raiden: Found too many compatible devices\n");
259 msg_perr(" Use parameters to specify desired device\n");
260
261 for (current = device;
262 current != NULL;
263 current = usb_device_free(current))
264 usb_device_show(" ", current);
265
266 device = NULL;
267
Anton Staafb2647882014-09-17 15:13:43 -0700268 return 1;
269 }
270
Anton Staaf5614e252015-03-24 14:33:33 -0700271 CHECK(find_endpoints(device),
272 "Raiden: Failed to find valid endpoints\n");
Anton Staafb4661ee2014-10-21 11:24:36 -0700273
Anton Staaf5614e252015-03-24 14:33:33 -0700274 CHECK(usb_device_claim(device),
275 "Raiden: Failed to claim USB device\n");
Anton Staafb4661ee2014-10-21 11:24:36 -0700276
Anton Staaf5614e252015-03-24 14:33:33 -0700277 CHECK(LIBUSB(libusb_control_transfer(
278 device->handle,
279 LIBUSB_ENDPOINT_OUT |
280 LIBUSB_REQUEST_TYPE_VENDOR |
281 LIBUSB_RECIPIENT_INTERFACE,
282 RAIDEN_DEBUG_SPI_REQ_ENABLE,
283 0,
284 device->interface_descriptor->bInterfaceNumber,
285 NULL,
286 0,
287 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700288 "Raiden: Failed to enable SPI bridge\n");
289
Anton Staafb2647882014-09-17 15:13:43 -0700290 register_spi_programmer(&spi_programmer_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700291 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700292
293 return 0;
294}