jtag: drivers: usb_blaster: clarify lowlevel driver selection code
[openocd.git] / src / jtag / drivers / usb_blaster / usb_blaster.c
index ccbed0e2be58439f2a7ece61612f607961f5156e..df9f2a174797d886ed245921526853255d2c5695 100644 (file)
@@ -4,6 +4,7 @@
  *   Inspired from original code from Kolja Waschk's USB-JTAG project
  *   (http://www.ixo.de/info/usb_jtag/), and from openocd project.
  *
+ *   Copyright (C) 2013 Franck Jullien franck.jullien@gmail.com
  *   Copyright (C) 2012 Robert Jarzmik robert.jarzmik@free.fr
  *   Copyright (C) 2011 Ali Lown ali@lown.me.uk
  *   Copyright (C) 2009 Catalin Patulea cat@vv.carleton.ca
@@ -19,6 +20,9 @@
  *   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/>.
+ *
  */
 
 /*
  *           | 6 MHz XTAL  |  | 24 MHz Osc. |
  *           |_____________|  |_____________|
  *
+ * USB-JTAG, Altera USB-Blaster II are typically implemented as a Cypress
+ * EZ-USB FX2LP followed by a CPLD.
+ *            _____________    _________
+ *           |             |  |         |
+ *      USB__| EZ-USB FX2  |__| EPM570  |__JTAG (B_TDO,B_TDI,B_TMS,B_TCK)
+ *           |_____________|  |_________|
+ *            __|__________
+ *           |             |
+ *           | 24 MHz XTAL |
+ *           |_____________|
  */
 
 #ifdef HAVE_CONFIG_H
@@ -82,6 +96,9 @@
  */
 #define BUF_LEN 4096
 
+/* USB-Blaster II specific command */
+#define CMD_COPY_TDO_BUFFER    0x5F
+
 enum gpio_steer {
        FIXED_0 = 0,
        FIXED_1,
@@ -103,6 +120,9 @@ struct ublast_info {
        struct ublast_lowlevel *drv;
        char *ublast_device_desc;
        uint16_t ublast_vid, ublast_pid;
+       uint16_t ublast_vid_uninit, ublast_pid_uninit;
+       int flags;
+       char *firmware_path;
 };
 
 /*
@@ -127,11 +147,11 @@ struct drvs_map {
 };
 
 static struct drvs_map lowlevel_drivers_map[] = {
-#if BUILD_USB_BLASTER_LIBFTDI
+#if BUILD_USB_BLASTER
        { .name = "ftdi", .drv_register = ublast_register_ftdi },
 #endif
-#if BUILD_USB_BLASTER_FTD2XX
-       { .name = "ftd2xx", .drv_register = ublast_register_ftd2xx },
+#if BUILD_USB_BLASTER_2
+       { .name = "ublast2", .drv_register = ublast2_register_libusb },
 #endif
        { NULL, NULL },
 };
@@ -631,8 +651,11 @@ static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
                        ublast_queue_bytes(&bits[i], trans);
                else
                        ublast_queue_bytes(byte0, trans);
-               if (read_tdos)
+               if (read_tdos) {
+                       if (info.flags & COPY_TDO_BUFFER)
+                               ublast_queue_byte(CMD_COPY_TDO_BUFFER);
                        ublast_read_byteshifted_tdos(&tdos[i], trans);
+               }
        }
 
        /*
@@ -645,8 +668,11 @@ static void ublast_queue_tdi(uint8_t *bits, int nb_bits, enum scan_type scan)
                else
                        ublast_clock_tdi(tdi, scan);
        }
-       if (nb1 && read_tdos)
+       if (nb1 && read_tdos) {
+               if (info.flags & COPY_TDO_BUFFER)
+                       ublast_queue_byte(CMD_COPY_TDO_BUFFER);
                ublast_read_bitbang_tdos(&tdos[nb8], nb1);
+       }
 
        if (bits)
                memcpy(bits, tdos, DIV_ROUND_UP(nb_bits, 8));
@@ -724,17 +750,47 @@ static int ublast_scan(struct scan_command *cmd)
        return ret;
 }
 
-static void ublast_msleep(int ms)
+static void ublast_usleep(int us)
 {
-       DEBUG_JTAG_IO("%s(ms=%d)",  __func__, ms);
-       jtag_sleep(ms);
+       DEBUG_JTAG_IO("%s(us=%d)",  __func__, us);
+       jtag_sleep(us);
+}
+
+static void ublast_initial_wipeout(void)
+{
+       static uint8_t tms_reset = 0xff;
+       uint8_t out_value;
+       uint32_t retlen;
+       int i;
+
+       out_value = ublast_build_out(SCAN_OUT);
+       for (i = 0; i < BUF_LEN; i++)
+               info.buf[i] = out_value | ((i % 2) ? TCK : 0);
+
+       /*
+        * Flush USB-Blaster queue fifos
+        *  - empty the write FIFO (128 bytes)
+        *  - empty the read FIFO (384 bytes)
+        */
+       ublast_buf_write(info.buf, BUF_LEN, &retlen);
+       /*
+        * Put JTAG in RESET state (five 1 on TMS)
+        */
+       ublast_tms_seq(&tms_reset, 5);
+       tap_set_state(TAP_RESET);
 }
 
 static int ublast_execute_queue(void)
 {
        struct jtag_command *cmd;
+       static int first_call = 1;
        int ret = ERROR_OK;
 
+       if (first_call) {
+               first_call--;
+               ublast_initial_wipeout();
+       }
+
        for (cmd = jtag_command_queue; ret == ERROR_OK && cmd != NULL;
             cmd = cmd->next) {
                switch (cmd->type) {
@@ -758,7 +814,7 @@ static int ublast_execute_queue(void)
                        ublast_tms(cmd->cmd.tms);
                        break;
                case JTAG_SLEEP:
-                       ublast_msleep(cmd->cmd.sleep->us);
+                       ublast_usleep(cmd->cmd.sleep->us);
                        break;
                case JTAG_SCAN:
                        ret = ublast_scan(cmd->cmd.scan);
@@ -775,57 +831,60 @@ static int ublast_execute_queue(void)
  *
  * Initialize the device :
  *  - open the USB device
- *  - empty the write FIFO (128 bytes)
- *  - empty the read FIFO (384 bytes)
+ *  - pretend it's initialized while actual init is delayed until first jtag command
  *
  * Returns ERROR_OK if USB device found, error if not.
  */
 static int ublast_init(void)
 {
-       static uint8_t tms_reset = 0xff;
        int ret, i;
 
-       if (info.lowlevel_name) {
-               for (i = 0; lowlevel_drivers_map[i].name; i++)
-                       if (!strcmp(lowlevel_drivers_map[i].name, info.lowlevel_name))
+       for (i = 0; lowlevel_drivers_map[i].name; i++) {
+               if (info.lowlevel_name) {
+                       if (!strcmp(lowlevel_drivers_map[i].name, info.lowlevel_name)) {
+                               info.drv = lowlevel_drivers_map[i].drv_register();
+                               if (!info.drv) {
+                                       LOG_ERROR("Error registering lowlevel driver \"%s\"",
+                                                 info.lowlevel_name);
+                                       return ERROR_JTAG_DEVICE_ERROR;
+                               }
                                break;
-               if (lowlevel_drivers_map[i].name)
+                       }
+               } else {
                        info.drv = lowlevel_drivers_map[i].drv_register();
-               if (!info.drv) {
-                       LOG_ERROR("no lowlevel driver found for %s or lowlevel driver opening error",
-                                 info.lowlevel_name);
-                       return ERROR_JTAG_DEVICE_ERROR;
-               }
-       } else {
-               LOG_INFO("No lowlevel driver configured, will try them all");
-               for (i = 0; !info.drv && lowlevel_drivers_map[i].name; i++)
-                       info.drv = lowlevel_drivers_map[i].drv_register();
-               if (!info.drv) {
-                       LOG_ERROR("no lowlevel driver found");
-                       return ERROR_JTAG_DEVICE_ERROR;
+                       if (info.drv) {
+                               info.lowlevel_name = strdup(lowlevel_drivers_map[i].name);
+                               LOG_INFO("No lowlevel driver configured, using %s", info.lowlevel_name);
+                               break;
+                       }
                }
        }
 
+       if (!info.drv) {
+               LOG_ERROR("No lowlevel driver available");
+               return ERROR_JTAG_DEVICE_ERROR;
+       }
+
        /*
         * Register the lowlevel driver
         */
        info.drv->ublast_vid = info.ublast_vid;
        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;
 
        ret = info.drv->open(info.drv);
-       if (ret == ERROR_OK) {
-               /*
-                * Flush USB-Blaster queue fifos
-                */
-               uint32_t retlen;
-               ublast_buf_write(info.buf, BUF_LEN, &retlen);
-               /*
-                * Put JTAG in RESET state (five 1 on TMS)
-                */
-               ublast_tms_seq(&tms_reset, 5);
-               tap_set_state(TAP_RESET);
-       }
+
+       /*
+        * Let lie here : the TAP is in an unknown state, but the first
+        * execute_queue() will trigger a ublast_initial_wipeout(), which will
+        * put the TAP in RESET.
+        */
+       tap_set_state(TAP_RESET);
        return ret;
 }
 
@@ -849,29 +908,36 @@ static int ublast_quit(void)
 
 COMMAND_HANDLER(ublast_handle_device_desc_command)
 {
-       if (CMD_ARGC == 1)
-               info.ublast_device_desc = strdup(CMD_ARGV[0]);
-       else
-               LOG_ERROR("require exactly one argument to "
-                                 "ublast_device_desc <description>");
+       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 > 2) {
+       if (CMD_ARGC > 4) {
                LOG_WARNING("ignoring extra IDs in ublast_vid_pid "
-                                       "(maximum is 1 pair)");
-               CMD_ARGC = 2;
+                                       "(maximum is 2 pairs)");
+               CMD_ARGC = 4;
        }
-       if (CMD_ARGC == 2) {
+
+       if (CMD_ARGC >= 2) {
                COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], info.ublast_vid);
                COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], info.ublast_pid);
        } else {
                LOG_WARNING("incomplete ublast_vid_pid configuration");
        }
 
+       if (CMD_ARGC == 4) {
+               COMMAND_PARSE_NUMBER(u16, CMD_ARGV[2], info.ublast_vid_uninit);
+               COMMAND_PARSE_NUMBER(u16, CMD_ARGV[3], info.ublast_pid_uninit);
+       } else {
+               LOG_WARNING("incomplete ublast_vid_pid configuration");
+       }
+
        return ERROR_OK;
 }
 
@@ -913,7 +979,7 @@ COMMAND_HANDLER(ublast_handle_pin_command)
 
                if (strlen(pin_value) > 1)
                        val = '?';
-               switch (tolower(val)) {
+               switch (tolower((unsigned char)val)) {
                case '0':
                        *steer = FIXED_0;
                        break;
@@ -943,14 +1009,25 @@ COMMAND_HANDLER(ublast_handle_pin_command)
 
 COMMAND_HANDLER(ublast_handle_lowlevel_drv_command)
 {
-       if (CMD_ARGC == 1)
-               info.lowlevel_name = strdup(CMD_ARGV[0]);
-       else
-               LOG_ERROR("require exactly one argument to "
-                                 "usb_blaster_lowlevel_driver (ftdi|ftd2xx)");
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
+       info.lowlevel_name = strdup(CMD_ARGV[0]);
+
        return ERROR_OK;
 }
 
+COMMAND_HANDLER(ublast_firmware_command)
+{
+       if (CMD_ARGC != 1)
+               return ERROR_COMMAND_SYNTAX_ERROR;
+
+       info.firmware_path = strdup(CMD_ARGV[0]);
+
+       return ERROR_OK;
+}
+
+
 static const struct command_registration ublast_command_handlers[] = {
        {
                .name = "usb_blaster_device_desc",
@@ -963,15 +1040,17 @@ static const struct command_registration ublast_command_handlers[] = {
                .name = "usb_blaster_vid_pid",
                .handler = ublast_handle_vid_pid_command,
                .mode = COMMAND_CONFIG,
-               .help = "the vendor ID and product ID of the USB-Blaster",
-               .usage = "vid pid",
+               .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",
                .handler = ublast_handle_lowlevel_drv_command,
                .mode = COMMAND_CONFIG,
-               .help = "set the lowlevel access for the USB Blaster (ftdi, ftd2xx)",
-               .usage = "(ftdi|ftd2xx)",
+               .help = "set the lowlevel access for the USB Blaster (ftdi, ublast2)",
+               .usage = "(ftdi|ublast2)",
        },
        {
                .name = "usb_blaster_pin",
@@ -979,6 +1058,13 @@ static const struct command_registration ublast_command_handlers[] = {
                .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",
+               .handler = &ublast_firmware_command,
+               .mode = COMMAND_CONFIG,
+               .help = "configure the USB-Blaster II firmware location",
+               .usage = "path/to/blaster_xxxx.hex",
        },
        COMMAND_REGISTRATION_DONE
 };

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)