2 * QEMU model of Xilinx uartlite.
4 * Copyright (c) 2009 Edgar E. Iglesias.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu/osdep.h"
27 #include "hw/char/xilinx_uartlite.h"
29 #include "hw/qdev-properties.h"
30 #include "hw/qdev-properties-system.h"
31 #include "hw/sysbus.h"
32 #include "qemu/module.h"
33 #include "chardev/char-fe.h"
34 #include "qom/object.h"
44 #define STATUS_RXVALID 0x01
45 #define STATUS_RXFULL 0x02
46 #define STATUS_TXEMPTY 0x04
47 #define STATUS_TXFULL 0x08
48 #define STATUS_IE 0x10
49 #define STATUS_OVERRUN 0x20
50 #define STATUS_FRAME 0x40
51 #define STATUS_PARITY 0x80
53 #define CONTROL_RST_TX 0x01
54 #define CONTROL_RST_RX 0x02
55 #define CONTROL_IE 0x10
57 struct XilinxUARTLite
{
58 SysBusDevice parent_obj
;
65 unsigned int rx_fifo_pos
;
66 unsigned int rx_fifo_len
;
71 static void uart_update_irq(XilinxUARTLite
*s
)
76 s
->regs
[R_STATUS
] |= STATUS_IE
;
78 irq
= (s
->regs
[R_STATUS
] & STATUS_IE
) && (s
->regs
[R_CTRL
] & CONTROL_IE
);
79 qemu_set_irq(s
->irq
, irq
);
82 static void uart_update_status(XilinxUARTLite
*s
)
86 r
= s
->regs
[R_STATUS
];
88 r
|= 1 << 2; /* Tx fifo is always empty. We are fast :) */
89 r
|= (s
->rx_fifo_len
== sizeof (s
->rx_fifo
)) << 1;
90 r
|= (!!s
->rx_fifo_len
);
91 s
->regs
[R_STATUS
] = r
;
94 static void xilinx_uartlite_reset(DeviceState
*dev
)
96 uart_update_status(XILINX_UARTLITE(dev
));
100 uart_read(void *opaque
, hwaddr addr
, unsigned int size
)
102 XilinxUARTLite
*s
= opaque
;
108 r
= s
->rx_fifo
[(s
->rx_fifo_pos
- s
->rx_fifo_len
) & 7];
111 uart_update_status(s
);
113 qemu_chr_fe_accept_input(&s
->chr
);
117 if (addr
< ARRAY_SIZE(s
->regs
))
119 DUART(qemu_log("%s addr=%x v=%x\n", __func__
, addr
, r
));
126 uart_write(void *opaque
, hwaddr addr
,
127 uint64_t val64
, unsigned int size
)
129 XilinxUARTLite
*s
= opaque
;
130 uint32_t value
= val64
;
131 unsigned char ch
= value
;
137 qemu_log_mask(LOG_GUEST_ERROR
, "%s: write to UART STATUS\n",
142 if (value
& CONTROL_RST_RX
) {
146 s
->regs
[addr
] = value
;
150 /* XXX this blocks entire thread. Rewrite to use
151 * qemu_chr_fe_write and background I/O callbacks */
152 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
153 s
->regs
[addr
] = value
;
156 s
->regs
[R_STATUS
] |= STATUS_IE
;
160 DUART(printf("%s addr=%x v=%x\n", __func__
, addr
, value
));
161 if (addr
< ARRAY_SIZE(s
->regs
))
162 s
->regs
[addr
] = value
;
165 uart_update_status(s
);
169 static const MemoryRegionOps uart_ops
= {
172 .endianness
= DEVICE_NATIVE_ENDIAN
,
174 .min_access_size
= 1,
179 static Property xilinx_uartlite_properties
[] = {
180 DEFINE_PROP_CHR("chardev", XilinxUARTLite
, chr
),
181 DEFINE_PROP_END_OF_LIST(),
184 static void uart_rx(void *opaque
, const uint8_t *buf
, int size
)
186 XilinxUARTLite
*s
= opaque
;
189 if (s
->rx_fifo_len
>= 8) {
190 printf("WARNING: UART dropped char.\n");
193 s
->rx_fifo
[s
->rx_fifo_pos
] = *buf
;
195 s
->rx_fifo_pos
&= 0x7;
198 uart_update_status(s
);
202 static int uart_can_rx(void *opaque
)
204 XilinxUARTLite
*s
= opaque
;
206 return s
->rx_fifo_len
< sizeof(s
->rx_fifo
);
209 static void uart_event(void *opaque
, QEMUChrEvent event
)
214 static void xilinx_uartlite_realize(DeviceState
*dev
, Error
**errp
)
216 XilinxUARTLite
*s
= XILINX_UARTLITE(dev
);
218 qemu_chr_fe_set_handlers(&s
->chr
, uart_can_rx
, uart_rx
,
219 uart_event
, NULL
, s
, NULL
, true);
222 static void xilinx_uartlite_init(Object
*obj
)
224 XilinxUARTLite
*s
= XILINX_UARTLITE(obj
);
226 sysbus_init_irq(SYS_BUS_DEVICE(obj
), &s
->irq
);
228 memory_region_init_io(&s
->mmio
, obj
, &uart_ops
, s
,
229 "xlnx.xps-uartlite", R_MAX
* 4);
230 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->mmio
);
233 static void xilinx_uartlite_class_init(ObjectClass
*klass
, void *data
)
235 DeviceClass
*dc
= DEVICE_CLASS(klass
);
237 dc
->reset
= xilinx_uartlite_reset
;
238 dc
->realize
= xilinx_uartlite_realize
;
239 device_class_set_props(dc
, xilinx_uartlite_properties
);
242 static const TypeInfo xilinx_uartlite_info
= {
243 .name
= TYPE_XILINX_UARTLITE
,
244 .parent
= TYPE_SYS_BUS_DEVICE
,
245 .instance_size
= sizeof(XilinxUARTLite
),
246 .instance_init
= xilinx_uartlite_init
,
247 .class_init
= xilinx_uartlite_class_init
,
250 static void xilinx_uart_register_types(void)
252 type_register_static(&xilinx_uartlite_info
);
255 type_init(xilinx_uart_register_types
)