Skip to contentSkip to conversation

mtd: mtk-quadspi: add support for memory-mapped flash reading

PATCH In review Chuanhong Guo 4 participants 12 messages Nov 6, 2019 – Nov 8, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover mtd: mtk-quadspi: add support for memory-mapped flash reading

This patchset adds support for optional memory-mapped flash reading.

BTW: This controller is a ridiculous one which only supports very limited spi-nor instructions. I can't rework the driver into a spi-mem one because MTK didn't provide register description in their datasheet and even if they do provide the documentation, the resulted driver will still be ridiculous because it'll need to check every supported instructions in support_op and do execution in one-by-one case in exec_op.

Chuanhong Guo (2):
mtd: mtk-quadspi: add support for memory-mapped flash reading
dt-bindings: mtd: mtk-quadspi: update bindings for mmap flash read

 .../devicetree/bindings/mtd/mtk-quadspi.txt   | 21 ++++++++++++++++++-
 drivers/mtd/spi-nor/mtk-quadspi.c             | 11 ++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH 1/2 mtd: mtk-quadspi: add support for memory-mapped flash reading

PIO reading mode on this controller is ridiculously inefficient (one cmd+addr+dummy sequence reads only one byte)
This patch adds support for reading from memory-mapped flash area which increases reading speed from 1MB/s to 5.6MB/s

---
 drivers/mtd/spi-nor/mtk-quadspi.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/mtd/spi-nor/mtk-quadspi.c +11 -0
index 34db01ab6cab..ba8b3be39896 100644
@@ -106,6 +106,7 @@ struct mtk_nor {
 	struct spi_nor nor;
 	struct device *dev;
 	void __iomem *base;	/* nor flash base address */
+	void __iomem *flash_base;
 	struct clk *spi_clk;
 	struct clk *nor_clk;
 };
@@ -272,6 +273,11 @@ static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
 	mtk_nor_set_read_mode(mtk_nor);
 	mtk_nor_set_addr(mtk_nor, addr);
 
+	if (mtk_nor->flash_base) {
+		memcpy_fromio(buffer, mtk_nor->flash_base + from, length);
+		return length;
+	}
+
 	for (i = 0; i < length; i++) {
 		ret = mtk_nor_execute_cmd(mtk_nor, MTK_NOR_PIO_READ_CMD);
 		if (ret < 0)
@@ -475,6 +481,11 @@ static int mtk_nor_drv_probe(struct platform_device *pdev)
 	if (IS_ERR(mtk_nor->base))
 		return PTR_ERR(mtk_nor->base);
 
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	mtk_nor->flash_base = devm_ioremap_resource(&pdev->dev, res);
+	if (IS_ERR(mtk_nor->flash_base))
+		mtk_nor->flash_base = NULL;
+
 	mtk_nor->spi_clk = devm_clk_get(&pdev->dev, "spi");
 	if (IS_ERR(mtk_nor->spi_clk))
 		return PTR_ERR(mtk_nor->spi_clk);
Signature · 1 line
2.21.0
#2 lore raw

Hi,

Quoted 23 lines · Chuanhong Guo

PIO reading mode on this controller is ridiculously inefficient (one cmd+addr+dummy sequence reads only one byte)
This patch adds support for reading from memory-mapped flash area which increases reading speed from 1MB/s to 5.6MB/s

---
 drivers/mtd/spi-nor/mtk-quadspi.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
drivers/mtd/spi-nor/mtk-quadspi.c +1 -0
index 34db01ab6cab..ba8b3be39896 100644
@@ -106,6 +106,7 @@ struct mtk_nor {
 	struct spi_nor nor;
 	struct device *dev;
 	void __iomem *base;	/* nor flash base address */
+	void __iomem *flash_base;
 	struct clk *spi_clk;
 	struct clk *nor_clk;
 };

drivers/mtd/spi-nor/mtk-quadspi.c static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length, · line 280

Quoted 8 lines
drivers/mtd/spi-nor/mtk-quadspi.c +5 -0
@@ -272,6 +273,11 @@ static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
 	mtk_nor_set_read_mode(mtk_nor);
 	mtk_nor_set_addr(mtk_nor, addr);
 
+	if (mtk_nor->flash_base) {
+		memcpy_fromio(buffer, mtk_nor->flash_base + from, length);
+		return length;
+	}
+

Don't you need to check if access is still within valid memory mapped window?

drivers/mtd/spi-nor/mtk-quadspi.c static int mtk_nor_drv_probe(struct platform_device *pdev) · line 485

Quoted 9 lines
drivers/mtd/spi-nor/mtk-quadspi.c +2 -0
 	for (i = 0; i < length; i++) {
 		ret = mtk_nor_execute_cmd(mtk_nor, MTK_NOR_PIO_READ_CMD);
 		if (ret < 0)
@@ -475,6 +481,11 @@ static int mtk_nor_drv_probe(struct platform_device *pdev)
 	if (IS_ERR(mtk_nor->base))
 		return PTR_ERR(mtk_nor->base);
 
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+	mtk_nor->flash_base = devm_ioremap_resource(&pdev->dev, res);

There is a single API now: devm_platform_ioremap_resource().

Quoted 7 lines
drivers/mtd/spi-nor/mtk-quadspi.c +3 -0
+	if (IS_ERR(mtk_nor->flash_base))
+		mtk_nor->flash_base = NULL;
+
 	mtk_nor->spi_clk = devm_clk_get(&pdev->dev, "spi");
 	if (IS_ERR(mtk_nor->spi_clk))
 		return PTR_ERR(mtk_nor->spi_clk);
Signature · 7 lines
Regards
Vignesh

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
#3 lore raw

Hi!

Quoted 4 lines · Vignesh Raghavendra
Quoted 4 lines
drivers/mtd/spi-nor/mtk-quadspi.c +0 -0
@@ -272,6 +273,11 @@ static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
      mtk_nor_set_read_mode(mtk_nor);
      mtk_nor_set_addr(mtk_nor, addr);
 
Quoted 8 lines

drivers/mtd/spi-nor/mtk-quadspi.c static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length, · line 280

Quoted 5 lines
drivers/mtd/spi-nor/mtk-quadspi.c +5 -0
+     if (mtk_nor->flash_base) {
+             memcpy_fromio(buffer, mtk_nor->flash_base + from, length);
+             return length;
+     }
+

Don't you need to check if access is still within valid memory mapped window?

The mapped area is 256MB and I don't quite believe there will be such a big NOR flash.
I'll add a check here in the next version.

Quoted 8 lines
Quoted 8 lines
drivers/mtd/spi-nor/mtk-quadspi.c +0 -0
      for (i = 0; i < length; i++) {
              ret = mtk_nor_execute_cmd(mtk_nor, MTK_NOR_PIO_READ_CMD);
              if (ret < 0)
@@ -475,6 +481,11 @@ static int mtk_nor_drv_probe(struct platform_device *pdev)
      if (IS_ERR(mtk_nor->base))
              return PTR_ERR(mtk_nor->base);
 
Quoted 4 lines

drivers/mtd/spi-nor/mtk-quadspi.c static int mtk_nor_drv_probe(struct platform_device *pdev) · line 485

Quoted 2 lines
drivers/mtd/spi-nor/mtk-quadspi.c +2 -0
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+     mtk_nor->flash_base = devm_ioremap_resource(&pdev->dev, res);

There is a single API now: devm_platform_ioremap_resource().

Cool. I'll change it.
Should I add another patch to change the same mapping operation right above this piece of code?

Regards,
Chuanhong Guo

#4 lore raw
Quoted 16 lines · Chuanhong Guo

Hi!

Quoted 4 lines · Vignesh Raghavendra
Quoted 4 lines
drivers/mtd/spi-nor/mtk-quadspi.c +0 -0
@@ -272,6 +273,11 @@ static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
      mtk_nor_set_read_mode(mtk_nor);
      mtk_nor_set_addr(mtk_nor, addr);
 
Quoted 8 lines

drivers/mtd/spi-nor/mtk-quadspi.c static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length, · line 280

Quoted 5 lines
drivers/mtd/spi-nor/mtk-quadspi.c +5 -0
+     if (mtk_nor->flash_base) {
+             memcpy_fromio(buffer, mtk_nor->flash_base + from, length);
+             return length;
+     }
+

Don't you need to check if access is still within valid memory mapped window?

Quoted 4 lines

The mapped area is 256MB and I don't quite believe there will be such a big NOR flash.
I'll add a check here in the next version.

There are 256MB (2GiB) NORs out there in market already. So, pretty soon, 256MB window won't be big enough :)

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
drivers/mtd/spi-nor/mtk-quadspi.c +0 -0
      for (i = 0; i < length; i++) {
              ret = mtk_nor_execute_cmd(mtk_nor, MTK_NOR_PIO_READ_CMD);
              if (ret < 0)
@@ -475,6 +481,11 @@ static int mtk_nor_drv_probe(struct platform_device *pdev)
      if (IS_ERR(mtk_nor->base))
              return PTR_ERR(mtk_nor->base);
 
Quoted 9 lines
Quoted 4 lines

drivers/mtd/spi-nor/mtk-quadspi.c static int mtk_nor_drv_probe(struct platform_device *pdev) · line 485

Quoted 2 lines
drivers/mtd/spi-nor/mtk-quadspi.c +2 -0
+     res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
+     mtk_nor->flash_base = devm_ioremap_resource(&pdev->dev, res);

There is a single API now: devm_platform_ioremap_resource().

Cool. I'll change it.
Should I add another patch to change the same mapping operation right above this piece of code?

That would be nice to do too, please send a separate patch.

Signature · 2 lines
Regards
Vignesh
#5 lore raw
In reply to Chuanhong Guo
Quoted 4 lines · Chuanhong Guo

PIO reading mode on this controller is ridiculously inefficient (one cmd+addr+dummy sequence reads only one byte)
This patch adds support for reading from memory-mapped flash area which increases reading speed from 1MB/s to 5.6MB/s

This may not be true for all MTK SoC. Which one are you testing?

Joe.C

#6 lore raw

Hi!

Quoted 9 lines · Yingjoe Chen
Quoted 4 lines · Chuanhong Guo

PIO reading mode on this controller is ridiculously inefficient (one cmd+addr+dummy sequence reads only one byte)
This patch adds support for reading from memory-mapped flash area which increases reading speed from 1MB/s to 5.6MB/s

This may not be true for all MTK SoC. Which one are you testing?

I tested it on MT7629.
There should be a 5x reading speed increment under DMA or direct read mode than PIO mode because PIO mode needs 30 or 36 clocks for every single byte of data while DMA or direct read only needs 24 or 30 clocks for initial command/address/dummy and every byte of data after that only need 8 clocks.

Regards,
Chuanhong Guo

#7 lore raw
In reply to Chuanhong Guo

Hi all!

Quoted 11 lines · Chuanhong Guo

PIO reading mode on this controller is ridiculously inefficient (one cmd+addr+dummy sequence reads only one byte)
This patch adds support for reading from memory-mapped flash area which increases reading speed from 1MB/s to 5.6MB/s

---
 drivers/mtd/spi-nor/mtk-quadspi.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

I'll abandon this patchset and implement DMA reading instead.

Regards,
Chuanhong Guo

#8 lore raw

PATCH 2/2 dt-bindings: mtd: mtk-quadspi: update bindings for mmap flash read

In reply to Chuanhong Guo

update register descriptions and add an example binding using it.

---
 .../devicetree/bindings/mtd/mtk-quadspi.txt   | 21 ++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/mtd/mtk-quadspi.txt +20 -1
index a12e3b5c495d..4860f6e96f5a 100644
@@ -12,7 +12,10 @@ Required properties:
 		  "mediatek,mt7623-nor", "mediatek,mt8173-nor"
 		  "mediatek,mt7629-nor", "mediatek,mt8173-nor"
 		  "mediatek,mt8173-nor"
-- reg: 		  physical base address and length of the controller's register
+- reg: 		  Contains one or two entries, each of which is a tuple consisting of a
+		  physical address and length. The first entry is the address and length
+		  of the controller register set. The optional second entry is the address
+		  and length of the area where the nor flash is mapped to.
 - clocks: 	  the phandle of the clocks needed by the nor controller
 - clock-names: 	  the names of the clocks
 		  the clocks should be named "spi" and "sf". "spi" is used for spi bus,
@@ -48,3 +51,19 @@ nor_flash: spi@1100d000 {
 	};
 };
 
+nor_flash: spi@11014000 {
+	compatible = "mediatek,mt7629-nor",
+		     "mediatek,mt8173-nor";
+	reg = <0x11014000 0xe0>,
+	      <0x30000000 0x10000000>;
+	clocks = <&pericfg CLK_PERI_FLASH_PD>,
+		 <&topckgen CLK_TOP_FLASH_SEL>;
+	clock-names = "spi", "sf";
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	flash@0 {
+		compatible = "jedec,spi-nor";
+		reg = <0>;
+	};
+};
Signature · 1 line
2.21.0
maintainer
#9 lore raw

Re: [PATCH 2/2] dt-bindings: mtd: mtk-quadspi: update bindings for mmap flash read

Quoted 12 lines · Chuanhong Guo

update register descriptions and add an example binding using it.

---
 .../devicetree/bindings/mtd/mtk-quadspi.txt   | 21 ++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/mtd/mtk-quadspi.txt +0 -0
index a12e3b5c495d..4860f6e96f5a 100644

Documentation/devicetree/bindings/mtd/mtk-quadspi.txt Required properties: · line 18

Quoted 8 lines
Documentation/devicetree/bindings/mtd/mtk-quadspi.txt +4 -1
@@ -12,7 +12,10 @@ Required properties:
 		  "mediatek,mt7623-nor", "mediatek,mt8173-nor"
 		  "mediatek,mt7629-nor", "mediatek,mt8173-nor"
 		  "mediatek,mt8173-nor"
-- reg: 		  physical base address and length of the controller's register
+- reg: 		  Contains one or two entries, each of which is a tuple consisting of a
+		  physical address and length. The first entry is the address and length
+		  of the controller register set. The optional second entry is the address
+		  and length of the area where the nor flash is mapped to.

All the compatibles support 2 entries? If not, which ones?

Quoted 26 lines
Documentation/devicetree/bindings/mtd/mtk-quadspi.txt +16 -0
 - clocks: 	  the phandle of the clocks needed by the nor controller
 - clock-names: 	  the names of the clocks
 		  the clocks should be named "spi" and "sf". "spi" is used for spi bus,
@@ -48,3 +51,19 @@ nor_flash: spi@1100d000 {
 	};
 };
 
+nor_flash: spi@11014000 {
+	compatible = "mediatek,mt7629-nor",
+		     "mediatek,mt8173-nor";
+	reg = <0x11014000 0xe0>,
+	      <0x30000000 0x10000000>;
+	clocks = <&pericfg CLK_PERI_FLASH_PD>,
+		 <&topckgen CLK_TOP_FLASH_SEL>;
+	clock-names = "spi", "sf";
+	#address-cells = <1>;
+	#size-cells = <0>;
+
+	flash@0 {
+		compatible = "jedec,spi-nor";
+		reg = <0>;
+	};
+};
Signature · 1 line
2.21.0
#10 lore raw

Re: [PATCH 2/2] dt-bindings: mtd: mtk-quadspi: update bindings for mmap flash read

Hi!

Quoted 9 lines · Rob Herring
Quoted 7 lines · Chuanhong Guo

update register descriptions and add an example binding using it.

---
 .../devicetree/bindings/mtd/mtk-quadspi.txt   | 21 ++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
Quoted 15 lines

Documentation/devicetree/bindings/mtd/mtk-quadspi.txt Required properties: · line 18

Quoted 13 lines
Documentation/devicetree/bindings/mtd/mtk-quadspi.txt +4 -1
index a12e3b5c495d..4860f6e96f5a 100644
@@ -12,7 +12,10 @@ Required properties:
                "mediatek,mt7623-nor", "mediatek,mt8173-nor"
                "mediatek,mt7629-nor", "mediatek,mt8173-nor"
                "mediatek,mt8173-nor"
-- reg:                 physical base address and length of the controller's register
+- reg:                 Contains one or two entries, each of which is a tuple consisting of a
+               physical address and length. The first entry is the address and length
+               of the controller register set. The optional second entry is the address
+               and length of the area where the nor flash is mapped to.

All the compatibles support 2 entries? If not, which ones?

It should be. I implemented it as an optional feature only because I don't know the mapped address space for all these chips and can't update every device trees.

Regards,
Chuanhong Guo