serial.c 24.7 KB
Newer Older
B
bellard 已提交
1
/*
2
 * QEMU 16550A UART emulation
3
 *
B
bellard 已提交
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 * Copyright (c) 2008 Citrix Systems, Inc.
6
 *
B
bellard 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
P
pbrook 已提交
25 26 27 28
#include "hw.h"
#include "qemu-char.h"
#include "isa.h"
#include "pc.h"
A
aurel32 已提交
29
#include "qemu-timer.h"
B
bellard 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

//#define DEBUG_SERIAL

#define UART_LCR_DLAB	0x80	/* Divisor latch access bit */

#define UART_IER_MSI	0x08	/* Enable Modem status interrupt */
#define UART_IER_RLSI	0x04	/* Enable receiver line status interrupt */
#define UART_IER_THRI	0x02	/* Enable Transmitter holding register int. */
#define UART_IER_RDI	0x01	/* Enable receiver data interrupt */

#define UART_IIR_NO_INT	0x01	/* No interrupts pending */
#define UART_IIR_ID	0x06	/* Mask for the interrupt ID */

#define UART_IIR_MSI	0x00	/* Modem status interrupt */
#define UART_IIR_THRI	0x02	/* Transmitter holding register empty */
#define UART_IIR_RDI	0x04	/* Receiver data interrupt */
#define UART_IIR_RLSI	0x06	/* Receiver line status interrupt */
47 48 49 50
#define UART_IIR_CTI    0x0C    /* Character Timeout Indication */

#define UART_IIR_FENF   0x80    /* Fifo enabled, but not functionning */
#define UART_IIR_FE     0xC0    /* Fifo enabled */
B
bellard 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

/*
 * These are the definitions for the Modem Control Register
 */
#define UART_MCR_LOOP	0x10	/* Enable loopback test mode */
#define UART_MCR_OUT2	0x08	/* Out2 complement */
#define UART_MCR_OUT1	0x04	/* Out1 complement */
#define UART_MCR_RTS	0x02	/* RTS complement */
#define UART_MCR_DTR	0x01	/* DTR complement */

/*
 * These are the definitions for the Modem Status Register
 */
#define UART_MSR_DCD	0x80	/* Data Carrier Detect */
#define UART_MSR_RI	0x40	/* Ring Indicator */
#define UART_MSR_DSR	0x20	/* Data Set Ready */
#define UART_MSR_CTS	0x10	/* Clear to Send */
#define UART_MSR_DDCD	0x08	/* Delta DCD */
#define UART_MSR_TERI	0x04	/* Trailing edge ring indicator */
#define UART_MSR_DDSR	0x02	/* Delta DSR */
#define UART_MSR_DCTS	0x01	/* Delta CTS */
#define UART_MSR_ANY_DELTA 0x0F	/* Any of the delta bits! */

#define UART_LSR_TEMT	0x40	/* Transmitter empty */
#define UART_LSR_THRE	0x20	/* Transmit-hold-register empty */
#define UART_LSR_BI	0x10	/* Break interrupt indicator */
#define UART_LSR_FE	0x08	/* Frame error indicator */
#define UART_LSR_PE	0x04	/* Parity error indicator */
#define UART_LSR_OE	0x02	/* Overrun error indicator */
#define UART_LSR_DR	0x01	/* Receiver data ready */
81
#define UART_LSR_INT_ANY 0x1E	/* Any of the lsr-interrupt-triggering status bits */
B
bellard 已提交
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
/* Interrupt trigger levels. The byte-counts are for 16550A - in newer UARTs the byte-count for each ITL is higher. */

#define UART_FCR_ITL_1      0x00 /* 1 byte ITL */
#define UART_FCR_ITL_2      0x40 /* 4 bytes ITL */
#define UART_FCR_ITL_3      0x80 /* 8 bytes ITL */
#define UART_FCR_ITL_4      0xC0 /* 14 bytes ITL */

#define UART_FCR_DMS        0x08    /* DMA Mode Select */
#define UART_FCR_XFR        0x04    /* XMIT Fifo Reset */
#define UART_FCR_RFR        0x02    /* RCVR Fifo Reset */
#define UART_FCR_FE         0x01    /* FIFO Enable */

#define UART_FIFO_LENGTH    16      /* 16550A Fifo Length */

#define XMIT_FIFO           0
#define RECV_FIFO           1
#define MAX_XMIT_RETRY      4

struct SerialFIFO {
    uint8_t data[UART_FIFO_LENGTH];
    uint8_t count;
    uint8_t itl;                        /* Interrupt Trigger Level */
    uint8_t tail;
    uint8_t head;
} typedef SerialFIFO;
A
aurel32 已提交
108

B
bellard 已提交
109
struct SerialState {
B
bellard 已提交
110
    uint16_t divider;
B
bellard 已提交
111
    uint8_t rbr; /* receive register */
112 113
    uint8_t thr; /* transmit holding register */
    uint8_t tsr; /* transmit shift register */
B
bellard 已提交
114 115 116 117 118
    uint8_t ier;
    uint8_t iir; /* read only */
    uint8_t lcr;
    uint8_t mcr;
    uint8_t lsr; /* read only */
B
bellard 已提交
119
    uint8_t msr; /* read only */
B
bellard 已提交
120
    uint8_t scr;
121
    uint8_t fcr;
B
bellard 已提交
122 123 124
    /* NOTE: this hidden state is necessary for tx irq generation as
       it can be reset while reading iir */
    int thr_ipending;
P
pbrook 已提交
125
    qemu_irq irq;
B
bellard 已提交
126
    CharDriverState *chr;
B
bellard 已提交
127
    int last_break_enable;
128
    int it_shift;
A
aurel32 已提交
129
    int baudbase;
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    int tsr_retry;

    uint64_t last_xmit_ts;              /* Time when the last byte was successfully sent out of the tsr */
    SerialFIFO recv_fifo;
    SerialFIFO xmit_fifo;

    struct QEMUTimer *fifo_timeout_timer;
    int timeout_ipending;                   /* timeout interrupt pending state */
    struct QEMUTimer *transmit_timer;


    uint64_t char_transmit_time;               /* time to transmit a char in ticks*/
    int poll_msl;

    struct QEMUTimer *modem_status_poll;
B
bellard 已提交
145
};
B
bellard 已提交
146

147
static void serial_receive1(void *opaque, const uint8_t *buf, int size);
148

149
static void fifo_clear(SerialState *s, int fifo)
B
bellard 已提交
150
{
151 152 153 154 155
    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
    memset(f->data, 0, UART_FIFO_LENGTH);
    f->count = 0;
    f->head = 0;
    f->tail = 0;
B
bellard 已提交
156 157
}

158
static int fifo_put(SerialState *s, int fifo, uint8_t chr)
A
aurel32 已提交
159
{
160
    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
A
aurel32 已提交
161

162
    f->data[f->head++] = chr;
A
aurel32 已提交
163

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
    if (f->head == UART_FIFO_LENGTH)
        f->head = 0;
    f->count++;

    return 1;
}

static uint8_t fifo_get(SerialState *s, int fifo)
{
    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
    uint8_t c;

    if(f->count == 0)
        return 0;

    c = f->data[f->tail++];
    if (f->tail == UART_FIFO_LENGTH)
        f->tail = 0;
    f->count--;

    return c;
}
A
aurel32 已提交
186

187 188 189 190 191 192
static void serial_update_irq(SerialState *s)
{
    uint8_t tmp_iir = UART_IIR_NO_INT;

    if ((s->ier & UART_IER_RLSI) && (s->lsr & UART_LSR_INT_ANY)) {
        tmp_iir = UART_IIR_RLSI;
193
    } else if ((s->ier & UART_IER_RDI) && s->timeout_ipending) {
194 195 196
        /* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
         * this is not in the specification but is observed on existing
         * hardware.  */
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        tmp_iir = UART_IIR_CTI;
    } else if ((s->ier & UART_IER_RDI) && (s->lsr & UART_LSR_DR)) {
        if (!(s->fcr & UART_FCR_FE)) {
           tmp_iir = UART_IIR_RDI;
        } else if (s->recv_fifo.count >= s->recv_fifo.itl) {
           tmp_iir = UART_IIR_RDI;
        }
    } else if ((s->ier & UART_IER_THRI) && s->thr_ipending) {
        tmp_iir = UART_IIR_THRI;
    } else if ((s->ier & UART_IER_MSI) && (s->msr & UART_MSR_ANY_DELTA)) {
        tmp_iir = UART_IIR_MSI;
    }

    s->iir = tmp_iir | (s->iir & 0xF0);

    if (tmp_iir != UART_IIR_NO_INT) {
        qemu_irq_raise(s->irq);
    } else {
        qemu_irq_lower(s->irq);
A
aurel32 已提交
216 217 218
    }
}

B
bellard 已提交
219 220
static void serial_update_parameters(SerialState *s)
{
221
    int speed, parity, data_bits, stop_bits, frame_size;
B
bellard 已提交
222
    QEMUSerialSetParams ssp;
B
bellard 已提交
223

224 225 226 227
    if (s->divider == 0)
        return;

    frame_size = 1;
B
bellard 已提交
228 229 230 231 232 233 234
    if (s->lcr & 0x08) {
        if (s->lcr & 0x10)
            parity = 'E';
        else
            parity = 'O';
    } else {
            parity = 'N';
235
            frame_size = 0;
B
bellard 已提交
236
    }
237
    if (s->lcr & 0x04)
B
bellard 已提交
238 239 240
        stop_bits = 2;
    else
        stop_bits = 1;
241

B
bellard 已提交
242
    data_bits = (s->lcr & 0x03) + 5;
243
    frame_size += data_bits + stop_bits;
A
aurel32 已提交
244
    speed = s->baudbase / s->divider;
B
bellard 已提交
245 246 247 248
    ssp.speed = speed;
    ssp.parity = parity;
    ssp.data_bits = data_bits;
    ssp.stop_bits = stop_bits;
249
    s->char_transmit_time =  (ticks_per_sec / speed) * frame_size;
B
bellard 已提交
250 251
    qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
#if 0
252
    printf("speed=%d parity=%c data=%d stop=%d\n",
B
bellard 已提交
253 254 255 256
           speed, parity, data_bits, stop_bits);
#endif
}

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
static void serial_update_msl(SerialState *s)
{
    uint8_t omsr;
    int flags;

    qemu_del_timer(s->modem_status_poll);

    if (qemu_chr_ioctl(s->chr,CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP) {
        s->poll_msl = -1;
        return;
    }

    omsr = s->msr;

    s->msr = (flags & CHR_TIOCM_CTS) ? s->msr | UART_MSR_CTS : s->msr & ~UART_MSR_CTS;
    s->msr = (flags & CHR_TIOCM_DSR) ? s->msr | UART_MSR_DSR : s->msr & ~UART_MSR_DSR;
    s->msr = (flags & CHR_TIOCM_CAR) ? s->msr | UART_MSR_DCD : s->msr & ~UART_MSR_DCD;
    s->msr = (flags & CHR_TIOCM_RI) ? s->msr | UART_MSR_RI : s->msr & ~UART_MSR_RI;

    if (s->msr != omsr) {
         /* Set delta bits */
         s->msr = s->msr | ((s->msr >> 4) ^ (omsr >> 4));
         /* UART_MSR_TERI only if change was from 1 -> 0 */
         if ((s->msr & UART_MSR_TERI) && !(omsr & UART_MSR_RI))
             s->msr &= ~UART_MSR_TERI;
         serial_update_irq(s);
    }

    /* The real 16550A apparently has a 250ns response latency to line status changes.
       We'll be lazy and poll only every 10ms, and only poll it at all if MSI interrupts are turned on */

    if (s->poll_msl)
        qemu_mod_timer(s->modem_status_poll, qemu_get_clock(vm_clock) + ticks_per_sec / 100);
}

static void serial_xmit(void *opaque)
{
    SerialState *s = opaque;
    uint64_t new_xmit_ts = qemu_get_clock(vm_clock);

    if (s->tsr_retry <= 0) {
        if (s->fcr & UART_FCR_FE) {
            s->tsr = fifo_get(s,XMIT_FIFO);
            if (!s->xmit_fifo.count)
                s->lsr |= UART_LSR_THRE;
        } else {
            s->tsr = s->thr;
            s->lsr |= UART_LSR_THRE;
        }
    }

    if (s->mcr & UART_MCR_LOOP) {
        /* in loopback mode, say that we just received a char */
        serial_receive1(s, &s->tsr, 1);
    } else if (qemu_chr_write(s->chr, &s->tsr, 1) != 1) {
        if ((s->tsr_retry > 0) && (s->tsr_retry <= MAX_XMIT_RETRY)) {
            s->tsr_retry++;
            qemu_mod_timer(s->transmit_timer,  new_xmit_ts + s->char_transmit_time);
            return;
        } else if (s->poll_msl < 0) {
            /* If we exceed MAX_XMIT_RETRY and the backend is not a real serial port, then
            drop any further failed writes instantly, until we get one that goes through.
            This is to prevent guests that log to unconnected pipes or pty's from stalling. */
            s->tsr_retry = -1;
        }
    }
    else {
        s->tsr_retry = 0;
    }

    s->last_xmit_ts = qemu_get_clock(vm_clock);
    if (!(s->lsr & UART_LSR_THRE))
        qemu_mod_timer(s->transmit_timer, s->last_xmit_ts + s->char_transmit_time);

    if (s->lsr & UART_LSR_THRE) {
        s->lsr |= UART_LSR_TEMT;
        s->thr_ipending = 1;
        serial_update_irq(s);
    }
}


B
bellard 已提交
339
static void serial_ioport_write(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
340
{
B
bellard 已提交
341
    SerialState *s = opaque;
342

B
bellard 已提交
343 344 345 346 347 348 349 350 351
    addr &= 7;
#ifdef DEBUG_SERIAL
    printf("serial: write addr=0x%02x val=0x%02x\n", addr, val);
#endif
    switch(addr) {
    default:
    case 0:
        if (s->lcr & UART_LCR_DLAB) {
            s->divider = (s->divider & 0xff00) | val;
B
bellard 已提交
352
            serial_update_parameters(s);
B
bellard 已提交
353
        } else {
354 355 356
            s->thr = (uint8_t) val;
            if(s->fcr & UART_FCR_FE) {
                  fifo_put(s, XMIT_FIFO, s->thr);
B
bellard 已提交
357
            s->thr_ipending = 0;
358
                  s->lsr &= ~UART_LSR_TEMT;
B
bellard 已提交
359
            s->lsr &= ~UART_LSR_THRE;
B
bellard 已提交
360
            serial_update_irq(s);
A
aurel32 已提交
361
            } else {
362 363 364
                  s->thr_ipending = 0;
                  s->lsr &= ~UART_LSR_THRE;
                  serial_update_irq(s);
A
aurel32 已提交
365
            }
366
            serial_xmit(s);
B
bellard 已提交
367 368 369 370 371
        }
        break;
    case 1:
        if (s->lcr & UART_LCR_DLAB) {
            s->divider = (s->divider & 0x00ff) | (val << 8);
B
bellard 已提交
372
            serial_update_parameters(s);
B
bellard 已提交
373
        } else {
B
bellard 已提交
374
            s->ier = val & 0x0f;
375 376 377 378 379 380 381 382 383 384 385
            /* If the backend device is a real serial port, turn polling of the modem
               status lines on physical port on or off depending on UART_IER_MSI state */
            if (s->poll_msl >= 0) {
                if (s->ier & UART_IER_MSI) {
                     s->poll_msl = 1;
                     serial_update_msl(s);
                } else {
                     qemu_del_timer(s->modem_status_poll);
                     s->poll_msl = 0;
                }
            }
B
bellard 已提交
386 387
            if (s->lsr & UART_LSR_THRE) {
                s->thr_ipending = 1;
388
                serial_update_irq(s);
B
bellard 已提交
389
            }
B
bellard 已提交
390 391 392
        }
        break;
    case 2:
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
        val = val & 0xFF;

        if (s->fcr == val)
            break;

        /* Did the enable/disable flag change? If so, make sure FIFOs get flushed */
        if ((val ^ s->fcr) & UART_FCR_FE)
            val |= UART_FCR_XFR | UART_FCR_RFR;

        /* FIFO clear */

        if (val & UART_FCR_RFR) {
            qemu_del_timer(s->fifo_timeout_timer);
            s->timeout_ipending=0;
            fifo_clear(s,RECV_FIFO);
        }

        if (val & UART_FCR_XFR) {
            fifo_clear(s,XMIT_FIFO);
        }

        if (val & UART_FCR_FE) {
            s->iir |= UART_IIR_FE;
            /* Set RECV_FIFO trigger Level */
            switch (val & 0xC0) {
            case UART_FCR_ITL_1:
                s->recv_fifo.itl = 1;
                break;
            case UART_FCR_ITL_2:
                s->recv_fifo.itl = 4;
                break;
            case UART_FCR_ITL_3:
                s->recv_fifo.itl = 8;
                break;
            case UART_FCR_ITL_4:
                s->recv_fifo.itl = 14;
                break;
            }
        } else
            s->iir &= ~UART_IIR_FE;

        /* Set fcr - or at least the bits in it that are supposed to "stick" */
        s->fcr = val & 0xC9;
        serial_update_irq(s);
B
bellard 已提交
437 438
        break;
    case 3:
B
bellard 已提交
439 440 441 442 443 444 445
        {
            int break_enable;
            s->lcr = val;
            serial_update_parameters(s);
            break_enable = (val >> 6) & 1;
            if (break_enable != s->last_break_enable) {
                s->last_break_enable = break_enable;
446
                qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
B
bellard 已提交
447
                               &break_enable);
B
bellard 已提交
448 449
            }
        }
B
bellard 已提交
450 451
        break;
    case 4:
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
        {
            int flags;
            int old_mcr = s->mcr;
            s->mcr = val & 0x1f;
            if (val & UART_MCR_LOOP)
                break;

            if (s->poll_msl >= 0 && old_mcr != s->mcr) {

                qemu_chr_ioctl(s->chr,CHR_IOCTL_SERIAL_GET_TIOCM, &flags);

                flags &= ~(CHR_TIOCM_RTS | CHR_TIOCM_DTR);

                if (val & UART_MCR_RTS)
                    flags |= CHR_TIOCM_RTS;
                if (val & UART_MCR_DTR)
                    flags |= CHR_TIOCM_DTR;

                qemu_chr_ioctl(s->chr,CHR_IOCTL_SERIAL_SET_TIOCM, &flags);
                /* Update the modem status after a one-character-send wait-time, since there may be a response
                   from the device/computer at the other end of the serial line */
                qemu_mod_timer(s->modem_status_poll, qemu_get_clock(vm_clock) + s->char_transmit_time);
            }
        }
B
bellard 已提交
476 477 478 479 480 481 482 483 484 485 486
        break;
    case 5:
        break;
    case 6:
        break;
    case 7:
        s->scr = val;
        break;
    }
}

B
bellard 已提交
487
static uint32_t serial_ioport_read(void *opaque, uint32_t addr)
B
bellard 已提交
488
{
B
bellard 已提交
489
    SerialState *s = opaque;
B
bellard 已提交
490 491 492 493 494 495 496
    uint32_t ret;

    addr &= 7;
    switch(addr) {
    default:
    case 0:
        if (s->lcr & UART_LCR_DLAB) {
497
            ret = s->divider & 0xff;
B
bellard 已提交
498
        } else {
499 500 501 502 503 504 505 506 507 508 509
            if(s->fcr & UART_FCR_FE) {
                ret = fifo_get(s,RECV_FIFO);
                if (s->recv_fifo.count == 0)
                    s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
                else
                    qemu_mod_timer(s->fifo_timeout_timer, qemu_get_clock (vm_clock) + s->char_transmit_time * 4);
                s->timeout_ipending = 0;
            } else {
                ret = s->rbr;
                s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
            }
B
bellard 已提交
510
            serial_update_irq(s);
511 512 513 514
            if (!(s->mcr & UART_MCR_LOOP)) {
                /* in loopback mode, don't receive any data */
                qemu_chr_accept_input(s->chr);
            }
B
bellard 已提交
515 516 517 518 519 520 521 522 523 524 525 526
        }
        break;
    case 1:
        if (s->lcr & UART_LCR_DLAB) {
            ret = (s->divider >> 8) & 0xff;
        } else {
            ret = s->ier;
        }
        break;
    case 2:
        ret = s->iir;
            s->thr_ipending = 0;
B
bellard 已提交
527
        serial_update_irq(s);
B
bellard 已提交
528 529 530 531 532 533 534 535 536
        break;
    case 3:
        ret = s->lcr;
        break;
    case 4:
        ret = s->mcr;
        break;
    case 5:
        ret = s->lsr;
537 538 539 540 541
        /* Clear break interrupt */
        if (s->lsr & UART_LSR_BI) {
            s->lsr &= ~UART_LSR_BI;
            serial_update_irq(s);
        }
B
bellard 已提交
542 543 544 545 546 547 548 549 550
        break;
    case 6:
        if (s->mcr & UART_MCR_LOOP) {
            /* in loopback, the modem output pins are connected to the
               inputs */
            ret = (s->mcr & 0x0c) << 4;
            ret |= (s->mcr & 0x02) << 3;
            ret |= (s->mcr & 0x01) << 5;
        } else {
551 552
            if (s->poll_msl >= 0)
                serial_update_msl(s);
B
bellard 已提交
553
            ret = s->msr;
554 555 556 557 558
            /* Clear delta bits & msr int after read, if they were set */
            if (s->msr & UART_MSR_ANY_DELTA) {
                s->msr &= 0xF0;
                serial_update_irq(s);
            }
B
bellard 已提交
559 560 561 562 563 564 565 566 567 568 569 570
        }
        break;
    case 7:
        ret = s->scr;
        break;
    }
#ifdef DEBUG_SERIAL
    printf("serial: read addr=0x%02x val=0x%02x\n", addr, ret);
#endif
    return ret;
}

B
bellard 已提交
571
static int serial_can_receive(SerialState *s)
B
bellard 已提交
572
{
573 574 575 576 577 578 579 580 581
    if(s->fcr & UART_FCR_FE) {
        if(s->recv_fifo.count < UART_FIFO_LENGTH)
        /* Advertise (fifo.itl - fifo.count) bytes when count < ITL, and 1 if above. If UART_FIFO_LENGTH - fifo.count is
        advertised the effect will be to almost always fill the fifo completely before the guest has a chance to respond,
        effectively overriding the ITL that the guest has set. */
             return (s->recv_fifo.count <= s->recv_fifo.itl) ? s->recv_fifo.itl - s->recv_fifo.count : 1;
        else
             return 0;
    } else {
B
bellard 已提交
582
    return !(s->lsr & UART_LSR_DR);
583
    }
B
bellard 已提交
584 585
}

B
bellard 已提交
586
static void serial_receive_break(SerialState *s)
B
bellard 已提交
587 588
{
    s->rbr = 0;
589 590
    /* When the LSR_DR is set a null byte is pushed into the fifo */
    fifo_put(s, RECV_FIFO, '\0');
B
bellard 已提交
591
    s->lsr |= UART_LSR_BI | UART_LSR_DR;
B
bellard 已提交
592
    serial_update_irq(s);
B
bellard 已提交
593 594
}

595 596 597 598 599 600 601 602 603
/* There's data in recv_fifo and s->rbr has not been read for 4 char transmit times */
static void fifo_timeout_int (void *opaque) {
    SerialState *s = opaque;
    if (s->recv_fifo.count) {
        s->timeout_ipending = 1;
        serial_update_irq(s);
    }
}

B
bellard 已提交
604
static int serial_can_receive1(void *opaque)
B
bellard 已提交
605
{
B
bellard 已提交
606 607 608 609 610 611 612
    SerialState *s = opaque;
    return serial_can_receive(s);
}

static void serial_receive1(void *opaque, const uint8_t *buf, int size)
{
    SerialState *s = opaque;
613 614 615 616 617 618 619 620 621 622 623 624 625
    if(s->fcr & UART_FCR_FE) {
        int i;
        for (i = 0; i < size; i++) {
            fifo_put(s, RECV_FIFO, buf[i]);
        }
        s->lsr |= UART_LSR_DR;
        /* call the timeout receive callback in 4 char transmit time */
        qemu_mod_timer(s->fifo_timeout_timer, qemu_get_clock (vm_clock) + s->char_transmit_time * 4);
    } else {
        s->rbr = buf[0];
        s->lsr |= UART_LSR_DR;
    }
    serial_update_irq(s);
B
bellard 已提交
626
}
B
bellard 已提交
627

B
bellard 已提交
628 629 630
static void serial_event(void *opaque, int event)
{
    SerialState *s = opaque;
631 632 633
#ifdef DEBUG_SERIAL
    printf("serial: event %x\n", event);
#endif
B
bellard 已提交
634 635 636 637
    if (event == CHR_EVENT_BREAK)
        serial_receive_break(s);
}

638 639 640 641
static void serial_save(QEMUFile *f, void *opaque)
{
    SerialState *s = opaque;

B
bellard 已提交
642
    qemu_put_be16s(f,&s->divider);
643 644 645 646 647 648 649 650
    qemu_put_8s(f,&s->rbr);
    qemu_put_8s(f,&s->ier);
    qemu_put_8s(f,&s->iir);
    qemu_put_8s(f,&s->lcr);
    qemu_put_8s(f,&s->mcr);
    qemu_put_8s(f,&s->lsr);
    qemu_put_8s(f,&s->msr);
    qemu_put_8s(f,&s->scr);
651
    qemu_put_8s(f,&s->fcr);
652 653 654 655 656
}

static int serial_load(QEMUFile *f, void *opaque, int version_id)
{
    SerialState *s = opaque;
657
    uint8_t fcr = 0;
658

659
    if(version_id > 3)
660 661
        return -EINVAL;

B
bellard 已提交
662 663 664 665
    if (version_id >= 2)
        qemu_get_be16s(f, &s->divider);
    else
        s->divider = qemu_get_byte(f);
666 667 668 669 670 671 672 673 674
    qemu_get_8s(f,&s->rbr);
    qemu_get_8s(f,&s->ier);
    qemu_get_8s(f,&s->iir);
    qemu_get_8s(f,&s->lcr);
    qemu_get_8s(f,&s->mcr);
    qemu_get_8s(f,&s->lsr);
    qemu_get_8s(f,&s->msr);
    qemu_get_8s(f,&s->scr);

675 676 677 678 679
    if (version_id >= 3)
        qemu_get_8s(f,&fcr);

    /* Initialize fcr via setter to perform essential side-effects */
    serial_ioport_write(s, 0x02, fcr);
680 681 682
    return 0;
}

683 684 685 686 687 688 689 690 691 692
static void serial_reset(void *opaque)
{
    SerialState *s = opaque;

    s->rbr = 0;
    s->ier = 0;
    s->iir = UART_IIR_NO_INT;
    s->lcr = 0;
    s->lsr = UART_LSR_TEMT | UART_LSR_THRE;
    s->msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS;
693 694 695
    /* Default to 9600 baud, no parity, one stop bit */
    s->divider = 0x0C;
    s->mcr = UART_MCR_OUT2;
696
    s->scr = 0;
697 698 699 700 701 702 703 704
    s->tsr_retry = 0;
    s->char_transmit_time = (ticks_per_sec / 9600) * 9;
    s->poll_msl = 0;

    fifo_clear(s,RECV_FIFO);
    fifo_clear(s,XMIT_FIFO);

    s->last_xmit_ts = qemu_get_clock(vm_clock);
705 706 707 708 709 710

    s->thr_ipending = 0;
    s->last_break_enable = 0;
    qemu_irq_lower(s->irq);
}

711 712 713 714 715
static void serial_init_core(SerialState *s, qemu_irq irq, int baudbase,
			     CharDriverState *chr)
{
    s->irq = irq;
    s->baudbase = baudbase;
716
    s->chr = chr ?: qemu_chr_open("null", "null", NULL);
717 718 719 720 721 722

    s->modem_status_poll = qemu_new_timer(vm_clock, (QEMUTimerCB *) serial_update_msl, s);

    s->fifo_timeout_timer = qemu_new_timer(vm_clock, (QEMUTimerCB *) fifo_timeout_int, s);
    s->transmit_timer = qemu_new_timer(vm_clock, (QEMUTimerCB *) serial_xmit, s);

723
    qemu_register_reset(serial_reset, s);
724 725
    serial_reset(s);

726 727
    qemu_chr_add_handlers(s->chr, serial_can_receive1, serial_receive1,
                          serial_event, s);
728 729
}

B
bellard 已提交
730
/* If fd is zero, it means that the serial device uses the console */
A
aurel32 已提交
731 732
SerialState *serial_init(int base, qemu_irq irq, int baudbase,
                         CharDriverState *chr)
B
bellard 已提交
733 734 735 736
{
    SerialState *s;

    s = qemu_mallocz(sizeof(SerialState));
A
aurel32 已提交
737

738
    serial_init_core(s, irq, baudbase, chr);
B
bellard 已提交
739

740
    register_savevm("serial", base, 3, serial_save, serial_load, s);
741

B
bellard 已提交
742 743 744
    register_ioport_write(base, 8, 1, serial_ioport_write, s);
    register_ioport_read(base, 8, 1, serial_ioport_read, s);
    return s;
B
bellard 已提交
745
}
746 747

/* Memory mapped interface */
B
Blue Swirl 已提交
748
static uint32_t serial_mm_readb(void *opaque, target_phys_addr_t addr)
749 750 751
{
    SerialState *s = opaque;

752
    return serial_ioport_read(s, addr >> s->it_shift) & 0xFF;
753 754
}

B
Blue Swirl 已提交
755 756
static void serial_mm_writeb(void *opaque, target_phys_addr_t addr,
                             uint32_t value)
757 758 759
{
    SerialState *s = opaque;

760
    serial_ioport_write(s, addr >> s->it_shift, value & 0xFF);
761 762
}

B
Blue Swirl 已提交
763
static uint32_t serial_mm_readw(void *opaque, target_phys_addr_t addr)
764 765
{
    SerialState *s = opaque;
766
    uint32_t val;
767

768
    val = serial_ioport_read(s, addr >> s->it_shift) & 0xFFFF;
769 770 771 772
#ifdef TARGET_WORDS_BIGENDIAN
    val = bswap16(val);
#endif
    return val;
773 774
}

B
Blue Swirl 已提交
775 776
static void serial_mm_writew(void *opaque, target_phys_addr_t addr,
                             uint32_t value)
777 778
{
    SerialState *s = opaque;
779 780 781
#ifdef TARGET_WORDS_BIGENDIAN
    value = bswap16(value);
#endif
782
    serial_ioport_write(s, addr >> s->it_shift, value & 0xFFFF);
783 784
}

B
Blue Swirl 已提交
785
static uint32_t serial_mm_readl(void *opaque, target_phys_addr_t addr)
786 787
{
    SerialState *s = opaque;
788
    uint32_t val;
789

790
    val = serial_ioport_read(s, addr >> s->it_shift);
791 792 793 794
#ifdef TARGET_WORDS_BIGENDIAN
    val = bswap32(val);
#endif
    return val;
795 796
}

B
Blue Swirl 已提交
797 798
static void serial_mm_writel(void *opaque, target_phys_addr_t addr,
                             uint32_t value)
799 800
{
    SerialState *s = opaque;
801 802 803
#ifdef TARGET_WORDS_BIGENDIAN
    value = bswap32(value);
#endif
804
    serial_ioport_write(s, addr >> s->it_shift, value);
805 806 807 808 809 810 811 812 813 814 815 816 817 818
}

static CPUReadMemoryFunc *serial_mm_read[] = {
    &serial_mm_readb,
    &serial_mm_readw,
    &serial_mm_readl,
};

static CPUWriteMemoryFunc *serial_mm_write[] = {
    &serial_mm_writeb,
    &serial_mm_writew,
    &serial_mm_writel,
};

819
SerialState *serial_mm_init (target_phys_addr_t base, int it_shift,
A
aurel32 已提交
820 821
                             qemu_irq irq, int baudbase,
                             CharDriverState *chr, int ioregister)
822 823 824 825 826
{
    SerialState *s;
    int s_io_memory;

    s = qemu_mallocz(sizeof(SerialState));
827

828 829
    s->it_shift = it_shift;

830 831
    serial_init_core(s, irq, baudbase, chr);
    register_savevm("serial", base, 3, serial_save, serial_load, s);
832

T
ths 已提交
833
    if (ioregister) {
834
        s_io_memory = cpu_register_io_memory(serial_mm_read,
T
ths 已提交
835 836 837
                                             serial_mm_write, s);
        cpu_register_physical_memory(base, 8 << it_shift, s_io_memory);
    }
838
    serial_update_msl(s);
839 840
    return s;
}