blob: 7ab2f5c0846f834a907e20fb337d4efc447370e8 [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.
Stefan Reinauerc566d202015-08-25 09:52:42 -070012 */
13
14#include <stdio.h>
15#include <string.h>
16#include "em100.h"
17
18/* SPI Trace related operations */
19
20/**
21 * reset_spi_trace: clear SPI trace buffer
22 * @param em100: em100 device structure
23 *
24 * out(16 bytes): 0xbd 0 .. 0
25 */
26int reset_spi_trace(struct em100 *em100)
27{
28 unsigned char cmd[16];
29 memset(cmd, 0, 16);
30 cmd[0] = 0xbd; /* reset SPI trace buffer*/
31 if (!send_cmd(em100->dev, cmd)) {
32 return 0;
33 }
34 return 1;
35}
36
37/**
38 * read_spi_trace: fetch SPI trace data
39 * @param em100: em100 device structure
40 * globals: curpos, counter, cmdid
41 *
42 * out(16 bytes): bc 00 00 00 08 00 00 00 00 15 00 00 00 00 00 00
43 * in(8x8192 bytes): 2 bytes (BE) number of records (0..0x3ff),
44 * then records of 8 bytes each
45 */
46static unsigned int counter = 0;
47static unsigned char curpos = 0;
48static unsigned char cmdid = 0xff; // timestamp, so never a valid command id
49
Martin Roth330de302015-09-17 16:33:07 -060050#define REPORT_BUFFER_LENGTH 8192
51#define REPORT_BUFFER_COUNT 8
52
Stefan Reinauerabd3e322019-12-04 18:29:24 -080053static int read_report_buffer(struct em100 *em100,
Martin Roth330de302015-09-17 16:33:07 -060054 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH])
Stefan Reinauerc566d202015-08-25 09:52:42 -070055{
Martin Roth330de302015-09-17 16:33:07 -060056 unsigned char cmd[16] = {0};
57 int len;
58 unsigned int report;
59
Stefan Reinauerc566d202015-08-25 09:52:42 -070060 cmd[0] = 0xbc; /* read SPI trace buffer*/
61
Stefan Reinauerabd3e322019-12-04 18:29:24 -080062 /*
Martin Roth330de302015-09-17 16:33:07 -060063 * Trace length, unit is 4k according to specs
64 *
65 * cmd1..cmd4 are probably u32BE on how many
66 * reports (8192 bytes each) to fetch
67 */
Stefan Reinauerc566d202015-08-25 09:52:42 -070068 cmd[1] = 0x00;
69 cmd[2] = 0x00;
70 cmd[3] = 0x00;
Martin Roth330de302015-09-17 16:33:07 -060071 cmd[4] = REPORT_BUFFER_COUNT;
Stefan Reinauerc566d202015-08-25 09:52:42 -070072 /* Timeout in ms */
73 cmd[5] = 0x00;
74 cmd[6] = 0x00;
75 cmd[7] = 0x00;
76 cmd[8] = 0x00;
77 /* Trace Config
78 * [1:0] 00 start/stop spi trace according to emulation status
79 * 01 start when TraceConfig[2] == 1
80 * 10 start when trig signal goes high
81 * 11 RFU
82 * [2] When TraceConfig[1:0] == 01 this bit starts the trace
83 * [7:3] RFU
84 */
85 cmd[9] = 0x15;
86
87 if (!send_cmd(em100->dev, cmd)) {
88 printf("sending trace command failed\n");
89 return 0;
90 }
Stefan Reinauerd7f959b2015-09-03 16:03:40 -070091
Martin Roth330de302015-09-17 16:33:07 -060092 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
Martin Rothb54d6ba2015-09-29 14:49:37 -060093 len = get_response(em100->dev, &reportdata[report][0],
94 REPORT_BUFFER_LENGTH);
Martin Roth330de302015-09-17 16:33:07 -060095 if (len != REPORT_BUFFER_LENGTH) {
96 printf("error, report length = %d instead of %d.\n\n",
97 len, REPORT_BUFFER_LENGTH);
Stefan Reinauerc566d202015-08-25 09:52:42 -070098 return 0;
99 }
Martin Roth330de302015-09-17 16:33:07 -0600100 }
101
102 return 1;
103}
104
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800105typedef enum { ADDR_NONE, ADDR_NO_OFF_3B, ADDR_3B, ADDR_4B, ADDR_DYNAMIC } address_t;
106
Martin Rothe8a72dd2015-09-17 17:14:48 -0600107struct spi_cmd_values {
Stefan Reinauer79533882019-11-22 00:57:29 -0800108 const char *cmd_name;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600109 uint8_t cmd;
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800110 address_t address_type;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600111 uint8_t pad_bytes;
112};
113
114struct spi_cmd_values spi_command_list[] = {
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800115 /* name cmd, addr, pad */
116 {"read SFDP", 0x5a, ADDR_NO_OFF_3B, 0},
117 {"write status register", 0x01, ADDR_NONE, 0},
118 {"page program", 0x02, ADDR_DYNAMIC, 0},
119 {"read", 0x03, ADDR_DYNAMIC, 0},
120 {"write disable", 0x04, ADDR_NONE, 0},
121 {"read status register", 0x05, ADDR_NONE, 0},
122 {"write enable", 0x06, ADDR_NONE, 0},
123 {"fast read", 0x0b, ADDR_DYNAMIC, 1},
124 {"EM100 specific", 0x11, ADDR_NONE, 0},
125 {"fast dual read", 0x3b, ADDR_DYNAMIC, 2},
126 {"chip erase", 0x60, ADDR_NONE, 0},
127 {"read JEDEC ID", 0x9f, ADDR_NONE, 0},
128 {"chip erase c7h", 0xc7, ADDR_NONE, 0},
129 {"chip erase 60h", 0x60, ADDR_NONE, 0},
130 {"sector erase", 0xd8, ADDR_DYNAMIC, 0},
131 {"dual I/O read", 0xbb, ADDR_DYNAMIC, 2},
132 {"quad I/O read", 0xeb, ADDR_DYNAMIC, 0},
133 {"quad read", 0x6b, ADDR_DYNAMIC, 0},
134 {"quad I/O dt read", 0xed, ADDR_DYNAMIC, 0},
135 {"quad page program", 0x38, ADDR_DYNAMIC, 0},
136 {"sector erase", 0x20, ADDR_DYNAMIC, 0},
137 {"block erase 32KB", 0x52, ADDR_DYNAMIC, 0},
138 {"block erase 64KB", 0xd8, ADDR_DYNAMIC, 0},
139 {"enter 4b mode", 0xb7, ADDR_NONE, 0},
140 {"exit 4b mode", 0xe9, ADDR_NONE, 0},
141 {"read 4b", 0x13, ADDR_4B, 0},
142 {"fast read 4b", 0x0c, ADDR_4B, 0},
143 {"dual I/O read 4b", 0xbc, ADDR_4B, 0},
144 {"dual out read 4b", 0x3c, ADDR_4B, 0},
145 {"quad I/O read 4b", 0xec, ADDR_4B, 0},
146 {"quad out read 4b", 0x6c, ADDR_4B, 0},
147 {"quad I/O dt read 4b", 0xee, ADDR_4B, 0},
148 {"page program 4b", 0x12, ADDR_4B, 0},
149 {"quad page program 4b", 0x3e, ADDR_4B, 0},
150 {"block erase 64KB 4b", 0xdc, ADDR_4B, 0},
151 {"block erase 32KB 4b", 0x5c, ADDR_4B, 0},
152 {"sector erase 4b", 0x21, ADDR_4B, 0},
153 {"enter quad I/O mode", 0x35, ADDR_NONE, 0},
154 {"exit quad I/O mode", 0xf5, ADDR_NONE, 0},
Martin Rothe8a72dd2015-09-17 17:14:48 -0600155
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800156 {"unknown command", 0xff, ADDR_NONE, 0}
157
Martin Rothe8a72dd2015-09-17 17:14:48 -0600158};
159
Stefan Reinauerabd3e322019-12-04 18:29:24 -0800160static struct spi_cmd_values * get_command_vals(uint8_t command)
161{
Martin Rothe8a72dd2015-09-17 17:14:48 -0600162 /* cache last command so a search isn't needed every time */
163 static struct spi_cmd_values *spi_cmd = &spi_command_list[3]; /* init to read */
164 int i;
165
166 if (spi_cmd->cmd != command) {
167 for (i = 0; spi_command_list[i].cmd != 0xff; i++) {
168 if (spi_command_list[i].cmd == command)
169 break;
170 }
171 spi_cmd = &spi_command_list[i];
172 }
173
174 return spi_cmd;
175}
176
177#define MAX_TRACE_BLOCKLENGTH 6
Martin Roth712262a2015-09-18 14:01:18 -0600178int read_spi_trace(struct em100 *em100, int display_terminal,
179 unsigned long addr_offset)
Martin Roth330de302015-09-17 16:33:07 -0600180{
Martin Rothb54d6ba2015-09-29 14:49:37 -0600181 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH] =
182 {{0}};
Martin Roth330de302015-09-17 16:33:07 -0600183 unsigned char *data;
184 unsigned int count, i, report;
185 static int outbytes = 0;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600186 static int additional_pad_bytes = 0;
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800187 static unsigned long address = 0;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600188 static unsigned long long timestamp = 0;
189 static unsigned long long start_timestamp = 0;
190 static struct spi_cmd_values *spi_cmd_vals = &spi_command_list[3];
Martin Roth330de302015-09-17 16:33:07 -0600191
192 if (!read_report_buffer(em100, reportdata))
193 return 0;
194
195 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
196 data = &reportdata[report][0];
Stefan Reinauerc566d202015-08-25 09:52:42 -0700197 count = (data[0] << 8) | data[1];
Stefan Reinauer2372d7a2015-10-10 10:46:45 +0000198 if (count > 1022) {
199 printf("Warning: EM100pro sends too much data.\n");
200 count = 1022;
201 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700202 for (i = 0; i < count; i++) {
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800203 int address_bytes = 0;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600204 unsigned int j = additional_pad_bytes;
205 additional_pad_bytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700206 unsigned char cmd = data[2 + i*8];
Stefan Reinauerdfb107d2020-12-10 19:22:40 -0800207
208 if (cmd == 0x00) {
209 /* packet without valid data */
210 continue;
211 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700212 if (cmd == 0xff) {
213 /* timestamp */
Stefan Reinauerc566d202015-08-25 09:52:42 -0700214 timestamp = data[2 + i*8 + 2];
215 timestamp = (timestamp << 8) | data[2 + i*8 + 3];
216 timestamp = (timestamp << 8) | data[2 + i*8 + 4];
217 timestamp = (timestamp << 8) | data[2 + i*8 + 5];
218 timestamp = (timestamp << 8) | data[2 + i*8 + 6];
219 timestamp = (timestamp << 8) | data[2 + i*8 + 7];
Martin Rothdfdff3e2015-09-18 09:42:03 -0600220 if (display_terminal)
221 read_spi_terminal(em100, 1);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700222 continue;
223 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600224
Stefan Reinauerc566d202015-08-25 09:52:42 -0700225 /* from here, it must be data */
226 if (cmd != cmdid) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600227 unsigned char spi_command = data[i * 8 + 4];
228 spi_cmd_vals = get_command_vals(spi_command);
229
Stefan Reinauerc566d202015-08-25 09:52:42 -0700230 /* new command */
231 cmdid = cmd;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600232 if (counter == 0)
233 start_timestamp = timestamp;
234
Stefan Reinauerdfb107d2020-12-10 19:22:40 -0800235 /* Special commands */
236 switch (spi_command) {
237 case 0xb7:
238 address_mode = 4;
239 break;
240 case 0xe9:
241 address_mode = 3;
242 break;
243 }
244
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800245 /* skip command byte */
246 j = 1;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600247
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800248 /* Set up address if used by this command */
249 switch (spi_cmd_vals->address_type) {
250 case ADDR_DYNAMIC:
251 address_bytes = address_mode;
252 break;
253 case ADDR_NO_OFF_3B:
254 case ADDR_3B:
255 address_bytes = 3;
256 break;
257 case ADDR_4B:
258 address_bytes = 4;
259 break;
260 case ADDR_NONE:
261 break;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600262 }
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800263
264 if (address_bytes == 3)
265 address = (data[i * 8 + 5] << 16)
266 + (data[i * 8 + 6] << 8)
267 + data[i * 8 + 7];
268 else if (address_bytes == 4)
269 address = (data[i * 8 + 5] << 24)
270 + (data[i * 8 + 6] << 16)
271 + (data[i * 8 + 7] << 8)
272 + data[i * 8 + 8];
273
274 /* skip address bytes and padding */
275 j += address_bytes + spi_cmd_vals->pad_bytes;
276
277 if (j > MAX_TRACE_BLOCKLENGTH) {
278 additional_pad_bytes = j -
279 MAX_TRACE_BLOCKLENGTH;
280 j = MAX_TRACE_BLOCKLENGTH;
281 }
282
Martin Rothe8a72dd2015-09-17 17:14:48 -0600283 printf("\nTime: %06lld.%08lld",
Martin Rothb54d6ba2015-09-29 14:49:37 -0600284 (timestamp - start_timestamp) /
285 100000000,
286 (timestamp - start_timestamp) %
287 100000000);
288 printf(" command # %-6d : 0x%02x - %s",
289 ++counter, spi_command,
290 spi_cmd_vals->cmd_name);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700291 curpos = 0;
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700292 outbytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700293 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600294
Stefan Reinauerc566d202015-08-25 09:52:42 -0700295 /* this exploits 8bit wrap around in curpos */
296 unsigned char blocklen = (data[2 + i*8 + 1] - curpos);
297 blocklen /= 8;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600298
299 for (; j < blocklen; j++) {
300 if (outbytes == 0) {
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800301 switch (spi_cmd_vals->address_type) {
302 case ADDR_DYNAMIC:
303 case ADDR_3B:
304 case ADDR_4B:
Martin Roth712262a2015-09-18 14:01:18 -0600305 printf("\n%08lx : ",
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800306 addr_offset + address);
307 break;
308 case ADDR_NO_OFF_3B:
309 printf("\n%08lx : ", address);
310 break;
311 case ADDR_NONE:
Martin Rothe8a72dd2015-09-17 17:14:48 -0600312 printf("\n : ");
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800313 break;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600314 }
315 }
316 printf("%02x ", data[i * 8 + 4 + j]);
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700317 outbytes++;
318 if (outbytes == 16) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600319 outbytes = 0;
Stefan Reinauer55e9b042020-12-11 13:41:53 -0800320 address += 16;
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700321 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700322 }
Martin Rothb54d6ba2015-09-29 14:49:37 -0600323 // this is because the em100 counts funny
324 curpos = data[2 + i*8 + 1] + 0x10;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600325 fflush(stdout);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700326 }
327 }
328 return 1;
329}
Martin Rothdfdff3e2015-09-18 09:42:03 -0600330
331#define UFIFO_SIZE 512
332#define UFIFO_TIMEOUT 0x00
333
334/*
335 * Polls the uFIFO buffer to see if there's any data. The HT registers don't
336 * seem to ever be updated to reflect that there's data present, and the
337 * Dediprog software doesn't use them either.
Martin Rothc69319c2015-09-29 14:09:40 -0600338 *
Martin Rothdfdff3e2015-09-18 09:42:03 -0600339 * Multiple messages can be in a single uFIFO transfer, so loop through
340 * the data looking for the signature.
341 */
Stefan Reinauerabd3e322019-12-04 18:29:24 -0800342int read_spi_terminal(struct em100 *em100, int show_counter)
343{
Martin Rothdfdff3e2015-09-18 09:42:03 -0600344 unsigned char data[UFIFO_SIZE] = { 0 };
345 static unsigned int msg_counter = 1; /* Number of messages */
Martin Rothc69319c2015-09-29 14:09:40 -0600346 uint16_t data_length;
Martin Rothdfdff3e2015-09-18 09:42:03 -0600347 unsigned char *data_start;
348 unsigned int j, k;
349 struct em100_msg *msg = NULL;
350
351 if (!read_ufifo(em100, UFIFO_SIZE, UFIFO_TIMEOUT, &data[0]))
352 return 0;
353
354 /* the first two bytes are the amount of valid data */
Martin Rothc69319c2015-09-29 14:09:40 -0600355 data_length = (data[0] << 8) + data[1];
356 if (data_length == 0)
Martin Rothdfdff3e2015-09-18 09:42:03 -0600357 return 1;
358
359 /* actual data starts after the length */
360 data_start = &data[sizeof(uint16_t)];
361
362 /* examine data; stop when we run out of message or buffer */
Martin Rothc69319c2015-09-29 14:09:40 -0600363 for (j = 0; j < data_length &&
Martin Rothdfdff3e2015-09-18 09:42:03 -0600364 j < UFIFO_SIZE - sizeof(struct em100_msg_header); j++) {
365
366 msg = (struct em100_msg *)(data_start + j);
367 if (msg->header.signature == EM100_MSG_SIGNATURE) {
368
369 if (show_counter)
370 printf("\nHT%06d: ", msg_counter);
371
372 /* print message byte according to format */
373 for (k = 0; k < msg->header.data_length; k++) {
Martin Rothc69319c2015-09-29 14:09:40 -0600374 if (&msg->data[k] >= data_start + data_length)
Martin Rothdfdff3e2015-09-18 09:42:03 -0600375 break;
376 if (&msg->data[k] >= &data[0] + UFIFO_SIZE)
377 break;
378
379 switch (msg->header.data_type) {
380 case ht_checkpoint_1byte:
381 case ht_checkpoint_2bytes:
382 case ht_checkpoint_4bytes:
383 case ht_hexadecimal_data:
384 case ht_timestamp_data:
385 printf("%02x ", msg->data[k]);
386 break;
387 case ht_ascii_data:
388 printf("%c", msg->data[k]);
389 break;
390 case ht_lookup_table:
391 /* TODO - support lookup table */
392 printf("Lookup unsupported: %02x%02x",
393 msg->data[k], msg->data[k + 1]);
394 k++;
395 break;
396 }
397 }
398
399 /* advance to the end of the message */
Martin Rothb54d6ba2015-09-29 14:49:37 -0600400 j += msg->header.data_length +
401 sizeof(struct em100_msg_header) - 1;
Martin Rothdfdff3e2015-09-18 09:42:03 -0600402 msg_counter++;
403 fflush(stdout);
404 }
405 }
406
407 return 1;
408}
409
410int init_spi_terminal (struct em100 *em100)
411{
412 int retval = 0x01;
413 uint16_t val;
414
415 retval &= write_ht_register(em100, ufifo_data_fmt_reg, 0);
416 retval &= write_ht_register(em100, status_reg, START_SPI_EMULATION);
417
418 /* set em100 to recognize spi command 0x11 */
419 retval &= write_fpga_register(em100, 0x82, EM100_SPECIFIC_CMD);
420 retval &= read_fpga_register(em100, 0x28, &val);
421
422 return retval;
423}