2 * QEMU 16450 UART emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu-char.h"
28 #include "qemu-timer.h"
30 //#define DEBUG_SERIAL
32 #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
34 #define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
35 #define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
36 #define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
37 #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
39 #define UART_IIR_NO_INT 0x01 /* No interrupts pending */
40 #define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
42 #define UART_IIR_MSI 0x00 /* Modem status interrupt */
43 #define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
44 #define UART_IIR_RDI 0x04 /* Receiver data interrupt */
45 #define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
48 * These are the definitions for the Modem Control Register
50 #define UART_MCR_LOOP 0x10 /* Enable loopback test mode */
51 #define UART_MCR_OUT2 0x08 /* Out2 complement */
52 #define UART_MCR_OUT1 0x04 /* Out1 complement */
53 #define UART_MCR_RTS 0x02 /* RTS complement */
54 #define UART_MCR_DTR 0x01 /* DTR complement */
57 * These are the definitions for the Modem Status Register
59 #define UART_MSR_DCD 0x80 /* Data Carrier Detect */
60 #define UART_MSR_RI 0x40 /* Ring Indicator */
61 #define UART_MSR_DSR 0x20 /* Data Set Ready */
62 #define UART_MSR_CTS 0x10 /* Clear to Send */
63 #define UART_MSR_DDCD 0x08 /* Delta DCD */
64 #define UART_MSR_TERI 0x04 /* Trailing edge ring indicator */
65 #define UART_MSR_DDSR 0x02 /* Delta DSR */
66 #define UART_MSR_DCTS 0x01 /* Delta CTS */
67 #define UART_MSR_ANY_DELTA 0x0F /* Any of the delta bits! */
69 #define UART_LSR_TEMT 0x40 /* Transmitter empty */
70 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
71 #define UART_LSR_BI 0x10 /* Break interrupt indicator */
72 #define UART_LSR_FE 0x08 /* Frame error indicator */
73 #define UART_LSR_PE 0x04 /* Parity error indicator */
74 #define UART_LSR_OE 0x02 /* Overrun error indicator */
75 #define UART_LSR_DR 0x01 /* Receiver data ready */
78 * Delay TX IRQ after sending as much characters as the given interval would
79 * contain on real hardware. This avoids overloading the guest if it processes
80 * its output buffer in a loop inside the TX IRQ handler.
82 #define THROTTLE_TX_INTERVAL 10 /* ms */
86 uint8_t rbr
; /* receive register */
88 uint8_t iir
; /* read only */
91 uint8_t lsr
; /* read only */
92 uint8_t msr
; /* read only */
94 /* NOTE: this hidden state is necessary for tx irq generation as
95 it can be reset while reading iir */
99 int last_break_enable
;
100 target_phys_addr_t base
;
106 static void serial_receive_byte(SerialState
*s
, int ch
);
108 static void serial_update_irq(SerialState
*s
)
110 if ((s
->lsr
& UART_LSR_DR
) && (s
->ier
& UART_IER_RDI
)) {
111 s
->iir
= UART_IIR_RDI
;
112 } else if (s
->thr_ipending
&& (s
->ier
& UART_IER_THRI
)) {
113 s
->iir
= UART_IIR_THRI
;
115 s
->iir
= UART_IIR_NO_INT
;
117 if (s
->iir
!= UART_IIR_NO_INT
) {
118 qemu_irq_raise(s
->irq
);
120 qemu_irq_lower(s
->irq
);
124 static void serial_tx_done(void *opaque
)
126 SerialState
*s
= opaque
;
128 if (s
->tx_burst
< 0) {
132 divider
= s
->divider
;
136 /* We assume 10 bits/char, OK for this purpose. */
137 s
->tx_burst
= THROTTLE_TX_INTERVAL
* 1000 /
138 (1000000 * 10 / (115200 / divider
));
141 s
->lsr
|= UART_LSR_THRE
;
142 s
->lsr
|= UART_LSR_TEMT
;
143 serial_update_irq(s
);
146 static void serial_update_parameters(SerialState
*s
)
148 int speed
, parity
, data_bits
, stop_bits
;
149 QEMUSerialSetParams ssp
;
163 data_bits
= (s
->lcr
& 0x03) + 5;
166 speed
= 115200 / s
->divider
;
169 ssp
.data_bits
= data_bits
;
170 ssp
.stop_bits
= stop_bits
;
171 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
173 printf("speed=%d parity=%c data=%d stop=%d\n",
174 speed
, parity
, data_bits
, stop_bits
);
178 static void serial_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
180 SerialState
*s
= opaque
;
185 printf("serial: write addr=0x%02x val=0x%02x\n", addr
, val
);
190 if (s
->lcr
& UART_LCR_DLAB
) {
191 s
->divider
= (s
->divider
& 0xff00) | val
;
192 serial_update_parameters(s
);
195 s
->lsr
&= ~UART_LSR_THRE
;
196 serial_update_irq(s
);
198 if (!(s
->mcr
& UART_MCR_LOOP
)) {
199 /* when not in loopback mode, send the char */
200 qemu_chr_write(s
->chr
, &ch
, 1);
202 /* in loopback mode, say that we just received a char */
203 serial_receive_byte(s
, ch
);
205 if (s
->tx_burst
> 0) {
208 } else if (s
->tx_burst
== 0) {
210 qemu_mod_timer(s
->tx_timer
, qemu_get_clock(vm_clock
) +
211 ticks_per_sec
* THROTTLE_TX_INTERVAL
/ 1000);
216 if (s
->lcr
& UART_LCR_DLAB
) {
217 s
->divider
= (s
->divider
& 0x00ff) | (val
<< 8);
218 serial_update_parameters(s
);
221 if (s
->lsr
& UART_LSR_THRE
) {
224 serial_update_irq(s
);
233 serial_update_parameters(s
);
234 break_enable
= (val
>> 6) & 1;
235 if (break_enable
!= s
->last_break_enable
) {
236 s
->last_break_enable
= break_enable
;
237 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_BREAK
,
255 static uint32_t serial_ioport_read(void *opaque
, uint32_t addr
)
257 SerialState
*s
= opaque
;
264 if (s
->lcr
& UART_LCR_DLAB
) {
265 ret
= s
->divider
& 0xff;
268 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
269 serial_update_irq(s
);
270 if (!(s
->mcr
& UART_MCR_LOOP
)) {
271 /* in loopback mode, don't receive any data */
272 qemu_chr_accept_input(s
->chr
);
277 if (s
->lcr
& UART_LCR_DLAB
) {
278 ret
= (s
->divider
>> 8) & 0xff;
285 /* reset THR pending bit */
286 if ((ret
& 0x7) == UART_IIR_THRI
)
288 serial_update_irq(s
);
300 if (s
->mcr
& UART_MCR_LOOP
) {
301 /* in loopback, the modem output pins are connected to the
303 ret
= (s
->mcr
& 0x0c) << 4;
304 ret
|= (s
->mcr
& 0x02) << 3;
305 ret
|= (s
->mcr
& 0x01) << 5;
315 printf("serial: read addr=0x%02x val=0x%02x\n", addr
, ret
);
320 static int serial_can_receive(SerialState
*s
)
322 return !(s
->lsr
& UART_LSR_DR
);
325 static void serial_receive_byte(SerialState
*s
, int ch
)
328 s
->lsr
|= UART_LSR_DR
;
329 serial_update_irq(s
);
332 static void serial_receive_break(SerialState
*s
)
335 s
->lsr
|= UART_LSR_BI
| UART_LSR_DR
;
336 serial_update_irq(s
);
339 static int serial_can_receive1(void *opaque
)
341 SerialState
*s
= opaque
;
342 return serial_can_receive(s
);
345 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
347 SerialState
*s
= opaque
;
348 serial_receive_byte(s
, buf
[0]);
351 static void serial_event(void *opaque
, int event
)
353 SerialState
*s
= opaque
;
354 if (event
== CHR_EVENT_BREAK
)
355 serial_receive_break(s
);
358 static void serial_save(QEMUFile
*f
, void *opaque
)
360 SerialState
*s
= opaque
;
362 qemu_put_be16s(f
,&s
->divider
);
363 qemu_put_8s(f
,&s
->rbr
);
364 qemu_put_8s(f
,&s
->ier
);
365 qemu_put_8s(f
,&s
->iir
);
366 qemu_put_8s(f
,&s
->lcr
);
367 qemu_put_8s(f
,&s
->mcr
);
368 qemu_put_8s(f
,&s
->lsr
);
369 qemu_put_8s(f
,&s
->msr
);
370 qemu_put_8s(f
,&s
->scr
);
373 static int serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
375 SerialState
*s
= opaque
;
381 qemu_get_be16s(f
, &s
->divider
);
383 s
->divider
= qemu_get_byte(f
);
384 qemu_get_8s(f
,&s
->rbr
);
385 qemu_get_8s(f
,&s
->ier
);
386 qemu_get_8s(f
,&s
->iir
);
387 qemu_get_8s(f
,&s
->lcr
);
388 qemu_get_8s(f
,&s
->mcr
);
389 qemu_get_8s(f
,&s
->lsr
);
390 qemu_get_8s(f
,&s
->msr
);
391 qemu_get_8s(f
,&s
->scr
);
396 static void serial_reset(void *opaque
)
398 SerialState
*s
= opaque
;
403 s
->iir
= UART_IIR_NO_INT
;
406 s
->lsr
= UART_LSR_TEMT
| UART_LSR_THRE
;
407 s
->msr
= UART_MSR_DCD
| UART_MSR_DSR
| UART_MSR_CTS
;
411 s
->last_break_enable
= 0;
412 qemu_irq_lower(s
->irq
);
415 /* If fd is zero, it means that the serial device uses the console */
416 SerialState
*serial_init(int base
, qemu_irq irq
, CharDriverState
*chr
)
420 s
= qemu_mallocz(sizeof(SerialState
));
425 s
->tx_timer
= qemu_new_timer(vm_clock
, serial_tx_done
, s
);
429 qemu_register_reset(serial_reset
, s
);
432 register_savevm("serial", base
, 2, serial_save
, serial_load
, s
);
434 register_ioport_write(base
, 8, 1, serial_ioport_write
, s
);
435 register_ioport_read(base
, 8, 1, serial_ioport_read
, s
);
437 qemu_chr_add_handlers(chr
, serial_can_receive1
, serial_receive1
,
442 /* Memory mapped interface */
443 uint32_t serial_mm_readb (void *opaque
, target_phys_addr_t addr
)
445 SerialState
*s
= opaque
;
447 return serial_ioport_read(s
, (addr
- s
->base
) >> s
->it_shift
) & 0xFF;
450 void serial_mm_writeb (void *opaque
,
451 target_phys_addr_t addr
, uint32_t value
)
453 SerialState
*s
= opaque
;
455 serial_ioport_write(s
, (addr
- s
->base
) >> s
->it_shift
, value
& 0xFF);
458 uint32_t serial_mm_readw (void *opaque
, target_phys_addr_t addr
)
460 SerialState
*s
= opaque
;
463 val
= serial_ioport_read(s
, (addr
- s
->base
) >> s
->it_shift
) & 0xFFFF;
464 #ifdef TARGET_WORDS_BIGENDIAN
470 void serial_mm_writew (void *opaque
,
471 target_phys_addr_t addr
, uint32_t value
)
473 SerialState
*s
= opaque
;
474 #ifdef TARGET_WORDS_BIGENDIAN
475 value
= bswap16(value
);
477 serial_ioport_write(s
, (addr
- s
->base
) >> s
->it_shift
, value
& 0xFFFF);
480 uint32_t serial_mm_readl (void *opaque
, target_phys_addr_t addr
)
482 SerialState
*s
= opaque
;
485 val
= serial_ioport_read(s
, (addr
- s
->base
) >> s
->it_shift
);
486 #ifdef TARGET_WORDS_BIGENDIAN
492 void serial_mm_writel (void *opaque
,
493 target_phys_addr_t addr
, uint32_t value
)
495 SerialState
*s
= opaque
;
496 #ifdef TARGET_WORDS_BIGENDIAN
497 value
= bswap32(value
);
499 serial_ioport_write(s
, (addr
- s
->base
) >> s
->it_shift
, value
);
502 static CPUReadMemoryFunc
*serial_mm_read
[] = {
508 static CPUWriteMemoryFunc
*serial_mm_write
[] = {
514 SerialState
*serial_mm_init (target_phys_addr_t base
, int it_shift
,
515 qemu_irq irq
, CharDriverState
*chr
,
521 s
= qemu_mallocz(sizeof(SerialState
));
526 s
->it_shift
= it_shift
;
528 s
->tx_timer
= qemu_new_timer(vm_clock
, serial_tx_done
, s
);
532 qemu_register_reset(serial_reset
, s
);
535 register_savevm("serial", base
, 2, serial_save
, serial_load
, s
);
538 s_io_memory
= cpu_register_io_memory(0, serial_mm_read
,
540 cpu_register_physical_memory(base
, 8 << it_shift
, s_io_memory
);
543 qemu_chr_add_handlers(chr
, serial_can_receive1
, serial_receive1
,