]> git.proxmox.com Git - mirror_qemu.git/blame - hw/char/serial-isa.c
isa: Use realizefn for ISADevice
[mirror_qemu.git] / hw / char / serial-isa.c
CommitLineData
488cb996
GH
1/*
2 * QEMU 16550A UART emulation
3 *
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2008 Citrix Systems, Inc.
6 *
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
25
0d09e41a
PB
26#include "hw/char/serial.h"
27#include "hw/isa/isa.h"
488cb996 28
eeceb084
AF
29#define ISA_SERIAL(obj) OBJECT_CHECK(ISASerialState, (obj), TYPE_ISA_SERIAL)
30
488cb996 31typedef struct ISASerialState {
eeceb084
AF
32 ISADevice parent_obj;
33
488cb996
GH
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
db895a1e 47static void serial_isa_realizefn(DeviceState *dev, Error **errp)
488cb996
GH
48{
49 static int index;
db895a1e 50 ISADevice *isadev = ISA_DEVICE(dev);
eeceb084 51 ISASerialState *isa = ISA_SERIAL(dev);
488cb996
GH
52 SerialState *s = &isa->state;
53
54 if (isa->index == -1) {
55 isa->index = index;
56 }
57 if (isa->index >= MAX_SERIAL_PORTS) {
db895a1e
AF
58 error_setg(errp, "Max. supported number of ISA serial ports is %d.",
59 MAX_SERIAL_PORTS);
60 return;
488cb996
GH
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;
db895a1e
AF
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);
488cb996
GH
74
75 memory_region_init_io(&s->io, &serial_io_ops, s, "serial", 8);
db895a1e 76 isa_register_ioport(isadev, &s->io, isa->iobase);
488cb996
GH
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_HEX32("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);
db895a1e
AF
101
102 dc->realize = serial_isa_realizefn;
488cb996
GH
103 dc->vmsd = &vmstate_isa_serial;
104 dc->props = serial_isa_properties;
105}
106
8c43a6f0 107static const TypeInfo serial_isa_info = {
eeceb084 108 .name = TYPE_ISA_SERIAL,
488cb996
GH
109 .parent = TYPE_ISA_DEVICE,
110 .instance_size = sizeof(ISASerialState),
111 .class_init = serial_isa_class_initfn,
112};
113
114static void serial_register_types(void)
115{
116 type_register_static(&serial_isa_info);
117}
118
119type_init(serial_register_types)
120
121bool serial_isa_init(ISABus *bus, int index, CharDriverState *chr)
122{
123 ISADevice *dev;
124
eeceb084 125 dev = isa_try_create(bus, TYPE_ISA_SERIAL);
488cb996
GH
126 if (!dev) {
127 return false;
128 }
129 qdev_prop_set_uint32(&dev->qdev, "index", index);
130 qdev_prop_set_chr(&dev->qdev, "chardev", chr);
131 if (qdev_init(&dev->qdev) < 0) {
132 return false;
133 }
134 return true;
135}