forked from LinuxCNC/mesaflash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
anyio.c
348 lines (314 loc) · 9.87 KB
/
anyio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//
// Copyright (C) 2013-2014 Michael Geszkiewicz
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "anyio.h"
#include "eeprom.h"
#include "bitfile.h"
#include "eth_boards.h"
#include "pci_boards.h"
#include "epp_boards.h"
#include "usb_boards.h"
#include "spi_boards.h"
#include "serial_boards.h"
supported_board_entry_t supported_boards[] = {
{"ETHER", BOARD_ETH | BOARD_WILDCARD},
{"7I92", BOARD_ETH},
{"7I93", BOARD_ETH},
{"7I96", BOARD_ETH},
{"7I80", BOARD_ETH},
{"7I76E", BOARD_ETH},
{"4I74", BOARD_PCI},
{"5I24", BOARD_PCI},
{"5I25", BOARD_PCI},
{"6I24", BOARD_PCI},
{"6I25", BOARD_PCI},
{"5I20", BOARD_PCI},
{"4I65", BOARD_PCI},
{"4I68", BOARD_PCI},
{"5I21", BOARD_PCI},
{"5I22", BOARD_PCI},
{"5I23", BOARD_PCI},
{"4I69", BOARD_PCI},
{"3X20", BOARD_PCI},
{"7I43", BOARD_MULTI_INTERFACE | BOARD_EPP | BOARD_USB},
{"7I90", BOARD_MULTI_INTERFACE | BOARD_EPP | BOARD_SPI | BOARD_SER},
{"7I64", BOARD_MULTI_INTERFACE | BOARD_USB | BOARD_SPI},
{NULL, 0},
};
board_t boards[MAX_BOARDS];
int boards_count;
int anyio_init(board_access_t *access) {
if (access == NULL) {
return -EINVAL;
}
return 0;
}
void anyio_cleanup(board_access_t *access) {
if (access == NULL) {
return;
}
if (access->open_iface & BOARD_ETH)
eth_boards_cleanup(access);
if (access->open_iface & BOARD_PCI)
pci_boards_cleanup(access);
if (access->open_iface & BOARD_EPP)
epp_boards_cleanup(access);
if (access->open_iface & BOARD_USB)
usb_boards_cleanup(access);
if (access->open_iface & BOARD_SPI)
spi_boards_cleanup(access);
if (access->open_iface & BOARD_SER)
serial_boards_cleanup(access);
access->open_iface = 0;
}
int anyio_find_dev(board_access_t *access) {
int i, ret = 0;
supported_board_entry_t *supported_board = NULL;
if (access == NULL) {
return -1;
}
for (i = 0; supported_boards[i].name != NULL; i++) {
if (strncmp(supported_boards[i].name, access->device_name, strlen(supported_boards[i].name)) == 0) {
supported_board = &supported_boards[i];
break;
}
}
if (supported_board == NULL) {
printf("ERROR: Unsupported device %s\n", access->device_name);
return -1;
}
access->open_iface = 0;
if (supported_board->type & BOARD_WILDCARD)
access->open_iface = BOARD_WILDCARD;
if (access->type == BOARD_ANY) {
if (supported_board->type & BOARD_MULTI_INTERFACE) {
printf("ERROR: you must select transport layer for board\n");
return -1;
}
if (supported_board->type & BOARD_ETH) {
ret = eth_boards_init(access);
access->open_iface |= BOARD_ETH;
eth_boards_scan(access);
}
if (supported_board->type & BOARD_PCI) {
ret = pci_boards_init(access);
if (ret < 0) {
return ret;
}
access->open_iface |= BOARD_PCI;
pci_boards_scan(access);
}
if (supported_board->type & BOARD_EPP) {
ret = epp_boards_init(access);
if (ret < 0) {
return ret;
}
access->open_iface |= BOARD_EPP;
epp_boards_scan(access);
}
if (supported_board->type & BOARD_USB) {
ret = usb_boards_init(access);
access->open_iface |= BOARD_USB;
usb_boards_scan(access);
}
if (supported_board->type & BOARD_SPI) {
ret = spi_boards_init(access);
access->open_iface |= BOARD_SPI;
spi_boards_scan(access);
}
if (supported_board->type & BOARD_SER) {
ret = serial_boards_init(access);
access->open_iface |= BOARD_SER;
serial_boards_scan(access);
}
} else {
if (access->type & BOARD_ETH) {
ret = eth_boards_init(access);
access->open_iface |= BOARD_ETH;
eth_boards_scan(access);
}
if (access->type & BOARD_PCI) {
ret = pci_boards_init(access);
if (ret < 0) {
return ret;
}
access->open_iface |= BOARD_PCI;
pci_boards_scan(access);
}
if (access->type & BOARD_EPP) {
ret = epp_boards_init(access);
if (ret < 0) {
return ret;
}
access->open_iface |= BOARD_EPP;
epp_boards_scan(access);
}
if (access->type & BOARD_USB) {
ret = usb_boards_init(access);
access->open_iface |= BOARD_USB;
usb_boards_scan(access);
}
if (access->type & BOARD_SPI) {
ret = spi_boards_init(access);
access->open_iface |= BOARD_SPI;
spi_boards_scan(access);
}
if (access->type & BOARD_SER) {
ret = serial_boards_init(access);
access->open_iface |= BOARD_SER;
serial_boards_scan(access);
}
}
return 0;
}
board_t *anyio_get_dev(board_access_t *access, int board_number) {
int i, j;
if (access == NULL) {
return NULL;
}
for (i = 0, j = 0; i < boards_count; i++) {
board_t *board = NULL;
board = &boards[i];
if (strncmp(access->device_name, board->llio.board_name, strlen(access->device_name)) == 0 || access->open_iface & BOARD_WILDCARD) {
j++;
if (j == board_number) {
return board;
}
}
}
return NULL;
}
int anyio_dev_write_flash(board_t *board, char *bitfile_name, int fallback_flag, int fix_boot_flag) {
int ret;
if (board == NULL) {
return -EINVAL;
}
if (board->llio.write_flash != NULL) {
u32 addr = board->flash_start_address;
if (fallback_flag == 1) {
addr = FALLBACK_ADDRESS;
}
ret = board->llio.write_flash(&(board->llio), bitfile_name, addr, fix_boot_flag);
} else {
printf("ERROR: Board %s doesn't support flash writing.\n", board->llio.board_name);
return -EINVAL;
}
return ret;
}
int anyio_dev_verify_flash(board_t *board, char *bitfile_name, int fallback_flag) {
int ret;
if (board == NULL) {
return -EINVAL;
}
if (board->llio.verify_flash != NULL) {
u32 addr = board->flash_start_address;
if (fallback_flag == 1) {
addr = FALLBACK_ADDRESS;
}
ret = board->llio.verify_flash(&(board->llio), bitfile_name, addr);
} else {
printf("ERROR: Board %s doesn't support flash verification.\n", board->llio.board_name);
return -EINVAL;
}
return ret;
}
int anyio_dev_program_fpga(board_t *board, char *bitfile_name) {
int ret;
if (board == NULL) {
return -EINVAL;
}
if (board->llio.reset == NULL) {
printf("ERROR: Board %s doesn't support FPGA resetting.\n", board->llio.board_name);
return -EINVAL;
}
if (board->llio.program_fpga == NULL) {
printf("ERROR: Board %s doesn't support FPGA programming.\n", board->llio.board_name);
return -EINVAL;
}
ret = board->llio.reset(&(board->llio));
if (ret != 0) {
return ret;
}
board->llio.program_fpga(&(board->llio), bitfile_name);
return 0;
}
int anyio_dev_set_remote_ip(board_t *board, char *lbp16_set_ip_addr) {
int ret;
if (board == NULL) {
return -EINVAL;
}
if ((board->type & BOARD_ETH) == 0) {
return -EPERM;
}
ret = eth_set_remote_ip(lbp16_set_ip_addr);
if (ret == 0) {
printf("Board IP updated successfully.\n");
printf("You must power cycle board to load updated eeprom settings.\n");
}
return ret;
}
int anyio_dev_reload(board_t *board, int fallback_flag) {
if (board == NULL) {
return -EINVAL;
}
if (board->llio.reload == NULL) {
printf("ERROR: Board %s doesn't support FPGA configuration reloading.\n", board->llio.board_name);
return -EINVAL;
}
return board->llio.reload(&(board->llio), fallback_flag);
}
int anyio_dev_reset(board_t *board) {
if (board == NULL) {
return -EINVAL;
}
if (board->llio.reset == NULL) {
printf("ERROR: Board %s doesn't support FPGA resetting.\n", board->llio.board_name);
return -EINVAL;
}
return board->llio.reset(&(board->llio));
}
void anyio_dev_print_hm2_info(board_t *board, int xml_flag) {
if (board == NULL) {
return;
}
hm2_read_idrom(&(board->llio.hm2));
hm2_print_pin_file(&(board->llio), xml_flag);
}
void anyio_dev_print_sserial_info(board_t *board) {
if (board == NULL) {
return;
}
hm2_read_idrom(&(board->llio.hm2));
sserial_module_init(&(board->llio));
}
void anyio_bitfile_print_info(char *bitfile_name, int verbose_flag) {
FILE *fp;
char part_name[32];
if (bitfile_name == NULL) {
return;
}
fp = fopen(bitfile_name, "rb");
if (fp == NULL) {
printf("Can't open file %s: %s\n", bitfile_name, strerror(errno));
return;
}
print_bitfile_header(fp, (char*) &part_name, verbose_flag);
fclose(fp);
}