Anastasia Klimchuk has uploaded this change for review.

View Change

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>
---
M ft2232_spi.c
1 file changed, 43 insertions(+), 13 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/56/52256/1
diff --git a/ft2232_spi.c b/ft2232_spi.c
index 1fb795b..dc7cab0 100644
--- a/ft2232_spi.c
+++ b/ft2232_spi.c
@@ -98,13 +98,15 @@
* 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.
*
- * 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
* dir: 0x0b CS=output, DI=input, DO=output, SK=output
*/
-static uint8_t pinlvl = 0x08;
-static uint8_t pindir = 0x0b;
-static struct ftdi_context ftdic_context;
+struct ft2232_data {
+ uint8_t pinlvl;
+ uint8_t pindir;
+ struct ftdi_context *ftdic_context;
+};

static const char *get_ft2232_devicename(int ft2232_vid, int ft2232_type)
{
@@ -162,7 +164,8 @@
static int ft2232_shutdown(void *data)
{
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];

msg_pdbg("Releasing I/Os\n");
@@ -180,6 +183,8 @@
return f;
}

+ free(ftdic);
+ free(spi_data);
return 0;
}

@@ -189,7 +194,8 @@
const unsigned char *writearr,
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;
/* failed is special. We use bitwise ops, but it is essentially bool. */
int i = 0, ret = 0, failed = 0;
@@ -220,8 +226,8 @@
*/
msg_pspew("Assert CS#\n");
buf[i++] = SET_BITS_LOW;
- buf[i++] = ~ 0x08 & pinlvl; /* assert CS (3rd) bit only */
- buf[i++] = pindir;
+ buf[i++] = ~ 0x08 & spi_data->pinlvl; /* assert CS (3rd) bit only */
+ buf[i++] = spi_data->pindir;

if (writecnt) {
buf[i++] = MPSSE_DO_WRITE | MPSSE_WRITE_NEG;
@@ -261,8 +267,8 @@

msg_pspew("De-assert CS#\n");
buf[i++] = SET_BITS_LOW;
- buf[i++] = pinlvl;
- buf[i++] = pindir;
+ buf[i++] = spi_data->pinlvl;
+ buf[i++] = spi_data->pindir;
ret = send_buf(ftdic, buf, i);
failed |= ret;
if (ret)
@@ -271,7 +277,7 @@
return failed ? -1 : 0;
}

-static const struct spi_master spi_master_ft2232 = {
+static struct spi_master spi_master_ft2232 = {
.features = SPI_MASTER_4BA,
.max_data_read = 64 * 1024,
.max_data_write = 256,
@@ -286,7 +292,6 @@
int ft2232_spi_init(void)
{
int ret = 0;
- struct ftdi_context *ftdic = &ftdic_context;
unsigned char buf[512];
int ft2232_vid = FTDI_VID;
int ft2232_type = FTDI_FT4232H_PID;
@@ -312,6 +317,11 @@
char *arg;
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");
if (arg) {
if (!strcasecmp(arg, "2232H")) {
@@ -521,8 +531,15 @@
(ft2232_interface == INTERFACE_B) ? "B" :
(ft2232_interface == INTERFACE_C) ? "C" : "D");

+ ftdic = calloc(1, sizeof(struct ftdi_context));
+ if (!ftdic) {
+ msg_perr("Unable to allocate memory for ftdi_context.\n");
+ return SPI_GENERIC_ERROR;
+ }
+
if (ftdi_init(ftdic) < 0) {
msg_perr("ftdi_init failed.\n");
+ free(ftdic);
return -3;
}

@@ -537,6 +554,7 @@
if (f < 0 && f != -5) {
msg_perr("Unable to open FTDI device: %d (%s)\n", f,
ftdi_get_error_string(ftdic));
+ free(ftdic);
return -4;
}

@@ -602,7 +620,17 @@
goto ftdi_err;
}

- register_shutdown(ft2232_shutdown, ftdic);
+ spi_data = calloc(1, sizeof(struct ft2232_data));
+ 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);

return 0;
@@ -612,6 +640,8 @@
msg_perr("Unable to close FTDI device: %d (%s)\n", f,
ftdi_get_error_string(ftdic));
}
+
+ free(ftdic);
return ret;
}


To view, visit change 52256. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I67518a58b4f35e0edaf06ac09c9374bdf06db0df
Gerrit-Change-Number: 52256
Gerrit-PatchSet: 1
Gerrit-Owner: Anastasia Klimchuk <aklm@chromium.org>
Gerrit-MessageType: newchange