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];
101 struct ChannelState chn[2];
104 #define SERIAL_MAXADDR 7
106 static void handle_kbd_command(ChannelState *s, int val);
107 static int serial_can_receive(void *opaque);
108 static void serial_receive_byte(ChannelState *s, int ch);
109 static inline void set_txint(ChannelState *s);
111 static void clear_queue(void *opaque)
113 ChannelState *s = opaque;
114 SERIOQueue *q = &s->queue;
115 q->rptr = q->wptr = q->count = 0;
118 static void put_queue(void *opaque, int b)
120 ChannelState *s = opaque;
121 SERIOQueue *q = &s->queue;
123 SER_DPRINTF("channel %c put: 0x%02x\n", CHN_C(s), b);
124 if (q->count >= SERIO_QUEUE_SIZE)
126 q->data[q->wptr] = b;
127 if (++q->wptr == SERIO_QUEUE_SIZE)
130 serial_receive_byte(s, 0);
133 static uint32_t get_queue(void *opaque)
135 ChannelState *s = opaque;
136 SERIOQueue *q = &s->queue;
142 val = q->data[q->rptr];
143 if (++q->rptr == SERIO_QUEUE_SIZE)
147 SER_DPRINTF("channel %c get 0x%02x\n", CHN_C(s), val);
149 serial_receive_byte(s, 0);
153 static int slavio_serial_update_irq_chn(ChannelState *s)
155 if ((s->wregs[1] & 1) && // interrupts enabled
156 (((s->wregs[1] & 2) && s->txint == 1) || // tx ints enabled, pending
157 ((((s->wregs[1] & 0x18) == 8) || ((s->wregs[1] & 0x18) == 0x10)) &&
158 s->rxint == 1) || // rx ints enabled, pending
159 ((s->wregs[15] & 0x80) && (s->rregs[0] & 0x80)))) { // break int e&p
165 static void slavio_serial_update_irq(ChannelState *s)
169 irq = slavio_serial_update_irq_chn(s);
170 irq |= slavio_serial_update_irq_chn(s->otherchn);
172 SER_DPRINTF("IRQ = %d\n", irq);
173 qemu_set_irq(s->irq, irq);
176 static void slavio_serial_reset_chn(ChannelState *s)
181 for (i = 0; i < SERIAL_MAXADDR; i++) {
194 s->rxint = s->txint = 0;
195 s->rxint_under_svc = s->txint_under_svc = 0;
199 static void slavio_serial_reset(void *opaque)
201 SerialState *s = opaque;
202 slavio_serial_reset_chn(&s->chn[0]);
203 slavio_serial_reset_chn(&s->chn[1]);
206 static inline void clr_rxint(ChannelState *s)
209 s->rxint_under_svc = 0;
210 if (s->chn == chn_a) {
211 if (s->wregs[9] & 0x10)
212 s->otherchn->rregs[2] = 0x60;
214 s->otherchn->rregs[2] = 0x06;
215 s->rregs[3] &= ~0x20;
217 if (s->wregs[9] & 0x10)
221 s->otherchn->rregs[3] &= ~4;
227 slavio_serial_update_irq(s);
230 static inline void set_rxint(ChannelState *s)
233 if (!s->txint_under_svc) {
234 s->rxint_under_svc = 1;
235 if (s->chn == chn_a) {
236 if (s->wregs[9] & 0x10)
237 s->otherchn->rregs[2] = 0x30;
239 s->otherchn->rregs[2] = 0x0c;
242 if (s->wregs[9] & 0x10)
246 s->otherchn->rregs[3] |= 4;
248 slavio_serial_update_irq(s);
252 static inline void clr_txint(ChannelState *s)
255 s->txint_under_svc = 0;
257 s->rregs[3] &= ~0x10;
259 s->otherchn->rregs[3] &= ~2;
264 slavio_serial_update_irq(s);
267 static inline void set_txint(ChannelState *s)
270 if (!s->rxint_under_svc) {
271 s->txint_under_svc = 1;
275 s->otherchn->rregs[3] |= 2;
277 slavio_serial_update_irq(s);
281 static void slavio_serial_update_parameters(ChannelState *s)
283 int speed, parity, data_bits, stop_bits;
284 QEMUSerialSetParams ssp;
286 if (!s->chr || s->type != ser)
289 if (s->wregs[4] & 1) {
297 if ((s->wregs[4] & 0x0c) == 0x0c)
301 switch (s->wregs[5] & 0x60) {
316 speed = 2457600 / ((s->wregs[12] | (s->wregs[13] << 8)) + 2);
317 switch (s->wregs[4] & 0xc0) {
333 ssp.data_bits = data_bits;
334 ssp.stop_bits = stop_bits;
335 SER_DPRINTF("channel %c: speed=%d parity=%c data=%d stop=%d\n", CHN_C(s),
336 speed, parity, data_bits, stop_bits);
337 qemu_chr_ioctl(s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
340 static void slavio_serial_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
342 SerialState *ser = opaque;
348 saddr = (addr & 3) >> 1;
349 channel = (addr & SERIAL_MAXADDR) >> 2;
350 s = &ser->chn[channel];
353 SER_DPRINTF("Write channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg, val & 0xff);
367 if (s->rxint_under_svc)
369 else if (s->txint_under_svc)
380 s->wregs[s->reg] = val;
386 s->wregs[s->reg] = val;
387 slavio_serial_update_parameters(s);
390 switch (val & 0xc0) {
395 slavio_serial_reset_chn(&ser->chn[1]);
398 slavio_serial_reset_chn(&ser->chn[0]);
401 slavio_serial_reset(ser);
414 SER_DPRINTF("Write channel %c, ch %d\n", CHN_C(s), val);
415 if (s->wregs[5] & 8) { // tx enabled
418 qemu_chr_write(s->chr, &s->tx, 1);
419 else if (s->type == kbd) {
420 handle_kbd_command(s, val);
422 s->rregs[0] |= 4; // Tx buffer empty
423 s->rregs[1] |= 1; // All sent
432 static uint32_t slavio_serial_mem_readb(void *opaque, target_phys_addr_t addr)
434 SerialState *ser = opaque;
440 saddr = (addr & 3) >> 1;
441 channel = (addr & SERIAL_MAXADDR) >> 2;
442 s = &ser->chn[channel];
445 SER_DPRINTF("Read channel %c, reg[%d] = %2.2x\n", CHN_C(s), s->reg, s->rregs[s->reg]);
446 ret = s->rregs[s->reg];
452 if (s->type == kbd || s->type == mouse)
456 SER_DPRINTF("Read channel %c, ch %d\n", CHN_C(s), ret);
464 static int serial_can_receive(void *opaque)
466 ChannelState *s = opaque;
469 if (((s->wregs[3] & 1) == 0) // Rx not enabled
470 || ((s->rregs[0] & 1) == 1)) // char already available
474 //SER_DPRINTF("channel %c can receive %d\n", CHN_C(s), ret);
478 static void serial_receive_byte(ChannelState *s, int ch)
480 SER_DPRINTF("channel %c put ch %d\n", CHN_C(s), ch);
486 static void serial_receive_break(ChannelState *s)
489 slavio_serial_update_irq(s);
492 static void serial_receive1(void *opaque, const uint8_t *buf, int size)
494 ChannelState *s = opaque;
495 serial_receive_byte(s, buf[0]);
498 static void serial_event(void *opaque, int event)
500 ChannelState *s = opaque;
501 if (event == CHR_EVENT_BREAK)
502 serial_receive_break(s);
505 static CPUReadMemoryFunc *slavio_serial_mem_read[3] = {
506 slavio_serial_mem_readb,
507 slavio_serial_mem_readb,
508 slavio_serial_mem_readb,
511 static CPUWriteMemoryFunc *slavio_serial_mem_write[3] = {
512 slavio_serial_mem_writeb,
513 slavio_serial_mem_writeb,
514 slavio_serial_mem_writeb,
517 static void slavio_serial_save_chn(QEMUFile *f, ChannelState *s)
521 qemu_put_be32s(f, &tmp); /* unused, was IRQ. */
522 qemu_put_be32s(f, &s->reg);
523 qemu_put_be32s(f, &s->rxint);
524 qemu_put_be32s(f, &s->txint);
525 qemu_put_be32s(f, &s->rxint_under_svc);
526 qemu_put_be32s(f, &s->txint_under_svc);
527 qemu_put_8s(f, &s->rx);
528 qemu_put_8s(f, &s->tx);
529 qemu_put_buffer(f, s->wregs, 16);
530 qemu_put_buffer(f, s->rregs, 16);
533 static void slavio_serial_save(QEMUFile *f, void *opaque)
535 SerialState *s = opaque;
537 slavio_serial_save_chn(f, &s->chn[0]);
538 slavio_serial_save_chn(f, &s->chn[1]);
541 static int slavio_serial_load_chn(QEMUFile *f, ChannelState *s, int version_id)
548 qemu_get_be32s(f, &tmp); /* unused */
549 qemu_get_be32s(f, &s->reg);
550 qemu_get_be32s(f, &s->rxint);
551 qemu_get_be32s(f, &s->txint);
552 if (version_id >= 2) {
553 qemu_get_be32s(f, &s->rxint_under_svc);
554 qemu_get_be32s(f, &s->txint_under_svc);
556 qemu_get_8s(f, &s->rx);
557 qemu_get_8s(f, &s->tx);
558 qemu_get_buffer(f, s->wregs, 16);
559 qemu_get_buffer(f, s->rregs, 16);
563 static int slavio_serial_load(QEMUFile *f, void *opaque, int version_id)
565 SerialState *s = opaque;
568 ret = slavio_serial_load_chn(f, &s->chn[0], version_id);
571 ret = slavio_serial_load_chn(f, &s->chn[1], version_id);
576 SerialState *slavio_serial_init(int base, qemu_irq irq, CharDriverState *chr1,
577 CharDriverState *chr2)
579 int slavio_serial_io_memory, i;
582 s = qemu_mallocz(sizeof(SerialState));
586 slavio_serial_io_memory = cpu_register_io_memory(0, slavio_serial_mem_read, slavio_serial_mem_write, s);
587 cpu_register_physical_memory(base, SERIAL_MAXADDR, slavio_serial_io_memory);
589 s->chn[0].chr = chr1;
590 s->chn[1].chr = chr2;
592 for (i = 0; i < 2; i++) {
594 s->chn[i].chn = 1 - i;
595 s->chn[i].type = ser;
597 qemu_chr_add_handlers(s->chn[i].chr, serial_can_receive,
598 serial_receive1, serial_event, &s->chn[i]);
601 s->chn[0].otherchn = &s->chn[1];
602 s->chn[1].otherchn = &s->chn[0];
603 register_savevm("slavio_serial", base, 2, slavio_serial_save, slavio_serial_load, s);
604 qemu_register_reset(slavio_serial_reset, s);
605 slavio_serial_reset(s);
609 static const uint8_t keycodes[128] = {
610 127, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 43, 53,
611 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 89, 76, 77, 78,
612 79, 80, 81, 82, 83, 84, 85, 86, 87, 42, 99, 88, 100, 101, 102, 103,
613 104, 105, 106, 107, 108, 109, 110, 47, 19, 121, 119, 5, 6, 8, 10, 12,
614 14, 16, 17, 18, 7, 98, 23, 68, 69, 70, 71, 91, 92, 93, 125, 112,
615 113, 114, 94, 50, 0, 0, 124, 9, 11, 0, 0, 0, 0, 0, 0, 0,
616 90, 0, 46, 22, 13, 111, 52, 20, 96, 24, 28, 74, 27, 123, 44, 66,
617 0, 45, 2, 4, 48, 0, 0, 21, 0, 0, 0, 0, 0, 120, 122, 67,
620 static void sunkbd_event(void *opaque, int ch)
622 ChannelState *s = opaque;
623 int release = ch & 0x80;
625 ch = keycodes[ch & 0x7f];
626 KBD_DPRINTF("Keycode %d (%s)\n", ch, release? "release" : "press");
627 put_queue(s, ch | release);
630 static void handle_kbd_command(ChannelState *s, int val)
632 KBD_DPRINTF("Command %d\n", val);
634 case 1: // Reset, return type code
637 put_queue(s, 4); // Type 4
639 case 7: // Query layout
643 put_queue(s, 19); // XXX, layout?
650 static void sunmouse_event(void *opaque,
651 int dx, int dy, int dz, int buttons_state)
653 ChannelState *s = opaque;
656 MS_DPRINTF("dx=%d dy=%d buttons=%01x\n", dx, dy, buttons_state);
658 ch = 0x80 | 0x7; /* protocol start byte, no buttons pressed */
660 if (buttons_state & MOUSE_EVENT_LBUTTON)
662 if (buttons_state & MOUSE_EVENT_MBUTTON)
664 if (buttons_state & MOUSE_EVENT_RBUTTON)
676 put_queue(s, ch & 0xff);
685 put_queue(s, ch & 0xff);
687 // MSC protocol specify two extra motion bytes
693 void slavio_serial_ms_kbd_init(int base, qemu_irq irq)
695 int slavio_serial_io_memory, i;
698 s = qemu_mallocz(sizeof(SerialState));
701 for (i = 0; i < 2; i++) {
703 s->chn[i].chn = 1 - i;
704 s->chn[i].chr = NULL;
706 s->chn[0].otherchn = &s->chn[1];
707 s->chn[1].otherchn = &s->chn[0];
708 s->chn[0].type = mouse;
709 s->chn[1].type = kbd;
711 slavio_serial_io_memory = cpu_register_io_memory(0, slavio_serial_mem_read, slavio_serial_mem_write, s);
712 cpu_register_physical_memory(base, SERIAL_MAXADDR, slavio_serial_io_memory);
714 qemu_add_mouse_event_handler(sunmouse_event, &s->chn[0], 0, "QEMU Sun Mouse");
715 qemu_add_kbd_event_handler(sunkbd_event, &s->chn[1]);
716 register_savevm("slavio_serial_mouse", base, 2, slavio_serial_save, slavio_serial_load, s);
717 qemu_register_reset(slavio_serial_reset, s);
718 slavio_serial_reset(s);