blob: dfd98ec4160c08127d45badd2cbca698d30b8167 [file] [log] [blame]
James Lairdc60de0e2013-03-27 13:00:23 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2012 James Laird <jhl@mafipulation.org>
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; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
James Lairdc60de0e2013-03-27 13:00:23 +000014 */
15
16/*
17 * Device should be connected as per "active serial" mode:
18 *
19 * +---------+------+-----------+
20 * | SPI | Pin | Altera |
21 * +---------+------+-----------+
22 * | SCLK | 1 | DCLK |
23 * | GND | 2,10 | GND |
24 * | VCC | 4 | VCC(TRGT) |
25 * | MISO | 7 | DATAOUT |
26 * | /CS | 8 | nCS |
27 * | MOSI | 9 | ASDI |
28 * +---------+------+-----------+
29 *
30 * See also the USB-Blaster Download Cable User Guide: http://www.altera.com/literature/ug/ug_usb_blstr.pdf
31 */
32
33#if CONFIG_USBBLASTER_SPI == 1
34
35#include <stdio.h>
36#include <string.h>
37#include <stdlib.h>
38#include <ctype.h>
39#include <ftdi.h>
40#include "flash.h"
41#include "programmer.h"
42#include "spi.h"
43
44/* Please keep sorted by vendor ID, then device ID. */
45#define ALTERA_VID 0x09fb
46#define ALTERA_USBBLASTER_PID 0x6001
47
Thomas Heijligen4f5169d2021-05-04 15:32:17 +020048static const struct dev_entry devs_usbblasterspi[] = {
James Lairdc60de0e2013-03-27 13:00:23 +000049 {ALTERA_VID, ALTERA_USBBLASTER_PID, OK, "Altera", "USB-Blaster"},
50
Evgeny Zinoviev83c56b82019-11-05 17:47:43 +030051 {0}
James Lairdc60de0e2013-03-27 13:00:23 +000052};
53
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +100054struct usbblaster_spi_data {
55 struct ftdi_context ftdic;
56};
James Lairdc60de0e2013-03-27 13:00:23 +000057
58// command bytes
59#define BIT_BYTE (1<<7) // byte mode (rather than bitbang)
60#define BIT_READ (1<<6) // read request
61#define BIT_LED (1<<5)
62#define BIT_CS (1<<3)
63#define BIT_TMS (1<<1)
64#define BIT_CLK (1<<0)
65
66#define BUF_SIZE 64
67
68/* The programmer shifts bits in the wrong order for SPI, so we use this method to reverse the bits when needed.
69 * http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -060070static uint8_t reverse(uint8_t b)
James Lairdc60de0e2013-03-27 13:00:23 +000071{
72 return ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
73}
74
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +100075static int send_write(unsigned int writecnt, const unsigned char *writearr, struct ftdi_context ftdic)
James Lairdc60de0e2013-03-27 13:00:23 +000076{
Angel Ponsf41d2482021-06-07 13:29:13 +020077 uint8_t buf[BUF_SIZE] = { 0 };
James Lairdc60de0e2013-03-27 13:00:23 +000078
James Lairdc60de0e2013-03-27 13:00:23 +000079 while (writecnt) {
Nico Huber519be662018-12-23 20:03:35 +010080 unsigned int i;
James Lairdc60de0e2013-03-27 13:00:23 +000081 unsigned int n_write = min(writecnt, BUF_SIZE - 1);
82 msg_pspew("writing %d-byte packet\n", n_write);
83
84 buf[0] = BIT_BYTE | (uint8_t)n_write;
85 for (i = 0; i < n_write; i++) {
86 buf[i+1] = reverse(writearr[i]);
87 }
88 if (ftdi_write_data(&ftdic, buf, n_write + 1) < 0) {
89 msg_perr("USB-Blaster write failed\n");
90 return -1;
91 }
92
93 writearr += n_write;
94 writecnt -= n_write;
95 }
96 return 0;
97}
98
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +100099static int send_read(unsigned int readcnt, unsigned char *readarr, struct ftdi_context ftdic)
James Lairdc60de0e2013-03-27 13:00:23 +0000100{
101 int i;
102 unsigned int n_read;
Angel Ponsf41d2482021-06-07 13:29:13 +0200103 uint8_t buf[BUF_SIZE] = { 0 };
James Lairdc60de0e2013-03-27 13:00:23 +0000104
105 n_read = readcnt;
106 while (n_read) {
107 unsigned int payload_size = min(n_read, BUF_SIZE - 1);
108 msg_pspew("reading %d-byte packet\n", payload_size);
109
110 buf[0] = BIT_BYTE | BIT_READ | (uint8_t)payload_size;
111 if (ftdi_write_data(&ftdic, buf, payload_size + 1) < 0) {
112 msg_perr("USB-Blaster write failed\n");
113 return -1;
114 }
115 n_read -= payload_size;
David Hendricks17d46372020-06-23 14:16:26 -0700116 }
James Lairdc60de0e2013-03-27 13:00:23 +0000117
118 n_read = readcnt;
119 while (n_read) {
120 int ret = ftdi_read_data(&ftdic, readarr, n_read);
121 if (ret < 0) {
122 msg_perr("USB-Blaster read failed\n");
123 return -1;
124 }
125 for (i = 0; i < ret; i++) {
126 readarr[i] = reverse(readarr[i]);
127 }
128 n_read -= ret;
129 readarr += ret;
130 }
131 return 0;
132}
133
134/* Returns 0 upon success, a negative number upon errors. */
Edward O'Callaghane4ddc362020-04-12 17:27:53 +1000135static int usbblaster_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
James Lairdc60de0e2013-03-27 13:00:23 +0000136 const unsigned char *writearr, unsigned char *readarr)
137{
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000138 struct usbblaster_spi_data *usbblaster_data = flash->mst->spi.data;
James Lairdc60de0e2013-03-27 13:00:23 +0000139 uint8_t cmd;
140 int ret = 0;
141
142 cmd = BIT_LED; // asserts /CS
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000143 if (ftdi_write_data(&usbblaster_data->ftdic, &cmd, 1) < 0) {
James Lairdc60de0e2013-03-27 13:00:23 +0000144 msg_perr("USB-Blaster enable chip select failed\n");
145 ret = -1;
146 }
147
148 if (!ret && writecnt)
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000149 ret = send_write(writecnt, writearr, usbblaster_data->ftdic);
James Lairdc60de0e2013-03-27 13:00:23 +0000150
151 if (!ret && readcnt)
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000152 ret = send_read(readcnt, readarr, usbblaster_data->ftdic);
James Lairdc60de0e2013-03-27 13:00:23 +0000153
154 cmd = BIT_CS;
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000155 if (ftdi_write_data(&usbblaster_data->ftdic, &cmd, 1) < 0) {
James Lairdc60de0e2013-03-27 13:00:23 +0000156 msg_perr("USB-Blaster disable chip select failed\n");
157 ret = -1;
158 }
159
160 return ret;
161}
162
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000163static int usbblaster_shutdown(void *data)
164{
165 free(data);
166 return 0;
167}
James Lairdc60de0e2013-03-27 13:00:23 +0000168
Nico Huber90739d12021-05-11 17:53:34 +0200169static const struct spi_master spi_master_usbblaster = {
James Lairdc60de0e2013-03-27 13:00:23 +0000170 .max_data_read = 256,
171 .max_data_write = 256,
172 .command = usbblaster_spi_send_command,
173 .multicommand = default_spi_send_multicommand,
174 .read = default_spi_read,
175 .write_256 = default_spi_write_256,
176 .write_aai = default_spi_write_aai,
177};
178
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100179/* Returns 0 upon success, a negative number upon errors. */
Thomas Heijligen4f5169d2021-05-04 15:32:17 +0200180static int usbblaster_spi_init(void)
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100181{
Angel Ponsf41d2482021-06-07 13:29:13 +0200182 uint8_t buf[BUF_SIZE + 1] = { 0 };
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000183 struct ftdi_context ftdic;
184 struct usbblaster_spi_data *usbblaster_data;
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100185
186 if (ftdi_init(&ftdic) < 0)
187 return -1;
188
189 if (ftdi_usb_open(&ftdic, ALTERA_VID, ALTERA_USBBLASTER_PID) < 0) {
190 msg_perr("Failed to open USB-Blaster: %s\n", ftdic.error_str);
191 return -1;
192 }
193
194 if (ftdi_usb_reset(&ftdic) < 0) {
195 msg_perr("USB-Blaster reset failed\n");
196 return -1;
197 }
198
199 if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
200 msg_perr("USB-Blaster set latency timer failed\n");
201 return -1;
202 }
203
204 if (ftdi_write_data_set_chunksize(&ftdic, 4096) < 0 ||
205 ftdi_read_data_set_chunksize(&ftdic, BUF_SIZE) < 0) {
206 msg_perr("USB-Blaster set chunk size failed\n");
207 return -1;
208 }
209
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100210 buf[sizeof(buf)-1] = BIT_LED | BIT_CS;
211 if (ftdi_write_data(&ftdic, buf, sizeof(buf)) < 0) {
212 msg_perr("USB-Blaster reset write failed\n");
213 return -1;
214 }
215 if (ftdi_read_data(&ftdic, buf, sizeof(buf)) < 0) {
216 msg_perr("USB-Blaster reset read failed\n");
217 return -1;
218 }
219
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000220 usbblaster_data = calloc(1, sizeof(*usbblaster_data));
221 if (!usbblaster_data) {
222 msg_perr("Unable to allocate space for SPI master data\n");
223 return -1;
224 }
225 usbblaster_data->ftdic = ftdic;
Anastasia Klimchuke1e9e2b2021-05-11 11:27:55 +1000226
227 if (register_shutdown(usbblaster_shutdown, usbblaster_data)) {
228 free(usbblaster_data);
229 return -1;
230 }
Nico Huber90739d12021-05-11 17:53:34 +0200231 register_spi_master(&spi_master_usbblaster, usbblaster_data);
Anastasia Klimchuke7045832021-03-23 16:34:07 +1100232 return 0;
233}
234
Thomas Heijligen4f5169d2021-05-04 15:32:17 +0200235const struct programmer_entry programmer_usbblaster_spi = {
236 .name = "usbblaster_spi",
237 .type = USB,
238 .devs.dev = devs_usbblasterspi,
239 .init = usbblaster_spi_init,
240 .map_flash_region = fallback_map,
241 .unmap_flash_region = fallback_unmap,
242 .delay = internal_delay,
243};
244
James Lairdc60de0e2013-03-27 13:00:23 +0000245#endif