blob: cb1ac76731dcf3fab213ae284d8684e9f071d777 [file] [log] [blame]
Michael Walle15d7dc42011-02-17 23:45:08 +01001/*
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
Peter Maydellea99dde2016-01-26 18:16:57 +000020#include "qemu/osdep.h"
Paolo Bonzini83c9f4c2013-02-04 15:40:22 +010021#include "hw/hw.h"
22#include "hw/sysbus.h"
Michael Walle15d7dc42011-02-17 23:45:08 +010023#include "trace.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020024#include "sysemu/char.h"
Michael Walle15d7dc42011-02-17 23:45:08 +010025
Andreas Färber0ee10242013-07-24 22:49:02 +020026#include "hw/char/lm32_juart.h"
Michael Walle15d7dc42011-02-17 23:45:08 +010027
28enum {
29 LM32_JUART_MIN_SAVE_VERSION = 0,
30 LM32_JUART_CURRENT_SAVE_VERSION = 0,
31 LM32_JUART_MAX_SAVE_VERSION = 0,
32};
33
34enum {
35 JTX_FULL = (1<<8),
36};
37
38enum {
39 JRX_FULL = (1<<8),
40};
41
Andreas Färbera0b97922013-07-24 22:56:36 +020042#define LM32_JUART(obj) OBJECT_CHECK(LM32JuartState, (obj), TYPE_LM32_JUART)
43
Michael Walle15d7dc42011-02-17 23:45:08 +010044struct LM32JuartState {
Andreas Färbera0b97922013-07-24 22:56:36 +020045 SysBusDevice parent_obj;
46
Michael Walle15d7dc42011-02-17 23:45:08 +010047 CharDriverState *chr;
48
49 uint32_t jtx;
50 uint32_t jrx;
51};
52typedef struct LM32JuartState LM32JuartState;
53
54uint32_t lm32_juart_get_jtx(DeviceState *d)
55{
Andreas Färbera0b97922013-07-24 22:56:36 +020056 LM32JuartState *s = LM32_JUART(d);
Michael Walle15d7dc42011-02-17 23:45:08 +010057
58 trace_lm32_juart_get_jtx(s->jtx);
59 return s->jtx;
60}
61
62uint32_t lm32_juart_get_jrx(DeviceState *d)
63{
Andreas Färbera0b97922013-07-24 22:56:36 +020064 LM32JuartState *s = LM32_JUART(d);
Michael Walle15d7dc42011-02-17 23:45:08 +010065
66 trace_lm32_juart_get_jrx(s->jrx);
67 return s->jrx;
68}
69
70void lm32_juart_set_jtx(DeviceState *d, uint32_t jtx)
71{
Andreas Färbera0b97922013-07-24 22:56:36 +020072 LM32JuartState *s = LM32_JUART(d);
Michael Walle15d7dc42011-02-17 23:45:08 +010073 unsigned char ch = jtx & 0xff;
74
75 trace_lm32_juart_set_jtx(s->jtx);
76
77 s->jtx = jtx;
78 if (s->chr) {
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +010079 /* XXX this blocks entire thread. Rewrite to use
80 * qemu_chr_fe_write and background I/O callbacks */
Michael Walle02d3bf72013-09-16 18:29:32 +020081 qemu_chr_fe_write_all(s->chr, &ch, 1);
Michael Walle15d7dc42011-02-17 23:45:08 +010082 }
83}
84
85void lm32_juart_set_jrx(DeviceState *d, uint32_t jtx)
86{
Andreas Färbera0b97922013-07-24 22:56:36 +020087 LM32JuartState *s = LM32_JUART(d);
Michael Walle15d7dc42011-02-17 23:45:08 +010088
89 trace_lm32_juart_set_jrx(s->jrx);
90 s->jrx &= ~JRX_FULL;
91}
92
93static void juart_rx(void *opaque, const uint8_t *buf, int size)
94{
95 LM32JuartState *s = opaque;
96
97 s->jrx = *buf | JRX_FULL;
98}
99
100static int juart_can_rx(void *opaque)
101{
102 LM32JuartState *s = opaque;
103
104 return !(s->jrx & JRX_FULL);
105}
106
107static void juart_event(void *opaque, int event)
108{
109}
110
111static void juart_reset(DeviceState *d)
112{
Andreas Färbera0b97922013-07-24 22:56:36 +0200113 LM32JuartState *s = LM32_JUART(d);
Michael Walle15d7dc42011-02-17 23:45:08 +0100114
115 s->jtx = 0;
116 s->jrx = 0;
117}
118
xiaoqiang zhaoc2ddaa62016-05-25 14:39:02 +0800119static void lm32_juart_realize(DeviceState *dev, Error **errp)
Michael Walle15d7dc42011-02-17 23:45:08 +0100120{
Andreas Färbera0b97922013-07-24 22:56:36 +0200121 LM32JuartState *s = LM32_JUART(dev);
Michael Walle15d7dc42011-02-17 23:45:08 +0100122
Michael Walle15d7dc42011-02-17 23:45:08 +0100123 if (s->chr) {
124 qemu_chr_add_handlers(s->chr, juart_can_rx, juart_rx, juart_event, s);
125 }
Michael Walle15d7dc42011-02-17 23:45:08 +0100126}
127
128static const VMStateDescription vmstate_lm32_juart = {
129 .name = "lm32-juart",
130 .version_id = 1,
131 .minimum_version_id = 1,
Juan Quintela35d08452014-04-16 16:01:33 +0200132 .fields = (VMStateField[]) {
Michael Walle15d7dc42011-02-17 23:45:08 +0100133 VMSTATE_UINT32(jtx, LM32JuartState),
134 VMSTATE_UINT32(jrx, LM32JuartState),
135 VMSTATE_END_OF_LIST()
136 }
137};
138
xiaoqiang zhaoc2ddaa62016-05-25 14:39:02 +0800139static Property lm32_juart_properties[] = {
140 DEFINE_PROP_CHR("chardev", LM32JuartState, chr),
141 DEFINE_PROP_END_OF_LIST(),
142};
143
Anthony Liguori999e12b2012-01-24 13:12:29 -0600144static void lm32_juart_class_init(ObjectClass *klass, void *data)
145{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600146 DeviceClass *dc = DEVICE_CLASS(klass);
Anthony Liguori999e12b2012-01-24 13:12:29 -0600147
Anthony Liguori39bffca2011-12-07 21:34:16 -0600148 dc->reset = juart_reset;
149 dc->vmsd = &vmstate_lm32_juart;
xiaoqiang zhaoc2ddaa62016-05-25 14:39:02 +0800150 dc->props = lm32_juart_properties;
151 dc->realize = lm32_juart_realize;
Anthony Liguori999e12b2012-01-24 13:12:29 -0600152}
153
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100154static const TypeInfo lm32_juart_info = {
Andreas Färbera0b97922013-07-24 22:56:36 +0200155 .name = TYPE_LM32_JUART,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600156 .parent = TYPE_SYS_BUS_DEVICE,
157 .instance_size = sizeof(LM32JuartState),
158 .class_init = lm32_juart_class_init,
Michael Walle15d7dc42011-02-17 23:45:08 +0100159};
160
Andreas Färber83f7d432012-02-09 15:20:55 +0100161static void lm32_juart_register_types(void)
Michael Walle15d7dc42011-02-17 23:45:08 +0100162{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600163 type_register_static(&lm32_juart_info);
Michael Walle15d7dc42011-02-17 23:45:08 +0100164}
165
Andreas Färber83f7d432012-02-09 15:20:55 +0100166type_init(lm32_juart_register_types)