blob: f92f3463813985761ca947687048b41e37223456 [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 "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++) {
97 len = get_response(em100->dev, &reportdata[report][0], REPORT_BUFFER_LENGTH);
98 if (len != REPORT_BUFFER_LENGTH) {
99 printf("error, report length = %d instead of %d.\n\n",
100 len, REPORT_BUFFER_LENGTH);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700101 return 0;
102 }
Martin Roth330de302015-09-17 16:33:07 -0600103 }
104
105 return 1;
106}
107
Martin Rothe8a72dd2015-09-17 17:14:48 -0600108struct spi_cmd_values {
109 char *cmd_name;
110 uint8_t cmd;
111 uint8_t uses_address;
112 uint8_t pad_bytes;
113};
114
115struct spi_cmd_values spi_command_list[] = {
116 /* name cmd, addr, pad */
117 {"write status register", 0x01, 0, 0},
118 {"page program", 0x02, 1, 0},
119 {"read", 0x03, 1, 0},
120 {"write disable", 0x04, 0, 0},
121 {"read status register", 0x05, 0, 0},
122 {"write enable", 0x06, 0, 0},
123 {"fast read", 0x0b, 1, 1},
124 {"EM100 specific", 0x11, 0, 0},
125 {"fast dual read", 0x3b, 1, 2},
126 {"chip erase", 0x60, 0, 0},
127 {"read JEDEC ID", 0x9f, 0, 0},
128 {"chip erase", 0xc7, 0, 0},
129 {"sector erase", 0xd8, 1, 0},
130
131 {"unknown command", 0xff, 0, 0}
132};
133
134static struct spi_cmd_values * get_command_vals(uint8_t command) {
135 /* cache last command so a search isn't needed every time */
136 static struct spi_cmd_values *spi_cmd = &spi_command_list[3]; /* init to read */
137 int i;
138
139 if (spi_cmd->cmd != command) {
140 for (i = 0; spi_command_list[i].cmd != 0xff; i++) {
141 if (spi_command_list[i].cmd == command)
142 break;
143 }
144 spi_cmd = &spi_command_list[i];
145 }
146
147 return spi_cmd;
148}
149
150#define MAX_TRACE_BLOCKLENGTH 6
Martin Roth330de302015-09-17 16:33:07 -0600151int read_spi_trace(struct em100 *em100)
152{
153 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH] = {{0}};
154 unsigned char *data;
155 unsigned int count, i, report;
156 static int outbytes = 0;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600157 static int additional_pad_bytes = 0;
158 static unsigned int address = 0;
159 static unsigned long long timestamp = 0;
160 static unsigned long long start_timestamp = 0;
161 static struct spi_cmd_values *spi_cmd_vals = &spi_command_list[3];
Martin Roth330de302015-09-17 16:33:07 -0600162
163 if (!read_report_buffer(em100, reportdata))
164 return 0;
165
166 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
167 data = &reportdata[report][0];
Stefan Reinauerc566d202015-08-25 09:52:42 -0700168 count = (data[0] << 8) | data[1];
169 for (i = 0; i < count; i++) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600170 unsigned int j = additional_pad_bytes;
171 additional_pad_bytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700172 unsigned char cmd = data[2 + i*8];
173 if (cmd == 0xff) {
174 /* timestamp */
Stefan Reinauerc566d202015-08-25 09:52:42 -0700175 timestamp = data[2 + i*8 + 2];
176 timestamp = (timestamp << 8) | data[2 + i*8 + 3];
177 timestamp = (timestamp << 8) | data[2 + i*8 + 4];
178 timestamp = (timestamp << 8) | data[2 + i*8 + 5];
179 timestamp = (timestamp << 8) | data[2 + i*8 + 6];
180 timestamp = (timestamp << 8) | data[2 + i*8 + 7];
Stefan Reinauerc566d202015-08-25 09:52:42 -0700181 continue;
182 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600183
Stefan Reinauerc566d202015-08-25 09:52:42 -0700184 /* from here, it must be data */
185 if (cmd != cmdid) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600186 unsigned char spi_command = data[i * 8 + 4];
187 spi_cmd_vals = get_command_vals(spi_command);
188
Stefan Reinauerc566d202015-08-25 09:52:42 -0700189 /* new command */
190 cmdid = cmd;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600191 if (counter == 0)
192 start_timestamp = timestamp;
193
194 /* set up address if used by this command*/
195 if (!spi_cmd_vals->uses_address) {
196 j = 1; /* skip command byte */
197 } else {
198 address = (data[i * 8 + 5] << 16) +
199 (data[i * 8 + 6] << 8) +
200 data[i * 8 + 7];
201
202 /* skip command, address bytes, and padding */
203 j = 4 + spi_cmd_vals->pad_bytes;
204 if (j > MAX_TRACE_BLOCKLENGTH) {
205 additional_pad_bytes = j - MAX_TRACE_BLOCKLENGTH;
206 j = MAX_TRACE_BLOCKLENGTH;
207 }
208 }
209 printf("\nTime: %06lld.%08lld",
210 (timestamp - start_timestamp) / 100000000,
211 (timestamp - start_timestamp) % 100000000);
212 printf(" command # %-6d : 0x%02x - %s", ++counter,
213 spi_command,spi_cmd_vals->cmd_name);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700214 curpos = 0;
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700215 outbytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700216 }
Martin Rothe8a72dd2015-09-17 17:14:48 -0600217
Stefan Reinauerc566d202015-08-25 09:52:42 -0700218 /* this exploits 8bit wrap around in curpos */
219 unsigned char blocklen = (data[2 + i*8 + 1] - curpos);
220 blocklen /= 8;
Martin Rothe8a72dd2015-09-17 17:14:48 -0600221
222 for (; j < blocklen; j++) {
223 if (outbytes == 0) {
224 if (spi_cmd_vals->uses_address) {
225 printf("\n%08x : ", address);
226 } else {
227 printf("\n : ");
228 }
229 }
230 printf("%02x ", data[i * 8 + 4 + j]);
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700231 outbytes++;
232 if (outbytes == 16) {
Martin Rothe8a72dd2015-09-17 17:14:48 -0600233 outbytes = 0;
234 if (spi_cmd_vals->uses_address)
235 address += 16;
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700236 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700237 }
238 curpos = data[2 + i*8 + 1] + 0x10; // this is because the em100 counts funny
Martin Rothe8a72dd2015-09-17 17:14:48 -0600239 fflush(stdout);
Stefan Reinauerc566d202015-08-25 09:52:42 -0700240 }
241 }
242 return 1;
243}