1
2
3
7
8
9
10
11
12
20
21
22
23
24
32
33
39
40
55
56
57
58
59
60
61
62
63
67
68
69
70
71
72
73
74
75
76
77
78
79
80
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
125
126
127
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
158
159
160
161
162
163
164
168
169
170
171
172
173
177
180
181
182
183
184
185
186
189
192
195
198
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
230
231
232
233
238
239
240
241
242
243
244
245
246
247
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
302
303
304
305
306
307
308
322
323
324
328
329
330
331
332
333
334
335
336
337
338
339
355
356
366
367
368
369
370
371
372
373
374
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <jtag/jtag.h>
#include <jtag/adapter.h>
#include <helper/system.h>
#include <helper/log.h>
#include "pld.h"
#include "raw_bit.h"
6 includes
#define BYPASS 0x3FF
#define USER0 0x00C
#define USER1 0x00E
enum intel_family_e {
INTEL_CYCLONEIII,
INTEL_CYCLONEIV,
INTEL_CYCLONEV,
INTEL_CYCLONE10,
INTEL_ARRIAII,
INTEL_UNKNOWN
...};
struct intel_pld_device {
struct jtag_tap *tap;
unsigned int boundary_scan_length;
int checkpos;
enum intel_family_e family;
...};
static int intel_check_config(struct intel_pld_device *intel_info)
{
if (intel_info->boundary_scan_length == 0) {
LOG_ERROR("unknown boundary scan length. Please specify with 'intel set_bscan'.");
return ERROR_FAIL;
}if (intel_info->boundary_scan_length == 0) { ... }
if (intel_info->checkpos >= 0 && (unsigned int)intel_info->checkpos >= intel_info->boundary_scan_length) {
LOG_ERROR("checkpos has to be smaller than scan length %d < %u",
intel_info->checkpos, intel_info->boundary_scan_length);
return ERROR_FAIL;
}if (intel_info->checkpos >= 0 && (unsigned int)intel_info->checkpos >= intel_info->boundary_scan_length) { ... }
return ERROR_OK;
}{ ... }
static int intel_read_file(struct raw_bit_file *bit_file, const char *filename)
{
if (!filename || !bit_file)
return ERROR_COMMAND_SYNTAX_ERROR;
const char *file_ending_pos = strrchr(filename, '.');
if (!file_ending_pos) {
LOG_ERROR("Unable to detect filename suffix");
return ERROR_PLD_FILE_LOAD_FAILED;
}if (!file_ending_pos) { ... }
if (strcasecmp(file_ending_pos, ".rbf") == 0)
return cpld_read_raw_bit_file(bit_file, filename);
LOG_ERROR("Unable to detect filetype");
return ERROR_PLD_FILE_LOAD_FAILED;
}{ ... }
static int intel_set_instr(struct jtag_tap *tap, uint16_t new_instr)
{
struct scan_field field;
field.num_bits = tap->ir_length;
void *t = calloc(DIV_ROUND_UP(field.num_bits, 8), 1);
if (!t) {
LOG_ERROR("Out of memory");
return ERROR_FAIL;
}if (!t) { ... }
field.out_value = t;
buf_set_u32(t, 0, field.num_bits, new_instr);
field.in_value = NULL;
jtag_add_ir_scan(tap, &field, TAP_IDLE);
free(t);
return ERROR_OK;
}{ ... }
static int intel_load(struct pld_device *pld_device, const char *filename)
{
unsigned int speed = adapter_get_speed_khz();
if (speed < 1)
speed = 1;
unsigned int cycles = DIV_ROUND_UP(speed, 200);
if (cycles < 1)
cycles = 1;
if (!pld_device || !pld_device->driver_priv)
return ERROR_FAIL;
struct intel_pld_device *intel_info = pld_device->driver_priv;
if (!intel_info || !intel_info->tap)
return ERROR_FAIL;
struct jtag_tap *tap = intel_info->tap;
int retval = intel_check_config(intel_info);
if (retval != ERROR_OK)
return retval;
struct raw_bit_file bit_file;
retval = intel_read_file(&bit_file, filename);
if (retval != ERROR_OK)
return retval;
retval = intel_set_instr(tap, 0x002);
if (retval != ERROR_OK) {
free(bit_file.data);
return retval;
}if (retval != ERROR_OK) { ... }
jtag_add_runtest(speed, TAP_IDLE);
retval = jtag_execute_queue();
if (retval != ERROR_OK) {
free(bit_file.data);
return retval;
}if (retval != ERROR_OK) { ... }
struct scan_field field;
field.num_bits = bit_file.length * 8;
field.out_value = bit_file.data;
field.in_value = NULL;
jtag_add_dr_scan(tap, 1, &field, TAP_DRPAUSE);
retval = jtag_execute_queue();
free(bit_file.data);
if (retval != ERROR_OK)
return retval;
retval = intel_set_instr(tap, 0x004);
if (retval != ERROR_OK)
return retval;
jtag_add_runtest(cycles, TAP_IDLE);
retval = jtag_execute_queue();
if (retval != ERROR_OK)
return retval;
if (intel_info->boundary_scan_length != 0) {
uint8_t *buf = calloc(DIV_ROUND_UP(intel_info->boundary_scan_length, 8), 1);
if (!buf) {
LOG_ERROR("Out of memory");
return ERROR_FAIL;
}if (!buf) { ... }
field.num_bits = intel_info->boundary_scan_length;
field.out_value = buf;
field.in_value = buf;
jtag_add_dr_scan(tap, 1, &field, TAP_DRPAUSE);
retval = jtag_execute_queue();
if (retval != ERROR_OK) {
free(buf);
return retval;
}if (retval != ERROR_OK) { ... }
if (intel_info->checkpos != -1)
retval = ((buf[intel_info->checkpos / 8] & (1 << (intel_info->checkpos % 8)))) ?
ERROR_OK : ERROR_FAIL;
free(buf);
if (retval != ERROR_OK) {
LOG_ERROR("Check failed");
return ERROR_FAIL;
}if (retval != ERROR_OK) { ... }
}if (intel_info->boundary_scan_length != 0) { ... } else {
LOG_INFO("unable to check. Please specify with position 'intel set_check_pos'.");
}else { ... }
retval = intel_set_instr(tap, 0x003);
if (retval != ERROR_OK)
return retval;
switch (intel_info->family) {
case INTEL_CYCLONEIII:
case INTEL_CYCLONEIV:
jtag_add_runtest(5 * speed + 512, TAP_IDLE);
break;case INTEL_CYCLONEIV:
case INTEL_CYCLONEV:
jtag_add_runtest(5 * speed + 512, TAP_IDLE);
break;case INTEL_CYCLONEV:
case INTEL_CYCLONE10:
jtag_add_runtest(DIV_ROUND_UP(512ul * speed, 125ul) + 512, TAP_IDLE);
break;case INTEL_CYCLONE10:
case INTEL_ARRIAII:
jtag_add_runtest(DIV_ROUND_UP(64ul * speed, 125ul) + 512, TAP_IDLE);
break;case INTEL_ARRIAII:
case INTEL_UNKNOWN:
LOG_ERROR("unknown family");
return ERROR_FAIL;case INTEL_UNKNOWN:
}switch (intel_info->family) { ... }
retval = intel_set_instr(tap, BYPASS);
if (retval != ERROR_OK)
return retval;
jtag_add_runtest(speed, TAP_IDLE);
return jtag_execute_queue();
}{ ... }
static int intel_get_ipdbg_hub(int user_num, struct pld_device *pld_device, struct pld_ipdbg_hub *hub)
{
if (!pld_device)
return ERROR_FAIL;
struct intel_pld_device *pld_device_info = pld_device->driver_priv;
if (!pld_device_info || !pld_device_info->tap)
return ERROR_FAIL;
hub->tap = pld_device_info->tap;
if (user_num == 0) {
hub->user_ir_code = USER0;
}if (user_num == 0) { ... } else if (user_num == 1) {
hub->user_ir_code = USER1;
}else if (user_num == 1) { ... } else {
LOG_ERROR("intel devices only have user register 0 & 1");
return ERROR_FAIL;
}else { ... }
return ERROR_OK;
}{ ... }
static int intel_get_jtagspi_userircode(struct pld_device *pld_device, unsigned int *ir)
{
*ir = USER1;
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(intel_set_bscan_command_handler)
{
unsigned int boundary_scan_length;
if (CMD_ARGC != 2)
return ERROR_COMMAND_SYNTAX_ERROR;
struct pld_device *pld_device = get_pld_device_by_name_or_numstr(CMD_ARGV[0]);
if (!pld_device) {
command_print(CMD, "pld device '#%s' is out of bounds or unknown", CMD_ARGV[0]);
return ERROR_FAIL;
}if (!pld_device) { ... }
COMMAND_PARSE_NUMBER(uint, CMD_ARGV[1], boundary_scan_length);
struct intel_pld_device *intel_info = pld_device->driver_priv;
if (!intel_info)
return ERROR_FAIL;
intel_info->boundary_scan_length = boundary_scan_length;
return ERROR_OK;
}{ ... }
COMMAND_HANDLER(intel_set_check_pos_command_handler)
{
int checkpos;
if (CMD_ARGC != 2)
return ERROR_COMMAND_SYNTAX_ERROR;
struct pld_device *pld_device = get_pld_device_by_name_or_numstr(CMD_ARGV[0]);
if (!pld_device) {
command_print(CMD, "pld device '#%s' is out of bounds or unknown", CMD_ARGV[0]);
return ERROR_FAIL;
}if (!pld_device) { ... }
COMMAND_PARSE_NUMBER(int, CMD_ARGV[1], checkpos);
struct intel_pld_device *intel_info = pld_device->driver_priv;
if (!intel_info)
return ERROR_FAIL;
intel_info->checkpos = checkpos;
return ERROR_OK;
}{ ... }
PLD_CREATE_COMMAND_HANDLER(intel_pld_create_command)
{
if (CMD_ARGC != 6)
return ERROR_COMMAND_SYNTAX_ERROR;
if (strcmp(CMD_ARGV[2], "-chain-position") != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
struct jtag_tap *tap = jtag_tap_by_string(CMD_ARGV[3]);
if (!tap) {
command_print(CMD, "Tap: %s does not exist", CMD_ARGV[3]);
return ERROR_FAIL;
}if (!tap) { ... }
enum intel_family_e family = INTEL_UNKNOWN;
if (strcmp(CMD_ARGV[4], "-family") != 0)
return ERROR_COMMAND_SYNTAX_ERROR;
if (strcmp(CMD_ARGV[5], "cycloneiii") == 0) {
family = INTEL_CYCLONEIII;
}if (strcmp(CMD_ARGV[5], "cycloneiii") == 0) { ... } else if (strcmp(CMD_ARGV[5], "cycloneiv") == 0) {
family = INTEL_CYCLONEIV;
}else if (strcmp(CMD_ARGV[5], "cycloneiv") == 0) { ... } else if (strcmp(CMD_ARGV[5], "cyclonev") == 0) {
family = INTEL_CYCLONEV;
}else if (strcmp(CMD_ARGV[5], "cyclonev") == 0) { ... } else if (strcmp(CMD_ARGV[5], "cyclone10") == 0) {
family = INTEL_CYCLONE10;
}else if (strcmp(CMD_ARGV[5], "cyclone10") == 0) { ... } else if (strcmp(CMD_ARGV[5], "arriaii") == 0) {
family = INTEL_ARRIAII;
}else if (strcmp(CMD_ARGV[5], "arriaii") == 0) { ... } else {
command_print(CMD, "unknown family");
return ERROR_FAIL;
}else { ... }
struct intel_pld_device *intel_info = malloc(sizeof(struct intel_pld_device));
if (!intel_info) {
LOG_ERROR("Out of memory");
return ERROR_FAIL;
}if (!intel_info) { ... }
intel_info->tap = tap;
intel_info->boundary_scan_length = 0;
intel_info->checkpos = -1;
intel_info->family = family;
pld->driver_priv = intel_info;
return ERROR_OK;
}{ ... }
static const struct command_registration intel_exec_command_handlers[] = {
{
.name = "set_bscan",
.mode = COMMAND_ANY,
.handler = intel_set_bscan_command_handler,
.help = "set boundary scan register length of FPGA",
.usage = "pld_name len",
...}, {
.name = "set_check_pos",
.mode = COMMAND_ANY,
.handler = intel_set_check_pos_command_handler,
.help = "set check_pos of FPGA",
.usage = "pld_name pos",
...},
COMMAND_REGISTRATION_DONE
...};
static const struct command_registration intel_command_handler[] = {
{
.name = "intel",
.mode = COMMAND_ANY,
.help = "intel specific commands",
.usage = "",
.chain = intel_exec_command_handlers,
...},
COMMAND_REGISTRATION_DONE
...};
struct pld_driver intel_pld = {
.name = "intel",
.commands = intel_command_handler,
.pld_create_command = &intel_pld_create_command,
.load = &intel_load,
.get_ipdbg_hub = intel_get_ipdbg_hub,
.get_jtagspi_userircode = intel_get_jtagspi_userircode,
...};