1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include "qemu/osdep.h"
21#include "hw/hw.h"
22#include "hw/sysbus.h"
23#include "trace.h"
24#include "chardev/char-fe.h"
25
26#include "hw/char/lm32_juart.h"
27
28enum {
29 LM32_JUART_MIN_SAVE_VERSION = 0,
30 LM32_JUART_CURRENT_SAVE_VERSION = 0,
31 LM32_JUART_MAX_SAVE_VERSION = 0,
32};
33
34enum {
35 JTX_FULL = (1<<8),
36};
37
38enum {
39 JRX_FULL = (1<<8),
40};
41
42#define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
43
44struct LM32JuartState {
45 SysBusDevice parent_obj;
46
47 CharBackend chr;
48
49 uint32_t jtx;
50 uint32_t jrx;
51};
52typedef struct LM32JuartState LM32JuartState;
53
54uint32_t lm32_juart_get_jtx(DeviceState *d)
55{
56 LM32JuartState *s = LM32_JUART(d);
57
58 trace_lm32_juart_get_jtx(s->jtx);
59 return s->jtx;
60}
61
62uint32_t lm32_juart_get_jrx(DeviceState *d)
63{
64 LM32JuartState *s = LM32_JUART(d);
65
66 trace_lm32_juart_get_jrx(s->jrx);
67 return s->jrx;
68}
69
70void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
71{
72 LM32JuartState *s = LM32_JUART(d);
73 unsigned char ch = jtx & 0xff;
74
75 trace_lm32_juart_set_jtx(s->jtx);
76
77 s->jtx = jtx;
78
79
80 qemu_chr_fe_write_all(&s->chr, &ch, 1);
81}
82
83void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
84{
85 LM32JuartState *s = LM32_JUART(d);
86
87 trace_lm32_juart_set_jrx(s->jrx);
88 s->jrx &= ~JRX_FULL;
89}
90
91static void juart_rx(void *opaque, const uint8_t *buf, int size)
92{
93 LM32JuartState *s = opaque;
94
95 s->jrx = *buf | JRX_FULL;
96}
97
98static int juart_can_rx(void *opaque)
99{
100 LM32JuartState *s = opaque;
101
102 return !(s->jrx & JRX_FULL);
103}
104
105static void juart_event(void *opaque, int event)
106{
107}
108
109static void juart_reset(DeviceState *d)
110{
111 LM32JuartState *s = LM32_JUART(d);
112
113 s->jtx = 0;
114 s->jrx = 0;
115}
116
117static void lm32_juart_realize(DeviceState *dev, Error **errp)
118{
119 LM32JuartState *s = LM32_JUART(dev);
120
121 qemu_chr_fe_set_handlers(&s->chr, juart_can_rx, juart_rx,
122 juart_event, NULL, s, NULL, true);
123}
124
125static const VMStateDescription vmstate_lm32_juart = {
126 .name = "lm32-juart",
127 .version_id = 1,
128 .minimum_version_id = 1,
129 .fields = (VMStateField[]) {
130 VMSTATE_UINT32(jtx, LM32JuartState),
131 VMSTATE_UINT32(jrx, LM32JuartState),
132 VMSTATE_END_OF_LIST()
133 }
134};
135
136static Property lm32_juart_properties[] = {
137 DEFINE_PROP_CHR("chardev", LM32JuartState, chr),
138 DEFINE_PROP_END_OF_LIST(),
139};
140
141static void lm32_juart_class_init(ObjectClass *klass, void *data)
142{
143 DeviceClass *dc = DEVICE_CLASS(klass);
144
145 dc->reset = juart_reset;
146 dc->vmsd = &vmstate_lm32_juart;
147 dc->props = lm32_juart_properties;
148 dc->realize = lm32_juart_realize;
149}
150
151static const TypeInfo lm32_juart_info = {
152 .name = TYPE_LM32_JUART,
153 .parent = TYPE_SYS_BUS_DEVICE,
154 .instance_size = sizeof(LM32JuartState),
155 .class_init = lm32_juart_class_init,
156};
157
158static void lm32_juart_register_types(void)
159{
160 type_register_static(&lm32_juart_info);
161}
162
163type_init(lm32_juart_register_types)
164