Blame view

hw/escc.c 26.6 KB
bellard authored
1
/*
2
 * QEMU ESCC (Z8030/Z8530/Z85C30/SCC/ESCC) serial port emulation
3
 *
4
 * Copyright (c) 2003-2005 Fabrice Bellard
5
 *
bellard authored
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 authored
25
#include "hw.h"
26
#include "sysbus.h"
27
#include "escc.h"
pbrook authored
28
29
30
#include "qemu-char.h"
#include "console.h"
31
/* debug serial */
bellard authored
32
33
34
35
36
//#define DEBUG_SERIAL

/* debug keyboard */
//#define DEBUG_KBD
37
/* debug mouse */
bellard authored
38
39
40
//#define DEBUG_MOUSE

/*
41
 * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001
bellard authored
42
43
 * (Slave I/O), also produced as NCR89C105. See
 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
44
 *
bellard authored
45
46
47
48
 * 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.
 *
49
50
51
52
53
54
55
56
 * 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 authored
57
58
 */
59
60
61
62
63
64
65
/*
 * Modifications:
 *  2006-Aug-10  Igor Kovalenko :   Renamed KBDQueue to SERIOQueue, implemented
 *                                  serial mouse queue.
 *                                  Implemented serial mouse protocol.
 */
66
#ifdef DEBUG_SERIAL
67
68
#define SER_DPRINTF(fmt, ...)                                   \
    do { printf("SER: " fmt , ## __VA_ARGS__); } while (0)
69
#else
70
#define SER_DPRINTF(fmt, ...)
71
72
#endif
#ifdef DEBUG_KBD
73
74
#define KBD_DPRINTF(fmt, ...)                                   \
    do { printf("KBD: " fmt , ## __VA_ARGS__); } while (0)
75
#else
76
#define KBD_DPRINTF(fmt, ...)
77
78
#endif
#ifdef DEBUG_MOUSE
79
80
#define MS_DPRINTF(fmt, ...)                                    \
    do { printf("MSC: " fmt , ## __VA_ARGS__); } while (0)
81
#else
82
#define MS_DPRINTF(fmt, ...)
83
84
85
86
87
88
#endif

typedef enum {
    chn_a, chn_b,
} chn_id_t;
89
90
#define CHN_C(s) ((s)->chn == chn_b? 'b' : 'a')
91
92
93
94
typedef enum {
    ser, kbd, mouse,
} chn_type_t;
95
#define SERIO_QUEUE_SIZE 256
96
97

typedef struct {
98
    uint8_t data[SERIO_QUEUE_SIZE];
99
    int rptr, wptr, count;
100
} SERIOQueue;
101
102
#define SERIAL_REGS 16
bellard authored
103
typedef struct ChannelState {
pbrook authored
104
    qemu_irq irq;
blueswir1 authored
105
106
    uint32_t reg;
    uint32_t rxint, txint, rxint_under_svc, txint_under_svc;
107
108
109
    chn_id_t chn; // this channel, A (base+4) or B (base+0)
    chn_type_t type;
    struct ChannelState *otherchn;
110
    uint8_t rx, tx, wregs[SERIAL_REGS], rregs[SERIAL_REGS];
111
    SERIOQueue queue;
bellard authored
112
    CharDriverState *chr;
113
    int e0_mode, led_mode, caps_lock_mode, num_lock_mode;
114
    int disabled;
115
    int clock;
bellard authored
116
117
118
} ChannelState;

struct SerialState {
119
    SysBusDevice busdev;
bellard authored
120
    struct ChannelState chn[2];
121
    int it_shift;
122
    int mmio_index;
bellard authored
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#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 authored
226
227
228
229
230
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);
231
232
233
234
235
236
237
static void clear_queue(void *opaque)
{
    ChannelState *s = opaque;
    SERIOQueue *q = &s->queue;
    q->rptr = q->wptr = q->count = 0;
}
238
239
240
static void put_queue(void *opaque, int b)
{
    ChannelState *s = opaque;
241
    SERIOQueue *q = &s->queue;
242
243
    SER_DPRINTF("channel %c put: 0x%02x\n", CHN_C(s), b);
244
    if (q->count >= SERIO_QUEUE_SIZE)
245
246
        return;
    q->data[q->wptr] = b;
247
    if (++q->wptr == SERIO_QUEUE_SIZE)
248
249
250
251
252
253
254
255
        q->wptr = 0;
    q->count++;
    serial_receive_byte(s, 0);
}

static uint32_t get_queue(void *opaque)
{
    ChannelState *s = opaque;
256
    SERIOQueue *q = &s->queue;
257
    int val;
258
259
    if (q->count == 0) {
blueswir1 authored
260
        return 0;
261
262
    } else {
        val = q->data[q->rptr];
263
        if (++q->rptr == SERIO_QUEUE_SIZE)
264
265
266
            q->rptr = 0;
        q->count--;
    }
267
    SER_DPRINTF("channel %c get 0x%02x\n", CHN_C(s), val);
268
    if (q->count > 0)
blueswir1 authored
269
        serial_receive_byte(s, 0);
270
271
272
    return val;
}
273
static int escc_update_irq_chn(ChannelState *s)
bellard authored
274
{
275
    if ((((s->wregs[W_INTR] & INTR_TXINT) && s->txint == 1) ||
276
277
278
         // tx ints enabled, pending
         ((((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINT1ST) ||
           ((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINTALL)) &&
blueswir1 authored
279
          s->rxint == 1) || // rx ints enabled, pending
280
281
         ((s->wregs[W_EXTINT] & EXTINT_BRKINT) &&
          (s->rregs[R_STATUS] & STATUS_BRK)))) { // break int e&p
282
        return 1;
bellard authored
283
    }
284
285
286
    return 0;
}
287
static void escc_update_irq(ChannelState *s)
288
289
290
{
    int irq;
291
292
    irq = escc_update_irq_chn(s);
    irq |= escc_update_irq_chn(s->otherchn);
293
pbrook authored
294
295
    SER_DPRINTF("IRQ = %d\n", irq);
    qemu_set_irq(s->irq, irq);
bellard authored
296
297
}
298
static void escc_reset_chn(ChannelState *s)
bellard authored
299
300
301
302
{
    int i;

    s->reg = 0;
blueswir1 authored
303
    for (i = 0; i < SERIAL_REGS; i++) {
blueswir1 authored
304
305
        s->rregs[i] = 0;
        s->wregs[i] = 0;
bellard authored
306
    }
307
308
309
310
311
312
    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
313
    if (s->disabled)
314
315
        s->rregs[R_STATUS] = STATUS_TXEMPTY | STATUS_DCD | STATUS_SYNC |
            STATUS_CTS | STATUS_TXUNDRN;
316
    else
317
        s->rregs[R_STATUS] = STATUS_TXEMPTY | STATUS_TXUNDRN;
318
    s->rregs[R_SPEC] = SPEC_BITS8 | SPEC_ALLSENT;
bellard authored
319
320
321

    s->rx = s->tx = 0;
    s->rxint = s->txint = 0;
322
    s->rxint_under_svc = s->txint_under_svc = 0;
323
    s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0;
324
    clear_queue(s);
bellard authored
325
326
}
327
static void escc_reset(void *opaque)
bellard authored
328
329
{
    SerialState *s = opaque;
330
331
    escc_reset_chn(&s->chn[0]);
    escc_reset_chn(&s->chn[1]);
bellard authored
332
333
}
334
335
336
static inline void set_rxint(ChannelState *s)
{
    s->rxint = 1;
337
338
    if (!s->txint_under_svc) {
        s->rxint_under_svc = 1;
339
        if (s->chn == chn_a) {
340
341
            if (s->wregs[W_MINTR] & MINTR_STATUSHI)
                s->otherchn->rregs[R_IVEC] = IVEC_HIRXINTA;
342
            else
343
                s->otherchn->rregs[R_IVEC] = IVEC_LORXINTA;
344
        } else {
345
346
            if (s->wregs[W_MINTR] & MINTR_STATUSHI)
                s->rregs[R_IVEC] = IVEC_HIRXINTB;
347
            else
348
                s->rregs[R_IVEC] = IVEC_LORXINTB;
349
        }
350
    }
351
    if (s->chn == chn_a)
352
        s->rregs[R_INTR] |= INTR_RXINTA;
353
    else
354
        s->otherchn->rregs[R_INTR] |= INTR_RXINTB;
355
    escc_update_irq(s);
356
357
}
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
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) {
            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;
        }
    }
    if (s->chn == chn_a)
        s->rregs[R_INTR] |= INTR_TXINTA;
    else
        s->otherchn->rregs[R_INTR] |= INTR_TXINTB;
376
    escc_update_irq(s);
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
}

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);
398
    escc_update_irq(s);
399
400
}
401
402
403
static inline void clr_txint(ChannelState *s)
{
    s->txint = 0;
404
    s->txint_under_svc = 0;
405
    if (s->chn == chn_a) {
406
407
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
408
        else
409
410
            s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
        s->rregs[R_INTR] &= ~INTR_TXINTA;
411
    } else {
412
413
        if (s->wregs[W_MINTR] & MINTR_STATUSHI)
            s->rregs[R_IVEC] = IVEC_HINOINT;
414
        else
415
416
            s->rregs[R_IVEC] = IVEC_LONOINT;
        s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
417
    }
418
419
    if (s->rxint)
        set_rxint(s);
420
    escc_update_irq(s);
421
422
}
423
static void escc_update_parameters(ChannelState *s)
424
425
426
427
428
429
430
{
    int speed, parity, data_bits, stop_bits;
    QEMUSerialSetParams ssp;

    if (!s->chr || s->type != ser)
        return;
431
432
    if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREN) {
        if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREV)
433
434
435
436
437
438
            parity = 'E';
        else
            parity = 'O';
    } else {
        parity = 'N';
    }
439
    if ((s->wregs[W_TXCTRL1] & TXCTRL1_STPMSK) == TXCTRL1_2STOP)
440
441
442
        stop_bits = 2;
    else
        stop_bits = 1;
443
444
    switch (s->wregs[W_TXCTRL2] & TXCTRL2_BITMSK) {
    case TXCTRL2_5BITS:
445
446
        data_bits = 5;
        break;
447
    case TXCTRL2_7BITS:
448
449
        data_bits = 7;
        break;
450
    case TXCTRL2_6BITS:
451
452
453
        data_bits = 6;
        break;
    default:
454
    case TXCTRL2_8BITS:
455
456
457
        data_bits = 8;
        break;
    }
458
    speed = s->clock / ((s->wregs[W_BRGLO] | (s->wregs[W_BRGHI] << 8)) + 2);
459
460
    switch (s->wregs[W_TXCTRL1] & TXCTRL1_CLKMSK) {
    case TXCTRL1_CLK1X:
461
        break;
462
    case TXCTRL1_CLK16X:
463
464
        speed /= 16;
        break;
465
    case TXCTRL1_CLK32X:
466
467
468
        speed /= 32;
        break;
    default:
469
    case TXCTRL1_CLK64X:
470
471
472
473
474
475
476
477
478
479
480
481
        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);
}
482
static void escc_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
bellard authored
483
{
484
    SerialState *serial = opaque;
bellard authored
485
486
487
488
489
    ChannelState *s;
    uint32_t saddr;
    int newreg, channel;

    val &= 0xff;
490
491
    saddr = (addr >> serial->it_shift) & 1;
    channel = (addr >> (serial->it_shift + 1)) & 1;
492
    s = &serial->chn[channel];
bellard authored
493
    switch (saddr) {
494
495
496
    case SERIAL_CTRL:
        SER_DPRINTF("Write channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg,
                    val & 0xff);
blueswir1 authored
497
498
        newreg = 0;
        switch (s->reg) {
499
500
501
        case W_CMD:
            newreg = val & CMD_PTR_MASK;
            val &= CMD_CMD_MASK;
blueswir1 authored
502
            switch (val) {
503
504
            case CMD_HI:
                newreg |= CMD_HI;
blueswir1 authored
505
                break;
506
            case CMD_CLR_TXINT:
507
                clr_txint(s);
blueswir1 authored
508
                break;
509
            case CMD_CLR_IUS:
510
511
512
513
                if (s->rxint_under_svc)
                    clr_rxint(s);
                else if (s->txint_under_svc)
                    clr_txint(s);
blueswir1 authored
514
515
516
517
518
                break;
            default:
                break;
            }
            break;
519
520
521
522
        case W_INTR ... W_RXCTRL:
        case W_SYNC1 ... W_TXBUF:
        case W_MISC1 ... W_CLOCK:
        case W_MISC2 ... W_EXTINT:
blueswir1 authored
523
524
            s->wregs[s->reg] = val;
            break;
525
526
        case W_TXCTRL1:
        case W_TXCTRL2:
527
            s->wregs[s->reg] = val;
528
            escc_update_parameters(s);
529
            break;
530
531
        case W_BRGLO:
        case W_BRGHI:
blueswir1 authored
532
            s->wregs[s->reg] = val;
533
            s->rregs[s->reg] = val;
534
            escc_update_parameters(s);
blueswir1 authored
535
            break;
536
537
        case W_MINTR:
            switch (val & MINTR_RST_MASK) {
blueswir1 authored
538
539
540
            case 0:
            default:
                break;
541
            case MINTR_RST_B:
542
                escc_reset_chn(&serial->chn[0]);
blueswir1 authored
543
                return;
544
            case MINTR_RST_A:
545
                escc_reset_chn(&serial->chn[1]);
blueswir1 authored
546
                return;
547
            case MINTR_RST_ALL:
548
                escc_reset(serial);
blueswir1 authored
549
550
551
552
553
554
555
556
557
558
559
                return;
            }
            break;
        default:
            break;
        }
        if (s->reg == 0)
            s->reg = newreg;
        else
            s->reg = 0;
        break;
560
    case SERIAL_DATA:
blueswir1 authored
561
        SER_DPRINTF("Write channel %c, ch %d\n", CHN_C(s), val);
562
        s->tx = val;
563
        if (s->wregs[W_TXCTRL2] & TXCTRL2_TXEN) { // tx enabled
blueswir1 authored
564
565
            if (s->chr)
                qemu_chr_write(s->chr, &s->tx, 1);
566
            else if (s->type == kbd && !s->disabled) {
blueswir1 authored
567
568
569
                handle_kbd_command(s, val);
            }
        }
570
571
        s->rregs[R_STATUS] |= STATUS_TXEMPTY; // Tx buffer empty
        s->rregs[R_SPEC] |= SPEC_ALLSENT; // All sent
572
        set_txint(s);
blueswir1 authored
573
        break;
bellard authored
574
    default:
blueswir1 authored
575
        break;
bellard authored
576
577
578
    }
}
579
static uint32_t escc_mem_readb(void *opaque, target_phys_addr_t addr)
bellard authored
580
{
581
    SerialState *serial = opaque;
bellard authored
582
583
584
585
586
    ChannelState *s;
    uint32_t saddr;
    uint32_t ret;
    int channel;
587
588
    saddr = (addr >> serial->it_shift) & 1;
    channel = (addr >> (serial->it_shift + 1)) & 1;
589
    s = &serial->chn[channel];
bellard authored
590
    switch (saddr) {
591
592
593
    case SERIAL_CTRL:
        SER_DPRINTF("Read channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg,
                    s->rregs[s->reg]);
blueswir1 authored
594
595
596
        ret = s->rregs[s->reg];
        s->reg = 0;
        return ret;
597
598
    case SERIAL_DATA:
        s->rregs[R_STATUS] &= ~STATUS_RXAV;
599
        clr_rxint(s);
blueswir1 authored
600
601
602
603
604
        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);
605
606
        if (s->chr)
            qemu_chr_accept_input(s->chr);
blueswir1 authored
607
        return ret;
bellard authored
608
    default:
blueswir1 authored
609
        break;
bellard authored
610
611
612
613
614
615
616
    }
    return 0;
}

static int serial_can_receive(void *opaque)
{
    ChannelState *s = opaque;
617
618
    int ret;
619
620
621
    if (((s->wregs[W_RXCTRL] & RXCTRL_RXEN) == 0) // Rx not enabled
        || ((s->rregs[R_STATUS] & STATUS_RXAV) == STATUS_RXAV))
        // char already available
blueswir1 authored
622
        ret = 0;
bellard authored
623
    else
blueswir1 authored
624
        ret = 1;
625
    return ret;
bellard authored
626
627
628
629
}

static void serial_receive_byte(ChannelState *s, int ch)
{
630
    SER_DPRINTF("channel %c put ch %d\n", CHN_C(s), ch);
631
    s->rregs[R_STATUS] |= STATUS_RXAV;
bellard authored
632
    s->rx = ch;
633
    set_rxint(s);
bellard authored
634
635
636
637
}

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

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);
}
655
656
static CPUReadMemoryFunc *escc_mem_read[3] = {
    escc_mem_readb,
657
658
    NULL,
    NULL,
bellard authored
659
660
};
661
662
static CPUWriteMemoryFunc *escc_mem_write[3] = {
    escc_mem_writeb,
663
664
    NULL,
    NULL,
bellard authored
665
666
};
667
static void escc_save_chn(QEMUFile *f, ChannelState *s)
bellard authored
668
{
blueswir1 authored
669
670
    uint32_t tmp = 0;
pbrook authored
671
    qemu_put_be32s(f, &tmp); /* unused, was IRQ.  */
bellard authored
672
673
674
    qemu_put_be32s(f, &s->reg);
    qemu_put_be32s(f, &s->rxint);
    qemu_put_be32s(f, &s->txint);
675
676
    qemu_put_be32s(f, &s->rxint_under_svc);
    qemu_put_be32s(f, &s->txint_under_svc);
bellard authored
677
678
    qemu_put_8s(f, &s->rx);
    qemu_put_8s(f, &s->tx);
679
680
    qemu_put_buffer(f, s->wregs, SERIAL_REGS);
    qemu_put_buffer(f, s->rregs, SERIAL_REGS);
bellard authored
681
682
}
683
static void escc_save(QEMUFile *f, void *opaque)
bellard authored
684
685
686
{
    SerialState *s = opaque;
687
688
    escc_save_chn(f, &s->chn[0]);
    escc_save_chn(f, &s->chn[1]);
bellard authored
689
690
}
691
static int escc_load_chn(QEMUFile *f, ChannelState *s, int version_id)
bellard authored
692
{
blueswir1 authored
693
    uint32_t tmp;
pbrook authored
694
695
    if (version_id > 2)
bellard authored
696
697
        return -EINVAL;
pbrook authored
698
    qemu_get_be32s(f, &tmp); /* unused */
bellard authored
699
700
701
    qemu_get_be32s(f, &s->reg);
    qemu_get_be32s(f, &s->rxint);
    qemu_get_be32s(f, &s->txint);
702
703
704
705
    if (version_id >= 2) {
        qemu_get_be32s(f, &s->rxint_under_svc);
        qemu_get_be32s(f, &s->txint_under_svc);
    }
bellard authored
706
707
    qemu_get_8s(f, &s->rx);
    qemu_get_8s(f, &s->tx);
708
709
    qemu_get_buffer(f, s->wregs, SERIAL_REGS);
    qemu_get_buffer(f, s->rregs, SERIAL_REGS);
bellard authored
710
711
712
    return 0;
}
713
static int escc_load(QEMUFile *f, void *opaque, int version_id)
bellard authored
714
715
716
717
{
    SerialState *s = opaque;
    int ret;
718
    ret = escc_load_chn(f, &s->chn[0], version_id);
bellard authored
719
    if (ret != 0)
blueswir1 authored
720
        return ret;
721
    ret = escc_load_chn(f, &s->chn[1], version_id);
bellard authored
722
723
724
725
    return ret;

}
726
727
728
int escc_init(target_phys_addr_t base, qemu_irq irqA, qemu_irq irqB,
              CharDriverState *chrA, CharDriverState *chrB,
              int clock, int it_shift)
bellard authored
729
{
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
    DeviceState *dev;
    SysBusDevice *s;
    SerialState *d;

    dev = qdev_create(NULL, "escc");
    qdev_set_prop_int(dev, "disabled", 0);
    qdev_set_prop_int(dev, "frequency", clock);
    qdev_set_prop_int(dev, "it_shift", it_shift);
    qdev_set_prop_ptr(dev, "chrB", chrB);
    qdev_set_prop_ptr(dev, "chrA", chrA);
    qdev_set_prop_int(dev, "chnBtype", ser);
    qdev_set_prop_int(dev, "chnAtype", ser);
    qdev_init(dev);
    s = sysbus_from_qdev(dev);
    sysbus_connect_irq(s, 0, irqA);
    sysbus_connect_irq(s, 1, irqB);
    if (base) {
        sysbus_mmio_map(s, 0, base);
bellard authored
748
    }
749
750
751

    d = FROM_SYSBUS(SerialState, s);
    return d->mmio_index;
bellard authored
752
753
}
754
755
756
757
758
759
760
761
762
763
764
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,
};
765
766
767
768
769
770
771
772
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,
773
    1, 3, 25, 26, 49, 52, 72, 73, 97, 99, 111, 118, 120, 122, 67, 0,
774
775
};
bellard authored
776
777
778
static void sunkbd_event(void *opaque, int ch)
{
    ChannelState *s = opaque;
779
780
    int release = ch & 0x80;
781
782
    KBD_DPRINTF("Untranslated keycode %2.2x (%s)\n", ch, release? "release" :
                "press");
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
    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:
805
806
        s->e0_mode = 1;
        return;
807
808
    default:
        break;
809
810
811
812
813
814
815
816
    }
    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);
817
818
819
820
821
822
    put_queue(s, ch | release);
}

static void handle_kbd_command(ChannelState *s, int val)
{
    KBD_DPRINTF("Command %d\n", val);
823
824
825
826
    if (s->led_mode) { // Ignore led byte
        s->led_mode = 0;
        return;
    }
827
828
    switch (val) {
    case 1: // Reset, return type code
829
        clear_queue(s);
blueswir1 authored
830
831
832
833
        put_queue(s, 0xff);
        put_queue(s, 4); // Type 4
        put_queue(s, 0x7f);
        break;
834
835
836
    case 0xe: // Set leds
        s->led_mode = 1;
        break;
837
    case 7: // Query layout
838
839
    case 0xf:
        clear_queue(s);
blueswir1 authored
840
841
842
        put_queue(s, 0xfe);
        put_queue(s, 0); // XXX, layout?
        break;
843
    default:
blueswir1 authored
844
        break;
845
    }
bellard authored
846
847
}
848
static void sunmouse_event(void *opaque,
bellard authored
849
850
851
852
853
                               int dx, int dy, int dz, int buttons_state)
{
    ChannelState *s = opaque;
    int ch;
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
    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)
        ch=127;
    else if (ch < -127)
        ch=-127;

    put_queue(s, ch & 0xff);

    ch = -dy;

    if (ch > 127)
        ch=127;
    else if (ch < -127)
        ch=-127;

    put_queue(s, ch & 0xff);

    // MSC protocol specify two extra motion bytes

    put_queue(s, 0);
    put_queue(s, 0);
bellard authored
889
890
}
891
void slavio_serial_ms_kbd_init(target_phys_addr_t base, qemu_irq irq,
892
                               int disabled, int clock, int it_shift)
bellard authored
893
{
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
    DeviceState *dev;
    SysBusDevice *s;

    dev = qdev_create(NULL, "escc");
    qdev_set_prop_int(dev, "disabled", disabled);
    qdev_set_prop_int(dev, "frequency", clock);
    qdev_set_prop_int(dev, "it_shift", it_shift);
    qdev_set_prop_ptr(dev, "chrB", NULL);
    qdev_set_prop_ptr(dev, "chrA", NULL);
    qdev_set_prop_int(dev, "chnBtype", mouse);
    qdev_set_prop_int(dev, "chnAtype", kbd);
    qdev_init(dev);
    s = sysbus_from_qdev(dev);
    sysbus_connect_irq(s, 0, irq);
    sysbus_connect_irq(s, 1, irq);
    sysbus_mmio_map(s, 0, base);
}
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
static void escc_init1(SysBusDevice *dev)
{
    SerialState *s = FROM_SYSBUS(SerialState, dev);
    int io;
    unsigned int i;
    uint32_t clock, disabled;

    s->it_shift = qdev_get_prop_int(&dev->qdev, "it_shift", 0);
    clock = qdev_get_prop_int(&dev->qdev, "frequency", 0);
    s->chn[0].chr = qdev_get_prop_ptr(&dev->qdev, "chrB");
    s->chn[1].chr = qdev_get_prop_ptr(&dev->qdev, "chrA");
    disabled = qdev_get_prop_int(&dev->qdev, "disabled", 0);
    s->chn[0].disabled = disabled;
    s->chn[1].disabled = disabled;
926
    for (i = 0; i < 2; i++) {
927
        sysbus_init_irq(dev, &s->chn[i].irq);
blueswir1 authored
928
        s->chn[i].chn = 1 - i;
929
        s->chn[i].clock = clock / 2;
930
931
932
933
        if (s->chn[i].chr) {
            qemu_chr_add_handlers(s->chn[i].chr, serial_can_receive,
                                  serial_receive1, serial_event, &s->chn[i]);
        }
934
935
936
    }
    s->chn[0].otherchn = &s->chn[1];
    s->chn[1].otherchn = &s->chn[0];
937
938
    s->chn[0].type = qdev_get_prop_int(&dev->qdev, "chnBtype", 0);
    s->chn[1].type = qdev_get_prop_int(&dev->qdev, "chnAtype", 0);
bellard authored
939
940
941
942
    io = cpu_register_io_memory(escc_mem_read, escc_mem_write, s);
    sysbus_init_mmio(dev, ESCC_SIZE << s->it_shift, io);
    s->mmio_index = io;
bellard authored
943
944
945
946
947
948
949
950
951
    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]);
    }
    register_savevm("escc", -1, 2, escc_save, escc_load, s);
952
    qemu_register_reset(escc_reset, s);
953
    escc_reset(s);
bellard authored
954
}
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977

static SysBusDeviceInfo escc_info = {
    .init = escc_init1,
    .qdev.name  = "escc",
    .qdev.size  = sizeof(SerialState),
    .qdev.props = (DevicePropList[]) {
        {.name = "frequency", .type = PROP_TYPE_INT},
        {.name = "it_shift", .type = PROP_TYPE_INT},
        {.name = "disabled", .type = PROP_TYPE_INT},
        {.name = "chrB", .type = PROP_TYPE_PTR},
        {.name = "chrA", .type = PROP_TYPE_PTR},
        {.name = "chnBtype", .type = PROP_TYPE_INT},
        {.name = "chnAtype", .type = PROP_TYPE_INT},
        {.name = NULL}
    }
};

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

device_init(escc_register_devices)