2 * QEMU 16550A UART emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2008 Citrix Systems, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qemu/module.h"
29 #include "sysemu/sysemu.h"
30 #include "hw/acpi/aml-build.h"
31 #include "hw/char/serial.h"
32 #include "hw/isa/isa.h"
33 #include "hw/qdev-properties.h"
34 #include "migration/vmstate.h"
36 #define ISA_SERIAL(obj) OBJECT_CHECK(ISASerialState, (obj), TYPE_ISA_SERIAL)
38 typedef struct ISASerialState
{
47 static const int isa_serial_io
[MAX_ISA_SERIAL_PORTS
] = {
48 0x3f8, 0x2f8, 0x3e8, 0x2e8
50 static const int isa_serial_irq
[MAX_ISA_SERIAL_PORTS
] = {
54 static void serial_isa_realizefn(DeviceState
*dev
, Error
**errp
)
57 ISADevice
*isadev
= ISA_DEVICE(dev
);
58 ISASerialState
*isa
= ISA_SERIAL(dev
);
59 SerialState
*s
= &isa
->state
;
61 if (isa
->index
== -1) {
64 if (isa
->index
>= MAX_ISA_SERIAL_PORTS
) {
65 error_setg(errp
, "Max. supported number of ISA serial ports is %d.",
66 MAX_ISA_SERIAL_PORTS
);
69 if (isa
->iobase
== -1) {
70 isa
->iobase
= isa_serial_io
[isa
->index
];
72 if (isa
->isairq
== -1) {
73 isa
->isairq
= isa_serial_irq
[isa
->index
];
77 isa_init_irq(isadev
, &s
->irq
, isa
->isairq
);
78 qdev_realize(DEVICE(s
), NULL
, errp
);
79 qdev_set_legacy_instance_id(dev
, isa
->iobase
, 3);
81 memory_region_init_io(&s
->io
, OBJECT(isa
), &serial_io_ops
, s
, "serial", 8);
82 isa_register_ioport(isadev
, &s
->io
, isa
->iobase
);
85 static void serial_isa_build_aml(ISADevice
*isadev
, Aml
*scope
)
87 ISASerialState
*isa
= ISA_SERIAL(isadev
);
91 crs
= aml_resource_template();
92 aml_append(crs
, aml_io(AML_DECODE16
, isa
->iobase
, isa
->iobase
, 0x00, 0x08));
93 aml_append(crs
, aml_irq_no_flags(isa
->isairq
));
95 dev
= aml_device("COM%d", isa
->index
+ 1);
96 aml_append(dev
, aml_name_decl("_HID", aml_eisaid("PNP0501")));
97 aml_append(dev
, aml_name_decl("_UID", aml_int(isa
->index
+ 1)));
98 aml_append(dev
, aml_name_decl("_STA", aml_int(0xf)));
99 aml_append(dev
, aml_name_decl("_CRS", crs
));
101 aml_append(scope
, dev
);
104 static const VMStateDescription vmstate_isa_serial
= {
107 .minimum_version_id
= 2,
108 .fields
= (VMStateField
[]) {
109 VMSTATE_STRUCT(state
, ISASerialState
, 0, vmstate_serial
, SerialState
),
110 VMSTATE_END_OF_LIST()
114 static Property serial_isa_properties
[] = {
115 DEFINE_PROP_UINT32("index", ISASerialState
, index
, -1),
116 DEFINE_PROP_UINT32("iobase", ISASerialState
, iobase
, -1),
117 DEFINE_PROP_UINT32("irq", ISASerialState
, isairq
, -1),
118 DEFINE_PROP_CHR("chardev", ISASerialState
, state
.chr
),
119 DEFINE_PROP_UINT32("wakeup", ISASerialState
, state
.wakeup
, 0),
120 DEFINE_PROP_END_OF_LIST(),
123 static void serial_isa_class_initfn(ObjectClass
*klass
, void *data
)
125 DeviceClass
*dc
= DEVICE_CLASS(klass
);
126 ISADeviceClass
*isa
= ISA_DEVICE_CLASS(klass
);
128 dc
->realize
= serial_isa_realizefn
;
129 dc
->vmsd
= &vmstate_isa_serial
;
130 isa
->build_aml
= serial_isa_build_aml
;
131 device_class_set_props(dc
, serial_isa_properties
);
132 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
135 static void serial_isa_initfn(Object
*o
)
137 ISASerialState
*self
= ISA_SERIAL(o
);
139 object_initialize_child(o
, "serial", &self
->state
, TYPE_SERIAL
);
142 static const TypeInfo serial_isa_info
= {
143 .name
= TYPE_ISA_SERIAL
,
144 .parent
= TYPE_ISA_DEVICE
,
145 .instance_size
= sizeof(ISASerialState
),
146 .instance_init
= serial_isa_initfn
,
147 .class_init
= serial_isa_class_initfn
,
150 static void serial_register_types(void)
152 type_register_static(&serial_isa_info
);
155 type_init(serial_register_types
)
157 static void serial_isa_init(ISABus
*bus
, int index
, Chardev
*chr
)
162 isadev
= isa_new(TYPE_ISA_SERIAL
);
163 dev
= DEVICE(isadev
);
164 qdev_prop_set_uint32(dev
, "index", index
);
165 qdev_prop_set_chr(dev
, "chardev", chr
);
166 isa_realize_and_unref(isadev
, bus
, &error_fatal
);
169 void serial_hds_isa_init(ISABus
*bus
, int from
, int to
)
174 assert(to
<= MAX_ISA_SERIAL_PORTS
);
176 for (i
= from
; i
< to
; ++i
) {
178 serial_isa_init(bus
, i
, serial_hd(i
));