blob: 2b42077a78f638395b19a0ef10142fb168379ce8 [file] [log] [blame]
Nicholas Chin197b7c72022-10-23 13:10:31 -06001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2022 Nicholas Chin <nic.c3.14@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 */
16
17#include <string.h>
18#include <stdlib.h>
19#include <libusb.h>
20#include "platform.h"
21#include "programmer.h"
22#include "flash.h"
23
24#define CH347_CMD_SPI_SET_CFG 0xC0
25#define CH347_CMD_SPI_CS_CTRL 0xC1
26#define CH347_CMD_SPI_OUT_IN 0xC2
27#define CH347_CMD_SPI_IN 0xC3
28#define CH347_CMD_SPI_OUT 0xC4
29#define CH347_CMD_SPI_GET_CFG 0xCA
30
31#define CH347_CS_ASSERT 0x00
32#define CH347_CS_DEASSERT 0x40
33#define CH347_CS_CHANGE 0x80
34#define CH347_CS_IGNORE 0x00
35
36#define WRITE_EP 0x06
37#define READ_EP 0x86
38
Nicholas Chin197b7c72022-10-23 13:10:31 -060039/* The USB descriptor says the max transfer size is 512 bytes, but the
40 * vendor driver only seems to transfer a maximum of 510 bytes at once,
41 * leaving 507 bytes for data as the command + length take up 3 bytes
42 */
43#define CH347_PACKET_SIZE 510
44#define CH347_MAX_DATA_LEN (CH347_PACKET_SIZE - 3)
45
46struct ch347_spi_data {
47 struct libusb_device_handle *handle;
Nico Hubere39549b2024-07-27 23:58:32 +020048 unsigned int iface;
Nicholas Chin197b7c72022-10-23 13:10:31 -060049};
50
51/* TODO: Add support for HID mode */
52static const struct dev_entry devs_ch347_spi[] = {
53 {0x1A86, 0x55DB, OK, "QinHeng Electronics", "USB To UART+SPI+I2C"},
Nico Huber448457a2024-07-28 00:02:54 +020054 {0x1A86, 0x55DE, OK, "QinHeng Electronics", "USB To UART+SPI+I2C+JTAG"},
Nicholas Chin197b7c72022-10-23 13:10:31 -060055 {0}
56};
57
58static int ch347_spi_shutdown(void *data)
59{
60 struct ch347_spi_data *ch347_data = data;
61
Nico Hubere39549b2024-07-27 23:58:32 +020062 libusb_release_interface(ch347_data->handle, ch347_data->iface);
63 libusb_attach_kernel_driver(ch347_data->handle, ch347_data->iface);
Nicholas Chin197b7c72022-10-23 13:10:31 -060064 libusb_close(ch347_data->handle);
65 libusb_exit(NULL);
66
67 free(data);
68 return 0;
69}
70
71static int ch347_cs_control(struct ch347_spi_data *ch347_data, uint8_t cs1, uint8_t cs2)
72{
73 uint8_t cmd[13] = {
74 [0] = CH347_CMD_SPI_CS_CTRL,
75 /* payload length, uint16 LSB: 10 */
76 [1] = 10,
77 [3] = cs1,
78 [8] = cs2
79 };
80
81 int32_t ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, cmd, sizeof(cmd), NULL, 1000);
82 if (ret < 0) {
83 msg_perr("Could not change CS!\n");
84 return -1;
85 }
86 return 0;
87}
88
89
90static int ch347_write(struct ch347_spi_data *ch347_data, unsigned int writecnt, const uint8_t *writearr)
91{
92 unsigned int data_len;
93 int packet_len;
94 int transferred;
95 int ret;
96 uint8_t resp_buf[4] = {0};
97 uint8_t buffer[CH347_PACKET_SIZE] = {0};
98 unsigned int bytes_written = 0;
99
100 while (bytes_written < writecnt) {
101 data_len = min(CH347_MAX_DATA_LEN, writecnt - bytes_written );
102 packet_len = data_len + 3;
103
104 buffer[0] = CH347_CMD_SPI_OUT;
105 buffer[1] = (data_len) & 0xFF;
106 buffer[2] = ((data_len) & 0xFF00) >> 8;
107 memcpy(buffer + 3, writearr + bytes_written, data_len);
108
109 ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, buffer, packet_len, &transferred, 1000);
110 if (ret < 0 || transferred != packet_len) {
111 msg_perr("Could not send write command\n");
112 return -1;
113 }
114
115 ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, resp_buf, sizeof(resp_buf), NULL, 1000);
116 if (ret < 0) {
117 msg_perr("Could not receive write command response\n");
118 return -1;
119 }
120 bytes_written += data_len;
121 }
122 return 0;
123}
124
125static int ch347_read(struct ch347_spi_data *ch347_data, unsigned int readcnt, uint8_t *readarr)
126{
127 uint8_t *read_ptr = readarr;
128 int ret;
129 int transferred;
130 unsigned int bytes_read = 0;
131 uint8_t buffer[CH347_PACKET_SIZE] = {0};
132 uint8_t command_buf[7] = {
133 [0] = CH347_CMD_SPI_IN,
134 [1] = 4,
135 [2] = 0,
136 [3] = readcnt & 0xFF,
137 [4] = (readcnt & 0xFF00) >> 8,
138 [5] = (readcnt & 0xFF0000) >> 16,
139 [6] = (readcnt & 0xFF000000) >> 24
140 };
141
142 ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, command_buf, sizeof(command_buf), &transferred, 1000);
143 if (ret < 0 || transferred != sizeof(command_buf)) {
144 msg_perr("Could not send read command\n");
145 return -1;
146 }
147
148 while (bytes_read < readcnt) {
149 ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, buffer, CH347_PACKET_SIZE, &transferred, 1000);
150 if (ret < 0) {
151 msg_perr("Could not read data\n");
152 return -1;
153 }
154 if (transferred > CH347_PACKET_SIZE) {
155 msg_perr("libusb bug: bytes received overflowed buffer\n");
156 return -1;
157 }
158 /* Response: u8 command, u16 data length, then the data that was read */
159 if (transferred < 3) {
160 msg_perr("CH347 returned an invalid response to read command\n");
161 return -1;
162 }
163 int ch347_data_length = read_le16(buffer, 1);
164 if (transferred - 3 < ch347_data_length) {
165 msg_perr("CH347 returned less data than data length header indicates\n");
166 return -1;
167 }
168 bytes_read += ch347_data_length;
169 if (bytes_read > readcnt) {
170 msg_perr("CH347 returned more bytes than requested\n");
171 return -1;
172 }
173 memcpy(read_ptr, buffer + 3, ch347_data_length);
174 read_ptr += ch347_data_length;
175 }
176 return 0;
177}
178
179static int ch347_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
180 unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
181{
Nico Huber9a11cbf2023-01-13 01:19:07 +0100182 struct ch347_spi_data *ch347_data = flash->mst.spi->data;
Nicholas Chin197b7c72022-10-23 13:10:31 -0600183 int ret = 0;
184
185 ch347_cs_control(ch347_data, CH347_CS_ASSERT | CH347_CS_CHANGE, CH347_CS_IGNORE);
186 if (writecnt) {
187 ret = ch347_write(ch347_data, writecnt, writearr);
188 if (ret < 0) {
189 msg_perr("CH347 write error\n");
190 return -1;
191 }
192 }
193 if (readcnt) {
194 ret = ch347_read(ch347_data, readcnt, readarr);
195 if (ret < 0) {
196 msg_perr("CH347 read error\n");
197 return -1;
198 }
199 }
200 ch347_cs_control(ch347_data, CH347_CS_DEASSERT | CH347_CS_CHANGE, CH347_CS_IGNORE);
201
202 return 0;
203}
204
205static int32_t ch347_spi_config(struct ch347_spi_data *ch347_data, uint8_t divisor)
206{
207 int32_t ret;
208 uint8_t buff[29] = {
209 [0] = CH347_CMD_SPI_SET_CFG,
210 [1] = (sizeof(buff) - 3) & 0xFF,
211 [2] = ((sizeof(buff) - 3) & 0xFF00) >> 8,
212 /* Not sure what these two bytes do, but the vendor
213 * drivers seem to unconditionally set these values
214 */
215 [5] = 4,
216 [6] = 1,
217 /* Clock polarity: bit 1 */
218 [9] = 0,
219 /* Clock phase: bit 0 */
220 [11] = 0,
221 /* Another mystery byte */
222 [14] = 2,
223 /* Clock divisor: bits 5:3 */
224 [15] = (divisor & 0x7) << 3,
225 /* Bit order: bit 7, 0=MSB */
226 [17] = 0,
227 /* Yet another mystery byte */
228 [19] = 7,
229 /* CS polarity: bit 7 CS2, bit 6 CS1. 0 = active low */
230 [24] = 0
231 };
232
233 ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, buff, sizeof(buff), NULL, 1000);
234 if (ret < 0) {
235 msg_perr("Could not configure SPI interface\n");
236 }
237
238 /* FIXME: Not sure if the CH347 sends error responses for
239 * invalid config data, if so the code should check
240 */
241 ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, buff, sizeof(buff), NULL, 1000);
242 if (ret < 0) {
243 msg_perr("Could not receive configure SPI command response\n");
244 }
245 return ret;
246}
247
248static const struct spi_master spi_master_ch347_spi = {
249 .features = SPI_MASTER_4BA,
250 .max_data_read = MAX_DATA_READ_UNLIMITED,
251 .max_data_write = MAX_DATA_WRITE_UNLIMITED,
252 .command = ch347_spi_send_command,
253 .multicommand = default_spi_send_multicommand,
254 .read = default_spi_read,
255 .write_256 = default_spi_write_256,
256 .write_aai = default_spi_write_aai,
257 .shutdown = ch347_spi_shutdown,
258 .probe_opcode = default_spi_probe_opcode,
259};
260
Nico Huberc32e9542023-02-21 00:46:37 +0000261static unsigned int ch347_div_to_khz(unsigned int div)
262{
263 /* divisor is a power of two starting from 2 for `div == 0` */
264 const unsigned int clk_khz = 120*1000;
265 return clk_khz / (1 << (div + 1));
266}
267
Nicholas Chin197b7c72022-10-23 13:10:31 -0600268/* Largely copied from ch341a_spi.c */
Nico Hubere3a26882023-01-11 21:45:51 +0100269static int ch347_spi_init(struct flashprog_programmer *const prog)
Nicholas Chin197b7c72022-10-23 13:10:31 -0600270{
271 struct ch347_spi_data *ch347_data = calloc(1, sizeof(*ch347_data));
272 if (!ch347_data) {
273 msg_perr("Could not allocate space for SPI data\n");
274 return 1;
275 }
276
Nico Huberc32e9542023-02-21 00:46:37 +0000277 unsigned int div = 3; /* Default to 7.5MHz */
278 char *const spispeed = extract_programmer_param("spispeed");
279 if (spispeed) {
280 char *endptr;
281 const unsigned long khz = strtoul(spispeed, &endptr, 10);
282 if (*endptr != '\0' || endptr == spispeed) {
283 msg_perr("Invalid `spispeed` argument, please provide the frequency in kHz.\n");
284 free(spispeed);
285 free(ch347_data);
286 return 1;
287 }
288 free(spispeed);
289
290 for (div = 0; div < 7; ++div) {
291 if (ch347_div_to_khz(div) <= khz)
292 break;
293 }
294 msg_pinfo("Using spispeed of %ukHz.\n", ch347_div_to_khz(div));
295 } else {
296 msg_pdbg("Using default spispeed of %ukHz.\n", ch347_div_to_khz(div));
297 }
298
Nicholas Chin197b7c72022-10-23 13:10:31 -0600299 int32_t ret = libusb_init(NULL);
300 if (ret < 0) {
301 msg_perr("Could not initialize libusb!\n");
302 free(ch347_data);
303 return 1;
304 }
305
306 /* Enable information, warning, and error messages (only). */
307#if LIBUSB_API_VERSION < 0x01000106
308 libusb_set_debug(NULL, 3);
309#else
310 libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
311#endif
312
Nico Huber448457a2024-07-28 00:02:54 +0200313 const struct dev_entry *dev_entry;
314 for (dev_entry = devs_ch347_spi; dev_entry->vendor_id != 0; ++dev_entry) {
315 ch347_data->handle = libusb_open_device_with_vid_pid(
316 NULL, dev_entry->vendor_id, dev_entry->device_id);
317 if (ch347_data->handle)
318 break;
319 }
Nicholas Chin197b7c72022-10-23 13:10:31 -0600320 if (ch347_data->handle == NULL) {
Nico Huber448457a2024-07-28 00:02:54 +0200321 msg_perr("Couldn't find CH347 device.\n");
Nicholas Chin197b7c72022-10-23 13:10:31 -0600322 free(ch347_data);
323 return 1;
324 }
325
Nico Hubere39549b2024-07-27 23:58:32 +0200326 struct libusb_config_descriptor *config;
327 ret = libusb_get_active_config_descriptor(libusb_get_device(ch347_data->handle), &config);
328 if (ret != LIBUSB_SUCCESS) {
329 msg_perr("Couldn't get config descriptor: %s (%d)\n", libusb_strerror(ret), ret);
330 ret = 1;
331 goto error_exit;
332 }
Nicholas Chin197b7c72022-10-23 13:10:31 -0600333
Nico Hubere39549b2024-07-27 23:58:32 +0200334 unsigned int iface;
335 for (iface = 0; iface < config->bNumInterfaces; ++iface) {
336 if (config->interface[iface].altsetting[0].bInterfaceClass == LIBUSB_CLASS_VENDOR_SPEC)
337 break;
338 }
339 if (iface == config->bNumInterfaces) {
340 msg_perr("Couldn't find compatible interface.\n");
341 ret = 1;
342 goto error_exit;
343 }
344 ch347_data->iface = iface;
345
346 ret = libusb_detach_kernel_driver(ch347_data->handle, iface);
Nicholas Chin197b7c72022-10-23 13:10:31 -0600347 if (ret != 0 && ret != LIBUSB_ERROR_NOT_FOUND)
348 msg_pwarn("Cannot detach the existing USB driver. Claiming the interface may fail. %s\n",
349 libusb_error_name(ret));
350
Nico Hubere39549b2024-07-27 23:58:32 +0200351 ret = libusb_claim_interface(ch347_data->handle, iface);
Nicholas Chin197b7c72022-10-23 13:10:31 -0600352 if (ret != 0) {
353 msg_perr("Failed to claim interface 2: '%s'\n", libusb_error_name(ret));
354 goto error_exit;
355 }
356
357 struct libusb_device *dev;
358 if (!(dev = libusb_get_device(ch347_data->handle))) {
359 msg_perr("Failed to get device from device handle.\n");
360 goto error_exit;
361 }
362
363 struct libusb_device_descriptor desc;
364 ret = libusb_get_device_descriptor(dev, &desc);
365 if (ret < 0) {
366 msg_perr("Failed to get device descriptor: '%s'\n", libusb_error_name(ret));
367 goto error_exit;
368 }
369
370 msg_pdbg("Device revision is %d.%01d.%01d\n",
371 (desc.bcdDevice >> 8) & 0x00FF,
372 (desc.bcdDevice >> 4) & 0x000F,
373 (desc.bcdDevice >> 0) & 0x000F);
374
Nico Huberc32e9542023-02-21 00:46:37 +0000375 /* TODO: add programmer cfg for things like CS pin */
376 if (ch347_spi_config(ch347_data, div) < 0)
Nicholas Chin197b7c72022-10-23 13:10:31 -0600377 goto error_exit;
378
Nico Huber89569d62023-01-12 23:31:40 +0100379 return register_spi_master(&spi_master_ch347_spi, 0, ch347_data);
Nicholas Chin197b7c72022-10-23 13:10:31 -0600380
381error_exit:
382 ch347_spi_shutdown(ch347_data);
383 return 1;
384}
385
386const struct programmer_entry programmer_ch347_spi = {
387 .name = "ch347_spi",
388 .type = USB,
389 .devs.dev = devs_ch347_spi,
390 .init = ch347_spi_init,
391};