PIO reading mode on this controller is pretty inefficient
(one cmd+addr+dummy sequence reads only one byte)
This patch adds support for reading using DMA mode which increases
reading speed from 1MB/s to 4MB/s
DMA busy checking is implemented with readl_poll_timeout because
I don't have access to IRQ-related docs. The speed increment comes
from those saved cmd+addr+dummy clocks.
This controller requires that DMA source/destination address and
reading length should be 16-byte aligned. We use a bounce buffer if
one of them is not aligned, read more than what we need, and copy
data from corresponding buffer offset.
Signed-off-by: Chuanhong Guo <[email protected]>
---
drivers/mtd/spi-nor/mtk-quadspi.c | 99 +++++++++++++++++++++++++++++--
1 file changed, 95 insertions(+), 4 deletions(-)
diff --git a/drivers/mtd/spi-nor/mtk-quadspi.c b/drivers/mtd/spi-nor/mtk-quadspi.c
index b1691680d174..ac0e531ce80c 100644
--- a/drivers/mtd/spi-nor/mtk-quadspi.c
+++ b/drivers/mtd/spi-nor/mtk-quadspi.c
@@ -7,6 +7,7 @@
#include <linux/clk.h>
#include <linux/delay.h>
#include <linux/device.h>
+#include <linux/dma-mapping.h>
#include <linux/init.h>
#include <linux/io.h>
#include <linux/iopoll.h>
@@ -17,6 +18,7 @@
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
+#include <linux/sched/task_stack.h>
#include <linux/slab.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
@@ -70,6 +72,10 @@
#define MTK_NOR_DELSEL2_REG 0xd0
#define MTK_NOR_DELSEL3_REG 0xd4
#define MTK_NOR_DELSEL4_REG 0xd8
+#define MTK_NOR_FDMA_CTL_REG 0x718
+#define MTK_NOR_FDMA_FADR_REG 0x71c
+#define MTK_NOR_FDMA_DADR_REG 0x720
+#define MTK_NOR_FDMA_END_DADR_REG 0x724
/* commands for mtk nor controller */
#define MTK_NOR_READ_CMD 0x0
@@ -88,6 +94,7 @@
#define MTK_NOR_DUAL_READ_EN 0x1
#define MTK_NOR_DUAL_DISABLE 0x0
#define MTK_NOR_FAST_READ 0x1
+#define MTK_NOR_DMA_TRIG 0x1
#define SFLASH_WRBUF_SIZE 128
@@ -97,7 +104,10 @@
#define MTK_NOR_MAX_SHIFT 7
/* nor controller 4-byte address mode enable bit */
#define MTK_NOR_4B_ADDR_EN BIT(4)
-
+/* DMA address has to be 16-byte aligned */
+#define MTK_NOR_DMA_ALIGN 16
+/* Limit bounce buffer size to 32KB */
+#define MTK_NOR_MAX_BBUF_READ (32 * 1024)
/* Helpers for accessing the program data / shift data registers */
#define MTK_NOR_PRG_REG(n) (MTK_NOR_PRGDATA0_REG + 4 * (n))
#define MTK_NOR_SHREG(n) (MTK_NOR_SHREG0_REG + 4 * (n))
@@ -260,13 +270,12 @@ static void mtk_nor_set_addr(struct mtk_nor *mtk_nor, u32 addr)
writeb(addr & 0xff, mtk_nor->base + MTK_NOR_RADR3_REG);
}
-static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
- u_char *buffer)
+static ssize_t mtk_nor_read_pio(struct mtk_nor *mtk_nor, loff_t from,
+ size_t length, u_char *buffer)
{
int i, ret;
int addr = (int)from;
u8 *buf = (u8 *)buffer;
- struct mtk_nor *mtk_nor = nor->priv;
/* set mode for fast read mode ,dual mode or quad mode */
mtk_nor_set_read_mode(mtk_nor);
@@ -281,6 +290,88 @@ static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
return length;
}
+static int mtk_nor_dma_exec(struct mtk_nor *mtk_nor)
+{
+ int reg;
+
+ reg = readl(mtk_nor->base + MTK_NOR_FDMA_CTL_REG);
+ writel(reg | MTK_NOR_DMA_TRIG, mtk_nor->base + MTK_NOR_FDMA_CTL_REG);
+ return readl_poll_timeout(mtk_nor->base + MTK_NOR_FDMA_CTL_REG, reg,
+ !(reg & MTK_NOR_DMA_TRIG), 20, 10000);
+}
+
+static ssize_t mtk_nor_read_dma(struct mtk_nor *mtk_nor, loff_t from,
+ size_t length, u_char *buffer)
+{
+ ssize_t ret;
+ ssize_t read_length = length & ~(MTK_NOR_DMA_ALIGN - 1);
+ dma_addr_t dma_addr;
+
+ mtk_nor_set_read_mode(mtk_nor);
+ mtk_nor_set_addr_width(mtk_nor);
+
+ dma_addr = dma_map_single(mtk_nor->dev, buffer, read_length,
+ DMA_FROM_DEVICE);
+ if (dma_mapping_error(mtk_nor->dev, dma_addr)) {
+ dev_err(mtk_nor->dev, "failed to map dma buffer.");
+ return -EINVAL;
+ }
+
+ writel(from, mtk_nor->base + MTK_NOR_FDMA_FADR_REG);
+ writel(dma_addr, mtk_nor->base + MTK_NOR_FDMA_DADR_REG);
+ writel((u32)dma_addr + read_length,
+ mtk_nor->base + MTK_NOR_FDMA_END_DADR_REG);
+ ret = mtk_nor_dma_exec(mtk_nor);
+ dma_unmap_single(mtk_nor->dev, dma_addr, read_length, DMA_FROM_DEVICE);
+ if (!ret)
+ ret = read_length;
+ return ret;
+}
+
+static ssize_t mtk_nor_read_dma_bounce(struct mtk_nor *mtk_nor, loff_t from,
+ size_t length, u_char *buffer)
+{
+ ssize_t nor_unaligned_len = from % MTK_NOR_DMA_ALIGN;
+ loff_t read_from = from & ~(MTK_NOR_DMA_ALIGN - 1);
+ ssize_t read_len;
+ u_char *buf;
+ u_char *bouncebuf;
+ size_t mem_unaligned_len;
+
+ if (length > MTK_NOR_MAX_BBUF_READ)
+ length = MTK_NOR_MAX_BBUF_READ;
+ read_len = length + nor_unaligned_len + MTK_NOR_DMA_ALIGN;
+
+ buf = kmalloc(read_len + MTK_NOR_DMA_ALIGN, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+
+ mem_unaligned_len = (u32)buf % MTK_NOR_DMA_ALIGN;
+ bouncebuf = (buf + MTK_NOR_DMA_ALIGN) - mem_unaligned_len;
+
+ read_len = mtk_nor_read_dma(mtk_nor, read_from, read_len, bouncebuf);
+ if (read_len > 0)
+ memcpy(buffer, bouncebuf + nor_unaligned_len, length);
+
+ kfree(buf);
+ return length;
+}
+
+static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
+ u_char *buffer)
+{
+ struct mtk_nor *mtk_nor = nor->priv;
+
+ if (length < MTK_NOR_DMA_ALIGN)
+ return mtk_nor_read_pio(mtk_nor, from, length, buffer);
+
+ if (object_is_on_stack(buffer) || !virt_addr_valid(buffer) ||
+ (u32)buffer % MTK_NOR_DMA_ALIGN || from % MTK_NOR_DMA_ALIGN)
+ return mtk_nor_read_dma_bounce(mtk_nor, from, length, buffer);
+
+ return mtk_nor_read_dma(mtk_nor, from, length, buffer);
+}
+
static int mtk_nor_write_single_byte(struct mtk_nor *mtk_nor,
int addr, int length, u8 *data)
{
--
2.21.0
1_1_1 reading mode on this controller only support 0x03 and 0x0b
as opcode, but spi-nor framework uses nor->read for SFDP reading
as well.
Add a check for opcode and if it's not supported, misuse 1_1_2
reading and extract corresponding bits from returned data.
Signed-off-by: Chuanhong Guo <[email protected]>
---
drivers/mtd/spi-nor/mtk-quadspi.c | 78 ++++++++++++++++++++++++++++++-
1 file changed, 76 insertions(+), 2 deletions(-)
diff --git a/drivers/mtd/spi-nor/mtk-quadspi.c b/drivers/mtd/spi-nor/mtk-quadspi.c
index ac0e531ce80c..46bf27c0e6e8 100644
--- a/drivers/mtd/spi-nor/mtk-quadspi.c
+++ b/drivers/mtd/spi-nor/mtk-quadspi.c
@@ -357,8 +357,8 @@ static ssize_t mtk_nor_read_dma_bounce(struct mtk_nor *mtk_nor, loff_t from,
return length;
}
-static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
- u_char *buffer)
+static ssize_t mtk_nor_flash_read(struct spi_nor *nor, loff_t from,
+ size_t length, u_char *buffer)
{
struct mtk_nor *mtk_nor = nor->priv;
@@ -372,6 +372,80 @@ static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
return mtk_nor_read_dma(mtk_nor, from, length, buffer);
}
+static ssize_t mtk_nor_generic_read(struct spi_nor *nor, loff_t from,
+ size_t length, u_char *buffer)
+{
+ struct mtk_nor *mtk_nor = nor->priv;
+ ssize_t nor_unaligned_len = from % MTK_NOR_DMA_ALIGN;
+ loff_t read_from = from & ~(MTK_NOR_DMA_ALIGN - 1);
+ ssize_t read_len;
+ u_char *buf, *bouncebuf, tmp;
+ size_t mem_unaligned_len, i;
+ dma_addr_t dma_addr;
+ int ret;
+
+ if (length > MTK_NOR_MAX_BBUF_READ / 2)
+ length = MTK_NOR_MAX_BBUF_READ / 2;
+ read_len = ((length + nor_unaligned_len) * 2 + MTK_NOR_DMA_ALIGN) &
+ ~(MTK_NOR_DMA_ALIGN - 1);
+
+ buf = kmalloc(read_len + MTK_NOR_DMA_ALIGN, GFP_KERNEL);
+ if (!buf)
+ return -ENOMEM;
+
+ mem_unaligned_len = (u32)buf % MTK_NOR_DMA_ALIGN;
+ bouncebuf = (buf + MTK_NOR_DMA_ALIGN) - mem_unaligned_len;
+
+ writeb(nor->read_opcode, mtk_nor->base + MTK_NOR_PRGDATA3_REG);
+ writeb(MTK_NOR_DUAL_READ_EN, mtk_nor->base + MTK_NOR_DUAL_REG);
+ mtk_nor_set_addr_width(mtk_nor);
+
+ dma_addr = dma_map_single(mtk_nor->dev, bouncebuf, read_len,
+ DMA_FROM_DEVICE);
+ ret = dma_mapping_error(mtk_nor->dev, dma_addr);
+ if (ret) {
+ dev_err(mtk_nor->dev, "failed to map dma buffer.");
+ goto err;
+ }
+
+ writel(read_from, mtk_nor->base + MTK_NOR_FDMA_FADR_REG);
+ writel(dma_addr, mtk_nor->base + MTK_NOR_FDMA_DADR_REG);
+ writel((u32)dma_addr + read_len,
+ mtk_nor->base + MTK_NOR_FDMA_END_DADR_REG);
+ ret = mtk_nor_dma_exec(mtk_nor);
+ dma_unmap_single(mtk_nor->dev, dma_addr, read_len, DMA_FROM_DEVICE);
+
+ if (ret)
+ goto err;
+
+ /* extract bits from DO line */
+ for (i = 0; i < length; i++) {
+ tmp = bouncebuf[(i + nor_unaligned_len) * 2];
+ buffer[i] = (tmp & BIT(7)) | ((tmp & BIT(5)) << 1) |
+ ((tmp & BIT(3)) << 2) | ((tmp & BIT(1)) << 3);
+ tmp = bouncebuf[(i + nor_unaligned_len) * 2 + 1];
+ buffer[i] |= (tmp & BIT(7)) >> 4 | ((tmp & BIT(5)) >> 3) |
+ ((tmp & BIT(3)) >> 2) | ((tmp & BIT(1)) >> 1);
+ }
+ ret = length;
+err:
+ kfree(buf);
+ return ret;
+}
+
+static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
+ u_char *buffer)
+{
+ if ((nor->read_proto != SNOR_PROTO_1_1_1) ||
+ (nor->read_opcode == SPINOR_OP_READ) ||
+ (nor->read_opcode == SPINOR_OP_READ_FAST))
+ return mtk_nor_flash_read(nor, from, length, buffer);
+ else if (nor->read_dummy == 8)
+ return mtk_nor_generic_read(nor, from, length, buffer);
+ else
+ return -EOPNOTSUPP;
+}
+
static int mtk_nor_write_single_byte(struct mtk_nor *mtk_nor,
int addr, int length, u8 *data)
{
--
2.21.0
Hi Chuanhong,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on arm-soc/for-next]
[cannot apply to v5.4-rc7 next-20191111]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Chuanhong-Guo/mtd-mtk-quadspi-add-support-for-DMA-reading/20191112-145019
base: https://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc.git for-next
config: sparc-allyesconfig (attached as .config)
compiler: sparc64-linux-gcc (GCC) 7.4.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.4.0 make.cross ARCH=sparc
If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <[email protected]>
All warnings (new ones prefixed by >>):
drivers/mtd/spi-nor/mtk-quadspi.c: In function 'mtk_nor_read_dma_bounce':
>> drivers/mtd/spi-nor/mtk-quadspi.c:349:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
mem_unaligned_len = (u32)buf % MTK_NOR_DMA_ALIGN;
^
drivers/mtd/spi-nor/mtk-quadspi.c: In function 'mtk_nor_read':
drivers/mtd/spi-nor/mtk-quadspi.c:369:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
(u32)buffer % MTK_NOR_DMA_ALIGN || from % MTK_NOR_DMA_ALIGN)
^
vim +349 drivers/mtd/spi-nor/mtk-quadspi.c
330
331 static ssize_t mtk_nor_read_dma_bounce(struct mtk_nor *mtk_nor, loff_t from,
332 size_t length, u_char *buffer)
333 {
334 ssize_t nor_unaligned_len = from % MTK_NOR_DMA_ALIGN;
335 loff_t read_from = from & ~(MTK_NOR_DMA_ALIGN - 1);
336 ssize_t read_len;
337 u_char *buf;
338 u_char *bouncebuf;
339 size_t mem_unaligned_len;
340
341 if (length > MTK_NOR_MAX_BBUF_READ)
342 length = MTK_NOR_MAX_BBUF_READ;
343 read_len = length + nor_unaligned_len + MTK_NOR_DMA_ALIGN;
344
345 buf = kmalloc(read_len + MTK_NOR_DMA_ALIGN, GFP_KERNEL);
346 if (!buf)
347 return -ENOMEM;
348
> 349 mem_unaligned_len = (u32)buf % MTK_NOR_DMA_ALIGN;
350 bouncebuf = (buf + MTK_NOR_DMA_ALIGN) - mem_unaligned_len;
351
352 read_len = mtk_nor_read_dma(mtk_nor, read_from, read_len, bouncebuf);
353 if (read_len > 0)
354 memcpy(buffer, bouncebuf + nor_unaligned_len, length);
355
356 kfree(buf);
357 return length;
358 }
359
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/[email protected] Intel Corporation
Hi Chuanhong,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on arm-soc/for-next]
[cannot apply to v5.4-rc7 next-20191108]
[if your patch is applied to the wrong git tree, please drop us a note to help
improve the system. BTW, we also suggest to use '--base' option to specify the
base tree in git format-patch, please see https://stackoverflow.com/a/37406982]
url: https://github.com/0day-ci/linux/commits/Chuanhong-Guo/mtd-mtk-quadspi-add-support-for-DMA-reading/20191112-145019
base: https://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc.git for-next
config: x86_64-randconfig-a004-201945 (attached as .config)
compiler: gcc-7 (Debian 7.4.0-14) 7.4.0
reproduce:
# save the attached .config to linux build tree
make ARCH=x86_64
If you fix the issue, kindly add following tag
Reported-by: kbuild test robot <[email protected]>
All warnings (new ones prefixed by >>):
drivers/mtd/spi-nor/mtk-quadspi.c: In function 'mtk_nor_read_dma_bounce':
drivers/mtd/spi-nor/mtk-quadspi.c:349:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
mem_unaligned_len = (u32)buf % MTK_NOR_DMA_ALIGN;
^
In file included from include/linux/err.h:5:0,
from include/linux/clk.h:12,
from drivers/mtd/spi-nor/mtk-quadspi.c:7:
drivers/mtd/spi-nor/mtk-quadspi.c: In function 'mtk_nor_read':
drivers/mtd/spi-nor/mtk-quadspi.c:369:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
(u32)buffer % MTK_NOR_DMA_ALIGN || from % MTK_NOR_DMA_ALIGN)
^
include/linux/compiler.h:58:52: note: in definition of macro '__trace_if_var'
#define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
^~~~
>> drivers/mtd/spi-nor/mtk-quadspi.c:368:2: note: in expansion of macro 'if'
if (object_is_on_stack(buffer) || !virt_addr_valid(buffer) ||
^~
drivers/mtd/spi-nor/mtk-quadspi.c:369:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
(u32)buffer % MTK_NOR_DMA_ALIGN || from % MTK_NOR_DMA_ALIGN)
^
include/linux/compiler.h:58:61: note: in definition of macro '__trace_if_var'
#define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
^~~~
>> drivers/mtd/spi-nor/mtk-quadspi.c:368:2: note: in expansion of macro 'if'
if (object_is_on_stack(buffer) || !virt_addr_valid(buffer) ||
^~
drivers/mtd/spi-nor/mtk-quadspi.c:369:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
(u32)buffer % MTK_NOR_DMA_ALIGN || from % MTK_NOR_DMA_ALIGN)
^
include/linux/compiler.h:69:3: note: in definition of macro '__trace_if_value'
(cond) ? \
^~~~
include/linux/compiler.h:56:28: note: in expansion of macro '__trace_if_var'
#define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
^~~~~~~~~~~~~~
>> drivers/mtd/spi-nor/mtk-quadspi.c:368:2: note: in expansion of macro 'if'
if (object_is_on_stack(buffer) || !virt_addr_valid(buffer) ||
^~
vim +/if +368 drivers/mtd/spi-nor/mtk-quadspi.c
359
360 static ssize_t mtk_nor_read(struct spi_nor *nor, loff_t from, size_t length,
361 u_char *buffer)
362 {
363 struct mtk_nor *mtk_nor = nor->priv;
364
365 if (length < MTK_NOR_DMA_ALIGN)
366 return mtk_nor_read_pio(mtk_nor, from, length, buffer);
367
> 368 if (object_is_on_stack(buffer) || !virt_addr_valid(buffer) ||
369 (u32)buffer % MTK_NOR_DMA_ALIGN || from % MTK_NOR_DMA_ALIGN)
370 return mtk_nor_read_dma_bounce(mtk_nor, from, length, buffer);
371
372 return mtk_nor_read_dma(mtk_nor, from, length, buffer);
373 }
374
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/hyperkitty/list/[email protected] Intel Corporation
Hi all!
On Tue, Nov 12, 2019 at 3:29 PM kbuild test robot <[email protected]> wrote:
> [...]
> All warnings (new ones prefixed by >>):
>
> drivers/mtd/spi-nor/mtk-quadspi.c: In function 'mtk_nor_read_dma_bounce':
> >> drivers/mtd/spi-nor/mtk-quadspi.c:349:22: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
> mem_unaligned_len = (u32)buf % MTK_NOR_DMA_ALIGN;
> ^
> drivers/mtd/spi-nor/mtk-quadspi.c: In function 'mtk_nor_read':
> drivers/mtd/spi-nor/mtk-quadspi.c:369:6: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
> (u32)buffer % MTK_NOR_DMA_ALIGN || from % MTK_NOR_DMA_ALIGN)
> ^
>
DMA mode on this controller requires that source address, destination
address and reading
length should all be 16-byte aligned. And because of this, I didn't
use the bounce buffer
provided by spi-nor framework and allocate its own one.
Should I just cast all these pointers to ulong or are there better
ways to check for address
alignments and/or obtain an aligned buffer?
Regards,
Chuanhong Guo