[PATCH v4 2/2] nvmem: aspeed: Add read-only AST2600 OTP support
Prasanth
prasanth.padarthi10 at gmail.com
Thu Dec 3 17:51:36 PST 2026
Hi Andrew,
Thanks for the detailed review. I’ll address these points in the next revision.
- fix the continuation indentation and unwrap the short bounds check.
- use readl_poll_timeout() for idle polling. The 1 µs delay will
remain only after issuing an OTP command, as required by ASPEED’s
clarification.
- reject odd addresses in the internal pair-read helper. Public reads
may still begin at an odd word by selecting the containing even pair.
- move offset and length validation into the helper that performs the
arithmetic and copying, while preserving zero-length success and
overflow-safe bounds checks.
- ensure the failure latch is checked before any further controller
operation. Probe already stops after the first initialization failure
and only performs relock cleanup.
- consider the mutex annotations and context-analysis support for
state protected by the mutex.
- inline the single-use initialization helper if that makes the flow clearer.
- use devm_kzalloc_obj() if there is no compatibility concern with the
supported kernel baseline.
The probe-time NVMEM keepout covers the fixed CONF0 range. Neal
confirmed that SEC54 can increase until an ARM reset, including while
Linux is running, and recommended checking it on each read.
https://lore.kernel.org/linux-aspeed/KL1PR0601MB3986A356BD06F98D0FF9AD5E80792@KL1PR0601MB3986.apcprd06.prod.outlook.com/
If SEC54 grows after probe and a request falls within the newly
protected range, would you prefer the driver to return -EACCES before
issuing the read, or rely on the controller’s response?
For the SEC00 readback, I am not aware of an AST2600-specific erratum.
It was added as a precaution the relock write has completed before
releasing the mutex to another caller. I’ll either document that
ordering requirement clearly or remove the readback if the controller
does not require a completion read.
Regards,
Prasanth
On Fri, Dec 4, 2026 at 6:30 AM Andrew Jeffery
<andrew at codeconstruct.com.au> wrote:
>
> On Fri, 2026-11-27 at 08:28 +0530, Prasanth Kumar Padarthi wrote:
> > AST2600's secure boot controller provides an 8-KiB OTP data region through
> > paired-word commands rather than memory-mapped storage. Expose permitted
> > words through NVMEM for privileged userspace bring-up diagnostics, which
> > compare raw OTP offsets with reference reads; no named cells are required
> > by this consumer.
> >
> > Earlier firmware may leave the OTP macro in programming mode, so Linux
> > must establish read mode before using it. Programming protection does not
> > prevent reads, but the configured secure prefix and SEC54 restrict the
> > readable region. SEC54 can increase until ARM reset, so a probe-time
> > snapshot alone cannot describe every later read restriction.
> >
> > SEC00 relocking does not cancel a running command. Since there is no
> > documented safe retry after a timeout, stop issuing commands rather than
> > risk overlapping an outstanding operation. Normal reads take less than
> > 1 us; the 100-ms bound is deliberately generous, not a specified maximum.
> >
> > The read sequence is adapted from ASPEED's downstream driver.
> >
> > Link: https://github.com/AspeedTech-BMC/linux/blob/769f62b7baa84d6998723b0ea60280e380183553/drivers/soc/aspeed/ast2600-otp.c
> > Link: https://lore.kernel.org/linux-aspeed/KL1PR0601MB3986A356BD06F98D0FF9AD5E80792@KL1PR0601MB3986.apcprd06.prod.outlook.com/
> >
> > Co-developed-by: Neal Liu <neal_liu at aspeedtech.com>
> > Signed-off-by: Neal Liu <neal_liu at aspeedtech.com>
> > Signed-off-by: Prasanth Kumar Padarthi <prasanth.padarthi10 at gmail.com>
> > ---
> > Changes since v3:
> > - Drop linux/mod_devicetable.h as requested by Uwe; platform_device.h
> > already provides the required declaration.
> >
> > v4 validation:
> > - ARM driver-object compilation with W=1 passed.
> > - Strict patch checkpatch: zero errors, zero checks, one new-file/
> > MAINTAINERS warning. The existing ASPEED matcher covers the driver.
> >
> > Earlier v3 validation, before this include-only change:
> > Tested on ASRock Rack B650D4U (AST2600 A3), Linux 7.4-rc1:
> > - ARM zImage build with W=1 completed.
> > - Repeated 12-byte reads and cross-pair/final-pair comparisons passed.
> > - Four concurrent readers completed 40 matching reads.
> > - Bytes 8172..8183 matched the saved ASPEED-derived reference fingerprint.
> > The kernel was RAM-booted with the working OpenBMC DTB and initramfs.
> >
> > Host-model checks passed 4096 protection combinations, 200 copying cases
> > and 24 timeout cases, including unlock-failure cleanup. Strict source
> > checkpatch passed. The existing ASPEED MAINTAINERS matcher covers the file.
> >
> > drivers/nvmem/Kconfig | 13 ++
> > drivers/nvmem/Makefile | 1 +
> > drivers/nvmem/nvmem-aspeed-otp.c | 298 +++++++++++++++++++++++++++++++
> > 3 files changed, 312 insertions(+)
> > create mode 100644 drivers/nvmem/nvmem-aspeed-otp.c
> >
> > diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
> > index d2f5bb2..65f6a88 100644
> > --- a/drivers/nvmem/Kconfig
> > +++ b/drivers/nvmem/Kconfig
> > @@ -164,6 +164,19 @@ config NVMEM_APPLE_SPMI
> > This driver can also be built as a module. If so, the module
> > will be called apple-nvmem-spmi.
> >
> > +config NVMEM_ASPEED_OTP
> > + tristate "ASPEED OTP support"
> > + depends on ARCH_ASPEED || COMPILE_TEST
> > + depends on HAS_IOMEM
> > + help
> > + Say Y here to enable read-only access to the OTP data region
> > + on ASPEED AST2600 BMCs through the NVMEM interface.
> > +
> > + This driver can also be built as a module. If so, the module
> > + will be called nvmem-aspeed-otp.
> > +
> > + If unsure, say N.
> > +
> > config NVMEM_BCM_OCOTP
> > tristate "Broadcom On-Chip OTP Controller support"
> > depends on ARCH_BCM_IPROC || COMPILE_TEST
> > diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
> > index 5e70bad..ca62940 100644
> > --- a/drivers/nvmem/Makefile
> > +++ b/drivers/nvmem/Makefile
> > @@ -23,6 +23,7 @@ obj-$(CONFIG_NVMEM_APPLE_EFUSES) += nvmem-apple-efuses.o
> > nvmem-apple-efuses-y := apple-efuses.o
> > obj-$(CONFIG_NVMEM_APPLE_SPMI) += apple_nvmem_spmi.o
> > apple_nvmem_spmi-y := apple-spmi-nvmem.o
> > +obj-$(CONFIG_NVMEM_ASPEED_OTP) += nvmem-aspeed-otp.o
>
> In my editor it looks like this needs another tab indent to align the
> assignment.
>
> > obj-$(CONFIG_NVMEM_BCM_OCOTP) += nvmem-bcm-ocotp.o
> > nvmem-bcm-ocotp-y := bcm-ocotp.o
> > obj-$(CONFIG_NVMEM_BRCM_NVRAM) += nvmem_brcm_nvram.o
> > diff --git a/drivers/nvmem/nvmem-aspeed-otp.c b/drivers/nvmem/nvmem-aspeed-otp.c
> > new file mode 100644
> > index 0000000..cbe05fc
> > --- /dev/null
> > +++ b/drivers/nvmem/nvmem-aspeed-otp.c
> > @@ -0,0 +1,298 @@
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Copyright (C) ASPEED Technology Inc.
> > + *
> > + * ASPEED AST2600 OTP Controller driver
> > + */
> > +
> > +#include <linux/bitfield.h>
> > +#include <linux/cleanup.h>
> > +#include <linux/io.h>
> > +#include <linux/iopoll.h>
> > +#include <linux/module.h>
> > +#include <linux/mutex.h>
> > +#include <linux/nvmem-provider.h>
> > +#include <linux/platform_device.h>
> > +#include <linux/unaligned.h>
> > +
> > +#define AST2600_OTP_PROTECT_KEY 0x00
> > +#define AST2600_OTP_UNLOCK_KEY 0x349fe38a
> > +#define AST2600_OTP_CMD 0x04
> > +#define AST2600_OTP_CMD_READ 0x23b1e361
> > +#define AST2600_OTP_CMD_WRITE_MODE 0x23b1e362
> > +#define AST2600_OTP_ADDR 0x10
> > +#define AST2600_OTP_STATUS 0x14
> > +#define AST2600_OTP_STATUS_DONE GENMASK(2, 1)
> > +#define AST2600_OTP_COMPARE_1 0x20
> > +#define AST2600_OTP_COMPARE_2 0x24
> > +#define AST2600_OTP_EXTRA_READ_PROTECT 0x54
> > +#define AST2600_OTP_EXTRA_READ_RANGE GENMASK(5, 0)
> > +
> > +/* OTP macro addresses selected through AST2600_OTP_ADDR, not MMIO offsets. */
> > +#define AST2600_OTP_CONF0_ADDR 0x800
> > +#define AST2600_OTP_CONF0_SEC_SIZE GENMASK(21, 16)
> > +#define AST2600_OTP_MR 0x1000
> > +#define AST2600_OTP_MRA 0x3000
> > +#define AST2600_OTP_MRB 0x5000
> > +
> > +/* Normal reads take less than 1 us; allow a generous bounded wait. */
> > +#define AST2600_OTP_TIMEOUT_US 100000
> > +#define AST2600_OTP_DATA_WORDS 0x800
> > +#define AST2600_OTP_SIZE (AST2600_OTP_DATA_WORDS * sizeof(u32))
> > +#define AST2600_OTP_SEC_SIZE_UNIT (32 * sizeof(u32))
> > +
> > +struct aspeed_otp {
> > + struct device *dev;
> > + void __iomem *base;
> > + /* Serialize the complete unlock, read and relock sequence. */
> > + struct mutex lock;
> > + struct nvmem_keepout secure_keepout;
> > + bool failed;
> > +};
> > +
> > +static int aspeed_otp_unlock(struct aspeed_otp *priv)
> > +{
> > + writel(AST2600_OTP_UNLOCK_KEY, priv->base + AST2600_OTP_PROTECT_KEY);
> > + if (readl(priv->base + AST2600_OTP_PROTECT_KEY) != 1)
> > + return -EIO;
> > +
> > + return 0;
> > +}
> > +
> > +static void aspeed_otp_lock(struct aspeed_otp *priv)
> > +{
> > + writel(0, priv->base + AST2600_OTP_PROTECT_KEY);
> > +}
> > +
> > +static int aspeed_otp_wait_complete(struct aspeed_otp *priv)
> > +{
> > + u32 status;
> > + int ret;
> > +
> > + /* Wait at least 1 us before the first sample of both idle flags. */
> > + ret = read_poll_timeout(readl, status,
>
> Perhaps readl_poll_timeout()?
>
> > + (status & AST2600_OTP_STATUS_DONE) ==
> > + AST2600_OTP_STATUS_DONE,
> > + 1, AST2600_OTP_TIMEOUT_US, true,
>
> aspeed_otp_wait_complete() is used in two contexts, the common case is
> after issuing an OTP command at which point we do want to wait a short
> period, but the other case (e.g. in aspeed_otp_read_data() and
> aspeed_otp_init_read_mode()) it seems to guard against commands issued
> outside the scope of the driver. I'm not sure we want to wait a short
> period in those circumstances.
>
> That said, the delay isn't significant...
>
> > + priv->base + AST2600_OTP_STATUS);
> > + if (ret) {
> > + /* Relocking SEC00 does not abort an outstanding command. */
> > + priv->failed = true;
> > + dev_err(priv->dev,
> > + "OTP completion timed out (status=%#x); further reads blocked\n",
> > + status);
> > + }
> > +
> > + return ret;
> > +}
> > +
> > +static int aspeed_otp_init_read_mode(struct aspeed_otp *priv)
> > +{
> > + static const u32 mode_addresses[] = {
> > + AST2600_OTP_MRA, AST2600_OTP_MRB, AST2600_OTP_MR,
> > + };
> > + int i, ret;
> > +
> > + ret = aspeed_otp_wait_complete(priv);
> > + if (ret)
> > + return ret;
> > +
> > + /* Earlier firmware may leave the macro in program mode; restore read mode. */
> > + for (i = 0; i < ARRAY_SIZE(mode_addresses); i++) {
> > + writel(mode_addresses[i], priv->base + AST2600_OTP_ADDR);
> > + writel(0, priv->base + AST2600_OTP_COMPARE_1);
> > + writel(AST2600_OTP_CMD_WRITE_MODE, priv->base + AST2600_OTP_CMD);
> > + ret = aspeed_otp_wait_complete(priv);
> > + if (ret)
> > + return ret;
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static int aspeed_otp_read_pair(struct aspeed_otp *priv,
> > + unsigned int aligned_word, u32 data[2])
> > +{
> > + int ret;
> > +
> > + /* The hardware reads two words starting at an even word index. */
>
> Perhaps we should enforce this and error out if the address is
> unaligned?
>
> if (aligned_word & 1)
> return -EINVAL;
>
> > + writel(aligned_word, priv->base + AST2600_OTP_ADDR);
> > + writel(AST2600_OTP_CMD_READ, priv->base + AST2600_OTP_CMD);
> > +
> > + ret = aspeed_otp_wait_complete(priv);
> > + if (ret)
> > + return ret;
> > +
> > + data[0] = readl(priv->base + AST2600_OTP_COMPARE_1);
> > + data[1] = readl(priv->base + AST2600_OTP_COMPARE_2);
> > +
> > + return 0;
> > +}
> > +
> > +static int aspeed_otp_init_unlocked(struct aspeed_otp *priv)
> > +{
> > + u32 data[2];
> > + int ret;
> > +
> > + ret = aspeed_otp_init_read_mode(priv);
> > + if (ret)
> > + return ret;
> > +
> > + ret = aspeed_otp_read_pair(priv, AST2600_OTP_CONF0_ADDR, data);
> > + if (ret)
> > + return ret;
> > +
> > + priv->secure_keepout.end =
> > + FIELD_GET(AST2600_OTP_CONF0_SEC_SIZE, data[0]) *
> > + AST2600_OTP_SEC_SIZE_UNIT;
> > +
> > + return 0;
> > +}
> > +
> > +static int aspeed_otp_init(struct aspeed_otp *priv)
> > +{
> > + int ret;
> > +
> > + /* The provider is not registered yet; there are no concurrent readers. */
>
> Given this, the mutex will be uncontended and there will be negligible
> impact on taking it :) You could then remove the comment, and annotate
> the relevant fields with __guarded_by(), and enable context analysis
> for the driver.
>
> > + ret = aspeed_otp_unlock(priv);
> > + if (!ret)
> > + ret = aspeed_otp_init_unlocked(priv);
>
> This is the only call-site for aspeed_otp_init_unlocked() - I'm not
> convinced there's much benefit to breaking it out, but, not necessary
> to change it either.
>
> > + aspeed_otp_lock(priv);
> > +
> > + return ret;
> > +}
> > +
> > +static int aspeed_otp_read_data(struct aspeed_otp *priv, unsigned int offset,
> > + void *val, size_t bytes)
> > +{
> > + unsigned int word_index = offset / sizeof(u32);
> > + u8 *buf = val;
> > + u32 data[2];
> > + int i, ret;
> > +
> > + ret = aspeed_otp_wait_complete(priv);
> > + if (ret)
> > + return ret;
> > +
> > + while (bytes) {
> > + ret = aspeed_otp_read_pair(priv, word_index & ~1U, data);
> > + if (ret)
> > + return ret;
> > +
> > + for (i = word_index & 1; i < ARRAY_SIZE(data) && bytes; i++) {
> > + put_unaligned_le32(data[i], buf);
> > + buf += sizeof(u32);
> > + bytes -= sizeof(u32);
>
> I would much prefer the validation of the value of bytes occur in this
> function rather than aspeed_otp_read(). Currently it requires we look
> across both implementation to ensure correctness.
>
> The same can be said for the alignment test of offset.
>
> The only downside of that is doing the tests under the mutex, but I
> don't see this a large penalty given the task of the driver, especially
> when we need valid arithmetic here to ensure memory safety.
>
> > + word_index++;
> > + }
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +static int aspeed_otp_read(void *context, unsigned int offset,
> > + void *val, size_t bytes)
> > +{
> > + struct aspeed_otp *priv = context;
> > + unsigned int secure_size;
> > + u32 extra_read;
> > + int ret;
> > +
> > + if (!bytes)
> > + return 0;
> > +
> > + if (offset % sizeof(u32) || bytes % sizeof(u32))
> > + return -EINVAL;
> > +
> > + if (offset >= AST2600_OTP_SIZE ||
> > + bytes > AST2600_OTP_SIZE - offset)
>
> I don't think it's necessary to wrap this.
>
> > + return -EINVAL;
> > +
> > + guard(mutex)(&priv->lock);
> > +
> > + if (priv->failed)
> > + return -EIO;
>
> Would we not want to test this prior to any operation?
> aspeed_otp_init_unlocked() performs multiple operations, and if we're
> admitting the possibility of failure, then subsequent operations
> shouldn't be tried.
>
> > +
> > + /* SEC54[5:0] can only grow until the ARM cores reset. */
> > + extra_read = readl(priv->base + AST2600_OTP_EXTRA_READ_PROTECT);
> > + secure_size = FIELD_GET(AST2600_OTP_EXTRA_READ_RANGE, extra_read) *
> > + AST2600_OTP_SEC_SIZE_UNIT;
> > + secure_size = max(priv->secure_keepout.end, secure_size);
> > + if (offset < secure_size)
> > + return -EACCES;
>
> This looks unnecessary? Nothing in the driver writes the read protect
> value, and the nvmem core handles keepout ranges itself:
>
> https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/nvmem/core.c?h=v7.3#n90
>
> Essentially, we should never hit this early-exit, and the calculations
> leading up to it are redundant.
>
> I don't think we should try to account for changes via e.g. /dev/mem...
>
> Even if we did hit it ... the OTP shouldn't return valid data for the
> read, right? That's the point? Can we not just issue the read and
> report whatever the OTP reports? Is -EACCESS necessary?
>
> > +
> > + ret = aspeed_otp_unlock(priv);
> > + if (!ret)
> > + ret = aspeed_otp_read_data(priv, offset, val, bytes);
> > + aspeed_otp_lock(priv);
> > + /* Complete the lock write before handing the mutex to another reader. */
> > + readl(priv->base + AST2600_OTP_PROTECT_KEY);
>
> Okay, but the level of caution there has me curious. Is this addressing
> an errata? The unlock write shouldn't overtake the lock write should
> it?
>
> > +
> > + return ret;
> > +}
> > +
> > +static int aspeed_otp_probe(struct platform_device *pdev)
> > +{
> > + struct nvmem_config config = {
> > + .name = "aspeed-otp",
> > + .id = NVMEM_DEVID_AUTO,
> > + .owner = THIS_MODULE,
> > + .type = NVMEM_TYPE_OTP,
> > + .read_only = true,
> > + .root_only = true,
> > + .word_size = sizeof(u32),
> > + .stride = sizeof(u32),
> > + .size = AST2600_OTP_SIZE,
> > + .reg_read = aspeed_otp_read,
> > + };
> > + struct aspeed_otp *priv;
> > + struct device *dev = &pdev->dev;
> > + int ret;
> > +
> > + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
>
> devm_kzalloc_obj() ?
>
> > + if (!priv)
> > + return -ENOMEM;
> > +
> > + priv->dev = dev;
> > + priv->base = devm_platform_ioremap_resource(pdev, 0);
> > + if (IS_ERR(priv->base))
> > + return PTR_ERR(priv->base);
> > +
> > + ret = devm_mutex_init(dev, &priv->lock);
> > + if (ret)
> > + return ret;
> > +
> > + ret = aspeed_otp_init(priv);
> > + if (ret)
> > + return dev_err_probe(dev, ret,
> > + "Failed to initialize OTP read mode and secure prefix\n");
> > +
> > + config.dev = dev;
> > + config.priv = priv;
> > + if (priv->secure_keepout.end) {
> > + config.keepout = &priv->secure_keepout;
> > + config.nkeepout = 1;
> > + }
> > +
> > + return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
> > +}
> > +
> > +static const struct of_device_id aspeed_otp_of_match[] = {
> > + { .compatible = "aspeed,ast2600-sbc" },
> > + { }
> > +};
> > +MODULE_DEVICE_TABLE(of, aspeed_otp_of_match);
> > +
> > +static struct platform_driver aspeed_otp_driver = {
> > + .probe = aspeed_otp_probe,
> > + .driver = {
> > + .name = "aspeed-otp",
> > + .of_match_table = aspeed_otp_of_match,
> > + },
> > +};
> > +module_platform_driver(aspeed_otp_driver);
> > +
> > +MODULE_AUTHOR("Neal Liu <neal_liu at aspeedtech.com>");
> > +MODULE_AUTHOR("Prasanth Kumar Padarthi <prasanth.padarthi10 at gmail.com>");
> > +MODULE_DESCRIPTION("ASPEED AST2600 read-only OTP NVMEM driver");
> > +MODULE_LICENSE("GPL");
More information about the linux-arm-kernel
mailing list