+static void cpy_ip(uint8_t *dst, uint8_t *src)
+{
+ int i, j;
+
+ for (i = 0, j = 3; i < 4; i++, j--)
+ dst[i] = src[j];
+}
+
+COMMAND_HANDLER(jlink_handle_config_ip_address_command)
+{
+ uint8_t ip_address[4];
+ uint32_t subnet_mask = 0;
+ int i, len;
+ uint8_t subnet_bits = 24;
+
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
+ command_print(CMD_CTX, "Reading configuration is not supported by the "
+ "device.");
+ return ERROR_OK;
+ }
+
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_ETHERNET)) {
+ command_print(CMD_CTX, "Ethernet connectivity is not supported by the "
+ "device.");
+ return ERROR_OK;
+ }
+
+ if (!CMD_ARGC) {
+ show_config_ip_address(CMD_CTX);
+ } else {
+ if (!string_to_ip(CMD_ARGV[0], ip_address, &i))
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ len = strlen(CMD_ARGV[0]);
+
+ /* Check for format A.B.C.D/E. */
+ if (i < len) {
+ if (CMD_ARGV[0][i] != '/')
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ COMMAND_PARSE_NUMBER(u8, CMD_ARGV[0] + i + 1, subnet_bits);
+ } else if (CMD_ARGC > 1) {
+ if (!string_to_ip(CMD_ARGV[1], (uint8_t *)&subnet_mask, &i))
+ return ERROR_COMMAND_SYNTAX_ERROR;
+ }
+
+ if (!subnet_mask)
+ subnet_mask = (uint32_t)(subnet_bits < 32 ?
+ ((1ULL << subnet_bits) - 1) : 0xffffffff);
+
+ cpy_ip(tmp_config.ip_address, ip_address);
+ cpy_ip(tmp_config.subnet_mask, (uint8_t *)&subnet_mask);
+ }
+
+ return ERROR_OK;
+}
+
+COMMAND_HANDLER(jlink_handle_config_reset_command)
+{
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG))
+ return ERROR_OK;
+
+ memcpy(&tmp_config, &config, sizeof(struct device_config));
+
+ return ERROR_OK;
+}
+
+
+COMMAND_HANDLER(jlink_handle_config_write_command)
+{
+ int ret;
+
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
+ command_print(CMD_CTX, "Reading configuration is not supported by the "
+ "device.");
+ return ERROR_OK;
+ }
+
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_WRITE_CONFIG)) {
+ command_print(CMD_CTX, "Writing configuration is not supported by the "
+ "device.");
+ return ERROR_OK;
+ }
+
+ if (!memcmp(&config, &tmp_config, sizeof(struct device_config))) {
+ command_print(CMD_CTX, "Operation not performed due to no changes in "
+ "the configuration.");
+ return ERROR_OK;
+ }
+
+ ret = jaylink_write_raw_config(devh, (const uint8_t *)&tmp_config);
+
+ if (ret != JAYLINK_OK) {
+ LOG_ERROR("jaylink_write_raw_config() failed: %s.",
+ jaylink_strerror(ret));
+ return ERROR_FAIL;
+ }
+
+ if (!read_device_config(&config)) {
+ LOG_ERROR("Failed to read device configuration for verification.");
+ return ERROR_FAIL;
+ }
+
+ if (memcmp(&config, &tmp_config, sizeof(struct device_config))) {
+ LOG_ERROR("Verification of device configuration failed. Please check "
+ "your device.");
+ return ERROR_FAIL;
+ }
+
+ memcpy(&tmp_config, &config, sizeof(struct device_config));
+ command_print(CMD_CTX, "The new device configuration applies after power "
+ "cycling the J-Link device.");
+
+ return ERROR_OK;
+}
+
+COMMAND_HANDLER(jlink_handle_config_command)
+{
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_READ_CONFIG)) {
+ command_print(CMD_CTX, "Device doesn't support reading configuration.");
+ return ERROR_OK;
+ }
+
+ if (CMD_ARGC == 0)
+ show_config(CMD_CTX);
+
+ return ERROR_OK;
+}
+
+COMMAND_HANDLER(jlink_handle_emucom_write_command)
+{
+ int ret;
+ size_t tmp;
+ uint32_t channel;
+ uint32_t length;
+ uint8_t *buf;
+ size_t dummy;
+
+ if (CMD_ARGC != 2)
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
+ LOG_ERROR("Device does not support EMUCOM.");
+ return ERROR_FAIL;
+ }
+
+ COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
+
+ tmp = strlen(CMD_ARGV[1]);
+
+ if (tmp % 2 != 0) {
+ LOG_ERROR("Data must be encoded as hexadecimal pairs.");
+ return ERROR_COMMAND_ARGUMENT_INVALID;
+ }
+
+ buf = malloc(tmp / 2);
+
+ if (!buf) {
+ LOG_ERROR("Failed to allocate buffer.");
+ return ERROR_FAIL;
+ }
+
+ dummy = unhexify(buf, CMD_ARGV[1], tmp / 2);
+
+ if (dummy != (tmp / 2)) {
+ LOG_ERROR("Data must be encoded as hexadecimal pairs.");
+ free(buf);
+ return ERROR_COMMAND_ARGUMENT_INVALID;
+ }
+
+ length = tmp / 2;
+ ret = jaylink_emucom_write(devh, channel, buf, &length);
+
+ free(buf);
+
+ if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
+ LOG_ERROR("Channel not supported by the device.");
+ return ERROR_FAIL;
+ } else if (ret != JAYLINK_OK) {
+ LOG_ERROR("Failed to write to channel: %s.", jaylink_strerror(ret));
+ return ERROR_FAIL;
+ }
+
+ if (length != (tmp / 2))
+ LOG_WARNING("Only %" PRIu32 " bytes written to the channel.", length);
+
+ return ERROR_OK;
+}
+
+COMMAND_HANDLER(jlink_handle_emucom_read_command)
+{
+ int ret;
+ uint32_t channel;
+ uint32_t length;
+ uint8_t *buf;
+ size_t tmp;
+
+ if (CMD_ARGC != 2)
+ return ERROR_COMMAND_SYNTAX_ERROR;
+
+ if (!jaylink_has_cap(caps, JAYLINK_DEV_CAP_EMUCOM)) {
+ LOG_ERROR("Device does not support EMUCOM.");
+ return ERROR_FAIL;
+ }
+
+ COMMAND_PARSE_NUMBER(u32, CMD_ARGV[0], channel);
+ COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], length);
+
+ buf = malloc(length * 3 + 1);
+
+ if (!buf) {
+ LOG_ERROR("Failed to allocate buffer.");
+ return ERROR_FAIL;
+ }
+
+ ret = jaylink_emucom_read(devh, channel, buf, &length);
+
+ if (ret == JAYLINK_ERR_DEV_NOT_SUPPORTED) {
+ LOG_ERROR("Channel is not supported by the device.");
+ free(buf);
+ return ERROR_FAIL;
+ } else if (ret == JAYLINK_ERR_DEV_NOT_AVAILABLE) {
+ LOG_ERROR("Channel is not available for the requested amount of data. "
+ "%" PRIu32 " bytes are avilable.", length);
+ free(buf);
+ return ERROR_FAIL;
+ } else if (ret != JAYLINK_OK) {
+ LOG_ERROR("Failed to read from channel: %s.", jaylink_strerror(ret));
+ free(buf);
+ return ERROR_FAIL;
+ }
+
+ tmp = hexify((char *)buf + length, buf, length, 2 * length + 1);
+
+ if (tmp != 2 * length) {
+ LOG_ERROR("Failed to convert data into hexadecimal string.");
+ free(buf);
+ return ERROR_FAIL;
+ }
+
+ command_print(CMD_CTX, "%s", buf + length);
+ free(buf);
+
+ return ERROR_OK;
+}
+
+static const struct command_registration jlink_config_subcommand_handlers[] = {
+ {
+ .name = "usb",
+ .handler = &jlink_handle_config_usb_address_command,
+ .mode = COMMAND_EXEC,
+ .help = "set the USB address",
+ .usage = "[0-3]",
+ },
+ {
+ .name = "targetpower",
+ .handler = &jlink_handle_config_target_power_command,
+ .mode = COMMAND_EXEC,
+ .help = "set the target power supply",
+ .usage = "[on|off]"
+ },
+ {
+ .name = "mac",
+ .handler = &jlink_handle_config_mac_address_command,
+ .mode = COMMAND_EXEC,
+ .help = "set the MAC Address",
+ .usage = "[ff:ff:ff:ff:ff:ff]",
+ },
+ {
+ .name = "ip",
+ .handler = &jlink_handle_config_ip_address_command,
+ .mode = COMMAND_EXEC,
+ .help = "set the IP address, where A.B.C.D is the IP address, "
+ "E the bit of the subnet mask, F.G.H.I the subnet mask",
+ .usage = "[A.B.C.D[/E] [F.G.H.I]]",
+ },
+ {
+ .name = "reset",
+ .handler = &jlink_handle_config_reset_command,
+ .mode = COMMAND_EXEC,
+ .help = "undo configuration changes",
+ .usage = "",
+ },
+ {
+ .name = "write",
+ .handler = &jlink_handle_config_write_command,
+ .mode = COMMAND_EXEC,
+ .help = "write configuration to the device",
+ .usage = "",
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
+static const struct command_registration jlink_emucom_subcommand_handlers[] = {
+ {
+ .name = "write",
+ .handler = &jlink_handle_emucom_write_command,
+ .mode = COMMAND_EXEC,
+ .help = "write to a channel",
+ .usage = "<channel> <data>",
+ },
+ {
+ .name = "read",
+ .handler = &jlink_handle_emucom_read_command,
+ .mode = COMMAND_EXEC,
+ .help = "read from a channel",
+ .usage = "<channel> <length>"
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
+static const struct command_registration jlink_subcommand_handlers[] = {
+ {
+ .name = "jtag",
+ .handler = &jlink_handle_jlink_jtag_command,
+ .mode = COMMAND_EXEC,
+ .help = "select the JTAG command version",
+ .usage = "[2|3]",
+ },
+ {
+ .name = "targetpower",
+ .handler = &jlink_handle_target_power_command,
+ .mode = COMMAND_EXEC,
+ .help = "set the target power supply",
+ .usage = "<on|off>"
+ },
+ {
+ .name = "freemem",
+ .handler = &jlink_handle_free_memory_command,
+ .mode = COMMAND_EXEC,
+ .help = "show free device memory",
+ .usage = "",
+ },
+ {
+ .name = "hwstatus",
+ .handler = &jlink_handle_hwstatus_command,
+ .mode = COMMAND_EXEC,
+ .help = "show the hardware status",
+ .usage = "",
+ },
+ {
+ .name = "usb",
+ .handler = &jlink_usb_command,
+ .mode = COMMAND_CONFIG,
+ .help = "set the USB address of the device that should be used",
+ .usage = "<0-3>"
+ },
+ {
+ .name = "serial",
+ .handler = &jlink_serial_command,
+ .mode = COMMAND_CONFIG,
+ .help = "set the serial number of the device that should be used",
+ .usage = "<serial number>"
+ },
+ {
+ .name = "config",
+ .handler = &jlink_handle_config_command,
+ .mode = COMMAND_EXEC,
+ .help = "access the device configuration. If no argument is given "
+ "this will show the device configuration",
+ .chain = jlink_config_subcommand_handlers,
+ .usage = "[<cmd>]",
+ },
+ {
+ .name = "emucom",
+ .mode = COMMAND_EXEC,
+ .help = "access EMUCOM channel",
+ .chain = jlink_emucom_subcommand_handlers,
+ .usage = "",
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
+static const struct command_registration jlink_command_handlers[] = {
+ {
+ .name = "jlink",
+ .mode = COMMAND_ANY,
+ .help = "perform jlink management",
+ .chain = jlink_subcommand_handlers,
+ .usage = "",
+ },
+ COMMAND_REGISTRATION_DONE
+};
+
+static int jlink_swd_init(void)
+{
+ iface = JAYLINK_TIF_SWD;
+
+ return ERROR_OK;
+}
+
+static void jlink_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
+{
+ assert(!(cmd & SWD_CMD_RnW));
+ jlink_swd_queue_cmd(cmd, NULL, value, ap_delay_clk);
+}
+
+static void jlink_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
+{
+ assert(cmd & SWD_CMD_RnW);
+ jlink_swd_queue_cmd(cmd, value, 0, ap_delay_clk);
+}
+
+static int_least32_t jlink_swd_frequency(int_least32_t hz)
+{
+ if (hz > 0)
+ jlink_speed(hz / 1000);
+
+ return hz;
+}
+
+/***************************************************************************/
+/* J-Link tap functions */
+
+static unsigned tap_length;
+/* In SWD mode use tms buffer for direction control */
+static uint8_t tms_buffer[JLINK_TAP_BUFFER_SIZE];
+static uint8_t tdi_buffer[JLINK_TAP_BUFFER_SIZE];
+static uint8_t tdo_buffer[JLINK_TAP_BUFFER_SIZE];
+
+struct pending_scan_result {
+ /** First bit position in tdo_buffer to read. */
+ unsigned first;
+ /** Number of bits to read. */
+ unsigned length;
+ /** Location to store the result */
+ void *buffer;
+ /** Offset in the destination buffer */
+ unsigned buffer_offset;
+};
+
+#define MAX_PENDING_SCAN_RESULTS 256
+
+static int pending_scan_results_length;
+static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
+
+static void jlink_tap_init(void)
+{
+ tap_length = 0;
+ pending_scan_results_length = 0;
+ memset(tms_buffer, 0, sizeof(tms_buffer));
+ memset(tdi_buffer, 0, sizeof(tdi_buffer));
+}
+
+static void jlink_clock_data(const uint8_t *out, unsigned out_offset,
+ const uint8_t *tms_out, unsigned tms_offset,
+ uint8_t *in, unsigned in_offset,
+ unsigned length)
+{
+ do {
+ unsigned available_length = JLINK_TAP_BUFFER_SIZE - tap_length / 8;
+
+ if (!available_length ||
+ (in && pending_scan_results_length == MAX_PENDING_SCAN_RESULTS)) {
+ if (jlink_flush() != ERROR_OK)
+ return;
+ available_length = JLINK_TAP_BUFFER_SIZE;
+ }
+
+ struct pending_scan_result *pending_scan_result =
+ &pending_scan_results_buffer[pending_scan_results_length];
+
+ unsigned scan_length = length > available_length ?
+ available_length : length;
+
+ if (out)
+ buf_set_buf(out, out_offset, tdi_buffer, tap_length, scan_length);
+ if (tms_out)
+ buf_set_buf(tms_out, tms_offset, tms_buffer, tap_length, scan_length);
+
+ if (in) {
+ pending_scan_result->first = tap_length;
+ pending_scan_result->length = scan_length;
+ pending_scan_result->buffer = in;
+ pending_scan_result->buffer_offset = in_offset;
+ pending_scan_results_length++;
+ }
+
+ tap_length += scan_length;
+ out_offset += scan_length;
+ tms_offset += scan_length;
+ in_offset += scan_length;
+ length -= scan_length;
+ } while (length > 0);
+}
+
+static int jlink_flush(void)
+{
+ int i;
+ int ret;
+
+ if (!tap_length)
+ return ERROR_OK;
+
+ jlink_last_state = jtag_debug_state_machine(tms_buffer, tdi_buffer,
+ tap_length, jlink_last_state);
+
+ ret = jaylink_jtag_io(devh, tms_buffer, tdi_buffer, tdo_buffer,
+ tap_length, jtag_command_version);
+
+ if (ret != JAYLINK_OK) {
+ LOG_ERROR("jaylink_jtag_io() failed: %s.", jaylink_strerror(ret));
+ jlink_tap_init();
+ return ERROR_JTAG_QUEUE_FAILED;
+ }
+
+ for (i = 0; i < pending_scan_results_length; i++) {
+ struct pending_scan_result *p = &pending_scan_results_buffer[i];
+
+ buf_set_buf(tdo_buffer, p->first, p->buffer,
+ p->buffer_offset, p->length);
+
+ DEBUG_JTAG_IO("Pending scan result, length = %d.", p->length);
+ }
+
+ jlink_tap_init();
+
+ return ERROR_OK;
+}
+
+static void fill_buffer(uint8_t *buf, uint32_t val, uint32_t len)
+{
+ unsigned int tap_pos = tap_length;
+
+ while (len > 32) {
+ buf_set_u32(buf, tap_pos, 32, val);
+ len -= 32;
+ tap_pos += 32;
+ }
+
+ if (len)
+ buf_set_u32(buf, tap_pos, len, val);
+}
+
+static void jlink_queue_data_out(const uint8_t *data, uint32_t len)
+{
+ const uint32_t dir_out = 0xffffffff;
+
+ if (data)
+ bit_copy(tdi_buffer, tap_length, data, 0, len);
+ else
+ fill_buffer(tdi_buffer, 0, len);
+
+ fill_buffer(tms_buffer, dir_out, len);
+ tap_length += len;
+}
+
+static void jlink_queue_data_in(uint32_t len)
+{
+ const uint32_t dir_in = 0;
+
+ fill_buffer(tms_buffer, dir_in, len);
+ tap_length += len;
+}
+
+static int jlink_swd_switch_seq(enum swd_special_seq seq)
+{
+ const uint8_t *s;
+ unsigned int s_len;
+
+ switch (seq) {
+ case LINE_RESET:
+ LOG_DEBUG("SWD line reset");
+ s = swd_seq_line_reset;
+ s_len = swd_seq_line_reset_len;
+ break;
+ case JTAG_TO_SWD:
+ LOG_DEBUG("JTAG-to-SWD");
+ s = swd_seq_jtag_to_swd;
+ s_len = swd_seq_jtag_to_swd_len;
+ break;
+ case SWD_TO_JTAG:
+ LOG_DEBUG("SWD-to-JTAG");
+ s = swd_seq_swd_to_jtag;
+ s_len = swd_seq_swd_to_jtag_len;
+ break;
+ default:
+ LOG_ERROR("Sequence %d not supported.", seq);
+ return ERROR_FAIL;
+ }
+
+ jlink_queue_data_out(s, s_len);
+
+ return ERROR_OK;
+}
+
+static int jlink_swd_run_queue(void)
+{
+ int i;
+ int ret;
+
+ LOG_DEBUG("Executing %d queued transactions.", pending_scan_results_length);
+
+ if (queued_retval != ERROR_OK) {
+ LOG_DEBUG("Skipping due to previous errors: %d.", queued_retval);
+ goto skip;
+ }
+
+ /*
+ * A transaction must be followed by another transaction or at least 8 idle
+ * cycles to ensure that data is clocked through the AP.
+ */
+ jlink_queue_data_out(NULL, 8);
+
+ ret = jaylink_swd_io(devh, tms_buffer, tdi_buffer, tdo_buffer, tap_length);
+
+ if (ret != JAYLINK_OK) {
+ LOG_ERROR("jaylink_swd_io() failed: %s.", jaylink_strerror(ret));
+ goto skip;
+ }
+
+ for (i = 0; i < pending_scan_results_length; i++) {
+ int ack = buf_get_u32(tdo_buffer, pending_scan_results_buffer[i].first, 3);
+
+ if (ack != SWD_ACK_OK) {
+ LOG_DEBUG("SWD ack not OK: %d %s", ack,
+ ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
+ queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
+ goto skip;
+ } else if (pending_scan_results_buffer[i].length) {
+ uint32_t data = buf_get_u32(tdo_buffer, 3 + pending_scan_results_buffer[i].first, 32);
+ int parity = buf_get_u32(tdo_buffer, 3 + 32 + pending_scan_results_buffer[i].first, 1);
+
+ if (parity != parity_u32(data)) {
+ LOG_ERROR("SWD: Read data parity mismatch.");
+ queued_retval = ERROR_FAIL;
+ goto skip;
+ }
+
+ if (pending_scan_results_buffer[i].buffer)
+ *(uint32_t *)pending_scan_results_buffer[i].buffer = data;
+ }
+ }
+
+skip:
+ jlink_tap_init();
+ ret = queued_retval;
+ queued_retval = ERROR_OK;
+
+ return ret;
+}
+
+static void jlink_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data, uint32_t ap_delay_clk)
+{
+ uint8_t data_parity_trn[DIV_ROUND_UP(32 + 1, 8)];
+ if (tap_length + 46 + 8 + ap_delay_clk >= swd_buffer_size * 8 ||
+ pending_scan_results_length == MAX_PENDING_SCAN_RESULTS) {
+ /* Not enough room in the queue. Run the queue. */
+ queued_retval = jlink_swd_run_queue();
+ }
+
+ if (queued_retval != ERROR_OK)
+ return;
+
+ cmd |= SWD_CMD_START | SWD_CMD_PARK;
+
+ jlink_queue_data_out(&cmd, 8);
+
+ pending_scan_results_buffer[pending_scan_results_length].first = tap_length;
+
+ if (cmd & SWD_CMD_RnW) {
+ /* Queue a read transaction. */
+ pending_scan_results_buffer[pending_scan_results_length].length = 32;
+ pending_scan_results_buffer[pending_scan_results_length].buffer = dst;
+
+ jlink_queue_data_in(1 + 3 + 32 + 1 + 1);
+ } else {
+ /* Queue a write transaction. */
+ pending_scan_results_buffer[pending_scan_results_length].length = 0;
+ jlink_queue_data_in(1 + 3 + 1);
+
+ buf_set_u32(data_parity_trn, 0, 32, data);
+ buf_set_u32(data_parity_trn, 32, 1, parity_u32(data));
+
+ jlink_queue_data_out(data_parity_trn, 32 + 1);
+ }
+
+ pending_scan_results_length++;
+
+ /* Insert idle cycles after AP accesses to avoid WAIT. */
+ if (cmd & SWD_CMD_APnDP)
+ jlink_queue_data_out(NULL, ap_delay_clk);
+}
+
+static const struct swd_driver jlink_swd = {
+ .init = &jlink_swd_init,
+ .frequency = &jlink_swd_frequency,
+ .switch_seq = &jlink_swd_switch_seq,
+ .read_reg = &jlink_swd_read_reg,
+ .write_reg = &jlink_swd_write_reg,
+ .run = &jlink_swd_run_queue,
+};
+
+static const char * const jlink_transports[] = { "jtag", "swd", NULL };
+
+struct jtag_interface jlink_interface = {
+ .name = "jlink",
+ .commands = jlink_command_handlers,
+ .transports = jlink_transports,
+ .swd = &jlink_swd,
+ .execute_queue = &jlink_execute_queue,
+ .speed = &jlink_speed,
+ .speed_div = &jlink_speed_div,
+ .khz = &jlink_khz,
+ .init = &jlink_init,
+ .quit = &jlink_quit,
+ .config_trace = &config_trace,
+ .poll_trace = &poll_trace,
+};