Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2018 Linaro Limited |
| 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 | /* |
| 18 | * Bit bang driver for the 96Boards Developerbox (a.k.a. Synquacer E-series) |
| 19 | * on-board debug UART. The Developerbox implements its debug UART using a |
| 20 | * CP2102N, a USB to UART bridge which also provides four GPIO pins. On |
| 21 | * Developerbox these can be hooked up to the onboard SPI NOR FLASH and used |
| 22 | * for emergency de-brick without any additional hardware programmer. Bit |
| 23 | * banging over USB is extremely slow compared to a proper SPI programmer so |
| 24 | * this is only practical as a de-brick tool. |
| 25 | * |
| 26 | * Schematic is available here: |
| 27 | * https://www.96boards.org/documentation/enterprise/developerbox/hardware-docs/ |
| 28 | * |
| 29 | * To prepare a Developerbox for programming via the debug UART, DSW4 must be |
| 30 | * changed from the default 00000000 to 10001000 (i.e. DSW4-1 and DSW4-5 |
| 31 | * should be turned on). |
| 32 | */ |
| 33 | |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 34 | #include <stdlib.h> |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 35 | #include <libusb.h> |
| 36 | #include "programmer.h" |
Nico Huber | d16a911 | 2024-01-07 00:11:44 +0100 | [diff] [blame] | 37 | #include "bitbang_spi.h" |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 38 | #include "spi.h" |
| 39 | |
| 40 | /* Bit positions for each pin. */ |
| 41 | #define DEVELOPERBOX_SPI_SCK 0 |
| 42 | #define DEVELOPERBOX_SPI_CS 1 |
| 43 | #define DEVELOPERBOX_SPI_MISO 2 |
| 44 | #define DEVELOPERBOX_SPI_MOSI 3 |
| 45 | |
| 46 | /* Config request types */ |
| 47 | #define REQTYPE_HOST_TO_DEVICE 0x40 |
| 48 | #define REQTYPE_DEVICE_TO_HOST 0xc0 |
| 49 | |
| 50 | /* Config request codes */ |
| 51 | #define CP210X_VENDOR_SPECIFIC 0xff |
| 52 | |
| 53 | /* CP210X_VENDOR_SPECIFIC */ |
| 54 | #define CP210X_WRITE_LATCH 0x37e1 |
| 55 | #define CP210X_READ_LATCH 0x00c2 |
| 56 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 57 | static const struct dev_entry devs_developerbox_spi[] = { |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 58 | {0x10c4, 0xea60, OK, "Silicon Labs", "CP2102N USB to UART Bridge Controller"}, |
| 59 | {0}, |
| 60 | }; |
| 61 | |
Jacob Garber | afc3ad6 | 2019-06-24 16:05:28 -0600 | [diff] [blame] | 62 | static struct libusb_context *usb_ctx; |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 63 | static libusb_device_handle *cp210x_handle; |
| 64 | |
| 65 | static int cp210x_gpio_get(void) |
| 66 | { |
| 67 | int res; |
| 68 | uint8_t gpio; |
| 69 | |
| 70 | res = libusb_control_transfer(cp210x_handle, REQTYPE_DEVICE_TO_HOST, |
| 71 | CP210X_VENDOR_SPECIFIC, CP210X_READ_LATCH, |
| 72 | 0, &gpio, 1, 0); |
| 73 | if (res < 0) { |
| 74 | msg_perr("Failed to read GPIO pins (%s)\n", libusb_error_name(res)); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | return gpio; |
| 79 | } |
| 80 | |
| 81 | static void cp210x_gpio_set(uint8_t val, uint8_t mask) |
| 82 | { |
| 83 | int res; |
| 84 | uint16_t gpio; |
| 85 | |
| 86 | gpio = ((val & 0xf) << 8) | (mask & 0xf); |
| 87 | |
| 88 | /* Set relay state on the card */ |
| 89 | res = libusb_control_transfer(cp210x_handle, REQTYPE_HOST_TO_DEVICE, |
| 90 | CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH, |
| 91 | gpio, NULL, 0, 0); |
| 92 | if (res < 0) |
| 93 | msg_perr("Failed to read GPIO pins (%s)\n", libusb_error_name(res)); |
| 94 | } |
| 95 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 96 | static void cp210x_bitbang_set_cs(int val, void *spi_data) |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 97 | { |
| 98 | cp210x_gpio_set(val << DEVELOPERBOX_SPI_CS, 1 << DEVELOPERBOX_SPI_CS); |
| 99 | } |
| 100 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 101 | static void cp210x_bitbang_set_sck(int val, void *spi_data) |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 102 | { |
| 103 | cp210x_gpio_set(val << DEVELOPERBOX_SPI_SCK, 1 << DEVELOPERBOX_SPI_SCK); |
| 104 | } |
| 105 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 106 | static void cp210x_bitbang_set_mosi(int val, void *spi_data) |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 107 | { |
| 108 | cp210x_gpio_set(val << DEVELOPERBOX_SPI_MOSI, 1 << DEVELOPERBOX_SPI_MOSI); |
| 109 | } |
| 110 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 111 | static int cp210x_bitbang_get_miso(void *spi_data) |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 112 | { |
| 113 | return !!(cp210x_gpio_get() & (1 << DEVELOPERBOX_SPI_MISO)); |
| 114 | } |
| 115 | |
Anastasia Klimchuk | 0e78818 | 2021-05-26 09:54:08 +1000 | [diff] [blame] | 116 | static void cp210x_bitbang_set_sck_set_mosi(int sck, int mosi, void *spi_data) |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 117 | { |
| 118 | cp210x_gpio_set(sck << DEVELOPERBOX_SPI_SCK | mosi << DEVELOPERBOX_SPI_MOSI, |
| 119 | 1 << DEVELOPERBOX_SPI_SCK | 1 << DEVELOPERBOX_SPI_MOSI); |
| 120 | } |
| 121 | |
| 122 | static const struct bitbang_spi_master bitbang_spi_master_cp210x = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 123 | .set_cs = cp210x_bitbang_set_cs, |
| 124 | .set_sck = cp210x_bitbang_set_sck, |
| 125 | .set_mosi = cp210x_bitbang_set_mosi, |
| 126 | .get_miso = cp210x_bitbang_get_miso, |
| 127 | .set_sck_set_mosi = cp210x_bitbang_set_sck_set_mosi, |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 128 | }; |
| 129 | |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 130 | static int developerbox_spi_shutdown(void *data) |
| 131 | { |
| 132 | libusb_close(cp210x_handle); |
| 133 | libusb_exit(usb_ctx); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 138 | static int developerbox_spi_init(struct flashprog_programmer *const prog) |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 139 | { |
Thomas Heijligen | 88e87c5 | 2022-08-05 17:56:20 +0200 | [diff] [blame] | 140 | if (libusb_init(&usb_ctx)) { |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 141 | msg_perr("Could not initialize libusb!\n"); |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | char *serialno = extract_programmer_param("serial"); |
| 146 | if (serialno) |
| 147 | msg_pdbg("Looking for serial number commencing %s\n", serialno); |
Daniel Thompson | 1d507a0 | 2018-07-12 11:02:28 +0100 | [diff] [blame] | 148 | cp210x_handle = usb_dev_get_by_vid_pid_serial(usb_ctx, |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 149 | devs_developerbox_spi[0].vendor_id, devs_developerbox_spi[0].device_id, serialno); |
| 150 | free(serialno); |
| 151 | if (!cp210x_handle) { |
| 152 | msg_perr("Could not find a Developerbox programmer on USB.\n"); |
| 153 | goto err_exit; |
| 154 | } |
| 155 | |
| 156 | if (register_shutdown(developerbox_spi_shutdown, NULL)) |
| 157 | goto err_exit; |
| 158 | |
Anastasia Klimchuk | a447c12 | 2021-05-31 11:20:01 +1000 | [diff] [blame] | 159 | if (register_spi_bitbang_master(&bitbang_spi_master_cp210x, NULL)) |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 160 | goto err_exit; |
| 161 | |
| 162 | return 0; |
| 163 | |
| 164 | err_exit: |
| 165 | libusb_exit(usb_ctx); |
| 166 | return 1; |
| 167 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 168 | |
| 169 | const struct programmer_entry programmer_developerbox = { |
| 170 | .name = "developerbox", |
| 171 | .type = USB, |
| 172 | .devs.dev = devs_developerbox_spi, |
| 173 | .init = developerbox_spi_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 174 | }; |