]> git.proxmox.com Git - mirror_qemu.git/blame - hw/char/lm32_juart.c
hw: replace most use of qemu_chr_fe_write with qemu_chr_fe_write_all
[mirror_qemu.git] / hw / char / lm32_juart.c
CommitLineData
15d7dc4f
MW
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 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
ea99dde1 20#include "qemu/osdep.h"
83c9f4ca
PB
21#include "hw/hw.h"
22#include "hw/sysbus.h"
15d7dc4f 23#include "trace.h"
dccfcd0e 24#include "sysemu/char.h"
15d7dc4f 25
0ee10242 26#include "hw/char/lm32_juart.h"
15d7dc4f
MW
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
a0b97927
AF
42#define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
43
15d7dc4f 44struct LM32JuartState {
a0b97927
AF
45 SysBusDevice parent_obj;
46
15d7dc4f
MW
47 CharDriverState *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{
a0b97927 56 LM32JuartState *s = LM32_JUART(d);
15d7dc4f
MW
57
58 trace_lm32_juart_get_jtx(s->jtx);
59 return s->jtx;
60}
61
62uint32_t lm32_juart_get_jrx(DeviceState *d)
63{
a0b97927 64 LM32JuartState *s = LM32_JUART(d);
15d7dc4f
MW
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{
a0b97927 72 LM32JuartState *s = LM32_JUART(d);
15d7dc4f
MW
73 unsigned char ch = jtx & 0xff;
74
75 trace_lm32_juart_set_jtx(s->jtx);
76
77 s->jtx = jtx;
78 if (s->chr) {
6ab3fc32
DB
79 /* XXX this blocks entire thread. Rewrite to use
80 * qemu_chr_fe_write and background I/O callbacks */
02d3bf7f 81 qemu_chr_fe_write_all(s->chr, &ch, 1);
15d7dc4f
MW
82 }
83}
84
85void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
86{
a0b97927 87 LM32JuartState *s = LM32_JUART(d);
15d7dc4f
MW
88
89 trace_lm32_juart_set_jrx(s->jrx);
90 s->jrx &= ~JRX_FULL;
91}
92
93static void juart_rx(void *opaque, const uint8_t *buf, int size)
94{
95 LM32JuartState *s = opaque;
96
97 s->jrx = *buf | JRX_FULL;
98}
99
100static int juart_can_rx(void *opaque)
101{
102 LM32JuartState *s = opaque;
103
104 return !(s->jrx & JRX_FULL);
105}
106
107static void juart_event(void *opaque, int event)
108{
109}
110
111static void juart_reset(DeviceState *d)
112{
a0b97927 113 LM32JuartState *s = LM32_JUART(d);
15d7dc4f
MW
114
115 s->jtx = 0;
116 s->jrx = 0;
117}
118
c2ddaa62 119static void lm32_juart_realize(DeviceState *dev, Error **errp)
15d7dc4f 120{
a0b97927 121 LM32JuartState *s = LM32_JUART(dev);
15d7dc4f 122
15d7dc4f
MW
123 if (s->chr) {
124 qemu_chr_add_handlers(s->chr, juart_can_rx, juart_rx, juart_event, s);
125 }
15d7dc4f
MW
126}
127
128static const VMStateDescription vmstate_lm32_juart = {
129 .name = "lm32-juart",
130 .version_id = 1,
131 .minimum_version_id = 1,
35d08458 132 .fields = (VMStateField[]) {
15d7dc4f
MW
133 VMSTATE_UINT32(jtx, LM32JuartState),
134 VMSTATE_UINT32(jrx, LM32JuartState),
135 VMSTATE_END_OF_LIST()
136 }
137};
138
c2ddaa62
XZ
139static Property lm32_juart_properties[] = {
140 DEFINE_PROP_CHR("chardev", LM32JuartState, chr),
141 DEFINE_PROP_END_OF_LIST(),
142};
143
999e12bb
AL
144static void lm32_juart_class_init(ObjectClass *klass, void *data)
145{
39bffca2 146 DeviceClass *dc = DEVICE_CLASS(klass);
999e12bb 147
39bffca2
AL
148 dc->reset = juart_reset;
149 dc->vmsd = &vmstate_lm32_juart;
c2ddaa62
XZ
150 dc->props = lm32_juart_properties;
151 dc->realize = lm32_juart_realize;
999e12bb
AL
152}
153
8c43a6f0 154static const TypeInfo lm32_juart_info = {
a0b97927 155 .name = TYPE_LM32_JUART,
39bffca2
AL
156 .parent = TYPE_SYS_BUS_DEVICE,
157 .instance_size = sizeof(LM32JuartState),
158 .class_init = lm32_juart_class_init,
15d7dc4f
MW
159};
160
83f7d43a 161static void lm32_juart_register_types(void)
15d7dc4f 162{
39bffca2 163 type_register_static(&lm32_juart_info);
15d7dc4f
MW
164}
165
83f7d43a 166type_init(lm32_juart_register_types)