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"
31 //#define DEBUG_SERIAL
33 #define UART_LCR_DLAB 0x80 /* Divisor latch access bit */
35 #define UART_IER_MSI 0x08 /* Enable Modem status interrupt */
36 #define UART_IER_RLSI 0x04 /* Enable receiver line status interrupt */
37 #define UART_IER_THRI 0x02 /* Enable Transmitter holding register int. */
38 #define UART_IER_RDI 0x01 /* Enable receiver data interrupt */
40 #define UART_IIR_NO_INT 0x01 /* No interrupts pending */
41 #define UART_IIR_ID 0x06 /* Mask for the interrupt ID */
43 #define UART_IIR_MSI 0x00 /* Modem status interrupt */
44 #define UART_IIR_THRI 0x02 /* Transmitter holding register empty */
45 #define UART_IIR_RDI 0x04 /* Receiver data interrupt */
46 #define UART_IIR_RLSI 0x06 /* Receiver line status interrupt */
47 #define UART_IIR_CTI 0x0C /* Character Timeout Indication */
49 #define UART_IIR_FENF 0x80 /* Fifo enabled, but not functionning */
50 #define UART_IIR_FE 0xC0 /* Fifo enabled */
53 * These are the definitions for the Modem Control Register
55 #define UART_MCR_LOOP 0x10 /* Enable loopback test mode */
56 #define UART_MCR_OUT2 0x08 /* Out2 complement */
57 #define UART_MCR_OUT1 0x04 /* Out1 complement */
58 #define UART_MCR_RTS 0x02 /* RTS complement */
59 #define UART_MCR_DTR 0x01 /* DTR complement */
62 * These are the definitions for the Modem Status Register
64 #define UART_MSR_DCD 0x80 /* Data Carrier Detect */
65 #define UART_MSR_RI 0x40 /* Ring Indicator */
66 #define UART_MSR_DSR 0x20 /* Data Set Ready */
67 #define UART_MSR_CTS 0x10 /* Clear to Send */
68 #define UART_MSR_DDCD 0x08 /* Delta DCD */
69 #define UART_MSR_TERI 0x04 /* Trailing edge ring indicator */
70 #define UART_MSR_DDSR 0x02 /* Delta DSR */
71 #define UART_MSR_DCTS 0x01 /* Delta CTS */
72 #define UART_MSR_ANY_DELTA 0x0F /* Any of the delta bits! */
74 #define UART_LSR_TEMT 0x40 /* Transmitter empty */
75 #define UART_LSR_THRE 0x20 /* Transmit-hold-register empty */
76 #define UART_LSR_BI 0x10 /* Break interrupt indicator */
77 #define UART_LSR_FE 0x08 /* Frame error indicator */
78 #define UART_LSR_PE 0x04 /* Parity error indicator */
79 #define UART_LSR_OE 0x02 /* Overrun error indicator */
80 #define UART_LSR_DR 0x01 /* Receiver data ready */
81 #define UART_LSR_INT_ANY 0x1E /* Any of the lsr-interrupt-triggering status bits */
83 /* Interrupt trigger levels. The byte-counts are for 16550A - in newer UARTs the byte-count for each ITL is higher. */
85 #define UART_FCR_ITL_1 0x00 /* 1 byte ITL */
86 #define UART_FCR_ITL_2 0x40 /* 4 bytes ITL */
87 #define UART_FCR_ITL_3 0x80 /* 8 bytes ITL */
88 #define UART_FCR_ITL_4 0xC0 /* 14 bytes ITL */
90 #define UART_FCR_DMS 0x08 /* DMA Mode Select */
91 #define UART_FCR_XFR 0x04 /* XMIT Fifo Reset */
92 #define UART_FCR_RFR 0x02 /* RCVR Fifo Reset */
93 #define UART_FCR_FE 0x01 /* FIFO Enable */
95 #define UART_FIFO_LENGTH 16 /* 16550A Fifo Length */
99 #define MAX_XMIT_RETRY 4
101 typedef struct SerialFIFO
{
102 uint8_t data
[UART_FIFO_LENGTH
];
104 uint8_t itl
; /* Interrupt Trigger Level */
111 uint8_t rbr
; /* receive register */
112 uint8_t thr
; /* transmit holding register */
113 uint8_t tsr
; /* transmit shift register */
115 uint8_t iir
; /* read only */
118 uint8_t lsr
; /* read only */
119 uint8_t msr
; /* read only */
122 uint8_t fcr_vmstate
; /* we can't write directly this value
123 it has side effects */
124 /* NOTE: this hidden state is necessary for tx irq generation as
125 it can be reset while reading iir */
128 CharDriverState
*chr
;
129 int last_break_enable
;
134 uint64_t last_xmit_ts
; /* Time when the last byte was successfully sent out of the tsr */
135 SerialFIFO recv_fifo
;
136 SerialFIFO xmit_fifo
;
138 struct QEMUTimer
*fifo_timeout_timer
;
139 int timeout_ipending
; /* timeout interrupt pending state */
140 struct QEMUTimer
*transmit_timer
;
143 uint64_t char_transmit_time
; /* time to transmit a char in ticks*/
146 struct QEMUTimer
*modem_status_poll
;
149 typedef struct ISASerialState
{
156 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
);
158 static void fifo_clear(SerialState
*s
, int fifo
)
160 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
161 memset(f
->data
, 0, UART_FIFO_LENGTH
);
167 static int fifo_put(SerialState
*s
, int fifo
, uint8_t chr
)
169 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
171 f
->data
[f
->head
++] = chr
;
173 if (f
->head
== UART_FIFO_LENGTH
)
180 static uint8_t fifo_get(SerialState
*s
, int fifo
)
182 SerialFIFO
*f
= (fifo
) ? &s
->recv_fifo
: &s
->xmit_fifo
;
188 c
= f
->data
[f
->tail
++];
189 if (f
->tail
== UART_FIFO_LENGTH
)
196 static void serial_update_irq(SerialState
*s
)
198 uint8_t tmp_iir
= UART_IIR_NO_INT
;
200 if ((s
->ier
& UART_IER_RLSI
) && (s
->lsr
& UART_LSR_INT_ANY
)) {
201 tmp_iir
= UART_IIR_RLSI
;
202 } else if ((s
->ier
& UART_IER_RDI
) && s
->timeout_ipending
) {
203 /* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
204 * this is not in the specification but is observed on existing
206 tmp_iir
= UART_IIR_CTI
;
207 } else if ((s
->ier
& UART_IER_RDI
) && (s
->lsr
& UART_LSR_DR
) &&
208 (!(s
->fcr
& UART_FCR_FE
) ||
209 s
->recv_fifo
.count
>= s
->recv_fifo
.itl
)) {
210 tmp_iir
= UART_IIR_RDI
;
211 } else if ((s
->ier
& UART_IER_THRI
) && s
->thr_ipending
) {
212 tmp_iir
= UART_IIR_THRI
;
213 } else if ((s
->ier
& UART_IER_MSI
) && (s
->msr
& UART_MSR_ANY_DELTA
)) {
214 tmp_iir
= UART_IIR_MSI
;
217 s
->iir
= tmp_iir
| (s
->iir
& 0xF0);
219 if (tmp_iir
!= UART_IIR_NO_INT
) {
220 qemu_irq_raise(s
->irq
);
222 qemu_irq_lower(s
->irq
);
226 static void serial_update_parameters(SerialState
*s
)
228 int speed
, parity
, data_bits
, stop_bits
, frame_size
;
229 QEMUSerialSetParams ssp
;
249 data_bits
= (s
->lcr
& 0x03) + 5;
250 frame_size
+= data_bits
+ stop_bits
;
251 speed
= s
->baudbase
/ s
->divider
;
254 ssp
.data_bits
= data_bits
;
255 ssp
.stop_bits
= stop_bits
;
256 s
->char_transmit_time
= (get_ticks_per_sec() / speed
) * frame_size
;
257 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
259 printf("speed=%d parity=%c data=%d stop=%d\n",
260 speed
, parity
, data_bits
, stop_bits
);
264 static void serial_update_msl(SerialState
*s
)
269 qemu_del_timer(s
->modem_status_poll
);
271 if (qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
) == -ENOTSUP
) {
278 s
->msr
= (flags
& CHR_TIOCM_CTS
) ? s
->msr
| UART_MSR_CTS
: s
->msr
& ~UART_MSR_CTS
;
279 s
->msr
= (flags
& CHR_TIOCM_DSR
) ? s
->msr
| UART_MSR_DSR
: s
->msr
& ~UART_MSR_DSR
;
280 s
->msr
= (flags
& CHR_TIOCM_CAR
) ? s
->msr
| UART_MSR_DCD
: s
->msr
& ~UART_MSR_DCD
;
281 s
->msr
= (flags
& CHR_TIOCM_RI
) ? s
->msr
| UART_MSR_RI
: s
->msr
& ~UART_MSR_RI
;
283 if (s
->msr
!= omsr
) {
285 s
->msr
= s
->msr
| ((s
->msr
>> 4) ^ (omsr
>> 4));
286 /* UART_MSR_TERI only if change was from 1 -> 0 */
287 if ((s
->msr
& UART_MSR_TERI
) && !(omsr
& UART_MSR_RI
))
288 s
->msr
&= ~UART_MSR_TERI
;
289 serial_update_irq(s
);
292 /* The real 16550A apparently has a 250ns response latency to line status changes.
293 We'll be lazy and poll only every 10ms, and only poll it at all if MSI interrupts are turned on */
296 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock(vm_clock
) + get_ticks_per_sec() / 100);
299 static void serial_xmit(void *opaque
)
301 SerialState
*s
= opaque
;
302 uint64_t new_xmit_ts
= qemu_get_clock(vm_clock
);
304 if (s
->tsr_retry
<= 0) {
305 if (s
->fcr
& UART_FCR_FE
) {
306 s
->tsr
= fifo_get(s
,XMIT_FIFO
);
307 if (!s
->xmit_fifo
.count
)
308 s
->lsr
|= UART_LSR_THRE
;
311 s
->lsr
|= UART_LSR_THRE
;
315 if (s
->mcr
& UART_MCR_LOOP
) {
316 /* in loopback mode, say that we just received a char */
317 serial_receive1(s
, &s
->tsr
, 1);
318 } else if (qemu_chr_write(s
->chr
, &s
->tsr
, 1) != 1) {
319 if ((s
->tsr_retry
> 0) && (s
->tsr_retry
<= MAX_XMIT_RETRY
)) {
321 qemu_mod_timer(s
->transmit_timer
, new_xmit_ts
+ s
->char_transmit_time
);
323 } else if (s
->poll_msl
< 0) {
324 /* If we exceed MAX_XMIT_RETRY and the backend is not a real serial port, then
325 drop any further failed writes instantly, until we get one that goes through.
326 This is to prevent guests that log to unconnected pipes or pty's from stalling. */
334 s
->last_xmit_ts
= qemu_get_clock(vm_clock
);
335 if (!(s
->lsr
& UART_LSR_THRE
))
336 qemu_mod_timer(s
->transmit_timer
, s
->last_xmit_ts
+ s
->char_transmit_time
);
338 if (s
->lsr
& UART_LSR_THRE
) {
339 s
->lsr
|= UART_LSR_TEMT
;
341 serial_update_irq(s
);
346 static void serial_ioport_write(void *opaque
, uint32_t addr
, uint32_t val
)
348 SerialState
*s
= opaque
;
352 printf("serial: write addr=0x%02x val=0x%02x\n", addr
, val
);
357 if (s
->lcr
& UART_LCR_DLAB
) {
358 s
->divider
= (s
->divider
& 0xff00) | val
;
359 serial_update_parameters(s
);
361 s
->thr
= (uint8_t) val
;
362 if(s
->fcr
& UART_FCR_FE
) {
363 fifo_put(s
, XMIT_FIFO
, s
->thr
);
365 s
->lsr
&= ~UART_LSR_TEMT
;
366 s
->lsr
&= ~UART_LSR_THRE
;
367 serial_update_irq(s
);
370 s
->lsr
&= ~UART_LSR_THRE
;
371 serial_update_irq(s
);
377 if (s
->lcr
& UART_LCR_DLAB
) {
378 s
->divider
= (s
->divider
& 0x00ff) | (val
<< 8);
379 serial_update_parameters(s
);
382 /* If the backend device is a real serial port, turn polling of the modem
383 status lines on physical port on or off depending on UART_IER_MSI state */
384 if (s
->poll_msl
>= 0) {
385 if (s
->ier
& UART_IER_MSI
) {
387 serial_update_msl(s
);
389 qemu_del_timer(s
->modem_status_poll
);
393 if (s
->lsr
& UART_LSR_THRE
) {
395 serial_update_irq(s
);
405 /* Did the enable/disable flag change? If so, make sure FIFOs get flushed */
406 if ((val
^ s
->fcr
) & UART_FCR_FE
)
407 val
|= UART_FCR_XFR
| UART_FCR_RFR
;
411 if (val
& UART_FCR_RFR
) {
412 qemu_del_timer(s
->fifo_timeout_timer
);
413 s
->timeout_ipending
=0;
414 fifo_clear(s
,RECV_FIFO
);
417 if (val
& UART_FCR_XFR
) {
418 fifo_clear(s
,XMIT_FIFO
);
421 if (val
& UART_FCR_FE
) {
422 s
->iir
|= UART_IIR_FE
;
423 /* Set RECV_FIFO trigger Level */
424 switch (val
& 0xC0) {
426 s
->recv_fifo
.itl
= 1;
429 s
->recv_fifo
.itl
= 4;
432 s
->recv_fifo
.itl
= 8;
435 s
->recv_fifo
.itl
= 14;
439 s
->iir
&= ~UART_IIR_FE
;
441 /* Set fcr - or at least the bits in it that are supposed to "stick" */
443 serial_update_irq(s
);
449 serial_update_parameters(s
);
450 break_enable
= (val
>> 6) & 1;
451 if (break_enable
!= s
->last_break_enable
) {
452 s
->last_break_enable
= break_enable
;
453 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_BREAK
,
461 int old_mcr
= s
->mcr
;
463 if (val
& UART_MCR_LOOP
)
466 if (s
->poll_msl
>= 0 && old_mcr
!= s
->mcr
) {
468 qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_GET_TIOCM
, &flags
);
470 flags
&= ~(CHR_TIOCM_RTS
| CHR_TIOCM_DTR
);
472 if (val
& UART_MCR_RTS
)
473 flags
|= CHR_TIOCM_RTS
;
474 if (val
& UART_MCR_DTR
)
475 flags
|= CHR_TIOCM_DTR
;
477 qemu_chr_ioctl(s
->chr
,CHR_IOCTL_SERIAL_SET_TIOCM
, &flags
);
478 /* Update the modem status after a one-character-send wait-time, since there may be a response
479 from the device/computer at the other end of the serial line */
480 qemu_mod_timer(s
->modem_status_poll
, qemu_get_clock(vm_clock
) + s
->char_transmit_time
);
494 static uint32_t serial_ioport_read(void *opaque
, uint32_t addr
)
496 SerialState
*s
= opaque
;
503 if (s
->lcr
& UART_LCR_DLAB
) {
504 ret
= s
->divider
& 0xff;
506 if(s
->fcr
& UART_FCR_FE
) {
507 ret
= fifo_get(s
,RECV_FIFO
);
508 if (s
->recv_fifo
.count
== 0)
509 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
511 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock (vm_clock
) + s
->char_transmit_time
* 4);
512 s
->timeout_ipending
= 0;
515 s
->lsr
&= ~(UART_LSR_DR
| UART_LSR_BI
);
517 serial_update_irq(s
);
518 if (!(s
->mcr
& UART_MCR_LOOP
)) {
519 /* in loopback mode, don't receive any data */
520 qemu_chr_accept_input(s
->chr
);
525 if (s
->lcr
& UART_LCR_DLAB
) {
526 ret
= (s
->divider
>> 8) & 0xff;
534 serial_update_irq(s
);
544 /* Clear break interrupt */
545 if (s
->lsr
& UART_LSR_BI
) {
546 s
->lsr
&= ~UART_LSR_BI
;
547 serial_update_irq(s
);
551 if (s
->mcr
& UART_MCR_LOOP
) {
552 /* in loopback, the modem output pins are connected to the
554 ret
= (s
->mcr
& 0x0c) << 4;
555 ret
|= (s
->mcr
& 0x02) << 3;
556 ret
|= (s
->mcr
& 0x01) << 5;
558 if (s
->poll_msl
>= 0)
559 serial_update_msl(s
);
561 /* Clear delta bits & msr int after read, if they were set */
562 if (s
->msr
& UART_MSR_ANY_DELTA
) {
564 serial_update_irq(s
);
573 printf("serial: read addr=0x%02x val=0x%02x\n", addr
, ret
);
578 static int serial_can_receive(SerialState
*s
)
580 if(s
->fcr
& UART_FCR_FE
) {
581 if(s
->recv_fifo
.count
< UART_FIFO_LENGTH
)
582 /* Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1 if above. If UART_FIFO_LENGTH - fifo.count is
583 advertised the effect will be to almost always fill the fifo completely before the guest has a chance to respond,
584 effectively overriding the ITL that the guest has set. */
585 return (s
->recv_fifo
.count
<= s
->recv_fifo
.itl
) ? s
->recv_fifo
.itl
- s
->recv_fifo
.count
: 1;
589 return !(s
->lsr
& UART_LSR_DR
);
593 static void serial_receive_break(SerialState
*s
)
596 /* When the LSR_DR is set a null byte is pushed into the fifo */
597 fifo_put(s
, RECV_FIFO
, '\0');
598 s
->lsr
|= UART_LSR_BI
| UART_LSR_DR
;
599 serial_update_irq(s
);
602 /* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
603 static void fifo_timeout_int (void *opaque
) {
604 SerialState
*s
= opaque
;
605 if (s
->recv_fifo
.count
) {
606 s
->timeout_ipending
= 1;
607 serial_update_irq(s
);
611 static int serial_can_receive1(void *opaque
)
613 SerialState
*s
= opaque
;
614 return serial_can_receive(s
);
617 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
619 SerialState
*s
= opaque
;
620 if(s
->fcr
& UART_FCR_FE
) {
622 for (i
= 0; i
< size
; i
++) {
623 fifo_put(s
, RECV_FIFO
, buf
[i
]);
625 s
->lsr
|= UART_LSR_DR
;
626 /* call the timeout receive callback in 4 char transmit time */
627 qemu_mod_timer(s
->fifo_timeout_timer
, qemu_get_clock (vm_clock
) + s
->char_transmit_time
* 4);
630 s
->lsr
|= UART_LSR_DR
;
632 serial_update_irq(s
);
635 static void serial_event(void *opaque
, int event
)
637 SerialState
*s
= opaque
;
639 printf("serial: event %x\n", event
);
641 if (event
== CHR_EVENT_BREAK
)
642 serial_receive_break(s
);
645 static void serial_pre_save(void *opaque
)
647 SerialState
*s
= opaque
;
648 s
->fcr_vmstate
= s
->fcr
;
651 static int serial_pre_load(void *opaque
)
653 SerialState
*s
= opaque
;
658 static int serial_post_load(void *opaque
, int version_id
)
660 SerialState
*s
= opaque
;
662 /* Initialize fcr via setter to perform essential side-effects */
663 serial_ioport_write(s
, 0x02, s
->fcr_vmstate
);
667 static const VMStateDescription vmstate_serial
= {
670 .minimum_version_id
= 2,
671 .pre_save
= serial_pre_save
,
672 .pre_load
= serial_pre_load
,
673 .post_load
= serial_post_load
,
674 .fields
= (VMStateField
[]) {
675 VMSTATE_UINT16_V(divider
, SerialState
, 2),
676 VMSTATE_UINT8(rbr
, SerialState
),
677 VMSTATE_UINT8(ier
, SerialState
),
678 VMSTATE_UINT8(iir
, SerialState
),
679 VMSTATE_UINT8(lcr
, SerialState
),
680 VMSTATE_UINT8(mcr
, SerialState
),
681 VMSTATE_UINT8(lsr
, SerialState
),
682 VMSTATE_UINT8(msr
, SerialState
),
683 VMSTATE_UINT8(scr
, SerialState
),
684 VMSTATE_UINT8_V(fcr_vmstate
, SerialState
, 3),
685 VMSTATE_END_OF_LIST()
689 static void serial_reset(void *opaque
)
691 SerialState
*s
= opaque
;
695 s
->iir
= UART_IIR_NO_INT
;
697 s
->lsr
= UART_LSR_TEMT
| UART_LSR_THRE
;
698 s
->msr
= UART_MSR_DCD
| UART_MSR_DSR
| UART_MSR_CTS
;
699 /* Default to 9600 baud, no parity, one stop bit */
701 s
->mcr
= UART_MCR_OUT2
;
704 s
->char_transmit_time
= (get_ticks_per_sec() / 9600) * 9;
707 fifo_clear(s
,RECV_FIFO
);
708 fifo_clear(s
,XMIT_FIFO
);
710 s
->last_xmit_ts
= qemu_get_clock(vm_clock
);
713 s
->last_break_enable
= 0;
714 qemu_irq_lower(s
->irq
);
717 static void serial_init_core(SerialState
*s
)
720 fprintf(stderr
, "Can't create serial device, empty char device\n");
724 s
->modem_status_poll
= qemu_new_timer(vm_clock
, (QEMUTimerCB
*) serial_update_msl
, s
);
726 s
->fifo_timeout_timer
= qemu_new_timer(vm_clock
, (QEMUTimerCB
*) fifo_timeout_int
, s
);
727 s
->transmit_timer
= qemu_new_timer(vm_clock
, (QEMUTimerCB
*) serial_xmit
, s
);
729 qemu_register_reset(serial_reset
, s
);
732 qemu_chr_add_handlers(s
->chr
, serial_can_receive1
, serial_receive1
,
736 static int serial_isa_initfn(ISADevice
*dev
)
738 ISASerialState
*isa
= DO_UPCAST(ISASerialState
, dev
, dev
);
739 SerialState
*s
= &isa
->state
;
741 s
->baudbase
= 115200;
742 isa_init_irq(dev
, &s
->irq
, isa
->isairq
);
744 vmstate_register(isa
->iobase
, &vmstate_serial
, s
);
746 register_ioport_write(isa
->iobase
, 8, 1, serial_ioport_write
, s
);
747 register_ioport_read(isa
->iobase
, 8, 1, serial_ioport_read
, s
);
751 static const int isa_serial_io
[MAX_SERIAL_PORTS
] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8 };
752 static const int isa_serial_irq
[MAX_SERIAL_PORTS
] = { 4, 3, 4, 3 };
754 SerialState
*serial_isa_init(int index
, CharDriverState
*chr
)
758 dev
= isa_create("isa-serial");
759 qdev_prop_set_uint32(&dev
->qdev
, "iobase", isa_serial_io
[index
]);
760 qdev_prop_set_uint32(&dev
->qdev
, "irq", isa_serial_irq
[index
]);
761 qdev_prop_set_chr(&dev
->qdev
, "chardev", chr
);
762 if (qdev_init(&dev
->qdev
) < 0)
764 return &DO_UPCAST(ISASerialState
, dev
, dev
)->state
;
767 SerialState
*serial_init(int base
, qemu_irq irq
, int baudbase
,
768 CharDriverState
*chr
)
772 s
= qemu_mallocz(sizeof(SerialState
));
775 s
->baudbase
= baudbase
;
779 vmstate_register(base
, &vmstate_serial
, s
);
781 register_ioport_write(base
, 8, 1, serial_ioport_write
, s
);
782 register_ioport_read(base
, 8, 1, serial_ioport_read
, s
);
786 /* Memory mapped interface */
787 static uint32_t serial_mm_readb(void *opaque
, target_phys_addr_t addr
)
789 SerialState
*s
= opaque
;
791 return serial_ioport_read(s
, addr
>> s
->it_shift
) & 0xFF;
794 static void serial_mm_writeb(void *opaque
, target_phys_addr_t addr
,
797 SerialState
*s
= opaque
;
799 serial_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFF);
802 static uint32_t serial_mm_readw(void *opaque
, target_phys_addr_t addr
)
804 SerialState
*s
= opaque
;
807 val
= serial_ioport_read(s
, addr
>> s
->it_shift
) & 0xFFFF;
808 #ifdef TARGET_WORDS_BIGENDIAN
814 static void serial_mm_writew(void *opaque
, target_phys_addr_t addr
,
817 SerialState
*s
= opaque
;
818 #ifdef TARGET_WORDS_BIGENDIAN
819 value
= bswap16(value
);
821 serial_ioport_write(s
, addr
>> s
->it_shift
, value
& 0xFFFF);
824 static uint32_t serial_mm_readl(void *opaque
, target_phys_addr_t addr
)
826 SerialState
*s
= opaque
;
829 val
= serial_ioport_read(s
, addr
>> s
->it_shift
);
830 #ifdef TARGET_WORDS_BIGENDIAN
836 static void serial_mm_writel(void *opaque
, target_phys_addr_t addr
,
839 SerialState
*s
= opaque
;
840 #ifdef TARGET_WORDS_BIGENDIAN
841 value
= bswap32(value
);
843 serial_ioport_write(s
, addr
>> s
->it_shift
, value
);
846 static CPUReadMemoryFunc
* const serial_mm_read
[] = {
852 static CPUWriteMemoryFunc
* const serial_mm_write
[] = {
858 SerialState
*serial_mm_init (target_phys_addr_t base
, int it_shift
,
859 qemu_irq irq
, int baudbase
,
860 CharDriverState
*chr
, int ioregister
)
865 s
= qemu_mallocz(sizeof(SerialState
));
867 s
->it_shift
= it_shift
;
869 s
->baudbase
= baudbase
;
873 vmstate_register(base
, &vmstate_serial
, s
);
876 s_io_memory
= cpu_register_io_memory(serial_mm_read
,
878 cpu_register_physical_memory(base
, 8 << it_shift
, s_io_memory
);
880 serial_update_msl(s
);
884 static ISADeviceInfo serial_isa_info
= {
885 .qdev
.name
= "isa-serial",
886 .qdev
.size
= sizeof(ISASerialState
),
887 .init
= serial_isa_initfn
,
888 .qdev
.props
= (Property
[]) {
889 DEFINE_PROP_HEX32("iobase", ISASerialState
, iobase
, 0x3f8),
890 DEFINE_PROP_UINT32("irq", ISASerialState
, isairq
, 4),
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
)