kernel: update nvmem subsystem to the latest upstream
[openwrt/staging/hauke.git] / target / linux / generic / backport-5.10 / 809-v5.19-0006-nvmem-Add-Apple-eFuse-driver.patch
1 From b6b7ef932ae838209254f016ecf8862d716a5ced Mon Sep 17 00:00:00 2001
2 From: Sven Peter <sven@svenpeter.dev>
3 Date: Fri, 29 Apr 2022 17:26:50 +0100
4 Subject: [PATCH] nvmem: Add Apple eFuse driver
5
6 Apple SoCs contain eFuses used to store factory-programmed data such
7 as calibration values for the PCIe or the Type-C PHY. They are organized
8 as 32bit values exposed as MMIO.
9
10 Signed-off-by: Sven Peter <sven@svenpeter.dev>
11 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
12 Link: https://lore.kernel.org/r/20220429162701.2222-6-srinivas.kandagatla@linaro.org
13 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
14 ---
15 drivers/nvmem/Kconfig | 12 ++++++
16 drivers/nvmem/Makefile | 2 +
17 drivers/nvmem/apple-efuses.c | 80 ++++++++++++++++++++++++++++++++++++
18 3 files changed, 94 insertions(+)
19 create mode 100644 drivers/nvmem/apple-efuses.c
20
21 --- a/drivers/nvmem/Kconfig
22 +++ b/drivers/nvmem/Kconfig
23 @@ -324,4 +324,16 @@ config NVMEM_SUNPLUS_OCOTP
24 This driver can also be built as a module. If so, the module
25 will be called nvmem-sunplus-ocotp.
26
27 +config NVMEM_APPLE_EFUSES
28 + tristate "Apple eFuse support"
29 + depends on ARCH_APPLE || COMPILE_TEST
30 + default ARCH_APPLE
31 + help
32 + Say y here to enable support for reading eFuses on Apple SoCs
33 + such as the M1. These are e.g. used to store factory programmed
34 + calibration data required for the PCIe or the USB-C PHY.
35 +
36 + This driver can also be built as a module. If so, the module will
37 + be called nvmem-apple-efuses.
38 +
39 endif
40 --- a/drivers/nvmem/Makefile
41 +++ b/drivers/nvmem/Makefile
42 @@ -65,3 +65,5 @@ obj-$(CONFIG_NVMEM_LAYERSCAPE_SFP) += nv
43 nvmem-layerscape-sfp-y := layerscape-sfp.o
44 obj-$(CONFIG_NVMEM_SUNPLUS_OCOTP) += nvmem_sunplus_ocotp.o
45 nvmem_sunplus_ocotp-y := sunplus-ocotp.o
46 +obj-$(CONFIG_NVMEM_APPLE_EFUSES) += nvmem-apple-efuses.o
47 +nvmem-apple-efuses-y := apple-efuses.o
48 --- /dev/null
49 +++ b/drivers/nvmem/apple-efuses.c
50 @@ -0,0 +1,80 @@
51 +// SPDX-License-Identifier: GPL-2.0-only
52 +/*
53 + * Apple SoC eFuse driver
54 + *
55 + * Copyright (C) The Asahi Linux Contributors
56 + */
57 +
58 +#include <linux/io.h>
59 +#include <linux/mod_devicetable.h>
60 +#include <linux/module.h>
61 +#include <linux/nvmem-provider.h>
62 +#include <linux/platform_device.h>
63 +
64 +struct apple_efuses_priv {
65 + void __iomem *fuses;
66 +};
67 +
68 +static int apple_efuses_read(void *context, unsigned int offset, void *val,
69 + size_t bytes)
70 +{
71 + struct apple_efuses_priv *priv = context;
72 + u32 *dst = val;
73 +
74 + while (bytes >= sizeof(u32)) {
75 + *dst++ = readl_relaxed(priv->fuses + offset);
76 + bytes -= sizeof(u32);
77 + offset += sizeof(u32);
78 + }
79 +
80 + return 0;
81 +}
82 +
83 +static int apple_efuses_probe(struct platform_device *pdev)
84 +{
85 + struct apple_efuses_priv *priv;
86 + struct resource *res;
87 + struct nvmem_config config = {
88 + .dev = &pdev->dev,
89 + .read_only = true,
90 + .reg_read = apple_efuses_read,
91 + .stride = sizeof(u32),
92 + .word_size = sizeof(u32),
93 + .name = "apple_efuses_nvmem",
94 + .id = NVMEM_DEVID_AUTO,
95 + .root_only = true,
96 + };
97 +
98 + priv = devm_kzalloc(config.dev, sizeof(*priv), GFP_KERNEL);
99 + if (!priv)
100 + return -ENOMEM;
101 +
102 + priv->fuses = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
103 + if (IS_ERR(priv->fuses))
104 + return PTR_ERR(priv->fuses);
105 +
106 + config.priv = priv;
107 + config.size = resource_size(res);
108 +
109 + return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
110 +}
111 +
112 +static const struct of_device_id apple_efuses_of_match[] = {
113 + { .compatible = "apple,efuses", },
114 + {}
115 +};
116 +
117 +MODULE_DEVICE_TABLE(of, apple_efuses_of_match);
118 +
119 +static struct platform_driver apple_efuses_driver = {
120 + .driver = {
121 + .name = "apple_efuses",
122 + .of_match_table = apple_efuses_of_match,
123 + },
124 + .probe = apple_efuses_probe,
125 +};
126 +
127 +module_platform_driver(apple_efuses_driver);
128 +
129 +MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
130 +MODULE_LICENSE("GPL");