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
;
159 typedef struct ISASerialState
{
167 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
);
169 static void fifo_clear(SerialState
*s
, int fifo
)
171 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
172 memset(f
->data
, 0, UART_FIFO_LENGTH
);
178 static int fifo_put(SerialState
*s
, int fifo
, uint8_t chr
)
180 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
182 /* Receive overruns do not overwrite FIFO contents. */
183 if (fifo
== XMIT_FIFO
|| f
->count
< UART_FIFO_LENGTH
) {
185 f
->data
[f
->head
++] = chr
;
187 if (f
->head
== UART_FIFO_LENGTH
)
191 if (f
->count
< UART_FIFO_LENGTH
)
193 else if (fifo
== RECV_FIFO
)
194 s
->lsr
|= UART_LSR_OE
;
199 static uint8_t fifo_get(SerialState
*s
, int fifo
)
201 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
207 c
= f
->data
[f
->tail
++];
208 if (f
->tail
== UART_FIFO_LENGTH
)
215 static void serial_update_irq(SerialState
*s
)
217 uint8_t tmp_iir
= UART_IIR_NO_INT
;
219 if ((s
->ier
& UART_IER_RLSI
) && (s
->lsr
& UART_LSR_INT_ANY
)) {
220 tmp_iir
= UART_IIR_RLSI
;
221 } else if ((s
->ier
& UART_IER_RDI
) && s
->timeout_ipending
) {
222 /* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
223 * this is not in the specification but is observed on existing
225 tmp_iir
= UART_IIR_CTI
;
226 } else if ((s
->ier
& UART_IER_RDI
) && (s
->lsr
& UART_LSR_DR
) &&
227 (!(s
->fcr
& UART_FCR_FE
) ||
228 s
->recv_fifo
.count
>= s
->recv_fifo
.itl
)) {
229 tmp_iir
= UART_IIR_RDI
;
230 } else if ((s
->ier
& UART_IER_THRI
) && s
->thr_ipending
) {
231 tmp_iir
= UART_IIR_THRI
;
232 } else if ((s
->ier
& UART_IER_MSI
) && (s
->msr
& UART_MSR_ANY_DELTA
)) {
233 tmp_iir
= UART_IIR_MSI
;
236 s
->iir
= tmp_iir
| (s
->iir
& 0xF0);
238 if (tmp_iir
!= UART_IIR_NO_INT
) {
239 qemu_irq_raise(s
->irq
);
241 qemu_irq_lower(s
->irq
);
245 static void serial_update_parameters(SerialState
*s
)
247 int speed
, parity
, data_bits
, stop_bits
, frame_size
;
248 QEMUSerialSetParams ssp
;
270 data_bits
= (s
->lcr
& 0x03) + 5;
271 frame_size
+= data_bits
+ stop_bits
;
272 speed
= s
->baudbase
/ s
->divider
;
275 ssp
.data_bits
= data_bits
;
276 ssp
.stop_bits
= stop_bits
;
277 s
->char_transmit_time
= (get_ticks_per_sec() / speed
) * frame_size
;
278 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
280 DPRINTF("speed=%d parity=%c data=%d stop=%d\n",
281 speed
, parity
, data_bits
, stop_bits
);
284 static void serial_update_msl(SerialState
*s
)
289 qemu_del_timer(s
->modem_status_poll
);
291 if (qemu_chr_fe_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
) {
298 s
->msr
= (flags
& CHR_TIOCM_CTS
) ? s
->msr
| UART_MSR_CTS
: s
->msr
& ~UART_MSR_CTS
;
299 s
->msr
= (flags
& CHR_TIOCM_DSR
) ? s
->msr
| UART_MSR_DSR
: s
->msr
& ~UART_MSR_DSR
;
300 s
->msr
= (flags
& CHR_TIOCM_CAR
) ? s
->msr
| UART_MSR_DCD
: s
->msr
& ~UART_MSR_DCD
;
301 s
->msr
= (flags
& CHR_TIOCM_RI
) ? s
->msr
| UART_MSR_RI
: s
->msr
& ~UART_MSR_RI
;
303 if (s
->msr
!= omsr
) {
305 s
->msr
= s
->msr
| ((s
->msr
>> 4) ^ (omsr
>> 4));
306 /* UART_MSR_TERI only if change was from 1 -> 0 */
307 if ((s
->msr
& UART_MSR_TERI
) && !(omsr
& UART_MSR_RI
))
308 s
->msr
&= ~UART_MSR_TERI
;
309 serial_update_irq(s
);
312 /* The real 16550A apparently has a 250ns response latency to line status changes.
313 We'll be lazy and poll only every 10ms, and only poll it at all if MSI interrupts are turned on */
316 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock_ns(vm_clock
) + get_ticks_per_sec() / 100);
319 static void serial_xmit(void *opaque
)
321 SerialState
*s
= opaque
;
322 uint64_t new_xmit_ts
= qemu_get_clock_ns(vm_clock
);
324 if (s
->tsr_retry
<= 0) {
325 if (s
->fcr
& UART_FCR_FE
) {
326 s
->tsr
= fifo_get(s
,XMIT_FIFO
);
327 if (!s
->xmit_fifo
.count
)
328 s
->lsr
|= UART_LSR_THRE
;
331 s
->lsr
|= UART_LSR_THRE
;
335 if (s
->mcr
& UART_MCR_LOOP
) {
336 /* in loopback mode, say that we just received a char */
337 serial_receive1(s
, &s
->tsr
, 1);
338 } else if (qemu_chr_fe_write(s
->chr
, &s
->tsr
, 1) != 1) {
339 if ((s
->tsr_retry
> 0) && (s
->tsr_retry
<= MAX_XMIT_RETRY
)) {
341 qemu_mod_timer(s
->transmit_timer
, new_xmit_ts
+ s
->char_transmit_time
);
343 } else if (s
->poll_msl
< 0) {
344 /* If we exceed MAX_XMIT_RETRY and the backend is not a real serial port, then
345 drop any further failed writes instantly, until we get one that goes through.
346 This is to prevent guests that log to unconnected pipes or pty's from stalling. */
354 s
->last_xmit_ts
= qemu_get_clock_ns(vm_clock
);
355 if (!(s
->lsr
& UART_LSR_THRE
))
356 qemu_mod_timer(s
->transmit_timer
, s
->last_xmit_ts
+ s
->char_transmit_time
);
358 if (s
->lsr
& UART_LSR_THRE
) {
359 s
->lsr
|= UART_LSR_TEMT
;
361 serial_update_irq(s
);
366 static void serial_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
368 SerialState
*s
= opaque
;
371 DPRINTF("write addr=0x%02x val=0x%02x\n", addr
, val
);
375 if (s
->lcr
& UART_LCR_DLAB
) {
376 s
->divider
= (s
->divider
& 0xff00) | val
;
377 serial_update_parameters(s
);
379 s
->thr
= (uint8_t) val
;
380 if(s
->fcr
& UART_FCR_FE
) {
381 fifo_put(s
, XMIT_FIFO
, s
->thr
);
383 s
->lsr
&= ~UART_LSR_TEMT
;
384 s
->lsr
&= ~UART_LSR_THRE
;
385 serial_update_irq(s
);
388 s
->lsr
&= ~UART_LSR_THRE
;
389 serial_update_irq(s
);
395 if (s
->lcr
& UART_LCR_DLAB
) {
396 s
->divider
= (s
->divider
& 0x00ff) | (val
<< 8);
397 serial_update_parameters(s
);
400 /* If the backend device is a real serial port, turn polling of the modem
401 status lines on physical port on or off depending on UART_IER_MSI state */
402 if (s
->poll_msl
>= 0) {
403 if (s
->ier
& UART_IER_MSI
) {
405 serial_update_msl(s
);
407 qemu_del_timer(s
->modem_status_poll
);
411 if (s
->lsr
& UART_LSR_THRE
) {
413 serial_update_irq(s
);
423 /* Did the enable/disable flag change? If so, make sure FIFOs get flushed */
424 if ((val
^ s
->fcr
) & UART_FCR_FE
)
425 val
|= UART_FCR_XFR
| UART_FCR_RFR
;
429 if (val
& UART_FCR_RFR
) {
430 qemu_del_timer(s
->fifo_timeout_timer
);
431 s
->timeout_ipending
=0;
432 fifo_clear(s
,RECV_FIFO
);
435 if (val
& UART_FCR_XFR
) {
436 fifo_clear(s
,XMIT_FIFO
);
439 if (val
& UART_FCR_FE
) {
440 s
->iir
|= UART_IIR_FE
;
441 /* Set RECV_FIFO trigger Level */
442 switch (val
& 0xC0) {
444 s
->recv_fifo
.itl
= 1;
447 s
->recv_fifo
.itl
= 4;
450 s
->recv_fifo
.itl
= 8;
453 s
->recv_fifo
.itl
= 14;
457 s
->iir
&= ~UART_IIR_FE
;
459 /* Set fcr - or at least the bits in it that are supposed to "stick" */
461 serial_update_irq(s
);
467 serial_update_parameters(s
);
468 break_enable
= (val
>> 6) & 1;
469 if (break_enable
!= s
->last_break_enable
) {
470 s
->last_break_enable
= break_enable
;
471 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_BREAK
,
479 int old_mcr
= s
->mcr
;
481 if (val
& UART_MCR_LOOP
)
484 if (s
->poll_msl
>= 0 && old_mcr
!= s
->mcr
) {
486 qemu_chr_fe_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
488 flags
&= ~(CHR_TIOCM_RTS
| CHR_TIOCM_DTR
);
490 if (val
& UART_MCR_RTS
)
491 flags
|= CHR_TIOCM_RTS
;
492 if (val
& UART_MCR_DTR
)
493 flags
|= CHR_TIOCM_DTR
;
495 qemu_chr_fe_ioctl(s
->chr
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
496 /* Update the modem status after a one-character-send wait-time, since there may be a response
497 from the device/computer at the other end of the serial line */
498 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock_ns(vm_clock
) + s
->char_transmit_time
);
512 static uint32_t serial_ioport_read(void *opaque
, uint32_t addr
)
514 SerialState
*s
= opaque
;
521 if (s
->lcr
& UART_LCR_DLAB
) {
522 ret
= s
->divider
& 0xff;
524 if(s
->fcr
& UART_FCR_FE
) {
525 ret
= fifo_get(s
,RECV_FIFO
);
526 if (s
->recv_fifo
.count
== 0)
527 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
529 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock_ns (vm_clock
) + s
->char_transmit_time
* 4);
530 s
->timeout_ipending
= 0;
533 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
535 serial_update_irq(s
);
536 if (!(s
->mcr
& UART_MCR_LOOP
)) {
537 /* in loopback mode, don't receive any data */
538 qemu_chr_accept_input(s
->chr
);
543 if (s
->lcr
& UART_LCR_DLAB
) {
544 ret
= (s
->divider
>> 8) & 0xff;
551 if ((ret
& UART_IIR_ID
) == UART_IIR_THRI
) {
553 serial_update_irq(s
);
564 /* Clear break and overrun interrupts */
565 if (s
->lsr
& (UART_LSR_BI
|UART_LSR_OE
)) {
566 s
->lsr
&= ~(UART_LSR_BI
|UART_LSR_OE
);
567 serial_update_irq(s
);
571 if (s
->mcr
& UART_MCR_LOOP
) {
572 /* in loopback, the modem output pins are connected to the
574 ret
= (s
->mcr
& 0x0c) << 4;
575 ret
|= (s
->mcr
& 0x02) << 3;
576 ret
|= (s
->mcr
& 0x01) << 5;
578 if (s
->poll_msl
>= 0)
579 serial_update_msl(s
);
581 /* Clear delta bits & msr int after read, if they were set */
582 if (s
->msr
& UART_MSR_ANY_DELTA
) {
584 serial_update_irq(s
);
592 DPRINTF("read addr=0x%02x val=0x%02x\n", addr
, ret
);
596 static int serial_can_receive(SerialState
*s
)
598 if(s
->fcr
& UART_FCR_FE
) {
599 if(s
->recv_fifo
.count
< UART_FIFO_LENGTH
)
600 /* Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1 if above. If UART_FIFO_LENGTH - fifo.count is
601 advertised the effect will be to almost always fill the fifo completely before the guest has a chance to respond,
602 effectively overriding the ITL that the guest has set. */
603 return (s
->recv_fifo
.count
<= s
->recv_fifo
.itl
) ? s
->recv_fifo
.itl
- s
->recv_fifo
.count
: 1;
607 return !(s
->lsr
& UART_LSR_DR
);
611 static void serial_receive_break(SerialState
*s
)
614 /* When the LSR_DR is set a null byte is pushed into the fifo */
615 fifo_put(s
, RECV_FIFO
, '\0');
616 s
->lsr
|= UART_LSR_BI
| UART_LSR_DR
;
617 serial_update_irq(s
);
620 /* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
621 static void fifo_timeout_int (void *opaque
) {
622 SerialState
*s
= opaque
;
623 if (s
->recv_fifo
.count
) {
624 s
->timeout_ipending
= 1;
625 serial_update_irq(s
);
629 static int serial_can_receive1(void *opaque
)
631 SerialState
*s
= opaque
;
632 return serial_can_receive(s
);
635 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
637 SerialState
*s
= opaque
;
638 if(s
->fcr
& UART_FCR_FE
) {
640 for (i
= 0; i
< size
; i
++) {
641 fifo_put(s
, RECV_FIFO
, buf
[i
]);
643 s
->lsr
|= UART_LSR_DR
;
644 /* call the timeout receive callback in 4 char transmit time */
645 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock_ns (vm_clock
) + s
->char_transmit_time
* 4);
647 if (s
->lsr
& UART_LSR_DR
)
648 s
->lsr
|= UART_LSR_OE
;
650 s
->lsr
|= UART_LSR_DR
;
652 serial_update_irq(s
);
655 static void serial_event(void *opaque
, int event
)
657 SerialState
*s
= opaque
;
658 DPRINTF("event %x\n", event
);
659 if (event
== CHR_EVENT_BREAK
)
660 serial_receive_break(s
);
663 static void serial_pre_save(void *opaque
)
665 SerialState
*s
= opaque
;
666 s
->fcr_vmstate
= s
->fcr
;
669 static int serial_post_load(void *opaque
, int version_id
)
671 SerialState
*s
= opaque
;
673 if (version_id
< 3) {
676 /* Initialize fcr via setter to perform essential side-effects */
677 serial_ioport_write(s
, 0x02, s
->fcr_vmstate
);
678 serial_update_parameters(s
);
682 static const VMStateDescription vmstate_serial
= {
685 .minimum_version_id
= 2,
686 .pre_save
= serial_pre_save
,
687 .post_load
= serial_post_load
,
688 .fields
= (VMStateField
[]) {
689 VMSTATE_UINT16_V(divider
, SerialState
, 2),
690 VMSTATE_UINT8(rbr
, SerialState
),
691 VMSTATE_UINT8(ier
, SerialState
),
692 VMSTATE_UINT8(iir
, SerialState
),
693 VMSTATE_UINT8(lcr
, SerialState
),
694 VMSTATE_UINT8(mcr
, SerialState
),
695 VMSTATE_UINT8(lsr
, SerialState
),
696 VMSTATE_UINT8(msr
, SerialState
),
697 VMSTATE_UINT8(scr
, SerialState
),
698 VMSTATE_UINT8_V(fcr_vmstate
, SerialState
, 3),
699 VMSTATE_END_OF_LIST()
703 static void serial_reset(void *opaque
)
705 SerialState
*s
= opaque
;
709 s
->iir
= UART_IIR_NO_INT
;
711 s
->lsr
= UART_LSR_TEMT
| UART_LSR_THRE
;
712 s
->msr
= UART_MSR_DCD
| UART_MSR_DSR
| UART_MSR_CTS
;
713 /* Default to 9600 baud, 1 start bit, 8 data bits, 1 stop bit, no parity. */
715 s
->mcr
= UART_MCR_OUT2
;
718 s
->char_transmit_time
= (get_ticks_per_sec() / 9600) * 10;
721 fifo_clear(s
,RECV_FIFO
);
722 fifo_clear(s
,XMIT_FIFO
);
724 s
->last_xmit_ts
= qemu_get_clock_ns(vm_clock
);
727 s
->last_break_enable
= 0;
728 qemu_irq_lower(s
->irq
);
731 static void serial_init_core(SerialState
*s
)
734 fprintf(stderr
, "Can't create serial device, empty char device\n");
738 s
->modem_status_poll
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) serial_update_msl
, s
);
740 s
->fifo_timeout_timer
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) fifo_timeout_int
, s
);
741 s
->transmit_timer
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) serial_xmit
, s
);
743 qemu_register_reset(serial_reset
, s
);
745 qemu_chr_add_handlers(s
->chr
, serial_can_receive1
, serial_receive1
,
749 /* Change the main reference oscillator frequency. */
750 void serial_set_frequency(SerialState
*s
, uint32_t frequency
)
752 s
->baudbase
= frequency
;
753 serial_update_parameters(s
);
756 static const int isa_serial_io
[MAX_SERIAL_PORTS
] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
757 static const int isa_serial_irq
[MAX_SERIAL_PORTS
] = { 4, 3, 4, 3 };
759 static const MemoryRegionPortio serial_portio
[] = {
760 { 0, 8, 1, .read
= serial_ioport_read
, .write
= serial_ioport_write
},
764 static const MemoryRegionOps serial_io_ops
= {
765 .old_portio
= serial_portio
768 static int serial_isa_initfn(ISADevice
*dev
)
771 ISASerialState
*isa
= DO_UPCAST(ISASerialState
, dev
, dev
);
772 SerialState
*s
= &isa
->state
;
774 if (isa
->index
== -1)
776 if (isa
->index
>= MAX_SERIAL_PORTS
)
778 if (isa
->iobase
== -1)
779 isa
->iobase
= isa_serial_io
[isa
->index
];
780 if (isa
->isairq
== -1)
781 isa
->isairq
= isa_serial_irq
[isa
->index
];
784 s
->baudbase
= 115200;
785 isa_init_irq(dev
, &s
->irq
, isa
->isairq
);
787 qdev_set_legacy_instance_id(&dev
->qdev
, isa
->iobase
, 3);
789 memory_region_init_io(&s
->io
, &serial_io_ops
, s
, "serial", 8);
790 isa_register_ioport(dev
, &s
->io
, isa
->iobase
);
794 static const VMStateDescription vmstate_isa_serial
= {
797 .minimum_version_id
= 2,
798 .fields
= (VMStateField
[]) {
799 VMSTATE_STRUCT(state
, ISASerialState
, 0, vmstate_serial
, SerialState
),
800 VMSTATE_END_OF_LIST()
804 SerialState
*serial_init(int base
, qemu_irq irq
, int baudbase
,
805 CharDriverState
*chr
)
809 s
= g_malloc0(sizeof(SerialState
));
812 s
->baudbase
= baudbase
;
816 vmstate_register(NULL
, base
, &vmstate_serial
, s
);
818 register_ioport_write(base
, 8, 1, serial_ioport_write
, s
);
819 register_ioport_read(base
, 8, 1, serial_ioport_read
, s
);
823 /* Memory mapped interface */
824 static uint64_t serial_mm_read(void *opaque
, target_phys_addr_t addr
,
827 SerialState
*s
= opaque
;
828 return serial_ioport_read(s
, addr
>> s
->it_shift
);
831 static void serial_mm_write(void *opaque
, target_phys_addr_t addr
,
832 uint64_t value
, unsigned size
)
834 SerialState
*s
= opaque
;
835 value
&= ~0u >> (32 - (size
* 8));
836 serial_ioport_write(s
, addr
>> s
->it_shift
, value
);
839 static const MemoryRegionOps serial_mm_ops
[3] = {
840 [DEVICE_NATIVE_ENDIAN
] = {
841 .read
= serial_mm_read
,
842 .write
= serial_mm_write
,
843 .endianness
= DEVICE_NATIVE_ENDIAN
,
845 [DEVICE_LITTLE_ENDIAN
] = {
846 .read
= serial_mm_read
,
847 .write
= serial_mm_write
,
848 .endianness
= DEVICE_LITTLE_ENDIAN
,
850 [DEVICE_BIG_ENDIAN
] = {
851 .read
= serial_mm_read
,
852 .write
= serial_mm_write
,
853 .endianness
= DEVICE_BIG_ENDIAN
,
857 SerialState
*serial_mm_init(MemoryRegion
*address_space
,
858 target_phys_addr_t base
, int it_shift
,
859 qemu_irq irq
, int baudbase
,
860 CharDriverState
*chr
, enum device_endian end
)
864 s
= g_malloc0(sizeof(SerialState
));
866 s
->it_shift
= it_shift
;
868 s
->baudbase
= baudbase
;
872 vmstate_register(NULL
, base
, &vmstate_serial
, s
);
874 memory_region_init_io(&s
->io
, &serial_mm_ops
[end
], s
,
875 "serial", 8 << it_shift
);
876 memory_region_add_subregion(address_space
, base
, &s
->io
);
878 serial_update_msl(s
);
882 static ISADeviceInfo serial_isa_info
= {
883 .qdev
.name
= "isa-serial",
884 .qdev
.size
= sizeof(ISASerialState
),
885 .qdev
.vmsd
= &vmstate_isa_serial
,
886 .init
= serial_isa_initfn
,
887 .qdev
.props
= (Property
[]) {
888 DEFINE_PROP_UINT32("index", ISASerialState
, index
, -1),
889 DEFINE_PROP_HEX32("iobase", ISASerialState
, iobase
, -1),
890 DEFINE_PROP_UINT32("irq", ISASerialState
, isairq
, -1),
891 DEFINE_PROP_CHR("chardev", ISASerialState
, state
.chr
),
892 DEFINE_PROP_END_OF_LIST(),
896 static void serial_register_devices(void)
898 isa_qdev_register(&serial_isa_info
);
901 device_init(serial_register_devices
)