mirror of
https://review.coreboot.org/flashrom.git
synced 2025-04-27 23:22:37 +02:00
ft2232_spi.c: Refactor singleton states into reentrant pattern
Move global singleton states into a struct and store within the spi_master data field for the life-time of the driver. This is one of the steps on the way to move spi_master data memory management behind the initialisation API, for more context see other patches under the same topic "register_master_api". TEST=builds BUG=b:140394053 Change-Id: I67518a58b4f35e0edaf06ac09c9374bdf06db0df Signed-off-by: Anastasia Klimchuk <aklm@chromium.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/52256 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Angel Pons <th3fanbus@gmail.com> Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
This commit is contained in:
parent
485c335a34
commit
9dad8c2143
91
ft2232_spi.c
91
ft2232_spi.c
@ -98,13 +98,15 @@ const struct dev_entry devs_ft2232spi[] = {
|
|||||||
* The pin signal direction bit offsets follow the same order; 0 means that
|
* The pin signal direction bit offsets follow the same order; 0 means that
|
||||||
* pin at the matching bit index is an input, 1 means pin is an output.
|
* pin at the matching bit index is an input, 1 means pin is an output.
|
||||||
*
|
*
|
||||||
* The default values (set below) are used for most devices:
|
* The default values (set below in ft2232_spi_init) are used for most devices:
|
||||||
* value: 0x08 CS=high, DI=low, DO=low, SK=low
|
* value: 0x08 CS=high, DI=low, DO=low, SK=low
|
||||||
* dir: 0x0b CS=output, DI=input, DO=output, SK=output
|
* dir: 0x0b CS=output, DI=input, DO=output, SK=output
|
||||||
*/
|
*/
|
||||||
static uint8_t pinlvl = 0x08;
|
struct ft2232_data {
|
||||||
static uint8_t pindir = 0x0b;
|
uint8_t pinlvl;
|
||||||
static struct ftdi_context ftdic_context;
|
uint8_t pindir;
|
||||||
|
struct ftdi_context ftdic_context;
|
||||||
|
};
|
||||||
|
|
||||||
static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
|
static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
|
||||||
{
|
{
|
||||||
@ -162,7 +164,8 @@ static int get_buf(struct ftdi_context *ftdic, const unsigned char *buf,
|
|||||||
static int ft2232_shutdown(void *data)
|
static int ft2232_shutdown(void *data)
|
||||||
{
|
{
|
||||||
int f;
|
int f;
|
||||||
struct ftdi_context *ftdic = (struct ftdi_context *) data;
|
struct ft2232_data *spi_data = (struct ft2232_data *) data;
|
||||||
|
struct ftdi_context *ftdic = &spi_data->ftdic_context;
|
||||||
unsigned char buf[3];
|
unsigned char buf[3];
|
||||||
|
|
||||||
msg_pdbg("Releasing I/Os\n");
|
msg_pdbg("Releasing I/Os\n");
|
||||||
@ -180,6 +183,7 @@ static int ft2232_shutdown(void *data)
|
|||||||
return f;
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
free(spi_data);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -189,7 +193,8 @@ static int ft2232_spi_send_command(const struct flashctx *flash,
|
|||||||
const unsigned char *writearr,
|
const unsigned char *writearr,
|
||||||
unsigned char *readarr)
|
unsigned char *readarr)
|
||||||
{
|
{
|
||||||
struct ftdi_context *ftdic = &ftdic_context;
|
struct ft2232_data *spi_data = flash->mst->spi.data;
|
||||||
|
struct ftdi_context *ftdic = &spi_data->ftdic_context;
|
||||||
static unsigned char *buf = NULL;
|
static unsigned char *buf = NULL;
|
||||||
/* failed is special. We use bitwise ops, but it is essentially bool. */
|
/* failed is special. We use bitwise ops, but it is essentially bool. */
|
||||||
int i = 0, ret = 0, failed = 0;
|
int i = 0, ret = 0, failed = 0;
|
||||||
@ -220,8 +225,8 @@ static int ft2232_spi_send_command(const struct flashctx *flash,
|
|||||||
*/
|
*/
|
||||||
msg_pspew("Assert CS#\n");
|
msg_pspew("Assert CS#\n");
|
||||||
buf[i++] = SET_BITS_LOW;
|
buf[i++] = SET_BITS_LOW;
|
||||||
buf[i++] = ~ 0x08 & pinlvl; /* assert CS (3rd) bit only */
|
buf[i++] = ~ 0x08 & spi_data->pinlvl; /* assert CS (3rd) bit only */
|
||||||
buf[i++] = pindir;
|
buf[i++] = spi_data->pindir;
|
||||||
|
|
||||||
if (writecnt) {
|
if (writecnt) {
|
||||||
buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG;
|
buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG;
|
||||||
@ -261,8 +266,8 @@ static int ft2232_spi_send_command(const struct flashctx *flash,
|
|||||||
|
|
||||||
msg_pspew("De-assert CS#\n");
|
msg_pspew("De-assert CS#\n");
|
||||||
buf[i++] = SET_BITS_LOW;
|
buf[i++] = SET_BITS_LOW;
|
||||||
buf[i++] = pinlvl;
|
buf[i++] = spi_data->pinlvl;
|
||||||
buf[i++] = pindir;
|
buf[i++] = spi_data->pindir;
|
||||||
ret = send_buf(ftdic, buf, i);
|
ret = send_buf(ftdic, buf, i);
|
||||||
failed |= ret;
|
failed |= ret;
|
||||||
if (ret)
|
if (ret)
|
||||||
@ -271,7 +276,7 @@ static int ft2232_spi_send_command(const struct flashctx *flash,
|
|||||||
return failed ? -1 : 0;
|
return failed ? -1 : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const struct spi_master spi_master_ft2232 = {
|
static struct spi_master spi_master_ft2232 = {
|
||||||
.features = SPI_MASTER_4BA,
|
.features = SPI_MASTER_4BA,
|
||||||
.max_data_read = 64 * 1024,
|
.max_data_read = 64 * 1024,
|
||||||
.max_data_write = 256,
|
.max_data_write = 256,
|
||||||
@ -286,7 +291,6 @@ static const struct spi_master spi_master_ft2232 = {
|
|||||||
int ft2232_spi_init(void)
|
int ft2232_spi_init(void)
|
||||||
{
|
{
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
struct ftdi_context *ftdic = &ftdic_context;
|
|
||||||
unsigned char buf[512];
|
unsigned char buf[512];
|
||||||
int ft2232_vid = FTDI_VID;
|
int ft2232_vid = FTDI_VID;
|
||||||
int ft2232_type = FTDI_FT4232H_PID;
|
int ft2232_type = FTDI_FT4232H_PID;
|
||||||
@ -312,6 +316,11 @@ int ft2232_spi_init(void)
|
|||||||
char *arg;
|
char *arg;
|
||||||
double mpsse_clk;
|
double mpsse_clk;
|
||||||
|
|
||||||
|
uint8_t pinlvl = 0x08;
|
||||||
|
uint8_t pindir = 0x0b;
|
||||||
|
struct ftdi_context ftdic;
|
||||||
|
struct ft2232_data *spi_data;
|
||||||
|
|
||||||
arg = extract_programmer_param("type");
|
arg = extract_programmer_param("type");
|
||||||
if (arg) {
|
if (arg) {
|
||||||
if (!strcasecmp(arg, "2232H")) {
|
if (!strcasecmp(arg, "2232H")) {
|
||||||
@ -521,50 +530,50 @@ int ft2232_spi_init(void)
|
|||||||
(ft2232_interface == INTERFACE_B) ? "B" :
|
(ft2232_interface == INTERFACE_B) ? "B" :
|
||||||
(ft2232_interface == INTERFACE_C) ? "C" : "D");
|
(ft2232_interface == INTERFACE_C) ? "C" : "D");
|
||||||
|
|
||||||
if (ftdi_init(ftdic) < 0) {
|
if (ftdi_init(&ftdic) < 0) {
|
||||||
msg_perr("ftdi_init failed.\n");
|
msg_perr("ftdi_init failed.\n");
|
||||||
return -3;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftdi_set_interface(ftdic, ft2232_interface) < 0) {
|
if (ftdi_set_interface(&ftdic, ft2232_interface) < 0) {
|
||||||
msg_perr("Unable to select channel (%s).\n", ftdi_get_error_string(ftdic));
|
msg_perr("Unable to select channel (%s).\n", ftdi_get_error_string(&ftdic));
|
||||||
}
|
}
|
||||||
|
|
||||||
arg = extract_programmer_param("serial");
|
arg = extract_programmer_param("serial");
|
||||||
f = ftdi_usb_open_desc(ftdic, ft2232_vid, ft2232_type, NULL, arg);
|
f = ftdi_usb_open_desc(&ftdic, ft2232_vid, ft2232_type, NULL, arg);
|
||||||
free(arg);
|
free(arg);
|
||||||
|
|
||||||
if (f < 0 && f != -5) {
|
if (f < 0 && f != -5) {
|
||||||
msg_perr("Unable to open FTDI device: %d (%s)\n", f,
|
msg_perr("Unable to open FTDI device: %d (%s)\n", f,
|
||||||
ftdi_get_error_string(ftdic));
|
ftdi_get_error_string(&ftdic));
|
||||||
return -4;
|
return -4;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftdic->type != TYPE_2232H && ftdic->type != TYPE_4232H && ftdic->type != TYPE_232H) {
|
if (ftdic.type != TYPE_2232H && ftdic.type != TYPE_4232H && ftdic.type != TYPE_232H) {
|
||||||
msg_pdbg("FTDI chip type %d is not high-speed.\n", ftdic->type);
|
msg_pdbg("FTDI chip type %d is not high-speed.\n", ftdic.type);
|
||||||
clock_5x = 0;
|
clock_5x = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftdi_usb_reset(ftdic) < 0) {
|
if (ftdi_usb_reset(&ftdic) < 0) {
|
||||||
msg_perr("Unable to reset FTDI device (%s).\n", ftdi_get_error_string(ftdic));
|
msg_perr("Unable to reset FTDI device (%s).\n", ftdi_get_error_string(&ftdic));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftdi_set_latency_timer(ftdic, 2) < 0) {
|
if (ftdi_set_latency_timer(&ftdic, 2) < 0) {
|
||||||
msg_perr("Unable to set latency timer (%s).\n", ftdi_get_error_string(ftdic));
|
msg_perr("Unable to set latency timer (%s).\n", ftdi_get_error_string(&ftdic));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftdi_write_data_set_chunksize(ftdic, 270)) {
|
if (ftdi_write_data_set_chunksize(&ftdic, 270)) {
|
||||||
msg_perr("Unable to set chunk size (%s).\n", ftdi_get_error_string(ftdic));
|
msg_perr("Unable to set chunk size (%s).\n", ftdi_get_error_string(&ftdic));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ftdi_set_bitmode(ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
|
if (ftdi_set_bitmode(&ftdic, 0x00, BITMODE_BITBANG_SPI) < 0) {
|
||||||
msg_perr("Unable to set bitmode to SPI (%s).\n", ftdi_get_error_string(ftdic));
|
msg_perr("Unable to set bitmode to SPI (%s).\n", ftdi_get_error_string(&ftdic));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clock_5x) {
|
if (clock_5x) {
|
||||||
msg_pdbg("Disable divide-by-5 front stage\n");
|
msg_pdbg("Disable divide-by-5 front stage\n");
|
||||||
buf[0] = DIS_DIV_5;
|
buf[0] = DIS_DIV_5;
|
||||||
if (send_buf(ftdic, buf, 1)) {
|
if (send_buf(&ftdic, buf, 1)) {
|
||||||
ret = -5;
|
ret = -5;
|
||||||
goto ftdi_err;
|
goto ftdi_err;
|
||||||
}
|
}
|
||||||
@ -577,7 +586,7 @@ int ft2232_spi_init(void)
|
|||||||
buf[0] = TCK_DIVISOR;
|
buf[0] = TCK_DIVISOR;
|
||||||
buf[1] = (divisor / 2 - 1) & 0xff;
|
buf[1] = (divisor / 2 - 1) & 0xff;
|
||||||
buf[2] = ((divisor / 2 - 1) >> 8) & 0xff;
|
buf[2] = ((divisor / 2 - 1) >> 8) & 0xff;
|
||||||
if (send_buf(ftdic, buf, 3)) {
|
if (send_buf(&ftdic, buf, 3)) {
|
||||||
ret = -6;
|
ret = -6;
|
||||||
goto ftdi_err;
|
goto ftdi_err;
|
||||||
}
|
}
|
||||||
@ -588,7 +597,7 @@ int ft2232_spi_init(void)
|
|||||||
/* Disconnect TDI/DO to TDO/DI for loopback. */
|
/* Disconnect TDI/DO to TDO/DI for loopback. */
|
||||||
msg_pdbg("No loopback of TDI/DO TDO/DI\n");
|
msg_pdbg("No loopback of TDI/DO TDO/DI\n");
|
||||||
buf[0] = LOOPBACK_END;
|
buf[0] = LOOPBACK_END;
|
||||||
if (send_buf(ftdic, buf, 1)) {
|
if (send_buf(&ftdic, buf, 1)) {
|
||||||
ret = -7;
|
ret = -7;
|
||||||
goto ftdi_err;
|
goto ftdi_err;
|
||||||
}
|
}
|
||||||
@ -597,20 +606,34 @@ int ft2232_spi_init(void)
|
|||||||
buf[0] = SET_BITS_LOW;
|
buf[0] = SET_BITS_LOW;
|
||||||
buf[1] = pinlvl;
|
buf[1] = pinlvl;
|
||||||
buf[2] = pindir;
|
buf[2] = pindir;
|
||||||
if (send_buf(ftdic, buf, 3)) {
|
if (send_buf(&ftdic, buf, 3)) {
|
||||||
ret = -8;
|
ret = -8;
|
||||||
goto ftdi_err;
|
goto ftdi_err;
|
||||||
}
|
}
|
||||||
|
|
||||||
register_shutdown(ft2232_shutdown, ftdic);
|
spi_data = calloc(1, sizeof(*spi_data));
|
||||||
|
if (!spi_data) {
|
||||||
|
msg_perr("Unable to allocate space for SPI master data\n");
|
||||||
|
return SPI_GENERIC_ERROR;
|
||||||
|
}
|
||||||
|
spi_data->pinlvl = pinlvl;
|
||||||
|
spi_data->pindir = pindir;
|
||||||
|
spi_data->ftdic_context = ftdic;
|
||||||
|
|
||||||
|
spi_master_ft2232.data = spi_data;
|
||||||
|
|
||||||
|
if (register_shutdown(ft2232_shutdown, spi_data)) {
|
||||||
|
free(spi_data);
|
||||||
|
goto ftdi_err;
|
||||||
|
}
|
||||||
register_spi_master(&spi_master_ft2232);
|
register_spi_master(&spi_master_ft2232);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
ftdi_err:
|
ftdi_err:
|
||||||
if ((f = ftdi_usb_close(ftdic)) < 0) {
|
if ((f = ftdi_usb_close(&ftdic)) < 0) {
|
||||||
msg_perr("Unable to close FTDI device: %d (%s)\n", f,
|
msg_perr("Unable to close FTDI device: %d (%s)\n", f,
|
||||||
ftdi_get_error_string(ftdic));
|
ftdi_get_error_string(&ftdic));
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user