blob: f3b331d61f154bf64ccd8ec7d1b00337c0ef7971 [file] [log] [blame]
Stefan Reinauerc566d202015-08-25 09:52:42 -07001/*
2 * Copyright 2012-2015 Google Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18#include <stdio.h>
19#include <string.h>
20#include <unistd.h>
21#include "em100.h"
22
23/* SPI flash related operations */
24
25int get_spi_flash_id(struct em100 *em100)
26{
27 unsigned char cmd[16];
28 unsigned char data[512];
29 memset(cmd, 0, 16);
30 cmd[0] = 0x30; /* Get SPI flash ID */
31 if (!send_cmd(em100->dev, cmd)) {
32 return 0;
33 }
34 int len = get_response(em100->dev, data, 512);
35 if (len == 3) {
36 int id = (data[0] << 16) | (data[1] << 8) | data[2];
37 return id;
38 }
39 return 0;
40}
41
42int erase_spi_flash(struct em100 *em100)
43{
44 unsigned char cmd[16];
45 memset(cmd, 0, 16);
46 cmd[0] = 0x31; /* Erase SPI flash */
47 if (!send_cmd(em100->dev, cmd)) {
48 return 0;
49 }
50 /* Specification says to wait 5s before
51 * issuing another USB command
52 */
53 sleep(5);
54 return 1;
55}
56
57int poll_spi_flash_status(struct em100 *em100)
58{
59 unsigned char cmd[16];
60 unsigned char data[1];
61 memset(cmd, 0, 16);
62 cmd[0] = 0x32; /* Poll SPI flash status */
63 if (!send_cmd(em100->dev, cmd)) {
64 return 0;
65 }
66 int len = get_response(em100->dev, data, 1);
67 if ((len == 1) && (data[0] == 1)) {
68 /* ready */
69 return 1;
70 }
71 /* busy (or read unsuccessful) */
72 return 0;
73}
74
75/**
76 * read_spi_flash_page: fetch SPI flash page
77 * @param em100: initialized em100 device structure
78 *
79 * out(16 bytes): 0x33 addr addr addr .. 0
80 * in(len + 255 bytes): 0xff ?? serno_lo serno_hi ?? ?? .. ??
81 */
82int read_spi_flash_page(struct em100 *em100, int addr, unsigned char *blk)
83{
84 unsigned char cmd[16];
85 unsigned char data[256];
86 memset(cmd, 0, 16);
Stefan Reinauerae4dcd02015-08-28 16:26:03 -070087 cmd[0] = 0x33; /* read SPI flash page */
Stefan Reinauerc566d202015-08-25 09:52:42 -070088 cmd[1] = (addr >> 16) & 0xff;
89 cmd[2] = (addr >> 8) & 0xff;
90 cmd[3] = addr & 0xff;
91 if (!send_cmd(em100->dev, cmd)) {
92 return 0;
93 }
94 int len = get_response(em100->dev, data, 256);
95
96 if (len == 256) {
97 memcpy(blk, data, 256);
98 return 1;
99 }
100 return 0;
101}
102
Stefan Reinauer65559f12015-08-28 16:28:51 -0700103int write_spi_flash_page(struct em100 *em100, int address, unsigned char *data)
Stefan Reinauerc566d202015-08-25 09:52:42 -0700104{
105 int length = 256;
106 int actual;
107 int bytes_sent=0;
108 int bytes_left;
109 unsigned char cmd[16];
110 memset(cmd, 0, 16);
111 cmd[0] = 0x34; /* host-to-em100 eeprom data */
Stefan Reinauer738812e2015-08-28 20:00:41 -0700112 cmd[1] = (address >> 16) & 0xff;
113 cmd[2] = (address >> 8) & 0xff;
114 cmd[3] = address & 0xff;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700115
116 if (!send_cmd(em100->dev, cmd)) {
Stefan Reinauer54474a62015-08-28 16:27:30 -0700117 printf("Error: Could not initiate host-to-EM100 transfer.\n");
Stefan Reinauerc566d202015-08-25 09:52:42 -0700118 return 0;
119 }
120
121 while ( bytes_sent < length) {
122 actual = 0;
123
124 bytes_left = length - bytes_sent;
125
126 libusb_bulk_transfer(em100->dev, 1 | LIBUSB_ENDPOINT_OUT,
127 data + bytes_sent, bytes_left, &actual, BULK_SEND_TIMEOUT);
128 bytes_sent += actual;
129 if (actual < bytes_left) {
130 printf("Tried sending %d bytes, sent %d\n", bytes_left, actual);
131 break;
132 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700133 }
134
Stefan Reinauer8421b712015-08-28 20:01:34 -0700135 if (bytes_sent != length)
136 printf ("SPI transfer failed\n");
137
Stefan Reinauerc566d202015-08-25 09:52:42 -0700138 return (bytes_sent == length);
139}
140
Stefan Reinauer40cba332015-08-28 20:03:05 -0700141int unlock_spi_flash(struct em100 *em100)
142{
143 unsigned char cmd[16];
144 memset(cmd, 0, 16);
145 cmd[0] = 0x36; /* Unlock SPI flash */
146 if (!send_cmd(em100->dev, cmd)) {
147 return 0;
148 }
149 /* Specification says to wait 5s before
150 * issuing another USB command
151 */
152 return 1;
153}
154
155/* Erase SPI flash sector
156 * There are 32 sectors 64kB each
157 *
158 */
159int erase_spi_flash_sector(struct em100 *em100, unsigned int sector)
160{
161 unsigned char cmd[16];
162
163 if (sector > 31) {
164 printf("Can't erase sector at address %x\n", sector << 16);
165 return 0;
166 }
167
168 memset(cmd, 0, 16);
169 cmd[0] = 0x37; /* Erase SPI flash sector */
170 cmd[1] = sector;
171 if (!send_cmd(em100->dev, cmd)) {
172 return 0;
173 }
174 /* Specification says to wait 5s before
175 * issuing another USB command
176 */
177 return 1;
178
179}
180
181
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700182/* SPI Hyper Terminal related operations */
183
184/* SPI Hyper Terminal resources:
185 *
186 * FIFOs:
187 * dFIFO 64 bytes Transfer information from host to application (EM100Pro)
188 * uFIFO 512 bytes Transfer information from application to host
189 *
190 * Registers:
191 * 0 1 byte RW FIFO overflow, pause/start emulation, FIFO valid data
192 * 1 1 byte RO length of valid data in dFIFO
193 * 2 1 byte RO length of valid data in uFIFO (?? in 1 byte ??)
194 * 3 1 byte RO EM100 identification on SPI bus to differentiate from
195 * real SPI flash
196 * 4 1 byte RW uFIFO data format to indicate to the software how to
197 * read the data
198 */
Stefan Reinauerc566d202015-08-25 09:52:42 -0700199
200/**
201 * read_ht_register: Read HT registers
202 * @param em100: initialized em100 device structure
203 *
204 * out(2 bytes): 0x50 RegAddr .. 0
205 * in(len + 1 byte): 0x02 val
206 */
207int read_ht_register(struct em100 *em100, int reg, uint8_t *val)
208{
209 unsigned char cmd[16];
210 unsigned char data[2];
211
212 memset(cmd, 0, 16);
213 cmd[0] = 0x50; /* read fpga register */
214 cmd[1] = reg;
215 if (!send_cmd(em100->dev, cmd)) {
216 return 0;
217 }
218 int len = get_response(em100->dev, data, 2);
219 if ((len == 2) && (data[0] == 1)) {
220 *val = data[1];
221 return 1;
222 }
223 return 0;
224}
225
226/**
227 * write_ht_register: Write HT registers
228 * @param em100: initialized em100 device structure
229 *
230 * out(3 bytes): 0x51 RegAddr Val .. 0
231 */
232int write_ht_register(struct em100 *em100, int reg, uint8_t val)
233{
234 unsigned char cmd[16];
235 memset(cmd, 0, 16);
236 cmd[0] = 0x51; /* write fpga registers */
237 cmd[1] = reg;
238 cmd[2] = val;
239 if (!send_cmd(em100->dev, cmd)) {
240 return 0;
241 }
242 return 1;
243}
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700244
245int write_dfifo(struct em100 *em100, unsigned int length, unsigned int timeout, unsigned char *blk)
246{
247 int actual;
248 int bytes_sent=0;
249 int bytes_left;
250 unsigned char cmd[16];
251 unsigned char data[512];
252
253 if (length > 512) { /* Really? Should be 64? */
254 printf("Error: Length of data to be written to dFIFO can't be > 512\n");
255 return 0;
256 }
257 memset(cmd, 0, 16);
258 cmd[0] = 0x52; /* write dFIFO */
259 cmd[1] = (length >> 8) & 0xff;
260 cmd[2] = length & 0xff;
261 cmd[3] = (timeout >> 8) & 0xff;
262 cmd[4] = timeout & 0xff;
263
264 if (!send_cmd(em100->dev, cmd)) {
265 printf("Error: Could not initiate host-to-EM100 transfer.\n");
266 return 0;
267 }
268
269 while ( bytes_sent < length) {
270 actual = 0;
271
272 bytes_left = length - bytes_sent;
273
274 libusb_bulk_transfer(em100->dev, 1 | LIBUSB_ENDPOINT_OUT,
275 data + bytes_sent, bytes_left, &actual, BULK_SEND_TIMEOUT);
276 bytes_sent += actual;
277 if (actual < bytes_left) {
278 printf("Tried sending %d bytes, sent %d\n", bytes_left, actual);
279 break;
280 }
281
282 printf("Sent %d bytes of %d\n", bytes_sent, length);
283 }
284
285 printf ("Transfer %s\n",bytes_sent == length ? "Succeeded" : "Failed");
286 return (bytes_sent == length);
287
288 int len = get_response(em100->dev, data, 512);
289
290 if (len == 1 && data[0] == length) {
291 return 1;
292 }
293 return 0;
294}
295
296int read_ufifo(struct em100 *em100, unsigned int length, unsigned int timeout, unsigned char *blk)
297{
298 unsigned char cmd[16];
299 unsigned char data[512];
300
301 if (length > 512) {
302 printf("Error: Length of data to be read from uFIFO can't be > 512\n");
303 return 0;
304 }
305 memset(cmd, 0, 16);
306 cmd[0] = 0x53; /* read uFIFO */
307 cmd[1] = (length >> 8) & 0xff;
308 cmd[2] = length & 0xff;
309 cmd[3] = (timeout >> 8) & 0xff;
310 cmd[4] = timeout & 0xff;
311 if (!send_cmd(em100->dev, cmd)) {
312 return 0;
313 }
314 int len = get_response(em100->dev, data, 512);
315
316 if (len == length) {
317 memcpy(blk, data, length);
318 return 1;
319 }
320 return 0;
321}