1 /***************************************************************************
2 * Copyright (C) 2007 by Pavel Chromy *
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. *
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. *
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 ***************************************************************************/
29 #include "replacements.h"
31 /* project specific includes */
35 #include "configuration.h"
36 #include "time_support.h"
47 /* PRESTO access library includes */
48 #if BUILD_PRESTO_FTD2XX == 1
50 #elif BUILD_PRESTO_LIBFTDI == 1
55 int presto_jtag_speed(int speed);
56 int presto_jtag_register_commands(struct command_context_s *cmd_ctx);
57 int presto_jtag_init(void);
58 int presto_jtag_quit(void);
60 jtag_interface_t presto_interface =
63 .execute_queue = bitq_execute_queue,
64 .support_pathmove = 1,
65 .speed = presto_jtag_speed,
66 .register_commands = presto_jtag_register_commands,
67 .init = presto_jtag_init,
68 .quit = presto_jtag_quit,
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);
79 bitq_interface_t presto_bitq =
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,
90 /* -------------------------------------------------------------------------- */
93 #define FT_DEVICE_NAME_LEN 64
94 #define FT_DEVICE_SERNUM_LEN 64
96 #define PRESTO_VID_PID 0x0403f1a0
97 #define PRESTO_VID (0x0403)
98 #define PRESTO_PID (0xf1a0)
100 #define BUFFER_SIZE (64*62)
102 typedef struct presto_s
104 #if BUILD_PRESTO_FTD2XX == 1
107 #elif BUILD_PRESTO_LIBFTDI == 1
108 struct ftdi_context ftdic;
112 char serial[FT_DEVICE_SERNUM_LEN];
114 u8 buff_out[BUFFER_SIZE];
117 u8 buff_in[BUFFER_SIZE];
118 int buff_in_exp; /* expected in buffer length */
119 int buff_in_len; /* length of data received */
122 unsigned long total_out;
123 unsigned long total_in;
125 int jtag_tms; /* last tms state */
126 int jtag_tck; /* last tck state */
133 presto_t presto_state;
134 presto_t *presto = &presto_state;
136 u8 presto_init_seq[] =
138 0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0
141 int presto_write(u8 *buf, int size, u32* bytes_written)
143 #if BUILD_PRESTO_FTD2XX == 1
144 DWORD dw_bytes_written;
145 if ((presto->status = FT_Write(presto->handle, buf, size, &dw_bytes_written)) != FT_OK)
147 *bytes_written = dw_bytes_written;
148 ERROR("FT_Write returned: %lu", presto->status);
149 return ERROR_JTAG_DEVICE_ERROR;
153 *bytes_written = dw_bytes_written;
156 #elif BUILD_PRESTO_LIBFTDI == 1
157 if ((presto->retval = ftdi_write_data(&presto->ftdic, buf, size)) < 0)
160 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
161 return ERROR_JTAG_DEVICE_ERROR;
165 *bytes_written = retval;
171 int presto_read(u8* buf, int size, u32* bytes_read)
173 #if BUILD_PRESTO_FTD2XX == 1
178 while ((*bytes_read < size) && timeout--)
180 if ((presto->status = FT_Read(presto->handle, buf + *bytes_read, size -
181 *bytes_read, &dw_bytes_read)) != FT_OK)
184 ERROR("FT_Read returned: %lu", presto->status);
185 return ERROR_JTAG_DEVICE_ERROR;
187 *bytes_read += dw_bytes_read;
189 #elif BUILD_PRESTO_LIBFTDI == 1
193 while ((*bytes_read < size) && timeout--)
195 if ((presto->retval = ftdi_read_data(&presto->ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
198 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
199 return ERROR_JTAG_DEVICE_ERROR;
201 *bytes_read += retval;
205 if (*bytes_read < size)
207 ERROR("couldn't read the requested number of bytes from PRESTO (%i < %i)", *bytes_read, size);
208 return ERROR_JTAG_DEVICE_ERROR;
214 #if BUILD_PRESTO_FTD2XX == 1
215 int presto_open_ftd2xx(char *req_serial)
220 char devname[FT_DEVICE_NAME_LEN];
224 unsigned long ftbytes;
226 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
229 /* Add non-standard Vid/Pid to the linux driver */
230 if ((presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID)) != FT_OK)
232 ERROR("couldn't add PRESTO VID/PID");
237 if ((presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY)) != FT_OK)
239 ERROR("FT_ListDevices failed: %i", (int)presto->status);
240 return ERROR_JTAG_INIT_FAILED;
243 for (i = 0; i < numdevs; i++)
245 if (FT_Open(i, &(presto->handle)) != FT_OK)
247 ERROR("FT_Open failed: %i", (int)presto->status);
251 if (FT_GetDeviceInfo(presto->handle, &device, &vidpid,
252 presto->serial, devname, NULL) == FT_OK)
254 if (vidpid == PRESTO_VID_PID
255 && (req_serial == NULL || !strcmp(presto->serial, req_serial)))
259 FT_Close(presto->handle);
260 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
263 if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
264 return ERROR_JTAG_INIT_FAILED;
265 if ((presto->status = FT_SetLatencyTimer(presto->handle, 1)) != FT_OK)
266 return ERROR_JTAG_INIT_FAILED;
267 if ((presto->status = FT_SetTimeouts(presto->handle, 100, 0)) != FT_OK)
268 return ERROR_JTAG_INIT_FAILED;
269 if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
270 return ERROR_JTAG_INIT_FAILED;
273 if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
274 return ERROR_JTAG_INIT_FAILED;
275 if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
276 return ERROR_JTAG_INIT_FAILED;
280 if ((presto->status = FT_SetBitMode(presto->handle, 0x80, 1)) != FT_OK)
281 return ERROR_JTAG_INIT_FAILED;
282 if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
283 return ERROR_JTAG_INIT_FAILED ;
284 if ((presto->status = FT_SetBaudRate(presto->handle, 9600)) != FT_OK)
285 return ERROR_JTAG_INIT_FAILED;
288 for (i = 0; i < 4 * 62; i++)
289 if ((presto->status=FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
290 return ERROR_JTAG_INIT_FAILED;
294 if ((presto->status = FT_SetBitMode(presto->handle, 0x00, 0)) != FT_OK)
295 return ERROR_JTAG_INIT_FAILED;
296 if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
297 return ERROR_JTAG_INIT_FAILED;
300 if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
301 return ERROR_JTAG_INIT_FAILED;
302 if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
303 return ERROR_JTAG_INIT_FAILED;
305 return ERROR_JTAG_INIT_FAILED;
308 if ((presto->status = FT_SetTimeouts(presto->handle, 0, 0)) != FT_OK)
309 return ERROR_JTAG_INIT_FAILED;
311 presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
312 if (presto->status != FT_OK)
313 return ERROR_JTAG_INIT_FAILED;
314 if (ftbytes != sizeof(presto_init_seq))
315 return ERROR_JTAG_INIT_FAILED;
320 #elif BUILD_PRESTO_LIBFTDI == 1
321 int presto_open_libftdi(char *req_serial)
326 DEBUG("searching for presto JTAG interface using libftdi");
328 /* context, vendor id, product id */
329 if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0)
331 ERROR("unable to open presto: %s", presto->ftdic.error_str);
332 return ERROR_JTAG_INIT_FAILED;
335 if (ftdi_usb_reset(&presto->ftdic) < 0)
337 ERROR("unable to reset presto device");
338 return ERROR_JTAG_INIT_FAILED;
341 if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0)
343 ERROR("unable to set latency timer");
344 return ERROR_JTAG_INIT_FAILED;
347 if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
349 ERROR("unable to purge presto buffer");
350 return ERROR_JTAG_INIT_FAILED;
354 if ((presto->retval = presto_write(&presto_data, 1, &ftbytes)) != ERROR_OK)
355 return ERROR_JTAG_INIT_FAILED;
356 if ((presto->retval = presto_read(&presto_data, 1, &ftbytes)) != ERROR_OK)
357 return ERROR_JTAG_INIT_FAILED;
361 #endif /* BUILD_PRESTO_LIBFTDI == 1 */
363 int presto_open(char *req_serial)
365 presto->buff_out_pos=0;
366 presto->buff_in_pos=0;
367 presto->buff_in_len=0;
368 presto->buff_in_exp=0;
375 presto->jtag_tdi_data=0;
376 presto->jtag_tdi_count=0;
378 #if BUILD_PRESTO_FTD2XX == 1
379 return presto_open_ftd2xx(req_serial);
380 #elif BUILD_PRESTO_LIBFTDI == 1
381 return presto_open_libftdi(req_serial);
385 int presto_close(void)
388 int result = ERROR_OK;
390 #if BUID_PRESTO_FTD2XX == 1
391 unsigned long ftbytes;
393 if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
396 presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
397 if (presto->status != FT_OK)
399 if (ftbytes != sizeof(presto_init_seq))
400 result = PRST_TIMEOUT;
402 if ((presto->status = FT_SetLatencyTimer(presto->handle, 16)) != FT_OK)
405 if ((presto->status = FT_Close(presto->handle)) != FT_OK)
408 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
410 #elif BUILD_PRESTO_LIBFTDI == 1
412 if ((presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq))) < 0)
414 result = ERROR_JTAG_DEVICE_ERROR;
417 if ((presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16)) < 0)
419 result = ERROR_JTAG_DEVICE_ERROR;
422 ftdi_deinit(&presto->ftdic);
430 int presto_flush(void)
434 if (presto->buff_out_pos == 0)
437 #if BUILD_PRESTO_FTD2XX == 1
438 if (presto->status != FT_OK)
439 #elif BUILD_PRESTO_LIBFTDI == 1
440 if (presto->retval != ERROR_OK)
442 return ERROR_JTAG_DEVICE_ERROR;
445 if (presto_write(presto->buff_out, presto->buff_out_pos, &ftbytes) != ERROR_OK)
447 presto->buff_out_pos = 0;
448 return ERROR_JTAG_DEVICE_ERROR;
451 presto->total_out += ftbytes;
453 if (presto->buff_out_pos != ftbytes)
455 presto->buff_out_pos = 0;
456 return ERROR_JTAG_DEVICE_ERROR;
459 presto->buff_out_pos = 0;
461 if (presto->buff_in_exp == 0)
464 presto->buff_in_pos = 0;
465 presto->buff_in_len = 0;
467 if (presto_read(presto->buff_in, presto->buff_in_exp, &ftbytes) != ERROR_OK)
469 presto->buff_in_exp = 0;
470 return ERROR_JTAG_DEVICE_ERROR;
473 presto->total_in += ftbytes;
475 if (ftbytes != presto->buff_in_exp)
477 presto->buff_in_exp = 0;
478 return ERROR_JTAG_DEVICE_ERROR;
481 presto->buff_in_len = presto->buff_in_exp;
482 presto->buff_in_exp = 0;
488 int presto_sendbyte(int data)
490 if (data == EOF) return presto_flush();
492 if (presto->buff_out_pos < BUFFER_SIZE)
494 presto->buff_out[presto->buff_out_pos++] = (u8)data;
495 if (((data & 0xC0) == 0x40) || ((data & 0xD0)== 0xD0))
496 presto->buff_in_exp++;
499 return ERROR_JTAG_DEVICE_ERROR;
501 if (presto->buff_out_pos >= BUFFER_SIZE)
502 return presto_flush();
508 int presto_getbyte(void)
510 if (presto->buff_in_pos < presto->buff_in_len)
511 return presto->buff_in[presto->buff_in_pos++];
513 if (presto->buff_in_exp == 0)
516 if (presto_flush() != ERROR_OK)
519 if (presto->buff_in_pos<presto->buff_in_len)
520 return presto->buff_in[presto->buff_in_pos++];
526 /* -------------------------------------------------------------------------- */
529 int presto_bitq_out(int tms, int tdi, int tdo_req)
531 unsigned char cmdparam;
533 if (presto->jtag_tck == 0)
535 presto_sendbyte(0xA4);
536 presto->jtag_tck = 1;
539 else if (!tdo_req && tms == presto->jtag_tms)
541 if (presto->jtag_tdi_count == 0)
542 presto->jtag_tdi_data = (tdi != 0);
544 presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count;
546 if (++presto->jtag_tdi_count == 4)
548 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
549 presto_sendbyte(presto->jtag_tdi_data);
550 presto->jtag_tdi_count = 0;
555 if (presto->jtag_tdi_count)
557 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
558 presto_sendbyte(presto->jtag_tdi_data);
559 presto->jtag_tdi_count = 0;
567 presto_sendbyte( 0xC0 | cmdparam);
569 if (tms != presto->jtag_tms)
572 presto_sendbyte(0xEC);
574 presto_sendbyte(0xE8);
575 presto->jtag_tms = tms;
579 presto_sendbyte(0xD4 | cmdparam);
581 presto_sendbyte(0xC4|cmdparam);
587 int presto_bitq_flush(void)
589 if (presto->jtag_tdi_count)
591 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
592 presto_sendbyte(presto->jtag_tdi_data);
593 presto->jtag_tdi_count = 0;
596 presto_sendbyte(0xCA);
597 presto->jtag_tck = 0;
599 presto_sendbyte(0xA0);
601 return presto_flush();
605 int presto_bitq_in_rdy(void)
607 if (presto->buff_in_pos>=presto->buff_in_len)
609 return presto->buff_in_len-presto->buff_in_pos;
613 int presto_bitq_in(void)
615 if (presto->buff_in_pos>=presto->buff_in_len)
617 if (presto->buff_in[presto->buff_in_pos++]&0x08) return 1;
622 int presto_bitq_sleep(unsigned long us)
633 waits = us / 170 + 2;
635 presto_sendbyte(0x80);
641 int presto_bitq_reset(int trst, int srst)
646 if (presto->jtag_tms)
652 presto_sendbyte(cmd);
657 /* -------------------------------------------------------------------------- */
659 char *presto_speed_text[4] =
667 int presto_jtag_speed(int speed)
670 if ((speed < 0) || (speed > 3))
672 INFO("valid speed values: 0 (3 MHz), 1 (1.5 MHz), 2 (750 kHz) and 3 (93.75 kHz)");
673 return ERROR_INVALID_ARGUMENTS;
677 INFO("setting speed to %d, max. TCK freq. is %s", speed, presto_speed_text[speed]);
678 return presto_sendbyte(0xA8 | speed);
684 int presto_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
690 presto_serial = strdup(args[0]);
694 ERROR("expected exactly one argument to presto_serial <serial-number>");
701 int presto_jtag_register_commands(struct command_context_s *cmd_ctx)
703 register_command(cmd_ctx, NULL, "presto_serial", presto_handle_serial_command,
704 COMMAND_CONFIG, NULL);
709 int presto_jtag_init(void)
711 if (presto_open(presto_serial) != ERROR_OK)
714 if (presto_serial != NULL)
715 ERROR("Cannot open PRESTO, serial number '%s'", presto_serial);
717 ERROR("Cannot open PRESTO");
718 return ERROR_JTAG_INIT_FAILED;
720 INFO("PRESTO open, serial number '%s'", presto->serial);
722 /* use JTAG speed setting from configuration file */
723 presto_jtag_speed(jtag_speed);
725 bitq_interface = &presto_bitq;
730 int presto_jtag_quit(void)
734 INFO("PRESTO closed");
739 presto_serial = NULL;