blob: b596e7ca9e68e7cab9f1d99667724a894423fa08 [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 Reinauer42dfb822015-08-28 16:59:42 -0700141/* SPI Hyper Terminal related operations */
142
143/* SPI Hyper Terminal resources:
144 *
145 * FIFOs:
146 * dFIFO 64 bytes Transfer information from host to application (EM100Pro)
147 * uFIFO 512 bytes Transfer information from application to host
148 *
149 * Registers:
150 * 0 1 byte RW FIFO overflow, pause/start emulation, FIFO valid data
151 * 1 1 byte RO length of valid data in dFIFO
152 * 2 1 byte RO length of valid data in uFIFO (?? in 1 byte ??)
153 * 3 1 byte RO EM100 identification on SPI bus to differentiate from
154 * real SPI flash
155 * 4 1 byte RW uFIFO data format to indicate to the software how to
156 * read the data
157 */
Stefan Reinauerc566d202015-08-25 09:52:42 -0700158
159/**
160 * read_ht_register: Read HT registers
161 * @param em100: initialized em100 device structure
162 *
163 * out(2 bytes): 0x50 RegAddr .. 0
164 * in(len + 1 byte): 0x02 val
165 */
166int read_ht_register(struct em100 *em100, int reg, uint8_t *val)
167{
168 unsigned char cmd[16];
169 unsigned char data[2];
170
171 memset(cmd, 0, 16);
172 cmd[0] = 0x50; /* read fpga register */
173 cmd[1] = reg;
174 if (!send_cmd(em100->dev, cmd)) {
175 return 0;
176 }
177 int len = get_response(em100->dev, data, 2);
178 if ((len == 2) && (data[0] == 1)) {
179 *val = data[1];
180 return 1;
181 }
182 return 0;
183}
184
185/**
186 * write_ht_register: Write HT registers
187 * @param em100: initialized em100 device structure
188 *
189 * out(3 bytes): 0x51 RegAddr Val .. 0
190 */
191int write_ht_register(struct em100 *em100, int reg, uint8_t val)
192{
193 unsigned char cmd[16];
194 memset(cmd, 0, 16);
195 cmd[0] = 0x51; /* write fpga registers */
196 cmd[1] = reg;
197 cmd[2] = val;
198 if (!send_cmd(em100->dev, cmd)) {
199 return 0;
200 }
201 return 1;
202}
Stefan Reinauer42dfb822015-08-28 16:59:42 -0700203
204int write_dfifo(struct em100 *em100, unsigned int length, unsigned int timeout, unsigned char *blk)
205{
206 int actual;
207 int bytes_sent=0;
208 int bytes_left;
209 unsigned char cmd[16];
210 unsigned char data[512];
211
212 if (length > 512) { /* Really? Should be 64? */
213 printf("Error: Length of data to be written to dFIFO can't be > 512\n");
214 return 0;
215 }
216 memset(cmd, 0, 16);
217 cmd[0] = 0x52; /* write dFIFO */
218 cmd[1] = (length >> 8) & 0xff;
219 cmd[2] = length & 0xff;
220 cmd[3] = (timeout >> 8) & 0xff;
221 cmd[4] = timeout & 0xff;
222
223 if (!send_cmd(em100->dev, cmd)) {
224 printf("Error: Could not initiate host-to-EM100 transfer.\n");
225 return 0;
226 }
227
228 while ( bytes_sent < length) {
229 actual = 0;
230
231 bytes_left = length - bytes_sent;
232
233 libusb_bulk_transfer(em100->dev, 1 | LIBUSB_ENDPOINT_OUT,
234 data + bytes_sent, bytes_left, &actual, BULK_SEND_TIMEOUT);
235 bytes_sent += actual;
236 if (actual < bytes_left) {
237 printf("Tried sending %d bytes, sent %d\n", bytes_left, actual);
238 break;
239 }
240
241 printf("Sent %d bytes of %d\n", bytes_sent, length);
242 }
243
244 printf ("Transfer %s\n",bytes_sent == length ? "Succeeded" : "Failed");
245 return (bytes_sent == length);
246
247 int len = get_response(em100->dev, data, 512);
248
249 if (len == 1 && data[0] == length) {
250 return 1;
251 }
252 return 0;
253}
254
255int read_ufifo(struct em100 *em100, unsigned int length, unsigned int timeout, unsigned char *blk)
256{
257 unsigned char cmd[16];
258 unsigned char data[512];
259
260 if (length > 512) {
261 printf("Error: Length of data to be read from uFIFO can't be > 512\n");
262 return 0;
263 }
264 memset(cmd, 0, 16);
265 cmd[0] = 0x53; /* read uFIFO */
266 cmd[1] = (length >> 8) & 0xff;
267 cmd[2] = length & 0xff;
268 cmd[3] = (timeout >> 8) & 0xff;
269 cmd[4] = timeout & 0xff;
270 if (!send_cmd(em100->dev, cmd)) {
271 return 0;
272 }
273 int len = get_response(em100->dev, data, 512);
274
275 if (len == length) {
276 memcpy(blk, data, length);
277 return 1;
278 }
279 return 0;
280}