- rename log functions to stop conflicts under win32 (wingdi)
[fw/openocd] / src / jtag / presto.c
1 /***************************************************************************
2  *   Copyright (C) 2007 by Pavel Chromy                                    *
3  *   chromy@asix.cz                                                        *
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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #if IS_CYGWIN == 1
25 #include "windows.h"
26 #endif
27
28 #include "replacements.h"
29
30 /* project specific includes */
31 #include "log.h"
32 #include "types.h"
33 #include "jtag.h"
34 #include "configuration.h"
35 #include "time_support.h"
36 #include "bitq.h"
37
38 /* system includes */
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42
43 #include <sys/time.h>
44 #include <time.h>
45
46 /* PRESTO access library includes */
47 #if BUILD_PRESTO_FTD2XX == 1
48 #include <ftd2xx.h>
49 #elif BUILD_PRESTO_LIBFTDI == 1
50 #include <ftdi.h>
51 #else
52 #error "BUG: either FTD2XX and LIBFTDI has to be used"
53 #endif
54
55
56 int presto_jtag_speed(int speed);
57 int presto_jtag_register_commands(struct command_context_s *cmd_ctx);
58 int presto_jtag_init(void);
59 int presto_jtag_quit(void);
60
61 jtag_interface_t presto_interface =
62 {
63         .name = "presto",
64         .execute_queue = bitq_execute_queue,
65         .speed = presto_jtag_speed,
66         .register_commands = presto_jtag_register_commands,
67         .init = presto_jtag_init,
68         .quit = presto_jtag_quit,
69 };
70
71
72 int presto_bitq_out(int tms, int tdi, int tdo_req);
73 int presto_bitq_flush(void);
74 int presto_bitq_sleep(unsigned long us);
75 int presto_bitq_reset(int trst, int srst);
76 int presto_bitq_in_rdy(void);
77 int presto_bitq_in(void);
78
79 bitq_interface_t presto_bitq =
80 {
81         .out = presto_bitq_out,
82         .flush = presto_bitq_flush,
83         .sleep = presto_bitq_sleep,
84         .reset = presto_bitq_reset,
85         .in_rdy = presto_bitq_in_rdy,
86         .in = presto_bitq_in,
87 };
88
89
90 /* -------------------------------------------------------------------------- */
91
92
93 #define FT_DEVICE_NAME_LEN 64
94 #define FT_DEVICE_SERNUM_LEN 64
95
96 #define PRESTO_VID_PID 0x0403f1a0
97 #define PRESTO_VID (0x0403)
98 #define PRESTO_PID (0xf1a0)
99
100 #define BUFFER_SIZE (64*62)
101
102 typedef struct presto_s
103 {
104 #if BUILD_PRESTO_FTD2XX == 1
105         FT_HANDLE handle;
106         FT_STATUS status;
107 #elif BUILD_PRESTO_LIBFTDI == 1
108         struct ftdi_context ftdic;
109         int retval;
110 #endif
111
112         char serial[FT_DEVICE_SERNUM_LEN];
113
114         u8 buff_out[BUFFER_SIZE];
115         int buff_out_pos;
116
117         u8 buff_in[BUFFER_SIZE];
118         int buff_in_exp; /* expected in buffer length */
119         int buff_in_len; /* length of data received */
120         int buff_in_pos;
121
122         unsigned long total_out;
123         unsigned long total_in;
124
125         int jtag_tms; /* last tms state */
126         int jtag_tck; /* last tck state */
127
128         int jtag_tdi_data;
129         int jtag_tdi_count;
130
131 } presto_t;
132
133 presto_t presto_state;
134 presto_t *presto = &presto_state;
135
136 u8 presto_init_seq[] =
137 {
138         0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0
139 };
140
141 int presto_write(u8 *buf, int size)
142 {
143 #if BUILD_PRESTO_FTD2XX == 1
144         DWORD ftbytes;
145         if ((presto->status = FT_Write(presto->handle, buf, size, &ftbytes)) != FT_OK)
146         {
147                 LOG_ERROR("FT_Write returned: %lu", presto->status);
148                 return ERROR_JTAG_DEVICE_ERROR;
149         }
150
151 #elif BUILD_PRESTO_LIBFTDI == 1
152         u32 ftbytes;
153         if ((presto->retval = ftdi_write_data(&presto->ftdic, buf, size)) < 0)
154         {
155                 LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
156                 return ERROR_JTAG_DEVICE_ERROR;
157         }
158         ftbytes = presto->retval;
159 #endif
160
161         if (ftbytes != size)
162         {
163                 LOG_ERROR("couldn't write the requested number of bytes to PRESTO (%i < %i)", ftbytes, size);
164                 return ERROR_JTAG_DEVICE_ERROR;
165         }
166
167         return ERROR_OK;
168 }
169
170 int presto_read(u8* buf, int size)
171 {
172 #if BUILD_PRESTO_FTD2XX == 1
173         DWORD ftbytes;
174         if ((presto->status = FT_Read(presto->handle, buf, size, &ftbytes)) != FT_OK)
175         {
176                 LOG_ERROR("FT_Read returned: %lu", presto->status);
177                 return ERROR_JTAG_DEVICE_ERROR;
178         }
179
180 #elif BUILD_PRESTO_LIBFTDI == 1
181         u32 ftbytes = 0;
182
183         struct timeval timeout, now;
184         gettimeofday(&timeout, NULL);
185         timeval_add_time(&timeout, 1, 0); /* one second timeout */
186
187         while (ftbytes < size)
188         {
189                 if ((presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes)) < 0)
190                 {
191                         LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
192                         return ERROR_JTAG_DEVICE_ERROR;
193                 }
194                 ftbytes += presto->retval;
195
196                 gettimeofday(&now, NULL);
197                 if ((now.tv_sec > timeout.tv_sec) || ((now.tv_sec == timeout.tv_sec) && (now.tv_usec > timeout.tv_usec)))
198                         break;
199         }
200 #endif
201
202         if (ftbytes != size)
203         {
204                 /* this is just a warning, there might have been timeout when detecting PRESTO, which is not fatal */
205                 LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%i < %i)", ftbytes, size);
206                 return ERROR_JTAG_DEVICE_ERROR;
207         }
208
209         return ERROR_OK;
210 }
211
212 #if BUILD_PRESTO_FTD2XX == 1
213 int presto_open_ftd2xx(char *req_serial)
214 {
215         int i;
216         DWORD numdevs;
217         DWORD vidpid;
218         char devname[FT_DEVICE_NAME_LEN];
219         FT_DEVICE device;
220
221         BYTE presto_data;
222         DWORD ftbytes;
223
224         presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
225
226 #if IS_WIN32 == 0
227         /* Add non-standard Vid/Pid to the linux driver */
228         if ((presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID)) != FT_OK)
229         {
230                 LOG_ERROR("couldn't add PRESTO VID/PID");
231                 exit(-1);
232         }
233 #endif
234
235         if ((presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY)) != FT_OK)
236         {
237                 LOG_ERROR("FT_ListDevices failed: %i", (int)presto->status);
238                 return ERROR_JTAG_DEVICE_ERROR;
239         }
240
241         LOG_DEBUG("FTDI devices available: %i", numdevs);
242         for (i = 0; i < numdevs; i++)
243         {
244                 if ((presto->status = FT_Open(i, &(presto->handle))) != FT_OK)
245                 {
246                         /* this is not fatal, the device may be legitimately open by other process, hence debug message only */
247                         LOG_DEBUG("FT_Open failed: %i", (int)presto->status);
248                         continue;
249                 }
250                 LOG_DEBUG("FTDI device %i open", i);
251
252                 if ((presto->status = FT_GetDeviceInfo(presto->handle, &device, &vidpid,
253                                 presto->serial, devname, NULL)) == FT_OK)
254                 {
255                         if (vidpid == PRESTO_VID_PID
256                                         && (req_serial == NULL || !strcmp(presto->serial, req_serial)))
257                                 break;
258                 }
259                 else
260                         LOG_DEBUG("FT_GetDeviceInfo failed: %i", presto->status);
261
262                 LOG_DEBUG("FTDI device %i does not match, closing", i);
263                 FT_Close(presto->handle);
264                 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
265         }
266
267         if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
268                 return ERROR_JTAG_DEVICE_ERROR; /* presto not open, return */
269
270         if ((presto->status = FT_SetLatencyTimer(presto->handle, 1)) != FT_OK)
271                 return ERROR_JTAG_DEVICE_ERROR;
272
273
274         if ((presto->status = FT_SetTimeouts(presto->handle, 100, 0)) != FT_OK)
275                 return ERROR_JTAG_DEVICE_ERROR;
276
277         if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
278                 return ERROR_JTAG_DEVICE_ERROR;
279
280         presto_data = 0xD0;
281         if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
282                 return ERROR_JTAG_DEVICE_ERROR;
283
284         /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
285            probably a bug in library threading */
286         usleep(100000);
287         if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
288                 return ERROR_JTAG_DEVICE_ERROR;
289
290         if (ftbytes!=1)
291         {
292                 LOG_DEBUG("PRESTO reset");
293
294                 if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
295                         return ERROR_JTAG_DEVICE_ERROR;
296                 if ((presto->status = FT_SetBitMode(presto->handle, 0x80, 1)) != FT_OK)
297                         return ERROR_JTAG_DEVICE_ERROR;
298                 if ((presto->status = FT_SetBaudRate(presto->handle, 9600)) != FT_OK)
299                         return ERROR_JTAG_DEVICE_ERROR;
300
301                 presto_data = 0;
302                 for (i = 0; i < 4 * 62; i++)
303                         if ((presto->status=FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
304                                 return ERROR_JTAG_DEVICE_ERROR;
305
306                 usleep(100000);
307
308                 if ((presto->status = FT_SetBitMode(presto->handle, 0x00, 0)) != FT_OK)
309                         return ERROR_JTAG_DEVICE_ERROR;
310
311                 if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
312                         return ERROR_JTAG_DEVICE_ERROR;
313
314                 presto_data = 0xD0;
315                 if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
316                         return ERROR_JTAG_DEVICE_ERROR;
317
318                 /* delay between first write/read turnaround (after purge?) necessary under Linux for unknown reason,
319                    probably a bug in library threading */
320                 usleep(100000);
321                 if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
322                         return ERROR_JTAG_DEVICE_ERROR;
323
324                 if (ftbytes!=1)
325                 {
326                         LOG_DEBUG("PRESTO not responding");
327                         return ERROR_JTAG_DEVICE_ERROR;
328                 }
329         }
330
331         if ((presto->status = FT_SetTimeouts(presto->handle, 0, 0)) != FT_OK)
332                 return ERROR_JTAG_DEVICE_ERROR;
333
334
335         presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
336         if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
337                 return ERROR_JTAG_DEVICE_ERROR;
338
339         return ERROR_OK;
340 }
341
342 #elif BUILD_PRESTO_LIBFTDI == 1
343 int presto_open_libftdi(char *req_serial)
344 {
345         u8 presto_data;
346
347         LOG_DEBUG("searching for PRESTO using libftdi");
348
349         /* initialize FTDI context structure */
350         if (ftdi_init(&presto->ftdic) < 0)
351         {
352                 LOG_ERROR("unable to init libftdi: %s", presto->ftdic.error_str);
353                 return ERROR_JTAG_DEVICE_ERROR;
354         }
355
356         /* context, vendor id, product id */
357         if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0)
358         {
359                 LOG_ERROR("unable to open PRESTO: %s", presto->ftdic.error_str);
360                 return ERROR_JTAG_DEVICE_ERROR;
361         }
362
363         if (ftdi_usb_reset(&presto->ftdic) < 0)
364         {
365                 LOG_ERROR("unable to reset PRESTO device");
366                 return ERROR_JTAG_DEVICE_ERROR;
367         }
368
369         if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0)
370         {
371                 LOG_ERROR("unable to set latency timer");
372                 return ERROR_JTAG_DEVICE_ERROR;
373         }
374
375         if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
376         {
377                 LOG_ERROR("unable to purge PRESTO buffers");
378                 return ERROR_JTAG_DEVICE_ERROR;
379         }
380
381         presto_data = 0xD0;
382         if (presto_write(&presto_data, 1) != ERROR_OK)
383         {
384                 LOG_ERROR("error writing to PRESTO");
385                 return ERROR_JTAG_DEVICE_ERROR;
386         }
387
388         if (presto_read(&presto_data, 1) != ERROR_OK)
389         {
390                 LOG_DEBUG("no response from PRESTO, retrying");
391
392                 if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
393                         return ERROR_JTAG_DEVICE_ERROR;
394
395                 presto_data = 0xD0;
396                 if (presto_write(&presto_data, 1) != ERROR_OK)
397                         return ERROR_JTAG_DEVICE_ERROR;
398
399                 if (presto_read(&presto_data, 1) != ERROR_OK)
400                 {
401                         LOG_ERROR("no response from PRESTO, giving up");
402                         return ERROR_JTAG_DEVICE_ERROR;
403                 }
404         }
405
406         if (presto_write(presto_init_seq, sizeof(presto_init_seq)) != ERROR_OK)
407         {
408                 LOG_ERROR("error writing PRESTO init sequence");
409                 return ERROR_JTAG_DEVICE_ERROR;
410         }
411
412         return ERROR_OK;
413 }
414 #endif /* BUILD_PRESTO_LIBFTDI == 1 */
415
416 int presto_open(char *req_serial)
417 {
418         presto->buff_out_pos=0;
419         presto->buff_in_pos=0;
420         presto->buff_in_len=0;
421         presto->buff_in_exp=0;
422
423         presto->total_out=0;
424         presto->total_in=0;
425
426         presto->jtag_tms=0;
427         presto->jtag_tck=0;
428         presto->jtag_tdi_data=0;
429         presto->jtag_tdi_count=0;
430
431 #if BUILD_PRESTO_FTD2XX == 1
432         return presto_open_ftd2xx(req_serial);
433 #elif BUILD_PRESTO_LIBFTDI == 1
434         return presto_open_libftdi(req_serial);
435 #endif
436 }
437
438 int presto_close(void)
439 {
440
441         int result = ERROR_OK;
442
443 #if BUILD_PRESTO_FTD2XX == 1
444         unsigned long ftbytes;
445
446         if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
447                 return result;
448
449         presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX);
450         if (presto->status != FT_OK)
451                 result = ERROR_JTAG_DEVICE_ERROR;
452
453         presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
454         if (presto->status != FT_OK || ftbytes != sizeof(presto_init_seq))
455                 result = ERROR_JTAG_DEVICE_ERROR;
456
457         if ((presto->status = FT_SetLatencyTimer(presto->handle, 16)) != FT_OK)
458                 result = ERROR_JTAG_DEVICE_ERROR;
459
460         if ((presto->status = FT_Close(presto->handle)) != FT_OK)
461                 result = ERROR_JTAG_DEVICE_ERROR;
462         else
463                 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
464
465 #elif BUILD_PRESTO_LIBFTDI == 1
466
467         if ((presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq))) != sizeof(presto_init_seq))
468                 result = ERROR_JTAG_DEVICE_ERROR;
469
470         if ((presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16)) < 0)
471                 result = ERROR_JTAG_DEVICE_ERROR;
472
473         if ((presto->retval = ftdi_usb_close(&presto->ftdic)) < 0)
474                 result = ERROR_JTAG_DEVICE_ERROR;
475         else
476                 ftdi_deinit(&presto->ftdic);
477 #endif
478
479         return result;
480 }
481
482
483 int presto_flush(void)
484 {
485         if (presto->buff_out_pos == 0)
486                 return ERROR_OK;
487
488 #if BUILD_PRESTO_FTD2XX == 1
489         if (presto->status != FT_OK)
490 #elif BUILD_PRESTO_LIBFTDI == 1
491         if (presto->retval < 0)
492 #endif
493         {
494                 LOG_DEBUG("error in previous communication, canceling I/O operation");
495                 return ERROR_JTAG_DEVICE_ERROR;
496         }
497
498         if (presto_write(presto->buff_out, presto->buff_out_pos) != ERROR_OK)
499         {
500                 presto->buff_out_pos = 0;
501                 return ERROR_JTAG_DEVICE_ERROR;
502         }
503
504         presto->total_out += presto->buff_out_pos;
505         presto->buff_out_pos = 0;
506
507         if (presto->buff_in_exp == 0)
508                 return ERROR_OK;
509
510         presto->buff_in_pos = 0;
511         presto->buff_in_len = 0;
512
513         if (presto_read(presto->buff_in, presto->buff_in_exp) != ERROR_OK)
514         {
515                 presto->buff_in_exp = 0;
516                 return ERROR_JTAG_DEVICE_ERROR;
517         }
518
519         presto->total_in += presto->buff_in_exp;
520         presto->buff_in_len = presto->buff_in_exp;
521         presto->buff_in_exp = 0;
522
523         return ERROR_OK;
524 }
525
526
527 int presto_sendbyte(int data)
528 {
529         if (data == EOF) return presto_flush();
530
531         if (presto->buff_out_pos < BUFFER_SIZE)
532         {
533                 presto->buff_out[presto->buff_out_pos++] = (u8)data;
534                 if (((data & 0xC0) == 0x40) || ((data & 0xD0)== 0xD0))
535                         presto->buff_in_exp++;
536         }
537         else
538                 return ERROR_JTAG_DEVICE_ERROR;
539
540 #if BUILD_PRESTO_FTD2XX == 1
541         if (presto->buff_out_pos >= BUFFER_SIZE)
542 #elif BUILD_PRESTO_LIBFTDI == 1
543         /* libftdi does not do background read, be sure that USB IN buffer does not overflow (128 bytes only!) */
544         if (presto->buff_out_pos >= BUFFER_SIZE || presto->buff_in_exp==128)
545 #endif
546                 return presto_flush();
547
548         return ERROR_OK;
549 }
550
551
552 int presto_getbyte(void)
553 {
554         if (presto->buff_in_pos < presto->buff_in_len)
555                 return presto->buff_in[presto->buff_in_pos++];
556
557         if (presto->buff_in_exp == 0)
558                 return -1;
559
560         if (presto_flush() != ERROR_OK)
561                 return -1;
562
563         if (presto->buff_in_pos<presto->buff_in_len)
564                 return presto->buff_in[presto->buff_in_pos++];
565
566         return -1;
567 }
568
569
570 /* -------------------------------------------------------------------------- */
571
572
573 int presto_bitq_out(int tms, int tdi, int tdo_req)
574 {
575         unsigned char cmdparam;
576
577         if (presto->jtag_tck == 0)
578         {
579                 presto_sendbyte(0xA4);
580                 presto->jtag_tck = 1;
581         }
582
583         else if (!tdo_req && tms == presto->jtag_tms)
584         {
585                 if (presto->jtag_tdi_count == 0)
586                         presto->jtag_tdi_data = (tdi != 0);
587                 else
588                         presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count;
589
590                 if (++presto->jtag_tdi_count == 4)
591                 {
592                         presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
593                         presto_sendbyte(presto->jtag_tdi_data);
594                         presto->jtag_tdi_count = 0;
595                 }
596                 return 0;
597         }
598
599         if (presto->jtag_tdi_count)
600         {
601                 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
602                 presto_sendbyte(presto->jtag_tdi_data);
603                 presto->jtag_tdi_count = 0;
604         }
605
606         if (tdi)
607                 cmdparam = 0x0B;
608         else
609                 cmdparam = 0x0A;
610
611         presto_sendbyte( 0xC0 | cmdparam);
612
613         if (tms != presto->jtag_tms)
614         {
615                 if (tms)
616                         presto_sendbyte(0xEC);
617                 else
618                         presto_sendbyte(0xE8);
619                 presto->jtag_tms = tms;
620         }
621
622         if (tdo_req)
623                 presto_sendbyte(0xD4 | cmdparam);
624         else
625                 presto_sendbyte(0xC4|cmdparam);
626
627         return 0;
628 }
629
630
631 int presto_bitq_flush(void)
632 {
633         if (presto->jtag_tdi_count)
634         {
635                 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
636                 presto_sendbyte(presto->jtag_tdi_data);
637                 presto->jtag_tdi_count = 0;
638         }
639
640         presto_sendbyte(0xCA);
641         presto->jtag_tck = 0;
642
643         presto_sendbyte(0xA0);
644
645         return presto_flush();
646 }
647
648
649 int presto_bitq_in_rdy(void)
650 {
651         if (presto->buff_in_pos>=presto->buff_in_len)
652                 return 0;
653         return presto->buff_in_len-presto->buff_in_pos;
654 }
655
656
657 int presto_bitq_in(void)
658 {
659         if (presto->buff_in_pos>=presto->buff_in_len)
660                 return -1;
661         if (presto->buff_in[presto->buff_in_pos++]&0x08) return 1;
662         return 0;
663 }
664
665
666 int presto_bitq_sleep(unsigned long us)
667 {
668         long waits;
669
670         if (us > 100000)
671         {
672                 presto_bitq_flush();
673                 jtag_sleep(us);
674                 return 0;
675         }
676
677         waits = us / 170 + 2;
678         while (waits--)
679                 presto_sendbyte(0x80);
680
681         return 0;
682 }
683
684
685 int presto_bitq_reset(int trst, int srst)
686 {
687         unsigned char cmd;
688
689         cmd = 0xE8;
690         if (presto->jtag_tms)
691                 cmd |= 0x04;
692
693         if (trst || srst)
694                 cmd |= 0x02;
695
696         presto_sendbyte(cmd);
697         return 0;
698 }
699
700
701 /* -------------------------------------------------------------------------- */
702
703 char *presto_speed_text[4] =
704 {
705         "3 MHz",
706         "1.5 MHz",
707         "750 kHz",
708         "93.75 kHz"
709 };
710
711 int presto_jtag_speed(int speed)
712 {
713
714         if ((speed < 0) || (speed > 3))
715         {
716                 LOG_INFO("valid speed values: 0 (3 MHz), 1 (1.5 MHz), 2 (750 kHz) and 3 (93.75 kHz)");
717                 return ERROR_INVALID_ARGUMENTS;
718         }
719
720         jtag_speed = speed;
721         LOG_INFO("setting speed to %d, max. TCK freq. is %s", speed, presto_speed_text[speed]);
722         return presto_sendbyte(0xA8 | speed);
723 }
724
725
726 char *presto_serial;
727
728 int presto_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
729 {
730         if (argc == 1)
731         {
732                 if (presto_serial)
733                         free(presto_serial);
734                 presto_serial = strdup(args[0]);
735         }
736         else
737         {
738                 LOG_ERROR("expected exactly one argument to presto_serial <serial-number>");
739         }
740
741         return ERROR_OK;
742 }
743
744
745 int presto_jtag_register_commands(struct command_context_s *cmd_ctx)
746 {
747         register_command(cmd_ctx, NULL, "presto_serial", presto_handle_serial_command,
748                 COMMAND_CONFIG, NULL);
749         return ERROR_OK;
750 }
751
752
753 int presto_jtag_init(void)
754 {
755         if (presto_open(presto_serial) != ERROR_OK)
756         {
757                 presto_close();
758                 if (presto_serial != NULL)
759                         LOG_ERROR("Cannot open PRESTO, serial number '%s'", presto_serial);
760                 else
761                         LOG_ERROR("Cannot open PRESTO");
762                 return ERROR_JTAG_INIT_FAILED;
763         }
764         LOG_INFO("PRESTO open, serial number '%s'", presto->serial);
765
766         /* use JTAG speed setting from configuration file */
767         presto_jtag_speed(jtag_speed);
768
769         bitq_interface = &presto_bitq;
770         return ERROR_OK;
771 }
772
773
774 int presto_jtag_quit(void)
775 {
776         bitq_cleanup();
777         presto_close();
778         LOG_INFO("PRESTO closed");
779
780         if (presto_serial)
781         {
782                 free(presto_serial);
783                 presto_serial = NULL;
784         }
785
786         return ERROR_OK;
787 }