diff mbox

[U-Boot,v5,03/26] onenand_spl_simple: Add a simple OneNAND read function

Message ID 1468348114-11442-4-git-send-email-ladis@linux-mips.org
State Accepted
Commit 735717d18a012a6f34c74dd98cec8f46bac5c51c
Delegated to: Tom Rini
Headers show

Commit Message

Ladislav Michl July 12, 2016, 6:28 p.m. UTC
Signed-off-by: Ladislav Michl <ladis@linux-mips.org>
---

Changes in v5: None
Changes in v4: None
Changes in v3: None
Changes in v2: None

 drivers/mtd/onenand/onenand_spl.c | 48 +++++++++++++++++++++++++++++++++++++++
 include/onenand_uboot.h           |  1 +
 2 files changed, 49 insertions(+)

Comments

Tom Rini July 23, 2016, 12:10 a.m. UTC | #1
On Tue, Jul 12, 2016 at 08:28:11PM +0200, Ladislav Michl wrote:

> Signed-off-by: Ladislav Michl <ladis@linux-mips.org>

Applied to u-boot/master, thanks!
diff mbox

Patch

diff --git a/drivers/mtd/onenand/onenand_spl.c b/drivers/mtd/onenand/onenand_spl.c
index fe6b7d9..1925f41 100644
--- a/drivers/mtd/onenand/onenand_spl.c
+++ b/drivers/mtd/onenand/onenand_spl.c
@@ -93,6 +93,54 @@  static int onenand_spl_read_page(uint32_t block, uint32_t page, uint32_t *buf,
 	return 0;
 }
 
+#ifdef CONFIG_SPL_UBI
+/* Temporary storage for non page aligned and non page sized reads. */
+static u8 scratch_buf[PAGE_4K];
+
+/**
+ * onenand_spl_read_block - Read data from physical eraseblock into a buffer
+ * @block:	Number of the physical eraseblock
+ * @offset:	Data offset from the start of @peb
+ * @len:	Data size to read
+ * @dst:	Address of the destination buffer
+ *
+ * Notes:
+ *	@offset + @len are not allowed to be larger than a physical
+ *	erase block. No sanity check done for simplicity reasons.
+ */
+int onenand_spl_read_block(int block, int offset, int len, void *dst)
+{
+	int page, read, psize;
+
+	psize = onenand_spl_get_geometry();
+	/* Calculate the page number */
+	page = offset / psize;
+	/* Offset to the start of a flash page */
+	offset = offset % psize;
+
+	while (len) {
+		/*
+		 * Non page aligned reads go to the scratch buffer.
+		 * Page aligned reads go directly to the destination.
+		 */
+		if (offset || len < psize) {
+			onenand_spl_read_page(block, page,
+			                      (uint32_t *)scratch_buf, psize);
+			read = min(len, psize - offset);
+			memcpy(dst, scratch_buf + offset, read);
+			offset = 0;
+		} else {
+			onenand_spl_read_page(block, page, dst, psize);
+			read = psize;
+		}
+		page++;
+		len -= read;
+		dst += read;
+	}
+	return 0;
+}
+#endif
+
 void onenand_spl_load_image(uint32_t offs, uint32_t size, void *dst)
 {
 	uint32_t *addr = (uint32_t *)dst;
diff --git a/include/onenand_uboot.h b/include/onenand_uboot.h
index fd01040..d69e0d2 100644
--- a/include/onenand_uboot.h
+++ b/include/onenand_uboot.h
@@ -49,6 +49,7 @@  extern int flexonenand_set_boundary(struct mtd_info *mtd, int die,
 					int boundary, int lock);
 
 /* SPL */
+int onenand_spl_read_block(int block, int offset, int len, void *dst);
 void onenand_spl_load_image(uint32_t offs, uint32_t size, void *dst);
 
 #endif /* __UBOOT_ONENAND_H */