1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 /***************************************************************************
4 * Copyright (C) 2006 by Dominic Rath *
5 * Dominic.Rath@gmx.de *
6 ***************************************************************************/
13 #include <helper/log.h>
14 #include <helper/replacements.h>
15 #include <helper/time_support.h>
20 extern struct pld_driver virtex2_pld;
22 static struct pld_driver *pld_drivers[] = {
27 static struct pld_device *pld_devices;
29 struct pld_device *get_pld_device_by_num(int num)
34 for (p = pld_devices; p; p = p->next) {
42 /* pld device <driver> [driver_options ...]
44 COMMAND_HANDLER(handle_pld_device_command)
50 return ERROR_COMMAND_SYNTAX_ERROR;
52 for (i = 0; pld_drivers[i]; i++) {
53 if (strcmp(CMD_ARGV[0], pld_drivers[i]->name) == 0) {
54 struct pld_device *p, *c;
56 /* register pld specific commands */
58 if (pld_drivers[i]->commands) {
59 retval = register_commands(CMD_CTX, NULL, pld_drivers[i]->commands);
60 if (retval != ERROR_OK) {
61 LOG_ERROR("couldn't register '%s' commands", CMD_ARGV[0]);
66 c = malloc(sizeof(struct pld_device));
67 c->driver = pld_drivers[i];
70 retval = CALL_COMMAND_HANDLER(
71 pld_drivers[i]->pld_device_command, c);
72 if (retval != ERROR_OK) {
73 LOG_ERROR("'%s' driver rejected pld device",
79 /* put pld device in linked list */
81 /* find last pld device */
82 for (p = pld_devices; p && p->next; p = p->next)
93 /* no matching pld driver found */
95 LOG_ERROR("pld driver '%s' not found", CMD_ARGV[0]);
102 COMMAND_HANDLER(handle_pld_devices_command)
104 struct pld_device *p;
108 command_print(CMD, "no pld devices configured");
112 for (p = pld_devices; p; p = p->next)
113 command_print(CMD, "#%i: %s", i++, p->driver->name);
118 COMMAND_HANDLER(handle_pld_load_command)
121 struct timeval start, end, duration;
122 struct pld_device *p;
124 gettimeofday(&start, NULL);
127 return ERROR_COMMAND_SYNTAX_ERROR;
130 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], dev_id);
131 p = get_pld_device_by_num(dev_id);
133 command_print(CMD, "pld device '#%s' is out of bounds", CMD_ARGV[0]);
137 retval = p->driver->load(p, CMD_ARGV[1]);
138 if (retval != ERROR_OK) {
139 command_print(CMD, "failed loading file %s to pld device %u",
140 CMD_ARGV[1], dev_id);
145 gettimeofday(&end, NULL);
146 timeval_subtract(&duration, &end, &start);
148 command_print(CMD, "loaded file %s to pld device %u in %jis %jius",
150 (intmax_t)duration.tv_sec, (intmax_t)duration.tv_usec);
156 static const struct command_registration pld_exec_command_handlers[] = {
159 .handler = handle_pld_devices_command,
160 .mode = COMMAND_EXEC,
161 .help = "list configured pld devices",
166 .handler = handle_pld_load_command,
167 .mode = COMMAND_EXEC,
168 .help = "load configuration file into PLD",
169 .usage = "pld_num filename",
171 COMMAND_REGISTRATION_DONE
174 static int pld_init(struct command_context *cmd_ctx)
179 return register_commands(cmd_ctx, "pld", pld_exec_command_handlers);
182 COMMAND_HANDLER(handle_pld_init_command)
185 return ERROR_COMMAND_SYNTAX_ERROR;
187 static bool pld_initialized;
188 if (pld_initialized) {
189 LOG_INFO("'pld init' has already been called");
192 pld_initialized = true;
194 LOG_DEBUG("Initializing PLDs...");
195 return pld_init(CMD_CTX);
198 static const struct command_registration pld_config_command_handlers[] = {
201 .mode = COMMAND_CONFIG,
202 .handler = handle_pld_device_command,
203 .help = "configure a PLD device",
204 .usage = "driver_name [driver_args ... ]",
208 .mode = COMMAND_CONFIG,
209 .handler = handle_pld_init_command,
210 .help = "initialize PLD devices",
213 COMMAND_REGISTRATION_DONE
215 static const struct command_registration pld_command_handler[] = {
219 .help = "programmable logic device commands",
221 .chain = pld_config_command_handlers,
223 COMMAND_REGISTRATION_DONE
225 int pld_register_commands(struct command_context *cmd_ctx)
227 return register_commands(cmd_ctx, NULL, pld_command_handler);