1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "hw/char/serial.h"
27#include "hw/isa/isa.h"
28
29#define ISA_SERIAL(obj) OBJECT_CHECK(ISASerialState, (obj), TYPE_ISA_SERIAL)
30
31typedef struct ISASerialState {
32 ISADevice parent_obj;
33
34 uint32_t index;
35 uint32_t iobase;
36 uint32_t isairq;
37 SerialState state;
38} ISASerialState;
39
40static const int isa_serial_io[MAX_SERIAL_PORTS] = {
41 0x3f8, 0x2f8, 0x3e8, 0x2e8
42};
43static const int isa_serial_irq[MAX_SERIAL_PORTS] = {
44 4, 3, 4, 3
45};
46
47static int serial_isa_initfn(ISADevice *dev)
48{
49 static int index;
50 ISASerialState *isa = ISA_SERIAL(dev);
51 SerialState *s = &isa->state;
52
53 if (isa->index == -1) {
54 isa->index = index;
55 }
56 if (isa->index >= MAX_SERIAL_PORTS) {
57 return -1;
58 }
59 if (isa->iobase == -1) {
60 isa->iobase = isa_serial_io[isa->index];
61 }
62 if (isa->isairq == -1) {
63 isa->isairq = isa_serial_irq[isa->index];
64 }
65 index++;
66
67 s->baudbase = 115200;
68 isa_init_irq(dev, &s->irq, isa->isairq);
69 serial_init_core(s);
70 qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 3);
71
72 memory_region_init_io(&s->io, &serial_io_ops, s, "serial", 8);
73 isa_register_ioport(dev, &s->io, isa->iobase);
74 return 0;
75}
76
77static const VMStateDescription vmstate_isa_serial = {
78 .name = "serial",
79 .version_id = 3,
80 .minimum_version_id = 2,
81 .fields = (VMStateField[]) {
82 VMSTATE_STRUCT(state, ISASerialState, 0, vmstate_serial, SerialState),
83 VMSTATE_END_OF_LIST()
84 }
85};
86
87static Property serial_isa_properties[] = {
88 DEFINE_PROP_UINT32("index", ISASerialState, index, -1),
89 DEFINE_PROP_HEX32("iobase", ISASerialState, iobase, -1),
90 DEFINE_PROP_UINT32("irq", ISASerialState, isairq, -1),
91 DEFINE_PROP_CHR("chardev", ISASerialState, state.chr),
92 DEFINE_PROP_UINT32("wakeup", ISASerialState, state.wakeup, 0),
93 DEFINE_PROP_END_OF_LIST(),
94};
95
96static void serial_isa_class_initfn(ObjectClass *klass, void *data)
97{
98 DeviceClass *dc = DEVICE_CLASS(klass);
99 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
100 ic->init = serial_isa_initfn;
101 dc->vmsd = &vmstate_isa_serial;
102 dc->props = serial_isa_properties;
103}
104
105static const TypeInfo serial_isa_info = {
106 .name = TYPE_ISA_SERIAL,
107 .parent = TYPE_ISA_DEVICE,
108 .instance_size = sizeof(ISASerialState),
109 .class_init = serial_isa_class_initfn,
110};
111
112static void serial_register_types(void)
113{
114 type_register_static(&serial_isa_info);
115}
116
117type_init(serial_register_types)
118
119bool serial_isa_init(ISABus *bus, int index, CharDriverState *chr)
120{
121 ISADevice *dev;
122
123 dev = isa_try_create(bus, TYPE_ISA_SERIAL);
124 if (!dev) {
125 return false;
126 }
127 qdev_prop_set_uint32(&dev->qdev, "index", index);
128 qdev_prop_set_chr(&dev->qdev, "chardev", chr);
129 if (qdev_init(&dev->qdev) < 0) {
130 return false;
131 }
132 return true;
133}
134