2 * ColdFire UART emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
10 #include "qemu-char.h"
11 #include "exec-memory.h"
31 /* UART Status Register bits. */
32 #define MCF_UART_RxRDY 0x01
33 #define MCF_UART_FFULL 0x02
34 #define MCF_UART_TxRDY 0x04
35 #define MCF_UART_TxEMP 0x08
36 #define MCF_UART_OE 0x10
37 #define MCF_UART_PE 0x20
38 #define MCF_UART_FE 0x40
39 #define MCF_UART_RB 0x80
41 /* Interrupt flags. */
42 #define MCF_UART_TxINT 0x01
43 #define MCF_UART_RxINT 0x02
44 #define MCF_UART_DBINT 0x04
45 #define MCF_UART_COSINT 0x80
48 #define MCF_UART_BC0 0x01
49 #define MCF_UART_BC1 0x02
50 #define MCF_UART_PT 0x04
51 #define MCF_UART_PM0 0x08
52 #define MCF_UART_PM1 0x10
53 #define MCF_UART_ERR 0x20
54 #define MCF_UART_RxIRQ 0x40
55 #define MCF_UART_RxRTS 0x80
57 static void mcf_uart_update(mcf_uart_state
*s
)
59 s
->isr
&= ~(MCF_UART_TxINT
| MCF_UART_RxINT
);
60 if (s
->sr
& MCF_UART_TxRDY
)
61 s
->isr
|= MCF_UART_TxINT
;
62 if ((s
->sr
& ((s
->mr
[0] & MCF_UART_RxIRQ
)
63 ? MCF_UART_FFULL
: MCF_UART_RxRDY
)) != 0)
64 s
->isr
|= MCF_UART_RxINT
;
66 qemu_set_irq(s
->irq
, (s
->isr
& s
->imr
) != 0);
69 uint64_t mcf_uart_read(void *opaque
, target_phys_addr_t addr
,
72 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
73 switch (addr
& 0x3f) {
75 return s
->mr
[s
->current_mr
];
88 for (i
= 0; i
< s
->fifo_len
; i
++)
89 s
->fifo
[i
] = s
->fifo
[i
+ 1];
90 s
->sr
&= ~MCF_UART_FFULL
;
92 s
->sr
&= ~MCF_UART_RxRDY
;
94 qemu_chr_accept_input(s
->chr
);
98 /* TODO: Implement IPCR. */
111 /* Update TxRDY flag and set data if present and enabled. */
112 static void mcf_uart_do_tx(mcf_uart_state
*s
)
114 if (s
->tx_enabled
&& (s
->sr
& MCF_UART_TxEMP
) == 0) {
116 qemu_chr_fe_write(s
->chr
, (unsigned char *)&s
->tb
, 1);
117 s
->sr
|= MCF_UART_TxEMP
;
120 s
->sr
|= MCF_UART_TxRDY
;
122 s
->sr
&= ~MCF_UART_TxRDY
;
126 static void mcf_do_command(mcf_uart_state
*s
, uint8_t cmd
)
129 switch ((cmd
>> 4) & 3) {
132 case 1: /* Reset mode register pointer. */
135 case 2: /* Reset receiver. */
138 s
->sr
&= ~(MCF_UART_RxRDY
| MCF_UART_FFULL
);
140 case 3: /* Reset transmitter. */
142 s
->sr
|= MCF_UART_TxEMP
;
143 s
->sr
&= ~MCF_UART_TxRDY
;
145 case 4: /* Reset error status. */
147 case 5: /* Reset break-change interrupt. */
148 s
->isr
&= ~MCF_UART_DBINT
;
150 case 6: /* Start break. */
151 case 7: /* Stop break. */
155 /* Transmitter command. */
156 switch ((cmd
>> 2) & 3) {
159 case 1: /* Enable. */
163 case 2: /* Disable. */
167 case 3: /* Reserved. */
168 fprintf(stderr
, "mcf_uart: Bad TX command\n");
172 /* Receiver command. */
176 case 1: /* Enable. */
182 case 3: /* Reserved. */
183 fprintf(stderr
, "mcf_uart: Bad RX command\n");
188 void mcf_uart_write(void *opaque
, target_phys_addr_t addr
,
189 uint64_t val
, unsigned size
)
191 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
192 switch (addr
& 0x3f) {
194 s
->mr
[s
->current_mr
] = val
;
198 /* CSR is ignored. */
200 case 0x08: /* Command Register. */
201 mcf_do_command(s
, val
);
203 case 0x0c: /* Transmit Buffer. */
204 s
->sr
&= ~MCF_UART_TxEMP
;
209 /* ACR is ignored. */
220 static void mcf_uart_reset(mcf_uart_state
*s
)
225 s
->sr
= MCF_UART_TxEMP
;
232 static void mcf_uart_push_byte(mcf_uart_state
*s
, uint8_t data
)
234 /* Break events overwrite the last byte if the fifo is full. */
235 if (s
->fifo_len
== 4)
238 s
->fifo
[s
->fifo_len
] = data
;
240 s
->sr
|= MCF_UART_RxRDY
;
241 if (s
->fifo_len
== 4)
242 s
->sr
|= MCF_UART_FFULL
;
247 static void mcf_uart_event(void *opaque
, int event
)
249 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
252 case CHR_EVENT_BREAK
:
253 s
->isr
|= MCF_UART_DBINT
;
254 mcf_uart_push_byte(s
, 0);
261 static int mcf_uart_can_receive(void *opaque
)
263 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
265 return s
->rx_enabled
&& (s
->sr
& MCF_UART_FFULL
) == 0;
268 static void mcf_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
270 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
272 mcf_uart_push_byte(s
, buf
[0]);
275 void *mcf_uart_init(qemu_irq irq
, CharDriverState
*chr
)
279 s
= g_malloc0(sizeof(mcf_uart_state
));
283 qemu_chr_add_handlers(chr
, mcf_uart_can_receive
, mcf_uart_receive
,
290 static const MemoryRegionOps mcf_uart_ops
= {
291 .read
= mcf_uart_read
,
292 .write
= mcf_uart_write
,
293 .endianness
= DEVICE_NATIVE_ENDIAN
,
296 void mcf_uart_mm_init(MemoryRegion
*sysmem
,
297 target_phys_addr_t base
,
299 CharDriverState
*chr
)
303 s
= mcf_uart_init(irq
, chr
);
304 memory_region_init_io(&s
->iomem
, &mcf_uart_ops
, s
, "uart", 0x40);
305 memory_region_add_subregion(sysmem
, base
, &s
->iomem
);