2 * ColdFire UART emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
9 #include "qemu/osdep.h"
11 #include "hw/sysbus.h"
12 #include "qemu/module.h"
13 #include "qapi/error.h"
14 #include "hw/m68k/mcf.h"
15 #include "hw/qdev-properties.h"
16 #include "hw/qdev-properties-system.h"
17 #include "chardev/char-fe.h"
18 #include "qom/object.h"
20 struct mcf_uart_state
{
21 SysBusDevice parent_obj
;
40 #define TYPE_MCF_UART "mcf-uart"
41 OBJECT_DECLARE_SIMPLE_TYPE(mcf_uart_state
, MCF_UART
)
43 /* UART Status Register bits. */
44 #define MCF_UART_RxRDY 0x01
45 #define MCF_UART_FFULL 0x02
46 #define MCF_UART_TxRDY 0x04
47 #define MCF_UART_TxEMP 0x08
48 #define MCF_UART_OE 0x10
49 #define MCF_UART_PE 0x20
50 #define MCF_UART_FE 0x40
51 #define MCF_UART_RB 0x80
53 /* Interrupt flags. */
54 #define MCF_UART_TxINT 0x01
55 #define MCF_UART_RxINT 0x02
56 #define MCF_UART_DBINT 0x04
57 #define MCF_UART_COSINT 0x80
60 #define MCF_UART_BC0 0x01
61 #define MCF_UART_BC1 0x02
62 #define MCF_UART_PT 0x04
63 #define MCF_UART_PM0 0x08
64 #define MCF_UART_PM1 0x10
65 #define MCF_UART_ERR 0x20
66 #define MCF_UART_RxIRQ 0x40
67 #define MCF_UART_RxRTS 0x80
69 static void mcf_uart_update(mcf_uart_state
*s
)
71 s
->isr
&= ~(MCF_UART_TxINT
| MCF_UART_RxINT
);
72 if (s
->sr
& MCF_UART_TxRDY
)
73 s
->isr
|= MCF_UART_TxINT
;
74 if ((s
->sr
& ((s
->mr
[0] & MCF_UART_RxIRQ
)
75 ? MCF_UART_FFULL
: MCF_UART_RxRDY
)) != 0)
76 s
->isr
|= MCF_UART_RxINT
;
78 qemu_set_irq(s
->irq
, (s
->isr
& s
->imr
) != 0);
81 uint64_t mcf_uart_read(void *opaque
, hwaddr addr
,
84 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
85 switch (addr
& 0x3f) {
87 return s
->mr
[s
->current_mr
];
100 for (i
= 0; i
< s
->fifo_len
; i
++)
101 s
->fifo
[i
] = s
->fifo
[i
+ 1];
102 s
->sr
&= ~MCF_UART_FFULL
;
103 if (s
->fifo_len
== 0)
104 s
->sr
&= ~MCF_UART_RxRDY
;
106 qemu_chr_fe_accept_input(&s
->chr
);
110 /* TODO: Implement IPCR. */
123 /* Update TxRDY flag and set data if present and enabled. */
124 static void mcf_uart_do_tx(mcf_uart_state
*s
)
126 if (s
->tx_enabled
&& (s
->sr
& MCF_UART_TxEMP
) == 0) {
127 /* XXX this blocks entire thread. Rewrite to use
128 * qemu_chr_fe_write and background I/O callbacks */
129 qemu_chr_fe_write_all(&s
->chr
, (unsigned char *)&s
->tb
, 1);
130 s
->sr
|= MCF_UART_TxEMP
;
133 s
->sr
|= MCF_UART_TxRDY
;
135 s
->sr
&= ~MCF_UART_TxRDY
;
139 static void mcf_do_command(mcf_uart_state
*s
, uint8_t cmd
)
142 switch ((cmd
>> 4) & 7) {
145 case 1: /* Reset mode register pointer. */
148 case 2: /* Reset receiver. */
151 s
->sr
&= ~(MCF_UART_RxRDY
| MCF_UART_FFULL
);
153 case 3: /* Reset transmitter. */
155 s
->sr
|= MCF_UART_TxEMP
;
156 s
->sr
&= ~MCF_UART_TxRDY
;
158 case 4: /* Reset error status. */
160 case 5: /* Reset break-change interrupt. */
161 s
->isr
&= ~MCF_UART_DBINT
;
163 case 6: /* Start break. */
164 case 7: /* Stop break. */
168 /* Transmitter command. */
169 switch ((cmd
>> 2) & 3) {
172 case 1: /* Enable. */
176 case 2: /* Disable. */
180 case 3: /* Reserved. */
181 fprintf(stderr
, "mcf_uart: Bad TX command\n");
185 /* Receiver command. */
189 case 1: /* Enable. */
195 case 3: /* Reserved. */
196 fprintf(stderr
, "mcf_uart: Bad RX command\n");
201 void mcf_uart_write(void *opaque
, hwaddr addr
,
202 uint64_t val
, unsigned size
)
204 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
205 switch (addr
& 0x3f) {
207 s
->mr
[s
->current_mr
] = val
;
211 /* CSR is ignored. */
213 case 0x08: /* Command Register. */
214 mcf_do_command(s
, val
);
216 case 0x0c: /* Transmit Buffer. */
217 s
->sr
&= ~MCF_UART_TxEMP
;
222 /* ACR is ignored. */
233 static void mcf_uart_reset(DeviceState
*dev
)
235 mcf_uart_state
*s
= MCF_UART(dev
);
240 s
->sr
= MCF_UART_TxEMP
;
247 static void mcf_uart_push_byte(mcf_uart_state
*s
, uint8_t data
)
249 /* Break events overwrite the last byte if the fifo is full. */
250 if (s
->fifo_len
== 4)
253 s
->fifo
[s
->fifo_len
] = data
;
255 s
->sr
|= MCF_UART_RxRDY
;
256 if (s
->fifo_len
== 4)
257 s
->sr
|= MCF_UART_FFULL
;
262 static void mcf_uart_event(void *opaque
, QEMUChrEvent event
)
264 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
267 case CHR_EVENT_BREAK
:
268 s
->isr
|= MCF_UART_DBINT
;
269 mcf_uart_push_byte(s
, 0);
276 static int mcf_uart_can_receive(void *opaque
)
278 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
280 return s
->rx_enabled
&& (s
->sr
& MCF_UART_FFULL
) == 0;
283 static void mcf_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
285 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
287 mcf_uart_push_byte(s
, buf
[0]);
290 static const MemoryRegionOps mcf_uart_ops
= {
291 .read
= mcf_uart_read
,
292 .write
= mcf_uart_write
,
293 .endianness
= DEVICE_NATIVE_ENDIAN
,
296 static void mcf_uart_instance_init(Object
*obj
)
298 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
299 mcf_uart_state
*s
= MCF_UART(dev
);
301 memory_region_init_io(&s
->iomem
, obj
, &mcf_uart_ops
, s
, "uart", 0x40);
302 sysbus_init_mmio(dev
, &s
->iomem
);
304 sysbus_init_irq(dev
, &s
->irq
);
307 static void mcf_uart_realize(DeviceState
*dev
, Error
**errp
)
309 mcf_uart_state
*s
= MCF_UART(dev
);
311 qemu_chr_fe_set_handlers(&s
->chr
, mcf_uart_can_receive
, mcf_uart_receive
,
312 mcf_uart_event
, NULL
, s
, NULL
, true);
315 static Property mcf_uart_properties
[] = {
316 DEFINE_PROP_CHR("chardev", mcf_uart_state
, chr
),
317 DEFINE_PROP_END_OF_LIST(),
320 static void mcf_uart_class_init(ObjectClass
*oc
, void *data
)
322 DeviceClass
*dc
= DEVICE_CLASS(oc
);
324 dc
->realize
= mcf_uart_realize
;
325 dc
->reset
= mcf_uart_reset
;
326 device_class_set_props(dc
, mcf_uart_properties
);
327 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
330 static const TypeInfo mcf_uart_info
= {
331 .name
= TYPE_MCF_UART
,
332 .parent
= TYPE_SYS_BUS_DEVICE
,
333 .instance_size
= sizeof(mcf_uart_state
),
334 .instance_init
= mcf_uart_instance_init
,
335 .class_init
= mcf_uart_class_init
,
338 static void mcf_uart_register(void)
340 type_register_static(&mcf_uart_info
);
343 type_init(mcf_uart_register
)
345 void *mcf_uart_init(qemu_irq irq
, Chardev
*chrdrv
)
349 dev
= qdev_new(TYPE_MCF_UART
);
351 qdev_prop_set_chr(dev
, "chardev", chrdrv
);
353 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
355 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, irq
);
360 void mcf_uart_mm_init(hwaddr base
, qemu_irq irq
, Chardev
*chrdrv
)
364 dev
= mcf_uart_init(irq
, chrdrv
);
365 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);