These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / 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 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/hw.h"
22 #include "hw/sysbus.h"
23 #include "trace.h"
24 #include "sysemu/char.h"
25
26 #include "hw/char/lm32_juart.h"
27
28 enum {
29     LM32_JUART_MIN_SAVE_VERSION = 0,
30     LM32_JUART_CURRENT_SAVE_VERSION = 0,
31     LM32_JUART_MAX_SAVE_VERSION = 0,
32 };
33
34 enum {
35     JTX_FULL = (1<<8),
36 };
37
38 enum {
39     JRX_FULL = (1<<8),
40 };
41
42 #define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
43
44 struct LM32JuartState {
45     SysBusDevice parent_obj;
46
47     CharDriverState *chr;
48
49     uint32_t jtx;
50     uint32_t jrx;
51 };
52 typedef struct LM32JuartState LM32JuartState;
53
54 uint32_t lm32_juart_get_jtx(DeviceState *d)
55 {
56     LM32JuartState *s = LM32_JUART(d);
57
58     trace_lm32_juart_get_jtx(s->jtx);
59     return s->jtx;
60 }
61
62 uint32_t lm32_juart_get_jrx(DeviceState *d)
63 {
64     LM32JuartState *s = LM32_JUART(d);
65
66     trace_lm32_juart_get_jrx(s->jrx);
67     return s->jrx;
68 }
69
70 void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
71 {
72     LM32JuartState *s = LM32_JUART(d);
73     unsigned char ch = jtx & 0xff;
74
75     trace_lm32_juart_set_jtx(s->jtx);
76
77     s->jtx = jtx;
78     if (s->chr) {
79         qemu_chr_fe_write_all(s->chr, &ch, 1);
80     }
81 }
82
83 void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
84 {
85     LM32JuartState *s = LM32_JUART(d);
86
87     trace_lm32_juart_set_jrx(s->jrx);
88     s->jrx &= ~JRX_FULL;
89 }
90
91 static void juart_rx(void *opaque, const uint8_t *buf, int size)
92 {
93     LM32JuartState *s = opaque;
94
95     s->jrx = *buf | JRX_FULL;
96 }
97
98 static int juart_can_rx(void *opaque)
99 {
100     LM32JuartState *s = opaque;
101
102     return !(s->jrx & JRX_FULL);
103 }
104
105 static void juart_event(void *opaque, int event)
106 {
107 }
108
109 static void juart_reset(DeviceState *d)
110 {
111     LM32JuartState *s = LM32_JUART(d);
112
113     s->jtx = 0;
114     s->jrx = 0;
115 }
116
117 static int lm32_juart_init(SysBusDevice *dev)
118 {
119     LM32JuartState *s = LM32_JUART(dev);
120
121     /* FIXME use a qdev chardev prop instead of qemu_char_get_next_serial() */
122     s->chr = qemu_char_get_next_serial();
123     if (s->chr) {
124         qemu_chr_add_handlers(s->chr, juart_can_rx, juart_rx, juart_event, s);
125     }
126
127     return 0;
128 }
129
130 static const VMStateDescription vmstate_lm32_juart = {
131     .name = "lm32-juart",
132     .version_id = 1,
133     .minimum_version_id = 1,
134     .fields = (VMStateField[]) {
135         VMSTATE_UINT32(jtx, LM32JuartState),
136         VMSTATE_UINT32(jrx, LM32JuartState),
137         VMSTATE_END_OF_LIST()
138     }
139 };
140
141 static void lm32_juart_class_init(ObjectClass *klass, void *data)
142 {
143     DeviceClass *dc = DEVICE_CLASS(klass);
144     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
145
146     k->init = lm32_juart_init;
147     dc->reset = juart_reset;
148     dc->vmsd = &vmstate_lm32_juart;
149     /* Reason: init() method uses qemu_char_get_next_serial() */
150     dc->cannot_instantiate_with_device_add_yet = true;
151 }
152
153 static const TypeInfo lm32_juart_info = {
154     .name          = TYPE_LM32_JUART,
155     .parent        = TYPE_SYS_BUS_DEVICE,
156     .instance_size = sizeof(LM32JuartState),
157     .class_init    = lm32_juart_class_init,
158 };
159
160 static void lm32_juart_register_types(void)
161 {
162     type_register_static(&lm32_juart_info);
163 }
164
165 type_init(lm32_juart_register_types)