2 * ColdFire UART emulation.
4 * Copyright (c) 2007 CodeSourcery.
6 * This code is licenced under the GPL
27 /* UART Status Register bits. */
28 #define MCF_UART_RxRDY 0x01
29 #define MCF_UART_FFULL 0x02
30 #define MCF_UART_TxRDY 0x04
31 #define MCF_UART_TxEMP 0x08
32 #define MCF_UART_OE 0x10
33 #define MCF_UART_PE 0x20
34 #define MCF_UART_FE 0x40
35 #define MCF_UART_RB 0x80
37 /* Interrupt flags. */
38 #define MCF_UART_TxINT 0x01
39 #define MCF_UART_RxINT 0x02
40 #define MCF_UART_DBINT 0x04
41 #define MCF_UART_COSINT 0x80
44 #define MCF_UART_BC0 0x01
45 #define MCF_UART_BC1 0x02
46 #define MCF_UART_PT 0x04
47 #define MCF_UART_PM0 0x08
48 #define MCF_UART_PM1 0x10
49 #define MCF_UART_ERR 0x20
50 #define MCF_UART_RxIRQ 0x40
51 #define MCF_UART_RxRTS 0x80
53 static void mcf_uart_update(mcf_uart_state
*s
)
55 s
->isr
&= ~(MCF_UART_TxINT
| MCF_UART_RxINT
);
56 if (s
->sr
& MCF_UART_TxRDY
)
57 s
->isr
|= MCF_UART_TxINT
;
58 if ((s
->sr
& ((s
->mr
[0] & MCF_UART_RxIRQ
)
59 ? MCF_UART_FFULL
: MCF_UART_RxRDY
)) != 0)
60 s
->isr
|= MCF_UART_RxINT
;
62 qemu_set_irq(s
->irq
, (s
->isr
& s
->imr
) != 0);
65 uint32_t mcf_uart_read(void *opaque
, target_phys_addr_t addr
)
67 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
68 switch (addr
& 0x3f) {
70 return s
->mr
[s
->current_mr
];
83 for (i
= 0; i
< s
->fifo_len
; i
++)
84 s
->fifo
[i
] = s
->fifo
[i
+ 1];
85 s
->sr
&= ~MCF_UART_FFULL
;
87 s
->sr
&= ~MCF_UART_RxRDY
;
92 /* TODO: Implement IPCR. */
105 /* Update TxRDY flag and set data if present and enabled. */
106 static void mcf_uart_do_tx(mcf_uart_state
*s
)
108 if (s
->tx_enabled
&& (s
->sr
& MCF_UART_TxEMP
) == 0) {
110 qemu_chr_write(s
->chr
, (unsigned char *)&s
->tb
, 1);
111 s
->sr
|= MCF_UART_TxEMP
;
114 s
->sr
|= MCF_UART_TxRDY
;
116 s
->sr
&= ~MCF_UART_TxRDY
;
120 static void mcf_do_command(mcf_uart_state
*s
, uint8_t cmd
)
123 switch ((cmd
>> 4) & 3) {
126 case 1: /* Reset mode register pointer. */
129 case 2: /* Reset receiver. */
132 s
->sr
&= ~(MCF_UART_RxRDY
| MCF_UART_FFULL
);
134 case 3: /* Reset transmitter. */
136 s
->sr
|= MCF_UART_TxEMP
;
137 s
->sr
&= ~MCF_UART_TxRDY
;
139 case 4: /* Reset error status. */
141 case 5: /* Reset break-change interrupt. */
142 s
->isr
&= ~MCF_UART_DBINT
;
144 case 6: /* Start break. */
145 case 7: /* Stop break. */
149 /* Transmitter command. */
150 switch ((cmd
>> 2) & 3) {
153 case 1: /* Enable. */
157 case 2: /* Disable. */
161 case 3: /* Reserved. */
162 fprintf(stderr
, "mcf_uart: Bad TX command\n");
166 /* Receiver command. */
170 case 1: /* Enable. */
176 case 3: /* Reserved. */
177 fprintf(stderr
, "mcf_uart: Bad RX command\n");
182 void mcf_uart_write(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
184 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
185 switch (addr
& 0x3f) {
187 s
->mr
[s
->current_mr
] = val
;
191 /* CSR is ignored. */
193 case 0x08: /* Command Register. */
194 mcf_do_command(s
, val
);
196 case 0x0c: /* Transmit Buffer. */
197 s
->sr
&= ~MCF_UART_TxEMP
;
202 /* ACR is ignored. */
213 static void mcf_uart_reset(mcf_uart_state
*s
)
218 s
->sr
= MCF_UART_TxEMP
;
225 static void mcf_uart_push_byte(mcf_uart_state
*s
, uint8_t data
)
227 /* Break events overwrite the last byte if the fifo is full. */
228 if (s
->fifo_len
== 4)
231 s
->fifo
[s
->fifo_len
] = data
;
233 s
->sr
|= MCF_UART_RxRDY
;
234 if (s
->fifo_len
== 4)
235 s
->sr
|= MCF_UART_FFULL
;
240 static void mcf_uart_event(void *opaque
, int event
)
242 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
245 case CHR_EVENT_BREAK
:
246 s
->isr
|= MCF_UART_DBINT
;
247 mcf_uart_push_byte(s
, 0);
254 static int mcf_uart_can_receive(void *opaque
)
256 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
258 return s
->rx_enabled
&& (s
->sr
& MCF_UART_FFULL
) == 0;
261 static void mcf_uart_receive(void *opaque
, const uint8_t *buf
, int size
)
263 mcf_uart_state
*s
= (mcf_uart_state
*)opaque
;
265 mcf_uart_push_byte(s
, buf
[0]);
268 void *mcf_uart_init(qemu_irq irq
, CharDriverState
*chr
)
272 s
= qemu_mallocz(sizeof(mcf_uart_state
));
276 qemu_chr_add_handlers(chr
, mcf_uart_can_receive
, mcf_uart_receive
,
284 static CPUReadMemoryFunc
*mcf_uart_readfn
[] = {
290 static CPUWriteMemoryFunc
*mcf_uart_writefn
[] = {
296 void mcf_uart_mm_init(target_phys_addr_t base
, qemu_irq irq
,
297 CharDriverState
*chr
)
302 s
= mcf_uart_init(irq
, chr
);
303 iomemtype
= cpu_register_io_memory(0, mcf_uart_readfn
,
304 mcf_uart_writefn
, s
);
305 cpu_register_physical_memory(base
, 0x40, iomemtype
);