From patchwork Thu Sep 14 06:49:22 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: 139530 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp300365vqi; Thu, 14 Sep 2023 05:11:28 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEE1RNV02e1uTLNsFRdgs1/HHa1W4VMCltq9518ZI9U6cAF7x4K7UVfwxjW1q2Ke+RAjcxB X-Received: by 2002:a05:6870:d698:b0:1d5:cdf7:bda9 with SMTP id z24-20020a056870d69800b001d5cdf7bda9mr5573529oap.41.1694693488019; Thu, 14 Sep 2023 05:11:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694693487; cv=none; d=google.com; s=arc-20160816; b=lJQ4/xHgMpA12YQQafo9Kp2VwoINPCbKwft2BKRTEKPtNWyF40KDgsq2VirZtvHdNe GxdeE5X5feJgFXAqqFG4AB5RFZUhv/Z4Qbz4C7mF/Gw5MxUQ8e8wtkiQ5HHOEpYKc5Ju 8Yc07b5pKZJB6XYC7OU7tJ67++4UR0BksSOrQNpAupaWuDTF15/xJxLnhihKQN3Wzv4D sAmdqhCQQEvoE9XB4e/3MApnOo8NfSWnItAouDvH94sBlUb5m79YqTaKdZphEfwXTCpI dfYSptRuiWRfODVU3AaW2LlzNWgQotcIzDWFCokiJP3t3GWw8Hn+5xN05/IEvzJIkGEp x/eg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=nLt56x/+byFP2a42jCI16rLI5Oh2/81pcuquOpoEQwU=; fh=3eHDZMhXUFVZgaZYZiY5IQDZ5JY4jR+sXzlRZHrXlZw=; b=MXglioOZC4c9QtnHWBhM0fwy+4sVKPV+aFTzAYnUCVS67nFvXVfLyoVVCLR8yScuuH qcRKLW5jnUOOQJdoBR/T68t0LGjbODbl3SjOu5CHEtZkh7pHnt2aTGUT+utEBwTfA2mE ltU8Pztm5+j2JT/tmZ8aTCRHV8ObDY9tzGrA6cYbDcRDpWQNGnGdzzgy5i+2hRMb4Rxf x6xHh/h//HX2oOWqfKrcr2g7PuGjh35W/N6/Ibrmw2JWpiy4Sx77d9iDhM+cxavNWdf2 HvGluwexoxqBEH2Y+EQBPCEmpuF02DKyQ47Ul4rG9aB1HRHJDrQ+Y9WJI0NgFRqqpWUJ kGQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=hfhGBqt9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from fry.vger.email (fry.vger.email. [2620:137:e000::3:8]) by mx.google.com with ESMTPS id ca20-20020a056a02069400b005740e906e46si1526583pgb.358.2023.09.14.05.11.27 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Sep 2023 05:11:27 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) client-ip=2620:137:e000::3:8; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=hfhGBqt9; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:8 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id AB65D82F4CB4; Wed, 13 Sep 2023 23:49:54 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235654AbjINGtn (ORCPT + 35 others); Thu, 14 Sep 2023 02:49:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232171AbjINGtl (ORCPT ); Thu, 14 Sep 2023 02:49:41 -0400 Received: from mail-lj1-x229.google.com (mail-lj1-x229.google.com [IPv6:2a00:1450:4864:20::229]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9FDB7CCD for ; Wed, 13 Sep 2023 23:49:37 -0700 (PDT) Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-2bceb02fd2bso9089841fa.1 for ; Wed, 13 Sep 2023 23:49:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694674176; x=1695278976; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=nLt56x/+byFP2a42jCI16rLI5Oh2/81pcuquOpoEQwU=; b=hfhGBqt9lDVZjZ7aiQRJJO7kkG8Xlip5rKZg1zMqq3cD1pICRoTlisBVsn7v4BYa0b lZgxxOrcmZmjggdryYpBh0ogHulzZ3NkkufkiXt1R5b5mxW10qYCDkiSNhbBeT/C+8Z9 M30jjrem6oTiN/XOMnAlG24UBVYz6guWlrZOgrhTLupkT5KethtvR6RBdKmdZQKFcqp+ Mb4PwuLN2xF5K34uQO4Aa4m1j0lzoSTObLhzghSZS2lebSxXwWbAf4JDA4oegfDxOa3f P0vGtU4FyBgsoTauFjNPos9lYpnB4xEdfye1aKnTG6KGw6Kap/YTXrg5/B8vA6iZnBvG 4ATg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694674176; x=1695278976; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=nLt56x/+byFP2a42jCI16rLI5Oh2/81pcuquOpoEQwU=; b=DxKd/Ssgr6CoDaXYwhDbUFDtniZ+n5WfmvW4W+gFSfWcU+JvCwzpKEUYZbe1b5MwMc 9qguzgfutc3tmOxkbn4ocSC4F3lNozzKo80RdkkLqiKcSylvEZrtHnB/nT0VSEQ1xGGr ErGmcq1KQceRmVEyluw+Zwz1c6U/x2p7RbHlJbi16jhOxJ04B33tuMwuWUyX+rxl+eHI vE5laAxqVpC2jY5B95Vp6bf5dq5daT3X6KqKIjwYLPG32NaHGSixkrcbUuKcVhAoEpON drpx+z41eh05MXQPkdXWU4anesdqtplsE7vY+Di5Yosp6FjFXvkkDxCmoVmVjudl/U/P SxCw== X-Gm-Message-State: AOJu0YwU781JzcxMaEtTI7abkz7UIGzNyNWqW7PdEdzS0yyHhL9XfsLH xtq5ObMaJClQMEzDMudmi1s= X-Received: by 2002:a2e:8850:0:b0:2bb:c22a:f28c with SMTP id z16-20020a2e8850000000b002bbc22af28cmr4775114ljj.32.1694674175493; Wed, 13 Sep 2023 23:49:35 -0700 (PDT) Received: from localhost.lan (031011218106.poznan.vectranet.pl. [31.11.218.106]) by smtp.gmail.com with ESMTPSA id l4-20020a2e7004000000b002b9f0b25ff6sm159739ljc.4.2023.09.13.23.49.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 23:49:35 -0700 (PDT) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla Cc: linux-mtd@lists.infradead.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= , =?utf-8?b?QXLEsW7DpyA=?= =?utf-8?b?w5xOQUw=?= , Florian Fainelli , Scott Branden Subject: [PATCH] nvmem: brcm_nvram: store a copy of NVRAM content Date: Thu, 14 Sep 2023 08:49:22 +0200 Message-Id: <20230914064922.3986-1-zajec5@gmail.com> X-Mailer: git-send-email 2.35.3 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 13 Sep 2023 23:49:54 -0700 (PDT) X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777013275053253119 X-GMAIL-MSGID: 1777014918777839372 From: Rafał Miłecki This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window. Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND. Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested. Link: https://lore.kernel.org/linux-mtd/CACna6rwf3_9QVjYcM+847biTX=K0EoWXuXcSMkJO1Vy_5vmVqA@mail.gmail.com/ Cc: Arınç ÜNAL Cc: Florian Fainelli Cc: Scott Branden Signed-off-by: Rafał Miłecki --- drivers/nvmem/brcm_nvram.c | 77 +++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 34 deletions(-) diff --git a/drivers/nvmem/brcm_nvram.c b/drivers/nvmem/brcm_nvram.c index 9737104f3b76..cfc1c485e20a 100644 --- a/drivers/nvmem/brcm_nvram.c +++ b/drivers/nvmem/brcm_nvram.c @@ -19,7 +19,8 @@ struct brcm_nvram { struct device *dev; - void __iomem *base; + uint8_t *data; + size_t size; struct nvmem_cell_info *cells; int ncells; }; @@ -36,10 +37,8 @@ static int brcm_nvram_read(void *context, unsigned int offset, void *val, size_t bytes) { struct brcm_nvram *priv = context; - u8 *dst = val; - while (bytes--) - *dst++ = readb(priv->base + offset++); + memcpy(val, priv->data + offset, bytes); return 0; } @@ -68,7 +67,12 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, { struct device *dev = priv->dev; char *var, *value, *eq; + uint8_t tmp; int idx; + int err = 0; + + tmp = priv->data[len - 1]; + priv->data[len - 1] = '\0'; priv->ncells = 0; for (var = data + sizeof(struct brcm_nvram_header); @@ -78,8 +82,10 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, } priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL); - if (!priv->cells) - return -ENOMEM; + if (!priv->cells) { + err = -ENOMEM; + goto out; + } for (var = data + sizeof(struct brcm_nvram_header), idx = 0; var < (char *)data + len && *var; @@ -91,8 +97,10 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, 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 out; + } priv->cells[idx].offset = value - (char *)data; priv->cells[idx].bytes = strlen(value); priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name); @@ -105,40 +113,32 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, } } - return 0; +out: + priv->data[len - 1] = tmp; + return err; } static int brcm_nvram_parse(struct brcm_nvram *priv) { + struct brcm_nvram_header *header = (struct brcm_nvram_header *)priv->data; struct device *dev = priv->dev; - struct brcm_nvram_header header; - uint8_t *data; size_t len; int err; - memcpy_fromio(&header, priv->base, sizeof(header)); - - if (memcmp(header.magic, NVRAM_MAGIC, 4)) { + if (memcmp(header->magic, NVRAM_MAGIC, 4)) { dev_err(dev, "Invalid NVRAM magic\n"); return -EINVAL; } - len = le32_to_cpu(header.len); - - data = kzalloc(len, GFP_KERNEL); - if (!data) - return -ENOMEM; - - memcpy_fromio(data, priv->base, len); - data[len - 1] = '\0'; - - err = brcm_nvram_add_cells(priv, data, len); - if (err) { - dev_err(dev, "Failed to add cells: %d\n", err); - return err; + len = le32_to_cpu(header->len); + if (len > priv->size) { + dev_err(dev, "NVRAM length (%zd) exceeds mapped size (%zd)\n", len, priv->size); + return -EINVAL; } - kfree(data); + err = brcm_nvram_add_cells(priv, priv->data, len); + if (err) + dev_err(dev, "Failed to add cells: %d\n", err); return 0; } @@ -150,8 +150,9 @@ static int brcm_nvram_probe(struct platform_device *pdev) .reg_read = brcm_nvram_read, }; struct device *dev = &pdev->dev; - struct resource *res; struct brcm_nvram *priv; + struct resource *res; + void __iomem *base; int err; priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); @@ -159,21 +160,29 @@ static int brcm_nvram_probe(struct platform_device *pdev) return -ENOMEM; priv->dev = dev; - priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); - if (IS_ERR(priv->base)) - return PTR_ERR(priv->base); + base = devm_platform_get_and_ioremap_resource(pdev, 0, &res); + if (IS_ERR(base)) + return PTR_ERR(base); + + priv->size = resource_size(res); + + priv->data = devm_kzalloc(dev, priv->size, GFP_KERNEL); + if (!priv->data) + return -ENOMEM; + + memcpy_fromio(priv->data, base, priv->size); err = brcm_nvram_parse(priv); if (err) return err; - bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res)); + bcm47xx_nvram_init_from_iomem(base, priv->size); config.dev = dev; config.cells = priv->cells; config.ncells = priv->ncells; config.priv = priv; - config.size = resource_size(res); + config.size = priv->size; return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config)); }