-
Notifications
You must be signed in to change notification settings - Fork 2
/
hdmitest.cpp
392 lines (319 loc) · 8.49 KB
/
hdmitest.cpp
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#ifdef linux
#include <sys/ioctl.h>
#include <linux/input.h>
#endif
#include <QDebug>
#include "hdmitest.h"
#include "gpio.h"
static const uint8_t zerobytes_6slx9csg324[] =
#include "zerobytes-6slx9csg324.h"
;
static const uint8_t zerobytes_6slx45csg324[] =
#include "zerobytes-6slx45csg324.h"
;
#define LX9_FIRMWARE "/lib/firmware/kovan-lx9.bit"
#define LX45_FIRMWARE "/lib/firmware/kovan-lx45.bit"
#define LX45_JTAG 0x34008093
#define LX9_JTAG 0x24001093
#define TDO_GPIO 16
#define TMS_GPIO 18
#define TCK_GPIO 20
#define TDI_GPIO 34
#define GPIO_PATH "/sys/class/gpio"
#define EXPORT_PATH GPIO_PATH "/export"
#define UNEXPORT_PATH GPIO_PATH "/unexport"
#define FPGA_IOC_MAGIC 'c'
#define FPGA_IOCWTEST _IOW(FPGA_IOC_MAGIC, 1, int)
#define FPGA_IOCRTEST _IOR(FPGA_IOC_MAGIC, 2, int)
#define FPGA_IOCRESET _IOW(FPGA_IOC_MAGIC, 3, int)
#define FPGA_IOCLED0 _IOW(FPGA_IOC_MAGIC, 4, int)
#define FPGA_IOCLED1 _IOW(FPGA_IOC_MAGIC, 5, int)
#define FPGA_IOCDONE _IOR(FPGA_IOC_MAGIC, 6, int)
#define FPGA_IOCINIT _IOR(FPGA_IOC_MAGIC, 7, int)
struct jtag_state {
int tdi;
int tms;
int tck;
int tdo;
};
/* Wiggle the TCK like, moving JTAG one step further along its state machine */
static int jtag_tick(struct jtag_state *state) {
gpio_set_value(state->tck, 0);
gpio_set_value(state->tck, 1);
gpio_set_value(state->tck, 0);
return 0;
}
/* Send five 1s through JTAG, which will bring it into reset state */
static int jtag_reset(struct jtag_state *state) {
int i;
for (i=0; i<5; i++) {
gpio_set_value(state->tms, 1);
jtag_tick(state);
}
return 0;
}
static int jtag_open(struct jtag_state *state) {
gpio_export(TDI_GPIO);
gpio_export(TMS_GPIO);
gpio_export(TCK_GPIO);
gpio_export(TDO_GPIO);
gpio_set_direction(TDI_GPIO, 0);
gpio_set_direction(TMS_GPIO, 1);
gpio_set_direction(TCK_GPIO, 1);
gpio_set_direction(TDO_GPIO, 1);
gpio_set_value(TDO_GPIO, 0);
gpio_set_value(TMS_GPIO, 0);
gpio_set_value(TCK_GPIO, 0);
state->tdi = TDI_GPIO;
state->tms = TMS_GPIO;
state->tck = TCK_GPIO;
state->tdo = TDO_GPIO;
jtag_reset(state);
return 0;
}
/* Reads the ID CODE out of the FPGA
* When the state machine is reset, the sequence 0, 1, 0, 0 will move
* it to a point where continually reading the TDO line will yield the
* ID code.
*
* This is because by default, the reset command loads the chip's ID
* into the data register, so all we have to do is read it out.
*/
static uint32_t jtag_idcode(struct jtag_state *state) {
int i;
uint32_t val = 0;
/* Reset the state machine */
jtag_reset(state);
/* Get into "Run-Test/ Idle" state */
gpio_set_value(state->tms, 0);
jtag_tick(state);
/* Get into "Select DR-Scan" state */
gpio_set_value(state->tms, 1);
jtag_tick(state);
/* Get into "Capture DR" state */
gpio_set_value(state->tms, 0);
jtag_tick(state);
/* Get into "Shift-DR" state */
gpio_set_value(state->tms, 0);
jtag_tick(state);
/* Read the code out */
for (i=0; i<32; i++) {
int ret = gpio_get_value(state->tdi);
val |= (ret<<i);
jtag_tick(state);
}
return val;
}
/* Close GPIOs and return everything to how it was */
static void jtag_cleanup(struct jtag_state *state) {
gpio_unexport(state->tdi);
gpio_unexport(state->tms);
gpio_unexport(state->tck);
gpio_unexport(state->tdo);
}
#define SWAP16(x) ((uint16_t)((((uint16_t)(x) & 0xFF00) >> 8) | \
(((uint16_t)(x) & 0x00FF) << 8)))
void HDMITest::loadFpgaFirmware(const uint8_t *bfr, ssize_t size) {
#ifdef linux
int i;
uint16_t length;
// Skip first three sections
for (i=0; i<3; i++) {
memcpy(&length, bfr, sizeof(length));
length = SWAP16(length);
bfr += sizeof(length);
size -= sizeof(length);
bfr += length;
size -= length;
}
if (*bfr != 'b') {
QString *str = new QString();
str->sprintf("Unexpected key: wanted 'b', got '%c' (0x%02x)", *bfr, *bfr);
emit testStateUpdated(TEST_ERROR, 0, str);
return;
}
bfr++;
size--;
memcpy(&length, bfr, sizeof(length));
length = SWAP16(length);
bfr += sizeof(length);
size -= sizeof(length);
char fpga_name[length+1];
bzero(fpga_name, sizeof(fpga_name));
memcpy(fpga_name, bfr, length);
bfr += length;
size -= length;
if (*bfr != 'c') {
QString *str = new QString();
str->sprintf("Unexpected key: wanted 'c', got '%c' (0x%02x)", *bfr, *bfr);
emit testStateUpdated(TEST_ERROR, 0, str);
return;
}
bfr++;
size--;
memcpy(&length, bfr, sizeof(length));
length = SWAP16(length);
bfr += sizeof(length);
size -= sizeof(length);
char date_code[length+1];
bzero(date_code, sizeof(date_code));
memcpy(date_code, bfr, length);
bfr += length;
size -= length;
if (*bfr != 'd') {
QString *str = new QString();
str->sprintf("Unexpected key: wanted 'd', got '%c' (0x%02x)", *bfr, *bfr);
emit testStateUpdated(TEST_ERROR, 0, str);
return;
}
bfr++;
size--;
memcpy(&length, bfr, sizeof(length));
length = SWAP16(length);
bfr += sizeof(length);
size -= sizeof(length);
char time_code[length+1];
bzero(time_code, sizeof(time_code));
memcpy(time_code, bfr, length);
bfr += length;
size -= length;
if (*bfr != 'e') {
QString *str = new QString();
str->sprintf("Unexpected key: wanted 'e', got '%c' (0x%02x)", *bfr, *bfr);
emit testStateUpdated(TEST_ERROR, 0, str);
return;
}
bfr++;
size--;
memcpy(&length, bfr, sizeof(length));
length = SWAP16(length);
bfr += sizeof(length);
size -= sizeof(length);
int fd = open("/dev/fpga", O_RDWR);
if (-1 == fd) {
emit testStateUpdated(TEST_ERROR, 0, new QString("Unable to open /dev/fpga"));
return;
}
if (ioctl(fd, FPGA_IOCRESET, NULL) < 0) {
emit testStateUpdated(TEST_ERROR, 0, new QString("Unable to reset FPGA"));
close(fd);
return;
}
if (write(fd, bfr, size) != size) {
emit testStateUpdated(TEST_ERROR, 0, new QString("Unable to write firmware"));
close(fd);
return;
}
close(fd);
emit testStateUpdated(TEST_INFO, 0, new QString("HDMI FPGA firmware loaded"));
#endif
return;
}
HDMITest::HDMITest()
{
name = new QString("HDMI Test");
loadDefaultFirmware();
}
void HDMITest::loadDefaultFirmware()
{
#ifdef linux
struct jtag_state state;
uint32_t idcode;
int fd;
jtag_open(&state);
idcode = jtag_idcode(&state);
jtag_cleanup(&state);
if (idcode == LX9_JTAG) {
uint8_t bytes[sizeof(zerobytes_6slx9csg324)];
fd = open(LX9_FIRMWARE, O_RDONLY);
if (-1 == fd) {
qDebug() << "Unable to open LX9 firmware";
return;
}
if (read(fd, bytes, sizeof(bytes)) != sizeof(bytes)) {
qDebug() << "Unable to read LX9 firmware";
close(fd);
return;
}
close(fd);
loadFpgaFirmware(bytes, sizeof(bytes));
}
else if (idcode == LX45_JTAG) {
uint8_t bytes[sizeof(zerobytes_6slx45csg324)];
fd = open(LX45_FIRMWARE, O_RDONLY);
if (-1 == fd) {
qDebug() << "Unable to open LX45 firmware";
return;
}
if (read(fd, bytes, sizeof(bytes)) != sizeof(bytes)) {
qDebug() << "Unable to read LX45 firmware";
close(fd);
return;
}
close(fd);
loadFpgaFirmware(bytes, sizeof(bytes));
}
else {
qDebug() << "Unrecognized JTAG code:" << idcode;
}
#endif
}
void HDMITest::loadTestFirmware()
{
#ifdef linux
struct jtag_state state;
uint32_t idcode;
jtag_open(&state);
idcode = jtag_idcode(&state);
jtag_cleanup(&state);
if (idcode == LX9_JTAG)
loadFpgaFirmware(zerobytes_6slx9csg324, sizeof(zerobytes_6slx9csg324));
else if (idcode == LX45_JTAG)
loadFpgaFirmware(zerobytes_6slx45csg324, sizeof(zerobytes_6slx45csg324));
else {
QString str;
str.sprintf("Unrecognized FPGA JTAG: 0x%08x", idcode);
qDebug() << str.toAscii();
return;
}
#endif
}
void HDMITest::runTest() {
QString *str;
#ifdef linux
int fd;
loadTestFirmware();
fd = open("/dev/input/event0", O_RDONLY);
struct input_event e;
if (-1 == fd) {
str = new QString("Unable to open switch: ");
str->append(strerror(errno));
emit testStateUpdated(TEST_ERROR, 0, str);
return;
}
str = new QString("Please press the side switch if the pattern appears");
emit testStateUpdated(TEST_INFO, 0, str);
if (read(fd, &e, sizeof(e)) != sizeof(e)) {
str = new QString("Unable to read switch: ");
str->append(strerror(errno));
emit testStateUpdated(TEST_ERROR, 0, str);
close(fd);
return;
}
close(fd);
str = new QString("Side switch OK");
emit testStateUpdated(TEST_INFO, 0, str);
#else
str = new QString("Skipping HDMI tests on this platform");
emit testStateUpdated(TEST_INFO, 0, str);
#endif
loadDefaultFirmware();
return;
}