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 | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame^] | 151 | int read_spi_trace(struct em100 *em100, int display_terminal) |
Martin Roth | 330de30 | 2015-09-17 16:33:07 -0600 | [diff] [blame] | 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]; |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame^] | 181 | if (display_terminal) |
| 182 | read_spi_terminal(em100, 1); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 183 | continue; |
| 184 | } |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame] | 185 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 186 | /* from here, it must be data */ |
| 187 | if (cmd != cmdid) { |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame] | 188 | unsigned char spi_command = data[i * 8 + 4]; |
| 189 | spi_cmd_vals = get_command_vals(spi_command); |
| 190 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 191 | /* new command */ |
| 192 | cmdid = cmd; |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame] | 193 | if (counter == 0) |
| 194 | start_timestamp = timestamp; |
| 195 | |
| 196 | /* set up address if used by this command*/ |
| 197 | if (!spi_cmd_vals->uses_address) { |
| 198 | j = 1; /* skip command byte */ |
| 199 | } else { |
| 200 | address = (data[i * 8 + 5] << 16) + |
| 201 | (data[i * 8 + 6] << 8) + |
| 202 | data[i * 8 + 7]; |
| 203 | |
| 204 | /* skip command, address bytes, and padding */ |
| 205 | j = 4 + spi_cmd_vals->pad_bytes; |
| 206 | if (j > MAX_TRACE_BLOCKLENGTH) { |
| 207 | additional_pad_bytes = j - MAX_TRACE_BLOCKLENGTH; |
| 208 | j = MAX_TRACE_BLOCKLENGTH; |
| 209 | } |
| 210 | } |
| 211 | printf("\nTime: %06lld.%08lld", |
| 212 | (timestamp - start_timestamp) / 100000000, |
| 213 | (timestamp - start_timestamp) % 100000000); |
| 214 | printf(" command # %-6d : 0x%02x - %s", ++counter, |
| 215 | spi_command,spi_cmd_vals->cmd_name); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 216 | curpos = 0; |
Stefan Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 217 | outbytes = 0; |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 218 | } |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame] | 219 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 220 | /* this exploits 8bit wrap around in curpos */ |
| 221 | unsigned char blocklen = (data[2 + i*8 + 1] - curpos); |
| 222 | blocklen /= 8; |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame] | 223 | |
| 224 | for (; j < blocklen; j++) { |
| 225 | if (outbytes == 0) { |
| 226 | if (spi_cmd_vals->uses_address) { |
| 227 | printf("\n%08x : ", address); |
| 228 | } else { |
| 229 | printf("\n : "); |
| 230 | } |
| 231 | } |
| 232 | printf("%02x ", data[i * 8 + 4 + j]); |
Stefan Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 233 | outbytes++; |
| 234 | if (outbytes == 16) { |
Martin Roth | e8a72dd | 2015-09-17 17:14:48 -0600 | [diff] [blame] | 235 | outbytes = 0; |
| 236 | if (spi_cmd_vals->uses_address) |
| 237 | address += 16; |
Stefan Reinauer | d7f959b | 2015-09-03 16:03:40 -0700 | [diff] [blame] | 238 | } |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 239 | } |
| 240 | 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] | 241 | fflush(stdout); |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | return 1; |
| 245 | } |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame^] | 246 | |
| 247 | #define UFIFO_SIZE 512 |
| 248 | #define UFIFO_TIMEOUT 0x00 |
| 249 | |
| 250 | /* |
| 251 | * Polls the uFIFO buffer to see if there's any data. The HT registers don't |
| 252 | * seem to ever be updated to reflect that there's data present, and the |
| 253 | * Dediprog software doesn't use them either. |
| 254 | * |
| 255 | * Multiple messages can be in a single uFIFO transfer, so loop through |
| 256 | * the data looking for the signature. |
| 257 | */ |
| 258 | int read_spi_terminal(struct em100 *em100, int show_counter) { |
| 259 | unsigned char data[UFIFO_SIZE] = { 0 }; |
| 260 | static unsigned int msg_counter = 1; /* Number of messages */ |
| 261 | uint16_t *data_length; |
| 262 | unsigned char *data_start; |
| 263 | unsigned int j, k; |
| 264 | struct em100_msg *msg = NULL; |
| 265 | |
| 266 | if (!read_ufifo(em100, UFIFO_SIZE, UFIFO_TIMEOUT, &data[0])) |
| 267 | return 0; |
| 268 | |
| 269 | /* the first two bytes are the amount of valid data */ |
| 270 | data_length = (uint16_t *) &data[0]; |
| 271 | if (*data_length == 0) |
| 272 | return 1; |
| 273 | |
| 274 | /* actual data starts after the length */ |
| 275 | data_start = &data[sizeof(uint16_t)]; |
| 276 | |
| 277 | /* examine data; stop when we run out of message or buffer */ |
| 278 | for (j = 0; j < *data_length && |
| 279 | j < UFIFO_SIZE - sizeof(struct em100_msg_header); j++) { |
| 280 | |
| 281 | msg = (struct em100_msg *)(data_start + j); |
| 282 | if (msg->header.signature == EM100_MSG_SIGNATURE) { |
| 283 | |
| 284 | if (show_counter) |
| 285 | printf("\nHT%06d: ", msg_counter); |
| 286 | |
| 287 | /* print message byte according to format */ |
| 288 | for (k = 0; k < msg->header.data_length; k++) { |
| 289 | if (&msg->data[k] >= data_start + *data_length) |
| 290 | break; |
| 291 | if (&msg->data[k] >= &data[0] + UFIFO_SIZE) |
| 292 | break; |
| 293 | |
| 294 | switch (msg->header.data_type) { |
| 295 | case ht_checkpoint_1byte: |
| 296 | case ht_checkpoint_2bytes: |
| 297 | case ht_checkpoint_4bytes: |
| 298 | case ht_hexadecimal_data: |
| 299 | case ht_timestamp_data: |
| 300 | printf("%02x ", msg->data[k]); |
| 301 | break; |
| 302 | case ht_ascii_data: |
| 303 | printf("%c", msg->data[k]); |
| 304 | break; |
| 305 | case ht_lookup_table: |
| 306 | /* TODO - support lookup table */ |
| 307 | printf("Lookup unsupported: %02x%02x", |
| 308 | msg->data[k], msg->data[k + 1]); |
| 309 | k++; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /* advance to the end of the message */ |
| 315 | j += msg->header.data_length + sizeof(struct em100_msg_header); |
| 316 | msg_counter++; |
| 317 | fflush(stdout); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return 1; |
| 322 | } |
| 323 | |
| 324 | int init_spi_terminal (struct em100 *em100) |
| 325 | { |
| 326 | int retval = 0x01; |
| 327 | uint16_t val; |
| 328 | |
| 329 | retval &= write_ht_register(em100, ufifo_data_fmt_reg, 0); |
| 330 | retval &= write_ht_register(em100, status_reg, START_SPI_EMULATION); |
| 331 | |
| 332 | /* set em100 to recognize spi command 0x11 */ |
| 333 | retval &= write_fpga_register(em100, 0x82, EM100_SPECIFIC_CMD); |
| 334 | retval &= read_fpga_register(em100, 0x28, &val); |
| 335 | |
| 336 | return retval; |
| 337 | } |