]>
Commit | Line | Data |
---|---|---|
15d7dc4f MW |
1 | /* |
2 | * LatticeMico32 JTAG UART model. | |
3 | * | |
4 | * Copyright (c) 2010 Michael Walle <[email protected]> | |
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 | ||
83c9f4ca PB |
20 | #include "hw/hw.h" |
21 | #include "hw/sysbus.h" | |
15d7dc4f | 22 | #include "trace.h" |
dccfcd0e | 23 | #include "sysemu/char.h" |
15d7dc4f | 24 | |
0ee10242 | 25 | #include "hw/char/lm32_juart.h" |
15d7dc4f MW |
26 | |
27 | enum { | |
28 | LM32_JUART_MIN_SAVE_VERSION = 0, | |
29 | LM32_JUART_CURRENT_SAVE_VERSION = 0, | |
30 | LM32_JUART_MAX_SAVE_VERSION = 0, | |
31 | }; | |
32 | ||
33 | enum { | |
34 | JTX_FULL = (1<<8), | |
35 | }; | |
36 | ||
37 | enum { | |
38 | JRX_FULL = (1<<8), | |
39 | }; | |
40 | ||
a0b97927 AF |
41 | #define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART) |
42 | ||
15d7dc4f | 43 | struct LM32JuartState { |
a0b97927 AF |
44 | SysBusDevice parent_obj; |
45 | ||
15d7dc4f MW |
46 | CharDriverState *chr; |
47 | ||
48 | uint32_t jtx; | |
49 | uint32_t jrx; | |
50 | }; | |
51 | typedef struct LM32JuartState LM32JuartState; | |
52 | ||
53 | uint32_t lm32_juart_get_jtx(DeviceState *d) | |
54 | { | |
a0b97927 | 55 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
56 | |
57 | trace_lm32_juart_get_jtx(s->jtx); | |
58 | return s->jtx; | |
59 | } | |
60 | ||
61 | uint32_t lm32_juart_get_jrx(DeviceState *d) | |
62 | { | |
a0b97927 | 63 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
64 | |
65 | trace_lm32_juart_get_jrx(s->jrx); | |
66 | return s->jrx; | |
67 | } | |
68 | ||
69 | void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx) | |
70 | { | |
a0b97927 | 71 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
72 | unsigned char ch = jtx & 0xff; |
73 | ||
74 | trace_lm32_juart_set_jtx(s->jtx); | |
75 | ||
76 | s->jtx = jtx; | |
77 | if (s->chr) { | |
02d3bf7f | 78 | qemu_chr_fe_write_all(s->chr, &ch, 1); |
15d7dc4f MW |
79 | } |
80 | } | |
81 | ||
82 | void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx) | |
83 | { | |
a0b97927 | 84 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
85 | |
86 | trace_lm32_juart_set_jrx(s->jrx); | |
87 | s->jrx &= ~JRX_FULL; | |
88 | } | |
89 | ||
90 | static void juart_rx(void *opaque, const uint8_t *buf, int size) | |
91 | { | |
92 | LM32JuartState *s = opaque; | |
93 | ||
94 | s->jrx = *buf | JRX_FULL; | |
95 | } | |
96 | ||
97 | static int juart_can_rx(void *opaque) | |
98 | { | |
99 | LM32JuartState *s = opaque; | |
100 | ||
101 | return !(s->jrx & JRX_FULL); | |
102 | } | |
103 | ||
104 | static void juart_event(void *opaque, int event) | |
105 | { | |
106 | } | |
107 | ||
108 | static void juart_reset(DeviceState *d) | |
109 | { | |
a0b97927 | 110 | LM32JuartState *s = LM32_JUART(d); |
15d7dc4f MW |
111 | |
112 | s->jtx = 0; | |
113 | s->jrx = 0; | |
114 | } | |
115 | ||
116 | static int lm32_juart_init(SysBusDevice *dev) | |
117 | { | |
a0b97927 | 118 | LM32JuartState *s = LM32_JUART(dev); |
15d7dc4f | 119 | |
d71b22bb | 120 | /* FIXME use a qdev chardev prop instead of qemu_char_get_next_serial() */ |
0beb4942 | 121 | s->chr = qemu_char_get_next_serial(); |
15d7dc4f MW |
122 | if (s->chr) { |
123 | qemu_chr_add_handlers(s->chr, juart_can_rx, juart_rx, juart_event, s); | |
124 | } | |
125 | ||
126 | return 0; | |
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 | ||
999e12bb AL |
140 | static void lm32_juart_class_init(ObjectClass *klass, void *data) |
141 | { | |
39bffca2 | 142 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb AL |
143 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
144 | ||
145 | k->init = lm32_juart_init; | |
39bffca2 AL |
146 | dc->reset = juart_reset; |
147 | dc->vmsd = &vmstate_lm32_juart; | |
9f9bdf43 MA |
148 | /* Reason: init() method uses qemu_char_get_next_serial() */ |
149 | dc->cannot_instantiate_with_device_add_yet = true; | |
999e12bb AL |
150 | } |
151 | ||
8c43a6f0 | 152 | static const TypeInfo lm32_juart_info = { |
a0b97927 | 153 | .name = TYPE_LM32_JUART, |
39bffca2 AL |
154 | .parent = TYPE_SYS_BUS_DEVICE, |
155 | .instance_size = sizeof(LM32JuartState), | |
156 | .class_init = lm32_juart_class_init, | |
15d7dc4f MW |
157 | }; |
158 | ||
83f7d43a | 159 | static void lm32_juart_register_types(void) |
15d7dc4f | 160 | { |
39bffca2 | 161 | type_register_static(&lm32_juart_info); |
15d7dc4f MW |
162 | } |
163 | ||
83f7d43a | 164 | type_init(lm32_juart_register_types) |