From patchwork Wed Oct 5 16:57:47 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jagan Teki X-Patchwork-Id: 678533 X-Patchwork-Delegate: jagannadh.teki@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from theia.denx.de (theia.denx.de [85.214.87.163]) by ozlabs.org (Postfix) with ESMTP id 3sq26x6h69z9s3s for ; Thu, 6 Oct 2016 03:58:49 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 1B25DA7700; Wed, 5 Oct 2016 18:58:45 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Y2dsE58-aIB1; Wed, 5 Oct 2016 18:58:44 +0200 (CEST) Received: from theia.denx.de (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 802E1A7704; Wed, 5 Oct 2016 18:58:42 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by theia.denx.de (Postfix) with ESMTP id 91104A76C3 for ; Wed, 5 Oct 2016 18:58:35 +0200 (CEST) Received: from theia.denx.de ([127.0.0.1]) by localhost (theia.denx.de [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Np_2DEnm-CYe for ; Wed, 5 Oct 2016 18:58:35 +0200 (CEST) X-policyd-weight: NOT_IN_SBL_XBL_SPAMHAUS=-1.5 NOT_IN_SPAMCOP=-1.5 NOT_IN_BL_NJABL=-1.5 (only DNSBL check requested) Received: from mail-pf0-f195.google.com (mail-pf0-f195.google.com [209.85.192.195]) by theia.denx.de (Postfix) with ESMTPS id 18860A76B8 for ; Wed, 5 Oct 2016 18:58:34 +0200 (CEST) Received: by mail-pf0-f195.google.com with SMTP id t25so6552719pfg.2 for ; Wed, 05 Oct 2016 09:58:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Oy9c8iD/TkczpAo6/Yu4bL2dqYy9c2ZWN1w4kU9jE4w=; b=ZgsUbA35/8JGaV/w3X/8X0k7r+7121UroIlpYj1pmCepiJVb9nNxB4PAbwlhViOQ3p hOvp/7GbE+SRT+rghxKALaT83Fe+HW5A4rVhOnCNBhlRqzDSPniiUdWB8j9+KFdKUKNT FHUoKup6fOBICn1GtsGJN9xaB+rqqBR1cJ/vvLYMWcmZ7wnR0GI7S0eXl+Recy3yGgn5 g9Mi5tn+rQbvNkwKY5fq69ghFVBERS5tMdoVJ5F0NV9LwIGW8TW10Gtli0I8/ZMRwz2E oQ3p/hoKx1GUtGLds1y0ugodUxHW+plRT2sajKi4itZynnIO0NrNxzS9D9RCo5EAWNBk bicQ== X-Gm-Message-State: AA6/9RmmMBg4RphdGAX/NBSIt4afpfeYeVTkOvtjEIdWp57W2Q68/s8Q4Z0QPPDTOGETrA== X-Received: by 10.98.210.66 with SMTP id c63mr10334527pfg.174.1475686712807; Wed, 05 Oct 2016 09:58:32 -0700 (PDT) Received: from Mr.J ([49.204.230.134]) by smtp.gmail.com with ESMTPSA id l82sm9803278pfk.85.2016.10.05.09.58.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 05 Oct 2016 09:58:31 -0700 (PDT) From: Jagan Teki To: u-boot@lists.denx.de Date: Wed, 5 Oct 2016 22:27:47 +0530 Message-Id: <1475686682-28576-2-git-send-email-jteki@openedev.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1475686682-28576-1-git-send-email-jteki@openedev.com> References: <1475686682-28576-1-git-send-email-jteki@openedev.com> Cc: Jagan Teki , Tom Rini Subject: [U-Boot] [PATCH RFC v8 01/16] dm: mtd: Add dm mtd core ops X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.15 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" - Add generic dm_mtd operations - Add mtd_read|erase|write_dm - Add add_mtd_device_dm The respetive MTD_UCLASS drivers must install the hooks to these dm_mtd_ops and other core ops are act as a interface b/w drivers vs command code. Signed-off-by: Jagan Teki --- drivers/mtd/mtd-uclass.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/mtd.h | 58 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+) diff --git a/drivers/mtd/mtd-uclass.c b/drivers/mtd/mtd-uclass.c index 7b7c48e..b4ffd92 100644 --- a/drivers/mtd/mtd-uclass.c +++ b/drivers/mtd/mtd-uclass.c @@ -1,4 +1,5 @@ /* + * Copyright (C) 2016 Jagan Teki * Copyright (C) 2015 Thomas Chou * * SPDX-License-Identifier: GPL-2.0+ @@ -8,6 +9,77 @@ #include #include #include +#include + +int mtd_read_dm(struct udevice *dev, loff_t from, size_t len, size_t *retlen, + u_char *buf) +{ + struct mtd_info *mtd = mtd_get_info(dev); + + *retlen = 0; + if (from < 0 || from > mtd->size || len > mtd->size - from) + return -EINVAL; + if (!len) + return 0; + + return mtd_get_ops(dev)->_read(dev, from, len, retlen, buf); +} + +int mtd_erase_dm(struct udevice *dev, struct erase_info *instr) +{ + struct mtd_info *mtd = mtd_get_info(dev); + + if (instr->addr > mtd->size || instr->len > mtd->size - instr->addr) + return -EINVAL; + if (!(mtd->flags & MTD_WRITEABLE)) + return -EROFS; + instr->fail_addr = MTD_FAIL_ADDR_UNKNOWN; + if (!instr->len) { + instr->state = MTD_ERASE_DONE; + return 0; + } + + return mtd_get_ops(dev)->_erase(dev, instr); +} + +int mtd_write_dm(struct udevice *dev, loff_t to, size_t len, size_t *retlen, + const u_char *buf) +{ + struct mtd_info *mtd = mtd_get_info(dev); + + *retlen = 0; + if (to < 0 || to > mtd->size || len > mtd->size - to) + return -EINVAL; + if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE)) + return -EROFS; + if (!len) + return 0; + + return mtd_get_ops(dev)->_write(dev, to, len, retlen, buf); +} + +int add_mtd_device_dm(struct udevice *dev) +{ + struct mtd_info *mtd = mtd_get_info(dev); + + BUG_ON(mtd->writesize == 0); + mtd->usecount = 0; + + if (is_power_of_2(mtd->erasesize)) + mtd->erasesize_shift = ffs(mtd->erasesize) - 1; + else + mtd->erasesize_shift = 0; + + if (is_power_of_2(mtd->writesize)) + mtd->writesize_shift = ffs(mtd->writesize) - 1; + else + mtd->writesize_shift = 0; + + mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1; + mtd->writesize_mask = (1 << mtd->writesize_shift) - 1; + + return 0; +} /* * Implement a MTD uclass which should include most flash drivers. diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h index 1fd17c3..1d22de1 100644 --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h @@ -506,4 +506,62 @@ void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset, const uint64_t length, uint64_t *len_incl_bad, int *truncated); #endif + +#ifdef CONFIG_MTD + +struct dm_mtd_ops { + int (*_erase)(struct udevice *dev, struct erase_info *instr); + int (*_read)(struct udevice *dev, loff_t from, size_t len, + size_t *retlen, u_char *buf); + int (*_write)(struct udevice *dev, loff_t to, size_t len, + size_t *retlen, const u_char *buf); +}; + +/* Access the serial operations for a device */ +#define mtd_get_ops(dev) ((struct dm_mtd_ops *)(dev)->driver->ops) + +/** + * mtd_read_dm() - Read data from MTD device + * + * @dev: MTD device + * @from: Offset into device in bytes to read from + * @len: Length of bytes to read + * @retlen: Length of return bytes read to + * @buf: Buffer to put the data that is read + * @return 0 if OK, -ve on error + */ +int mtd_read_dm(struct udevice *dev, loff_t from, size_t len, size_t *retlen, + u_char *buf); + +/** + * mtd_write_dm() - Write data to MTD device + * + * @dev: MTD device + * @to: Offset into device in bytes to write to + * @len: Length of bytes to write + * @retlen: Length of return bytes to write to + * @buf: Buffer containing bytes to write + * @return 0 if OK, -ve on error + */ +int mtd_write_dm(struct udevice *dev, loff_t to, size_t len, size_t *retlen, + const u_char *buf); + +/** + * mtd_erase_dm() - Erase blocks of the MTD device + * + * @dev: MTD device + * @instr: Erase info details of MTD device + * @return 0 if OK, -ve on error + */ +int mtd_erase_dm(struct udevice *dev, struct erase_info *instr); + +/** + * add_mtd_device_dm() - Add MTD device + * + * @dev: MTD device + * @return 0 if OK, -ve on error + */ +int add_mtd_device_dm(struct udevice *dev); + +#endif /* CONFIG_MTD */ #endif /* __MTD_MTD_H__ */