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 "sysemu/char.h"
13 #include "exec/address-spaces.h"
14 #include "qapi/error.h"
17 SysBusDevice parent_obj
;
36 #define TYPE_MCF_UART "mcf-uart"
37 #define MCF_UART(obj) OBJECT_CHECK(mcf_uart_state, (obj), TYPE_MCF_UART)
39 /* UART Status Register bits. */
40 #define MCF_UART_RxRDY 0x01
41 #define MCF_UART_FFULL 0x02
42 #define MCF_UART_TxRDY 0x04
43 #define MCF_UART_TxEMP 0x08
44 #define MCF_UART_OE 0x10
45 #define MCF_UART_PE 0x20
46 #define MCF_UART_FE 0x40
47 #define MCF_UART_RB 0x80
49 /* Interrupt flags. */
50 #define MCF_UART_TxINT 0x01
51 #define MCF_UART_RxINT 0x02
52 #define MCF_UART_DBINT 0x04
53 #define MCF_UART_COSINT 0x80
56 #define MCF_UART_BC0 0x01
57 #define MCF_UART_BC1 0x02
58 #define MCF_UART_PT 0x04
59 #define MCF_UART_PM0 0x08
60 #define MCF_UART_PM1 0x10
61 #define MCF_UART_ERR 0x20
62 #define MCF_UART_RxIRQ 0x40
63 #define MCF_UART_RxRTS 0x80
65 static void mcf_uart_update(mcf_uart_state
*s
)
67 s
->isr
&= ~(MCF_UART_TxINT
| MCF_UART_RxINT
);
68 if (s
->sr
& MCF_UART_TxRDY
)
69 s
->isr
|= MCF_UART_TxINT
;
70 if ((s
->sr
& ((s
->mr
[0] & MCF_UART_RxIRQ
)
71 ? MCF_UART_FFULL
: MCF_UART_RxRDY
)) != 0)
72 s
->isr
|= MCF_UART_RxINT
;
74 qemu_set_irq(s
->irq
, (s
->isr
& s
->imr
) != 0);
77 uint64_t mcf_uart_read(void *opaque
, hwaddr addr
,
80 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
81 switch (addr
& 0x3f) {
83 return s
->mr
[s
->current_mr
];
96 for (i
= 0; i
< s
->fifo_len
; i
++)
97 s
->fifo
[i
] = s
->fifo
[i
+ 1];
98 s
->sr
&= ~MCF_UART_FFULL
;
100 s
->sr
&= ~MCF_UART_RxRDY
;
102 qemu_chr_fe_accept_input(&s
->chr
);
106 /* TODO: Implement IPCR. */
119 /* Update TxRDY flag and set data if present and enabled. */
120 static void mcf_uart_do_tx(mcf_uart_state
*s
)
122 if (s
->tx_enabled
&& (s
->sr
& MCF_UART_TxEMP
) == 0) {
123 /* XXX this blocks entire thread. Rewrite to use
124 * qemu_chr_fe_write and background I/O callbacks */
125 qemu_chr_fe_write_all(&s
->chr
, (unsigned char *)&s
->tb
, 1);
126 s
->sr
|= MCF_UART_TxEMP
;
129 s
->sr
|= MCF_UART_TxRDY
;
131 s
->sr
&= ~MCF_UART_TxRDY
;
135 static void mcf_do_command(mcf_uart_state
*s
, uint8_t cmd
)
138 switch ((cmd
>> 4) & 7) {
141 case 1: /* Reset mode register pointer. */
144 case 2: /* Reset receiver. */
147 s
->sr
&= ~(MCF_UART_RxRDY
| MCF_UART_FFULL
);
149 case 3: /* Reset transmitter. */
151 s
->sr
|= MCF_UART_TxEMP
;
152 s
->sr
&= ~MCF_UART_TxRDY
;
154 case 4: /* Reset error status. */
156 case 5: /* Reset break-change interrupt. */
157 s
->isr
&= ~MCF_UART_DBINT
;
159 case 6: /* Start break. */
160 case 7: /* Stop break. */
164 /* Transmitter command. */
165 switch ((cmd
>> 2) & 3) {
168 case 1: /* Enable. */
172 case 2: /* Disable. */
176 case 3: /* Reserved. */
177 fprintf(stderr
, "mcf_uart: Bad TX command\n");
181 /* Receiver command. */
185 case 1: /* Enable. */
191 case 3: /* Reserved. */
192 fprintf(stderr
, "mcf_uart: Bad RX command\n");
197 void mcf_uart_write(void *opaque
, hwaddr addr
,
198 uint64_t val
, unsigned size
)
200 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
201 switch (addr
& 0x3f) {
203 s
->mr
[s
->current_mr
] = val
;
207 /* CSR is ignored. */
209 case 0x08: /* Command Register. */
210 mcf_do_command(s
, val
);
212 case 0x0c: /* Transmit Buffer. */
213 s
->sr
&= ~MCF_UART_TxEMP
;
218 /* ACR is ignored. */
229 static void mcf_uart_reset(DeviceState
*dev
)
231 mcf_uart_state
*s
= MCF_UART(dev
);
236 s
->sr
= MCF_UART_TxEMP
;
243 static void mcf_uart_push_byte(mcf_uart_state
*s
, uint8_t data
)
245 /* Break events overwrite the last byte if the fifo is full. */
246 if (s
->fifo_len
== 4)
249 s
->fifo
[s
->fifo_len
] = data
;
251 s
->sr
|= MCF_UART_RxRDY
;
252 if (s
->fifo_len
== 4)
253 s
->sr
|= MCF_UART_FFULL
;
258 static void mcf_uart_event(void *opaque
, int event
)
260 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
263 case CHR_EVENT_BREAK
:
264 s
->isr
|= MCF_UART_DBINT
;
265 mcf_uart_push_byte(s
, 0);
272 static int mcf_uart_can_receive(void *opaque
)
274 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
276 return s
->rx_enabled
&& (s
->sr
& MCF_UART_FFULL
) == 0;
279 static void mcf_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
281 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
283 mcf_uart_push_byte(s
, buf
[0]);
286 static const MemoryRegionOps mcf_uart_ops
= {
287 .read
= mcf_uart_read
,
288 .write
= mcf_uart_write
,
289 .endianness
= DEVICE_NATIVE_ENDIAN
,
292 static void mcf_uart_instance_init(Object
*obj
)
294 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
295 mcf_uart_state
*s
= MCF_UART(dev
);
297 memory_region_init_io(&s
->iomem
, obj
, &mcf_uart_ops
, s
, "uart", 0x40);
298 sysbus_init_mmio(dev
, &s
->iomem
);
300 sysbus_init_irq(dev
, &s
->irq
);
303 static void mcf_uart_realize(DeviceState
*dev
, Error
**errp
)
305 mcf_uart_state
*s
= MCF_UART(dev
);
307 qemu_chr_fe_set_handlers(&s
->chr
, mcf_uart_can_receive
, mcf_uart_receive
,
308 mcf_uart_event
, s
, NULL
, true);
311 static Property mcf_uart_properties
[] = {
312 DEFINE_PROP_CHR("chardev", mcf_uart_state
, chr
),
313 DEFINE_PROP_END_OF_LIST(),
316 static void mcf_uart_class_init(ObjectClass
*oc
, void *data
)
318 DeviceClass
*dc
= DEVICE_CLASS(oc
);
320 dc
->realize
= mcf_uart_realize
;
321 dc
->reset
= mcf_uart_reset
;
322 dc
->props
= mcf_uart_properties
;
323 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
326 static const TypeInfo mcf_uart_info
= {
327 .name
= TYPE_MCF_UART
,
328 .parent
= TYPE_SYS_BUS_DEVICE
,
329 .instance_size
= sizeof(mcf_uart_state
),
330 .instance_init
= mcf_uart_instance_init
,
331 .class_init
= mcf_uart_class_init
,
334 static void mcf_uart_register(void)
336 type_register_static(&mcf_uart_info
);
339 type_init(mcf_uart_register
)
341 void *mcf_uart_init(qemu_irq irq
, Chardev
*chrdrv
)
345 dev
= qdev_create(NULL
, TYPE_MCF_UART
);
347 qdev_prop_set_chr(dev
, "chardev", chrdrv
);
349 qdev_init_nofail(dev
);
351 sysbus_connect_irq(SYS_BUS_DEVICE(dev
), 0, irq
);
356 void mcf_uart_mm_init(hwaddr base
, qemu_irq irq
, Chardev
*chrdrv
)
360 dev
= mcf_uart_init(irq
, chrdrv
);
361 sysbus_mmio_map(SYS_BUS_DEVICE(dev
), 0, base
);