1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include "hw/hw.h"
28#include "sysemu/char.h"
29#include "hw/isa/isa.h"
30#include "hw/i386/pc.h"
31
32#define TYPE_ISA_DEBUGCON_DEVICE "isa-debugcon"
33#define ISA_DEBUGCON_DEVICE(obj) \
34 OBJECT_CHECK(ISADebugconState, (obj), TYPE_ISA_DEBUGCON_DEVICE)
35
36
37
38typedef struct DebugconState {
39 MemoryRegion io;
40 CharDriverState *chr;
41 uint32_t readback;
42} DebugconState;
43
44typedef struct ISADebugconState {
45 ISADevice parent_obj;
46
47 uint32_t iobase;
48 DebugconState state;
49} ISADebugconState;
50
51static void debugcon_ioport_write(void *opaque, hwaddr addr, uint64_t val,
52 unsigned width)
53{
54 DebugconState *s = opaque;
55 unsigned char ch = val;
56
57#ifdef DEBUG_DEBUGCON
58 printf(" [debugcon: write addr=0x%04" HWADDR_PRIx " val=0x%02" PRIx64 "]\n", addr, val);
59#endif
60
61 qemu_chr_fe_write(s->chr, &ch, 1);
62}
63
64
65static uint64_t debugcon_ioport_read(void *opaque, hwaddr addr, unsigned width)
66{
67 DebugconState *s = opaque;
68
69#ifdef DEBUG_DEBUGCON
70 printf("debugcon: read addr=0x%04" HWADDR_PRIx "\n", addr);
71#endif
72
73 return s->readback;
74}
75
76static const MemoryRegionOps debugcon_ops = {
77 .read = debugcon_ioport_read,
78 .write = debugcon_ioport_write,
79 .valid.min_access_size = 1,
80 .valid.max_access_size = 1,
81 .endianness = DEVICE_LITTLE_ENDIAN,
82};
83
84static void debugcon_realize_core(DebugconState *s, Error **errp)
85{
86 if (!s->chr) {
87 error_setg(errp, "Can't create debugcon device, empty char device");
88 return;
89 }
90
91 qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, s);
92}
93
94static void debugcon_isa_realizefn(DeviceState *dev, Error **errp)
95{
96 ISADevice *d = ISA_DEVICE(dev);
97 ISADebugconState *isa = ISA_DEBUGCON_DEVICE(dev);
98 DebugconState *s = &isa->state;
99 Error *err = NULL;
100
101 debugcon_realize_core(s, &err);
102 if (err != NULL) {
103 error_propagate(errp, err);
104 return;
105 }
106 memory_region_init_io(&s->io, OBJECT(dev), &debugcon_ops, s,
107 TYPE_ISA_DEBUGCON_DEVICE, 1);
108 memory_region_add_subregion(isa_address_space_io(d),
109 isa->iobase, &s->io);
110}
111
112static Property debugcon_isa_properties[] = {
113 DEFINE_PROP_UINT32("iobase", ISADebugconState, iobase, 0xe9),
114 DEFINE_PROP_CHR("chardev", ISADebugconState, state.chr),
115 DEFINE_PROP_UINT32("readback", ISADebugconState, state.readback, 0xe9),
116 DEFINE_PROP_END_OF_LIST(),
117};
118
119static void debugcon_isa_class_initfn(ObjectClass *klass, void *data)
120{
121 DeviceClass *dc = DEVICE_CLASS(klass);
122
123 dc->realize = debugcon_isa_realizefn;
124 dc->props = debugcon_isa_properties;
125 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
126}
127
128static const TypeInfo debugcon_isa_info = {
129 .name = TYPE_ISA_DEBUGCON_DEVICE,
130 .parent = TYPE_ISA_DEVICE,
131 .instance_size = sizeof(ISADebugconState),
132 .class_init = debugcon_isa_class_initfn,
133};
134
135static void debugcon_register_types(void)
136{
137 type_register_static(&debugcon_isa_info);
138}
139
140type_init(debugcon_register_types)
141