configure: add missing --disable-modules option
[qemu/ar7.git] / hw / char / milkymist-uart.c
blob9b89b7e63673b2a7b79e81c7e215a98884097b12
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/hw.h"
25 #include "hw/sysbus.h"
26 #include "trace.h"
27 #include "sysemu/char.h"
28 #include "qemu/error-report.h"
30 enum {
31 R_RXTX = 0,
32 R_DIV,
33 R_STAT,
34 R_CTRL,
35 R_DBG,
36 R_MAX
39 enum {
40 STAT_THRE = (1<<0),
41 STAT_RX_EVT = (1<<1),
42 STAT_TX_EVT = (1<<2),
45 enum {
46 CTRL_RX_IRQ_EN = (1<<0),
47 CTRL_TX_IRQ_EN = (1<<1),
48 CTRL_THRU_EN = (1<<2),
51 enum {
52 DBG_BREAK_EN = (1<<0),
55 #define TYPE_MILKYMIST_UART "milkymist-uart"
56 #define MILKYMIST_UART(obj) \
57 OBJECT_CHECK(MilkymistUartState, (obj), TYPE_MILKYMIST_UART)
59 struct MilkymistUartState {
60 SysBusDevice parent_obj;
62 MemoryRegion regs_region;
63 CharDriverState *chr;
64 qemu_irq irq;
66 uint32_t regs[R_MAX];
68 typedef struct MilkymistUartState MilkymistUartState;
70 static void uart_update_irq(MilkymistUartState *s)
72 int rx_event = s->regs[R_STAT] & STAT_RX_EVT;
73 int tx_event = s->regs[R_STAT] & STAT_TX_EVT;
74 int rx_irq_en = s->regs[R_CTRL] & CTRL_RX_IRQ_EN;
75 int tx_irq_en = s->regs[R_CTRL] & CTRL_TX_IRQ_EN;
77 if ((rx_irq_en && rx_event) || (tx_irq_en && tx_event)) {
78 trace_milkymist_uart_raise_irq();
79 qemu_irq_raise(s->irq);
80 } else {
81 trace_milkymist_uart_lower_irq();
82 qemu_irq_lower(s->irq);
86 static uint64_t uart_read(void *opaque, hwaddr addr,
87 unsigned size)
89 MilkymistUartState *s = opaque;
90 uint32_t r = 0;
92 addr >>= 2;
93 switch (addr) {
94 case R_RXTX:
95 r = s->regs[addr];
96 break;
97 case R_DIV:
98 case R_STAT:
99 case R_CTRL:
100 case R_DBG:
101 r = s->regs[addr];
102 break;
104 default:
105 error_report("milkymist_uart: read access to unknown register 0x"
106 TARGET_FMT_plx, addr << 2);
107 break;
110 trace_milkymist_uart_memory_read(addr << 2, r);
112 return r;
115 static void uart_write(void *opaque, hwaddr addr, uint64_t value,
116 unsigned size)
118 MilkymistUartState *s = opaque;
119 unsigned char ch = value;
121 trace_milkymist_uart_memory_write(addr, value);
123 addr >>= 2;
124 switch (addr) {
125 case R_RXTX:
126 if (s->chr) {
127 qemu_chr_fe_write_all(s->chr, &ch, 1);
129 s->regs[R_STAT] |= STAT_TX_EVT;
130 break;
131 case R_DIV:
132 case R_CTRL:
133 case R_DBG:
134 s->regs[addr] = value;
135 break;
137 case R_STAT:
138 /* write one to clear bits */
139 s->regs[addr] &= ~(value & (STAT_RX_EVT | STAT_TX_EVT));
140 qemu_chr_accept_input(s->chr);
141 break;
143 default:
144 error_report("milkymist_uart: write access to unknown register 0x"
145 TARGET_FMT_plx, addr << 2);
146 break;
149 uart_update_irq(s);
152 static const MemoryRegionOps uart_mmio_ops = {
153 .read = uart_read,
154 .write = uart_write,
155 .valid = {
156 .min_access_size = 4,
157 .max_access_size = 4,
159 .endianness = DEVICE_NATIVE_ENDIAN,
162 static void uart_rx(void *opaque, const uint8_t *buf, int size)
164 MilkymistUartState *s = opaque;
166 assert(!(s->regs[R_STAT] & STAT_RX_EVT));
168 s->regs[R_STAT] |= STAT_RX_EVT;
169 s->regs[R_RXTX] = *buf;
171 uart_update_irq(s);
174 static int uart_can_rx(void *opaque)
176 MilkymistUartState *s = opaque;
178 return !(s->regs[R_STAT] & STAT_RX_EVT);
181 static void uart_event(void *opaque, int event)
185 static void milkymist_uart_reset(DeviceState *d)
187 MilkymistUartState *s = MILKYMIST_UART(d);
188 int i;
190 for (i = 0; i < R_MAX; i++) {
191 s->regs[i] = 0;
194 /* THRE is always set */
195 s->regs[R_STAT] = STAT_THRE;
198 static void milkymist_uart_realize(DeviceState *dev, Error **errp)
200 MilkymistUartState *s = MILKYMIST_UART(dev);
202 /* FIXME use a qdev chardev prop instead of qemu_char_get_next_serial() */
203 s->chr = qemu_char_get_next_serial();
204 if (s->chr) {
205 qemu_chr_add_handlers(s->chr, uart_can_rx, uart_rx, uart_event, s);
209 static void milkymist_uart_init(Object *obj)
211 SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
212 MilkymistUartState *s = MILKYMIST_UART(obj);
214 sysbus_init_irq(sbd, &s->irq);
216 memory_region_init_io(&s->regs_region, OBJECT(s), &uart_mmio_ops, s,
217 "milkymist-uart", R_MAX * 4);
218 sysbus_init_mmio(sbd, &s->regs_region);
221 static const VMStateDescription vmstate_milkymist_uart = {
222 .name = "milkymist-uart",
223 .version_id = 1,
224 .minimum_version_id = 1,
225 .fields = (VMStateField[]) {
226 VMSTATE_UINT32_ARRAY(regs, MilkymistUartState, R_MAX),
227 VMSTATE_END_OF_LIST()
231 static void milkymist_uart_class_init(ObjectClass *klass, void *data)
233 DeviceClass *dc = DEVICE_CLASS(klass);
235 dc->realize = milkymist_uart_realize;
236 dc->reset = milkymist_uart_reset;
237 dc->vmsd = &vmstate_milkymist_uart;
238 /* Reason: realize() method uses qemu_char_get_next_serial() */
239 dc->cannot_instantiate_with_device_add_yet = true;
242 static const TypeInfo milkymist_uart_info = {
243 .name = TYPE_MILKYMIST_UART,
244 .parent = TYPE_SYS_BUS_DEVICE,
245 .instance_size = sizeof(MilkymistUartState),
246 .instance_init = milkymist_uart_init,
247 .class_init = milkymist_uart_class_init,
250 static void milkymist_uart_register_types(void)
252 type_register_static(&milkymist_uart_info);
255 type_init(milkymist_uart_register_types)