blob: d6f9d893aa4bee970a51b78e203c8ec899c1565f [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
Souvik Ghoshd75cd672016-06-17 14:21:39 -070084static int send_command(const struct flashctx *flash,
85 unsigned int write_count,
Anton Staafb2647882014-09-17 15:13:43 -070086 unsigned int read_count,
87 const unsigned char *write_buffer,
88 unsigned char *read_buffer)
89{
90 uint8_t buffer[MAX_PACKET_SIZE];
91 int transferred;
92
93 if (write_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
94 msg_perr("Raiden: invalid write_count of %d\n", write_count);
95 return SPI_INVALID_LENGTH;
96 }
97
98 if (read_count > MAX_PACKET_SIZE - PACKET_HEADER_SIZE) {
99 msg_perr("Raiden: invalid read_count of %d\n", read_count);
100 return SPI_INVALID_LENGTH;
101 }
102
103 buffer[0] = write_count;
104 buffer[1] = read_count;
105
106 memcpy(buffer + PACKET_HEADER_SIZE, write_buffer, write_count);
107
Anton Staaf5614e252015-03-24 14:33:33 -0700108 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
109 out_endpoint,
110 buffer,
111 write_count + PACKET_HEADER_SIZE,
112 &transferred,
113 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700114 "Raiden: OUT transfer failed\n"
115 " write_count = %d\n"
116 " read_count = %d\n",
117 write_count,
118 read_count);
119
120 if (transferred != write_count + PACKET_HEADER_SIZE) {
121 msg_perr("Raiden: Write failure (wrote %d, expected %d)\n",
122 transferred, write_count + PACKET_HEADER_SIZE);
123 return 0x10001;
124 }
125
Anton Staaf5614e252015-03-24 14:33:33 -0700126 CHECK(LIBUSB(libusb_bulk_transfer(device->handle,
127 in_endpoint,
128 buffer,
129 read_count + PACKET_HEADER_SIZE,
130 &transferred,
131 TRANSFER_TIMEOUT_MS)),
Anton Staafb2647882014-09-17 15:13:43 -0700132 "Raiden: IN transfer failed\n"
133 " write_count = %d\n"
134 " read_count = %d\n",
135 write_count,
136 read_count);
137
138 if (transferred != read_count + PACKET_HEADER_SIZE) {
139 msg_perr("Raiden: Read failure (read %d, expected %d)\n",
140 transferred, read_count + PACKET_HEADER_SIZE);
141 return 0x10002;
142 }
143
144 memcpy(read_buffer, buffer + PACKET_HEADER_SIZE, read_count);
145
146 return buffer[0] | (buffer[1] << 8);
147}
148
149/*
150 * Unfortunately there doesn't seem to be a way to specify the maximum number
151 * of bytes that your SPI device can read/write, these values are the maximum
152 * data chunk size that flashrom will package up with an additional four bytes
153 * of command for the flash device, resulting in a 62 byte packet, that we then
154 * add two bytes to in either direction, making our way up to the 64 byte
155 * maximum USB packet size for the device.
156 *
157 * The largest command that flashrom generates is the byte program command, so
158 * we use that command header maximum size here. The definition of
159 * JEDEC_BYTE_PROGRAM_OUTSIZE includes enough space for a single byte of data
160 * to write, so we add one byte of space back.
161 */
162#define MAX_DATA_SIZE (MAX_PACKET_SIZE - \
163 PACKET_HEADER_SIZE - \
164 JEDEC_BYTE_PROGRAM_OUTSIZE + \
165 1)
166
167static const struct spi_programmer spi_programmer_raiden_debug = {
168 .type = SPI_CONTROLLER_RAIDEN_DEBUG,
169 .max_data_read = MAX_DATA_SIZE,
170 .max_data_write = MAX_DATA_SIZE,
171 .command = send_command,
172 .multicommand = default_spi_send_multicommand,
173 .read = default_spi_read,
174 .write_256 = default_spi_write_256,
175};
176
Anton Staaf5614e252015-03-24 14:33:33 -0700177static int match_endpoint(struct libusb_endpoint_descriptor const *descriptor,
178 enum libusb_endpoint_direction direction)
Anton Staafd27536d2014-09-30 08:10:17 -0700179{
Anton Staaf5614e252015-03-24 14:33:33 -0700180 return (((descriptor->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) ==
181 direction) &&
182 ((descriptor->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) ==
183 LIBUSB_TRANSFER_TYPE_BULK));
184}
Anton Staafd27536d2014-09-30 08:10:17 -0700185
Souvik Ghosh5cdb7e52016-06-23 12:57:38 -0700186static int find_endpoints(void)
Anton Staaf5614e252015-03-24 14:33:33 -0700187{
188 int i;
189 int in_count = 0;
190 int out_count = 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700191
Anton Staaf5614e252015-03-24 14:33:33 -0700192 for (i = 0; i < device->interface_descriptor->bNumEndpoints; i++) {
193 struct libusb_endpoint_descriptor const *endpoint =
194 &device->interface_descriptor->endpoint[i];
Anton Staafd27536d2014-09-30 08:10:17 -0700195
Anton Staaf5614e252015-03-24 14:33:33 -0700196 if (match_endpoint(endpoint, LIBUSB_ENDPOINT_IN)) {
197 in_count++;
198 in_endpoint = endpoint->bEndpointAddress;
199 } else if (match_endpoint(endpoint, LIBUSB_ENDPOINT_OUT)) {
200 out_count++;
201 out_endpoint = endpoint->bEndpointAddress;
202 }
203 }
204
205 if (in_count != 1 || out_count != 1) {
206 msg_perr("Raiden: Failed to find one IN and one OUT endpoint\n"
207 " found %d IN and %d OUT endpoints\n",
208 in_count,
209 out_count);
210 return 1;
211 }
212
213 msg_pdbg("Raiden: Found IN endpoint = 0x%02x\n", in_endpoint);
214 msg_pdbg("Raiden: Found OUT endpoint = 0x%02x\n", out_endpoint);
215
216 return 0;
Anton Staafd27536d2014-09-30 08:10:17 -0700217}
218
Souvik Ghosh052210a2016-07-11 12:29:05 -0700219static int shutdown(struct flashctx *flash, void * data)
Anton Staaf4589cd12015-03-23 13:36:44 -0700220{
Anton Staaf5614e252015-03-24 14:33:33 -0700221 CHECK(LIBUSB(libusb_control_transfer(
222 device->handle,
223 LIBUSB_ENDPOINT_OUT |
224 LIBUSB_REQUEST_TYPE_VENDOR |
225 LIBUSB_RECIPIENT_INTERFACE,
226 RAIDEN_DEBUG_SPI_REQ_DISABLE,
227 0,
228 device->interface_descriptor->bInterfaceNumber,
229 NULL,
230 0,
231 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700232 "Raiden: Failed to disable SPI bridge\n");
233
Anton Staaf5614e252015-03-24 14:33:33 -0700234 usb_device_free(device);
235
236 device = NULL;
Anton Staaf4589cd12015-03-23 13:36:44 -0700237 libusb_exit(NULL);
238
239 return 0;
240}
241
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700242int raiden_debug_spi_init(struct flashctx *flash)
Anton Staafb2647882014-09-17 15:13:43 -0700243{
Anton Staaf5614e252015-03-24 14:33:33 -0700244 struct usb_match match;
Mary Ruthveneafafd82016-05-03 14:33:53 -0700245 int request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE;
246 char *target_str = extract_programmer_param("target");
247
248 if (target_str) {
249 if (!strcasecmp(target_str, "ap"))
250 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_AP;
251 else if (!strcasecmp(target_str, "ec"))
252 request_enable = RAIDEN_DEBUG_SPI_REQ_ENABLE_EC;
253 else {
254 msg_perr("Invalid target: %s\n", target_str);
255 free(target_str);
256 return 1;
257 }
258 }
259 free(target_str);
Anton Staafd27536d2014-09-30 08:10:17 -0700260
Anton Staaf5614e252015-03-24 14:33:33 -0700261 usb_match_init(&match);
Anton Staafb2647882014-09-17 15:13:43 -0700262
Anton Staaf5614e252015-03-24 14:33:33 -0700263 usb_match_value_default(&match.vid, GOOGLE_VID);
264 usb_match_value_default(&match.class, LIBUSB_CLASS_VENDOR_SPEC);
265 usb_match_value_default(&match.subclass, GOOGLE_RAIDEN_SPI_SUBCLASS);
266 usb_match_value_default(&match.protocol, GOOGLE_RAIDEN_SPI_PROTOCOL);
Anton Staafb2647882014-09-17 15:13:43 -0700267
Anton Staaf5614e252015-03-24 14:33:33 -0700268 CHECK(LIBUSB(libusb_init(NULL)), "Raiden: libusb_init failed\n");
269
270 CHECK(usb_device_find(&match, &device),
271 "Raiden: Failed to find devices\n");
272
273 if (device->next != NULL) {
274 struct usb_device *current;
275
276 msg_perr("Raiden: Found too many compatible devices\n");
277 msg_perr(" Use parameters to specify desired device\n");
278
279 for (current = device;
280 current != NULL;
281 current = usb_device_free(current))
282 usb_device_show(" ", current);
283
284 device = NULL;
285
Anton Staafb2647882014-09-17 15:13:43 -0700286 return 1;
287 }
288
Souvik Ghosh5cdb7e52016-06-23 12:57:38 -0700289 CHECK(find_endpoints(),
Anton Staaf5614e252015-03-24 14:33:33 -0700290 "Raiden: Failed to find valid endpoints\n");
Anton Staafb4661ee2014-10-21 11:24:36 -0700291
Anton Staaf5614e252015-03-24 14:33:33 -0700292 CHECK(usb_device_claim(device),
293 "Raiden: Failed to claim USB device\n");
Anton Staafb4661ee2014-10-21 11:24:36 -0700294
Anton Staaf5614e252015-03-24 14:33:33 -0700295 CHECK(LIBUSB(libusb_control_transfer(
296 device->handle,
297 LIBUSB_ENDPOINT_OUT |
298 LIBUSB_REQUEST_TYPE_VENDOR |
299 LIBUSB_RECIPIENT_INTERFACE,
Mary Ruthveneafafd82016-05-03 14:33:53 -0700300 request_enable,
Anton Staaf5614e252015-03-24 14:33:33 -0700301 0,
302 device->interface_descriptor->bInterfaceNumber,
303 NULL,
304 0,
305 TRANSFER_TIMEOUT_MS)),
Anton Staaf4589cd12015-03-23 13:36:44 -0700306 "Raiden: Failed to enable SPI bridge\n");
307
Anton Staafb2647882014-09-17 15:13:43 -0700308 register_spi_programmer(&spi_programmer_raiden_debug);
Anton Staaf4589cd12015-03-23 13:36:44 -0700309 register_shutdown(shutdown, NULL);
Anton Staafb2647882014-09-17 15:13:43 -0700310
311 return 0;
312}