Merge remote-tracking branch 'remotes/philmd/tags/pflash-20210511' into staging
[qemu.git] / hw / char / lm32_juart.c
1 /*
2 * LatticeMico32 JTAG UART model.
3 *
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "hw/sysbus.h"
22 #include "migration/vmstate.h"
23 #include "qemu/module.h"
24 #include "trace.h"
25 #include "chardev/char-fe.h"
26
27 #include "hw/char/lm32_juart.h"
28 #include "hw/qdev-properties.h"
29 #include "hw/qdev-properties-system.h"
30 #include "qom/object.h"
31
32 enum {
33 LM32_JUART_MIN_SAVE_VERSION = 0,
34 LM32_JUART_CURRENT_SAVE_VERSION = 0,
35 LM32_JUART_MAX_SAVE_VERSION = 0,
36 };
37
38 enum {
39 JTX_FULL = (1<<8),
40 };
41
42 enum {
43 JRX_FULL = (1<<8),
44 };
45
46 OBJECT_DECLARE_SIMPLE_TYPE(LM32JuartState, LM32_JUART)
47
48 struct LM32JuartState {
49 SysBusDevice parent_obj;
50
51 CharBackend chr;
52
53 uint32_t jtx;
54 uint32_t jrx;
55 };
56
57 uint32_t lm32_juart_get_jtx(DeviceState *d)
58 {
59 LM32JuartState *s = LM32_JUART(d);
60
61 trace_lm32_juart_get_jtx(s->jtx);
62 return s->jtx;
63 }
64
65 uint32_t lm32_juart_get_jrx(DeviceState *d)
66 {
67 LM32JuartState *s = LM32_JUART(d);
68
69 trace_lm32_juart_get_jrx(s->jrx);
70 return s->jrx;
71 }
72
73 void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
74 {
75 LM32JuartState *s = LM32_JUART(d);
76 unsigned char ch = jtx & 0xff;
77
78 trace_lm32_juart_set_jtx(s->jtx);
79
80 s->jtx = jtx;
81 /* XXX this blocks entire thread. Rewrite to use
82 * qemu_chr_fe_write and background I/O callbacks */
83 qemu_chr_fe_write_all(&s->chr, &ch, 1);
84 }
85
86 void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
87 {
88 LM32JuartState *s = LM32_JUART(d);
89
90 trace_lm32_juart_set_jrx(s->jrx);
91 s->jrx &= ~JRX_FULL;
92 }
93
94 static void juart_rx(void *opaque, const uint8_t *buf, int size)
95 {
96 LM32JuartState *s = opaque;
97
98 s->jrx = *buf | JRX_FULL;
99 }
100
101 static int juart_can_rx(void *opaque)
102 {
103 LM32JuartState *s = opaque;
104
105 return !(s->jrx & JRX_FULL);
106 }
107
108 static void juart_event(void *opaque, QEMUChrEvent event)
109 {
110 }
111
112 static void juart_reset(DeviceState *d)
113 {
114 LM32JuartState *s = LM32_JUART(d);
115
116 s->jtx = 0;
117 s->jrx = 0;
118 }
119
120 static void lm32_juart_realize(DeviceState *dev, Error **errp)
121 {
122 LM32JuartState *s = LM32_JUART(dev);
123
124 qemu_chr_fe_set_handlers(&s->chr, juart_can_rx, juart_rx,
125 juart_event, NULL, s, NULL, true);
126 }
127
128 static const VMStateDescription vmstate_lm32_juart = {
129 .name = "lm32-juart",
130 .version_id = 1,
131 .minimum_version_id = 1,
132 .fields = (VMStateField[]) {
133 VMSTATE_UINT32(jtx, LM32JuartState),
134 VMSTATE_UINT32(jrx, LM32JuartState),
135 VMSTATE_END_OF_LIST()
136 }
137 };
138
139 static Property lm32_juart_properties[] = {
140 DEFINE_PROP_CHR("chardev", LM32JuartState, chr),
141 DEFINE_PROP_END_OF_LIST(),
142 };
143
144 static void lm32_juart_class_init(ObjectClass *klass, void *data)
145 {
146 DeviceClass *dc = DEVICE_CLASS(klass);
147
148 dc->reset = juart_reset;
149 dc->vmsd = &vmstate_lm32_juart;
150 device_class_set_props(dc, lm32_juart_properties);
151 dc->realize = lm32_juart_realize;
152 }
153
154 static const TypeInfo lm32_juart_info = {
155 .name = TYPE_LM32_JUART,
156 .parent = TYPE_SYS_BUS_DEVICE,
157 .instance_size = sizeof(LM32JuartState),
158 .class_init = lm32_juart_class_init,
159 };
160
161 static void lm32_juart_register_types(void)
162 {
163 type_register_static(&lm32_juart_info);
164 }
165
166 type_init(lm32_juart_register_types)