blob: 3a9059e83ef77311692b08a83f7d4883c77b1ba6 [file] [log] [blame]
Daniel Thompson45e91a22018-06-04 13:46:29 +01001/*
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 Thompson45e91a22018-06-04 13:46:29 +010034#include <stdlib.h>
Daniel Thompson45e91a22018-06-04 13:46:29 +010035#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 Heijligencc853d82021-05-04 15:32:17 +020056static const struct dev_entry devs_developerbox_spi[] = {
Daniel Thompson45e91a22018-06-04 13:46:29 +010057 {0x10c4, 0xea60, OK, "Silicon Labs", "CP2102N USB to UART Bridge Controller"},
58 {0},
59};
60
Jacob Garberafc3ad62019-06-24 16:05:28 -060061static struct libusb_context *usb_ctx;
Daniel Thompson45e91a22018-06-04 13:46:29 +010062static libusb_device_handle *cp210x_handle;
63
64static 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
80static 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
Anastasia Klimchuk0e788182021-05-26 09:54:08 +100095static void cp210x_bitbang_set_cs(int val, void *spi_data)
Daniel Thompson45e91a22018-06-04 13:46:29 +010096{
97 cp210x_gpio_set(val << DEVELOPERBOX_SPI_CS, 1 << DEVELOPERBOX_SPI_CS);
98}
99
Anastasia Klimchuk0e788182021-05-26 09:54:08 +1000100static void cp210x_bitbang_set_sck(int val, void *spi_data)
Daniel Thompson45e91a22018-06-04 13:46:29 +0100101{
102 cp210x_gpio_set(val << DEVELOPERBOX_SPI_SCK, 1 << DEVELOPERBOX_SPI_SCK);
103}
104
Anastasia Klimchuk0e788182021-05-26 09:54:08 +1000105static void cp210x_bitbang_set_mosi(int val, void *spi_data)
Daniel Thompson45e91a22018-06-04 13:46:29 +0100106{
107 cp210x_gpio_set(val << DEVELOPERBOX_SPI_MOSI, 1 << DEVELOPERBOX_SPI_MOSI);
108}
109
Anastasia Klimchuk0e788182021-05-26 09:54:08 +1000110static int cp210x_bitbang_get_miso(void *spi_data)
Daniel Thompson45e91a22018-06-04 13:46:29 +0100111{
112 return !!(cp210x_gpio_get() & (1 << DEVELOPERBOX_SPI_MISO));
113}
114
Anastasia Klimchuk0e788182021-05-26 09:54:08 +1000115static void cp210x_bitbang_set_sck_set_mosi(int sck, int mosi, void *spi_data)
Daniel Thompson45e91a22018-06-04 13:46:29 +0100116{
117 cp210x_gpio_set(sck << DEVELOPERBOX_SPI_SCK | mosi << DEVELOPERBOX_SPI_MOSI,
118 1 << DEVELOPERBOX_SPI_SCK | 1 << DEVELOPERBOX_SPI_MOSI);
119}
120
121static const struct bitbang_spi_master bitbang_spi_master_cp210x = {
Thomas Heijligen43040f22022-06-23 14:38:35 +0200122 .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 Thompson45e91a22018-06-04 13:46:29 +0100127};
128
Daniel Thompson45e91a22018-06-04 13:46:29 +0100129static int developerbox_spi_shutdown(void *data)
130{
131 libusb_close(cp210x_handle);
132 libusb_exit(usb_ctx);
133
134 return 0;
135}
136
Nico Hubere3a26882023-01-11 21:45:51 +0100137static int developerbox_spi_init(struct flashprog_programmer *const prog)
Daniel Thompson45e91a22018-06-04 13:46:29 +0100138{
Thomas Heijligen88e87c52022-08-05 17:56:20 +0200139 if (libusb_init(&usb_ctx)) {
Daniel Thompson45e91a22018-06-04 13:46:29 +0100140 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 Thompson1d507a02018-07-12 11:02:28 +0100147 cp210x_handle = usb_dev_get_by_vid_pid_serial(usb_ctx,
Daniel Thompson45e91a22018-06-04 13:46:29 +0100148 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
Anastasia Klimchuka447c122021-05-31 11:20:01 +1000158 if (register_spi_bitbang_master(&bitbang_spi_master_cp210x, NULL))
Daniel Thompson45e91a22018-06-04 13:46:29 +0100159 goto err_exit;
160
161 return 0;
162
163err_exit:
164 libusb_exit(usb_ctx);
165 return 1;
166}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200167
168const struct programmer_entry programmer_developerbox = {
169 .name = "developerbox",
170 .type = USB,
171 .devs.dev = devs_developerbox_spi,
172 .init = developerbox_spi_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200173};