blob: 50c2fc6db2e131612310052a06a514ee4c8abf00 [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 "em100.h"
21
22/* SPI Trace related operations */
23
24/**
25 * reset_spi_trace: clear SPI trace buffer
26 * @param em100: em100 device structure
27 *
28 * out(16 bytes): 0xbd 0 .. 0
29 */
30int reset_spi_trace(struct em100 *em100)
31{
32 unsigned char cmd[16];
33 memset(cmd, 0, 16);
34 cmd[0] = 0xbd; /* reset SPI trace buffer*/
35 if (!send_cmd(em100->dev, cmd)) {
36 return 0;
37 }
38 return 1;
39}
40
41/**
42 * read_spi_trace: fetch SPI trace data
43 * @param em100: em100 device structure
44 * globals: curpos, counter, cmdid
45 *
46 * out(16 bytes): bc 00 00 00 08 00 00 00 00 15 00 00 00 00 00 00
47 * in(8x8192 bytes): 2 bytes (BE) number of records (0..0x3ff),
48 * then records of 8 bytes each
49 */
50static unsigned int counter = 0;
51static unsigned char curpos = 0;
52static unsigned char cmdid = 0xff; // timestamp, so never a valid command id
53
Martin Roth330de302015-09-17 16:33:07 -060054#define REPORT_BUFFER_LENGTH 8192
55#define REPORT_BUFFER_COUNT 8
56
57static int read_report_buffer(struct em100 *em100,
58 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH])
Stefan Reinauerc566d202015-08-25 09:52:42 -070059{
Martin Roth330de302015-09-17 16:33:07 -060060 unsigned char cmd[16] = {0};
61 int len;
62 unsigned int report;
63
Stefan Reinauerc566d202015-08-25 09:52:42 -070064 cmd[0] = 0xbc; /* read SPI trace buffer*/
65
Martin Roth330de302015-09-17 16:33:07 -060066 /*
67 * Trace length, unit is 4k according to specs
68 *
69 * cmd1..cmd4 are probably u32BE on how many
70 * reports (8192 bytes each) to fetch
71 */
Stefan Reinauerc566d202015-08-25 09:52:42 -070072 cmd[1] = 0x00;
73 cmd[2] = 0x00;
74 cmd[3] = 0x00;
Martin Roth330de302015-09-17 16:33:07 -060075 cmd[4] = REPORT_BUFFER_COUNT;
Stefan Reinauerc566d202015-08-25 09:52:42 -070076 /* Timeout in ms */
77 cmd[5] = 0x00;
78 cmd[6] = 0x00;
79 cmd[7] = 0x00;
80 cmd[8] = 0x00;
81 /* Trace Config
82 * [1:0] 00 start/stop spi trace according to emulation status
83 * 01 start when TraceConfig[2] == 1
84 * 10 start when trig signal goes high
85 * 11 RFU
86 * [2] When TraceConfig[1:0] == 01 this bit starts the trace
87 * [7:3] RFU
88 */
89 cmd[9] = 0x15;
90
91 if (!send_cmd(em100->dev, cmd)) {
92 printf("sending trace command failed\n");
93 return 0;
94 }
Stefan Reinauerd7f959b2015-09-03 16:03:40 -070095
Martin Roth330de302015-09-17 16:33:07 -060096 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
Martin Rothb54d6ba2015-09-29 14:49:37 -060097 len = get_response(em100->dev, &reportdata[report][0],
98 REPORT_BUFFER_LENGTH);
Martin Roth330de302015-09-17 16:33:07 -060099 if (len != REPORT_BUFFER_LENGTH) {
100 printf("error, report length = %d instead of %d.\n\n",
101 len, REPORT_BUFFER_LENGTH);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700102 return 0;
103 }
Martin Roth330de302015-09-17 16:33:07 -0600104 }
105
106 return 1;
107}
108
Martin Rothe8a72dd2015-09-17 17:14:48 -0600109struct spi_cmd_values {
110 char *cmd_name;
111 uint8_t cmd;
112 uint8_t uses_address;
113 uint8_t pad_bytes;
114};
115
116struct spi_cmd_values spi_command_list[] = {
117 /* name cmd, addr, pad */
118 {"write status register", 0x01, 0, 0},
119 {"page program", 0x02, 1, 0},
120 {"read", 0x03, 1, 0},
121 {"write disable", 0x04, 0, 0},
122 {"read status register", 0x05, 0, 0},
123 {"write enable", 0x06, 0, 0},
124 {"fast read", 0x0b, 1, 1},
125 {"EM100 specific", 0x11, 0, 0},
126 {"fast dual read", 0x3b, 1, 2},
127 {"chip erase", 0x60, 0, 0},
128 {"read JEDEC ID", 0x9f, 0, 0},
129 {"chip erase", 0xc7, 0, 0},
130 {"sector erase", 0xd8, 1, 0},
131
132 {"unknown command", 0xff, 0, 0}
133};
134
135static struct spi_cmd_values * get_command_vals(uint8_t command) {
136 /* cache last command so a search isn't needed every time */
137 static struct spi_cmd_values *spi_cmd = &spi_command_list[3]; /* init to read */
138 int i;
139
140 if (spi_cmd->cmd != command) {
141 for (i = 0; spi_command_list[i].cmd != 0xff; i++) {
142 if (spi_command_list[i].cmd == command)
143 break;
144 }
145 spi_cmd = &spi_command_list[i];
146 }
147
148 return spi_cmd;
149}
150
151#define MAX_TRACE_BLOCKLENGTH 6
Martin Roth712262a2015-09-18 14:01:18 -0600152int read_spi_trace(struct em100 *em100, int display_terminal,
153 unsigned long addr_offset)
Martin Roth330de302015-09-17 16:33:07 -0600154{
Martin Rothb54d6ba2015-09-29 14:49:37 -0600155 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH] =
156 {{0}};
Martin Roth330de302015-09-17 16:33:07 -0600157 unsigned char *data;
158 unsigned int count, i, report;
159 static int outbytes = 0;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600160 static int additional_pad_bytes = 0;
161 static unsigned int address = 0;
162 static unsigned long long timestamp = 0;
163 static unsigned long long start_timestamp = 0;
164 static struct spi_cmd_values *spi_cmd_vals = &spi_command_list[3];
Martin Roth330de302015-09-17 16:33:07 -0600165
166 if (!read_report_buffer(em100, reportdata))
167 return 0;
168
169 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
170 data = &reportdata[report][0];
Stefan Reinauerc566d202015-08-25 09:52:42 -0700171 count = (data[0] << 8) | data[1];
Stefan Reinauer2372d7a2015-10-10 10:46:45 +0000172 if (count > 1022) {
173 printf("Warning: EM100pro sends too much data.\n");
174 count = 1022;
175 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700176 for (i = 0; i < count; i++) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600177 unsigned int j = additional_pad_bytes;
178 additional_pad_bytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700179 unsigned char cmd = data[2 + i*8];
180 if (cmd == 0xff) {
181 /* timestamp */
Stefan Reinauerc566d202015-08-25 09:52:42 -0700182 timestamp = data[2 + i*8 + 2];
183 timestamp = (timestamp << 8) | data[2 + i*8 + 3];
184 timestamp = (timestamp << 8) | data[2 + i*8 + 4];
185 timestamp = (timestamp << 8) | data[2 + i*8 + 5];
186 timestamp = (timestamp << 8) | data[2 + i*8 + 6];
187 timestamp = (timestamp << 8) | data[2 + i*8 + 7];
Martin Rothdfdff3e2015-09-18 09:42:03 -0600188 if (display_terminal)
189 read_spi_terminal(em100, 1);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700190 continue;
191 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600192
Stefan Reinauerc566d202015-08-25 09:52:42 -0700193 /* from here, it must be data */
194 if (cmd != cmdid) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600195 unsigned char spi_command = data[i * 8 + 4];
196 spi_cmd_vals = get_command_vals(spi_command);
197
Stefan Reinauerc566d202015-08-25 09:52:42 -0700198 /* new command */
199 cmdid = cmd;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600200 if (counter == 0)
201 start_timestamp = timestamp;
202
203 /* set up address if used by this command*/
204 if (!spi_cmd_vals->uses_address) {
205 j = 1; /* skip command byte */
206 } else {
207 address = (data[i * 8 + 5] << 16) +
208 (data[i * 8 + 6] << 8) +
209 data[i * 8 + 7];
210
211 /* skip command, address bytes, and padding */
212 j = 4 + spi_cmd_vals->pad_bytes;
213 if (j > MAX_TRACE_BLOCKLENGTH) {
Martin Rothb54d6ba2015-09-29 14:49:37 -0600214 additional_pad_bytes = j -
215 MAX_TRACE_BLOCKLENGTH;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600216 j = MAX_TRACE_BLOCKLENGTH;
217 }
218 }
219 printf("\nTime: %06lld.%08lld",
Martin Rothb54d6ba2015-09-29 14:49:37 -0600220 (timestamp - start_timestamp) /
221 100000000,
222 (timestamp - start_timestamp) %
223 100000000);
224 printf(" command # %-6d : 0x%02x - %s",
225 ++counter, spi_command,
226 spi_cmd_vals->cmd_name);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700227 curpos = 0;
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700228 outbytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700229 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600230
Stefan Reinauerc566d202015-08-25 09:52:42 -0700231 /* this exploits 8bit wrap around in curpos */
232 unsigned char blocklen = (data[2 + i*8 + 1] - curpos);
233 blocklen /= 8;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600234
235 for (; j < blocklen; j++) {
236 if (outbytes == 0) {
237 if (spi_cmd_vals->uses_address) {
Martin Roth712262a2015-09-18 14:01:18 -0600238 printf("\n%08lx : ",
239 addr_offset +
240 address);
Martin Rothe8a72dd2015-09-17 17:14:48 -0600241 } else {
242 printf("\n : ");
243 }
244 }
245 printf("%02x ", data[i * 8 + 4 + j]);
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700246 outbytes++;
247 if (outbytes == 16) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600248 outbytes = 0;
249 if (spi_cmd_vals->uses_address)
250 address += 16;
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700251 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700252 }
Martin Rothb54d6ba2015-09-29 14:49:37 -0600253 // this is because the em100 counts funny
254 curpos = data[2 + i*8 + 1] + 0x10;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600255 fflush(stdout);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700256 }
257 }
258 return 1;
259}
Martin Rothdfdff3e2015-09-18 09:42:03 -0600260
261#define UFIFO_SIZE 512
262#define UFIFO_TIMEOUT 0x00
263
264/*
265 * Polls the uFIFO buffer to see if there's any data. The HT registers don't
266 * seem to ever be updated to reflect that there's data present, and the
267 * Dediprog software doesn't use them either.
Martin Rothc69319c2015-09-29 14:09:40 -0600268 *
Martin Rothdfdff3e2015-09-18 09:42:03 -0600269 * Multiple messages can be in a single uFIFO transfer, so loop through
270 * the data looking for the signature.
271 */
272int read_spi_terminal(struct em100 *em100, int show_counter) {
273 unsigned char data[UFIFO_SIZE] = { 0 };
274 static unsigned int msg_counter = 1; /* Number of messages */
Martin Rothc69319c2015-09-29 14:09:40 -0600275 uint16_t data_length;
Martin Rothdfdff3e2015-09-18 09:42:03 -0600276 unsigned char *data_start;
277 unsigned int j, k;
278 struct em100_msg *msg = NULL;
279
280 if (!read_ufifo(em100, UFIFO_SIZE, UFIFO_TIMEOUT, &data[0]))
281 return 0;
282
283 /* the first two bytes are the amount of valid data */
Martin Rothc69319c2015-09-29 14:09:40 -0600284 data_length = (data[0] << 8) + data[1];
285 if (data_length == 0)
Martin Rothdfdff3e2015-09-18 09:42:03 -0600286 return 1;
287
288 /* actual data starts after the length */
289 data_start = &data[sizeof(uint16_t)];
290
291 /* examine data; stop when we run out of message or buffer */
Martin Rothc69319c2015-09-29 14:09:40 -0600292 for (j = 0; j < data_length &&
Martin Rothdfdff3e2015-09-18 09:42:03 -0600293 j < UFIFO_SIZE - sizeof(struct em100_msg_header); j++) {
294
295 msg = (struct em100_msg *)(data_start + j);
296 if (msg->header.signature == EM100_MSG_SIGNATURE) {
297
298 if (show_counter)
299 printf("\nHT%06d: ", msg_counter);
300
301 /* print message byte according to format */
302 for (k = 0; k < msg->header.data_length; k++) {
Martin Rothc69319c2015-09-29 14:09:40 -0600303 if (&msg->data[k] >= data_start + data_length)
Martin Rothdfdff3e2015-09-18 09:42:03 -0600304 break;
305 if (&msg->data[k] >= &data[0] + UFIFO_SIZE)
306 break;
307
308 switch (msg->header.data_type) {
309 case ht_checkpoint_1byte:
310 case ht_checkpoint_2bytes:
311 case ht_checkpoint_4bytes:
312 case ht_hexadecimal_data:
313 case ht_timestamp_data:
314 printf("%02x ", msg->data[k]);
315 break;
316 case ht_ascii_data:
317 printf("%c", msg->data[k]);
318 break;
319 case ht_lookup_table:
320 /* TODO - support lookup table */
321 printf("Lookup unsupported: %02x%02x",
322 msg->data[k], msg->data[k + 1]);
323 k++;
324 break;
325 }
326 }
327
328 /* advance to the end of the message */
Martin Rothb54d6ba2015-09-29 14:49:37 -0600329 j += msg->header.data_length +
330 sizeof(struct em100_msg_header) - 1;
Martin Rothdfdff3e2015-09-18 09:42:03 -0600331 msg_counter++;
332 fflush(stdout);
333 }
334 }
335
336 return 1;
337}
338
339int init_spi_terminal (struct em100 *em100)
340{
341 int retval = 0x01;
342 uint16_t val;
343
344 retval &= write_ht_register(em100, ufifo_data_fmt_reg, 0);
345 retval &= write_ht_register(em100, status_reg, START_SPI_EMULATION);
346
347 /* set em100 to recognize spi command 0x11 */
348 retval &= write_fpga_register(em100, 0x82, EM100_SPECIFIC_CMD);
349 retval &= read_fpga_register(em100, 0x28, &val);
350
351 return retval;
352}