2 * ColdFire UART emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
8 #include "qemu/osdep.h"
10 #include "hw/sysbus.h"
11 #include "hw/m68k/mcf.h"
12 #include "chardev/char-fe.h"
15 SysBusDevice parent_obj
;
34 #define TYPE_MCF_UART "mcf-uart"
35 #define MCF_UART(obj) OBJECT_CHECK(mcf_uart_state, (obj), TYPE_MCF_UART)
37 /* UART Status Register bits. */
38 #define MCF_UART_RxRDY 0x01
39 #define MCF_UART_FFULL 0x02
40 #define MCF_UART_TxRDY 0x04
41 #define MCF_UART_TxEMP 0x08
42 #define MCF_UART_OE 0x10
43 #define MCF_UART_PE 0x20
44 #define MCF_UART_FE 0x40
45 #define MCF_UART_RB 0x80
47 /* Interrupt flags. */
48 #define MCF_UART_TxINT 0x01
49 #define MCF_UART_RxINT 0x02
50 #define MCF_UART_DBINT 0x04
51 #define MCF_UART_COSINT 0x80
54 #define MCF_UART_BC0 0x01
55 #define MCF_UART_BC1 0x02
56 #define MCF_UART_PT 0x04
57 #define MCF_UART_PM0 0x08
58 #define MCF_UART_PM1 0x10
59 #define MCF_UART_ERR 0x20
60 #define MCF_UART_RxIRQ 0x40
61 #define MCF_UART_RxRTS 0x80
63 static void mcf_uart_update(mcf_uart_state
*s
)
65 s
->isr
&= ~(MCF_UART_TxINT
| MCF_UART_RxINT
);
66 if (s
->sr
& MCF_UART_TxRDY
)
67 s
->isr
|= MCF_UART_TxINT
;
68 if ((s
->sr
& ((s
->mr
[0] & MCF_UART_RxIRQ
)
69 ? MCF_UART_FFULL
: MCF_UART_RxRDY
)) != 0)
70 s
->isr
|= MCF_UART_RxINT
;
72 qemu_set_irq(s
->irq
, (s
->isr
& s
->imr
) != 0);
75 uint64_t mcf_uart_read(void *opaque
, hwaddr addr
,
78 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
79 switch (addr
& 0x3f) {
81 return s
->mr
[s
->current_mr
];
94 for (i
= 0; i
< s
->fifo_len
; i
++)
95 s
->fifo
[i
] = s
->fifo
[i
+ 1];
96 s
->sr
&= ~MCF_UART_FFULL
;
98 s
->sr
&= ~MCF_UART_RxRDY
;
100 qemu_chr_fe_accept_input(&s
->chr
);
104 /* TODO: Implement IPCR. */
117 /* Update TxRDY flag and set data if present and enabled. */
118 static void mcf_uart_do_tx(mcf_uart_state
*s
)
120 if (s
->tx_enabled
&& (s
->sr
& MCF_UART_TxEMP
) == 0) {
121 /* XXX this blocks entire thread. Rewrite to use
122 * qemu_chr_fe_write and background I/O callbacks */
123 qemu_chr_fe_write_all(&s
->chr
, (unsigned char *)&s
->tb
, 1);
124 s
->sr
|= MCF_UART_TxEMP
;
127 s
->sr
|= MCF_UART_TxRDY
;
129 s
->sr
&= ~MCF_UART_TxRDY
;
133 static void mcf_do_command(mcf_uart_state
*s
, uint8_t cmd
)
136 switch ((cmd
>> 4) & 7) {
139 case 1: /* Reset mode register pointer. */
142 case 2: /* Reset receiver. */
145 s
->sr
&= ~(MCF_UART_RxRDY
| MCF_UART_FFULL
);
147 case 3: /* Reset transmitter. */
149 s
->sr
|= MCF_UART_TxEMP
;
150 s
->sr
&= ~MCF_UART_TxRDY
;
152 case 4: /* Reset error status. */
154 case 5: /* Reset break-change interrupt. */
155 s
->isr
&= ~MCF_UART_DBINT
;
157 case 6: /* Start break. */
158 case 7: /* Stop break. */
162 /* Transmitter command. */
163 switch ((cmd
>> 2) & 3) {
166 case 1: /* Enable. */
170 case 2: /* Disable. */
174 case 3: /* Reserved. */
175 fprintf(stderr
, "mcf_uart: Bad TX command\n");
179 /* Receiver command. */
183 case 1: /* Enable. */
189 case 3: /* Reserved. */
190 fprintf(stderr
, "mcf_uart: Bad RX command\n");
195 void mcf_uart_write(void *opaque
, hwaddr addr
,
196 uint64_t val
, unsigned size
)
198 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
199 switch (addr
& 0x3f) {
201 s
->mr
[s
->current_mr
] = val
;
205 /* CSR is ignored. */
207 case 0x08: /* Command Register. */
208 mcf_do_command(s
, val
);
210 case 0x0c: /* Transmit Buffer. */
211 s
->sr
&= ~MCF_UART_TxEMP
;
216 /* ACR is ignored. */
227 static void mcf_uart_reset(DeviceState
*dev
)
229 mcf_uart_state
*s
= MCF_UART(dev
);
234 s
->sr
= MCF_UART_TxEMP
;
241 static void mcf_uart_push_byte(mcf_uart_state
*s
, uint8_t data
)
243 /* Break events overwrite the last byte if the fifo is full. */
244 if (s
->fifo_len
== 4)
247 s
->fifo
[s
->fifo_len
] = data
;
249 s
->sr
|= MCF_UART_RxRDY
;
250 if (s
->fifo_len
== 4)
251 s
->sr
|= MCF_UART_FFULL
;
256 static void mcf_uart_event(void *opaque
, int event
)
258 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
261 case CHR_EVENT_BREAK
:
262 s
->isr
|= MCF_UART_DBINT
;
263 mcf_uart_push_byte(s
, 0);
270 static int mcf_uart_can_receive(void *opaque
)
272 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
274 return s
->rx_enabled
&& (s
->sr
& MCF_UART_FFULL
) == 0;
277 static void mcf_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
279 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
281 mcf_uart_push_byte(s
, buf
[0]);
284 static const MemoryRegionOps mcf_uart_ops
= {
285 .read
= mcf_uart_read
,
286 .write
= mcf_uart_write
,
287 .endianness
= DEVICE_NATIVE_ENDIAN
,
290 static void mcf_uart_instance_init(Object
*obj
)
292 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
293 mcf_uart_state
*s
= MCF_UART(dev
);
295 memory_region_init_io(&s
->iomem
, obj
, &mcf_uart_ops
, s
, "uart", 0x40);
296 sysbus_init_mmio(dev
, &s
->iomem
);
298 sysbus_init_irq(dev
, &s
->irq
);
301 static void mcf_uart_realize(DeviceState
*dev
, Error
**errp
)
303 mcf_uart_state
*s
= MCF_UART(dev
);
305 qemu_chr_fe_set_handlers(&s
->chr
, mcf_uart_can_receive
, mcf_uart_receive
,
306 mcf_uart_event
, NULL
, s
, NULL
, true);
309 static Property mcf_uart_properties
[] = {
310 DEFINE_PROP_CHR("chardev", mcf_uart_state
, chr
),
311 DEFINE_PROP_END_OF_LIST(),
314 static void mcf_uart_class_init(ObjectClass
*oc
, void *data
)
316 DeviceClass
*dc
= DEVICE_CLASS(oc
);
318 dc
->realize
= mcf_uart_realize
;
319 dc
->reset
= mcf_uart_reset
;
320 dc
->props
= mcf_uart_properties
;
321 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
324 static const TypeInfo mcf_uart_info
= {
325 .name
= TYPE_MCF_UART
,
326 .parent
= TYPE_SYS_BUS_DEVICE
,
327 .instance_size
= sizeof(mcf_uart_state
),
328 .instance_init
= mcf_uart_instance_init
,
329 .class_init
= mcf_uart_class_init
,
332 static void mcf_uart_register(void)
334 type_register_static(&mcf_uart_info
);
337 type_init(mcf_uart_register
)
339 void *mcf_uart_init(qemu_irq irq
, Chardev
*chrdrv
)
343 dev
= qdev_create(NULL
, TYPE_MCF_UART
);
345 qdev_prop_set_chr(dev
, "chardev", chrdrv
);
347 qdev_init_nofail(dev
);
349 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, irq
);
354 void mcf_uart_mm_init(hwaddr base
, qemu_irq irq
, Chardev
*chrdrv
)
358 dev
= mcf_uart_init(irq
, chrdrv
);
359 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);