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
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.
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>
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
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.
27 +config NVMEM_APPLE_EFUSES
28 + tristate "Apple eFuse support"
29 + depends on ARCH_APPLE || COMPILE_TEST
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.
36 + This driver can also be built as a module. If so, the module will
37 + be called nvmem-apple-efuses.
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
49 +++ b/drivers/nvmem/apple-efuses.c
51 +// SPDX-License-Identifier: GPL-2.0-only
53 + * Apple SoC eFuse driver
55 + * Copyright (C) The Asahi Linux Contributors
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>
64 +struct apple_efuses_priv {
65 + void __iomem *fuses;
68 +static int apple_efuses_read(void *context, unsigned int offset, void *val,
71 + struct apple_efuses_priv *priv = context;
74 + while (bytes >= sizeof(u32)) {
75 + *dst++ = readl_relaxed(priv->fuses + offset);
76 + bytes -= sizeof(u32);
77 + offset += sizeof(u32);
83 +static int apple_efuses_probe(struct platform_device *pdev)
85 + struct apple_efuses_priv *priv;
86 + struct resource *res;
87 + struct nvmem_config config = {
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,
98 + priv = devm_kzalloc(config.dev, sizeof(*priv), GFP_KERNEL);
102 + priv->fuses = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
103 + if (IS_ERR(priv->fuses))
104 + return PTR_ERR(priv->fuses);
106 + config.priv = priv;
107 + config.size = resource_size(res);
109 + return PTR_ERR_OR_ZERO(devm_nvmem_register(config.dev, &config));
112 +static const struct of_device_id apple_efuses_of_match[] = {
113 + { .compatible = "apple,efuses", },
117 +MODULE_DEVICE_TABLE(of, apple_efuses_of_match);
119 +static struct platform_driver apple_efuses_driver = {
121 + .name = "apple_efuses",
122 + .of_match_table = apple_efuses_of_match,
124 + .probe = apple_efuses_probe,
127 +module_platform_driver(apple_efuses_driver);
129 +MODULE_AUTHOR("Sven Peter <sven@svenpeter.dev>");
130 +MODULE_LICENSE("GPL");