OpenOCD
presto.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2007 by Pavel Chromy *
5  * chromy@asix.cz *
6  ***************************************************************************/
7 
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16 
17 #if IS_CYGWIN == 1
18 #include "windows.h"
19 #endif
20 
21 #include <jtag/adapter.h>
22 #include <jtag/interface.h>
23 #include <helper/time_support.h>
24 #include "bitq.h"
25 
26 /* PRESTO access library includes */
27 #include "libftdi_helper.h"
28 
29 /* -------------------------------------------------------------------------- */
30 
31 #define FT_DEVICE_NAME_LEN 64
32 #define FT_DEVICE_SERNUM_LEN 64
33 
34 #define PRESTO_VID_PID 0x0403f1a0
35 #define PRESTO_VID (0x0403)
36 #define PRESTO_PID (0xf1a0)
37 
38 #define BUFFER_SIZE (64*62)
39 
40 struct presto {
41  struct ftdi_context ftdic;
42  int retval;
43 
45 
48 
49  uint8_t buff_in[BUFFER_SIZE];
50  int buff_in_exp;/* expected in buffer length */
51  int buff_in_len;/* length of data received */
53 
54  unsigned long total_out;
55  unsigned long total_in;
56 
57  int jtag_tms; /* last tms state */
58  int jtag_tck; /* last tck state */
59  int jtag_rst; /* last trst state */
60 
63 
65 };
66 
67 static struct presto presto_state;
68 static struct presto *presto = &presto_state;
69 
70 static uint8_t presto_init_seq[] = {
71  0x80, 0xA0, 0xA8, 0xB0, 0xC0, 0xE0
72 };
73 
74 static int presto_write(uint8_t *buf, uint32_t size)
75 {
76  uint32_t ftbytes;
77  presto->retval = ftdi_write_data(&presto->ftdic, buf, size);
78  if (presto->retval < 0) {
79  LOG_ERROR("ftdi_write_data: %s", ftdi_get_error_string(&presto->ftdic));
81  }
82  ftbytes = presto->retval;
83 
84  if (ftbytes != size) {
85  LOG_ERROR("couldn't write the requested number of bytes to PRESTO (%u < %u)",
86  (unsigned)ftbytes, (unsigned)size);
88  }
89 
90  return ERROR_OK;
91 }
92 
93 static int presto_read(uint8_t *buf, uint32_t size)
94 {
95  uint32_t ftbytes = 0;
96 
97  // 1 second timeout
98  int64_t then = timeval_ms() + 1000;
99 
100  while (ftbytes < size) {
101  presto->retval = ftdi_read_data(&presto->ftdic, buf + ftbytes, size - ftbytes);
102  if (presto->retval < 0) {
103  LOG_ERROR("ftdi_read_data: %s", ftdi_get_error_string(&presto->ftdic));
105  }
106  ftbytes += presto->retval;
107 
108  if (timeval_ms() > then)
109  break;
110  }
111 
112  if (ftbytes != size) {
113  /* this is just a warning, there might have been timeout when detecting PRESTO,
114  *which is not fatal */
115  LOG_WARNING("couldn't read the requested number of bytes from PRESTO (%u < %u)",
116  (unsigned)ftbytes, (unsigned)size);
118  }
119 
120  return ERROR_OK;
121 }
122 
123 static int presto_open_libftdi(const char *req_serial)
124 {
125  uint8_t presto_data;
126 
127  LOG_DEBUG("searching for PRESTO using libftdi");
128 
129  /* initialize FTDI context structure */
130  if (ftdi_init(&presto->ftdic) < 0) {
131  LOG_ERROR("unable to init libftdi: %s", presto->ftdic.error_str);
133  }
134 
135  /* context, vendor id, product id */
136  if (ftdi_usb_open_desc(&presto->ftdic, PRESTO_VID, PRESTO_PID, NULL, req_serial) < 0) {
137  LOG_ERROR("unable to open PRESTO: %s", presto->ftdic.error_str);
139  }
140 
141  if (ftdi_usb_reset(&presto->ftdic) < 0) {
142  LOG_ERROR("unable to reset PRESTO device");
144  }
145 
146  if (ftdi_set_latency_timer(&presto->ftdic, 1) < 0) {
147  LOG_ERROR("unable to set latency timer");
149  }
150 
151  if (ftdi_tcioflush(&presto->ftdic) < 0) {
152  LOG_ERROR("unable to flush PRESTO buffers");
154  }
155 
156  presto_data = 0xD0;
157  if (presto_write(&presto_data, 1) != ERROR_OK) {
158  LOG_ERROR("error writing to PRESTO");
160  }
161 
162  if (presto_read(&presto_data, 1) != ERROR_OK) {
163  LOG_DEBUG("no response from PRESTO, retrying");
164 
165  if (ftdi_tcioflush(&presto->ftdic) < 0)
167 
168  presto_data = 0xD0;
169  if (presto_write(&presto_data, 1) != ERROR_OK)
171 
172  if (presto_read(&presto_data, 1) != ERROR_OK) {
173  LOG_ERROR("no response from PRESTO, giving up");
175  }
176  }
177 
179  LOG_ERROR("error writing PRESTO init sequence");
181  }
182 
183  return ERROR_OK;
184 }
185 
186 static int presto_open(const char *req_serial)
187 {
188  presto->buff_out_pos = 0;
189  presto->buff_in_pos = 0;
190  presto->buff_in_len = 0;
191  presto->buff_in_exp = 0;
192 
193  presto->total_out = 0;
194  presto->total_in = 0;
195 
196  presto->jtag_tms = 0;
197  presto->jtag_tck = 0;
198  presto->jtag_rst = 0;
199  presto->jtag_tdi_data = 0;
200  presto->jtag_tdi_count = 0;
201 
202  presto->jtag_speed = 0;
203 
204  return presto_open_libftdi(req_serial);
205 }
206 
207 static int presto_close(void)
208 {
209 
210  int result = ERROR_OK;
211 
212  presto->retval = ftdi_write_data(&presto->ftdic, presto_init_seq, sizeof(presto_init_seq));
213  if (presto->retval != sizeof(presto_init_seq))
214  result = ERROR_JTAG_DEVICE_ERROR;
215 
216  presto->retval = ftdi_set_latency_timer(&presto->ftdic, 16);
217  if (presto->retval < 0)
218  result = ERROR_JTAG_DEVICE_ERROR;
219 
220  presto->retval = ftdi_usb_close(&presto->ftdic);
221  if (presto->retval < 0)
222  result = ERROR_JTAG_DEVICE_ERROR;
223  else
224  ftdi_deinit(&presto->ftdic);
225 
226  return result;
227 }
228 
229 static int presto_flush(void)
230 {
231  if (presto->buff_out_pos == 0)
232  return ERROR_OK;
233 
234  if (presto->retval < 0) {
235  LOG_DEBUG("error in previous communication, canceling I/O operation");
237  }
238 
240  presto->buff_out_pos = 0;
242  }
243 
245  presto->buff_out_pos = 0;
246 
247  if (presto->buff_in_exp == 0)
248  return ERROR_OK;
249 
250  presto->buff_in_pos = 0;
251  presto->buff_in_len = 0;
252 
254  presto->buff_in_exp = 0;
256  }
257 
260  presto->buff_in_exp = 0;
261 
262  return ERROR_OK;
263 }
264 
265 static int presto_sendbyte(int data)
266 {
267  if (data == EOF)
268  return presto_flush();
269 
271  presto->buff_out[presto->buff_out_pos++] = (uint8_t)data;
272  if (((data & 0xC0) == 0x40) || ((data & 0xD0) == 0xD0))
273  presto->buff_in_exp++;
274  } else
276 
277  /* libftdi does not do background read, be sure that USB IN buffer does not overflow (128
278  *bytes only!) */
279  if (presto->buff_out_pos >= BUFFER_SIZE || presto->buff_in_exp == 128)
280  return presto_flush();
281 
282  return ERROR_OK;
283 }
284 
285 #if 0
286 static int presto_getbyte(void)
287 {
289  return presto->buff_in[presto->buff_in_pos++];
290 
291  if (presto->buff_in_exp == 0)
292  return -1;
293 
294  if (presto_flush() != ERROR_OK)
295  return -1;
296 
298  return presto->buff_in[presto->buff_in_pos++];
299 
300  return -1;
301 }
302 #endif
303 
304 /* -------------------------------------------------------------------------- */
305 
306 static int presto_tdi_flush(void)
307 {
308  if (presto->jtag_tdi_count == 0)
309  return 0;
310 
311  if (presto->jtag_tck == 0) {
312  LOG_ERROR("BUG: unexpected TAP condition, TCK low");
313  return -1;
314  }
315 
316  presto->jtag_tdi_data |= (presto->jtag_tdi_count - 1) << 4;
318  presto->jtag_tdi_count = 0;
319  presto->jtag_tdi_data = 0;
320 
321  return 0;
322 }
323 
324 static int presto_tck_idle(void)
325 {
326  if (presto->jtag_tck == 1) {
327  presto_sendbyte(0xCA);
328  presto->jtag_tck = 0;
329  }
330 
331  return 0;
332 }
333 
334 /* -------------------------------------------------------------------------- */
335 
336 static int presto_bitq_out(int tms, int tdi, int tdo_req)
337 {
338  int i;
339  unsigned char cmd;
340 
341  if (presto->jtag_tck == 0)
342  presto_sendbyte(0xA4); /* LED indicator - JTAG active */
343  else if (presto->jtag_speed == 0 && !tdo_req && tms == presto->jtag_tms) {
344  presto->jtag_tdi_data |= (tdi != 0) << presto->jtag_tdi_count;
345 
346  if (++presto->jtag_tdi_count == 4)
348 
349  return 0;
350  }
351 
353 
354  cmd = tdi ? 0xCB : 0xCA;
356 
357  if (tms != presto->jtag_tms) {
358  presto_sendbyte((tms ? 0xEC : 0xE8) | (presto->jtag_rst ? 0x02 : 0));
359  presto->jtag_tms = tms;
360  }
361 
362  /* delay with TCK low */
363  for (i = presto->jtag_speed; i > 1; i--)
365 
366  cmd |= 0x04;
367  presto_sendbyte(cmd | (tdo_req ? 0x10 : 0));
368 
369  /* delay with TCK high */
370  for (i = presto->jtag_speed; i > 1; i--)
372 
373  presto->jtag_tck = 1;
374 
375  return 0;
376 }
377 
378 static int presto_bitq_flush(void)
379 {
381  presto_tck_idle();
382 
383  presto_sendbyte(0xA0); /* LED indicator - JTAG idle */
384 
385  return presto_flush();
386 }
387 
388 static int presto_bitq_in_rdy(void)
389 {
391  return 0;
393 }
394 
395 static int presto_bitq_in(void)
396 {
398  return -1;
399  if (presto->buff_in[presto->buff_in_pos++]&0x08)
400  return 1;
401  return 0;
402 }
403 
404 static int presto_bitq_sleep(unsigned long us)
405 {
406  long waits;
407 
409  presto_tck_idle();
410 
411  if (us > 100000) {
413  jtag_sleep(us);
414  return 0;
415  }
416 
417  waits = us / 170 + 2;
418  while (waits--)
419  presto_sendbyte(0x80);
420 
421  return 0;
422 }
423 
424 static int presto_bitq_reset(int trst, int srst)
425 {
427  presto_tck_idle();
428 
429  /* add a delay after possible TCK transition */
430  presto_sendbyte(0x80);
431  presto_sendbyte(0x80);
432 
433  presto->jtag_rst = trst || srst;
434  presto_sendbyte((presto->jtag_rst ? 0xEA : 0xE8) | (presto->jtag_tms ? 0x04 : 0));
435 
436  return 0;
437 }
438 
439 static struct bitq_interface presto_bitq = {
440  .out = &presto_bitq_out,
441  .flush = &presto_bitq_flush,
442  .sleep = &presto_bitq_sleep,
443  .reset = &presto_bitq_reset,
444  .in_rdy = &presto_bitq_in_rdy,
445  .in = &presto_bitq_in,
446 };
447 
448 /* -------------------------------------------------------------------------- */
449 
450 static int presto_adapter_khz(int khz, int *jtag_speed)
451 {
452  if (khz <= 0) {
453  *jtag_speed = 0;
455  }
456 
457  if (khz >= 3000)
458  *jtag_speed = 0;
459  else
460  *jtag_speed = (1000 + khz-1)/khz;
461 
462  return 0;
463 }
464 
465 static int presto_jtag_speed_div(int speed, int *khz)
466 {
467  if ((speed < 0) || (speed > 1000)) {
468  *khz = 0;
470  }
471 
472  if (speed == 0)
473  *khz = 3000;
474  else
475  *khz = 1000/speed;
476 
477  return 0;
478 }
479 
480 static int presto_jtag_speed(int speed)
481 {
482  int khz;
483 
484  if (presto_jtag_speed_div(speed, &khz))
486 
487  presto->jtag_speed = speed;
488 
489  if (khz%1000 == 0)
490  LOG_INFO("setting speed to %d, max. TCK freq. is %d MHz", speed, khz/1000);
491  else
492  LOG_INFO("setting speed to %d, max. TCK freq. is %d kHz", speed, khz);
493 
494  return 0;
495 }
496 
497 static int presto_jtag_init(void)
498 {
499  const char *presto_serial = adapter_get_required_serial();
500 
501  if (presto_open(presto_serial) != ERROR_OK) {
502  presto_close();
503  if (presto_serial)
504  LOG_ERROR("Cannot open PRESTO, serial number '%s'", presto_serial);
505  else
506  LOG_ERROR("Cannot open PRESTO");
507  return ERROR_JTAG_INIT_FAILED;
508  }
509  LOG_INFO("PRESTO open, serial number '%s'", presto->serial);
510 
512  return ERROR_OK;
513 }
514 
515 static int presto_jtag_quit(void)
516 {
517  bitq_cleanup();
518  presto_close();
519  LOG_INFO("PRESTO closed");
520  return ERROR_OK;
521 }
522 
523 static struct jtag_interface presto_interface = {
525 };
526 
528  .name = "presto",
529  .transport_ids = TRANSPORT_JTAG,
530  .transport_preferred_id = TRANSPORT_JTAG,
531 
532  .init = presto_jtag_init,
533  .quit = presto_jtag_quit,
534  .speed = presto_jtag_speed,
535  .khz = presto_adapter_khz,
536  .speed_div = presto_jtag_speed_div,
537 
538  .jtag_ops = &presto_interface,
539 };
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Definition: adapter.c:309
int bitq_execute_queue(struct jtag_command *cmd_queue)
Definition: bitq.c:200
void bitq_cleanup(void)
Definition: bitq.c:279
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:405
uint32_t size
Size of dw_spi_transaction::buffer.
Definition: dw-spi-helper.h:4
void jtag_sleep(uint32_t us)
Definition: jtag/core.c:1074
#define ERROR_JTAG_DEVICE_ERROR
Definition: jtag.h:558
#define ERROR_JTAG_INIT_FAILED
Definition: jtag.h:552
static int ftdi_tcioflush(struct ftdi_context *ftdi)
#define LOG_WARNING(expr ...)
Definition: log.h:144
#define LOG_ERROR(expr ...)
Definition: log.h:147
#define LOG_INFO(expr ...)
Definition: log.h:141
#define LOG_DEBUG(expr ...)
Definition: log.h:124
#define ERROR_OK
Definition: log.h:182
static int presto_bitq_reset(int trst, int srst)
Definition: presto.c:424
static struct jtag_interface presto_interface
Definition: presto.c:523
static uint8_t presto_init_seq[]
Definition: presto.c:70
static int presto_jtag_speed_div(int speed, int *khz)
Definition: presto.c:465
static int presto_adapter_khz(int khz, int *jtag_speed)
Definition: presto.c:450
static struct presto presto_state
Definition: presto.c:67
static int presto_bitq_flush(void)
Definition: presto.c:378
static int presto_bitq_in(void)
Definition: presto.c:395
static int presto_bitq_sleep(unsigned long us)
Definition: presto.c:404
static int presto_open(const char *req_serial)
Definition: presto.c:186
static int presto_tdi_flush(void)
Definition: presto.c:306
static int presto_jtag_quit(void)
Definition: presto.c:515
#define BUFFER_SIZE
Definition: presto.c:38
static int presto_flush(void)
Definition: presto.c:229
static int presto_tck_idle(void)
Definition: presto.c:324
#define PRESTO_VID
Definition: presto.c:35
static int presto_jtag_init(void)
Definition: presto.c:497
struct adapter_driver presto_adapter_driver
Definition: presto.c:527
static int presto_jtag_speed(int speed)
Definition: presto.c:480
static int presto_bitq_out(int tms, int tdi, int tdo_req)
Definition: presto.c:336
static int presto_sendbyte(int data)
Definition: presto.c:265
static struct bitq_interface presto_bitq
Definition: presto.c:439
static int presto_bitq_in_rdy(void)
Definition: presto.c:388
static int presto_write(uint8_t *buf, uint32_t size)
Definition: presto.c:74
static int presto_read(uint8_t *buf, uint32_t size)
Definition: presto.c:93
#define PRESTO_PID
Definition: presto.c:36
static int presto_open_libftdi(const char *req_serial)
Definition: presto.c:123
#define FT_DEVICE_SERNUM_LEN
Definition: presto.c:32
static int presto_close(void)
Definition: presto.c:207
Represents a driver for a debugging interface.
Definition: interface.h:208
const char *const name
The name of the interface driver.
Definition: interface.h:210
int(* out)(int tms, int tdi, int tdo_req)
Definition: bitq.h:15
Represents a driver for a debugging interface.
Definition: interface.h:183
int(* execute_queue)(struct jtag_command *cmd_queue)
Execute commands in the supplied queue.
Definition: interface.h:196
Definition: presto.c:40
uint8_t buff_out[BUFFER_SIZE]
Definition: presto.c:46
int jtag_rst
Definition: presto.c:59
int jtag_tck
Definition: presto.c:58
uint8_t buff_in[BUFFER_SIZE]
Definition: presto.c:49
int retval
Definition: presto.c:42
int jtag_tms
Definition: presto.c:57
struct ftdi_context ftdic
Definition: presto.c:41
int jtag_tdi_data
Definition: presto.c:61
unsigned long total_in
Definition: presto.c:55
int buff_in_pos
Definition: presto.c:52
int jtag_tdi_count
Definition: presto.c:62
int buff_in_len
Definition: presto.c:51
char serial[FT_DEVICE_SERNUM_LEN]
Definition: presto.c:44
int jtag_speed
Definition: presto.c:64
unsigned long total_out
Definition: presto.c:54
int buff_out_pos
Definition: presto.c:47
int buff_in_exp
Definition: presto.c:50
int64_t timeval_ms(void)
#define TRANSPORT_JTAG
Definition: transport.h:19
#define NULL
Definition: usb.h:16
uint8_t cmd
Definition: vdebug.c:1