openocd: fix SPDX tag format for files .c
[openocd.git] / src / pld / pld.c
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 <helper/log.h>
14 #include <helper/replacements.h>
15 #include <helper/time_support.h>
16
17
18 /* pld drivers
19 */
20 extern struct pld_driver virtex2_pld;
21
22 static struct pld_driver *pld_drivers[] = {
23 &virtex2_pld,
24 NULL,
25 };
26
27 static struct pld_device *pld_devices;
28
29 struct pld_device *get_pld_device_by_num(int num)
30 {
31 struct pld_device *p;
32 int i = 0;
33
34 for (p = pld_devices; p; p = p->next) {
35 if (i++ == num)
36 return p;
37 }
38
39 return NULL;
40 }
41
42 /* pld device <driver> [driver_options ...]
43 */
44 COMMAND_HANDLER(handle_pld_device_command)
45 {
46 int i;
47 int found = 0;
48
49 if (CMD_ARGC < 1)
50 return ERROR_COMMAND_SYNTAX_ERROR;
51
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;
55
56 /* register pld specific commands */
57 int retval;
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]);
62 return ERROR_FAIL;
63 }
64 }
65
66 c = malloc(sizeof(struct pld_device));
67 c->driver = pld_drivers[i];
68 c->next = NULL;
69
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",
74 CMD_ARGV[0]);
75 free(c);
76 return ERROR_OK;
77 }
78
79 /* put pld device in linked list */
80 if (pld_devices) {
81 /* find last pld device */
82 for (p = pld_devices; p && p->next; p = p->next)
83 ;
84 if (p)
85 p->next = c;
86 } else
87 pld_devices = c;
88
89 found = 1;
90 }
91 }
92
93 /* no matching pld driver found */
94 if (!found) {
95 LOG_ERROR("pld driver '%s' not found", CMD_ARGV[0]);
96 exit(-1);
97 }
98
99 return ERROR_OK;
100 }
101
102 COMMAND_HANDLER(handle_pld_devices_command)
103 {
104 struct pld_device *p;
105 int i = 0;
106
107 if (!pld_devices) {
108 command_print(CMD, "no pld devices configured");
109 return ERROR_OK;
110 }
111
112 for (p = pld_devices; p; p = p->next)
113 command_print(CMD, "#%i: %s", i++, p->driver->name);
114
115 return ERROR_OK;
116 }
117
118 COMMAND_HANDLER(handle_pld_load_command)
119 {
120 int retval;
121 struct timeval start, end, duration;
122 struct pld_device *p;
123
124 gettimeofday(&start, NULL);
125
126 if (CMD_ARGC < 2)
127 return ERROR_COMMAND_SYNTAX_ERROR;
128
129 unsigned dev_id;
130 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], dev_id);
131 p = get_pld_device_by_num(dev_id);
132 if (!p) {
133 command_print(CMD, "pld device '#%s' is out of bounds", CMD_ARGV[0]);
134 return ERROR_OK;
135 }
136
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);
141 switch (retval) {
142 }
143 return retval;
144 } else {
145 gettimeofday(&end, NULL);
146 timeval_subtract(&duration, &end, &start);
147
148 command_print(CMD, "loaded file %s to pld device %u in %jis %jius",
149 CMD_ARGV[1], dev_id,
150 (intmax_t)duration.tv_sec, (intmax_t)duration.tv_usec);
151 }
152
153 return ERROR_OK;
154 }
155
156 static const struct command_registration pld_exec_command_handlers[] = {
157 {
158 .name = "devices",
159 .handler = handle_pld_devices_command,
160 .mode = COMMAND_EXEC,
161 .help = "list configured pld devices",
162 .usage = "",
163 },
164 {
165 .name = "load",
166 .handler = handle_pld_load_command,
167 .mode = COMMAND_EXEC,
168 .help = "load configuration file into PLD",
169 .usage = "pld_num filename",
170 },
171 COMMAND_REGISTRATION_DONE
172 };
173
174 static int pld_init(struct command_context *cmd_ctx)
175 {
176 if (!pld_devices)
177 return ERROR_OK;
178
179 return register_commands(cmd_ctx, "pld", pld_exec_command_handlers);
180 }
181
182 COMMAND_HANDLER(handle_pld_init_command)
183 {
184 if (CMD_ARGC != 0)
185 return ERROR_COMMAND_SYNTAX_ERROR;
186
187 static bool pld_initialized;
188 if (pld_initialized) {
189 LOG_INFO("'pld init' has already been called");
190 return ERROR_OK;
191 }
192 pld_initialized = true;
193
194 LOG_DEBUG("Initializing PLDs...");
195 return pld_init(CMD_CTX);
196 }
197
198 static const struct command_registration pld_config_command_handlers[] = {
199 {
200 .name = "device",
201 .mode = COMMAND_CONFIG,
202 .handler = handle_pld_device_command,
203 .help = "configure a PLD device",
204 .usage = "driver_name [driver_args ... ]",
205 },
206 {
207 .name = "init",
208 .mode = COMMAND_CONFIG,
209 .handler = handle_pld_init_command,
210 .help = "initialize PLD devices",
211 .usage = ""
212 },
213 COMMAND_REGISTRATION_DONE
214 };
215 static const struct command_registration pld_command_handler[] = {
216 {
217 .name = "pld",
218 .mode = COMMAND_ANY,
219 .help = "programmable logic device commands",
220 .usage = "",
221 .chain = pld_config_command_handlers,
222 },
223 COMMAND_REGISTRATION_DONE
224 };
225 int pld_register_commands(struct command_context *cmd_ctx)
226 {
227 return register_commands(cmd_ctx, NULL, pld_command_handler);
228 }

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)