2 * QEMU Sparc SLAVIO serial port emulation
4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26 //#define DEBUG_SERIAL
35 * This is the serial port, mouse and keyboard part of chip STP2001
36 * (Slave I/O), also produced as NCR89C105. See
37 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
39 * The serial ports implement full AMD AM8530 or Zilog Z8530 chips,
40 * mouse and keyboard ports don't implement all functions and they are
41 * only asynchronous. There is no DMA.
47 * 2006-Aug-10 Igor Kovalenko : Renamed KBDQueue to SERIOQueue, implemented
49 * Implemented serial mouse protocol.
53 #define SER_DPRINTF(fmt, args...) \
54 do { printf("SER: " fmt , ##args); } while (0)
55 #define pic_set_irq(irq, level) \
56 do { printf("SER: set_irq(%d): %d\n", (irq), (level)); pic_set_irq((irq),(level));} while (0)
58 #define SER_DPRINTF(fmt, args...)
61 #define KBD_DPRINTF(fmt, args...) \
62 do { printf("KBD: " fmt , ##args); } while (0)
64 #define KBD_DPRINTF(fmt, args...)
67 #define MS_DPRINTF(fmt, args...) \
68 do { printf("MSC: " fmt , ##args); } while (0)
70 #define MS_DPRINTF(fmt, args...)
77 #define CHN_C(s) ((s)->chn == chn_b? 'b' : 'a')
83 #define SERIO_QUEUE_SIZE 256
86 uint8_t data
[SERIO_QUEUE_SIZE
];
87 int rptr
, wptr
, count
;
90 typedef struct ChannelState
{
93 int rxint
, txint
, rxint_under_svc
, txint_under_svc
;
94 chn_id_t chn
; // this channel, A (base+4) or B (base+0)
96 struct ChannelState
*otherchn
;
97 uint8_t rx
, tx
, wregs
[16], rregs
[16];
103 struct ChannelState chn
[2];
106 #define SERIAL_MAXADDR 7
108 static void handle_kbd_command(ChannelState
*s
, int val
);
109 static int serial_can_receive(void *opaque
);
110 static void serial_receive_byte(ChannelState
*s
, int ch
);
111 static inline void set_txint(ChannelState
*s
);
113 static void put_queue(void *opaque
, int b
)
115 ChannelState
*s
= opaque
;
116 SERIOQueue
*q
= &s
->queue
;
118 SER_DPRINTF("channel %c put: 0x%02x\n", CHN_C(s
), b
);
119 if (q
->count
>= SERIO_QUEUE_SIZE
)
121 q
->data
[q
->wptr
] = b
;
122 if (++q
->wptr
== SERIO_QUEUE_SIZE
)
125 serial_receive_byte(s
, 0);
128 static uint32_t get_queue(void *opaque
)
130 ChannelState
*s
= opaque
;
131 SERIOQueue
*q
= &s
->queue
;
137 val
= q
->data
[q
->rptr
];
138 if (++q
->rptr
== SERIO_QUEUE_SIZE
)
142 KBD_DPRINTF("channel %c get 0x%02x\n", CHN_C(s
), val
);
144 serial_receive_byte(s
, 0);
148 static int slavio_serial_update_irq_chn(ChannelState
*s
)
150 if ((s
->wregs
[1] & 1) && // interrupts enabled
151 (((s
->wregs
[1] & 2) && s
->txint
== 1) || // tx ints enabled, pending
152 ((((s
->wregs
[1] & 0x18) == 8) || ((s
->wregs
[1] & 0x18) == 0x10)) &&
153 s
->rxint
== 1) || // rx ints enabled, pending
154 ((s
->wregs
[15] & 0x80) && (s
->rregs
[0] & 0x80)))) { // break int e&p
160 static void slavio_serial_update_irq(ChannelState
*s
)
164 irq
= slavio_serial_update_irq_chn(s
);
165 irq
|= slavio_serial_update_irq_chn(s
->otherchn
);
167 pic_set_irq(s
->irq
, irq
);
170 static void slavio_serial_reset_chn(ChannelState
*s
)
175 for (i
= 0; i
< SERIAL_MAXADDR
; i
++) {
188 s
->rxint
= s
->txint
= 0;
189 s
->rxint_under_svc
= s
->txint_under_svc
= 0;
192 static void slavio_serial_reset(void *opaque
)
194 SerialState
*s
= opaque
;
195 slavio_serial_reset_chn(&s
->chn
[0]);
196 slavio_serial_reset_chn(&s
->chn
[1]);
199 static inline void clr_rxint(ChannelState
*s
)
202 s
->rxint_under_svc
= 0;
204 s
->rregs
[3] &= ~0x20;
206 s
->otherchn
->rregs
[3] &= ~4;
211 slavio_serial_update_irq(s
);
214 static inline void set_rxint(ChannelState
*s
)
217 if (!s
->txint_under_svc
) {
218 s
->rxint_under_svc
= 1;
222 s
->otherchn
->rregs
[3] |= 4;
224 slavio_serial_update_irq(s
);
228 static inline void clr_txint(ChannelState
*s
)
231 s
->txint_under_svc
= 0;
233 s
->rregs
[3] &= ~0x10;
235 s
->otherchn
->rregs
[3] &= ~2;
240 slavio_serial_update_irq(s
);
243 static inline void set_txint(ChannelState
*s
)
246 if (!s
->rxint_under_svc
) {
247 s
->txint_under_svc
= 1;
251 s
->otherchn
->rregs
[3] |= 2;
253 slavio_serial_update_irq(s
);
257 static void slavio_serial_update_parameters(ChannelState
*s
)
259 int speed
, parity
, data_bits
, stop_bits
;
260 QEMUSerialSetParams ssp
;
262 if (!s
->chr
|| s
->type
!= ser
)
265 if (s
->wregs
[4] & 1) {
273 if ((s
->wregs
[4] & 0x0c) == 0x0c)
277 switch (s
->wregs
[5] & 0x60) {
292 speed
= 2457600 / ((s
->wregs
[12] | (s
->wregs
[13] << 8)) + 2);
293 switch (s
->wregs
[4] & 0xc0) {
309 ssp
.data_bits
= data_bits
;
310 ssp
.stop_bits
= stop_bits
;
311 SER_DPRINTF("channel %c: speed=%d parity=%c data=%d stop=%d\n", CHN_C(s
),
312 speed
, parity
, data_bits
, stop_bits
);
313 qemu_chr_ioctl(s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
316 static void slavio_serial_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
318 SerialState
*ser
= opaque
;
324 saddr
= (addr
& 3) >> 1;
325 channel
= (addr
& SERIAL_MAXADDR
) >> 2;
326 s
= &ser
->chn
[channel
];
329 SER_DPRINTF("Write channel %c, reg[%d] = %2.2x\n", CHN_C(s
), s
->reg
, val
& 0xff);
343 if (s
->rxint_under_svc
)
345 else if (s
->txint_under_svc
)
356 s
->wregs
[s
->reg
] = val
;
362 s
->wregs
[s
->reg
] = val
;
363 slavio_serial_update_parameters(s
);
366 switch (val
& 0xc0) {
371 slavio_serial_reset_chn(&ser
->chn
[1]);
374 slavio_serial_reset_chn(&ser
->chn
[0]);
377 slavio_serial_reset(ser
);
390 SER_DPRINTF("Write channel %c, ch %d\n", CHN_C(s
), val
);
391 if (s
->wregs
[5] & 8) { // tx enabled
394 qemu_chr_write(s
->chr
, &s
->tx
, 1);
395 else if (s
->type
== kbd
) {
396 handle_kbd_command(s
, val
);
398 s
->rregs
[0] |= 4; // Tx buffer empty
399 s
->rregs
[1] |= 1; // All sent
408 static uint32_t slavio_serial_mem_readb(void *opaque
, target_phys_addr_t addr
)
410 SerialState
*ser
= opaque
;
416 saddr
= (addr
& 3) >> 1;
417 channel
= (addr
& SERIAL_MAXADDR
) >> 2;
418 s
= &ser
->chn
[channel
];
421 SER_DPRINTF("Read channel %c, reg[%d] = %2.2x\n", CHN_C(s
), s
->reg
, s
->rregs
[s
->reg
]);
422 ret
= s
->rregs
[s
->reg
];
428 if (s
->type
== kbd
|| s
->type
== mouse
)
432 SER_DPRINTF("Read channel %c, ch %d\n", CHN_C(s
), ret
);
440 static int serial_can_receive(void *opaque
)
442 ChannelState
*s
= opaque
;
445 if (((s
->wregs
[3] & 1) == 0) // Rx not enabled
446 || ((s
->rregs
[0] & 1) == 1)) // char already available
450 //SER_DPRINTF("channel %c can receive %d\n", CHN_C(s), ret);
454 static void serial_receive_byte(ChannelState
*s
, int ch
)
456 SER_DPRINTF("channel %c put ch %d\n", CHN_C(s
), ch
);
462 static void serial_receive_break(ChannelState
*s
)
465 slavio_serial_update_irq(s
);
468 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
470 ChannelState
*s
= opaque
;
471 serial_receive_byte(s
, buf
[0]);
474 static void serial_event(void *opaque
, int event
)
476 ChannelState
*s
= opaque
;
477 if (event
== CHR_EVENT_BREAK
)
478 serial_receive_break(s
);
481 static CPUReadMemoryFunc
*slavio_serial_mem_read
[3] = {
482 slavio_serial_mem_readb
,
483 slavio_serial_mem_readb
,
484 slavio_serial_mem_readb
,
487 static CPUWriteMemoryFunc
*slavio_serial_mem_write
[3] = {
488 slavio_serial_mem_writeb
,
489 slavio_serial_mem_writeb
,
490 slavio_serial_mem_writeb
,
493 static void slavio_serial_save_chn(QEMUFile
*f
, ChannelState
*s
)
495 qemu_put_be32s(f
, &s
->irq
);
496 qemu_put_be32s(f
, &s
->reg
);
497 qemu_put_be32s(f
, &s
->rxint
);
498 qemu_put_be32s(f
, &s
->txint
);
499 qemu_put_be32s(f
, &s
->rxint_under_svc
);
500 qemu_put_be32s(f
, &s
->txint_under_svc
);
501 qemu_put_8s(f
, &s
->rx
);
502 qemu_put_8s(f
, &s
->tx
);
503 qemu_put_buffer(f
, s
->wregs
, 16);
504 qemu_put_buffer(f
, s
->rregs
, 16);
507 static void slavio_serial_save(QEMUFile
*f
, void *opaque
)
509 SerialState
*s
= opaque
;
511 slavio_serial_save_chn(f
, &s
->chn
[0]);
512 slavio_serial_save_chn(f
, &s
->chn
[1]);
515 static int slavio_serial_load_chn(QEMUFile
*f
, ChannelState
*s
, int version_id
)
520 qemu_get_be32s(f
, &s
->irq
);
521 qemu_get_be32s(f
, &s
->reg
);
522 qemu_get_be32s(f
, &s
->rxint
);
523 qemu_get_be32s(f
, &s
->txint
);
524 if (version_id
>= 2) {
525 qemu_get_be32s(f
, &s
->rxint_under_svc
);
526 qemu_get_be32s(f
, &s
->txint_under_svc
);
528 qemu_get_8s(f
, &s
->rx
);
529 qemu_get_8s(f
, &s
->tx
);
530 qemu_get_buffer(f
, s
->wregs
, 16);
531 qemu_get_buffer(f
, s
->rregs
, 16);
535 static int slavio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
537 SerialState
*s
= opaque
;
540 ret
= slavio_serial_load_chn(f
, &s
->chn
[0], version_id
);
543 ret
= slavio_serial_load_chn(f
, &s
->chn
[1], version_id
);
548 SerialState
*slavio_serial_init(int base
, int irq
, CharDriverState
*chr1
, CharDriverState
*chr2
)
550 int slavio_serial_io_memory
, i
;
553 s
= qemu_mallocz(sizeof(SerialState
));
557 slavio_serial_io_memory
= cpu_register_io_memory(0, slavio_serial_mem_read
, slavio_serial_mem_write
, s
);
558 cpu_register_physical_memory(base
, SERIAL_MAXADDR
, slavio_serial_io_memory
);
560 s
->chn
[0].chr
= chr1
;
561 s
->chn
[1].chr
= chr2
;
563 for (i
= 0; i
< 2; i
++) {
565 s
->chn
[i
].chn
= 1 - i
;
566 s
->chn
[i
].type
= ser
;
568 qemu_chr_add_read_handler(s
->chn
[i
].chr
, serial_can_receive
, serial_receive1
, &s
->chn
[i
]);
569 qemu_chr_add_event_handler(s
->chn
[i
].chr
, serial_event
);
572 s
->chn
[0].otherchn
= &s
->chn
[1];
573 s
->chn
[1].otherchn
= &s
->chn
[0];
574 register_savevm("slavio_serial", base
, 2, slavio_serial_save
, slavio_serial_load
, s
);
575 qemu_register_reset(slavio_serial_reset
, s
);
576 slavio_serial_reset(s
);
580 static const uint8_t keycodes
[128] = {
581 127, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 43, 53,
582 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 89, 76, 77, 78,
583 79, 80, 81, 82, 83, 84, 85, 86, 87, 42, 99, 88, 100, 101, 102, 103,
584 104, 105, 106, 107, 108, 109, 110, 47, 19, 121, 119, 5, 6, 8, 10, 12,
585 14, 16, 17, 18, 7, 98, 23, 68, 69, 70, 71, 91, 92, 93, 125, 112,
586 113, 114, 94, 50, 0, 0, 124, 9, 11, 0, 0, 0, 0, 0, 0, 0,
587 90, 0, 46, 22, 13, 111, 52, 20, 96, 24, 28, 74, 27, 123, 44, 66,
588 0, 45, 2, 4, 48, 0, 0, 21, 0, 0, 0, 0, 0, 120, 122, 67,
591 static void sunkbd_event(void *opaque
, int ch
)
593 ChannelState
*s
= opaque
;
594 int release
= ch
& 0x80;
596 ch
= keycodes
[ch
& 0x7f];
597 KBD_DPRINTF("Keycode %d (%s)\n", ch
, release
? "release" : "press");
598 put_queue(s
, ch
| release
);
601 static void handle_kbd_command(ChannelState
*s
, int val
)
603 KBD_DPRINTF("Command %d\n", val
);
605 case 1: // Reset, return type code
607 put_queue(s
, 5); // Type 5
609 case 7: // Query layout
611 put_queue(s
, 0x20); // XXX, layout?
618 static void sunmouse_event(void *opaque
,
619 int dx
, int dy
, int dz
, int buttons_state
)
621 ChannelState
*s
= opaque
;
624 /* XXX: SDL sometimes generates nul events: we delete them */
625 if (dx
== 0 && dy
== 0 && dz
== 0 && buttons_state
== 0)
627 MS_DPRINTF("dx=%d dy=%d buttons=%01x\n", dx
, dy
, buttons_state
);
629 ch
= 0x80 | 0x7; /* protocol start byte, no buttons pressed */
631 if (buttons_state
& MOUSE_EVENT_LBUTTON
)
633 if (buttons_state
& MOUSE_EVENT_MBUTTON
)
635 if (buttons_state
& MOUSE_EVENT_RBUTTON
)
647 put_queue(s
, ch
& 0xff);
656 put_queue(s
, ch
& 0xff);
658 // MSC protocol specify two extra motion bytes
664 void slavio_serial_ms_kbd_init(int base
, int irq
)
666 int slavio_serial_io_memory
, i
;
669 s
= qemu_mallocz(sizeof(SerialState
));
672 for (i
= 0; i
< 2; i
++) {
674 s
->chn
[i
].chn
= 1 - i
;
675 s
->chn
[i
].chr
= NULL
;
677 s
->chn
[0].otherchn
= &s
->chn
[1];
678 s
->chn
[1].otherchn
= &s
->chn
[0];
679 s
->chn
[0].type
= mouse
;
680 s
->chn
[1].type
= kbd
;
682 slavio_serial_io_memory
= cpu_register_io_memory(0, slavio_serial_mem_read
, slavio_serial_mem_write
, s
);
683 cpu_register_physical_memory(base
, SERIAL_MAXADDR
, slavio_serial_io_memory
);
685 qemu_add_mouse_event_handler(sunmouse_event
, &s
->chn
[0], 0);
686 qemu_add_kbd_event_handler(sunkbd_event
, &s
->chn
[1]);
687 qemu_register_reset(slavio_serial_reset
, s
);
688 slavio_serial_reset(s
);