]>
Commit | Line | Data |
---|---|---|
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 | 21 | #include "hw/sysbus.h" |
d6454270 | 22 | #include "migration/vmstate.h" |
0b8fa32f | 23 | #include "qemu/module.h" |
15d7dc4f | 24 | #include "trace.h" |
4d43a603 | 25 | #include "chardev/char-fe.h" |
15d7dc4f | 26 | |
0ee10242 | 27 | #include "hw/char/lm32_juart.h" |
a27bd6c7 | 28 | #include "hw/qdev-properties.h" |
db1015e9 | 29 | #include "qom/object.h" |
15d7dc4f MW |
30 | |
31 | enum { | |
32 | LM32_JUART_MIN_SAVE_VERSION = 0, | |
33 | LM32_JUART_CURRENT_SAVE_VERSION = 0, | |
34 | LM32_JUART_MAX_SAVE_VERSION = 0, | |
35 | }; | |
36 | ||
37 | enum { | |
38 | JTX_FULL = (1<<8), | |
39 | }; | |
40 | ||
41 | enum { | |
42 | JRX_FULL = (1<<8), | |
43 | }; | |
44 | ||
db1015e9 | 45 | typedef struct LM32JuartState LM32JuartState; |
8110fa1d EH |
46 | DECLARE_INSTANCE_CHECKER(LM32JuartState, LM32_JUART, |
47 | TYPE_LM32_JUART) | |
a0b97927 | 48 | |
15d7dc4f | 49 | struct LM32JuartState { |
a0b97927 AF |
50 | SysBusDevice parent_obj; |
51 | ||
becdfa00 | 52 | CharBackend chr; |
15d7dc4f MW |
53 | |
54 | uint32_t jtx; | |
55 | uint32_t jrx; | |
56 | }; | |
15d7dc4f MW |
57 | |
58 | uint32_t lm32_juart_get_jtx(DeviceState *d) | |
59 | { | |
a0b97927 | 60 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
61 | |
62 | trace_lm32_juart_get_jtx(s->jtx); | |
63 | return s->jtx; | |
64 | } | |
65 | ||
66 | uint32_t lm32_juart_get_jrx(DeviceState *d) | |
67 | { | |
a0b97927 | 68 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
69 | |
70 | trace_lm32_juart_get_jrx(s->jrx); | |
71 | return s->jrx; | |
72 | } | |
73 | ||
74 | void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx) | |
75 | { | |
a0b97927 | 76 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
77 | unsigned char ch = jtx & 0xff; |
78 | ||
79 | trace_lm32_juart_set_jtx(s->jtx); | |
80 | ||
81 | s->jtx = jtx; | |
fa394ed6 MAL |
82 | /* XXX this blocks entire thread. Rewrite to use |
83 | * qemu_chr_fe_write and background I/O callbacks */ | |
84 | qemu_chr_fe_write_all(&s->chr, &ch, 1); | |
15d7dc4f MW |
85 | } |
86 | ||
87 | void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx) | |
88 | { | |
a0b97927 | 89 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
90 | |
91 | trace_lm32_juart_set_jrx(s->jrx); | |
92 | s->jrx &= ~JRX_FULL; | |
93 | } | |
94 | ||
95 | static void juart_rx(void *opaque, const uint8_t *buf, int size) | |
96 | { | |
97 | LM32JuartState *s = opaque; | |
98 | ||
99 | s->jrx = *buf | JRX_FULL; | |
100 | } | |
101 | ||
102 | static int juart_can_rx(void *opaque) | |
103 | { | |
104 | LM32JuartState *s = opaque; | |
105 | ||
106 | return !(s->jrx & JRX_FULL); | |
107 | } | |
108 | ||
083b266f | 109 | static void juart_event(void *opaque, QEMUChrEvent event) |
15d7dc4f MW |
110 | { |
111 | } | |
112 | ||
113 | static void juart_reset(DeviceState *d) | |
114 | { | |
a0b97927 | 115 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
116 | |
117 | s->jtx = 0; | |
118 | s->jrx = 0; | |
119 | } | |
120 | ||
c2ddaa62 | 121 | static void lm32_juart_realize(DeviceState *dev, Error **errp) |
15d7dc4f | 122 | { |
a0b97927 | 123 | LM32JuartState *s = LM32_JUART(dev); |
15d7dc4f | 124 | |
fa394ed6 | 125 | qemu_chr_fe_set_handlers(&s->chr, juart_can_rx, juart_rx, |
81517ba3 | 126 | juart_event, NULL, s, NULL, true); |
15d7dc4f MW |
127 | } |
128 | ||
129 | static const VMStateDescription vmstate_lm32_juart = { | |
130 | .name = "lm32-juart", | |
131 | .version_id = 1, | |
132 | .minimum_version_id = 1, | |
35d08458 | 133 | .fields = (VMStateField[]) { |
15d7dc4f MW |
134 | VMSTATE_UINT32(jtx, LM32JuartState), |
135 | VMSTATE_UINT32(jrx, LM32JuartState), | |
136 | VMSTATE_END_OF_LIST() | |
137 | } | |
138 | }; | |
139 | ||
c2ddaa62 XZ |
140 | static Property lm32_juart_properties[] = { |
141 | DEFINE_PROP_CHR("chardev", LM32JuartState, chr), | |
142 | DEFINE_PROP_END_OF_LIST(), | |
143 | }; | |
144 | ||
999e12bb AL |
145 | static void lm32_juart_class_init(ObjectClass *klass, void *data) |
146 | { | |
39bffca2 | 147 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 148 | |
39bffca2 AL |
149 | dc->reset = juart_reset; |
150 | dc->vmsd = &vmstate_lm32_juart; | |
4f67d30b | 151 | device_class_set_props(dc, lm32_juart_properties); |
c2ddaa62 | 152 | dc->realize = lm32_juart_realize; |
999e12bb AL |
153 | } |
154 | ||
8c43a6f0 | 155 | static const TypeInfo lm32_juart_info = { |
a0b97927 | 156 | .name = TYPE_LM32_JUART, |
39bffca2 AL |
157 | .parent = TYPE_SYS_BUS_DEVICE, |
158 | .instance_size = sizeof(LM32JuartState), | |
159 | .class_init = lm32_juart_class_init, | |
15d7dc4f MW |
160 | }; |
161 | ||
83f7d43a | 162 | static void lm32_juart_register_types(void) |
15d7dc4f | 163 | { |
39bffca2 | 164 | type_register_static(&lm32_juart_info); |
15d7dc4f MW |
165 | } |
166 | ||
83f7d43a | 167 | type_init(lm32_juart_register_types) |