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