escc.c 26.7 KB
Newer Older
bellard's avatar
bellard committed
1
/*
2
 * QEMU ESCC (Z8030/Z8530/Z85C30/SCC/ESCC) serial port emulation
3
 *
bellard's avatar
bellard committed
4
 * Copyright (c) 2003-2005 Fabrice Bellard
5
 *
bellard's avatar
bellard committed
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.
 */
24

pbrook's avatar
pbrook committed
25
#include "hw.h"
26
#include "sysbus.h"
27
#include "escc.h"
pbrook's avatar
pbrook committed
28 29 30
#include "qemu-char.h"
#include "console.h"

bellard's avatar
bellard committed
31
/* debug serial */
bellard's avatar
bellard committed
32 33 34 35 36
//#define DEBUG_SERIAL

/* debug keyboard */
//#define DEBUG_KBD

bellard's avatar
bellard committed
37
/* debug mouse */
bellard's avatar
bellard committed
38 39 40
//#define DEBUG_MOUSE

/*
Blue Swirl's avatar
Blue Swirl committed
41 42 43 44
 * Chipset docs:
 * "Z80C30/Z85C30/Z80230/Z85230/Z85233 SCC/ESCC User Manual",
 * http://www.zilog.com/docs/serial/scc_escc_um.pdf
 *
45
 * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001
bellard's avatar
bellard committed
46 47
 * (Slave I/O), also produced as NCR89C105. See
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
48
 *
bellard's avatar
bellard committed
49 50 51 52
 * 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.
 *
53 54 55 56 57 58 59 60
 * 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
bellard's avatar
bellard committed
61 62
 */

63 64 65 66 67
/*
 * Modifications:
 *  2006-Aug-10  Igor Kovalenko :   Renamed KBDQueue to SERIOQueue, implemented
 *                                  serial mouse queue.
 *                                  Implemented serial mouse protocol.
68 69
 *
 *  2010-May-23  Artyom Tarasenko:  Reworked IUS logic
70 71
 */

bellard's avatar
bellard committed
72
#ifdef DEBUG_SERIAL
73 74
#define SER_DPRINTF(fmt, ...)                                   \
    do { printf("SER: " fmt , ## __VA_ARGS__); } while (0)
bellard's avatar
bellard committed
75
#else
76
#define SER_DPRINTF(fmt, ...)
bellard's avatar
bellard committed
77 78
#endif
#ifdef DEBUG_KBD
79 80
#define KBD_DPRINTF(fmt, ...)                                   \
    do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0)
bellard's avatar
bellard committed
81
#else
82
#define KBD_DPRINTF(fmt, ...)
bellard's avatar
bellard committed
83 84
#endif
#ifdef DEBUG_MOUSE
85 86
#define MS_DPRINTF(fmt, ...)                                    \
    do { printf("MSC: " fmt , ## __VA_ARGS__); } while (0)
bellard's avatar
bellard committed
87
#else
88
#define MS_DPRINTF(fmt, ...)
bellard's avatar
bellard committed
89 90 91 92
#endif

typedef enum {
    chn_a, chn_b,
93
} ChnID;
bellard's avatar
bellard committed
94

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

bellard's avatar
bellard committed
97 98
typedef enum {
    ser, kbd, mouse,
99
} ChnType;
bellard's avatar
bellard committed
100

101
#define SERIO_QUEUE_SIZE 256
bellard's avatar
bellard committed
102 103

typedef struct {
104
    uint8_t data[SERIO_QUEUE_SIZE];
bellard's avatar
bellard committed
105
    int rptr, wptr, count;
106
} SERIOQueue;
bellard's avatar
bellard committed
107

108
#define SERIAL_REGS 16
bellard's avatar
bellard committed
109
typedef struct ChannelState {
pbrook's avatar
pbrook committed
110
    qemu_irq irq;
blueswir1's avatar
blueswir1 committed
111 112
    uint32_t reg;
    uint32_t rxint, txint, rxint_under_svc, txint_under_svc;
113 114
    ChnID chn; // this channel, A (base+4) or B (base+0)
    ChnType type;
bellard's avatar
bellard committed
115
    struct ChannelState *otherchn;
116
    uint8_t rx, tx, wregs[SERIAL_REGS], rregs[SERIAL_REGS];
117
    SERIOQueue queue;
bellard's avatar
bellard committed
118
    CharDriverState *chr;
119
    int e0_mode, led_mode, caps_lock_mode, num_lock_mode;
120
    int disabled;
121
    int clock;
122
    uint32_t vmstate_dummy;
bellard's avatar
bellard committed
123 124 125
} ChannelState;

struct SerialState {
126
    SysBusDevice busdev;
bellard's avatar
bellard committed
127
    struct ChannelState chn[2];
128
    uint32_t it_shift;
129
    int mmio_index;
130 131
    uint32_t disabled;
    uint32_t frequency;
bellard's avatar
bellard committed
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#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
bellard's avatar
bellard committed
235

bellard's avatar
bellard committed
236 237 238 239
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);

blueswir1's avatar
blueswir1 committed
240 241 242 243 244 245 246
static void clear_queue(void *opaque)
{
    ChannelState *s = opaque;
    SERIOQueue *q = &s->queue;
    q->rptr = q->wptr = q->count = 0;
}

bellard's avatar
bellard committed
247 248 249
static void put_queue(void *opaque, int b)
{
    ChannelState *s = opaque;
250
    SERIOQueue *q = &s->queue;
bellard's avatar
bellard committed
251

252
    SER_DPRINTF("channel %c put: 0x%02x\n", CHN_C(s), b);
253
    if (q->count >= SERIO_QUEUE_SIZE)
bellard's avatar
bellard committed
254 255
        return;
    q->data[q->wptr] = b;
256
    if (++q->wptr == SERIO_QUEUE_SIZE)
bellard's avatar
bellard committed
257 258 259 260 261 262 263 264
        q->wptr = 0;
    q->count++;
    serial_receive_byte(s, 0);
}

static uint32_t get_queue(void *opaque)
{
    ChannelState *s = opaque;
265
    SERIOQueue *q = &s->queue;
bellard's avatar
bellard committed
266
    int val;
267

bellard's avatar
bellard committed
268
    if (q->count == 0) {
blueswir1's avatar
blueswir1 committed
269
        return 0;
bellard's avatar
bellard committed
270 271
    } else {
        val = q->data[q->rptr];
272
        if (++q->rptr == SERIO_QUEUE_SIZE)
bellard's avatar
bellard committed
273 274 275
            q->rptr = 0;
        q->count--;
    }
blueswir1's avatar
blueswir1 committed
276
    SER_DPRINTF("channel %c get 0x%02x\n", CHN_C(s), val);
bellard's avatar
bellard committed
277
    if (q->count > 0)
blueswir1's avatar
blueswir1 committed
278
        serial_receive_byte(s, 0);
bellard's avatar
bellard committed
279 280 281
    return val;
}

282
static int escc_update_irq_chn(ChannelState *s)
bellard's avatar
bellard committed
283
{
284
    if ((((s->wregs[W_INTR] & INTR_TXINT) && (s->txint == 1)) ||
285 286 287
         // tx ints enabled, pending
         ((((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINT1ST) ||
           ((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINTALL)) &&
blueswir1's avatar
blueswir1 committed
288
          s->rxint == 1) || // rx ints enabled, pending
289 290
         ((s->wregs[W_EXTINT] & EXTINT_BRKINT) &&
          (s->rregs[R_STATUS] & STATUS_BRK)))) { // break int e&p
bellard's avatar
bellard committed
291
        return 1;
bellard's avatar
bellard committed
292
    }
bellard's avatar
bellard committed
293 294 295
    return 0;
}

296
static void escc_update_irq(ChannelState *s)
bellard's avatar
bellard committed
297 298 299
{
    int irq;

300 301
    irq = escc_update_irq_chn(s);
    irq |= escc_update_irq_chn(s->otherchn);
bellard's avatar
bellard committed
302

pbrook's avatar
pbrook committed
303 304
    SER_DPRINTF("IRQ = %d\n", irq);
    qemu_set_irq(s->irq, irq);
bellard's avatar
bellard committed
305 306
}

307
static void escc_reset_chn(ChannelState *s)
bellard's avatar
bellard committed
308 309 310 311
{
    int i;

    s->reg = 0;
blueswir1's avatar
blueswir1 committed
312
    for (i = 0; i < SERIAL_REGS; i++) {
blueswir1's avatar
blueswir1 committed
313 314
        s->rregs[i] = 0;
        s->wregs[i] = 0;
bellard's avatar
bellard committed
315
    }
316 317 318 319 320 321
    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
322
    if (s->disabled)
323 324
        s->rregs[R_STATUS] = STATUS_TXEMPTY | STATUS_DCD | STATUS_SYNC |
            STATUS_CTS | STATUS_TXUNDRN;
325
    else
326
        s->rregs[R_STATUS] = STATUS_TXEMPTY | STATUS_TXUNDRN;
327
    s->rregs[R_SPEC] = SPEC_BITS8 | SPEC_ALLSENT;
bellard's avatar
bellard committed
328 329 330

    s->rx = s->tx = 0;
    s->rxint = s->txint = 0;
bellard's avatar
bellard committed
331
    s->rxint_under_svc = s->txint_under_svc = 0;
332
    s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0;
blueswir1's avatar
blueswir1 committed
333
    clear_queue(s);
bellard's avatar
bellard committed
334 335
}

336
static void escc_reset(DeviceState *d)
bellard's avatar
bellard committed
337
{
338 339
    SerialState *s = container_of(d, SerialState, busdev.qdev);

340 341
    escc_reset_chn(&s->chn[0]);
    escc_reset_chn(&s->chn[1]);
bellard's avatar
bellard committed
342 343
}

344 345 346
static inline void set_rxint(ChannelState *s)
{
    s->rxint = 1;
347 348 349 350
    /* 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) {
351
        s->rregs[R_INTR] |= INTR_RXINTA;
352 353 354 355 356
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->otherchn->rregs[R_IVEC] = IVEC_HIRXINTA;
        else
            s->otherchn->rregs[R_IVEC] = IVEC_LORXINTA;
    } else {
357
        s->otherchn->rregs[R_INTR] |= INTR_RXINTB;
358 359 360 361 362
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->rregs[R_IVEC] = IVEC_HIRXINTB;
        else
            s->rregs[R_IVEC] = IVEC_LORXINTB;
    }
363
    escc_update_irq(s);
364 365
}

366 367 368 369 370 371
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) {
Aurelien Jarno's avatar
Aurelien Jarno committed
372 373 374
            if (s->wregs[W_INTR] & INTR_TXINT) {
                s->rregs[R_INTR] |= INTR_TXINTA;
            }
375 376 377 378 379 380
            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;
Aurelien Jarno's avatar
Aurelien Jarno committed
381 382 383
            if (s->wregs[W_INTR] & INTR_TXINT) {
                s->otherchn->rregs[R_INTR] |= INTR_TXINTB;
            }
384
        }
385
    escc_update_irq(s);
386
    }
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
}

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);
408
    escc_update_irq(s);
409 410
}

411 412 413
static inline void clr_txint(ChannelState *s)
{
    s->txint = 0;
bellard's avatar
bellard committed
414
    s->txint_under_svc = 0;
415
    if (s->chn == chn_a) {
416 417
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
418
        else
419 420
            s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
        s->rregs[R_INTR] &= ~INTR_TXINTA;
421
    } else {
422
        s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
423 424
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->rregs[R_IVEC] = IVEC_HINOINT;
425
        else
426 427
            s->rregs[R_IVEC] = IVEC_LONOINT;
        s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
428
    }
bellard's avatar
bellard committed
429 430
    if (s->rxint)
        set_rxint(s);
431
    escc_update_irq(s);
432 433
}

434
static void escc_update_parameters(ChannelState *s)
435 436 437 438 439 440 441
{
    int speed, parity, data_bits, stop_bits;
    QEMUSerialSetParams ssp;

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

442 443
    if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREN) {
        if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREV)
444 445 446 447 448 449
            parity = 'E';
        else
            parity = 'O';
    } else {
        parity = 'N';
    }
450
    if ((s->wregs[W_TXCTRL1] & TXCTRL1_STPMSK) == TXCTRL1_2STOP)
451 452 453
        stop_bits = 2;
    else
        stop_bits = 1;
454 455
    switch (s->wregs[W_TXCTRL2] & TXCTRL2_BITMSK) {
    case TXCTRL2_5BITS:
456 457
        data_bits = 5;
        break;
458
    case TXCTRL2_7BITS:
459 460
        data_bits = 7;
        break;
461
    case TXCTRL2_6BITS:
462 463 464
        data_bits = 6;
        break;
    default:
465
    case TXCTRL2_8BITS:
466 467 468
        data_bits = 8;
        break;
    }
469
    speed = s->clock / ((s->wregs[W_BRGLO] | (s->wregs[W_BRGHI] << 8)) + 2);
470 471
    switch (s->wregs[W_TXCTRL1] & TXCTRL1_CLKMSK) {
    case TXCTRL1_CLK1X:
472
        break;
473
    case TXCTRL1_CLK16X:
474 475
        speed /= 16;
        break;
476
    case TXCTRL1_CLK32X:
477 478 479
        speed /= 32;
        break;
    default:
480
    case TXCTRL1_CLK64X:
481 482 483 484 485 486 487 488 489 490 491 492
        speed /= 64;
        break;
    }
    ssp.speed = speed;
    ssp.parity = parity;
    ssp.data_bits = data_bits;
    ssp.stop_bits = stop_bits;
    SER_DPRINTF("channel %c: speed=%d parity=%c data=%d stop=%d\n", CHN_C(s),
                speed, parity, data_bits, stop_bits);
    qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
}

493
static void escc_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard's avatar
bellard committed
494
{
495
    SerialState *serial = opaque;
bellard's avatar
bellard committed
496 497 498 499 500
    ChannelState *s;
    uint32_t saddr;
    int newreg, channel;

    val &= 0xff;
501 502
    saddr = (addr >> serial->it_shift) & 1;
    channel = (addr >> (serial->it_shift + 1)) & 1;
503
    s = &serial->chn[channel];
bellard's avatar
bellard committed
504
    switch (saddr) {
505 506 507
    case SERIAL_CTRL:
        SER_DPRINTF("Write channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg,
                    val & 0xff);
blueswir1's avatar
blueswir1 committed
508 509
        newreg = 0;
        switch (s->reg) {
510 511 512
        case W_CMD:
            newreg = val & CMD_PTR_MASK;
            val &= CMD_CMD_MASK;
blueswir1's avatar
blueswir1 committed
513
            switch (val) {
514 515
            case CMD_HI:
                newreg |= CMD_HI;
blueswir1's avatar
blueswir1 committed
516
                break;
517
            case CMD_CLR_TXINT:
518
                clr_txint(s);
blueswir1's avatar
blueswir1 committed
519
                break;
520
            case CMD_CLR_IUS:
521 522 523 524 525 526 527 528 529
                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);
blueswir1's avatar
blueswir1 committed
530 531 532 533 534
                break;
            default:
                break;
            }
            break;
535 536 537 538
        case W_INTR ... W_RXCTRL:
        case W_SYNC1 ... W_TXBUF:
        case W_MISC1 ... W_CLOCK:
        case W_MISC2 ... W_EXTINT:
blueswir1's avatar
blueswir1 committed
539 540
            s->wregs[s->reg] = val;
            break;
541 542
        case W_TXCTRL1:
        case W_TXCTRL2:
543
            s->wregs[s->reg] = val;
544
            escc_update_parameters(s);
545
            break;
546 547
        case W_BRGLO:
        case W_BRGHI:
blueswir1's avatar
blueswir1 committed
548
            s->wregs[s->reg] = val;
549
            s->rregs[s->reg] = val;
550
            escc_update_parameters(s);
blueswir1's avatar
blueswir1 committed
551
            break;
552 553
        case W_MINTR:
            switch (val & MINTR_RST_MASK) {
blueswir1's avatar
blueswir1 committed
554 555 556
            case 0:
            default:
                break;
557
            case MINTR_RST_B:
558
                escc_reset_chn(&serial->chn[0]);
blueswir1's avatar
blueswir1 committed
559
                return;
560
            case MINTR_RST_A:
561
                escc_reset_chn(&serial->chn[1]);
blueswir1's avatar
blueswir1 committed
562
                return;
563
            case MINTR_RST_ALL:
564
                escc_reset(&serial->busdev.qdev);
blueswir1's avatar
blueswir1 committed
565 566 567 568 569 570 571 572 573 574 575
                return;
            }
            break;
        default:
            break;
        }
        if (s->reg == 0)
            s->reg = newreg;
        else
            s->reg = 0;
        break;
576
    case SERIAL_DATA:
blueswir1's avatar
blueswir1 committed
577
        SER_DPRINTF("Write channel %c, ch %d\n", CHN_C(s), val);
578
        s->tx = val;
579
        if (s->wregs[W_TXCTRL2] & TXCTRL2_TXEN) { // tx enabled
blueswir1's avatar
blueswir1 committed
580 581
            if (s->chr)
                qemu_chr_write(s->chr, &s->tx, 1);
582
            else if (s->type == kbd && !s->disabled) {
blueswir1's avatar
blueswir1 committed
583 584 585
                handle_kbd_command(s, val);
            }
        }
586 587
        s->rregs[R_STATUS] |= STATUS_TXEMPTY; // Tx buffer empty
        s->rregs[R_SPEC] |= SPEC_ALLSENT; // All sent
588
        set_txint(s);
blueswir1's avatar
blueswir1 committed
589
        break;
bellard's avatar
bellard committed
590
    default:
blueswir1's avatar
blueswir1 committed
591
        break;
bellard's avatar
bellard committed
592 593 594
    }
}

595
static uint32_t escc_mem_readb(void *opaque, target_phys_addr_t addr)
bellard's avatar
bellard committed
596
{
597
    SerialState *serial = opaque;
bellard's avatar
bellard committed
598 599 600 601 602
    ChannelState *s;
    uint32_t saddr;
    uint32_t ret;
    int channel;

603 604
    saddr = (addr >> serial->it_shift) & 1;
    channel = (addr >> (serial->it_shift + 1)) & 1;
605
    s = &serial->chn[channel];
bellard's avatar
bellard committed
606
    switch (saddr) {
607 608 609
    case SERIAL_CTRL:
        SER_DPRINTF("Read channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg,
                    s->rregs[s->reg]);
blueswir1's avatar
blueswir1 committed
610 611 612
        ret = s->rregs[s->reg];
        s->reg = 0;
        return ret;
613 614
    case SERIAL_DATA:
        s->rregs[R_STATUS] &= ~STATUS_RXAV;
615
        clr_rxint(s);
blueswir1's avatar
blueswir1 committed
616 617 618 619 620
        if (s->type == kbd || s->type == mouse)
            ret = get_queue(s);
        else
            ret = s->rx;
        SER_DPRINTF("Read channel %c, ch %d\n", CHN_C(s), ret);
621 622
        if (s->chr)
            qemu_chr_accept_input(s->chr);
blueswir1's avatar
blueswir1 committed
623
        return ret;
bellard's avatar
bellard committed
624
    default:
blueswir1's avatar
blueswir1 committed
625
        break;
bellard's avatar
bellard committed
626 627 628 629 630 631 632
    }
    return 0;
}

static int serial_can_receive(void *opaque)
{
    ChannelState *s = opaque;
bellard's avatar
bellard committed
633 634
    int ret;

635 636 637
    if (((s->wregs[W_RXCTRL] & RXCTRL_RXEN) == 0) // Rx not enabled
        || ((s->rregs[R_STATUS] & STATUS_RXAV) == STATUS_RXAV))
        // char already available
blueswir1's avatar
blueswir1 committed
638
        ret = 0;
bellard's avatar
bellard committed
639
    else
blueswir1's avatar
blueswir1 committed
640
        ret = 1;
bellard's avatar
bellard committed
641
    return ret;
bellard's avatar
bellard committed
642 643 644 645
}

static void serial_receive_byte(ChannelState *s, int ch)
{
646
    SER_DPRINTF("channel %c put ch %d\n", CHN_C(s), ch);
647
    s->rregs[R_STATUS] |= STATUS_RXAV;
bellard's avatar
bellard committed
648
    s->rx = ch;
649
    set_rxint(s);
bellard's avatar
bellard committed
650 651 652 653
}

static void serial_receive_break(ChannelState *s)
{
654
    s->rregs[R_STATUS] |= STATUS_BRK;
655
    escc_update_irq(s);
bellard's avatar
bellard committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
}

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

671
static CPUReadMemoryFunc * const escc_mem_read[3] = {
672
    escc_mem_readb,
673 674
    NULL,
    NULL,
bellard's avatar
bellard committed
675 676
};

677
static CPUWriteMemoryFunc * const escc_mem_write[3] = {
678
    escc_mem_writeb,
679 680
    NULL,
    NULL,
bellard's avatar
bellard committed
681 682
};

683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
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()
bellard's avatar
bellard committed
700
    }
701
};
bellard's avatar
bellard committed
702

703 704 705 706 707 708 709 710 711 712 713
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()
    }
};
bellard's avatar
bellard committed
714

715
int escc_init(target_phys_addr_t base, qemu_irq irqA, qemu_irq irqB,
716 717
              CharDriverState *chrA, CharDriverState *chrB,
              int clock, int it_shift)
bellard's avatar
bellard committed
718
{
719 720 721 722 723
    DeviceState *dev;
    SysBusDevice *s;
    SerialState *d;

    dev = qdev_create(NULL, "escc");
724 725 726
    qdev_prop_set_uint32(dev, "disabled", 0);
    qdev_prop_set_uint32(dev, "frequency", clock);
    qdev_prop_set_uint32(dev, "it_shift", it_shift);
Blue Swirl's avatar
Blue Swirl committed
727 728
    qdev_prop_set_chr(dev, "chrB", chrB);
    qdev_prop_set_chr(dev, "chrA", chrA);
729 730
    qdev_prop_set_uint32(dev, "chnBtype", ser);
    qdev_prop_set_uint32(dev, "chnAtype", ser);
Markus Armbruster's avatar
Markus Armbruster committed
731
    qdev_init_nofail(dev);
732
    s = sysbus_from_qdev(dev);
733 734
    sysbus_connect_irq(s, 0, irqB);
    sysbus_connect_irq(s, 1, irqA);
735 736
    if (base) {
        sysbus_mmio_map(s, 0, base);
bellard's avatar
bellard committed
737
    }
738 739 740

    d = FROM_SYSBUS(SerialState, s);
    return d->mmio_index;
bellard's avatar
bellard committed
741 742
}

bellard's avatar
bellard committed
743 744 745 746 747 748 749 750 751 752 753
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,
};

blueswir1's avatar
blueswir1 committed
754 755 756 757 758 759 760 761
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,
762
    1, 3, 25, 26, 49, 52, 72, 73, 97, 99, 111, 118, 120, 122, 67, 0,
blueswir1's avatar
blueswir1 committed
763 764
};

bellard's avatar
bellard committed
765 766 767
static void sunkbd_event(void *opaque, int ch)
{
    ChannelState *s = opaque;
bellard's avatar
bellard committed
768 769
    int release = ch & 0x80;

770 771
    KBD_DPRINTF("Untranslated keycode %2.2x (%s)\n", ch, release? "release" :
                "press");
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
    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:
blueswir1's avatar
blueswir1 committed
794 795
        s->e0_mode = 1;
        return;
796 797
    default:
        break;
blueswir1's avatar
blueswir1 committed
798 799 800 801 802 803 804 805
    }
    if (s->e0_mode) {
        s->e0_mode = 0;
        ch = e0_keycodes[ch & 0x7f];
    } else {
        ch = keycodes[ch & 0x7f];
    }
    KBD_DPRINTF("Translated keycode %2.2x\n", ch);
bellard's avatar
bellard committed
806 807 808 809 810 811
    put_queue(s, ch | release);
}

static void handle_kbd_command(ChannelState *s, int val)
{
    KBD_DPRINTF("Command %d\n", val);
blueswir1's avatar
blueswir1 committed
812 813 814 815
    if (s->led_mode) { // Ignore led byte
        s->led_mode = 0;
        return;
    }
bellard's avatar
bellard committed
816 817
    switch (val) {
    case 1: // Reset, return type code
blueswir1's avatar
blueswir1 committed
818
        clear_queue(s);
blueswir1's avatar
blueswir1 committed
819 820 821 822
        put_queue(s, 0xff);
        put_queue(s, 4); // Type 4
        put_queue(s, 0x7f);
        break;
blueswir1's avatar
blueswir1 committed
823 824 825
    case 0xe: // Set leds
        s->led_mode = 1;
        break;
bellard's avatar
bellard committed
826
    case 7: // Query layout
blueswir1's avatar
blueswir1 committed
827 828
    case 0xf:
        clear_queue(s);
blueswir1's avatar
blueswir1 committed
829 830 831
        put_queue(s, 0xfe);
        put_queue(s, 0); // XXX, layout?
        break;
bellard's avatar
bellard committed
832
    default:
blueswir1's avatar
blueswir1 committed
833
        break;
bellard's avatar
bellard committed
834
    }
bellard's avatar
bellard committed
835 836
}

837
static void sunmouse_event(void *opaque,
bellard's avatar
bellard committed
838 839 840 841 842
                               int dx, int dy, int dz, int buttons_state)
{
    ChannelState *s = opaque;
    int ch;

843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
    MS_DPRINTF("dx=%d dy=%d buttons=%01x\n", dx, dy, buttons_state);

    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)
859
        ch = 127;
860
    else if (ch < -127)
861
        ch = -127;
862 863 864 865 866 867

    put_queue(s, ch & 0xff);

    ch = -dy;

    if (ch > 127)
868
        ch = 127;
869
    else if (ch < -127)
870
        ch = -127;
871 872 873 874 875 876 877

    put_queue(s, ch & 0xff);

    // MSC protocol specify two extra motion bytes

    put_queue(s, 0);
    put_queue(s, 0);
bellard's avatar
bellard committed
878 879
}

880
void slavio_serial_ms_kbd_init(target_phys_addr_t base, qemu_irq irq,
881
                               int disabled, int clock, int it_shift)
bellard's avatar
bellard committed
882
{
883 884 885 886
    DeviceState *dev;
    SysBusDevice *s;

    dev = qdev_create(NULL, "escc");
887 888 889
    qdev_prop_set_uint32(dev, "disabled", disabled);
    qdev_prop_set_uint32(dev, "frequency", clock);
    qdev_prop_set_uint32(dev, "it_shift", it_shift);
Blue Swirl's avatar
Blue Swirl committed
890 891
    qdev_prop_set_chr(dev, "chrB", NULL);
    qdev_prop_set_chr(dev, "chrA", NULL);
892 893
    qdev_prop_set_uint32(dev, "chnBtype", mouse);
    qdev_prop_set_uint32(dev, "chnAtype", kbd);
Markus Armbruster's avatar
Markus Armbruster committed
894
    qdev_init_nofail(dev);
895 896 897 898 899
    s = sysbus_from_qdev(dev);
    sysbus_connect_irq(s, 0, irq);
    sysbus_connect_irq(s, 1, irq);
    sysbus_mmio_map(s, 0, base);
}
900

901
static int escc_init1(SysBusDevice *dev)
902 903 904 905
{
    SerialState *s = FROM_SYSBUS(SerialState, dev);
    int io;
    unsigned int i;
906 907 908

    s->chn[0].disabled = s->disabled;
    s->chn[1].disabled = s->disabled;
bellard's avatar
bellard committed
909
    for (i = 0; i < 2; i++) {
910
        sysbus_init_irq(dev, &s->chn[i].irq);
blueswir1's avatar
blueswir1 committed
911
        s->chn[i].chn = 1 - i;
912
        s->chn[i].clock = s->frequency / 2;
913 914 915 916
        if (s->chn[i].chr) {
            qemu_chr_add_handlers(s->chn[i].chr, serial_can_receive,
                                  serial_receive1, serial_event, &s->chn[i]);
        }
bellard's avatar
bellard committed
917 918 919
    }
    s->chn[0].otherchn = &s->chn[1];
    s->chn[1].otherchn = &s->chn[0];
bellard's avatar
bellard committed
920

921 922
    io = cpu_register_io_memory(escc_mem_read, escc_mem_write, s,
                                DEVICE_NATIVE_ENDIAN);
923 924
    sysbus_init_mmio(dev, ESCC_SIZE << s->it_shift, io);
    s->mmio_index = io;
bellard's avatar
bellard committed
925

926 927 928 929 930 931 932
    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]);
    }
933

934
    return 0;
bellard's avatar
bellard committed
935
}
936 937 938 939 940

static SysBusDeviceInfo escc_info = {
    .init = escc_init1,
    .qdev.name  = "escc",
    .qdev.size  = sizeof(SerialState),
941 942
    .qdev.vmsd  = &vmstate_escc,
    .qdev.reset = escc_reset,
943
    .qdev.props = (Property[]) {
944 945 946 947 948 949 950 951 952
        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(),
953 954 955 956 957 958 959 960 961
    }
};

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

device_init(escc_register_devices)