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_fe_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
)
110 MilkymistUartState
*s
= opaque
;
113 if (qemu_chr_fe_read(s
->chr
, buf
, 1) == 0) {
117 s
->regs
[R_RXTX
] = buf
[0];
118 trace_milkymist_uart_pulse_irq_rx();
119 qemu_irq_pulse(s
->rx_irq
);
122 static void milkymist_uart_reset(DeviceState
*d
)
124 MilkymistUartState
*s
= container_of(d
, MilkymistUartState
, busdev
.qdev
);
127 for (i
= 0; i
< R_MAX
; i
++) {
132 static int milkymist_uart_init(SysBusDevice
*dev
)
134 MilkymistUartState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
137 sysbus_init_irq(dev
, &s
->rx_irq
);
138 sysbus_init_irq(dev
, &s
->tx_irq
);
140 uart_regs
= cpu_register_io_memory(uart_read_fn
, uart_write_fn
, s
,
141 DEVICE_NATIVE_ENDIAN
);
142 sysbus_init_mmio(dev
, R_MAX
* 4, uart_regs
);
144 s
->chr
= qdev_init_chardev(&dev
->qdev
);
146 qemu_chr_fe_open(s
->chr
);
147 qemu_chr_fe_set_handlers(s
->chr
, uart_rx
, NULL
, NULL
, s
);
153 static const VMStateDescription vmstate_milkymist_uart
= {
154 .name
= "milkymist-uart",
156 .minimum_version_id
= 1,
157 .minimum_version_id_old
= 1,
158 .fields
= (VMStateField
[]) {
159 VMSTATE_UINT32_ARRAY(regs
, MilkymistUartState
, R_MAX
),
160 VMSTATE_END_OF_LIST()
164 static SysBusDeviceInfo milkymist_uart_info
= {
165 .init
= milkymist_uart_init
,
166 .qdev
.name
= "milkymist-uart",
167 .qdev
.size
= sizeof(MilkymistUartState
),
168 .qdev
.vmsd
= &vmstate_milkymist_uart
,
169 .qdev
.reset
= milkymist_uart_reset
,
172 static void milkymist_uart_register(void)
174 sysbus_register_withprop(&milkymist_uart_info
);
177 device_init(milkymist_uart_register
)