OpenOCD
pld.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "pld.h"
13 #include <sys/stat.h>
14 #include <helper/log.h>
15 #include <helper/replacements.h>
16 #include <helper/time_support.h>
17 
18 
19 static struct pld_driver *pld_drivers[] = {
20  &efinix_pld,
21  &gatemate_pld,
22  &gowin_pld,
23  &intel_pld,
24  &lattice_pld,
25  &virtex2_pld,
26  NULL,
27 };
28 
29 static struct pld_device *pld_devices;
30 
32 {
33  struct pld_device *p;
34  int i = 0;
35 
36  for (p = pld_devices; p; p = p->next) {
37  if (i++ == num) {
38  LOG_WARNING("DEPRECATED: use pld name \"%s\" instead of number %d", p->name, num);
39  return p;
40  }
41  }
42 
43  return NULL;
44 }
45 
47 {
48  for (struct pld_device *p = pld_devices; p; p = p->next) {
49  if (strcmp(p->name, name) == 0)
50  return p;
51  }
52 
53  return NULL;
54 }
55 
57 {
58  struct pld_device *dev = get_pld_device_by_name(str);
59  if (dev)
60  return dev;
61 
62  char *end;
63  unsigned long dev_num = strtoul(str, &end, 0);
64  if (*end || dev_num > INT_MAX) {
65  LOG_ERROR("Invalid argument");
66  return NULL;
67  }
68 
69  return get_pld_device_by_num(dev_num);
70 }
71 
72 
73 int pld_has_jtagspi_instruction(struct pld_device *pld_device, bool *has_instruction)
74 {
75  *has_instruction = false; /* default is using a proxy bitstream */
76 
77  if (!pld_device)
78  return ERROR_FAIL;
79 
81  if (!pld_driver) {
82  LOG_ERROR("pld device has no associated driver");
83  return ERROR_FAIL;
84  }
85 
87  return pld_driver->has_jtagspi_instruction(pld_device, has_instruction);
88  /* else, take the default (proxy bitstream) */
89  return ERROR_OK;
90 }
91 
92 int pld_get_jtagspi_userircode(struct pld_device *pld_device, unsigned int *ir)
93 {
94  if (!pld_device)
95  return ERROR_FAIL;
96 
98  if (!pld_driver) {
99  LOG_ERROR("pld device has no associated driver");
100  return ERROR_FAIL;
101  }
102 
105 
106  return ERROR_FAIL;
107 }
108 
109 int pld_get_jtagspi_stuff_bits(struct pld_device *pld_device, unsigned int *facing_read_bits,
110  unsigned int *trailing_write_bits)
111 {
112  if (!pld_device)
113  return ERROR_FAIL;
114 
116  if (!pld_driver) {
117  LOG_ERROR("pld device has no associated driver");
118  return ERROR_FAIL;
119  }
120 
122  return pld_driver->get_stuff_bits(pld_device, facing_read_bits, trailing_write_bits);
123 
124  return ERROR_OK;
125 }
126 
128 {
129  if (!pld_device)
130  return ERROR_FAIL;
131 
133  if (!pld_driver) {
134  LOG_ERROR("pld device has no associated driver");
135  return ERROR_FAIL;
136  }
137 
140 
141  return ERROR_FAIL;
142 }
143 
145 {
146  if (!pld_device)
147  return ERROR_FAIL;
148 
150  if (!pld_driver) {
151  LOG_ERROR("pld device has no associated driver");
152  return ERROR_FAIL;
153  }
154 
157 
158  return ERROR_FAIL;
159 }
160 
161 COMMAND_HANDLER(handle_pld_create_command)
162 {
163  if (CMD_ARGC < 2)
165 
166  struct pld_driver *pld_driver = NULL;
167 
168  for (int i = 0; pld_drivers[i]; i++) {
169  if (strcmp(CMD_ARGV[1], pld_drivers[i]->name) == 0) {
170  pld_driver = pld_drivers[i];
171  break;
172  }
173  }
174 
175  if (!pld_driver) {
176  LOG_ERROR("pld driver '%s' not found", CMD_ARGV[1]);
177  return ERROR_FAIL; /* exit(-1); */
178  }
179 
181  LOG_ERROR("pld device with name '%s' already exists", CMD_ARGV[0]);
182  return ERROR_FAIL;
183  }
184 
185  struct pld_device *pld_device = malloc(sizeof(struct pld_device));
186  if (!pld_device) {
187  LOG_ERROR("Out of memory");
188  return ERROR_FAIL;
189  }
190 
192  pld_device->next = NULL;
193 
194  int retval = CALL_COMMAND_HANDLER(pld_driver->pld_create_command, pld_device);
195  if (retval != ERROR_OK) {
196  LOG_ERROR("'%s' driver rejected pld device",
197  CMD_ARGV[1]);
198  free(pld_device);
199  return ERROR_OK;
200  }
201  pld_device->name = strdup(CMD_ARGV[0]);
202  if (!pld_device->name) {
203  LOG_ERROR("Out of memory");
204  free(pld_device);
205  return ERROR_FAIL;
206  }
207 
208  /* register pld specific commands */
209  if (pld_driver->commands) {
211  if (retval != ERROR_OK) {
212  LOG_ERROR("couldn't register '%s' commands", CMD_ARGV[1]);
213  free(pld_device->name);
214  free(pld_device);
215  return ERROR_FAIL;
216  }
217  }
218 
219  if (pld_devices) {
220  /* find last pld device */
221  struct pld_device *p = pld_devices;
222  for (; p && p->next; p = p->next)
223  ;
224  if (p)
225  p->next = pld_device;
226  } else {
228  }
229 
230  return ERROR_OK;
231 }
232 
233 COMMAND_HANDLER(handle_pld_devices_command)
234 {
235  struct pld_device *p;
236  int i = 0;
237 
238  if (!pld_devices) {
239  command_print(CMD, "no pld devices configured");
240  return ERROR_OK;
241  }
242 
243  for (p = pld_devices; p; p = p->next)
244  command_print(CMD, "#%i: %s (driver: %s)", i++, p->name, p->driver->name);
245 
246  return ERROR_OK;
247 }
248 
249 COMMAND_HANDLER(handle_pld_load_command)
250 {
251  int retval;
252  struct timeval start, end, duration;
253  struct pld_device *p;
254 
256 
257  if (CMD_ARGC < 2)
259 
261  if (!p) {
262  command_print(CMD, "pld device '#%s' is out of bounds or unknown", CMD_ARGV[0]);
263  return ERROR_OK;
264  }
265 
266  struct stat input_stat;
267  if (stat(CMD_ARGV[1], &input_stat) == -1) {
268  LOG_ERROR("couldn't stat() %s: %s", CMD_ARGV[1], strerror(errno));
270  }
271 
272  if (S_ISDIR(input_stat.st_mode)) {
273  LOG_ERROR("%s is a directory", CMD_ARGV[1]);
275  }
276 
277  if (input_stat.st_size == 0) {
278  LOG_ERROR("Empty file %s", CMD_ARGV[1]);
280  }
281 
282  retval = p->driver->load(p, CMD_ARGV[1]);
283  if (retval != ERROR_OK) {
284  command_print(CMD, "failed loading file %s to pld device %s",
285  CMD_ARGV[1], CMD_ARGV[0]);
286  return retval;
287  } else {
288  gettimeofday(&end, NULL);
289  timeval_subtract(&duration, &end, &start);
290 
291  command_print(CMD, "loaded file %s to pld device %s in %jis %jius",
292  CMD_ARGV[1], CMD_ARGV[0],
293  (intmax_t)duration.tv_sec, (intmax_t)duration.tv_usec);
294  }
295 
296  return ERROR_OK;
297 }
298 
299 static const struct command_registration pld_exec_command_handlers[] = {
300  {
301  .name = "devices",
302  .handler = handle_pld_devices_command,
303  .mode = COMMAND_EXEC,
304  .help = "list configured pld devices",
305  .usage = "",
306  },
307  {
308  .name = "load",
309  .handler = handle_pld_load_command,
310  .mode = COMMAND_EXEC,
311  .help = "load configuration file into PLD",
312  .usage = "pld_name filename",
313  },
315 };
316 
317 static int pld_init(struct command_context *cmd_ctx)
318 {
319  if (!pld_devices)
320  return ERROR_OK;
321 
322  return register_commands(cmd_ctx, "pld", pld_exec_command_handlers);
323 }
324 
325 COMMAND_HANDLER(handle_pld_init_command)
326 {
327  if (CMD_ARGC != 0)
329 
330  static bool pld_initialized;
331  if (pld_initialized) {
332  LOG_INFO("'pld init' has already been called");
333  return ERROR_OK;
334  }
335  pld_initialized = true;
336 
337  LOG_DEBUG("Initializing PLDs...");
338  return pld_init(CMD_CTX);
339 }
340 
341 static const struct command_registration pld_config_command_handlers[] = {
342  {
343  .name = "create",
344  .mode = COMMAND_CONFIG,
345  .handler = handle_pld_create_command,
346  .help = "create a PLD device",
347  .usage = "name.pld driver_name [driver_args ... ]",
348  },
349  {
350  .name = "init",
351  .mode = COMMAND_CONFIG,
352  .handler = handle_pld_init_command,
353  .help = "initialize PLD devices",
354  .usage = ""
355  },
357 };
358 static const struct command_registration pld_command_handler[] = {
359  {
360  .name = "pld",
361  .mode = COMMAND_ANY,
362  .help = "programmable logic device commands",
363  .usage = "",
365  },
367 };
369 {
370  return register_commands(cmd_ctx, NULL, pld_command_handler);
371 }
const char * name
Definition: armv4_5.c:76
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:118
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:274
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
struct pld_driver efinix_pld
Definition: efinix.c:300
struct pld_driver gatemate_pld
Definition: gatemate.c:300
struct pld_driver gowin_pld
Definition: gowin.c:592
struct pld_driver intel_pld
Definition: intel.c:367
struct pld_driver lattice_pld
Definition: lattice.c:653
static int64_t start
Definition: log.c:42
#define LOG_WARNING(expr ...)
Definition: log.h:129
#define ERROR_FAIL
Definition: log.h:170
#define LOG_ERROR(expr ...)
Definition: log.h:132
#define LOG_INFO(expr ...)
Definition: log.h:126
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:164
int pld_has_jtagspi_instruction(struct pld_device *pld_device, bool *has_instruction)
Definition: pld.c:73
COMMAND_HANDLER(handle_pld_create_command)
Definition: pld.c:161
struct pld_device * get_pld_device_by_name(const char *name)
Definition: pld.c:46
struct pld_device * get_pld_device_by_num(int num)
Definition: pld.c:31
struct pld_device * get_pld_device_by_name_or_numstr(const char *str)
Definition: pld.c:56
int pld_get_jtagspi_stuff_bits(struct pld_device *pld_device, unsigned int *facing_read_bits, unsigned int *trailing_write_bits)
Definition: pld.c:109
static int pld_init(struct command_context *cmd_ctx)
Definition: pld.c:317
int pld_get_jtagspi_userircode(struct pld_device *pld_device, unsigned int *ir)
Definition: pld.c:92
static const struct command_registration pld_config_command_handlers[]
Definition: pld.c:341
int pld_connect_spi_to_jtag(struct pld_device *pld_device)
Definition: pld.c:127
static const struct command_registration pld_exec_command_handlers[]
Definition: pld.c:299
static const struct command_registration pld_command_handler[]
Definition: pld.c:358
static struct pld_driver * pld_drivers[]
Definition: pld.c:19
static struct pld_device * pld_devices
Definition: pld.c:29
int pld_register_commands(struct command_context *cmd_ctx)
Definition: pld.c:368
int pld_disconnect_spi_from_jtag(struct pld_device *pld_device)
Definition: pld.c:144
#define ERROR_PLD_FILE_LOAD_FAILED
Definition: pld.h:62
struct pld_driver virtex2_pld
Definition: virtex2.c:472
int gettimeofday(struct timeval *tv, struct timezone *tz)
const char * name
Definition: command.h:235
Definition: pld.h:48
struct pld_device * next
Definition: pld.h:51
struct pld_driver * driver
Definition: pld.h:49
char * name
Definition: pld.h:52
Definition: pld.h:31
int(* connect_spi_to_jtag)(struct pld_device *pld_device)
Definition: pld.h:39
int(* load)(struct pld_device *pld_device, const char *filename)
Definition: pld.h:35
const char * name
Definition: pld.h:32
int(* get_stuff_bits)(struct pld_device *pld_device, unsigned int *facing_read_bits, unsigned int *trailing_write_bits)
Definition: pld.h:41
int(* disconnect_spi_from_jtag)(struct pld_device *pld_device)
Definition: pld.h:40
int(* has_jtagspi_instruction)(struct pld_device *device, bool *has_instruction)
Definition: pld.h:37
int(* get_jtagspi_userircode)(struct pld_device *pld_device, unsigned int *ir)
Definition: pld.h:38
const struct command_registration * commands
Definition: pld.h:34
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
Definition: time_support.c:21
#define NULL
Definition: usb.h:16