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 "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"
31 LM32_JUART_MIN_SAVE_VERSION
= 0,
32 LM32_JUART_CURRENT_SAVE_VERSION
= 0,
33 LM32_JUART_MAX_SAVE_VERSION
= 0,
44 #define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
46 struct LM32JuartState
{
47 SysBusDevice parent_obj
;
54 typedef struct LM32JuartState LM32JuartState
;
56 uint32_t lm32_juart_get_jtx(DeviceState
*d
)
58 LM32JuartState
*s
= LM32_JUART(d
);
60 trace_lm32_juart_get_jtx(s
->jtx
);
64 uint32_t lm32_juart_get_jrx(DeviceState
*d
)
66 LM32JuartState
*s
= LM32_JUART(d
);
68 trace_lm32_juart_get_jrx(s
->jrx
);
72 void lm32_juart_set_jtx(DeviceState
*d
, uint32_t jtx
)
74 LM32JuartState
*s
= LM32_JUART(d
);
75 unsigned char ch
= jtx
& 0xff;
77 trace_lm32_juart_set_jtx(s
->jtx
);
80 /* XXX this blocks entire thread. Rewrite to use
81 * qemu_chr_fe_write and background I/O callbacks */
82 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
85 void lm32_juart_set_jrx(DeviceState
*d
, uint32_t jtx
)
87 LM32JuartState
*s
= LM32_JUART(d
);
89 trace_lm32_juart_set_jrx(s
->jrx
);
93 static void juart_rx(void *opaque
, const uint8_t *buf
, int size
)
95 LM32JuartState
*s
= opaque
;
97 s
->jrx
= *buf
| JRX_FULL
;
100 static int juart_can_rx(void *opaque
)
102 LM32JuartState
*s
= opaque
;
104 return !(s
->jrx
& JRX_FULL
);
107 static void juart_event(void *opaque
, QEMUChrEvent event
)
111 static void juart_reset(DeviceState
*d
)
113 LM32JuartState
*s
= LM32_JUART(d
);
119 static void lm32_juart_realize(DeviceState
*dev
, Error
**errp
)
121 LM32JuartState
*s
= LM32_JUART(dev
);
123 qemu_chr_fe_set_handlers(&s
->chr
, juart_can_rx
, juart_rx
,
124 juart_event
, NULL
, s
, NULL
, true);
127 static const VMStateDescription vmstate_lm32_juart
= {
128 .name
= "lm32-juart",
130 .minimum_version_id
= 1,
131 .fields
= (VMStateField
[]) {
132 VMSTATE_UINT32(jtx
, LM32JuartState
),
133 VMSTATE_UINT32(jrx
, LM32JuartState
),
134 VMSTATE_END_OF_LIST()
138 static Property lm32_juart_properties
[] = {
139 DEFINE_PROP_CHR("chardev", LM32JuartState
, chr
),
140 DEFINE_PROP_END_OF_LIST(),
143 static void lm32_juart_class_init(ObjectClass
*klass
, void *data
)
145 DeviceClass
*dc
= DEVICE_CLASS(klass
);
147 dc
->reset
= juart_reset
;
148 dc
->vmsd
= &vmstate_lm32_juart
;
149 device_class_set_props(dc
, lm32_juart_properties
);
150 dc
->realize
= lm32_juart_realize
;
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
,
160 static void lm32_juart_register_types(void)
162 type_register_static(&lm32_juart_info
);
165 type_init(lm32_juart_register_types
)