Skip to content

Commit

Permalink
jtagspi/pld: add support from lattice ecp2/ecp3 driver
Browse files Browse the repository at this point in the history
Provide jtagspi with specific procedures to be able to
use jtagspi for programming spi-flash devices on lattice
ecp2 and ecp3 devices.

Change-Id: I39028aba47a74a0479be16d52d318f4bff7f2ed4
Signed-off-by: Daniel Anselmi <[email protected]>
Reviewed-on: https://review.openocd.org/c/openocd/+/7823
Tested-by: jenkins
Reviewed-by: Antonio Borneo <[email protected]>
  • Loading branch information
danselmi authored and borneoa committed Sep 23, 2023
1 parent fe5ed48 commit 2b99846
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/pld/ecp2_3.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define ISC_DISABLE 0x1E
#define LSCC_READ_STATUS 0x53
#define LSCC_BITSTREAM_BURST 0x02
#define PROGRAM_SPI 0x3A

#define STATUS_DONE_BIT 0x00020000
#define STATUS_ERROR_BITS_ECP2 0x00040003
Expand Down Expand Up @@ -249,3 +250,57 @@ int lattice_ecp3_load(struct lattice_pld_device *lattice_device, struct lattice_
const uint32_t expected = STATUS_DONE_BIT;
return lattice_verify_status_register_u32(lattice_device, out, expected, mask, false);
}

int lattice_ecp2_3_connect_spi_to_jtag(struct lattice_pld_device *pld_device_info)
{
if (!pld_device_info)
return ERROR_FAIL;

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

// erase configuration
int retval = lattice_set_instr(tap, ISC_ENABLE, TAP_IDLE);
if (retval != ERROR_OK)
return retval;
retval = lattice_set_instr(tap, ISC_ERASE, TAP_IDLE);
if (retval != ERROR_OK)
return retval;
retval = lattice_set_instr(tap, ISC_DISABLE, TAP_IDLE);
if (retval != ERROR_OK)
return retval;

// connect jtag to spi pins
retval = lattice_set_instr(tap, PROGRAM_SPI, TAP_IDLE);
if (retval != ERROR_OK)
return retval;

return jtag_execute_queue();
}

int lattice_ecp2_3_disconnect_spi_from_jtag(struct lattice_pld_device *pld_device_info)
{
if (!pld_device_info)
return ERROR_FAIL;

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

int retval = lattice_set_instr(tap, BYPASS, TAP_IDLE);
if (retval != ERROR_OK)
return retval;

return jtag_execute_queue();
}

int lattice_ecp2_3_get_facing_read_bits(struct lattice_pld_device *pld_device_info, unsigned int *facing_read_bits)
{
if (!pld_device_info)
return ERROR_FAIL;

*facing_read_bits = 1;

return ERROR_OK;
}
3 changes: 3 additions & 0 deletions src/pld/ecp2_3.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ int lattice_ecp2_3_read_usercode(struct jtag_tap *tap, uint32_t *usercode, uint3
int lattice_ecp2_3_write_usercode(struct lattice_pld_device *lattice_device, uint32_t usercode);
int lattice_ecp2_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file);
int lattice_ecp3_load(struct lattice_pld_device *lattice_device, struct lattice_bit_file *bit_file);
int lattice_ecp2_3_connect_spi_to_jtag(struct lattice_pld_device *pld_device_info);
int lattice_ecp2_3_disconnect_spi_from_jtag(struct lattice_pld_device *pld_device_info);
int lattice_ecp2_3_get_facing_read_bits(struct lattice_pld_device *pld_device_info, unsigned int *facing_read_bits);

#endif /* OPENOCD_PLD_ECP2_3_H */
62 changes: 62 additions & 0 deletions src/pld/lattice.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,64 @@ static int lattice_get_ipdbg_hub(int user_num, struct pld_device *pld_device, st
return ERROR_OK;
}

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

struct lattice_pld_device *pld_device_info = pld_device->driver_priv;

int retval = lattice_check_device_family(pld_device_info);
if (retval != ERROR_OK)
return retval;

if (pld_device_info->family == LATTICE_ECP2 || pld_device_info->family == LATTICE_ECP3)
return lattice_ecp2_3_connect_spi_to_jtag(pld_device_info);

return ERROR_FAIL;
}

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

struct lattice_pld_device *pld_device_info = pld_device->driver_priv;

int retval = lattice_check_device_family(pld_device_info);
if (retval != ERROR_OK)
return retval;

if (pld_device_info->family == LATTICE_ECP2 || pld_device_info->family == LATTICE_ECP3)
return lattice_ecp2_3_disconnect_spi_from_jtag(pld_device_info);

return ERROR_FAIL;
}

static int lattice_get_stuff_bits(struct pld_device *pld_device, unsigned int *facing_read_bits,
unsigned int *trailing_write_bits)
{
if (!pld_device)
return ERROR_FAIL;

struct lattice_pld_device *pld_device_info = pld_device->driver_priv;

int retval = lattice_check_device_family(pld_device_info);
if (retval != ERROR_OK)
return retval;

if (pld_device_info->family == LATTICE_ECP2 || pld_device_info->family == LATTICE_ECP3)
return lattice_ecp2_3_get_facing_read_bits(pld_device_info, facing_read_bits);

return ERROR_FAIL;
}

static int lattice_has_jtagspi_instruction(struct pld_device *device, bool *has_instruction)
{
*has_instruction = true;
return ERROR_OK;
}

PLD_CREATE_COMMAND_HANDLER(lattice_pld_create_command)
{
if (CMD_ARGC != 4 && CMD_ARGC != 6)
Expand Down Expand Up @@ -551,4 +609,8 @@ struct pld_driver lattice_pld = {
.pld_create_command = &lattice_pld_create_command,
.load = &lattice_load_command,
.get_ipdbg_hub = lattice_get_ipdbg_hub,
.has_jtagspi_instruction = lattice_has_jtagspi_instruction,
.connect_spi_to_jtag = lattice_connect_spi_to_jtag,
.disconnect_spi_from_jtag = lattice_disconnect_spi_from_jtag,
.get_stuff_bits = lattice_get_stuff_bits,
};

0 comments on commit 2b99846

Please sign in to comment.