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
25 #include "hw/sysbus.h"
27 #include "char/char.h"
28 #include "qemu/error-report.h"
46 CTRL_RX_IRQ_EN = (1<<0),
47 CTRL_TX_IRQ_EN = (1<<1),
48 CTRL_THRU_EN = (1<<2),
52 DBG_BREAK_EN = (1<<0),
55 struct MilkymistUartState {
57 MemoryRegion regs_region;
63 typedef struct MilkymistUartState MilkymistUartState;
65 static void uart_update_irq(MilkymistUartState *s)
67 int rx_event = s->regs[R_STAT] & STAT_RX_EVT;
68 int tx_event = s->regs[R_STAT] & STAT_TX_EVT;
69 int rx_irq_en = s->regs[R_CTRL] & CTRL_RX_IRQ_EN;
70 int tx_irq_en = s->regs[R_CTRL] & CTRL_TX_IRQ_EN;
72 if ((rx_irq_en && rx_event) || (tx_irq_en && tx_event)) {
73 trace_milkymist_uart_raise_irq();
74 qemu_irq_raise(s->irq);
76 trace_milkymist_uart_lower_irq();
77 qemu_irq_lower(s->irq);
81 static uint64_t uart_read(void *opaque, hwaddr addr,
84 MilkymistUartState *s = opaque;
100 error_report("milkymist_uart: read access to unknown register 0x"
101 TARGET_FMT_plx, addr << 2);
105 trace_milkymist_uart_memory_read(addr << 2, r);
110 static void uart_write(void *opaque, hwaddr addr, uint64_t value,
113 MilkymistUartState *s = opaque;
114 unsigned char ch = value;
116 trace_milkymist_uart_memory_write(addr, value);
122 qemu_chr_fe_write(s->chr, &ch, 1);
124 s->regs[R_STAT] |= STAT_TX_EVT;
129 s->regs[addr] = value;
133 /* write one to clear bits */
134 s->regs[addr] &= ~(value & (STAT_RX_EVT | STAT_TX_EVT));
135 qemu_chr_accept_input(s->chr);
139 error_report("milkymist_uart: write access to unknown register 0x"
140 TARGET_FMT_plx, addr << 2);
147 static const MemoryRegionOps uart_mmio_ops = {
151 .min_access_size = 4,
152 .max_access_size = 4,
154 .endianness = DEVICE_NATIVE_ENDIAN,
157 static void uart_rx(void *opaque, const uint8_t *buf, int size)
159 MilkymistUartState *s = opaque;
161 assert(!(s->regs[R_STAT] & STAT_RX_EVT));
163 s->regs[R_STAT] |= STAT_RX_EVT;
164 s->regs[R_RXTX] = *buf;
169 static int uart_can_rx(void *opaque)
171 MilkymistUartState *s = opaque;
173 return !(s->regs[R_STAT] & STAT_RX_EVT);
176 static void uart_event(void *opaque, int event)
180 static void milkymist_uart_reset(DeviceState *d)
182 MilkymistUartState *s = container_of(d, MilkymistUartState, busdev.qdev);
185 for (i = 0; i < R_MAX; i++) {
189 /* THRE is always set */
190 s->regs[R_STAT] = STAT_THRE;
193 static int milkymist_uart_init(SysBusDevice *dev)
195 MilkymistUartState *s = FROM_SYSBUS(typeof(*s), dev);
197 sysbus_init_irq(dev, &s->irq);
199 memory_region_init_io(&s->regs_region, &uart_mmio_ops, s,
200 "milkymist-uart", R_MAX * 4);
201 sysbus_init_mmio(dev, &s->regs_region);
203 s->chr = qemu_char_get_next_serial();
205 qemu_chr_add_handlers(s->chr, uart_can_rx, uart_rx, uart_event, s);
211 static const VMStateDescription vmstate_milkymist_uart = {
212 .name = "milkymist-uart",
214 .minimum_version_id = 1,
215 .minimum_version_id_old = 1,
216 .fields = (VMStateField[]) {
217 VMSTATE_UINT32_ARRAY(regs, MilkymistUartState, R_MAX),
218 VMSTATE_END_OF_LIST()
222 static void milkymist_uart_class_init(ObjectClass *klass, void *data)
224 DeviceClass *dc = DEVICE_CLASS(klass);
225 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
227 k->init = milkymist_uart_init;
228 dc->reset = milkymist_uart_reset;
229 dc->vmsd = &vmstate_milkymist_uart;
232 static const TypeInfo milkymist_uart_info = {
233 .name = "milkymist-uart",
234 .parent = TYPE_SYS_BUS_DEVICE,
235 .instance_size = sizeof(MilkymistUartState),
236 .class_init = milkymist_uart_class_init,
239 static void milkymist_uart_register_types(void)
241 type_register_static(&milkymist_uart_info);
244 type_init(milkymist_uart_register_types)