serial.c 14.8 KB
Newer Older
B
bellard 已提交
1 2
/*
 * QEMU 16450 UART emulation
3
 *
B
bellard 已提交
4
 * Copyright (c) 2003-2004 Fabrice Bellard
5
 *
B
bellard 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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 已提交
24 25 26 27
#include "hw.h"
#include "qemu-char.h"
#include "isa.h"
#include "pc.h"
A
aurel32 已提交
28
#include "qemu-timer.h"
B
bellard 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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

//#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 */

/*
 * 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 */

A
aurel32 已提交
77 78 79 80 81 82 83
/*
 * Delay TX IRQ after sending as much characters as the given interval would
 * contain on real hardware. This avoids overloading the guest if it processes
 * its output buffer in a loop inside the TX IRQ handler.
 */
#define THROTTLE_TX_INTERVAL	10 /* ms */

B
bellard 已提交
84
struct SerialState {
B
bellard 已提交
85
    uint16_t divider;
B
bellard 已提交
86 87 88 89 90 91
    uint8_t rbr; /* receive register */
    uint8_t ier;
    uint8_t iir; /* read only */
    uint8_t lcr;
    uint8_t mcr;
    uint8_t lsr; /* read only */
B
bellard 已提交
92
    uint8_t msr; /* read only */
B
bellard 已提交
93 94 95 96
    uint8_t scr;
    /* NOTE: this hidden state is necessary for tx irq generation as
       it can be reset while reading iir */
    int thr_ipending;
P
pbrook 已提交
97
    qemu_irq irq;
B
bellard 已提交
98
    CharDriverState *chr;
B
bellard 已提交
99
    int last_break_enable;
100
    target_phys_addr_t base;
101
    int it_shift;
A
aurel32 已提交
102
    int baudbase;
A
aurel32 已提交
103 104
    QEMUTimer *tx_timer;
    int tx_burst;
B
bellard 已提交
105
};
B
bellard 已提交
106

107 108
static void serial_receive_byte(SerialState *s, int ch);

B
bellard 已提交
109
static void serial_update_irq(SerialState *s)
B
bellard 已提交
110 111 112 113 114 115 116 117 118
{
    if ((s->lsr & UART_LSR_DR) && (s->ier & UART_IER_RDI)) {
        s->iir = UART_IIR_RDI;
    } else if (s->thr_ipending && (s->ier & UART_IER_THRI)) {
        s->iir = UART_IIR_THRI;
    } else {
        s->iir = UART_IIR_NO_INT;
    }
    if (s->iir != UART_IIR_NO_INT) {
P
pbrook 已提交
119
        qemu_irq_raise(s->irq);
B
bellard 已提交
120
    } else {
P
pbrook 已提交
121
        qemu_irq_lower(s->irq);
B
bellard 已提交
122 123 124
    }
}

A
aurel32 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138
static void serial_tx_done(void *opaque)
{
    SerialState *s = opaque;

    if (s->tx_burst < 0) {
        uint16_t divider;

        if (s->divider)
          divider = s->divider;
        else
          divider = 1;

        /* We assume 10 bits/char, OK for this purpose. */
        s->tx_burst = THROTTLE_TX_INTERVAL * 1000 /
A
aurel32 已提交
139
            (1000000 * 10 / (s->baudbase / divider));
A
aurel32 已提交
140 141 142 143 144 145 146
    }
    s->thr_ipending = 1;
    s->lsr |= UART_LSR_THRE;
    s->lsr |= UART_LSR_TEMT;
    serial_update_irq(s);
}

B
bellard 已提交
147 148 149
static void serial_update_parameters(SerialState *s)
{
    int speed, parity, data_bits, stop_bits;
B
bellard 已提交
150
    QEMUSerialSetParams ssp;
B
bellard 已提交
151 152 153 154 155 156 157 158 159

    if (s->lcr & 0x08) {
        if (s->lcr & 0x10)
            parity = 'E';
        else
            parity = 'O';
    } else {
            parity = 'N';
    }
160
    if (s->lcr & 0x04)
B
bellard 已提交
161 162 163 164 165 166
        stop_bits = 2;
    else
        stop_bits = 1;
    data_bits = (s->lcr & 0x03) + 5;
    if (s->divider == 0)
        return;
A
aurel32 已提交
167
    speed = s->baudbase / s->divider;
B
bellard 已提交
168 169 170 171 172 173
    ssp.speed = speed;
    ssp.parity = parity;
    ssp.data_bits = data_bits;
    ssp.stop_bits = stop_bits;
    qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
#if 0
174
    printf("speed=%d parity=%c data=%d stop=%d\n",
B
bellard 已提交
175 176 177 178
           speed, parity, data_bits, stop_bits);
#endif
}

B
bellard 已提交
179
static void serial_ioport_write(void *opaque, uint32_t addr, uint32_t val)
B
bellard 已提交
180
{
B
bellard 已提交
181
    SerialState *s = opaque;
B
bellard 已提交
182
    unsigned char ch;
183

B
bellard 已提交
184 185 186 187 188 189 190 191 192
    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 已提交
193
            serial_update_parameters(s);
B
bellard 已提交
194 195 196
        } else {
            s->thr_ipending = 0;
            s->lsr &= ~UART_LSR_THRE;
B
bellard 已提交
197
            serial_update_irq(s);
B
bellard 已提交
198
            ch = val;
199 200 201
            if (!(s->mcr & UART_MCR_LOOP)) {
                /* when not in loopback mode, send the char */
                qemu_chr_write(s->chr, &ch, 1);
A
aurel32 已提交
202
            } else {
203 204 205
                /* in loopback mode, say that we just received a char */
                serial_receive_byte(s, ch);
            }
A
aurel32 已提交
206 207 208 209 210 211 212 213
            if (s->tx_burst > 0) {
                s->tx_burst--;
                serial_tx_done(s);
            } else if (s->tx_burst == 0) {
                s->tx_burst--;
                qemu_mod_timer(s->tx_timer, qemu_get_clock(vm_clock) +
                               ticks_per_sec * THROTTLE_TX_INTERVAL / 1000);
            }
B
bellard 已提交
214 215 216 217 218
        }
        break;
    case 1:
        if (s->lcr & UART_LCR_DLAB) {
            s->divider = (s->divider & 0x00ff) | (val << 8);
B
bellard 已提交
219
            serial_update_parameters(s);
B
bellard 已提交
220
        } else {
B
bellard 已提交
221 222 223 224
            s->ier = val & 0x0f;
            if (s->lsr & UART_LSR_THRE) {
                s->thr_ipending = 1;
            }
B
bellard 已提交
225
            serial_update_irq(s);
B
bellard 已提交
226 227 228 229 230
        }
        break;
    case 2:
        break;
    case 3:
B
bellard 已提交
231 232 233 234 235 236 237
        {
            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;
238
                qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_BREAK,
B
bellard 已提交
239
                               &break_enable);
B
bellard 已提交
240 241
            }
        }
B
bellard 已提交
242 243
        break;
    case 4:
B
bellard 已提交
244
        s->mcr = val & 0x1f;
B
bellard 已提交
245 246 247 248 249 250 251 252 253 254 255
        break;
    case 5:
        break;
    case 6:
        break;
    case 7:
        s->scr = val;
        break;
    }
}

B
bellard 已提交
256
static uint32_t serial_ioport_read(void *opaque, uint32_t addr)
B
bellard 已提交
257
{
B
bellard 已提交
258
    SerialState *s = opaque;
B
bellard 已提交
259 260 261 262 263 264 265
    uint32_t ret;

    addr &= 7;
    switch(addr) {
    default:
    case 0:
        if (s->lcr & UART_LCR_DLAB) {
266
            ret = s->divider & 0xff;
B
bellard 已提交
267 268 269
        } else {
            ret = s->rbr;
            s->lsr &= ~(UART_LSR_DR | UART_LSR_BI);
B
bellard 已提交
270
            serial_update_irq(s);
271 272 273 274
            if (!(s->mcr & UART_MCR_LOOP)) {
                /* in loopback mode, don't receive any data */
                qemu_chr_accept_input(s->chr);
            }
B
bellard 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288
        }
        break;
    case 1:
        if (s->lcr & UART_LCR_DLAB) {
            ret = (s->divider >> 8) & 0xff;
        } else {
            ret = s->ier;
        }
        break;
    case 2:
        ret = s->iir;
        /* reset THR pending bit */
        if ((ret & 0x7) == UART_IIR_THRI)
            s->thr_ipending = 0;
B
bellard 已提交
289
        serial_update_irq(s);
B
bellard 已提交
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
        break;
    case 3:
        ret = s->lcr;
        break;
    case 4:
        ret = s->mcr;
        break;
    case 5:
        ret = s->lsr;
        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 {
            ret = s->msr;
        }
        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 已提交
321
static int serial_can_receive(SerialState *s)
B
bellard 已提交
322 323 324 325
{
    return !(s->lsr & UART_LSR_DR);
}

B
bellard 已提交
326
static void serial_receive_byte(SerialState *s, int ch)
B
bellard 已提交
327 328 329
{
    s->rbr = ch;
    s->lsr |= UART_LSR_DR;
B
bellard 已提交
330
    serial_update_irq(s);
B
bellard 已提交
331 332
}

B
bellard 已提交
333
static void serial_receive_break(SerialState *s)
B
bellard 已提交
334 335 336
{
    s->rbr = 0;
    s->lsr |= UART_LSR_BI | UART_LSR_DR;
B
bellard 已提交
337
    serial_update_irq(s);
B
bellard 已提交
338 339
}

B
bellard 已提交
340
static int serial_can_receive1(void *opaque)
B
bellard 已提交
341
{
B
bellard 已提交
342 343 344 345 346 347 348 349 350
    SerialState *s = opaque;
    return serial_can_receive(s);
}

static void serial_receive1(void *opaque, const uint8_t *buf, int size)
{
    SerialState *s = opaque;
    serial_receive_byte(s, buf[0]);
}
B
bellard 已提交
351

B
bellard 已提交
352 353 354 355 356 357 358
static void serial_event(void *opaque, int event)
{
    SerialState *s = opaque;
    if (event == CHR_EVENT_BREAK)
        serial_receive_break(s);
}

359 360 361 362
static void serial_save(QEMUFile *f, void *opaque)
{
    SerialState *s = opaque;

B
bellard 已提交
363
    qemu_put_be16s(f,&s->divider);
364 365 366 367 368 369 370 371 372 373 374 375 376 377
    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);
}

static int serial_load(QEMUFile *f, void *opaque, int version_id)
{
    SerialState *s = opaque;

B
bellard 已提交
378
    if(version_id > 2)
379 380
        return -EINVAL;

B
bellard 已提交
381 382 383 384
    if (version_id >= 2)
        qemu_get_be16s(f, &s->divider);
    else
        s->divider = qemu_get_byte(f);
385 386 387 388 389 390 391 392 393 394 395 396
    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);

    return 0;
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
static void serial_reset(void *opaque)
{
    SerialState *s = opaque;

    s->divider = 0;
    s->rbr = 0;
    s->ier = 0;
    s->iir = UART_IIR_NO_INT;
    s->lcr = 0;
    s->mcr = 0;
    s->lsr = UART_LSR_TEMT | UART_LSR_THRE;
    s->msr = UART_MSR_DCD | UART_MSR_DSR | UART_MSR_CTS;
    s->scr = 0;

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

B
bellard 已提交
416
/* If fd is zero, it means that the serial device uses the console */
A
aurel32 已提交
417 418
SerialState *serial_init(int base, qemu_irq irq, int baudbase,
                         CharDriverState *chr)
B
bellard 已提交
419 420 421 422 423 424
{
    SerialState *s;

    s = qemu_mallocz(sizeof(SerialState));
    if (!s)
        return NULL;
B
bellard 已提交
425
    s->irq = irq;
A
aurel32 已提交
426
    s->baudbase = baudbase;
427

A
aurel32 已提交
428 429 430 431
    s->tx_timer = qemu_new_timer(vm_clock, serial_tx_done, s);
    if (!s->tx_timer)
        return NULL;

432 433
    qemu_register_reset(serial_reset, s);
    serial_reset(s);
B
bellard 已提交
434

B
bellard 已提交
435
    register_savevm("serial", base, 2, serial_save, serial_load, s);
436

B
bellard 已提交
437 438
    register_ioport_write(base, 8, 1, serial_ioport_write, s);
    register_ioport_read(base, 8, 1, serial_ioport_read, s);
B
bellard 已提交
439
    s->chr = chr;
440 441
    qemu_chr_add_handlers(chr, serial_can_receive1, serial_receive1,
                          serial_event, s);
B
bellard 已提交
442
    return s;
B
bellard 已提交
443
}
444 445

/* Memory mapped interface */
T
ths 已提交
446
uint32_t serial_mm_readb (void *opaque, target_phys_addr_t addr)
447 448 449 450 451 452
{
    SerialState *s = opaque;

    return serial_ioport_read(s, (addr - s->base) >> s->it_shift) & 0xFF;
}

T
ths 已提交
453 454
void serial_mm_writeb (void *opaque,
                       target_phys_addr_t addr, uint32_t value)
455 456 457 458 459 460
{
    SerialState *s = opaque;

    serial_ioport_write(s, (addr - s->base) >> s->it_shift, value & 0xFF);
}

T
ths 已提交
461
uint32_t serial_mm_readw (void *opaque, target_phys_addr_t addr)
462 463
{
    SerialState *s = opaque;
464
    uint32_t val;
465

466 467 468 469 470
    val = serial_ioport_read(s, (addr - s->base) >> s->it_shift) & 0xFFFF;
#ifdef TARGET_WORDS_BIGENDIAN
    val = bswap16(val);
#endif
    return val;
471 472
}

T
ths 已提交
473 474
void serial_mm_writew (void *opaque,
                       target_phys_addr_t addr, uint32_t value)
475 476
{
    SerialState *s = opaque;
477 478 479
#ifdef TARGET_WORDS_BIGENDIAN
    value = bswap16(value);
#endif
480 481 482
    serial_ioport_write(s, (addr - s->base) >> s->it_shift, value & 0xFFFF);
}

T
ths 已提交
483
uint32_t serial_mm_readl (void *opaque, target_phys_addr_t addr)
484 485
{
    SerialState *s = opaque;
486
    uint32_t val;
487

488 489 490 491 492
    val = serial_ioport_read(s, (addr - s->base) >> s->it_shift);
#ifdef TARGET_WORDS_BIGENDIAN
    val = bswap32(val);
#endif
    return val;
493 494
}

T
ths 已提交
495 496
void serial_mm_writel (void *opaque,
                       target_phys_addr_t addr, uint32_t value)
497 498
{
    SerialState *s = opaque;
499 500 501
#ifdef TARGET_WORDS_BIGENDIAN
    value = bswap32(value);
#endif
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
    serial_ioport_write(s, (addr - s->base) >> s->it_shift, value);
}

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,
};

517
SerialState *serial_mm_init (target_phys_addr_t base, int it_shift,
A
aurel32 已提交
518 519
                             qemu_irq irq, int baudbase,
                             CharDriverState *chr, int ioregister)
520 521 522 523 524 525 526 527 528 529
{
    SerialState *s;
    int s_io_memory;

    s = qemu_mallocz(sizeof(SerialState));
    if (!s)
        return NULL;
    s->irq = irq;
    s->base = base;
    s->it_shift = it_shift;
A
aurel32 已提交
530
    s->baudbase= baudbase;
531

A
aurel32 已提交
532 533 534 535
    s->tx_timer = qemu_new_timer(vm_clock, serial_tx_done, s);
    if (!s->tx_timer)
        return NULL;

536 537 538
    qemu_register_reset(serial_reset, s);
    serial_reset(s);

B
bellard 已提交
539
    register_savevm("serial", base, 2, serial_save, serial_load, s);
540

T
ths 已提交
541 542 543 544 545
    if (ioregister) {
        s_io_memory = cpu_register_io_memory(0, serial_mm_read,
                                             serial_mm_write, s);
        cpu_register_physical_memory(base, 8 << it_shift, s_io_memory);
    }
546
    s->chr = chr;
547 548
    qemu_chr_add_handlers(chr, serial_can_receive1, serial_receive1,
                          serial_event, s);
549 550
    return s;
}