ditto hw/xilinx_uartlite.c
[qemu/aliguori.git] / hw / lm32_juart.c
blob06155089f6c4bc850ce5f24e76aec4ef4dcb0352
1 /*
2 * LatticeMico32 JTAG UART model.
4 * Copyright (c) 2010 Michael Walle <michael@walle.cc>
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.
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.
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/>.
20 #include "hw.h"
21 #include "sysbus.h"
22 #include "trace.h"
23 #include "qemu-char.h"
25 #include "lm32_juart.h"
27 enum {
28 LM32_JUART_MIN_SAVE_VERSION = 0,
29 LM32_JUART_CURRENT_SAVE_VERSION = 0,
30 LM32_JUART_MAX_SAVE_VERSION = 0,
33 enum {
34 JTX_FULL = (1<<8),
37 enum {
38 JRX_FULL = (1<<8),
41 struct LM32JuartState {
42 SysBusDevice busdev;
43 CharDriverState *chr;
45 uint32_t jtx;
46 uint32_t jrx;
48 typedef struct LM32JuartState LM32JuartState;
50 static void juart_update_handlers(LM32JuartState *s);
52 uint32_t lm32_juart_get_jtx(DeviceState *d)
54 LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
56 trace_lm32_juart_get_jtx(s->jtx);
57 return s->jtx;
60 uint32_t lm32_juart_get_jrx(DeviceState *d)
62 LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
64 trace_lm32_juart_get_jrx(s->jrx);
65 return s->jrx;
68 void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
70 LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
71 unsigned char ch = jtx & 0xff;
73 trace_lm32_juart_set_jtx(s->jtx);
75 s->jtx = jtx;
76 if (s->chr) {
77 qemu_chr_fe_write(s->chr, &ch, 1);
81 void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
83 LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
85 trace_lm32_juart_set_jrx(s->jrx);
86 s->jrx &= ~JRX_FULL;
87 juart_update_handlers(s);
90 static void juart_rx(LM32JuartState *s, const uint8_t *buf, int size)
92 s->jrx = *buf | JRX_FULL;
93 juart_update_handlers(s);
96 static int juart_can_rx(LM32JuartState *s)
98 return !(s->jrx & JRX_FULL);
101 static void juart_rx_handler(void *opaque)
103 LM32JuartState *s = opaque;
104 uint8_t buf[32];
105 int size;
107 size = juart_can_rx(s);
108 size = MIN(size, sizeof(buf));
109 size = qemu_chr_fe_read(s->chr, buf, size);
111 juart_rx(s, buf, size);
114 static void juart_update_handlers(LM32JuartState *s)
116 if (juart_can_rx(s) > 0) {
117 qemu_chr_fe_set_handlers(s->chr, juart_rx_handler, NULL, NULL, s);
118 } else {
119 qemu_chr_fe_set_handlers(s->chr, NULL, NULL, NULL, s);
123 static void juart_reset(DeviceState *d)
125 LM32JuartState *s = container_of(d, LM32JuartState, busdev.qdev);
127 s->jtx = 0;
128 s->jrx = 0;
129 juart_update_handlers(s);
132 static int lm32_juart_init(SysBusDevice *dev)
134 LM32JuartState *s = FROM_SYSBUS(typeof(*s), dev);
136 s->chr = qdev_init_chardev(&dev->qdev);
137 if (s->chr) {
138 qemu_chr_fe_open(s->chr);
139 juart_update_handlers(s);
142 return 0;
145 static const VMStateDescription vmstate_lm32_juart = {
146 .name = "lm32-juart",
147 .version_id = 1,
148 .minimum_version_id = 1,
149 .minimum_version_id_old = 1,
150 .fields = (VMStateField[]) {
151 VMSTATE_UINT32(jtx, LM32JuartState),
152 VMSTATE_UINT32(jrx, LM32JuartState),
153 VMSTATE_END_OF_LIST()
157 static SysBusDeviceInfo lm32_juart_info = {
158 .init = lm32_juart_init,
159 .qdev.name = "lm32-juart",
160 .qdev.size = sizeof(LM32JuartState),
161 .qdev.vmsd = &vmstate_lm32_juart,
162 .qdev.reset = juart_reset,
165 static void lm32_juart_register(void)
167 sysbus_register_withprop(&lm32_juart_info);
170 device_init(lm32_juart_register)