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 void serial_isa_realizefn(DeviceState *dev, Error **errp)
48{
49 static int index;
50 ISADevice *isadev = ISA_DEVICE(dev);
51 ISASerialState *isa = ISA_SERIAL(dev);
52 SerialState *s = &isa->state;
53
54 if (isa->index == -1) {
55 isa->index = index;
56 }
57 if (isa->index >= MAX_SERIAL_PORTS) {
58 error_setg(errp, "Max. supported number of ISA serial ports is %d.",
59 MAX_SERIAL_PORTS);
60 return;
61 }
62 if (isa->iobase == -1) {
63 isa->iobase = isa_serial_io[isa->index];
64 }
65 if (isa->isairq == -1) {
66 isa->isairq = isa_serial_irq[isa->index];
67 }
68 index++;
69
70 s->baudbase = 115200;
71 isa_init_irq(isadev, &s->irq, isa->isairq);
72 serial_realize_core(s, errp);
73 qdev_set_legacy_instance_id(dev, isa->iobase, 3);
74
75 memory_region_init_io(&s->io, OBJECT(isa), &serial_io_ops, s, "serial", 8);
76 isa_register_ioport(isadev, &s->io, isa->iobase);
77}
78
79static const VMStateDescription vmstate_isa_serial = {
80 .name = "serial",
81 .version_id = 3,
82 .minimum_version_id = 2,
83 .fields = (VMStateField[]) {
84 VMSTATE_STRUCT(state, ISASerialState, 0, vmstate_serial, SerialState),
85 VMSTATE_END_OF_LIST()
86 }
87};
88
89static Property serial_isa_properties[] = {
90 DEFINE_PROP_UINT32("index", ISASerialState, index, -1),
91 DEFINE_PROP_UINT32("iobase", ISASerialState, iobase, -1),
92 DEFINE_PROP_UINT32("irq", ISASerialState, isairq, -1),
93 DEFINE_PROP_CHR("chardev", ISASerialState, state.chr),
94 DEFINE_PROP_UINT32("wakeup", ISASerialState, state.wakeup, 0),
95 DEFINE_PROP_END_OF_LIST(),
96};
97
98static void serial_isa_class_initfn(ObjectClass *klass, void *data)
99{
100 DeviceClass *dc = DEVICE_CLASS(klass);
101
102 dc->realize = serial_isa_realizefn;
103 dc->vmsd = &vmstate_isa_serial;
104 dc->props = serial_isa_properties;
105 set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
106}
107
108static const TypeInfo serial_isa_info = {
109 .name = TYPE_ISA_SERIAL,
110 .parent = TYPE_ISA_DEVICE,
111 .instance_size = sizeof(ISASerialState),
112 .class_init = serial_isa_class_initfn,
113};
114
115static void serial_register_types(void)
116{
117 type_register_static(&serial_isa_info);
118}
119
120type_init(serial_register_types)
121
122static void serial_isa_init(ISABus *bus, int index, CharDriverState *chr)
123{
124 DeviceState *dev;
125 ISADevice *isadev;
126
127 isadev = isa_create(bus, TYPE_ISA_SERIAL);
128 dev = DEVICE(isadev);
129 qdev_prop_set_uint32(dev, "index", index);
130 qdev_prop_set_chr(dev, "chardev", chr);
131 qdev_init_nofail(dev);
132}
133
134void serial_hds_isa_init(ISABus *bus, int n)
135{
136 int i;
137
138 assert(n <= MAX_SERIAL_PORTS);
139
140 for (i = 0; i < n; ++i) {
141 if (serial_hds[i]) {
142 serial_isa_init(bus, i, serial_hds[i]);
143 }
144 }
145}
146