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 "chardev/char-fe.h"
19 SysBusDevice parent_obj
;
38 #define TYPE_MCF_UART "mcf-uart"
39 #define MCF_UART(obj) OBJECT_CHECK(mcf_uart_state, (obj), TYPE_MCF_UART)
41 /* UART Status Register bits. */
42 #define MCF_UART_RxRDY 0x01
43 #define MCF_UART_FFULL 0x02
44 #define MCF_UART_TxRDY 0x04
45 #define MCF_UART_TxEMP 0x08
46 #define MCF_UART_OE 0x10
47 #define MCF_UART_PE 0x20
48 #define MCF_UART_FE 0x40
49 #define MCF_UART_RB 0x80
51 /* Interrupt flags. */
52 #define MCF_UART_TxINT 0x01
53 #define MCF_UART_RxINT 0x02
54 #define MCF_UART_DBINT 0x04
55 #define MCF_UART_COSINT 0x80
58 #define MCF_UART_BC0 0x01
59 #define MCF_UART_BC1 0x02
60 #define MCF_UART_PT 0x04
61 #define MCF_UART_PM0 0x08
62 #define MCF_UART_PM1 0x10
63 #define MCF_UART_ERR 0x20
64 #define MCF_UART_RxIRQ 0x40
65 #define MCF_UART_RxRTS 0x80
67 static void mcf_uart_update(mcf_uart_state
*s
)
69 s
->isr
&= ~(MCF_UART_TxINT
| MCF_UART_RxINT
);
70 if (s
->sr
& MCF_UART_TxRDY
)
71 s
->isr
|= MCF_UART_TxINT
;
72 if ((s
->sr
& ((s
->mr
[0] & MCF_UART_RxIRQ
)
73 ? MCF_UART_FFULL
: MCF_UART_RxRDY
)) != 0)
74 s
->isr
|= MCF_UART_RxINT
;
76 qemu_set_irq(s
->irq
, (s
->isr
& s
->imr
) != 0);
79 uint64_t mcf_uart_read(void *opaque
, hwaddr addr
,
82 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
83 switch (addr
& 0x3f) {
85 return s
->mr
[s
->current_mr
];
98 for (i
= 0; i
< s
->fifo_len
; i
++)
99 s
->fifo
[i
] = s
->fifo
[i
+ 1];
100 s
->sr
&= ~MCF_UART_FFULL
;
101 if (s
->fifo_len
== 0)
102 s
->sr
&= ~MCF_UART_RxRDY
;
104 qemu_chr_fe_accept_input(&s
->chr
);
108 /* TODO: Implement IPCR. */
121 /* Update TxRDY flag and set data if present and enabled. */
122 static void mcf_uart_do_tx(mcf_uart_state
*s
)
124 if (s
->tx_enabled
&& (s
->sr
& MCF_UART_TxEMP
) == 0) {
125 /* XXX this blocks entire thread. Rewrite to use
126 * qemu_chr_fe_write and background I/O callbacks */
127 qemu_chr_fe_write_all(&s
->chr
, (unsigned char *)&s
->tb
, 1);
128 s
->sr
|= MCF_UART_TxEMP
;
131 s
->sr
|= MCF_UART_TxRDY
;
133 s
->sr
&= ~MCF_UART_TxRDY
;
137 static void mcf_do_command(mcf_uart_state
*s
, uint8_t cmd
)
140 switch ((cmd
>> 4) & 7) {
143 case 1: /* Reset mode register pointer. */
146 case 2: /* Reset receiver. */
149 s
->sr
&= ~(MCF_UART_RxRDY
| MCF_UART_FFULL
);
151 case 3: /* Reset transmitter. */
153 s
->sr
|= MCF_UART_TxEMP
;
154 s
->sr
&= ~MCF_UART_TxRDY
;
156 case 4: /* Reset error status. */
158 case 5: /* Reset break-change interrupt. */
159 s
->isr
&= ~MCF_UART_DBINT
;
161 case 6: /* Start break. */
162 case 7: /* Stop break. */
166 /* Transmitter command. */
167 switch ((cmd
>> 2) & 3) {
170 case 1: /* Enable. */
174 case 2: /* Disable. */
178 case 3: /* Reserved. */
179 fprintf(stderr
, "mcf_uart: Bad TX command\n");
183 /* Receiver command. */
187 case 1: /* Enable. */
193 case 3: /* Reserved. */
194 fprintf(stderr
, "mcf_uart: Bad RX command\n");
199 void mcf_uart_write(void *opaque
, hwaddr addr
,
200 uint64_t val
, unsigned size
)
202 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
203 switch (addr
& 0x3f) {
205 s
->mr
[s
->current_mr
] = val
;
209 /* CSR is ignored. */
211 case 0x08: /* Command Register. */
212 mcf_do_command(s
, val
);
214 case 0x0c: /* Transmit Buffer. */
215 s
->sr
&= ~MCF_UART_TxEMP
;
220 /* ACR is ignored. */
231 static void mcf_uart_reset(DeviceState
*dev
)
233 mcf_uart_state
*s
= MCF_UART(dev
);
238 s
->sr
= MCF_UART_TxEMP
;
245 static void mcf_uart_push_byte(mcf_uart_state
*s
, uint8_t data
)
247 /* Break events overwrite the last byte if the fifo is full. */
248 if (s
->fifo_len
== 4)
251 s
->fifo
[s
->fifo_len
] = data
;
253 s
->sr
|= MCF_UART_RxRDY
;
254 if (s
->fifo_len
== 4)
255 s
->sr
|= MCF_UART_FFULL
;
260 static void mcf_uart_event(void *opaque
, QEMUChrEvent event
)
262 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
265 case CHR_EVENT_BREAK
:
266 s
->isr
|= MCF_UART_DBINT
;
267 mcf_uart_push_byte(s
, 0);
274 static int mcf_uart_can_receive(void *opaque
)
276 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
278 return s
->rx_enabled
&& (s
->sr
& MCF_UART_FFULL
) == 0;
281 static void mcf_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
283 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
285 mcf_uart_push_byte(s
, buf
[0]);
288 static const MemoryRegionOps mcf_uart_ops
= {
289 .read
= mcf_uart_read
,
290 .write
= mcf_uart_write
,
291 .endianness
= DEVICE_NATIVE_ENDIAN
,
294 static void mcf_uart_instance_init(Object
*obj
)
296 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
297 mcf_uart_state
*s
= MCF_UART(dev
);
299 memory_region_init_io(&s
->iomem
, obj
, &mcf_uart_ops
, s
, "uart", 0x40);
300 sysbus_init_mmio(dev
, &s
->iomem
);
302 sysbus_init_irq(dev
, &s
->irq
);
305 static void mcf_uart_realize(DeviceState
*dev
, Error
**errp
)
307 mcf_uart_state
*s
= MCF_UART(dev
);
309 qemu_chr_fe_set_handlers(&s
->chr
, mcf_uart_can_receive
, mcf_uart_receive
,
310 mcf_uart_event
, NULL
, s
, NULL
, true);
313 static Property mcf_uart_properties
[] = {
314 DEFINE_PROP_CHR("chardev", mcf_uart_state
, chr
),
315 DEFINE_PROP_END_OF_LIST(),
318 static void mcf_uart_class_init(ObjectClass
*oc
, void *data
)
320 DeviceClass
*dc
= DEVICE_CLASS(oc
);
322 dc
->realize
= mcf_uart_realize
;
323 dc
->reset
= mcf_uart_reset
;
324 device_class_set_props(dc
, mcf_uart_properties
);
325 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
328 static const TypeInfo mcf_uart_info
= {
329 .name
= TYPE_MCF_UART
,
330 .parent
= TYPE_SYS_BUS_DEVICE
,
331 .instance_size
= sizeof(mcf_uart_state
),
332 .instance_init
= mcf_uart_instance_init
,
333 .class_init
= mcf_uart_class_init
,
336 static void mcf_uart_register(void)
338 type_register_static(&mcf_uart_info
);
341 type_init(mcf_uart_register
)
343 void *mcf_uart_init(qemu_irq irq
, Chardev
*chrdrv
)
347 dev
= qdev_new(TYPE_MCF_UART
);
349 qdev_prop_set_chr(dev
, "chardev", chrdrv
);
351 sysbus_realize_and_unref(SYS_BUS_DEVICE(dev
), &error_fatal
);
353 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, irq
);
358 void mcf_uart_mm_init(hwaddr base
, qemu_irq irq
, Chardev
*chrdrv
)
362 dev
= mcf_uart_init(irq
, chrdrv
);
363 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);