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
25 #include "qemu/osdep.h"
27 #include "hw/sysbus.h"
29 #include "sysemu/char.h"
30 #include "qemu/error-report.h"
93 #define TYPE_LM32_UART "lm32-uart"
94 #define LM32_UART(obj) OBJECT_CHECK(LM32UartState, (obj), TYPE_LM32_UART)
96 struct LM32UartState
{
97 SysBusDevice parent_obj
;
100 CharDriverState
*chr
;
103 uint32_t regs
[R_MAX
];
105 typedef struct LM32UartState LM32UartState
;
107 static void uart_update_irq(LM32UartState
*s
)
111 if ((s
->regs
[R_LSR
] & (LSR_OE
| LSR_PE
| LSR_FE
| LSR_BI
))
112 && (s
->regs
[R_IER
] & IER_RLSI
)) {
114 s
->regs
[R_IIR
] = IIR_ID1
| IIR_ID0
;
115 } else if ((s
->regs
[R_LSR
] & LSR_DR
) && (s
->regs
[R_IER
] & IER_RBRI
)) {
117 s
->regs
[R_IIR
] = IIR_ID1
;
118 } else if ((s
->regs
[R_LSR
] & LSR_THRE
) && (s
->regs
[R_IER
] & IER_THRI
)) {
120 s
->regs
[R_IIR
] = IIR_ID0
;
121 } else if ((s
->regs
[R_MSR
] & 0x0f) && (s
->regs
[R_IER
] & IER_MSI
)) {
126 s
->regs
[R_IIR
] = IIR_STAT
;
129 trace_lm32_uart_irq_state(irq
);
130 qemu_set_irq(s
->irq
, irq
);
133 static uint64_t uart_read(void *opaque
, hwaddr addr
,
136 LM32UartState
*s
= opaque
;
143 s
->regs
[R_LSR
] &= ~LSR_DR
;
145 qemu_chr_accept_input(s
->chr
);
156 error_report("lm32_uart: read access to write only register 0x"
157 TARGET_FMT_plx
, addr
<< 2);
160 error_report("lm32_uart: read access to unknown register 0x"
161 TARGET_FMT_plx
, addr
<< 2);
165 trace_lm32_uart_memory_read(addr
<< 2, r
);
169 static void uart_write(void *opaque
, hwaddr addr
,
170 uint64_t value
, unsigned size
)
172 LM32UartState
*s
= opaque
;
173 unsigned char ch
= value
;
175 trace_lm32_uart_memory_write(addr
, value
);
181 qemu_chr_fe_write_all(s
->chr
, &ch
, 1);
188 s
->regs
[addr
] = value
;
193 error_report("lm32_uart: write access to read only register 0x"
194 TARGET_FMT_plx
, addr
<< 2);
197 error_report("lm32_uart: write access to unknown register 0x"
198 TARGET_FMT_plx
, addr
<< 2);
204 static const MemoryRegionOps uart_ops
= {
207 .endianness
= DEVICE_NATIVE_ENDIAN
,
209 .min_access_size
= 4,
210 .max_access_size
= 4,
214 static void uart_rx(void *opaque
, const uint8_t *buf
, int size
)
216 LM32UartState
*s
= opaque
;
218 if (s
->regs
[R_LSR
] & LSR_DR
) {
219 s
->regs
[R_LSR
] |= LSR_OE
;
222 s
->regs
[R_LSR
] |= LSR_DR
;
223 s
->regs
[R_RXTX
] = *buf
;
228 static int uart_can_rx(void *opaque
)
230 LM32UartState
*s
= opaque
;
232 return !(s
->regs
[R_LSR
] & LSR_DR
);
235 static void uart_event(void *opaque
, int event
)
239 static void uart_reset(DeviceState
*d
)
241 LM32UartState
*s
= LM32_UART(d
);
244 for (i
= 0; i
< R_MAX
; i
++) {
249 s
->regs
[R_LSR
] = LSR_THRE
| LSR_TEMT
;
252 static int lm32_uart_init(SysBusDevice
*dev
)
254 LM32UartState
*s
= LM32_UART(dev
);
256 sysbus_init_irq(dev
, &s
->irq
);
258 memory_region_init_io(&s
->iomem
, OBJECT(s
), &uart_ops
, s
,
260 sysbus_init_mmio(dev
, &s
->iomem
);
262 /* FIXME use a qdev chardev prop instead of qemu_char_get_next_serial() */
263 s
->chr
= qemu_char_get_next_serial();
265 qemu_chr_add_handlers(s
->chr
, uart_can_rx
, uart_rx
, uart_event
, s
);
271 static const VMStateDescription vmstate_lm32_uart
= {
274 .minimum_version_id
= 1,
275 .fields
= (VMStateField
[]) {
276 VMSTATE_UINT32_ARRAY(regs
, LM32UartState
, R_MAX
),
277 VMSTATE_END_OF_LIST()
281 static void lm32_uart_class_init(ObjectClass
*klass
, void *data
)
283 DeviceClass
*dc
= DEVICE_CLASS(klass
);
284 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
286 k
->init
= lm32_uart_init
;
287 dc
->reset
= uart_reset
;
288 dc
->vmsd
= &vmstate_lm32_uart
;
289 /* Reason: init() method uses qemu_char_get_next_serial() */
290 dc
->cannot_instantiate_with_device_add_yet
= true;
293 static const TypeInfo lm32_uart_info
= {
294 .name
= TYPE_LM32_UART
,
295 .parent
= TYPE_SYS_BUS_DEVICE
,
296 .instance_size
= sizeof(LM32UartState
),
297 .class_init
= lm32_uart_class_init
,
300 static void lm32_uart_register_types(void)
302 type_register_static(&lm32_uart_info
);
305 type_init(lm32_uart_register_types
)