hla: if the idcode callback returns 0, treat as a wildcard
[fw/openocd] / src / jtag / hla / hla_interface.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, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.           *
22  ***************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 /* project specific includes */
29 #include <jtag/interface.h>
30 #include <transport/transport.h>
31 #include <helper/time_support.h>
32
33 #include <jtag/hla/hla_tcl.h>
34 #include <jtag/hla/hla_layout.h>
35 #include <jtag/hla/hla_transport.h>
36 #include <jtag/hla/hla_interface.h>
37
38 #include <target/target.h>
39
40 static struct hl_interface_s hl_if = { {0, 0, 0, 0, 0, HL_TRANSPORT_UNKNOWN, false, NULL, 0}, 0, 0 };
41
42 int hl_interface_open(enum hl_transports tr)
43 {
44         LOG_DEBUG("hl_interface_open");
45
46         enum reset_types jtag_reset_config = jtag_get_reset_config();
47
48         if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
49                 if (jtag_reset_config & RESET_SRST_NO_GATING)
50                         hl_if.param.connect_under_reset = true;
51                 else
52                         LOG_WARNING("\'srst_nogate\' reset_config option is required");
53         }
54
55         /* set transport mode */
56         hl_if.param.transport = tr;
57
58         int result = hl_if.layout->open(&hl_if);
59         if (result != ERROR_OK)
60                 return result;
61
62         return hl_interface_init_reset();
63 }
64
65 int hl_interface_init_target(struct target *t)
66 {
67         int res;
68
69         LOG_DEBUG("hl_interface_init_target");
70
71         /* this is the interface for the current target and we
72          * can setup the private pointer in the tap structure
73          * if the interface match the tap idcode
74          */
75         res = hl_if.layout->api->idcode(hl_if.fd, &t->tap->idcode);
76
77         if (res != ERROR_OK)
78                 return res;
79
80         unsigned ii, limit = t->tap->expected_ids_cnt;
81         int found = 0;
82
83         for (ii = 0; ii < limit; ii++) {
84                 uint32_t expected = t->tap->expected_ids[ii];
85
86                 /* treat "-expected-id 0" as a "don't-warn" wildcard */
87                 if (!expected || !t->tap->idcode ||
88                     (t->tap->idcode == expected)) {
89                         found = 1;
90                         break;
91                 }
92         }
93
94         if (found == 0) {
95                 LOG_ERROR("hl_interface_init_target: target not found: idcode: 0x%08x",
96                                 t->tap->idcode);
97                 return ERROR_FAIL;
98         }
99
100         t->tap->priv = &hl_if;
101         t->tap->hasidcode = 1;
102
103         return ERROR_OK;
104 }
105
106 static int hl_interface_init(void)
107 {
108         LOG_DEBUG("hl_interface_init");
109
110         /* here we can initialize the layout */
111         return hl_layout_init(&hl_if);
112 }
113
114 static int hl_interface_quit(void)
115 {
116         LOG_DEBUG("hl_interface_quit");
117
118         if (hl_if.param.trace_f) {
119                 fclose(hl_if.param.trace_f);
120                 hl_if.param.trace_f = NULL;
121         }
122
123         return ERROR_OK;
124 }
125
126 static int hl_interface_execute_queue(void)
127 {
128         LOG_DEBUG("hl_interface_execute_queue: ignored");
129
130         return ERROR_OK;
131 }
132
133 int hl_interface_init_reset(void)
134 {
135         /* incase the adapter has not already handled asserting srst
136          * we will attempt it again */
137         if (hl_if.param.connect_under_reset) {
138                 jtag_add_reset(0, 1);
139                 hl_if.layout->api->assert_srst(hl_if.fd, 0);
140         }
141
142         return ERROR_OK;
143 }
144
145 COMMAND_HANDLER(hl_interface_handle_device_desc_command)
146 {
147         LOG_DEBUG("hl_interface_handle_device_desc_command");
148
149         if (CMD_ARGC == 1) {
150                 hl_if.param.device_desc = strdup(CMD_ARGV[0]);
151         } else {
152                 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
153         }
154
155         return ERROR_OK;
156 }
157
158 COMMAND_HANDLER(hl_interface_handle_serial_command)
159 {
160         LOG_DEBUG("hl_interface_handle_serial_command");
161
162         if (CMD_ARGC == 1) {
163                 hl_if.param.serial = strdup(CMD_ARGV[0]);
164         } else {
165                 LOG_ERROR("expected exactly one argument to hl_serial <serial-number>");
166         }
167
168         return ERROR_OK;
169 }
170
171 COMMAND_HANDLER(hl_interface_handle_layout_command)
172 {
173         LOG_DEBUG("hl_interface_handle_layout_command");
174
175         if (CMD_ARGC != 1) {
176                 LOG_ERROR("Need exactly one argument to stlink_layout");
177                 return ERROR_COMMAND_SYNTAX_ERROR;
178         }
179
180         if (hl_if.layout) {
181                 LOG_ERROR("already specified hl_layout %s",
182                                 hl_if.layout->name);
183                 return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
184                     ? ERROR_FAIL : ERROR_OK;
185         }
186
187         for (const struct hl_layout *l = hl_layout_get_list(); l->name;
188              l++) {
189                 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
190                         hl_if.layout = l;
191                         return ERROR_OK;
192                 }
193         }
194
195         LOG_ERROR("No adapter layout '%s' found", CMD_ARGV[0]);
196         return ERROR_FAIL;
197 }
198
199 COMMAND_HANDLER(hl_interface_handle_vid_pid_command)
200 {
201         LOG_DEBUG("hl_interface_handle_vid_pid_command");
202
203         if (CMD_ARGC != 2) {
204                 LOG_WARNING("ignoring extra IDs in hl_vid_pid (maximum is 1 pair)");
205                 return ERROR_COMMAND_SYNTAX_ERROR;
206         }
207
208         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], hl_if.param.vid);
209         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], hl_if.param.pid);
210
211         return ERROR_OK;
212 }
213
214 COMMAND_HANDLER(stlink_interface_handle_api_command)
215 {
216         if (CMD_ARGC != 1)
217                 return ERROR_COMMAND_SYNTAX_ERROR;
218
219         unsigned new_api;
220         COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], new_api);
221         if ((new_api == 0) || (new_api > 2))
222                 return ERROR_COMMAND_SYNTAX_ERROR;
223
224         hl_if.param.api = new_api;
225
226         return ERROR_OK;
227 }
228
229 COMMAND_HANDLER(interface_handle_trace_command)
230 {
231         FILE *f;
232         unsigned source_hz;
233
234         if (CMD_ARGC != 2)
235                 return ERROR_COMMAND_SYNTAX_ERROR;
236
237         f = fopen(CMD_ARGV[0], "a");
238         if (!f)
239                 return ERROR_COMMAND_SYNTAX_ERROR;
240
241         COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], source_hz);
242         if (source_hz == 0) {
243                 fclose(f);
244                 return ERROR_COMMAND_SYNTAX_ERROR;
245         }
246
247         hl_if.param.trace_f = f;
248         hl_if.param.trace_source_hz = source_hz;
249
250         return ERROR_OK;
251 }
252
253 static const struct command_registration hl_interface_command_handlers[] = {
254         {
255          .name = "hla_device_desc",
256          .handler = &hl_interface_handle_device_desc_command,
257          .mode = COMMAND_CONFIG,
258          .help = "set the a device description of the adapter",
259          .usage = "description_string",
260          },
261         {
262          .name = "hla_serial",
263          .handler = &hl_interface_handle_serial_command,
264          .mode = COMMAND_CONFIG,
265          .help = "set the serial number of the adapter",
266          .usage = "serial_string",
267          },
268         {
269          .name = "hla_layout",
270          .handler = &hl_interface_handle_layout_command,
271          .mode = COMMAND_CONFIG,
272          .help = "set the layout of the adapter",
273          .usage = "layout_name",
274          },
275         {
276          .name = "hla_vid_pid",
277          .handler = &hl_interface_handle_vid_pid_command,
278          .mode = COMMAND_CONFIG,
279          .help = "the vendor and product ID of the adapter",
280          .usage = "(vid pid)* ",
281          },
282          {
283          .name = "stlink_api",
284          .handler = &stlink_interface_handle_api_command,
285          .mode = COMMAND_CONFIG,
286          .help = "set the desired stlink api level",
287          .usage = "api version 1 or 2",
288          },
289          {
290          .name = "trace",
291          .handler = &interface_handle_trace_command,
292          .mode = COMMAND_CONFIG,
293          .help = "configure trace reception",
294          .usage = "destination_path source_lock_hz",
295          },
296         COMMAND_REGISTRATION_DONE
297 };
298
299 struct jtag_interface hl_interface = {
300         .name = "hla",
301         .supported = 0,
302         .commands = hl_interface_command_handlers,
303         .transports = hl_transports,
304         .init = hl_interface_init,
305         .quit = hl_interface_quit,
306         .execute_queue = hl_interface_execute_queue,
307 };