0cc30654bb81a971ef0157dd1cb8945da4169725
[openocd.git] / src / jtag / arm-jtag-ew.c
1 /***************************************************************************
2 * Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com> *
3 * based on Dominic Rath's and Benedikt Sauter's usbprog.c *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "interface.h"
26 #include "commands.h"
27 #include <usb.h>
28
29
30 #define USB_VID 0x15ba
31 #define USB_PID 0x001e
32
33 #define ARMJTAGEW_EPT_BULK_OUT 0x01u
34 #define ARMJTAGEW_EPT_BULK_IN 0x82u
35
36 #define ARMJTAGEW_USB_TIMEOUT 2000
37
38 #define ARMJTAGEW_IN_BUFFER_SIZE (4*1024)
39 #define ARMJTAGEW_OUT_BUFFER_SIZE (4*1024)
40
41
42 /* USB command request codes. */
43 #define CMD_GET_VERSION 0x00
44 #define CMD_SELECT_DPIMPL 0x10
45 #define CMD_SET_TCK_FREQUENCY 0x11
46 #define CMD_GET_TCK_FREQUENCY 0x12
47 #define CMD_MEASURE_MAX_TCK_FREQ 0x15
48 #define CMD_MEASURE_RTCK_RESPONSE 0x16
49 #define CMD_TAP_SHIFT 0x17
50 #define CMD_SET_TAPHW_STATE 0x20
51 #define CMD_GET_TAPHW_STATE 0x21
52 #define CMD_TGPWR_SETUP 0x22
53
54 /* Global USB buffers */
55 static uint8_t usb_in_buffer[ARMJTAGEW_IN_BUFFER_SIZE];
56 static uint8_t usb_out_buffer[ARMJTAGEW_OUT_BUFFER_SIZE];
57
58 /* External interface functions */
59 static int armjtagew_execute_queue(void);
60 static int armjtagew_speed(int speed);
61 static int armjtagew_khz(int khz, int *jtag_speed);
62 static int armjtagew_register_commands(struct command_context_s *cmd_ctx);
63 static int armjtagew_init(void);
64 static int armjtagew_quit(void);
65
66 /* CLI command handler functions */
67 static int armjtagew_handle_armjtagew_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc);
68
69 /* Queue command functions */
70 static void armjtagew_end_state(tap_state_t state);
71 static void armjtagew_state_move(void);
72 static void armjtagew_path_move(int num_states, tap_state_t *path);
73 static void armjtagew_runtest(int num_cycles);
74 static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command);
75 static void armjtagew_reset(int trst, int srst);
76 //static void armjtagew_simple_command(uint8_t command);
77 static int armjtagew_get_status(void);
78
79 /* tap buffer functions */
80 static void armjtagew_tap_init(void);
81 static int armjtagew_tap_execute(void);
82 static void armjtagew_tap_ensure_space(int scans, int bits);
83 static void armjtagew_tap_append_step(int tms, int tdi);
84 static void armjtagew_tap_append_scan(int length, uint8_t *buffer, scan_command_t *command);
85
86 /* ARM-JTAG-EW lowlevel functions */
87 typedef struct armjtagew_jtag
88 {
89 struct usb_dev_handle* usb_handle;
90 } armjtagew_jtag_t;
91
92 static armjtagew_jtag_t *armjtagew_usb_open(void);
93 static void armjtagew_usb_close(armjtagew_jtag_t *armjtagew_jtag);
94 static int armjtagew_usb_message(armjtagew_jtag_t *armjtagew_jtag, int out_length, int in_length);
95 static int armjtagew_usb_write(armjtagew_jtag_t *armjtagew_jtag, int out_length);
96 static int armjtagew_usb_read(armjtagew_jtag_t *armjtagew_jtag, int exp_in_length);
97
98 /* helper functions */
99 static int armjtagew_get_version_info(void);
100
101 #ifdef _DEBUG_USB_COMMS_
102 static void armjtagew_debug_buffer(uint8_t *buffer, int length);
103 #endif
104
105 static armjtagew_jtag_t* armjtagew_jtag_handle;
106
107
108
109 /***************************************************************************/
110 /* External interface implementation */
111
112 jtag_interface_t armjtagew_interface =
113 {
114 .name = "arm-jtag-ew",
115 .execute_queue = armjtagew_execute_queue,
116 .speed = armjtagew_speed,
117 .khz = armjtagew_khz,
118 .register_commands = armjtagew_register_commands,
119 .init = armjtagew_init,
120 .quit = armjtagew_quit
121 };
122
123
124 static int armjtagew_execute_queue(void)
125 {
126 jtag_command_t *cmd = jtag_command_queue;
127 int scan_size;
128 enum scan_type type;
129 uint8_t *buffer;
130
131 while (cmd != NULL)
132 {
133 switch (cmd->type)
134 {
135 case JTAG_RUNTEST:
136 DEBUG_JTAG_IO( "runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, \
137 cmd->cmd.runtest->end_state);
138
139 armjtagew_end_state(cmd->cmd.runtest->end_state);
140 armjtagew_runtest(cmd->cmd.runtest->num_cycles);
141 break;
142
143 case JTAG_STATEMOVE:
144 DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
145
146 armjtagew_end_state(cmd->cmd.statemove->end_state);
147 armjtagew_state_move();
148 break;
149
150 case JTAG_PATHMOVE:
151 DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
152 cmd->cmd.pathmove->num_states, \
153 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
154
155 armjtagew_path_move(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
156 break;
157
158 case JTAG_SCAN:
159 DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
160
161 armjtagew_end_state(cmd->cmd.scan->end_state);
162
163 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
164 DEBUG_JTAG_IO("scan input, length = %d", scan_size);
165
166 #ifdef _DEBUG_USB_COMMS_
167 armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
168 #endif
169 type = jtag_scan_type(cmd->cmd.scan);
170 armjtagew_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size, cmd->cmd.scan);
171 break;
172
173 case JTAG_RESET:
174 DEBUG_JTAG_IO("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
175
176 armjtagew_tap_execute();
177
178 if (cmd->cmd.reset->trst == 1)
179 {
180 tap_set_state(TAP_RESET);
181 }
182 armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
183 break;
184
185 case JTAG_SLEEP:
186 DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
187 armjtagew_tap_execute();
188 jtag_sleep(cmd->cmd.sleep->us);
189 break;
190
191 default:
192 LOG_ERROR("BUG: unknown JTAG command type encountered");
193 exit(-1);
194 }
195 cmd = cmd->next;
196 }
197
198 return armjtagew_tap_execute();
199 }
200
201
202 /* Sets speed in kHz. */
203 static int armjtagew_speed(int speed)
204 {
205 int result;
206 int speed_real;
207
208
209 usb_out_buffer[0] = CMD_SET_TCK_FREQUENCY;
210 buf_set_u32(usb_out_buffer+1, 0, 32, speed);
211
212 result = armjtagew_usb_message(armjtagew_jtag_handle, 4, 4);
213
214 if (result < 0)
215 {
216 LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result);
217 return ERROR_JTAG_DEVICE_ERROR;
218 }
219
220 usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY;
221 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 4);
222 speed_real = (int)buf_get_u32(usb_in_buffer,0,32);
223 if (result < 0)
224 {
225 LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result);
226 return ERROR_JTAG_DEVICE_ERROR;
227 }
228 else
229 {
230 LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real);
231 }
232
233 return ERROR_OK;
234 }
235
236
237 static int armjtagew_khz(int khz, int *jtag_speed)
238 {
239 *jtag_speed = khz;
240
241 return ERROR_OK;
242 }
243
244 static int armjtagew_register_commands(struct command_context_s *cmd_ctx)
245 {
246 register_command(cmd_ctx, NULL, "armjtagew_info", armjtagew_handle_armjtagew_info_command, COMMAND_EXEC,
247 "query armjtagew info");
248 return ERROR_OK;
249 }
250
251 static int armjtagew_init(void)
252 {
253 int check_cnt;
254
255 armjtagew_jtag_handle = armjtagew_usb_open();
256
257 if (armjtagew_jtag_handle == 0)
258 {
259 LOG_ERROR("Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
260 return ERROR_JTAG_INIT_FAILED;
261 }
262
263 check_cnt = 0;
264 while (check_cnt < 3)
265 {
266 if (armjtagew_get_version_info() == ERROR_OK)
267 {
268 /* attempt to get status */
269 armjtagew_get_status();
270 break;
271 }
272
273 check_cnt++;
274 }
275
276 if (check_cnt == 3)
277 {
278 LOG_INFO("ARM-JTAG-EW initial read failed, don't worry");
279 }
280
281 LOG_INFO("ARM-JTAG-EW JTAG Interface ready");
282
283 armjtagew_reset(0, 0);
284 armjtagew_tap_init();
285
286 return ERROR_OK;
287 }
288
289 static int armjtagew_quit(void)
290 {
291 armjtagew_usb_close(armjtagew_jtag_handle);
292 return ERROR_OK;
293 }
294
295 /***************************************************************************/
296 /* Queue command implementations */
297
298 static void armjtagew_end_state(tap_state_t state)
299 {
300 if (tap_is_state_stable(state))
301 {
302 tap_set_end_state(state);
303 }
304 else
305 {
306 LOG_ERROR("BUG: %i is not a valid end state", state);
307 exit(-1);
308 }
309 }
310
311 /* Goes to the end state. */
312 static void armjtagew_state_move(void)
313 {
314 int i;
315 int tms = 0;
316 uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
317 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
318
319 for (i = 0; i < tms_count; i++)
320 {
321 tms = (tms_scan >> i) & 1;
322 armjtagew_tap_append_step(tms, 0);
323 }
324
325 tap_set_state(tap_get_end_state());
326 }
327
328 static void armjtagew_path_move(int num_states, tap_state_t *path)
329 {
330 int i;
331
332 for (i = 0; i < num_states; i++)
333 {
334 /*
335 * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode.
336 * Either handle that here, or update the documentation with examples
337 * how to fix that in the configuration files.
338 */
339 if (path[i] == tap_state_transition(tap_get_state(), false))
340 {
341 armjtagew_tap_append_step(0, 0);
342 }
343 else if (path[i] == tap_state_transition(tap_get_state(), true))
344 {
345 armjtagew_tap_append_step(1, 0);
346 }
347 else
348 {
349 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(path[i]));
350 exit(-1);
351 }
352
353 tap_set_state(path[i]);
354 }
355
356 tap_set_end_state(tap_get_state());
357 }
358
359 static void armjtagew_runtest(int num_cycles)
360 {
361 int i;
362
363 tap_state_t saved_end_state = tap_get_end_state();
364
365 /* only do a state_move when we're not already in IDLE */
366 if (tap_get_state() != TAP_IDLE)
367 {
368 armjtagew_end_state(TAP_IDLE);
369 armjtagew_state_move();
370 }
371
372 /* execute num_cycles */
373 for (i = 0; i < num_cycles; i++)
374 {
375 armjtagew_tap_append_step(0, 0);
376 }
377
378 /* finish in end_state */
379 armjtagew_end_state(saved_end_state);
380 if (tap_get_state() != tap_get_end_state())
381 {
382 armjtagew_state_move();
383 }
384 }
385
386 static void armjtagew_scan(bool ir_scan, enum scan_type type, uint8_t *buffer, int scan_size, scan_command_t *command)
387 {
388 tap_state_t saved_end_state;
389
390 armjtagew_tap_ensure_space(1, scan_size + 8);
391
392 saved_end_state = tap_get_end_state();
393
394 /* Move to appropriate scan state */
395 armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
396
397 armjtagew_state_move();
398 armjtagew_end_state(saved_end_state);
399
400 /* Scan */
401 armjtagew_tap_append_scan(scan_size, buffer, command);
402
403 /* We are in Exit1, go to Pause */
404 armjtagew_tap_append_step(0, 0);
405
406 tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
407
408 if (tap_get_state() != tap_get_end_state())
409 {
410 armjtagew_state_move();
411 }
412 }
413
414 static void armjtagew_reset(int trst, int srst)
415 {
416 const uint8_t trst_mask = (1u<<5);
417 const uint8_t srst_mask = (1u<<6);
418 uint8_t val = 0;
419 uint8_t outp_en = 0;
420 uint8_t change_mask = 0;
421 int result;
422
423 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
424
425 if (srst == 0)
426 {
427 val |= srst_mask;
428 outp_en &= ~srst_mask; /* tristate */
429 change_mask |= srst_mask;
430 }
431 else if (srst == 1)
432 {
433 val &= ~srst_mask;
434 outp_en |= srst_mask;
435 change_mask |= srst_mask;
436 }
437
438 if (trst == 0)
439 {
440 val |= trst_mask;
441 outp_en &= ~trst_mask; /* tristate */
442 change_mask |= trst_mask;
443 }
444 else if (trst == 1)
445 {
446 val &= ~trst_mask;
447 outp_en |= trst_mask;
448 change_mask |= trst_mask;
449 }
450
451 usb_out_buffer[0] = CMD_SET_TAPHW_STATE;
452 usb_out_buffer[1] = val;
453 usb_out_buffer[2] = outp_en;
454 usb_out_buffer[3] = change_mask;
455 result = armjtagew_usb_write(armjtagew_jtag_handle, 4);
456 if (result != 4)
457 {
458 LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result);
459 }
460 }
461
462
463 static int armjtagew_get_status(void)
464 {
465 int result;
466
467 usb_out_buffer[0] = CMD_GET_TAPHW_STATE;
468 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 12);
469
470 if (result == 0)
471 {
472 unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16);
473 LOG_INFO("U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s\n",
474 (int)(buf_get_u32(usb_in_buffer + 0, 0, 16)),
475 (int)(buf_get_u32(usb_in_buffer + 2, 0, 16)),
476 (int)(buf_get_u32(usb_in_buffer + 4, 0, 16)),
477 (int)(buf_get_u32(usb_in_buffer + 6, 0, 16)),
478 usb_in_buffer[9],
479 usb_in_buffer[11] ? "OVERCURRENT" : "OK",
480 usb_in_buffer[10] ? "enabled" : "disabled");
481
482 if (u_tg < 1500)
483 {
484 LOG_ERROR("Vref too low. Check Target Power\n");
485 }
486 }
487 else
488 {
489 LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)\n", result);
490 }
491
492 return ERROR_OK;
493 }
494
495 static int armjtagew_get_version_info(void)
496 {
497 int result;
498 char sn[16];
499 char auxinfo[257];
500
501 /* query hardware version */
502 usb_out_buffer[0] = CMD_GET_VERSION;
503 result = armjtagew_usb_message(armjtagew_jtag_handle, 1, 4+15+256);
504
505 if (result != 0)
506 {
507 LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)\n", result);
508 return ERROR_JTAG_DEVICE_ERROR;
509 }
510
511
512 memcpy(sn, usb_in_buffer+4, 15);
513 sn[15] = '\0';
514 memcpy(auxinfo, usb_in_buffer+4+15, 256);
515 auxinfo[256] = '\0';
516
517 LOG_INFO("ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \
518 usb_in_buffer[1], usb_in_buffer[0], \
519 isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \
520 sn, auxinfo);
521 return ERROR_OK;
522 }
523
524 static int armjtagew_handle_armjtagew_info_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
525 {
526 if (armjtagew_get_version_info() == ERROR_OK)
527 {
528 /* attempt to get status */
529 armjtagew_get_status();
530 }
531
532 return ERROR_OK;
533 }
534
535 /***************************************************************************/
536 /* ARM-JTAG-EW tap functions */
537
538 /* 2048 is the max value we can use here */
539 #define ARMJTAGEW_TAP_BUFFER_SIZE 2048
540
541 static int tap_length;
542 static uint8_t tms_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
543 static uint8_t tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
544 static uint8_t tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
545
546 typedef struct
547 {
548 int first; /* First bit position in tdo_buffer to read */
549 int length; /* Number of bits to read */
550 scan_command_t *command; /* Corresponding scan command */
551 uint8_t *buffer;
552 } pending_scan_result_t;
553
554 #define MAX_PENDING_SCAN_RESULTS 256
555
556 static int pending_scan_results_length;
557 static pending_scan_result_t pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
558
559 static int last_tms;
560
561 static void armjtagew_tap_init(void)
562 {
563 tap_length = 0;
564 pending_scan_results_length = 0;
565 }
566
567 static void armjtagew_tap_ensure_space(int scans, int bits)
568 {
569 int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
570 int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length;
571
572 if (scans > available_scans || bits > available_bits)
573 {
574 armjtagew_tap_execute();
575 }
576 }
577
578 static void armjtagew_tap_append_step(int tms, int tdi)
579 {
580 last_tms = tms;
581 int index = tap_length / 8;
582
583 if (index < ARMJTAGEW_TAP_BUFFER_SIZE)
584 {
585 int bit_index = tap_length % 8;
586 uint8_t bit = 1 << bit_index;
587
588 if (tms)
589 {
590 tms_buffer[index] |= bit;
591 }
592 else
593 {
594 tms_buffer[index] &= ~bit;
595 }
596
597 if (tdi)
598 {
599 tdi_buffer[index] |= bit;
600 }
601 else
602 {
603 tdi_buffer[index] &= ~bit;
604 }
605
606 tap_length++;
607 }
608 else
609 {
610 LOG_ERROR("armjtagew_tap_append_step, overflow");
611 }
612 }
613
614 void armjtagew_tap_append_scan(int length, uint8_t *buffer, scan_command_t *command)
615 {
616 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[pending_scan_results_length];
617 int i;
618
619 pending_scan_result->first = tap_length;
620 pending_scan_result->length = length;
621 pending_scan_result->command = command;
622 pending_scan_result->buffer = buffer;
623
624 for (i = 0; i < length; i++)
625 {
626 armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
627 }
628 pending_scan_results_length++;
629 }
630
631 /* Pad and send a tap sequence to the device, and receive the answer.
632 * For the purpose of padding we assume that we are in idle or pause state. */
633 static int armjtagew_tap_execute(void)
634 {
635 int byte_length;
636 int tms_offset;
637 int tdi_offset;
638 int i;
639 int result;
640
641 if (tap_length > 0)
642 {
643 /* Pad last byte so that tap_length is divisible by 8 */
644 while (tap_length % 8 != 0)
645 {
646 /* More of the last TMS value keeps us in the same state,
647 * analogous to free-running JTAG interfaces. */
648 armjtagew_tap_append_step(last_tms, 0);
649 }
650
651 byte_length = tap_length / 8;
652
653 usb_out_buffer[0] = CMD_TAP_SHIFT;
654 buf_set_u32(usb_out_buffer+1, 0, 16, byte_length);
655
656 tms_offset = 3;
657 for (i = 0; i < byte_length; i++)
658 {
659 usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i],8);
660 }
661
662 tdi_offset = tms_offset + byte_length;
663 for (i = 0; i < byte_length; i++)
664 {
665 usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i],8);
666 }
667
668 result = armjtagew_usb_message(armjtagew_jtag_handle, 3 + 2 * byte_length, byte_length + 4);
669
670 if (result == 0)
671 {
672 int stat;
673
674 stat = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32);
675 if (stat) {
676 LOG_ERROR("armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command", stat);
677 return ERROR_JTAG_QUEUE_FAILED;
678 }
679
680 for (i = 0; i < byte_length; i++)
681 {
682 tdo_buffer[i] = flip_u32(usb_in_buffer[i],8);
683 }
684
685 for (i = 0; i < pending_scan_results_length; i++)
686 {
687 pending_scan_result_t *pending_scan_result = &pending_scan_results_buffer[i];
688 uint8_t *buffer = pending_scan_result->buffer;
689 int length = pending_scan_result->length;
690 int first = pending_scan_result->first;
691 scan_command_t *command = pending_scan_result->command;
692
693 /* Copy to buffer */
694 buf_set_buf(tdo_buffer, first, buffer, 0, length);
695
696 DEBUG_JTAG_IO("pending scan result, length = %d", length);
697
698 #ifdef _DEBUG_USB_COMMS_
699 armjtagew_debug_buffer(buffer, byte_length);
700 #endif
701
702 if (jtag_read_buffer(buffer, command) != ERROR_OK)
703 {
704 armjtagew_tap_init();
705 return ERROR_JTAG_QUEUE_FAILED;
706 }
707
708 if (pending_scan_result->buffer != NULL)
709 {
710 free(pending_scan_result->buffer);
711 }
712 }
713 }
714 else
715 {
716 LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d", result, byte_length);
717 return ERROR_JTAG_QUEUE_FAILED;
718 }
719
720 armjtagew_tap_init();
721 }
722
723 return ERROR_OK;
724 }
725
726 /*****************************************************************************/
727 /* JLink USB low-level functions */
728
729 static armjtagew_jtag_t* armjtagew_usb_open()
730 {
731 struct usb_bus *busses;
732 struct usb_bus *bus;
733 struct usb_device *dev;
734
735 armjtagew_jtag_t *result;
736
737 result = (armjtagew_jtag_t*) malloc(sizeof(armjtagew_jtag_t));
738
739 usb_init();
740 usb_find_busses();
741 usb_find_devices();
742
743 busses = usb_get_busses();
744
745 /* find armjtagew_jtag device in usb bus */
746
747 for (bus = busses; bus; bus = bus->next)
748 {
749 for (dev = bus->devices; dev; dev = dev->next)
750 {
751 if ((dev->descriptor.idVendor == USB_VID) && (dev->descriptor.idProduct == USB_PID))
752 {
753 result->usb_handle = usb_open(dev);
754
755 #if 0
756 /* usb_set_configuration required under win32 */
757 usb_set_configuration(result->usb_handle, dev->config[0].bConfigurationValue);
758 #endif
759 usb_claim_interface(result->usb_handle, 0);
760
761 #if 0
762 /*
763 * This makes problems under Mac OS X. And is not needed
764 * under Windows. Hopefully this will not break a linux build
765 */
766 usb_set_altinterface(result->usb_handle, 0);
767 #endif
768 return result;
769 }
770 }
771 }
772
773 free(result);
774 return NULL;
775 }
776
777 static void armjtagew_usb_close(armjtagew_jtag_t *armjtagew_jtag)
778 {
779 usb_close(armjtagew_jtag->usb_handle);
780 free(armjtagew_jtag);
781 }
782
783 /* Send a message and receive the reply. */
784 static int armjtagew_usb_message(armjtagew_jtag_t *armjtagew_jtag, int out_length, int in_length)
785 {
786 int result;
787
788 result = armjtagew_usb_write(armjtagew_jtag, out_length);
789 if (result == out_length)
790 {
791 result = armjtagew_usb_read(armjtagew_jtag, in_length);
792 if (result != in_length)
793 {
794 LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)", in_length, result);
795 return -1;
796 }
797 }
798 else
799 {
800 LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
801 return -1;
802 }
803 return 0;
804 }
805
806 /* Write data from out_buffer to USB. */
807 static int armjtagew_usb_write(armjtagew_jtag_t *armjtagew_jtag, int out_length)
808 {
809 int result;
810
811 if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE)
812 {
813 LOG_ERROR("armjtagew_jtag_write illegal out_length=%d (max=%d)", out_length, ARMJTAGEW_OUT_BUFFER_SIZE);
814 return -1;
815 }
816
817 result = usb_bulk_write(armjtagew_jtag->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \
818 (char*)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT);
819
820 DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
821
822 #ifdef _DEBUG_USB_COMMS_
823 armjtagew_debug_buffer(usb_out_buffer, out_length);
824 #endif
825 return result;
826 }
827
828 /* Read data from USB into in_buffer. */
829 static int armjtagew_usb_read(armjtagew_jtag_t *armjtagew_jtag, int exp_in_length)
830 {
831 int result = usb_bulk_read(armjtagew_jtag->usb_handle, ARMJTAGEW_EPT_BULK_IN, \
832 (char*)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT);
833
834 DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result);
835
836 #ifdef _DEBUG_USB_COMMS_
837 armjtagew_debug_buffer(usb_in_buffer, result);
838 #endif
839 return result;
840 }
841
842
843 #ifdef _DEBUG_USB_COMMS_
844 #define BYTES_PER_LINE 16
845
846 static void armjtagew_debug_buffer(uint8_t *buffer, int length)
847 {
848 char line[81];
849 char s[4];
850 int i;
851 int j;
852
853 for (i = 0; i < length; i += BYTES_PER_LINE)
854 {
855 snprintf(line, 5, "%04x", i);
856 for (j = i; j < i + BYTES_PER_LINE && j < length; j++)
857 {
858 snprintf(s, 4, " %02x", buffer[j]);
859 strcat(line, s);
860 }
861 LOG_DEBUG("%s", line);
862 }
863 }
864 #endif
865