2 * ColdFire UART emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licensed under the GPL
10 #include "qemu-char.h"
29 /* UART Status Register bits. */
30 #define MCF_UART_RxRDY 0x01
31 #define MCF_UART_FFULL 0x02
32 #define MCF_UART_TxRDY 0x04
33 #define MCF_UART_TxEMP 0x08
34 #define MCF_UART_OE 0x10
35 #define MCF_UART_PE 0x20
36 #define MCF_UART_FE 0x40
37 #define MCF_UART_RB 0x80
39 /* Interrupt flags. */
40 #define MCF_UART_TxINT 0x01
41 #define MCF_UART_RxINT 0x02
42 #define MCF_UART_DBINT 0x04
43 #define MCF_UART_COSINT 0x80
46 #define MCF_UART_BC0 0x01
47 #define MCF_UART_BC1 0x02
48 #define MCF_UART_PT 0x04
49 #define MCF_UART_PM0 0x08
50 #define MCF_UART_PM1 0x10
51 #define MCF_UART_ERR 0x20
52 #define MCF_UART_RxIRQ 0x40
53 #define MCF_UART_RxRTS 0x80
55 static void mcf_uart_update(mcf_uart_state
*s
)
57 s
->isr
&= ~(MCF_UART_TxINT
| MCF_UART_RxINT
);
58 if (s
->sr
& MCF_UART_TxRDY
)
59 s
->isr
|= MCF_UART_TxINT
;
60 if ((s
->sr
& ((s
->mr
[0] & MCF_UART_RxIRQ
)
61 ? MCF_UART_FFULL
: MCF_UART_RxRDY
)) != 0)
62 s
->isr
|= MCF_UART_RxINT
;
64 qemu_set_irq(s
->irq
, (s
->isr
& s
->imr
) != 0);
67 uint32_t mcf_uart_read(void *opaque
, target_phys_addr_t addr
)
69 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
70 switch (addr
& 0x3f) {
72 return s
->mr
[s
->current_mr
];
85 for (i
= 0; i
< s
->fifo_len
; i
++)
86 s
->fifo
[i
] = s
->fifo
[i
+ 1];
87 s
->sr
&= ~MCF_UART_FFULL
;
89 s
->sr
&= ~MCF_UART_RxRDY
;
91 qemu_chr_accept_input(s
->chr
);
95 /* TODO: Implement IPCR. */
108 /* Update TxRDY flag and set data if present and enabled. */
109 static void mcf_uart_do_tx(mcf_uart_state
*s
)
111 if (s
->tx_enabled
&& (s
->sr
& MCF_UART_TxEMP
) == 0) {
113 qemu_chr_fe_write(s
->chr
, (unsigned char *)&s
->tb
, 1);
114 s
->sr
|= MCF_UART_TxEMP
;
117 s
->sr
|= MCF_UART_TxRDY
;
119 s
->sr
&= ~MCF_UART_TxRDY
;
123 static void mcf_do_command(mcf_uart_state
*s
, uint8_t cmd
)
126 switch ((cmd
>> 4) & 3) {
129 case 1: /* Reset mode register pointer. */
132 case 2: /* Reset receiver. */
135 s
->sr
&= ~(MCF_UART_RxRDY
| MCF_UART_FFULL
);
137 case 3: /* Reset transmitter. */
139 s
->sr
|= MCF_UART_TxEMP
;
140 s
->sr
&= ~MCF_UART_TxRDY
;
142 case 4: /* Reset error status. */
144 case 5: /* Reset break-change interrupt. */
145 s
->isr
&= ~MCF_UART_DBINT
;
147 case 6: /* Start break. */
148 case 7: /* Stop break. */
152 /* Transmitter command. */
153 switch ((cmd
>> 2) & 3) {
156 case 1: /* Enable. */
160 case 2: /* Disable. */
164 case 3: /* Reserved. */
165 fprintf(stderr
, "mcf_uart: Bad TX command\n");
169 /* Receiver command. */
173 case 1: /* Enable. */
179 case 3: /* Reserved. */
180 fprintf(stderr
, "mcf_uart: Bad RX command\n");
185 void mcf_uart_write(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
187 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
188 switch (addr
& 0x3f) {
190 s
->mr
[s
->current_mr
] = val
;
194 /* CSR is ignored. */
196 case 0x08: /* Command Register. */
197 mcf_do_command(s
, val
);
199 case 0x0c: /* Transmit Buffer. */
200 s
->sr
&= ~MCF_UART_TxEMP
;
205 /* ACR is ignored. */
216 static void mcf_uart_reset(mcf_uart_state
*s
)
221 s
->sr
= MCF_UART_TxEMP
;
228 static void mcf_uart_push_byte(mcf_uart_state
*s
, uint8_t data
)
230 /* Break events overwrite the last byte if the fifo is full. */
231 if (s
->fifo_len
== 4)
234 s
->fifo
[s
->fifo_len
] = data
;
236 s
->sr
|= MCF_UART_RxRDY
;
237 if (s
->fifo_len
== 4)
238 s
->sr
|= MCF_UART_FFULL
;
243 static void mcf_uart_event(void *opaque
, int event
)
245 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
248 case CHR_EVENT_BREAK
:
249 s
->isr
|= MCF_UART_DBINT
;
250 mcf_uart_push_byte(s
, 0);
257 static int mcf_uart_can_receive(void *opaque
)
259 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
261 return s
->rx_enabled
&& (s
->sr
& MCF_UART_FFULL
) == 0;
264 static void mcf_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
266 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
268 mcf_uart_push_byte(s
, buf
[0]);
271 void *mcf_uart_init(qemu_irq irq
, CharDriverState
*chr
)
275 s
= g_malloc0(sizeof(mcf_uart_state
));
279 qemu_chr_add_handlers(chr
, mcf_uart_can_receive
, mcf_uart_receive
,
287 static CPUReadMemoryFunc
* const mcf_uart_readfn
[] = {
293 static CPUWriteMemoryFunc
* const mcf_uart_writefn
[] = {
299 void mcf_uart_mm_init(target_phys_addr_t base
, qemu_irq irq
,
300 CharDriverState
*chr
)
305 s
= mcf_uart_init(irq
, chr
);
306 iomemtype
= cpu_register_io_memory(mcf_uart_readfn
,
308 DEVICE_NATIVE_ENDIAN
);
309 cpu_register_physical_memory(base
, 0x40, iomemtype
);