openocd: remove NULL comparisons with checkpatch [1/2]
[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, 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 #include <jtag/hla/hla_tcl.h>
32 #include <jtag/hla/hla_layout.h>
33 #include <jtag/hla/hla_transport.h>
34 #include <jtag/hla/hla_interface.h>
35
36 #include <target/target.h>
37
38 static struct hl_interface_s hl_if = { {0, 0, { 0 }, { 0 }, HL_TRANSPORT_UNKNOWN, false, -1, false, 7184}, 0, 0 };
39
40 int hl_interface_open(enum hl_transports tr)
41 {
42         LOG_DEBUG("hl_interface_open");
43
44         enum reset_types jtag_reset_config = jtag_get_reset_config();
45
46         if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
47                 if (jtag_reset_config & RESET_SRST_NO_GATING)
48                         hl_if.param.connect_under_reset = true;
49                 else
50                         LOG_WARNING("\'srst_nogate\' reset_config option is required");
51         }
52
53         /* set transport mode */
54         hl_if.param.transport = tr;
55
56         int result = hl_if.layout->open(&hl_if);
57         if (result != ERROR_OK)
58                 return result;
59
60         return hl_interface_init_reset();
61 }
62
63 int hl_interface_init_target(struct target *t)
64 {
65         int res;
66
67         LOG_DEBUG("hl_interface_init_target");
68
69         /* this is the interface for the current target and we
70          * can setup the private pointer in the tap structure
71          * if the interface match the tap idcode
72          */
73         res = hl_if.layout->api->idcode(hl_if.handle, &t->tap->idcode);
74
75         if (res != ERROR_OK)
76                 return res;
77
78         unsigned ii, limit = t->tap->expected_ids_cnt;
79         int found = 0;
80
81         for (ii = 0; ii < limit; ii++) {
82                 uint32_t expected = t->tap->expected_ids[ii];
83
84                 /* treat "-expected-id 0" as a "don't-warn" wildcard */
85                 if (!expected || !t->tap->idcode ||
86                     (t->tap->idcode == expected)) {
87                         found = 1;
88                         break;
89                 }
90         }
91
92         if (found == 0) {
93                 LOG_WARNING("UNEXPECTED idcode: 0x%08" PRIx32, t->tap->idcode);
94                 for (ii = 0; ii < limit; ii++)
95                         LOG_ERROR("expected %u of %u: 0x%08" PRIx32, ii + 1, limit,
96                                 t->tap->expected_ids[ii]);
97
98                 return ERROR_FAIL;
99         }
100
101         t->tap->priv = &hl_if;
102         t->tap->hasidcode = 1;
103
104         return ERROR_OK;
105 }
106
107 static int hl_interface_init(void)
108 {
109         LOG_DEBUG("hl_interface_init");
110
111         /* here we can initialize the layout */
112         return hl_layout_init(&hl_if);
113 }
114
115 static int hl_interface_quit(void)
116 {
117         LOG_DEBUG("hl_interface_quit");
118
119         if (hl_if.layout->api->close)
120                 hl_if.layout->api->close(hl_if.handle);
121
122         jtag_command_queue_reset();
123
124         free((void *)hl_if.param.device_desc);
125         free((void *)hl_if.param.serial);
126
127         return ERROR_OK;
128 }
129
130 static int hl_interface_reset(int req_trst, int req_srst)
131 {
132         return hl_if.layout->api->assert_srst(hl_if.handle, req_srst ? 0 : 1);
133 }
134
135 int hl_interface_init_reset(void)
136 {
137         /* in case the adapter has not already handled asserting srst
138          * we will attempt it again */
139         if (hl_if.param.connect_under_reset) {
140                 adapter_assert_reset();
141         } else {
142                 adapter_deassert_reset();
143         }
144
145         return ERROR_OK;
146 }
147
148 static int hl_interface_khz(int khz, int *jtag_speed)
149 {
150         if (!hl_if.layout->api->speed)
151                 return ERROR_OK;
152
153         *jtag_speed = hl_if.layout->api->speed(hl_if.handle, khz, true);
154         return ERROR_OK;
155 }
156
157 static int hl_interface_speed_div(int speed, int *khz)
158 {
159         *khz = speed;
160         return ERROR_OK;
161 }
162
163 static int hl_interface_speed(int speed)
164 {
165         if (!hl_if.layout->api->speed)
166                 return ERROR_OK;
167
168         if (!hl_if.handle) {
169                 /* pass speed as initial param as interface not open yet */
170                 hl_if.param.initial_interface_speed = speed;
171                 return ERROR_OK;
172         }
173
174         hl_if.layout->api->speed(hl_if.handle, speed, false);
175
176         return ERROR_OK;
177 }
178
179 int hl_interface_override_target(const char **targetname)
180 {
181         if (hl_if.layout->api->override_target) {
182                 if (hl_if.layout->api->override_target(*targetname)) {
183                         *targetname = "hla_target";
184                         return ERROR_OK;
185                 } else
186                         return ERROR_FAIL;
187         }
188         return ERROR_FAIL;
189 }
190
191 static int hl_interface_config_trace(bool enabled, enum tpiu_pin_protocol pin_protocol,
192                 uint32_t port_size, unsigned int *trace_freq,
193                 unsigned int traceclkin_freq, uint16_t *prescaler)
194 {
195         if (hl_if.layout->api->config_trace)
196                 return hl_if.layout->api->config_trace(hl_if.handle, enabled,
197                         pin_protocol, port_size, trace_freq, traceclkin_freq, prescaler);
198         else if (enabled) {
199                 LOG_ERROR("The selected interface does not support tracing");
200                 return ERROR_FAIL;
201         }
202
203         return ERROR_OK;
204 }
205
206 static int hl_interface_poll_trace(uint8_t *buf, size_t *size)
207 {
208         if (hl_if.layout->api->poll_trace)
209                 return hl_if.layout->api->poll_trace(hl_if.handle, buf, size);
210
211         return ERROR_FAIL;
212 }
213
214 COMMAND_HANDLER(hl_interface_handle_device_desc_command)
215 {
216         LOG_DEBUG("hl_interface_handle_device_desc_command");
217
218         if (CMD_ARGC == 1) {
219                 hl_if.param.device_desc = strdup(CMD_ARGV[0]);
220         } else {
221                 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
222         }
223
224         return ERROR_OK;
225 }
226
227 COMMAND_HANDLER(hl_interface_handle_serial_command)
228 {
229         LOG_DEBUG("hl_interface_handle_serial_command");
230
231         if (CMD_ARGC == 1) {
232                 hl_if.param.serial = strdup(CMD_ARGV[0]);
233         } else {
234                 LOG_ERROR("expected exactly one argument to hl_serial <serial-number>");
235         }
236
237         return ERROR_OK;
238 }
239
240 COMMAND_HANDLER(hl_interface_handle_layout_command)
241 {
242         LOG_DEBUG("hl_interface_handle_layout_command");
243
244         if (CMD_ARGC != 1) {
245                 LOG_ERROR("Need exactly one argument to stlink_layout");
246                 return ERROR_COMMAND_SYNTAX_ERROR;
247         }
248
249         if (hl_if.layout) {
250                 LOG_ERROR("already specified hl_layout %s",
251                                 hl_if.layout->name);
252                 return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
253                     ? ERROR_FAIL : ERROR_OK;
254         }
255
256         for (const struct hl_layout *l = hl_layout_get_list(); l->name;
257              l++) {
258                 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
259                         hl_if.layout = l;
260                         return ERROR_OK;
261                 }
262         }
263
264         LOG_ERROR("No adapter layout '%s' found", CMD_ARGV[0]);
265         return ERROR_FAIL;
266 }
267
268 COMMAND_HANDLER(hl_interface_handle_vid_pid_command)
269 {
270         if (CMD_ARGC > HLA_MAX_USB_IDS * 2) {
271                 LOG_WARNING("ignoring extra IDs in hla_vid_pid "
272                         "(maximum is %d pairs)", HLA_MAX_USB_IDS);
273                 CMD_ARGC = HLA_MAX_USB_IDS * 2;
274         }
275         if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
276                 LOG_WARNING("incomplete hla_vid_pid configuration directive");
277                 return ERROR_COMMAND_SYNTAX_ERROR;
278         }
279
280         unsigned i;
281         for (i = 0; i < CMD_ARGC; i += 2) {
282                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], hl_if.param.vid[i / 2]);
283                 COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], hl_if.param.pid[i / 2]);
284         }
285
286         /*
287          * Explicitly terminate, in case there are multiple instances of
288          * hla_vid_pid.
289          */
290         hl_if.param.vid[i / 2] = hl_if.param.pid[i / 2] = 0;
291
292         return ERROR_OK;
293 }
294
295 COMMAND_HANDLER(hl_interface_handle_stlink_backend_command)
296 {
297         /* default values */
298         bool use_stlink_tcp = false;
299         uint16_t stlink_tcp_port = 7184;
300
301         if (CMD_ARGC == 0 || CMD_ARGC > 2)
302                 return ERROR_COMMAND_SYNTAX_ERROR;
303         else if (strcmp(CMD_ARGV[0], "usb") == 0) {
304                 if (CMD_ARGC > 1)
305                         return ERROR_COMMAND_SYNTAX_ERROR;
306                 /* else use_stlink_tcp = false (already the case ) */
307         } else if (strcmp(CMD_ARGV[0], "tcp") == 0) {
308                 use_stlink_tcp = true;
309                 if (CMD_ARGC == 2)
310                         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], stlink_tcp_port);
311         } else
312                 return ERROR_COMMAND_SYNTAX_ERROR;
313
314         hl_if.param.use_stlink_tcp = use_stlink_tcp;
315         hl_if.param.stlink_tcp_port = stlink_tcp_port;
316
317         return ERROR_OK;
318 }
319
320 COMMAND_HANDLER(interface_handle_hla_command)
321 {
322         if (CMD_ARGC != 1)
323                 return ERROR_COMMAND_SYNTAX_ERROR;
324
325         if (!hl_if.layout->api->custom_command) {
326                 LOG_ERROR("The selected adapter doesn't support custom commands");
327                 return ERROR_FAIL;
328         }
329
330         hl_if.layout->api->custom_command(hl_if.handle, CMD_ARGV[0]);
331
332         return ERROR_OK;
333 }
334
335 static const struct command_registration hl_interface_command_handlers[] = {
336         {
337          .name = "hla_device_desc",
338          .handler = &hl_interface_handle_device_desc_command,
339          .mode = COMMAND_CONFIG,
340          .help = "set the device description of the adapter",
341          .usage = "description_string",
342          },
343         {
344          .name = "hla_serial",
345          .handler = &hl_interface_handle_serial_command,
346          .mode = COMMAND_CONFIG,
347          .help = "set the serial number of the adapter",
348          .usage = "serial_string",
349          },
350         {
351          .name = "hla_layout",
352          .handler = &hl_interface_handle_layout_command,
353          .mode = COMMAND_CONFIG,
354          .help = "set the layout of the adapter",
355          .usage = "layout_name",
356          },
357         {
358          .name = "hla_vid_pid",
359          .handler = &hl_interface_handle_vid_pid_command,
360          .mode = COMMAND_CONFIG,
361          .help = "the vendor and product ID of the adapter",
362          .usage = "(vid pid)*",
363          },
364         {
365          .name = "hla_stlink_backend",
366          .handler = &hl_interface_handle_stlink_backend_command,
367          .mode = COMMAND_CONFIG,
368          .help = "select which ST-Link backend to use",
369          .usage = "usb | tcp [port]",
370         },
371          {
372          .name = "hla_command",
373          .handler = &interface_handle_hla_command,
374          .mode = COMMAND_EXEC,
375          .help = "execute a custom adapter-specific command",
376          .usage = "<command>",
377          },
378         COMMAND_REGISTRATION_DONE
379 };
380
381 struct adapter_driver hl_adapter_driver = {
382         .name = "hla",
383         .transports = hl_transports,
384         .commands = hl_interface_command_handlers,
385
386         .init = hl_interface_init,
387         .quit = hl_interface_quit,
388         .reset = hl_interface_reset,
389         .speed = &hl_interface_speed,
390         .khz = &hl_interface_khz,
391         .speed_div = &hl_interface_speed_div,
392         .config_trace = &hl_interface_config_trace,
393         .poll_trace = &hl_interface_poll_trace,
394
395         /* no ops for HLA, targets hla_target and stm8 intercept them all */
396 };