jtag: Add an option to ignore the bypass bit
[fw/openocd] / src / jtag / hla / hla_tcl.c
1 /***************************************************************************
2  *   Copyright (C) 2011 by Mathias Kuester                                 *
3  *   Mathias Kuester <kesmtp@freenet.de>                                   *
4  *                                                                         *
5  *   Copyright (C) 2012 by Spencer Oliver                                  *
6  *   spen@spen-soft.co.uk                                                  *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; either version 2 of the License, or     *
11  *   (at your option) any later version.                                   *
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
20  ***************************************************************************/
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 /* project specific includes */
27 #include <jtag/interface.h>
28 #include <transport/transport.h>
29 #include <helper/time_support.h>
30
31 static int jim_newtap_expected_id(struct jim_nvp *n, struct jim_getopt_info *goi,
32                                   struct jtag_tap *tap)
33 {
34         jim_wide w;
35         int e = jim_getopt_wide(goi, &w);
36         if (e != JIM_OK) {
37                 Jim_SetResultFormatted(goi->interp, "option: %s bad parameter",
38                                        n->name);
39                 return e;
40         }
41
42         uint32_t *p = realloc(tap->expected_ids,
43                               (tap->expected_ids_cnt + 1) * sizeof(uint32_t));
44         if (!p) {
45                 Jim_SetResultFormatted(goi->interp, "no memory");
46                 return JIM_ERR;
47         }
48
49         tap->expected_ids = p;
50         tap->expected_ids[tap->expected_ids_cnt++] = w;
51
52         return JIM_OK;
53 }
54
55 #define NTAP_OPT_IRLEN     0
56 #define NTAP_OPT_IRMASK    1
57 #define NTAP_OPT_IRCAPTURE 2
58 #define NTAP_OPT_ENABLED   3
59 #define NTAP_OPT_DISABLED  4
60 #define NTAP_OPT_EXPECTED_ID 5
61 #define NTAP_OPT_VERSION   6
62 #define NTAP_OPT_BYPASS    7
63
64 static int jim_hl_newtap_cmd(struct jim_getopt_info *goi)
65 {
66         struct jtag_tap *tap;
67         int x;
68         int e;
69         struct jim_nvp *n;
70         char *cp;
71         const struct jim_nvp opts[] = {
72                 { .name = "-irlen",       .value = NTAP_OPT_IRLEN },
73                 { .name = "-irmask",       .value = NTAP_OPT_IRMASK },
74                 { .name = "-ircapture",       .value = NTAP_OPT_IRCAPTURE },
75                 { .name = "-enable",       .value = NTAP_OPT_ENABLED },
76                 { .name = "-disable",       .value = NTAP_OPT_DISABLED },
77                 { .name = "-expected-id",       .value = NTAP_OPT_EXPECTED_ID },
78                 { .name = "-ignore-version",       .value = NTAP_OPT_VERSION },
79                 { .name = "-ignore-bypass",       .value = NTAP_OPT_BYPASS },
80                 { .name = NULL, .value = -1},
81         };
82
83         tap = calloc(1, sizeof(struct jtag_tap));
84         if (!tap) {
85                 Jim_SetResultFormatted(goi->interp, "no memory");
86                 return JIM_ERR;
87         }
88
89         /*
90          * we expect CHIP + TAP + OPTIONS
91          * */
92         if (goi->argc < 3) {
93                 Jim_SetResultFormatted(goi->interp,
94                                        "Missing CHIP TAP OPTIONS ....");
95                 free(tap);
96                 return JIM_ERR;
97         }
98
99         const char *tmp;
100         jim_getopt_string(goi, &tmp, NULL);
101         tap->chip = strdup(tmp);
102
103         jim_getopt_string(goi, &tmp, NULL);
104         tap->tapname = strdup(tmp);
105
106         /* name + dot + name + null */
107         x = strlen(tap->chip) + 1 + strlen(tap->tapname) + 1;
108         cp = malloc(x);
109         sprintf(cp, "%s.%s", tap->chip, tap->tapname);
110         tap->dotted_name = cp;
111
112         LOG_DEBUG("Creating New Tap, Chip: %s, Tap: %s, Dotted: %s, %d params",
113                   tap->chip, tap->tapname, tap->dotted_name, goi->argc);
114
115         while (goi->argc) {
116                 e = jim_getopt_nvp(goi, opts, &n);
117                 if (e != JIM_OK) {
118                         jim_getopt_nvp_unknown(goi, opts, 0);
119                         free(cp);
120                         free(tap);
121                         return e;
122                 }
123                 LOG_DEBUG("Processing option: %s", n->name);
124                 switch (n->value) {
125                 case NTAP_OPT_EXPECTED_ID:
126                         e = jim_newtap_expected_id(n, goi, tap);
127                         if (e != JIM_OK) {
128                                 free(cp);
129                                 free(tap);
130                                 return e;
131                         }
132                         break;
133                 case NTAP_OPT_IRLEN:
134                 case NTAP_OPT_IRMASK:
135                 case NTAP_OPT_IRCAPTURE:
136                         /* dummy read to ignore the next argument */
137                         jim_getopt_wide(goi, NULL);
138                         break;
139                 }               /* switch (n->value) */
140         }                       /* while (goi->argc) */
141
142         /* default is enabled-after-reset */
143         tap->enabled = !tap->disabled_after_reset;
144
145         jtag_tap_init(tap);
146         return JIM_OK;
147 }
148
149 int jim_hl_newtap(Jim_Interp *interp, int argc, Jim_Obj * const *argv)
150 {
151         struct jim_getopt_info goi;
152         jim_getopt_setup(&goi, interp, argc - 1, argv + 1);
153         return jim_hl_newtap_cmd(&goi);
154 }