Skip to content

Commit 31ca70b

Browse files
philmdalistair23
authored andcommitted
hw/char/mchp_pfsoc_mmuart: QOM'ify PolarFire MMUART
- Embed SerialMM in MchpPfSoCMMUartState and QOM-initialize it - Alias SERIAL_MM 'chardev' property on MCHP_PFSOC_UART - Forward SerialMM sysbus IRQ in mchp_pfsoc_mmuart_realize() - Add DeviceReset() method - Add vmstate structure for migration - Register device in 'input' category - Keep mchp_pfsoc_mmuart_create() behavior Note, serial_mm_init() calls qdev_set_legacy_instance_id(). This call is only needed for backwards-compatibility of incoming migration data with old versions of QEMU which implemented migration of devices with hand-rolled code. Since this device didn't previously handle migration at all, then it doesn't need to set the legacy instance ID. Signed-off-by: Philippe Mathieu-Daudé <[email protected]> Reviewed-by: Bin Meng <[email protected]> Tested-by: Bin Meng <[email protected]> Reviewed-by: Alistair Francis <[email protected]> Message-id: [email protected] Signed-off-by: Alistair Francis <[email protected]>
1 parent 24ce762 commit 31ca70b

File tree

2 files changed

+93
-16
lines changed

2 files changed

+93
-16
lines changed

hw/char/mchp_pfsoc_mmuart.c

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222

2323
#include "qemu/osdep.h"
2424
#include "qemu/log.h"
25-
#include "chardev/char.h"
25+
#include "qapi/error.h"
26+
#include "migration/vmstate.h"
2627
#include "hw/char/mchp_pfsoc_mmuart.h"
28+
#include "hw/qdev-properties.h"
2729

2830
#define REGS_OFFSET 0x20
2931

@@ -67,26 +69,95 @@ static const MemoryRegionOps mchp_pfsoc_mmuart_ops = {
6769
},
6870
};
6971

70-
MchpPfSoCMMUartState *mchp_pfsoc_mmuart_create(MemoryRegion *sysmem,
71-
hwaddr base, qemu_irq irq, Chardev *chr)
72+
static void mchp_pfsoc_mmuart_reset(DeviceState *dev)
73+
{
74+
MchpPfSoCMMUartState *s = MCHP_PFSOC_UART(dev);
75+
76+
memset(s->reg, 0, sizeof(s->reg));
77+
device_cold_reset(DEVICE(&s->serial_mm));
78+
}
79+
80+
static void mchp_pfsoc_mmuart_init(Object *obj)
7281
{
73-
MchpPfSoCMMUartState *s;
82+
MchpPfSoCMMUartState *s = MCHP_PFSOC_UART(obj);
7483

75-
s = g_new0(MchpPfSoCMMUartState, 1);
84+
object_initialize_child(obj, "serial-mm", &s->serial_mm, TYPE_SERIAL_MM);
85+
object_property_add_alias(obj, "chardev", OBJECT(&s->serial_mm), "chardev");
86+
}
7687

77-
memory_region_init(&s->container, NULL, "mchp.pfsoc.mmuart", 0x1000);
88+
static void mchp_pfsoc_mmuart_realize(DeviceState *dev, Error **errp)
89+
{
90+
MchpPfSoCMMUartState *s = MCHP_PFSOC_UART(dev);
7891

79-
memory_region_init_io(&s->iomem, NULL, &mchp_pfsoc_mmuart_ops, s,
92+
qdev_prop_set_uint8(DEVICE(&s->serial_mm), "regshift", 2);
93+
qdev_prop_set_uint32(DEVICE(&s->serial_mm), "baudbase", 399193);
94+
qdev_prop_set_uint8(DEVICE(&s->serial_mm), "endianness",
95+
DEVICE_LITTLE_ENDIAN);
96+
if (!sysbus_realize(SYS_BUS_DEVICE(&s->serial_mm), errp)) {
97+
return;
98+
}
99+
100+
sysbus_pass_irq(SYS_BUS_DEVICE(dev), SYS_BUS_DEVICE(&s->serial_mm));
101+
102+
memory_region_init(&s->container, OBJECT(s), "mchp.pfsoc.mmuart", 0x1000);
103+
sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->container);
104+
105+
memory_region_add_subregion(&s->container, 0,
106+
sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->serial_mm), 0));
107+
108+
memory_region_init_io(&s->iomem, OBJECT(s), &mchp_pfsoc_mmuart_ops, s,
80109
"mchp.pfsoc.mmuart.regs", 0x1000 - REGS_OFFSET);
81110
memory_region_add_subregion(&s->container, REGS_OFFSET, &s->iomem);
111+
}
82112

83-
s->base = base;
84-
s->irq = irq;
113+
static const VMStateDescription mchp_pfsoc_mmuart_vmstate = {
114+
.name = "mchp.pfsoc.uart",
115+
.version_id = 0,
116+
.minimum_version_id = 0,
117+
.fields = (VMStateField[]) {
118+
VMSTATE_UINT32_ARRAY(reg, MchpPfSoCMMUartState,
119+
MCHP_PFSOC_MMUART_REG_COUNT),
120+
VMSTATE_END_OF_LIST()
121+
}
122+
};
123+
124+
static void mchp_pfsoc_mmuart_class_init(ObjectClass *oc, void *data)
125+
{
126+
DeviceClass *dc = DEVICE_CLASS(oc);
127+
128+
dc->realize = mchp_pfsoc_mmuart_realize;
129+
dc->reset = mchp_pfsoc_mmuart_reset;
130+
dc->vmsd = &mchp_pfsoc_mmuart_vmstate;
131+
set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
132+
}
133+
134+
static const TypeInfo mchp_pfsoc_mmuart_info = {
135+
.name = TYPE_MCHP_PFSOC_UART,
136+
.parent = TYPE_SYS_BUS_DEVICE,
137+
.instance_size = sizeof(MchpPfSoCMMUartState),
138+
.instance_init = mchp_pfsoc_mmuart_init,
139+
.class_init = mchp_pfsoc_mmuart_class_init,
140+
};
141+
142+
static void mchp_pfsoc_mmuart_register_types(void)
143+
{
144+
type_register_static(&mchp_pfsoc_mmuart_info);
145+
}
146+
147+
type_init(mchp_pfsoc_mmuart_register_types)
148+
149+
MchpPfSoCMMUartState *mchp_pfsoc_mmuart_create(MemoryRegion *sysmem,
150+
hwaddr base,
151+
qemu_irq irq, Chardev *chr)
152+
{
153+
DeviceState *dev = qdev_new(TYPE_MCHP_PFSOC_UART);
154+
SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
85155

86-
s->serial = serial_mm_init(&s->container, 0, 2, irq, 399193, chr,
87-
DEVICE_LITTLE_ENDIAN);
156+
qdev_prop_set_chr(dev, "chardev", chr);
157+
sysbus_realize(sbd, &error_fatal);
88158

89-
memory_region_add_subregion(sysmem, base, &s->container);
159+
memory_region_add_subregion(sysmem, base, sysbus_mmio_get_region(sbd, 0));
160+
sysbus_connect_irq(sbd, 0, irq);
90161

91-
return s;
162+
return MCHP_PFSOC_UART(dev);
92163
}

include/hw/char/mchp_pfsoc_mmuart.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,23 @@
2828
#ifndef HW_MCHP_PFSOC_MMUART_H
2929
#define HW_MCHP_PFSOC_MMUART_H
3030

31+
#include "hw/sysbus.h"
3132
#include "hw/char/serial.h"
3233

3334
#define MCHP_PFSOC_MMUART_REG_COUNT 13
3435

36+
#define TYPE_MCHP_PFSOC_UART "mchp.pfsoc.uart"
37+
OBJECT_DECLARE_SIMPLE_TYPE(MchpPfSoCMMUartState, MCHP_PFSOC_UART)
38+
3539
typedef struct MchpPfSoCMMUartState {
40+
/*< private >*/
41+
SysBusDevice parent_obj;
42+
43+
/*< public >*/
3644
MemoryRegion container;
3745
MemoryRegion iomem;
38-
hwaddr base;
39-
qemu_irq irq;
4046

41-
SerialMM *serial;
47+
SerialMM serial_mm;
4248

4349
uint32_t reg[MCHP_PFSOC_MMUART_REG_COUNT];
4450
} MchpPfSoCMMUartState;

0 commit comments

Comments
 (0)