From patchwork Thu Jul 9 11:17:06 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagan Teki X-Patchwork-Id: 241120 List-Id: U-Boot discussion From: jagan at amarulasolutions.com (Jagan Teki) Date: Thu, 9 Jul 2020 16:47:06 +0530 Subject: [PATCH 2/5] mtd: Add dm-mtd core ops In-Reply-To: <20200709111709.68904-1-jagan@amarulasolutions.com> References: <20200709111709.68904-1-jagan@amarulasolutions.com> Message-ID: <20200709111709.68904-3-jagan@amarulasolutions.com> - 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 Cc: Vignesh R Signed-off-by: Jagan Teki --- drivers/mtd/mtd-uclass.c | 38 +++++++++++++++++++++++++++++++++++++ include/mtd.h | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) 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 * Copyright (C) 2015 Thomas Chou */ @@ -8,6 +10,42 @@ #include #include #include +#include + +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 +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);