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 "hw/m68k/mcf.h"
14 #include "hw/qdev-properties.h"
15 #include "chardev/char-fe.h"
18 SysBusDevice parent_obj
;
37 #define TYPE_MCF_UART "mcf-uart"
38 #define MCF_UART(obj) OBJECT_CHECK(mcf_uart_state, (obj), TYPE_MCF_UART)
40 /* UART Status Register bits. */
41 #define MCF_UART_RxRDY 0x01
42 #define MCF_UART_FFULL 0x02
43 #define MCF_UART_TxRDY 0x04
44 #define MCF_UART_TxEMP 0x08
45 #define MCF_UART_OE 0x10
46 #define MCF_UART_PE 0x20
47 #define MCF_UART_FE 0x40
48 #define MCF_UART_RB 0x80
50 /* Interrupt flags. */
51 #define MCF_UART_TxINT 0x01
52 #define MCF_UART_RxINT 0x02
53 #define MCF_UART_DBINT 0x04
54 #define MCF_UART_COSINT 0x80
57 #define MCF_UART_BC0 0x01
58 #define MCF_UART_BC1 0x02
59 #define MCF_UART_PT 0x04
60 #define MCF_UART_PM0 0x08
61 #define MCF_UART_PM1 0x10
62 #define MCF_UART_ERR 0x20
63 #define MCF_UART_RxIRQ 0x40
64 #define MCF_UART_RxRTS 0x80
66 static void mcf_uart_update(mcf_uart_state
*s
)
68 s
->isr
&= ~(MCF_UART_TxINT
| MCF_UART_RxINT
);
69 if (s
->sr
& MCF_UART_TxRDY
)
70 s
->isr
|= MCF_UART_TxINT
;
71 if ((s
->sr
& ((s
->mr
[0] & MCF_UART_RxIRQ
)
72 ? MCF_UART_FFULL
: MCF_UART_RxRDY
)) != 0)
73 s
->isr
|= MCF_UART_RxINT
;
75 qemu_set_irq(s
->irq
, (s
->isr
& s
->imr
) != 0);
78 uint64_t mcf_uart_read(void *opaque
, hwaddr addr
,
81 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
82 switch (addr
& 0x3f) {
84 return s
->mr
[s
->current_mr
];
97 for (i
= 0; i
< s
->fifo_len
; i
++)
98 s
->fifo
[i
] = s
->fifo
[i
+ 1];
99 s
->sr
&= ~MCF_UART_FFULL
;
100 if (s
->fifo_len
== 0)
101 s
->sr
&= ~MCF_UART_RxRDY
;
103 qemu_chr_fe_accept_input(&s
->chr
);
107 /* TODO: Implement IPCR. */
120 /* Update TxRDY flag and set data if present and enabled. */
121 static void mcf_uart_do_tx(mcf_uart_state
*s
)
123 if (s
->tx_enabled
&& (s
->sr
& MCF_UART_TxEMP
) == 0) {
124 /* XXX this blocks entire thread. Rewrite to use
125 * qemu_chr_fe_write and background I/O callbacks */
126 qemu_chr_fe_write_all(&s
->chr
, (unsigned char *)&s
->tb
, 1);
127 s
->sr
|= MCF_UART_TxEMP
;
130 s
->sr
|= MCF_UART_TxRDY
;
132 s
->sr
&= ~MCF_UART_TxRDY
;
136 static void mcf_do_command(mcf_uart_state
*s
, uint8_t cmd
)
139 switch ((cmd
>> 4) & 7) {
142 case 1: /* Reset mode register pointer. */
145 case 2: /* Reset receiver. */
148 s
->sr
&= ~(MCF_UART_RxRDY
| MCF_UART_FFULL
);
150 case 3: /* Reset transmitter. */
152 s
->sr
|= MCF_UART_TxEMP
;
153 s
->sr
&= ~MCF_UART_TxRDY
;
155 case 4: /* Reset error status. */
157 case 5: /* Reset break-change interrupt. */
158 s
->isr
&= ~MCF_UART_DBINT
;
160 case 6: /* Start break. */
161 case 7: /* Stop break. */
165 /* Transmitter command. */
166 switch ((cmd
>> 2) & 3) {
169 case 1: /* Enable. */
173 case 2: /* Disable. */
177 case 3: /* Reserved. */
178 fprintf(stderr
, "mcf_uart: Bad TX command\n");
182 /* Receiver command. */
186 case 1: /* Enable. */
192 case 3: /* Reserved. */
193 fprintf(stderr
, "mcf_uart: Bad RX command\n");
198 void mcf_uart_write(void *opaque
, hwaddr addr
,
199 uint64_t val
, unsigned size
)
201 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
202 switch (addr
& 0x3f) {
204 s
->mr
[s
->current_mr
] = val
;
208 /* CSR is ignored. */
210 case 0x08: /* Command Register. */
211 mcf_do_command(s
, val
);
213 case 0x0c: /* Transmit Buffer. */
214 s
->sr
&= ~MCF_UART_TxEMP
;
219 /* ACR is ignored. */
230 static void mcf_uart_reset(DeviceState
*dev
)
232 mcf_uart_state
*s
= MCF_UART(dev
);
237 s
->sr
= MCF_UART_TxEMP
;
244 static void mcf_uart_push_byte(mcf_uart_state
*s
, uint8_t data
)
246 /* Break events overwrite the last byte if the fifo is full. */
247 if (s
->fifo_len
== 4)
250 s
->fifo
[s
->fifo_len
] = data
;
252 s
->sr
|= MCF_UART_RxRDY
;
253 if (s
->fifo_len
== 4)
254 s
->sr
|= MCF_UART_FFULL
;
259 static void mcf_uart_event(void *opaque
, int event
)
261 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
264 case CHR_EVENT_BREAK
:
265 s
->isr
|= MCF_UART_DBINT
;
266 mcf_uart_push_byte(s
, 0);
273 static int mcf_uart_can_receive(void *opaque
)
275 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
277 return s
->rx_enabled
&& (s
->sr
& MCF_UART_FFULL
) == 0;
280 static void mcf_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
282 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
284 mcf_uart_push_byte(s
, buf
[0]);
287 static const MemoryRegionOps mcf_uart_ops
= {
288 .read
= mcf_uart_read
,
289 .write
= mcf_uart_write
,
290 .endianness
= DEVICE_NATIVE_ENDIAN
,
293 static void mcf_uart_instance_init(Object
*obj
)
295 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
296 mcf_uart_state
*s
= MCF_UART(dev
);
298 memory_region_init_io(&s
->iomem
, obj
, &mcf_uart_ops
, s
, "uart", 0x40);
299 sysbus_init_mmio(dev
, &s
->iomem
);
301 sysbus_init_irq(dev
, &s
->irq
);
304 static void mcf_uart_realize(DeviceState
*dev
, Error
**errp
)
306 mcf_uart_state
*s
= MCF_UART(dev
);
308 qemu_chr_fe_set_handlers(&s
->chr
, mcf_uart_can_receive
, mcf_uart_receive
,
309 mcf_uart_event
, NULL
, s
, NULL
, true);
312 static Property mcf_uart_properties
[] = {
313 DEFINE_PROP_CHR("chardev", mcf_uart_state
, chr
),
314 DEFINE_PROP_END_OF_LIST(),
317 static void mcf_uart_class_init(ObjectClass
*oc
, void *data
)
319 DeviceClass
*dc
= DEVICE_CLASS(oc
);
321 dc
->realize
= mcf_uart_realize
;
322 dc
->reset
= mcf_uart_reset
;
323 dc
->props
= mcf_uart_properties
;
324 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
327 static const TypeInfo mcf_uart_info
= {
328 .name
= TYPE_MCF_UART
,
329 .parent
= TYPE_SYS_BUS_DEVICE
,
330 .instance_size
= sizeof(mcf_uart_state
),
331 .instance_init
= mcf_uart_instance_init
,
332 .class_init
= mcf_uart_class_init
,
335 static void mcf_uart_register(void)
337 type_register_static(&mcf_uart_info
);
340 type_init(mcf_uart_register
)
342 void *mcf_uart_init(qemu_irq irq
, Chardev
*chrdrv
)
346 dev
= qdev_create(NULL
, TYPE_MCF_UART
);
348 qdev_prop_set_chr(dev
, "chardev", chrdrv
);
350 qdev_init_nofail(dev
);
352 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, irq
);
357 void mcf_uart_mm_init(hwaddr base
, qemu_irq irq
, Chardev
*chrdrv
)
361 dev
= mcf_uart_init(irq
, chrdrv
);
362 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);