summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnastasia Klimchuk <aklm@chromium.org>2021-05-27 09:46:23 +1000
committerEdward O'Callaghan <quasisec@chromium.org>2021-06-03 05:20:56 +0000
commitf7b0674266874e40cc19984e7b556154ac9093c1 (patch)
tree9e588dfe6d319ad00d11a6adab20978fbaa9f7a7
parentab93e7038741624aea3c14da6d60f7fc50a32121 (diff)
downloadflashrom-f7b0674266874e40cc19984e7b556154ac9093c1.tar.gz
flashrom-f7b0674266874e40cc19984e7b556154ac9093c1.tar.bz2
flashrom-f7b0674266874e40cc19984e7b556154ac9093c1.zip
pony_spi.c: Refactor singleton states into reentrant pattern
Move global singleton states into a struct and store within the bitbang_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". BUG=b:185191942 TEST=builds Change-Id: Icf1125dadcdaa287ebe3c07ca95adb770bb19412 Signed-off-by: Anastasia Klimchuk <aklm@chromium.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/54997 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
-rw-r--r--pony_spi.c77
1 files changed, 49 insertions, 28 deletions
diff --git a/pony_spi.c b/pony_spi.c
index 940cae1ce..6d5db6f3c 100644
--- a/pony_spi.c
+++ b/pony_spi.c
@@ -49,16 +49,20 @@ enum pony_type {
TYPE_AJAWE
};
-/* Pins for master->slave direction */
-static int pony_negate_cs = 1;
-static int pony_negate_sck = 0;
-static int pony_negate_mosi = 0;
-/* Pins for slave->master direction */
-static int pony_negate_miso = 0;
+struct pony_spi_data {
+ /* Pins for master->slave direction */
+ int negate_cs;
+ int negate_sck;
+ int negate_mosi;
+ /* Pins for slave->master direction */
+ int negate_miso;
+};
static void pony_bitbang_set_cs(int val, void *spi_data)
{
- if (pony_negate_cs)
+ struct pony_spi_data *data = spi_data;
+
+ if (data->negate_cs)
val ^= 1;
sp_set_pin(PIN_TXD, val);
@@ -66,7 +70,9 @@ static void pony_bitbang_set_cs(int val, void *spi_data)
static void pony_bitbang_set_sck(int val, void *spi_data)
{
- if (pony_negate_sck)
+ struct pony_spi_data *data = spi_data;
+
+ if (data->negate_sck)
val ^= 1;
sp_set_pin(PIN_RTS, val);
@@ -74,7 +80,9 @@ static void pony_bitbang_set_sck(int val, void *spi_data)
static void pony_bitbang_set_mosi(int val, void *spi_data)
{
- if (pony_negate_mosi)
+ struct pony_spi_data *data = spi_data;
+
+ if (data->negate_mosi)
val ^= 1;
sp_set_pin(PIN_DTR, val);
@@ -82,9 +90,10 @@ static void pony_bitbang_set_mosi(int val, void *spi_data)
static int pony_bitbang_get_miso(void *spi_data)
{
+ struct pony_spi_data *data = spi_data;
int tmp = sp_get_pin(PIN_CTS);
- if (pony_negate_miso)
+ if (data->negate_miso)
tmp ^= 1;
return tmp;
@@ -107,6 +116,7 @@ static int pony_spi_shutdown(void *data)
else
msg_pdbg("Pony SPI shutdown completed.\n");
+ free(data);
return ret;
}
@@ -119,6 +129,16 @@ int pony_spi_init(void)
int have_device = 0;
int have_prog = 0;
+ struct pony_spi_data *data = calloc(1, sizeof(*data));
+ if (!data) {
+ msg_perr("Unable to allocate space for SPI master data\n");
+ return 1;
+ }
+ data->negate_cs = 1;
+ data->negate_sck = 0;
+ data->negate_mosi = 0;
+ data->negate_miso = 0;
+
/* The parameter is in format "dev=/dev/device,type=serbang" */
arg = extract_programmer_param("dev");
if (arg && strlen(arg)) {
@@ -127,8 +147,9 @@ int pony_spi_init(void)
free(arg);
return 1;
}
- if (register_shutdown(pony_spi_shutdown, NULL) != 0) {
+ if (register_shutdown(pony_spi_shutdown, data) != 0) {
free(arg);
+ free(data);
serialport_shutdown(NULL);
return 1;
}
@@ -165,25 +186,25 @@ int pony_spi_init(void)
*/
switch (type) {
case TYPE_AJAWE:
- pony_negate_cs = 1;
- pony_negate_sck = 1;
- pony_negate_mosi = 1;
- pony_negate_miso = 1;
+ data->negate_cs = 1;
+ data->negate_sck = 1;
+ data->negate_mosi = 1;
+ data->negate_miso = 1;
name = "AJAWe";
break;
case TYPE_SERBANG:
- pony_negate_cs = 0;
- pony_negate_sck = 0;
- pony_negate_mosi = 0;
- pony_negate_miso = 1;
+ data->negate_cs = 0;
+ data->negate_sck = 0;
+ data->negate_mosi = 0;
+ data->negate_miso = 1;
name = "serbang";
break;
default:
case TYPE_SI_PROG:
- pony_negate_cs = 1;
- pony_negate_sck = 0;
- pony_negate_mosi = 0;
- pony_negate_miso = 0;
+ data->negate_cs = 1;
+ data->negate_sck = 0;
+ data->negate_mosi = 0;
+ data->negate_miso = 0;
name = "SI-Prog";
break;
}
@@ -192,9 +213,9 @@ int pony_spi_init(void)
/*
* Detect if there is a compatible hardware programmer connected.
*/
- pony_bitbang_set_cs(1, NULL);
- pony_bitbang_set_sck(1, NULL);
- pony_bitbang_set_mosi(1, NULL);
+ pony_bitbang_set_cs(1, data);
+ pony_bitbang_set_sck(1, data);
+ pony_bitbang_set_mosi(1, data);
switch (type) {
case TYPE_AJAWE:
@@ -224,8 +245,8 @@ int pony_spi_init(void)
return 1;
}
- if (register_spi_bitbang_master(&bitbang_spi_master_pony, NULL)) {
+ if (register_spi_bitbang_master(&bitbang_spi_master_pony, data))
return 1;
- }
+
return 0;
}