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: 13384668 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id F25BAEDE987 for ; Thu, 14 Sep 2023 06:50:12 +0000 (UTC) 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: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:In-Reply-To:References: List-Owner; bh=QHt/vjeuXiOJsTdc8zUv9Lq6s7vdCGwTv7p9n+zDdAs=; b=aPcgl7te7bvH1/ 9mqwZn1NbllszR0vz2B3TrGVz0tpAlFpb2pwL6xCEE0pnrudPW810ewDW7tAzvDce9NhMJOjYYpjn D5xfLpFA4guoKL4nIoxluT+6cRZCbYmVOPBA//XWokw9xussd+lxrdTfrYQXSruyx512KDOtcX0WC KnVQoRP0JXgjNfOHczjWi/p/i9P/ArGe2knJRP7uD8U1kcahszv8sPnL5cwK5e4loxVl+CEZSx4SC rN5M12WI77UgcVzSkoWp+Z+erM+YoXjgcPh4WoeMZp28ckgv9H2ZJ+TEEO4BJ3suTkGQvMI1YB1t4 HjUOdGVdLjvGkcgvsvaA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qggAa-007Y5J-0o; Thu, 14 Sep 2023 06:49:44 +0000 Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qggAW-007Y4L-1i; Thu, 14 Sep 2023 06:49:42 +0000 Received: by mail-lj1-x234.google.com with SMTP id 38308e7fff4ca-2bfb12b24e5so9284261fa.0; 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=lists.infradead.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=BrWprnoysZ+qU/d4GNicXSoIWc2GAlSCkQpMFD6X6hw+EU0MDCi9TVHTVHSV8nQwn8 Sw4NnaTiHuEQbqhBZ+rg9vCtU2IrNQdXwhU8yjUoxDWFZ1Swvg7NS46LZ58gl8tAbRQs NKZhC0tcLvqosn3XiBLKkxEq+d+zo5og+/SIyc6e5GKX0p0BWP2deVAK4SPKc3CHK5Ah rtnXrXjgEIeoaQCgCMZTu2dwv7GC/8m/DxgLf1HXRIxyXVEBAiCQqF/ZPI6Rs4eDmtt8 C0bGJI2xS1dBUkUgCPY+4Ah7MxX9t3Dnujf2L5leLM8F8LXNWK23xFMr5g1I1Ifg10Iw FdFA== 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=RzqohgOq4tS66DdPtcwIW9iPT8dJmEO3uYVKAPr1/c/HBvvnbJvCBM/zw6FUbb3gVp UGokbXfmSEDWtge/zvi8+LrIDAHSwlXsk30D/3yNW7Gu44JYAhDg/MkW9Oa7PGASDfbR qMIxqRrScHRZcHc42J6Ymp/0DHIKc6s+o7DIFU4UTCDjvHBWoEgGWDqRgW1rpi6OJ1z8 EcpJNtv82xuPGXAczvogz1hmzSl4QTVodVRPlaPLOv81TPwhYcMIkgh+07hK24OZzCyG r80YnSZ79skC2gUnKc5ReFxb09EKiTCs2ACXCF5zxSf2MDBkIUVdiqE+jWQtfDk72MQP YQ3w== X-Gm-Message-State: AOJu0YySehsRjaMir+Qj4oybcY5OKtJHUFi2qrlTx9g/onAeTFnLBboh JHUU8vHfpx6bQG9NjyZT0gU= X-Google-Smtp-Source: AGHT+IGmZW4nrpil9KXEo+bbhmVfahNQoU3dZnFCrmBmZD4hG2m18HihW7kyboCIQAU1X8pIArdHsA== 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230913_234940_591490_1C37B1E4 X-CRM114-Status: GOOD ( 19.26 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org 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)); }