Anastasia Klimchuk has uploaded this change for review. ( https://review.coreboot.org/c/flashrom/+/51731 )
Change subject: tree: Remove forward-declarations of structs for spi masters ......................................................................
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
Change-Id: I256bd7c763efc010fc1f29f7c5853f150ac10739 Signed-off-by: Anastasia Klimchuk aklm@chromium.org --- M mstarddc_spi.c M ni845x_spi.c M usbblaster_spi.c 3 files changed, 218 insertions(+), 225 deletions(-)
git pull ssh://review.coreboot.org:29418/flashrom refs/changes/31/51731/1
diff --git a/mstarddc_spi.c b/mstarddc_spi.c index 48d6204..c041bd3 100644 --- a/mstarddc_spi.c +++ b/mstarddc_spi.c @@ -31,8 +31,6 @@ #include "programmer.h" #include "spi.h"
-static const struct spi_master spi_master_mstarddc; - static int mstarddc_fd; static int mstarddc_addr; static int mstarddc_doreset = 1; @@ -68,6 +66,83 @@ }
/* Returns 0 upon success, a negative number upon errors. */ +static int mstarddc_spi_send_command(const struct flashctx *flash, + unsigned int writecnt, + unsigned int readcnt, + const unsigned char *writearr, + unsigned char *readarr) +{ + int ret = 0; + uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t)); + if (cmd == NULL) { + msg_perr("Error allocating memory: errno %d.\n", errno); + ret = -1; + } + + if (!ret && writecnt) { + cmd[0] = MSTARDDC_SPI_WRITE; + memcpy(cmd + 1, writearr, writecnt); + if (write(mstarddc_fd, cmd, writecnt + 1) < 0) { + msg_perr("Error sending write command: errno %d.\n", + errno); + ret = -1; + } + } + + if (!ret && readcnt) { + struct i2c_rdwr_ioctl_data i2c_data; + struct i2c_msg msg[2]; + + cmd[0] = MSTARDDC_SPI_READ; + i2c_data.nmsgs = 2; + i2c_data.msgs = msg; + i2c_data.msgs[0].addr = mstarddc_addr; + i2c_data.msgs[0].len = 1; + i2c_data.msgs[0].flags = 0; + i2c_data.msgs[0].buf = cmd; + i2c_data.msgs[1].addr = mstarddc_addr; + i2c_data.msgs[1].len = readcnt; + i2c_data.msgs[1].flags = I2C_M_RD; + i2c_data.msgs[1].buf = readarr; + + if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) { + msg_perr("Error sending read command: errno %d.\n", + errno); + ret = -1; + } + } + + if (!ret && (writecnt || readcnt)) { + cmd[0] = MSTARDDC_SPI_END; + if (write(mstarddc_fd, cmd, 1) < 0) { + msg_perr("Error sending end command: errno %d.\n", + errno); + ret = -1; + } + } + + /* Do not reset if something went wrong, as it might prevent from + * retrying flashing. */ + if (ret != 0) + mstarddc_doreset = 0; + + if (cmd) + free(cmd); + + return ret; +} + +static const struct spi_master spi_master_mstarddc = { + .max_data_read = 256, + .max_data_write = 256, + .command = mstarddc_spi_send_command, + .multicommand = default_spi_send_multicommand, + .read = default_spi_read, + .write_256 = default_spi_write_256, + .write_aai = default_spi_write_aai, +}; + +/* Returns 0 upon success, a negative number upon errors. */ int mstarddc_spi_init(void) { int ret = 0; @@ -152,81 +227,4 @@ return ret; }
-/* Returns 0 upon success, a negative number upon errors. */ -static int mstarddc_spi_send_command(const struct flashctx *flash, - unsigned int writecnt, - unsigned int readcnt, - const unsigned char *writearr, - unsigned char *readarr) -{ - int ret = 0; - uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t)); - if (cmd == NULL) { - msg_perr("Error allocating memory: errno %d.\n", errno); - ret = -1; - } - - if (!ret && writecnt) { - cmd[0] = MSTARDDC_SPI_WRITE; - memcpy(cmd + 1, writearr, writecnt); - if (write(mstarddc_fd, cmd, writecnt + 1) < 0) { - msg_perr("Error sending write command: errno %d.\n", - errno); - ret = -1; - } - } - - if (!ret && readcnt) { - struct i2c_rdwr_ioctl_data i2c_data; - struct i2c_msg msg[2]; - - cmd[0] = MSTARDDC_SPI_READ; - i2c_data.nmsgs = 2; - i2c_data.msgs = msg; - i2c_data.msgs[0].addr = mstarddc_addr; - i2c_data.msgs[0].len = 1; - i2c_data.msgs[0].flags = 0; - i2c_data.msgs[0].buf = cmd; - i2c_data.msgs[1].addr = mstarddc_addr; - i2c_data.msgs[1].len = readcnt; - i2c_data.msgs[1].flags = I2C_M_RD; - i2c_data.msgs[1].buf = readarr; - - if (ioctl(mstarddc_fd, I2C_RDWR, &i2c_data) < 0) { - msg_perr("Error sending read command: errno %d.\n", - errno); - ret = -1; - } - } - - if (!ret && (writecnt || readcnt)) { - cmd[0] = MSTARDDC_SPI_END; - if (write(mstarddc_fd, cmd, 1) < 0) { - msg_perr("Error sending end command: errno %d.\n", - errno); - ret = -1; - } - } - - /* Do not reset if something went wrong, as it might prevent from - * retrying flashing. */ - if (ret != 0) - mstarddc_doreset = 0; - - if (cmd) - free(cmd); - - return ret; -} - -static const struct spi_master spi_master_mstarddc = { - .max_data_read = 256, - .max_data_write = 256, - .command = mstarddc_spi_send_command, - .multicommand = default_spi_send_multicommand, - .read = default_spi_read, - .write_256 = default_spi_write_256, - .write_aai = default_spi_write_aai, -}; - #endif diff --git a/ni845x_spi.c b/ni845x_spi.c index 153e03e..765da8a 100644 --- a/ni845x_spi.c +++ b/ni845x_spi.c @@ -40,8 +40,6 @@ 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 @@ 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 @@ .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; +} diff --git a/usbblaster_spi.c b/usbblaster_spi.c index 58a8a0e..020c3bf 100644 --- a/usbblaster_spi.c +++ b/usbblaster_spi.c @@ -51,8 +51,6 @@ {0} };
-static const struct spi_master spi_master_usbblaster; - static struct ftdi_context ftdic;
// command bytes @@ -72,51 +70,6 @@ return ((b * 0x0802LU & 0x22110LU) | (b * 0x8020LU & 0x88440LU)) * 0x10101LU >> 16; }
- -/* Returns 0 upon success, a negative number upon errors. */ -int usbblaster_spi_init(void) -{ - uint8_t buf[BUF_SIZE + 1]; - - if (ftdi_init(&ftdic) < 0) - return -1; - - if (ftdi_usb_open(&ftdic, ALTERA_VID, ALTERA_USBBLASTER_PID) < 0) { - msg_perr("Failed to open USB-Blaster: %s\n", ftdic.error_str); - return -1; - } - - if (ftdi_usb_reset(&ftdic) < 0) { - msg_perr("USB-Blaster reset failed\n"); - return -1; - } - - if (ftdi_set_latency_timer(&ftdic, 2) < 0) { - msg_perr("USB-Blaster set latency timer failed\n"); - return -1; - } - - if (ftdi_write_data_set_chunksize(&ftdic, 4096) < 0 || - ftdi_read_data_set_chunksize(&ftdic, BUF_SIZE) < 0) { - msg_perr("USB-Blaster set chunk size failed\n"); - return -1; - } - - memset(buf, 0, sizeof(buf)); - buf[sizeof(buf)-1] = BIT_LED | BIT_CS; - if (ftdi_write_data(&ftdic, buf, sizeof(buf)) < 0) { - msg_perr("USB-Blaster reset write failed\n"); - return -1; - } - if (ftdi_read_data(&ftdic, buf, sizeof(buf)) < 0) { - msg_perr("USB-Blaster reset read failed\n"); - return -1; - } - - register_spi_master(&spi_master_usbblaster); - return 0; -} - static int send_write(unsigned int writecnt, const unsigned char *writearr) { uint8_t buf[BUF_SIZE]; @@ -217,4 +170,48 @@ .write_aai = default_spi_write_aai, };
+/* Returns 0 upon success, a negative number upon errors. */ +int usbblaster_spi_init(void) +{ + uint8_t buf[BUF_SIZE + 1]; + + if (ftdi_init(&ftdic) < 0) + return -1; + + if (ftdi_usb_open(&ftdic, ALTERA_VID, ALTERA_USBBLASTER_PID) < 0) { + msg_perr("Failed to open USB-Blaster: %s\n", ftdic.error_str); + return -1; + } + + if (ftdi_usb_reset(&ftdic) < 0) { + msg_perr("USB-Blaster reset failed\n"); + return -1; + } + + if (ftdi_set_latency_timer(&ftdic, 2) < 0) { + msg_perr("USB-Blaster set latency timer failed\n"); + return -1; + } + + if (ftdi_write_data_set_chunksize(&ftdic, 4096) < 0 || + ftdi_read_data_set_chunksize(&ftdic, BUF_SIZE) < 0) { + msg_perr("USB-Blaster set chunk size failed\n"); + return -1; + } + + memset(buf, 0, sizeof(buf)); + buf[sizeof(buf)-1] = BIT_LED | BIT_CS; + if (ftdi_write_data(&ftdic, buf, sizeof(buf)) < 0) { + msg_perr("USB-Blaster reset write failed\n"); + return -1; + } + if (ftdi_read_data(&ftdic, buf, sizeof(buf)) < 0) { + msg_perr("USB-Blaster reset read failed\n"); + return -1; + } + + register_spi_master(&spi_master_usbblaster); + return 0; +} + #endif