Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 30 | int 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 | */ |
| 50 | static unsigned int counter = 0; |
| 51 | static unsigned char curpos = 0; |
| 52 | static unsigned char cmdid = 0xff; // timestamp, so never a valid command id |
| 53 | |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 54 | #define REPORT_BUFFER_LENGTH 8192 |
| 55 | #define REPORT_BUFFER_COUNT 8 |
| 56 | |
| 57 | static int read_report_buffer(struct em100 *em100, |
| 58 | unsigned char reportdata[REPORT_BUFFER_COUNT][REPORT_BUFFER_LENGTH]) |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 59 | { |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 60 | unsigned char cmd[16] = {0}; |
| 61 | int len; |
| 62 | unsigned int report; |
| 63 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 64 | cmd[0] = 0xbc; /* read SPI trace buffer*/ |
| 65 | |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 66 | /* |
| 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 Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 72 | cmd[1] = 0x00; |
| 73 | cmd[2] = 0x00; |
| 74 | cmd[3] = 0x00; |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 75 | cmd[4] = REPORT_BUFFER_COUNT; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 76 | /* 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 Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 95 | |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 96 | 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 Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 101 | return 0; |
| 102 | } |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | return 1; |
| 106 | } |
| 107 | |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 108 | struct spi_cmd_values { |
| 109 | char *cmd_name; |
| 110 | uint8_t cmd; |
| 111 | uint8_t uses_address; |
| 112 | uint8_t pad_bytes; |
| 113 | }; |
| 114 | |
| 115 | struct 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 | |
| 134 | static 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 Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 151 | int 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 Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 157 | 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 Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 162 | |
| 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 Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 168 | count = (data[0] << 8) | data[1]; |
| 169 | for (i = 0; i < count; i++) { |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 170 | unsigned int j = additional_pad_bytes; |
| 171 | additional_pad_bytes = 0; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 172 | unsigned char cmd = data[2 + i*8]; |
| 173 | if (cmd == 0xff) { |
| 174 | /* timestamp */ |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 175 | 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 Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 181 | continue; |
| 182 | } |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 183 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 184 | /* from here, it must be data */ |
| 185 | if (cmd != cmdid) { |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 186 | unsigned char spi_command = data[i * 8 + 4]; |
| 187 | spi_cmd_vals = get_command_vals(spi_command); |
| 188 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 189 | /* new command */ |
| 190 | cmdid = cmd; |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 191 | 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 Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 214 | curpos = 0; |
Stefan Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 215 | outbytes = 0; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 216 | } |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 217 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 218 | /* this exploits 8bit wrap around in curpos */ |
| 219 | unsigned char blocklen = (data[2 + i*8 + 1] - curpos); |
| 220 | blocklen /= 8; |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 221 | |
| 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 Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 231 | outbytes++; |
| 232 | if (outbytes == 16) { |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 233 | outbytes = 0; |
| 234 | if (spi_cmd_vals->uses_address) |
| 235 | address += 16; |
Stefan Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 236 | } |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 237 | } |
| 238 | curpos = data[2 + i*8 + 1] + 0x10; // this is because the em100 counts funny |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame^] | 239 | fflush(stdout); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | return 1; |
| 243 | } |