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