Received: by 2002:ac0:a5a7:0:0:0:0:0 with SMTP id m36-v6csp971542imm; Wed, 8 Aug 2018 08:39:26 -0700 (PDT) X-Google-Smtp-Source: AA+uWPzXwFG/FaFSHmlTKTD2Oz+lT0Kzo+PvwMWl8onxRLOgsZKFBVBUzGg9JBui5OyxLBUNhfH4 X-Received: by 2002:a17:902:b81:: with SMTP id 1-v6mr3139949plr.164.1533742766512; Wed, 08 Aug 2018 08:39:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1533742766; cv=none; d=google.com; s=arc-20160816; b=yPyI0rJgrprI3gm1/+aVBtTI1Tb7TED1yJVrR9x/ATrb+BKy3Mu8s9cPNuvzV88QoH rtg7e+hk3nx6T2YDLhJ8yI9uw774kzlq1txY9KJjS2zAFxrd5zAWqoTw9VjEMRRjuGA/ glsVAQNMyn9oqx/Vmm56C0E0+UDeN0RaHZ7vqIljJNCmJiuSkeeLa3C8zaJMhdp8MPEb JC49yZe0SpS2OYc/qXzzJ7DpFA9jvPpUerlLcKqWXH2Pm8EoGMudKtUv9h6FI6gUsC7K 52vGe9XZqneaJQDHawzFM1jg5PVLMkJUqPsxBSaNMD54OCe+8SQOCPiUHdjdK+19dJ2w cDfA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=yrklqQoVna1AmqNCU3D6TLUmDxCRyEqqCAbn0AY+9ek=; b=r4BIQCRxINVZupm6+Rh9PDCSS0ZVld6y+cIiYeR0Vvd93123sMIpp3JNZp3h/cj3g0 wp5v2lw2Bj2/hsN3Ffsw3x9oLGplPUaBeyaMET5CcQgAt5GNBpGKFb9jN/p3olsb8fn9 RP2ytqq/eMM/5rF4UClo7CeVikkx5ZRd7oN91mCNTDB/Z9hG6Gcx1WICZO5qHPYHa9vV rbeMcPjAjuQo3vbHOmEzK3Do2cyj8adZLrP2x0yFqxtV8pGTFyXGM6EZAmrBJa34GMaE CUIyGxolJ828Wpnd6AJSbTNaAbs0GzE7uP3tmnIewgEXRyxkn/wY7YxuotCppoqLn3HD A7NA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=tGOhqhgB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id v18-v6si4471352pgh.162.2018.08.08.08.39.12; Wed, 08 Aug 2018 08:39:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@bgdev-pl.20150623.gappssmtp.com header.s=20150623 header.b=tGOhqhgB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728149AbeHHRxu (ORCPT + 99 others); Wed, 8 Aug 2018 13:53:50 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:44873 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728036AbeHHRxt (ORCPT ); Wed, 8 Aug 2018 13:53:49 -0400 Received: by mail-wr1-f66.google.com with SMTP id r16-v6so2382633wrt.11 for ; Wed, 08 Aug 2018 08:33:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bgdev-pl.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=yrklqQoVna1AmqNCU3D6TLUmDxCRyEqqCAbn0AY+9ek=; b=tGOhqhgB+VQgpU+wvkZEyKTCEfpDSRa9otYxlA03cNtCasel7cpD3/KdUhww2wm4Pm fjecI5ekdjsXq/qtOgD40K5558+7j9MiGovw1hv3MRtLpIZAn7heVXGUqXD+JfqmsnwQ TXJRpYoLiDY6qtKE7TgSXcyICr489v0zRi6FJ1fUuu3+eticUHpF2b2AOogwbp9BmN/N NGIEklrO1b7ixyrtRAQoTrGihAn1LjnzY3rew+cvC68zGxI58LDEa5CmJ7R3xwj8Risv 7GOXKvnUmzX7bbj5hXhy5QCUv3xKFAEv+/DkgzpyWsFci4GBN+q6Y+8dWoVmQTjRJM3W q00g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=yrklqQoVna1AmqNCU3D6TLUmDxCRyEqqCAbn0AY+9ek=; b=bWwESqLFBbRWy4267tm2sX0Sjbp0j/8NqGHJqSYPDBhueTWTmmDEN5k/vIy5GMrvhe qjIlE2bwdogXCyRWNMI+YaThh/8KIQrjzpBY64nQUSSurqQmb10K9cT4300gqZ2E/VRh dZxx1ygtT8zEdTRD4+Gh8CVa5Zr+CaomxsLHR4Y9OV/yW/A1fK9DL3gCMvTRZyzcIUEc Ox9oKgDRzvjQC3PksJR9wC+4K3cthxcRubaJQy8nDiYNBDNuRSKhx0Eep7hEiD9fOw0I /G0jJTwMlIoxgbqGRSa53YdH6XrphS2L51duRi0ndxb4hrS+pmwVfCwg7213HM7aPYII 7YGQ== X-Gm-Message-State: AOUpUlGQNfgxH9f8G1vlW6RhU+3grceO/mVXgxvDE4dFHH0pVg8BzK4G jUpczVY7Nf3ZgQU+DRa5BJ9WZw== X-Received: by 2002:adf:9a55:: with SMTP id z79-v6mr2083051wrb.153.1533742418005; Wed, 08 Aug 2018 08:33:38 -0700 (PDT) Received: from brgl-bgdev.lan (LFbn-MAR-1-609-89.w90-118.abo.wanadoo.fr. [90.118.185.89]) by smtp.gmail.com with ESMTPSA id s2-v6sm5240915wrw.52.2018.08.08.08.33.36 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 08 Aug 2018 08:33:37 -0700 (PDT) From: Bartosz Golaszewski To: Jonathan Corbet , Sekhar Nori , Kevin Hilman , Russell King , Arnd Bergmann , Greg Kroah-Hartman , David Woodhouse , Brian Norris , Boris Brezillon , Marek Vasut , Richard Weinberger , Grygorii Strashko , "David S . Miller" , Srinivas Kandagatla , Naren , Mauro Carvalho Chehab , Andrew Morton , Lukas Wunner , Dan Carpenter , Florian Fainelli , Ivan Khoronzhuk , Sven Van Asbroeck , Paolo Abeni , Alban Bedel , Rob Herring , David Lechner , Andrew Lunn Cc: linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-i2c@vger.kernel.org, linux-mtd@lists.infradead.org, linux-omap@vger.kernel.org, netdev@vger.kernel.org, Bartosz Golaszewski Subject: [PATCH 06/28] mtd: Add support for reading MTD devices via the nvmem API Date: Wed, 8 Aug 2018 17:31:28 +0200 Message-Id: <20180808153150.23444-7-brgl@bgdev.pl> X-Mailer: git-send-email 2.18.0 In-Reply-To: <20180808153150.23444-1-brgl@bgdev.pl> References: <20180808153150.23444-1-brgl@bgdev.pl> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alban Bedel Allow drivers that use the nvmem API to read data stored on MTD devices. For this the mtd devices are registered as read-only NVMEM providers. On OF systems only devices that have the 'nvmem-provider' property are registered, on non-OF system all MTD devices are registered. Signed-off-by: Alban Bedel [Bartosz: - use the managed variant of nvmem_register(), - set the nvmem name] Signed-off-by: Bartosz Golaszewski --- drivers/mtd/Kconfig | 1 + drivers/mtd/mtdcore.c | 50 +++++++++++++++++++++++++++++++++++++++++ include/linux/mtd/mtd.h | 2 ++ 3 files changed, 53 insertions(+) diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig index 46ab7feec6b6..f5549482d0df 100644 --- a/drivers/mtd/Kconfig +++ b/drivers/mtd/Kconfig @@ -1,5 +1,6 @@ menuconfig MTD tristate "Memory Technology Device (MTD) support" + imply NVMEM help Memory Technology Devices are flash, RAM and similar chips, often used for solid state file systems on embedded devices. This option diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 42395df06be9..a57302eaceb5 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -488,6 +488,49 @@ int mtd_pairing_groups(struct mtd_info *mtd) } EXPORT_SYMBOL_GPL(mtd_pairing_groups); +static int mtd_nvmem_reg_read(void *priv, unsigned int offset, + void *val, size_t bytes) +{ + struct mtd_info *mtd = priv; + size_t retlen; + int err; + + err = mtd_read(mtd, offset, bytes, &retlen, val); + if (err && err != -EUCLEAN) + return err; + + return retlen == bytes ? 0 : -EIO; +} + +static int mtd_nvmem_add(struct mtd_info *mtd) +{ + struct nvmem_config config = { }; + + config.dev = &mtd->dev; + config.owner = THIS_MODULE; + config.name = mtd->name; + config.reg_read = mtd_nvmem_reg_read; + config.size = mtd->size; + config.word_size = 1; + config.stride = 1; + config.read_only = true; + config.root_only = true; + config.priv = mtd; + + mtd->nvmem = devm_nvmem_register(&mtd->dev, &config); + if (IS_ERR(mtd->nvmem)) { + /* Just ignore if there is no NVMEM support in the kernel */ + if (PTR_ERR(mtd->nvmem) == -ENOSYS) { + mtd->nvmem = NULL; + } else { + dev_err(&mtd->dev, "Failed to register NVMEM device\n"); + return PTR_ERR(mtd->nvmem); + } + } + + return 0; +} + static struct dentry *dfs_dir_mtd; /** @@ -570,6 +613,11 @@ int add_mtd_device(struct mtd_info *mtd) if (error) goto fail_added; + /* Add the nvmem provider */ + error = mtd_nvmem_add(mtd); + if (error) + goto fail_nvmem_add; + if (!IS_ERR_OR_NULL(dfs_dir_mtd)) { mtd->dbg.dfs_dir = debugfs_create_dir(dev_name(&mtd->dev), dfs_dir_mtd); if (IS_ERR_OR_NULL(mtd->dbg.dfs_dir)) { @@ -595,6 +643,8 @@ int add_mtd_device(struct mtd_info *mtd) __module_get(THIS_MODULE); return 0; +fail_nvmem_add: + device_unregister(&mtd->dev); fail_added: of_node_put(mtd_get_of_node(mtd)); idr_remove(&mtd_idr, i); diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h index a86c4fa93115..8121c6582285 100644 --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h @@ -25,6 +25,7 @@ #include #include #include +#include #include @@ -339,6 +340,7 @@ struct mtd_info { struct device dev; int usecount; struct mtd_debug_info dbg; + struct nvmem_device *nvmem; }; int mtd_ooblayout_ecc(struct mtd_info *mtd, int section, -- 2.18.0