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
102 typedef struct SerialFIFO
{
103 uint8_t data
[UART_FIFO_LENGTH
];
105 uint8_t itl
; /* Interrupt Trigger Level */
112 uint8_t rbr
; /* receive register */
113 uint8_t thr
; /* transmit holding register */
114 uint8_t tsr
; /* transmit shift register */
116 uint8_t iir
; /* read only */
119 uint8_t lsr
; /* read only */
120 uint8_t msr
; /* read only */
123 uint8_t fcr_vmstate
; /* we can't write directly this value
124 it has side effects */
125 /* NOTE: this hidden state is necessary for tx irq generation as
126 it can be reset while reading iir */
129 CharDriverState
*chr
;
130 int last_break_enable
;
135 uint64_t last_xmit_ts
; /* Time when the last byte was successfully sent out of the tsr */
136 SerialFIFO recv_fifo
;
137 SerialFIFO xmit_fifo
;
139 struct QEMUTimer
*fifo_timeout_timer
;
140 int timeout_ipending
; /* timeout interrupt pending state */
141 struct QEMUTimer
*transmit_timer
;
144 uint64_t char_transmit_time
; /* time to transmit a char in ticks*/
147 struct QEMUTimer
*modem_status_poll
;
150 typedef struct ISASerialState
{
158 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
);
160 static void fifo_clear(SerialState
*s
, int fifo
)
162 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
163 memset(f
->data
, 0, UART_FIFO_LENGTH
);
169 static int fifo_put(SerialState
*s
, int fifo
, uint8_t chr
)
171 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
173 /* Receive overruns do not overwrite FIFO contents. */
174 if (fifo
== XMIT_FIFO
|| f
->count
< UART_FIFO_LENGTH
) {
176 f
->data
[f
->head
++] = chr
;
178 if (f
->head
== UART_FIFO_LENGTH
)
182 if (f
->count
< UART_FIFO_LENGTH
)
184 else if (fifo
== RECV_FIFO
)
185 s
->lsr
|= UART_LSR_OE
;
190 static uint8_t fifo_get(SerialState
*s
, int fifo
)
192 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
198 c
= f
->data
[f
->tail
++];
199 if (f
->tail
== UART_FIFO_LENGTH
)
206 static void serial_update_irq(SerialState
*s
)
208 uint8_t tmp_iir
= UART_IIR_NO_INT
;
210 if ((s
->ier
& UART_IER_RLSI
) && (s
->lsr
& UART_LSR_INT_ANY
)) {
211 tmp_iir
= UART_IIR_RLSI
;
212 } else if ((s
->ier
& UART_IER_RDI
) && s
->timeout_ipending
) {
213 /* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
214 * this is not in the specification but is observed on existing
216 tmp_iir
= UART_IIR_CTI
;
217 } else if ((s
->ier
& UART_IER_RDI
) && (s
->lsr
& UART_LSR_DR
) &&
218 (!(s
->fcr
& UART_FCR_FE
) ||
219 s
->recv_fifo
.count
>= s
->recv_fifo
.itl
)) {
220 tmp_iir
= UART_IIR_RDI
;
221 } else if ((s
->ier
& UART_IER_THRI
) && s
->thr_ipending
) {
222 tmp_iir
= UART_IIR_THRI
;
223 } else if ((s
->ier
& UART_IER_MSI
) && (s
->msr
& UART_MSR_ANY_DELTA
)) {
224 tmp_iir
= UART_IIR_MSI
;
227 s
->iir
= tmp_iir
| (s
->iir
& 0xF0);
229 if (tmp_iir
!= UART_IIR_NO_INT
) {
230 qemu_irq_raise(s
->irq
);
232 qemu_irq_lower(s
->irq
);
236 static void serial_update_parameters(SerialState
*s
)
238 int speed
, parity
, data_bits
, stop_bits
, frame_size
;
239 QEMUSerialSetParams ssp
;
261 data_bits
= (s
->lcr
& 0x03) + 5;
262 frame_size
+= data_bits
+ stop_bits
;
263 speed
= s
->baudbase
/ s
->divider
;
266 ssp
.data_bits
= data_bits
;
267 ssp
.stop_bits
= stop_bits
;
268 s
->char_transmit_time
= (get_ticks_per_sec() / speed
) * frame_size
;
269 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
271 printf("speed=%d parity=%c data=%d stop=%d\n",
272 speed
, parity
, data_bits
, stop_bits
);
276 static void serial_update_msl(SerialState
*s
)
281 qemu_del_timer(s
->modem_status_poll
);
283 if (qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
) {
290 s
->msr
= (flags
& CHR_TIOCM_CTS
) ? s
->msr
| UART_MSR_CTS
: s
->msr
& ~UART_MSR_CTS
;
291 s
->msr
= (flags
& CHR_TIOCM_DSR
) ? s
->msr
| UART_MSR_DSR
: s
->msr
& ~UART_MSR_DSR
;
292 s
->msr
= (flags
& CHR_TIOCM_CAR
) ? s
->msr
| UART_MSR_DCD
: s
->msr
& ~UART_MSR_DCD
;
293 s
->msr
= (flags
& CHR_TIOCM_RI
) ? s
->msr
| UART_MSR_RI
: s
->msr
& ~UART_MSR_RI
;
295 if (s
->msr
!= omsr
) {
297 s
->msr
= s
->msr
| ((s
->msr
>> 4) ^ (omsr
>> 4));
298 /* UART_MSR_TERI only if change was from 1 -> 0 */
299 if ((s
->msr
& UART_MSR_TERI
) && !(omsr
& UART_MSR_RI
))
300 s
->msr
&= ~UART_MSR_TERI
;
301 serial_update_irq(s
);
304 /* The real 16550A apparently has a 250ns response latency to line status changes.
305 We'll be lazy and poll only every 10ms, and only poll it at all if MSI interrupts are turned on */
308 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 100);
311 static void serial_xmit(void *opaque
)
313 SerialState
*s
= opaque
;
314 uint64_t new_xmit_ts
= qemu_get_clock(vm_clock
);
316 if (s
->tsr_retry
<= 0) {
317 if (s
->fcr
& UART_FCR_FE
) {
318 s
->tsr
= fifo_get(s
,XMIT_FIFO
);
319 if (!s
->xmit_fifo
.count
)
320 s
->lsr
|= UART_LSR_THRE
;
323 s
->lsr
|= UART_LSR_THRE
;
327 if (s
->mcr
& UART_MCR_LOOP
) {
328 /* in loopback mode, say that we just received a char */
329 serial_receive1(s
, &s
->tsr
, 1);
330 } else if (qemu_chr_write(s
->chr
, &s
->tsr
, 1) != 1) {
331 if ((s
->tsr_retry
> 0) && (s
->tsr_retry
<= MAX_XMIT_RETRY
)) {
333 qemu_mod_timer(s
->transmit_timer
, new_xmit_ts
+ s
->char_transmit_time
);
335 } else if (s
->poll_msl
< 0) {
336 /* If we exceed MAX_XMIT_RETRY and the backend is not a real serial port, then
337 drop any further failed writes instantly, until we get one that goes through.
338 This is to prevent guests that log to unconnected pipes or pty's from stalling. */
346 s
->last_xmit_ts
= qemu_get_clock(vm_clock
);
347 if (!(s
->lsr
& UART_LSR_THRE
))
348 qemu_mod_timer(s
->transmit_timer
, s
->last_xmit_ts
+ s
->char_transmit_time
);
350 if (s
->lsr
& UART_LSR_THRE
) {
351 s
->lsr
|= UART_LSR_TEMT
;
353 serial_update_irq(s
);
358 static void serial_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
360 SerialState
*s
= opaque
;
364 printf("serial: write addr=0x%02x val=0x%02x\n", addr
, val
);
369 if (s
->lcr
& UART_LCR_DLAB
) {
370 s
->divider
= (s
->divider
& 0xff00) | val
;
371 serial_update_parameters(s
);
373 s
->thr
= (uint8_t) val
;
374 if(s
->fcr
& UART_FCR_FE
) {
375 fifo_put(s
, XMIT_FIFO
, s
->thr
);
377 s
->lsr
&= ~UART_LSR_TEMT
;
378 s
->lsr
&= ~UART_LSR_THRE
;
379 serial_update_irq(s
);
382 s
->lsr
&= ~UART_LSR_THRE
;
383 serial_update_irq(s
);
389 if (s
->lcr
& UART_LCR_DLAB
) {
390 s
->divider
= (s
->divider
& 0x00ff) | (val
<< 8);
391 serial_update_parameters(s
);
394 /* If the backend device is a real serial port, turn polling of the modem
395 status lines on physical port on or off depending on UART_IER_MSI state */
396 if (s
->poll_msl
>= 0) {
397 if (s
->ier
& UART_IER_MSI
) {
399 serial_update_msl(s
);
401 qemu_del_timer(s
->modem_status_poll
);
405 if (s
->lsr
& UART_LSR_THRE
) {
407 serial_update_irq(s
);
417 /* Did the enable/disable flag change? If so, make sure FIFOs get flushed */
418 if ((val
^ s
->fcr
) & UART_FCR_FE
)
419 val
|= UART_FCR_XFR
| UART_FCR_RFR
;
423 if (val
& UART_FCR_RFR
) {
424 qemu_del_timer(s
->fifo_timeout_timer
);
425 s
->timeout_ipending
=0;
426 fifo_clear(s
,RECV_FIFO
);
429 if (val
& UART_FCR_XFR
) {
430 fifo_clear(s
,XMIT_FIFO
);
433 if (val
& UART_FCR_FE
) {
434 s
->iir
|= UART_IIR_FE
;
435 /* Set RECV_FIFO trigger Level */
436 switch (val
& 0xC0) {
438 s
->recv_fifo
.itl
= 1;
441 s
->recv_fifo
.itl
= 4;
444 s
->recv_fifo
.itl
= 8;
447 s
->recv_fifo
.itl
= 14;
451 s
->iir
&= ~UART_IIR_FE
;
453 /* Set fcr - or at least the bits in it that are supposed to "stick" */
455 serial_update_irq(s
);
461 serial_update_parameters(s
);
462 break_enable
= (val
>> 6) & 1;
463 if (break_enable
!= s
->last_break_enable
) {
464 s
->last_break_enable
= break_enable
;
465 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_BREAK
,
473 int old_mcr
= s
->mcr
;
475 if (val
& UART_MCR_LOOP
)
478 if (s
->poll_msl
>= 0 && old_mcr
!= s
->mcr
) {
480 qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
482 flags
&= ~(CHR_TIOCM_RTS
| CHR_TIOCM_DTR
);
484 if (val
& UART_MCR_RTS
)
485 flags
|= CHR_TIOCM_RTS
;
486 if (val
& UART_MCR_DTR
)
487 flags
|= CHR_TIOCM_DTR
;
489 qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
490 /* Update the modem status after a one-character-send wait-time, since there may be a response
491 from the device/computer at the other end of the serial line */
492 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock(vm_clock
) + s
->char_transmit_time
);
506 static uint32_t serial_ioport_read(void *opaque
, uint32_t addr
)
508 SerialState
*s
= opaque
;
515 if (s
->lcr
& UART_LCR_DLAB
) {
516 ret
= s
->divider
& 0xff;
518 if(s
->fcr
& UART_FCR_FE
) {
519 ret
= fifo_get(s
,RECV_FIFO
);
520 if (s
->recv_fifo
.count
== 0)
521 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
523 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock (vm_clock
) + s
->char_transmit_time
* 4);
524 s
->timeout_ipending
= 0;
527 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
529 serial_update_irq(s
);
530 if (!(s
->mcr
& UART_MCR_LOOP
)) {
531 /* in loopback mode, don't receive any data */
532 qemu_chr_accept_input(s
->chr
);
537 if (s
->lcr
& UART_LCR_DLAB
) {
538 ret
= (s
->divider
>> 8) & 0xff;
545 if ((ret
& UART_IIR_ID
) == UART_IIR_THRI
) {
547 serial_update_irq(s
);
558 /* Clear break and overrun interrupts */
559 if (s
->lsr
& (UART_LSR_BI
|UART_LSR_OE
)) {
560 s
->lsr
&= ~(UART_LSR_BI
|UART_LSR_OE
);
561 serial_update_irq(s
);
565 if (s
->mcr
& UART_MCR_LOOP
) {
566 /* in loopback, the modem output pins are connected to the
568 ret
= (s
->mcr
& 0x0c) << 4;
569 ret
|= (s
->mcr
& 0x02) << 3;
570 ret
|= (s
->mcr
& 0x01) << 5;
572 if (s
->poll_msl
>= 0)
573 serial_update_msl(s
);
575 /* Clear delta bits & msr int after read, if they were set */
576 if (s
->msr
& UART_MSR_ANY_DELTA
) {
578 serial_update_irq(s
);
587 printf("serial: read addr=0x%02x val=0x%02x\n", addr
, ret
);
592 static int serial_can_receive(SerialState
*s
)
594 if(s
->fcr
& UART_FCR_FE
) {
595 if(s
->recv_fifo
.count
< UART_FIFO_LENGTH
)
596 /* Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1 if above. If UART_FIFO_LENGTH - fifo.count is
597 advertised the effect will be to almost always fill the fifo completely before the guest has a chance to respond,
598 effectively overriding the ITL that the guest has set. */
599 return (s
->recv_fifo
.count
<= s
->recv_fifo
.itl
) ? s
->recv_fifo
.itl
- s
->recv_fifo
.count
: 1;
603 return !(s
->lsr
& UART_LSR_DR
);
607 static void serial_receive_break(SerialState
*s
)
610 /* When the LSR_DR is set a null byte is pushed into the fifo */
611 fifo_put(s
, RECV_FIFO
, '\0');
612 s
->lsr
|= UART_LSR_BI
| UART_LSR_DR
;
613 serial_update_irq(s
);
616 /* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
617 static void fifo_timeout_int (void *opaque
) {
618 SerialState
*s
= opaque
;
619 if (s
->recv_fifo
.count
) {
620 s
->timeout_ipending
= 1;
621 serial_update_irq(s
);
625 static int serial_can_receive1(void *opaque
)
627 SerialState
*s
= opaque
;
628 return serial_can_receive(s
);
631 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
633 SerialState
*s
= opaque
;
634 if(s
->fcr
& UART_FCR_FE
) {
636 for (i
= 0; i
< size
; i
++) {
637 fifo_put(s
, RECV_FIFO
, buf
[i
]);
639 s
->lsr
|= UART_LSR_DR
;
640 /* call the timeout receive callback in 4 char transmit time */
641 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock (vm_clock
) + s
->char_transmit_time
* 4);
643 if (s
->lsr
& UART_LSR_DR
)
644 s
->lsr
|= UART_LSR_OE
;
646 s
->lsr
|= UART_LSR_DR
;
648 serial_update_irq(s
);
651 static void serial_event(void *opaque
, int event
)
653 SerialState
*s
= opaque
;
655 printf("serial: event %x\n", event
);
657 if (event
== CHR_EVENT_BREAK
)
658 serial_receive_break(s
);
661 static void serial_pre_save(void *opaque
)
663 SerialState
*s
= opaque
;
664 s
->fcr_vmstate
= s
->fcr
;
667 static int serial_post_load(void *opaque
, int version_id
)
669 SerialState
*s
= opaque
;
671 if (version_id
< 3) {
674 /* Initialize fcr via setter to perform essential side-effects */
675 serial_ioport_write(s
, 0x02, s
->fcr_vmstate
);
679 static const VMStateDescription vmstate_serial
= {
682 .minimum_version_id
= 2,
683 .pre_save
= serial_pre_save
,
684 .post_load
= serial_post_load
,
685 .fields
= (VMStateField
[]) {
686 VMSTATE_UINT16_V(divider
, SerialState
, 2),
687 VMSTATE_UINT8(rbr
, SerialState
),
688 VMSTATE_UINT8(ier
, SerialState
),
689 VMSTATE_UINT8(iir
, SerialState
),
690 VMSTATE_UINT8(lcr
, SerialState
),
691 VMSTATE_UINT8(mcr
, SerialState
),
692 VMSTATE_UINT8(lsr
, SerialState
),
693 VMSTATE_UINT8(msr
, SerialState
),
694 VMSTATE_UINT8(scr
, SerialState
),
695 VMSTATE_UINT8_V(fcr_vmstate
, SerialState
, 3),
696 VMSTATE_END_OF_LIST()
700 static void serial_reset(void *opaque
)
702 SerialState
*s
= opaque
;
706 s
->iir
= UART_IIR_NO_INT
;
708 s
->lsr
= UART_LSR_TEMT
| UART_LSR_THRE
;
709 s
->msr
= UART_MSR_DCD
| UART_MSR_DSR
| UART_MSR_CTS
;
710 /* Default to 9600 baud, 1 start bit, 8 data bits, 1 stop bit, no parity. */
712 s
->mcr
= UART_MCR_OUT2
;
715 s
->char_transmit_time
= (get_ticks_per_sec() / 9600) * 10;
718 fifo_clear(s
,RECV_FIFO
);
719 fifo_clear(s
,XMIT_FIFO
);
721 s
->last_xmit_ts
= qemu_get_clock(vm_clock
);
724 s
->last_break_enable
= 0;
725 qemu_irq_lower(s
->irq
);
728 static void serial_init_core(SerialState
*s
)
731 fprintf(stderr
, "Can't create serial device, empty char device\n");
735 s
->modem_status_poll
= qemu_new_timer(vm_clock
, (QEMUTimerCB
*) serial_update_msl
, s
);
737 s
->fifo_timeout_timer
= qemu_new_timer(vm_clock
, (QEMUTimerCB
*) fifo_timeout_int
, s
);
738 s
->transmit_timer
= qemu_new_timer(vm_clock
, (QEMUTimerCB
*) serial_xmit
, s
);
740 qemu_register_reset(serial_reset
, s
);
742 qemu_chr_add_handlers(s
->chr
, serial_can_receive1
, serial_receive1
,
746 /* Change the main reference oscillator frequency. */
747 void serial_set_frequency(SerialState
*s
, uint32_t frequency
)
749 s
->baudbase
= frequency
;
750 serial_update_parameters(s
);
753 static const int isa_serial_io
[MAX_SERIAL_PORTS
] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
754 static const int isa_serial_irq
[MAX_SERIAL_PORTS
] = { 4, 3, 4, 3 };
756 static int serial_isa_initfn(ISADevice
*dev
)
759 ISASerialState
*isa
= DO_UPCAST(ISASerialState
, dev
, dev
);
760 SerialState
*s
= &isa
->state
;
762 if (isa
->index
== -1)
764 if (isa
->index
>= MAX_SERIAL_PORTS
)
766 if (isa
->iobase
== -1)
767 isa
->iobase
= isa_serial_io
[isa
->index
];
768 if (isa
->isairq
== -1)
769 isa
->isairq
= isa_serial_irq
[isa
->index
];
772 s
->baudbase
= 115200;
773 isa_init_irq(dev
, &s
->irq
, isa
->isairq
);
775 qdev_set_legacy_instance_id(&dev
->qdev
, isa
->iobase
, 3);
777 register_ioport_write(isa
->iobase
, 8, 1, serial_ioport_write
, s
);
778 register_ioport_read(isa
->iobase
, 8, 1, serial_ioport_read
, s
);
782 SerialState
*serial_isa_init(int index
, CharDriverState
*chr
)
786 dev
= isa_create("isa-serial");
787 qdev_prop_set_uint32(&dev
->qdev
, "index", index
);
788 qdev_prop_set_chr(&dev
->qdev
, "chardev", chr
);
789 if (qdev_init(&dev
->qdev
) < 0)
791 return &DO_UPCAST(ISASerialState
, dev
, dev
)->state
;
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
= qemu_mallocz(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 uint32_t serial_mm_readb(void *opaque
, target_phys_addr_t addr
)
826 SerialState
*s
= opaque
;
828 return serial_ioport_read(s
, addr
>> s
->it_shift
) & 0xFF;
831 static void serial_mm_writeb(void *opaque
, target_phys_addr_t addr
,
834 SerialState
*s
= opaque
;
836 serial_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFF);
839 static uint32_t serial_mm_readw_be(void *opaque
, target_phys_addr_t addr
)
841 SerialState
*s
= opaque
;
844 val
= serial_ioport_read(s
, addr
>> s
->it_shift
) & 0xFFFF;
849 static uint32_t serial_mm_readw_le(void *opaque
, target_phys_addr_t addr
)
851 SerialState
*s
= opaque
;
854 val
= serial_ioport_read(s
, addr
>> s
->it_shift
) & 0xFFFF;
858 static void serial_mm_writew_be(void *opaque
, target_phys_addr_t addr
,
861 SerialState
*s
= opaque
;
863 value
= bswap16(value
);
864 serial_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
867 static void serial_mm_writew_le(void *opaque
, target_phys_addr_t addr
,
870 SerialState
*s
= opaque
;
872 serial_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
875 static uint32_t serial_mm_readl_be(void *opaque
, target_phys_addr_t addr
)
877 SerialState
*s
= opaque
;
880 val
= serial_ioport_read(s
, addr
>> s
->it_shift
);
885 static uint32_t serial_mm_readl_le(void *opaque
, target_phys_addr_t addr
)
887 SerialState
*s
= opaque
;
890 val
= serial_ioport_read(s
, addr
>> s
->it_shift
);
894 static void serial_mm_writel_be(void *opaque
, target_phys_addr_t addr
,
897 SerialState
*s
= opaque
;
899 value
= bswap32(value
);
900 serial_ioport_write(s
, addr
>> s
->it_shift
, value
);
903 static void serial_mm_writel_le(void *opaque
, target_phys_addr_t addr
,
906 SerialState
*s
= opaque
;
908 serial_ioport_write(s
, addr
>> s
->it_shift
, value
);
911 static CPUReadMemoryFunc
* const serial_mm_read_be
[] = {
917 static CPUWriteMemoryFunc
* const serial_mm_write_be
[] = {
919 &serial_mm_writew_be
,
920 &serial_mm_writel_be
,
923 static CPUReadMemoryFunc
* const serial_mm_read_le
[] = {
929 static CPUWriteMemoryFunc
* const serial_mm_write_le
[] = {
931 &serial_mm_writew_le
,
932 &serial_mm_writel_le
,
935 SerialState
*serial_mm_init (target_phys_addr_t base
, int it_shift
,
936 qemu_irq irq
, int baudbase
,
937 CharDriverState
*chr
, int ioregister
,
943 s
= qemu_mallocz(sizeof(SerialState
));
945 s
->it_shift
= it_shift
;
947 s
->baudbase
= baudbase
;
951 vmstate_register(NULL
, base
, &vmstate_serial
, s
);
955 s_io_memory
= cpu_register_io_memory(serial_mm_read_be
,
956 serial_mm_write_be
, s
);
958 s_io_memory
= cpu_register_io_memory(serial_mm_read_le
,
959 serial_mm_write_le
, s
);
961 cpu_register_physical_memory(base
, 8 << it_shift
, s_io_memory
);
963 serial_update_msl(s
);
967 static ISADeviceInfo serial_isa_info
= {
968 .qdev
.name
= "isa-serial",
969 .qdev
.size
= sizeof(ISASerialState
),
970 .qdev
.vmsd
= &vmstate_isa_serial
,
971 .init
= serial_isa_initfn
,
972 .qdev
.props
= (Property
[]) {
973 DEFINE_PROP_UINT32("index", ISASerialState
, index
, -1),
974 DEFINE_PROP_HEX32("iobase", ISASerialState
, iobase
, -1),
975 DEFINE_PROP_UINT32("irq", ISASerialState
, isairq
, -1),
976 DEFINE_PROP_CHR("chardev", ISASerialState
, state
.chr
),
977 DEFINE_PROP_END_OF_LIST(),
981 static void serial_register_devices(void)
983 isa_qdev_register(&serial_isa_info
);
986 device_init(serial_register_devices
)