2 * QEMU model of the Milkymist 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.milkymist.org/socdoc/uart.pdf
27 #include "qemu-char.h"
28 #include "qemu-error.h"
36 struct MilkymistUartState
{
44 typedef struct MilkymistUartState MilkymistUartState
;
46 static uint32_t uart_read(void *opaque
, target_phys_addr_t addr
)
48 MilkymistUartState
*s
= opaque
;
59 error_report("milkymist_uart: read access to unknown register 0x"
60 TARGET_FMT_plx
, addr
<< 2);
64 trace_milkymist_uart_memory_read(addr
<< 2, r
);
69 static void uart_write(void *opaque
, target_phys_addr_t addr
, uint32_t value
)
71 MilkymistUartState
*s
= opaque
;
72 unsigned char ch
= value
;
74 trace_milkymist_uart_memory_write(addr
, value
);
80 qemu_chr_write(s
->chr
, &ch
, 1);
82 trace_milkymist_uart_pulse_irq_tx();
83 qemu_irq_pulse(s
->tx_irq
);
86 s
->regs
[addr
] = value
;
90 error_report("milkymist_uart: write access to unknown register 0x"
91 TARGET_FMT_plx
, addr
<< 2);
96 static CPUReadMemoryFunc
* const uart_read_fn
[] = {
102 static CPUWriteMemoryFunc
* const uart_write_fn
[] = {
108 static void uart_rx(void *opaque
, const uint8_t *buf
, int size
)
110 MilkymistUartState
*s
= opaque
;
112 s
->regs
[R_RXTX
] = *buf
;
113 trace_milkymist_uart_pulse_irq_rx();
114 qemu_irq_pulse(s
->rx_irq
);
117 static int uart_can_rx(void *opaque
)
122 static void uart_event(void *opaque
, int event
)
126 static void milkymist_uart_reset(DeviceState
*d
)
128 MilkymistUartState
*s
= container_of(d
, MilkymistUartState
, busdev
.qdev
);
131 for (i
= 0; i
< R_MAX
; i
++) {
136 static int milkymist_uart_init(SysBusDevice
*dev
)
138 MilkymistUartState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
141 sysbus_init_irq(dev
, &s
->rx_irq
);
142 sysbus_init_irq(dev
, &s
->tx_irq
);
144 uart_regs
= cpu_register_io_memory(uart_read_fn
, uart_write_fn
, s
,
145 DEVICE_NATIVE_ENDIAN
);
146 sysbus_init_mmio(dev
, R_MAX
* 4, uart_regs
);
148 s
->chr
= qdev_init_chardev(&dev
->qdev
);
150 qemu_chr_add_handlers(s
->chr
, uart_can_rx
, uart_rx
, uart_event
, s
);
156 static const VMStateDescription vmstate_milkymist_uart
= {
157 .name
= "milkymist-uart",
159 .minimum_version_id
= 1,
160 .minimum_version_id_old
= 1,
161 .fields
= (VMStateField
[]) {
162 VMSTATE_UINT32_ARRAY(regs
, MilkymistUartState
, R_MAX
),
163 VMSTATE_END_OF_LIST()
167 static SysBusDeviceInfo milkymist_uart_info
= {
168 .init
= milkymist_uart_init
,
169 .qdev
.name
= "milkymist-uart",
170 .qdev
.size
= sizeof(MilkymistUartState
),
171 .qdev
.vmsd
= &vmstate_milkymist_uart
,
172 .qdev
.reset
= milkymist_uart_reset
,
175 static void milkymist_uart_register(void)
177 sysbus_register_withprop(&milkymist_uart_info
);
180 device_init(milkymist_uart_register
)