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