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