2 * QEMU model of the LatticeMico32 UART block.
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 * Specification available at:
21 * http://www.latticesemi.com/documents/mico32uart.pdf
26 #include "hw/sysbus.h"
28 #include "char/char.h"
29 #include "qemu/error-report.h"
92 struct LM32UartState {
100 typedef struct LM32UartState LM32UartState;
102 static void uart_update_irq(LM32UartState *s)
106 if ((s->regs[R_LSR] & (LSR_OE | LSR_PE | LSR_FE | LSR_BI))
107 && (s->regs[R_IER] & IER_RLSI)) {
109 s->regs[R_IIR] = IIR_ID1 | IIR_ID0;
110 } else if ((s->regs[R_LSR] & LSR_DR) && (s->regs[R_IER] & IER_RBRI)) {
112 s->regs[R_IIR] = IIR_ID1;
113 } else if ((s->regs[R_LSR] & LSR_THRE) && (s->regs[R_IER] & IER_THRI)) {
115 s->regs[R_IIR] = IIR_ID0;
116 } else if ((s->regs[R_MSR] & 0x0f) && (s->regs[R_IER] & IER_MSI)) {
121 s->regs[R_IIR] = IIR_STAT;
124 trace_lm32_uart_irq_state(irq);
125 qemu_set_irq(s->irq, irq);
128 static uint64_t uart_read(void *opaque, hwaddr addr,
131 LM32UartState *s = opaque;
138 s->regs[R_LSR] &= ~LSR_DR;
140 qemu_chr_accept_input(s->chr);
151 error_report("lm32_uart: read access to write only register 0x"
152 TARGET_FMT_plx, addr << 2);
155 error_report("lm32_uart: read access to unknown register 0x"
156 TARGET_FMT_plx, addr << 2);
160 trace_lm32_uart_memory_read(addr << 2, r);
164 static void uart_write(void *opaque, hwaddr addr,
165 uint64_t value, unsigned size)
167 LM32UartState *s = opaque;
168 unsigned char ch = value;
170 trace_lm32_uart_memory_write(addr, value);
176 qemu_chr_fe_write(s->chr, &ch, 1);
183 s->regs[addr] = value;
188 error_report("lm32_uart: write access to read only register 0x"
189 TARGET_FMT_plx, addr << 2);
192 error_report("lm32_uart: write access to unknown register 0x"
193 TARGET_FMT_plx, addr << 2);
199 static const MemoryRegionOps uart_ops = {
202 .endianness = DEVICE_NATIVE_ENDIAN,
204 .min_access_size = 4,
205 .max_access_size = 4,
209 static void uart_rx(void *opaque, const uint8_t *buf, int size)
211 LM32UartState *s = opaque;
213 if (s->regs[R_LSR] & LSR_DR) {
214 s->regs[R_LSR] |= LSR_OE;
217 s->regs[R_LSR] |= LSR_DR;
218 s->regs[R_RXTX] = *buf;
223 static int uart_can_rx(void *opaque)
225 LM32UartState *s = opaque;
227 return !(s->regs[R_LSR] & LSR_DR);
230 static void uart_event(void *opaque, int event)
234 static void uart_reset(DeviceState *d)
236 LM32UartState *s = container_of(d, LM32UartState, busdev.qdev);
239 for (i = 0; i < R_MAX; i++) {
244 s->regs[R_LSR] = LSR_THRE | LSR_TEMT;
247 static int lm32_uart_init(SysBusDevice *dev)
249 LM32UartState *s = FROM_SYSBUS(typeof(*s), dev);
251 sysbus_init_irq(dev, &s->irq);
253 memory_region_init_io(&s->iomem, &uart_ops, s, "uart", R_MAX * 4);
254 sysbus_init_mmio(dev, &s->iomem);
256 s->chr = qemu_char_get_next_serial();
258 qemu_chr_add_handlers(s->chr, uart_can_rx, uart_rx, uart_event, s);
264 static const VMStateDescription vmstate_lm32_uart = {
267 .minimum_version_id = 1,
268 .minimum_version_id_old = 1,
269 .fields = (VMStateField[]) {
270 VMSTATE_UINT32_ARRAY(regs, LM32UartState, R_MAX),
271 VMSTATE_END_OF_LIST()
275 static void lm32_uart_class_init(ObjectClass *klass, void *data)
277 DeviceClass *dc = DEVICE_CLASS(klass);
278 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
280 k->init = lm32_uart_init;
281 dc->reset = uart_reset;
282 dc->vmsd = &vmstate_lm32_uart;
285 static const TypeInfo lm32_uart_info = {
287 .parent = TYPE_SYS_BUS_DEVICE,
288 .instance_size = sizeof(LM32UartState),
289 .class_init = lm32_uart_class_init,
292 static void lm32_uart_register_types(void)
294 type_register_static(&lm32_uart_info);
297 type_init(lm32_uart_register_types)