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