int lcmxo2_2000hc_cpld_verify()

in meta-portwell/meta-pwneptune/recipes-neptune/plat-libs/files/cpld/lattice.c [1701:2067]


int lcmxo2_2000hc_cpld_verify(FILE *jed_fd)
{
	int i;
	unsigned int data=0;
	unsigned int tdo;
	unsigned int *jed_data;
	unsigned int *dr_data;
	unsigned int row  = 0;
	int cmp_err = 0;
	int parser_err = 0;

	int cfg_err = 0;
	int ufm_err = 0;

	int ret_err = -1;

	unsigned int alloc_size = ((cur_dev->dr_bits /32 + 1) * sizeof(unsigned int) + 4096 ) % 4096;

	dr_data = (unsigned int *)malloc(alloc_size);
	if (dr_data == NULL) {
		if(debug)
			printf("memory allocation for dr_data failed.\n");

		goto err_handle;
	}

	jed_data = (unsigned int *)malloc(alloc_size);
	if (jed_data == NULL) {
		if(debug)
			printf("memory allocation for jed_data failed.\n");

		goto err_handle;
	}

	//RUNTEST	IDLE	15 TCK	1.00E-003 SEC;
	ast_jtag_run_test_idle( 0, 0, 3);
	usleep(3000);

	//! Check the IDCODE_PUB
	//SIR	8	TDI  (E0);
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, LCMXO2_IDCODE_PUB);

	//SDR 32	TDI  (00000000)
	//		TDO  (012BB043)
	//		MASK (FFFFFFFF);
	ast_jtag_tdo_xfer( 0, 32, dr_data);

	if(dr_data[0] != 0x12BB043) {
		if (debug)
			printf("ID Fail : %08x [0x012BB043] \n",dr_data[0]);

		free(dr_data);
	}

	//! Program Bscan register

	//! Shift in Preload(0x1C) instruction
	//SIR	8	TDI  (1C);
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x1C);

	//SDR 424 TDI  (FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF);
	memset(dr_data, 0xff, ((424/32) + 1) * sizeof(unsigned int));
	ast_jtag_tdi_xfer(0, 424, dr_data);

	//! Enable the Flash (Transparent Mode)
	//! Shift in ISC ENABLE X (0x74) instruction
	//SIR 8	TDI  (74);
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x74);

	//SDR 8	TDI  (00);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	data = 0x00;
	ast_jtag_tdi_xfer( 0, 8, &data);
	usleep(3000);

	//! Shift in ISC ERASE(0x0E) instruction
	//SIR 8	TDI  (0E);
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x0E);

	//SDR 8	TDI  (01);
	//RUNTEST IDLE	2 TCK	1.00E+000 SEC;
	data = 0x01;
	ast_jtag_tdi_xfer(0, 8, &data);
	usleep(3000);

	//! Shift in BYPASS(0xFF) instruction
	//SIR 8	TDI  (FF)
	//	TDO  (00)
	//	MASK (C0);
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, BYPASS);

	//! Shift in ISC ENABLE X (0x74) instruction
	//SIR 8	TDI  (74);
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x74);

	//SDR 8	TDI  (08);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	data = 0x08;
	ast_jtag_tdi_xfer( 0, 8, &data);
	usleep(3000);

	//! Verify USERCODE
	if (debug)
		printf("Verify USERCODE \n");

	//! Shift in READ USERCODE(0xC0) instruction
	//SIR 8	TDI  (C0);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0xC0);
	usleep(3000);

	//SDR 32	TDI  (00000000)
	//		TDO  (00000000)
	//		MASK (FFFFFFFF);
	dr_data[0] = 0x00000000;
	ast_jtag_tdo_xfer(0, 32, dr_data);

	if (debug)
		printf("CPLD USERCODE = 0x%x\n", dr_data[0]);


	//! Verify the Flash
	if (debug)
		printf("Starting to Verify Device . . . This will take a few seconds\n");

	//! Shift in LSC_INIT_ADDRESS(0x46) instruction
	//SIR 8	TDI  (46);
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x46);

	//SDR 8	TDI  (04);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	data = 0x04;
	ast_jtag_tdi_xfer( 0, 8, &data);
	usleep(3000);


	//! Shift in LSC_READ_INCR_NV(0x73) instruction
	//SIR 8	TDI  (73);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x73);
	usleep(3000);

	fseek(jed_fd, 0, SEEK_SET);
	jed_file_parse_header(jed_fd);

	if (debug)
		printf("Verify CONFIG 3198 \n");
	cmp_err = 0;
	row = 0;

	//for(row = 0; row < cur_dev->row_num; row++) {
	for(row = 0; row < 3198; row++) {
		memset(dr_data, 0, 16);
		memset(jed_data, 0, 16);
		parser_err = jed_file_parser(jed_fd, 128, jed_data);
		if (parser_err == -1) {
			jed_file_parse_header(jed_fd);
			parser_err = jed_file_parser(jed_fd, 128, jed_data);
			if (parser_err == -1) {
				goto err_handle;
			}
		}

		ast_jtag_tdo_xfer( 0, 128, dr_data);

		for(i = 0; i < 4; i++) {
			if(dr_data[i] != jed_data[i]) {
				cmp_err = 1;
				goto err_handle;
			}
		}

		//RUNTEST	IDLE	2 TCK	1.00E-003 SEC;
		usleep(3000);
	}
	cfg_err = 0;

	jed_file_parse_header(jed_fd);

	//! Verify the UFM

	//! Shift in LSC_INIT_ADDR_UFM(0x47) instruction
	//SIR	8	TDI  (47);
	//RUNTEST	IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x47);
	usleep(3000);


	//! Shift in LSC_READ_INCR_NV(0x73) instruction
	//SIR	8	TDI  (73);
	//RUNTEST	IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x73);
	usleep(3000);

	if (debug)
		printf("Verify the UFM 640 \n");
	//! Shift out Data Row
	for(row =0 ; row < 640; row++) {
		memset(dr_data, 0, 16);
		memset(jed_data, 0, 16);
		parser_err = jed_file_parser(jed_fd, 128, jed_data);
		if (parser_err == -1) {
			jed_file_parse_header(jed_fd);
			parser_err = jed_file_parser(jed_fd, 128, jed_data);
			if (parser_err == -1) {
				goto err_handle;
			}
		}

		ast_jtag_tdo_xfer( 0, 128, dr_data);

		//for(i = 0; i < (cur_dev->dr_bits /32); i++) {
		for(i = 0; i < 4; i++) {
			if(dr_data[i] != jed_data[i]) {
				cmp_err = 1;
				goto err_handle;
			}
		}
		//RUNTEST	IDLE	2 TCK	1.00E-003 SEC;
		usleep(3000);
	}
	ufm_err = 0;

	//! Verify USERCODE
	if (debug)
		printf("Verify USERCODE \n");

	//! Shift in READ USERCODE(0xC0) instruction
	//SIR 8	TDI  (C0);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0xC0);
	usleep(3000);

	//SDR 32	TDI  (00000000)
	//		TDO  (00000000)
	//		MASK (FFFFFFFF);
	dr_data[0] = 0x00000000;
	ast_jtag_tdo_xfer(0, 32, dr_data);

	//! Read the status bit

	//! Shift in LSC_READ_STATUS(0x3C) instruction
	//SIR 8	TDI  (3C);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x3C);
	usleep(3000);

	//SDR 32	TDI  (00000000)
	//		TDO  (00000000)
	//		MASK (00003000);
	dr_data[0] = 0x00000000;
	ast_jtag_tdo_xfer( 0, 32, dr_data);


	//! Verify Feature Rows

	//! Shift in LSC_READ_STATUS(0x3C) instruction
	//SIR 8	TDI  (3C);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x3C);
	usleep(3000);

	//SDR 32	TDI  (00000000)
	//		TDO  (00000000)
	//		MASK (00010000);
	dr_data[0] = 0x00000000;
	ast_jtag_tdo_xfer( 0, 32, dr_data);

	//! Shift in LSC_READ_FEATURE (0xE7) instruction
	//SIR 8	TDI  (E7);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0xE7);
	usleep(3000);

	//SDR 64	TDI  (0000000000000000)
	//		TDO  (0000000000000000);
	dr_data[0] = 0x00000000;
	dr_data[1] = 0x00000000;
	ast_jtag_tdo_xfer( 0, 64, dr_data);

	//! Shift in in LSC_READ_FEABITS(0xFB) instruction
	//SIR 8	TDI  (FB);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0xFB);
	usleep(3000);

	//SDR 16	TDI  (0000)
	//		TDO  (0620)
	//		MASK (FFF2);
	dr_data[0] = 0x00000000;
	ast_jtag_tdo_xfer( 0, 16, dr_data);
	if (debug)
		printf("read %x [0x0620] \n", dr_data[0] & 0xffff);

	//! Read the status bit

	//! Shift in LSC_READ_STATUS(0x3C) instruction
	//SIR 8	TDI  (3C);
	//RUNTEST IDLE	2 TCK	1.00E-003 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x3C);
	usleep(3000);

	//SDR 32	TDI  (00000000)
	//		TDO  (00000000)
	//		MASK (00003000);
	dr_data[0] = 0x00000000;
	ast_jtag_tdo_xfer(0, 32, dr_data);

	//! Verify Done Bit

	//! Shift in BYPASS(0xFF) instruction
	//SIR 8	TDI  (FF)
	//	TDO  (04)
	//	MASK (C4);
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, BYPASS);

	//! Exit the programming mode

	//! Shift in ISC DISABLE(0x26) instruction
	//SIR 8	TDI  (26);
	//RUNTEST IDLE	2 TCK	1.00E+000 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, 0x26);
	usleep(1000);

	//! Shift in BYPASS(0xFF) instruction
	//SIR 8	TDI  (FF);
	//RUNTEST IDLE	2 TCK	1.00E-001 SEC;
	ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, BYPASS);
	usleep(1000);

	//! Verify SRAM DONE Bit

	//! Shift in BYPASS(0xFF) instruction
	//SIR 8	TDI  (FF)
	//		TDO  (04)
	//		MASK (84);
	tdo = ast_jtag_sir_xfer(0, LATTICE_INS_LENGTH, BYPASS);

	if (!cfg_err && !ufm_err)
		ret_err = 0;

err_handle:
	if (dr_data != NULL)
		free(dr_data);

	if (jed_data != NULL)
		free(jed_data);

	if(cmp_err) {
		if (debug)
			printf("Verify Error !!\n");
	} else {
		if (debug)
			printf("Verify Done !!\n");
	}

	if (parser_err) {
		if (debug)
			printf("Error while parsing JED file\n");
	} else {
		if (debug)
			printf("NO Error while parsing JED file\n");
	}

	return ret_err;

}