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
));
138 error_report("milkymist_uart: write access to unknown register 0x"
139 TARGET_FMT_plx
, addr
<< 2);
146 static const MemoryRegionOps uart_mmio_ops
= {
150 .min_access_size
= 4,
151 .max_access_size
= 4,
153 .endianness
= DEVICE_NATIVE_ENDIAN
,
156 static void uart_rx(void *opaque
, const uint8_t *buf
, int size
)
158 MilkymistUartState
*s
= opaque
;
160 assert(!(s
->regs
[R_STAT
] & STAT_RX_EVT
));
162 s
->regs
[R_STAT
] |= STAT_RX_EVT
;
163 s
->regs
[R_RXTX
] = *buf
;
168 static int uart_can_rx(void *opaque
)
170 MilkymistUartState
*s
= opaque
;
172 return !(s
->regs
[R_STAT
] & STAT_RX_EVT
);
175 static void uart_event(void *opaque
, int event
)
179 static void milkymist_uart_reset(DeviceState
*d
)
181 MilkymistUartState
*s
= container_of(d
, MilkymistUartState
, busdev
.qdev
);
184 for (i
= 0; i
< R_MAX
; i
++) {
188 /* THRE is always set */
189 s
->regs
[R_STAT
] = STAT_THRE
;
192 static int milkymist_uart_init(SysBusDevice
*dev
)
194 MilkymistUartState
*s
= FROM_SYSBUS(typeof(*s
), dev
);
196 sysbus_init_irq(dev
, &s
->irq
);
198 memory_region_init_io(&s
->regs_region
, &uart_mmio_ops
, s
,
199 "milkymist-uart", R_MAX
* 4);
200 sysbus_init_mmio(dev
, &s
->regs_region
);
202 s
->chr
= qemu_char_get_next_serial();
204 qemu_chr_add_handlers(s
->chr
, uart_can_rx
, uart_rx
, uart_event
, s
);
210 static const VMStateDescription vmstate_milkymist_uart
= {
211 .name
= "milkymist-uart",
213 .minimum_version_id
= 1,
214 .minimum_version_id_old
= 1,
215 .fields
= (VMStateField
[]) {
216 VMSTATE_UINT32_ARRAY(regs
, MilkymistUartState
, R_MAX
),
217 VMSTATE_END_OF_LIST()
221 static void milkymist_uart_class_init(ObjectClass
*klass
, void *data
)
223 DeviceClass
*dc
= DEVICE_CLASS(klass
);
224 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
226 k
->init
= milkymist_uart_init
;
227 dc
->reset
= milkymist_uart_reset
;
228 dc
->vmsd
= &vmstate_milkymist_uart
;
231 static const TypeInfo milkymist_uart_info
= {
232 .name
= "milkymist-uart",
233 .parent
= TYPE_SYS_BUS_DEVICE
,
234 .instance_size
= sizeof(MilkymistUartState
),
235 .class_init
= milkymist_uart_class_init
,
238 static void milkymist_uart_register_types(void)
240 type_register_static(&milkymist_uart_info
);
243 type_init(milkymist_uart_register_types
)