Skip to contentSkip to conversation

mtd: bcm47xxsflash: support reading flash out of mapping window

PATCH Superseded Rafał Miłecki 4 participants 9 messages Jan 23, 2017 – Feb 8, 2017 Memory technology devices (MTD)

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 mtd: bcm47xxsflash: support reading flash out of mapping window

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for read crossing boundary (performance)

---
 drivers/mtd/devices/bcm47xxsflash.c | 23 ++++++++++++++++++++---
 drivers/mtd/devices/bcm47xxsflash.h |  3 +++
 2 files changed, 23 insertions(+), 3 deletions(-)
drivers/mtd/devices/bcm47xxsflash.c +20 -3
index 4decd8c0360a..138689619ab4 100644
@@ -105,15 +105,32 @@ static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
 			      size_t *retlen, u_char *buf)
 {
 	struct bcm47xxsflash *b47s = mtd->priv;
+	size_t orig_len = len;
 
 	/* Check address range */
 	if ((from + len) > mtd->size)
 		return -EINVAL;
 
-	memcpy_fromio(buf, b47s->window + from, len);
-	*retlen = len;
+	/* Read as much as possible using fast MMIO window */
+	if (from < BCM47XXSFLASH_WINDOW_SZ) {
+		size_t memcpy_len;
 
-	return len;
+		memcpy_len = min(len, (size_t)(BCM47XXSFLASH_WINDOW_SZ - from));
+		memcpy_fromio(buf, b47s->window + from, memcpy_len);
+		from += memcpy_len;
+		len -= memcpy_len;
+	}
+
+	/* Use indirect access for content out of the window */
+	for (; len; len--) {
+		b47s->cc_write(b47s, BCMA_CC_FLASHADDR, from++);
+		bcm47xxsflash_cmd(b47s, OPCODE_ST_READ4B);
+		*buf++ = b47s->cc_read(b47s, BCMA_CC_FLASHDATA);
+	}
+
+	*retlen = orig_len;
+
+	return orig_len;
 }
 
 static int bcm47xxsflash_write_st(struct mtd_info *mtd, u32 offset, size_t len,
drivers/mtd/devices/bcm47xxsflash.h +3 -0
index 1564b62b412e..b2d7b38f75fd 100644
@@ -3,6 +3,8 @@
 
 #include <linux/mtd/mtd.h>
 
+#define BCM47XXSFLASH_WINDOW_SZ			SZ_16M
+
 /* Used for ST flashes only. */
 #define OPCODE_ST_WREN		0x0006		/* Write Enable */
 #define OPCODE_ST_WRDIS		0x0004		/* Write Disable */
@@ -16,6 +18,7 @@
 #define OPCODE_ST_RES		0x03ab		/* Read Electronic Signature */
 #define OPCODE_ST_CSA		0x1000		/* Keep chip select asserted */
 #define OPCODE_ST_SSE		0x0220		/* Sub-sector Erase */
+#define OPCODE_ST_READ4B	0x6313		/* Read Data Bytes in 4Byte addressing mode */
 
 /* Used for Atmel flashes only. */
 #define OPCODE_AT_READ				0x07e8
Signature · 1 line
2.11.0
Acked-by on v3 1/1
#1 lore raw
Quoted 8 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

Quoted 6 lines

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for read crossing boundary (performance)

It's not because of performance, but because you can start reading at 15 MiB offset and read 2 MiB , at which point the previous patch would IMO fail.

Thanks!

Quoted 67 lines
---
 drivers/mtd/devices/bcm47xxsflash.c | 23 ++++++++++++++++++++---
 drivers/mtd/devices/bcm47xxsflash.h |  3 +++
 2 files changed, 23 insertions(+), 3 deletions(-)
drivers/mtd/devices/bcm47xxsflash.c +20 -3
index 4decd8c0360a..138689619ab4 100644
@@ -105,15 +105,32 @@ static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
 			      size_t *retlen, u_char *buf)
 {
 	struct bcm47xxsflash *b47s = mtd->priv;
+	size_t orig_len = len;
 
 	/* Check address range */
 	if ((from + len) > mtd->size)
 		return -EINVAL;
 
-	memcpy_fromio(buf, b47s->window + from, len);
-	*retlen = len;
+	/* Read as much as possible using fast MMIO window */
+	if (from < BCM47XXSFLASH_WINDOW_SZ) {
+		size_t memcpy_len;
 
-	return len;
+		memcpy_len = min(len, (size_t)(BCM47XXSFLASH_WINDOW_SZ - from));
+		memcpy_fromio(buf, b47s->window + from, memcpy_len);
+		from += memcpy_len;
+		len -= memcpy_len;
+	}
+
+	/* Use indirect access for content out of the window */
+	for (; len; len--) {
+		b47s->cc_write(b47s, BCMA_CC_FLASHADDR, from++);
+		bcm47xxsflash_cmd(b47s, OPCODE_ST_READ4B);
+		*buf++ = b47s->cc_read(b47s, BCMA_CC_FLASHDATA);
+	}
+
+	*retlen = orig_len;
+
+	return orig_len;
 }
 
 static int bcm47xxsflash_write_st(struct mtd_info *mtd, u32 offset, size_t len,
drivers/mtd/devices/bcm47xxsflash.h +3 -0
index 1564b62b412e..b2d7b38f75fd 100644
@@ -3,6 +3,8 @@
 
 #include <linux/mtd/mtd.h>
 
+#define BCM47XXSFLASH_WINDOW_SZ			SZ_16M
+
 /* Used for ST flashes only. */
 #define OPCODE_ST_WREN		0x0006		/* Write Enable */
 #define OPCODE_ST_WRDIS		0x0004		/* Write Disable */
@@ -16,6 +18,7 @@
 #define OPCODE_ST_RES		0x03ab		/* Read Electronic Signature */
 #define OPCODE_ST_CSA		0x1000		/* Keep chip select asserted */
 #define OPCODE_ST_SSE		0x0220		/* Sub-sector Erase */
+#define OPCODE_ST_READ4B	0x6313		/* Read Data Bytes in 4Byte addressing mode */
 
 /* Used for Atmel flashes only. */
 #define OPCODE_AT_READ				0x07e8
Signature · 2 lines
Best regards,
Marek Vasut
#2 lore raw
Quoted 16 lines · Marek Vasut
Quoted 8 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

Quoted 6 lines

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for read crossing boundary (performance)

Quoted 3 lines

It's not because of performance, but because you can start reading at 15 MiB offset and read 2 MiB , at which point the previous patch would IMO fail.

It never failed. It was slow but was working as expected.

Quoted 3 lines

Thanks!

Thanks

#3 lore raw
Quoted 18 lines · Rafał Miłecki
Quoted 17 lines · Marek Vasut
Quoted 15 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for read crossing boundary (performance)

Quoted 5 lines
Quoted 3 lines

It's not because of performance, but because you can start reading at 15 MiB offset and read 2 MiB , at which point the previous patch would IMO fail.

It never failed. It was slow but was working as expected.

Maybe you didn't test with dumb enough mis-usage ;) I think something like this would've triggered it:

$ dd if=/dev/mtd0 of=/dev/null seek=15M bs=2M
Assuming mtd0 is the whole flash device, and the flash is larger than 17
MiB.

But then again, it should be OK now and we can move on to the next problem.

Quoted 5 lines
Quoted 3 lines

Thanks!

Thanks

Signature · 2 lines
Best regards,
Marek Vasut
#4 lore raw
In reply to Rafał Miłecki

Hi,

Quoted 34 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for read crossing boundary (performance)

---
 drivers/mtd/devices/bcm47xxsflash.c | 23 ++++++++++++++++++++---
 drivers/mtd/devices/bcm47xxsflash.h |  3 +++
 2 files changed, 23 insertions(+), 3 deletions(-)
drivers/mtd/devices/bcm47xxsflash.c +1 -1
index 4decd8c0360a..138689619ab4 100644
@@ -105,15 +105,32 @@ static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
 			      size_t *retlen, u_char *buf)
 {
 	struct bcm47xxsflash *b47s = mtd->priv;
+	size_t orig_len = len;
 
 	/* Check address range */
 	if ((from + len) > mtd->size)
 		return -EINVAL;
 
-	memcpy_fromio(buf, b47s->window + from, len);

drivers/mtd/devices/bcm47xxsflash.c static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len, · line 121

Quoted 10 lines
drivers/mtd/devices/bcm47xxsflash.c +7 -2
-	*retlen = len;
+	/* Read as much as possible using fast MMIO window */
+	if (from < BCM47XXSFLASH_WINDOW_SZ) {
+		size_t memcpy_len;
 
-	return len;
+		memcpy_len = min(len, (size_t)(BCM47XXSFLASH_WINDOW_SZ - from));
+		memcpy_fromio(buf, b47s->window + from, memcpy_len);
+		from += memcpy_len;
+		len -= memcpy_len;

Am I crazy, or should you either be doing 'buf += memcpy_len'? Otherwise, you'll be copying over the first few elements of 'buf' below, instead of finishing off the tail end.

Brian

Quoted 39 lines
drivers/mtd/devices/bcm47xxsflash.c +12 -0
+	}
+
+	/* Use indirect access for content out of the window */
+	for (; len; len--) {
+		b47s->cc_write(b47s, BCMA_CC_FLASHADDR, from++);
+		bcm47xxsflash_cmd(b47s, OPCODE_ST_READ4B);
+		*buf++ = b47s->cc_read(b47s, BCMA_CC_FLASHDATA);
+	}
+
+	*retlen = orig_len;
+
+	return orig_len;
 }
 
 static int bcm47xxsflash_write_st(struct mtd_info *mtd, u32 offset, size_t len,
drivers/mtd/devices/bcm47xxsflash.h +3 -0
index 1564b62b412e..b2d7b38f75fd 100644
@@ -3,6 +3,8 @@
 
 #include <linux/mtd/mtd.h>
 
+#define BCM47XXSFLASH_WINDOW_SZ			SZ_16M
+
 /* Used for ST flashes only. */
 #define OPCODE_ST_WREN		0x0006		/* Write Enable */
 #define OPCODE_ST_WRDIS		0x0004		/* Write Disable */
@@ -16,6 +18,7 @@
 #define OPCODE_ST_RES		0x03ab		/* Read Electronic Signature */
 #define OPCODE_ST_CSA		0x1000		/* Keep chip select asserted */
 #define OPCODE_ST_SSE		0x0220		/* Sub-sector Erase */
+#define OPCODE_ST_READ4B	0x6313		/* Read Data Bytes in 4Byte addressing mode */
 
 /* Used for Atmel flashes only. */
 #define OPCODE_AT_READ				0x07e8
Signature · 1 line
2.11.0
#5 lore raw
Quoted 40 lines · Brian Norris
Quoted 39 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for read crossing boundary (performance)

---
 drivers/mtd/devices/bcm47xxsflash.c | 23 ++++++++++++++++++++---
 drivers/mtd/devices/bcm47xxsflash.h |  3 +++
 2 files changed, 23 insertions(+), 3 deletions(-)
drivers/mtd/devices/bcm47xxsflash.c +4 -2
index 4decd8c0360a..138689619ab4 100644
@@ -105,15 +105,32 @@ static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
                            size_t *retlen, u_char *buf)
 {
      struct bcm47xxsflash *b47s = mtd->priv;
+     size_t orig_len = len;
 
      /* Check address range */
      if ((from + len) > mtd->size)
              return -EINVAL;
 
-     memcpy_fromio(buf, b47s->window + from, len);
-     *retlen = len;
+     /* Read as much as possible using fast MMIO window */
+     if (from < BCM47XXSFLASH_WINDOW_SZ) {
+             size_t memcpy_len;
 
Quoted 9 lines

drivers/mtd/devices/bcm47xxsflash.c static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len, · line 121

Quoted 5 lines
drivers/mtd/devices/bcm47xxsflash.c +4 -1
-     return len;
+             memcpy_len = min(len, (size_t)(BCM47XXSFLASH_WINDOW_SZ - from));
+             memcpy_fromio(buf, b47s->window + from, memcpy_len);
+             from += memcpy_len;
+             len -= memcpy_len;

Am I crazy, or should you either be doing 'buf += memcpy_len'? Otherwise, you'll be copying over the first few elements of 'buf' below, instead of finishing off the tail end.

Excellent catch, thank you!

Signature · 1 line
Rafał
#6 lore raw

PATCH v4 1/1 mtd: bcm47xxsflash: support reading flash out of mapping window

In reply to Rafał Miłecki

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for reads crossing boundary
V4: Add missing buf increment (thanks Brian!)

---
 drivers/mtd/devices/bcm47xxsflash.c | 24 +++++++++++++++++++++---
 drivers/mtd/devices/bcm47xxsflash.h |  3 +++
 2 files changed, 24 insertions(+), 3 deletions(-)
drivers/mtd/devices/bcm47xxsflash.c +21 -3
index 4decd8c0360a..e2bd81817df4 100644
@@ -105,15 +105,33 @@ static int bcm47xxsflash_read(struct mtd_info *mtd, loff_t from, size_t len,
 			      size_t *retlen, u_char *buf)
 {
 	struct bcm47xxsflash *b47s = mtd->priv;
+	size_t orig_len = len;
 
 	/* Check address range */
 	if ((from + len) > mtd->size)
 		return -EINVAL;
 
-	memcpy_fromio(buf, b47s->window + from, len);
-	*retlen = len;
+	/* Read as much as possible using fast MMIO window */
+	if (from < BCM47XXSFLASH_WINDOW_SZ) {
+		size_t memcpy_len;
 
-	return len;
+		memcpy_len = min(len, (size_t)(BCM47XXSFLASH_WINDOW_SZ - from));
+		memcpy_fromio(buf, b47s->window + from, memcpy_len);
+		from += memcpy_len;
+		len -= memcpy_len;
+		buf += memcpy_len;
+	}
+
+	/* Use indirect access for content out of the window */
+	for (; len; len--) {
+		b47s->cc_write(b47s, BCMA_CC_FLASHADDR, from++);
+		bcm47xxsflash_cmd(b47s, OPCODE_ST_READ4B);
+		*buf++ = b47s->cc_read(b47s, BCMA_CC_FLASHDATA);
+	}
+
+	*retlen = orig_len;
+
+	return orig_len;
 }
 
 static int bcm47xxsflash_write_st(struct mtd_info *mtd, u32 offset, size_t len,
drivers/mtd/devices/bcm47xxsflash.h +3 -0
index 1564b62b412e..b2d7b38f75fd 100644
@@ -3,6 +3,8 @@
 
 #include <linux/mtd/mtd.h>
 
+#define BCM47XXSFLASH_WINDOW_SZ			SZ_16M
+
 /* Used for ST flashes only. */
 #define OPCODE_ST_WREN		0x0006		/* Write Enable */
 #define OPCODE_ST_WRDIS		0x0004		/* Write Disable */
@@ -16,6 +18,7 @@
 #define OPCODE_ST_RES		0x03ab		/* Read Electronic Signature */
 #define OPCODE_ST_CSA		0x1000		/* Keep chip select asserted */
 #define OPCODE_ST_SSE		0x0220		/* Sub-sector Erase */
+#define OPCODE_ST_READ4B	0x6313		/* Read Data Bytes in 4Byte addressing mode */
 
 /* Used for Atmel flashes only. */
 #define OPCODE_AT_READ				0x07e8
Signature · 1 line
2.11.0
#7 lore raw
Quoted 8 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

Quoted 8 lines

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for reads crossing boundary
V4: Add missing buf increment (thanks Brian!)

Presumably you tested for this case now? Seems like quick turnaround :)

#8 lore raw
Quoted 18 lines · Brian Norris
Quoted 8 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

For reading flash content we use MMIO but it's possible to read only first 16 MiB this way. It's simply an arch design/limitation. To support flash sizes bigger than 16 MiB implement indirect access using ChipCommon registers.
This has been tested using MX25L25635F.

Quoted 8 lines

---
V2: Simplify line writing to buf
Add some trivial comment for OPCODE_ST_READ4B
Both requested by Marek
V3: Use as much as possible MMIO access for reads crossing boundary
V4: Add missing buf increment (thanks Brian!)

Quoted 1 line

Presumably you tested for this case now? Seems like quick turnaround :)

Anyway, LGTM. Applied to l2-mtd.git.