aboutsummaryrefslogtreecommitdiff
path: root/src/pld/gowin.c
blob: bbc2fe15f6f59d86ddcd0da4d308d78a0da02a99 (plain)
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// SPDX-License-Identifier: GPL-2.0-or-later

/***************************************************************************
 *   Copyright (C) 2022 by Daniel Anselmi                                  *
 *   danselmi@gmx.ch                                                       *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <jtag/jtag.h>
#include <jtag/adapter.h>
#include <helper/bits.h>
#include "pld.h"
#include "raw_bit.h"

#define NO_OP                       0x02
#define ERASE_SRAM                  0x05
#define SRAM_ERASE_DONE             0x09
#define IDCODE                      0x11
#define ADDRESS_INITIALIZATION      0x12
#define READ_USERCODE               0x13
#define CONFIG_ENABLE               0x15
#define TRANSFER_CONFIGURATION_DATA 0x17
#define CONFIG_DISABLE              0x3A
#define RELOAD                      0x3C
#define STATUS_REGISTER             0x41
#define ERASE_FLASH                 0x75
#define ENABLE_2ND_FLASH            0x78

#define USER1                       0x42
#define USER2                       0x43

#define STAUS_MASK_MEMORY_ERASE     BIT(5)
#define STAUS_MASK_SYSTEM_EDIT_MODE BIT(7)

struct gowin_pld_device {
	struct jtag_tap *tap;
};

struct gowin_bit_file {
	struct raw_bit_file raw_file;
	size_t capacity;
	uint32_t id;
	uint16_t stored_checksum;
	int compressed;
	int crc_en;
	uint16_t checksum;
	uint8_t replace8x;
	uint8_t replace4x;
	uint8_t replace2x;
};

static uint64_t gowin_read_fs_file_bitsequence(const char *bits, int length)
{
	uint64_t res = 0;
	for (int i = 0; i < length; i++)
		res = (res << 1) | (*bits++ == '1' ? 1 : 0);
	return res;
}

static int gowin_add_byte_to_bit_file(struct gowin_bit_file *bit_file, uint8_t byte)
{
	if (bit_file->raw_file.length + 1 > bit_file->capacity) {
		uint8_t *buffer;
		if (bit_file->raw_file.data)
			buffer = realloc(bit_file->raw_file.data, bit_file->capacity + 8192);
		else
			buffer = malloc(8192);
		if (!buffer) {
			LOG_ERROR("Out of memory");
			return ERROR_FAIL;
		}
		bit_file->raw_file.data = buffer;
		bit_file->capacity += 8192;
	}

	bit_file->raw_file.data[bit_file->raw_file.length++] = byte;

	return ERROR_OK;
}

static int gowin_read_fs_file_header(struct gowin_bit_file *bit_file, FILE *stream)
{
	if (!bit_file)
		return ERROR_FAIL;

	int end_of_header = 0;
	while (!end_of_header) {
		char buffer[256];
		char *line = fgets(buffer, 256, stream);
		if (!line || feof(stream) || ferror(stream))
			return ERROR_FAIL;

		if (line[0] == '/')
			continue;

		size_t line_length = strlen(line);
		if (line[line_length - 1] != '\n')
			return ERROR_FAIL;
		line_length--;

		for (unsigned int i = 0; i < line_length; i += 8) {
			uint8_t byte = gowin_read_fs_file_bitsequence(line + i, 8);
			int retval = gowin_add_byte_to_bit_file(bit_file, byte);
			if (retval != ERROR_OK)
				return retval;
		}

		uint8_t key = gowin_read_fs_file_bitsequence(line, 8);
		line += 8;
		uint64_t value = gowin_read_fs_file_bitsequence(line, line_length - 8);

		if (key == 0x06) {
			bit_file->id = value & 0xffffffff;
		} else if (key == 0x3B) {
			end_of_header = 1;
			bit_file->crc_en = (value & BIT(23)) ? 1 : 0;
		}
	}

	return ERROR_OK;
}

static int gowin_read_fs_file(struct gowin_bit_file *bit_file, const char *filename)
{
	FILE *input_file = fopen(filename, "r");

	if (!input_file) {
		LOG_ERROR("Couldn't open %s: %s", filename, strerror(errno));
		return ERROR_PLD_FILE_LOAD_FAILED;
	}

	int retval = gowin_read_fs_file_header(bit_file, input_file);
	if (retval != ERROR_OK) {
		free(bit_file->raw_file.data);
		fclose(input_file);
		return retval;
	}

	char digits_buffer[9]; /* 8 + 1 trailing zero */
	do {
		char *digits = fgets(digits_buffer, 9, input_file);
		if (feof(input_file))
			break;
		if (!digits || ferror(input_file)) {
			free(bit_file->raw_file.data);
			fclose(input_file);
			return ERROR_FAIL;
		}
		if (digits[0] == '\n')
			continue;

		if (strlen(digits) != 8) {
			free(bit_file->raw_file.data);
			fclose(input_file);
			return ERROR_FAIL;
		}
		uint8_t byte = gowin_read_fs_file_bitsequence(digits, 8);
		retval = gowin_add_byte_to_bit_file(bit_file, byte);
		if (retval != ERROR_OK) {
			free(bit_file->raw_file.data);
			fclose(input_file);
			return ERROR_FAIL;
		}
	} while (1);

	fclose(input_file);
	return ERROR_OK;
}

static int gowin_read_file(struct gowin_bit_file *bit_file, const char *filename, bool *is_fs)
{
	memset(bit_file, 0, sizeof(struct gowin_bit_file));

	if (!filename || !bit_file)
		return ERROR_COMMAND_SYNTAX_ERROR;

	const char *file_suffix_pos = strrchr(filename, '.');
	if (!file_suffix_pos) {
		LOG_ERROR("Unable to detect filename suffix");
		return ERROR_PLD_FILE_LOAD_FAILED;
	}

	/* check if binary .bin or ascii .fs */
	if (strcasecmp(file_suffix_pos, ".bin") == 0) {
		*is_fs = false;
		return cpld_read_raw_bit_file(&bit_file->raw_file, filename);
	} else if (strcasecmp(file_suffix_pos, ".fs") == 0) {
		*is_fs = true;
		return gowin_read_fs_file(bit_file, filename);
	}

	LOG_ERROR("Filetype not supported, expecting .fs or .bin file");
	return ERROR_PLD_FILE_LOAD_FAILED;
}

static int gowin_set_instr(struct jtag_tap *tap, uint8_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;
	}
	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);
	jtag_add_runtest(3, TAP_IDLE);
	free(t);
	return ERROR_OK;
}

static int gowin_read_register(struct jtag_tap *tap, uint32_t reg, uint32_t *result)
{
	struct scan_field field;

	int retval = gowin_set_instr(tap, reg);
	if (retval != ERROR_OK)
		return retval;
	retval = jtag_execute_queue();
	if (retval != ERROR_OK)
		return retval;

	uint8_t buf[4] = {0};
	field.check_mask = NULL;
	field.check_value = NULL;
	field.num_bits = 32;
	field.out_value = buf;
	field.in_value = buf;

	jtag_add_dr_scan(tap, 1, &field, TAP_IDLE);
	retval = jtag_execute_queue();
	*result = le_to_h_u32(buf);
	return retval;
}

static int gowin_check_status_flag(struct jtag_tap *tap, uint32_t mask, uint32_t flag)
{
	uint32_t status = 0;

	int retries = 0;
	do {
		int retval = gowin_read_register(tap, STATUS_REGISTER, &status);
		if (retval != ERROR_OK)
			return retval;
		if (retries++ == 100000)
			return ERROR_FAIL;
	} while ((status & mask) != flag);

	return ERROR_OK;
}

static int gowin_enable_config(struct jtag_tap *tap)
{
	int retval = gowin_set_instr(tap, CONFIG_ENABLE);
	if (retval != ERROR_OK)
		return retval;
	retval = jtag_execute_queue();
	if (retval != ERROR_OK)
		return retval;

	return gowin_check_status_flag(tap, STAUS_MASK_SYSTEM_EDIT_MODE, STAUS_MASK_SYSTEM_EDIT_MODE);
}

static int gowin_disable_config(struct jtag_tap *tap)
{
	int retval = gowin_set_instr(tap, CONFIG_DISABLE);
	if (retval != ERROR_OK)
		return retval;
	retval = jtag_execute_queue();
	if (retval != ERROR_OK)
		return retval;

	return gowin_check_status_flag(tap, STAUS_MASK_SYSTEM_EDIT_MODE, 0);
}

static int gowin_reload(struct jtag_tap *tap)
{
	int retval = gowin_set_instr(tap, RELOAD);
	if (retval != ERROR_OK)
		return retval;
	retval = gowin_set_instr(tap, NO_OP);
	if (retval != ERROR_OK)
		return retval;
	return jtag_execute_queue();
}

static int gowin_runtest_idle(struct jtag_tap *tap, unsigned int frac_sec)
{
	int speed = adapter_get_speed_khz() * 1000;
	int cycles = DIV_ROUND_UP(speed, frac_sec);
	jtag_add_runtest(cycles, TAP_IDLE);
	return jtag_execute_queue();
}

static int gowin_erase_sram(struct jtag_tap *tap, bool tx_erase_done)
{
	/* config is already enabled */
	int retval = gowin_set_instr(tap, ERASE_SRAM);
	if (retval != ERROR_OK)
		return retval;
	retval = gowin_set_instr(tap, NO_OP);
	if (retval != ERROR_OK)
		return retval;

	/* Delay or Run Test 2~10ms */
	/* 10 ms is worst case for GW2A-55 */
	jtag_add_sleep(10000);
	retval = jtag_execute_queue();
	if (retval != ERROR_OK)
		return retval;

	retval = gowin_check_status_flag(tap, STAUS_MASK_MEMORY_ERASE,
									STAUS_MASK_MEMORY_ERASE);
	if (retval != ERROR_OK)
		return retval;

	if (tx_erase_done) {
		retval = gowin_set_instr(tap, SRAM_ERASE_DONE);
		if (retval != ERROR_OK)
			return retval;
		retval = gowin_set_instr(tap, NO_OP);
		if (retval != ERROR_OK)
			return retval;
		retval = jtag_execute_queue();
		if (retval != ERROR_OK)
			return retval;
		/* gen clock cycles in RUN/IDLE for 500us -> 1/500us = 2000/s */
		retval = gowin_runtest_idle(tap, 2000);
		if (retval != ERROR_OK)
			return retval;
	}

	retval = gowin_set_instr(tap, NO_OP);
	if (retval != ERROR_OK)
		return retval;
	return jtag_execute_queue();
}

static int gowin_load_to_sram(struct pld_device *pld_device, const char *filename)
{
	if (!pld_device)
		return ERROR_FAIL;

	struct gowin_pld_device *gowin_info = pld_device->driver_priv;

	if (!gowin_info || !gowin_info->tap)
		return ERROR_FAIL;
	struct jtag_tap *tap = gowin_info->tap;

	bool is_fs = false;
	struct gowin_bit_file bit_file;
	int retval = gowin_read_file(&bit_file, filename, &is_fs);
	if (retval != ERROR_OK)
		return retval;

	for (unsigned int i = 0; i < bit_file.raw_file.length; i++)
		bit_file.raw_file.data[i] = flip_u32(bit_file.raw_file.data[i], 8);

	uint32_t id;
	retval = gowin_read_register(tap, IDCODE, &id);
	if (retval != ERROR_OK) {
		free(bit_file.raw_file.data);
		return retval;
	}

	if (is_fs && id != bit_file.id) {
		free(bit_file.raw_file.data);
		LOG_ERROR("Id on device (0x%8.8" PRIx32 ") and id in bit-stream (0x%8.8" PRIx32 ") don't match.",
			id, bit_file.id);
		return ERROR_FAIL;
	}

	retval = gowin_enable_config(tap);
	if (retval != ERROR_OK) {
		free(bit_file.raw_file.data);
		return retval;
	}

	retval = gowin_erase_sram(tap, false);
	if (retval != ERROR_OK) {
		free(bit_file.raw_file.data);
		return retval;
	}

	retval = gowin_set_instr(tap, ADDRESS_INITIALIZATION);
	if (retval != ERROR_OK) {
		free(bit_file.raw_file.data);
		return retval;
	}
	retval = gowin_set_instr(tap, TRANSFER_CONFIGURATION_DATA);
	if (retval != ERROR_OK) {
		free(bit_file.raw_file.data);
		return retval;
	}

	/* scan out the bitstream */
	struct scan_field field;
	field.num_bits = bit_file.raw_file.length * 8;
	field.out_value = bit_file.raw_file.data;
	field.in_value = bit_file.raw_file.data;
	jtag_add_dr_scan(gowin_info->tap, 1, &field, TAP_IDLE);
	jtag_add_runtest(3, TAP_IDLE);

	retval = jtag_execute_queue();
	if (retval != ERROR_OK) {
		free(bit_file.raw_file.data);
		return retval;
	}

	retval = gowin_disable_config(tap);
	free(bit_file.raw_file.data);
	if (retval != ERROR_OK)
		return retval;

	retval = gowin_set_instr(gowin_info->tap, NO_OP);
	if (retval != ERROR_OK)
		return retval;

	retval = jtag_execute_queue();

	return retval;
}

static int gowin_read_register_command(struct pld_device *pld_device, uint32_t cmd, uint32_t *value)
{
	if (!pld_device)
		return ERROR_FAIL;

	struct gowin_pld_device *gowin_info = pld_device->driver_priv;

	if (!gowin_info || !gowin_info->tap)
		return ERROR_FAIL;

	return gowin_read_register(gowin_info->tap, cmd, value);
}

static int gowin_reload_command(struct pld_device *pld_device)
{
	if (!pld_device)
		return ERROR_FAIL;

	struct gowin_pld_device *gowin_info = pld_device->driver_priv;

	if (!gowin_info || !gowin_info->tap)
		return ERROR_FAIL;

	return gowin_reload(gowin_info->tap);
}

static int gowin_get_ipdbg_hub(int user_num, struct pld_device *pld_device, struct pld_ipdbg_hub *hub)
{
	if (!pld_device)
		return ERROR_FAIL;

	struct gowin_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 == 1) {
		hub->user_ir_code = USER1;
	} else if (user_num == 2) {
		hub->user_ir_code = USER2;
	} else {
		LOG_ERROR("gowin devices only have user register 1 & 2");
		return ERROR_FAIL;
	}
	return ERROR_OK;
}

COMMAND_HANDLER(gowin_read_status_command_handler)
{
	if (CMD_ARGC != 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

	struct pld_device *device = get_pld_device_by_name_or_numstr(CMD_ARGV[0]);
	if (!device) {
		command_print(CMD, "pld device '#%s' is out of bounds or unknown", CMD_ARGV[0]);
		return ERROR_FAIL;
	}

	uint32_t status = 0;
	int retval = gowin_read_register_command(device, STATUS_REGISTER, &status);

	if (retval == ERROR_OK)
		command_print(CMD, "0x%8.8" PRIx32, status);

	return retval;
}

COMMAND_HANDLER(gowin_read_user_register_command_handler)
{
	if (CMD_ARGC != 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

	struct pld_device *device = get_pld_device_by_name_or_numstr(CMD_ARGV[0]);
	if (!device) {
		command_print(CMD, "pld device '#%s' is out of bounds or unknown", CMD_ARGV[0]);
		return ERROR_FAIL;
	}

	uint32_t user_reg = 0;
	int retval = gowin_read_register_command(device, READ_USERCODE, &user_reg);

	if (retval == ERROR_OK)
		command_print(CMD, "0x%8.8" PRIx32, user_reg);

	return retval;
}

COMMAND_HANDLER(gowin_reload_command_handler)
{
	if (CMD_ARGC != 1)
		return ERROR_COMMAND_SYNTAX_ERROR;

	struct pld_device *device = get_pld_device_by_name_or_numstr(CMD_ARGV[0]);
	if (!device) {
		command_print(CMD, "pld device '#%s' is out of bounds or unknown", CMD_ARGV[0]);
		return ERROR_FAIL;
	}

	return gowin_reload_command(device);
}

static const struct command_registration gowin_exec_command_handlers[] = {
	{
		.name = "read_status",
		.mode = COMMAND_EXEC,
		.handler = gowin_read_status_command_handler,
		.help = "reading status register from FPGA",
		.usage = "pld_name",
	}, {
		.name = "read_user",
		.mode = COMMAND_EXEC,
		.handler = gowin_read_user_register_command_handler,
		.help = "reading user register from FPGA",
		.usage = "pld_name",
	}, {
		.name = "refresh",
		.mode = COMMAND_EXEC,
		.handler = gowin_reload_command_handler,
		.help = "reload bitstream from flash to SRAM",
		.usage = "pld_name",
	},
	COMMAND_REGISTRATION_DONE
};

static const struct command_registration gowin_command_handler[] = {
	{
		.name = "gowin",
		.mode = COMMAND_ANY,
		.help = "gowin specific commands",
		.usage = "",
		.chain = gowin_exec_command_handlers
	},
	COMMAND_REGISTRATION_DONE
};

PLD_CREATE_COMMAND_HANDLER(gowin_pld_create_command)
{
	if (CMD_ARGC != 4)
		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;
	}

	struct gowin_pld_device *gowin_info = malloc(sizeof(struct gowin_pld_device));
	if (!gowin_info) {
		LOG_ERROR("Out of memory");
		return ERROR_FAIL;
	}
	gowin_info->tap = tap;

	pld->driver_priv = gowin_info;

	return ERROR_OK;
}

struct pld_driver gowin_pld = {
	.name = "gowin",
	.commands = gowin_command_handler,
	.pld_create_command = &gowin_pld_create_command,
	.load = &gowin_load_to_sram,
	.get_ipdbg_hub = gowin_get_ipdbg_hub,
};