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