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 /* see docs/specs/pci-serial.txt */
28 #include "hw/char/serial.h"
29 #include "hw/pci/pci.h"
30 #include "qapi/qmp/qerror.h"
32 #define PCI_SERIAL_MAX_PORTS 4
34 typedef struct PCISerialState
{
40 typedef struct PCIMultiSerialState
{
44 char *name
[PCI_SERIAL_MAX_PORTS
];
45 SerialState state
[PCI_SERIAL_MAX_PORTS
];
46 uint32_t level
[PCI_SERIAL_MAX_PORTS
];
49 } PCIMultiSerialState
;
51 static void multi_serial_pci_exit(PCIDevice
*dev
);
53 static void serial_pci_realize(PCIDevice
*dev
, Error
**errp
)
55 PCISerialState
*pci
= DO_UPCAST(PCISerialState
, dev
, dev
);
56 SerialState
*s
= &pci
->state
;
60 serial_realize_core(s
, &err
);
62 error_propagate(errp
, err
);
66 pci
->dev
.config
[PCI_CLASS_PROG
] = pci
->prog_if
;
67 pci
->dev
.config
[PCI_INTERRUPT_PIN
] = 0x01;
68 s
->irq
= pci_allocate_irq(&pci
->dev
);
70 memory_region_init_io(&s
->io
, OBJECT(pci
), &serial_io_ops
, s
, "serial", 8);
71 pci_register_bar(&pci
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io
);
74 static void multi_serial_irq_mux(void *opaque
, int n
, int level
)
76 PCIMultiSerialState
*pci
= opaque
;
79 pci
->level
[n
] = level
;
80 for (i
= 0; i
< pci
->ports
; i
++) {
85 pci_set_irq(&pci
->dev
, pending
);
88 static void multi_serial_pci_realize(PCIDevice
*dev
, Error
**errp
)
90 PCIDeviceClass
*pc
= PCI_DEVICE_GET_CLASS(dev
);
91 PCIMultiSerialState
*pci
= DO_UPCAST(PCIMultiSerialState
, dev
, dev
);
96 switch (pc
->device_id
) {
104 assert(nr_ports
> 0);
105 assert(nr_ports
<= PCI_SERIAL_MAX_PORTS
);
107 pci
->dev
.config
[PCI_CLASS_PROG
] = pci
->prog_if
;
108 pci
->dev
.config
[PCI_INTERRUPT_PIN
] = 0x01;
109 memory_region_init(&pci
->iobar
, OBJECT(pci
), "multiserial", 8 * nr_ports
);
110 pci_register_bar(&pci
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->iobar
);
111 pci
->irqs
= qemu_allocate_irqs(multi_serial_irq_mux
, pci
,
114 for (i
= 0; i
< nr_ports
; i
++) {
116 s
->baudbase
= 115200;
117 serial_realize_core(s
, &err
);
119 error_propagate(errp
, err
);
120 multi_serial_pci_exit(dev
);
123 s
->irq
= pci
->irqs
[i
];
124 pci
->name
[i
] = g_strdup_printf("uart #%d", i
+1);
125 memory_region_init_io(&s
->io
, OBJECT(pci
), &serial_io_ops
, s
,
127 memory_region_add_subregion(&pci
->iobar
, 8 * i
, &s
->io
);
132 static void serial_pci_exit(PCIDevice
*dev
)
134 PCISerialState
*pci
= DO_UPCAST(PCISerialState
, dev
, dev
);
135 SerialState
*s
= &pci
->state
;
138 qemu_free_irq(s
->irq
);
141 static void multi_serial_pci_exit(PCIDevice
*dev
)
143 PCIMultiSerialState
*pci
= DO_UPCAST(PCIMultiSerialState
, dev
, dev
);
147 for (i
= 0; i
< pci
->ports
; i
++) {
150 memory_region_del_subregion(&pci
->iobar
, &s
->io
);
151 g_free(pci
->name
[i
]);
153 qemu_free_irqs(pci
->irqs
, pci
->ports
);
156 static const VMStateDescription vmstate_pci_serial
= {
157 .name
= "pci-serial",
159 .minimum_version_id
= 1,
160 .fields
= (VMStateField
[]) {
161 VMSTATE_PCI_DEVICE(dev
, PCISerialState
),
162 VMSTATE_STRUCT(state
, PCISerialState
, 0, vmstate_serial
, SerialState
),
163 VMSTATE_END_OF_LIST()
167 static const VMStateDescription vmstate_pci_multi_serial
= {
168 .name
= "pci-serial-multi",
170 .minimum_version_id
= 1,
171 .fields
= (VMStateField
[]) {
172 VMSTATE_PCI_DEVICE(dev
, PCIMultiSerialState
),
173 VMSTATE_STRUCT_ARRAY(state
, PCIMultiSerialState
, PCI_SERIAL_MAX_PORTS
,
174 0, vmstate_serial
, SerialState
),
175 VMSTATE_UINT32_ARRAY(level
, PCIMultiSerialState
, PCI_SERIAL_MAX_PORTS
),
176 VMSTATE_END_OF_LIST()
180 static Property serial_pci_properties
[] = {
181 DEFINE_PROP_CHR("chardev", PCISerialState
, state
.chr
),
182 DEFINE_PROP_UINT8("prog_if", PCISerialState
, prog_if
, 0x02),
183 DEFINE_PROP_END_OF_LIST(),
186 static Property multi_2x_serial_pci_properties
[] = {
187 DEFINE_PROP_CHR("chardev1", PCIMultiSerialState
, state
[0].chr
),
188 DEFINE_PROP_CHR("chardev2", PCIMultiSerialState
, state
[1].chr
),
189 DEFINE_PROP_UINT8("prog_if", PCIMultiSerialState
, prog_if
, 0x02),
190 DEFINE_PROP_END_OF_LIST(),
193 static Property multi_4x_serial_pci_properties
[] = {
194 DEFINE_PROP_CHR("chardev1", PCIMultiSerialState
, state
[0].chr
),
195 DEFINE_PROP_CHR("chardev2", PCIMultiSerialState
, state
[1].chr
),
196 DEFINE_PROP_CHR("chardev3", PCIMultiSerialState
, state
[2].chr
),
197 DEFINE_PROP_CHR("chardev4", PCIMultiSerialState
, state
[3].chr
),
198 DEFINE_PROP_UINT8("prog_if", PCIMultiSerialState
, prog_if
, 0x02),
199 DEFINE_PROP_END_OF_LIST(),
202 static void serial_pci_class_initfn(ObjectClass
*klass
, void *data
)
204 DeviceClass
*dc
= DEVICE_CLASS(klass
);
205 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(klass
);
206 pc
->realize
= serial_pci_realize
;
207 pc
->exit
= serial_pci_exit
;
208 pc
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
209 pc
->device_id
= PCI_DEVICE_ID_REDHAT_SERIAL
;
211 pc
->class_id
= PCI_CLASS_COMMUNICATION_SERIAL
;
212 dc
->vmsd
= &vmstate_pci_serial
;
213 dc
->props
= serial_pci_properties
;
214 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
217 static void multi_2x_serial_pci_class_initfn(ObjectClass
*klass
, void *data
)
219 DeviceClass
*dc
= DEVICE_CLASS(klass
);
220 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(klass
);
221 pc
->realize
= multi_serial_pci_realize
;
222 pc
->exit
= multi_serial_pci_exit
;
223 pc
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
224 pc
->device_id
= PCI_DEVICE_ID_REDHAT_SERIAL2
;
226 pc
->class_id
= PCI_CLASS_COMMUNICATION_SERIAL
;
227 dc
->vmsd
= &vmstate_pci_multi_serial
;
228 dc
->props
= multi_2x_serial_pci_properties
;
229 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
232 static void multi_4x_serial_pci_class_initfn(ObjectClass
*klass
, void *data
)
234 DeviceClass
*dc
= DEVICE_CLASS(klass
);
235 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(klass
);
236 pc
->realize
= multi_serial_pci_realize
;
237 pc
->exit
= multi_serial_pci_exit
;
238 pc
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
239 pc
->device_id
= PCI_DEVICE_ID_REDHAT_SERIAL4
;
241 pc
->class_id
= PCI_CLASS_COMMUNICATION_SERIAL
;
242 dc
->vmsd
= &vmstate_pci_multi_serial
;
243 dc
->props
= multi_4x_serial_pci_properties
;
244 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
247 static const TypeInfo serial_pci_info
= {
248 .name
= "pci-serial",
249 .parent
= TYPE_PCI_DEVICE
,
250 .instance_size
= sizeof(PCISerialState
),
251 .class_init
= serial_pci_class_initfn
,
254 static const TypeInfo multi_2x_serial_pci_info
= {
255 .name
= "pci-serial-2x",
256 .parent
= TYPE_PCI_DEVICE
,
257 .instance_size
= sizeof(PCIMultiSerialState
),
258 .class_init
= multi_2x_serial_pci_class_initfn
,
261 static const TypeInfo multi_4x_serial_pci_info
= {
262 .name
= "pci-serial-4x",
263 .parent
= TYPE_PCI_DEVICE
,
264 .instance_size
= sizeof(PCIMultiSerialState
),
265 .class_init
= multi_4x_serial_pci_class_initfn
,
268 static void serial_pci_register_types(void)
270 type_register_static(&serial_pci_info
);
271 type_register_static(&multi_2x_serial_pci_info
);
272 type_register_static(&multi_4x_serial_pci_info
);
275 type_init(serial_pci_register_types
)