xref: /qemu/hw/char/serial-isa.c (revision a27bd6c7)
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 
26 #include "qemu/osdep.h"
27 #include "qapi/error.h"
28 #include "qemu/module.h"
29 #include "hw/char/serial.h"
30 #include "hw/isa/isa.h"
31 #include "hw/qdev-properties.h"
32 #include "migration/vmstate.h"
33 
34 #define ISA_SERIAL(obj) OBJECT_CHECK(ISASerialState, (obj), TYPE_ISA_SERIAL)
35 
36 typedef struct ISASerialState {
37     ISADevice parent_obj;
38 
39     uint32_t index;
40     uint32_t iobase;
41     uint32_t isairq;
42     SerialState state;
43 } ISASerialState;
44 
45 static const int isa_serial_io[MAX_ISA_SERIAL_PORTS] = {
46     0x3f8, 0x2f8, 0x3e8, 0x2e8
47 };
48 static const int isa_serial_irq[MAX_ISA_SERIAL_PORTS] = {
49     4, 3, 4, 3
50 };
51 
52 static void serial_isa_realizefn(DeviceState *dev, Error **errp)
53 {
54     static int index;
55     ISADevice *isadev = ISA_DEVICE(dev);
56     ISASerialState *isa = ISA_SERIAL(dev);
57     SerialState *s = &isa->state;
58 
59     if (isa->index == -1) {
60         isa->index = index;
61     }
62     if (isa->index >= MAX_ISA_SERIAL_PORTS) {
63         error_setg(errp, "Max. supported number of ISA serial ports is %d.",
64                    MAX_ISA_SERIAL_PORTS);
65         return;
66     }
67     if (isa->iobase == -1) {
68         isa->iobase = isa_serial_io[isa->index];
69     }
70     if (isa->isairq == -1) {
71         isa->isairq = isa_serial_irq[isa->index];
72     }
73     index++;
74 
75     s->baudbase = 115200;
76     isa_init_irq(isadev, &s->irq, isa->isairq);
77     serial_realize_core(s, errp);
78     qdev_set_legacy_instance_id(dev, isa->iobase, 3);
79 
80     memory_region_init_io(&s->io, OBJECT(isa), &serial_io_ops, s, "serial", 8);
81     isa_register_ioport(isadev, &s->io, isa->iobase);
82 }
83 
84 static const VMStateDescription vmstate_isa_serial = {
85     .name = "serial",
86     .version_id = 3,
87     .minimum_version_id = 2,
88     .fields = (VMStateField[]) {
89         VMSTATE_STRUCT(state, ISASerialState, 0, vmstate_serial, SerialState),
90         VMSTATE_END_OF_LIST()
91     }
92 };
93 
94 static Property serial_isa_properties[] = {
95     DEFINE_PROP_UINT32("index",  ISASerialState, index,   -1),
96     DEFINE_PROP_UINT32("iobase",  ISASerialState, iobase,  -1),
97     DEFINE_PROP_UINT32("irq",    ISASerialState, isairq,  -1),
98     DEFINE_PROP_CHR("chardev",   ISASerialState, state.chr),
99     DEFINE_PROP_UINT32("wakeup", ISASerialState, state.wakeup, 0),
100     DEFINE_PROP_END_OF_LIST(),
101 };
102 
103 static void serial_isa_class_initfn(ObjectClass *klass, void *data)
104 {
105     DeviceClass *dc = DEVICE_CLASS(klass);
106 
107     dc->realize = serial_isa_realizefn;
108     dc->vmsd = &vmstate_isa_serial;
109     dc->props = serial_isa_properties;
110     set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
111 }
112 
113 static const TypeInfo serial_isa_info = {
114     .name          = TYPE_ISA_SERIAL,
115     .parent        = TYPE_ISA_DEVICE,
116     .instance_size = sizeof(ISASerialState),
117     .class_init    = serial_isa_class_initfn,
118 };
119 
120 static void serial_register_types(void)
121 {
122     type_register_static(&serial_isa_info);
123 }
124 
125 type_init(serial_register_types)
126 
127 static void serial_isa_init(ISABus *bus, int index, Chardev *chr)
128 {
129     DeviceState *dev;
130     ISADevice *isadev;
131 
132     isadev = isa_create(bus, TYPE_ISA_SERIAL);
133     dev = DEVICE(isadev);
134     qdev_prop_set_uint32(dev, "index", index);
135     qdev_prop_set_chr(dev, "chardev", chr);
136     qdev_init_nofail(dev);
137 }
138 
139 void serial_hds_isa_init(ISABus *bus, int from, int to)
140 {
141     int i;
142 
143     assert(from >= 0);
144     assert(to <= MAX_ISA_SERIAL_PORTS);
145 
146     for (i = from; i < to; ++i) {
147         if (serial_hd(i)) {
148             serial_isa_init(bus, i, serial_hd(i));
149         }
150     }
151 }
152