From patchwork Tue Jul 18 16:06:52 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= X-Patchwork-Id: 1809653 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=K2Tw5oJ5; dkim=fail reason="signature verification failed" (2048-bit key; secure) header.d=infradead.org header.i=@infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=KvRluwX2; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20221208 header.b=GT99Yn5X; dkim-atps=neutral Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4R5Qgj5HHLz20Cs for ; Wed, 19 Jul 2023 16:21:21 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=9vhNeuDZmKwzuoSTCDcEChc2JqT626fG9S0Bum38NFk=; b=K2Tw5oJ5vcyTnn E7DaZlcV0q9G9xPKCewqgEFhazIA/3JlCUZHWf17o5XsU94BcJJtBapMJ537JN2X1QLpwPx6O4lk3 KcVrcFkV0igp3pHpyViqwQ+Cyc1FmlyMTsFmPjjjjvqO03f0p7pV4kCWIiuO6AjdUU8CaKTbr873Y 0dxCz1YiNtCtUtusppNcbNdMJVL5jCRqgs4VuRy0eVjdaMekhEiadIoXTN1EO5Y5sW4gxy3BR9So7 eWHO/9ykVVRGrvQCE1BNrO/7atM/0oYDm2VNNEZb5Yk6eLPYRs7UngBdriaiUhZ+150VlYzulr6ez dlKjweD0QIfmhsQpr90w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qM0YP-005fxm-01; Wed, 19 Jul 2023 06:20:53 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qM0YO-005fxA-0I for linux-mtd@bombadil.infradead.org; Wed, 19 Jul 2023 06:20:52 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Transfer-Encoding:Content-Type :MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:Content-ID:Content-Description; bh=PYO3tGH2MBohSxIGrRwDWvJzs2Un0ySfOiS4DZ0h6R8=; b=KvRluwX2AgDlAmYU94RJUEwt2M jU5MgvthOk7pksxjQQAmOWky89c+8Ep+L+VyhT5UMc0GCgoyvqk+DYGHIg5Dd6fSP7wUDeNCq4DzT OtvcAZFD3KYisXTpFNFPXgTpdl5VAdeN4yh9dJ4AxqJ9DWlXEUOI80rMorXU1qRfSKtshsqm5uiW9 l2tiUmsT19343UN19IY/H5cCwbAmJm0q+NRbrfTsy7AVSG6bCoRqUSl/4hg0Je24duGp4e/bAvyj3 klt8djPemIJOcmJm9FSuW1W5aA5xT1JDkYeWBuv1InAf98hvh7wh4sBfa8S4k2c0LZZhyt86C55Hy xxTU3t1Q==; Received: from mail-lf1-x12f.google.com ([2a00:1450:4864:20::12f]) by desiato.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qLnFG-00BkNZ-1v for linux-mtd@lists.infradead.org; Tue, 18 Jul 2023 16:08:16 +0000 Received: by mail-lf1-x12f.google.com with SMTP id 2adb3069b0e04-4fba1288bbdso9595151e87.1 for ; Tue, 18 Jul 2023 09:08:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1689696425; x=1692288425; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=PYO3tGH2MBohSxIGrRwDWvJzs2Un0ySfOiS4DZ0h6R8=; b=GT99Yn5XNGnVERhtU/ltZKp8nqyrPcRtnYkCGPugt5FhzQoaa/0GvD0HqIXaM6JOFF IgRU0uYYHMoYzyeaI89rmAciAXqlpRzEYVfQXhtqyDKvXFs1vhZUxT+7BHs1Paa1GSzJ zEOFtgxvUPTWPetEU8L11PxGwIlTniwuNrCHEdKGU2U4kfvWj5q8AIkqRaUu6mqVWrmS rwGZYRmyp7hdXKaoT6NrlyhZOWFOcUMaTYTpjYSW9BIx6izB6Bi/TSYAk92hdEB7kS8K DF4E8CKO23kQ3RBN9hVGRi8+/ZGM4D0kbgQCLGDOOuF8+fpoYG3bGn5NCjyG8oADPKAt rpPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689696425; x=1692288425; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PYO3tGH2MBohSxIGrRwDWvJzs2Un0ySfOiS4DZ0h6R8=; b=VImVGjVk1bDDVhe9tO6GpV7M8vP7HAH6ZRC2im9/ZQb3R/moeLpIDj5/yJ/LDxODe5 JE5iXIVuSmORgcd56pw6N1aqZCtZbiLKTKemIE9xl/FHYySIY6AienQdMabN3E69p47b i/EiMfaXkMT5vJgBCXVyQ3P4bAkbR4reLOODGfduFD04bGrzOSYM613+KYR5uEza2wq9 W+Kfwhsb0YeKw7UD5+sK8CpVlSslHB82RO2qEcn8pZu5yyQmNgpomA/wKhqEZy7/l5DV 6UgVJXkIZkuiksKa3hZk65ceZdtPzMHtC7cU9oYH/9epBBXNUOBd9Fy6/2hP+H1ebRGr Jv8A== X-Gm-Message-State: ABy/qLavkPwzkhWrVOGEU97Irr7vdaq0LrUgngiPzuz+TfqFsFL7wO+N eAXSVar24iLkZz5qe+d1yBE= X-Google-Smtp-Source: APBJJlGxOuFZuyQgOiPahWuMwqDffd4PQl0ksLJAlOHbs1Csjgy4iJRO8XlMcxTIbZgA5RYj3AfVdw== X-Received: by 2002:a05:6512:473:b0:4fb:cb10:64d0 with SMTP id x19-20020a056512047300b004fbcb1064d0mr15639048lfd.29.1689696425315; Tue, 18 Jul 2023 09:07:05 -0700 (PDT) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id t14-20020ac24c0e000000b004f86aef886asm502295lfq.54.2023.07.18.09.07.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Jul 2023 09:07:05 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Rob Herring , Krzysztof Kozlowski , Conor Dooley Cc: Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Michael Walle , Christian Marangi , devicetree@vger.kernel.org, linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH WIP 3/3] nvmem: u-boot-env: convert to NVMEM layout Date: Tue, 18 Jul 2023 18:06:52 +0200 Message-Id: <20230718160652.26756-3-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230718160652.26756-1-zajec5@gmail.com> References: <20230718160652.26756-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230718_170814_764079_2C1CDB0D X-CRM114-Status: GOOD ( 28.38 ) X-Spam-Score: 0.1 (/) X-Spam-Report: =?unknown-8bit?q?Spam_detection_software=2C_running_on_the_sy?= =?unknown-8bit?q?stem_=22desiato=2Einfradead=2Eorg=22=2C?= =?unknown-8bit?q?_has_NOT_identified_this_incoming_email_as_spam=2E__The_ori?= =?unknown-8bit?q?ginal?= =?unknown-8bit?q?_message_has_been_attached_to_this_so_you_can_view_it_or_la?= =?unknown-8bit?q?bel?= =?unknown-8bit?q?_similar_future_email=2E__If_you_have_any_questions=2C_see?= =?unknown-8bit?q?_the_administrator_of_that_system_for_details=2E?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_preview=3A__From=3A_Rafa=C5=82_Mi=C5=82ecki_This_a?= =?unknown-8bit?q?voids_code_duplication_=28NVMEM?= =?unknown-8bit?q?_device_access=29_and_allows_reusing_this_on_top_of_other_N?= =?unknown-8bit?q?VMEM_devices=2E_Signed-off-by=3A?= =?unknown-8bit?q?_Rafa=C5=82_Mi=C5=82ecki_---_drivers/mtd/mtdcore=2Ec_=7C_7_?= =?unknown-8bit?q?++_drivers/nvmem/Kconfig_=7C?= =?unknown-8bit?q?_15_++-_drivers/nvmem/Makefile_=7C_2_-_drivers/nvmem/layout?= =?unknown-8bit?q?s/Kconfig_=7C_10_++?= =?unknown-8bit?q?_drivers/nvmem/layouts/Makefile_=7C_1_+_=5B=2E=2E=2E=5D_?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_Content_analysis_details=3A___=280=2E1_points=2C_5=2E0_req?= =?unknown-8bit?q?uired=29?= =?unknown-8bit?q?_?= =?unknown-8bit?q?_pts_rule_name______________description?= =?unknown-8bit?q?_----_----------------------_------------------------------?= =?unknown-8bit?q?--------------------?= =?unknown-8bit?q?_0=2E2_FREEMAIL=5FENVFROM=5FEND=5FDIGIT_Envelope-from_freem?= =?unknown-8bit?q?ail_username_ends?= =?unknown-8bit?q?_in_digit?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_0=2E0_SPF=5FHELO=5FNONE__________SPF=3A_HELO_does_not_publ?= =?unknown-8bit?q?ish_an_SPF_Record?= =?unknown-8bit?q?_-0=2E0_RCVD=5FIN=5FDNSWL=5FNONE_____RBL=3A_Sender_listed_a?= =?unknown-8bit?q?t_https=3A//www=2Ednswl=2Eorg/=2C?= =?unknown-8bit?q?_no_trust?= =?unknown-8bit?b?IFsyYTAwOjE0NTA6NDg2NDoyMDowOjA6MDoxMmYgbGlzdGVkIGluXQ==?= =?unknown-8bit?b?IFtsaXN0LmRuc3dsLm9yZ10=?= =?unknown-8bit?q?_-0=2E0_SPF=5FPASS_______________SPF=3A_sender_matches_SPF_?= =?unknown-8bit?q?record?= =?unknown-8bit?q?_0=2E0_FREEMAIL=5FFROM__________Sender_email_is_commonly_ab?= =?unknown-8bit?q?used_enduser_mail?= =?unknown-8bit?q?_provider?= =?unknown-8bit?q?_=5Bzajec5=5Bat=5Dgmail=2Ecom=5D?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FEF__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_envelope-from_domain?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID=5FAU__________Message_has_a_valid_DKIM?= =?unknown-8bit?q?_or_DK_signature_from?= =?unknown-8bit?q?_author=27s_domain?= =?unknown-8bit?q?_0=2E1_DKIM=5FSIGNED____________Message_has_a_DKIM_or_DK_si?= =?unknown-8bit?q?gnature=2C_not_necessarily?= =?unknown-8bit?q?_valid?= =?unknown-8bit?q?_-0=2E1_DKIM=5FVALID_____________Message_has_at_least_one_v?= =?unknown-8bit?q?alid_DKIM_or_DK_signature?= X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org From: Rafał Miłecki This avoids code duplication (NVMEM device access) and allows reusing this on top of other NVMEM devices. Signed-off-by: Rafał Miłecki --- drivers/mtd/mtdcore.c | 7 ++ drivers/nvmem/Kconfig | 15 ++- drivers/nvmem/Makefile | 2 - drivers/nvmem/layouts/Kconfig | 10 ++ drivers/nvmem/layouts/Makefile | 1 + drivers/nvmem/{ => layouts}/u-boot-env.c | 112 +++++++++-------------- 6 files changed, 68 insertions(+), 79 deletions(-) rename drivers/nvmem/{ => layouts}/u-boot-env.c (67%) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 3d781ffb8c32..5f96affdfb0f 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -556,6 +556,13 @@ static int mtd_nvmem_add(struct mtd_info *mtd) config.ignore_wp = true; config.priv = mtd; + if (IS_ENABLED(NVMEM_U_BOOT_ENV) && + (of_device_is_compatible(node, "u-boot,env") || + of_device_is_compatible(node, "brcm,env"))) { + /* FIXME */ + pr_warn("Missing support for deprecated U-Boot env binding\n"); + } + mtd->nvmem = nvmem_register(&config); if (IS_ERR(mtd->nvmem)) { /* Just ignore if there is no NVMEM support in the kernel */ diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index da9befa3d6c4..ff99c284a582 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -347,18 +347,15 @@ config NVMEM_SUNXI_SID will be called nvmem_sunxi_sid. config NVMEM_U_BOOT_ENV - tristate "U-Boot environment variables support" + bool "U-Boot environment variables support" depends on OF && MTD - select CRC32 - select GENERIC_NET_UTILS + select NVMEM_LAYOUT_U_BOOT_ENV help - U-Boot stores its setup as environment variables. This driver adds - support for verifying & exporting such data. It also exposes variables - as NVMEM cells so they can be referenced by other drivers. - - Currently this drivers works only with env variables on top of MTD. + This option enables support for the old device tree binding describing + U-Boot environment variables block on top of MTD partition. - If compiled as module it will be called nvmem_u-boot-env. + Those bindings have been deprecated in favor of using a proper NVMEM + layout. config NVMEM_UNIPHIER_EFUSE tristate "UniPhier SoCs eFuse support" diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index cc23ce4ffb1f..2558752fd9a3 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -69,8 +69,6 @@ obj-$(CONFIG_NVMEM_SUNPLUS_OCOTP) += nvmem_sunplus_ocotp.o nvmem_sunplus_ocotp-y := sunplus-ocotp.o obj-$(CONFIG_NVMEM_SUNXI_SID) += nvmem_sunxi_sid.o nvmem_sunxi_sid-y := sunxi_sid.o -obj-$(CONFIG_NVMEM_U_BOOT_ENV) += nvmem_u-boot-env.o -nvmem_u-boot-env-y := u-boot-env.o obj-$(CONFIG_NVMEM_UNIPHIER_EFUSE) += nvmem-uniphier-efuse.o nvmem-uniphier-efuse-y := uniphier-efuse.o obj-$(CONFIG_NVMEM_VF610_OCOTP) += nvmem-vf610-ocotp.o diff --git a/drivers/nvmem/layouts/Kconfig b/drivers/nvmem/layouts/Kconfig index 7ff1ee1c1f05..f33999be787e 100644 --- a/drivers/nvmem/layouts/Kconfig +++ b/drivers/nvmem/layouts/Kconfig @@ -20,4 +20,14 @@ config NVMEM_LAYOUT_ONIE_TLV If unsure, say N. +config NVMEM_LAYOUT_U_BOOT_ENV + tristate "U-Boot environment variables layout support" + depends on OF + select CRC32 + select GENERIC_NET_UTILS + help + U-Boot stores its setup as environment variables. This driver adds + support for verifying & exporting such data. It also exposes variables + as NVMEM cells so they can be referenced by other drivers. + endmenu diff --git a/drivers/nvmem/layouts/Makefile b/drivers/nvmem/layouts/Makefile index 2974bd7d33ed..4940c9db0665 100644 --- a/drivers/nvmem/layouts/Makefile +++ b/drivers/nvmem/layouts/Makefile @@ -5,3 +5,4 @@ obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o obj-$(CONFIG_NVMEM_LAYOUT_ONIE_TLV) += onie-tlv.o +obj-$(CONFIG_NVMEM_LAYOUT_U_BOOT_ENV) += u-boot-env.o diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/layouts/u-boot-env.c similarity index 67% rename from drivers/nvmem/u-boot-env.c rename to drivers/nvmem/layouts/u-boot-env.c index ee9fd9989b6e..32eae6df8d96 100644 --- a/drivers/nvmem/u-boot-env.c +++ b/drivers/nvmem/layouts/u-boot-env.c @@ -8,11 +8,9 @@ #include #include #include -#include #include #include #include -#include #include enum u_boot_env_format { @@ -25,7 +23,7 @@ struct u_boot_env { struct device *dev; enum u_boot_env_format format; - struct mtd_info *mtd; + struct nvmem_device *nvmem; /* Cells */ struct nvmem_cell_info *cells; @@ -50,28 +48,6 @@ struct u_boot_env_image_broadcom { uint8_t data[0]; } __packed; -static int u_boot_env_read(void *context, unsigned int offset, void *val, - size_t bytes) -{ - struct u_boot_env *priv = context; - struct device *dev = priv->dev; - size_t bytes_read; - int err; - - err = mtd_read(priv->mtd, offset, bytes, &bytes_read, val); - if (err && !mtd_is_bitflip(err)) { - dev_err(dev, "Failed to read from mtd: %d\n", err); - return err; - } - - if (bytes_read != bytes) { - dev_err(dev, "Failed to read %zu bytes\n", bytes); - return -EIO; - } - - return 0; -} - static int u_boot_env_read_post_process_ethaddr(void *context, const char *id, int index, unsigned int offset, void *buf, size_t bytes) { @@ -95,17 +71,25 @@ static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf, size_t data_offset, size_t data_len) { struct device *dev = priv->dev; + struct device_node *layout_np; char *data = buf + data_offset; char *var, *value, *eq; + int err = 0; int idx; + layout_np = of_nvmem_layout_get_container(priv->nvmem); + if (!layout_np) + return -ENOENT; + priv->ncells = 0; for (var = data; var < data + data_len && *var; var += strlen(var) + 1) priv->ncells++; priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL); - if (!priv->cells) - return -ENOMEM; + if (!priv->cells) { + err = -ENOMEM; + goto err_of_node_put; + } for (var = data, idx = 0; var < data + data_len && *var; @@ -117,11 +101,13 @@ static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf, value = eq + 1; priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL); - if (!priv->cells[idx].name) - return -ENOMEM; + if (!priv->cells[idx].name) { + err = -ENOMEM; + goto err_of_node_put; + } priv->cells[idx].offset = data_offset + value - data; priv->cells[idx].bytes = strlen(value); - priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name); + priv->cells[idx].np = of_get_child_by_name(layout_np, priv->cells[idx].name); if (!strcmp(var, "ethaddr")) { priv->cells[idx].raw_len = strlen(value); priv->cells[idx].bytes = ETH_ALEN; @@ -132,7 +118,10 @@ static int u_boot_env_add_cells(struct u_boot_env *priv, uint8_t *buf, if (WARN_ON(idx != priv->ncells)) priv->ncells = idx; - return 0; +err_of_node_put: + of_node_put(layout_np); + + return err; } static int u_boot_env_parse(struct u_boot_env *priv) @@ -143,21 +132,25 @@ static int u_boot_env_parse(struct u_boot_env *priv) size_t crc32_offset; size_t data_offset; size_t data_len; + size_t env_size; uint32_t crc32; uint32_t calc; - size_t bytes; + ssize_t bytes; uint8_t *buf; int err; - buf = kcalloc(1, priv->mtd->size, GFP_KERNEL); + env_size = nvmem_device_size(priv->nvmem); + + buf = kcalloc(1, env_size, GFP_KERNEL); if (!buf) { err = -ENOMEM; goto err_out; } - err = mtd_read(priv->mtd, 0, priv->mtd->size, &bytes, buf); - if ((err && !mtd_is_bitflip(err)) || bytes != priv->mtd->size) { - dev_err(dev, "Failed to read from mtd: %d\n", err); + bytes = nvmem_device_read(priv->nvmem, 0, env_size, buf); + if (bytes < 0 || bytes != env_size) { + dev_err(dev, "Failed to read from NVMEM device: %zd\n", bytes); + err = -EIO; goto err_kfree; } @@ -179,8 +172,8 @@ static int u_boot_env_parse(struct u_boot_env *priv) break; } crc32 = le32_to_cpu(*(__le32 *)(buf + crc32_offset)); - crc32_data_len = priv->mtd->size - crc32_data_offset; - data_len = priv->mtd->size - data_offset; + crc32_data_len = env_size - crc32_data_offset; + data_len = env_size - data_offset; calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L; if (calc != crc32) { @@ -189,7 +182,7 @@ static int u_boot_env_parse(struct u_boot_env *priv) goto err_kfree; } - buf[priv->mtd->size - 1] = '\0'; + buf[env_size - 1] = '\0'; err = u_boot_env_add_cells(priv, buf, data_offset, data_len); if (err) dev_err(dev, "Failed to add cells: %d\n", err); @@ -200,14 +193,9 @@ static int u_boot_env_parse(struct u_boot_env *priv) return err; } -static int u_boot_env_probe(struct platform_device *pdev) +static int u_boot_env_layout_add_cells(struct device *dev, struct nvmem_device *nvmem, + struct nvmem_layout *layout) { - struct nvmem_config config = { - .name = "u-boot-env", - .reg_read = u_boot_env_read, - }; - struct device *dev = &pdev->dev; - struct device_node *np = dev->of_node; struct u_boot_env *priv; int err; @@ -216,28 +204,18 @@ static int u_boot_env_probe(struct platform_device *pdev) return -ENOMEM; priv->dev = dev; - priv->format = (uintptr_t)of_device_get_match_data(dev); + priv->format = (uintptr_t)nvmem_layout_get_match_data(nvmem, layout); - priv->mtd = of_get_mtd_device_by_node(np); - if (IS_ERR(priv->mtd)) { - dev_err_probe(dev, PTR_ERR(priv->mtd), "Failed to get %pOF MTD\n", np); - return PTR_ERR(priv->mtd); - } + priv->nvmem = nvmem; err = u_boot_env_parse(priv); if (err) return err; - config.dev = dev; - config.cells = priv->cells; - config.ncells = priv->ncells; - config.priv = priv; - config.size = priv->mtd->size; - - return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); + return 0; } -static const struct of_device_id u_boot_env_of_match_table[] = { +static const struct of_device_id u_boot_env_layout_of_match_table[] = { { .compatible = "u-boot,env", .data = (void *)U_BOOT_FORMAT_SINGLE, }, { .compatible = "u-boot,env-redundant-bool", .data = (void *)U_BOOT_FORMAT_REDUNDANT, }, { .compatible = "u-boot,env-redundant-count", .data = (void *)U_BOOT_FORMAT_REDUNDANT, }, @@ -245,15 +223,13 @@ static const struct of_device_id u_boot_env_of_match_table[] = { {}, }; -static struct platform_driver u_boot_env_driver = { - .probe = u_boot_env_probe, - .driver = { - .name = "u_boot_env", - .of_match_table = u_boot_env_of_match_table, - }, +static struct nvmem_layout u_boot_env_layout = { + .name = "u_boot_env", + .of_match_table = u_boot_env_layout_of_match_table, + .add_cells = u_boot_env_layout_add_cells, }; -module_platform_driver(u_boot_env_driver); +module_nvmem_layout_driver(u_boot_env_layout); MODULE_AUTHOR("Rafał Miłecki"); MODULE_LICENSE("GPL"); -MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table); +MODULE_DEVICE_TABLE(of, u_boot_env_layout_of_match_table);