ditto for etraxfs_ser.c
[qemu/aliguori.git] / hw / milkymist-uart.c
blobb13d0092f1c12a6482439b5fa65f62145bb7fd70
1 /*
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
24 #include "hw.h"
25 #include "sysbus.h"
26 #include "trace.h"
27 #include "qemu-char.h"
28 #include "qemu-error.h"
30 enum {
31 R_RXTX = 0,
32 R_DIV,
33 R_MAX
36 struct MilkymistUartState {
37 SysBusDevice busdev;
38 CharDriverState *chr;
39 qemu_irq rx_irq;
40 qemu_irq tx_irq;
42 uint32_t regs[R_MAX];
44 typedef struct MilkymistUartState MilkymistUartState;
46 static uint32_t uart_read(void *opaque, target_phys_addr_t addr)
48 MilkymistUartState *s = opaque;
49 uint32_t r = 0;
51 addr >>= 2;
52 switch (addr) {
53 case R_RXTX:
54 case R_DIV:
55 r = s->regs[addr];
56 break;
58 default:
59 error_report("milkymist_uart: read access to unknown register 0x"
60 TARGET_FMT_plx, addr << 2);
61 break;
64 trace_milkymist_uart_memory_read(addr << 2, r);
66 return 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);
76 addr >>= 2;
77 switch (addr) {
78 case R_RXTX:
79 if (s->chr) {
80 qemu_chr_fe_write(s->chr, &ch, 1);
82 trace_milkymist_uart_pulse_irq_tx();
83 qemu_irq_pulse(s->tx_irq);
84 break;
85 case R_DIV:
86 s->regs[addr] = value;
87 break;
89 default:
90 error_report("milkymist_uart: write access to unknown register 0x"
91 TARGET_FMT_plx, addr << 2);
92 break;
96 static CPUReadMemoryFunc * const uart_read_fn[] = {
97 NULL,
98 NULL,
99 &uart_read,
102 static CPUWriteMemoryFunc * const uart_write_fn[] = {
103 NULL,
104 NULL,
105 &uart_write,
108 static void uart_rx(void *opaque)
110 MilkymistUartState *s = opaque;
111 uint8_t buf[1];
113 if (qemu_chr_fe_read(s->chr, buf, 1) == 0) {
114 return;
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);
125 int i;
127 for (i = 0; i < R_MAX; i++) {
128 s->regs[i] = 0;
132 static int milkymist_uart_init(SysBusDevice *dev)
134 MilkymistUartState *s = FROM_SYSBUS(typeof(*s), dev);
135 int uart_regs;
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);
145 if (s->chr) {
146 qemu_chr_fe_open(s->chr);
147 qemu_chr_fe_set_handlers(s->chr, uart_rx, NULL, NULL, s);
150 return 0;
153 static const VMStateDescription vmstate_milkymist_uart = {
154 .name = "milkymist-uart",
155 .version_id = 1,
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)