blob: 43d5fa9e03b6c768d4fdd2ee596ef8b608880691 [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
108int read_spi_trace(struct em100 *em100)
109{
110 unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH] = {{0}};
111 unsigned char *data;
112 unsigned int count, i, report;
113 static int outbytes = 0;
114
115 if (!read_report_buffer(em100, reportdata))
116 return 0;
117
118 for (report = 0; report < REPORT_BUFFER_COUNT; report++) {
119 data = &reportdata[report][0];
Stefan Reinauerc566d202015-08-25 09:52:42 -0700120 count = (data[0] << 8) | data[1];
121 for (i = 0; i < count; i++) {
122 unsigned int j;
123 unsigned char cmd = data[2 + i*8];
124 if (cmd == 0xff) {
125 /* timestamp */
126 unsigned long long timestamp = 0;
127 timestamp = data[2 + i*8 + 2];
128 timestamp = (timestamp << 8) | data[2 + i*8 + 3];
129 timestamp = (timestamp << 8) | data[2 + i*8 + 4];
130 timestamp = (timestamp << 8) | data[2 + i*8 + 5];
131 timestamp = (timestamp << 8) | data[2 + i*8 + 6];
132 timestamp = (timestamp << 8) | data[2 + i*8 + 7];
133 printf("\ntimestamp: %lld.%lld", timestamp / 100000000, timestamp % 100000000);
134 continue;
135 }
136#if 0
137 printf("{(%d)", curpos);
138 for (j = 0; j < 8; j++) {
139 printf("%02x ", data[2 + i*8 + j]);
140 }
141 printf("}");
142#endif
143 /* from here, it must be data */
144 if (cmd != cmdid) {
145 /* new command */
146 cmdid = cmd;
147 printf("\nspi command %6d: ", ++counter);
148 curpos = 0;
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700149 outbytes = 0;
Stefan Reinauerc566d202015-08-25 09:52:42 -0700150 }
151 /* this exploits 8bit wrap around in curpos */
152 unsigned char blocklen = (data[2 + i*8 + 1] - curpos);
153 blocklen /= 8;
154 for (j = 0; j < blocklen; j++) {
155 printf("%02x ", data[2 + i*8 + 2 + j]);
Stefan Reinauerd7f959b2015-09-03 16:03:40 -0700156 outbytes++;
157 if (outbytes == 16) {
158 outbytes=0;
159 printf("\n ");
160 }
Stefan Reinauerc566d202015-08-25 09:52:42 -0700161 }
162 curpos = data[2 + i*8 + 1] + 0x10; // this is because the em100 counts funny
163 }
164 }
165 return 1;
166}