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 "qemu/osdep.h"
26 #include "hw/sysbus.h"
28 #include "sysemu/char.h"
29 #include "qemu/error-report.h"
47 CTRL_RX_IRQ_EN
= (1<<0),
48 CTRL_TX_IRQ_EN
= (1<<1),
49 CTRL_THRU_EN
= (1<<2),
53 DBG_BREAK_EN
= (1<<0),
56 #define TYPE_MILKYMIST_UART "milkymist-uart"
57 #define MILKYMIST_UART(obj) \
58 OBJECT_CHECK(MilkymistUartState, (obj), TYPE_MILKYMIST_UART)
60 struct MilkymistUartState
{
61 SysBusDevice parent_obj
;
63 MemoryRegion regs_region
;
69 typedef struct MilkymistUartState MilkymistUartState
;
71 static void uart_update_irq(MilkymistUartState
*s
)
73 int rx_event
= s
->regs
[R_STAT
] & STAT_RX_EVT
;
74 int tx_event
= s
->regs
[R_STAT
] & STAT_TX_EVT
;
75 int rx_irq_en
= s
->regs
[R_CTRL
] & CTRL_RX_IRQ_EN
;
76 int tx_irq_en
= s
->regs
[R_CTRL
] & CTRL_TX_IRQ_EN
;
78 if ((rx_irq_en
&& rx_event
) || (tx_irq_en
&& tx_event
)) {
79 trace_milkymist_uart_raise_irq();
80 qemu_irq_raise(s
->irq
);
82 trace_milkymist_uart_lower_irq();
83 qemu_irq_lower(s
->irq
);
87 static uint64_t uart_read(void *opaque
, hwaddr addr
,
90 MilkymistUartState
*s
= opaque
;
106 error_report("milkymist_uart: read access to unknown register 0x"
107 TARGET_FMT_plx
, addr
<< 2);
111 trace_milkymist_uart_memory_read(addr
<< 2, r
);
116 static void uart_write(void *opaque
, hwaddr addr
, uint64_t value
,
119 MilkymistUartState
*s
= opaque
;
120 unsigned char ch
= value
;
122 trace_milkymist_uart_memory_write(addr
, value
);
128 qemu_chr_fe_write_all(s
->chr
, &ch
, 1);
130 s
->regs
[R_STAT
] |= STAT_TX_EVT
;
135 s
->regs
[addr
] = value
;
139 /* write one to clear bits */
140 s
->regs
[addr
] &= ~(value
& (STAT_RX_EVT
| STAT_TX_EVT
));
141 qemu_chr_accept_input(s
->chr
);
145 error_report("milkymist_uart: write access to unknown register 0x"
146 TARGET_FMT_plx
, addr
<< 2);
153 static const MemoryRegionOps uart_mmio_ops
= {
157 .min_access_size
= 4,
158 .max_access_size
= 4,
160 .endianness
= DEVICE_NATIVE_ENDIAN
,
163 static void uart_rx(void *opaque
, const uint8_t *buf
, int size
)
165 MilkymistUartState
*s
= opaque
;
167 assert(!(s
->regs
[R_STAT
] & STAT_RX_EVT
));
169 s
->regs
[R_STAT
] |= STAT_RX_EVT
;
170 s
->regs
[R_RXTX
] = *buf
;
175 static int uart_can_rx(void *opaque
)
177 MilkymistUartState
*s
= opaque
;
179 return !(s
->regs
[R_STAT
] & STAT_RX_EVT
);
182 static void uart_event(void *opaque
, int event
)
186 static void milkymist_uart_reset(DeviceState
*d
)
188 MilkymistUartState
*s
= MILKYMIST_UART(d
);
191 for (i
= 0; i
< R_MAX
; i
++) {
195 /* THRE is always set */
196 s
->regs
[R_STAT
] = STAT_THRE
;
199 static void milkymist_uart_realize(DeviceState
*dev
, Error
**errp
)
201 MilkymistUartState
*s
= MILKYMIST_UART(dev
);
203 /* FIXME use a qdev chardev prop instead of qemu_char_get_next_serial() */
204 s
->chr
= qemu_char_get_next_serial();
206 qemu_chr_add_handlers(s
->chr
, uart_can_rx
, uart_rx
, uart_event
, s
);
210 static void milkymist_uart_init(Object
*obj
)
212 SysBusDevice
*sbd
= SYS_BUS_DEVICE(obj
);
213 MilkymistUartState
*s
= MILKYMIST_UART(obj
);
215 sysbus_init_irq(sbd
, &s
->irq
);
217 memory_region_init_io(&s
->regs_region
, OBJECT(s
), &uart_mmio_ops
, s
,
218 "milkymist-uart", R_MAX
* 4);
219 sysbus_init_mmio(sbd
, &s
->regs_region
);
222 static const VMStateDescription vmstate_milkymist_uart
= {
223 .name
= "milkymist-uart",
225 .minimum_version_id
= 1,
226 .fields
= (VMStateField
[]) {
227 VMSTATE_UINT32_ARRAY(regs
, MilkymistUartState
, R_MAX
),
228 VMSTATE_END_OF_LIST()
232 static void milkymist_uart_class_init(ObjectClass
*klass
, void *data
)
234 DeviceClass
*dc
= DEVICE_CLASS(klass
);
236 dc
->realize
= milkymist_uart_realize
;
237 dc
->reset
= milkymist_uart_reset
;
238 dc
->vmsd
= &vmstate_milkymist_uart
;
239 /* Reason: realize() method uses qemu_char_get_next_serial() */
240 dc
->cannot_instantiate_with_device_add_yet
= true;
243 static const TypeInfo milkymist_uart_info
= {
244 .name
= TYPE_MILKYMIST_UART
,
245 .parent
= TYPE_SYS_BUS_DEVICE
,
246 .instance_size
= sizeof(MilkymistUartState
),
247 .instance_init
= milkymist_uart_init
,
248 .class_init
= milkymist_uart_class_init
,
251 static void milkymist_uart_register_types(void)
253 type_register_static(&milkymist_uart_info
);
256 type_init(milkymist_uart_register_types
)