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" |
| 37 | #include "spi.h" |
| 38 | |
| 39 | /* Bit positions for each pin. */ |
| 40 | #define DEVELOPERBOX_SPI_SCK 0 |
| 41 | #define DEVELOPERBOX_SPI_CS 1 |
| 42 | #define DEVELOPERBOX_SPI_MISO 2 |
| 43 | #define DEVELOPERBOX_SPI_MOSI 3 |
| 44 | |
| 45 | /* Config request types */ |
| 46 | #define REQTYPE_HOST_TO_DEVICE 0x40 |
| 47 | #define REQTYPE_DEVICE_TO_HOST 0xc0 |
| 48 | |
| 49 | /* Config request codes */ |
| 50 | #define CP210X_VENDOR_SPECIFIC 0xff |
| 51 | |
| 52 | /* CP210X_VENDOR_SPECIFIC */ |
| 53 | #define CP210X_WRITE_LATCH 0x37e1 |
| 54 | #define CP210X_READ_LATCH 0x00c2 |
| 55 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 56 | static const struct dev_entry devs_developerbox_spi[] = { |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 57 | {0x10c4, 0xea60, OK, "Silicon Labs", "CP2102N USB to UART Bridge Controller"}, |
| 58 | {0}, |
| 59 | }; |
| 60 | |
Jacob Garber | afc3ad6 | 2019-06-24 16:05:28 -0600 | [diff] [blame] | 61 | static struct libusb_context *usb_ctx; |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 62 | static libusb_device_handle *cp210x_handle; |
| 63 | |
| 64 | static int cp210x_gpio_get(void) |
| 65 | { |
| 66 | int res; |
| 67 | uint8_t gpio; |
| 68 | |
| 69 | res = libusb_control_transfer(cp210x_handle, REQTYPE_DEVICE_TO_HOST, |
| 70 | CP210X_VENDOR_SPECIFIC, CP210X_READ_LATCH, |
| 71 | 0, &gpio, 1, 0); |
| 72 | if (res < 0) { |
| 73 | msg_perr("Failed to read GPIO pins (%s)\n", libusb_error_name(res)); |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | return gpio; |
| 78 | } |
| 79 | |
| 80 | static void cp210x_gpio_set(uint8_t val, uint8_t mask) |
| 81 | { |
| 82 | int res; |
| 83 | uint16_t gpio; |
| 84 | |
| 85 | gpio = ((val & 0xf) << 8) | (mask & 0xf); |
| 86 | |
| 87 | /* Set relay state on the card */ |
| 88 | res = libusb_control_transfer(cp210x_handle, REQTYPE_HOST_TO_DEVICE, |
| 89 | CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH, |
| 90 | gpio, NULL, 0, 0); |
| 91 | if (res < 0) |
| 92 | msg_perr("Failed to read GPIO pins (%s)\n", libusb_error_name(res)); |
| 93 | } |
| 94 | |
| 95 | static void cp210x_bitbang_set_cs(int val) |
| 96 | { |
| 97 | cp210x_gpio_set(val << DEVELOPERBOX_SPI_CS, 1 << DEVELOPERBOX_SPI_CS); |
| 98 | } |
| 99 | |
| 100 | static void cp210x_bitbang_set_sck(int val) |
| 101 | { |
| 102 | cp210x_gpio_set(val << DEVELOPERBOX_SPI_SCK, 1 << DEVELOPERBOX_SPI_SCK); |
| 103 | } |
| 104 | |
| 105 | static void cp210x_bitbang_set_mosi(int val) |
| 106 | { |
| 107 | cp210x_gpio_set(val << DEVELOPERBOX_SPI_MOSI, 1 << DEVELOPERBOX_SPI_MOSI); |
| 108 | } |
| 109 | |
| 110 | static int cp210x_bitbang_get_miso(void) |
| 111 | { |
| 112 | return !!(cp210x_gpio_get() & (1 << DEVELOPERBOX_SPI_MISO)); |
| 113 | } |
| 114 | |
| 115 | static void cp210x_bitbang_set_sck_set_mosi(int sck, int mosi) |
| 116 | { |
| 117 | cp210x_gpio_set(sck << DEVELOPERBOX_SPI_SCK | mosi << DEVELOPERBOX_SPI_MOSI, |
| 118 | 1 << DEVELOPERBOX_SPI_SCK | 1 << DEVELOPERBOX_SPI_MOSI); |
| 119 | } |
| 120 | |
| 121 | static const struct bitbang_spi_master bitbang_spi_master_cp210x = { |
Thomas Heijligen | 43040f2 | 2022-06-23 14:38:35 +0200 | [diff] [blame] | 122 | .set_cs = cp210x_bitbang_set_cs, |
| 123 | .set_sck = cp210x_bitbang_set_sck, |
| 124 | .set_mosi = cp210x_bitbang_set_mosi, |
| 125 | .get_miso = cp210x_bitbang_get_miso, |
| 126 | .set_sck_set_mosi = cp210x_bitbang_set_sck_set_mosi, |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 127 | }; |
| 128 | |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 129 | static int developerbox_spi_shutdown(void *data) |
| 130 | { |
| 131 | libusb_close(cp210x_handle); |
| 132 | libusb_exit(usb_ctx); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 137 | static int developerbox_spi_init(void) |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 138 | { |
Thomas Heijligen | 88e87c5 | 2022-08-05 17:56:20 +0200 | [diff] [blame] | 139 | if (libusb_init(&usb_ctx)) { |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 140 | msg_perr("Could not initialize libusb!\n"); |
| 141 | return 1; |
| 142 | } |
| 143 | |
| 144 | char *serialno = extract_programmer_param("serial"); |
| 145 | if (serialno) |
| 146 | msg_pdbg("Looking for serial number commencing %s\n", serialno); |
Daniel Thompson | 1d507a0 | 2018-07-12 11:02:28 +0100 | [diff] [blame] | 147 | cp210x_handle = usb_dev_get_by_vid_pid_serial(usb_ctx, |
Daniel Thompson | 45e91a2 | 2018-06-04 13:46:29 +0100 | [diff] [blame] | 148 | devs_developerbox_spi[0].vendor_id, devs_developerbox_spi[0].device_id, serialno); |
| 149 | free(serialno); |
| 150 | if (!cp210x_handle) { |
| 151 | msg_perr("Could not find a Developerbox programmer on USB.\n"); |
| 152 | goto err_exit; |
| 153 | } |
| 154 | |
| 155 | if (register_shutdown(developerbox_spi_shutdown, NULL)) |
| 156 | goto err_exit; |
| 157 | |
| 158 | if (register_spi_bitbang_master(&bitbang_spi_master_cp210x)) |
| 159 | goto err_exit; |
| 160 | |
| 161 | return 0; |
| 162 | |
| 163 | err_exit: |
| 164 | libusb_exit(usb_ctx); |
| 165 | return 1; |
| 166 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 167 | |
| 168 | const struct programmer_entry programmer_developerbox = { |
| 169 | .name = "developerbox", |
| 170 | .type = USB, |
| 171 | .devs.dev = devs_developerbox_spi, |
| 172 | .init = developerbox_spi_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 173 | }; |