diff options
author | Keno Fischer <keno@juliacomputing.com> | 2015-11-15 14:58:25 +0000 |
---|---|---|
committer | David Hendricks <david.hendricks@gmail.com> | 2017-11-16 06:28:01 +0000 |
commit | 1f33cb58001f95c3de69f037acae6f72baddca2b (patch) | |
tree | 863f297f755279f16fbb4f4e2615b01279ae9733 /linux_spi.c | |
parent | 22f2dc5ec0b13a413a9ce42a5836ec2aa3b1abfc (diff) | |
download | flashrom-1f33cb58001f95c3de69f037acae6f72baddca2b.tar.gz flashrom-1f33cb58001f95c3de69f037acae6f72baddca2b.tar.bz2 flashrom-1f33cb58001f95c3de69f037acae6f72baddca2b.zip |
linux_spi: Dynamically detect max buffer size
Read max buffer size from sysfs if available.
Change-Id: Ic541e548ced8488f074d388f1c92174cad123064
Signed-off-by: Keno Fischer <keno@juliacomputing.com>
Signed-off-by: David Hendricks <dhendricks@fb.com>
Reviewed-on: https://review.coreboot.org/22441
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
Diffstat (limited to 'linux_spi.c')
-rw-r--r-- | linux_spi.c | 52 |
1 files changed, 45 insertions, 7 deletions
diff --git a/linux_spi.c b/linux_spi.c index e51fbc420..611559ac0 100644 --- a/linux_spi.c +++ b/linux_spi.c @@ -42,6 +42,8 @@ */ static int fd = -1; +#define BUF_SIZE_FROM_SYSFS "/sys/module/spidev/parameters/bufsiz" +static size_t max_kernel_buf_size; static int linux_spi_shutdown(void *data); static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt, @@ -127,8 +129,45 @@ int linux_spi_init(void) return 1; } - register_spi_master(&spi_master_linux); + /* Read max buffer size from sysfs, or use page size as fallback. */ + FILE *fp; + fp = fopen(BUF_SIZE_FROM_SYSFS, "r"); + if (!fp) { + msg_pwarn("Cannot open %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); + goto out; + } + + char buf[10]; + memset(buf, 0, sizeof(buf)); + if (!fread(buf, 1, sizeof(buf) - 1, fp)) { + if (feof(fp)) + msg_pwarn("Cannot read %s: file is empty.\n", BUF_SIZE_FROM_SYSFS); + else + msg_pwarn("Cannot read %s: %s.\n", BUF_SIZE_FROM_SYSFS, strerror(errno)); + goto out; + } + + long int tmp; + errno = 0; + tmp = strtol(buf, NULL, 0); + if ((tmp < 0) || errno) { + msg_pwarn("Buffer size %ld from %s seems wrong.\n", tmp, BUF_SIZE_FROM_SYSFS); + } else { + msg_pdbg("%s: Using value from %s as max buffer size.\n", __func__, BUF_SIZE_FROM_SYSFS); + max_kernel_buf_size = (size_t)tmp; + } +out: + if (fp) + fclose(fp); + + if (!max_kernel_buf_size) { + msg_pdbg("%s: Using page size as max buffer size.\n", __func__); + max_kernel_buf_size = (size_t)getpagesize(); + } + + msg_pdbg("%s: max_kernel_buf_size: %zu\n", __func__, max_kernel_buf_size); + register_spi_master(&spi_master_linux); return 0; } @@ -179,17 +218,16 @@ static int linux_spi_send_command(struct flashctx *flash, unsigned int writecnt, return 0; } -static int linux_spi_read(struct flashctx *flash, uint8_t *buf, - unsigned int start, unsigned int len) +static int linux_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len) { - return spi_read_chunked(flash, buf, start, len, - (unsigned int)getpagesize()); + /* Read buffer is fully utilized for data. */ + return spi_read_chunked(flash, buf, start, len, max_kernel_buf_size); } static int linux_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len) { - return spi_write_chunked(flash, buf, start, len, - ((unsigned int)getpagesize()) - 4); + /* 5 bytes must be reserved for longest possible command + address. */ + return spi_write_chunked(flash, buf, start, len, max_kernel_buf_size - 5); } #endif // CONFIG_LINUX_SPI == 1 |