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)
56 #define SER_DPRINTF(fmt, args...)
59 #define KBD_DPRINTF(fmt, args...) \
60 do { printf("KBD: " fmt , ##args); } while (0)
62 #define KBD_DPRINTF(fmt, args...)
65 #define MS_DPRINTF(fmt, args...) \
66 do { printf("MSC: " fmt , ##args); } while (0)
68 #define MS_DPRINTF(fmt, args...)
75 #define CHN_C(s) ((s)->chn == chn_b? 'b' : 'a')
81 #define SERIO_QUEUE_SIZE 256
84 uint8_t data[SERIO_QUEUE_SIZE];
85 int rptr, wptr, count;
88 typedef struct ChannelState {
91 int rxint, txint, rxint_under_svc, txint_under_svc;
92 chn_id_t chn; // this channel, A (base+4) or B (base+0)
94 struct ChannelState *otherchn;
95 uint8_t rx, tx, wregs[16], rregs[16];
98 int e0_mode, led_mode, caps_lock_mode, num_lock_mode;
102 struct ChannelState chn[2];
105 #define SERIAL_MAXADDR 7
106 #define SERIAL_SIZE (SERIAL_MAXADDR + 1)
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 clear_queue(void *opaque)
115 ChannelState *s = opaque;
116 SERIOQueue *q = &s->queue;
117 q->rptr = q->wptr = q->count = 0;
120 static void put_queue(void *opaque, int b)
122 ChannelState *s = opaque;
123 SERIOQueue *q = &s->queue;
125 SER_DPRINTF("channel %c put: 0x%02x\n", CHN_C(s), b);
126 if (q->count >= SERIO_QUEUE_SIZE)
128 q->data[q->wptr] = b;
129 if (++q->wptr == SERIO_QUEUE_SIZE)
132 serial_receive_byte(s, 0);
135 static uint32_t get_queue(void *opaque)
137 ChannelState *s = opaque;
138 SERIOQueue *q = &s->queue;
144 val = q->data[q->rptr];
145 if (++q->rptr == SERIO_QUEUE_SIZE)
149 SER_DPRINTF("channel %c get 0x%02x\n", CHN_C(s), val);
151 serial_receive_byte(s, 0);
155 static int slavio_serial_update_irq_chn(ChannelState *s)
157 if ((s->wregs[1] & 1) && // interrupts enabled
158 (((s->wregs[1] & 2) && s->txint == 1) || // tx ints enabled, pending
159 ((((s->wregs[1] & 0x18) == 8) || ((s->wregs[1] & 0x18) == 0x10)) &&
160 s->rxint == 1) || // rx ints enabled, pending
161 ((s->wregs[15] & 0x80) && (s->rregs[0] & 0x80)))) { // break int e&p
167 static void slavio_serial_update_irq(ChannelState *s)
171 irq = slavio_serial_update_irq_chn(s);
172 irq |= slavio_serial_update_irq_chn(s->otherchn);
174 SER_DPRINTF("IRQ = %d\n", irq);
175 qemu_set_irq(s->irq, irq);
178 static void slavio_serial_reset_chn(ChannelState *s)
183 for (i = 0; i < SERIAL_SIZE; i++) {
196 s->rxint = s->txint = 0;
197 s->rxint_under_svc = s->txint_under_svc = 0;
198 s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0;
202 static void slavio_serial_reset(void *opaque)
204 SerialState *s = opaque;
205 slavio_serial_reset_chn(&s->chn[0]);
206 slavio_serial_reset_chn(&s->chn[1]);
209 static inline void clr_rxint(ChannelState *s)
212 s->rxint_under_svc = 0;
213 if (s->chn == chn_a) {
214 if (s->wregs[9] & 0x10)
215 s->otherchn->rregs[2] = 0x60;
217 s->otherchn->rregs[2] = 0x06;
218 s->rregs[3] &= ~0x20;
220 if (s->wregs[9] & 0x10)
224 s->otherchn->rregs[3] &= ~4;
228 slavio_serial_update_irq(s);
231 static inline void set_rxint(ChannelState *s)
234 if (!s->txint_under_svc) {
235 s->rxint_under_svc = 1;
236 if (s->chn == chn_a) {
237 if (s->wregs[9] & 0x10)
238 s->otherchn->rregs[2] = 0x30;
240 s->otherchn->rregs[2] = 0x0c;
242 if (s->wregs[9] & 0x10)
251 s->otherchn->rregs[3] |= 4;
252 slavio_serial_update_irq(s);
255 static inline void clr_txint(ChannelState *s)
258 s->txint_under_svc = 0;
259 if (s->chn == chn_a) {
260 if (s->wregs[9] & 0x10)
261 s->otherchn->rregs[2] = 0x60;
263 s->otherchn->rregs[2] = 0x06;
264 s->rregs[3] &= ~0x10;
266 if (s->wregs[9] & 0x10)
270 s->otherchn->rregs[3] &= ~2;
274 slavio_serial_update_irq(s);
277 static inline void set_txint(ChannelState *s)
280 if (!s->rxint_under_svc) {
281 s->txint_under_svc = 1;
282 if (s->chn == chn_a) {
283 if (s->wregs[9] & 0x10)
284 s->otherchn->rregs[2] = 0x10;
286 s->otherchn->rregs[2] = 0x08;
294 s->otherchn->rregs[3] |= 2;
295 slavio_serial_update_irq(s);
298 static void slavio_serial_update_parameters(ChannelState *s)
300 int speed, parity, data_bits, stop_bits;
301 QEMUSerialSetParams ssp;
303 if (!s->chr || s->type != ser)
306 if (s->wregs[4] & 1) {
314 if ((s->wregs[4] & 0x0c) == 0x0c)
318 switch (s->wregs[5] & 0x60) {
333 speed = 2457600 / ((s->wregs[12] | (s->wregs[13] << 8)) + 2);
334 switch (s->wregs[4] & 0xc0) {
350 ssp.data_bits = data_bits;
351 ssp.stop_bits = stop_bits;
352 SER_DPRINTF("channel %c: speed=%d parity=%c data=%d stop=%d\n", CHN_C(s),
353 speed, parity, data_bits, stop_bits);
354 qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
357 static void slavio_serial_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
359 SerialState *serial = opaque;
365 saddr = (addr & 3) >> 1;
366 channel = (addr & SERIAL_MAXADDR) >> 2;
367 s = &serial->chn[channel];
370 SER_DPRINTF("Write channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg, val & 0xff);
384 if (s->rxint_under_svc)
386 else if (s->txint_under_svc)
397 s->wregs[s->reg] = val;
403 s->wregs[s->reg] = val;
404 slavio_serial_update_parameters(s);
407 switch (val & 0xc0) {
412 slavio_serial_reset_chn(&serial->chn[1]);
415 slavio_serial_reset_chn(&serial->chn[0]);
418 slavio_serial_reset(serial);
431 SER_DPRINTF("Write channel %c, ch %d\n", CHN_C(s), val);
433 if (s->wregs[5] & 8) { // tx enabled
435 qemu_chr_write(s->chr, &s->tx, 1);
436 else if (s->type == kbd) {
437 handle_kbd_command(s, val);
440 s->rregs[0] |= 4; // Tx buffer empty
441 s->rregs[1] |= 1; // All sent
449 static uint32_t slavio_serial_mem_readb(void *opaque, target_phys_addr_t addr)
451 SerialState *serial = opaque;
457 saddr = (addr & 3) >> 1;
458 channel = (addr & SERIAL_MAXADDR) >> 2;
459 s = &serial->chn[channel];
462 SER_DPRINTF("Read channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg, s->rregs[s->reg]);
463 ret = s->rregs[s->reg];
469 if (s->type == kbd || s->type == mouse)
473 SER_DPRINTF("Read channel %c, ch %d\n", CHN_C(s), ret);
481 static int serial_can_receive(void *opaque)
483 ChannelState *s = opaque;
486 if (((s->wregs[3] & 1) == 0) // Rx not enabled
487 || ((s->rregs[0] & 1) == 1)) // char already available
491 //SER_DPRINTF("channel %c can receive %d\n", CHN_C(s), ret);
495 static void serial_receive_byte(ChannelState *s, int ch)
497 SER_DPRINTF("channel %c put ch %d\n", CHN_C(s), ch);
503 static void serial_receive_break(ChannelState *s)
506 slavio_serial_update_irq(s);
509 static void serial_receive1(void *opaque, const uint8_t *buf, int size)
511 ChannelState *s = opaque;
512 serial_receive_byte(s, buf[0]);
515 static void serial_event(void *opaque, int event)
517 ChannelState *s = opaque;
518 if (event == CHR_EVENT_BREAK)
519 serial_receive_break(s);
522 static CPUReadMemoryFunc *slavio_serial_mem_read[3] = {
523 slavio_serial_mem_readb,
524 slavio_serial_mem_readb,
525 slavio_serial_mem_readb,
528 static CPUWriteMemoryFunc *slavio_serial_mem_write[3] = {
529 slavio_serial_mem_writeb,
530 slavio_serial_mem_writeb,
531 slavio_serial_mem_writeb,
534 static void slavio_serial_save_chn(QEMUFile *f, ChannelState *s)
538 qemu_put_be32s(f, &tmp); /* unused, was IRQ. */
539 qemu_put_be32s(f, &s->reg);
540 qemu_put_be32s(f, &s->rxint);
541 qemu_put_be32s(f, &s->txint);
542 qemu_put_be32s(f, &s->rxint_under_svc);
543 qemu_put_be32s(f, &s->txint_under_svc);
544 qemu_put_8s(f, &s->rx);
545 qemu_put_8s(f, &s->tx);
546 qemu_put_buffer(f, s->wregs, 16);
547 qemu_put_buffer(f, s->rregs, 16);
550 static void slavio_serial_save(QEMUFile *f, void *opaque)
552 SerialState *s = opaque;
554 slavio_serial_save_chn(f, &s->chn[0]);
555 slavio_serial_save_chn(f, &s->chn[1]);
558 static int slavio_serial_load_chn(QEMUFile *f, ChannelState *s, int version_id)
565 qemu_get_be32s(f, &tmp); /* unused */
566 qemu_get_be32s(f, &s->reg);
567 qemu_get_be32s(f, &s->rxint);
568 qemu_get_be32s(f, &s->txint);
569 if (version_id >= 2) {
570 qemu_get_be32s(f, &s->rxint_under_svc);
571 qemu_get_be32s(f, &s->txint_under_svc);
573 qemu_get_8s(f, &s->rx);
574 qemu_get_8s(f, &s->tx);
575 qemu_get_buffer(f, s->wregs, 16);
576 qemu_get_buffer(f, s->rregs, 16);
580 static int slavio_serial_load(QEMUFile *f, void *opaque, int version_id)
582 SerialState *s = opaque;
585 ret = slavio_serial_load_chn(f, &s->chn[0], version_id);
588 ret = slavio_serial_load_chn(f, &s->chn[1], version_id);
593 SerialState *slavio_serial_init(target_phys_addr_t base, qemu_irq irq,
594 CharDriverState *chr1, CharDriverState *chr2)
596 int slavio_serial_io_memory, i;
599 s = qemu_mallocz(sizeof(SerialState));
603 slavio_serial_io_memory = cpu_register_io_memory(0, slavio_serial_mem_read, slavio_serial_mem_write, s);
604 cpu_register_physical_memory(base, SERIAL_SIZE, slavio_serial_io_memory);
606 s->chn[0].chr = chr1;
607 s->chn[1].chr = chr2;
609 for (i = 0; i < 2; i++) {
611 s->chn[i].chn = 1 - i;
612 s->chn[i].type = ser;
614 qemu_chr_add_handlers(s->chn[i].chr, serial_can_receive,
615 serial_receive1, serial_event, &s->chn[i]);
618 s->chn[0].otherchn = &s->chn[1];
619 s->chn[1].otherchn = &s->chn[0];
620 register_savevm("slavio_serial", base, 2, slavio_serial_save, slavio_serial_load, s);
621 qemu_register_reset(slavio_serial_reset, s);
622 slavio_serial_reset(s);
626 static const uint8_t keycodes[128] = {
627 127, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 43, 53,
628 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 89, 76, 77, 78,
629 79, 80, 81, 82, 83, 84, 85, 86, 87, 42, 99, 88, 100, 101, 102, 103,
630 104, 105, 106, 107, 108, 109, 110, 47, 19, 121, 119, 5, 6, 8, 10, 12,
631 14, 16, 17, 18, 7, 98, 23, 68, 69, 70, 71, 91, 92, 93, 125, 112,
632 113, 114, 94, 50, 0, 0, 124, 9, 11, 0, 0, 0, 0, 0, 0, 0,
633 90, 0, 46, 22, 13, 111, 52, 20, 96, 24, 28, 74, 27, 123, 44, 66,
634 0, 45, 2, 4, 48, 0, 0, 21, 0, 0, 0, 0, 0, 120, 122, 67,
637 static const uint8_t e0_keycodes[128] = {
638 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
639 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 90, 76, 0, 0,
640 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
641 0, 0, 0, 0, 0, 109, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0,
642 0, 0, 0, 0, 0, 0, 0, 68, 69, 70, 0, 91, 0, 93, 0, 112,
643 113, 114, 94, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
644 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
645 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
648 static void sunkbd_event(void *opaque, int ch)
650 ChannelState *s = opaque;
651 int release = ch & 0x80;
653 KBD_DPRINTF("Untranslated keycode %2.2x (%s)\n", ch, release? "release" : "press");
655 case 58: // Caps lock press
656 s->caps_lock_mode ^= 1;
657 if (s->caps_lock_mode == 2)
658 return; // Drop second press
660 case 69: // Num lock press
661 s->num_lock_mode ^= 1;
662 if (s->num_lock_mode == 2)
663 return; // Drop second press
665 case 186: // Caps lock release
666 s->caps_lock_mode ^= 2;
667 if (s->caps_lock_mode == 3)
668 return; // Drop first release
670 case 197: // Num lock release
671 s->num_lock_mode ^= 2;
672 if (s->num_lock_mode == 3)
673 return; // Drop first release
683 ch = e0_keycodes[ch & 0x7f];
685 ch = keycodes[ch & 0x7f];
687 KBD_DPRINTF("Translated keycode %2.2x\n", ch);
688 put_queue(s, ch | release);
691 static void handle_kbd_command(ChannelState *s, int val)
693 KBD_DPRINTF("Command %d\n", val);
694 if (s->led_mode) { // Ignore led byte
699 case 1: // Reset, return type code
702 put_queue(s, 4); // Type 4
705 case 0xe: // Set leds
708 case 7: // Query layout
712 put_queue(s, 0); // XXX, layout?
719 static void sunmouse_event(void *opaque,
720 int dx, int dy, int dz, int buttons_state)
722 ChannelState *s = opaque;
725 MS_DPRINTF("dx=%d dy=%d buttons=%01x\n", dx, dy, buttons_state);
727 ch = 0x80 | 0x7; /* protocol start byte, no buttons pressed */
729 if (buttons_state & MOUSE_EVENT_LBUTTON)
731 if (buttons_state & MOUSE_EVENT_MBUTTON)
733 if (buttons_state & MOUSE_EVENT_RBUTTON)
745 put_queue(s, ch & 0xff);
754 put_queue(s, ch & 0xff);
756 // MSC protocol specify two extra motion bytes
762 void slavio_serial_ms_kbd_init(target_phys_addr_t base, qemu_irq irq)
764 int slavio_serial_io_memory, i;
767 s = qemu_mallocz(sizeof(SerialState));
770 for (i = 0; i < 2; i++) {
772 s->chn[i].chn = 1 - i;
773 s->chn[i].chr = NULL;
775 s->chn[0].otherchn = &s->chn[1];
776 s->chn[1].otherchn = &s->chn[0];
777 s->chn[0].type = mouse;
778 s->chn[1].type = kbd;
780 slavio_serial_io_memory = cpu_register_io_memory(0, slavio_serial_mem_read, slavio_serial_mem_write, s);
781 cpu_register_physical_memory(base, SERIAL_SIZE, slavio_serial_io_memory);
783 qemu_add_mouse_event_handler(sunmouse_event, &s->chn[0], 0, "QEMU Sun Mouse");
784 qemu_add_kbd_event_handler(sunkbd_event, &s->chn[1]);
785 register_savevm("slavio_serial_mouse", base, 2, slavio_serial_save, slavio_serial_load, s);
786 qemu_register_reset(slavio_serial_reset, s);
787 slavio_serial_reset(s);