diff mbox series

mtd: spi-nor: use spi-mem dirmap API

Message ID 97330369-c42e-0ce3-ab1e-0f4433e6acad@cogentembedded.com
State Changes Requested
Delegated to: Ambarus Tudor
Headers show
Series mtd: spi-nor: use spi-mem dirmap API | expand

Commit Message

Sergei Shtylyov Oct. 25, 2019, 8:26 p.m. UTC
Make use of the spi-mem direct mapping API to let advanced controllers
optimize read/write operations when they support direct mapping.

Based on the original patch by Boris Brezillon <boris.brezillon@bootlin.com>.

Signed-off-by: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>

---
This patch is against the 'spi-nor/next' branch of the MTD 'linux.git' repo.

 drivers/mtd/spi-nor/spi-nor.c |   79 ++++++++++++++++++++++++++++++++++++++++++
 include/linux/mtd/spi-nor.h   |    5 ++
 2 files changed, 84 insertions(+)

Comments

Boris Brezillon Oct. 26, 2019, 7:36 a.m. UTC | #1
On Fri, 25 Oct 2019 23:26:42 +0300
Sergei Shtylyov <sergei.shtylyov@cogentembedded.com> wrote:

> Make use of the spi-mem direct mapping API to let advanced controllers
> optimize read/write operations when they support direct mapping.
> 
> Based on the original patch by Boris Brezillon <boris.brezillon@bootlin.com>.
> 
> Signed-off-by: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>
> 
> ---
> This patch is against the 'spi-nor/next' branch of the MTD 'linux.git' repo.
> 
>  drivers/mtd/spi-nor/spi-nor.c |   79 ++++++++++++++++++++++++++++++++++++++++++
>  include/linux/mtd/spi-nor.h   |    5 ++
>  2 files changed, 84 insertions(+)
> 
> Index: linux/drivers/mtd/spi-nor/spi-nor.c
> ===================================================================
> --- linux.orig/drivers/mtd/spi-nor/spi-nor.c
> +++ linux/drivers/mtd/spi-nor/spi-nor.c
> @@ -2562,6 +2562,14 @@ static int spi_nor_read(struct mtd_info
>  	if (ret)
>  		return ret;
>  
> +	if (nor->dirmap.rdesc) {
> +		ret = spi_mem_dirmap_read(nor->dirmap.rdesc, from, len, buf);

This spi_mem_dirmap_read() call should be moved to
spi_nor_spimem_read_data().

> +		if (ret < 0)
> +			goto read_err;
> +		*retlen += ret;
> +		goto done;
> +	}
> +
>  	while (len) {
>  		loff_t addr = from;
>  
> @@ -2582,6 +2590,7 @@ static int spi_nor_read(struct mtd_info
>  		from += ret;
>  		len -= ret;
>  	}
> +done:
>  	ret = 0;
>  
>  read_err:
> @@ -2686,6 +2695,14 @@ static int spi_nor_write(struct mtd_info
>  	if (ret)
>  		return ret;
>  
> +	if (nor->dirmap.wdesc) {
> +		ret = spi_mem_dirmap_write(nor->dirmap.wdesc, to, len, buf);

Same here, this should be moved to spi_nor_spimem_write_data(). BTW, I
wonder how this can work since write_enable() is called in the below for
loop (which you skip). Is your SPI controller sending the WE cmd
automatically?

> +		if (ret < 0)
> +			goto write_err;
> +		*retlen += ret;
> +		goto done;
> +	}
> +
>  	for (i = 0; i < len; ) {
>  		ssize_t written;
>  		loff_t addr = to + i;
> @@ -2723,6 +2740,8 @@ static int spi_nor_write(struct mtd_info
>  		*retlen += written;
>  		i += written;
>  	}
> +done:
> +	ret = 0;
>  
>  write_err:
>  	spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
> @@ -4991,6 +5010,58 @@ int spi_nor_scan(struct spi_nor *nor, co
>  }
>  EXPORT_SYMBOL_GPL(spi_nor_scan);
>
Sergei Shtylyov Nov. 7, 2019, 6:51 p.m. UTC | #2
Hello!

On 10/26/2019 10:36 AM, Boris Brezillon wrote:

>> Make use of the spi-mem direct mapping API to let advanced controllers
>> optimize read/write operations when they support direct mapping.
>>
>> Based on the original patch by Boris Brezillon <boris.brezillon@bootlin.com>.
>>
>> Signed-off-by: Sergei Shtylyov <sergei.shtylyov@cogentembedded.com>
>>
>> ---
>> This patch is against the 'spi-nor/next' branch of the MTD 'linux.git' repo.
>>
>>  drivers/mtd/spi-nor/spi-nor.c |   79 ++++++++++++++++++++++++++++++++++++++++++
>>  include/linux/mtd/spi-nor.h   |    5 ++
>>  2 files changed, 84 insertions(+)
>>
>> Index: linux/drivers/mtd/spi-nor/spi-nor.c
>> ===================================================================
>> --- linux.orig/drivers/mtd/spi-nor/spi-nor.c
>> +++ linux/drivers/mtd/spi-nor/spi-nor.c
>> @@ -2562,6 +2562,14 @@ static int spi_nor_read(struct mtd_info
>>  	if (ret)
>>  		return ret;
>>  
>> +	if (nor->dirmap.rdesc) {
>> +		ret = spi_mem_dirmap_read(nor->dirmap.rdesc, from, len, buf);
> 
> This spi_mem_dirmap_read() call should be moved to
> spi_nor_spimem_read_data().
> 
>> +		if (ret < 0)
>> +			goto read_err;
>> +		*retlen += ret;
>> +		goto done;
>> +	}
>> +
>>  	while (len) {
>>  		loff_t addr = from;
>>  
>> @@ -2582,6 +2590,7 @@ static int spi_nor_read(struct mtd_info
>>  		from += ret;
>>  		len -= ret;
>>  	}
>> +done:
>>  	ret = 0;
>>  
>>  read_err:
>> @@ -2686,6 +2695,14 @@ static int spi_nor_write(struct mtd_info
>>  	if (ret)
>>  		return ret;
>>  
>> +	if (nor->dirmap.wdesc) {
>> +		ret = spi_mem_dirmap_write(nor->dirmap.wdesc, to, len, buf);
> 
> Same here, this should be moved to spi_nor_spimem_write_data(). BTW, I
> wonder how this can work since write_enable() is called in the below for
> loop (which you skip). Is your SPI controller sending the WE cmd
> automatically?

   Probably not, it just doesn't have a write dirmap, only read, so this part
didn't get tested... :-)

>> +		if (ret < 0)
>> +			goto write_err;
>> +		*retlen += ret;
>> +		goto done;
>> +	}
>> +
>>  	for (i = 0; i < len; ) {
>>  		ssize_t written;
>>  		loff_t addr = to + i;
[...]

MBR, Sergei
diff mbox series

Patch

Index: linux/drivers/mtd/spi-nor/spi-nor.c
===================================================================
--- linux.orig/drivers/mtd/spi-nor/spi-nor.c
+++ linux/drivers/mtd/spi-nor/spi-nor.c
@@ -2562,6 +2562,14 @@  static int spi_nor_read(struct mtd_info
 	if (ret)
 		return ret;
 
+	if (nor->dirmap.rdesc) {
+		ret = spi_mem_dirmap_read(nor->dirmap.rdesc, from, len, buf);
+		if (ret < 0)
+			goto read_err;
+		*retlen += ret;
+		goto done;
+	}
+
 	while (len) {
 		loff_t addr = from;
 
@@ -2582,6 +2590,7 @@  static int spi_nor_read(struct mtd_info
 		from += ret;
 		len -= ret;
 	}
+done:
 	ret = 0;
 
 read_err:
@@ -2686,6 +2695,14 @@  static int spi_nor_write(struct mtd_info
 	if (ret)
 		return ret;
 
+	if (nor->dirmap.wdesc) {
+		ret = spi_mem_dirmap_write(nor->dirmap.wdesc, to, len, buf);
+		if (ret < 0)
+			goto write_err;
+		*retlen += ret;
+		goto done;
+	}
+
 	for (i = 0; i < len; ) {
 		ssize_t written;
 		loff_t addr = to + i;
@@ -2723,6 +2740,8 @@  static int spi_nor_write(struct mtd_info
 		*retlen += written;
 		i += written;
 	}
+done:
+	ret = 0;
 
 write_err:
 	spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
@@ -4991,6 +5010,58 @@  int spi_nor_scan(struct spi_nor *nor, co
 }
 EXPORT_SYMBOL_GPL(spi_nor_scan);
 
+static int spi_nor_create_read_dirmap(struct spi_nor *nor)
+{
+	struct spi_mem_dirmap_info info = {
+		.op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
+				      SPI_MEM_OP_ADDR(nor->addr_width, 0, 1),
+				      SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
+				      SPI_MEM_OP_DATA_IN(0, NULL, 1)),
+		.offset = 0,
+		.length = nor->mtd.size,
+	};
+	struct spi_mem_op *op = &info.op_tmpl;
+
+	/* get transfer protocols. */
+	op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
+	op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);
+	op->dummy.buswidth = op->addr.buswidth;
+	op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->read_proto);
+
+	/* convert the dummy cycles to the number of bytes */
+	op->dummy.nbytes = (nor->read_dummy * op->dummy.buswidth) / 8;
+
+	nor->dirmap.rdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
+						       &info);
+	return PTR_ERR_OR_ZERO(nor->dirmap.rdesc);
+}
+
+static int spi_nor_create_write_dirmap(struct spi_nor *nor)
+{
+	struct spi_mem_dirmap_info info = {
+		.op_tmpl = SPI_MEM_OP(SPI_MEM_OP_CMD(nor->program_opcode, 1),
+				      SPI_MEM_OP_ADDR(nor->addr_width, 0, 1),
+				      SPI_MEM_OP_NO_DUMMY,
+				      SPI_MEM_OP_DATA_OUT(0, NULL, 1)),
+		.offset = 0,
+		.length = nor->mtd.size,
+	};
+	struct spi_mem_op *op = &info.op_tmpl;
+
+	/* get transfer protocols. */
+	op->cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->write_proto);
+	op->addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->write_proto);
+	op->dummy.buswidth = op->addr.buswidth;
+	op->data.buswidth = spi_nor_get_protocol_data_nbits(nor->write_proto);
+
+	if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
+		op->addr.nbytes = 0;
+
+	nor->dirmap.wdesc = devm_spi_mem_dirmap_create(nor->dev, nor->spimem,
+						       &info);
+	return PTR_ERR_OR_ZERO(nor->dirmap.wdesc);
+}
+
 static int spi_nor_probe(struct spi_mem *spimem)
 {
 	struct spi_device *spi = spimem->spi;
@@ -5052,6 +5123,14 @@  static int spi_nor_probe(struct spi_mem
 			return -ENOMEM;
 	}
 
+	ret = spi_nor_create_read_dirmap(nor);
+	if (ret)
+		return ret;
+
+	ret = spi_nor_create_write_dirmap(nor);
+	if (ret)
+		return ret;
+
 	return mtd_device_register(&nor->mtd, data ? data->parts : NULL,
 				   data ? data->nr_parts : 0);
 }
Index: linux/include/linux/mtd/spi-nor.h
===================================================================
--- linux.orig/include/linux/mtd/spi-nor.h
+++ linux/include/linux/mtd/spi-nor.h
@@ -611,6 +611,11 @@  struct spi_nor {
 	int (*clear_sr_bp)(struct spi_nor *nor);
 	struct spi_nor_flash_parameter params;
 
+	struct {
+		struct spi_mem_dirmap_desc *rdesc;
+		struct spi_mem_dirmap_desc *wdesc;
+	} dirmap;
+
 	void *priv;
 };