aboutsummaryrefslogtreecommitdiff
path: root/src/jtag/adapter.c
diff options
context:
space:
mode:
authorAntonio Borneo <borneo.antonio@gmail.com>2021-10-07 12:13:28 +0200
committerAntonio Borneo <borneo.antonio@gmail.com>2021-11-13 10:47:23 +0000
commitb8ec1d4e7ddcb4cc3993a2d31225fd17527f267d (patch)
tree59e1e049355a82c52e6e773bc66b11204a8e52da /src/jtag/adapter.c
parent4cb3c9fae2a6a6cb5cf2c664179edace22abf5f6 (diff)
downloadriscv-openocd-b8ec1d4e7ddcb4cc3993a2d31225fd17527f267d.zip
riscv-openocd-b8ec1d4e7ddcb4cc3993a2d31225fd17527f267d.tar.gz
riscv-openocd-b8ec1d4e7ddcb4cc3993a2d31225fd17527f267d.tar.bz2
jtag: move adapter init/quit and speed to adapter.c
The configuration code for adapter parameters is spread around. Move in adapter.c the code that handles the configuration of adapter speed. For convenience, move also the functions adapter_init() and adapter_quit(), that anyway have no reason to be in file core.c To simplify the review, the code moved is not modified. It will be cleaned and adapted in the following changes. Change-Id: I2b38975a0cd2e74d3d2de6c56ea17818ff225fd8 Signed-off-by: Antonio Borneo <borneo.antonio@gmail.com> Reviewed-on: https://review.openocd.org/c/openocd/+/6641 Tested-by: jenkins Reviewed-by: Tarek BOCHKATI <tarek.bouchkati@gmail.com>
Diffstat (limited to 'src/jtag/adapter.c')
-rw-r--r--src/jtag/adapter.c182
1 files changed, 182 insertions, 0 deletions
diff --git a/src/jtag/adapter.c b/src/jtag/adapter.c
index 47a1d79..bdb131c 100644
--- a/src/jtag/adapter.c
+++ b/src/jtag/adapter.c
@@ -37,6 +37,188 @@ static struct {
char *usb_location;
} adapter_config;
+/* speed in kHz*/
+static int speed_khz;
+/* speed to fallback to when RCLK is requested but not supported */
+static int rclk_fallback_speed_khz;
+static enum {CLOCK_MODE_UNSELECTED, CLOCK_MODE_KHZ, CLOCK_MODE_RCLK} clock_mode;
+
+static bool adapter_initialized;
+
+bool is_adapter_initialized(void)
+{
+ return adapter_initialized;
+}
+
+/**
+ * Do low-level setup like initializing registers, output signals,
+ * and clocking.
+ */
+int adapter_init(struct command_context *cmd_ctx)
+{
+ if (is_adapter_initialized())
+ return ERROR_OK;
+
+ if (!adapter_driver) {
+ /* nothing was previously specified by "adapter driver" command */
+ LOG_ERROR("Debug Adapter has to be specified, "
+ "see \"adapter driver\" command");
+ return ERROR_JTAG_INVALID_INTERFACE;
+ }
+
+ int retval;
+ retval = adapter_driver->init();
+ if (retval != ERROR_OK)
+ return retval;
+ adapter_initialized = true;
+
+ if (!adapter_driver->speed) {
+ LOG_INFO("This adapter doesn't support configurable speed");
+ return ERROR_OK;
+ }
+
+ if (clock_mode == CLOCK_MODE_UNSELECTED) {
+ LOG_ERROR("An adapter speed is not selected in the init script."
+ " Insert a call to \"adapter speed\" or \"jtag_rclk\" to proceed.");
+ return ERROR_JTAG_INIT_FAILED;
+ }
+
+ int requested_khz = jtag_get_speed_khz();
+ int actual_khz = requested_khz;
+ int jtag_speed_var = 0;
+ retval = jtag_get_speed(&jtag_speed_var);
+ if (retval != ERROR_OK)
+ return retval;
+ retval = adapter_driver->speed(jtag_speed_var);
+ if (retval != ERROR_OK)
+ return retval;
+ retval = jtag_get_speed_readable(&actual_khz);
+ if (retval != ERROR_OK)
+ LOG_INFO("adapter-specific clock speed value %d", jtag_speed_var);
+ else if (actual_khz) {
+ /* Adaptive clocking -- JTAG-specific */
+ if ((clock_mode == CLOCK_MODE_RCLK)
+ || ((clock_mode == CLOCK_MODE_KHZ) && !requested_khz)) {
+ LOG_INFO("RCLK (adaptive clock speed) not supported - fallback to %d kHz"
+ , actual_khz);
+ } else
+ LOG_INFO("clock speed %d kHz", actual_khz);
+ } else
+ LOG_INFO("RCLK (adaptive clock speed)");
+
+ return ERROR_OK;
+}
+
+int adapter_quit(void)
+{
+ if (is_adapter_initialized() && adapter_driver->quit) {
+ /* close the JTAG interface */
+ int result = adapter_driver->quit();
+ if (result != ERROR_OK)
+ LOG_ERROR("failed: %d", result);
+ }
+
+ struct jtag_tap *t = jtag_all_taps();
+ while (t) {
+ struct jtag_tap *n = t->next_tap;
+ jtag_tap_free(t);
+ t = n;
+ }
+
+ return ERROR_OK;
+}
+
+unsigned jtag_get_speed_khz(void)
+{
+ return speed_khz;
+}
+
+static int adapter_khz_to_speed(unsigned khz, int *speed)
+{
+ LOG_DEBUG("convert khz to interface specific speed value");
+ speed_khz = khz;
+ if (!is_adapter_initialized())
+ return ERROR_OK;
+ LOG_DEBUG("have interface set up");
+ if (!adapter_driver->khz) {
+ LOG_ERROR("Translation from khz to jtag_speed not implemented");
+ return ERROR_FAIL;
+ }
+ int speed_div1;
+ int retval = adapter_driver->khz(jtag_get_speed_khz(), &speed_div1);
+ if (retval != ERROR_OK)
+ return retval;
+ *speed = speed_div1;
+ return ERROR_OK;
+}
+
+static int jtag_rclk_to_speed(unsigned fallback_speed_khz, int *speed)
+{
+ int retval = adapter_khz_to_speed(0, speed);
+ if ((retval != ERROR_OK) && fallback_speed_khz) {
+ LOG_DEBUG("trying fallback speed...");
+ retval = adapter_khz_to_speed(fallback_speed_khz, speed);
+ }
+ return retval;
+}
+
+static int jtag_set_speed(int speed)
+{
+ /* this command can be called during CONFIG,
+ * in which case jtag isn't initialized */
+ return is_adapter_initialized() ? adapter_driver->speed(speed) : ERROR_OK;
+}
+
+int jtag_config_khz(unsigned khz)
+{
+ LOG_DEBUG("handle jtag khz");
+ clock_mode = CLOCK_MODE_KHZ;
+ int speed = 0;
+ int retval = adapter_khz_to_speed(khz, &speed);
+ return (retval != ERROR_OK) ? retval : jtag_set_speed(speed);
+}
+
+int jtag_config_rclk(unsigned fallback_speed_khz)
+{
+ LOG_DEBUG("handle jtag rclk");
+ clock_mode = CLOCK_MODE_RCLK;
+ rclk_fallback_speed_khz = fallback_speed_khz;
+ int speed = 0;
+ int retval = jtag_rclk_to_speed(fallback_speed_khz, &speed);
+ return (retval != ERROR_OK) ? retval : jtag_set_speed(speed);
+}
+
+int jtag_get_speed(int *speed)
+{
+ switch (clock_mode) {
+ case CLOCK_MODE_KHZ:
+ adapter_khz_to_speed(jtag_get_speed_khz(), speed);
+ break;
+ case CLOCK_MODE_RCLK:
+ jtag_rclk_to_speed(rclk_fallback_speed_khz, speed);
+ break;
+ default:
+ LOG_ERROR("BUG: unknown jtag clock mode");
+ return ERROR_FAIL;
+ }
+ return ERROR_OK;
+}
+
+int jtag_get_speed_readable(int *khz)
+{
+ int jtag_speed_var = 0;
+ int retval = jtag_get_speed(&jtag_speed_var);
+ if (retval != ERROR_OK)
+ return retval;
+ if (!is_adapter_initialized())
+ return ERROR_OK;
+ if (!adapter_driver->speed_div) {
+ LOG_ERROR("Translation from jtag_speed to khz not implemented");
+ return ERROR_FAIL;
+ }
+ return adapter_driver->speed_div(jtag_speed_var, khz);
+}
+
/*
* 1 char: bus
* 2 * 7 chars: max 7 ports