stlink: fix connect under reset issues
[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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, 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, 0, false}, 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 == expected)) {
88                         found = 1;
89                         break;
90                 }
91         }
92
93         if (found == 0) {
94                 LOG_ERROR("hl_interface_init_target: target not found: idcode: 0x%08x",
95                                 t->tap->idcode);
96                 return ERROR_FAIL;
97         }
98
99         t->tap->priv = &hl_if;
100         t->tap->hasidcode = 1;
101
102         return ERROR_OK;
103 }
104
105 static int hl_interface_init(void)
106 {
107         LOG_DEBUG("hl_interface_init");
108
109         /* here we can initialize the layout */
110         return hl_layout_init(&hl_if);
111 }
112
113 static int hl_interface_quit(void)
114 {
115         LOG_DEBUG("hl_interface_quit");
116
117         return ERROR_OK;
118 }
119
120 static int hl_interface_execute_queue(void)
121 {
122         LOG_DEBUG("hl_interface_execute_queue: ignored");
123
124         return ERROR_OK;
125 }
126
127 int hl_interface_init_reset(void)
128 {
129         /* incase the adapter has not already handled asserting srst
130          * we will attempt it again */
131         if (hl_if.param.connect_under_reset) {
132                 jtag_add_reset(0, 1);
133                 hl_if.layout->api->assert_srst(hl_if.fd, 0);
134         }
135
136         return ERROR_OK;
137 }
138
139 COMMAND_HANDLER(hl_interface_handle_device_desc_command)
140 {
141         LOG_DEBUG("hl_interface_handle_device_desc_command");
142
143         if (CMD_ARGC == 1) {
144                 hl_if.param.device_desc = strdup(CMD_ARGV[0]);
145         } else {
146                 LOG_ERROR("expected exactly one argument to hl_device_desc <description>");
147         }
148
149         return ERROR_OK;
150 }
151
152 COMMAND_HANDLER(hl_interface_handle_serial_command)
153 {
154         LOG_DEBUG("hl_interface_handle_serial_command");
155
156         if (CMD_ARGC == 1) {
157                 hl_if.param.serial = strdup(CMD_ARGV[0]);
158         } else {
159                 LOG_ERROR("expected exactly one argument to hl_serial <serial-number>");
160         }
161
162         return ERROR_OK;
163 }
164
165 COMMAND_HANDLER(hl_interface_handle_layout_command)
166 {
167         LOG_DEBUG("hl_interface_handle_layout_command");
168
169         if (CMD_ARGC != 1) {
170                 LOG_ERROR("Need exactly one argument to stlink_layout");
171                 return ERROR_COMMAND_SYNTAX_ERROR;
172         }
173
174         if (hl_if.layout) {
175                 LOG_ERROR("already specified hl_layout %s",
176                                 hl_if.layout->name);
177                 return (strcmp(hl_if.layout->name, CMD_ARGV[0]) != 0)
178                     ? ERROR_FAIL : ERROR_OK;
179         }
180
181         for (const struct hl_layout *l = hl_layout_get_list(); l->name;
182              l++) {
183                 if (strcmp(l->name, CMD_ARGV[0]) == 0) {
184                         hl_if.layout = l;
185                         return ERROR_OK;
186                 }
187         }
188
189         LOG_ERROR("No adapter layout '%s' found", CMD_ARGV[0]);
190         return ERROR_FAIL;
191 }
192
193 COMMAND_HANDLER(hl_interface_handle_vid_pid_command)
194 {
195         LOG_DEBUG("hl_interface_handle_vid_pid_command");
196
197         if (CMD_ARGC != 2) {
198                 LOG_WARNING("ignoring extra IDs in hl_vid_pid (maximum is 1 pair)");
199                 return ERROR_COMMAND_SYNTAX_ERROR;
200         }
201
202         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], hl_if.param.vid);
203         COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], hl_if.param.pid);
204
205         return ERROR_OK;
206 }
207
208 COMMAND_HANDLER(stlink_interface_handle_api_command)
209 {
210         if (CMD_ARGC != 1)
211                 return ERROR_COMMAND_SYNTAX_ERROR;
212
213         unsigned new_api;
214         COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], new_api);
215         if ((new_api == 0) || (new_api > 2))
216                 return ERROR_COMMAND_SYNTAX_ERROR;
217
218         hl_if.param.api = new_api;
219
220         return ERROR_OK;
221 }
222
223 static const struct command_registration hl_interface_command_handlers[] = {
224         {
225          .name = "hla_device_desc",
226          .handler = &hl_interface_handle_device_desc_command,
227          .mode = COMMAND_CONFIG,
228          .help = "set the a device description of the adapter",
229          .usage = "description_string",
230          },
231         {
232          .name = "hla_serial",
233          .handler = &hl_interface_handle_serial_command,
234          .mode = COMMAND_CONFIG,
235          .help = "set the serial number of the adapter",
236          .usage = "serial_string",
237          },
238         {
239          .name = "hla_layout",
240          .handler = &hl_interface_handle_layout_command,
241          .mode = COMMAND_CONFIG,
242          .help = "set the layout of the adapter",
243          .usage = "layout_name",
244          },
245         {
246          .name = "hla_vid_pid",
247          .handler = &hl_interface_handle_vid_pid_command,
248          .mode = COMMAND_CONFIG,
249          .help = "the vendor and product ID of the adapter",
250          .usage = "(vid pid)* ",
251          },
252          {
253          .name = "stlink_api",
254          .handler = &stlink_interface_handle_api_command,
255          .mode = COMMAND_CONFIG,
256          .help = "set the desired stlink api level",
257          .usage = "api version 1 or 2",
258          },
259         COMMAND_REGISTRATION_DONE
260 };
261
262 struct jtag_interface hl_interface = {
263         .name = "hla",
264         .supported = 0,
265         .commands = hl_interface_command_handlers,
266         .transports = hl_transports,
267         .init = hl_interface_init,
268         .quit = hl_interface_quit,
269         .execute_queue = hl_interface_execute_queue,
270 };