diff --git a/hw/misc/esp32s3_rng.c b/hw/misc/esp32s3_rng.c new file mode 100644 index 0000000000..c4fed3d816 --- /dev/null +++ b/hw/misc/esp32s3_rng.c @@ -0,0 +1,56 @@ +/* + * ESP32S3 Random Number Generator peripheral + * + * Copyright (c) 2019-2024 Espressif Systems (Shanghai) Co. Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 or + * (at your option) any later version. + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qemu/error-report.h" +#include "qemu/guest-random.h" +#include "qapi/error.h" +#include "hw/hw.h" +#include "hw/sysbus.h" +#include "hw/misc/esp32s3_rng.h" + + +static uint64_t esp32s3_rng_read(void *opaque, hwaddr addr, unsigned int size) +{ + uint32_t r = 0; + qemu_guest_getrandom_nofail(&r, sizeof(r)); + return r; +} + +static const MemoryRegionOps esp32s3_rng_ops = { + .read = esp32s3_rng_read, + .endianness = DEVICE_LITTLE_ENDIAN, +}; + +static void esp32s3_rng_init(Object *obj) +{ + Esp32s3RngState *s = ESP32S3_RNG(obj); + SysBusDevice *sbd = SYS_BUS_DEVICE(obj); + + memory_region_init_io(&s->iomem, obj, &esp32s3_rng_ops, s, + TYPE_ESP32S3_RNG, sizeof(uint32_t)); + sysbus_init_mmio(sbd, &s->iomem); +} + + +static const TypeInfo esp32s3_rng_info = { + .name = TYPE_ESP32S3_RNG, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(Esp32s3RngState), + .instance_init = esp32s3_rng_init, +}; + +static void esp32s3_rng_register_types(void) +{ + type_register_static(&esp32s3_rng_info); +} + +type_init(esp32s3_rng_register_types) diff --git a/hw/misc/meson.build b/hw/misc/meson.build index d3b593955c..1d9227e6b9 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -165,6 +165,7 @@ system_ss.add(when: 'CONFIG_XTENSA_ESP32S3', if_true: files( 'esp32s3_sha.c', 'esp32c3_jtag.c', 'esp32s3_rtc_cntl.c', + 'esp32s3_rng.c', )) if gcrypt.found() diff --git a/include/hw/misc/esp32s3_rng.h b/include/hw/misc/esp32s3_rng.h new file mode 100644 index 0000000000..acb4972ccf --- /dev/null +++ b/include/hw/misc/esp32s3_rng.h @@ -0,0 +1,27 @@ +/* + * ESP32-S3 random number generator + * + * Copyright (c) 2024 Espressif Systems (Shanghai) Co. Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 or + * (at your option) any later version. + */ + +#pragma once + +#include "hw/hw.h" +#include "hw/sysbus.h" + +#define DR_REG_WDEV_BASE 0x3ff75000 + +#define TYPE_ESP32S3_RNG "misc.esp32s3.rng" +#define ESP32S3_RNG(obj) OBJECT_CHECK(Esp32s3RngState, (obj), TYPE_ESP32S3_RNG) + +typedef struct Esp32s3RngState { + SysBusDevice parent_obj; + MemoryRegion iomem; +} Esp32s3RngState; + +#define ESP32S3_RNG_BASE 0x6003507C//(DR_REG_WDEV_BASE + 0x07c) +