1 /***************************************************************************
2 * Copyright (C) 2007 by Benedikt Sauter *
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. *
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. *
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 ***************************************************************************/
22 * This file is based on Dominic Rath's amt_jtagaccel.c.
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).
30 * http://www.embedded-projects.net/usbprog
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);
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(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size);
63 jtag_interface_t usbprog_interface =
66 .execute_queue = usbprog_execute_queue,
67 .speed = usbprog_speed,
68 .register_commands = usbprog_register_commands,
73 #define UNKOWN_COMMAND 0x00
74 #define PORT_DIRECTION 0x01
77 #define PORT_SETBIT 0x04
78 #define PORT_GETBIT 0x05
79 #define WRITE_TDI 0x06
81 #define WRITE_AND_READ 0x08
82 #define WRITE_TMS 0x09
83 #define WRITE_TMS_CHAIN 0x0A
87 struct usb_dev_handle* usb_handle;
90 static struct usbprog_jtag * usbprog_jtag_handle;
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);
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);
102 static char tms_chain[64];
103 static int tms_chain_index;
105 static void usbprog_jtag_tms_collect(char tms_scan);
106 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag);
108 static void usbprog_write(int tck, int tms, int tdi);
109 static void usbprog_reset(int trst, int srst);
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);
117 static int usbprog_speed(int speed)
122 static int usbprog_register_commands(struct command_context_s *cmd_ctx)
127 static int usbprog_execute_queue(void)
129 jtag_command_t *cmd = jtag_command_queue; /* currently processed command */
139 #ifdef _DEBUG_JTAG_IO_
140 LOG_DEBUG("reset trst: %i srst %i", cmd->cmd.reset->trst, cmd->cmd.reset->srst);
142 if (cmd->cmd.reset->trst == 1)
144 tap_set_state(TAP_RESET);
146 usbprog_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
149 #ifdef _DEBUG_JTAG_IO_
150 LOG_DEBUG("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles, cmd->cmd.runtest->end_state);
152 if (cmd->cmd.runtest->end_state != TAP_INVALID)
153 usbprog_end_state(cmd->cmd.runtest->end_state);
154 usbprog_runtest(cmd->cmd.runtest->num_cycles);
157 #ifdef _DEBUG_JTAG_IO_
158 LOG_DEBUG("statemove end in %i", cmd->cmd.statemove->end_state);
160 if (cmd->cmd.statemove->end_state != TAP_INVALID)
161 usbprog_end_state(cmd->cmd.statemove->end_state);
162 usbprog_state_move();
165 #ifdef _DEBUG_JTAG_IO_
166 LOG_DEBUG("pathmove: %i states, end in %i", cmd->cmd.pathmove->num_states,
167 cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
169 usbprog_path_move(cmd->cmd.pathmove);
172 #ifdef _DEBUG_JTAG_IO_
173 LOG_DEBUG("scan end in %i", cmd->cmd.scan->end_state);
175 if (cmd->cmd.scan->end_state != TAP_INVALID)
176 usbprog_end_state(cmd->cmd.scan->end_state);
177 scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
178 type = jtag_scan_type(cmd->cmd.scan);
179 usbprog_scan(cmd->cmd.scan->ir_scan, type, buffer, scan_size);
180 if (jtag_read_buffer(buffer, cmd->cmd.scan) != ERROR_OK)
181 return ERROR_JTAG_QUEUE_FAILED;
186 #ifdef _DEBUG_JTAG_IO_
187 LOG_DEBUG("sleep %i", cmd->cmd.sleep->us);
189 jtag_sleep(cmd->cmd.sleep->us);
192 LOG_ERROR("BUG: unknown JTAG command type encountered");
202 static int usbprog_init(void)
204 usbprog_jtag_handle = usbprog_jtag_open();
207 if (usbprog_jtag_handle == 0)
209 LOG_ERROR("Can't find USB JTAG Interface! Please check connection and permissions.");
210 return ERROR_JTAG_INIT_FAILED;
213 LOG_INFO("USB JTAG Interface ready!");
215 usbprog_jtag_init(usbprog_jtag_handle);
217 usbprog_write(0, 0, 0);
222 static int usbprog_quit(void)
227 /*************** jtag execute commands **********************/
228 static void usbprog_end_state(tap_state_t state)
230 if (tap_is_state_stable(state))
231 tap_set_end_state(state);
234 LOG_ERROR("BUG: %i is not a valid end state", state);
239 static void usbprog_state_move(void)
242 u8 tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
243 int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
245 usbprog_jtag_write_tms(usbprog_jtag_handle, (char)tms_scan);
246 for (i = 0; i < tms_count; i++)
248 tms = (tms_scan >> i) & 1;
251 tap_set_state(tap_get_end_state());
254 static void usbprog_path_move(pathmove_command_t *cmd)
256 int num_states = cmd->num_states;
259 /* There may be queued transitions, and before following a specified
260 path, we must flush those queued transitions */
261 usbprog_jtag_tms_send(usbprog_jtag_handle);
266 if (tap_state_transition(tap_get_state(), false) == cmd->path[state_count])
269 usbprog_write(0, 0, 0);
270 usbprog_write(1, 0, 0);
272 else if (tap_state_transition(tap_get_state(), true) == cmd->path[state_count])
275 usbprog_write(0, 1, 0);
276 usbprog_write(1, 1, 0);
280 LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition", tap_state_name(tap_get_state()), tap_state_name(cmd->path[state_count]));
284 tap_set_state(cmd->path[state_count]);
289 tap_set_end_state(tap_get_state());
292 static void usbprog_runtest(int num_cycles)
296 /* only do a state_move when we're not already in IDLE */
297 if (tap_get_state() != TAP_IDLE)
299 usbprog_end_state(TAP_IDLE);
300 usbprog_state_move();
303 /* execute num_cycles */
306 usbprog_jtag_tms_send(usbprog_jtag_handle);
307 usbprog_write(0, 0, 0);
311 usbprog_jtag_tms_send(usbprog_jtag_handle);
312 /* LOG_INFO("NUM CYCLES %i",num_cycles); */
315 for (i = 0; i < num_cycles; i++)
317 usbprog_write(1, 0, 0);
318 usbprog_write(0, 0, 0);
321 #ifdef _DEBUG_JTAG_IO_
322 LOG_DEBUG("runtest: cur_state %s end_state %s", tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()));
325 /* finish in end_state */
327 usbprog_end_state(saved_end_state);
328 if (tap_get_state() != tap_get_end_state())
329 usbprog_state_move();
333 static void usbprog_scan(bool ir_scan, enum scan_type type, u8 *buffer, int scan_size)
335 tap_state_t saved_end_state = tap_get_end_state();
338 usbprog_end_state(TAP_IRSHIFT);
340 usbprog_end_state(TAP_DRSHIFT);
342 /* Only move if we're not already there */
343 if (tap_get_state() != tap_get_end_state())
344 usbprog_state_move();
346 usbprog_end_state(saved_end_state);
348 usbprog_jtag_tms_send(usbprog_jtag_handle);
350 void (*f)(struct usbprog_jtag *usbprog_jtag, char * buffer, int size);
352 case SCAN_OUT: f = &usbprog_jtag_write_tdi; break;
353 case SCAN_IN: f = &usbprog_jtag_read_tdo; break;
354 case SCAN_IO: f = &usbprog_jtag_write_and_read; break;
356 LOG_ERROR("unknown scan type: %i", type);
359 f(usbprog_jtag_handle, (char *)buffer, scan_size);
361 /* The adapter does the transition to PAUSE internally */
363 tap_set_state(TAP_IRPAUSE);
365 tap_set_state(TAP_DRPAUSE);
367 if (tap_get_state() != tap_get_end_state())
368 usbprog_state_move();
371 /*************** jtag wrapper functions *********************/
373 static void usbprog_write(int tck, int tms, int tdi)
375 unsigned char output_value=0x00;
378 output_value |= (1<<TMS_BIT);
380 output_value |= (1<<TDI_BIT);
382 output_value |= (1<<TCK_BIT);
384 usbprog_jtag_write_slice(usbprog_jtag_handle,output_value);
387 /* (1) assert or (0) deassert reset lines */
388 static void usbprog_reset(int trst, int srst)
390 LOG_DEBUG("trst: %i, srst: %i", trst, srst);
393 usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 0);
395 usbprog_jtag_set_bit(usbprog_jtag_handle, 5, 1);
398 usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 0);
400 usbprog_jtag_set_bit(usbprog_jtag_handle, 4, 1);
403 /*************** jtag lowlevel functions ********************/
405 struct usb_bus *busses;
407 struct usbprog_jtag* usbprog_jtag_open(void)
410 struct usb_device *dev;
412 struct usbprog_jtag *tmp;
414 tmp = (struct usbprog_jtag*)malloc(sizeof(struct usbprog_jtag));
421 busses = usb_get_busses();
423 /* find usbprog_jtag device in usb bus */
425 for (bus = busses; bus; bus = bus->next)
427 for (dev = bus->devices; dev; dev = dev->next)
429 /* condition for sucessfully hit (too bad, I only check the vendor id)*/
430 if (dev->descriptor.idVendor == VID && dev->descriptor.idProduct == PID)
432 tmp->usb_handle = usb_open(dev);
433 usb_set_configuration(tmp->usb_handle, 1);
434 usb_claim_interface(tmp->usb_handle, 0);
435 usb_set_altinterface(tmp->usb_handle, 0);
444 static void usbprog_jtag_close(struct usbprog_jtag *usbprog_jtag)
446 usb_close(usbprog_jtag->usb_handle);
451 static unsigned char usbprog_jtag_message(struct usbprog_jtag *usbprog_jtag, char *msg, int msglen)
453 int res = usb_bulk_write(usbprog_jtag->usb_handle, 3, msg,msglen, 100);
454 if ((msg[0] == 2) || (msg[0] == 1) || (msg[0] == 4) || (msg[0] == 0) || \
455 (msg[0] == 6) || (msg[0] == 0x0A) || (msg[0] == 9))
459 /* LOG_INFO("HALLLLOOO %i",(int)msg[0]); */
460 res = usb_bulk_read(usbprog_jtag->usb_handle, 0x82, msg, 2, 100);
462 return (unsigned char)msg[1];
471 static void usbprog_jtag_init(struct usbprog_jtag *usbprog_jtag)
473 usbprog_jtag_set_direction(usbprog_jtag, 0xFE);
476 static void usbprog_jtag_write_and_read(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
478 char tmp[64]; /* fastes packet size for usb controller */
479 int send_bits, bufindex = 0, fillindex = 0, i, loops;
482 /* 61 byte can be transfered (488 bit) */
499 tmp[0] = WRITE_AND_READ;
500 tmp[1] = (char)(send_bits >> 8); /* high */
501 tmp[2] = (char)(send_bits); /* low */
504 for (i = 0; i < loops; i++)
506 tmp[3 + i] = buffer[bufindex];
510 if (usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000) == 64)
512 /* LOG_INFO("HALLLLOOO2 %i",(int)tmp[0]); */
515 while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 1000) < 1)
522 for (i = 0; i < loops; i++)
525 buffer[fillindex++] = swap;
531 static void usbprog_jtag_read_tdo(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
533 char tmp[64]; /* fastes packet size for usb controller */
534 int send_bits, fillindex = 0, i, loops;
537 /* 61 byte can be transfered (488 bit) */
554 tmp[0] = WRITE_AND_READ;
555 tmp[1] = (char)(send_bits >> 8); /* high */
556 tmp[2] = (char)(send_bits); /* low */
558 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 3, 1000);
560 /* LOG_INFO("HALLLLOOO3 %i",(int)tmp[0]); */
563 while (usb_bulk_read(usbprog_jtag->usb_handle, 0x82, tmp, 64, 10) < 1)
570 for (i = 0; i < loops; i++)
573 buffer[fillindex++] = swap;
578 static void usbprog_jtag_write_tdi(struct usbprog_jtag *usbprog_jtag, char * buffer, int size)
580 char tmp[64]; /* fastes packet size for usb controller */
581 int send_bits, bufindex = 0, i, loops;
583 /* 61 byte can be transfered (488 bit) */
601 tmp[1] = (char)(send_bits >> 8); /* high */
602 tmp[2] = (char)(send_bits); /* low */
605 for (i = 0; i < loops; i++)
607 tmp[3 + i] = buffer[bufindex];
610 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, 64, 1000);
614 static void usbprog_jtag_write_tms(struct usbprog_jtag *usbprog_jtag, char tms_scan)
616 usbprog_jtag_tms_collect(tms_scan);
619 static void usbprog_jtag_set_direction(struct usbprog_jtag *usbprog_jtag, unsigned char direction)
622 tmp[0] = PORT_DIRECTION;
623 tmp[1] = (char)direction;
624 usbprog_jtag_message(usbprog_jtag, tmp, 2);
627 static void usbprog_jtag_write_slice(struct usbprog_jtag *usbprog_jtag,unsigned char value)
631 tmp[1] = (char)value;
632 usbprog_jtag_message(usbprog_jtag, tmp, 2);
636 static unsigned char usbprog_jtag_get_port(struct usbprog_jtag *usbprog_jtag)
641 return usbprog_jtag_message(usbprog_jtag, tmp, 2);
645 static void usbprog_jtag_set_bit(struct usbprog_jtag *usbprog_jtag,int bit, int value)
648 tmp[0] = PORT_SETBIT;
654 usbprog_jtag_message(usbprog_jtag, tmp, 3);
658 static int usbprog_jtag_get_bit(struct usbprog_jtag *usbprog_jtag, int bit)
661 tmp[0] = PORT_GETBIT;
664 if (usbprog_jtag_message(usbprog_jtag, tmp, 2) > 0)
671 static void usbprog_jtag_tms_collect(char tms_scan)
673 tms_chain[tms_chain_index] = tms_scan;
677 static void usbprog_jtag_tms_send(struct usbprog_jtag *usbprog_jtag)
680 /* LOG_INFO("TMS SEND"); */
681 if (tms_chain_index > 0)
683 char tmp[tms_chain_index + 2];
684 tmp[0] = WRITE_TMS_CHAIN;
685 tmp[1] = (char)(tms_chain_index);
686 for (i = 0; i < tms_chain_index + 1; i++)
687 tmp[2 + i] = tms_chain[i];
688 usb_bulk_write(usbprog_jtag->usb_handle, 3, tmp, tms_chain_index + 2, 1000);