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