diff --git a/hw/timer/esp32s3_systimer.c b/hw/timer/esp32s3_systimer.c new file mode 100644 index 0000000000..b5e0ee17c7 --- /dev/null +++ b/hw/timer/esp32s3_systimer.c @@ -0,0 +1,60 @@ +/* + * ESP32-S3 System Timer + * + * 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. + */ +#include "qemu/osdep.h" +#include "hw/hw.h" +#include "hw/sysbus.h" +#include "hw/registerfields.h" +#include "hw/boards.h" +#include "hw/timer/esp32s3_systimer.h" + + +static void esp32c3_systimer_write(void *opaque, hwaddr addr, uint64_t value, unsigned int size) +{ + ESP32S3SysTimerClass *class = ESP32S3_SYSTIMER_GET_CLASS(opaque); + ESP32S3SysTimerState *s = ESP32S3_SYSTIMER(opaque); + + class->parent_systimer_write(opaque, addr, value, size); + + if (addr == A_SYSTIMER_INT_ENA) { + /* If the timer virtual interrupt is enabled too late, we may miss the interrupt in QEMU + * and not reprogram the next iteration. */ + class->parent_class.comparators_reprogram(&s->parent); + } +} + + + +static void esp32s3_systimer_class_init(ObjectClass *klass, void *data) +{ + ESP32S3SysTimerClass* esp32s3 = ESP32S3_SYSTIMER_CLASS(klass); + ESPSysTimerClass* esp = ESP_SYSTIMER_CLASS(klass); + + /* Override the register read method */ + esp32s3->parent_systimer_write = esp->systimer_ops.write; + esp->systimer_ops.write = esp32c3_systimer_write; +} + + +static const TypeInfo esp32s3_systimer_info = { + .name = TYPE_ESP32S3_SYSTIMER, + .parent = TYPE_ESP_SYSTIMER, + .instance_size = sizeof(ESP32S3SysTimerState), + .class_init = esp32s3_systimer_class_init, + .class_size = sizeof(ESP32S3SysTimerClass), +}; + + +static void esp32s3_systimer_register_types(void) +{ + type_register_static(&esp32s3_systimer_info); +} + + +type_init(esp32s3_systimer_register_types) diff --git a/hw/timer/meson.build b/hw/timer/meson.build index 99f1ce946c..b09e8c1c6a 100644 --- a/hw/timer/meson.build +++ b/hw/timer/meson.build @@ -38,6 +38,8 @@ system_ss.add(when: 'CONFIG_RISCV_ESP32C3', if_true: files( )) system_ss.add(when: 'CONFIG_XTENSA_ESP32S3', if_true: files( 'esp32c3_timg.c', + 'esp_systimer.c', + 'esp32s3_systimer.c' )) specific_ss.add(when: 'CONFIG_IBEX', if_true: files('ibex_timer.c')) system_ss.add(when: 'CONFIG_SIFIVE_PWM', if_true: files('sifive_pwm.c')) diff --git a/include/hw/timer/esp32s3_systimer.h b/include/hw/timer/esp32s3_systimer.h new file mode 100644 index 0000000000..4131f4e35b --- /dev/null +++ b/include/hw/timer/esp32s3_systimer.h @@ -0,0 +1,32 @@ +/* + * ESP32-S3 System Timer emulation + * + * 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/registerfields.h" +#include "hw/timer/esp_systimer.h" + +#define TYPE_ESP32S3_SYSTIMER "esp32s3.systimer" +#define ESP32S3_SYSTIMER(obj) OBJECT_CHECK(ESP32S3SysTimerState, (obj), TYPE_ESP32S3_SYSTIMER) +#define ESP32S3_SYSTIMER_GET_CLASS(obj) OBJECT_GET_CLASS(ESP32S3SysTimerClass, obj, TYPE_ESP32S3_SYSTIMER) +#define ESP32S3_SYSTIMER_CLASS(klass) OBJECT_CLASS_CHECK(ESP32S3SysTimerClass, (klass), TYPE_ESP32S3_SYSTIMER) + + +typedef struct ESP32S3SysTimerState { + ESPSysTimerState parent; +} ESP32S3SysTimerState; + + +typedef struct ESP32S3SysTimerClass { + ESPSysTimerClass parent_class; + + /* Virtual attributes/methods overriden */ + void (*parent_systimer_write)(void *opaque, hwaddr addr, uint64_t value, unsigned int size); +} ESP32S3SysTimerClass; \ No newline at end of file