hw/nvram/aspeed_otp: Add ASPEED OTP memory device model
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. Signed-off-by: Kane-Chen-AS <kane_chen@aspeedtech.com> Reviewed-by: Cédric Le Goater <clg@redhat.com> Link: https://lore.kernel.org/qemu-devel/20250812094011.2617526-2-kane_chen@aspeedtech.com Signed-off-by: Cédric Le Goater <clg@redhat.com>
This commit is contained in:
parent
4975b64efb
commit
688a3dae78
3 changed files with 136 additions and 0 deletions
99
hw/nvram/aspeed_otp.c
Normal file
99
hw/nvram/aspeed_otp.c
Normal file
|
|
@ -0,0 +1,99 @@
|
|||
/*
|
||||
* 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 "system/block-backend.h"
|
||||
#include "hw/qdev-properties.h"
|
||||
#include "hw/nvram/aspeed_otp.h"
|
||||
|
||||
static uint64_t aspeed_otp_read(void *opaque, hwaddr offset, unsigned size)
|
||||
{
|
||||
AspeedOTPState *s = opaque;
|
||||
uint64_t val = 0;
|
||||
|
||||
memcpy(&val, s->storage + offset, size);
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
static void aspeed_otp_write(void *opaque, hwaddr otp_addr,
|
||||
uint64_t val, unsigned size)
|
||||
{
|
||||
AspeedOTPState *s = opaque;
|
||||
|
||||
memcpy(s->storage + otp_addr, &val, size);
|
||||
}
|
||||
|
||||
static bool aspeed_otp_init_storage(AspeedOTPState *s, Error **errp)
|
||||
{
|
||||
uint32_t *p;
|
||||
int i, num;
|
||||
|
||||
num = s->size / sizeof(uint32_t);
|
||||
p = (uint32_t *)s->storage;
|
||||
for (i = 0; i < num; i++) {
|
||||
p[i] = (i % 2 == 0) ? 0x00000000 : 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static const MemoryRegionOps aspeed_otp_ops = {
|
||||
.read = aspeed_otp_read,
|
||||
.write = aspeed_otp_write,
|
||||
.endianness = DEVICE_LITTLE_ENDIAN,
|
||||
.valid.min_access_size = 1,
|
||||
.valid.max_access_size = 4,
|
||||
};
|
||||
|
||||
static void aspeed_otp_realize(DeviceState *dev, Error **errp)
|
||||
{
|
||||
AspeedOTPState *s = ASPEED_OTP(dev);
|
||||
|
||||
if (s->size == 0) {
|
||||
error_setg(errp, "aspeed.otp: 'size' property must be set");
|
||||
return;
|
||||
}
|
||||
|
||||
s->storage = blk_blockalign(s->blk, s->size);
|
||||
|
||||
if (!aspeed_otp_init_storage(s, errp)) {
|
||||
return;
|
||||
}
|
||||
|
||||
memory_region_init_io(&s->mmio, OBJECT(dev), &aspeed_otp_ops,
|
||||
s, "aspeed.otp", s->size);
|
||||
address_space_init(&s->as, &s->mmio, NULL);
|
||||
}
|
||||
|
||||
static const Property aspeed_otp_properties[] = {
|
||||
DEFINE_PROP_UINT64("size", AspeedOTPState, size, 0),
|
||||
};
|
||||
|
||||
static void aspeed_otp_class_init(ObjectClass *klass, const void *data)
|
||||
{
|
||||
DeviceClass *dc = DEVICE_CLASS(klass);
|
||||
dc->realize = aspeed_otp_realize;
|
||||
device_class_set_props(dc, aspeed_otp_properties);
|
||||
}
|
||||
|
||||
static const TypeInfo aspeed_otp_info = {
|
||||
.name = TYPE_ASPEED_OTP,
|
||||
.parent = TYPE_DEVICE,
|
||||
.instance_size = sizeof(AspeedOTPState),
|
||||
.class_init = aspeed_otp_class_init,
|
||||
};
|
||||
|
||||
static void aspeed_otp_register_types(void)
|
||||
{
|
||||
type_register_static(&aspeed_otp_info);
|
||||
}
|
||||
|
||||
type_init(aspeed_otp_register_types)
|
||||
|
|
@ -19,3 +19,7 @@ system_ss.add(when: 'CONFIG_XLNX_BBRAM', if_true: files('xlnx-bbram.c'))
|
|||
|
||||
specific_ss.add(when: 'CONFIG_PSERIES', if_true: files('spapr_nvram.c'))
|
||||
specific_ss.add(when: 'CONFIG_ACPI', if_true: files('fw_cfg-acpi.c'))
|
||||
|
||||
system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files(
|
||||
'aspeed_otp.c',
|
||||
))
|
||||
33
include/hw/nvram/aspeed_otp.h
Normal file
33
include/hw/nvram/aspeed_otp.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* ASPEED OTP (One-Time Programmable) memory
|
||||
*
|
||||
* Copyright (C) 2025 Aspeed
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-2.0-or-later
|
||||
*/
|
||||
|
||||
#ifndef ASPEED_OTP_H
|
||||
#define ASPEED_OTP_H
|
||||
|
||||
#include "system/memory.h"
|
||||
#include "hw/block/block.h"
|
||||
#include "system/address-spaces.h"
|
||||
|
||||
#define TYPE_ASPEED_OTP "aspeed-otp"
|
||||
OBJECT_DECLARE_SIMPLE_TYPE(AspeedOTPState, ASPEED_OTP)
|
||||
|
||||
typedef struct AspeedOTPState {
|
||||
DeviceState parent_obj;
|
||||
|
||||
BlockBackend *blk;
|
||||
|
||||
uint64_t size;
|
||||
|
||||
AddressSpace as;
|
||||
|
||||
MemoryRegion mmio;
|
||||
|
||||
uint8_t *storage;
|
||||
} AspeedOTPState;
|
||||
|
||||
#endif /* ASPEED_OTP_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue