Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 1 | /* |
| 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 Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 39 | /* 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 | |
| 46 | struct ch347_spi_data { |
| 47 | struct libusb_device_handle *handle; |
Nico Huber | e39549b | 2024-07-27 23:58:32 +0200 | [diff] [blame^] | 48 | unsigned int iface; |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | /* TODO: Add support for HID mode */ |
| 52 | static const struct dev_entry devs_ch347_spi[] = { |
| 53 | {0x1A86, 0x55DB, OK, "QinHeng Electronics", "USB To UART+SPI+I2C"}, |
| 54 | {0} |
| 55 | }; |
| 56 | |
| 57 | static int ch347_spi_shutdown(void *data) |
| 58 | { |
| 59 | struct ch347_spi_data *ch347_data = data; |
| 60 | |
Nico Huber | e39549b | 2024-07-27 23:58:32 +0200 | [diff] [blame^] | 61 | libusb_release_interface(ch347_data->handle, ch347_data->iface); |
| 62 | libusb_attach_kernel_driver(ch347_data->handle, ch347_data->iface); |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 63 | libusb_close(ch347_data->handle); |
| 64 | libusb_exit(NULL); |
| 65 | |
| 66 | free(data); |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int ch347_cs_control(struct ch347_spi_data *ch347_data, uint8_t cs1, uint8_t cs2) |
| 71 | { |
| 72 | uint8_t cmd[13] = { |
| 73 | [0] = CH347_CMD_SPI_CS_CTRL, |
| 74 | /* payload length, uint16 LSB: 10 */ |
| 75 | [1] = 10, |
| 76 | [3] = cs1, |
| 77 | [8] = cs2 |
| 78 | }; |
| 79 | |
| 80 | int32_t ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, cmd, sizeof(cmd), NULL, 1000); |
| 81 | if (ret < 0) { |
| 82 | msg_perr("Could not change CS!\n"); |
| 83 | return -1; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | static int ch347_write(struct ch347_spi_data *ch347_data, unsigned int writecnt, const uint8_t *writearr) |
| 90 | { |
| 91 | unsigned int data_len; |
| 92 | int packet_len; |
| 93 | int transferred; |
| 94 | int ret; |
| 95 | uint8_t resp_buf[4] = {0}; |
| 96 | uint8_t buffer[CH347_PACKET_SIZE] = {0}; |
| 97 | unsigned int bytes_written = 0; |
| 98 | |
| 99 | while (bytes_written < writecnt) { |
| 100 | data_len = min(CH347_MAX_DATA_LEN, writecnt - bytes_written ); |
| 101 | packet_len = data_len + 3; |
| 102 | |
| 103 | buffer[0] = CH347_CMD_SPI_OUT; |
| 104 | buffer[1] = (data_len) & 0xFF; |
| 105 | buffer[2] = ((data_len) & 0xFF00) >> 8; |
| 106 | memcpy(buffer + 3, writearr + bytes_written, data_len); |
| 107 | |
| 108 | ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, buffer, packet_len, &transferred, 1000); |
| 109 | if (ret < 0 || transferred != packet_len) { |
| 110 | msg_perr("Could not send write command\n"); |
| 111 | return -1; |
| 112 | } |
| 113 | |
| 114 | ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, resp_buf, sizeof(resp_buf), NULL, 1000); |
| 115 | if (ret < 0) { |
| 116 | msg_perr("Could not receive write command response\n"); |
| 117 | return -1; |
| 118 | } |
| 119 | bytes_written += data_len; |
| 120 | } |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int ch347_read(struct ch347_spi_data *ch347_data, unsigned int readcnt, uint8_t *readarr) |
| 125 | { |
| 126 | uint8_t *read_ptr = readarr; |
| 127 | int ret; |
| 128 | int transferred; |
| 129 | unsigned int bytes_read = 0; |
| 130 | uint8_t buffer[CH347_PACKET_SIZE] = {0}; |
| 131 | uint8_t command_buf[7] = { |
| 132 | [0] = CH347_CMD_SPI_IN, |
| 133 | [1] = 4, |
| 134 | [2] = 0, |
| 135 | [3] = readcnt & 0xFF, |
| 136 | [4] = (readcnt & 0xFF00) >> 8, |
| 137 | [5] = (readcnt & 0xFF0000) >> 16, |
| 138 | [6] = (readcnt & 0xFF000000) >> 24 |
| 139 | }; |
| 140 | |
| 141 | ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, command_buf, sizeof(command_buf), &transferred, 1000); |
| 142 | if (ret < 0 || transferred != sizeof(command_buf)) { |
| 143 | msg_perr("Could not send read command\n"); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | while (bytes_read < readcnt) { |
| 148 | ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, buffer, CH347_PACKET_SIZE, &transferred, 1000); |
| 149 | if (ret < 0) { |
| 150 | msg_perr("Could not read data\n"); |
| 151 | return -1; |
| 152 | } |
| 153 | if (transferred > CH347_PACKET_SIZE) { |
| 154 | msg_perr("libusb bug: bytes received overflowed buffer\n"); |
| 155 | return -1; |
| 156 | } |
| 157 | /* Response: u8 command, u16 data length, then the data that was read */ |
| 158 | if (transferred < 3) { |
| 159 | msg_perr("CH347 returned an invalid response to read command\n"); |
| 160 | return -1; |
| 161 | } |
| 162 | int ch347_data_length = read_le16(buffer, 1); |
| 163 | if (transferred - 3 < ch347_data_length) { |
| 164 | msg_perr("CH347 returned less data than data length header indicates\n"); |
| 165 | return -1; |
| 166 | } |
| 167 | bytes_read += ch347_data_length; |
| 168 | if (bytes_read > readcnt) { |
| 169 | msg_perr("CH347 returned more bytes than requested\n"); |
| 170 | return -1; |
| 171 | } |
| 172 | memcpy(read_ptr, buffer + 3, ch347_data_length); |
| 173 | read_ptr += ch347_data_length; |
| 174 | } |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | static int ch347_spi_send_command(const struct flashctx *flash, unsigned int writecnt, |
| 179 | unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr) |
| 180 | { |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 181 | struct ch347_spi_data *ch347_data = flash->mst.spi->data; |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 182 | int ret = 0; |
| 183 | |
| 184 | ch347_cs_control(ch347_data, CH347_CS_ASSERT | CH347_CS_CHANGE, CH347_CS_IGNORE); |
| 185 | if (writecnt) { |
| 186 | ret = ch347_write(ch347_data, writecnt, writearr); |
| 187 | if (ret < 0) { |
| 188 | msg_perr("CH347 write error\n"); |
| 189 | return -1; |
| 190 | } |
| 191 | } |
| 192 | if (readcnt) { |
| 193 | ret = ch347_read(ch347_data, readcnt, readarr); |
| 194 | if (ret < 0) { |
| 195 | msg_perr("CH347 read error\n"); |
| 196 | return -1; |
| 197 | } |
| 198 | } |
| 199 | ch347_cs_control(ch347_data, CH347_CS_DEASSERT | CH347_CS_CHANGE, CH347_CS_IGNORE); |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | static int32_t ch347_spi_config(struct ch347_spi_data *ch347_data, uint8_t divisor) |
| 205 | { |
| 206 | int32_t ret; |
| 207 | uint8_t buff[29] = { |
| 208 | [0] = CH347_CMD_SPI_SET_CFG, |
| 209 | [1] = (sizeof(buff) - 3) & 0xFF, |
| 210 | [2] = ((sizeof(buff) - 3) & 0xFF00) >> 8, |
| 211 | /* Not sure what these two bytes do, but the vendor |
| 212 | * drivers seem to unconditionally set these values |
| 213 | */ |
| 214 | [5] = 4, |
| 215 | [6] = 1, |
| 216 | /* Clock polarity: bit 1 */ |
| 217 | [9] = 0, |
| 218 | /* Clock phase: bit 0 */ |
| 219 | [11] = 0, |
| 220 | /* Another mystery byte */ |
| 221 | [14] = 2, |
| 222 | /* Clock divisor: bits 5:3 */ |
| 223 | [15] = (divisor & 0x7) << 3, |
| 224 | /* Bit order: bit 7, 0=MSB */ |
| 225 | [17] = 0, |
| 226 | /* Yet another mystery byte */ |
| 227 | [19] = 7, |
| 228 | /* CS polarity: bit 7 CS2, bit 6 CS1. 0 = active low */ |
| 229 | [24] = 0 |
| 230 | }; |
| 231 | |
| 232 | ret = libusb_bulk_transfer(ch347_data->handle, WRITE_EP, buff, sizeof(buff), NULL, 1000); |
| 233 | if (ret < 0) { |
| 234 | msg_perr("Could not configure SPI interface\n"); |
| 235 | } |
| 236 | |
| 237 | /* FIXME: Not sure if the CH347 sends error responses for |
| 238 | * invalid config data, if so the code should check |
| 239 | */ |
| 240 | ret = libusb_bulk_transfer(ch347_data->handle, READ_EP, buff, sizeof(buff), NULL, 1000); |
| 241 | if (ret < 0) { |
| 242 | msg_perr("Could not receive configure SPI command response\n"); |
| 243 | } |
| 244 | return ret; |
| 245 | } |
| 246 | |
| 247 | static const struct spi_master spi_master_ch347_spi = { |
| 248 | .features = SPI_MASTER_4BA, |
| 249 | .max_data_read = MAX_DATA_READ_UNLIMITED, |
| 250 | .max_data_write = MAX_DATA_WRITE_UNLIMITED, |
| 251 | .command = ch347_spi_send_command, |
| 252 | .multicommand = default_spi_send_multicommand, |
| 253 | .read = default_spi_read, |
| 254 | .write_256 = default_spi_write_256, |
| 255 | .write_aai = default_spi_write_aai, |
| 256 | .shutdown = ch347_spi_shutdown, |
| 257 | .probe_opcode = default_spi_probe_opcode, |
| 258 | }; |
| 259 | |
Nico Huber | c32e954 | 2023-02-21 00:46:37 +0000 | [diff] [blame] | 260 | static unsigned int ch347_div_to_khz(unsigned int div) |
| 261 | { |
| 262 | /* divisor is a power of two starting from 2 for `div == 0` */ |
| 263 | const unsigned int clk_khz = 120*1000; |
| 264 | return clk_khz / (1 << (div + 1)); |
| 265 | } |
| 266 | |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 267 | /* Largely copied from ch341a_spi.c */ |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 268 | static int ch347_spi_init(struct flashprog_programmer *const prog) |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 269 | { |
| 270 | struct ch347_spi_data *ch347_data = calloc(1, sizeof(*ch347_data)); |
| 271 | if (!ch347_data) { |
| 272 | msg_perr("Could not allocate space for SPI data\n"); |
| 273 | return 1; |
| 274 | } |
| 275 | |
Nico Huber | c32e954 | 2023-02-21 00:46:37 +0000 | [diff] [blame] | 276 | unsigned int div = 3; /* Default to 7.5MHz */ |
| 277 | char *const spispeed = extract_programmer_param("spispeed"); |
| 278 | if (spispeed) { |
| 279 | char *endptr; |
| 280 | const unsigned long khz = strtoul(spispeed, &endptr, 10); |
| 281 | if (*endptr != '\0' || endptr == spispeed) { |
| 282 | msg_perr("Invalid `spispeed` argument, please provide the frequency in kHz.\n"); |
| 283 | free(spispeed); |
| 284 | free(ch347_data); |
| 285 | return 1; |
| 286 | } |
| 287 | free(spispeed); |
| 288 | |
| 289 | for (div = 0; div < 7; ++div) { |
| 290 | if (ch347_div_to_khz(div) <= khz) |
| 291 | break; |
| 292 | } |
| 293 | msg_pinfo("Using spispeed of %ukHz.\n", ch347_div_to_khz(div)); |
| 294 | } else { |
| 295 | msg_pdbg("Using default spispeed of %ukHz.\n", ch347_div_to_khz(div)); |
| 296 | } |
| 297 | |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 298 | int32_t ret = libusb_init(NULL); |
| 299 | if (ret < 0) { |
| 300 | msg_perr("Could not initialize libusb!\n"); |
| 301 | free(ch347_data); |
| 302 | return 1; |
| 303 | } |
| 304 | |
| 305 | /* Enable information, warning, and error messages (only). */ |
| 306 | #if LIBUSB_API_VERSION < 0x01000106 |
| 307 | libusb_set_debug(NULL, 3); |
| 308 | #else |
| 309 | libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); |
| 310 | #endif |
| 311 | |
| 312 | uint16_t vid = devs_ch347_spi[0].vendor_id; |
| 313 | uint16_t pid = devs_ch347_spi[0].device_id; |
| 314 | ch347_data->handle = libusb_open_device_with_vid_pid(NULL, vid, pid); |
| 315 | if (ch347_data->handle == NULL) { |
| 316 | msg_perr("Couldn't open device %04x:%04x.\n", vid, pid); |
| 317 | free(ch347_data); |
| 318 | return 1; |
| 319 | } |
| 320 | |
Nico Huber | e39549b | 2024-07-27 23:58:32 +0200 | [diff] [blame^] | 321 | struct libusb_config_descriptor *config; |
| 322 | ret = libusb_get_active_config_descriptor(libusb_get_device(ch347_data->handle), &config); |
| 323 | if (ret != LIBUSB_SUCCESS) { |
| 324 | msg_perr("Couldn't get config descriptor: %s (%d)\n", libusb_strerror(ret), ret); |
| 325 | ret = 1; |
| 326 | goto error_exit; |
| 327 | } |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 328 | |
Nico Huber | e39549b | 2024-07-27 23:58:32 +0200 | [diff] [blame^] | 329 | unsigned int iface; |
| 330 | for (iface = 0; iface < config->bNumInterfaces; ++iface) { |
| 331 | if (config->interface[iface].altsetting[0].bInterfaceClass == LIBUSB_CLASS_VENDOR_SPEC) |
| 332 | break; |
| 333 | } |
| 334 | if (iface == config->bNumInterfaces) { |
| 335 | msg_perr("Couldn't find compatible interface.\n"); |
| 336 | ret = 1; |
| 337 | goto error_exit; |
| 338 | } |
| 339 | ch347_data->iface = iface; |
| 340 | |
| 341 | ret = libusb_detach_kernel_driver(ch347_data->handle, iface); |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 342 | if (ret != 0 && ret != LIBUSB_ERROR_NOT_FOUND) |
| 343 | msg_pwarn("Cannot detach the existing USB driver. Claiming the interface may fail. %s\n", |
| 344 | libusb_error_name(ret)); |
| 345 | |
Nico Huber | e39549b | 2024-07-27 23:58:32 +0200 | [diff] [blame^] | 346 | ret = libusb_claim_interface(ch347_data->handle, iface); |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 347 | if (ret != 0) { |
| 348 | msg_perr("Failed to claim interface 2: '%s'\n", libusb_error_name(ret)); |
| 349 | goto error_exit; |
| 350 | } |
| 351 | |
| 352 | struct libusb_device *dev; |
| 353 | if (!(dev = libusb_get_device(ch347_data->handle))) { |
| 354 | msg_perr("Failed to get device from device handle.\n"); |
| 355 | goto error_exit; |
| 356 | } |
| 357 | |
| 358 | struct libusb_device_descriptor desc; |
| 359 | ret = libusb_get_device_descriptor(dev, &desc); |
| 360 | if (ret < 0) { |
| 361 | msg_perr("Failed to get device descriptor: '%s'\n", libusb_error_name(ret)); |
| 362 | goto error_exit; |
| 363 | } |
| 364 | |
| 365 | msg_pdbg("Device revision is %d.%01d.%01d\n", |
| 366 | (desc.bcdDevice >> 8) & 0x00FF, |
| 367 | (desc.bcdDevice >> 4) & 0x000F, |
| 368 | (desc.bcdDevice >> 0) & 0x000F); |
| 369 | |
Nico Huber | c32e954 | 2023-02-21 00:46:37 +0000 | [diff] [blame] | 370 | /* TODO: add programmer cfg for things like CS pin */ |
| 371 | if (ch347_spi_config(ch347_data, div) < 0) |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 372 | goto error_exit; |
| 373 | |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 374 | return register_spi_master(&spi_master_ch347_spi, 0, ch347_data); |
Nicholas Chin | 197b7c7 | 2022-10-23 13:10:31 -0600 | [diff] [blame] | 375 | |
| 376 | error_exit: |
| 377 | ch347_spi_shutdown(ch347_data); |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | const struct programmer_entry programmer_ch347_spi = { |
| 382 | .name = "ch347_spi", |
| 383 | .type = USB, |
| 384 | .devs.dev = devs_ch347_spi, |
| 385 | .init = ch347_spi_init, |
| 386 | }; |