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 .speed = presto_jtag_speed,
65 .register_commands = presto_jtag_register_commands,
66 .init = presto_jtag_init,
67 .quit = presto_jtag_quit,
71 int presto_bitq_out(int tms, int tdi, int tdo_req);
72 int presto_bitq_flush(void);
73 int presto_bitq_sleep(unsigned long us);
74 int presto_bitq_reset(int trst, int srst);
75 int presto_bitq_in_rdy(void);
76 int presto_bitq_in(void);
78 bitq_interface_t presto_bitq =
80 .out = presto_bitq_out,
81 .flush = presto_bitq_flush,
82 .sleep = presto_bitq_sleep,
83 .reset = presto_bitq_reset,
84 .in_rdy = presto_bitq_in_rdy,
89 /* -------------------------------------------------------------------------- */
92 #define FT_DEVICE_NAME_LEN 64
93 #define FT_DEVICE_SERNUM_LEN 64
95 #define PRESTO_VID_PID 0x0403f1a0
96 #define PRESTO_VID (0x0403)
97 #define PRESTO_PID (0xf1a0)
99 #define BUFFER_SIZE (64*62)
101 typedef struct presto_s
103 #if BUILD_PRESTO_FTD2XX == 1
106 #elif BUILD_PRESTO_LIBFTDI == 1
107 struct ftdi_context ftdic;
111 char serial[FT_DEVICE_SERNUM_LEN];
113 u8 buff_out[BUFFER_SIZE];
116 u8 buff_in[BUFFER_SIZE];
117 int buff_in_exp; /* expected in buffer length */
118 int buff_in_len; /* length of data received */
121 unsigned long total_out;
122 unsigned long total_in;
124 int jtag_tms; /* last tms state */
125 int jtag_tck; /* last tck state */
132 presto_t presto_state;
133 presto_t *presto = &presto_state;
135 u8 presto_init_seq[] =
137 0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0
140 int presto_write(u8 *buf, int size, u32* bytes_written)
142 #if BUILD_PRESTO_FTD2XX == 1
143 DWORD dw_bytes_written;
144 if ((presto->status = FT_Write(presto->handle, buf, size, &dw_bytes_written)) != FT_OK)
146 *bytes_written = dw_bytes_written;
147 ERROR("FT_Write returned: %lu", presto->status);
148 return ERROR_JTAG_DEVICE_ERROR;
152 *bytes_written = dw_bytes_written;
155 #elif BUILD_PRESTO_LIBFTDI == 1
156 if ((presto->retval = ftdi_write_data(&presto->ftdic, buf, size)) < 0)
159 ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
160 return ERROR_JTAG_DEVICE_ERROR;
164 *bytes_written = retval;
170 int presto_read(u8* buf, int size, u32* bytes_read)
172 #if BUILD_PRESTO_FTD2XX == 1
177 while ((*bytes_read < size) && timeout--)
179 if ((presto->status = FT_Read(presto->handle, buf + *bytes_read, size -
180 *bytes_read, &dw_bytes_read)) != FT_OK)
183 ERROR("FT_Read returned: %lu", presto->status);
184 return ERROR_JTAG_DEVICE_ERROR;
186 *bytes_read += dw_bytes_read;
188 #elif BUILD_PRESTO_LIBFTDI == 1
192 while ((*bytes_read < size) && timeout--)
194 if ((presto->retval = ftdi_read_data(&presto->ftdic, buf + *bytes_read, size - *bytes_read)) < 0)
197 ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
198 return ERROR_JTAG_DEVICE_ERROR;
200 *bytes_read += retval;
204 if (*bytes_read < size)
206 ERROR("couldn't read the requested number of bytes from PRESTO (%i < %i)", *bytes_read, size);
207 return ERROR_JTAG_DEVICE_ERROR;
213 #if BUILD_PRESTO_FTD2XX == 1
214 int presto_open_ftd2xx(char *req_serial)
219 char devname[FT_DEVICE_NAME_LEN];
223 unsigned long ftbytes;
225 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
228 /* Add non-standard Vid/Pid to the linux driver */
229 if ((presto->status = FT_SetVIDPID(PRESTO_VID, PRESTO_PID)) != FT_OK)
231 ERROR("couldn't add PRESTO VID/PID");
236 if ((presto->status = FT_ListDevices(&numdevs, NULL, FT_LIST_NUMBER_ONLY)) != FT_OK)
238 ERROR("FT_ListDevices failed: %i", (int)presto->status);
239 return ERROR_JTAG_INIT_FAILED;
242 for (i = 0; i < numdevs; i++)
244 if (FT_Open(i, &(presto->handle)) != FT_OK)
246 ERROR("FT_Open failed: %i", (int)presto->status);
250 if (FT_GetDeviceInfo(presto->handle, &device, &vidpid,
251 presto->serial, devname, NULL) == FT_OK)
253 if (vidpid == PRESTO_VID_PID
254 && (req_serial == NULL || !strcmp(presto->serial, req_serial)))
258 FT_Close(presto->handle);
259 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
262 if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
263 return ERROR_JTAG_INIT_FAILED;
264 if ((presto->status = FT_SetLatencyTimer(presto->handle, 1)) != FT_OK)
265 return ERROR_JTAG_INIT_FAILED;
266 if ((presto->status = FT_SetTimeouts(presto->handle, 100, 0)) != FT_OK)
267 return ERROR_JTAG_INIT_FAILED;
268 if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
269 return ERROR_JTAG_INIT_FAILED;
272 if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
273 return ERROR_JTAG_INIT_FAILED;
274 if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
275 return ERROR_JTAG_INIT_FAILED;
279 if ((presto->status = FT_SetBitMode(presto->handle, 0x80, 1)) != FT_OK)
280 return ERROR_JTAG_INIT_FAILED;
281 if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
282 return ERROR_JTAG_INIT_FAILED ;
283 if ((presto->status = FT_SetBaudRate(presto->handle, 9600)) != FT_OK)
284 return ERROR_JTAG_INIT_FAILED;
287 for (i = 0; i < 4 * 62; i++)
288 if ((presto->status=FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
289 return ERROR_JTAG_INIT_FAILED;
293 if ((presto->status = FT_SetBitMode(presto->handle, 0x00, 0)) != FT_OK)
294 return ERROR_JTAG_INIT_FAILED;
295 if ((presto->status = FT_Purge(presto->handle, FT_PURGE_TX | FT_PURGE_RX)) != FT_OK)
296 return ERROR_JTAG_INIT_FAILED;
299 if ((presto->status = FT_Write(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
300 return ERROR_JTAG_INIT_FAILED;
301 if ((presto->status = FT_Read(presto->handle, &presto_data, 1, &ftbytes)) != FT_OK)
302 return ERROR_JTAG_INIT_FAILED;
304 return ERROR_JTAG_INIT_FAILED;
307 if ((presto->status = FT_SetTimeouts(presto->handle, 0, 0)) != FT_OK)
308 return ERROR_JTAG_INIT_FAILED;
310 presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
311 if (presto->status != FT_OK)
312 return ERROR_JTAG_INIT_FAILED;
313 if (ftbytes != sizeof(presto_init_seq))
314 return ERROR_JTAG_INIT_FAILED;
319 #elif BUILD_PRESTO_LIBFTDI == 1
320 int presto_open_libftdi(char *req_serial)
325 DEBUG("searching for presto JTAG interface using libftdi");
327 /* context, vendor id, product id */
328 if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0)
330 ERROR("unable to open presto: %s", presto->ftdic.error_str);
331 return ERROR_JTAG_INIT_FAILED;
334 if (ftdi_usb_reset(&presto->ftdic) < 0)
336 ERROR("unable to reset presto device");
337 return ERROR_JTAG_INIT_FAILED;
340 if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0)
342 ERROR("unable to set latency timer");
343 return ERROR_JTAG_INIT_FAILED;
346 if (ftdi_usb_purge_buffers(&presto->ftdic) < 0)
348 ERROR("unable to purge presto buffer");
349 return ERROR_JTAG_INIT_FAILED;
353 if ((presto->retval = presto_write(&presto_data, 1, &ftbytes)) != ERROR_OK)
354 return ERROR_JTAG_INIT_FAILED;
355 if ((presto->retval = presto_read(&presto_data, 1, &ftbytes)) != ERROR_OK)
356 return ERROR_JTAG_INIT_FAILED;
360 #endif /* BUILD_PRESTO_LIBFTDI == 1 */
362 int presto_open(char *req_serial)
364 presto->buff_out_pos=0;
365 presto->buff_in_pos=0;
366 presto->buff_in_len=0;
367 presto->buff_in_exp=0;
374 presto->jtag_tdi_data=0;
375 presto->jtag_tdi_count=0;
377 #if BUILD_PRESTO_FTD2XX == 1
378 return presto_open_ftd2xx(req_serial);
379 #elif BUILD_PRESTO_LIBFTDI == 1
380 return presto_open_libftdi(req_serial);
384 int presto_close(void)
387 int result = ERROR_OK;
389 #if BUID_PRESTO_FTD2XX == 1
390 unsigned long ftbytes;
392 if (presto->handle == (FT_HANDLE)INVALID_HANDLE_VALUE)
395 presto->status = FT_Write(presto->handle, &presto_init_seq, sizeof(presto_init_seq), &ftbytes);
396 if (presto->status != FT_OK)
398 if (ftbytes != sizeof(presto_init_seq))
399 result = PRST_TIMEOUT;
401 if ((presto->status = FT_SetLatencyTimer(presto->handle, 16)) != FT_OK)
404 if ((presto->status = FT_Close(presto->handle)) != FT_OK)
407 presto->handle = (FT_HANDLE)INVALID_HANDLE_VALUE;
409 #elif BUILD_PRESTO_LIBFTDI == 1
411 if ((presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq))) < 0)
413 result = ERROR_JTAG_DEVICE_ERROR;
416 if ((presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16)) < 0)
418 result = ERROR_JTAG_DEVICE_ERROR;
421 ftdi_deinit(&presto->ftdic);
429 int presto_flush(void)
433 if (presto->buff_out_pos == 0)
436 #if BUILD_PRESTO_FTD2XX == 1
437 if (presto->status != FT_OK)
438 #elif BUILD_PRESTO_LIBFTDI == 1
439 if (presto->retval != ERROR_OK)
441 return ERROR_JTAG_DEVICE_ERROR;
444 if (presto_write(presto->buff_out, presto->buff_out_pos, &ftbytes) != ERROR_OK)
446 presto->buff_out_pos = 0;
447 return ERROR_JTAG_DEVICE_ERROR;
450 presto->total_out += ftbytes;
452 if (presto->buff_out_pos != ftbytes)
454 presto->buff_out_pos = 0;
455 return ERROR_JTAG_DEVICE_ERROR;
458 presto->buff_out_pos = 0;
460 if (presto->buff_in_exp == 0)
463 presto->buff_in_pos = 0;
464 presto->buff_in_len = 0;
466 if (presto_read(presto->buff_in, presto->buff_in_exp, &ftbytes) != ERROR_OK)
468 presto->buff_in_exp = 0;
469 return ERROR_JTAG_DEVICE_ERROR;
472 presto->total_in += ftbytes;
474 if (ftbytes != presto->buff_in_exp)
476 presto->buff_in_exp = 0;
477 return ERROR_JTAG_DEVICE_ERROR;
480 presto->buff_in_len = presto->buff_in_exp;
481 presto->buff_in_exp = 0;
487 int presto_sendbyte(int data)
489 if (data == EOF) return presto_flush();
491 if (presto->buff_out_pos < BUFFER_SIZE)
493 presto->buff_out[presto->buff_out_pos++] = (u8)data;
494 if (((data & 0xC0) == 0x40) || ((data & 0xD0)== 0xD0))
495 presto->buff_in_exp++;
498 return ERROR_JTAG_DEVICE_ERROR;
500 if (presto->buff_out_pos >= BUFFER_SIZE)
501 return presto_flush();
507 int presto_getbyte(void)
509 if (presto->buff_in_pos < presto->buff_in_len)
510 return presto->buff_in[presto->buff_in_pos++];
512 if (presto->buff_in_exp == 0)
515 if (presto_flush() != ERROR_OK)
518 if (presto->buff_in_pos<presto->buff_in_len)
519 return presto->buff_in[presto->buff_in_pos++];
525 /* -------------------------------------------------------------------------- */
528 int presto_bitq_out(int tms, int tdi, int tdo_req)
530 unsigned char cmdparam;
532 if (presto->jtag_tck == 0)
534 presto_sendbyte(0xA4);
535 presto->jtag_tck = 1;
538 else if (!tdo_req && tms == presto->jtag_tms)
540 if (presto->jtag_tdi_count == 0)
541 presto->jtag_tdi_data = (tdi != 0);
543 presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count;
545 if (++presto->jtag_tdi_count == 4)
547 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
548 presto_sendbyte(presto->jtag_tdi_data);
549 presto->jtag_tdi_count = 0;
554 if (presto->jtag_tdi_count)
556 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
557 presto_sendbyte(presto->jtag_tdi_data);
558 presto->jtag_tdi_count = 0;
566 presto_sendbyte( 0xC0 | cmdparam);
568 if (tms != presto->jtag_tms)
571 presto_sendbyte(0xEC);
573 presto_sendbyte(0xE8);
574 presto->jtag_tms = tms;
578 presto_sendbyte(0xD4 | cmdparam);
580 presto_sendbyte(0xC4|cmdparam);
586 int presto_bitq_flush(void)
588 if (presto->jtag_tdi_count)
590 presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
591 presto_sendbyte(presto->jtag_tdi_data);
592 presto->jtag_tdi_count = 0;
595 presto_sendbyte(0xCA);
596 presto->jtag_tck = 0;
598 presto_sendbyte(0xA0);
600 return presto_flush();
604 int presto_bitq_in_rdy(void)
606 if (presto->buff_in_pos>=presto->buff_in_len)
608 return presto->buff_in_len-presto->buff_in_pos;
612 int presto_bitq_in(void)
614 if (presto->buff_in_pos>=presto->buff_in_len)
616 if (presto->buff_in[presto->buff_in_pos++]&0x08) return 1;
621 int presto_bitq_sleep(unsigned long us)
632 waits = us / 170 + 2;
634 presto_sendbyte(0x80);
640 int presto_bitq_reset(int trst, int srst)
645 if (presto->jtag_tms)
651 presto_sendbyte(cmd);
656 /* -------------------------------------------------------------------------- */
658 char *presto_speed_text[4] =
666 int presto_jtag_speed(int speed)
669 if ((speed < 0) || (speed > 3))
671 INFO("valid speed values: 0 (3 MHz), 1 (1.5 MHz), 2 (750 kHz) and 3 (93.75 kHz)");
672 return ERROR_INVALID_ARGUMENTS;
676 INFO("setting speed to %d, max. TCK freq. is %s", speed, presto_speed_text[speed]);
677 return presto_sendbyte(0xA8 | speed);
683 int presto_handle_serial_command(struct command_context_s *cmd_ctx, char *cmd, char **args, int argc)
689 presto_serial = strdup(args[0]);
693 ERROR("expected exactly one argument to presto_serial <serial-number>");
700 int presto_jtag_register_commands(struct command_context_s *cmd_ctx)
702 register_command(cmd_ctx, NULL, "presto_serial", presto_handle_serial_command,
703 COMMAND_CONFIG, NULL);
708 int presto_jtag_init(void)
710 if (presto_open(presto_serial) != ERROR_OK)
713 if (presto_serial != NULL)
714 ERROR("Cannot open PRESTO, serial number '%s'", presto_serial);
716 ERROR("Cannot open PRESTO");
717 return ERROR_JTAG_INIT_FAILED;
719 INFO("PRESTO open, serial number '%s'", presto->serial);
721 /* use JTAG speed setting from configuration file */
722 presto_jtag_speed(jtag_speed);
724 bitq_interface = &presto_bitq;
729 int presto_jtag_quit(void)
733 INFO("PRESTO closed");
738 presto_serial = NULL;