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/qdev-properties.h"
28 #include "hw/sysbus.h"
29 #include "migration/vmstate.h"
31 #include "chardev/char-fe.h"
32 #include "qemu/error-report.h"
33 #include "qemu/module.h"
34 #include "qom/object.h"
97 #define TYPE_LM32_UART "lm32-uart"
98 typedef struct LM32UartState LM32UartState
;
99 DECLARE_INSTANCE_CHECKER(LM32UartState
, LM32_UART
,
102 struct LM32UartState
{
103 SysBusDevice parent_obj
;
109 uint32_t regs
[R_MAX
];
112 static void uart_update_irq(LM32UartState
*s
)
116 if ((s
->regs
[R_LSR
] & (LSR_OE
| LSR_PE
| LSR_FE
| LSR_BI
))
117 && (s
->regs
[R_IER
] & IER_RLSI
)) {
119 s
->regs
[R_IIR
] = IIR_ID1
| IIR_ID0
;
120 } else if ((s
->regs
[R_LSR
] & LSR_DR
) && (s
->regs
[R_IER
] & IER_RBRI
)) {
122 s
->regs
[R_IIR
] = IIR_ID1
;
123 } else if ((s
->regs
[R_LSR
] & LSR_THRE
) && (s
->regs
[R_IER
] & IER_THRI
)) {
125 s
->regs
[R_IIR
] = IIR_ID0
;
126 } else if ((s
->regs
[R_MSR
] & 0x0f) && (s
->regs
[R_IER
] & IER_MSI
)) {
131 s
->regs
[R_IIR
] = IIR_STAT
;
134 trace_lm32_uart_irq_state(irq
);
135 qemu_set_irq(s
->irq
, irq
);
138 static uint64_t uart_read(void *opaque
, hwaddr addr
,
141 LM32UartState
*s
= opaque
;
148 s
->regs
[R_LSR
] &= ~LSR_DR
;
150 qemu_chr_fe_accept_input(&s
->chr
);
161 error_report("lm32_uart: read access to write only register 0x"
162 TARGET_FMT_plx
, addr
<< 2);
165 error_report("lm32_uart: read access to unknown register 0x"
166 TARGET_FMT_plx
, addr
<< 2);
170 trace_lm32_uart_memory_read(addr
<< 2, r
);
174 static void uart_write(void *opaque
, hwaddr addr
,
175 uint64_t value
, unsigned size
)
177 LM32UartState
*s
= opaque
;
178 unsigned char ch
= value
;
180 trace_lm32_uart_memory_write(addr
, value
);
185 /* XXX this blocks entire thread. Rewrite to use
186 * qemu_chr_fe_write and background I/O callbacks */
187 qemu_chr_fe_write_all(&s
->chr
, &ch
, 1);
193 s
->regs
[addr
] = value
;
198 error_report("lm32_uart: write access to read only register 0x"
199 TARGET_FMT_plx
, addr
<< 2);
202 error_report("lm32_uart: write access to unknown register 0x"
203 TARGET_FMT_plx
, addr
<< 2);
209 static const MemoryRegionOps uart_ops
= {
212 .endianness
= DEVICE_NATIVE_ENDIAN
,
214 .min_access_size
= 4,
215 .max_access_size
= 4,
219 static void uart_rx(void *opaque
, const uint8_t *buf
, int size
)
221 LM32UartState
*s
= opaque
;
223 if (s
->regs
[R_LSR
] & LSR_DR
) {
224 s
->regs
[R_LSR
] |= LSR_OE
;
227 s
->regs
[R_LSR
] |= LSR_DR
;
228 s
->regs
[R_RXTX
] = *buf
;
233 static int uart_can_rx(void *opaque
)
235 LM32UartState
*s
= opaque
;
237 return !(s
->regs
[R_LSR
] & LSR_DR
);
240 static void uart_event(void *opaque
, QEMUChrEvent event
)
244 static void uart_reset(DeviceState
*d
)
246 LM32UartState
*s
= LM32_UART(d
);
249 for (i
= 0; i
< R_MAX
; i
++) {
254 s
->regs
[R_LSR
] = LSR_THRE
| LSR_TEMT
;
257 static void lm32_uart_init(Object
*obj
)
259 LM32UartState
*s
= LM32_UART(obj
);
260 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
262 sysbus_init_irq(dev
, &s
->irq
);
264 memory_region_init_io(&s
->iomem
, obj
, &uart_ops
, s
,
266 sysbus_init_mmio(dev
, &s
->iomem
);
269 static void lm32_uart_realize(DeviceState
*dev
, Error
**errp
)
271 LM32UartState
*s
= LM32_UART(dev
);
273 qemu_chr_fe_set_handlers(&s
->chr
, uart_can_rx
, uart_rx
,
274 uart_event
, NULL
, s
, NULL
, true);
277 static const VMStateDescription vmstate_lm32_uart
= {
280 .minimum_version_id
= 1,
281 .fields
= (VMStateField
[]) {
282 VMSTATE_UINT32_ARRAY(regs
, LM32UartState
, R_MAX
),
283 VMSTATE_END_OF_LIST()
287 static Property lm32_uart_properties
[] = {
288 DEFINE_PROP_CHR("chardev", LM32UartState
, chr
),
289 DEFINE_PROP_END_OF_LIST(),
292 static void lm32_uart_class_init(ObjectClass
*klass
, void *data
)
294 DeviceClass
*dc
= DEVICE_CLASS(klass
);
296 dc
->reset
= uart_reset
;
297 dc
->vmsd
= &vmstate_lm32_uart
;
298 device_class_set_props(dc
, lm32_uart_properties
);
299 dc
->realize
= lm32_uart_realize
;
302 static const TypeInfo lm32_uart_info
= {
303 .name
= TYPE_LM32_UART
,
304 .parent
= TYPE_SYS_BUS_DEVICE
,
305 .instance_size
= sizeof(LM32UartState
),
306 .instance_init
= lm32_uart_init
,
307 .class_init
= lm32_uart_class_init
,
310 static void lm32_uart_register_types(void)
312 type_register_static(&lm32_uart_info
);
315 type_init(lm32_uart_register_types
)