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 "qemu/osdep.h"
29 #include "qapi/error.h"
30 #include "hw/char/serial.h"
31 #include "hw/pci/pci.h"
33 #define PCI_SERIAL_MAX_PORTS 4
35 typedef struct PCISerialState
{
41 typedef struct PCIMultiSerialState
{
45 char *name
[PCI_SERIAL_MAX_PORTS
];
46 SerialState state
[PCI_SERIAL_MAX_PORTS
];
47 uint32_t level
[PCI_SERIAL_MAX_PORTS
];
50 } PCIMultiSerialState
;
52 static void multi_serial_pci_exit(PCIDevice
*dev
);
54 static void serial_pci_realize(PCIDevice
*dev
, Error
**errp
)
56 PCISerialState
*pci
= DO_UPCAST(PCISerialState
, dev
, dev
);
57 SerialState
*s
= &pci
->state
;
61 serial_realize_core(s
, &err
);
63 error_propagate(errp
, err
);
67 pci
->dev
.config
[PCI_CLASS_PROG
] = pci
->prog_if
;
68 pci
->dev
.config
[PCI_INTERRUPT_PIN
] = 0x01;
69 s
->irq
= pci_allocate_irq(&pci
->dev
);
71 memory_region_init_io(&s
->io
, OBJECT(pci
), &serial_io_ops
, s
, "serial", 8);
72 pci_register_bar(&pci
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &s
->io
);
75 static void multi_serial_irq_mux(void *opaque
, int n
, int level
)
77 PCIMultiSerialState
*pci
= opaque
;
80 pci
->level
[n
] = level
;
81 for (i
= 0; i
< pci
->ports
; i
++) {
86 pci_set_irq(&pci
->dev
, pending
);
89 static void multi_serial_pci_realize(PCIDevice
*dev
, Error
**errp
)
91 PCIDeviceClass
*pc
= PCI_DEVICE_GET_CLASS(dev
);
92 PCIMultiSerialState
*pci
= DO_UPCAST(PCIMultiSerialState
, dev
, dev
);
97 switch (pc
->device_id
) {
105 assert(nr_ports
> 0);
106 assert(nr_ports
<= PCI_SERIAL_MAX_PORTS
);
108 pci
->dev
.config
[PCI_CLASS_PROG
] = pci
->prog_if
;
109 pci
->dev
.config
[PCI_INTERRUPT_PIN
] = 0x01;
110 memory_region_init(&pci
->iobar
, OBJECT(pci
), "multiserial", 8 * nr_ports
);
111 pci_register_bar(&pci
->dev
, 0, PCI_BASE_ADDRESS_SPACE_IO
, &pci
->iobar
);
112 pci
->irqs
= qemu_allocate_irqs(multi_serial_irq_mux
, pci
,
115 for (i
= 0; i
< nr_ports
; i
++) {
117 s
->baudbase
= 115200;
118 serial_realize_core(s
, &err
);
120 error_propagate(errp
, err
);
121 multi_serial_pci_exit(dev
);
124 s
->irq
= pci
->irqs
[i
];
125 pci
->name
[i
] = g_strdup_printf("uart #%d", i
+1);
126 memory_region_init_io(&s
->io
, OBJECT(pci
), &serial_io_ops
, s
,
128 memory_region_add_subregion(&pci
->iobar
, 8 * i
, &s
->io
);
133 static void serial_pci_exit(PCIDevice
*dev
)
135 PCISerialState
*pci
= DO_UPCAST(PCISerialState
, dev
, dev
);
136 SerialState
*s
= &pci
->state
;
139 qemu_free_irq(s
->irq
);
142 static void multi_serial_pci_exit(PCIDevice
*dev
)
144 PCIMultiSerialState
*pci
= DO_UPCAST(PCIMultiSerialState
, dev
, dev
);
148 for (i
= 0; i
< pci
->ports
; i
++) {
151 memory_region_del_subregion(&pci
->iobar
, &s
->io
);
152 g_free(pci
->name
[i
]);
154 qemu_free_irqs(pci
->irqs
, pci
->ports
);
157 static const VMStateDescription vmstate_pci_serial
= {
158 .name
= "pci-serial",
160 .minimum_version_id
= 1,
161 .fields
= (VMStateField
[]) {
162 VMSTATE_PCI_DEVICE(dev
, PCISerialState
),
163 VMSTATE_STRUCT(state
, PCISerialState
, 0, vmstate_serial
, SerialState
),
164 VMSTATE_END_OF_LIST()
168 static const VMStateDescription vmstate_pci_multi_serial
= {
169 .name
= "pci-serial-multi",
171 .minimum_version_id
= 1,
172 .fields
= (VMStateField
[]) {
173 VMSTATE_PCI_DEVICE(dev
, PCIMultiSerialState
),
174 VMSTATE_STRUCT_ARRAY(state
, PCIMultiSerialState
, PCI_SERIAL_MAX_PORTS
,
175 0, vmstate_serial
, SerialState
),
176 VMSTATE_UINT32_ARRAY(level
, PCIMultiSerialState
, PCI_SERIAL_MAX_PORTS
),
177 VMSTATE_END_OF_LIST()
181 static Property serial_pci_properties
[] = {
182 DEFINE_PROP_CHR("chardev", PCISerialState
, state
.chr
),
183 DEFINE_PROP_UINT8("prog_if", PCISerialState
, prog_if
, 0x02),
184 DEFINE_PROP_END_OF_LIST(),
187 static Property multi_2x_serial_pci_properties
[] = {
188 DEFINE_PROP_CHR("chardev1", PCIMultiSerialState
, state
[0].chr
),
189 DEFINE_PROP_CHR("chardev2", PCIMultiSerialState
, state
[1].chr
),
190 DEFINE_PROP_UINT8("prog_if", PCIMultiSerialState
, prog_if
, 0x02),
191 DEFINE_PROP_END_OF_LIST(),
194 static Property multi_4x_serial_pci_properties
[] = {
195 DEFINE_PROP_CHR("chardev1", PCIMultiSerialState
, state
[0].chr
),
196 DEFINE_PROP_CHR("chardev2", PCIMultiSerialState
, state
[1].chr
),
197 DEFINE_PROP_CHR("chardev3", PCIMultiSerialState
, state
[2].chr
),
198 DEFINE_PROP_CHR("chardev4", PCIMultiSerialState
, state
[3].chr
),
199 DEFINE_PROP_UINT8("prog_if", PCIMultiSerialState
, prog_if
, 0x02),
200 DEFINE_PROP_END_OF_LIST(),
203 static void serial_pci_class_initfn(ObjectClass
*klass
, void *data
)
205 DeviceClass
*dc
= DEVICE_CLASS(klass
);
206 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(klass
);
207 pc
->realize
= serial_pci_realize
;
208 pc
->exit
= serial_pci_exit
;
209 pc
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
210 pc
->device_id
= PCI_DEVICE_ID_REDHAT_SERIAL
;
212 pc
->class_id
= PCI_CLASS_COMMUNICATION_SERIAL
;
213 dc
->vmsd
= &vmstate_pci_serial
;
214 dc
->props
= serial_pci_properties
;
215 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
218 static void multi_2x_serial_pci_class_initfn(ObjectClass
*klass
, void *data
)
220 DeviceClass
*dc
= DEVICE_CLASS(klass
);
221 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(klass
);
222 pc
->realize
= multi_serial_pci_realize
;
223 pc
->exit
= multi_serial_pci_exit
;
224 pc
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
225 pc
->device_id
= PCI_DEVICE_ID_REDHAT_SERIAL2
;
227 pc
->class_id
= PCI_CLASS_COMMUNICATION_SERIAL
;
228 dc
->vmsd
= &vmstate_pci_multi_serial
;
229 dc
->props
= multi_2x_serial_pci_properties
;
230 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
233 static void multi_4x_serial_pci_class_initfn(ObjectClass
*klass
, void *data
)
235 DeviceClass
*dc
= DEVICE_CLASS(klass
);
236 PCIDeviceClass
*pc
= PCI_DEVICE_CLASS(klass
);
237 pc
->realize
= multi_serial_pci_realize
;
238 pc
->exit
= multi_serial_pci_exit
;
239 pc
->vendor_id
= PCI_VENDOR_ID_REDHAT
;
240 pc
->device_id
= PCI_DEVICE_ID_REDHAT_SERIAL4
;
242 pc
->class_id
= PCI_CLASS_COMMUNICATION_SERIAL
;
243 dc
->vmsd
= &vmstate_pci_multi_serial
;
244 dc
->props
= multi_4x_serial_pci_properties
;
245 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
248 static const TypeInfo serial_pci_info
= {
249 .name
= "pci-serial",
250 .parent
= TYPE_PCI_DEVICE
,
251 .instance_size
= sizeof(PCISerialState
),
252 .class_init
= serial_pci_class_initfn
,
253 .interfaces
= (InterfaceInfo
[]) {
254 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
259 static const TypeInfo multi_2x_serial_pci_info
= {
260 .name
= "pci-serial-2x",
261 .parent
= TYPE_PCI_DEVICE
,
262 .instance_size
= sizeof(PCIMultiSerialState
),
263 .class_init
= multi_2x_serial_pci_class_initfn
,
264 .interfaces
= (InterfaceInfo
[]) {
265 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
270 static const TypeInfo multi_4x_serial_pci_info
= {
271 .name
= "pci-serial-4x",
272 .parent
= TYPE_PCI_DEVICE
,
273 .instance_size
= sizeof(PCIMultiSerialState
),
274 .class_init
= multi_4x_serial_pci_class_initfn
,
275 .interfaces
= (InterfaceInfo
[]) {
276 { INTERFACE_CONVENTIONAL_PCI_DEVICE
},
281 static void serial_pci_register_types(void)
283 type_register_static(&serial_pci_info
);
284 type_register_static(&multi_2x_serial_pci_info
);
285 type_register_static(&multi_4x_serial_pci_info
);
288 type_init(serial_pci_register_types
)