You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

397 lines
10 KiB

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