From patchwork Wed Jun 15 19:42:59 2022 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: 581973 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5806:0:0:0:0 with SMTP id j6csp759962max; Wed, 15 Jun 2022 12:43:19 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tDb4XvFScmdxOiMc3yc98PQVUOxwAdKkWm5DLTche/p8Ar/iJDsw5+ko6oGlrPYOfO72tl X-Received: by 2002:a17:907:629c:b0:6e1:6ad:5dd8 with SMTP id nd28-20020a170907629c00b006e106ad5dd8mr1194728ejc.641.1655322198957; Wed, 15 Jun 2022 12:43:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655322198; cv=none; d=google.com; s=arc-20160816; b=TCq5yB48eqrLFA/KvEruiermNwE2XCscPighLKc00U5wvTXet1SiVGRcM8kD1YI6Ne yDZkDq8HhquEXbrlSR1xN/KIjVEBTkZcxGIluosdEni+8DyToVsjjFrlfPtKd56bcE0x h1wxFTwnWqtc5E6T/W1134JpoZSCNymclg/iYq2Qd19Nmpng7hGes0c9z5BRePl8cxad EVhMTWcz5tN51VpGUeN2Lvqe66/h+CyeTxvzjvDjNljIc6XESQEpt62CvkGIlRL9IZeR IMX4xQ3THsy+wT8bldwgo1Po3Cr+35Q98pyXDkwHP8+zaYplAk49PzMsKlLA0m6XRKH9 F83Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature; bh=NeAaQxHNgpvGGApabF2dZ5cx3n1PATnxM0M/iIuIjaA=; b=wg7xSH/TQGQkjwdoLiGGdw/4GzjANVr6BlFJO0EsJWquXhxYNw+KKc3LEvHHyOR80Q AlZfzjxcTqWAbdEoVy7eRElKeHs6JONm8CKlFeYWDHxy8gr40B3/cdRwiFmhj0CO+b6l ObQ5Jrt7rm7oMR4TLShxexugCzgZ98iLMGF604/5pujJ0gr1CsqoPByVQiUbVmAU6FJK SnZh64xkicL3RPlVCIs088F89AjJIYftjfVTEASRblwv/L/ApTmjotW0vPLwvIlZaiPX T9R9uLKUutl4aFdRlSF6OUArf6otn4AY/eRqHtBEQnGdMWH/mE20RQj02c7zZf0qt+Ui sThQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=k14tMQCV; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id js12-20020a17090797cc00b00718d0985a80si10345422ejc.621.2022.06.15.12.43.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 12:43:18 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=k14tMQCV; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id C3A0C832DA; Wed, 15 Jun 2022 21:43:15 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="k14tMQCV"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0A47D83020; Wed, 15 Jun 2022 21:43:14 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ej1-x62a.google.com (mail-ej1-x62a.google.com [IPv6:2a00:1450:4864:20::62a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id A005883020 for ; Wed, 15 Jun 2022 21:43:11 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=zajec5@gmail.com Received: by mail-ej1-x62a.google.com with SMTP id gl15so25253186ejb.4 for ; Wed, 15 Jun 2022 12:43:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=NeAaQxHNgpvGGApabF2dZ5cx3n1PATnxM0M/iIuIjaA=; b=k14tMQCVOVeOrgEvHimQMUqpKcCa52Ahn9gpZ0rCs6UBPmduwGS/RyEeSxGl32iBsa 6sdJ2TwTf6gszE58GZjCVQJGrDA8gpYriykjwER+3ZPpzBx0LofuIzLo3P27yLoq7fgW 7Rt6xQ+cd9bin8kCAxVf9OAmEkf0xoPvJLzAq1UnhUnZXXHDizAgllKcQfoCJgc78lH8 f21xjPvnjHZJTTHcF5sfaryaMZuoSMBC9rM7poPFB2MPk1aJEBojUV7SB2CYo3Di4L+B +NVJ/3Qlpw2Fql50DHr+uEEqIoYmTRaMWDB+tUmgCak9rIhAlxwXqizC2JzH6fYVEb/l H8aQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=NeAaQxHNgpvGGApabF2dZ5cx3n1PATnxM0M/iIuIjaA=; b=puIgJAld2+Zrh8TxcMjc8Y1r56IvAZnfkIvUhiGDkklkz0i8e8f9daQR7nCPifGWBB DQnJQSdvBKOOwnqMU2gGZ92r7Zs8gbhGloqyu9KRpYO8heDGjepLzzCjjJDbAIqvLYT+ LnKJ8i95qmb/KFruZyWdwJp9saWJbq8B0iOiXu2vEr9SNepLa5vZV9/rsbxe1HWoCvs9 MX9t4Cbhes+m64pzaFocMhR6dXdfkJrgj1J5C0QOzEoC2baJ/fCTF82kgeFUAskDGfrI Dpz12v2pwA4VvcO4TUyC3B2Q1KTJQA5Jnz6RbQb2ejEbhomiRGmFV6SB7yPES+i1fOaa 96Sw== X-Gm-Message-State: AJIora/DjqoWRQ9o67mNyw5vtG9iQqBC+TDhw0XRAMsBBhREDrDpCSq5 VE6w1IRLYBKjTvut+b8C0aM= X-Received: by 2002:a17:906:9244:b0:70c:f626:944d with SMTP id c4-20020a170906924400b0070cf626944dmr1300802ejx.496.1655322191129; Wed, 15 Jun 2022 12:43:11 -0700 (PDT) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id a16-20020aa7d750000000b0042bd6f745fasm18096eds.92.2022.06.15.12.43.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 12:43:10 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra Cc: Tom Rini , Ahmad Fatoum , linux-arm-kernel@lists.infradead.org, u-boot@lists.denx.de, devicetree@vger.kernel.org, linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V4 1/2] mtd: allow getting MTD device associated with a specific DT node Date: Wed, 15 Jun 2022 21:42:59 +0200 Message-Id: <20220615194300.13358-1-zajec5@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean From: Rafał Miłecki MTD subsystem API allows interacting with MTD devices (e.g. reading, writing, handling bad blocks). So far a random driver could get MTD device only by its name (get_mtd_device_nm()). This change allows getting them also by a DT node. This API is required for drivers handling DT defined MTD partitions in a specific way (e.g. U-Boot (sub)partition with environment variables). Signed-off-by: Rafał Miłecki Acked-by: Miquel Raynal Reviewed-by: Ahmad Fatoum --- V3: First introduction of of_get_mtd_device_by_node() V4: Use EPROBE_DEFER Srinivas: in V3 Miquel said it's OK to push this patch through NVMEM --- drivers/mtd/mtdcore.c | 28 ++++++++++++++++++++++++++++ include/linux/mtd/mtd.h | 1 + 2 files changed, 29 insertions(+) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index 9eb0680db312..3613cc142f25 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -1154,6 +1154,34 @@ int __get_mtd_device(struct mtd_info *mtd) } EXPORT_SYMBOL_GPL(__get_mtd_device); +/** + * of_get_mtd_device_by_node - obtain an MTD device associated with a given node + * + * @np: device tree node + */ +struct mtd_info *of_get_mtd_device_by_node(struct device_node *np) +{ + struct mtd_info *mtd = NULL; + struct mtd_info *tmp; + int err; + + mutex_lock(&mtd_table_mutex); + + err = -EPROBE_DEFER; + mtd_for_each_device(tmp) { + if (mtd_get_of_node(tmp) == np) { + mtd = tmp; + err = __get_mtd_device(mtd); + break; + } + } + + mutex_unlock(&mtd_table_mutex); + + return err ? ERR_PTR(err) : mtd; +} +EXPORT_SYMBOL_GPL(of_get_mtd_device_by_node); + /** * get_mtd_device_nm - obtain a validated handle for an MTD device by * device name diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h index 955aee14b0f7..6fc841ceef31 100644 --- a/include/linux/mtd/mtd.h +++ b/include/linux/mtd/mtd.h @@ -677,6 +677,7 @@ extern int mtd_device_unregister(struct mtd_info *master); extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num); extern int __get_mtd_device(struct mtd_info *mtd); extern void __put_mtd_device(struct mtd_info *mtd); +extern struct mtd_info *of_get_mtd_device_by_node(struct device_node *np); extern struct mtd_info *get_mtd_device_nm(const char *name); extern void put_mtd_device(struct mtd_info *mtd); From patchwork Wed Jun 15 19:43:00 2022 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: 581974 Delivered-To: patch@linaro.org Received: by 2002:a05:7000:5806:0:0:0:0 with SMTP id j6csp760081max; Wed, 15 Jun 2022 12:43:28 -0700 (PDT) X-Google-Smtp-Source: AGRyM1spx3c5LKZUVZlhO3K5eMLrFzvX7gJyF+hwnIWn3Gl1xsFUsDDgvyw3YwDsPYgN9PVft3Hw X-Received: by 2002:a17:907:3f0a:b0:711:f0e2:ad67 with SMTP id hq10-20020a1709073f0a00b00711f0e2ad67mr1249773ejc.277.1655322208690; Wed, 15 Jun 2022 12:43:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655322208; cv=none; d=google.com; s=arc-20160816; b=iTE1lb/SM7HOJzuvI55mytvxNAy9xemvcwF0hcDIJP8RFHGEx6gaHXeMVKqRhM+Nle 1B3QpHJzGZWMHHV3jXkpdP0h9RYZr/gAeQlbXfOGoxEWZBVPXE997/DMdR+L756cBlmk 6tbEd/fzGgfhd8v88mF8JokZyCulpTpSjJmSwSbHC9xPNFoV6Xcaz36HMREK6lpZC0CW 2XtYge7p+ZxessWy1yiY8PVvUCXpp/SB13FY4WuKsl134QqwEhb0XSDCm2IqDDzFDbwF 5kj1yfAnZsNrU6ohU1CV+CRwibOPwUSQZNEvwbTWHgKK2V5nuxW+l1UZw5n+/zcKIyAh 4Zpw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WJt5W0EU+sFGaIOZbZ00omdFEDMKZn+VKOgfz5mNEys=; b=WzDkqxl2GBnQS+3X+1XeAbUafOXMU6YMnRlYixchFjB+98BdJER5ltztYN3V21NegQ C4DFO2hExpq9Eg1OjPDhmWEiePsKsSEnPCxk+LQPepydfVg1h2aX2UA95xCtUztnuF5M yq4+W6kCdo4J5TB7MAlfL332BAmHGbkevQukxlR8OrmUOUeBtiWzevSm9lrOau4cdj54 XumL8SNm+Ac35zl8jRjwhCkIyqwQj5J43rnh9thfB+F1P7Kd0wAKq2pB4p0IwYBI3dCU cpM697J2xFarG/wiSqkkUCCQablWwIMGW2GgPTFvmRa6bQ9QBiaxbVMdZWImvrZtJ0WZ aoCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=b6ysLzlm; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from phobos.denx.de (phobos.denx.de. [2a01:238:438b:c500:173d:9f52:ddab:ee01]) by mx.google.com with ESMTPS id i10-20020aa7dd0a000000b0042e2a928df9si69623edv.287.2022.06.15.12.43.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 12:43:28 -0700 (PDT) Received-SPF: pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=b6ysLzlm; spf=pass (google.com: domain of u-boot-bounces@lists.denx.de designates 2a01:238:438b:c500:173d:9f52:ddab:ee01 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id B0F4783DB8; Wed, 15 Jun 2022 21:43:18 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="b6ysLzlm"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id D29B383DB8; Wed, 15 Jun 2022 21:43:16 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-ej1-x632.google.com (mail-ej1-x632.google.com [IPv6:2a00:1450:4864:20::632]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 9D59B839BD for ; Wed, 15 Jun 2022 21:43:13 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=zajec5@gmail.com Received: by mail-ej1-x632.google.com with SMTP id bg6so25379656ejb.0 for ; Wed, 15 Jun 2022 12:43:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=WJt5W0EU+sFGaIOZbZ00omdFEDMKZn+VKOgfz5mNEys=; b=b6ysLzlmtRDwylwJm+veQg5s5/vYYErEooJ/Za89I9oViMbUzDMhBbTkShsSQheCLT Ze1sBG5WIQkFwVMq6NgJ9BGXQdNcbFyl74LC5B2CYlPuAje9SLLZB68Um551EM2RMPf/ Budgdhvy4B+A/OxGLMsVQudyDpCj4T93lsk36tK3MCawYkmCLvOEAIX6ocrMQtxu4niR naWZdckq5bJeboip25/FUNax9JBIQ2MgCNOqZs9fOG81GPolMPMTF3Y97P9rKNFQL6j/ eH2OEfNVSW84FcB8cCuqjF471WIPYOPZsM+41Z9m63xpcjmKNXGiyzMOxeCDpOVGV4Xs MjGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=WJt5W0EU+sFGaIOZbZ00omdFEDMKZn+VKOgfz5mNEys=; b=fwvkjTTLkBubh/to/ibHY79Dj65YhM/gNC9UgdYeMki3LoIK0pFcR/58kcGknCZXhG xemAP3Y02Jnip2bLI07x7aU3rJ+M+zHp1THe40F50E1GYBjDMX+ljGfEpRq3Wze7S8Gp StF1GWaPp2xBozyqZi0ZgoTJqSpECF+ZfGXslgGLGtOgs27Z2uAJ+/g13/kYKuP5k2hV /VaX+pMru2b88hAHSGNkYdVcclZoFtoCgTis+NqKBeMtD/6b98kpQmXjclSv451zPeze 1z0cge52mN3qymiwZsaWhDlo7zXyMiCj8Tj9f8h2VWMERSrj8fXEYbur2OcAGk3cuRka +7+A== X-Gm-Message-State: AJIora88f1FfKkyLh0ROpI/ZGBNHwjWBU4sUDWMF23WYiETWk1Cx5jms dtHSg2LMZieJRASAVjSX5Lc= X-Received: by 2002:a17:906:649b:b0:712:24cd:6102 with SMTP id e27-20020a170906649b00b0071224cd6102mr1320880ejm.664.1655322193217; Wed, 15 Jun 2022 12:43:13 -0700 (PDT) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id a16-20020aa7d750000000b0042bd6f745fasm18096eds.92.2022.06.15.12.43.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 15 Jun 2022 12:43:12 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla , Miquel Raynal , Richard Weinberger , Vignesh Raghavendra Cc: Tom Rini , Ahmad Fatoum , linux-arm-kernel@lists.infradead.org, u-boot@lists.denx.de, devicetree@vger.kernel.org, linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V4 2/2] nvmem: add driver handling U-Boot environment variables Date: Wed, 15 Jun 2022 21:43:00 +0200 Message-Id: <20220615194300.13358-2-zajec5@gmail.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220615194300.13358-1-zajec5@gmail.com> References: <20220615194300.13358-1-zajec5@gmail.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.5 at phobos.denx.de X-Virus-Status: Clean From: Rafał Miłecki U-Boot stores its setup as environment variables. It's a list of key-value pairs stored on flash device with a custom header. This commit adds an NVMEM driver that: 1. Provides NVMEM access to environment vars binary data 2. Extracts variables as NVMEM cells Current Linux's NVMEM sysfs API allows reading whole NVMEM data block. It can be used by user-space tools for reading U-Boot env vars block without the hassle of finding its location. Parsing will still need to be re-done there. Kernel-parsed NVMEM cells can be read however by Linux drivers. This may be useful for Ethernet drivers for reading device MAC address which is often stored as U-Boot env variable. Signed-off-by: Rafał Miłecki --- V3: Use of_get_mtd_device_by_node() (thanks Ahmad) & update description V2: Drop ARCH_BCM4908 dependency as there are plenty architectures using U-Boot bootloader. Thanks Srinivas. V4: Mention MTD in Kconfig help Use correct MTD device (instead of parent) - simplifies code a lot Use flexible arrays syntax ([]) Use of_device_get_match_data() helper Use dev_err_probe() As noticed by Ahmad a missing NVMEM subsystem feature is user-space access to parsed NVMEM cells. That is something I started working on some time ago and I'm planning to get back to at some point, please check: [PATCH 2/2] nvmem: expose NVMEM cells in sysfs https://lore.kernel.org/lkml/20211220064730.28806-2-zajec5@gmail.com/ --- MAINTAINERS | 1 + drivers/nvmem/Kconfig | 13 +++ drivers/nvmem/Makefile | 2 + drivers/nvmem/u-boot-env.c | 218 +++++++++++++++++++++++++++++++++++++ 4 files changed, 234 insertions(+) create mode 100644 drivers/nvmem/u-boot-env.c diff --git a/MAINTAINERS b/MAINTAINERS index 475e28365385..43b427fa76b0 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -20411,6 +20411,7 @@ U-BOOT ENVIRONMENT VARIABLES M: Rafał Miłecki S: Maintained F: Documentation/devicetree/bindings/nvmem/u-boot,env.yaml +F: drivers/nvmem/u-boot-env.c UACCE ACCELERATOR FRAMEWORK M: Zhangfei Gao diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig index d72d879a6d34..bab8a29c9861 100644 --- a/drivers/nvmem/Kconfig +++ b/drivers/nvmem/Kconfig @@ -344,4 +344,17 @@ config NVMEM_APPLE_EFUSES This driver can also be built as a module. If so, the module will be called nvmem-apple-efuses. +config NVMEM_U_BOOT_ENV + tristate "U-Boot environment variables support" + depends on OF && MTD + select CRC32 + 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. + + If compiled as module it will be called nvmem_u-boot-env. + endif diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile index c710b64f9fe4..399f9972d45b 100644 --- a/drivers/nvmem/Makefile +++ b/drivers/nvmem/Makefile @@ -69,3 +69,5 @@ obj-$(CONFIG_NVMEM_APPLE_EFUSES) += nvmem-apple-efuses.o nvmem-apple-efuses-y := apple-efuses.o obj-$(CONFIG_MICROCHIP_OTPC) += nvmem-microchip-otpc.o nvmem-microchip-otpc-y := microchip-otpc.o +obj-$(CONFIG_NVMEM_U_BOOT_ENV) += nvmem_u-boot-env.o +nvmem_u-boot-env-y := u-boot-env.o diff --git a/drivers/nvmem/u-boot-env.c b/drivers/nvmem/u-boot-env.c new file mode 100644 index 000000000000..9b9abfb8f187 --- /dev/null +++ b/drivers/nvmem/u-boot-env.c @@ -0,0 +1,218 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2022 Rafał Miłecki + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +enum u_boot_env_format { + U_BOOT_FORMAT_SINGLE, + U_BOOT_FORMAT_REDUNDANT, +}; + +struct u_boot_env { + struct device *dev; + enum u_boot_env_format format; + + struct mtd_info *mtd; + + /* Cells */ + struct nvmem_cell_info *cells; + int ncells; +}; + +struct u_boot_env_image_single { + __le32 crc32; + uint8_t data[]; +} __packed; + +struct u_boot_env_image_redundant { + __le32 crc32; + u8 mark; + uint8_t data[]; +} __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_add_cells(struct u_boot_env *priv, uint8_t *buf, + size_t data_offset, size_t data_len) +{ + struct device *dev = priv->dev; + char *data = buf + data_offset; + char *var, *value, *eq; + int idx; + + 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; + + for (var = data, idx = 0; + var < data + data_len && *var; + var = value + strlen(value) + 1, idx++) { + eq = strchr(var, '='); + if (!eq) + break; + *eq = '\0'; + value = eq + 1; + + priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL); + if (!priv->cells[idx].name) + return -ENOMEM; + priv->cells[idx].offset = data_offset + value - data; + priv->cells[idx].bytes = strlen(value); + } + + if (WARN_ON(idx != priv->ncells)) + priv->ncells = idx; + + return 0; +} + +static int u_boot_env_parse(struct u_boot_env *priv) +{ + struct device *dev = priv->dev; + size_t crc32_data_offset; + size_t crc32_data_len; + size_t crc32_offset; + size_t data_offset; + size_t data_len; + uint32_t crc32; + uint32_t calc; + size_t bytes; + uint8_t *buf; + int err; + + buf = kcalloc(1, priv->mtd->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); + goto err_kfree; + } + + switch (priv->format) { + case U_BOOT_FORMAT_SINGLE: + crc32_offset = offsetof(struct u_boot_env_image_single, crc32); + crc32_data_offset = offsetof(struct u_boot_env_image_single, data); + data_offset = offsetof(struct u_boot_env_image_single, data); + break; + case U_BOOT_FORMAT_REDUNDANT: + crc32_offset = offsetof(struct u_boot_env_image_redundant, crc32); + crc32_data_offset = offsetof(struct u_boot_env_image_redundant, mark); + data_offset = offsetof(struct u_boot_env_image_redundant, data); + break; + } + crc32 = le32_to_cpu(*(uint32_t *)(buf + crc32_offset)); + crc32_data_len = priv->mtd->size - crc32_data_offset; + data_len = priv->mtd->size - data_offset; + + calc = crc32(~0, buf + crc32_data_offset, crc32_data_len) ^ ~0L; + if (calc != crc32) { + dev_err(dev, "Invalid calculated CRC32: 0x%08x (expected: 0x%08x)\n", calc, crc32); + err = -EINVAL; + goto err_kfree; + } + + buf[priv->mtd->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); + +err_kfree: + kfree(buf); +err_out: + return err; +} + +static int u_boot_env_probe(struct platform_device *pdev) +{ + 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; + + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); + if (!priv) + return -ENOMEM; + priv->dev = dev; + + priv->format = (uintptr_t)of_device_get_match_data(dev); + + 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); + } + + 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)); +} + +static const struct of_device_id u_boot_env_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, }, + {}, +}; + +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, + }, +}; +module_platform_driver(u_boot_env_driver); + +MODULE_AUTHOR("Rafał Miłecki"); +MODULE_LICENSE("GPL"); +MODULE_DEVICE_TABLE(of, u_boot_env_of_match_table);