From 1f33cb58001f95c3de69f037acae6f72baddca2b Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Sun, 15 Nov 2015 14:58:25 +0000 Subject: linux_spi: Dynamically detect max buffer size Read max buffer size from sysfs if available. Change-Id: Ic541e548ced8488f074d388f1c92174cad123064 Signed-off-by: Keno Fischer Signed-off-by: David Hendricks Reviewed-on: https://review.coreboot.org/22441 Tested-by: build bot (Jenkins) Reviewed-by: Nico Huber --- linux_spi.c | 52 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 7 deletions(-) (limited to 'linux_spi.c') 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 -- cgit v1.2.3