summaryrefslogtreecommitdiffstats
path: root/ni845x_spi.c
diff options
context:
space:
mode:
authorAnastasia Klimchuk <aklm@chromium.org>2021-03-23 16:34:07 +1100
committerEdward O'Callaghan <quasisec@chromium.org>2021-04-01 01:17:48 +0000
commite704583ad53b24e1b39aa7a2e5077fa95d5f9244 (patch)
treed30504f5ac4310850372b124c33447b8f8ce3176 /ni845x_spi.c
parent6d79a6ab808463f6895a9665858ff8b220d5118b (diff)
downloadflashrom-e704583ad53b24e1b39aa7a2e5077fa95d5f9244.tar.gz
flashrom-e704583ad53b24e1b39aa7a2e5077fa95d5f9244.tar.bz2
flashrom-e704583ad53b24e1b39aa7a2e5077fa95d5f9244.zip
tree: Remove forward-declarations of structs for spi masters
Reorder functions to avoid forward-declarations of structs. Similar thing was done earlier for functions declarations, this patch takes care of structs declarations. BUG=b:140394053 TEST=builds objdump -d is identical objdump -s only difference is version number Change-Id: I256bd7c763efc010fc1f29f7c5853f150ac10739 Signed-off-by: Anastasia Klimchuk <aklm@chromium.org> Reviewed-on: https://review.coreboot.org/c/flashrom/+/51731 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Nico Huber <nico.h@gmx.de> Reviewed-by: Edward O'Callaghan <quasisec@chromium.org>
Diffstat (limited to 'ni845x_spi.c')
-rw-r--r--ni845x_spi.c196
1 files changed, 97 insertions, 99 deletions
diff --git a/ni845x_spi.c b/ni845x_spi.c
index 153e03ed8..765da8a6e 100644
--- a/ni845x_spi.c
+++ b/ni845x_spi.c
@@ -40,8 +40,6 @@ enum voltage_coerce_mode {
USE_HIGHER
};
-static const struct spi_master spi_programmer_ni845x;
-
static unsigned char CS_number; // use chip select 0 as default
static enum USB845x_type device_pid = Unknown_NI845X_Device;
@@ -403,103 +401,6 @@ static int ni845x_spi_shutdown(void *data)
return 0;
}
-int ni845x_spi_init(void)
-{
- char *speed_str = NULL;
- char *CS_str = NULL;
- char *voltage = NULL;
- char *endptr = NULL;
- int requested_io_voltage_mV = 1200; // default the IO voltage to 1.2V
- int spi_speed_KHz = 1000; // selecting 1 MHz SCK is a good bet
- char *serial_number = NULL; // by default open the first connected device
- char *ignore_io_voltage_limits_str = NULL;
- int32 tmp = 0;
-
- // read the cs parameter (which Chip select should we use)
- CS_str = extract_programmer_param("cs");
- if (CS_str) {
- CS_number = CS_str[0] - '0';
- free(CS_str);
- if (strlen(CS_str) > 1 || CS_number < 0 || 7 < CS_number) {
- msg_perr("Only CS 0-7 supported\n");
- return 1;
- }
- }
-
- voltage = extract_programmer_param("voltage");
- if (voltage != NULL) {
- requested_io_voltage_mV = parse_voltage(voltage);
- free(voltage);
- if (requested_io_voltage_mV < 0)
- return 1;
- }
-
- serial_number = extract_programmer_param("serial");
-
- speed_str = extract_programmer_param("spispeed");
- if (speed_str) {
- spi_speed_KHz = strtoul(speed_str, &endptr, 0);
- if (*endptr) {
- msg_perr("The spispeed parameter passed with invalid format: %s\n",
- speed_str);
- msg_perr("Please pass the parameter with a simple number in kHz\n");
- return 1;
- }
- free(speed_str);
- }
-
- ignore_io_voltage_limits = false;
- ignore_io_voltage_limits_str = extract_programmer_param("ignore_io_voltage_limits");
- if (ignore_io_voltage_limits_str
- && strcmp(ignore_io_voltage_limits_str, "yes") == 0) {
- ignore_io_voltage_limits = true;
- }
-
- if (ni845x_spi_open(serial_number, &device_handle)) {
- if (serial_number) {
- msg_pinfo("Could not find any connected NI USB-8451/8452 with serialnumber: %s!\n",
- serial_number);
- ni845x_spi_print_available_devices();
- msg_pinfo("Check the S/N field on the bottom of the device,\n"
- "or use 'lsusb -v -d 3923:7166 | grep Serial' for USB-8451\n"
- "or 'lsusb -v -d 3923:7514 | grep Serial' for USB-8452\n");
- free(serial_number);
- } else {
- msg_pinfo("Could not find any connected NI USB-845x device!\n");
- }
- return 1;
- }
- free(serial_number);
-
- // open the SPI config handle
- tmp = ni845xSpiConfigurationOpen(&configuration_handle);
- if (tmp != 0) {
- ni845x_report_error("ni845xSpiConfigurationOpen", tmp);
- ni845x_spi_shutdown(NULL);
- return 1;
- }
-
- if (usb8452_spi_set_io_voltage(requested_io_voltage_mV, &io_voltage_in_mV, USE_LOWER) < 0) {
- ni845x_spi_shutdown(NULL);
- return 1; // no alert here usb8452_spi_set_io_voltage already printed that
- }
-
- if (ni845x_spi_set_speed(spi_speed_KHz)) {
- msg_perr("Unable to set SPI speed\n");
- ni845x_spi_shutdown(NULL);
- return 1;
- }
-
- if (register_shutdown(ni845x_spi_shutdown, NULL)) {
- ni845x_spi_shutdown(NULL);
- return 1;
- }
-
- register_spi_master(&spi_programmer_ni845x);
-
- return 0;
-}
-
static void ni845x_warn_over_max_voltage(const struct flashctx *flash)
{
if (device_pid == USB8451) {
@@ -636,3 +537,100 @@ static const struct spi_master spi_programmer_ni845x = {
.write_256 = default_spi_write_256,
.write_aai = default_spi_write_aai,
};
+
+int ni845x_spi_init(void)
+{
+ char *speed_str = NULL;
+ char *CS_str = NULL;
+ char *voltage = NULL;
+ char *endptr = NULL;
+ int requested_io_voltage_mV = 1200; // default the IO voltage to 1.2V
+ int spi_speed_KHz = 1000; // selecting 1 MHz SCK is a good bet
+ char *serial_number = NULL; // by default open the first connected device
+ char *ignore_io_voltage_limits_str = NULL;
+ int32 tmp = 0;
+
+ // read the cs parameter (which Chip select should we use)
+ CS_str = extract_programmer_param("cs");
+ if (CS_str) {
+ CS_number = CS_str[0] - '0';
+ free(CS_str);
+ if (strlen(CS_str) > 1 || CS_number < 0 || 7 < CS_number) {
+ msg_perr("Only CS 0-7 supported\n");
+ return 1;
+ }
+ }
+
+ voltage = extract_programmer_param("voltage");
+ if (voltage != NULL) {
+ requested_io_voltage_mV = parse_voltage(voltage);
+ free(voltage);
+ if (requested_io_voltage_mV < 0)
+ return 1;
+ }
+
+ serial_number = extract_programmer_param("serial");
+
+ speed_str = extract_programmer_param("spispeed");
+ if (speed_str) {
+ spi_speed_KHz = strtoul(speed_str, &endptr, 0);
+ if (*endptr) {
+ msg_perr("The spispeed parameter passed with invalid format: %s\n",
+ speed_str);
+ msg_perr("Please pass the parameter with a simple number in kHz\n");
+ return 1;
+ }
+ free(speed_str);
+ }
+
+ ignore_io_voltage_limits = false;
+ ignore_io_voltage_limits_str = extract_programmer_param("ignore_io_voltage_limits");
+ if (ignore_io_voltage_limits_str
+ && strcmp(ignore_io_voltage_limits_str, "yes") == 0) {
+ ignore_io_voltage_limits = true;
+ }
+
+ if (ni845x_spi_open(serial_number, &device_handle)) {
+ if (serial_number) {
+ msg_pinfo("Could not find any connected NI USB-8451/8452 with serialnumber: %s!\n",
+ serial_number);
+ ni845x_spi_print_available_devices();
+ msg_pinfo("Check the S/N field on the bottom of the device,\n"
+ "or use 'lsusb -v -d 3923:7166 | grep Serial' for USB-8451\n"
+ "or 'lsusb -v -d 3923:7514 | grep Serial' for USB-8452\n");
+ free(serial_number);
+ } else {
+ msg_pinfo("Could not find any connected NI USB-845x device!\n");
+ }
+ return 1;
+ }
+ free(serial_number);
+
+ // open the SPI config handle
+ tmp = ni845xSpiConfigurationOpen(&configuration_handle);
+ if (tmp != 0) {
+ ni845x_report_error("ni845xSpiConfigurationOpen", tmp);
+ ni845x_spi_shutdown(NULL);
+ return 1;
+ }
+
+ if (usb8452_spi_set_io_voltage(requested_io_voltage_mV, &io_voltage_in_mV, USE_LOWER) < 0) {
+ ni845x_spi_shutdown(NULL);
+ return 1; // no alert here usb8452_spi_set_io_voltage already printed that
+ }
+
+ if (ni845x_spi_set_speed(spi_speed_KHz)) {
+ msg_perr("Unable to set SPI speed\n");
+ ni845x_spi_shutdown(NULL);
+ return 1;
+ }
+
+ if (register_shutdown(ni845x_spi_shutdown, NULL)) {
+ ni845x_spi_shutdown(NULL);
+ return 1;
+ }
+
+ register_spi_master(&spi_programmer_ni845x);
+
+ return 0;
+}