target/xtensa: avoid IHI for writes to non-executable memory
[openocd.git] / src / jtag / drivers / usb_blaster / usb_blaster.c
index 165ebdcd35a75e93064542987fbb6b72ef9e569a..c84055c4a1cea7bc2b3cdf4dcc6dc080a0fbbdb9 100644 (file)
@@ -1,3 +1,5 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+
 /*
  *   Driver for USB-JTAG, Altera USB-Blaster and compatibles
  *
  *   Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
  *   Copyright (C) 2006 Kolja Waschk usbjtag@ixo.de
  *
- *   This program is free software; you can redistribute it and/or modify
- *   it under the terms of the GNU General Public License as published by
- *   the Free Software Foundation; either version 2 of the License, or
- *   (at your option) any later version.
- *
- *   This program is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
- *
  */
 
 /*
@@ -77,6 +66,7 @@
 #include <jtag/interface.h>
 #include <jtag/commands.h>
 #include <helper/time_support.h>
+#include <helper/replacements.h>
 #include "ublast_access.h"
 
 /* system includes */
@@ -118,7 +108,6 @@ struct ublast_info {
 
        char *lowlevel_name;
        struct ublast_lowlevel *drv;
-       char *ublast_device_desc;
        uint16_t ublast_vid, ublast_pid;
        uint16_t ublast_vid_uninit, ublast_pid_uninit;
        int flags;
@@ -139,7 +128,7 @@ static struct ublast_info info = {
 };
 
 /*
- * Available lowlevel drivers (FTDI, FTD2xx, ...)
+ * Available lowlevel drivers (FTDI, libusb, ...)
  */
 struct drvs_map {
        char *name;
@@ -174,7 +163,7 @@ static int ublast_buf_read(uint8_t *buf, unsigned size, uint32_t *bytes_read)
        int ret = info.drv->read(info.drv, buf, size, bytes_read);
        char *str = hexdump(buf, *bytes_read);
 
-       LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %u", size, str,
+       LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %" PRIu32, size, str,
                      *bytes_read);
        free(str);
        return ret;
@@ -185,7 +174,7 @@ static int ublast_buf_write(uint8_t *buf, int size, uint32_t *bytes_written)
        int ret = info.drv->write(info.drv, buf, size, bytes_written);
        char *str = hexdump(buf, *bytes_written);
 
-       LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %u", size, str,
+       LOG_DEBUG_IO("(size=%d, buf=[%s]) -> %" PRIu32, size, str,
                      *bytes_written);
        free(str);
        return ret;
@@ -198,7 +187,7 @@ static int nb_buf_remaining(void)
 
 static void ublast_flush_buffer(void)
 {
-       unsigned int retlen;
+       uint32_t retlen;
        int nb = info.bufidx, ret = ERROR_OK;
 
        while (ret == ERROR_OK && nb > 0) {
@@ -253,7 +242,7 @@ static void ublast_flush_buffer(void)
 
 /**
  * ublast_queue_byte - queue one 'bitbang mode' byte for USB Blaster
- * @abyte: the byte to queue
+ * @param abyte the byte to queue
  *
  * Queues one byte in 'bitbang mode' to the USB Blaster. The byte is not
  * actually sent, but stored in a buffer. The write is performed once
@@ -271,11 +260,11 @@ static void ublast_queue_byte(uint8_t abyte)
 
 /**
  * ublast_compute_pin - compute if gpio should be asserted
- * @steer: control (ie. TRST driven, SRST driven, of fixed)
+ * @param steer control (ie. TRST driven, SRST driven, of fixed)
  *
  * Returns pin value (1 means driven high, 0 mean driven low)
  */
-bool ublast_compute_pin(enum gpio_steer steer)
+static bool ublast_compute_pin(enum gpio_steer steer)
 {
        switch (steer) {
        case FIXED_0:
@@ -293,7 +282,7 @@ bool ublast_compute_pin(enum gpio_steer steer)
 
 /**
  * ublast_build_out - build bitbang mode output byte
- * @type: says if reading back TDO is required
+ * @param type says if reading back TDO is required
  *
  * Returns the compute bitbang mode byte
  */
@@ -313,8 +302,8 @@ static uint8_t ublast_build_out(enum scan_type type)
 
 /**
  * ublast_reset - reset the JTAG device is possible
- * @trst: 1 if TRST is to be asserted
- * @srst: 1 if SRST is to be asserted
+ * @param trst 1 if TRST is to be asserted
+ * @param srst 1 if SRST is to be asserted
  */
 static void ublast_reset(int trst, int srst)
 {
@@ -329,7 +318,7 @@ static void ublast_reset(int trst, int srst)
 
 /**
  * ublast_clock_tms - clock a TMS transition
- * @tms: the TMS to be sent
+ * @param tms the TMS to be sent
  *
  * Triggers a TMS transition (ie. one JTAG TAP state move).
  */
@@ -360,16 +349,16 @@ static void ublast_idle_clock(void)
 
 /**
  * ublast_clock_tdi - Output a TDI with bitbang mode
- * @tdi: the TDI bit to be shifted out
- * @type: scan type (ie. does a readback of TDO is required)
+ * @param tdi the TDI bit to be shifted out
+ * @param type scan type (ie. does a readback of TDO is required)
  *
  * Output a TDI bit and assert clock to push it into the JTAG device :
- *  - writing out TCK=0, TMS=<old_state>=0, TDI=<tdi>
- * - writing out TCK=1, TMS=<new_state>, TDI=<tdi> which triggers the JTAG
- *    device aquiring the data.
+ *  - writing out TCK=0, TMS=\<old_state>=0, TDI=\<tdi>
+ *  - writing out TCK=1, TMS=\<new_state>, TDI=\<tdi> which triggers the JTAG
+ *    device acquiring the data.
  *
  * If a TDO is to be read back, the required read is requested (bitbang mode),
- * and the USB Blaster will send back a byte with bit0 reprensenting the TDO.
+ * and the USB Blaster will send back a byte with bit0 representing the TDO.
  */
 static void ublast_clock_tdi(int tdi, enum scan_type type)
 {
@@ -387,11 +376,11 @@ static void ublast_clock_tdi(int tdi, enum scan_type type)
 
 /**
  * ublast_clock_tdi_flip_tms - Output a TDI with bitbang mode, change JTAG state
- * @tdi: the TDI bit to be shifted out
- * @type: scan type (ie. does a readback of TDO is required)
+ * @param tdi the TDI bit to be shifted out
+ * @param type scan type (ie. does a readback of TDO is required)
  *
  * This function is the same as ublast_clock_tdi(), but it changes also the TMS
- * while outputing the TDI. This should be the last TDI output of a TDI
+ * while output the TDI. This should be the last TDI output of a TDI
  * sequence, which will change state from :
  *   - IRSHIFT -> IREXIT1
  *   - or DRSHIFT -> DREXIT1
@@ -416,8 +405,8 @@ static void ublast_clock_tdi_flip_tms(int tdi, enum scan_type type)
 
 /**
  * ublast_queue_bytes - queue bytes for the USB Blaster
- * @bytes: byte array
- * @nb_bytes: number of bytes
+ * @param bytes byte array
+ * @param nb_bytes number of bytes
  *
  * Queues bytes to be sent to the USB Blaster. The bytes are not
  * actually sent, but stored in a buffer. The write is performed once
@@ -443,13 +432,13 @@ static void ublast_queue_bytes(uint8_t *bytes, int nb_bytes)
 
 /**
  * ublast_tms_seq - write a TMS sequence transition to JTAG
- * @bits: TMS bits to be written (bit0, bit1 .. bitN)
- * @nb_bits: number of TMS bits (between 1 and 8)
- * @skip: number of TMS bits to skip at the beginning of the series
+ * @param bits TMS bits to be written (bit0, bit1 .. bitN)
+ * @param nb_bits number of TMS bits (between 1 and 8)
+ * @param skip number of TMS bits to skip at the beginning of the series
  *
- * Write a serie of TMS transitions, where each transition consists in :
- *  - writing out TCK=0, TMS=<new_state>, TDI=<???>
- *  - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
+ * Write a series of TMS transitions, where each transition consists in :
+ *  - writing out TCK=0, TMS=\<new_state>, TDI=\<???>
+ *  - writing out TCK=1, TMS=\<new_state>, TDI=\<???> which triggers the transition
  * The function ensures that at the end of the sequence, the clock (TCK) is put
  * low.
  */
@@ -465,7 +454,7 @@ static void ublast_tms_seq(const uint8_t *bits, int nb_bits, int skip)
 
 /**
  * ublast_tms - write a tms command
- * @cmd: tms command
+ * @param cmd tms command
  */
 static void ublast_tms(struct tms_command *cmd)
 {
@@ -475,11 +464,11 @@ static void ublast_tms(struct tms_command *cmd)
 
 /**
  * ublast_path_move - write a TMS sequence transition to JTAG
- * @cmd: path transition
+ * @param cmd path transition
  *
- * Write a serie of TMS transitions, where each transition consists in :
- *  - writing out TCK=0, TMS=<new_state>, TDI=<???>
- *  - writing out TCK=1, TMS=<new_state>, TDI=<???> which triggers the transition
+ * Write a series of TMS transitions, where each transition consists in :
+ *  - writing out TCK=0, TMS=\<new_state>, TDI=\<???>
+ *  - writing out TCK=1, TMS=\<new_state>, TDI=\<???> which triggers the transition
  * The function ensures that at the end of the sequence, the clock (TCK) is put
  * low.
  */
@@ -501,8 +490,8 @@ static void ublast_path_move(struct pathmove_command *cmd)
 
 /**
  * ublast_state_move - move JTAG state to the target state
- * @state: the target state
- * @skip: number of bits to skip at the beginning of the path
+ * @param state the target state
+ * @param skip number of bits to skip at the beginning of the path
  *
  * Input the correct TMS sequence to the JTAG TAP so that we end up in the
  * target state. This assumes the current state (tap_get_state()) is correct.
@@ -524,8 +513,8 @@ static void ublast_state_move(tap_state_t state, int skip)
 
 /**
  * ublast_read_byteshifted_tdos - read TDO of byteshift writes
- * @buf: the buffer to store the bits
- * @nb_bits: the number of bits
+ * @param buf the buffer to store the bits
+ * @param nb_bytes the number of bytes
  *
  * Reads back from USB Blaster TDO bits, triggered by a 'byteshift write', ie. eight
  * bits per received byte from USB interface, and store them in buffer.
@@ -534,11 +523,11 @@ static void ublast_state_move(tap_state_t state, int skip)
  * bit0), second bit in (byte0, bit1), ...), which is what we want to return,
  * simply read bytes from USB interface and store them.
  *
- * Returns ERROR_OK if OK, ERROR_xxx if a read error occured
+ * Returns ERROR_OK if OK, ERROR_xxx if a read error occurred
  */
 static int ublast_read_byteshifted_tdos(uint8_t *buf, int nb_bytes)
 {
-       unsigned int retlen;
+       uint32_t retlen;
        int ret = ERROR_OK;
 
        LOG_DEBUG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bytes * 8);
@@ -552,25 +541,25 @@ static int ublast_read_byteshifted_tdos(uint8_t *buf, int nb_bytes)
 
 /**
  * ublast_read_bitbang_tdos - read TDO of bitbang writes
- * @buf: the buffer to store the bits
- * @nb_bits: the number of bits
+ * @param buf the buffer to store the bits
+ * @param nb_bits the number of bits
  *
  * Reads back from USB Blaster TDO bits, triggered by a 'bitbang write', ie. one
  * bit per received byte from USB interface, and store them in buffer, where :
  *  - first bit is stored in byte0, bit0 (LSB)
  *  - second bit is stored in byte0, bit 1
  *  ...
- *  - eight bit is sotred in byte0, bit 7
- *  - ninth bit is sotred in byte1, bit 0
+ *  - eight bit is stored in byte0, bit 7
+ *  - ninth bit is stored in byte1, bit 0
  *  - etc ...
  *
- * Returns ERROR_OK if OK, ERROR_xxx if a read error occured
+ * Returns ERROR_OK if OK, ERROR_xxx if a read error occurred
  */
 static int ublast_read_bitbang_tdos(uint8_t *buf, int nb_bits)
 {
        int nb1 = nb_bits;
        int i, ret = ERROR_OK;
-       unsigned int retlen;
+       uint32_t retlen;
        uint8_t tmp[8];
 
        LOG_DEBUG_IO("%s(buf=%p, num_bits=%d)", __func__, buf, nb_bits);
@@ -592,11 +581,11 @@ static int ublast_read_bitbang_tdos(uint8_t *buf, int nb_bits)
 
 /**
  * ublast_queue_tdi - short description
- * @bits: bits to be queued on TDI (or NULL if 0 are to be queued)
- * @nb_bits: number of bits
- * @scan: scan type (ie. if TDO read back is required or not)
+ * @param bits bits to be queued on TDI (or NULL if 0 are to be queued)
+ * @param nb_bits number of bits
+ * @param scan scan type (ie. if TDO read back is required or not)
  *
- * Outputs a serie of TDI bits on TDI.
+ * Outputs a series of TDI bits on TDI.
  * As a side effect, the last TDI bit is sent along a TMS=1, and triggers a JTAG
  * TAP state shift if input bits were non NULL.
  *
@@ -703,11 +692,11 @@ static void ublast_stableclocks(int cycles)
 
 /**
  * ublast_scan - launches a DR-scan or IR-scan
- * @cmd: the command to launch
+ * @param cmd the command to launch
  *
  * Launch a JTAG IR-scan or DR-scan
  *
- * Returns ERROR_OK if OK, ERROR_xxx if a read/write error occured.
+ * Returns ERROR_OK if OK, ERROR_xxx if a read/write error occurred.
  */
 static int ublast_scan(struct scan_command *cmd)
 {
@@ -736,8 +725,7 @@ static int ublast_scan(struct scan_command *cmd)
        ublast_queue_tdi(buf, scan_bits, type);
 
        ret = jtag_read_buffer(buf, cmd);
-       if (buf)
-               free(buf);
+       free(buf);
        /*
         * ublast_queue_tdi sends the last bit with TMS=1. We are therefore
         * already in Exit1-DR/IR and have to skip the first step on our way
@@ -777,7 +765,7 @@ static void ublast_initial_wipeout(void)
        tap_set_state(TAP_RESET);
 }
 
-static int ublast_execute_queue(void)
+static int ublast_execute_queue(struct jtag_command *cmd_queue)
 {
        struct jtag_command *cmd;
        static int first_call = 1;
@@ -788,7 +776,7 @@ static int ublast_execute_queue(void)
                ublast_initial_wipeout();
        }
 
-       for (cmd = jtag_command_queue; ret == ERROR_OK && cmd != NULL;
+       for (cmd = cmd_queue; ret == ERROR_OK && cmd;
             cmd = cmd->next) {
                switch (cmd->type) {
                case JTAG_RESET:
@@ -816,6 +804,11 @@ static int ublast_execute_queue(void)
                case JTAG_SCAN:
                        ret = ublast_scan(cmd->cmd.scan);
                        break;
+               default:
+                       LOG_ERROR("BUG: unknown JTAG command type 0x%X",
+                                 cmd->type);
+                       ret = ERROR_FAIL;
+                       break;
                }
        }
 
@@ -869,7 +862,6 @@ static int ublast_init(void)
        info.drv->ublast_pid = info.ublast_pid;
        info.drv->ublast_vid_uninit = info.ublast_vid_uninit;
        info.drv->ublast_pid_uninit = info.ublast_pid_uninit;
-       info.drv->ublast_device_desc = info.ublast_device_desc;
        info.drv->firmware_path = info.firmware_path;
 
        info.flags |= info.drv->flags;
@@ -897,22 +889,12 @@ static int ublast_init(void)
 static int ublast_quit(void)
 {
        uint8_t byte0 = 0;
-       unsigned int retlen;
+       uint32_t retlen;
 
        ublast_buf_write(&byte0, 1, &retlen);
        return info.drv->close(info.drv);
 }
 
-COMMAND_HANDLER(ublast_handle_device_desc_command)
-{
-       if (CMD_ARGC != 1)
-               return ERROR_COMMAND_SYNTAX_ERROR;
-
-       info.ublast_device_desc = strdup(CMD_ARGV[0]);
-
-       return ERROR_OK;
-}
-
 COMMAND_HANDLER(ublast_handle_vid_pid_command)
 {
        if (CMD_ARGC > 4) {
@@ -1025,39 +1007,32 @@ COMMAND_HANDLER(ublast_firmware_command)
 }
 
 
-static const struct command_registration ublast_command_handlers[] = {
+static const struct command_registration ublast_subcommand_handlers[] = {
        {
-               .name = "usb_blaster_device_desc",
-               .handler = ublast_handle_device_desc_command,
-               .mode = COMMAND_CONFIG,
-               .help = "set the USB device description of the USB-Blaster",
-               .usage = "description-string",
-       },
-       {
-               .name = "usb_blaster_vid_pid",
+               .name = "vid_pid",
                .handler = ublast_handle_vid_pid_command,
                .mode = COMMAND_CONFIG,
-               .help = "the vendor ID and product ID of the USB-Blaster and " \
-                       "vendor ID and product ID of the uninitialized device " \
+               .help = "the vendor ID and product ID of the USB-Blaster and "
+                       "vendor ID and product ID of the uninitialized device "
                        "for USB-Blaster II",
                .usage = "vid pid vid_uninit pid_uninit",
        },
        {
-               .name = "usb_blaster_lowlevel_driver",
+               .name = "lowlevel_driver",
                .handler = ublast_handle_lowlevel_drv_command,
                .mode = COMMAND_CONFIG,
                .help = "set the lowlevel access for the USB Blaster (ftdi, ublast2)",
                .usage = "(ftdi|ublast2)",
        },
        {
-               .name = "usb_blaster_pin",
+               .name = "pin",
                .handler = ublast_handle_pin_command,
                .mode = COMMAND_ANY,
                .help = "show or set pin state for the unused GPIO pins",
                .usage = "(pin6|pin8) (0|1|s|t)",
        },
                {
-               .name = "usb_blaster_firmware",
+               .name = "firmware",
                .handler = &ublast_firmware_command,
                .mode = COMMAND_CONFIG,
                .help = "configure the USB-Blaster II firmware location",
@@ -1066,13 +1041,29 @@ static const struct command_registration ublast_command_handlers[] = {
        COMMAND_REGISTRATION_DONE
 };
 
-struct jtag_interface usb_blaster_interface = {
+static const struct command_registration ublast_command_handlers[] = {
+       {
+               .name = "usb_blaster",
+               .mode = COMMAND_ANY,
+               .help = "perform usb_blaster management",
+               .chain = ublast_subcommand_handlers,
+               .usage = "",
+       },
+       COMMAND_REGISTRATION_DONE
+};
+
+static struct jtag_interface usb_blaster_interface = {
+       .supported = DEBUG_CAP_TMS_SEQ,
+       .execute_queue = ublast_execute_queue,
+};
+
+struct adapter_driver usb_blaster_adapter_driver = {
        .name = "usb_blaster",
        .transports = jtag_only,
        .commands = ublast_command_handlers,
-       .supported = DEBUG_CAP_TMS_SEQ,
 
-       .execute_queue = ublast_execute_queue,
        .init = ublast_init,
        .quit = ublast_quit,
+
+       .jtag_ops = &usb_blaster_interface,
 };

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)