escc.c 25.8 KB
Newer Older
B
bellard 已提交
1
/*
2
 * QEMU ESCC (Z8030/Z8530/Z85C30/SCC/ESCC) serial port emulation
3
 *
B
bellard 已提交
4
 * Copyright (c) 2003-2005 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.
 */
B
Blue Swirl 已提交
24

P
pbrook 已提交
25
#include "hw.h"
B
Blue Swirl 已提交
26
#include "sysbus.h"
27
#include "escc.h"
P
pbrook 已提交
28 29
#include "qemu-char.h"
#include "console.h"
30
#include "trace.h"
B
bellard 已提交
31 32

/*
B
Blue Swirl 已提交
33 34 35 36
 * Chipset docs:
 * "Z80C30/Z85C30/Z80230/Z85230/Z85233 SCC/ESCC User Manual",
 * http://www.zilog.com/docs/serial/scc_escc_um.pdf
 *
37
 * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001
B
bellard 已提交
38 39
 * (Slave I/O), also produced as NCR89C105. See
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
40
 *
B
bellard 已提交
41 42 43 44
 * The serial ports implement full AMD AM8530 or Zilog Z8530 chips,
 * mouse and keyboard ports don't implement all functions and they are
 * only asynchronous. There is no DMA.
 *
45 46 47 48 49 50 51 52
 * Z85C30 is also used on PowerMacs. There are some small differences
 * between Sparc version (sunzilog) and PowerMac (pmac):
 *  Offset between control and data registers
 *  There is some kind of lockup bug, but we can ignore it
 *  CTS is inverted
 *  DMA on pmac using DBDMA chip
 *  pmac can do IRDA and faster rates, sunzilog can only do 38400
 *  pmac baud rate generator clock is 3.6864 MHz, sunzilog 4.9152 MHz
B
bellard 已提交
53 54
 */

B
bellard 已提交
55 56 57 58 59
/*
 * Modifications:
 *  2006-Aug-10  Igor Kovalenko :   Renamed KBDQueue to SERIOQueue, implemented
 *                                  serial mouse queue.
 *                                  Implemented serial mouse protocol.
60 61
 *
 *  2010-May-23  Artyom Tarasenko:  Reworked IUS logic
B
bellard 已提交
62 63
 */

B
bellard 已提交
64 65
typedef enum {
    chn_a, chn_b,
B
Blue Swirl 已提交
66
} ChnID;
B
bellard 已提交
67

68 69
#define CHN_C(s) ((s)->chn == chn_b? 'b' : 'a')

B
bellard 已提交
70 71
typedef enum {
    ser, kbd, mouse,
B
Blue Swirl 已提交
72
} ChnType;
B
bellard 已提交
73

B
bellard 已提交
74
#define SERIO_QUEUE_SIZE 256
B
bellard 已提交
75 76

typedef struct {
B
bellard 已提交
77
    uint8_t data[SERIO_QUEUE_SIZE];
B
bellard 已提交
78
    int rptr, wptr, count;
B
bellard 已提交
79
} SERIOQueue;
B
bellard 已提交
80

81
#define SERIAL_REGS 16
B
bellard 已提交
82
typedef struct ChannelState {
P
pbrook 已提交
83
    qemu_irq irq;
B
blueswir1 已提交
84
    uint32_t rxint, txint, rxint_under_svc, txint_under_svc;
B
bellard 已提交
85
    struct ChannelState *otherchn;
86 87
    uint32_t reg;
    uint8_t wregs[SERIAL_REGS], rregs[SERIAL_REGS];
B
bellard 已提交
88
    SERIOQueue queue;
B
bellard 已提交
89
    CharDriverState *chr;
90
    int e0_mode, led_mode, caps_lock_mode, num_lock_mode;
B
blueswir1 已提交
91
    int disabled;
92
    int clock;
93
    uint32_t vmstate_dummy;
94 95 96
    ChnID chn; // this channel, A (base+4) or B (base+0)
    ChnType type;
    uint8_t rx, tx;
B
bellard 已提交
97 98 99
} ChannelState;

struct SerialState {
B
Blue Swirl 已提交
100
    SysBusDevice busdev;
B
bellard 已提交
101
    struct ChannelState chn[2];
102
    uint32_t it_shift;
A
Avi Kivity 已提交
103
    MemoryRegion mmio;
G
Gerd Hoffmann 已提交
104 105
    uint32_t disabled;
    uint32_t frequency;
B
bellard 已提交
106 107
};

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
#define SERIAL_CTRL 0
#define SERIAL_DATA 1

#define W_CMD     0
#define CMD_PTR_MASK   0x07
#define CMD_CMD_MASK   0x38
#define CMD_HI         0x08
#define CMD_CLR_TXINT  0x28
#define CMD_CLR_IUS    0x38
#define W_INTR    1
#define INTR_INTALL    0x01
#define INTR_TXINT     0x02
#define INTR_RXMODEMSK 0x18
#define INTR_RXINT1ST  0x08
#define INTR_RXINTALL  0x10
#define W_IVEC    2
#define W_RXCTRL  3
#define RXCTRL_RXEN    0x01
#define W_TXCTRL1 4
#define TXCTRL1_PAREN  0x01
#define TXCTRL1_PAREV  0x02
#define TXCTRL1_1STOP  0x04
#define TXCTRL1_1HSTOP 0x08
#define TXCTRL1_2STOP  0x0c
#define TXCTRL1_STPMSK 0x0c
#define TXCTRL1_CLK1X  0x00
#define TXCTRL1_CLK16X 0x40
#define TXCTRL1_CLK32X 0x80
#define TXCTRL1_CLK64X 0xc0
#define TXCTRL1_CLKMSK 0xc0
#define W_TXCTRL2 5
#define TXCTRL2_TXEN   0x08
#define TXCTRL2_BITMSK 0x60
#define TXCTRL2_5BITS  0x00
#define TXCTRL2_7BITS  0x20
#define TXCTRL2_6BITS  0x40
#define TXCTRL2_8BITS  0x60
#define W_SYNC1   6
#define W_SYNC2   7
#define W_TXBUF   8
#define W_MINTR   9
#define MINTR_STATUSHI 0x10
#define MINTR_RST_MASK 0xc0
#define MINTR_RST_B    0x40
#define MINTR_RST_A    0x80
#define MINTR_RST_ALL  0xc0
#define W_MISC1  10
#define W_CLOCK  11
#define CLOCK_TRXC     0x08
#define W_BRGLO  12
#define W_BRGHI  13
#define W_MISC2  14
#define MISC2_PLLDIS   0x30
#define W_EXTINT 15
#define EXTINT_DCD     0x08
#define EXTINT_SYNCINT 0x10
#define EXTINT_CTSINT  0x20
#define EXTINT_TXUNDRN 0x40
#define EXTINT_BRKINT  0x80

#define R_STATUS  0
#define STATUS_RXAV    0x01
#define STATUS_ZERO    0x02
#define STATUS_TXEMPTY 0x04
#define STATUS_DCD     0x08
#define STATUS_SYNC    0x10
#define STATUS_CTS     0x20
#define STATUS_TXUNDRN 0x40
#define STATUS_BRK     0x80
#define R_SPEC    1
#define SPEC_ALLSENT   0x01
#define SPEC_BITS8     0x06
#define R_IVEC    2
#define IVEC_TXINTB    0x00
#define IVEC_LONOINT   0x06
#define IVEC_LORXINTA  0x0c
#define IVEC_LORXINTB  0x04
#define IVEC_LOTXINTA  0x08
#define IVEC_HINOINT   0x60
#define IVEC_HIRXINTA  0x30
#define IVEC_HIRXINTB  0x20
#define IVEC_HITXINTA  0x10
#define R_INTR    3
#define INTR_EXTINTB   0x01
#define INTR_TXINTB    0x02
#define INTR_RXINTB    0x04
#define INTR_EXTINTA   0x08
#define INTR_TXINTA    0x10
#define INTR_RXINTA    0x20
#define R_IPEN    4
#define R_TXCTRL1 5
#define R_TXCTRL2 6
#define R_BC      7
#define R_RXBUF   8
#define R_RXCTRL  9
#define R_MISC   10
#define R_MISC1  11
#define R_BRGLO  12
#define R_BRGHI  13
#define R_MISC1I 14
#define R_EXTINT 15
B
bellard 已提交
209

B
bellard 已提交
210 211 212 213
static void handle_kbd_command(ChannelState *s, int val);
static int serial_can_receive(void *opaque);
static void serial_receive_byte(ChannelState *s, int ch);

B
blueswir1 已提交
214 215 216 217 218 219 220
static void clear_queue(void *opaque)
{
    ChannelState *s = opaque;
    SERIOQueue *q = &s->queue;
    q->rptr = q->wptr = q->count = 0;
}

B
bellard 已提交
221 222 223
static void put_queue(void *opaque, int b)
{
    ChannelState *s = opaque;
B
bellard 已提交
224
    SERIOQueue *q = &s->queue;
B
bellard 已提交
225

226
    trace_escc_put_queue(CHN_C(s), b);
B
bellard 已提交
227
    if (q->count >= SERIO_QUEUE_SIZE)
B
bellard 已提交
228 229
        return;
    q->data[q->wptr] = b;
B
bellard 已提交
230
    if (++q->wptr == SERIO_QUEUE_SIZE)
B
bellard 已提交
231 232 233 234 235 236 237 238
        q->wptr = 0;
    q->count++;
    serial_receive_byte(s, 0);
}

static uint32_t get_queue(void *opaque)
{
    ChannelState *s = opaque;
B
bellard 已提交
239
    SERIOQueue *q = &s->queue;
B
bellard 已提交
240
    int val;
241

B
bellard 已提交
242
    if (q->count == 0) {
B
blueswir1 已提交
243
        return 0;
B
bellard 已提交
244 245
    } else {
        val = q->data[q->rptr];
B
bellard 已提交
246
        if (++q->rptr == SERIO_QUEUE_SIZE)
B
bellard 已提交
247 248 249
            q->rptr = 0;
        q->count--;
    }
250
    trace_escc_get_queue(CHN_C(s), val);
B
bellard 已提交
251
    if (q->count > 0)
B
blueswir1 已提交
252
        serial_receive_byte(s, 0);
B
bellard 已提交
253 254 255
    return val;
}

256
static int escc_update_irq_chn(ChannelState *s)
B
bellard 已提交
257
{
258
    if ((((s->wregs[W_INTR] & INTR_TXINT) && (s->txint == 1)) ||
259 260 261
         // tx ints enabled, pending
         ((((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINT1ST) ||
           ((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINTALL)) &&
B
blueswir1 已提交
262
          s->rxint == 1) || // rx ints enabled, pending
263 264
         ((s->wregs[W_EXTINT] & EXTINT_BRKINT) &&
          (s->rregs[R_STATUS] & STATUS_BRK)))) { // break int e&p
B
bellard 已提交
265
        return 1;
B
bellard 已提交
266
    }
B
bellard 已提交
267 268 269
    return 0;
}

270
static void escc_update_irq(ChannelState *s)
B
bellard 已提交
271 272 273
{
    int irq;

274 275
    irq = escc_update_irq_chn(s);
    irq |= escc_update_irq_chn(s->otherchn);
B
bellard 已提交
276

277
    trace_escc_update_irq(irq);
P
pbrook 已提交
278
    qemu_set_irq(s->irq, irq);
B
bellard 已提交
279 280
}

281
static void escc_reset_chn(ChannelState *s)
B
bellard 已提交
282 283 284 285
{
    int i;

    s->reg = 0;
B
blueswir1 已提交
286
    for (i = 0; i < SERIAL_REGS; i++) {
B
blueswir1 已提交
287 288
        s->rregs[i] = 0;
        s->wregs[i] = 0;
B
bellard 已提交
289
    }
290 291 292 293 294 295
    s->wregs[W_TXCTRL1] = TXCTRL1_1STOP; // 1X divisor, 1 stop bit, no parity
    s->wregs[W_MINTR] = MINTR_RST_ALL;
    s->wregs[W_CLOCK] = CLOCK_TRXC; // Synch mode tx clock = TRxC
    s->wregs[W_MISC2] = MISC2_PLLDIS; // PLL disabled
    s->wregs[W_EXTINT] = EXTINT_DCD | EXTINT_SYNCINT | EXTINT_CTSINT |
        EXTINT_TXUNDRN | EXTINT_BRKINT; // Enable most interrupts
B
blueswir1 已提交
296
    if (s->disabled)
297 298
        s->rregs[R_STATUS] = STATUS_TXEMPTY | STATUS_DCD | STATUS_SYNC |
            STATUS_CTS | STATUS_TXUNDRN;
B
blueswir1 已提交
299
    else
300
        s->rregs[R_STATUS] = STATUS_TXEMPTY | STATUS_TXUNDRN;
B
blueswir1 已提交
301
    s->rregs[R_SPEC] = SPEC_BITS8 | SPEC_ALLSENT;
B
bellard 已提交
302 303 304

    s->rx = s->tx = 0;
    s->rxint = s->txint = 0;
B
bellard 已提交
305
    s->rxint_under_svc = s->txint_under_svc = 0;
306
    s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0;
B
blueswir1 已提交
307
    clear_queue(s);
B
bellard 已提交
308 309
}

310
static void escc_reset(DeviceState *d)
B
bellard 已提交
311
{
312 313
    SerialState *s = container_of(d, SerialState, busdev.qdev);

314 315
    escc_reset_chn(&s->chn[0]);
    escc_reset_chn(&s->chn[1]);
B
bellard 已提交
316 317
}

B
bellard 已提交
318 319 320
static inline void set_rxint(ChannelState *s)
{
    s->rxint = 1;
321 322 323 324
    /* XXX: missing daisy chainnig: chn_b rx should have a lower priority
       than chn_a rx/tx/special_condition service*/
    s->rxint_under_svc = 1;
    if (s->chn == chn_a) {
325
        s->rregs[R_INTR] |= INTR_RXINTA;
326 327 328 329 330
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->otherchn->rregs[R_IVEC] = IVEC_HIRXINTA;
        else
            s->otherchn->rregs[R_IVEC] = IVEC_LORXINTA;
    } else {
331
        s->otherchn->rregs[R_INTR] |= INTR_RXINTB;
332 333 334 335 336
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->rregs[R_IVEC] = IVEC_HIRXINTB;
        else
            s->rregs[R_IVEC] = IVEC_LORXINTB;
    }
337
    escc_update_irq(s);
B
bellard 已提交
338 339
}

340 341 342 343 344 345
static inline void set_txint(ChannelState *s)
{
    s->txint = 1;
    if (!s->rxint_under_svc) {
        s->txint_under_svc = 1;
        if (s->chn == chn_a) {
A
Aurelien Jarno 已提交
346 347 348
            if (s->wregs[W_INTR] & INTR_TXINT) {
                s->rregs[R_INTR] |= INTR_TXINTA;
            }
349 350 351 352 353 354
            if (s->wregs[W_MINTR] & MINTR_STATUSHI)
                s->otherchn->rregs[R_IVEC] = IVEC_HITXINTA;
            else
                s->otherchn->rregs[R_IVEC] = IVEC_LOTXINTA;
        } else {
            s->rregs[R_IVEC] = IVEC_TXINTB;
A
Aurelien Jarno 已提交
355 356 357
            if (s->wregs[W_INTR] & INTR_TXINT) {
                s->otherchn->rregs[R_INTR] |= INTR_TXINTB;
            }
358
        }
359
    escc_update_irq(s);
360
    }
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
}

static inline void clr_rxint(ChannelState *s)
{
    s->rxint = 0;
    s->rxint_under_svc = 0;
    if (s->chn == chn_a) {
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
        else
            s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
        s->rregs[R_INTR] &= ~INTR_RXINTA;
    } else {
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->rregs[R_IVEC] = IVEC_HINOINT;
        else
            s->rregs[R_IVEC] = IVEC_LONOINT;
        s->otherchn->rregs[R_INTR] &= ~INTR_RXINTB;
    }
    if (s->txint)
        set_txint(s);
382
    escc_update_irq(s);
383 384
}

B
bellard 已提交
385 386 387
static inline void clr_txint(ChannelState *s)
{
    s->txint = 0;
B
bellard 已提交
388
    s->txint_under_svc = 0;
B
blueswir1 已提交
389
    if (s->chn == chn_a) {
390 391
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
B
blueswir1 已提交
392
        else
393 394
            s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
        s->rregs[R_INTR] &= ~INTR_TXINTA;
B
blueswir1 已提交
395
    } else {
396
        s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
397 398
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->rregs[R_IVEC] = IVEC_HINOINT;
B
blueswir1 已提交
399
        else
400 401
            s->rregs[R_IVEC] = IVEC_LONOINT;
        s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
B
blueswir1 已提交
402
    }
B
bellard 已提交
403 404
    if (s->rxint)
        set_rxint(s);
405
    escc_update_irq(s);
B
bellard 已提交
406 407
}

408
static void escc_update_parameters(ChannelState *s)
409 410 411 412 413 414 415
{
    int speed, parity, data_bits, stop_bits;
    QEMUSerialSetParams ssp;

    if (!s->chr || s->type != ser)
        return;

416 417
    if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREN) {
        if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREV)
418 419 420 421 422 423
            parity = 'E';
        else
            parity = 'O';
    } else {
        parity = 'N';
    }
424
    if ((s->wregs[W_TXCTRL1] & TXCTRL1_STPMSK) == TXCTRL1_2STOP)
425 426 427
        stop_bits = 2;
    else
        stop_bits = 1;
428 429
    switch (s->wregs[W_TXCTRL2] & TXCTRL2_BITMSK) {
    case TXCTRL2_5BITS:
430 431
        data_bits = 5;
        break;
432
    case TXCTRL2_7BITS:
433 434
        data_bits = 7;
        break;
435
    case TXCTRL2_6BITS:
436 437 438
        data_bits = 6;
        break;
    default:
439
    case TXCTRL2_8BITS:
440 441 442
        data_bits = 8;
        break;
    }
443
    speed = s->clock / ((s->wregs[W_BRGLO] | (s->wregs[W_BRGHI] << 8)) + 2);
444 445
    switch (s->wregs[W_TXCTRL1] & TXCTRL1_CLKMSK) {
    case TXCTRL1_CLK1X:
446
        break;
447
    case TXCTRL1_CLK16X:
448 449
        speed /= 16;
        break;
450
    case TXCTRL1_CLK32X:
451 452 453
        speed /= 32;
        break;
    default:
454
    case TXCTRL1_CLK64X:
455 456 457 458 459 460 461
        speed /= 64;
        break;
    }
    ssp.speed = speed;
    ssp.parity = parity;
    ssp.data_bits = data_bits;
    ssp.stop_bits = stop_bits;
462
    trace_escc_update_parameters(CHN_C(s), speed, parity, data_bits, stop_bits);
463 464 465
    qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
}

A
Avi Kivity 已提交
466 467
static void escc_mem_write(void *opaque, target_phys_addr_t addr,
                           uint64_t val, unsigned size)
B
bellard 已提交
468
{
469
    SerialState *serial = opaque;
B
bellard 已提交
470 471 472 473 474
    ChannelState *s;
    uint32_t saddr;
    int newreg, channel;

    val &= 0xff;
475 476
    saddr = (addr >> serial->it_shift) & 1;
    channel = (addr >> (serial->it_shift + 1)) & 1;
477
    s = &serial->chn[channel];
B
bellard 已提交
478
    switch (saddr) {
479
    case SERIAL_CTRL:
480
        trace_escc_mem_writeb_ctrl(CHN_C(s), s->reg, val & 0xff);
B
blueswir1 已提交
481 482
        newreg = 0;
        switch (s->reg) {
483 484 485
        case W_CMD:
            newreg = val & CMD_PTR_MASK;
            val &= CMD_CMD_MASK;
B
blueswir1 已提交
486
            switch (val) {
487 488
            case CMD_HI:
                newreg |= CMD_HI;
B
blueswir1 已提交
489
                break;
490
            case CMD_CLR_TXINT:
B
bellard 已提交
491
                clr_txint(s);
B
blueswir1 已提交
492
                break;
493
            case CMD_CLR_IUS:
494 495 496 497 498 499 500 501 502
                if (s->rxint_under_svc) {
                    s->rxint_under_svc = 0;
                    if (s->txint) {
                        set_txint(s);
                    }
                } else if (s->txint_under_svc) {
                    s->txint_under_svc = 0;
                }
                escc_update_irq(s);
B
blueswir1 已提交
503 504 505 506 507
                break;
            default:
                break;
            }
            break;
508 509 510 511
        case W_INTR ... W_RXCTRL:
        case W_SYNC1 ... W_TXBUF:
        case W_MISC1 ... W_CLOCK:
        case W_MISC2 ... W_EXTINT:
B
blueswir1 已提交
512 513
            s->wregs[s->reg] = val;
            break;
514 515
        case W_TXCTRL1:
        case W_TXCTRL2:
B
blueswir1 已提交
516
            s->wregs[s->reg] = val;
517
            escc_update_parameters(s);
B
blueswir1 已提交
518
            break;
519 520
        case W_BRGLO:
        case W_BRGHI:
B
blueswir1 已提交
521
            s->wregs[s->reg] = val;
B
blueswir1 已提交
522
            s->rregs[s->reg] = val;
523
            escc_update_parameters(s);
B
blueswir1 已提交
524
            break;
525 526
        case W_MINTR:
            switch (val & MINTR_RST_MASK) {
B
blueswir1 已提交
527 528 529
            case 0:
            default:
                break;
530
            case MINTR_RST_B:
531
                escc_reset_chn(&serial->chn[0]);
B
blueswir1 已提交
532
                return;
533
            case MINTR_RST_A:
534
                escc_reset_chn(&serial->chn[1]);
B
blueswir1 已提交
535
                return;
536
            case MINTR_RST_ALL:
537
                escc_reset(&serial->busdev.qdev);
B
blueswir1 已提交
538 539 540 541 542 543 544 545 546 547 548
                return;
            }
            break;
        default:
            break;
        }
        if (s->reg == 0)
            s->reg = newreg;
        else
            s->reg = 0;
        break;
549
    case SERIAL_DATA:
550
        trace_escc_mem_writeb_data(CHN_C(s), val);
551
        s->tx = val;
552
        if (s->wregs[W_TXCTRL2] & TXCTRL2_TXEN) { // tx enabled
B
blueswir1 已提交
553
            if (s->chr)
554
                qemu_chr_fe_write(s->chr, &s->tx, 1);
B
blueswir1 已提交
555
            else if (s->type == kbd && !s->disabled) {
B
blueswir1 已提交
556 557 558
                handle_kbd_command(s, val);
            }
        }
559 560
        s->rregs[R_STATUS] |= STATUS_TXEMPTY; // Tx buffer empty
        s->rregs[R_SPEC] |= SPEC_ALLSENT; // All sent
561
        set_txint(s);
B
blueswir1 已提交
562
        break;
B
bellard 已提交
563
    default:
B
blueswir1 已提交
564
        break;
B
bellard 已提交
565 566 567
    }
}

A
Avi Kivity 已提交
568 569
static uint64_t escc_mem_read(void *opaque, target_phys_addr_t addr,
                              unsigned size)
B
bellard 已提交
570
{
571
    SerialState *serial = opaque;
B
bellard 已提交
572 573 574 575 576
    ChannelState *s;
    uint32_t saddr;
    uint32_t ret;
    int channel;

577 578
    saddr = (addr >> serial->it_shift) & 1;
    channel = (addr >> (serial->it_shift + 1)) & 1;
579
    s = &serial->chn[channel];
B
bellard 已提交
580
    switch (saddr) {
581
    case SERIAL_CTRL:
582
        trace_escc_mem_readb_ctrl(CHN_C(s), s->reg, s->rregs[s->reg]);
B
blueswir1 已提交
583 584 585
        ret = s->rregs[s->reg];
        s->reg = 0;
        return ret;
586 587
    case SERIAL_DATA:
        s->rregs[R_STATUS] &= ~STATUS_RXAV;
B
bellard 已提交
588
        clr_rxint(s);
B
blueswir1 已提交
589 590 591 592
        if (s->type == kbd || s->type == mouse)
            ret = get_queue(s);
        else
            ret = s->rx;
593
        trace_escc_mem_readb_data(CHN_C(s), ret);
594 595
        if (s->chr)
            qemu_chr_accept_input(s->chr);
B
blueswir1 已提交
596
        return ret;
B
bellard 已提交
597
    default:
B
blueswir1 已提交
598
        break;
B
bellard 已提交
599 600 601 602
    }
    return 0;
}

A
Avi Kivity 已提交
603 604 605 606 607 608 609 610 611 612
static const MemoryRegionOps escc_mem_ops = {
    .read = escc_mem_read,
    .write = escc_mem_write,
    .endianness = DEVICE_NATIVE_ENDIAN,
    .valid = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
};

B
bellard 已提交
613 614 615
static int serial_can_receive(void *opaque)
{
    ChannelState *s = opaque;
B
bellard 已提交
616 617
    int ret;

618 619 620
    if (((s->wregs[W_RXCTRL] & RXCTRL_RXEN) == 0) // Rx not enabled
        || ((s->rregs[R_STATUS] & STATUS_RXAV) == STATUS_RXAV))
        // char already available
B
blueswir1 已提交
621
        ret = 0;
B
bellard 已提交
622
    else
B
blueswir1 已提交
623
        ret = 1;
B
bellard 已提交
624
    return ret;
B
bellard 已提交
625 626 627 628
}

static void serial_receive_byte(ChannelState *s, int ch)
{
629
    trace_escc_serial_receive_byte(CHN_C(s), ch);
630
    s->rregs[R_STATUS] |= STATUS_RXAV;
B
bellard 已提交
631
    s->rx = ch;
B
bellard 已提交
632
    set_rxint(s);
B
bellard 已提交
633 634 635 636
}

static void serial_receive_break(ChannelState *s)
{
637
    s->rregs[R_STATUS] |= STATUS_BRK;
638
    escc_update_irq(s);
B
bellard 已提交
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
}

static void serial_receive1(void *opaque, const uint8_t *buf, int size)
{
    ChannelState *s = opaque;
    serial_receive_byte(s, buf[0]);
}

static void serial_event(void *opaque, int event)
{
    ChannelState *s = opaque;
    if (event == CHR_EVENT_BREAK)
        serial_receive_break(s);
}

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
static const VMStateDescription vmstate_escc_chn = {
    .name ="escc_chn",
    .version_id = 2,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField []) {
        VMSTATE_UINT32(vmstate_dummy, ChannelState),
        VMSTATE_UINT32(reg, ChannelState),
        VMSTATE_UINT32(rxint, ChannelState),
        VMSTATE_UINT32(txint, ChannelState),
        VMSTATE_UINT32(rxint_under_svc, ChannelState),
        VMSTATE_UINT32(txint_under_svc, ChannelState),
        VMSTATE_UINT8(rx, ChannelState),
        VMSTATE_UINT8(tx, ChannelState),
        VMSTATE_BUFFER(wregs, ChannelState),
        VMSTATE_BUFFER(rregs, ChannelState),
        VMSTATE_END_OF_LIST()
B
bellard 已提交
671
    }
672
};
B
bellard 已提交
673

674 675 676 677 678 679 680 681 682 683 684
static const VMStateDescription vmstate_escc = {
    .name ="escc",
    .version_id = 2,
    .minimum_version_id = 1,
    .minimum_version_id_old = 1,
    .fields      = (VMStateField []) {
        VMSTATE_STRUCT_ARRAY(chn, SerialState, 2, 2, vmstate_escc_chn,
                             ChannelState),
        VMSTATE_END_OF_LIST()
    }
};
B
bellard 已提交
685

A
Avi Kivity 已提交
686
MemoryRegion *escc_init(target_phys_addr_t base, qemu_irq irqA, qemu_irq irqB,
A
aurel32 已提交
687 688
              CharDriverState *chrA, CharDriverState *chrB,
              int clock, int it_shift)
B
bellard 已提交
689
{
B
Blue Swirl 已提交
690 691 692 693 694
    DeviceState *dev;
    SysBusDevice *s;
    SerialState *d;

    dev = qdev_create(NULL, "escc");
G
Gerd Hoffmann 已提交
695 696 697
    qdev_prop_set_uint32(dev, "disabled", 0);
    qdev_prop_set_uint32(dev, "frequency", clock);
    qdev_prop_set_uint32(dev, "it_shift", it_shift);
B
Blue Swirl 已提交
698 699
    qdev_prop_set_chr(dev, "chrB", chrB);
    qdev_prop_set_chr(dev, "chrA", chrA);
G
Gerd Hoffmann 已提交
700 701
    qdev_prop_set_uint32(dev, "chnBtype", ser);
    qdev_prop_set_uint32(dev, "chnAtype", ser);
M
Markus Armbruster 已提交
702
    qdev_init_nofail(dev);
B
Blue Swirl 已提交
703
    s = sysbus_from_qdev(dev);
704 705
    sysbus_connect_irq(s, 0, irqB);
    sysbus_connect_irq(s, 1, irqA);
B
Blue Swirl 已提交
706 707
    if (base) {
        sysbus_mmio_map(s, 0, base);
B
bellard 已提交
708
    }
B
Blue Swirl 已提交
709 710

    d = FROM_SYSBUS(SerialState, s);
A
Avi Kivity 已提交
711
    return &d->mmio;
B
bellard 已提交
712 713
}

B
bellard 已提交
714 715 716 717 718 719 720 721 722 723 724
static const uint8_t keycodes[128] = {
    127, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 43, 53,
    54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 89, 76, 77, 78,
    79, 80, 81, 82, 83, 84, 85, 86, 87, 42, 99, 88, 100, 101, 102, 103,
    104, 105, 106, 107, 108, 109, 110, 47, 19, 121, 119, 5, 6, 8, 10, 12,
    14, 16, 17, 18, 7, 98, 23, 68, 69, 70, 71, 91, 92, 93, 125, 112,
    113, 114, 94, 50, 0, 0, 124, 9, 11, 0, 0, 0, 0, 0, 0, 0,
    90, 0, 46, 22, 13, 111, 52, 20, 96, 24, 28, 74, 27, 123, 44, 66,
    0, 45, 2, 4, 48, 0, 0, 21, 0, 0, 0, 0, 0, 120, 122, 67,
};

B
blueswir1 已提交
725 726 727 728 729 730 731 732
static const uint8_t e0_keycodes[128] = {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 76, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 109, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 68, 69, 70, 0, 91, 0, 93, 0, 112,
    113, 114, 94, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
733
    1, 3, 25, 26, 49, 52, 72, 73, 97, 99, 111, 118, 120, 122, 67, 0,
B
blueswir1 已提交
734 735
};

B
bellard 已提交
736 737 738
static void sunkbd_event(void *opaque, int ch)
{
    ChannelState *s = opaque;
B
bellard 已提交
739 740
    int release = ch & 0x80;

741
    trace_escc_sunkbd_event_in(ch);
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    switch (ch) {
    case 58: // Caps lock press
        s->caps_lock_mode ^= 1;
        if (s->caps_lock_mode == 2)
            return; // Drop second press
        break;
    case 69: // Num lock press
        s->num_lock_mode ^= 1;
        if (s->num_lock_mode == 2)
            return; // Drop second press
        break;
    case 186: // Caps lock release
        s->caps_lock_mode ^= 2;
        if (s->caps_lock_mode == 3)
            return; // Drop first release
        break;
    case 197: // Num lock release
        s->num_lock_mode ^= 2;
        if (s->num_lock_mode == 3)
            return; // Drop first release
        break;
    case 0xe0:
B
blueswir1 已提交
764 765
        s->e0_mode = 1;
        return;
766 767
    default:
        break;
B
blueswir1 已提交
768 769 770 771 772 773 774
    }
    if (s->e0_mode) {
        s->e0_mode = 0;
        ch = e0_keycodes[ch & 0x7f];
    } else {
        ch = keycodes[ch & 0x7f];
    }
775
    trace_escc_sunkbd_event_out(ch);
B
bellard 已提交
776 777 778 779 780
    put_queue(s, ch | release);
}

static void handle_kbd_command(ChannelState *s, int val)
{
781
    trace_escc_kbd_command(val);
B
blueswir1 已提交
782 783 784 785
    if (s->led_mode) { // Ignore led byte
        s->led_mode = 0;
        return;
    }
B
bellard 已提交
786 787
    switch (val) {
    case 1: // Reset, return type code
B
blueswir1 已提交
788
        clear_queue(s);
B
blueswir1 已提交
789 790 791 792
        put_queue(s, 0xff);
        put_queue(s, 4); // Type 4
        put_queue(s, 0x7f);
        break;
B
blueswir1 已提交
793 794 795
    case 0xe: // Set leds
        s->led_mode = 1;
        break;
B
bellard 已提交
796
    case 7: // Query layout
B
blueswir1 已提交
797 798
    case 0xf:
        clear_queue(s);
B
blueswir1 已提交
799 800 801
        put_queue(s, 0xfe);
        put_queue(s, 0); // XXX, layout?
        break;
B
bellard 已提交
802
    default:
B
blueswir1 已提交
803
        break;
B
bellard 已提交
804
    }
B
bellard 已提交
805 806
}

807
static void sunmouse_event(void *opaque,
B
bellard 已提交
808 809 810 811 812
                               int dx, int dy, int dz, int buttons_state)
{
    ChannelState *s = opaque;
    int ch;

813
    trace_escc_sunmouse_event(dx, dy, buttons_state);
B
bellard 已提交
814 815 816 817 818 819 820 821 822 823 824 825 826 827
    ch = 0x80 | 0x7; /* protocol start byte, no buttons pressed */

    if (buttons_state & MOUSE_EVENT_LBUTTON)
        ch ^= 0x4;
    if (buttons_state & MOUSE_EVENT_MBUTTON)
        ch ^= 0x2;
    if (buttons_state & MOUSE_EVENT_RBUTTON)
        ch ^= 0x1;

    put_queue(s, ch);

    ch = dx;

    if (ch > 127)
M
Michael S. Tsirkin 已提交
828
        ch = 127;
B
bellard 已提交
829
    else if (ch < -127)
M
Michael S. Tsirkin 已提交
830
        ch = -127;
B
bellard 已提交
831 832 833 834 835 836

    put_queue(s, ch & 0xff);

    ch = -dy;

    if (ch > 127)
837
        ch = 127;
B
bellard 已提交
838
    else if (ch < -127)
839
        ch = -127;
B
bellard 已提交
840 841 842 843 844 845 846

    put_queue(s, ch & 0xff);

    // MSC protocol specify two extra motion bytes

    put_queue(s, 0);
    put_queue(s, 0);
B
bellard 已提交
847 848
}

A
Anthony Liguori 已提交
849
void slavio_serial_ms_kbd_init(target_phys_addr_t base, qemu_irq irq,
850
                               int disabled, int clock, int it_shift)
B
bellard 已提交
851
{
B
Blue Swirl 已提交
852 853 854 855
    DeviceState *dev;
    SysBusDevice *s;

    dev = qdev_create(NULL, "escc");
G
Gerd Hoffmann 已提交
856 857 858
    qdev_prop_set_uint32(dev, "disabled", disabled);
    qdev_prop_set_uint32(dev, "frequency", clock);
    qdev_prop_set_uint32(dev, "it_shift", it_shift);
B
Blue Swirl 已提交
859 860
    qdev_prop_set_chr(dev, "chrB", NULL);
    qdev_prop_set_chr(dev, "chrA", NULL);
G
Gerd Hoffmann 已提交
861 862
    qdev_prop_set_uint32(dev, "chnBtype", mouse);
    qdev_prop_set_uint32(dev, "chnAtype", kbd);
M
Markus Armbruster 已提交
863
    qdev_init_nofail(dev);
B
Blue Swirl 已提交
864 865 866 867 868
    s = sysbus_from_qdev(dev);
    sysbus_connect_irq(s, 0, irq);
    sysbus_connect_irq(s, 1, irq);
    sysbus_mmio_map(s, 0, base);
}
869

870
static int escc_init1(SysBusDevice *dev)
B
Blue Swirl 已提交
871 872 873
{
    SerialState *s = FROM_SYSBUS(SerialState, dev);
    unsigned int i;
G
Gerd Hoffmann 已提交
874 875 876

    s->chn[0].disabled = s->disabled;
    s->chn[1].disabled = s->disabled;
B
bellard 已提交
877
    for (i = 0; i < 2; i++) {
B
Blue Swirl 已提交
878
        sysbus_init_irq(dev, &s->chn[i].irq);
B
blueswir1 已提交
879
        s->chn[i].chn = 1 - i;
G
Gerd Hoffmann 已提交
880
        s->chn[i].clock = s->frequency / 2;
B
Blue Swirl 已提交
881 882 883 884
        if (s->chn[i].chr) {
            qemu_chr_add_handlers(s->chn[i].chr, serial_can_receive,
                                  serial_receive1, serial_event, &s->chn[i]);
        }
B
bellard 已提交
885 886 887
    }
    s->chn[0].otherchn = &s->chn[1];
    s->chn[1].otherchn = &s->chn[0];
B
bellard 已提交
888

A
Avi Kivity 已提交
889 890 891
    memory_region_init_io(&s->mmio, &escc_mem_ops, s, "escc",
                          ESCC_SIZE << s->it_shift);
    sysbus_init_mmio_region(dev, &s->mmio);
B
bellard 已提交
892

B
Blue Swirl 已提交
893 894 895 896 897 898 899
    if (s->chn[0].type == mouse) {
        qemu_add_mouse_event_handler(sunmouse_event, &s->chn[0], 0,
                                     "QEMU Sun Mouse");
    }
    if (s->chn[1].type == kbd) {
        qemu_add_kbd_event_handler(sunkbd_event, &s->chn[1]);
    }
900

901
    return 0;
B
bellard 已提交
902
}
B
Blue Swirl 已提交
903 904 905 906 907

static SysBusDeviceInfo escc_info = {
    .init = escc_init1,
    .qdev.name  = "escc",
    .qdev.size  = sizeof(SerialState),
908 909
    .qdev.vmsd  = &vmstate_escc,
    .qdev.reset = escc_reset,
G
Gerd Hoffmann 已提交
910
    .qdev.props = (Property[]) {
911 912 913 914 915 916 917 918 919
        DEFINE_PROP_UINT32("frequency", SerialState, frequency,   0),
        DEFINE_PROP_UINT32("it_shift",  SerialState, it_shift,    0),
        DEFINE_PROP_UINT32("disabled",  SerialState, disabled,    0),
        DEFINE_PROP_UINT32("disabled",  SerialState, disabled,    0),
        DEFINE_PROP_UINT32("chnBtype",  SerialState, chn[0].type, 0),
        DEFINE_PROP_UINT32("chnAtype",  SerialState, chn[1].type, 0),
        DEFINE_PROP_CHR("chrB", SerialState, chn[0].chr),
        DEFINE_PROP_CHR("chrA", SerialState, chn[1].chr),
        DEFINE_PROP_END_OF_LIST(),
B
Blue Swirl 已提交
920 921 922 923 924 925 926 927 928
    }
};

static void escc_register_devices(void)
{
    sysbus_register_withprop(&escc_info);
}

device_init(escc_register_devices)