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