2 * QEMU ESCC (Z8030/Z8530/Z85C30/SCC/ESCC) 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
25 #include "qemu/osdep.h"
27 #include "hw/qdev-properties.h"
28 #include "hw/qdev-properties-system.h"
29 #include "hw/sysbus.h"
30 #include "migration/vmstate.h"
31 #include "qemu/module.h"
32 #include "hw/char/escc.h"
33 #include "ui/console.h"
38 * "Z80C30/Z85C30/Z80230/Z85230/Z85233 SCC/ESCC User Manual",
39 * http://www.zilog.com/docs/serial/scc_escc_um.pdf
41 * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001
42 * (Slave I/O), also produced as NCR89C105. See
43 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
45 * The serial ports implement full AMD AM8530 or Zilog Z8530 chips,
46 * mouse and keyboard ports don't implement all functions and they are
47 * only asynchronous. There is no DMA.
49 * Z85C30 is also used on PowerMacs and m68k Macs.
51 * There are some small differences between Sparc version (sunzilog)
52 * and PowerMac (pmac):
53 * Offset between control and data registers
54 * There is some kind of lockup bug, but we can ignore it
56 * DMA on pmac using DBDMA chip
57 * pmac can do IRDA and faster rates, sunzilog can only do 38400
58 * pmac baud rate generator clock is 3.6864 MHz, sunzilog 4.9152 MHz
60 * Linux driver for m68k Macs is the same as for PowerMac (pmac_zilog),
61 * but registers are grouped by type and not by channel:
62 * channel is selected by bit 0 of the address (instead of bit 1)
63 * and register is selected by bit 1 of the address (instead of bit 0).
68 * 2006-Aug-10 Igor Kovalenko : Renamed KBDQueue to SERIOQueue, implemented
70 * Implemented serial mouse protocol.
72 * 2010-May-23 Artyom Tarasenko: Reworked IUS logic
75 #define CHN_C(s) ((s)->chn == escc_chn_b ? 'b' : 'a')
81 #define CMD_PTR_MASK 0x07
82 #define CMD_CMD_MASK 0x38
84 #define CMD_CLR_TXINT 0x28
85 #define CMD_CLR_IUS 0x38
87 #define INTR_INTALL 0x01
88 #define INTR_TXINT 0x02
89 #define INTR_PAR_SPEC 0x04
90 #define INTR_RXMODEMSK 0x18
91 #define INTR_RXINT1ST 0x08
92 #define INTR_RXINTALL 0x10
93 #define INTR_WTRQ_TXRX 0x20
96 #define RXCTRL_RXEN 0x01
97 #define RXCTRL_HUNT 0x10
99 #define TXCTRL1_PAREN 0x01
100 #define TXCTRL1_PAREV 0x02
101 #define TXCTRL1_1STOP 0x04
102 #define TXCTRL1_1HSTOP 0x08
103 #define TXCTRL1_2STOP 0x0c
104 #define TXCTRL1_STPMSK 0x0c
105 #define TXCTRL1_CLK1X 0x00
106 #define TXCTRL1_CLK16X 0x40
107 #define TXCTRL1_CLK32X 0x80
108 #define TXCTRL1_CLK64X 0xc0
109 #define TXCTRL1_CLKMSK 0xc0
111 #define TXCTRL2_TXCRC 0x01
112 #define TXCTRL2_TXEN 0x08
113 #define TXCTRL2_BITMSK 0x60
114 #define TXCTRL2_5BITS 0x00
115 #define TXCTRL2_7BITS 0x20
116 #define TXCTRL2_6BITS 0x40
117 #define TXCTRL2_8BITS 0x60
122 #define MINTR_VIS 0x01
123 #define MINTR_NV 0x02
124 #define MINTR_STATUSHI 0x10
125 #define MINTR_SOFTIACK 0x20
126 #define MINTR_RST_MASK 0xc0
127 #define MINTR_RST_B 0x40
128 #define MINTR_RST_A 0x80
129 #define MINTR_RST_ALL 0xc0
131 #define MISC1_ENC_MASK 0x60
133 #define CLOCK_TRXC 0x08
137 #define MISC2_BRG_EN 0x01
138 #define MISC2_BRG_SRC 0x02
139 #define MISC2_LCL_LOOP 0x10
140 #define MISC2_PLLCMD0 0x20
141 #define MISC2_PLLCMD1 0x40
142 #define MISC2_PLLCMD2 0x80
144 #define EXTINT_DCD 0x08
145 #define EXTINT_SYNCINT 0x10
146 #define EXTINT_CTSINT 0x20
147 #define EXTINT_TXUNDRN 0x40
148 #define EXTINT_BRKINT 0x80
151 #define STATUS_RXAV 0x01
152 #define STATUS_ZERO 0x02
153 #define STATUS_TXEMPTY 0x04
154 #define STATUS_DCD 0x08
155 #define STATUS_SYNC 0x10
156 #define STATUS_CTS 0x20
157 #define STATUS_TXUNDRN 0x40
158 #define STATUS_BRK 0x80
160 #define SPEC_ALLSENT 0x01
161 #define SPEC_BITS8 0x06
163 #define IVEC_TXINTB 0x00
164 #define IVEC_LONOINT 0x06
165 #define IVEC_LORXINTA 0x0c
166 #define IVEC_LORXINTB 0x04
167 #define IVEC_LOTXINTA 0x08
168 #define IVEC_HINOINT 0x60
169 #define IVEC_HIRXINTA 0x30
170 #define IVEC_HIRXINTB 0x20
171 #define IVEC_HITXINTA 0x10
173 #define INTR_EXTINTB 0x01
174 #define INTR_TXINTB 0x02
175 #define INTR_RXINTB 0x04
176 #define INTR_EXTINTA 0x08
177 #define INTR_TXINTA 0x10
178 #define INTR_RXINTA 0x20
186 #define MISC_2CLKMISS 0x40
193 static void handle_kbd_command(ESCCChannelState
*s
, int val
);
194 static int serial_can_receive(void *opaque
);
195 static void serial_receive_byte(ESCCChannelState
*s
, int ch
);
197 static int reg_shift(ESCCState
*s
)
199 return s
->bit_swap
? s
->it_shift
+ 1 : s
->it_shift
;
202 static int chn_shift(ESCCState
*s
)
204 return s
->bit_swap
? s
->it_shift
: s
->it_shift
+ 1;
207 static void clear_queue(void *opaque
)
209 ESCCChannelState
*s
= opaque
;
210 ESCCSERIOQueue
*q
= &s
->queue
;
211 q
->rptr
= q
->wptr
= q
->count
= 0;
214 static void put_queue(void *opaque
, int b
)
216 ESCCChannelState
*s
= opaque
;
217 ESCCSERIOQueue
*q
= &s
->queue
;
219 trace_escc_put_queue(CHN_C(s
), b
);
220 if (q
->count
>= ESCC_SERIO_QUEUE_SIZE
) {
223 q
->data
[q
->wptr
] = b
;
224 if (++q
->wptr
== ESCC_SERIO_QUEUE_SIZE
) {
228 serial_receive_byte(s
, 0);
231 static uint32_t get_queue(void *opaque
)
233 ESCCChannelState
*s
= opaque
;
234 ESCCSERIOQueue
*q
= &s
->queue
;
240 val
= q
->data
[q
->rptr
];
241 if (++q
->rptr
== ESCC_SERIO_QUEUE_SIZE
) {
246 trace_escc_get_queue(CHN_C(s
), val
);
248 serial_receive_byte(s
, 0);
253 static int escc_update_irq_chn(ESCCChannelState
*s
)
255 if ((((s
->wregs
[W_INTR
] & INTR_TXINT
) && (s
->txint
== 1)) ||
256 /* tx ints enabled, pending */
257 ((((s
->wregs
[W_INTR
] & INTR_RXMODEMSK
) == INTR_RXINT1ST
) ||
258 ((s
->wregs
[W_INTR
] & INTR_RXMODEMSK
) == INTR_RXINTALL
)) &&
260 /* rx ints enabled, pending */
261 ((s
->wregs
[W_EXTINT
] & EXTINT_BRKINT
) &&
262 (s
->rregs
[R_STATUS
] & STATUS_BRK
)))) {
269 static void escc_update_irq(ESCCChannelState
*s
)
273 irq
= escc_update_irq_chn(s
);
274 irq
|= escc_update_irq_chn(s
->otherchn
);
276 trace_escc_update_irq(irq
);
277 qemu_set_irq(s
->irq
, irq
);
280 static void escc_reset_chn(ESCCChannelState
*s
)
284 s
->rxint
= s
->txint
= 0;
285 s
->rxint_under_svc
= s
->txint_under_svc
= 0;
286 s
->e0_mode
= s
->led_mode
= s
->caps_lock_mode
= s
->num_lock_mode
= 0;
290 static void escc_soft_reset_chn(ESCCChannelState
*s
)
295 s
->wregs
[W_INTR
] &= INTR_PAR_SPEC
| INTR_WTRQ_TXRX
;
296 s
->wregs
[W_RXCTRL
] &= ~RXCTRL_RXEN
;
298 s
->wregs
[W_TXCTRL1
] |= TXCTRL1_1STOP
;
299 s
->wregs
[W_TXCTRL2
] &= TXCTRL2_TXCRC
| TXCTRL2_8BITS
;
300 s
->wregs
[W_MINTR
] &= ~MINTR_SOFTIACK
;
301 s
->wregs
[W_MISC1
] &= MISC1_ENC_MASK
;
303 s
->wregs
[W_MISC2
] &= MISC2_BRG_EN
| MISC2_BRG_SRC
|
304 MISC2_PLLCMD1
| MISC2_PLLCMD2
;
305 s
->wregs
[W_MISC2
] |= MISC2_PLLCMD0
;
306 /* Enable most interrupts */
307 s
->wregs
[W_EXTINT
] = EXTINT_DCD
| EXTINT_SYNCINT
| EXTINT_CTSINT
|
308 EXTINT_TXUNDRN
| EXTINT_BRKINT
;
310 s
->rregs
[R_STATUS
] &= STATUS_DCD
| STATUS_SYNC
| STATUS_CTS
| STATUS_BRK
;
311 s
->rregs
[R_STATUS
] |= STATUS_TXEMPTY
| STATUS_TXUNDRN
;
313 s
->rregs
[R_STATUS
] |= STATUS_DCD
| STATUS_SYNC
| STATUS_CTS
;
315 s
->rregs
[R_SPEC
] &= SPEC_ALLSENT
;
316 s
->rregs
[R_SPEC
] |= SPEC_BITS8
;
317 s
->rregs
[R_INTR
] = 0;
318 s
->rregs
[R_MISC
] &= MISC_2CLKMISS
;
321 static void escc_hard_reset_chn(ESCCChannelState
*s
)
323 escc_soft_reset_chn(s
);
326 * Hard reset is almost identical to soft reset above, except that the
327 * values of WR9 (W_MINTR), WR10 (W_MISC1), WR11 (W_CLOCK) and WR14
328 * (W_MISC2) have extra bits forced to 0/1
330 s
->wregs
[W_MINTR
] &= MINTR_VIS
| MINTR_NV
;
331 s
->wregs
[W_MINTR
] |= MINTR_RST_B
| MINTR_RST_A
;
332 s
->wregs
[W_MISC1
] = 0;
333 s
->wregs
[W_CLOCK
] = CLOCK_TRXC
;
334 s
->wregs
[W_MISC2
] &= MISC2_PLLCMD1
| MISC2_PLLCMD2
;
335 s
->wregs
[W_MISC2
] |= MISC2_LCL_LOOP
| MISC2_PLLCMD0
;
338 static void escc_reset(DeviceState
*d
)
340 ESCCState
*s
= ESCC(d
);
343 for (i
= 0; i
< 2; i
++) {
344 ESCCChannelState
*cs
= &s
->chn
[i
];
347 * According to the ESCC datasheet "Miscellaneous Questions" section
348 * on page 384, the values of the ESCC registers are not guaranteed on
349 * power-on until an explicit hardware or software reset has been
350 * issued. For now we zero the registers so that a device reset always
351 * returns the emulated device to a fixed state.
353 for (j
= 0; j
< ESCC_SERIAL_REGS
; j
++) {
359 * ...but there is an exception. The "Transmit Interrupts and Transmit
360 * Buffer Empty Bit" section on page 50 of the ESCC datasheet says of
361 * the STATUS_TXEMPTY bit in R_STATUS: "After a hardware reset
362 * (including a hardware reset by software), or a channel reset, this
363 * bit is set to 1". The Sun PROM checks this bit early on startup and
364 * gets stuck in an infinite loop if it is not set.
366 cs
->rregs
[R_STATUS
] |= STATUS_TXEMPTY
;
372 static inline void set_rxint(ESCCChannelState
*s
)
376 * XXX: missing daisy chaining: escc_chn_b rx should have a lower priority
377 * than chn_a rx/tx/special_condition service
379 s
->rxint_under_svc
= 1;
380 if (s
->chn
== escc_chn_a
) {
381 s
->rregs
[R_INTR
] |= INTR_RXINTA
;
382 if (s
->wregs
[W_MINTR
] & MINTR_STATUSHI
) {
383 s
->otherchn
->rregs
[R_IVEC
] = IVEC_HIRXINTA
;
385 s
->otherchn
->rregs
[R_IVEC
] = IVEC_LORXINTA
;
388 s
->otherchn
->rregs
[R_INTR
] |= INTR_RXINTB
;
389 if (s
->wregs
[W_MINTR
] & MINTR_STATUSHI
) {
390 s
->rregs
[R_IVEC
] = IVEC_HIRXINTB
;
392 s
->rregs
[R_IVEC
] = IVEC_LORXINTB
;
398 static inline void set_txint(ESCCChannelState
*s
)
401 if (!s
->rxint_under_svc
) {
402 s
->txint_under_svc
= 1;
403 if (s
->chn
== escc_chn_a
) {
404 if (s
->wregs
[W_INTR
] & INTR_TXINT
) {
405 s
->rregs
[R_INTR
] |= INTR_TXINTA
;
407 if (s
->wregs
[W_MINTR
] & MINTR_STATUSHI
) {
408 s
->otherchn
->rregs
[R_IVEC
] = IVEC_HITXINTA
;
410 s
->otherchn
->rregs
[R_IVEC
] = IVEC_LOTXINTA
;
413 s
->rregs
[R_IVEC
] = IVEC_TXINTB
;
414 if (s
->wregs
[W_INTR
] & INTR_TXINT
) {
415 s
->otherchn
->rregs
[R_INTR
] |= INTR_TXINTB
;
422 static inline void clr_rxint(ESCCChannelState
*s
)
425 s
->rxint_under_svc
= 0;
426 if (s
->chn
== escc_chn_a
) {
427 if (s
->wregs
[W_MINTR
] & MINTR_STATUSHI
) {
428 s
->otherchn
->rregs
[R_IVEC
] = IVEC_HINOINT
;
430 s
->otherchn
->rregs
[R_IVEC
] = IVEC_LONOINT
;
432 s
->rregs
[R_INTR
] &= ~INTR_RXINTA
;
434 if (s
->wregs
[W_MINTR
] & MINTR_STATUSHI
) {
435 s
->rregs
[R_IVEC
] = IVEC_HINOINT
;
437 s
->rregs
[R_IVEC
] = IVEC_LONOINT
;
439 s
->otherchn
->rregs
[R_INTR
] &= ~INTR_RXINTB
;
447 static inline void clr_txint(ESCCChannelState
*s
)
450 s
->txint_under_svc
= 0;
451 if (s
->chn
== escc_chn_a
) {
452 if (s
->wregs
[W_MINTR
] & MINTR_STATUSHI
) {
453 s
->otherchn
->rregs
[R_IVEC
] = IVEC_HINOINT
;
455 s
->otherchn
->rregs
[R_IVEC
] = IVEC_LONOINT
;
457 s
->rregs
[R_INTR
] &= ~INTR_TXINTA
;
459 s
->otherchn
->rregs
[R_INTR
] &= ~INTR_TXINTB
;
460 if (s
->wregs
[W_MINTR
] & MINTR_STATUSHI
) {
461 s
->rregs
[R_IVEC
] = IVEC_HINOINT
;
463 s
->rregs
[R_IVEC
] = IVEC_LONOINT
;
465 s
->otherchn
->rregs
[R_INTR
] &= ~INTR_TXINTB
;
473 static void escc_update_parameters(ESCCChannelState
*s
)
475 int speed
, parity
, data_bits
, stop_bits
;
476 QEMUSerialSetParams ssp
;
478 if (!qemu_chr_fe_backend_connected(&s
->chr
) || s
->type
!= escc_serial
) {
482 if (s
->wregs
[W_TXCTRL1
] & TXCTRL1_PAREN
) {
483 if (s
->wregs
[W_TXCTRL1
] & TXCTRL1_PAREV
) {
491 if ((s
->wregs
[W_TXCTRL1
] & TXCTRL1_STPMSK
) == TXCTRL1_2STOP
) {
496 switch (s
->wregs
[W_TXCTRL2
] & TXCTRL2_BITMSK
) {
511 speed
= s
->clock
/ ((s
->wregs
[W_BRGLO
] | (s
->wregs
[W_BRGHI
] << 8)) + 2);
512 switch (s
->wregs
[W_TXCTRL1
] & TXCTRL1_CLKMSK
) {
528 ssp
.data_bits
= data_bits
;
529 ssp
.stop_bits
= stop_bits
;
530 trace_escc_update_parameters(CHN_C(s
), speed
, parity
, data_bits
, stop_bits
);
531 qemu_chr_fe_ioctl(&s
->chr
, CHR_IOCTL_SERIAL_SET_PARAMS
, &ssp
);
534 static void escc_mem_write(void *opaque
, hwaddr addr
,
535 uint64_t val
, unsigned size
)
537 ESCCState
*serial
= opaque
;
543 saddr
= (addr
>> reg_shift(serial
)) & 1;
544 channel
= (addr
>> chn_shift(serial
)) & 1;
545 s
= &serial
->chn
[channel
];
548 trace_escc_mem_writeb_ctrl(CHN_C(s
), s
->reg
, val
& 0xff);
552 newreg
= val
& CMD_PTR_MASK
;
562 if (s
->rxint_under_svc
) {
563 s
->rxint_under_svc
= 0;
567 } else if (s
->txint_under_svc
) {
568 s
->txint_under_svc
= 0;
577 s
->wregs
[s
->reg
] = val
;
578 if (val
& RXCTRL_HUNT
) {
579 s
->rregs
[R_STATUS
] |= STATUS_SYNC
;
582 case W_INTR
... W_IVEC
:
583 case W_SYNC1
... W_TXBUF
:
584 case W_MISC1
... W_CLOCK
:
585 case W_MISC2
... W_EXTINT
:
586 s
->wregs
[s
->reg
] = val
;
589 s
->wregs
[s
->reg
] = val
;
591 * The ESCC datasheet states that SPEC_ALLSENT is always set in
592 * sync mode, and set in async mode when all characters have
593 * cleared the transmitter. Since writes to SERIAL_DATA use the
594 * blocking qemu_chr_fe_write_all() function to write each
595 * character, the guest can never see the state when async data
596 * is in the process of being transmitted so we can set this bit
597 * unconditionally regardless of the state of the W_TXCTRL1 mode
600 s
->rregs
[R_SPEC
] |= SPEC_ALLSENT
;
601 escc_update_parameters(s
);
604 s
->wregs
[s
->reg
] = val
;
605 escc_update_parameters(s
);
609 s
->wregs
[s
->reg
] = val
;
610 s
->rregs
[s
->reg
] = val
;
611 escc_update_parameters(s
);
614 switch (val
& MINTR_RST_MASK
) {
619 trace_escc_soft_reset_chn(CHN_C(&serial
->chn
[0]));
620 escc_soft_reset_chn(&serial
->chn
[0]);
623 trace_escc_soft_reset_chn(CHN_C(&serial
->chn
[1]));
624 escc_soft_reset_chn(&serial
->chn
[1]);
627 trace_escc_hard_reset();
628 escc_hard_reset_chn(&serial
->chn
[0]);
629 escc_hard_reset_chn(&serial
->chn
[1]);
643 trace_escc_mem_writeb_data(CHN_C(s
), val
);
645 * Lower the irq when data is written to the Tx buffer and no other
646 * interrupts are currently pending. The irq will be raised again once
647 * the Tx buffer becomes empty below.
652 if (s
->wregs
[W_TXCTRL2
] & TXCTRL2_TXEN
) { /* tx enabled */
653 if (qemu_chr_fe_backend_connected(&s
->chr
)) {
655 * XXX this blocks entire thread. Rewrite to use
656 * qemu_chr_fe_write and background I/O callbacks
658 qemu_chr_fe_write_all(&s
->chr
, &s
->tx
, 1);
659 } else if (s
->type
== escc_kbd
&& !s
->disabled
) {
660 handle_kbd_command(s
, val
);
663 s
->rregs
[R_STATUS
] |= STATUS_TXEMPTY
; /* Tx buffer empty */
664 s
->rregs
[R_SPEC
] |= SPEC_ALLSENT
; /* All sent */
672 static uint64_t escc_mem_read(void *opaque
, hwaddr addr
,
675 ESCCState
*serial
= opaque
;
681 saddr
= (addr
>> reg_shift(serial
)) & 1;
682 channel
= (addr
>> chn_shift(serial
)) & 1;
683 s
= &serial
->chn
[channel
];
686 trace_escc_mem_readb_ctrl(CHN_C(s
), s
->reg
, s
->rregs
[s
->reg
]);
687 ret
= s
->rregs
[s
->reg
];
691 s
->rregs
[R_STATUS
] &= ~STATUS_RXAV
;
693 if (s
->type
== escc_kbd
|| s
->type
== escc_mouse
) {
698 trace_escc_mem_readb_data(CHN_C(s
), ret
);
699 qemu_chr_fe_accept_input(&s
->chr
);
707 static const MemoryRegionOps escc_mem_ops
= {
708 .read
= escc_mem_read
,
709 .write
= escc_mem_write
,
710 .endianness
= DEVICE_NATIVE_ENDIAN
,
712 .min_access_size
= 1,
713 .max_access_size
= 1,
717 static int serial_can_receive(void *opaque
)
719 ESCCChannelState
*s
= opaque
;
722 if (((s
->wregs
[W_RXCTRL
] & RXCTRL_RXEN
) == 0) /* Rx not enabled */
723 || ((s
->rregs
[R_STATUS
] & STATUS_RXAV
) == STATUS_RXAV
)) {
724 /* char already available */
732 static void serial_receive_byte(ESCCChannelState
*s
, int ch
)
734 trace_escc_serial_receive_byte(CHN_C(s
), ch
);
735 s
->rregs
[R_STATUS
] |= STATUS_RXAV
;
740 static void serial_receive_break(ESCCChannelState
*s
)
742 s
->rregs
[R_STATUS
] |= STATUS_BRK
;
746 static void serial_receive1(void *opaque
, const uint8_t *buf
, int size
)
748 ESCCChannelState
*s
= opaque
;
749 serial_receive_byte(s
, buf
[0]);
752 static void serial_event(void *opaque
, QEMUChrEvent event
)
754 ESCCChannelState
*s
= opaque
;
755 if (event
== CHR_EVENT_BREAK
) {
756 serial_receive_break(s
);
760 static const VMStateDescription vmstate_escc_chn
= {
763 .minimum_version_id
= 1,
764 .fields
= (VMStateField
[]) {
765 VMSTATE_UINT32(vmstate_dummy
, ESCCChannelState
),
766 VMSTATE_UINT32(reg
, ESCCChannelState
),
767 VMSTATE_UINT32(rxint
, ESCCChannelState
),
768 VMSTATE_UINT32(txint
, ESCCChannelState
),
769 VMSTATE_UINT32(rxint_under_svc
, ESCCChannelState
),
770 VMSTATE_UINT32(txint_under_svc
, ESCCChannelState
),
771 VMSTATE_UINT8(rx
, ESCCChannelState
),
772 VMSTATE_UINT8(tx
, ESCCChannelState
),
773 VMSTATE_BUFFER(wregs
, ESCCChannelState
),
774 VMSTATE_BUFFER(rregs
, ESCCChannelState
),
775 VMSTATE_END_OF_LIST()
779 static const VMStateDescription vmstate_escc
= {
782 .minimum_version_id
= 1,
783 .fields
= (VMStateField
[]) {
784 VMSTATE_STRUCT_ARRAY(chn
, ESCCState
, 2, 2, vmstate_escc_chn
,
786 VMSTATE_END_OF_LIST()
790 static void sunkbd_handle_event(DeviceState
*dev
, QemuConsole
*src
,
793 ESCCChannelState
*s
= (ESCCChannelState
*)dev
;
797 assert(evt
->type
== INPUT_EVENT_KIND_KEY
);
798 key
= evt
->u
.key
.data
;
799 qcode
= qemu_input_key_value_to_qcode(key
->key
);
800 trace_escc_sunkbd_event_in(qcode
, QKeyCode_str(qcode
),
803 if (qcode
== Q_KEY_CODE_CAPS_LOCK
) {
805 s
->caps_lock_mode
^= 1;
806 if (s
->caps_lock_mode
== 2) {
807 return; /* Drop second press */
810 s
->caps_lock_mode
^= 2;
811 if (s
->caps_lock_mode
== 3) {
812 return; /* Drop first release */
817 if (qcode
== Q_KEY_CODE_NUM_LOCK
) {
819 s
->num_lock_mode
^= 1;
820 if (s
->num_lock_mode
== 2) {
821 return; /* Drop second press */
824 s
->num_lock_mode
^= 2;
825 if (s
->num_lock_mode
== 3) {
826 return; /* Drop first release */
831 if (qcode
>= qemu_input_map_qcode_to_sun_len
) {
835 keycode
= qemu_input_map_qcode_to_sun
[qcode
];
839 trace_escc_sunkbd_event_out(keycode
);
840 put_queue(s
, keycode
);
843 static QemuInputHandler sunkbd_handler
= {
844 .name
= "sun keyboard",
845 .mask
= INPUT_EVENT_MASK_KEY
,
846 .event
= sunkbd_handle_event
,
849 static void handle_kbd_command(ESCCChannelState
*s
, int val
)
851 trace_escc_kbd_command(val
);
852 if (s
->led_mode
) { /* Ignore led byte */
857 case 1: /* Reset, return type code */
860 put_queue(s
, 4); /* Type 4 */
863 case 0xe: /* Set leds */
866 case 7: /* Query layout */
870 put_queue(s
, 0x21); /* en-us layout */
877 static void sunmouse_event(void *opaque
,
878 int dx
, int dy
, int dz
, int buttons_state
)
880 ESCCChannelState
*s
= opaque
;
883 trace_escc_sunmouse_event(dx
, dy
, buttons_state
);
884 ch
= 0x80 | 0x7; /* protocol start byte, no buttons pressed */
886 if (buttons_state
& MOUSE_EVENT_LBUTTON
) {
889 if (buttons_state
& MOUSE_EVENT_MBUTTON
) {
892 if (buttons_state
& MOUSE_EVENT_RBUTTON
) {
902 } else if (ch
< -127) {
906 put_queue(s
, ch
& 0xff);
912 } else if (ch
< -127) {
916 put_queue(s
, ch
& 0xff);
918 /* MSC protocol specifies two extra motion bytes */
924 static void escc_init1(Object
*obj
)
926 ESCCState
*s
= ESCC(obj
);
927 SysBusDevice
*dev
= SYS_BUS_DEVICE(obj
);
930 for (i
= 0; i
< 2; i
++) {
931 sysbus_init_irq(dev
, &s
->chn
[i
].irq
);
932 s
->chn
[i
].chn
= 1 - i
;
934 s
->chn
[0].otherchn
= &s
->chn
[1];
935 s
->chn
[1].otherchn
= &s
->chn
[0];
937 sysbus_init_mmio(dev
, &s
->mmio
);
940 static void escc_realize(DeviceState
*dev
, Error
**errp
)
942 ESCCState
*s
= ESCC(dev
);
945 s
->chn
[0].disabled
= s
->disabled
;
946 s
->chn
[1].disabled
= s
->disabled
;
948 memory_region_init_io(&s
->mmio
, OBJECT(dev
), &escc_mem_ops
, s
, "escc",
949 ESCC_SIZE
<< s
->it_shift
);
951 for (i
= 0; i
< 2; i
++) {
952 if (qemu_chr_fe_backend_connected(&s
->chn
[i
].chr
)) {
953 s
->chn
[i
].clock
= s
->frequency
/ 2;
954 qemu_chr_fe_set_handlers(&s
->chn
[i
].chr
, serial_can_receive
,
955 serial_receive1
, serial_event
, NULL
,
956 &s
->chn
[i
], NULL
, true);
960 if (s
->chn
[0].type
== escc_mouse
) {
961 qemu_add_mouse_event_handler(sunmouse_event
, &s
->chn
[0], 0,
964 if (s
->chn
[1].type
== escc_kbd
) {
965 s
->chn
[1].hs
= qemu_input_handler_register((DeviceState
*)(&s
->chn
[1]),
970 static Property escc_properties
[] = {
971 DEFINE_PROP_UINT32("frequency", ESCCState
, frequency
, 0),
972 DEFINE_PROP_UINT32("it_shift", ESCCState
, it_shift
, 0),
973 DEFINE_PROP_BOOL("bit_swap", ESCCState
, bit_swap
, false),
974 DEFINE_PROP_UINT32("disabled", ESCCState
, disabled
, 0),
975 DEFINE_PROP_UINT32("chnBtype", ESCCState
, chn
[0].type
, 0),
976 DEFINE_PROP_UINT32("chnAtype", ESCCState
, chn
[1].type
, 0),
977 DEFINE_PROP_CHR("chrB", ESCCState
, chn
[0].chr
),
978 DEFINE_PROP_CHR("chrA", ESCCState
, chn
[1].chr
),
979 DEFINE_PROP_END_OF_LIST(),
982 static void escc_class_init(ObjectClass
*klass
, void *data
)
984 DeviceClass
*dc
= DEVICE_CLASS(klass
);
986 dc
->reset
= escc_reset
;
987 dc
->realize
= escc_realize
;
988 dc
->vmsd
= &vmstate_escc
;
989 device_class_set_props(dc
, escc_properties
);
990 set_bit(DEVICE_CATEGORY_INPUT
, dc
->categories
);
993 static const TypeInfo escc_info
= {
995 .parent
= TYPE_SYS_BUS_DEVICE
,
996 .instance_size
= sizeof(ESCCState
),
997 .instance_init
= escc_init1
,
998 .class_init
= escc_class_init
,
1001 static void escc_register_types(void)
1003 type_register_static(&escc_info
);
1006 type_init(escc_register_types
)