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