Skip to contentSkip to conversation

Add QEMU model for ASPEED OTP memory and integrate with SoC

PATCH In review Kane Chen via 3 participants 7 messages Jun 26, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover Add QEMU model for ASPEED OTP memory and integrate with SoC

From: Kane-Chen-AS <[email protected]>

This patch series introduces a QEMU model for the ASPEED OTP (One-Time Programmable) memory, along with its integration into the Secure Boot Controller (SBC) and supported SoC (AST2600).

The OTP model emulates a simple fuse array used for secure boot or device configuration, implemented with internal buffers; external file/device support not included in this version. It exposes an AddressSpace to support transaction-based access from controllers like the SBC.

This series includes:
- OTP memory device implementation
- SBC integration with command decoding (READ/PROG)
- Direct integration in AST2600 SoC without requiring user parameters

Any feedback or suggestions are appreciated!

---

Kane-Chen-AS (3):
hw/misc/aspeed_otp: Add ASPEED OTP memory device model hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC hw/arm: Integrate ASPEED OTP memory support into AST2600 SoCs

 include/hw/misc/aspeed_otpmem.h |  33 +++++++++
 include/hw/misc/aspeed_sbc.h    |   5 ++
 hw/arm/aspeed_ast2600.c         |   2 +-
 hw/misc/aspeed_otpmem.c         |  91 +++++++++++++++++++++++++
 hw/misc/aspeed_sbc.c            | 115 ++++++++++++++++++++++++++++++++
 hw/misc/meson.build             |   1 +
 hw/misc/trace-events            |   5 ++
 7 files changed, 251 insertions(+), 1 deletion(-)
 create mode 100644 include/hw/misc/aspeed_otpmem.h
 create mode 100644 hw/misc/aspeed_otpmem.c
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 hw/misc/aspeed_otp: Add ASPEED OTP memory device model

From: Kane-Chen-AS <[email protected]>

Introduce a QEMU device model for ASPEED's One-Time Programmable (OTP) memory.

This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.

The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.

---
 include/hw/misc/aspeed_otpmem.h | 33 ++++++++++++
 hw/misc/aspeed_otpmem.c         | 91 +++++++++++++++++++++++++++++++++
 hw/misc/meson.build             |  1 +
 3 files changed, 125 insertions(+)
 create mode 100644 include/hw/misc/aspeed_otpmem.h
 create mode 100644 hw/misc/aspeed_otpmem.c
include/hw/misc/aspeed_otpmem.h new file +33 -0
new file mode 100644
index 0000000000..64cd4d1a7c
@@ -0,0 +1,33 @@
+/*
+ *  ASPEED OTP (One-Time Programmable) memory
+ *
+ *  Copyright (C) 2025 Aspeed
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef ASPEED_OTPMEM_H
+#define ASPEED_OTPMEM_H
+
+#include "system/memory.h"
+#include "hw/block/block.h"
+#include "system/memory.h"
+#include "system/address-spaces.h"
+
+#define OTPMEM_SIZE 0x4000
+#define TYPE_ASPEED_OTPMEM "aspeed.otpmem"
+OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState, ASPEED_OTPMEM)
+
+typedef struct AspeedOTPMemState {
+    DeviceState parent_obj;
+
+    uint64_t size;
+
+    AddressSpace as;
+
+    MemoryRegion mmio;
+
+    uint8_t *storage;
+} AspeedOTPMemState;
+
+#endif /* ASPEED_OTPMEM_H */
hw/misc/aspeed_otpmem.c new file +91 -0

91 lines not on this page · raw message

hw/misc/meson.build +1 -0
index 6d47de482c..ed1eaaa2ad 100644
@@ -136,6 +136,7 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files(
   'aspeed_sbc.c',
   'aspeed_sdmc.c',
   'aspeed_xdma.c',
+  'aspeed_otpmem.c',
   'aspeed_peci.c',
   'aspeed_sli.c'))
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v1 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model

Quoted 9 lines · Kane Chen

From: Kane-Chen-AS <[email protected]>

Introduce a QEMU device model for ASPEED's One-Time Programmable (OTP) memory.

This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.

Quoted 8 lines

The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.

---
  include/hw/misc/aspeed_otpmem.h | 33 ++++++++++++
  hw/misc/aspeed_otpmem.c         | 91 +++++++++++++++++++++++++++++++++

Peter suggested to move these files under hw/nvram/ with the other OTP models.

Please consider renaming to :

include/hw/nvram/aspeed_otp.h
hw/nvram/aspeed_otp.c

Quoted 17 lines
  3 files changed, 125 insertions(+)

create mode 100644 include/hw/misc/aspeed_otpmem.h create mode 100644 hw/misc/aspeed_otpmem.c

include/hw/misc/aspeed_otpmem.h new file +7 -0
new file mode 100644
index 0000000000..64cd4d1a7c
@@ -0,0 +1,33 @@
+/*
+ *  ASPEED OTP (One-Time Programmable) memory
+ *
+ *  Copyright (C) 2025 Aspeed
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later
+ */

include/hw/misc/aspeed_otpmem.h line 17

Quoted 10 lines
include/hw/misc/aspeed_otpmem.h new file +10 -0
+
+#ifndef ASPEED_OTPMEM_H
+#define ASPEED_OTPMEM_H
+
+#include "system/memory.h"
+#include "hw/block/block.h"
+#include "system/memory.h"
+#include "system/address-spaces.h"
+
+#define OTPMEM_SIZE 0x4000

This define doesn't seem useful. May be instead, set the OTP object "size" from the parent model at realize time.

Quoted 125 lines
include/hw/misc/aspeed_otpmem.h new file +16 -0
+#define TYPE_ASPEED_OTPMEM "aspeed.otpmem"
+OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState, ASPEED_OTPMEM)
+
+typedef struct AspeedOTPMemState {
+    DeviceState parent_obj;
+
+    uint64_t size;
+
+    AddressSpace as;
+
+    MemoryRegion mmio;
+
+    uint8_t *storage;
+} AspeedOTPMemState;
+
+#endif /* ASPEED_OTPMEM_H */
hw/misc/aspeed_otpmem.c new file +91 -0
new file mode 100644
index 0000000000..a77d4186f8
@@ -0,0 +1,91 @@
+/*
+ *  ASPEED OTP (One-Time Programmable) memory
+ *
+ *  Copyright (C) 2025 Aspeed
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "trace.h"
+#include "system/block-backend-global-state.h"
+#include "system/block-backend-io.h"
+#include "hw/misc/aspeed_otpmem.h"
+
+static uint64_t aspeed_otpmem_read(void *opaque, hwaddr offset, unsigned size)
+{
+    AspeedOTPMemState *s = opaque;
+    uint64_t val = 0;
+
+    memcpy(&val, s->storage + offset, size);
+
+    return val;
+}
+
+static void aspeed_otpmem_write(void *opaque, hwaddr offset,
+                                uint64_t val, unsigned size)
+{
+    AspeedOTPMemState *s = opaque;
+
+    memcpy(s->storage + offset, &val, size);
+}
+
+static void aspeed_otpmem_init_storage(uint8_t *storage, uint64_t size)
+{
+    uint32_t *p;
+    int i, num;
+
+    num = size / sizeof(uint32_t);
+    p = (uint32_t *)storage;
+    for (i = 0; i < num; i++) {
+        p[i] = (i % 2 == 0) ? 0x00000000 : 0xFFFFFFFF;
+    }
+}
+
+static const MemoryRegionOps aspeed_otpmem_ops = {
+    .read = aspeed_otpmem_read,
+    .write = aspeed_otpmem_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid.min_access_size = 1,
+    .valid.max_access_size = 4,
+};
+
+static void aspeed_otpmem_realize(DeviceState *dev, Error **errp)
+{
+    AspeedOTPMemState *s = ASPEED_OTPMEM(dev);
+
+    s->storage = g_malloc(s->size);
+
+    aspeed_otpmem_init_storage(s->storage, s->size);
+
+    memory_region_init_io(&s->mmio, OBJECT(dev), &aspeed_otpmem_ops,
+                          s, "aspeed.otpmem", s->size);
+    address_space_init(&s->as, &s->mmio, NULL);
+}
+
+static const Property aspeed_otpmem_properties[] = {
+    DEFINE_PROP_UINT64("size", AspeedOTPMemState, size, OTPMEM_SIZE),
+};
+
+static void aspeed_otpmem_class_init(ObjectClass *klass, const void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    dc->realize = aspeed_otpmem_realize;
+    device_class_set_props(dc, aspeed_otpmem_properties);
+}
+
+static const TypeInfo aspeed_otpmem_info = {
+    .name          = TYPE_ASPEED_OTPMEM,
+    .parent        = TYPE_DEVICE,
+    .instance_size = sizeof(AspeedOTPMemState),
+    .class_init    = aspeed_otpmem_class_init,
+};
+
+static void aspeed_otpmem_register_types(void)
+{
+    type_register_static(&aspeed_otpmem_info);
+}
+
+type_init(aspeed_otpmem_register_types)
hw/misc/meson.build +1 -0
index 6d47de482c..ed1eaaa2ad 100644
@@ -136,6 +136,7 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files(
    'aspeed_sbc.c',
    'aspeed_sdmc.c',
    'aspeed_xdma.c',
+  'aspeed_otpmem.c',
    'aspeed_peci.c',
    'aspeed_sli.c'))
 
#3 lore raw

RE: [PATCH v1 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model

Hi Cédric,

Thanks for your feedback. I will move the OTP implementation to hw/nvram/ as suggested, and adjust the related code accordingly.

Best Regards,
Kane

Quoted 195 lines

-----Original Message-----
From: Cédric Le Goater <[email protected]>
Sent: Thursday, June 26, 2025 4:23 PM
To: Kane Chen <[email protected]>; Peter Maydell <[email protected]>; Steven Lee <[email protected]>; Troy Lee <[email protected]>; Jamin Lin <[email protected]>; Andrew Jeffery <[email protected]>; Joel Stanley <[email protected]>; open list:ASPEED BMCs <[email protected]>; open list:All patches CC here <[email protected]>
Cc: Troy Lee <[email protected]>
Subject: Re: [PATCH v1 1/3] hw/misc/aspeed_otp: Add ASPEED OTP memory device model

Quoted 3 lines · Kane Chen

From: Kane-Chen-AS <[email protected]>

Introduce a QEMU device model for ASPEED's One-Time Programmable

(OTP)

Quoted 6 lines

memory.

This model simulates a word-addressable OTP region used for secure fuse storage. The OTP memory can operate with an internal memory buffer.

Quoted 8 lines

The OTP model provides a memory-like interface through a dedicated AddressSpace, allowing other device models (e.g., SBC) to issue transactions as if accessing a memory-mapped region.

---
  include/hw/misc/aspeed_otpmem.h | 33 ++++++++++++
  hw/misc/aspeed_otpmem.c         | 91

+++++++++++++++++++++++++++++++++

Peter suggested to move these files under hw/nvram/ with the other OTP models.

Please consider renaming to :

include/hw/nvram/aspeed_otp.h
hw/nvram/aspeed_otp.c

Quoted 16 lines
  3 files changed, 125 insertions(+)

create mode 100644 include/hw/misc/aspeed_otpmem.h create mode 100644 hw/misc/aspeed_otpmem.c

(quoted diff) +0 -0

b/include/hw/misc/aspeed_otpmem.h new file mode 100644 index 0000000000..64cd4d1a7c

include/hw/misc/aspeed_otpmem.h new file +6 -0
@@ -0,0 +1,33 @@
+/*
+ *  ASPEED OTP (One-Time Programmable) memory
+ *
+ *  Copyright (C) 2025 Aspeed
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later  */

include/hw/misc/aspeed_otpmem.h line 16

Quoted 10 lines
include/hw/misc/aspeed_otpmem.h new file +10 -0
+
+#ifndef ASPEED_OTPMEM_H
+#define ASPEED_OTPMEM_H
+
+#include "system/memory.h"
+#include "hw/block/block.h"
+#include "system/memory.h"
+#include "system/address-spaces.h"
+
+#define OTPMEM_SIZE 0x4000

This define doesn't seem useful. May be instead, set the OTP object "size" from the parent model at realize time.

include/hw/misc/aspeed_otpmem.h line 18

Quoted 2 lines
include/hw/misc/aspeed_otpmem.h new file +2 -0
+#define TYPE_ASPEED_OTPMEM "aspeed.otpmem"
+OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPMemState,

ASPEED_OTPMEM)

Quoted 68 lines
include/hw/misc/aspeed_otpmem.h new file +14 -0
+
+typedef struct AspeedOTPMemState {
+    DeviceState parent_obj;
+
+    uint64_t size;
+
+    AddressSpace as;
+
+    MemoryRegion mmio;
+
+    uint8_t *storage;
+} AspeedOTPMemState;
+
+#endif /* ASPEED_OTPMEM_H */
hw/misc/aspeed_otpmem.c new renamed from hw/misc/aspeed_otpmem.c +0 -0

file mode 100644 index 0000000000..a77d4186f8

hw/misc/aspeed_otpmem.c new file +49 -0
@@ -0,0 +1,91 @@
+/*
+ *  ASPEED OTP (One-Time Programmable) memory
+ *
+ *  Copyright (C) 2025 Aspeed
+ *
+ *  SPDX-License-Identifier: GPL-2.0-or-later  */
+
+#include "qemu/osdep.h"
+#include "qemu/log.h"
+#include "qapi/error.h"
+#include "trace.h"
+#include "system/block-backend-global-state.h"
+#include "system/block-backend-io.h"
+#include "hw/misc/aspeed_otpmem.h"
+
+static uint64_t aspeed_otpmem_read(void *opaque, hwaddr offset,
+unsigned size) {
+    AspeedOTPMemState *s = opaque;
+    uint64_t val = 0;
+
+    memcpy(&val, s->storage + offset, size);
+
+    return val;
+}
+
+static void aspeed_otpmem_write(void *opaque, hwaddr offset,
+                                uint64_t val, unsigned size) {
+    AspeedOTPMemState *s = opaque;
+
+    memcpy(s->storage + offset, &val, size); }
+
+static void aspeed_otpmem_init_storage(uint8_t *storage, uint64_t
+size) {
+    uint32_t *p;
+    int i, num;
+
+    num = size / sizeof(uint32_t);
+    p = (uint32_t *)storage;
+    for (i = 0; i < num; i++) {
+        p[i] = (i % 2 == 0) ? 0x00000000 : 0xFFFFFFFF;
+    }
+}
+
+static const MemoryRegionOps aspeed_otpmem_ops = {
+    .read = aspeed_otpmem_read,
+    .write = aspeed_otpmem_write,
+    .endianness = DEVICE_LITTLE_ENDIAN,
+    .valid.min_access_size = 1,
+    .valid.max_access_size = 4,

hw/misc/aspeed_otpmem.c line 59

Quoted 10 lines
hw/misc/aspeed_otpmem.c new file +10 -0
+};
+
+static void aspeed_otpmem_realize(DeviceState *dev, Error **errp) {
+    AspeedOTPMemState *s = ASPEED_OTPMEM(dev);
+
+    s->storage = g_malloc(s->size);
+
+    aspeed_otpmem_init_storage(s->storage, s->size);
+
+    memory_region_init_io(&s->mmio, OBJECT(dev),

&aspeed_otpmem_ops,

hw/misc/aspeed_otpmem.c

Quoted 5 lines
hw/misc/aspeed_otpmem.c new file +5 -0
+                          s, "aspeed.otpmem", s->size);
+    address_space_init(&s->as, &s->mmio, NULL); }
+
+static const Property aspeed_otpmem_properties[] = {
+    DEFINE_PROP_UINT64("size", AspeedOTPMemState, size,

OTPMEM_SIZE),

hw/misc/meson.build system_ss.add(when: 'CONFIG_ASPEED_SOC',

Quoted 26 lines
hw/misc/aspeed_otpmem.c new file +21 -0
+};
+
+static void aspeed_otpmem_class_init(ObjectClass *klass, const void
+*data) {
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    dc->realize = aspeed_otpmem_realize;
+    device_class_set_props(dc, aspeed_otpmem_properties); }
+
+static const TypeInfo aspeed_otpmem_info = {
+    .name          = TYPE_ASPEED_OTPMEM,
+    .parent        = TYPE_DEVICE,
+    .instance_size = sizeof(AspeedOTPMemState),
+    .class_init    = aspeed_otpmem_class_init,
+};
+
+static void aspeed_otpmem_register_types(void)
+{
+    type_register_static(&aspeed_otpmem_info);
+}
+
+type_init(aspeed_otpmem_register_types)
hw/misc/meson.build index renamed from hw/misc/meson.build +0 -0

6d47de482c..ed1eaaa2ad 100644

hw/misc/meson.build +0 -0
@@ -136,6 +136,7 @@ system_ss.add(when: 'CONFIG_ASPEED_SOC',

if_true: files(

Quoted 7 lines
hw/misc/meson.build +1 -0
    'aspeed_sbc.c',
    'aspeed_sdmc.c',
    'aspeed_xdma.c',
+  'aspeed_otpmem.c',
    'aspeed_peci.c',
    'aspeed_sli.c'))
 
#4 lore raw

PATCH 2/3 hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC

In reply to Kane Chen via

From: Kane-Chen-AS <[email protected]>

This patch connects the aspeed.otpmem device to the ASPEED Secure Boot Controller (SBC) model. It implements OTP memory access via the SBC's command interface and enables emulation of secure fuse programming flows.

The following OTP commands are supported:
- READ: reads a 32-bit word from OTP memory into internal registers
- PROG: programs a 32-bit word value to the specified OTP address

Trace events are added to observe read/program operations and command handling flow.

---
 include/hw/misc/aspeed_sbc.h |   5 ++
 hw/misc/aspeed_sbc.c         | 113 +++++++++++++++++++++++++++++++++++
 hw/misc/trace-events         |   5 ++
 3 files changed, 123 insertions(+)
include/hw/misc/aspeed_sbc.h +5 -0
index 405e6782b9..667d6088c0 100644
@@ -10,6 +10,7 @@
 #define ASPEED_SBC_H
 
 #include "hw/sysbus.h"
+#include "hw/misc/aspeed_otpmem.h"
 
 #define TYPE_ASPEED_SBC "aspeed.sbc"
 #define TYPE_ASPEED_AST2600_SBC TYPE_ASPEED_SBC "-ast2600"
@@ -36,10 +37,14 @@ struct AspeedSBCState {
     MemoryRegion iomem;
 
     uint32_t regs[ASPEED_SBC_NR_REGS];
+
+    AspeedOTPMemState otpmem;
 };
 
 struct AspeedSBCClass {
     SysBusDeviceClass parent_class;
+
+    bool has_otpmem;
 };
 
 #endif /* ASPEED_SBC_H */
hw/misc/aspeed_sbc.c +113 -0

152 lines not on this page · raw message

hw/misc/trace-events +5 -0
index e3f64c0ff6..6f216a77fd 100644
@@ -90,6 +90,11 @@ slavio_sysctrl_mem_readl(uint32_t ret) "Read system control 0x%08x"
 slavio_led_mem_writew(uint32_t val) "Write diagnostic LED 0x%04x"
 slavio_led_mem_readw(uint32_t ret) "Read diagnostic LED 0x%04x"
 
+# aspped_sbc.c
+aspeed_sbc_handle_cmd(uint32_t cmd, uint32_t addr, bool ret) "Handling command 0x%" PRIx32 " for OTP addr 0x%" PRIx32 " Result: %d"
+aspeed_sbc_otp_read(uint32_t addr, uint32_t value) "OTP Memory read: addr 0x%" PRIx32 " value 0x%" PRIx32
+aspeed_sbc_otp_prog(uint32_t addr, uint32_t value) "OTP Memory write: addr 0x%" PRIx32 " value 0x%" PRIx32
+
 # aspeed_scu.c
 aspeed_scu_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32
 aspeed_scu_read(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v1 2/3] hw/misc/aspeed_sbc: Connect ASPEED OTP memory device to SBC

Quoted 82 lines · Kane Chen

From: Kane-Chen-AS <[email protected]>

This patch connects the aspeed.otpmem device to the ASPEED Secure Boot Controller (SBC) model. It implements OTP memory access via the SBC's command interface and enables emulation of secure fuse programming flows.

The following OTP commands are supported:
- READ: reads a 32-bit word from OTP memory into internal registers
- PROG: programs a 32-bit word value to the specified OTP address

Trace events are added to observe read/program operations and command handling flow.

---
  include/hw/misc/aspeed_sbc.h |   5 ++
  hw/misc/aspeed_sbc.c         | 113 +++++++++++++++++++++++++++++++++++
  hw/misc/trace-events         |   5 ++
  3 files changed, 123 insertions(+)
include/hw/misc/aspeed_sbc.h +5 -0
index 405e6782b9..667d6088c0 100644
@@ -10,6 +10,7 @@
  #define ASPEED_SBC_H
 
  #include "hw/sysbus.h"
+#include "hw/misc/aspeed_otpmem.h"
 
  #define TYPE_ASPEED_SBC "aspeed.sbc"
  #define TYPE_ASPEED_AST2600_SBC TYPE_ASPEED_SBC "-ast2600"
@@ -36,10 +37,14 @@ struct AspeedSBCState {
      MemoryRegion iomem;
 
      uint32_t regs[ASPEED_SBC_NR_REGS];
+
+    AspeedOTPMemState otpmem;
  };
 
  struct AspeedSBCClass {
      SysBusDeviceClass parent_class;
+
+    bool has_otpmem;
  };
 
  #endif /* ASPEED_SBC_H */
hw/misc/aspeed_sbc.c +10 -0
index a7d101ba71..5cbf55c216 100644
@@ -15,9 +15,13 @@
  #include "hw/misc/aspeed_sbc.h"
  #include "qapi/error.h"
  #include "migration/vmstate.h"
+#include "trace.h"
 
  #define R_PROT          (0x000 / 4)
+#define R_CMD           (0x004 / 4)
+#define R_ADDR          (0x010 / 4)
  #define R_STATUS        (0x014 / 4)
+#define R_CAMP1         (0x020 / 4)
  #define R_QSR           (0x040 / 4)
 
  /* R_STATUS */
@@ -41,6 +45,10 @@
  #define QSR_RSA_MASK           (0x3 << 12)
  #define QSR_HASH_MASK          (0x3 << 10)
 
+/* OTP command */
+#define SBC_OTP_CMD_READ 0x23b1e361
+#define SBC_OTP_CMD_PROG 0x23b1e364
+
  static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size)
  {
      AspeedSBCState *s = ASPEED_SBC(opaque);
@@ -57,6 +65,87 @@ static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size)
      return s->regs[addr];
  }
 
+static bool aspeed_sbc_otpmem_read(AspeedSBCState *s,
+                                   uint32_t otp_addr, Error **errp)

hw/misc/aspeed_sbc.c static uint64_t aspeed_sbc_read(void *opaque, hwaddr addr, unsigned int size) · line 79

Quoted 10 lines
hw/misc/aspeed_sbc.c +10 -0
+{
+    MemTxResult ret;
+    AspeedOTPMemState *otpmem = &s->otpmem;
+    uint32_t value, otp_offset;
+
+    otp_offset = otp_addr << 2;
+    ret = address_space_read(&otpmem->as, otp_offset, MEMTXATTRS_UNSPECIFIED,
+                             &value, sizeof(value));
+    if (ret != MEMTX_OK) {
+        error_setg(errp, "Failed to read OTP memory, addr = %x\n", otp_addr);

Since the error can not be propagated, doing directly here a :

qemu_log_mask(LOG_GUEST_ERROR, ...)

seems cleaner. Same for address_space_write()

Quoted 87 lines
hw/misc/aspeed_sbc.c +73 -0
+        return false;
+    }
+    s->regs[R_CAMP1] = value;
+    trace_aspeed_sbc_otp_read(otp_addr, value);
+
+    return true;
+}
+
+static bool aspeed_sbc_otpmem_prog(AspeedSBCState *s,
+                                   uint32_t otp_addr, Error **errp)
+{
+    MemTxResult ret;
+    AspeedOTPMemState *otpmem = &s->otpmem;
+    uint32_t value = s->regs[R_CAMP1];;
+
+    ret = address_space_write(&otpmem->as, otp_addr, MEMTXATTRS_UNSPECIFIED,
+                        &value, sizeof(value));
+    if (ret != MEMTX_OK) {
+        error_setg(errp, "Failed to write OTP memory, addr = %x\n", otp_addr);
+        return false;
+    }
+
+    trace_aspeed_sbc_otp_prog(otp_addr, value);
+
+    return true;
+}
+
+static void aspeed_sbc_handle_command(void *opaque, uint32_t cmd)
+{
+    AspeedSBCState *s = ASPEED_SBC(opaque);
+    AspeedSBCClass *sc = ASPEED_SBC_GET_CLASS(opaque);
+    Error *local_err = NULL;
+    bool ret = false;
+    uint32_t otp_addr;
+
+    if (!sc->has_otpmem) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: OTP memory is not supported\n",
+                      __func__);
+        return;
+    }
+
+    s->regs[R_STATUS] &= ~(OTP_MEM_IDLE | OTP_IDLE);
+    otp_addr = s->regs[R_ADDR];
+
+    switch (cmd) {
+    case SBC_OTP_CMD_READ:
+        ret = aspeed_sbc_otpmem_read(s, otp_addr, &local_err);
+        break;
+    case SBC_OTP_CMD_PROG:
+        ret = aspeed_sbc_otpmem_prog(s, otp_addr, &local_err);
+        break;
+    default:
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: Unknown command 0x%x\n",
+                      __func__, cmd);
+        break;
+    }
+
+    trace_aspeed_sbc_handle_cmd(cmd, otp_addr, ret);
+    if (ret == false && local_err) {
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "%s: %s\n",
+                      __func__, error_get_pretty(local_err));
+        error_free(local_err);
+    }
+    s->regs[R_STATUS] |= (OTP_MEM_IDLE | OTP_IDLE);
+}
+
  static void aspeed_sbc_write(void *opaque, hwaddr addr, uint64_t data,
                                unsigned int size)
  {
@@ -78,6 +167,9 @@ static void aspeed_sbc_write(void *opaque, hwaddr addr, uint64_t data,
                        "%s: write to read only register 0x%" HWADDR_PRIx "\n",
                        __func__, addr << 2);
          return;
+    case R_CMD:
+        aspeed_sbc_handle_command(opaque, data);
+        return;
      default:
          break;
      }
@@ -115,6 +207,26 @@ static void aspeed_sbc_reset(DeviceState *dev)
      s->regs[R_QSR] = s->signing_settings;
  }
 
+static void aspeed_sbc_instance_init(Object *obj)

hw/misc/aspeed_sbc.c static void aspeed_sbc_reset(DeviceState *dev) · line 220

Quoted 10 lines
hw/misc/aspeed_sbc.c +10 -0
+{
+    AspeedSBCClass *sc = ASPEED_SBC_GET_CLASS(obj);
+    AspeedSBCState *s = ASPEED_SBC(obj);
+    Error *local_errp = NULL;
+    bool ret;
+
+    if (sc->has_otpmem) {
+        object_initialize_child(OBJECT(s), "otp", &s->otpmem,
+                                TYPE_ASPEED_OTPMEM);
+        ret = qdev_realize(DEVICE(&s->otpmem), NULL, &local_errp);

The realize part should be done under aspeed_sbc_realize(). Before you could set the OTP object size too. As you wish.

Thanks,

C.

Quoted 36 lines
hw/misc/aspeed_sbc.c +10 -0
+        if (!ret && local_errp) {
+            qemu_log_mask(LOG_GUEST_ERROR,
+                          "%s: %s\n",
+                          __func__, error_get_pretty(local_errp));
+            error_free(local_errp);
+        }
+    }
+}
+
  static void aspeed_sbc_realize(DeviceState *dev, Error **errp)
  {
      AspeedSBCState *s = ASPEED_SBC(dev);
@@ -155,6 +267,7 @@ static const TypeInfo aspeed_sbc_info = {
      .name = TYPE_ASPEED_SBC,
      .parent = TYPE_SYS_BUS_DEVICE,
      .instance_size = sizeof(AspeedSBCState),
+    .instance_init = aspeed_sbc_instance_init,
      .class_init = aspeed_sbc_class_init,
      .class_size = sizeof(AspeedSBCClass)
  };
hw/misc/trace-events +5 -0
index e3f64c0ff6..6f216a77fd 100644
@@ -90,6 +90,11 @@ slavio_sysctrl_mem_readl(uint32_t ret) "Read system control 0x%08x"
  slavio_led_mem_writew(uint32_t val) "Write diagnostic LED 0x%04x"
  slavio_led_mem_readw(uint32_t ret) "Read diagnostic LED 0x%04x"
 
+# aspped_sbc.c
+aspeed_sbc_handle_cmd(uint32_t cmd, uint32_t addr, bool ret) "Handling command 0x%" PRIx32 " for OTP addr 0x%" PRIx32 " Result: %d"
+aspeed_sbc_otp_read(uint32_t addr, uint32_t value) "OTP Memory read: addr 0x%" PRIx32 " value 0x%" PRIx32
+aspeed_sbc_otp_prog(uint32_t addr, uint32_t value) "OTP Memory write: addr 0x%" PRIx32 " value 0x%" PRIx32
+
  # aspeed_scu.c
  aspeed_scu_write(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32
  aspeed_scu_read(uint64_t offset, unsigned size, uint32_t data) "To 0x%" PRIx64 " of size %u: 0x%" PRIx32
#6 lore raw

PATCH 3/3 hw/arm: Integrate ASPEED OTP memory support into AST2600 SoCs

In reply to Kane Chen via

From: Kane-Chen-AS <[email protected]>

The has_otpmem attribute is enabled in the SBC subclasses for AST2600 to control the presence of OTP support per SoC type.

---
 hw/arm/aspeed_ast2600.c | 2 +-
 hw/misc/aspeed_sbc.c    | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
hw/arm/aspeed_ast2600.c +1 -1
index d12707f0ab..59ffd41a4a 100644
@@ -261,7 +261,7 @@ static void aspeed_soc_ast2600_init(Object *obj)
 
     object_initialize_child(obj, "i3c", &s->i3c, TYPE_ASPEED_I3C);
 
-    object_initialize_child(obj, "sbc", &s->sbc, TYPE_ASPEED_SBC);
+    object_initialize_child(obj, "sbc", &s->sbc, TYPE_ASPEED_AST2600_SBC);
 
     object_initialize_child(obj, "iomem", &s->iomem, TYPE_UNIMPLEMENTED_DEVICE);
     object_initialize_child(obj, "video", &s->video, TYPE_UNIMPLEMENTED_DEVICE);
hw/misc/aspeed_sbc.c +2 -0
index 5cbf55c216..717cad92dc 100644
@@ -275,8 +275,10 @@ static const TypeInfo aspeed_sbc_info = {
 static void aspeed_ast2600_sbc_class_init(ObjectClass *klass, const void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
+    AspeedSBCClass *sc = ASPEED_SBC_CLASS(klass);
 
     dc->desc = "AST2600 Secure Boot Controller";
+    sc->has_otpmem = true;
 }
 
 static const TypeInfo aspeed_ast2600_sbc_info = {
Signature · 1 line
2.43.0