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"
26 #include "hw/sysbus.h"
27 #include "sysemu/char.h"
37 #define STATUS_RXVALID 0x01
38 #define STATUS_RXFULL 0x02
39 #define STATUS_TXEMPTY 0x04
40 #define STATUS_TXFULL 0x08
41 #define STATUS_IE 0x10
42 #define STATUS_OVERRUN 0x20
43 #define STATUS_FRAME 0x40
44 #define STATUS_PARITY 0x80
46 #define CONTROL_RST_TX 0x01
47 #define CONTROL_RST_RX 0x02
48 #define CONTROL_IE 0x10
50 #define TYPE_XILINX_UARTLITE "xlnx.xps-uartlite"
51 #define XILINX_UARTLITE(obj) \
52 OBJECT_CHECK(XilinxUARTLite, (obj), TYPE_XILINX_UARTLITE)
54 typedef struct XilinxUARTLite
{
55 SysBusDevice parent_obj
;
62 unsigned int rx_fifo_pos
;
63 unsigned int rx_fifo_len
;
68 static void uart_update_irq(XilinxUARTLite
*s
)
73 s
->regs
[R_STATUS
] |= STATUS_IE
;
75 irq
= (s
->regs
[R_STATUS
] & STATUS_IE
) && (s
->regs
[R_CTRL
] & CONTROL_IE
);
76 qemu_set_irq(s
->irq
, irq
);
79 static void uart_update_status(XilinxUARTLite
*s
)
83 r
= s
->regs
[R_STATUS
];
85 r
|= 1 << 2; /* Tx fifo is always empty. We are fast :) */
86 r
|= (s
->rx_fifo_len
== sizeof (s
->rx_fifo
)) << 1;
87 r
|= (!!s
->rx_fifo_len
);
88 s
->regs
[R_STATUS
] = r
;
91 static void xilinx_uartlite_reset(DeviceState
*dev
)
93 uart_update_status(XILINX_UARTLITE(dev
));
97 uart_read(void *opaque
, hwaddr addr
, unsigned int size
)
99 XilinxUARTLite
*s
= opaque
;
105 r
= s
->rx_fifo
[(s
->rx_fifo_pos
- s
->rx_fifo_len
) & 7];
108 uart_update_status(s
);
110 qemu_chr_fe_accept_input(&s
->chr
);
114 if (addr
< ARRAY_SIZE(s
->regs
))
116 DUART(qemu_log("%s addr=%x v=%x\n", __func__
, addr
, r
));
123 uart_write(void *opaque
, hwaddr addr
,
124 uint64_t val64
, unsigned int size
)
126 XilinxUARTLite
*s
= opaque
;
127 uint32_t value
= val64
;
128 unsigned char ch
= value
;
134 hw_error("write to UART STATUS?\n");
138 if (value
& CONTROL_RST_RX
) {
142 s
->regs
[addr
] = value
;
146 /* XXX this blocks entire thread. Rewrite to use
147 * qemu_chr_fe_write and background I/O callbacks */
148 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
149 s
->regs
[addr
] = value
;
152 s
->regs
[R_STATUS
] |= STATUS_IE
;
156 DUART(printf("%s addr=%x v=%x\n", __func__
, addr
, value
));
157 if (addr
< ARRAY_SIZE(s
->regs
))
158 s
->regs
[addr
] = value
;
161 uart_update_status(s
);
165 static const MemoryRegionOps uart_ops
= {
168 .endianness
= DEVICE_NATIVE_ENDIAN
,
170 .min_access_size
= 1,
175 static Property xilinx_uartlite_properties
[] = {
176 DEFINE_PROP_CHR("chardev", XilinxUARTLite
, chr
),
177 DEFINE_PROP_END_OF_LIST(),
180 static void uart_rx(void *opaque
, const uint8_t *buf
, int size
)
182 XilinxUARTLite
*s
= opaque
;
185 if (s
->rx_fifo_len
>= 8) {
186 printf("WARNING: UART dropped char.\n");
189 s
->rx_fifo
[s
->rx_fifo_pos
] = *buf
;
191 s
->rx_fifo_pos
&= 0x7;
194 uart_update_status(s
);
198 static int uart_can_rx(void *opaque
)
200 XilinxUARTLite
*s
= opaque
;
202 return s
->rx_fifo_len
< sizeof(s
->rx_fifo
);
205 static void uart_event(void *opaque
, int event
)
210 static void xilinx_uartlite_realize(DeviceState
*dev
, Error
**errp
)
212 XilinxUARTLite
*s
= XILINX_UARTLITE(dev
);
214 qemu_chr_fe_set_handlers(&s
->chr
, uart_can_rx
, uart_rx
,
215 uart_event
, s
, NULL
, true);
218 static void xilinx_uartlite_init(Object
*obj
)
220 XilinxUARTLite
*s
= XILINX_UARTLITE(obj
);
222 sysbus_init_irq(SYS_BUS_DEVICE(obj
), &s
->irq
);
224 memory_region_init_io(&s
->mmio
, obj
, &uart_ops
, s
,
225 "xlnx.xps-uartlite", R_MAX
* 4);
226 sysbus_init_mmio(SYS_BUS_DEVICE(obj
), &s
->mmio
);
229 static void xilinx_uartlite_class_init(ObjectClass
*klass
, void *data
)
231 DeviceClass
*dc
= DEVICE_CLASS(klass
);
233 dc
->reset
= xilinx_uartlite_reset
;
234 dc
->realize
= xilinx_uartlite_realize
;
235 dc
->props
= xilinx_uartlite_properties
;
238 static const TypeInfo xilinx_uartlite_info
= {
239 .name
= TYPE_XILINX_UARTLITE
,
240 .parent
= TYPE_SYS_BUS_DEVICE
,
241 .instance_size
= sizeof(XilinxUARTLite
),
242 .instance_init
= xilinx_uartlite_init
,
243 .class_init
= xilinx_uartlite_class_init
,
246 static void xilinx_uart_register_types(void)
248 type_register_static(&xilinx_uartlite_info
);
251 type_init(xilinx_uart_register_types
)