Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1 | /* |
Stefan Reinauer | 5cf0da7 | 2015-08-17 15:22:20 -0700 | [diff] [blame] | 2 | * Copyright 2012-2015 Google Inc. |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 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 Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <string.h> |
Martin Roth | 92c453b | 2015-09-01 10:35:02 -0600 | [diff] [blame] | 17 | #include <strings.h> |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 18 | #include <getopt.h> |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 19 | #include <unistd.h> |
Stefan Reinauer | 8db88b9 | 2020-12-17 19:22:13 -0800 | [diff] [blame] | 20 | #include <ctype.h> |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 21 | #include <dirent.h> |
| 22 | #include <errno.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <wordexp.h> |
| 25 | |
Stefan Reinauer | c566d20 | 2015-08-25 09:52:42 -0700 | [diff] [blame] | 26 | #include "em100.h" |
Martin Roth | 4203a38 | 2014-08-02 08:14:37 -0600 | [diff] [blame] | 27 | |
Stefan Reinauer | afe0325 | 2020-11-21 16:03:59 -0800 | [diff] [blame] | 28 | #define MAX_USB_ERRORS 10 |
| 29 | |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 30 | TFILE *configs; |
| 31 | char *database_version; |
Stefan Reinauer | b20610e | 2019-12-06 11:14:33 -0800 | [diff] [blame] | 32 | int debug = 0; |
Stefan Reinauer | dfb107d | 2020-12-10 19:22:40 -0800 | [diff] [blame] | 33 | int address_mode = 3; |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 34 | |
Stefan Reinauer | ed59fc8 | 2020-11-21 16:11:40 -0800 | [diff] [blame] | 35 | volatile sig_atomic_t exit_requested = 0; |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 36 | static struct em100 em100_state; |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 37 | |
Stefan Reinauer | 6a5ed5b | 2019-11-19 14:52:16 -0800 | [diff] [blame] | 38 | static void exit_handler(int sig __unused) |
| 39 | { |
Stefan Reinauer | ed59fc8 | 2020-11-21 16:11:40 -0800 | [diff] [blame] | 40 | exit_requested = 1; |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Aaron Durbin | 637fe48 | 2012-10-17 17:39:19 -0500 | [diff] [blame] | 43 | struct em100_hold_pin_states { |
| 44 | const char *description; |
| 45 | int value; |
| 46 | }; |
| 47 | |
Stefan Reinauer | fba0063 | 2012-10-18 09:17:00 -0700 | [diff] [blame] | 48 | static const struct em100_hold_pin_states hold_pin_states[] = { |
Aaron Durbin | 637fe48 | 2012-10-17 17:39:19 -0500 | [diff] [blame] | 49 | { "FLOAT", 0x2 }, |
| 50 | { "LOW", 0x0 }, |
| 51 | { "INPUT", 0x3 }, |
| 52 | { NULL, 0x0 }, |
| 53 | }; |
| 54 | |
Stefan Reinauer | 54f91d9 | 2015-08-18 12:00:07 -0700 | [diff] [blame] | 55 | /* High Level functions */ |
| 56 | |
Stefan Reinauer | b82d79b | 2015-08-18 11:14:33 -0700 | [diff] [blame] | 57 | static int set_state(struct em100 *em100, int run) |
| 58 | { |
Martin Roth | da63eb7 | 2015-09-11 12:16:23 -0600 | [diff] [blame] | 59 | int retval = write_fpga_register(em100, 0x28, run & 1); |
| 60 | |
| 61 | if (retval) |
| 62 | printf("%s EM100Pro\n", run ? "Started" : "Stopped"); |
| 63 | |
| 64 | return retval; |
| 65 | } |
| 66 | |
| 67 | static void get_current_state(struct em100 *em100) |
| 68 | { |
| 69 | uint16_t state; |
| 70 | if (read_fpga_register(em100, 0x28, &state)) |
| 71 | printf("EM100Pro currently %s\n", state ? "running" : "stopped"); |
| 72 | else |
| 73 | printf("EM100Pro state unknown\n"); |
| 74 | } |
| 75 | |
Duncan Laurie | f436156 | 2020-04-15 12:54:51 -0700 | [diff] [blame] | 76 | static int set_address_mode(struct em100 *em100, int mode) |
| 77 | { |
| 78 | int retval; |
| 79 | |
| 80 | if (mode < 3 || mode > 4) { |
| 81 | printf("Invalid address mode: %d\n", mode); |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | retval = write_fpga_register(em100, 0x4f, mode == 4); |
| 86 | if (retval) |
| 87 | printf("Enabled %d byte address mode\n", mode); |
| 88 | else |
| 89 | printf("Failed to enable %d byte address mode\n", mode); |
| 90 | |
| 91 | return retval; |
| 92 | } |
| 93 | |
Stefan Reinauer | 7953388 | 2019-11-22 00:57:29 -0800 | [diff] [blame] | 94 | static const char *get_pin_string(int pin) { |
Martin Roth | da63eb7 | 2015-09-11 12:16:23 -0600 | [diff] [blame] | 95 | switch (pin) { |
| 96 | case 0: |
| 97 | return ("low"); |
| 98 | break; |
| 99 | case 2: |
| 100 | return ("float"); |
| 101 | break; |
| 102 | case 3: |
| 103 | return ("input"); |
| 104 | break; |
| 105 | } |
| 106 | return ("unknown"); |
| 107 | } |
| 108 | |
| 109 | static void get_current_pin_state(struct em100 *em100) |
| 110 | { |
| 111 | uint16_t val = 0xffff; |
| 112 | read_fpga_register(em100, 0x2a, &val); |
| 113 | printf("EM100Pro hold pin currently %s\n", get_pin_string(val)); |
Stefan Reinauer | b82d79b | 2015-08-18 11:14:33 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static int set_hold_pin_state(struct em100 *em100, int pin_state) |
| 117 | { |
| 118 | uint16_t val; |
| 119 | |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 120 | /* Read and acknowledge hold pin state setting bit 2 of pin state response. */ |
Stefan Reinauer | b82d79b | 2015-08-18 11:14:33 -0700 | [diff] [blame] | 121 | if (!read_fpga_register(em100, 0x2a, &val)) { |
| 122 | printf("Couldn't get hold pin state.\n"); |
| 123 | return 0; |
| 124 | } |
| 125 | write_fpga_register(em100, 0x2a, (1 << 2) | val); |
| 126 | |
| 127 | |
| 128 | if (!read_fpga_register(em100, 0x2a, &val)) { |
| 129 | printf("Couldn't get hold pin state.\n"); |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | /* Now set desired pin state. */ |
| 134 | write_fpga_register(em100, 0x2a, pin_state); |
| 135 | |
| 136 | /* Read the pin state. */ |
| 137 | if (!read_fpga_register(em100, 0x2a, &val)) { |
| 138 | printf("Couldn't get hold pin state.\n"); |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | if (val != pin_state) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 143 | printf("Invalid pin state response: 0x%04x %s" |
| 144 | " (expected 0x%04x %s)\n", val, |
| 145 | get_pin_string(val), pin_state, |
| 146 | get_pin_string(pin_state)); |
Stefan Reinauer | b82d79b | 2015-08-18 11:14:33 -0700 | [diff] [blame] | 147 | return 0; |
| 148 | } |
| 149 | |
Martin Roth | da63eb7 | 2015-09-11 12:16:23 -0600 | [diff] [blame] | 150 | printf("Hold pin state set to %s\n", get_pin_string(val)); |
Stefan Reinauer | b82d79b | 2015-08-18 11:14:33 -0700 | [diff] [blame] | 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | static int set_hold_pin_state_from_str(struct em100 *em100, const char *state) |
| 155 | { |
| 156 | int pin_state; |
| 157 | const struct em100_hold_pin_states *s = &hold_pin_states[0]; |
| 158 | |
| 159 | while (s->description != NULL) { |
Martin Roth | f4f581a | 2015-09-18 11:39:27 -0600 | [diff] [blame] | 160 | if (!strcasecmp(s->description, state)) |
Stefan Reinauer | b82d79b | 2015-08-18 11:14:33 -0700 | [diff] [blame] | 161 | break; |
| 162 | s++; |
| 163 | } |
| 164 | if (s->description == NULL) { |
| 165 | printf("Invalid hold pin state: %s\n", state); |
| 166 | return 0; |
| 167 | } |
| 168 | pin_state = s->value; |
| 169 | |
| 170 | return set_hold_pin_state(em100, pin_state); |
| 171 | } |
| 172 | |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 173 | static int set_fpga_voltage(struct em100 *em100, int voltage_code) |
| 174 | { |
| 175 | int val; |
| 176 | |
| 177 | if (!fpga_reconfigure(em100)) { |
| 178 | printf("Couldn't reconfigure FPGA.\n"); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | if (!fpga_set_voltage(em100, voltage_code)) { |
| 183 | printf("Couldn't set FPGA voltage.\n"); |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* Must wait 2s before issuing any other USB comand */ |
| 188 | sleep(2); |
| 189 | |
| 190 | if (!fpga_get_voltage(em100, &val)) { |
| 191 | printf("Couldn't get FPGA voltage.\n"); |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | if (val != voltage_code) { |
| 196 | printf("Invalid voltage response: %#x (expected %#x)\n", val, |
| 197 | voltage_code); |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | printf("Voltage set to %s\n", val == 18 ? "1.8" : "3.3"); |
| 202 | |
| 203 | return 1; |
| 204 | } |
| 205 | |
| 206 | static int set_fpga_voltage_from_str(struct em100 *em100, |
| 207 | const char *voltage_str) |
| 208 | { |
| 209 | int voltage_code; |
| 210 | |
| 211 | if (!strcmp(voltage_str, "3.3")) |
| 212 | voltage_code = 33; |
| 213 | else if (!strcmp(voltage_str, "1.8")) |
| 214 | voltage_code = 18; |
| 215 | else { |
| 216 | printf("Invalid voltage, use 1.8 or 3.3.\n"); |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | return set_fpga_voltage(em100, voltage_code); |
| 221 | } |
| 222 | |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 223 | /** |
Stefan Reinauer | 98eb753 | 2019-11-13 18:42:07 -0800 | [diff] [blame] | 224 | * get_device_info: fetch device's serial number and hardware version |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 225 | * @param em100: initialized em100 device structure |
| 226 | */ |
Stefan Reinauer | 98eb753 | 2019-11-13 18:42:07 -0800 | [diff] [blame] | 227 | static int get_device_info(struct em100 *em100) |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 228 | { |
| 229 | unsigned char data[256]; |
| 230 | if (read_spi_flash_page(em100, 0x1fff00, data)) { |
| 231 | em100->serialno = (data[5] << 24) | (data[4] << 16) | \ |
| 232 | (data[3] << 8) | data[2]; |
Stefan Reinauer | 98eb753 | 2019-11-13 18:42:07 -0800 | [diff] [blame] | 233 | em100->hwversion = data[1]; |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 234 | return 1; |
| 235 | } |
| 236 | return 0; |
| 237 | } |
| 238 | |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 239 | static int set_serialno(struct em100 *em100, unsigned int serialno) |
| 240 | { |
| 241 | unsigned char data[512]; |
| 242 | unsigned int old_serialno; |
| 243 | |
| 244 | if (!read_spi_flash_page(em100, 0x1fff00, data)) |
| 245 | return 0; |
| 246 | |
| 247 | old_serialno = (data[5] << 24) | (data[4] << 16) | \ |
| 248 | (data[3] << 8) | data[2]; |
| 249 | |
| 250 | if (old_serialno == serialno) { |
| 251 | printf("Serial number unchanged.\n"); |
| 252 | return 1; |
| 253 | } |
| 254 | |
| 255 | data[2] = serialno; |
| 256 | data[3] = serialno >> 8; |
| 257 | data[4] = serialno >> 16; |
| 258 | data[5] = serialno >> 24; |
| 259 | |
| 260 | if (old_serialno != 0xffffffff) { |
| 261 | /* preserve magic */ |
| 262 | read_spi_flash_page(em100, 0x1f0000, data + 256); |
| 263 | /* Unlock and erase sector. Reading |
| 264 | * the SPI flash ID is requires to |
| 265 | * actually unlock the chip. |
| 266 | */ |
| 267 | unlock_spi_flash(em100); |
| 268 | get_spi_flash_id(em100); |
| 269 | erase_spi_flash_sector(em100, 0x1f); |
| 270 | /* write back magic */ |
| 271 | write_spi_flash_page(em100, 0x1f0000, data + 256); |
| 272 | } |
| 273 | |
| 274 | if (!write_spi_flash_page(em100, 0x1fff00, data)) { |
| 275 | printf("Error: Could not write SPI flash.\n"); |
| 276 | return 0; |
| 277 | } |
Stefan Reinauer | 98eb753 | 2019-11-13 18:42:07 -0800 | [diff] [blame] | 278 | |
| 279 | /* Re-read serial number */ |
| 280 | get_device_info(em100); |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 281 | if (em100->serialno != 0xffffffff) |
Stefan Reinauer | bd8e1d0 | 2019-11-28 15:48:51 -0800 | [diff] [blame] | 282 | printf("New serial number: %s%06d\n", |
| 283 | em100->hwversion == HWVERSION_EM100PRO_EARLY ? "DP" : "EM", |
| 284 | em100->serialno); |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 285 | else |
| 286 | printf("New serial number: N.A.\n"); |
| 287 | |
| 288 | return 1; |
| 289 | } |
| 290 | |
Stefan Reinauer | 14d3512 | 2015-09-03 16:02:55 -0700 | [diff] [blame] | 291 | static int em100_debug(struct em100 *em100) |
| 292 | { |
| 293 | int i; |
Stefan Reinauer | 828687b | 2020-11-21 17:27:05 -0800 | [diff] [blame] | 294 | printf("Voltages:\n"); |
Stefan Reinauer | 14d3512 | 2015-09-03 16:02:55 -0700 | [diff] [blame] | 295 | set_led(em100, both_off); |
| 296 | printf(" 1.2V: %dmV\n", get_voltage(em100, in_v1_2)); |
| 297 | printf(" E_VCC: %dmV\n", get_voltage(em100, in_e_vcc)); |
| 298 | set_led(em100, both_on); |
| 299 | printf(" REF+: %dmV\n", get_voltage(em100, in_ref_plus)); |
| 300 | printf(" REF-: %dmV\n", get_voltage(em100, in_ref_minus)); |
| 301 | set_led(em100, red_on); |
| 302 | printf(" Buffer VCC: %dmV\n", get_voltage(em100, in_buffer_vcc)); |
| 303 | printf(" Trig VCC: %dmV\n", get_voltage(em100, in_trigger_vcc)); |
| 304 | set_led(em100, both_on); |
| 305 | printf(" RST VCC: %dmV\n", get_voltage(em100, in_reset_vcc)); |
| 306 | printf(" 3.3V: %dmV\n", get_voltage(em100, in_v3_3)); |
| 307 | set_led(em100, red_on); |
| 308 | printf(" Buffer 3.3V: %dmV\n", get_voltage(em100, in_buffer_v3_3)); |
| 309 | printf(" 5V: %dmV\n", get_voltage(em100, in_v5)); |
| 310 | set_led(em100, green_on); |
| 311 | printf("\nFPGA registers:"); |
| 312 | for (i = 0; i < 256; i += 2) { |
| 313 | uint16_t val; |
| 314 | if ((i % 16) == 0) |
| 315 | printf("\n %04x: ", i); |
| 316 | if (read_fpga_register(em100, i, &val)) |
| 317 | printf("%04x ", val); |
| 318 | else |
| 319 | printf("XXXX "); |
| 320 | } |
| 321 | |
| 322 | printf("\n"); |
| 323 | return 1; |
| 324 | } |
| 325 | |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 326 | static int check_status(struct em100 *em100) |
| 327 | { |
| 328 | int spi_flash_id; |
| 329 | |
| 330 | spi_flash_id = get_spi_flash_id(em100); |
Stefan Reinauer | 22340e8 | 2015-08-24 12:26:20 -0700 | [diff] [blame] | 331 | /* Check for Micron (formerly Numonyx, formerly STMicro) |
| 332 | * M25P16 spi flash part |
| 333 | */ |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 334 | if (spi_flash_id == 0x202015) |
| 335 | return 1; |
| 336 | return 0; |
| 337 | } |
| 338 | |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 339 | static int em100_init(struct em100 *em100, libusb_context *ctx, |
| 340 | libusb_device_handle *dev) |
Stefan Reinauer | 9fbe20d | 2015-09-09 12:21:00 -0700 | [diff] [blame] | 341 | { |
Stefan Reinauer | 3e8fdf1 | 2020-11-19 16:38:26 -0800 | [diff] [blame] | 342 | if (libusb_set_auto_detach_kernel_driver(dev, 1) != LIBUSB_SUCCESS) { |
| 343 | printf("Can't detach kernel driver.\n"); |
| 344 | return 0; |
Stefan Reinauer | 9fbe20d | 2015-09-09 12:21:00 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Stefan Reinauer | 848fe72 | 2020-11-19 16:39:45 -0800 | [diff] [blame] | 347 | if (libusb_claim_interface(dev, 0)) { |
Stefan Reinauer | 9fbe20d | 2015-09-09 12:21:00 -0700 | [diff] [blame] | 348 | printf("Could not claim interface.\n"); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | em100->dev = dev; |
| 353 | em100->ctx = ctx; |
| 354 | |
| 355 | if (!check_status(em100)) { |
| 356 | printf("Device status unknown.\n"); |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | if (!get_version(em100)) { |
| 361 | printf("Failed to fetch version information.\n"); |
| 362 | return 0; |
| 363 | } |
| 364 | |
Stefan Reinauer | 98eb753 | 2019-11-13 18:42:07 -0800 | [diff] [blame] | 365 | if (!get_device_info(em100)) { |
| 366 | printf("Failed to fetch serial number and hardware version.\n"); |
Stefan Reinauer | 9fbe20d | 2015-09-09 12:21:00 -0700 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | return 1; |
| 371 | } |
| 372 | |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 373 | static int em100_attach(struct em100 *em100, int bus, int device, |
Stefan Reinauer | 7953388 | 2019-11-22 00:57:29 -0800 | [diff] [blame] | 374 | uint32_t serial_number) |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 375 | { |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 376 | libusb_device_handle *dev = NULL; |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 377 | libusb_context *ctx = NULL; |
| 378 | |
| 379 | if (libusb_init(&ctx) < 0) { |
| 380 | printf("Could not init libusb.\n"); |
| 381 | return 0; |
| 382 | } |
| 383 | |
Stefan Reinauer | 220a099 | 2018-06-09 19:23:58 +0000 | [diff] [blame] | 384 | #if LIBUSB_API_VERSION < 0x01000106 |
Stefan Reinauer | d73d50b | 2020-11-19 16:37:07 -0800 | [diff] [blame] | 385 | libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_INFO); |
Stefan Reinauer | 220a099 | 2018-06-09 19:23:58 +0000 | [diff] [blame] | 386 | #else |
Stefan Reinauer | d73d50b | 2020-11-19 16:37:07 -0800 | [diff] [blame] | 387 | libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO); |
Stefan Reinauer | 220a099 | 2018-06-09 19:23:58 +0000 | [diff] [blame] | 388 | #endif |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 389 | |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 390 | if ((!bus || !device) && !serial_number) { |
Stefan Reinauer | 081bb82 | 2015-09-03 16:26:10 -0700 | [diff] [blame] | 391 | dev = libusb_open_device_with_vid_pid(ctx, 0x4b4, 0x1235); |
| 392 | } else { |
| 393 | libusb_device **devs, *d; |
| 394 | int i; |
| 395 | |
| 396 | if (libusb_get_device_list(ctx, &devs) < 0) { |
| 397 | printf("Could not find USB devices.\n"); |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | for (i = 0; (d = devs[i]) != NULL; i++) { |
| 402 | if ((bus > 0 && (libusb_get_bus_number(d) == bus)) && |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 403 | (device > 0 && |
| 404 | (libusb_get_device_address(d) == device))) { |
| 405 | |
Stefan Reinauer | 081bb82 | 2015-09-03 16:26:10 -0700 | [diff] [blame] | 406 | struct libusb_device_descriptor desc; |
| 407 | libusb_get_device_descriptor(d, &desc); |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 408 | if (desc.idVendor == 0x4b4 && |
| 409 | desc.idProduct == 0x1235) { |
Stefan Reinauer | 081bb82 | 2015-09-03 16:26:10 -0700 | [diff] [blame] | 410 | if (libusb_open(d, &dev)) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 411 | printf("Couldn't open EM100pro" |
| 412 | " device.\n"); |
Stefan Reinauer | 081bb82 | 2015-09-03 16:26:10 -0700 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | } else { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 416 | printf("USB device on bus %03d:%02d is" |
| 417 | " not an EM100pro.\n", |
| 418 | bus, device); |
Stefan Reinauer | 081bb82 | 2015-09-03 16:26:10 -0700 | [diff] [blame] | 419 | return 0; |
| 420 | } |
| 421 | break; |
| 422 | } |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 423 | if (serial_number) { |
| 424 | struct libusb_device_descriptor desc; |
| 425 | libusb_get_device_descriptor(d, &desc); |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 426 | if (desc.idVendor == 0x4b4 && |
| 427 | desc.idProduct == 0x1235) { |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 428 | if (libusb_open(d, &dev)) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 429 | printf("Couldn't open EM100pro" |
| 430 | " device.\n"); |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 431 | continue; |
| 432 | } |
| 433 | if (!dev) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 434 | printf("Couldn't open EM100pro" |
| 435 | " device.\n"); |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 436 | continue; |
| 437 | } |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 438 | |
Stefan Reinauer | 9fbe20d | 2015-09-09 12:21:00 -0700 | [diff] [blame] | 439 | if (em100_init(em100, ctx, dev) && |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 440 | (serial_number == em100->serialno)) |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 441 | break; |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 442 | |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 443 | libusb_release_interface(dev, 0); |
| 444 | libusb_close(dev); |
| 445 | em100->dev = NULL; |
| 446 | em100->ctx = NULL; |
| 447 | em100->serialno = 0; |
| 448 | dev = NULL; |
| 449 | } |
| 450 | } |
Stefan Reinauer | 081bb82 | 2015-09-03 16:26:10 -0700 | [diff] [blame] | 451 | } |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 452 | |
Stefan Reinauer | 081bb82 | 2015-09-03 16:26:10 -0700 | [diff] [blame] | 453 | libusb_free_device_list(devs, 1); |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Stefan Reinauer | 8b33778 | 2015-12-09 16:17:29 -0800 | [diff] [blame] | 456 | if (!dev) { |
| 457 | if (bus && device) |
| 458 | printf("Could not find EM100pro at %03d:%03d.\n", bus, device); |
| 459 | else if (serial_number) |
Stefan Reinauer | 163dbc1 | 2019-11-21 00:04:58 -0800 | [diff] [blame] | 460 | printf("Could not find EM100pro with serial number EM%06d.\n", |
Stefan Reinauer | 8b33778 | 2015-12-09 16:17:29 -0800 | [diff] [blame] | 461 | serial_number); |
| 462 | else |
| 463 | printf("Could not find EM100pro device.\n"); |
| 464 | |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 465 | return 0; |
| 466 | } |
| 467 | |
Stefan Reinauer | 9fbe20d | 2015-09-09 12:21:00 -0700 | [diff] [blame] | 468 | return em100_init(em100, ctx, dev); |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | static int em100_detach(struct em100 *em100) |
| 472 | { |
| 473 | if (libusb_release_interface(em100->dev, 0) != 0) { |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 474 | printf("Releasing interface failed.\n"); |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 475 | return 1; |
| 476 | } |
| 477 | |
| 478 | libusb_close(em100->dev); |
| 479 | libusb_exit(em100->ctx); |
| 480 | |
| 481 | return 0; |
| 482 | } |
| 483 | |
Stefan Reinauer | 7eda75e | 2015-09-03 16:32:13 -0700 | [diff] [blame] | 484 | static int em100_list(void) |
| 485 | { |
| 486 | struct em100 em100; |
| 487 | libusb_device **devs, *dev; |
| 488 | libusb_context *ctx = NULL; |
| 489 | int i, count = 0; |
| 490 | |
| 491 | if (libusb_init(&ctx) < 0) { |
| 492 | printf("Could not init libusb.\n"); |
| 493 | return 0; |
| 494 | } |
| 495 | |
Stefan Reinauer | 220a099 | 2018-06-09 19:23:58 +0000 | [diff] [blame] | 496 | #if LIBUSB_API_VERSION < 0x01000106 |
Stefan Reinauer | 7eda75e | 2015-09-03 16:32:13 -0700 | [diff] [blame] | 497 | libusb_set_debug(ctx, 3); |
Stefan Reinauer | 220a099 | 2018-06-09 19:23:58 +0000 | [diff] [blame] | 498 | #else |
| 499 | libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, 3); |
| 500 | #endif |
Stefan Reinauer | 7eda75e | 2015-09-03 16:32:13 -0700 | [diff] [blame] | 501 | |
| 502 | if (libusb_get_device_list(ctx, &devs) < 0) { |
| 503 | printf("Could not find USB devices.\n"); |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | for (i = 0; (dev = devs[i]) != NULL; i++) { |
| 508 | struct libusb_device_descriptor desc; |
| 509 | libusb_get_device_descriptor(dev, &desc); |
| 510 | if (desc.idVendor != 0x4b4 || desc.idProduct != 0x1235) |
| 511 | continue; |
| 512 | |
Martin Roth | 0c6a454 | 2015-09-09 12:08:13 -0600 | [diff] [blame] | 513 | if (!em100_attach(&em100, libusb_get_bus_number(dev), |
| 514 | libusb_get_device_address(dev), 0)) { |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 515 | printf("Could not read from EM100 at Bus %03d Device" |
| 516 | " %03d\n", libusb_get_bus_number(dev), |
Martin Roth | 0c6a454 | 2015-09-09 12:08:13 -0600 | [diff] [blame] | 517 | libusb_get_device_address(dev)); |
| 518 | continue; |
| 519 | } |
Stefan Reinauer | bd8e1d0 | 2019-11-28 15:48:51 -0800 | [diff] [blame] | 520 | printf(" Bus %03d Device %03d: EM100pro %s%06d\n", |
Stefan Reinauer | 7eda75e | 2015-09-03 16:32:13 -0700 | [diff] [blame] | 521 | libusb_get_bus_number(dev), |
| 522 | libusb_get_device_address(dev), |
Stefan Reinauer | bd8e1d0 | 2019-11-28 15:48:51 -0800 | [diff] [blame] | 523 | em100.hwversion == HWVERSION_EM100PRO_EARLY ? "DP" : "EM", |
Stefan Reinauer | 7eda75e | 2015-09-03 16:32:13 -0700 | [diff] [blame] | 524 | em100.serialno); |
| 525 | em100_detach(&em100); |
| 526 | count++; |
| 527 | } |
| 528 | if (count == 0) |
| 529 | printf("No EM100pro devices found.\n"); |
| 530 | libusb_exit(ctx); |
| 531 | return 1; |
| 532 | } |
| 533 | |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 534 | static int set_chip_type(struct em100 *em100, const chipdesc *desc) |
| 535 | { |
| 536 | unsigned char cmd[16]; |
| 537 | /* result counts unsuccessful send_cmd()s. |
| 538 | * These are then converted in a boolean success value |
| 539 | */ |
| 540 | int result = 0; |
| 541 | int i; |
Simon Glass | 0008e67 | 2019-01-04 13:59:55 -0700 | [diff] [blame] | 542 | int fpga_voltage, chip_voltage = 0; |
| 543 | int req_voltage = 0; |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 544 | |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 545 | printf("Configuring SPI flash chip emulation.\n"); |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 546 | |
| 547 | memset(cmd, 0, 16); |
| 548 | |
Stefan Reinauer | 53cf234 | 2015-08-28 16:56:40 -0700 | [diff] [blame] | 549 | fpga_voltage = em100->fpga & 0x8000 ? 1800 : 3300; |
| 550 | |
| 551 | for (i = 0; i < desc->init_len; i++) { |
| 552 | if(desc->init[i][0] != 0x11 || desc->init[i][1] != 0x04) |
| 553 | continue; |
| 554 | |
| 555 | chip_voltage = (desc->init[i][2] << 8) | desc->init[i][3]; |
| 556 | |
| 557 | switch (chip_voltage) { |
| 558 | case 1601: /* 1.65V-2V */ |
| 559 | case 1800: |
| 560 | if (fpga_voltage == 3300) |
Simon Glass | 0008e67 | 2019-01-04 13:59:55 -0700 | [diff] [blame] | 561 | req_voltage = 18; |
Stefan Reinauer | 53cf234 | 2015-08-28 16:56:40 -0700 | [diff] [blame] | 562 | break; |
| 563 | case 2500: /* supported by both 1.8V and 3.3V FPGA */ |
| 564 | break; |
| 565 | case 3300: |
| 566 | if (fpga_voltage == 1800) |
Simon Glass | 0008e67 | 2019-01-04 13:59:55 -0700 | [diff] [blame] | 567 | req_voltage = 33; |
Stefan Reinauer | 53cf234 | 2015-08-28 16:56:40 -0700 | [diff] [blame] | 568 | } |
| 569 | break; |
| 570 | } |
| 571 | |
Simon Glass | 0008e67 | 2019-01-04 13:59:55 -0700 | [diff] [blame] | 572 | if (req_voltage) { |
| 573 | if (!set_fpga_voltage(em100, req_voltage)) { |
| 574 | printf("Error: The current FPGA firmware (%.1fV) does " |
| 575 | "not support %s %s (%.1fV)\n", |
| 576 | (float)fpga_voltage / 1000, desc->vendor, |
| 577 | desc->name, (float)chip_voltage / 1000); |
| 578 | return 0; |
| 579 | } |
Stefan Reinauer | 53cf234 | 2015-08-28 16:56:40 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 582 | for (i = 0; i < desc->init_len; i++) { |
| 583 | memcpy(&cmd[0], &desc->init[i][0], BYTES_PER_INIT_ENTRY); |
| 584 | result += !send_cmd(em100->dev, cmd); |
| 585 | } |
| 586 | |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 587 | /* |
| 588 | * Set FPGA registers as the Dediprog software does: |
| 589 | * 0xc4 is set every time the chip type is updated |
| 590 | * 0x10 and 0x81 are set once when the software is initialized. |
| 591 | */ |
| 592 | write_fpga_register(em100, 0xc4, 0x01); |
| 593 | write_fpga_register(em100, 0x10, 0x00); |
| 594 | write_fpga_register(em100, 0x81, 0x00); |
| 595 | |
Stefan Reinauer | 5287511 | 2015-08-20 13:57:20 -0700 | [diff] [blame] | 596 | return !result; |
| 597 | } |
| 598 | |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 599 | /** |
| 600 | * Searches for a specific FPGA register in the chip initialisation |
| 601 | * sequence and returns the value in out. |
| 602 | * |
| 603 | * @reg1: e.g. FPGA write command (0x23) |
| 604 | * @reg2: e.g. FPGA register |
| 605 | * |
| 606 | * Returns 0 on success. |
| 607 | */ |
| 608 | static int get_chip_init_val(const chipdesc *desc, |
| 609 | const uint8_t reg1, |
| 610 | const uint8_t reg2, |
| 611 | uint16_t *out) |
| 612 | { |
| 613 | int i; |
| 614 | |
| 615 | for (i = 0; i < desc->init_len; i++) { |
| 616 | if (desc->init[i][0] == reg1 && desc->init[i][1] == reg2) { |
| 617 | *out = (desc->init[i][2] << 8) | desc->init[i][3]; |
| 618 | return 0; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | return 1; |
| 623 | } |
| 624 | |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 625 | typedef struct { |
| 626 | uint16_t venid; |
| 627 | uint16_t devid; |
| 628 | int found; |
| 629 | chipdesc chip; |
| 630 | } vendev_t; |
| 631 | |
| 632 | static int get_chip_type_entry(char *name __unused, TFILE *dcfg, void *data, int ok __unused) |
| 633 | { |
| 634 | uint16_t comp; |
| 635 | chipdesc chip; |
| 636 | |
| 637 | vendev_t *v = (vendev_t *)data; |
| 638 | |
| 639 | parse_dcfg(&chip, dcfg); |
| 640 | |
| 641 | if (get_chip_init_val(&chip, 0x23, FPGA_REG_DEVID, &comp) || v->devid != comp) |
| 642 | return 0; |
| 643 | if (get_chip_init_val(&chip, 0x23, FPGA_REG_VENDID, &comp) || v->venid != comp) |
| 644 | return 0; |
| 645 | v->found = 1; |
| 646 | v->chip = chip; |
| 647 | return 1; |
| 648 | } |
| 649 | |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 650 | /** |
| 651 | * Tries to identify the currently emulated SPI flash by looking at |
| 652 | * known registers in the FPGA and matches those bits with the |
| 653 | * chip initialisation sequence. |
| 654 | * |
| 655 | * Returns 0 on success. |
| 656 | */ |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 657 | static int get_chip_type(struct em100 *em100, chipdesc *out) |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 658 | { |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 659 | vendev_t v; |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 660 | |
| 661 | /* Read manufacturer and vendor id from FPGA */ |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 662 | if (!read_fpga_register(em100, FPGA_REG_VENDID, &v.venid)) |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 663 | return 1; |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 664 | if (!read_fpga_register(em100, FPGA_REG_DEVID, &v.devid)) |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 665 | return 1; |
| 666 | |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 667 | tar_for_each(configs, get_chip_type_entry, (void *)&v); |
| 668 | if (!v.found) |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 669 | return 1; |
| 670 | |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 671 | *out = v.chip; |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | static int list_chips_entry(char *name __unused, TFILE *file, void *data __unused, int ok __unused) |
| 677 | { |
| 678 | static chipdesc chip; |
| 679 | /* Is the file a dcfg file? Then print the name, otherwise skip. */ |
| 680 | |
| 681 | if (!parse_dcfg(&chip, file)) |
| 682 | printf(" • %s %s\n", chip.vendor, chip.name); |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 683 | |
| 684 | return 0; |
| 685 | } |
| 686 | |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 687 | static chipdesc *setup_chips(const char *desiredchip) |
| 688 | { |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 689 | static chipdesc chip; |
Stefan Reinauer | 32c7e7b | 2019-12-02 10:43:05 -0800 | [diff] [blame] | 690 | char *configs_name = get_em100_file("configs.tar.xz"); |
| 691 | configs = tar_load_compressed(configs_name); |
| 692 | free(configs_name); |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 693 | if (!configs) { |
Stefan Reinauer | d05f960 | 2020-01-08 10:32:56 -0800 | [diff] [blame] | 694 | printf("Can't find chip configs in $EM100_HOME/configs.tar.xz.\n" |
| 695 | "Please run: em100 --update-files.\n"); |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 696 | return NULL; |
| 697 | } |
| 698 | |
| 699 | TFILE *version = tar_find(configs,"configs/VERSION", 1); |
| 700 | if (!version) { |
| 701 | printf("Can't find VERSION of chip configs.\n"); |
| 702 | return NULL; |
| 703 | } |
| 704 | database_version = (char *)version->address; |
| 705 | tar_close(version); |
| 706 | |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 707 | if (desiredchip) { |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 708 | char chipname[256]; |
| 709 | sprintf(chipname, "configs/%s.cfg", desiredchip); |
| 710 | TFILE *dcfg = tar_find(configs, chipname, 0); |
| 711 | if (!dcfg) { |
| 712 | printf("Supported chips:\n\n"); |
| 713 | tar_for_each(configs, list_chips_entry, NULL); |
| 714 | printf("\nCould not find a chip matching '%s' to be emulated.\n", |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 715 | desiredchip); |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 716 | return NULL; |
| 717 | } |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 718 | parse_dcfg(&chip, dcfg); |
| 719 | tar_close(dcfg); |
| 720 | return &chip; |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 721 | } |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 722 | return NULL; |
| 723 | } |
| 724 | |
| 725 | static char *get_em100_home(void) |
| 726 | { |
| 727 | static char directory[FILENAME_BUFFER_SIZE] = "\0"; |
| 728 | |
| 729 | if (directory[0] != 0) |
| 730 | return directory; |
| 731 | |
| 732 | /* find out file */ |
| 733 | wordexp_t p; |
| 734 | char *em100_home = getenv("EM100_HOME"); |
| 735 | if (em100_home) |
| 736 | wordexp("$EM100_HOME/", &p, 0); |
| 737 | else |
| 738 | wordexp("$HOME/.em100/", &p, 0); |
| 739 | |
| 740 | strncpy(directory, p.we_wordv[0], FILENAME_BUFFER_SIZE - 1); |
| 741 | wordfree(&p); |
| 742 | |
| 743 | DIR *dir = opendir(directory); |
| 744 | if (dir) { |
Stefan Reinauer | 7243d62 | 2019-12-04 14:32:02 -0800 | [diff] [blame] | 745 | /* success */ |
| 746 | closedir(dir); |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 747 | } else if (errno == ENOENT) { |
Stefan Reinauer | 62f2aa6 | 2019-12-02 11:22:26 -0800 | [diff] [blame] | 748 | if (mkdir(directory, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)) { |
| 749 | perror(directory); |
| 750 | directory[0]=0; |
| 751 | return NULL; |
| 752 | } |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 753 | } else { |
| 754 | perror("EM100_HOME inaccessible"); |
| 755 | directory[0]=0; |
| 756 | return NULL; |
| 757 | } |
| 758 | |
| 759 | return directory; |
| 760 | } |
| 761 | |
Stefan Reinauer | 7953388 | 2019-11-22 00:57:29 -0800 | [diff] [blame] | 762 | char *get_em100_file(const char *name) |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 763 | { |
Stefan Reinauer | a115fc7 | 2019-12-02 10:58:42 -0800 | [diff] [blame] | 764 | char file[FILENAME_BUFFER_SIZE + 1]; |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 765 | strncpy(file, get_em100_home(), FILENAME_BUFFER_SIZE); |
| 766 | strncat(file, name, FILENAME_BUFFER_SIZE - strlen(file) - 1); |
| 767 | return strdup(file); |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 770 | static const struct option longopts[] = { |
| 771 | {"set", 1, 0, 'c'}, |
| 772 | {"download", 1, 0, 'd'}, |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 773 | {"start-address", 1, 0, 'a'}, |
Duncan Laurie | f436156 | 2020-04-15 12:54:51 -0700 | [diff] [blame] | 774 | {"address-mode", 1, 0, 'm'}, |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 775 | {"start", 0, 0, 'r'}, |
| 776 | {"stop", 0, 0, 's'}, |
| 777 | {"verify", 0, 0, 'v'}, |
Aaron Durbin | 637fe48 | 2012-10-17 17:39:19 -0500 | [diff] [blame] | 778 | {"holdpin", 1, 0, 'p'}, |
Stefan Reinauer | 14d3512 | 2015-09-03 16:02:55 -0700 | [diff] [blame] | 779 | {"debug", 0, 0, 'D'}, |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 780 | {"help", 0, 0, 'h'}, |
Stefan Reinauer | a5b46d4 | 2015-09-03 16:14:25 -0700 | [diff] [blame] | 781 | {"trace", 0, 0, 't'}, |
Martin Roth | 712262a | 2015-09-18 14:01:18 -0600 | [diff] [blame] | 782 | {"offset", 1, 0, 'O'}, |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 783 | {"set-serialno", 1, 0, 'S'}, |
Stefan Reinauer | a2b67d3 | 2015-09-01 16:30:43 -0700 | [diff] [blame] | 784 | {"firmware-update", 1, 0, 'F'}, |
| 785 | {"firmware-dump", 1, 0, 'f'}, |
Stefan Reinauer | a9278a9 | 2015-09-01 16:16:27 -0700 | [diff] [blame] | 786 | {"firmware-write", 1, 0, 'g'}, |
Stefan Reinauer | 7eda75e | 2015-09-03 16:32:13 -0700 | [diff] [blame] | 787 | {"device", 1, 0, 'x'}, |
| 788 | {"list-devices", 0, 0, 'l'}, |
Stefan Reinauer | dfd556c | 2019-11-20 17:57:40 -0800 | [diff] [blame] | 789 | {"update-files", 0, 0, 'U'}, |
Stefan Reinauer | 3b92108 | 2019-11-26 16:56:26 -0800 | [diff] [blame] | 790 | {"terminal", 0, 0, 'T'}, |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 791 | {"traceconsole", 0, 0, 'R'}, |
| 792 | {"length", 1, 0, 'L'}, |
Stefan Reinauer | 146eeba | 2020-12-17 19:26:02 -0800 | [diff] [blame] | 793 | {"brief", 0, 0, 'b'}, |
Stefan Reinauer | 3b92108 | 2019-11-26 16:56:26 -0800 | [diff] [blame] | 794 | {"compatible", 0, 0, 'C'}, |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 795 | {NULL, 0, 0, 0} |
| 796 | }; |
| 797 | |
Stefan Reinauer | a5b46d4 | 2015-09-03 16:14:25 -0700 | [diff] [blame] | 798 | static void usage(char *name) |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 799 | { |
Stefan Reinauer | a5b46d4 | 2015-09-03 16:14:25 -0700 | [diff] [blame] | 800 | printf("em100: EM100pro command line utility\n\nExample:\n" |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 801 | " %s --stop --set M25P80 -d file.bin -v --start -t -O 0xfff00000\n" |
Stefan Reinauer | a5b46d4 | 2015-09-03 16:14:25 -0700 | [diff] [blame] | 802 | "\nUsage:\n" |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 803 | " -c|--set CHIP: select chip emulation\n" |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 804 | " -d|--download FILE: download FILE into EM100pro\n" |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 805 | " -a|--start address: only works with -d (E.g. -d file.bin -a 0x300000)\n" |
Duncan Laurie | f436156 | 2020-04-15 12:54:51 -0700 | [diff] [blame] | 806 | " -m|--address-mode MODE: force 3 or 4 byte address mode\n" |
Patrick Rudolph | 9a56e46 | 2019-10-15 09:05:45 +0200 | [diff] [blame] | 807 | " -u|--upload FILE: upload from EM100pro into FILE\n" |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 808 | " -r|--start: em100 shall run\n" |
| 809 | " -s|--stop: em100 shall stop\n" |
| 810 | " -v|--verify: verify EM100 content matches the file\n" |
| 811 | " -t|--trace: trace mode\n" |
| 812 | " -O|--offset HEX_VAL: address offset for trace mode\n" |
| 813 | " -T|--terminal: terminal mode\n" |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 814 | " -R|--traceconsole: trace console mode\n" |
| 815 | " -L|--length HEX_VAL: length of buffer for traceconsole mode\n" |
Stefan Reinauer | 146eeba | 2020-12-17 19:26:02 -0800 | [diff] [blame] | 816 | " -b|--brief: brief mode for traces\n" |
Stefan Reinauer | a27fa81 | 2019-11-24 21:58:02 -0800 | [diff] [blame] | 817 | " -F|--firmware-update FILE|auto: update EM100pro firmware (dangerous)\n" |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 818 | " -f|--firmware-dump FILE: export raw EM100pro firmware to file\n" |
| 819 | " -g|--firmware-write FILE: export EM100pro firmware to DPFW file\n" |
| 820 | " -S|--set-serialno NUM: set serial number to NUM\n" |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 821 | " -V|--set-voltage [1.8|3.3] switch FPGA voltage\n" |
Stefan Reinauer | a5b46d4 | 2015-09-03 16:14:25 -0700 | [diff] [blame] | 822 | " -p|--holdpin [LOW|FLOAT|INPUT]: set the hold pin state\n" |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 823 | " -x|--device BUS:DEV use EM100pro on USB bus/device\n" |
Stefan Reinauer | 163dbc1 | 2019-11-21 00:04:58 -0800 | [diff] [blame] | 824 | " -x|--device EMxxxxxx use EM100pro with serial no EMxxxxxx\n" |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 825 | " -l|--list-devices list all connected EM100pro devices\n" |
Stefan Reinauer | dfd556c | 2019-11-20 17:57:40 -0800 | [diff] [blame] | 826 | " -U|--update-files update device (chip) and firmware database\n" |
Stefan Reinauer | 3b92108 | 2019-11-26 16:56:26 -0800 | [diff] [blame] | 827 | " -C|--compatible enable compatibility mode (patch image for EM100Pro)\n" |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 828 | " -D|--debug: print debug information.\n" |
| 829 | " -h|--help: this help text\n\n", |
| 830 | name); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 833 | int main(int argc, char **argv) |
| 834 | { |
| 835 | int opt, idx; |
| 836 | const char *desiredchip = NULL; |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 837 | const char *serialno = NULL; |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 838 | const char *filename = NULL, *read_filename = NULL; |
Stefan Reinauer | a2b67d3 | 2015-09-01 16:30:43 -0700 | [diff] [blame] | 839 | const char *firmware_in = NULL, *firmware_out = NULL; |
Aaron Durbin | 637fe48 | 2012-10-17 17:39:19 -0500 | [diff] [blame] | 840 | const char *holdpin = NULL; |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 841 | int do_start = 0, do_stop = 0; |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 842 | int verify = 0, trace = 0, terminal=0, traceconsole=0; |
Stefan Reinauer | b20610e | 2019-12-06 11:14:33 -0800 | [diff] [blame] | 843 | int compatibility = 0; |
Stefan Reinauer | 081bb82 | 2015-09-03 16:26:10 -0700 | [diff] [blame] | 844 | int bus = 0, device = 0; |
Stefan Reinauer | a9278a9 | 2015-09-01 16:16:27 -0700 | [diff] [blame] | 845 | int firmware_is_dpfw = 0; |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 846 | unsigned int serial_number = 0; |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 847 | unsigned long address_offset = 0, address_length = 0; |
Stefan Reinauer | 7953388 | 2019-11-22 00:57:29 -0800 | [diff] [blame] | 848 | unsigned int spi_start_address = 0; |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 849 | const char *voltage = NULL; |
Stefan Reinauer | 7ea18c0 | 2020-09-18 20:53:11 -0700 | [diff] [blame] | 850 | struct sigaction signal_action; |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 851 | struct em100 *em100 = &em100_state; |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 852 | |
Stefan Reinauer | 146eeba | 2020-12-17 19:26:02 -0800 | [diff] [blame] | 853 | while ((opt = getopt_long(argc, argv, "c:d:a:m:u:rsvtO:F:f:g:S:V:p:DCx:lUhTRL:b", |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 854 | longopts, &idx)) != -1) { |
| 855 | switch (opt) { |
| 856 | case 'c': |
| 857 | desiredchip = optarg; |
| 858 | break; |
| 859 | case 'd': |
Stefan Reinauer | 7a22c45 | 2020-12-17 19:21:04 -0800 | [diff] [blame] | 860 | if(access(optarg, F_OK) == 0) |
| 861 | filename = optarg; |
| 862 | else |
| 863 | printf("Can't open '%s'.\n", optarg); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 864 | break; |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 865 | case 'a': |
Stefan Reinauer | 7953388 | 2019-11-22 00:57:29 -0800 | [diff] [blame] | 866 | sscanf(optarg, "%x", &spi_start_address); |
| 867 | printf("SPI address: 0x%08x\n", spi_start_address); |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 868 | break; |
Duncan Laurie | f436156 | 2020-04-15 12:54:51 -0700 | [diff] [blame] | 869 | case 'm': |
| 870 | sscanf(optarg, "%d", &address_mode); |
| 871 | break; |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 872 | case 'u': |
| 873 | read_filename = optarg; |
| 874 | break; |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 875 | case 'V': |
| 876 | voltage = optarg; |
| 877 | break; |
Aaron Durbin | 637fe48 | 2012-10-17 17:39:19 -0500 | [diff] [blame] | 878 | case 'p': |
| 879 | holdpin = optarg; |
| 880 | break; |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 881 | case 'r': |
| 882 | do_start = 1; |
| 883 | break; |
| 884 | case 's': |
Aaron Durbin | f558702 | 2012-10-16 15:37:12 -0500 | [diff] [blame] | 885 | do_stop = 1; |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 886 | break; |
| 887 | case 'v': |
| 888 | verify = 1; |
| 889 | break; |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 890 | case 't': |
| 891 | trace = 1; |
| 892 | break; |
Martin Roth | 712262a | 2015-09-18 14:01:18 -0600 | [diff] [blame] | 893 | case 'O': |
| 894 | sscanf(optarg, "%lx", &address_offset); |
| 895 | printf("Address offset: 0x%08lx\n", address_offset); |
| 896 | break; |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 897 | case 'T': |
| 898 | terminal = 1; |
| 899 | break; |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 900 | case 'S': |
| 901 | serialno = optarg; |
Martin Roth | e683519 | 2015-09-09 12:15:05 -0600 | [diff] [blame] | 902 | break; |
Stefan Reinauer | 14d3512 | 2015-09-03 16:02:55 -0700 | [diff] [blame] | 903 | case 'D': |
| 904 | debug=1; |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 905 | break; |
Stefan Reinauer | a2b67d3 | 2015-09-01 16:30:43 -0700 | [diff] [blame] | 906 | case 'F': |
| 907 | firmware_in = optarg; |
| 908 | break; |
| 909 | case 'f': |
| 910 | firmware_out = optarg; |
| 911 | break; |
Stefan Reinauer | a9278a9 | 2015-09-01 16:16:27 -0700 | [diff] [blame] | 912 | case 'g': |
| 913 | firmware_out = optarg; |
| 914 | firmware_is_dpfw = 1; |
| 915 | break; |
Stefan Reinauer | 7eda75e | 2015-09-03 16:32:13 -0700 | [diff] [blame] | 916 | case 'x': |
Stefan Reinauer | 8db88b9 | 2020-12-17 19:22:13 -0800 | [diff] [blame] | 917 | if ((toupper(optarg[0]) == 'D' && toupper(optarg[1]) == 'P') || |
| 918 | (toupper(optarg[0]) == 'E' && toupper(optarg[1]) == 'M')) |
Stefan Reinauer | c601b34 | 2015-09-08 12:30:03 -0700 | [diff] [blame] | 919 | sscanf(optarg + 2, "%d", &serial_number); |
| 920 | else |
| 921 | sscanf(optarg, "%d:%d", &bus, &device); |
Stefan Reinauer | 7eda75e | 2015-09-03 16:32:13 -0700 | [diff] [blame] | 922 | break; |
| 923 | case 'l': |
| 924 | em100_list(); |
| 925 | return 0; |
Stefan Reinauer | dfd556c | 2019-11-20 17:57:40 -0800 | [diff] [blame] | 926 | case 'U': |
| 927 | update_all_files(); |
| 928 | return 0; |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 929 | case 'R': |
| 930 | traceconsole = 1; |
| 931 | break; |
| 932 | case 'L': |
| 933 | sscanf(optarg, "%lx", &address_length); |
| 934 | break; |
Stefan Reinauer | 146eeba | 2020-12-17 19:26:02 -0800 | [diff] [blame] | 935 | case 'b': |
| 936 | trace_brief = 1; |
| 937 | break; |
Stefan Reinauer | 3b92108 | 2019-11-26 16:56:26 -0800 | [diff] [blame] | 938 | case 'C': |
| 939 | compatibility = 1; |
| 940 | break; |
Stefan Reinauer | a5b46d4 | 2015-09-03 16:14:25 -0700 | [diff] [blame] | 941 | default: |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 942 | case 'h': |
Stefan Reinauer | a5b46d4 | 2015-09-03 16:14:25 -0700 | [diff] [blame] | 943 | usage(argv[0]); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 944 | return 0; |
| 945 | } |
| 946 | } |
| 947 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 948 | if (!em100_attach(em100, bus, device, serial_number)) { |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 949 | return 1; |
| 950 | } |
| 951 | |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 952 | const chipdesc *chip = setup_chips(desiredchip); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 953 | if (desiredchip && !chip) { |
| 954 | em100_detach(em100); |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 955 | return 1; |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 956 | } |
Stefan Reinauer | e67e892 | 2019-11-21 15:30:46 -0800 | [diff] [blame] | 957 | |
Stefan Reinauer | 7ea18c0 | 2020-09-18 20:53:11 -0700 | [diff] [blame] | 958 | /* Set up signal handler. This is used for two reasons: |
| 959 | * 1) to create a way to cleanly exit trace mode. |
| 960 | * 2) to make sure that the em100 is not left in an improper state |
Stefan Reinauer | 2634d3b | 2020-11-18 21:33:20 -0800 | [diff] [blame] | 961 | * when receiving SIGINT/SIGTERM for other reasons during operation. |
| 962 | * In this second case, we just ignore the first signal until em100 |
| 963 | * naturally terminates or receives a second signal. This is OK |
| 964 | * because the utility is short-running in nature. |
Stefan Reinauer | 7ea18c0 | 2020-09-18 20:53:11 -0700 | [diff] [blame] | 965 | */ |
| 966 | signal_action.sa_handler = exit_handler; |
| 967 | signal_action.sa_flags = 0; |
| 968 | sigemptyset(&signal_action.sa_mask); |
| 969 | sigaction(SIGINT, &signal_action, NULL); |
Stefan Reinauer | 2634d3b | 2020-11-18 21:33:20 -0800 | [diff] [blame] | 970 | sigaction(SIGTERM, &signal_action, NULL); |
Stefan Reinauer | ae88135 | 2021-03-08 10:57:38 -0800 | [diff] [blame] | 971 | sigaction(SIGHUP, &signal_action, NULL); |
Stefan Reinauer | 937401e | 2019-11-24 13:55:24 -0800 | [diff] [blame] | 972 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 973 | if (em100->hwversion == HWVERSION_EM100PRO || em100->hwversion == HWVERSION_EM100PRO_EARLY) { |
| 974 | printf("MCU version: %d.%02d\n", em100->mcu >> 8, em100->mcu & 0xff); |
Stefan Reinauer | 14deb03 | 2015-08-28 16:33:00 -0700 | [diff] [blame] | 975 | /* While the Dediprog software for Windows will refuse to work |
| 976 | * with 1.8V chips on older FPGA versions, it does not |
| 977 | * specifically output a voltage when reporting the FPGA |
| 978 | * version. We emulate this behavior here. Version 0.51 is |
| 979 | * known to behave the old way, 0.75 is behaving the new |
| 980 | * way. |
| 981 | */ |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 982 | if (em100->fpga > 0x0033) /* 0.51 */ |
Stefan Reinauer | 937401e | 2019-11-24 13:55:24 -0800 | [diff] [blame] | 983 | printf("FPGA version: %d.%02d (%s)\n", |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 984 | em100->fpga >> 8 & 0x7f, em100->fpga & 0xff, |
| 985 | em100->fpga & 0x8000 ? "1.8V" : "3.3V"); |
Stefan Reinauer | 937401e | 2019-11-24 13:55:24 -0800 | [diff] [blame] | 986 | else |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 987 | printf("FPGA version: %d.%02d\n", em100->fpga >> 8, |
| 988 | em100->fpga & 0xff); |
Stefan Reinauer | 937401e | 2019-11-24 13:55:24 -0800 | [diff] [blame] | 989 | } else {/* EM100Pro-G2 */ |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 990 | printf("MCU version: %d.%d\n", em100->mcu >> 8, em100->mcu & 0xff); |
Stefan Reinauer | 937401e | 2019-11-24 13:55:24 -0800 | [diff] [blame] | 991 | printf("FPGA version: %d.%03d\n", |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 992 | em100->fpga >> 8 & 0x7f, em100->fpga & 0xff); |
Stefan Reinauer | 14deb03 | 2015-08-28 16:33:00 -0700 | [diff] [blame] | 993 | } |
Stefan Reinauer | 937401e | 2019-11-24 13:55:24 -0800 | [diff] [blame] | 994 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 995 | printf("Hardware version: %u\n", em100->hwversion); |
Stefan Reinauer | 14deb03 | 2015-08-28 16:33:00 -0700 | [diff] [blame] | 996 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 997 | if (em100->serialno != 0xffffffff) |
Stefan Reinauer | bd8e1d0 | 2019-11-28 15:48:51 -0800 | [diff] [blame] | 998 | printf("Serial number: %s%06d\n", |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 999 | em100->hwversion == HWVERSION_EM100PRO_EARLY ? "DP" : "EM", em100->serialno); |
Stefan Reinauer | 14deb03 | 2015-08-28 16:33:00 -0700 | [diff] [blame] | 1000 | else |
| 1001 | printf("Serial number: N.A.\n"); |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 1002 | printf("SPI flash database: %s\n", database_version); |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1003 | get_current_state(em100); |
| 1004 | get_current_pin_state(em100); |
Martin Roth | da63eb7 | 2015-09-11 12:16:23 -0600 | [diff] [blame] | 1005 | printf("\n"); |
| 1006 | |
| 1007 | if (debug) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1008 | em100_debug(em100); |
Stefan Reinauer | 14d3512 | 2015-09-03 16:02:55 -0700 | [diff] [blame] | 1009 | } |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1010 | |
Stefan Reinauer | a2b67d3 | 2015-09-01 16:30:43 -0700 | [diff] [blame] | 1011 | if (firmware_in) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1012 | firmware_update(em100, firmware_in, verify); |
| 1013 | return em100_detach(em100); |
Stefan Reinauer | a2b67d3 | 2015-09-01 16:30:43 -0700 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | if (firmware_out) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1017 | firmware_dump(em100, firmware_out, firmware_is_dpfw); |
| 1018 | return em100_detach(em100); |
Stefan Reinauer | a2b67d3 | 2015-09-01 16:30:43 -0700 | [diff] [blame] | 1019 | } |
| 1020 | |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 1021 | if (serialno) { |
Martin Roth | 12e5886 | 2015-09-09 12:23:31 -0600 | [diff] [blame] | 1022 | int offset = 0; |
| 1023 | /* if the user specified a serial containing DP, skip that */ |
| 1024 | if ((serialno[0] == 'D' || serialno[0] == 'd') && |
| 1025 | (serialno[1] == 'P' || serialno[1] == 'p')) |
| 1026 | offset = 2; |
Stefan Reinauer | 163dbc1 | 2019-11-21 00:04:58 -0800 | [diff] [blame] | 1027 | /* if the user specified a serial containing EM, skip that */ |
| 1028 | if ((serialno[0] == 'E' || serialno[0] == 'e') && |
| 1029 | (serialno[1] == 'M' || serialno[1] == 'm')) |
| 1030 | offset = 2; |
Martin Roth | 12e5886 | 2015-09-09 12:23:31 -0600 | [diff] [blame] | 1031 | |
| 1032 | if (sscanf(serialno + offset, "%d", &serial_number) != 1) |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 1033 | printf("Error: Can't parse serial number '%s'\n", |
| 1034 | serialno); |
| 1035 | else |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1036 | set_serialno(em100, serial_number); |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 1037 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1038 | return em100_detach(em100); |
Stefan Reinauer | e636d2a | 2015-08-28 16:58:57 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1041 | if (do_stop) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1042 | set_state(em100, 0); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | if (desiredchip) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1046 | if (!set_chip_type(em100, chip)) { |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1047 | printf("Failed configuring chip type.\n"); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1048 | em100_detach(em100); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1049 | return 0; |
| 1050 | } |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 1051 | printf("Chip set to %s %s.\n", chip->vendor, chip->name); |
Duncan Laurie | f436156 | 2020-04-15 12:54:51 -0700 | [diff] [blame] | 1052 | |
| 1053 | /* Automatically enable 4 byte address mode for chips >16MB |
| 1054 | * unless specified by the user on the command line. |
| 1055 | */ |
| 1056 | if (!address_mode && chip->size > (16 * 1024 * 1024)) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1057 | set_address_mode(em100, 4); |
Duncan Laurie | f436156 | 2020-04-15 12:54:51 -0700 | [diff] [blame] | 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | if (address_mode) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1062 | if (set_address_mode(em100, address_mode) < 0) { |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1063 | em100_detach(em100); |
Duncan Laurie | f436156 | 2020-04-15 12:54:51 -0700 | [diff] [blame] | 1064 | return 1; |
| 1065 | } |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1066 | } |
| 1067 | |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 1068 | if (voltage) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1069 | if (!set_fpga_voltage_from_str(em100, voltage)) { |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 1070 | printf("Failed configuring FPGA voltage.\n"); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1071 | em100_detach(em100); |
Simon Glass | 98cd62c | 2018-12-26 14:31:20 -0700 | [diff] [blame] | 1072 | return 1; |
| 1073 | } |
| 1074 | } |
| 1075 | |
Aaron Durbin | 637fe48 | 2012-10-17 17:39:19 -0500 | [diff] [blame] | 1076 | if (holdpin) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1077 | if (!set_hold_pin_state_from_str(em100, holdpin)) { |
Aaron Durbin | 637fe48 | 2012-10-17 17:39:19 -0500 | [diff] [blame] | 1078 | printf("Failed configuring hold pin state.\n"); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1079 | em100_detach(em100); |
Aaron Durbin | 637fe48 | 2012-10-17 17:39:19 -0500 | [diff] [blame] | 1080 | return 0; |
| 1081 | } |
| 1082 | } |
| 1083 | |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 1084 | if (read_filename) { |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 1085 | int maxlen = 0x4000000; /* largest size - 64MB */ |
| 1086 | |
| 1087 | if (!desiredchip) { |
| 1088 | /* Read configured SPI emulation from EM100 */ |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 1089 | chipdesc emulated_chip; |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 1090 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1091 | if (!get_chip_type(em100, &emulated_chip)) { |
Stefan Reinauer | db126f4 | 2019-11-18 18:25:45 -0800 | [diff] [blame] | 1092 | printf("Configured to emulate %dkB chip\n", emulated_chip.size / 1024); |
| 1093 | maxlen = emulated_chip.size; |
Patrick Rudolph | 8b557a9 | 2019-10-10 10:15:14 +0200 | [diff] [blame] | 1094 | } |
| 1095 | } else { |
| 1096 | maxlen = chip->size; |
| 1097 | } |
| 1098 | |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 1099 | void *data = malloc(maxlen); |
| 1100 | if (data == NULL) { |
| 1101 | printf("FATAL: couldn't allocate memory\n"); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1102 | em100_detach(em100); |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 1103 | return 1; |
| 1104 | } |
| 1105 | FILE *fdata = fopen(read_filename, "wb"); |
| 1106 | if (!fdata) { |
| 1107 | perror("Could not open download file"); |
| 1108 | free(data); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1109 | em100_detach(em100); |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 1110 | return 1; |
| 1111 | } |
| 1112 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1113 | read_sdram(em100, data, 0x00000000, maxlen); |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 1114 | |
| 1115 | int length = fwrite(data, maxlen, 1, fdata); |
| 1116 | fclose(fdata); |
| 1117 | free(data); |
| 1118 | |
| 1119 | if (length != 1) { |
| 1120 | printf("FATAL: failed to write"); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1121 | em100_detach(em100); |
Duncan Laurie | 578f068 | 2016-12-06 11:38:28 -0800 | [diff] [blame] | 1122 | return 1; |
| 1123 | } |
| 1124 | } |
| 1125 | |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1126 | if (filename) { |
Stefan Reinauer | 7953388 | 2019-11-22 00:57:29 -0800 | [diff] [blame] | 1127 | unsigned int maxlen = desiredchip ? chip->size : 0x4000000; /* largest size - 64MB */ |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1128 | void *data = malloc(maxlen); |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1129 | int done; |
| 1130 | void *readback = NULL; |
| 1131 | |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1132 | if (data == NULL) { |
| 1133 | printf("FATAL: couldn't allocate memory\n"); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1134 | em100_detach(em100); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1135 | return 1; |
| 1136 | } |
| 1137 | FILE *fdata = fopen(filename, "rb"); |
| 1138 | if (!fdata) { |
| 1139 | perror("Could not open upload file"); |
Stefan Reinauer | 0bced2e | 2015-09-08 12:58:56 -0700 | [diff] [blame] | 1140 | free(data); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1141 | em100_detach(em100); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1142 | return 1; |
| 1143 | } |
| 1144 | |
Stefan Reinauer | 7953388 | 2019-11-22 00:57:29 -0800 | [diff] [blame] | 1145 | unsigned int length = 0; |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1146 | while ((!feof(fdata)) && (length < maxlen)) { |
| 1147 | int blocksize = 65536; |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 1148 | length += blocksize * fread(data+length, blocksize, 1, |
| 1149 | fdata); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1150 | } |
| 1151 | fclose(fdata); |
| 1152 | |
| 1153 | if (length > maxlen) { |
| 1154 | printf("FATAL: length > maxlen\n"); |
Stefan Reinauer | 0bced2e | 2015-09-08 12:58:56 -0700 | [diff] [blame] | 1155 | free(data); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1156 | em100_detach(em100); |
Stefan Reinauer | 0bced2e | 2015-09-08 12:58:56 -0700 | [diff] [blame] | 1157 | return 1; |
| 1158 | } |
| 1159 | |
| 1160 | if (length == 0) { |
| 1161 | printf("FATAL: No file to upload.\n"); |
| 1162 | free(data); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1163 | em100_detach(em100); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1164 | return 1; |
| 1165 | } |
| 1166 | |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1167 | if (desiredchip && (length != (chip->size - spi_start_address)) ) |
| 1168 | { |
| 1169 | printf("FATAL: file size does not match to chip size.\n"); |
| 1170 | free(data); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1171 | em100_detach(em100); |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1172 | return 1; |
| 1173 | } |
| 1174 | |
Stefan Reinauer | 3b92108 | 2019-11-26 16:56:26 -0800 | [diff] [blame] | 1175 | if (compatibility) |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1176 | autocorrect_image(em100, data, length); |
Stefan Reinauer | 3b92108 | 2019-11-26 16:56:26 -0800 | [diff] [blame] | 1177 | |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1178 | if (spi_start_address) { |
| 1179 | readback = malloc(maxlen); |
Jacob Garber | ca833ce | 2019-08-16 15:04:50 -0600 | [diff] [blame] | 1180 | if (readback == NULL) { |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1181 | printf("FATAL: couldn't allocate memory(size: %x)\n", maxlen); |
Jacob Garber | ca833ce | 2019-08-16 15:04:50 -0600 | [diff] [blame] | 1182 | free(data); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1183 | em100_detach(em100); |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1184 | return 1; |
| 1185 | } |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1186 | done = read_sdram(em100, readback, 0, maxlen); |
Jacob Garber | b4a35cd | 2019-08-16 15:10:51 -0600 | [diff] [blame] | 1187 | if (done) { |
| 1188 | memcpy((unsigned char*)readback + spi_start_address, data, length); |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1189 | write_sdram(em100, (unsigned char*)readback, 0x00000000, maxlen); |
Jacob Garber | b4a35cd | 2019-08-16 15:10:51 -0600 | [diff] [blame] | 1190 | } else { |
| 1191 | printf("Error: sdram readback failed\n"); |
| 1192 | } |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1193 | free(readback); |
| 1194 | } else { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1195 | write_sdram(em100, (unsigned char*)data, 0x00000000, length); |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1198 | if (verify) { |
Tsung Ho Wu | 9fda674 | 2019-01-15 10:56:56 -0800 | [diff] [blame] | 1199 | readback = malloc(length); |
Jacob Garber | ca833ce | 2019-08-16 15:04:50 -0600 | [diff] [blame] | 1200 | if (readback == NULL) { |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1201 | printf("FATAL: couldn't allocate memory\n"); |
Jacob Garber | ca833ce | 2019-08-16 15:04:50 -0600 | [diff] [blame] | 1202 | free(data); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1203 | em100_detach(em100); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1204 | return 1; |
| 1205 | } |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1206 | done = read_sdram(em100, readback, spi_start_address, length); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1207 | if (done && (memcmp(data, readback, length) == 0)) |
| 1208 | printf("Verify: PASS\n"); |
Ryan O'Leary | 5c72c8d | 2020-08-27 15:14:16 -0700 | [diff] [blame] | 1209 | else { |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1210 | printf("Verify: FAIL\n"); |
Ryan O'Leary | 5c72c8d | 2020-08-27 15:14:16 -0700 | [diff] [blame] | 1211 | free(readback); |
| 1212 | free(data); |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1213 | em100_detach(em100); |
Ryan O'Leary | 5c72c8d | 2020-08-27 15:14:16 -0700 | [diff] [blame] | 1214 | return 1; |
| 1215 | } |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1216 | free(readback); |
| 1217 | } |
| 1218 | |
| 1219 | free(data); |
| 1220 | } |
| 1221 | |
| 1222 | if (do_start) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1223 | set_state(em100, 1); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 1226 | if (trace || terminal || traceconsole) { |
Stefan Reinauer | afe0325 | 2020-11-21 16:03:59 -0800 | [diff] [blame] | 1227 | int usb_errors = 0; |
| 1228 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1229 | if ((holdpin == NULL) && (!set_hold_pin_state(em100, 3))) { |
Stefan Reinauer | cf24ed7 | 2015-08-24 12:21:45 -0700 | [diff] [blame] | 1230 | printf("Error: Failed to set EM100 to input\n"); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1231 | em100_detach(em100); |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 1232 | return 1; |
| 1233 | } |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 1234 | |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 1235 | if (!do_start && !do_stop) |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1236 | set_state(em100, 1); |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 1237 | |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 1238 | printf ("Starting "); |
| 1239 | |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 1240 | if (trace || traceconsole) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1241 | reset_spi_trace(em100); |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 1242 | printf("trace%s", terminal ? " & " : ""); |
| 1243 | } |
| 1244 | |
| 1245 | if (terminal) { |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1246 | init_spi_terminal(em100); |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 1247 | printf("terminal"); |
| 1248 | } |
| 1249 | |
Stefan Reinauer | 3d08fa9 | 2020-12-22 17:17:29 -0800 | [diff] [blame] | 1250 | printf(". Press CTRL-C to exit.\n\n"); |
| 1251 | fflush(stdout); |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 1252 | |
Stefan Reinauer | ed59fc8 | 2020-11-21 16:11:40 -0800 | [diff] [blame] | 1253 | while (!exit_requested && usb_errors < MAX_USB_ERRORS) { |
Stefan Reinauer | db54382 | 2020-12-11 16:29:44 -0800 | [diff] [blame] | 1254 | int ret; |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 1255 | |
| 1256 | if (traceconsole) |
| 1257 | ret = read_spi_trace_console(em100, address_offset, address_length); |
| 1258 | else if (trace) |
Stefan Reinauer | db54382 | 2020-12-11 16:29:44 -0800 | [diff] [blame] | 1259 | ret = read_spi_trace(em100, terminal, address_offset); |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 1260 | else if (terminal) |
Stefan Reinauer | db54382 | 2020-12-11 16:29:44 -0800 | [diff] [blame] | 1261 | ret = read_spi_terminal(em100, 0); |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 1262 | |
Stefan Reinauer | db54382 | 2020-12-11 16:29:44 -0800 | [diff] [blame] | 1263 | if (ret == 0) |
| 1264 | usb_errors++; |
Stefan Reinauer | cafd151 | 2020-12-11 17:36:28 -0800 | [diff] [blame] | 1265 | if (ret < 0) |
| 1266 | break; |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 1267 | } |
Stefan Reinauer | afe0325 | 2020-11-21 16:03:59 -0800 | [diff] [blame] | 1268 | if (usb_errors >= MAX_USB_ERRORS) |
| 1269 | printf("Error: Bailed out with too many USB errors.\n"); |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 1270 | |
Martin Roth | b54d6ba | 2015-09-29 14:49:37 -0600 | [diff] [blame] | 1271 | if (!do_start && !do_stop) |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1272 | set_state(em100, 0); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1273 | |
Martin Roth | dfdff3e | 2015-09-18 09:42:03 -0600 | [diff] [blame] | 1274 | if (trace) |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1275 | reset_spi_trace(em100); |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 1276 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1277 | if ((holdpin == NULL) && (!set_hold_pin_state(em100, 2))) { |
Stefan Reinauer | cf24ed7 | 2015-08-24 12:21:45 -0700 | [diff] [blame] | 1278 | printf("Error: Failed to set EM100 to float\n"); |
Stefan Reinauer | 003ac42 | 2020-11-21 20:28:17 -0800 | [diff] [blame] | 1279 | em100_detach(em100); |
Patrick Georgi | f3558c6 | 2014-10-28 18:16:57 +0100 | [diff] [blame] | 1280 | return 1; |
| 1281 | } |
| 1282 | } |
| 1283 | |
Stefan Reinauer | 7136747 | 2020-11-18 21:12:20 -0800 | [diff] [blame] | 1284 | return em100_detach(em100); |
Stefan Reinauer | 0ce691f | 2012-08-03 14:20:24 -0700 | [diff] [blame] | 1285 | } |