xref: /aosp_15_r20/external/flashrom/usbblaster_spi.c (revision 0d6140be3aa665ecc836e8907834fcd3e3b018fc)
1 /*
2  * This file is part of the flashrom project.
3  *
4  * Copyright (C) 2012 James Laird <[email protected]>
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.
14  */
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 #include <stdio.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <ftdi.h>
38 #include "flash.h"
39 #include "programmer.h"
40 #include "spi.h"
41 
42 /* Please keep sorted by vendor ID, then device ID. */
43 #define ALTERA_VID		0x09fb
44 #define ALTERA_USBBLASTER_PID	0x6001
45 
46 static const struct dev_entry devs_usbblasterspi[] = {
47 	{ALTERA_VID, ALTERA_USBBLASTER_PID, OK, "Altera", "USB-Blaster"},
48 
49 	{0}
50 };
51 
52 struct usbblaster_spi_data {
53 	struct ftdi_context ftdic;
54 };
55 
56 // command bytes
57 #define BIT_BYTE	(1<<7)	// byte mode (rather than bitbang)
58 #define BIT_READ	(1<<6)	// read request
59 #define BIT_LED		(1<<5)
60 #define BIT_CS		(1<<3)
61 #define BIT_TMS		(1<<1)
62 #define BIT_CLK		(1<<0)
63 
64 #define BUF_SIZE	64
65 
66 /* The programmer shifts bits in the wrong order for SPI, so we use this method to reverse the bits when needed.
67  * http://graphics.stanford.edu/~seander/bithacks.html#ReverseByteWith32Bits */
reverse(uint8_t b)68 static uint8_t reverse(uint8_t b)
69 {
70 	return ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16;
71 }
72 
send_write(unsigned int writecnt,const unsigned char * writearr,struct ftdi_context ftdic)73 static int send_write(unsigned int writecnt, const unsigned char *writearr, struct ftdi_context ftdic)
74 {
75 	uint8_t buf[BUF_SIZE] = { 0 };
76 
77 	while (writecnt) {
78 		unsigned int i;
79 		unsigned int n_write = min(writecnt, BUF_SIZE - 1);
80 		msg_pspew("writing %d-byte packet\n", n_write);
81 
82 		buf[0] = BIT_BYTE | (uint8_t)n_write;
83 		for (i = 0; i < n_write; i++) {
84 			buf[i+1] = reverse(writearr[i]);
85 		}
86 		if (ftdi_write_data(&ftdic, buf, n_write + 1) < 0) {
87 			msg_perr("USB-Blaster write failed\n");
88 			return -1;
89 		}
90 
91 		writearr += n_write;
92 		writecnt -= n_write;
93 	}
94 	return 0;
95 }
96 
send_read(unsigned int readcnt,unsigned char * readarr,struct ftdi_context ftdic)97 static int send_read(unsigned int readcnt, unsigned char *readarr, struct ftdi_context ftdic)
98 {
99 	int i;
100 	unsigned int n_read;
101 	uint8_t buf[BUF_SIZE] = { 0 };
102 
103 	n_read = readcnt;
104 	while (n_read) {
105 		unsigned int payload_size = min(n_read, BUF_SIZE - 1);
106 		msg_pspew("reading %d-byte packet\n", payload_size);
107 
108 		buf[0] = BIT_BYTE | BIT_READ | (uint8_t)payload_size;
109 		if (ftdi_write_data(&ftdic, buf, payload_size + 1) < 0) {
110 			msg_perr("USB-Blaster write failed\n");
111 			return -1;
112 		}
113 		n_read -= payload_size;
114 	}
115 
116 	n_read = readcnt;
117 	while (n_read) {
118 		int ret = ftdi_read_data(&ftdic, readarr, n_read);
119 		if (ret < 0) {
120 			msg_perr("USB-Blaster read failed\n");
121 			return -1;
122 		}
123 		for (i = 0; i < ret; i++) {
124 			readarr[i] = reverse(readarr[i]);
125 		}
126 		n_read -= ret;
127 		readarr += ret;
128 	}
129 	return 0;
130 }
131 
132 /* Returns 0 upon success, a negative number upon errors. */
usbblaster_spi_send_command(const struct flashctx * flash,unsigned int writecnt,unsigned int readcnt,const unsigned char * writearr,unsigned char * readarr)133 static int usbblaster_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
134 				       const unsigned char *writearr, unsigned char *readarr)
135 {
136 	struct usbblaster_spi_data *usbblaster_data = flash->mst->spi.data;
137 	uint8_t cmd;
138 	int ret = 0;
139 
140 	cmd = BIT_LED; // asserts /CS
141 	if (ftdi_write_data(&usbblaster_data->ftdic, &cmd, 1) < 0) {
142 		msg_perr("USB-Blaster enable chip select failed\n");
143 		ret = -1;
144 	}
145 
146 	if (!ret && writecnt)
147 		ret = send_write(writecnt, writearr, usbblaster_data->ftdic);
148 
149 	if (!ret && readcnt)
150 		ret = send_read(readcnt, readarr, usbblaster_data->ftdic);
151 
152 	cmd = BIT_CS;
153 	if (ftdi_write_data(&usbblaster_data->ftdic, &cmd, 1) < 0) {
154 		msg_perr("USB-Blaster disable chip select failed\n");
155 		ret = -1;
156 	}
157 
158 	return ret;
159 }
160 
usbblaster_shutdown(void * data)161 static int usbblaster_shutdown(void *data)
162 {
163 	free(data);
164 	return 0;
165 }
166 
167 static const struct spi_master spi_master_usbblaster = {
168 	.max_data_read	= 256,
169 	.max_data_write	= 256,
170 	.command	= usbblaster_spi_send_command,
171 	.read		= default_spi_read,
172 	.write_256	= default_spi_write_256,
173 	.shutdown	= usbblaster_shutdown,
174 };
175 
176 /* Returns 0 upon success, a negative number upon errors. */
usbblaster_spi_init(const struct programmer_cfg * cfg)177 static int usbblaster_spi_init(const struct programmer_cfg *cfg)
178 {
179 	uint8_t buf[BUF_SIZE + 1] = { 0 };
180 	struct ftdi_context ftdic;
181 	struct usbblaster_spi_data *usbblaster_data;
182 
183 	if (ftdi_init(&ftdic) < 0)
184 		return -1;
185 
186 	if (ftdi_usb_open(&ftdic, ALTERA_VID, ALTERA_USBBLASTER_PID) < 0) {
187 		msg_perr("Failed to open USB-Blaster: %s\n", ftdic.error_str);
188 		return -1;
189 	}
190 
191 	if (ftdi_usb_reset(&ftdic) < 0) {
192 		msg_perr("USB-Blaster reset failed\n");
193 		return -1;
194 	}
195 
196 	if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
197 		msg_perr("USB-Blaster set latency timer failed\n");
198 		return -1;
199 	}
200 
201 	if (ftdi_write_data_set_chunksize(&ftdic, 4096) < 0 ||
202 	    ftdi_read_data_set_chunksize(&ftdic, BUF_SIZE) < 0) {
203 		msg_perr("USB-Blaster set chunk size failed\n");
204 		return -1;
205 	}
206 
207 	buf[sizeof(buf)-1] = BIT_LED | BIT_CS;
208 	if (ftdi_write_data(&ftdic, buf, sizeof(buf)) < 0) {
209 		msg_perr("USB-Blaster reset write failed\n");
210 		return -1;
211 	}
212 	if (ftdi_read_data(&ftdic, buf, sizeof(buf)) < 0) {
213 		msg_perr("USB-Blaster reset read failed\n");
214 		return -1;
215 	}
216 
217 	usbblaster_data = calloc(1, sizeof(*usbblaster_data));
218 	if (!usbblaster_data) {
219 		msg_perr("Unable to allocate space for SPI master data\n");
220 		return -1;
221 	}
222 	usbblaster_data->ftdic = ftdic;
223 
224 	return register_spi_master(&spi_master_usbblaster, usbblaster_data);
225 }
226 
227 const struct programmer_entry programmer_usbblaster_spi = {
228 	.name			= "usbblaster_spi",
229 	.type			= USB,
230 	.devs.dev		= devs_usbblasterspi,
231 	.init			= usbblaster_spi_init,
232 };
233