[2/5] mtd: Add dm-mtd core ops

Message ID 20200709111709.68904-3-jagan@amarulasolutions.com
State New
Headers show
Series
  • mtd: Implement MTD UCLASS (use SPINOR)
Related show

Commit Message

Jagan Teki July 9, 2020, 11:17 a.m. UTC
- Add generic mtd operations for UCLASS_MTD
- Add mtd_dread|derase|dwrite

The respetive MTD_UCLASS drivers must install the hooks to these
mtd_ops and other core ops are act as a interface b/w drivers
vs command code.

Cc: Simon Glass <sjg@chromium.org>
Cc: Vignesh R <vigneshr@ti.com>
Signed-off-by: Jagan Teki <jagan@amarulasolutions.com>
---
 drivers/mtd/mtd-uclass.c | 38 +++++++++++++++++++++++++++++++++++++
 include/mtd.h            | 41 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 79 insertions(+)

Patch

diff --git a/drivers/mtd/mtd-uclass.c b/drivers/mtd/mtd-uclass.c
index 5418217431..bb00336a60 100644
--- a/drivers/mtd/mtd-uclass.c
+++ b/drivers/mtd/mtd-uclass.c
@@ -1,5 +1,7 @@ 
 // SPDX-License-Identifier: GPL-2.0+
 /*
+ * Copyright (c) 2020 Amarula Solutions(India)
+ * Copyright (c) 2020 Jagan Teki <jagan@amarulasolutons.com>
  * Copyright (C) 2015 Thomas Chou <thomas@wytron.com.tw>
  */
 
@@ -8,6 +10,42 @@ 
 #include <dm/device-internal.h>
 #include <errno.h>
 #include <mtd.h>
+#include <linux/log2.h>
+
+int mtd_dread(struct udevice *dev, loff_t from, size_t len, u_char *buf)
+{
+	const struct mtd_ops *ops = mtd_get_ops(dev);
+
+	if (!ops->read)
+		return -EOPNOTSUPP;
+
+	return ops->read(dev, from, len, buf);
+}
+
+int mtd_derase(struct udevice *dev, loff_t off, size_t len)
+{
+	const struct mtd_ops *ops = mtd_get_ops(dev);
+	struct erase_info instr;
+
+	if (!ops->erase)
+		return -EOPNOTSUPP;
+
+	memset(&instr, 0, sizeof(instr));
+	instr.addr = off;
+	instr.len = len;
+
+	return ops->erase(dev, &instr);
+}
+
+int mtd_dwrite(struct udevice *dev, loff_t to, size_t len, const u_char *buf)
+{
+	const struct mtd_ops *ops = mtd_get_ops(dev);
+
+	if (!ops->write)
+		return -EOPNOTSUPP;
+
+	return ops->write(dev, to, len, buf);
+}
 
 /**
  * mtd_probe - Probe the device @dev if not already done
diff --git a/include/mtd.h b/include/mtd.h
index b0f8693386..b94afe8945 100644
--- a/include/mtd.h
+++ b/include/mtd.h
@@ -8,6 +8,47 @@ 
 
 #include <linux/mtd/mtd.h>
 
+struct mtd_ops {
+	int (*erase)(struct udevice *dev, struct erase_info *instr);
+	int (*read)(struct udevice *dev, loff_t from, size_t len,
+		    u_char *buf);
+	int (*write)(struct udevice *dev, loff_t to, size_t len,
+		     const u_char *buf);
+};
+
+#define mtd_get_ops(dev) ((struct mtd_ops *)(dev)->driver->ops)
+
+/**
+ * mtd_dread() - Read data from mtd device
+ *
+ * @dev:	mtd udevice
+ * @from:	Offset into device in bytes to read from
+ * @len:	Length of bytes to read
+ * @buf:	Buffer to put the data that is read
+ * @return 0 if OK, -ve on error
+ */
+int mtd_dread(struct udevice *dev, loff_t from, size_t len, u_char *buf);
+
+/**
+ * mtd_dwrite() - Write data to mtd device
+ *
+ * @dev:	mtd udevice
+ * @to:		Offset into device in bytes to write to
+ * @len:	Length of bytes to write
+ * @buf:	Buffer containing bytes to write
+ * @return 0 if OK, -ve on error
+ */
+int mtd_dwrite(struct udevice *dev, loff_t to, size_t len, const u_char *buf);
+
+/**
+ * mtd_derase() - Erase blocks of the mtd device
+ *
+ * @dev:	mtd udevice
+ * @instr:	Erase info details of mtd device
+ * @return 0 if OK, -ve on error
+ */
+int mtd_derase(struct udevice *dev, loff_t off, size_t len);
+
 int mtd_probe(struct udevice *dev);
 int mtd_probe_devices(void);