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.1 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 "qemu/osdep.h"
21 #include "hw/sysbus.h"
22 #include "migration/vmstate.h"
23 #include "qemu/module.h"
25 #include "chardev/char-fe.h"
27 #include "hw/char/lm32_juart.h"
28 #include "hw/qdev-properties.h"
29 #include "hw/qdev-properties-system.h"
30 #include "qom/object.h"
33 LM32_JUART_MIN_SAVE_VERSION
= 0,
34 LM32_JUART_CURRENT_SAVE_VERSION
= 0,
35 LM32_JUART_MAX_SAVE_VERSION
= 0,
46 OBJECT_DECLARE_SIMPLE_TYPE(LM32JuartState
, LM32_JUART
)
48 struct LM32JuartState
{
49 SysBusDevice parent_obj
;
57 uint32_t lm32_juart_get_jtx(DeviceState
*d
)
59 LM32JuartState
*s
= LM32_JUART(d
);
61 trace_lm32_juart_get_jtx(s
->jtx
);
65 uint32_t lm32_juart_get_jrx(DeviceState
*d
)
67 LM32JuartState
*s
= LM32_JUART(d
);
69 trace_lm32_juart_get_jrx(s
->jrx
);
73 void lm32_juart_set_jtx(DeviceState
*d
, uint32_t jtx
)
75 LM32JuartState
*s
= LM32_JUART(d
);
76 unsigned char ch
= jtx
& 0xff;
78 trace_lm32_juart_set_jtx(s
->jtx
);
81 /* XXX this blocks entire thread. Rewrite to use
82 * qemu_chr_fe_write and background I/O callbacks */
83 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
86 void lm32_juart_set_jrx(DeviceState
*d
, uint32_t jtx
)
88 LM32JuartState
*s
= LM32_JUART(d
);
90 trace_lm32_juart_set_jrx(s
->jrx
);
94 static void juart_rx(void *opaque
, const uint8_t *buf
, int size
)
96 LM32JuartState
*s
= opaque
;
98 s
->jrx
= *buf
| JRX_FULL
;
101 static int juart_can_rx(void *opaque
)
103 LM32JuartState
*s
= opaque
;
105 return !(s
->jrx
& JRX_FULL
);
108 static void juart_event(void *opaque
, QEMUChrEvent event
)
112 static void juart_reset(DeviceState
*d
)
114 LM32JuartState
*s
= LM32_JUART(d
);
120 static void lm32_juart_realize(DeviceState
*dev
, Error
**errp
)
122 LM32JuartState
*s
= LM32_JUART(dev
);
124 qemu_chr_fe_set_handlers(&s
->chr
, juart_can_rx
, juart_rx
,
125 juart_event
, NULL
, s
, NULL
, true);
128 static const VMStateDescription vmstate_lm32_juart
= {
129 .name
= "lm32-juart",
131 .minimum_version_id
= 1,
132 .fields
= (VMStateField
[]) {
133 VMSTATE_UINT32(jtx
, LM32JuartState
),
134 VMSTATE_UINT32(jrx
, LM32JuartState
),
135 VMSTATE_END_OF_LIST()
139 static Property lm32_juart_properties
[] = {
140 DEFINE_PROP_CHR("chardev", LM32JuartState
, chr
),
141 DEFINE_PROP_END_OF_LIST(),
144 static void lm32_juart_class_init(ObjectClass
*klass
, void *data
)
146 DeviceClass
*dc
= DEVICE_CLASS(klass
);
148 dc
->reset
= juart_reset
;
149 dc
->vmsd
= &vmstate_lm32_juart
;
150 device_class_set_props(dc
, lm32_juart_properties
);
151 dc
->realize
= lm32_juart_realize
;
154 static const TypeInfo lm32_juart_info
= {
155 .name
= TYPE_LM32_JUART
,
156 .parent
= TYPE_SYS_BUS_DEVICE
,
157 .instance_size
= sizeof(LM32JuartState
),
158 .class_init
= lm32_juart_class_init
,
161 static void lm32_juart_register_types(void)
163 type_register_static(&lm32_juart_info
);
166 type_init(lm32_juart_register_types
)