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
28 #include "qemu-char.h"
29 #include "qemu-error.h"
92 struct LM32UartState
{
99 typedef struct LM32UartState LM32UartState
;
101 static void uart_update_handlers(LM32UartState
*s
);
103 static void uart_update_irq(LM32UartState
*s
)
107 if ((s
->regs
[R_LSR
] & (LSR_OE
| LSR_PE
| LSR_FE
| LSR_BI
))
108 && (s
->regs
[R_IER
] & IER_RLSI
)) {
110 s
->regs
[R_IIR
] = IIR_ID1
| IIR_ID0
;
111 } else if ((s
->regs
[R_LSR
] & LSR_DR
) && (s
->regs
[R_IER
] & IER_RBRI
)) {
113 s
->regs
[R_IIR
] = IIR_ID1
;
114 } else if ((s
->regs
[R_LSR
] & LSR_THRE
) && (s
->regs
[R_IER
] & IER_THRI
)) {
116 s
->regs
[R_IIR
] = IIR_ID0
;
117 } else if ((s
->regs
[R_MSR
] & 0x0f) && (s
->regs
[R_IER
] & IER_MSI
)) {
122 s
->regs
[R_IIR
] = IIR_STAT
;
125 trace_lm32_uart_irq_state(irq
);
126 qemu_set_irq(s
->irq
, irq
);
129 static uint32_t uart_read(void *opaque
, target_phys_addr_t addr
)
131 LM32UartState
*s
= opaque
;
138 s
->regs
[R_LSR
] &= ~LSR_DR
;
140 uart_update_handlers(s
);
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
, target_phys_addr_t addr
, uint32_t value
)
166 LM32UartState
*s
= opaque
;
167 unsigned char ch
= value
;
169 trace_lm32_uart_memory_write(addr
, value
);
175 qemu_chr_fe_write(s
->chr
, &ch
, 1);
182 s
->regs
[addr
] = value
;
187 error_report("lm32_uart: write access to read only register 0x"
188 TARGET_FMT_plx
, addr
<< 2);
191 error_report("lm32_uart: write access to unknown register 0x"
192 TARGET_FMT_plx
, addr
<< 2);
198 static CPUReadMemoryFunc
* const uart_read_fn
[] = {
204 static CPUWriteMemoryFunc
* const uart_write_fn
[] = {
210 static void uart_rx(LM32UartState
*s
, const uint8_t *buf
, int size
)
212 if (s
->regs
[R_LSR
] & LSR_DR
) {
213 s
->regs
[R_LSR
] |= LSR_OE
;
216 s
->regs
[R_LSR
] |= LSR_DR
;
217 s
->regs
[R_RXTX
] = *buf
;
220 uart_update_handlers(s
);
223 static int uart_can_rx(LM32UartState
*s
)
225 return !(s
->regs
[R_LSR
] & LSR_DR
);
228 static void uart_rx_handler(void *opaque
)
230 LM32UartState
*s
= opaque
;
234 size
= uart_can_rx(s
);
235 size
= MIN(size
, sizeof(buf
));
236 size
= qemu_chr_fe_read(s
->chr
, buf
, size
);
238 uart_rx(s
, buf
, size
);
241 static void uart_update_handlers(LM32UartState
*s
)
243 if (uart_can_rx(s
) > 0) {
244 qemu_chr_fe_set_handlers(s
->chr
, uart_rx_handler
, NULL
, NULL
, s
);
246 qemu_chr_fe_set_handlers(s
->chr
, NULL
, NULL
, NULL
, s
);
250 static void uart_reset(DeviceState
*d
)
252 LM32UartState
*s
= container_of(d
, LM32UartState
, busdev
.qdev
);
255 for (i
= 0; i
< R_MAX
; i
++) {
260 s
->regs
[R_LSR
] = LSR_THRE
| LSR_TEMT
;
261 uart_update_handlers(s
);
264 static int lm32_uart_init(SysBusDevice
*dev
)
266 LM32UartState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
269 sysbus_init_irq(dev
, &s
->irq
);
271 uart_regs
= cpu_register_io_memory(uart_read_fn
, uart_write_fn
, s
,
272 DEVICE_NATIVE_ENDIAN
);
273 sysbus_init_mmio(dev
, R_MAX
* 4, uart_regs
);
275 s
->chr
= qdev_init_chardev(&dev
->qdev
);
277 qemu_chr_fe_open(s
->chr
);
278 uart_update_handlers(s
);
284 static const VMStateDescription vmstate_lm32_uart
= {
287 .minimum_version_id
= 1,
288 .minimum_version_id_old
= 1,
289 .fields
= (VMStateField
[]) {
290 VMSTATE_UINT32_ARRAY(regs
, LM32UartState
, R_MAX
),
291 VMSTATE_END_OF_LIST()
295 static SysBusDeviceInfo lm32_uart_info
= {
296 .init
= lm32_uart_init
,
297 .qdev
.name
= "lm32-uart",
298 .qdev
.size
= sizeof(LM32UartState
),
299 .qdev
.vmsd
= &vmstate_lm32_uart
,
300 .qdev
.reset
= uart_reset
,
303 static void lm32_uart_register(void)
305 sysbus_register_withprop(&lm32_uart_info
);
308 device_init(lm32_uart_register
)