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
;
144 uint64_t last_xmit_ts
; /* Time when the last byte was successfully sent out of the tsr */
145 SerialFIFO recv_fifo
;
146 SerialFIFO xmit_fifo
;
148 struct QEMUTimer
*fifo_timeout_timer
;
149 int timeout_ipending
; /* timeout interrupt pending state */
150 struct QEMUTimer
*transmit_timer
;
153 uint64_t char_transmit_time
; /* time to transmit a char in ticks*/
156 struct QEMUTimer
*modem_status_poll
;
160 typedef struct ISASerialState
{
168 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
);
170 static void fifo_clear(SerialState
*s
, int fifo
)
172 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
173 memset(f
->data
, 0, UART_FIFO_LENGTH
);
179 static int fifo_put(SerialState
*s
, int fifo
, uint8_t chr
)
181 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
183 /* Receive overruns do not overwrite FIFO contents. */
184 if (fifo
== XMIT_FIFO
|| f
->count
< UART_FIFO_LENGTH
) {
186 f
->data
[f
->head
++] = chr
;
188 if (f
->head
== UART_FIFO_LENGTH
)
192 if (f
->count
< UART_FIFO_LENGTH
)
194 else if (fifo
== RECV_FIFO
)
195 s
->lsr
|= UART_LSR_OE
;
200 static uint8_t fifo_get(SerialState
*s
, int fifo
)
202 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
208 c
= f
->data
[f
->tail
++];
209 if (f
->tail
== UART_FIFO_LENGTH
)
216 static void serial_update_irq(SerialState
*s
)
218 uint8_t tmp_iir
= UART_IIR_NO_INT
;
220 if ((s
->ier
& UART_IER_RLSI
) && (s
->lsr
& UART_LSR_INT_ANY
)) {
221 tmp_iir
= UART_IIR_RLSI
;
222 } else if ((s
->ier
& UART_IER_RDI
) && s
->timeout_ipending
) {
223 /* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
224 * this is not in the specification but is observed on existing
226 tmp_iir
= UART_IIR_CTI
;
227 } else if ((s
->ier
& UART_IER_RDI
) && (s
->lsr
& UART_LSR_DR
) &&
228 (!(s
->fcr
& UART_FCR_FE
) ||
229 s
->recv_fifo
.count
>= s
->recv_fifo
.itl
)) {
230 tmp_iir
= UART_IIR_RDI
;
231 } else if ((s
->ier
& UART_IER_THRI
) && s
->thr_ipending
) {
232 tmp_iir
= UART_IIR_THRI
;
233 } else if ((s
->ier
& UART_IER_MSI
) && (s
->msr
& UART_MSR_ANY_DELTA
)) {
234 tmp_iir
= UART_IIR_MSI
;
237 s
->iir
= tmp_iir
| (s
->iir
& 0xF0);
239 if (tmp_iir
!= UART_IIR_NO_INT
) {
240 qemu_irq_raise(s
->irq
);
242 qemu_irq_lower(s
->irq
);
246 static void serial_update_parameters(SerialState
*s
)
248 int speed
, parity
, data_bits
, stop_bits
, frame_size
;
249 QEMUSerialSetParams ssp
;
271 data_bits
= (s
->lcr
& 0x03) + 5;
272 frame_size
+= data_bits
+ stop_bits
;
273 speed
= s
->baudbase
/ s
->divider
;
276 ssp
.data_bits
= data_bits
;
277 ssp
.stop_bits
= stop_bits
;
278 s
->char_transmit_time
= (get_ticks_per_sec() / speed
) * frame_size
;
279 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
281 DPRINTF("speed=%d parity=%c data=%d stop=%d\n",
282 speed
, parity
, data_bits
, stop_bits
);
285 static void serial_update_msl(SerialState
*s
)
290 qemu_del_timer(s
->modem_status_poll
);
292 if (qemu_chr_fe_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
) {
299 s
->msr
= (flags
& CHR_TIOCM_CTS
) ? s
->msr
| UART_MSR_CTS
: s
->msr
& ~UART_MSR_CTS
;
300 s
->msr
= (flags
& CHR_TIOCM_DSR
) ? s
->msr
| UART_MSR_DSR
: s
->msr
& ~UART_MSR_DSR
;
301 s
->msr
= (flags
& CHR_TIOCM_CAR
) ? s
->msr
| UART_MSR_DCD
: s
->msr
& ~UART_MSR_DCD
;
302 s
->msr
= (flags
& CHR_TIOCM_RI
) ? s
->msr
| UART_MSR_RI
: s
->msr
& ~UART_MSR_RI
;
304 if (s
->msr
!= omsr
) {
306 s
->msr
= s
->msr
| ((s
->msr
>> 4) ^ (omsr
>> 4));
307 /* UART_MSR_TERI only if change was from 1 -> 0 */
308 if ((s
->msr
& UART_MSR_TERI
) && !(omsr
& UART_MSR_RI
))
309 s
->msr
&= ~UART_MSR_TERI
;
310 serial_update_irq(s
);
313 /* The real 16550A apparently has a 250ns response latency to line status changes.
314 We'll be lazy and poll only every 10ms, and only poll it at all if MSI interrupts are turned on */
317 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock_ns(vm_clock
) + get_ticks_per_sec() / 100);
320 static void serial_xmit(void *opaque
)
322 SerialState
*s
= opaque
;
323 uint64_t new_xmit_ts
= qemu_get_clock_ns(vm_clock
);
325 if (s
->tsr_retry
<= 0) {
326 if (s
->fcr
& UART_FCR_FE
) {
327 s
->tsr
= fifo_get(s
,XMIT_FIFO
);
328 if (!s
->xmit_fifo
.count
)
329 s
->lsr
|= UART_LSR_THRE
;
330 } else if ((s
->lsr
& UART_LSR_THRE
)) {
334 s
->lsr
|= UART_LSR_THRE
;
335 s
->lsr
&= ~UART_LSR_TEMT
;
339 if (s
->mcr
& UART_MCR_LOOP
) {
340 /* in loopback mode, say that we just received a char */
341 serial_receive1(s
, &s
->tsr
, 1);
342 } else if (qemu_chr_fe_write(s
->chr
, &s
->tsr
, 1) != 1) {
343 if ((s
->tsr_retry
>= 0) && (s
->tsr_retry
<= MAX_XMIT_RETRY
)) {
345 qemu_mod_timer(s
->transmit_timer
, new_xmit_ts
+ s
->char_transmit_time
);
347 } else if (s
->poll_msl
< 0) {
348 /* If we exceed MAX_XMIT_RETRY and the backend is not a real serial port, then
349 drop any further failed writes instantly, until we get one that goes through.
350 This is to prevent guests that log to unconnected pipes or pty's from stalling. */
358 s
->last_xmit_ts
= qemu_get_clock_ns(vm_clock
);
359 if (!(s
->lsr
& UART_LSR_THRE
))
360 qemu_mod_timer(s
->transmit_timer
, s
->last_xmit_ts
+ s
->char_transmit_time
);
362 if (s
->lsr
& UART_LSR_THRE
) {
363 s
->lsr
|= UART_LSR_TEMT
;
365 serial_update_irq(s
);
370 static void serial_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
372 SerialState
*s
= opaque
;
375 DPRINTF("write addr=0x%02x val=0x%02x\n", addr
, val
);
379 if (s
->lcr
& UART_LCR_DLAB
) {
380 s
->divider
= (s
->divider
& 0xff00) | val
;
381 serial_update_parameters(s
);
383 s
->thr
= (uint8_t) val
;
384 if(s
->fcr
& UART_FCR_FE
) {
385 fifo_put(s
, XMIT_FIFO
, s
->thr
);
387 s
->lsr
&= ~UART_LSR_TEMT
;
388 s
->lsr
&= ~UART_LSR_THRE
;
389 serial_update_irq(s
);
392 s
->lsr
&= ~UART_LSR_THRE
;
393 serial_update_irq(s
);
399 if (s
->lcr
& UART_LCR_DLAB
) {
400 s
->divider
= (s
->divider
& 0x00ff) | (val
<< 8);
401 serial_update_parameters(s
);
404 /* If the backend device is a real serial port, turn polling of the modem
405 status lines on physical port on or off depending on UART_IER_MSI state */
406 if (s
->poll_msl
>= 0) {
407 if (s
->ier
& UART_IER_MSI
) {
409 serial_update_msl(s
);
411 qemu_del_timer(s
->modem_status_poll
);
415 if (s
->lsr
& UART_LSR_THRE
) {
417 serial_update_irq(s
);
427 /* Did the enable/disable flag change? If so, make sure FIFOs get flushed */
428 if ((val
^ s
->fcr
) & UART_FCR_FE
)
429 val
|= UART_FCR_XFR
| UART_FCR_RFR
;
433 if (val
& UART_FCR_RFR
) {
434 qemu_del_timer(s
->fifo_timeout_timer
);
435 s
->timeout_ipending
=0;
436 fifo_clear(s
,RECV_FIFO
);
439 if (val
& UART_FCR_XFR
) {
440 fifo_clear(s
,XMIT_FIFO
);
443 if (val
& UART_FCR_FE
) {
444 s
->iir
|= UART_IIR_FE
;
445 /* Set RECV_FIFO trigger Level */
446 switch (val
& 0xC0) {
448 s
->recv_fifo
.itl
= 1;
451 s
->recv_fifo
.itl
= 4;
454 s
->recv_fifo
.itl
= 8;
457 s
->recv_fifo
.itl
= 14;
461 s
->iir
&= ~UART_IIR_FE
;
463 /* Set fcr - or at least the bits in it that are supposed to "stick" */
465 serial_update_irq(s
);
471 serial_update_parameters(s
);
472 break_enable
= (val
>> 6) & 1;
473 if (break_enable
!= s
->last_break_enable
) {
474 s
->last_break_enable
= break_enable
;
475 qemu_chr_fe_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_BREAK
,
483 int old_mcr
= s
->mcr
;
485 if (val
& UART_MCR_LOOP
)
488 if (s
->poll_msl
>= 0 && old_mcr
!= s
->mcr
) {
490 qemu_chr_fe_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
492 flags
&= ~(CHR_TIOCM_RTS
| CHR_TIOCM_DTR
);
494 if (val
& UART_MCR_RTS
)
495 flags
|= CHR_TIOCM_RTS
;
496 if (val
& UART_MCR_DTR
)
497 flags
|= CHR_TIOCM_DTR
;
499 qemu_chr_fe_ioctl(s
->chr
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
500 /* Update the modem status after a one-character-send wait-time, since there may be a response
501 from the device/computer at the other end of the serial line */
502 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock_ns(vm_clock
) + s
->char_transmit_time
);
516 static uint32_t serial_ioport_read(void *opaque
, uint32_t addr
)
518 SerialState
*s
= opaque
;
525 if (s
->lcr
& UART_LCR_DLAB
) {
526 ret
= s
->divider
& 0xff;
528 if(s
->fcr
& UART_FCR_FE
) {
529 ret
= fifo_get(s
,RECV_FIFO
);
530 if (s
->recv_fifo
.count
== 0)
531 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
533 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock_ns (vm_clock
) + s
->char_transmit_time
* 4);
534 s
->timeout_ipending
= 0;
537 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
539 serial_update_irq(s
);
540 if (!(s
->mcr
& UART_MCR_LOOP
)) {
541 /* in loopback mode, don't receive any data */
542 qemu_chr_accept_input(s
->chr
);
547 if (s
->lcr
& UART_LCR_DLAB
) {
548 ret
= (s
->divider
>> 8) & 0xff;
555 if ((ret
& UART_IIR_ID
) == UART_IIR_THRI
) {
557 serial_update_irq(s
);
568 /* Clear break and overrun interrupts */
569 if (s
->lsr
& (UART_LSR_BI
|UART_LSR_OE
)) {
570 s
->lsr
&= ~(UART_LSR_BI
|UART_LSR_OE
);
571 serial_update_irq(s
);
575 if (s
->mcr
& UART_MCR_LOOP
) {
576 /* in loopback, the modem output pins are connected to the
578 ret
= (s
->mcr
& 0x0c) << 4;
579 ret
|= (s
->mcr
& 0x02) << 3;
580 ret
|= (s
->mcr
& 0x01) << 5;
582 if (s
->poll_msl
>= 0)
583 serial_update_msl(s
);
585 /* Clear delta bits & msr int after read, if they were set */
586 if (s
->msr
& UART_MSR_ANY_DELTA
) {
588 serial_update_irq(s
);
596 DPRINTF("read addr=0x%02x val=0x%02x\n", addr
, ret
);
600 static int serial_can_receive(SerialState
*s
)
602 if(s
->fcr
& UART_FCR_FE
) {
603 if(s
->recv_fifo
.count
< UART_FIFO_LENGTH
)
604 /* Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1 if above. If UART_FIFO_LENGTH - fifo.count is
605 advertised the effect will be to almost always fill the fifo completely before the guest has a chance to respond,
606 effectively overriding the ITL that the guest has set. */
607 return (s
->recv_fifo
.count
<= s
->recv_fifo
.itl
) ? s
->recv_fifo
.itl
- s
->recv_fifo
.count
: 1;
611 return !(s
->lsr
& UART_LSR_DR
);
615 static void serial_receive_break(SerialState
*s
)
618 /* When the LSR_DR is set a null byte is pushed into the fifo */
619 fifo_put(s
, RECV_FIFO
, '\0');
620 s
->lsr
|= UART_LSR_BI
| UART_LSR_DR
;
621 serial_update_irq(s
);
624 /* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
625 static void fifo_timeout_int (void *opaque
) {
626 SerialState
*s
= opaque
;
627 if (s
->recv_fifo
.count
) {
628 s
->timeout_ipending
= 1;
629 serial_update_irq(s
);
633 static int serial_can_receive1(void *opaque
)
635 SerialState
*s
= opaque
;
636 return serial_can_receive(s
);
639 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
641 SerialState
*s
= opaque
;
644 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER
);
646 if(s
->fcr
& UART_FCR_FE
) {
648 for (i
= 0; i
< size
; i
++) {
649 fifo_put(s
, RECV_FIFO
, buf
[i
]);
651 s
->lsr
|= UART_LSR_DR
;
652 /* call the timeout receive callback in 4 char transmit time */
653 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock_ns (vm_clock
) + s
->char_transmit_time
* 4);
655 if (s
->lsr
& UART_LSR_DR
)
656 s
->lsr
|= UART_LSR_OE
;
658 s
->lsr
|= UART_LSR_DR
;
660 serial_update_irq(s
);
663 static void serial_event(void *opaque
, int event
)
665 SerialState
*s
= opaque
;
666 DPRINTF("event %x\n", event
);
667 if (event
== CHR_EVENT_BREAK
)
668 serial_receive_break(s
);
671 static void serial_pre_save(void *opaque
)
673 SerialState
*s
= opaque
;
674 s
->fcr_vmstate
= s
->fcr
;
677 static int serial_post_load(void *opaque
, int version_id
)
679 SerialState
*s
= opaque
;
681 if (version_id
< 3) {
684 /* Initialize fcr via setter to perform essential side-effects */
685 serial_ioport_write(s
, 0x02, s
->fcr_vmstate
);
686 serial_update_parameters(s
);
690 static const VMStateDescription vmstate_serial
= {
693 .minimum_version_id
= 2,
694 .pre_save
= serial_pre_save
,
695 .post_load
= serial_post_load
,
696 .fields
= (VMStateField
[]) {
697 VMSTATE_UINT16_V(divider
, SerialState
, 2),
698 VMSTATE_UINT8(rbr
, SerialState
),
699 VMSTATE_UINT8(ier
, SerialState
),
700 VMSTATE_UINT8(iir
, SerialState
),
701 VMSTATE_UINT8(lcr
, SerialState
),
702 VMSTATE_UINT8(mcr
, SerialState
),
703 VMSTATE_UINT8(lsr
, SerialState
),
704 VMSTATE_UINT8(msr
, SerialState
),
705 VMSTATE_UINT8(scr
, SerialState
),
706 VMSTATE_UINT8_V(fcr_vmstate
, SerialState
, 3),
707 VMSTATE_END_OF_LIST()
711 static void serial_reset(void *opaque
)
713 SerialState
*s
= opaque
;
717 s
->iir
= UART_IIR_NO_INT
;
719 s
->lsr
= UART_LSR_TEMT
| UART_LSR_THRE
;
720 s
->msr
= UART_MSR_DCD
| UART_MSR_DSR
| UART_MSR_CTS
;
721 /* Default to 9600 baud, 1 start bit, 8 data bits, 1 stop bit, no parity. */
723 s
->mcr
= UART_MCR_OUT2
;
726 s
->char_transmit_time
= (get_ticks_per_sec() / 9600) * 10;
729 fifo_clear(s
,RECV_FIFO
);
730 fifo_clear(s
,XMIT_FIFO
);
732 s
->last_xmit_ts
= qemu_get_clock_ns(vm_clock
);
735 s
->last_break_enable
= 0;
736 qemu_irq_lower(s
->irq
);
739 static void serial_init_core(SerialState
*s
)
742 fprintf(stderr
, "Can't create serial device, empty char device\n");
746 s
->modem_status_poll
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) serial_update_msl
, s
);
748 s
->fifo_timeout_timer
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) fifo_timeout_int
, s
);
749 s
->transmit_timer
= qemu_new_timer_ns(vm_clock
, (QEMUTimerCB
*) serial_xmit
, s
);
751 qemu_register_reset(serial_reset
, s
);
753 qemu_chr_add_handlers(s
->chr
, serial_can_receive1
, serial_receive1
,
757 /* Change the main reference oscillator frequency. */
758 void serial_set_frequency(SerialState
*s
, uint32_t frequency
)
760 s
->baudbase
= frequency
;
761 serial_update_parameters(s
);
764 static const int isa_serial_io
[MAX_SERIAL_PORTS
] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
765 static const int isa_serial_irq
[MAX_SERIAL_PORTS
] = { 4, 3, 4, 3 };
767 static const MemoryRegionPortio serial_portio
[] = {
768 { 0, 8, 1, .read
= serial_ioport_read
, .write
= serial_ioport_write
},
772 static const MemoryRegionOps serial_io_ops
= {
773 .old_portio
= serial_portio
776 static int serial_isa_initfn(ISADevice
*dev
)
779 ISASerialState
*isa
= DO_UPCAST(ISASerialState
, dev
, dev
);
780 SerialState
*s
= &isa
->state
;
782 if (isa
->index
== -1)
784 if (isa
->index
>= MAX_SERIAL_PORTS
)
786 if (isa
->iobase
== -1)
787 isa
->iobase
= isa_serial_io
[isa
->index
];
788 if (isa
->isairq
== -1)
789 isa
->isairq
= isa_serial_irq
[isa
->index
];
792 s
->baudbase
= 115200;
793 isa_init_irq(dev
, &s
->irq
, isa
->isairq
);
795 qdev_set_legacy_instance_id(&dev
->qdev
, isa
->iobase
, 3);
797 memory_region_init_io(&s
->io
, &serial_io_ops
, s
, "serial", 8);
798 isa_register_ioport(dev
, &s
->io
, isa
->iobase
);
802 static const VMStateDescription vmstate_isa_serial
= {
805 .minimum_version_id
= 2,
806 .fields
= (VMStateField
[]) {
807 VMSTATE_STRUCT(state
, ISASerialState
, 0, vmstate_serial
, SerialState
),
808 VMSTATE_END_OF_LIST()
812 SerialState
*serial_init(int base
, qemu_irq irq
, int baudbase
,
813 CharDriverState
*chr
)
817 s
= g_malloc0(sizeof(SerialState
));
820 s
->baudbase
= baudbase
;
824 vmstate_register(NULL
, base
, &vmstate_serial
, s
);
826 register_ioport_write(base
, 8, 1, serial_ioport_write
, s
);
827 register_ioport_read(base
, 8, 1, serial_ioport_read
, s
);
831 /* Memory mapped interface */
832 static uint64_t serial_mm_read(void *opaque
, target_phys_addr_t addr
,
835 SerialState
*s
= opaque
;
836 return serial_ioport_read(s
, addr
>> s
->it_shift
);
839 static void serial_mm_write(void *opaque
, target_phys_addr_t addr
,
840 uint64_t value
, unsigned size
)
842 SerialState
*s
= opaque
;
843 value
&= ~0u >> (32 - (size
* 8));
844 serial_ioport_write(s
, addr
>> s
->it_shift
, value
);
847 static const MemoryRegionOps serial_mm_ops
[3] = {
848 [DEVICE_NATIVE_ENDIAN
] = {
849 .read
= serial_mm_read
,
850 .write
= serial_mm_write
,
851 .endianness
= DEVICE_NATIVE_ENDIAN
,
853 [DEVICE_LITTLE_ENDIAN
] = {
854 .read
= serial_mm_read
,
855 .write
= serial_mm_write
,
856 .endianness
= DEVICE_LITTLE_ENDIAN
,
858 [DEVICE_BIG_ENDIAN
] = {
859 .read
= serial_mm_read
,
860 .write
= serial_mm_write
,
861 .endianness
= DEVICE_BIG_ENDIAN
,
865 SerialState
*serial_mm_init(MemoryRegion
*address_space
,
866 target_phys_addr_t base
, int it_shift
,
867 qemu_irq irq
, int baudbase
,
868 CharDriverState
*chr
, enum device_endian end
)
872 s
= g_malloc0(sizeof(SerialState
));
874 s
->it_shift
= it_shift
;
876 s
->baudbase
= baudbase
;
880 vmstate_register(NULL
, base
, &vmstate_serial
, s
);
882 memory_region_init_io(&s
->io
, &serial_mm_ops
[end
], s
,
883 "serial", 8 << it_shift
);
884 memory_region_add_subregion(address_space
, base
, &s
->io
);
886 serial_update_msl(s
);
890 static Property serial_isa_properties
[] = {
891 DEFINE_PROP_UINT32("index", ISASerialState
, index
, -1),
892 DEFINE_PROP_HEX32("iobase", ISASerialState
, iobase
, -1),
893 DEFINE_PROP_UINT32("irq", ISASerialState
, isairq
, -1),
894 DEFINE_PROP_CHR("chardev", ISASerialState
, state
.chr
),
895 DEFINE_PROP_UINT32("wakeup", ISASerialState
, state
.wakeup
, 0),
896 DEFINE_PROP_END_OF_LIST(),
899 static void serial_isa_class_initfn(ObjectClass
*klass
, void *data
)
901 DeviceClass
*dc
= DEVICE_CLASS(klass
);
902 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
903 ic
->init
= serial_isa_initfn
;
904 dc
->vmsd
= &vmstate_isa_serial
;
905 dc
->props
= serial_isa_properties
;
908 static TypeInfo serial_isa_info
= {
909 .name
= "isa-serial",
910 .parent
= TYPE_ISA_DEVICE
,
911 .instance_size
= sizeof(ISASerialState
),
912 .class_init
= serial_isa_class_initfn
,
915 static void serial_register_types(void)
917 type_register_static(&serial_isa_info
);
920 type_init(serial_register_types
)