2 * QEMU 16550A UART emulation
4 * Copyright (c) 2003-2004 Fabrice Bellard
5 * Copyright (c) 2008 Citrix Systems, Inc.
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 #include "qemu-char.h"
29 #include "qemu-timer.h"
32 //#define DEBUG_SERIAL
34 #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
36 #define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
37 #define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
38 #define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
39 #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
41 #define UART_IIR_NO_INT 0x01 /* No interrupts pending */
42 #define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
44 #define UART_IIR_MSI 0x00 /* Modem status interrupt */
45 #define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
46 #define UART_IIR_RDI 0x04 /* Receiver data interrupt */
47 #define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
48 #define UART_IIR_CTI 0x0C /* Character Timeout Indication */
50 #define UART_IIR_FENF 0x80 /* Fifo enabled, but not functionning */
51 #define UART_IIR_FE 0xC0 /* Fifo enabled */
54 * These are the definitions for the Modem Control Register
56 #define UART_MCR_LOOP 0x10 /* Enable loopback test mode */
57 #define UART_MCR_OUT2 0x08 /* Out2 complement */
58 #define UART_MCR_OUT1 0x04 /* Out1 complement */
59 #define UART_MCR_RTS 0x02 /* RTS complement */
60 #define UART_MCR_DTR 0x01 /* DTR complement */
63 * These are the definitions for the Modem Status Register
65 #define UART_MSR_DCD 0x80 /* Data Carrier Detect */
66 #define UART_MSR_RI 0x40 /* Ring Indicator */
67 #define UART_MSR_DSR 0x20 /* Data Set Ready */
68 #define UART_MSR_CTS 0x10 /* Clear to Send */
69 #define UART_MSR_DDCD 0x08 /* Delta DCD */
70 #define UART_MSR_TERI 0x04 /* Trailing edge ring indicator */
71 #define UART_MSR_DDSR 0x02 /* Delta DSR */
72 #define UART_MSR_DCTS 0x01 /* Delta CTS */
73 #define UART_MSR_ANY_DELTA 0x0F /* Any of the delta bits! */
75 #define UART_LSR_TEMT 0x40 /* Transmitter empty */
76 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
77 #define UART_LSR_BI 0x10 /* Break interrupt indicator */
78 #define UART_LSR_FE 0x08 /* Frame error indicator */
79 #define UART_LSR_PE 0x04 /* Parity error indicator */
80 #define UART_LSR_OE 0x02 /* Overrun error indicator */
81 #define UART_LSR_DR 0x01 /* Receiver data ready */
82 #define UART_LSR_INT_ANY 0x1E /* Any of the lsr-interrupt-triggering status bits */
84 /* Interrupt trigger levels. The byte-counts are for 16550A - in newer UARTs the byte-count for each ITL is higher. */
86 #define UART_FCR_ITL_1 0x00 /* 1 byte ITL */
87 #define UART_FCR_ITL_2 0x40 /* 4 bytes ITL */
88 #define UART_FCR_ITL_3 0x80 /* 8 bytes ITL */
89 #define UART_FCR_ITL_4 0xC0 /* 14 bytes ITL */
91 #define UART_FCR_DMS 0x08 /* DMA Mode Select */
92 #define UART_FCR_XFR 0x04 /* XMIT Fifo Reset */
93 #define UART_FCR_RFR 0x02 /* RCVR Fifo Reset */
94 #define UART_FCR_FE 0x01 /* FIFO Enable */
96 #define UART_FIFO_LENGTH 16 /* 16550A Fifo Length */
100 #define MAX_XMIT_RETRY 4
103 #define DPRINTF(fmt, ...) \
104 do { fprintf(stderr, "serial: " fmt , ## __VA_ARGS__); } while (0)
106 #define DPRINTF(fmt, ...) \
110 typedef struct SerialFIFO
{
111 uint8_t data
[UART_FIFO_LENGTH
];
113 uint8_t itl
; /* Interrupt Trigger Level */
120 uint8_t rbr
; /* receive register */
121 uint8_t thr
; /* transmit holding register */
122 uint8_t tsr
; /* transmit shift register */
124 uint8_t iir
; /* read only */
127 uint8_t lsr
; /* read only */
128 uint8_t msr
; /* read only */
131 uint8_t fcr_vmstate
; /* we can't write directly this value
132 it has side effects */
133 /* NOTE: this hidden state is necessary for tx irq generation as
134 it can be reset while reading iir */
137 CharDriverState
*chr
;
138 int last_break_enable
;
143 uint64_t last_xmit_ts
; /* Time when the last byte was successfully sent out of the tsr */
144 SerialFIFO recv_fifo
;
145 SerialFIFO xmit_fifo
;
147 struct QEMUTimer
*fifo_timeout_timer
;
148 int timeout_ipending
; /* timeout interrupt pending state */
149 struct QEMUTimer
*transmit_timer
;
152 uint64_t char_transmit_time
; /* time to transmit a char in ticks*/
155 struct QEMUTimer
*modem_status_poll
;
158 typedef struct ISASerialState
{
166 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
);
168 static void fifo_clear(SerialState
*s
, int fifo
)
170 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
171 memset(f
->data
, 0, UART_FIFO_LENGTH
);
177 static int fifo_put(SerialState
*s
, int fifo
, uint8_t chr
)
179 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
181 /* Receive overruns do not overwrite FIFO contents. */
182 if (fifo
== XMIT_FIFO
|| f
->count
< UART_FIFO_LENGTH
) {
184 f
->data
[f
->head
++] = chr
;
186 if (f
->head
== UART_FIFO_LENGTH
)
190 if (f
->count
< UART_FIFO_LENGTH
)
192 else if (fifo
== RECV_FIFO
)
193 s
->lsr
|= UART_LSR_OE
;
198 static uint8_t fifo_get(SerialState
*s
, int fifo
)
200 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
206 c
= f
->data
[f
->tail
++];
207 if (f
->tail
== UART_FIFO_LENGTH
)
214 static void serial_update_irq(SerialState
*s
)
216 uint8_t tmp_iir
= UART_IIR_NO_INT
;
218 if ((s
->ier
& UART_IER_RLSI
) && (s
->lsr
& UART_LSR_INT_ANY
)) {
219 tmp_iir
= UART_IIR_RLSI
;
220 } else if ((s
->ier
& UART_IER_RDI
) && s
->timeout_ipending
) {
221 /* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
222 * this is not in the specification but is observed on existing
224 tmp_iir
= UART_IIR_CTI
;
225 } else if ((s
->ier
& UART_IER_RDI
) && (s
->lsr
& UART_LSR_DR
) &&
226 (!(s
->fcr
& UART_FCR_FE
) ||
227 s
->recv_fifo
.count
>= s
->recv_fifo
.itl
)) {
228 tmp_iir
= UART_IIR_RDI
;
229 } else if ((s
->ier
& UART_IER_THRI
) && s
->thr_ipending
) {
230 tmp_iir
= UART_IIR_THRI
;
231 } else if ((s
->ier
& UART_IER_MSI
) && (s
->msr
& UART_MSR_ANY_DELTA
)) {
232 tmp_iir
= UART_IIR_MSI
;
235 s
->iir
= tmp_iir
| (s
->iir
& 0xF0);
237 if (tmp_iir
!= UART_IIR_NO_INT
) {
238 qemu_irq_raise(s
->irq
);
240 qemu_irq_lower(s
->irq
);
244 static void serial_update_parameters(SerialState
*s
)
246 int speed
, parity
, data_bits
, stop_bits
, frame_size
;
247 QEMUSerialSetParams ssp
;
269 data_bits
= (s
->lcr
& 0x03) + 5;
270 frame_size
+= data_bits
+ stop_bits
;
271 speed
= s
->baudbase
/ s
->divider
;
274 ssp
.data_bits
= data_bits
;
275 ssp
.stop_bits
= stop_bits
;
276 s
->char_transmit_time
= (get_ticks_per_sec() / speed
) * frame_size
;
277 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
279 DPRINTF("speed=%d parity=%c data=%d stop=%d\n",
280 speed
, parity
, data_bits
, stop_bits
);
283 static void serial_update_msl(SerialState
*s
)
288 qemu_del_timer(s
->modem_status_poll
);
290 if (qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
) {
297 s
->msr
= (flags
& CHR_TIOCM_CTS
) ? s
->msr
| UART_MSR_CTS
: s
->msr
& ~UART_MSR_CTS
;
298 s
->msr
= (flags
& CHR_TIOCM_DSR
) ? s
->msr
| UART_MSR_DSR
: s
->msr
& ~UART_MSR_DSR
;
299 s
->msr
= (flags
& CHR_TIOCM_CAR
) ? s
->msr
| UART_MSR_DCD
: s
->msr
& ~UART_MSR_DCD
;
300 s
->msr
= (flags
& CHR_TIOCM_RI
) ? s
->msr
| UART_MSR_RI
: s
->msr
& ~UART_MSR_RI
;
302 if (s
->msr
!= omsr
) {
304 s
->msr
= s
->msr
| ((s
->msr
>> 4) ^ (omsr
>> 4));
305 /* UART_MSR_TERI only if change was from 1 -> 0 */
306 if ((s
->msr
& UART_MSR_TERI
) && !(omsr
& UART_MSR_RI
))
307 s
->msr
&= ~UART_MSR_TERI
;
308 serial_update_irq(s
);
311 /* The real 16550A apparently has a 250ns response latency to line status changes.
312 We'll be lazy and poll only every 10ms, and only poll it at all if MSI interrupts are turned on */
315 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock_ns(vm_clock
) + get_ticks_per_sec() / 100);
318 static void serial_xmit(void *opaque
)
320 SerialState
*s
= opaque
;
321 uint64_t new_xmit_ts
= qemu_get_clock_ns(vm_clock
);
323 if (s
->tsr_retry
<= 0) {
324 if (s
->fcr
& UART_FCR_FE
) {
325 s
->tsr
= fifo_get(s
,XMIT_FIFO
);
326 if (!s
->xmit_fifo
.count
)
327 s
->lsr
|= UART_LSR_THRE
;
330 s
->lsr
|= UART_LSR_THRE
;
334 if (s
->mcr
& UART_MCR_LOOP
) {
335 /* in loopback mode, say that we just received a char */
336 serial_receive1(s
, &s
->tsr
, 1);
337 } else if (qemu_chr_write(s
->chr
, &s
->tsr
, 1) != 1) {
338 if ((s
->tsr_retry
> 0) && (s
->tsr_retry
<= MAX_XMIT_RETRY
)) {
340 qemu_mod_timer(s
->transmit_timer
, new_xmit_ts
+ s
->char_transmit_time
);
342 } else if (s
->poll_msl
< 0) {
343 /* If we exceed MAX_XMIT_RETRY and the backend is not a real serial port, then
344 drop any further failed writes instantly, until we get one that goes through.
345 This is to prevent guests that log to unconnected pipes or pty's from stalling. */
353 s
->last_xmit_ts
= qemu_get_clock_ns(vm_clock
);
354 if (!(s
->lsr
& UART_LSR_THRE
))
355 qemu_mod_timer(s
->transmit_timer
, s
->last_xmit_ts
+ s
->char_transmit_time
);
357 if (s
->lsr
& UART_LSR_THRE
) {
358 s
->lsr
|= UART_LSR_TEMT
;
360 serial_update_irq(s
);
365 static void serial_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
367 SerialState
*s
= opaque
;
370 DPRINTF("write addr=0x%02x val=0x%02x\n", addr
, val
);
374 if (s
->lcr
& UART_LCR_DLAB
) {
375 s
->divider
= (s
->divider
& 0xff00) | val
;
376 serial_update_parameters(s
);
378 s
->thr
= (uint8_t) val
;
379 if(s
->fcr
& UART_FCR_FE
) {
380 fifo_put(s
, XMIT_FIFO
, s
->thr
);
382 s
->lsr
&= ~UART_LSR_TEMT
;
383 s
->lsr
&= ~UART_LSR_THRE
;
384 serial_update_irq(s
);
387 s
->lsr
&= ~UART_LSR_THRE
;
388 serial_update_irq(s
);
394 if (s
->lcr
& UART_LCR_DLAB
) {
395 s
->divider
= (s
->divider
& 0x00ff) | (val
<< 8);
396 serial_update_parameters(s
);
399 /* If the backend device is a real serial port, turn polling of the modem
400 status lines on physical port on or off depending on UART_IER_MSI state */
401 if (s
->poll_msl
>= 0) {
402 if (s
->ier
& UART_IER_MSI
) {
404 serial_update_msl(s
);
406 qemu_del_timer(s
->modem_status_poll
);
410 if (s
->lsr
& UART_LSR_THRE
) {
412 serial_update_irq(s
);
422 /* Did the enable/disable flag change? If so, make sure FIFOs get flushed */
423 if ((val
^ s
->fcr
) & UART_FCR_FE
)
424 val
|= UART_FCR_XFR
| UART_FCR_RFR
;
428 if (val
& UART_FCR_RFR
) {
429 qemu_del_timer(s
->fifo_timeout_timer
);
430 s
->timeout_ipending
=0;
431 fifo_clear(s
,RECV_FIFO
);
434 if (val
& UART_FCR_XFR
) {
435 fifo_clear(s
,XMIT_FIFO
);
438 if (val
& UART_FCR_FE
) {
439 s
->iir
|= UART_IIR_FE
;
440 /* Set RECV_FIFO trigger Level */
441 switch (val
& 0xC0) {
443 s
->recv_fifo
.itl
= 1;
446 s
->recv_fifo
.itl
= 4;
449 s
->recv_fifo
.itl
= 8;
452 s
->recv_fifo
.itl
= 14;
456 s
->iir
&= ~UART_IIR_FE
;
458 /* Set fcr - or at least the bits in it that are supposed to "stick" */
460 serial_update_irq(s
);
466 serial_update_parameters(s
);
467 break_enable
= (val
>> 6) & 1;
468 if (break_enable
!= s
->last_break_enable
) {
469 s
->last_break_enable
= break_enable
;
470 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_BREAK
,
478 int old_mcr
= s
->mcr
;
480 if (val
& UART_MCR_LOOP
)
483 if (s
->poll_msl
>= 0 && old_mcr
!= s
->mcr
) {
485 qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
487 flags
&= ~(CHR_TIOCM_RTS
| CHR_TIOCM_DTR
);
489 if (val
& UART_MCR_RTS
)
490 flags
|= CHR_TIOCM_RTS
;
491 if (val
& UART_MCR_DTR
)
492 flags
|= CHR_TIOCM_DTR
;
494 qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
495 /* Update the modem status after a one-character-send wait-time, since there may be a response
496 from the device/computer at the other end of the serial line */
497 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock_ns(vm_clock
) + s
->char_transmit_time
);
511 static uint32_t serial_ioport_read(void *opaque
, uint32_t addr
)
513 SerialState
*s
= opaque
;
520 if (s
->lcr
& UART_LCR_DLAB
) {
521 ret
= s
->divider
& 0xff;
523 if(s
->fcr
& UART_FCR_FE
) {
524 ret
= fifo_get(s
,RECV_FIFO
);
525 if (s
->recv_fifo
.count
== 0)
526 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
528 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock_ns (vm_clock
) + s
->char_transmit_time
* 4);
529 s
->timeout_ipending
= 0;
532 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
534 serial_update_irq(s
);
535 if (!(s
->mcr
& UART_MCR_LOOP
)) {
536 /* in loopback mode, don't receive any data */
537 qemu_chr_accept_input(s
->chr
);
542 if (s
->lcr
& UART_LCR_DLAB
) {
543 ret
= (s
->divider
>> 8) & 0xff;
550 if ((ret
& UART_IIR_ID
) == UART_IIR_THRI
) {
552 serial_update_irq(s
);
563 /* Clear break and overrun interrupts */
564 if (s
->lsr
& (UART_LSR_BI
|UART_LSR_OE
)) {
565 s
->lsr
&= ~(UART_LSR_BI
|UART_LSR_OE
);
566 serial_update_irq(s
);
570 if (s
->mcr
& UART_MCR_LOOP
) {
571 /* in loopback, the modem output pins are connected to the
573 ret
= (s
->mcr
& 0x0c) << 4;
574 ret
|= (s
->mcr
& 0x02) << 3;
575 ret
|= (s
->mcr
& 0x01) << 5;
577 if (s
->poll_msl
>= 0)
578 serial_update_msl(s
);
580 /* Clear delta bits & msr int after read, if they were set */
581 if (s
->msr
& UART_MSR_ANY_DELTA
) {
583 serial_update_irq(s
);
591 DPRINTF("read addr=0x%02x val=0x%02x\n", addr
, ret
);
595 static int serial_can_receive(SerialState
*s
)
597 if(s
->fcr
& UART_FCR_FE
) {
598 if(s
->recv_fifo
.count
< UART_FIFO_LENGTH
)
599 /* Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1 if above. If UART_FIFO_LENGTH - fifo.count is
600 advertised the effect will be to almost always fill the fifo completely before the guest has a chance to respond,
601 effectively overriding the ITL that the guest has set. */
602 return (s
->recv_fifo
.count
<= s
->recv_fifo
.itl
) ? s
->recv_fifo
.itl
- s
->recv_fifo
.count
: 1;
606 return !(s
->lsr
& UART_LSR_DR
);
610 static void serial_receive_break(SerialState
*s
)
613 /* When the LSR_DR is set a null byte is pushed into the fifo */
614 fifo_put(s
, RECV_FIFO
, '\0');
615 s
->lsr
|= UART_LSR_BI
| UART_LSR_DR
;
616 serial_update_irq(s
);
619 /* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
620 static void fifo_timeout_int (void *opaque
) {
621 SerialState
*s
= opaque
;
622 if (s
->recv_fifo
.count
) {
623 s
->timeout_ipending
= 1;
624 serial_update_irq(s
);
628 static int serial_can_receive1(void *opaque
)
630 SerialState
*s
= opaque
;
631 return serial_can_receive(s
);
634 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
636 SerialState
*s
= opaque
;
637 if(s
->fcr
& UART_FCR_FE
) {
639 for (i
= 0; i
< size
; i
++) {
640 fifo_put(s
, RECV_FIFO
, buf
[i
]);
642 s
->lsr
|= UART_LSR_DR
;
643 /* call the timeout receive callback in 4 char transmit time */
644 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock_ns (vm_clock
) + s
->char_transmit_time
* 4);
646 if (s
->lsr
& UART_LSR_DR
)
647 s
->lsr
|= UART_LSR_OE
;
649 s
->lsr
|= UART_LSR_DR
;
651 serial_update_irq(s
);
654 static void serial_event(void *opaque
, int event
)
656 SerialState
*s
= opaque
;
657 DPRINTF("event %x\n", event
);
658 if (event
== CHR_EVENT_BREAK
)
659 serial_receive_break(s
);
662 static void serial_pre_save(void *opaque
)
664 SerialState
*s
= opaque
;
665 s
->fcr_vmstate
= s
->fcr
;
668 static int serial_post_load(void *opaque
, int version_id
)
670 SerialState
*s
= opaque
;
672 if (version_id
< 3) {
675 /* Initialize fcr via setter to perform essential side-effects */
676 serial_ioport_write(s
, 0x02, s
->fcr_vmstate
);
677 serial_update_parameters(s
);
681 static const VMStateDescription vmstate_serial
= {
684 .minimum_version_id
= 2,
685 .pre_save
= serial_pre_save
,
686 .post_load
= serial_post_load
,
687 .fields
= (VMStateField
[]) {
688 VMSTATE_UINT16_V(divider
, SerialState
, 2),
689 VMSTATE_UINT8(rbr
, SerialState
),
690 VMSTATE_UINT8(ier
, SerialState
),
691 VMSTATE_UINT8(iir
, SerialState
),
692 VMSTATE_UINT8(lcr
, SerialState
),
693 VMSTATE_UINT8(mcr
, SerialState
),
694 VMSTATE_UINT8(lsr
, SerialState
),
695 VMSTATE_UINT8(msr
, SerialState
),
696 VMSTATE_UINT8(scr
, SerialState
),
697 VMSTATE_UINT8_V(fcr_vmstate
, SerialState
, 3),
698 VMSTATE_END_OF_LIST()
702 static void serial_reset(void *opaque
)
704 SerialState
*s
= opaque
;
708 s
->iir
= UART_IIR_NO_INT
;
710 s
->lsr
= UART_LSR_TEMT
| UART_LSR_THRE
;
711 s
->msr
= UART_MSR_DCD
| UART_MSR_DSR
| UART_MSR_CTS
;
712 /* Default to 9600 baud, 1 start bit, 8 data bits, 1 stop bit, no parity. */
714 s
->mcr
= UART_MCR_OUT2
;
717 s
->char_transmit_time
= (get_ticks_per_sec() / 9600) * 10;
720 fifo_clear(s
,RECV_FIFO
);
721 fifo_clear(s
,XMIT_FIFO
);
723 s
->last_xmit_ts
= qemu_get_clock_ns(vm_clock
);
726 s
->last_break_enable
= 0;
727 qemu_irq_lower(s
->irq
);
730 static void serial_init_core(SerialState
*s
)
733 fprintf(stderr
, "Can't create serial device, empty char device\n");
737 s
->modem_status_poll
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) serial_update_msl
, s
);
739 s
->fifo_timeout_timer
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) fifo_timeout_int
, s
);
740 s
->transmit_timer
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) serial_xmit
, s
);
742 qemu_register_reset(serial_reset
, s
);
744 qemu_chr_add_handlers(s
->chr
, serial_can_receive1
, serial_receive1
,
748 /* Change the main reference oscillator frequency. */
749 void serial_set_frequency(SerialState
*s
, uint32_t frequency
)
751 s
->baudbase
= frequency
;
752 serial_update_parameters(s
);
755 static const int isa_serial_io
[MAX_SERIAL_PORTS
] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
756 static const int isa_serial_irq
[MAX_SERIAL_PORTS
] = { 4, 3, 4, 3 };
758 static int serial_isa_initfn(ISADevice
*dev
)
761 ISASerialState
*isa
= DO_UPCAST(ISASerialState
, dev
, dev
);
762 SerialState
*s
= &isa
->state
;
764 if (isa
->index
== -1)
766 if (isa
->index
>= MAX_SERIAL_PORTS
)
768 if (isa
->iobase
== -1)
769 isa
->iobase
= isa_serial_io
[isa
->index
];
770 if (isa
->isairq
== -1)
771 isa
->isairq
= isa_serial_irq
[isa
->index
];
774 s
->baudbase
= 115200;
775 isa_init_irq(dev
, &s
->irq
, isa
->isairq
);
777 qdev_set_legacy_instance_id(&dev
->qdev
, isa
->iobase
, 3);
779 register_ioport_write(isa
->iobase
, 8, 1, serial_ioport_write
, s
);
780 register_ioport_read(isa
->iobase
, 8, 1, serial_ioport_read
, s
);
781 isa_init_ioport_range(dev
, isa
->iobase
, 8);
785 static const VMStateDescription vmstate_isa_serial
= {
788 .minimum_version_id
= 2,
789 .fields
= (VMStateField
[]) {
790 VMSTATE_STRUCT(state
, ISASerialState
, 0, vmstate_serial
, SerialState
),
791 VMSTATE_END_OF_LIST()
795 SerialState
*serial_init(int base
, qemu_irq irq
, int baudbase
,
796 CharDriverState
*chr
)
800 s
= qemu_mallocz(sizeof(SerialState
));
803 s
->baudbase
= baudbase
;
807 vmstate_register(NULL
, base
, &vmstate_serial
, s
);
809 register_ioport_write(base
, 8, 1, serial_ioport_write
, s
);
810 register_ioport_read(base
, 8, 1, serial_ioport_read
, s
);
814 /* Memory mapped interface */
815 static uint32_t serial_mm_readb(void *opaque
, target_phys_addr_t addr
)
817 SerialState
*s
= opaque
;
819 return serial_ioport_read(s
, addr
>> s
->it_shift
) & 0xFF;
822 static void serial_mm_writeb(void *opaque
, target_phys_addr_t addr
,
825 SerialState
*s
= opaque
;
827 serial_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFF);
830 static uint32_t serial_mm_readw_be(void *opaque
, target_phys_addr_t addr
)
832 SerialState
*s
= opaque
;
835 val
= serial_ioport_read(s
, addr
>> s
->it_shift
) & 0xFFFF;
840 static uint32_t serial_mm_readw_le(void *opaque
, target_phys_addr_t addr
)
842 SerialState
*s
= opaque
;
845 val
= serial_ioport_read(s
, addr
>> s
->it_shift
) & 0xFFFF;
849 static void serial_mm_writew_be(void *opaque
, target_phys_addr_t addr
,
852 SerialState
*s
= opaque
;
854 value
= bswap16(value
);
855 serial_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
858 static void serial_mm_writew_le(void *opaque
, target_phys_addr_t addr
,
861 SerialState
*s
= opaque
;
863 serial_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
866 static uint32_t serial_mm_readl_be(void *opaque
, target_phys_addr_t addr
)
868 SerialState
*s
= opaque
;
871 val
= serial_ioport_read(s
, addr
>> s
->it_shift
);
876 static uint32_t serial_mm_readl_le(void *opaque
, target_phys_addr_t addr
)
878 SerialState
*s
= opaque
;
881 val
= serial_ioport_read(s
, addr
>> s
->it_shift
);
885 static void serial_mm_writel_be(void *opaque
, target_phys_addr_t addr
,
888 SerialState
*s
= opaque
;
890 value
= bswap32(value
);
891 serial_ioport_write(s
, addr
>> s
->it_shift
, value
);
894 static void serial_mm_writel_le(void *opaque
, target_phys_addr_t addr
,
897 SerialState
*s
= opaque
;
899 serial_ioport_write(s
, addr
>> s
->it_shift
, value
);
902 static CPUReadMemoryFunc
* const serial_mm_read_be
[] = {
908 static CPUWriteMemoryFunc
* const serial_mm_write_be
[] = {
910 &serial_mm_writew_be
,
911 &serial_mm_writel_be
,
914 static CPUReadMemoryFunc
* const serial_mm_read_le
[] = {
920 static CPUWriteMemoryFunc
* const serial_mm_write_le
[] = {
922 &serial_mm_writew_le
,
923 &serial_mm_writel_le
,
926 SerialState
*serial_mm_init (target_phys_addr_t base
, int it_shift
,
927 qemu_irq irq
, int baudbase
,
928 CharDriverState
*chr
, int ioregister
,
934 s
= qemu_mallocz(sizeof(SerialState
));
936 s
->it_shift
= it_shift
;
938 s
->baudbase
= baudbase
;
942 vmstate_register(NULL
, base
, &vmstate_serial
, s
);
946 s_io_memory
= cpu_register_io_memory(serial_mm_read_be
,
947 serial_mm_write_be
, s
,
948 DEVICE_NATIVE_ENDIAN
);
950 s_io_memory
= cpu_register_io_memory(serial_mm_read_le
,
951 serial_mm_write_le
, s
,
952 DEVICE_NATIVE_ENDIAN
);
954 cpu_register_physical_memory(base
, 8 << it_shift
, s_io_memory
);
956 serial_update_msl(s
);
960 static ISADeviceInfo serial_isa_info
= {
961 .qdev
.name
= "isa-serial",
962 .qdev
.size
= sizeof(ISASerialState
),
963 .qdev
.vmsd
= &vmstate_isa_serial
,
964 .init
= serial_isa_initfn
,
965 .qdev
.props
= (Property
[]) {
966 DEFINE_PROP_UINT32("index", ISASerialState
, index
, -1),
967 DEFINE_PROP_HEX32("iobase", ISASerialState
, iobase
, -1),
968 DEFINE_PROP_UINT32("irq", ISASerialState
, isairq
, -1),
969 DEFINE_PROP_CHR("chardev", ISASerialState
, state
.chr
),
970 DEFINE_PROP_END_OF_LIST(),
974 static void serial_register_devices(void)
976 isa_qdev_register(&serial_isa_info
);
979 device_init(serial_register_devices
)