hla_transport: split command registration per transport
[fw/openocd] / src / jtag / hla / hla_transport.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 <jtag/tcl.h>
29 #include <transport/transport.h>
30 #include <helper/time_support.h>
31 #include <target/target.h>
32 #include <jtag/hla/hla_tcl.h>
33 #include <jtag/hla/hla_transport.h>
34 #include <jtag/hla/hla_interface.h>
35
36 COMMAND_HANDLER(hl_transport_jtag_command)
37 {
38         LOG_DEBUG("hl_transport_jtag_command");
39
40         return ERROR_OK;
41 }
42
43 COMMAND_HANDLER(hl_transport_reset_command)
44 {
45         return hl_interface_init_reset();
46 }
47
48 static const struct command_registration
49 hl_swd_transport_subcommand_handlers[] = {
50         {
51          .name = "newdap",
52          .mode = COMMAND_CONFIG,
53          .jim_handler = jim_hl_newtap,
54          .help = "declare a new SWD DAP",
55          },
56         COMMAND_REGISTRATION_DONE
57 };
58
59 static const struct command_registration hl_swd_transport_command_handlers[] = {
60         {
61          .name = "swd",
62          .mode = COMMAND_ANY,
63          .help = "SWD command group",
64          .usage = "",
65          .chain = hl_swd_transport_subcommand_handlers,
66          },
67         COMMAND_REGISTRATION_DONE
68 };
69
70 static const struct command_registration
71 hl_transport_jtag_subcommand_handlers[] = {
72         {
73          .name = "newtap",
74          .mode = COMMAND_CONFIG,
75          .jim_handler = jim_hl_newtap,
76          .help = "Create a new TAP instance named basename.tap_type, "
77          "and appends it to the scan chain.",
78          .usage = "basename tap_type '-irlen' count "
79          "['-expected_id' number] ",
80          },
81         {
82          .name = "init",
83          .mode = COMMAND_ANY,
84          .handler = hl_transport_jtag_command,
85          .usage = ""
86          },
87         {
88          .name = "arp_init",
89          .mode = COMMAND_ANY,
90          .handler = hl_transport_jtag_command,
91          .usage = ""
92          },
93         {
94          .name = "arp_init-reset",
95          .mode = COMMAND_ANY,
96          .handler = hl_transport_reset_command,
97          .usage = ""
98          },
99         {
100          .name = "tapisenabled",
101          .mode = COMMAND_EXEC,
102          .jim_handler = jim_jtag_tap_enabler,
103          },
104         {
105          .name = "tapenable",
106          .mode = COMMAND_EXEC,
107          .jim_handler = jim_jtag_tap_enabler,
108          },
109         {
110          .name = "tapdisable",
111          .mode = COMMAND_EXEC,
112          .handler = hl_transport_jtag_command,
113          .usage = "",
114          },
115         {
116          .name = "configure",
117          .mode = COMMAND_EXEC,
118          .handler = hl_transport_jtag_command,
119          .usage = "",
120          },
121         {
122          .name = "cget",
123          .mode = COMMAND_EXEC,
124          .jim_handler = jim_jtag_configure,
125          },
126         {
127          .name = "names",
128          .mode = COMMAND_ANY,
129          .handler = hl_transport_jtag_command,
130          .usage = "",
131          },
132
133         COMMAND_REGISTRATION_DONE
134 };
135
136 static const struct command_registration hl_jtag_transport_command_handlers[] = {
137         {
138          .name = "jtag",
139          .mode = COMMAND_ANY,
140          .help = "perform jtag tap actions",
141          .usage = "",
142          .chain = hl_transport_jtag_subcommand_handlers,
143          },
144         {
145          .name = "jtag_ntrst_delay",
146          .mode = COMMAND_ANY,
147          .handler = hl_transport_jtag_command,
148          .usage = "",
149          },
150         COMMAND_REGISTRATION_DONE
151 };
152
153
154 static int hl_transport_init(struct command_context *cmd_ctx)
155 {
156         LOG_DEBUG("hl_transport_init");
157         struct target *t = get_current_target(cmd_ctx);
158         struct transport *transport;
159         enum hl_transports tr;
160
161         if (!t) {
162                 LOG_ERROR("no current target");
163                 return ERROR_FAIL;
164         }
165
166         transport = get_current_transport();
167
168         if (!transport) {
169                 LOG_ERROR("no transport selected");
170                 return ERROR_FAIL;
171         }
172
173         LOG_DEBUG("current transport %s", transport->name);
174
175         /* get selected transport as enum */
176         tr = HL_TRANSPORT_UNKNOWN;
177
178         if (strcmp(transport->name, "hla_swd") == 0)
179                 tr = HL_TRANSPORT_SWD;
180         else if (strcmp(transport->name, "hla_jtag") == 0)
181                 tr = HL_TRANSPORT_JTAG;
182
183         int retval = hl_interface_open(tr);
184
185         if (retval != ERROR_OK)
186                 return retval;
187
188         return hl_interface_init_target(t);
189 }
190
191 static int hl_jtag_transport_select(struct command_context *cmd_ctx)
192 {
193         LOG_DEBUG("hl_jtag_transport_select");
194
195         /* NOTE:  interface init must already have been done.
196          * That works with only C code ... no Tcl glue required.
197          */
198
199         return register_commands(cmd_ctx, NULL,
200                                 hl_jtag_transport_command_handlers);
201 }
202
203 static int hl_swd_transport_select(struct command_context *cmd_ctx)
204 {
205         LOG_DEBUG("hl_swd_transport_select");
206         return register_commands(cmd_ctx, NULL,
207                                 hl_swd_transport_command_handlers);
208 }
209
210 static struct transport hl_swd_transport = {
211         .name = "hla_swd",
212         .select = hl_swd_transport_select,
213         .init = hl_transport_init,
214         .override_target = hl_interface_override_target,
215 };
216
217 static struct transport hl_jtag_transport = {
218         .name = "hla_jtag",
219         .select = hl_jtag_transport_select,
220         .init = hl_transport_init,
221         .override_target = hl_interface_override_target,
222 };
223
224 const char *hl_transports[] = { "hla_swd", "hla_jtag", NULL };
225
226 static void hl_constructor(void) __attribute__ ((constructor));
227 static void hl_constructor(void)
228 {
229         transport_register(&hl_swd_transport);
230         transport_register(&hl_jtag_transport);
231 }
232
233 bool transport_is_hla(void)
234 {
235         struct transport *t;
236         t = get_current_transport();
237         return t == &hl_swd_transport || t == &hl_jtag_transport;
238 }