c1dea1f26bf5242baf9cec6d489d99be4e6a50f1
[openwrt/staging/blocktrron.git] /
1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
2 Date: Tue, 2 Mar 2021 08:33:13 +0100
3 Subject: [PATCH] firmware: bcm47xx_nvram: support platform device "brcm,nvram"
4
5 Add support for platform device providing mapping resource. This allows
6 reading NVRAM based on DT mapping binding. It's required for devices
7 that boot depending on NVRAM stored setup and provides early access to
8 NVRAM data.
9
10 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
11 ---
12 drivers/firmware/broadcom/bcm47xx_nvram.c | 55 +++++++++++++++++++++++
13 1 file changed, 55 insertions(+)
14
15 --- a/drivers/firmware/broadcom/bcm47xx_nvram.c
16 +++ b/drivers/firmware/broadcom/bcm47xx_nvram.c
17 @@ -13,6 +13,7 @@
18 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/mtd/mtd.h>
21 +#include <linux/platform_device.h>
22 #include <linux/bcm47xx_nvram.h>
23
24 #define NVRAM_MAGIC 0x48534C46 /* 'FLSH' */
25 @@ -162,6 +163,60 @@ static int nvram_init(void)
26 return -ENXIO;
27 }
28
29 +static int brcm_nvram_probe(struct platform_device *pdev)
30 +{
31 + struct nvram_header __iomem *header;
32 + struct device *dev = &pdev->dev;
33 + struct resource *res;
34 + void __iomem *mmio;
35 + size_t copy_len;
36 +
37 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
38 + if (!res) {
39 + dev_err(dev, "Failed to get resource\n");
40 + return -ENODEV;
41 + }
42 +
43 + mmio = ioremap(res->start, resource_size(res));
44 + if (!mmio)
45 + return -ENOMEM;
46 +
47 + header = (struct nvram_header *)mmio;
48 + copy_len = DIV_ROUND_UP(sizeof(*header) + header->len, 4);
49 + if (header->magic != NVRAM_MAGIC) {
50 + dev_err(dev, "No NVRAM found at %pR\n", res);
51 + return -EPROTO;
52 + } else if (copy_len > resource_size(res)) {
53 + dev_err(dev, "NVRAM size exceeds %pR\n", res);
54 + return -ERANGE;
55 + } else if (copy_len >= NVRAM_SPACE) {
56 + dev_err(dev, "NVRAM size exceeds buffer size %d\n", NVRAM_SPACE);
57 + return -ENOMEM;
58 + }
59 +
60 + __ioread32_copy(nvram_buf, mmio, copy_len);
61 + nvram_buf[NVRAM_SPACE - 1] = '\0';
62 +
63 + iounmap(mmio);
64 +
65 + return 0;
66 +}
67 +
68 +static const struct of_device_id brcm_nvram_of_match[] = {
69 + { .compatible = "brcm,nvram "},
70 + {},
71 +};
72 +
73 +static struct platform_driver brcm_nvram_driver = {
74 + .driver = {
75 + .name = "brcm_nvram",
76 + .of_match_table = brcm_nvram_of_match,
77 + },
78 + .probe = brcm_nvram_probe,
79 +};
80 +
81 +module_platform_driver(brcm_nvram_driver);
82 +
83 int bcm47xx_nvram_getenv(const char *name, char *val, size_t val_len)
84 {
85 char *var, *value, *end, *eq;