Audit and eliminate redundant #include directives from src/jtag.
[openocd.git] / src / jtag / usbprog.c
1 /***************************************************************************
2 * Copyright (C) 2007 by Benedikt Sauter *
3 * sauter@ixbat.de *
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 /*
22 * This file is based on Dominic Rath's amt_jtagaccel.c.
23 *
24 * usbprog is a free programming adapter. You can easily install
25 * different firmware versions from an "online pool" over USB.
26 * The adapter can be used for programming and debugging AVR and ARM
27 * processors, as USB to RS232 converter, as JTAG interface or as
28 * simple I/O interface (5 lines).
29 *
30 * http://www.embedded-projects.net/usbprog
31 */
32
33 #ifdef HAVE_CONFIG_H
34 #include "config.h"
35 #endif
36
37 #include "jtag.h"
38
39 #include <usb.h>
40
41
42 #define VID 0x1781
43 #define PID 0x0c63
44
45 /* Pins at usbprog */
46 #define TDO_BIT 0
47 #define TDI_BIT 3
48 #define TCK_BIT 2
49 #define TMS_BIT 1
50
51 static int usbprog_execute_queue(void);
52 static int usbprog_speed(int speed);
53 static int usbprog_register_commands(struct command_context_s *cmd_ctx);
54 static int usbprog_init(void);
55 static int usbprog_quit(void);
56
57 static void usbprog_end_state(tap_state_t state);
58 static void usbprog_state_move(void);
59 static void usbprog_path_move(pathmove_command_t *cmd);
60 static void usbprog_runtest(int num_cycles);
61 static void usbprog_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size);
62
63 jtag_interface_t usbprog_interface =
64 {
65 .name = "usbprog",
66 .execute_queue = usbprog_execute_queue,
67 .speed = usbprog_speed,
68 .register_commands = usbprog_register_commands,
69 .init = usbprog_init,
70 .quit = usbprog_quit
71 };
72
73 #define UNKOWN_COMMAND 0x00
74 #define PORT_DIRECTION 0x01
75 #define PORT_SET 0x02
76 #define PORT_GET 0x03
77 #define PORT_SETBIT 0x04
78 #define PORT_GETBIT 0x05
79 #define WRITE_TDI 0x06
80 #define READ_TDO 0x07
81 #define WRITE_AND_READ 0x08
82 #define WRITE_TMS 0x09
83 #define WRITE_TMS_CHAIN 0x0A
84
85 struct usbprog_jtag
86 {
87 struct usb_dev_handle* usb_handle;
88 };
89
90 static struct usbprog_jtag * usbprog_jtag_handle;
91
92 static struct usbprog_jtag* usbprog_jtag_open(void);
93 //static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag);
94 static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag);
95 static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen);
96
97 static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
98 static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
99 static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
100 static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan);
101
102 static char tms_chain[64];
103 static int tms_chain_index;
104
105 static void usbprog_jtag_tms_collect(char tms_scan);
106 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag);
107
108 static void usbprog_write(int tck, int tms, int tdi);
109 static void usbprog_reset(int trst, int srst);
110
111 static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction);
112 static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value);
113 //static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag);
114 static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value);
115 //static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit);
116
117 static int usbprog_speed(int speed)
118 {
119 return ERROR_OK;
120 }
121
122 static int usbprog_register_commands(struct command_context_s *cmd_ctx)
123 {
124 return ERROR_OK;
125 }
126
127 static int usbprog_execute_queue(void)
128 {
129 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
130 int scan_size;
131 enum scan_type type;
132 u8 *buffer;
133
134 while (cmd)
135 {
136 switch (cmd->type)
137 {
138 case JTAG_END_STATE:
139 #ifdef _DEBUG_JTAG_IO_
140 LOG_DEBUG("end_state: %i", cmd->cmd.end_state->end_state);
141 #endif
142 if (cmd->cmd.end_state->end_state != TAP_INVALID)
143 usbprog_end_state(cmd->cmd.end_state->end_state);
144 break;
145 case JTAG_RESET:
146 #ifdef _DEBUG_JTAG_IO_
147 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
148 #endif
149 if (cmd->cmd.reset->trst == 1)
150 {
151 tap_set_state(TAP_RESET);
152 }
153 usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
154 break;
155 case JTAG_RUNTEST:
156 #ifdef _DEBUG_JTAG_IO_
157 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
158 #endif
159 if (cmd->cmd.runtest->end_state != TAP_INVALID)
160 usbprog_end_state(cmd->cmd.runtest->end_state);
161 usbprog_runtest(cmd->cmd.runtest->num_cycles);
162 break;
163 case JTAG_STATEMOVE:
164 #ifdef _DEBUG_JTAG_IO_
165 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
166 #endif
167 if (cmd->cmd.statemove->end_state != TAP_INVALID)
168 usbprog_end_state(cmd->cmd.statemove->end_state);
169 usbprog_state_move();
170 break;
171 case JTAG_PATHMOVE:
172 #ifdef _DEBUG_JTAG_IO_
173 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
174 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
175 #endif
176 usbprog_path_move(cmd->cmd.pathmove);
177 break;
178 case JTAG_SCAN:
179 #ifdef _DEBUG_JTAG_IO_
180 LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
181 #endif
182 if (cmd->cmd.scan->end_state != TAP_INVALID)
183 usbprog_end_state(cmd->cmd.scan->end_state);
184 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
185 type = jtag_scan_type(cmd->cmd.scan);
186 usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
187 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
188 return ERROR_JTAG_QUEUE_FAILED;
189 if (buffer)
190 free(buffer);
191 break;
192 case JTAG_SLEEP:
193 #ifdef _DEBUG_JTAG_IO_
194 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
195 #endif
196 jtag_sleep(cmd->cmd.sleep->us);
197 break;
198 default:
199 LOG_ERROR("BUG: unknown JTAG command type encountered");
200 exit(-1);
201 }
202
203 cmd = cmd->next;
204 }
205
206 return ERROR_OK;
207 }
208
209 static int usbprog_init(void)
210 {
211 usbprog_jtag_handle = usbprog_jtag_open();
212
213 tms_chain_index = 0;
214 if (usbprog_jtag_handle == 0)
215 {
216 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
217 return ERROR_JTAG_INIT_FAILED;
218 }
219
220 LOG_INFO("USB JTAG Interface ready!");
221
222 usbprog_jtag_init(usbprog_jtag_handle);
223 usbprog_reset(0, 0);
224 usbprog_write(0, 0, 0);
225
226 return ERROR_OK;
227 }
228
229 static int usbprog_quit(void)
230 {
231 return ERROR_OK;
232 }
233
234 /*************** jtag execute commands **********************/
235 static void usbprog_end_state(tap_state_t state)
236 {
237 if (tap_is_state_stable(state))
238 tap_set_end_state(state);
239 else
240 {
241 LOG_ERROR("BUG: %i is not a valid end state", state);
242 exit(-1);
243 }
244 }
245
246 static void usbprog_state_move(void)
247 {
248 int i = 0, tms = 0;
249 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
250
251 usbprog_jtag_write_tms(usbprog_jtag_handle, (char)tms_scan);
252 for (i = 0; i < 7; i++)
253 {
254 tms = (tms_scan >> i) & 1;
255 }
256
257 tap_set_state(tap_get_end_state());
258 }
259
260 static void usbprog_path_move(pathmove_command_t *cmd)
261 {
262 int num_states = cmd->num_states;
263 int state_count;
264
265 /* There may be queued transitions, and before following a specified
266 path, we must flush those queued transitions */
267 usbprog_jtag_tms_send(usbprog_jtag_handle);
268
269 state_count = 0;
270 while (num_states)
271 {
272 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
273 {
274 /* LOG_INFO("1"); */
275 usbprog_write(0, 0, 0);
276 usbprog_write(1, 0, 0);
277 }
278 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
279 {
280 /* LOG_INFO("2"); */
281 usbprog_write(0, 1, 0);
282 usbprog_write(1, 1, 0);
283 }
284 else
285 {
286 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
287 exit(-1);
288 }
289
290 tap_set_state(cmd->path[state_count]);
291 state_count++;
292 num_states--;
293 }
294
295 tap_set_end_state(tap_get_state());
296 }
297
298 static void usbprog_runtest(int num_cycles)
299 {
300 int i;
301
302 /* only do a state_move when we're not already in IDLE */
303 if (tap_get_state() != TAP_IDLE)
304 {
305 usbprog_end_state(TAP_IDLE);
306 usbprog_state_move();
307 }
308
309 /* execute num_cycles */
310 if (num_cycles > 0)
311 {
312 usbprog_jtag_tms_send(usbprog_jtag_handle);
313 usbprog_write(0, 0, 0);
314 }
315 else
316 {
317 usbprog_jtag_tms_send(usbprog_jtag_handle);
318 /* LOG_INFO("NUM CYCLES %i",num_cycles); */
319 }
320
321 for (i = 0; i < num_cycles; i++)
322 {
323 usbprog_write(1, 0, 0);
324 usbprog_write(0, 0, 0);
325 }
326
327 #ifdef _DEBUG_JTAG_IO_
328 LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()));
329 #endif
330
331 /* finish in end_state */
332 /*
333 usbprog_end_state(saved_end_state);
334 if (tap_get_state() != tap_get_end_state())
335 usbprog_state_move();
336 */
337 }
338
339 static void usbprog_scan(int ir_scan, enum scan_type type, u8 *buffer, int scan_size)
340 {
341 tap_state_t saved_end_state = tap_get_end_state();
342
343 if (ir_scan)
344 usbprog_end_state(TAP_IRSHIFT);
345 else
346 usbprog_end_state(TAP_DRSHIFT);
347
348 /* Only move if we're not already there */
349 if (tap_get_state() != tap_get_end_state())
350 usbprog_state_move();
351
352 usbprog_end_state(saved_end_state);
353
354 usbprog_jtag_tms_send(usbprog_jtag_handle);
355
356 void (*f)(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
357 switch (type) {
358 case SCAN_OUT: f = &usbprog_jtag_write_tdi; break;
359 case SCAN_IN: f = &usbprog_jtag_read_tdo; break;
360 case SCAN_IO: f = &usbprog_jtag_write_and_read; break;
361 default:
362 LOG_ERROR("unknown scan type: %i", type);
363 exit(-1);
364 }
365 f(usbprog_jtag_handle, (char *)buffer, scan_size);
366
367 /* The adapter does the transition to PAUSE internally */
368 if (ir_scan)
369 tap_set_state(TAP_IRPAUSE);
370 else
371 tap_set_state(TAP_DRPAUSE);
372
373 if (tap_get_state() != tap_get_end_state())
374 usbprog_state_move();
375 }
376
377 /*************** jtag wrapper functions *********************/
378
379 static void usbprog_write(int tck, int tms, int tdi)
380 {
381 unsigned char output_value=0x00;
382
383 if (tms)
384 output_value |= (1<<TMS_BIT);
385 if (tdi)
386 output_value |= (1<<TDI_BIT);
387 if (tck)
388 output_value |= (1<<TCK_BIT);
389
390 usbprog_jtag_write_slice(usbprog_jtag_handle,output_value);
391 }
392
393 /* (1) assert or (0) deassert reset lines */
394 static void usbprog_reset(int trst, int srst)
395 {
396 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
397
398 if (trst)
399 usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 0);
400 else
401 usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 1);
402
403 if (srst)
404 usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 0);
405 else
406 usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 1);
407 }
408
409 /*************** jtag lowlevel functions ********************/
410
411 struct usb_bus *busses;
412
413 struct usbprog_jtag* usbprog_jtag_open(void)
414 {
415 struct usb_bus *bus;
416 struct usb_device *dev;
417
418 struct usbprog_jtag *tmp;
419
420 tmp = (struct usbprog_jtag*)malloc(sizeof(struct usbprog_jtag));
421
422 usb_set_debug(10);
423 usb_init();
424 usb_find_busses();
425 usb_find_devices();
426
427 busses = usb_get_busses();
428
429 /* find usbprog_jtag device in usb bus */
430
431 for (bus = busses; bus; bus = bus->next)
432 {
433 for (dev = bus->devices; dev; dev = dev->next)
434 {
435 /* condition for sucessfully hit (too bad, I only check the vendor id)*/
436 if (dev->descriptor.idVendor == VID && dev->descriptor.idProduct == PID)
437 {
438 tmp->usb_handle = usb_open(dev);
439 usb_set_configuration(tmp->usb_handle, 1);
440 usb_claim_interface(tmp->usb_handle, 0);
441 usb_set_altinterface(tmp->usb_handle, 0);
442 return tmp;
443 }
444 }
445 }
446 return 0;
447 }
448
449 #if 0
450 static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag)
451 {
452 usb_close(usbprog_jtag->usb_handle);
453 free(usbprog_jtag);
454 }
455 #endif
456
457 static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen)
458 {
459 int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg,msglen, 100);
460 if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) || \
461 (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9))
462 return 1;
463 if (res == msglen)
464 {
465 /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
466 res = usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100);
467 if (res > 0)
468 return (unsigned char)msg[1];
469 else
470 return -1;
471 }
472 else
473 return -1;
474 return 0;
475 }
476
477 static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag)
478 {
479 usbprog_jtag_set_direction(usbprog_jtag, 0xFE);
480 }
481
482 static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
483 {
484 char tmp[64]; /* fastes packet size for usb controller */
485 int send_bits, bufindex = 0, fillindex = 0, i, loops;
486
487 char swap;
488 /* 61 byte can be transfered (488 bit) */
489
490 while (size > 0)
491 {
492 if (size > 488)
493 {
494 send_bits = 488;
495 size = size - 488;
496 loops = 61;
497 }
498 else
499 {
500 send_bits = size;
501 loops = size / 8;
502 loops++;
503 size = 0;
504 }
505 tmp[0] = WRITE_AND_READ;
506 tmp[1] = (char)(send_bits >> 8); /* high */
507 tmp[2] = (char)(send_bits); /* low */
508 i = 0;
509
510 for (i = 0; i < loops; i++)
511 {
512 tmp[3 + i] = buffer[bufindex];
513 bufindex++;
514 }
515
516 if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64)
517 {
518 /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
519 usleep(1);
520 int timeout = 0;
521 while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1)
522 {
523 timeout++;
524 if (timeout > 10)
525 break;
526 }
527
528 for (i = 0; i < loops; i++)
529 {
530 swap = tmp[3 + i];
531 buffer[fillindex++] = swap;
532 }
533 }
534 }
535 }
536
537 static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
538 {
539 char tmp[64]; /* fastes packet size for usb controller */
540 int send_bits, fillindex = 0, i, loops;
541
542 char swap;
543 /* 61 byte can be transfered (488 bit) */
544
545 while (size > 0)
546 {
547 if (size > 488)
548 {
549 send_bits = 488;
550 size = size - 488;
551 loops = 61;
552 }
553 else
554 {
555 send_bits = size;
556 loops = size / 8;
557 loops++;
558 size = 0;
559 }
560 tmp[0] = WRITE_AND_READ;
561 tmp[1] = (char)(send_bits >> 8); /* high */
562 tmp[2] = (char)(send_bits); /* low */
563
564 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 3, 1000);
565
566 /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
567 int timeout = 0;
568 usleep(1);
569 while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1)
570 {
571 timeout++;
572 if (timeout > 10)
573 break;
574 }
575
576 for (i = 0; i < loops; i++)
577 {
578 swap = tmp[3 + i];
579 buffer[fillindex++] = swap;
580 }
581 }
582 }
583
584 static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
585 {
586 char tmp[64]; /* fastes packet size for usb controller */
587 int send_bits, bufindex = 0, i, loops;
588
589 /* 61 byte can be transfered (488 bit) */
590 while (size > 0)
591 {
592 if (size > 488)
593 {
594 send_bits = 488;
595 size = size - 488;
596 loops = 61;
597 }
598 else
599 {
600 send_bits = size;
601 loops = size/8;
602 /* if(loops==0) */
603 loops++;
604 size = 0;
605 }
606 tmp[0] = WRITE_TDI;
607 tmp[1] = (char)(send_bits >> 8); /* high */
608 tmp[2] = (char)(send_bits); /* low */
609 i = 0;
610
611 for (i = 0; i < loops; i++)
612 {
613 tmp[3 + i] = buffer[bufindex];
614 bufindex++;
615 }
616 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000);
617 }
618 }
619
620 static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan)
621 {
622 usbprog_jtag_tms_collect(tms_scan);
623 }
624
625 static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction)
626 {
627 char tmp[2];
628 tmp[0] = PORT_DIRECTION;
629 tmp[1] = (char)direction;
630 usbprog_jtag_message(usbprog_jtag, tmp, 2);
631 }
632
633 static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value)
634 {
635 char tmp[2];
636 tmp[0] = PORT_SET;
637 tmp[1] = (char)value;
638 usbprog_jtag_message(usbprog_jtag, tmp, 2);
639 }
640
641 #if 0
642 static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag)
643 {
644 char tmp[2];
645 tmp[0] = PORT_GET;
646 tmp[1] = 0x00;
647 return usbprog_jtag_message(usbprog_jtag, tmp, 2);
648 }
649 #endif
650
651 static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value)
652 {
653 char tmp[3];
654 tmp[0] = PORT_SETBIT;
655 tmp[1] = (char)bit;
656 if (value == 1)
657 tmp[2] = 0x01;
658 else
659 tmp[2] = 0x00;
660 usbprog_jtag_message(usbprog_jtag, tmp, 3);
661 }
662
663 #if 0
664 static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit)
665 {
666 char tmp[2];
667 tmp[0] = PORT_GETBIT;
668 tmp[1] = (char)bit;
669
670 if (usbprog_jtag_message(usbprog_jtag, tmp, 2) > 0)
671 return 1;
672 else
673 return 0;
674 }
675 #endif
676
677 static void usbprog_jtag_tms_collect(char tms_scan)
678 {
679 tms_chain[tms_chain_index] = tms_scan;
680 tms_chain_index++;
681 }
682
683 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag)
684 {
685 int i;
686 /* LOG_INFO("TMS SEND"); */
687 if (tms_chain_index > 0)
688 {
689 char tmp[tms_chain_index + 2];
690 tmp[0] = WRITE_TMS_CHAIN;
691 tmp[1] = (char)(tms_chain_index);
692 for (i = 0; i < tms_chain_index + 1; i++)
693 tmp[2 + i] = tms_chain[i];
694 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, tms_chain_index + 2, 1000);
695 tms_chain_index = 0;
696 }
697 }