From: Sergey Kambalin <[email protected]> Add a dummy BCM2838 (BCM2711/Raspberry Pi 4B) thermal sensor: a single status register that always reports a fixed 25C reading with both "valid" bits set. Real hardware reports live temperature; a fixed reading is enough for guests that just probe the sensor and read it, without modeling actual thermal behavior.
Part of Sergey Kambalin's original Raspberry Pi 4B series (patchwork series 829638, posted to qemu-devel in 2024, never merged). Ported to current master by Marcelo Manzo: header paths moved under hw/core/, class_init()'s const void *data. No functional change otherwise (this device has no reset or migration state to begin with). Boot-tested: guest's thermal_zone0/temp reports 25310 (25.31C), matching the fixed reading this device returns. Signed-off-by: Sergey Kambalin <[email protected]> Signed-off-by: Marcelo Manzo <[email protected]> --- hw/misc/bcm2838_thermal.c | 98 +++++++++++++++++++++++++++++++ hw/misc/meson.build | 1 + include/hw/misc/bcm2838_thermal.h | 23 ++++++++ 3 files changed, 122 insertions(+) create mode 100644 hw/misc/bcm2838_thermal.c create mode 100644 include/hw/misc/bcm2838_thermal.h diff --git a/hw/misc/bcm2838_thermal.c b/hw/misc/bcm2838_thermal.c new file mode 100644 index 00000000..40148ddb --- /dev/null +++ b/hw/misc/bcm2838_thermal.c @@ -0,0 +1,98 @@ +/* + * BCM2838 dummy thermal sensor + * + * Copyright (C) 2022 Maksim Kopusov <[email protected]> + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "qapi/error.h" +#include "hw/misc/bcm2838_thermal.h" +#include "hw/core/registerfields.h" +#include "migration/vmstate.h" +#include "qemu/error-report.h" + +REG32(STAT, 0x200) +FIELD(STAT, DATA, 0, 10) +FIELD(STAT, VALID_1, 10, 1) +FIELD(STAT, VALID_2, 16, 1) + +#define BCM2838_THERMAL_SIZE 0xf00 + +#define THERMAL_OFFSET_C 410040 +#define THERMAL_COEFF (-487.0f) +#define MILLIDEGREE_COEFF 1000 + +static uint16_t bcm2838_thermal_temp2adc(int temp_C) +{ + return (temp_C * MILLIDEGREE_COEFF - THERMAL_OFFSET_C) / THERMAL_COEFF; +} + +static uint64_t bcm2838_thermal_read(void *opaque, hwaddr addr, unsigned size) +{ + uint32_t val = 0; + + switch (addr) { + case A_STAT: + /* Temperature is always 25°C */ + val = FIELD_DP32(val, STAT, DATA, bcm2838_thermal_temp2adc(25)); + val = FIELD_DP32(val, STAT, VALID_1, 1); + val = FIELD_DP32(val, STAT, VALID_2, 1); + + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, "%s can't access addr: 0x%"HWADDR_PRIx, + TYPE_BCM2838_THERMAL, addr); + } + return val; +} + +static void bcm2838_thermal_write(void *opaque, hwaddr addr, + uint64_t value, unsigned size) +{ + qemu_log_mask(LOG_GUEST_ERROR, "%s: write 0x%" PRIx64 + " to 0x%" HWADDR_PRIx "\n", + __func__, value, addr); +} + +static const MemoryRegionOps bcm2838_thermal_ops = { + .read = bcm2838_thermal_read, + .write = bcm2838_thermal_write, + .impl.max_access_size = 4, + .valid.min_access_size = 4, + .endianness = DEVICE_LITTLE_ENDIAN, +}; + +static void bcm2838_thermal_realize(DeviceState *dev, Error **errp) +{ + Bcm2838ThermalState *s = BCM2838_THERMAL(dev); + + memory_region_init_io(&s->iomem, OBJECT(s), &bcm2838_thermal_ops, + s, TYPE_BCM2838_THERMAL, BCM2838_THERMAL_SIZE); + sysbus_init_mmio(SYS_BUS_DEVICE(s), &s->iomem); +} + +static void bcm2838_thermal_class_init(ObjectClass *klass, const void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + + dc->realize = bcm2838_thermal_realize; + + /* This device has no state: no need for vmstate or reset */ +} + +static const TypeInfo bcm2838_thermal_info = { + .name = TYPE_BCM2838_THERMAL, + .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(Bcm2838ThermalState), + .class_init = bcm2838_thermal_class_init, +}; + +static void bcm2838_thermal_register_types(void) +{ + type_register_static(&bcm2838_thermal_info); +} + +type_init(bcm2838_thermal_register_types) diff --git a/hw/misc/meson.build b/hw/misc/meson.build index cf73ab4c..b4ac064d 100644 --- a/hw/misc/meson.build +++ b/hw/misc/meson.build @@ -95,6 +95,7 @@ system_ss.add(when: 'CONFIG_RASPI', if_true: files( 'bcm2835_cprman.c', 'bcm2835_powermgt.c', 'bcm2838_rng200.c', + 'bcm2838_thermal.c', )) system_ss.add(when: 'CONFIG_SLAVIO', if_true: files('slavio_misc.c')) system_ss.add(when: 'CONFIG_ZYNQ', if_true: files('zynq_slcr.c')) diff --git a/include/hw/misc/bcm2838_thermal.h b/include/hw/misc/bcm2838_thermal.h new file mode 100644 index 00000000..1ba524ed --- /dev/null +++ b/include/hw/misc/bcm2838_thermal.h @@ -0,0 +1,23 @@ +/* + * BCM2838 dummy thermal sensor + * + * Copyright (C) 2022 Maksim Kopusov <[email protected]> + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#ifndef BCM2838_THERMAL_H +#define BCM2838_THERMAL_H + +#include "hw/core/sysbus.h" +#include "qom/object.h" + +#define TYPE_BCM2838_THERMAL "bcm2838-thermal" +OBJECT_DECLARE_SIMPLE_TYPE(Bcm2838ThermalState, BCM2838_THERMAL) + +struct Bcm2838ThermalState { + SysBusDevice busdev; + MemoryRegion iomem; +}; + +#endif /* BCM2838_THERMAL_H */ -- 2.47.1
