From patchwork Mon Dec 20 18:44:44 2021 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: 12696870 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 69FAEC433EF for ; Mon, 20 Dec 2021 18:46:21 +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: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=rznp9/XOh1xLqrErTFc4ufwRiyO8z8iy1xftJiuA2gs=; b=YyHNCvmRtkA/e5 QKk02Ts+0UAMlWGyev3nYbUaks0gZZhmGIPLVgLPFNU0weNkXx5xFt4dfyuUc4J733j3mVhvPAK6D Tovo5b5pRCEgV2GRJwuxTloq4UA8uA0k8Nn2ELzSKMJt/o/kHtwgYB1xwyACoQV/a7CtfYIVK2uU2 ceZTna8wkjRwBA5aZRe5nr0lYqmJvNjYGTJOmcwgiQERoXRRhuUejgHnqrVGRGZQG0tf/ap4iXi4x RgOCpqRJ2fJLHI7+2rwZqAa97WS8pR/auDigDfD7o2LkJJBWWcbHhpcVVdiB+6m9FbQvHoxMPrYe0 XNkSBNR69kXmjI1bWJDQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1mzNeY-003sCO-A0; Mon, 20 Dec 2021 18:44:54 +0000 Received: from mail-lf1-x12f.google.com ([2a00:1450:4864:20::12f]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1mzNeU-003sAk-J8 for linux-arm-kernel@lists.infradead.org; Mon, 20 Dec 2021 18:44:52 +0000 Received: by mail-lf1-x12f.google.com with SMTP id u13so16501085lff.12 for ; Mon, 20 Dec 2021 10:44:50 -0800 (PST) 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=xwyysmftigZMwkoovGWOrS2OfXQlj0gb0VWFGJp3JU4=; b=hZ8zj/bBjjSey4EYsj3XFfYiBIUSf0CqjE5ISBdpyXkZbuhlSnYLUvWV/mFredrusP Qw2tD8fVwgdjayiDgm96oFjW44SWAQktSiykvysyXac6CD/Y7CCmleIMklnUez2LbnlT jhr3BMWfZRE8tuVAKYEu/PHc5CnXEzbGXlH3k2M3+W28JRQfrPO8GyO7CE9VPvgh+d5Y I0DCy2q+1ZhV5JMpgxGMdHEWY5f7bci8HR3DH6vIs2JoDY7LyWwGjr/oLQMpOOS45IG3 Cy3LWV5aefRRsUJRnNXFgM3AL7f8AkHCte50ho8+m0i1G/E8uWbWcgqhxjOgFOKHL5p7 LdiA== 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=xwyysmftigZMwkoovGWOrS2OfXQlj0gb0VWFGJp3JU4=; b=ZkHbgv9zoWIzG4c7W6ndxwDv44os6Hwb10QesklYUDIF1hC0wTPFadEpvTlkidkMsy c8Y3DQwRNpVjXeR7HY1WvOoAJJSDxZYJ8UhrvO5jao+54eKCi1PDLBCXlcwnepaQS8aH N917nKHYCmxaf5Dg/aZ2GAS9pGMlJjccI4IE3H1mCRDG9umVuV3LMjZ+0FfTMLKpM/VE JF7qNCENcd/I6Hb9mVmXUHhb1V5WF3qCMMC0oVz3MqXKr4xTASTDWRFBu5BQ4T3jGCuM jQqCiihFHJKLD6ZMFYMDqrDVrWiRHw/DjP60wA/cFdEC+OpsK0DbBWmTMJxEZ5bP8UeT 5KVQ== X-Gm-Message-State: AOAM530wMprPCcAL1iNE6yIwNhHkwjm0aDpaZEW+5YAgFchrhqpNRgOk ZEy6XCZ11ptgUIVR5PDVP24= X-Google-Smtp-Source: ABdhPJwojmRFO1iheIXySReLnzodnXqWsLojzZvsltPx3OXB5x/gudm31I2UoMy6mAZDOczZSfFvbg== X-Received: by 2002:a05:6512:1054:: with SMTP id c20mr2434248lfb.247.1640025888947; Mon, 20 Dec 2021 10:44:48 -0800 (PST) Received: from localhost.lan (ip-194-187-74-233.konfederacka.maverick.com.pl. [194.187.74.233]) by smtp.gmail.com with ESMTPSA id k32sm222870lfv.196.2021.12.20.10.44.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Dec 2021 10:44:48 -0800 (PST) From: =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= To: Srinivas Kandagatla Cc: linux-arm-kernel@lists.infradead.org, linux-mips@vger.kernel.org, Florian Fainelli , bcm-kernel-feedback-list@broadcom.com, linux-kernel@vger.kernel.org, =?utf-8?b?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH V2] nvmem: brcm_nvram: parse NVRAM content into NVMEM cells Date: Mon, 20 Dec 2021 19:44:44 +0100 Message-Id: <20211220184444.6693-1-zajec5@gmail.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211220184226.6485-1-zajec5@gmail.com> References: <20211220184226.6485-1-zajec5@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211220_104450_682543_F9CE178D X-CRM114-Status: GOOD ( 18.52 ) 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 NVRAM consist of header and NUL separated key-value pairs. Parse it and create NVMEM cell for every key-value entry. Signed-off-by: Rafał Miłecki --- V2: Drop dev_info() calls I used for debugging --- drivers/nvmem/brcm_nvram.c | 90 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/drivers/nvmem/brcm_nvram.c b/drivers/nvmem/brcm_nvram.c index bd2ecaaf4585..439f00b9eef6 100644 --- a/drivers/nvmem/brcm_nvram.c +++ b/drivers/nvmem/brcm_nvram.c @@ -6,12 +6,26 @@ #include #include #include +#include #include #include +#include + +#define NVRAM_MAGIC "FLSH" struct brcm_nvram { struct device *dev; void __iomem *base; + struct nvmem_cell_info *cells; + int ncells; +}; + +struct brcm_nvram_header { + char magic[4]; + __le32 len; + __le32 crc_ver_init; /* 0:7 crc, 8:15 ver, 16:31 sdram_init */ + __le32 config_refresh; /* 0:15 sdram_config, 16:31 sdram_refresh */ + __le32 config_ncdl; /* ncdl values for memc */ }; static int brcm_nvram_read(void *context, unsigned int offset, void *val, @@ -26,6 +40,75 @@ static int brcm_nvram_read(void *context, unsigned int offset, void *val, return 0; } +static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data, + size_t len) +{ + struct device *dev = priv->dev; + char *var, *value, *eq; + int idx; + + priv->ncells = 0; + for (var = data + sizeof(struct brcm_nvram_header); + var < (char *)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 + sizeof(struct brcm_nvram_header), idx = 0; + var < (char *)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 = value - (char *)data; + priv->cells[idx].bytes = strlen(value); + } + + return 0; +} + +static int brcm_nvram_parse(struct brcm_nvram *priv) +{ + 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)) { + dev_err(dev, "Invalid NVRAM magic\n"); + return -EINVAL; + } + + len = le32_to_cpu(header.len); + + data = kcalloc(1, len, GFP_KERNEL); + 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; + } + + kfree(data); + + return 0; +} + static int brcm_nvram_probe(struct platform_device *pdev) { struct nvmem_config config = { @@ -35,6 +118,7 @@ static int brcm_nvram_probe(struct platform_device *pdev) struct device *dev = &pdev->dev; struct resource *res; struct brcm_nvram *priv; + int err; priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); if (!priv) @@ -46,7 +130,13 @@ static int brcm_nvram_probe(struct platform_device *pdev) if (IS_ERR(priv->base)) return PTR_ERR(priv->base); + err = brcm_nvram_parse(priv); + if (err) + return err; + config.dev = dev; + config.cells = priv->cells; + config.ncells = priv->ncells; config.priv = priv; config.size = resource_size(res);