serial.c 23.5 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.
 */
G
Gerd Hoffmann 已提交
25

P
Paolo Bonzini 已提交
26
#include "hw/char/serial.h"
27
#include "sysemu/char.h"
28
#include "qemu/timer.h"
29
#include "exec/address-spaces.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
/* 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 XMIT_FIFO           0
#define RECV_FIFO           1
#define MAX_XMIT_RETRY      4

99 100
#ifdef DEBUG_SERIAL
#define DPRINTF(fmt, ...) \
101
do { fprintf(stderr, "serial: " fmt , ## __VA_ARGS__); } while (0)
102 103
#else
#define DPRINTF(fmt, ...) \
104
do {} while (0)
105 106
#endif

107
static void serial_receive1(void *opaque, const uint8_t *buf, int size);
108

109
static void fifo_clear(SerialState *s, int fifo)
B
bellard 已提交
110
{
111 112 113 114 115
    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 已提交
116 117
}

118
static int fifo_put(SerialState *s, int fifo, uint8_t chr)
A
aurel32 已提交
119
{
120
    SerialFIFO *f = (fifo) ? &s->recv_fifo : &s->xmit_fifo;
A
aurel32 已提交
121

122 123
    /* Receive overruns do not overwrite FIFO contents. */
    if (fifo == XMIT_FIFO || f->count < UART_FIFO_LENGTH) {
A
aurel32 已提交
124

125 126 127 128 129 130 131 132 133 134
        f->data[f->head++] = chr;

        if (f->head == UART_FIFO_LENGTH)
            f->head = 0;
    }

    if (f->count < UART_FIFO_LENGTH)
        f->count++;
    else if (fifo == RECV_FIFO)
        s->lsr |= UART_LSR_OE;
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

    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 已提交
154

155 156 157 158 159 160
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;
161
    } else if ((s->ier & UART_IER_RDI) && s->timeout_ipending) {
162 163 164
        /* Note that(s->ier & UART_IER_RDI) can mask this interrupt,
         * this is not in the specification but is observed on existing
         * hardware.  */
165
        tmp_iir = UART_IIR_CTI;
166 167 168 169
    } else if ((s->ier & UART_IER_RDI) && (s->lsr & UART_LSR_DR) &&
               (!(s->fcr & UART_FCR_FE) ||
                s->recv_fifo.count >= s->recv_fifo.itl)) {
        tmp_iir = UART_IIR_RDI;
170 171 172 173 174 175 176 177 178 179 180 181
    } 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 已提交
182 183 184
    }
}

B
bellard 已提交
185 186
static void serial_update_parameters(SerialState *s)
{
187
    int speed, parity, data_bits, stop_bits, frame_size;
B
bellard 已提交
188
    QEMUSerialSetParams ssp;
B
bellard 已提交
189

190 191 192
    if (s->divider == 0)
        return;

S
Stefan Weil 已提交
193
    /* Start bit. */
194
    frame_size = 1;
B
bellard 已提交
195
    if (s->lcr & 0x08) {
S
Stefan Weil 已提交
196 197
        /* Parity bit. */
        frame_size++;
B
bellard 已提交
198 199 200 201 202 203 204
        if (s->lcr & 0x10)
            parity = 'E';
        else
            parity = 'O';
    } else {
            parity = 'N';
    }
205
    if (s->lcr & 0x04)
B
bellard 已提交
206 207 208
        stop_bits = 2;
    else
        stop_bits = 1;
209

B
bellard 已提交
210
    data_bits = (s->lcr & 0x03) + 5;
211
    frame_size += data_bits + stop_bits;
A
aurel32 已提交
212
    speed = s->baudbase / s->divider;
B
bellard 已提交
213 214 215 216
    ssp.speed = speed;
    ssp.parity = parity;
    ssp.data_bits = data_bits;
    ssp.stop_bits = stop_bits;
217
    s->char_transmit_time =  (get_ticks_per_sec() / speed) * frame_size;
218
    qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
219 220

    DPRINTF("speed=%d parity=%c data=%d stop=%d\n",
B
bellard 已提交
221 222 223
           speed, parity, data_bits, stop_bits);
}

224 225 226 227 228 229 230
static void serial_update_msl(SerialState *s)
{
    uint8_t omsr;
    int flags;

    qemu_del_timer(s->modem_status_poll);

231
    if (qemu_chr_fe_ioctl(s->chr,CHR_IOCTL_SERIAL_GET_TIOCM, &flags) == -ENOTSUP) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        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)
256
        qemu_mod_timer(s->modem_status_poll, qemu_get_clock_ns(vm_clock) + get_ticks_per_sec() / 100);
257 258
}

259
static gboolean serial_xmit(GIOChannel *chan, GIOCondition cond, void *opaque)
260 261 262 263 264 265 266 267
{
    SerialState *s = opaque;

    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;
268 269
        } else if ((s->lsr & UART_LSR_THRE)) {
            return FALSE;
270 271 272
        } else {
            s->tsr = s->thr;
            s->lsr |= UART_LSR_THRE;
273
            s->lsr &= ~UART_LSR_TEMT;
274 275 276 277 278 279
        }
    }

    if (s->mcr & UART_MCR_LOOP) {
        /* in loopback mode, say that we just received a char */
        serial_receive1(s, &s->tsr, 1);
280
    } else if (qemu_chr_fe_write(s->chr, &s->tsr, 1) != 1) {
281 282
        if (s->tsr_retry >= 0 && s->tsr_retry < MAX_XMIT_RETRY &&
            qemu_chr_fe_add_watch(s->chr, G_IO_OUT, serial_xmit, s) > 0) {
283
            s->tsr_retry++;
284
            return FALSE;
285
        }
286 287
        s->tsr_retry = 0;
    } else {
288 289 290
        s->tsr_retry = 0;
    }

291
    s->last_xmit_ts = qemu_get_clock_ns(vm_clock);
292 293 294 295 296 297

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

    return FALSE;
300 301 302
}


303 304
static void serial_ioport_write(void *opaque, hwaddr addr, uint64_t val,
                                unsigned size)
B
bellard 已提交
305
{
B
bellard 已提交
306
    SerialState *s = opaque;
307

B
bellard 已提交
308
    addr &= 7;
K
Kevin Wolf 已提交
309
    DPRINTF("write addr=0x%" HWADDR_PRIx " val=0x%" PRIx64 "\n", addr, val);
B
bellard 已提交
310 311 312 313 314
    switch(addr) {
    default:
    case 0:
        if (s->lcr & UART_LCR_DLAB) {
            s->divider = (s->divider & 0xff00) | val;
B
bellard 已提交
315
            serial_update_parameters(s);
B
bellard 已提交
316
        } else {
317 318
            s->thr = (uint8_t) val;
            if(s->fcr & UART_FCR_FE) {
A
Aurelien Jarno 已提交
319 320 321 322 323
                fifo_put(s, XMIT_FIFO, s->thr);
                s->thr_ipending = 0;
                s->lsr &= ~UART_LSR_TEMT;
                s->lsr &= ~UART_LSR_THRE;
                serial_update_irq(s);
A
aurel32 已提交
324
            } else {
A
Aurelien Jarno 已提交
325 326 327
                s->thr_ipending = 0;
                s->lsr &= ~UART_LSR_THRE;
                serial_update_irq(s);
A
aurel32 已提交
328
            }
329
            serial_xmit(NULL, G_IO_OUT, s);
B
bellard 已提交
330 331 332 333 334
        }
        break;
    case 1:
        if (s->lcr & UART_LCR_DLAB) {
            s->divider = (s->divider & 0x00ff) | (val << 8);
B
bellard 已提交
335
            serial_update_parameters(s);
B
bellard 已提交
336
        } else {
B
bellard 已提交
337
            s->ier = val & 0x0f;
338 339 340 341 342 343 344 345 346 347 348
            /* 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 已提交
349 350
            if (s->lsr & UART_LSR_THRE) {
                s->thr_ipending = 1;
351
                serial_update_irq(s);
B
bellard 已提交
352
            }
B
bellard 已提交
353 354 355
        }
        break;
    case 2:
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
        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 已提交
400 401
        break;
    case 3:
B
bellard 已提交
402 403 404 405 406 407 408
        {
            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;
409
                qemu_chr_fe_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
B
bellard 已提交
410
                               &break_enable);
B
bellard 已提交
411 412
            }
        }
B
bellard 已提交
413 414
        break;
    case 4:
415 416 417 418 419 420 421 422 423
        {
            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) {

424
                qemu_chr_fe_ioctl(s->chr,CHR_IOCTL_SERIAL_GET_TIOCM, &flags);
425 426 427 428 429 430 431 432

                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;

433
                qemu_chr_fe_ioctl(s->chr,CHR_IOCTL_SERIAL_SET_TIOCM, &flags);
434 435
                /* 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 */
436
                qemu_mod_timer(s->modem_status_poll, qemu_get_clock_ns(vm_clock) + s->char_transmit_time);
437 438
            }
        }
B
bellard 已提交
439 440 441 442 443 444 445 446 447 448 449
        break;
    case 5:
        break;
    case 6:
        break;
    case 7:
        s->scr = val;
        break;
    }
}

450
static uint64_t serial_ioport_read(void *opaque, hwaddr addr, unsigned size)
B
bellard 已提交
451
{
B
bellard 已提交
452
    SerialState *s = opaque;
B
bellard 已提交
453 454 455 456 457 458 459
    uint32_t ret;

    addr &= 7;
    switch(addr) {
    default:
    case 0:
        if (s->lcr & UART_LCR_DLAB) {
460
            ret = s->divider & 0xff;
B
bellard 已提交
461
        } else {
462 463 464 465 466
            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
467
                    qemu_mod_timer(s->fifo_timeout_timer, qemu_get_clock_ns (vm_clock) + s->char_transmit_time * 4);
468 469 470 471 472
                s->timeout_ipending = 0;
            } else {
                ret = s->rbr;
                s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
            }
B
bellard 已提交
473
            serial_update_irq(s);
474 475 476 477
            if (!(s->mcr & UART_MCR_LOOP)) {
                /* in loopback mode, don't receive any data */
                qemu_chr_accept_input(s->chr);
            }
B
bellard 已提交
478 479 480 481 482 483 484 485 486 487 488
        }
        break;
    case 1:
        if (s->lcr & UART_LCR_DLAB) {
            ret = (s->divider >> 8) & 0xff;
        } else {
            ret = s->ier;
        }
        break;
    case 2:
        ret = s->iir;
489
        if ((ret & UART_IIR_ID) == UART_IIR_THRI) {
B
bellard 已提交
490
            s->thr_ipending = 0;
491 492
            serial_update_irq(s);
        }
B
bellard 已提交
493 494 495 496 497 498 499 500 501
        break;
    case 3:
        ret = s->lcr;
        break;
    case 4:
        ret = s->mcr;
        break;
    case 5:
        ret = s->lsr;
502 503 504
        /* Clear break and overrun interrupts */
        if (s->lsr & (UART_LSR_BI|UART_LSR_OE)) {
            s->lsr &= ~(UART_LSR_BI|UART_LSR_OE);
505 506
            serial_update_irq(s);
        }
B
bellard 已提交
507 508 509 510 511 512 513 514 515
        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 {
516 517
            if (s->poll_msl >= 0)
                serial_update_msl(s);
B
bellard 已提交
518
            ret = s->msr;
519 520 521 522 523
            /* 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 已提交
524 525 526 527 528 529
        }
        break;
    case 7:
        ret = s->scr;
        break;
    }
K
Kevin Wolf 已提交
530
    DPRINTF("read addr=0x%" HWADDR_PRIx " val=0x%02x\n", addr, ret);
B
bellard 已提交
531 532 533
    return ret;
}

B
bellard 已提交
534
static int serial_can_receive(SerialState *s)
B
bellard 已提交
535
{
536 537 538 539 540 541 542 543 544
    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 已提交
545
    return !(s->lsr & UART_LSR_DR);
546
    }
B
bellard 已提交
547 548
}

B
bellard 已提交
549
static void serial_receive_break(SerialState *s)
B
bellard 已提交
550 551
{
    s->rbr = 0;
552 553
    /* When the LSR_DR is set a null byte is pushed into the fifo */
    fifo_put(s, RECV_FIFO, '\0');
B
bellard 已提交
554
    s->lsr |= UART_LSR_BI | UART_LSR_DR;
B
bellard 已提交
555
    serial_update_irq(s);
B
bellard 已提交
556 557
}

558 559 560 561 562 563 564 565 566
/* 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 已提交
567
static int serial_can_receive1(void *opaque)
B
bellard 已提交
568
{
B
bellard 已提交
569 570 571 572 573 574 575
    SerialState *s = opaque;
    return serial_can_receive(s);
}

static void serial_receive1(void *opaque, const uint8_t *buf, int size)
{
    SerialState *s = opaque;
576 577 578 579

    if (s->wakeup) {
        qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER);
    }
580 581 582 583 584 585 586
    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 */
587
        qemu_mod_timer(s->fifo_timeout_timer, qemu_get_clock_ns (vm_clock) + s->char_transmit_time * 4);
588
    } else {
589 590
        if (s->lsr & UART_LSR_DR)
            s->lsr |= UART_LSR_OE;
591 592 593 594
        s->rbr = buf[0];
        s->lsr |= UART_LSR_DR;
    }
    serial_update_irq(s);
B
bellard 已提交
595
}
B
bellard 已提交
596

B
bellard 已提交
597 598 599
static void serial_event(void *opaque, int event)
{
    SerialState *s = opaque;
600
    DPRINTF("event %x\n", event);
B
bellard 已提交
601 602 603 604
    if (event == CHR_EVENT_BREAK)
        serial_receive_break(s);
}

605
static void serial_pre_save(void *opaque)
606
{
607
    SerialState *s = opaque;
J
Juan Quintela 已提交
608
    s->fcr_vmstate = s->fcr;
609 610
}

611
static int serial_post_load(void *opaque, int version_id)
J
Juan Quintela 已提交
612 613
{
    SerialState *s = opaque;
614

615 616 617
    if (version_id < 3) {
        s->fcr_vmstate = 0;
    }
618
    /* Initialize fcr via setter to perform essential side-effects */
619
    serial_ioport_write(s, 0x02, s->fcr_vmstate, 1);
620
    serial_update_parameters(s);
621 622 623
    return 0;
}

G
Gerd Hoffmann 已提交
624
const VMStateDescription vmstate_serial = {
J
Juan Quintela 已提交
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
    .name = "serial",
    .version_id = 3,
    .minimum_version_id = 2,
    .pre_save = serial_pre_save,
    .post_load = serial_post_load,
    .fields      = (VMStateField []) {
        VMSTATE_UINT16_V(divider, SerialState, 2),
        VMSTATE_UINT8(rbr, SerialState),
        VMSTATE_UINT8(ier, SerialState),
        VMSTATE_UINT8(iir, SerialState),
        VMSTATE_UINT8(lcr, SerialState),
        VMSTATE_UINT8(mcr, SerialState),
        VMSTATE_UINT8(lsr, SerialState),
        VMSTATE_UINT8(msr, SerialState),
        VMSTATE_UINT8(scr, SerialState),
        VMSTATE_UINT8_V(fcr_vmstate, SerialState, 3),
        VMSTATE_END_OF_LIST()
    }
};

645 646 647 648 649 650 651 652 653 654
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;
S
Stefan Weil 已提交
655
    /* Default to 9600 baud, 1 start bit, 8 data bits, 1 stop bit, no parity. */
656 657
    s->divider = 0x0C;
    s->mcr = UART_MCR_OUT2;
658
    s->scr = 0;
659
    s->tsr_retry = 0;
S
Stefan Weil 已提交
660
    s->char_transmit_time = (get_ticks_per_sec() / 9600) * 10;
661 662 663 664 665
    s->poll_msl = 0;

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

666
    s->last_xmit_ts = qemu_get_clock_ns(vm_clock);
667 668 669 670 671 672

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

G
Gerd Hoffmann 已提交
673
void serial_init_core(SerialState *s)
674
{
G
Gerd Hoffmann 已提交
675
    if (!s->chr) {
676 677 678 679
        fprintf(stderr, "Can't create serial device, empty char device\n");
	exit(1);
    }

680
    s->modem_status_poll = qemu_new_timer_ns(vm_clock, (QEMUTimerCB *) serial_update_msl, s);
681

682
    s->fifo_timeout_timer = qemu_new_timer_ns(vm_clock, (QEMUTimerCB *) fifo_timeout_int, s);
683
    qemu_register_reset(serial_reset, s);
684

685 686
    qemu_chr_add_handlers(s->chr, serial_can_receive1, serial_receive1,
                          serial_event, s);
687 688
}

G
Gerd Hoffmann 已提交
689 690 691 692 693 694
void serial_exit_core(SerialState *s)
{
    qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
    qemu_unregister_reset(serial_reset, s);
}

695 696 697 698 699 700 701
/* Change the main reference oscillator frequency. */
void serial_set_frequency(SerialState *s, uint32_t frequency)
{
    s->baudbase = frequency;
    serial_update_parameters(s);
}

G
Gerd Hoffmann 已提交
702
const MemoryRegionOps serial_io_ops = {
703 704 705 706 707 708 709
    .read = serial_ioport_read,
    .write = serial_ioport_write,
    .impl = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
    .endianness = DEVICE_LITTLE_ENDIAN,
710 711
};

A
aurel32 已提交
712
SerialState *serial_init(int base, qemu_irq irq, int baudbase,
J
Julien Grall 已提交
713
                         CharDriverState *chr, MemoryRegion *system_io)
B
bellard 已提交
714 715 716
{
    SerialState *s;

717
    s = g_malloc0(sizeof(SerialState));
A
aurel32 已提交
718

G
Gerd Hoffmann 已提交
719 720 721 722
    s->irq = irq;
    s->baudbase = baudbase;
    s->chr = chr;
    serial_init_core(s);
B
bellard 已提交
723

A
Alex Williamson 已提交
724
    vmstate_register(NULL, base, &vmstate_serial, s);
725

726
    memory_region_init_io(&s->io, &serial_io_ops, s, "serial", 8);
J
Julien Grall 已提交
727
    memory_region_add_subregion(system_io, base, &s->io);
728

B
bellard 已提交
729
    return s;
B
bellard 已提交
730
}
731 732

/* Memory mapped interface */
A
Avi Kivity 已提交
733
static uint64_t serial_mm_read(void *opaque, hwaddr addr,
734
                               unsigned size)
735 736
{
    SerialState *s = opaque;
737
    return serial_ioport_read(s, addr >> s->it_shift, 1);
738 739
}

A
Avi Kivity 已提交
740
static void serial_mm_write(void *opaque, hwaddr addr,
741
                            uint64_t value, unsigned size)
B
Blue Swirl 已提交
742 743
{
    SerialState *s = opaque;
744
    value &= ~0u >> (32 - (size * 8));
745
    serial_ioport_write(s, addr >> s->it_shift, value, 1);
B
Blue Swirl 已提交
746 747
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
static const MemoryRegionOps serial_mm_ops[3] = {
    [DEVICE_NATIVE_ENDIAN] = {
        .read = serial_mm_read,
        .write = serial_mm_write,
        .endianness = DEVICE_NATIVE_ENDIAN,
    },
    [DEVICE_LITTLE_ENDIAN] = {
        .read = serial_mm_read,
        .write = serial_mm_write,
        .endianness = DEVICE_LITTLE_ENDIAN,
    },
    [DEVICE_BIG_ENDIAN] = {
        .read = serial_mm_read,
        .write = serial_mm_write,
        .endianness = DEVICE_BIG_ENDIAN,
    },
764 765
};

766
SerialState *serial_mm_init(MemoryRegion *address_space,
A
Avi Kivity 已提交
767
                            hwaddr base, int it_shift,
768 769
                            qemu_irq irq, int baudbase,
                            CharDriverState *chr, enum device_endian end)
770 771 772
{
    SerialState *s;

773
    s = g_malloc0(sizeof(SerialState));
774

775
    s->it_shift = it_shift;
G
Gerd Hoffmann 已提交
776 777 778
    s->irq = irq;
    s->baudbase = baudbase;
    s->chr = chr;
779

G
Gerd Hoffmann 已提交
780
    serial_init_core(s);
A
Alex Williamson 已提交
781
    vmstate_register(NULL, base, &vmstate_serial, s);
782

783 784
    memory_region_init_io(&s->io, &serial_mm_ops[end], s,
                          "serial", 8 << it_shift);
785
    memory_region_add_subregion(address_space, base, &s->io);
786

787
    serial_update_msl(s);
788 789
    return s;
}