blob: 07bc8a51d5bcdf5ec14ff12e809c72f4e51671e2 [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
Martin Rothb54d6ba2015-09-29 14:49:37 -060015 * Foundation, Inc.
Stefan Reinauerc566d202015-08-25 09:52:42 -070016 */
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,
Martin Rothb54d6ba2015-09-29 14:49:37 -0600127 data + bytes_sent, bytes_left, &actual,
128 BULK_SEND_TIMEOUT);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700129 bytes_sent += actual;
130 if (actual < bytes_left) {
Martin Rothb54d6ba2015-09-29 14:49:37 -0600131 printf("Tried sending %d bytes, sent %d\n", bytes_left,
132 actual);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700133 break;
134 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700135 }
136
Stefan Reinauer8421b712015-08-28 20:01:34 -0700137 if (bytes_sent != length)
138 printf ("SPI transfer failed\n");
139
Stefan Reinauerc566d202015-08-25 09:52:42 -0700140 return (bytes_sent == length);
141}
142
Stefan Reinauer40cba332015-08-28 20:03:05 -0700143int unlock_spi_flash(struct em100 *em100)
144{
145 unsigned char cmd[16];
146 memset(cmd, 0, 16);
147 cmd[0] = 0x36; /* Unlock SPI flash */
148 if (!send_cmd(em100->dev, cmd)) {
149 return 0;
150 }
151 /* Specification says to wait 5s before
152 * issuing another USB command
153 */
154 return 1;
155}
156
157/* Erase SPI flash sector
158 * There are 32 sectors 64kB each
159 *
160 */
161int erase_spi_flash_sector(struct em100 *em100, unsigned int sector)
162{
163 unsigned char cmd[16];
164
165 if (sector > 31) {
166 printf("Can't erase sector at address %x\n", sector << 16);
167 return 0;
168 }
169
170 memset(cmd, 0, 16);
171 cmd[0] = 0x37; /* Erase SPI flash sector */
172 cmd[1] = sector;
173 if (!send_cmd(em100->dev, cmd)) {
174 return 0;
175 }
176 /* Specification says to wait 5s before
177 * issuing another USB command
178 */
179 return 1;
180
181}
182
183
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700184/* SPI Hyper Terminal related operations */
185
186/* SPI Hyper Terminal resources:
187 *
188 * FIFOs:
189 * dFIFO 64 bytes Transfer information from host to application (EM100Pro)
190 * uFIFO 512 bytes Transfer information from application to host
191 *
192 * Registers:
193 * 0 1 byte RW FIFO overflow, pause/start emulation, FIFO valid data
194 * 1 1 byte RO length of valid data in dFIFO
195 * 2 1 byte RO length of valid data in uFIFO (?? in 1 byte ??)
196 * 3 1 byte RO EM100 identification on SPI bus to differentiate from
197 * real SPI flash
198 * 4 1 byte RW uFIFO data format to indicate to the software how to
199 * read the data
200 */
Stefan Reinauerc566d202015-08-25 09:52:42 -0700201
202/**
203 * read_ht_register: Read HT registers
204 * @param em100: initialized em100 device structure
205 *
206 * out(2 bytes): 0x50 RegAddr .. 0
207 * in(len + 1 byte): 0x02 val
208 */
209int read_ht_register(struct em100 *em100, int reg, uint8_t *val)
210{
211 unsigned char cmd[16];
212 unsigned char data[2];
213
214 memset(cmd, 0, 16);
215 cmd[0] = 0x50; /* read fpga register */
216 cmd[1] = reg;
217 if (!send_cmd(em100->dev, cmd)) {
218 return 0;
219 }
220 int len = get_response(em100->dev, data, 2);
221 if ((len == 2) && (data[0] == 1)) {
222 *val = data[1];
223 return 1;
224 }
225 return 0;
226}
227
228/**
229 * write_ht_register: Write HT registers
230 * @param em100: initialized em100 device structure
231 *
232 * out(3 bytes): 0x51 RegAddr Val .. 0
233 */
234int write_ht_register(struct em100 *em100, int reg, uint8_t val)
235{
236 unsigned char cmd[16];
237 memset(cmd, 0, 16);
238 cmd[0] = 0x51; /* write fpga registers */
239 cmd[1] = reg;
240 cmd[2] = val;
241 if (!send_cmd(em100->dev, cmd)) {
242 return 0;
243 }
244 return 1;
245}
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700246
Martin Rothb54d6ba2015-09-29 14:49:37 -0600247int write_dfifo(struct em100 *em100, unsigned int length, unsigned int timeout,
248 unsigned char *blk)
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700249{
250 int actual;
251 int bytes_sent=0;
252 int bytes_left;
253 unsigned char cmd[16];
254 unsigned char data[512];
255
256 if (length > 512) { /* Really? Should be 64? */
Martin Rothb54d6ba2015-09-29 14:49:37 -0600257 printf("Error: Length of data to be written to dFIFO can't"
258 " be > 512\n");
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700259 return 0;
260 }
261 memset(cmd, 0, 16);
262 cmd[0] = 0x52; /* write dFIFO */
263 cmd[1] = (length >> 8) & 0xff;
264 cmd[2] = length & 0xff;
265 cmd[3] = (timeout >> 8) & 0xff;
266 cmd[4] = timeout & 0xff;
267
268 if (!send_cmd(em100->dev, cmd)) {
269 printf("Error: Could not initiate host-to-EM100 transfer.\n");
270 return 0;
271 }
272
273 while ( bytes_sent < length) {
274 actual = 0;
275
276 bytes_left = length - bytes_sent;
277
278 libusb_bulk_transfer(em100->dev, 1 | LIBUSB_ENDPOINT_OUT,
Martin Rothb54d6ba2015-09-29 14:49:37 -0600279 data + bytes_sent, bytes_left, &actual,
280 BULK_SEND_TIMEOUT);
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700281 bytes_sent += actual;
282 if (actual < bytes_left) {
Martin Rothb54d6ba2015-09-29 14:49:37 -0600283 printf("Tried sending %d bytes, sent %d\n", bytes_left,
284 actual);
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700285 break;
286 }
287
288 printf("Sent %d bytes of %d\n", bytes_sent, length);
289 }
290
291 printf ("Transfer %s\n",bytes_sent == length ? "Succeeded" : "Failed");
292 return (bytes_sent == length);
293
294 int len = get_response(em100->dev, data, 512);
295
296 if (len == 1 && data[0] == length) {
297 return 1;
298 }
299 return 0;
300}
301
Martin Rothb54d6ba2015-09-29 14:49:37 -0600302int read_ufifo(struct em100 *em100, unsigned int length, unsigned int timeout,
303 unsigned char *blk)
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700304{
305 unsigned char cmd[16];
306 unsigned char data[512];
Martin Rothdfdff3e2015-09-18 09:42:03 -0600307 unsigned char data2[2];
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700308
309 if (length > 512) {
Martin Rothb54d6ba2015-09-29 14:49:37 -0600310 printf("Error: Length of data to be read from uFIFO can't be"
311 " > 512\n");
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700312 return 0;
313 }
314 memset(cmd, 0, 16);
315 cmd[0] = 0x53; /* read uFIFO */
316 cmd[1] = (length >> 8) & 0xff;
317 cmd[2] = length & 0xff;
318 cmd[3] = (timeout >> 8) & 0xff;
319 cmd[4] = timeout & 0xff;
320 if (!send_cmd(em100->dev, cmd)) {
321 return 0;
322 }
323 int len = get_response(em100->dev, data, 512);
324
Martin Rothdfdff3e2015-09-18 09:42:03 -0600325 /* get second response from read ufifo command */
326 get_response(em100->dev, data2, 2);
327
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700328 if (len == length) {
329 memcpy(blk, data, length);
330 return 1;
331 }
332 return 0;
333}