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.
46 #define SER_DPRINTF(fmt, args...) \
47 do { printf("SER: " fmt , ##args); } while (0)
48 #define pic_set_irq(irq, level) \
49 do { printf("SER: set_irq(%d): %d\n", (irq), (level)); pic_set_irq((irq),(level));} while (0)
51 #define SER_DPRINTF(fmt, args...)
54 #define KBD_DPRINTF(fmt, args...) \
55 do { printf("KBD: " fmt , ##args); } while (0)
57 #define KBD_DPRINTF(fmt, args...)
60 #define MS_DPRINTF(fmt, args...) \
61 do { printf("SER: " fmt , ##args); } while (0)
63 #define MS_DPRINTF(fmt, args...)
74 #define KBD_QUEUE_SIZE 256
77 uint8_t data
[KBD_QUEUE_SIZE
];
78 int rptr
, wptr
, count
;
81 typedef struct ChannelState
{
85 chn_id_t chn
; // this channel, A (base+4) or B (base+0)
87 struct ChannelState
*otherchn
;
88 uint8_t rx
, tx
, wregs
[16], rregs
[16];
94 struct ChannelState chn
[2];
97 #define SERIAL_MAXADDR 7
99 static void handle_kbd_command(ChannelState
*s
, int val
);
100 static int serial_can_receive(void *opaque
);
101 static void serial_receive_byte(ChannelState
*s
, int ch
);
103 static void put_queue(void *opaque
, int b
)
105 ChannelState
*s
= opaque
;
106 KBDQueue
*q
= &s
->queue
;
108 KBD_DPRINTF("put: 0x%02x\n", b
);
109 if (q
->count
>= KBD_QUEUE_SIZE
)
111 q
->data
[q
->wptr
] = b
;
112 if (++q
->wptr
== KBD_QUEUE_SIZE
)
115 serial_receive_byte(s
, 0);
118 static uint32_t get_queue(void *opaque
)
120 ChannelState
*s
= opaque
;
121 KBDQueue
*q
= &s
->queue
;
127 val
= q
->data
[q
->rptr
];
128 if (++q
->rptr
== KBD_QUEUE_SIZE
)
132 KBD_DPRINTF("get 0x%02x\n", val
);
134 serial_receive_byte(s
, 0);
138 static void slavio_serial_update_irq(ChannelState
*s
)
140 if ((s
->wregs
[1] & 1) && // interrupts enabled
141 (((s
->wregs
[1] & 2) && s
->txint
== 1) || // tx ints enabled, pending
142 ((((s
->wregs
[1] & 0x18) == 8) || ((s
->wregs
[1] & 0x18) == 0x10)) &&
143 s
->rxint
== 1) || // rx ints enabled, pending
144 ((s
->wregs
[15] & 0x80) && (s
->rregs
[0] & 0x80)))) { // break int e&p
145 pic_set_irq(s
->irq
, 1);
147 pic_set_irq(s
->irq
, 0);
151 static void slavio_serial_reset_chn(ChannelState
*s
)
156 for (i
= 0; i
< SERIAL_MAXADDR
; i
++) {
169 s
->rxint
= s
->txint
= 0;
172 static void slavio_serial_reset(void *opaque
)
174 SerialState
*s
= opaque
;
175 slavio_serial_reset_chn(&s
->chn
[0]);
176 slavio_serial_reset_chn(&s
->chn
[1]);
179 static inline void clr_rxint(ChannelState
*s
)
183 s
->rregs
[3] &= ~0x20;
185 s
->otherchn
->rregs
[3] &= ~4;
187 slavio_serial_update_irq(s
);
190 static inline void set_rxint(ChannelState
*s
)
196 s
->otherchn
->rregs
[3] |= 4;
198 slavio_serial_update_irq(s
);
201 static inline void clr_txint(ChannelState
*s
)
205 s
->rregs
[3] &= ~0x10;
207 s
->otherchn
->rregs
[3] &= ~2;
209 slavio_serial_update_irq(s
);
212 static inline void set_txint(ChannelState
*s
)
218 s
->otherchn
->rregs
[3] |= 2;
220 slavio_serial_update_irq(s
);
223 static void slavio_serial_mem_writeb(void *opaque
, target_phys_addr_t addr
, uint32_t val
)
225 SerialState
*ser
= opaque
;
231 saddr
= (addr
& 3) >> 1;
232 channel
= (addr
& SERIAL_MAXADDR
) >> 2;
233 s
= &ser
->chn
[channel
];
236 SER_DPRINTF("Write channel %c, reg[%d] = %2.2x\n", channel
? 'b' : 'a', s
->reg
, val
& 0xff);
262 s
->wregs
[s
->reg
] = val
;
265 switch (val
& 0xc0) {
270 slavio_serial_reset_chn(&ser
->chn
[1]);
273 slavio_serial_reset_chn(&ser
->chn
[0]);
276 slavio_serial_reset(ser
);
289 SER_DPRINTF("Write channel %c, ch %d\n", channel
? 'b' : 'a', val
);
290 if (s
->wregs
[5] & 8) { // tx enabled
293 qemu_chr_write(s
->chr
, &s
->tx
, 1);
294 else if (s
->type
== kbd
) {
295 handle_kbd_command(s
, val
);
298 s
->rregs
[0] |= 4; // Tx buffer empty
299 s
->rregs
[1] |= 1; // All sent
301 slavio_serial_update_irq(s
);
309 static uint32_t slavio_serial_mem_readb(void *opaque
, target_phys_addr_t addr
)
311 SerialState
*ser
= opaque
;
317 saddr
= (addr
& 3) >> 1;
318 channel
= (addr
& SERIAL_MAXADDR
) >> 2;
319 s
= &ser
->chn
[channel
];
322 SER_DPRINTF("Read channel %c, reg[%d] = %2.2x\n", channel
? 'b' : 'a', s
->reg
, s
->rregs
[s
->reg
]);
323 ret
= s
->rregs
[s
->reg
];
333 SER_DPRINTF("Read channel %c, ch %d\n", channel
? 'b' : 'a', ret
);
341 static int serial_can_receive(void *opaque
)
343 ChannelState
*s
= opaque
;
344 if (((s
->wregs
[3] & 1) == 0) // Rx not enabled
345 || ((s
->rregs
[0] & 1) == 1)) // char already available
351 static void serial_receive_byte(ChannelState
*s
, int ch
)
353 SER_DPRINTF("put ch %d\n", ch
);
359 static void serial_receive_break(ChannelState
*s
)
362 slavio_serial_update_irq(s
);
365 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
367 ChannelState
*s
= opaque
;
368 serial_receive_byte(s
, buf
[0]);
371 static void serial_event(void *opaque
, int event
)
373 ChannelState
*s
= opaque
;
374 if (event
== CHR_EVENT_BREAK
)
375 serial_receive_break(s
);
378 static CPUReadMemoryFunc
*slavio_serial_mem_read
[3] = {
379 slavio_serial_mem_readb
,
380 slavio_serial_mem_readb
,
381 slavio_serial_mem_readb
,
384 static CPUWriteMemoryFunc
*slavio_serial_mem_write
[3] = {
385 slavio_serial_mem_writeb
,
386 slavio_serial_mem_writeb
,
387 slavio_serial_mem_writeb
,
390 static void slavio_serial_save_chn(QEMUFile
*f
, ChannelState
*s
)
392 qemu_put_be32s(f
, &s
->irq
);
393 qemu_put_be32s(f
, &s
->reg
);
394 qemu_put_be32s(f
, &s
->rxint
);
395 qemu_put_be32s(f
, &s
->txint
);
396 qemu_put_8s(f
, &s
->rx
);
397 qemu_put_8s(f
, &s
->tx
);
398 qemu_put_buffer(f
, s
->wregs
, 16);
399 qemu_put_buffer(f
, s
->rregs
, 16);
402 static void slavio_serial_save(QEMUFile
*f
, void *opaque
)
404 SerialState
*s
= opaque
;
406 slavio_serial_save_chn(f
, &s
->chn
[0]);
407 slavio_serial_save_chn(f
, &s
->chn
[1]);
410 static int slavio_serial_load_chn(QEMUFile
*f
, ChannelState
*s
, int version_id
)
415 qemu_get_be32s(f
, &s
->irq
);
416 qemu_get_be32s(f
, &s
->reg
);
417 qemu_get_be32s(f
, &s
->rxint
);
418 qemu_get_be32s(f
, &s
->txint
);
419 qemu_get_8s(f
, &s
->rx
);
420 qemu_get_8s(f
, &s
->tx
);
421 qemu_get_buffer(f
, s
->wregs
, 16);
422 qemu_get_buffer(f
, s
->rregs
, 16);
426 static int slavio_serial_load(QEMUFile
*f
, void *opaque
, int version_id
)
428 SerialState
*s
= opaque
;
431 ret
= slavio_serial_load_chn(f
, &s
->chn
[0], version_id
);
434 ret
= slavio_serial_load_chn(f
, &s
->chn
[1], version_id
);
439 SerialState
*slavio_serial_init(int base
, int irq
, CharDriverState
*chr1
, CharDriverState
*chr2
)
441 int slavio_serial_io_memory
, i
;
444 s
= qemu_mallocz(sizeof(SerialState
));
448 slavio_serial_io_memory
= cpu_register_io_memory(0, slavio_serial_mem_read
, slavio_serial_mem_write
, s
);
449 cpu_register_physical_memory(base
, SERIAL_MAXADDR
, slavio_serial_io_memory
);
451 s
->chn
[0].chr
= chr1
;
452 s
->chn
[1].chr
= chr2
;
454 for (i
= 0; i
< 2; i
++) {
456 s
->chn
[i
].chn
= 1 - i
;
457 s
->chn
[i
].type
= ser
;
459 qemu_chr_add_read_handler(s
->chn
[i
].chr
, serial_can_receive
, serial_receive1
, &s
->chn
[i
]);
460 qemu_chr_add_event_handler(s
->chn
[i
].chr
, serial_event
);
463 s
->chn
[0].otherchn
= &s
->chn
[1];
464 s
->chn
[1].otherchn
= &s
->chn
[0];
465 register_savevm("slavio_serial", base
, 1, slavio_serial_save
, slavio_serial_load
, s
);
466 qemu_register_reset(slavio_serial_reset
, s
);
467 slavio_serial_reset(s
);
471 static const uint8_t keycodes
[128] = {
472 127, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 43, 53,
473 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 89, 76, 77, 78,
474 79, 80, 81, 82, 83, 84, 85, 86, 87, 42, 99, 88, 100, 101, 102, 103,
475 104, 105, 106, 107, 108, 109, 110, 47, 19, 121, 119, 5, 6, 8, 10, 12,
476 14, 16, 17, 18, 7, 98, 23, 68, 69, 70, 71, 91, 92, 93, 125, 112,
477 113, 114, 94, 50, 0, 0, 124, 9, 11, 0, 0, 0, 0, 0, 0, 0,
478 90, 0, 46, 22, 13, 111, 52, 20, 96, 24, 28, 74, 27, 123, 44, 66,
479 0, 45, 2, 4, 48, 0, 0, 21, 0, 0, 0, 0, 0, 120, 122, 67,
482 static void sunkbd_event(void *opaque
, int ch
)
484 ChannelState
*s
= opaque
;
485 int release
= ch
& 0x80;
487 ch
= keycodes
[ch
& 0x7f];
488 KBD_DPRINTF("Keycode %d (%s)\n", ch
, release
? "release" : "press");
489 put_queue(s
, ch
| release
);
492 static void handle_kbd_command(ChannelState
*s
, int val
)
494 KBD_DPRINTF("Command %d\n", val
);
496 case 1: // Reset, return type code
498 put_queue(s
, 5); // Type 5
500 case 7: // Query layout
502 put_queue(s
, 0x20); // XXX, layout?
509 static void sunmouse_event(void *opaque
,
510 int dx
, int dy
, int dz
, int buttons_state
)
512 ChannelState
*s
= opaque
;
517 serial_receive_byte(s
, ch
);
520 void slavio_serial_ms_kbd_init(int base
, int irq
)
522 int slavio_serial_io_memory
, i
;
525 s
= qemu_mallocz(sizeof(SerialState
));
528 for (i
= 0; i
< 2; i
++) {
530 s
->chn
[i
].chn
= 1 - i
;
531 s
->chn
[i
].chr
= NULL
;
533 s
->chn
[0].otherchn
= &s
->chn
[1];
534 s
->chn
[1].otherchn
= &s
->chn
[0];
535 s
->chn
[0].type
= mouse
;
536 s
->chn
[1].type
= kbd
;
538 slavio_serial_io_memory
= cpu_register_io_memory(0, slavio_serial_mem_read
, slavio_serial_mem_write
, s
);
539 cpu_register_physical_memory(base
, SERIAL_MAXADDR
, slavio_serial_io_memory
);
541 qemu_add_mouse_event_handler(sunmouse_event
, &s
->chn
[0], 0);
542 qemu_add_kbd_event_handler(sunkbd_event
, &s
->chn
[1]);
543 qemu_register_reset(slavio_serial_reset
, s
);
544 slavio_serial_reset(s
);