1e80cfcfcSbellard /*
2b4ed08e0Sblueswir1 * QEMU ESCC (Z8030/Z8530/Z85C30/SCC/ESCC) serial port emulation
3e80cfcfcSbellard *
48be1f5c8Sbellard * Copyright (c) 2003-2005 Fabrice Bellard
5e80cfcfcSbellard *
6e80cfcfcSbellard * Permission is hereby granted, free of charge, to any person obtaining a copy
7e80cfcfcSbellard * of this software and associated documentation files (the "Software"), to deal
8e80cfcfcSbellard * in the Software without restriction, including without limitation the rights
9e80cfcfcSbellard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10e80cfcfcSbellard * copies of the Software, and to permit persons to whom the Software is
11e80cfcfcSbellard * furnished to do so, subject to the following conditions:
12e80cfcfcSbellard *
13e80cfcfcSbellard * The above copyright notice and this permission notice shall be included in
14e80cfcfcSbellard * all copies or substantial portions of the Software.
15e80cfcfcSbellard *
16e80cfcfcSbellard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17e80cfcfcSbellard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18e80cfcfcSbellard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19e80cfcfcSbellard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20e80cfcfcSbellard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21e80cfcfcSbellard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22e80cfcfcSbellard * THE SOFTWARE.
23e80cfcfcSbellard */
246c319c82SBlue Swirl
250430891cSPeter Maydell #include "qemu/osdep.h"
2664552b6bSMarkus Armbruster #include "hw/irq.h"
27a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
28ce35e229SEduardo Habkost #include "hw/qdev-properties-system.h"
2983c9f4caSPaolo Bonzini #include "hw/sysbus.h"
30d6454270SMarkus Armbruster #include "migration/vmstate.h"
310b8fa32fSMarkus Armbruster #include "qemu/module.h"
320d09e41aSPaolo Bonzini #include "hw/char/escc.h"
3328ecbaeeSPaolo Bonzini #include "ui/console.h"
346b90a4cdSHenrik Carlqvist
356b90a4cdSHenrik Carlqvist #include "qemu/cutils.h"
3630c2f238SBlue Swirl #include "trace.h"
37e80cfcfcSbellard
38e80cfcfcSbellard /*
3909330e90SBlue Swirl * Chipset docs:
4009330e90SBlue Swirl * "Z80C30/Z85C30/Z80230/Z85230/Z85233 SCC/ESCC User Manual",
4109330e90SBlue Swirl * http://www.zilog.com/docs/serial/scc_escc_um.pdf
4209330e90SBlue Swirl *
43b4ed08e0Sblueswir1 * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001
44e80cfcfcSbellard * (Slave I/O), also produced as NCR89C105. See
45e80cfcfcSbellard * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt
46e80cfcfcSbellard *
47e80cfcfcSbellard * The serial ports implement full AMD AM8530 or Zilog Z8530 chips,
48e80cfcfcSbellard * mouse and keyboard ports don't implement all functions and they are
49e80cfcfcSbellard * only asynchronous. There is no DMA.
50e80cfcfcSbellard *
51b43047a2SLaurent Vivier * Z85C30 is also used on PowerMacs and m68k Macs.
52b43047a2SLaurent Vivier *
53b43047a2SLaurent Vivier * There are some small differences between Sparc version (sunzilog)
54b43047a2SLaurent Vivier * and PowerMac (pmac):
55b4ed08e0Sblueswir1 * Offset between control and data registers
56b4ed08e0Sblueswir1 * There is some kind of lockup bug, but we can ignore it
57b4ed08e0Sblueswir1 * CTS is inverted
58b4ed08e0Sblueswir1 * DMA on pmac using DBDMA chip
59b4ed08e0Sblueswir1 * pmac can do IRDA and faster rates, sunzilog can only do 38400
60b4ed08e0Sblueswir1 * pmac baud rate generator clock is 3.6864 MHz, sunzilog 4.9152 MHz
61b43047a2SLaurent Vivier *
62b43047a2SLaurent Vivier * Linux driver for m68k Macs is the same as for PowerMac (pmac_zilog),
63b43047a2SLaurent Vivier * but registers are grouped by type and not by channel:
64b43047a2SLaurent Vivier * channel is selected by bit 0 of the address (instead of bit 1)
65b43047a2SLaurent Vivier * and register is selected by bit 1 of the address (instead of bit 0).
66e80cfcfcSbellard */
67e80cfcfcSbellard
68715748faSbellard /*
69715748faSbellard * Modifications:
70715748faSbellard * 2006-Aug-10 Igor Kovalenko : Renamed KBDQueue to SERIOQueue, implemented
71715748faSbellard * serial mouse queue.
72715748faSbellard * Implemented serial mouse protocol.
739fc391f8SArtyom Tarasenko *
749fc391f8SArtyom Tarasenko * 2010-May-23 Artyom Tarasenko: Reworked IUS logic
75715748faSbellard */
76715748faSbellard
772cc75c32SLaurent Vivier #define CHN_C(s) ((s)->chn == escc_chn_b ? 'b' : 'a')
78e80cfcfcSbellard
7912abac85Sblueswir1 #define SERIAL_CTRL 0
8012abac85Sblueswir1 #define SERIAL_DATA 1
8112abac85Sblueswir1
8212abac85Sblueswir1 #define W_CMD 0
8312abac85Sblueswir1 #define CMD_PTR_MASK 0x07
8412abac85Sblueswir1 #define CMD_CMD_MASK 0x38
8512abac85Sblueswir1 #define CMD_HI 0x08
8612abac85Sblueswir1 #define CMD_CLR_TXINT 0x28
8712abac85Sblueswir1 #define CMD_CLR_IUS 0x38
8812abac85Sblueswir1 #define W_INTR 1
8912abac85Sblueswir1 #define INTR_INTALL 0x01
9012abac85Sblueswir1 #define INTR_TXINT 0x02
911f476e78SMark Cave-Ayland #define INTR_PAR_SPEC 0x04
9212abac85Sblueswir1 #define INTR_RXMODEMSK 0x18
9312abac85Sblueswir1 #define INTR_RXINT1ST 0x08
9412abac85Sblueswir1 #define INTR_RXINTALL 0x10
951f476e78SMark Cave-Ayland #define INTR_WTRQ_TXRX 0x20
9612abac85Sblueswir1 #define W_IVEC 2
9712abac85Sblueswir1 #define W_RXCTRL 3
9812abac85Sblueswir1 #define RXCTRL_RXEN 0x01
9915a2a1a4SMark Cave-Ayland #define RXCTRL_HUNT 0x10
10012abac85Sblueswir1 #define W_TXCTRL1 4
10112abac85Sblueswir1 #define TXCTRL1_PAREN 0x01
10212abac85Sblueswir1 #define TXCTRL1_PAREV 0x02
10312abac85Sblueswir1 #define TXCTRL1_1STOP 0x04
10412abac85Sblueswir1 #define TXCTRL1_1HSTOP 0x08
10512abac85Sblueswir1 #define TXCTRL1_2STOP 0x0c
10612abac85Sblueswir1 #define TXCTRL1_STPMSK 0x0c
10712abac85Sblueswir1 #define TXCTRL1_CLK1X 0x00
10812abac85Sblueswir1 #define TXCTRL1_CLK16X 0x40
10912abac85Sblueswir1 #define TXCTRL1_CLK32X 0x80
11012abac85Sblueswir1 #define TXCTRL1_CLK64X 0xc0
11112abac85Sblueswir1 #define TXCTRL1_CLKMSK 0xc0
11212abac85Sblueswir1 #define W_TXCTRL2 5
1131f476e78SMark Cave-Ayland #define TXCTRL2_TXCRC 0x01
11412abac85Sblueswir1 #define TXCTRL2_TXEN 0x08
11512abac85Sblueswir1 #define TXCTRL2_BITMSK 0x60
11612abac85Sblueswir1 #define TXCTRL2_5BITS 0x00
11712abac85Sblueswir1 #define TXCTRL2_7BITS 0x20
11812abac85Sblueswir1 #define TXCTRL2_6BITS 0x40
11912abac85Sblueswir1 #define TXCTRL2_8BITS 0x60
12012abac85Sblueswir1 #define W_SYNC1 6
12112abac85Sblueswir1 #define W_SYNC2 7
12212abac85Sblueswir1 #define W_TXBUF 8
12312abac85Sblueswir1 #define W_MINTR 9
124160509aeSMark Cave-Ayland #define MINTR_VIS 0x01
125160509aeSMark Cave-Ayland #define MINTR_NV 0x02
12612abac85Sblueswir1 #define MINTR_STATUSHI 0x10
1271f476e78SMark Cave-Ayland #define MINTR_SOFTIACK 0x20
12812abac85Sblueswir1 #define MINTR_RST_MASK 0xc0
12912abac85Sblueswir1 #define MINTR_RST_B 0x40
13012abac85Sblueswir1 #define MINTR_RST_A 0x80
13112abac85Sblueswir1 #define MINTR_RST_ALL 0xc0
13212abac85Sblueswir1 #define W_MISC1 10
1331f476e78SMark Cave-Ayland #define MISC1_ENC_MASK 0x60
13412abac85Sblueswir1 #define W_CLOCK 11
13512abac85Sblueswir1 #define CLOCK_TRXC 0x08
13612abac85Sblueswir1 #define W_BRGLO 12
13712abac85Sblueswir1 #define W_BRGHI 13
13812abac85Sblueswir1 #define W_MISC2 14
1391f476e78SMark Cave-Ayland #define MISC2_BRG_EN 0x01
1401f476e78SMark Cave-Ayland #define MISC2_BRG_SRC 0x02
1411f476e78SMark Cave-Ayland #define MISC2_LCL_LOOP 0x10
1421f476e78SMark Cave-Ayland #define MISC2_PLLCMD0 0x20
1431f476e78SMark Cave-Ayland #define MISC2_PLLCMD1 0x40
1441f476e78SMark Cave-Ayland #define MISC2_PLLCMD2 0x80
14512abac85Sblueswir1 #define W_EXTINT 15
14612abac85Sblueswir1 #define EXTINT_DCD 0x08
14712abac85Sblueswir1 #define EXTINT_SYNCINT 0x10
14812abac85Sblueswir1 #define EXTINT_CTSINT 0x20
14912abac85Sblueswir1 #define EXTINT_TXUNDRN 0x40
15012abac85Sblueswir1 #define EXTINT_BRKINT 0x80
15112abac85Sblueswir1
15212abac85Sblueswir1 #define R_STATUS 0
15312abac85Sblueswir1 #define STATUS_RXAV 0x01
15412abac85Sblueswir1 #define STATUS_ZERO 0x02
15512abac85Sblueswir1 #define STATUS_TXEMPTY 0x04
15612abac85Sblueswir1 #define STATUS_DCD 0x08
15712abac85Sblueswir1 #define STATUS_SYNC 0x10
15812abac85Sblueswir1 #define STATUS_CTS 0x20
15912abac85Sblueswir1 #define STATUS_TXUNDRN 0x40
16012abac85Sblueswir1 #define STATUS_BRK 0x80
16112abac85Sblueswir1 #define R_SPEC 1
16212abac85Sblueswir1 #define SPEC_ALLSENT 0x01
16312abac85Sblueswir1 #define SPEC_BITS8 0x06
16412abac85Sblueswir1 #define R_IVEC 2
16512abac85Sblueswir1 #define IVEC_TXINTB 0x00
16612abac85Sblueswir1 #define IVEC_LONOINT 0x06
16712abac85Sblueswir1 #define IVEC_LORXINTA 0x0c
16812abac85Sblueswir1 #define IVEC_LORXINTB 0x04
16912abac85Sblueswir1 #define IVEC_LOTXINTA 0x08
17012abac85Sblueswir1 #define IVEC_HINOINT 0x60
17112abac85Sblueswir1 #define IVEC_HIRXINTA 0x30
17212abac85Sblueswir1 #define IVEC_HIRXINTB 0x20
17312abac85Sblueswir1 #define IVEC_HITXINTA 0x10
17412abac85Sblueswir1 #define R_INTR 3
17512abac85Sblueswir1 #define INTR_EXTINTB 0x01
17612abac85Sblueswir1 #define INTR_TXINTB 0x02
17712abac85Sblueswir1 #define INTR_RXINTB 0x04
17812abac85Sblueswir1 #define INTR_EXTINTA 0x08
17912abac85Sblueswir1 #define INTR_TXINTA 0x10
18012abac85Sblueswir1 #define INTR_RXINTA 0x20
18112abac85Sblueswir1 #define R_IPEN 4
18212abac85Sblueswir1 #define R_TXCTRL1 5
18312abac85Sblueswir1 #define R_TXCTRL2 6
18412abac85Sblueswir1 #define R_BC 7
18512abac85Sblueswir1 #define R_RXBUF 8
18612abac85Sblueswir1 #define R_RXCTRL 9
18712abac85Sblueswir1 #define R_MISC 10
1881f476e78SMark Cave-Ayland #define MISC_2CLKMISS 0x40
18912abac85Sblueswir1 #define R_MISC1 11
19012abac85Sblueswir1 #define R_BRGLO 12
19112abac85Sblueswir1 #define R_BRGHI 13
19212abac85Sblueswir1 #define R_MISC1I 14
19312abac85Sblueswir1 #define R_EXTINT 15
194e80cfcfcSbellard
1956b90a4cdSHenrik Carlqvist static uint8_t sunkbd_layout_dip_switch(const char *sunkbd_layout);
1962cc75c32SLaurent Vivier static void handle_kbd_command(ESCCChannelState *s, int val);
1978be1f5c8Sbellard static int serial_can_receive(void *opaque);
1982cc75c32SLaurent Vivier static void serial_receive_byte(ESCCChannelState *s, int ch);
1998be1f5c8Sbellard
reg_shift(ESCCState * s)200b43047a2SLaurent Vivier static int reg_shift(ESCCState *s)
201b43047a2SLaurent Vivier {
202b43047a2SLaurent Vivier return s->bit_swap ? s->it_shift + 1 : s->it_shift;
203b43047a2SLaurent Vivier }
204b43047a2SLaurent Vivier
chn_shift(ESCCState * s)205b43047a2SLaurent Vivier static int chn_shift(ESCCState *s)
206b43047a2SLaurent Vivier {
207b43047a2SLaurent Vivier return s->bit_swap ? s->it_shift : s->it_shift + 1;
208b43047a2SLaurent Vivier }
209b43047a2SLaurent Vivier
clear_queue(void * opaque)21067deb562Sblueswir1 static void clear_queue(void *opaque)
21167deb562Sblueswir1 {
2122cc75c32SLaurent Vivier ESCCChannelState *s = opaque;
2132cc75c32SLaurent Vivier ESCCSERIOQueue *q = &s->queue;
21467deb562Sblueswir1 q->rptr = q->wptr = q->count = 0;
21567deb562Sblueswir1 }
21667deb562Sblueswir1
put_queue(void * opaque,int b)2178be1f5c8Sbellard static void put_queue(void *opaque, int b)
2188be1f5c8Sbellard {
2192cc75c32SLaurent Vivier ESCCChannelState *s = opaque;
2202cc75c32SLaurent Vivier ESCCSERIOQueue *q = &s->queue;
2218be1f5c8Sbellard
22230c2f238SBlue Swirl trace_escc_put_queue(CHN_C(s), b);
2232cc75c32SLaurent Vivier if (q->count >= ESCC_SERIO_QUEUE_SIZE) {
2248be1f5c8Sbellard return;
2252cc75c32SLaurent Vivier }
2268be1f5c8Sbellard q->data[q->wptr] = b;
2272cc75c32SLaurent Vivier if (++q->wptr == ESCC_SERIO_QUEUE_SIZE) {
2288be1f5c8Sbellard q->wptr = 0;
2292cc75c32SLaurent Vivier }
2308be1f5c8Sbellard q->count++;
2318be1f5c8Sbellard serial_receive_byte(s, 0);
2328be1f5c8Sbellard }
2338be1f5c8Sbellard
get_queue(void * opaque)2348be1f5c8Sbellard static uint32_t get_queue(void *opaque)
2358be1f5c8Sbellard {
2362cc75c32SLaurent Vivier ESCCChannelState *s = opaque;
2372cc75c32SLaurent Vivier ESCCSERIOQueue *q = &s->queue;
2388be1f5c8Sbellard int val;
2398be1f5c8Sbellard
2408be1f5c8Sbellard if (q->count == 0) {
2418be1f5c8Sbellard return 0;
2428be1f5c8Sbellard } else {
2438be1f5c8Sbellard val = q->data[q->rptr];
2442cc75c32SLaurent Vivier if (++q->rptr == ESCC_SERIO_QUEUE_SIZE) {
2458be1f5c8Sbellard q->rptr = 0;
2462cc75c32SLaurent Vivier }
2478be1f5c8Sbellard q->count--;
2488be1f5c8Sbellard }
24930c2f238SBlue Swirl trace_escc_get_queue(CHN_C(s), val);
2500e042025SMark Cave-Ayland if (q->count > 0) {
2518be1f5c8Sbellard serial_receive_byte(s, 0);
2520e042025SMark Cave-Ayland }
2538be1f5c8Sbellard return val;
2548be1f5c8Sbellard }
2558be1f5c8Sbellard
escc_update_irq_chn(ESCCChannelState * s)2562cc75c32SLaurent Vivier static int escc_update_irq_chn(ESCCChannelState *s)
257e80cfcfcSbellard {
2589fc391f8SArtyom Tarasenko if ((((s->wregs[W_INTR] & INTR_TXINT) && (s->txint == 1)) ||
2590e042025SMark Cave-Ayland /* tx ints enabled, pending */
26012abac85Sblueswir1 ((((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINT1ST) ||
26112abac85Sblueswir1 ((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINTALL)) &&
2620e042025SMark Cave-Ayland s->rxint == 1) ||
2630e042025SMark Cave-Ayland /* rx ints enabled, pending */
26412abac85Sblueswir1 ((s->wregs[W_EXTINT] & EXTINT_BRKINT) &&
2650e042025SMark Cave-Ayland (s->rregs[R_STATUS] & STATUS_BRK)))) {
2660e042025SMark Cave-Ayland /* break int e&p */
267e4a89056Sbellard return 1;
268e80cfcfcSbellard }
269e4a89056Sbellard return 0;
270e4a89056Sbellard }
271e4a89056Sbellard
escc_update_irq(ESCCChannelState * s)2722cc75c32SLaurent Vivier static void escc_update_irq(ESCCChannelState *s)
273e4a89056Sbellard {
274e4a89056Sbellard int irq;
275e4a89056Sbellard
276b4ed08e0Sblueswir1 irq = escc_update_irq_chn(s);
277b4ed08e0Sblueswir1 irq |= escc_update_irq_chn(s->otherchn);
278e4a89056Sbellard
27930c2f238SBlue Swirl trace_escc_update_irq(irq);
280d537cf6cSpbrook qemu_set_irq(s->irq, irq);
281e80cfcfcSbellard }
282e80cfcfcSbellard
escc_reset_chn(ESCCChannelState * s)2832cc75c32SLaurent Vivier static void escc_reset_chn(ESCCChannelState *s)
284e80cfcfcSbellard {
285e80cfcfcSbellard s->reg = 0;
286e80cfcfcSbellard s->rx = s->tx = 0;
287e80cfcfcSbellard s->rxint = s->txint = 0;
288e4a89056Sbellard s->rxint_under_svc = s->txint_under_svc = 0;
289bbbb2f0aSblueswir1 s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0;
29034acb67fSMark Cave-Ayland s->sunmouse_dx = s->sunmouse_dy = s->sunmouse_buttons = 0;
29167deb562Sblueswir1 clear_queue(s);
292e80cfcfcSbellard }
293e80cfcfcSbellard
escc_soft_reset_chn(ESCCChannelState * s)2948e8aa965SMark Cave-Ayland static void escc_soft_reset_chn(ESCCChannelState *s)
2958e8aa965SMark Cave-Ayland {
29699b0f058SMark Cave-Ayland escc_reset_chn(s);
29799b0f058SMark Cave-Ayland
2981f476e78SMark Cave-Ayland s->wregs[W_CMD] = 0;
2991f476e78SMark Cave-Ayland s->wregs[W_INTR] &= INTR_PAR_SPEC | INTR_WTRQ_TXRX;
3001f476e78SMark Cave-Ayland s->wregs[W_RXCTRL] &= ~RXCTRL_RXEN;
3011f476e78SMark Cave-Ayland /* 1 stop bit */
3021f476e78SMark Cave-Ayland s->wregs[W_TXCTRL1] |= TXCTRL1_1STOP;
3031f476e78SMark Cave-Ayland s->wregs[W_TXCTRL2] &= TXCTRL2_TXCRC | TXCTRL2_8BITS;
3041f476e78SMark Cave-Ayland s->wregs[W_MINTR] &= ~MINTR_SOFTIACK;
3051f476e78SMark Cave-Ayland s->wregs[W_MISC1] &= MISC1_ENC_MASK;
3068e8aa965SMark Cave-Ayland /* PLL disabled */
3071f476e78SMark Cave-Ayland s->wregs[W_MISC2] &= MISC2_BRG_EN | MISC2_BRG_SRC |
3081f476e78SMark Cave-Ayland MISC2_PLLCMD1 | MISC2_PLLCMD2;
3091f476e78SMark Cave-Ayland s->wregs[W_MISC2] |= MISC2_PLLCMD0;
3108e8aa965SMark Cave-Ayland /* Enable most interrupts */
3118e8aa965SMark Cave-Ayland s->wregs[W_EXTINT] = EXTINT_DCD | EXTINT_SYNCINT | EXTINT_CTSINT |
3128e8aa965SMark Cave-Ayland EXTINT_TXUNDRN | EXTINT_BRKINT;
3131f476e78SMark Cave-Ayland
3141f476e78SMark Cave-Ayland s->rregs[R_STATUS] &= STATUS_DCD | STATUS_SYNC | STATUS_CTS | STATUS_BRK;
3151f476e78SMark Cave-Ayland s->rregs[R_STATUS] |= STATUS_TXEMPTY | STATUS_TXUNDRN;
3168e8aa965SMark Cave-Ayland if (s->disabled) {
3171f476e78SMark Cave-Ayland s->rregs[R_STATUS] |= STATUS_DCD | STATUS_SYNC | STATUS_CTS;
3188e8aa965SMark Cave-Ayland }
3191f476e78SMark Cave-Ayland s->rregs[R_SPEC] &= SPEC_ALLSENT;
3201f476e78SMark Cave-Ayland s->rregs[R_SPEC] |= SPEC_BITS8;
3211f476e78SMark Cave-Ayland s->rregs[R_INTR] = 0;
3221f476e78SMark Cave-Ayland s->rregs[R_MISC] &= MISC_2CLKMISS;
3238e8aa965SMark Cave-Ayland }
3248e8aa965SMark Cave-Ayland
escc_hard_reset_chn(ESCCChannelState * s)325bf4fbb69SMark Cave-Ayland static void escc_hard_reset_chn(ESCCChannelState *s)
326bf4fbb69SMark Cave-Ayland {
327160509aeSMark Cave-Ayland escc_soft_reset_chn(s);
328bf4fbb69SMark Cave-Ayland
329160509aeSMark Cave-Ayland /*
330160509aeSMark Cave-Ayland * Hard reset is almost identical to soft reset above, except that the
331160509aeSMark Cave-Ayland * values of WR9 (W_MINTR), WR10 (W_MISC1), WR11 (W_CLOCK) and WR14
332160509aeSMark Cave-Ayland * (W_MISC2) have extra bits forced to 0/1
333160509aeSMark Cave-Ayland */
334160509aeSMark Cave-Ayland s->wregs[W_MINTR] &= MINTR_VIS | MINTR_NV;
335160509aeSMark Cave-Ayland s->wregs[W_MINTR] |= MINTR_RST_B | MINTR_RST_A;
336160509aeSMark Cave-Ayland s->wregs[W_MISC1] = 0;
337bf4fbb69SMark Cave-Ayland s->wregs[W_CLOCK] = CLOCK_TRXC;
338160509aeSMark Cave-Ayland s->wregs[W_MISC2] &= MISC2_PLLCMD1 | MISC2_PLLCMD2;
339160509aeSMark Cave-Ayland s->wregs[W_MISC2] |= MISC2_LCL_LOOP | MISC2_PLLCMD0;
340bf4fbb69SMark Cave-Ayland }
341bf4fbb69SMark Cave-Ayland
escc_reset(DeviceState * d)342bdb78caeSBlue Swirl static void escc_reset(DeviceState *d)
343e80cfcfcSbellard {
34481069b20SAndreas Färber ESCCState *s = ESCC(d);
3459d248a4bSMark Cave-Ayland int i, j;
346bdb78caeSBlue Swirl
3479d248a4bSMark Cave-Ayland for (i = 0; i < 2; i++) {
3489d248a4bSMark Cave-Ayland ESCCChannelState *cs = &s->chn[i];
3499d248a4bSMark Cave-Ayland
3509d248a4bSMark Cave-Ayland /*
3519d248a4bSMark Cave-Ayland * According to the ESCC datasheet "Miscellaneous Questions" section
3529d248a4bSMark Cave-Ayland * on page 384, the values of the ESCC registers are not guaranteed on
3539d248a4bSMark Cave-Ayland * power-on until an explicit hardware or software reset has been
3549d248a4bSMark Cave-Ayland * issued. For now we zero the registers so that a device reset always
3559d248a4bSMark Cave-Ayland * returns the emulated device to a fixed state.
3569d248a4bSMark Cave-Ayland */
3579d248a4bSMark Cave-Ayland for (j = 0; j < ESCC_SERIAL_REGS; j++) {
3589d248a4bSMark Cave-Ayland cs->rregs[j] = 0;
3599d248a4bSMark Cave-Ayland cs->wregs[j] = 0;
3609d248a4bSMark Cave-Ayland }
361c29cd47eSMark Cave-Ayland
362c29cd47eSMark Cave-Ayland /*
363c29cd47eSMark Cave-Ayland * ...but there is an exception. The "Transmit Interrupts and Transmit
364c29cd47eSMark Cave-Ayland * Buffer Empty Bit" section on page 50 of the ESCC datasheet says of
365c29cd47eSMark Cave-Ayland * the STATUS_TXEMPTY bit in R_STATUS: "After a hardware reset
366c29cd47eSMark Cave-Ayland * (including a hardware reset by software), or a channel reset, this
367c29cd47eSMark Cave-Ayland * bit is set to 1". The Sun PROM checks this bit early on startup and
368c29cd47eSMark Cave-Ayland * gets stuck in an infinite loop if it is not set.
369c29cd47eSMark Cave-Ayland */
370c29cd47eSMark Cave-Ayland cs->rregs[R_STATUS] |= STATUS_TXEMPTY;
371c29cd47eSMark Cave-Ayland
3729d248a4bSMark Cave-Ayland escc_reset_chn(cs);
3739d248a4bSMark Cave-Ayland }
374e80cfcfcSbellard }
375e80cfcfcSbellard
set_rxint(ESCCChannelState * s)3762cc75c32SLaurent Vivier static inline void set_rxint(ESCCChannelState *s)
377ba3c64fbSbellard {
378ba3c64fbSbellard s->rxint = 1;
3790e042025SMark Cave-Ayland /*
3800e042025SMark Cave-Ayland * XXX: missing daisy chaining: escc_chn_b rx should have a lower priority
3810e042025SMark Cave-Ayland * than chn_a rx/tx/special_condition service
3820e042025SMark Cave-Ayland */
383e4a89056Sbellard s->rxint_under_svc = 1;
3842cc75c32SLaurent Vivier if (s->chn == escc_chn_a) {
3859fc391f8SArtyom Tarasenko s->rregs[R_INTR] |= INTR_RXINTA;
3860e042025SMark Cave-Ayland if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
38712abac85Sblueswir1 s->otherchn->rregs[R_IVEC] = IVEC_HIRXINTA;
3880e042025SMark Cave-Ayland } else {
38912abac85Sblueswir1 s->otherchn->rregs[R_IVEC] = IVEC_LORXINTA;
3900e042025SMark Cave-Ayland }
39167deb562Sblueswir1 } else {
3929fc391f8SArtyom Tarasenko s->otherchn->rregs[R_INTR] |= INTR_RXINTB;
3930e042025SMark Cave-Ayland if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
39412abac85Sblueswir1 s->rregs[R_IVEC] = IVEC_HIRXINTB;
3950e042025SMark Cave-Ayland } else {
39612abac85Sblueswir1 s->rregs[R_IVEC] = IVEC_LORXINTB;
397b9652ca3Sblueswir1 }
3980e042025SMark Cave-Ayland }
399b4ed08e0Sblueswir1 escc_update_irq(s);
400ba3c64fbSbellard }
401ba3c64fbSbellard
set_txint(ESCCChannelState * s)4022cc75c32SLaurent Vivier static inline void set_txint(ESCCChannelState *s)
40380637a6aSblueswir1 {
40480637a6aSblueswir1 s->txint = 1;
40580637a6aSblueswir1 if (!s->rxint_under_svc) {
40680637a6aSblueswir1 s->txint_under_svc = 1;
4072cc75c32SLaurent Vivier if (s->chn == escc_chn_a) {
408f53671c0SAurelien Jarno if (s->wregs[W_INTR] & INTR_TXINT) {
4099fc391f8SArtyom Tarasenko s->rregs[R_INTR] |= INTR_TXINTA;
410f53671c0SAurelien Jarno }
4110e042025SMark Cave-Ayland if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
41280637a6aSblueswir1 s->otherchn->rregs[R_IVEC] = IVEC_HITXINTA;
4130e042025SMark Cave-Ayland } else {
41480637a6aSblueswir1 s->otherchn->rregs[R_IVEC] = IVEC_LOTXINTA;
4150e042025SMark Cave-Ayland }
41680637a6aSblueswir1 } else {
41780637a6aSblueswir1 s->rregs[R_IVEC] = IVEC_TXINTB;
418f53671c0SAurelien Jarno if (s->wregs[W_INTR] & INTR_TXINT) {
41980637a6aSblueswir1 s->otherchn->rregs[R_INTR] |= INTR_TXINTB;
4209fc391f8SArtyom Tarasenko }
421f53671c0SAurelien Jarno }
422b4ed08e0Sblueswir1 escc_update_irq(s);
42380637a6aSblueswir1 }
4249fc391f8SArtyom Tarasenko }
42580637a6aSblueswir1
clr_rxint(ESCCChannelState * s)4262cc75c32SLaurent Vivier static inline void clr_rxint(ESCCChannelState *s)
42780637a6aSblueswir1 {
42880637a6aSblueswir1 s->rxint = 0;
42980637a6aSblueswir1 s->rxint_under_svc = 0;
4302cc75c32SLaurent Vivier if (s->chn == escc_chn_a) {
4310e042025SMark Cave-Ayland if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
43280637a6aSblueswir1 s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
4330e042025SMark Cave-Ayland } else {
43480637a6aSblueswir1 s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
4350e042025SMark Cave-Ayland }
43680637a6aSblueswir1 s->rregs[R_INTR] &= ~INTR_RXINTA;
43780637a6aSblueswir1 } else {
4380e042025SMark Cave-Ayland if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
43980637a6aSblueswir1 s->rregs[R_IVEC] = IVEC_HINOINT;
4400e042025SMark Cave-Ayland } else {
44180637a6aSblueswir1 s->rregs[R_IVEC] = IVEC_LONOINT;
4420e042025SMark Cave-Ayland }
44380637a6aSblueswir1 s->otherchn->rregs[R_INTR] &= ~INTR_RXINTB;
44480637a6aSblueswir1 }
4450e042025SMark Cave-Ayland if (s->txint) {
44680637a6aSblueswir1 set_txint(s);
4470e042025SMark Cave-Ayland }
448b4ed08e0Sblueswir1 escc_update_irq(s);
44980637a6aSblueswir1 }
45080637a6aSblueswir1
clr_txint(ESCCChannelState * s)4512cc75c32SLaurent Vivier static inline void clr_txint(ESCCChannelState *s)
452ba3c64fbSbellard {
453ba3c64fbSbellard s->txint = 0;
454e4a89056Sbellard s->txint_under_svc = 0;
4552cc75c32SLaurent Vivier if (s->chn == escc_chn_a) {
4560e042025SMark Cave-Ayland if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
45712abac85Sblueswir1 s->otherchn->rregs[R_IVEC] = IVEC_HINOINT;
4580e042025SMark Cave-Ayland } else {
45912abac85Sblueswir1 s->otherchn->rregs[R_IVEC] = IVEC_LONOINT;
4600e042025SMark Cave-Ayland }
46112abac85Sblueswir1 s->rregs[R_INTR] &= ~INTR_TXINTA;
462b9652ca3Sblueswir1 } else {
4639fc391f8SArtyom Tarasenko s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
4640e042025SMark Cave-Ayland if (s->wregs[W_MINTR] & MINTR_STATUSHI) {
46512abac85Sblueswir1 s->rregs[R_IVEC] = IVEC_HINOINT;
4660e042025SMark Cave-Ayland } else {
46712abac85Sblueswir1 s->rregs[R_IVEC] = IVEC_LONOINT;
4680e042025SMark Cave-Ayland }
46912abac85Sblueswir1 s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB;
470b9652ca3Sblueswir1 }
4710e042025SMark Cave-Ayland if (s->rxint) {
472e4a89056Sbellard set_rxint(s);
4730e042025SMark Cave-Ayland }
474b4ed08e0Sblueswir1 escc_update_irq(s);
475ba3c64fbSbellard }
476ba3c64fbSbellard
escc_update_parameters(ESCCChannelState * s)4772cc75c32SLaurent Vivier static void escc_update_parameters(ESCCChannelState *s)
47835db099dSbellard {
47935db099dSbellard int speed, parity, data_bits, stop_bits;
48035db099dSbellard QEMUSerialSetParams ssp;
48135db099dSbellard
4820e042025SMark Cave-Ayland if (!qemu_chr_fe_backend_connected(&s->chr) || s->type != escc_serial) {
48335db099dSbellard return;
4840e042025SMark Cave-Ayland }
48535db099dSbellard
48612abac85Sblueswir1 if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREN) {
4870e042025SMark Cave-Ayland if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREV) {
48835db099dSbellard parity = 'E';
4890e042025SMark Cave-Ayland } else {
49035db099dSbellard parity = 'O';
4910e042025SMark Cave-Ayland }
49235db099dSbellard } else {
49335db099dSbellard parity = 'N';
49435db099dSbellard }
4950e042025SMark Cave-Ayland if ((s->wregs[W_TXCTRL1] & TXCTRL1_STPMSK) == TXCTRL1_2STOP) {
49635db099dSbellard stop_bits = 2;
4970e042025SMark Cave-Ayland } else {
49835db099dSbellard stop_bits = 1;
4990e042025SMark Cave-Ayland }
50012abac85Sblueswir1 switch (s->wregs[W_TXCTRL2] & TXCTRL2_BITMSK) {
50112abac85Sblueswir1 case TXCTRL2_5BITS:
50235db099dSbellard data_bits = 5;
50335db099dSbellard break;
50412abac85Sblueswir1 case TXCTRL2_7BITS:
50535db099dSbellard data_bits = 7;
50635db099dSbellard break;
50712abac85Sblueswir1 case TXCTRL2_6BITS:
50835db099dSbellard data_bits = 6;
50935db099dSbellard break;
51035db099dSbellard default:
51112abac85Sblueswir1 case TXCTRL2_8BITS:
51235db099dSbellard data_bits = 8;
51335db099dSbellard break;
51435db099dSbellard }
515b4ed08e0Sblueswir1 speed = s->clock / ((s->wregs[W_BRGLO] | (s->wregs[W_BRGHI] << 8)) + 2);
51612abac85Sblueswir1 switch (s->wregs[W_TXCTRL1] & TXCTRL1_CLKMSK) {
51712abac85Sblueswir1 case TXCTRL1_CLK1X:
51835db099dSbellard break;
51912abac85Sblueswir1 case TXCTRL1_CLK16X:
52035db099dSbellard speed /= 16;
52135db099dSbellard break;
52212abac85Sblueswir1 case TXCTRL1_CLK32X:
52335db099dSbellard speed /= 32;
52435db099dSbellard break;
52535db099dSbellard default:
52612abac85Sblueswir1 case TXCTRL1_CLK64X:
52735db099dSbellard speed /= 64;
52835db099dSbellard break;
52935db099dSbellard }
53035db099dSbellard ssp.speed = speed;
53135db099dSbellard ssp.parity = parity;
53235db099dSbellard ssp.data_bits = data_bits;
53335db099dSbellard ssp.stop_bits = stop_bits;
53430c2f238SBlue Swirl trace_escc_update_parameters(CHN_C(s), speed, parity, data_bits, stop_bits);
5355345fdb4SMarc-André Lureau qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp);
53635db099dSbellard }
53735db099dSbellard
escc_mem_write(void * opaque,hwaddr addr,uint64_t val,unsigned size)538a8170e5eSAvi Kivity static void escc_mem_write(void *opaque, hwaddr addr,
53923c5e4caSAvi Kivity uint64_t val, unsigned size)
540e80cfcfcSbellard {
5413cf63ff2SPaolo Bonzini ESCCState *serial = opaque;
5422cc75c32SLaurent Vivier ESCCChannelState *s;
543e80cfcfcSbellard uint32_t saddr;
544e80cfcfcSbellard int newreg, channel;
545e80cfcfcSbellard
546e80cfcfcSbellard val &= 0xff;
547b43047a2SLaurent Vivier saddr = (addr >> reg_shift(serial)) & 1;
548b43047a2SLaurent Vivier channel = (addr >> chn_shift(serial)) & 1;
549b3ceef24Sblueswir1 s = &serial->chn[channel];
550e80cfcfcSbellard switch (saddr) {
55112abac85Sblueswir1 case SERIAL_CTRL:
55230c2f238SBlue Swirl trace_escc_mem_writeb_ctrl(CHN_C(s), s->reg, val & 0xff);
553e80cfcfcSbellard newreg = 0;
554e80cfcfcSbellard switch (s->reg) {
55512abac85Sblueswir1 case W_CMD:
55612abac85Sblueswir1 newreg = val & CMD_PTR_MASK;
55712abac85Sblueswir1 val &= CMD_CMD_MASK;
558e80cfcfcSbellard switch (val) {
55912abac85Sblueswir1 case CMD_HI:
56012abac85Sblueswir1 newreg |= CMD_HI;
561e80cfcfcSbellard break;
56212abac85Sblueswir1 case CMD_CLR_TXINT:
563ba3c64fbSbellard clr_txint(s);
564ba3c64fbSbellard break;
56512abac85Sblueswir1 case CMD_CLR_IUS:
5669fc391f8SArtyom Tarasenko if (s->rxint_under_svc) {
5679fc391f8SArtyom Tarasenko s->rxint_under_svc = 0;
5689fc391f8SArtyom Tarasenko if (s->txint) {
5699fc391f8SArtyom Tarasenko set_txint(s);
5709fc391f8SArtyom Tarasenko }
5719fc391f8SArtyom Tarasenko } else if (s->txint_under_svc) {
5729fc391f8SArtyom Tarasenko s->txint_under_svc = 0;
5739fc391f8SArtyom Tarasenko }
5749fc391f8SArtyom Tarasenko escc_update_irq(s);
575e80cfcfcSbellard break;
576e80cfcfcSbellard default:
577e80cfcfcSbellard break;
578e80cfcfcSbellard }
579e80cfcfcSbellard break;
58015a2a1a4SMark Cave-Ayland case W_RXCTRL:
58115a2a1a4SMark Cave-Ayland s->wregs[s->reg] = val;
58215a2a1a4SMark Cave-Ayland if (val & RXCTRL_HUNT) {
58315a2a1a4SMark Cave-Ayland s->rregs[R_STATUS] |= STATUS_SYNC;
58415a2a1a4SMark Cave-Ayland }
58515a2a1a4SMark Cave-Ayland break;
58615a2a1a4SMark Cave-Ayland case W_INTR ... W_IVEC:
58712abac85Sblueswir1 case W_SYNC1 ... W_TXBUF:
58812abac85Sblueswir1 case W_MISC1 ... W_CLOCK:
58912abac85Sblueswir1 case W_MISC2 ... W_EXTINT:
590e80cfcfcSbellard s->wregs[s->reg] = val;
591e80cfcfcSbellard break;
59212abac85Sblueswir1 case W_TXCTRL1:
593319e89cdSMark Cave-Ayland s->wregs[s->reg] = val;
594319e89cdSMark Cave-Ayland /*
595319e89cdSMark Cave-Ayland * The ESCC datasheet states that SPEC_ALLSENT is always set in
596319e89cdSMark Cave-Ayland * sync mode, and set in async mode when all characters have
597319e89cdSMark Cave-Ayland * cleared the transmitter. Since writes to SERIAL_DATA use the
598319e89cdSMark Cave-Ayland * blocking qemu_chr_fe_write_all() function to write each
599319e89cdSMark Cave-Ayland * character, the guest can never see the state when async data
600319e89cdSMark Cave-Ayland * is in the process of being transmitted so we can set this bit
601319e89cdSMark Cave-Ayland * unconditionally regardless of the state of the W_TXCTRL1 mode
602319e89cdSMark Cave-Ayland * bits.
603319e89cdSMark Cave-Ayland */
604319e89cdSMark Cave-Ayland s->rregs[R_SPEC] |= SPEC_ALLSENT;
605319e89cdSMark Cave-Ayland escc_update_parameters(s);
606319e89cdSMark Cave-Ayland break;
60712abac85Sblueswir1 case W_TXCTRL2:
608796d8286Sblueswir1 s->wregs[s->reg] = val;
609b4ed08e0Sblueswir1 escc_update_parameters(s);
610796d8286Sblueswir1 break;
61112abac85Sblueswir1 case W_BRGLO:
61212abac85Sblueswir1 case W_BRGHI:
61335db099dSbellard s->wregs[s->reg] = val;
614796d8286Sblueswir1 s->rregs[s->reg] = val;
615b4ed08e0Sblueswir1 escc_update_parameters(s);
61635db099dSbellard break;
61712abac85Sblueswir1 case W_MINTR:
61812abac85Sblueswir1 switch (val & MINTR_RST_MASK) {
619e80cfcfcSbellard case 0:
620e80cfcfcSbellard default:
621e80cfcfcSbellard break;
62212abac85Sblueswir1 case MINTR_RST_B:
6238e8aa965SMark Cave-Ayland trace_escc_soft_reset_chn(CHN_C(&serial->chn[0]));
6248e8aa965SMark Cave-Ayland escc_soft_reset_chn(&serial->chn[0]);
625e80cfcfcSbellard return;
62612abac85Sblueswir1 case MINTR_RST_A:
6278e8aa965SMark Cave-Ayland trace_escc_soft_reset_chn(CHN_C(&serial->chn[1]));
6288e8aa965SMark Cave-Ayland escc_soft_reset_chn(&serial->chn[1]);
629e80cfcfcSbellard return;
63012abac85Sblueswir1 case MINTR_RST_ALL:
631bf4fbb69SMark Cave-Ayland trace_escc_hard_reset();
632bf4fbb69SMark Cave-Ayland escc_hard_reset_chn(&serial->chn[0]);
633bf4fbb69SMark Cave-Ayland escc_hard_reset_chn(&serial->chn[1]);
634e80cfcfcSbellard return;
635e80cfcfcSbellard }
636e80cfcfcSbellard break;
637e80cfcfcSbellard default:
638e80cfcfcSbellard break;
639e80cfcfcSbellard }
6400e042025SMark Cave-Ayland if (s->reg == 0) {
641e80cfcfcSbellard s->reg = newreg;
6420e042025SMark Cave-Ayland } else {
643e80cfcfcSbellard s->reg = 0;
6440e042025SMark Cave-Ayland }
645e80cfcfcSbellard break;
64612abac85Sblueswir1 case SERIAL_DATA:
64730c2f238SBlue Swirl trace_escc_mem_writeb_data(CHN_C(s), val);
6486b99a110SStephen Checkoway /*
6496b99a110SStephen Checkoway * Lower the irq when data is written to the Tx buffer and no other
6506b99a110SStephen Checkoway * interrupts are currently pending. The irq will be raised again once
6516b99a110SStephen Checkoway * the Tx buffer becomes empty below.
6526b99a110SStephen Checkoway */
6536b99a110SStephen Checkoway s->txint = 0;
6546b99a110SStephen Checkoway escc_update_irq(s);
655e80cfcfcSbellard s->tx = val;
6560e042025SMark Cave-Ayland if (s->wregs[W_TXCTRL2] & TXCTRL2_TXEN) { /* tx enabled */
65702388b59SThomas Huth if (s->wregs[W_MISC2] & MISC2_LCL_LOOP) {
65802388b59SThomas Huth serial_receive_byte(s, s->tx);
65902388b59SThomas Huth } else if (qemu_chr_fe_backend_connected(&s->chr)) {
6600e042025SMark Cave-Ayland /*
6610e042025SMark Cave-Ayland * XXX this blocks entire thread. Rewrite to use
6620e042025SMark Cave-Ayland * qemu_chr_fe_write and background I/O callbacks
6630e042025SMark Cave-Ayland */
6645345fdb4SMarc-André Lureau qemu_chr_fe_write_all(&s->chr, &s->tx, 1);
6652cc75c32SLaurent Vivier } else if (s->type == escc_kbd && !s->disabled) {
6668be1f5c8Sbellard handle_kbd_command(s, val);
6678be1f5c8Sbellard }
66896c4f569Sblueswir1 }
6690e042025SMark Cave-Ayland s->rregs[R_STATUS] |= STATUS_TXEMPTY; /* Tx buffer empty */
6700e042025SMark Cave-Ayland s->rregs[R_SPEC] |= SPEC_ALLSENT; /* All sent */
671ba3c64fbSbellard set_txint(s);
672e80cfcfcSbellard break;
673e80cfcfcSbellard default:
674e80cfcfcSbellard break;
675e80cfcfcSbellard }
676e80cfcfcSbellard }
677e80cfcfcSbellard
escc_mem_read(void * opaque,hwaddr addr,unsigned size)678a8170e5eSAvi Kivity static uint64_t escc_mem_read(void *opaque, hwaddr addr,
67923c5e4caSAvi Kivity unsigned size)
680e80cfcfcSbellard {
6813cf63ff2SPaolo Bonzini ESCCState *serial = opaque;
6822cc75c32SLaurent Vivier ESCCChannelState *s;
683e80cfcfcSbellard uint32_t saddr;
684e80cfcfcSbellard uint32_t ret;
685e80cfcfcSbellard int channel;
686e80cfcfcSbellard
687b43047a2SLaurent Vivier saddr = (addr >> reg_shift(serial)) & 1;
688b43047a2SLaurent Vivier channel = (addr >> chn_shift(serial)) & 1;
689b3ceef24Sblueswir1 s = &serial->chn[channel];
690e80cfcfcSbellard switch (saddr) {
69112abac85Sblueswir1 case SERIAL_CTRL:
69230c2f238SBlue Swirl trace_escc_mem_readb_ctrl(CHN_C(s), s->reg, s->rregs[s->reg]);
693e80cfcfcSbellard ret = s->rregs[s->reg];
694e80cfcfcSbellard s->reg = 0;
695e80cfcfcSbellard return ret;
69612abac85Sblueswir1 case SERIAL_DATA:
69712abac85Sblueswir1 s->rregs[R_STATUS] &= ~STATUS_RXAV;
698ba3c64fbSbellard clr_rxint(s);
6992cc75c32SLaurent Vivier if (s->type == escc_kbd || s->type == escc_mouse) {
7008be1f5c8Sbellard ret = get_queue(s);
7012cc75c32SLaurent Vivier } else {
7028be1f5c8Sbellard ret = s->rx;
7032cc75c32SLaurent Vivier }
70430c2f238SBlue Swirl trace_escc_mem_readb_data(CHN_C(s), ret);
7055345fdb4SMarc-André Lureau qemu_chr_fe_accept_input(&s->chr);
7068be1f5c8Sbellard return ret;
707e80cfcfcSbellard default:
708e80cfcfcSbellard break;
709e80cfcfcSbellard }
710e80cfcfcSbellard return 0;
711e80cfcfcSbellard }
712e80cfcfcSbellard
71323c5e4caSAvi Kivity static const MemoryRegionOps escc_mem_ops = {
71423c5e4caSAvi Kivity .read = escc_mem_read,
71523c5e4caSAvi Kivity .write = escc_mem_write,
71623c5e4caSAvi Kivity .endianness = DEVICE_NATIVE_ENDIAN,
71723c5e4caSAvi Kivity .valid = {
71823c5e4caSAvi Kivity .min_access_size = 1,
71923c5e4caSAvi Kivity .max_access_size = 1,
72023c5e4caSAvi Kivity },
72123c5e4caSAvi Kivity };
72223c5e4caSAvi Kivity
serial_can_receive(void * opaque)723e80cfcfcSbellard static int serial_can_receive(void *opaque)
724e80cfcfcSbellard {
7252cc75c32SLaurent Vivier ESCCChannelState *s = opaque;
726e4a89056Sbellard int ret;
727e4a89056Sbellard
7280e042025SMark Cave-Ayland if (((s->wregs[W_RXCTRL] & RXCTRL_RXEN) == 0) /* Rx not enabled */
7290e042025SMark Cave-Ayland || ((s->rregs[R_STATUS] & STATUS_RXAV) == STATUS_RXAV)) {
7300e042025SMark Cave-Ayland /* char already available */
731e4a89056Sbellard ret = 0;
7320e042025SMark Cave-Ayland } else {
733e4a89056Sbellard ret = 1;
7340e042025SMark Cave-Ayland }
735e4a89056Sbellard return ret;
736e80cfcfcSbellard }
737e80cfcfcSbellard
serial_receive_byte(ESCCChannelState * s,int ch)7382cc75c32SLaurent Vivier static void serial_receive_byte(ESCCChannelState *s, int ch)
739e80cfcfcSbellard {
74030c2f238SBlue Swirl trace_escc_serial_receive_byte(CHN_C(s), ch);
74112abac85Sblueswir1 s->rregs[R_STATUS] |= STATUS_RXAV;
742e80cfcfcSbellard s->rx = ch;
743ba3c64fbSbellard set_rxint(s);
744e80cfcfcSbellard }
745e80cfcfcSbellard
serial_receive_break(ESCCChannelState * s)7462cc75c32SLaurent Vivier static void serial_receive_break(ESCCChannelState *s)
747e80cfcfcSbellard {
74812abac85Sblueswir1 s->rregs[R_STATUS] |= STATUS_BRK;
749b4ed08e0Sblueswir1 escc_update_irq(s);
750e80cfcfcSbellard }
751e80cfcfcSbellard
serial_receive1(void * opaque,const uint8_t * buf,int size)752e80cfcfcSbellard static void serial_receive1(void *opaque, const uint8_t *buf, int size)
753e80cfcfcSbellard {
7542cc75c32SLaurent Vivier ESCCChannelState *s = opaque;
755e80cfcfcSbellard serial_receive_byte(s, buf[0]);
756e80cfcfcSbellard }
757e80cfcfcSbellard
serial_event(void * opaque,QEMUChrEvent event)758083b266fSPhilippe Mathieu-Daudé static void serial_event(void *opaque, QEMUChrEvent event)
759e80cfcfcSbellard {
7602cc75c32SLaurent Vivier ESCCChannelState *s = opaque;
7610e042025SMark Cave-Ayland if (event == CHR_EVENT_BREAK) {
762e80cfcfcSbellard serial_receive_break(s);
763e80cfcfcSbellard }
7640e042025SMark Cave-Ayland }
765e80cfcfcSbellard
766bdb78caeSBlue Swirl static const VMStateDescription vmstate_escc_chn = {
767bdb78caeSBlue Swirl .name = "escc_chn",
768bdb78caeSBlue Swirl .version_id = 2,
769bdb78caeSBlue Swirl .minimum_version_id = 1,
7702f6cab05SRichard Henderson .fields = (const VMStateField[]) {
7712cc75c32SLaurent Vivier VMSTATE_UINT32(vmstate_dummy, ESCCChannelState),
7722cc75c32SLaurent Vivier VMSTATE_UINT32(reg, ESCCChannelState),
7732cc75c32SLaurent Vivier VMSTATE_UINT32(rxint, ESCCChannelState),
7742cc75c32SLaurent Vivier VMSTATE_UINT32(txint, ESCCChannelState),
7752cc75c32SLaurent Vivier VMSTATE_UINT32(rxint_under_svc, ESCCChannelState),
7762cc75c32SLaurent Vivier VMSTATE_UINT32(txint_under_svc, ESCCChannelState),
7772cc75c32SLaurent Vivier VMSTATE_UINT8(rx, ESCCChannelState),
7782cc75c32SLaurent Vivier VMSTATE_UINT8(tx, ESCCChannelState),
7792cc75c32SLaurent Vivier VMSTATE_BUFFER(wregs, ESCCChannelState),
7802cc75c32SLaurent Vivier VMSTATE_BUFFER(rregs, ESCCChannelState),
781bdb78caeSBlue Swirl VMSTATE_END_OF_LIST()
782e80cfcfcSbellard }
783bdb78caeSBlue Swirl };
784e80cfcfcSbellard
785bdb78caeSBlue Swirl static const VMStateDescription vmstate_escc = {
786bdb78caeSBlue Swirl .name = "escc",
787bdb78caeSBlue Swirl .version_id = 2,
788bdb78caeSBlue Swirl .minimum_version_id = 1,
7892f6cab05SRichard Henderson .fields = (const VMStateField[]) {
7903cf63ff2SPaolo Bonzini VMSTATE_STRUCT_ARRAY(chn, ESCCState, 2, 2, vmstate_escc_chn,
7912cc75c32SLaurent Vivier ESCCChannelState),
792bdb78caeSBlue Swirl VMSTATE_END_OF_LIST()
793e80cfcfcSbellard }
794bdb78caeSBlue Swirl };
795e80cfcfcSbellard
sunkbd_handle_event(DeviceState * dev,QemuConsole * src,InputEvent * evt)79665e7545eSGerd Hoffmann static void sunkbd_handle_event(DeviceState *dev, QemuConsole *src,
79765e7545eSGerd Hoffmann InputEvent *evt)
798e80cfcfcSbellard {
7992cc75c32SLaurent Vivier ESCCChannelState *s = (ESCCChannelState *)dev;
80065e7545eSGerd Hoffmann int qcode, keycode;
801b5a1b443SEric Blake InputKeyEvent *key;
8028be1f5c8Sbellard
803568c73a4SEric Blake assert(evt->type == INPUT_EVENT_KIND_KEY);
80432bafa8fSEric Blake key = evt->u.key.data;
805b5a1b443SEric Blake qcode = qemu_input_key_value_to_qcode(key->key);
806977c736fSMarkus Armbruster trace_escc_sunkbd_event_in(qcode, QKeyCode_str(qcode),
807b5a1b443SEric Blake key->down);
80865e7545eSGerd Hoffmann
80965e7545eSGerd Hoffmann if (qcode == Q_KEY_CODE_CAPS_LOCK) {
810b5a1b443SEric Blake if (key->down) {
811bbbb2f0aSblueswir1 s->caps_lock_mode ^= 1;
81265e7545eSGerd Hoffmann if (s->caps_lock_mode == 2) {
81365e7545eSGerd Hoffmann return; /* Drop second press */
81443febf49Sblueswir1 }
81543febf49Sblueswir1 } else {
81665e7545eSGerd Hoffmann s->caps_lock_mode ^= 2;
81765e7545eSGerd Hoffmann if (s->caps_lock_mode == 3) {
81865e7545eSGerd Hoffmann return; /* Drop first release */
81943febf49Sblueswir1 }
8208be1f5c8Sbellard }
82165e7545eSGerd Hoffmann }
82265e7545eSGerd Hoffmann
82365e7545eSGerd Hoffmann if (qcode == Q_KEY_CODE_NUM_LOCK) {
824b5a1b443SEric Blake if (key->down) {
82565e7545eSGerd Hoffmann s->num_lock_mode ^= 1;
82665e7545eSGerd Hoffmann if (s->num_lock_mode == 2) {
82765e7545eSGerd Hoffmann return; /* Drop second press */
82865e7545eSGerd Hoffmann }
82965e7545eSGerd Hoffmann } else {
83065e7545eSGerd Hoffmann s->num_lock_mode ^= 2;
83165e7545eSGerd Hoffmann if (s->num_lock_mode == 3) {
83265e7545eSGerd Hoffmann return; /* Drop first release */
83365e7545eSGerd Hoffmann }
83465e7545eSGerd Hoffmann }
83565e7545eSGerd Hoffmann }
83665e7545eSGerd Hoffmann
8379aaf11e7SDaniel P. Berrangé if (qcode >= qemu_input_map_qcode_to_sun_len) {
838e709a61aSDaniel P. Berrange return;
839e709a61aSDaniel P. Berrange }
840e709a61aSDaniel P. Berrange
841e709a61aSDaniel P. Berrange keycode = qemu_input_map_qcode_to_sun[qcode];
842b5a1b443SEric Blake if (!key->down) {
84365e7545eSGerd Hoffmann keycode |= 0x80;
84465e7545eSGerd Hoffmann }
84565e7545eSGerd Hoffmann trace_escc_sunkbd_event_out(keycode);
84665e7545eSGerd Hoffmann put_queue(s, keycode);
84765e7545eSGerd Hoffmann }
84865e7545eSGerd Hoffmann
849b1be65f6SPhilippe Mathieu-Daudé static const QemuInputHandler sunkbd_handler = {
85065e7545eSGerd Hoffmann .name = "sun keyboard",
85165e7545eSGerd Hoffmann .mask = INPUT_EVENT_MASK_KEY,
85265e7545eSGerd Hoffmann .event = sunkbd_handle_event,
85365e7545eSGerd Hoffmann };
8548be1f5c8Sbellard
sunkbd_layout_dip_switch(const char * kbd_layout)8556b90a4cdSHenrik Carlqvist static uint8_t sunkbd_layout_dip_switch(const char *kbd_layout)
8566b90a4cdSHenrik Carlqvist {
8576b90a4cdSHenrik Carlqvist /* Return the value of the dip-switches in a SUN Type 5 keyboard */
8586b90a4cdSHenrik Carlqvist static uint8_t ret = 0xff;
8596b90a4cdSHenrik Carlqvist
8606b90a4cdSHenrik Carlqvist if ((ret == 0xff) && kbd_layout) {
8616b90a4cdSHenrik Carlqvist int i;
8626b90a4cdSHenrik Carlqvist struct layout_values {
8636b90a4cdSHenrik Carlqvist const char *lang;
8646b90a4cdSHenrik Carlqvist uint8_t dip;
8656b90a4cdSHenrik Carlqvist } languages[] =
8666b90a4cdSHenrik Carlqvist /*
8676b90a4cdSHenrik Carlqvist * Dip values from table 3-16 Layouts for Type 4, 5 and 5c Keyboards
8686b90a4cdSHenrik Carlqvist */
8696b90a4cdSHenrik Carlqvist {
8706b90a4cdSHenrik Carlqvist {"en-us", 0x21}, /* U.S.A. (US5.kt) */
8716b90a4cdSHenrik Carlqvist /* 0x22 is some other US (US_UNIX5.kt) */
8726b90a4cdSHenrik Carlqvist {"fr", 0x23}, /* France (France5.kt) */
8736b90a4cdSHenrik Carlqvist {"da", 0x24}, /* Denmark (Denmark5.kt) */
8746b90a4cdSHenrik Carlqvist {"de", 0x25}, /* Germany (Germany5.kt) */
8756b90a4cdSHenrik Carlqvist {"it", 0x26}, /* Italy (Italy5.kt) */
8766b90a4cdSHenrik Carlqvist {"nl", 0x27}, /* The Netherlands (Netherland5.kt) */
8776b90a4cdSHenrik Carlqvist {"no", 0x28}, /* Norway (Norway.kt) */
8786b90a4cdSHenrik Carlqvist {"pt", 0x29}, /* Portugal (Portugal5.kt) */
8796b90a4cdSHenrik Carlqvist {"es", 0x2a}, /* Spain (Spain5.kt) */
8806b90a4cdSHenrik Carlqvist {"sv", 0x2b}, /* Sweden (Sweden5.kt) */
8816b90a4cdSHenrik Carlqvist {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */
8826b90a4cdSHenrik Carlqvist {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */
8836b90a4cdSHenrik Carlqvist {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */
8846b90a4cdSHenrik Carlqvist {"ko", 0x2f}, /* Korea (Korea5.kt) */
8856b90a4cdSHenrik Carlqvist {"tw", 0x30}, /* Taiwan (Taiwan5.kt) */
8866b90a4cdSHenrik Carlqvist {"ja", 0x31}, /* Japan (Japan5.kt) */
8876b90a4cdSHenrik Carlqvist {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */
8886b90a4cdSHenrik Carlqvist {"hu", 0x33}, /* Hungary (Hungary5.kt) */
8896b90a4cdSHenrik Carlqvist {"pl", 0x34}, /* Poland (Poland5.kt) */
8906b90a4cdSHenrik Carlqvist {"cz", 0x35}, /* Czech (Czech5.kt) */
8916b90a4cdSHenrik Carlqvist {"ru", 0x36}, /* Russia (Russia5.kt) */
8926b90a4cdSHenrik Carlqvist {"lv", 0x37}, /* Latvia (Latvia5.kt) */
8936b90a4cdSHenrik Carlqvist {"tr", 0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */
8946b90a4cdSHenrik Carlqvist {"gr", 0x39}, /* Greece (Greece5.kt) */
8956b90a4cdSHenrik Carlqvist {"ar", 0x3a}, /* Arabic (Arabic5.kt) */
8966b90a4cdSHenrik Carlqvist {"lt", 0x3b}, /* Lithuania (Lithuania5.kt) */
8976b90a4cdSHenrik Carlqvist {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */
8986b90a4cdSHenrik Carlqvist {"be", 0x3c}, /* Belgium (Belgian5.kt) */
8996b90a4cdSHenrik Carlqvist };
9006b90a4cdSHenrik Carlqvist
9016b90a4cdSHenrik Carlqvist for (i = 0;
9026b90a4cdSHenrik Carlqvist i < sizeof(languages) / sizeof(struct layout_values);
9036b90a4cdSHenrik Carlqvist i++) {
9046b90a4cdSHenrik Carlqvist if (!strcmp(kbd_layout, languages[i].lang)) {
9056b90a4cdSHenrik Carlqvist ret = languages[i].dip;
9066b90a4cdSHenrik Carlqvist return ret;
9076b90a4cdSHenrik Carlqvist }
9086b90a4cdSHenrik Carlqvist }
9096b90a4cdSHenrik Carlqvist
9106b90a4cdSHenrik Carlqvist /* Found no known language code */
9116b90a4cdSHenrik Carlqvist if ((kbd_layout[0] >= '0') && (kbd_layout[0] <= '9')) {
9126b90a4cdSHenrik Carlqvist unsigned int tmp;
9136b90a4cdSHenrik Carlqvist
9146b90a4cdSHenrik Carlqvist /* As a fallback we also accept numeric dip switch value */
9156b90a4cdSHenrik Carlqvist if (!qemu_strtoui(kbd_layout, NULL, 0, &tmp)) {
9166b90a4cdSHenrik Carlqvist ret = tmp;
9176b90a4cdSHenrik Carlqvist }
9186b90a4cdSHenrik Carlqvist }
9196b90a4cdSHenrik Carlqvist }
9206b90a4cdSHenrik Carlqvist
9216b90a4cdSHenrik Carlqvist if (ret == 0xff) {
9226b90a4cdSHenrik Carlqvist /* Final fallback if keyboard_layout was not set or recognized */
9236b90a4cdSHenrik Carlqvist ret = 0x21; /* en-us layout */
9246b90a4cdSHenrik Carlqvist }
9256b90a4cdSHenrik Carlqvist return ret;
9266b90a4cdSHenrik Carlqvist }
9276b90a4cdSHenrik Carlqvist
handle_kbd_command(ESCCChannelState * s,int val)9282cc75c32SLaurent Vivier static void handle_kbd_command(ESCCChannelState *s, int val)
9298be1f5c8Sbellard {
93030c2f238SBlue Swirl trace_escc_kbd_command(val);
9310e042025SMark Cave-Ayland if (s->led_mode) { /* Ignore led byte */
93243febf49Sblueswir1 s->led_mode = 0;
93343febf49Sblueswir1 return;
93443febf49Sblueswir1 }
9358be1f5c8Sbellard switch (val) {
9360e042025SMark Cave-Ayland case 1: /* Reset, return type code */
93767deb562Sblueswir1 clear_queue(s);
9388be1f5c8Sbellard put_queue(s, 0xff);
9390e042025SMark Cave-Ayland put_queue(s, 4); /* Type 4 */
94043febf49Sblueswir1 put_queue(s, 0x7f);
94143febf49Sblueswir1 break;
9420e042025SMark Cave-Ayland case 0xe: /* Set leds */
94343febf49Sblueswir1 s->led_mode = 1;
9448be1f5c8Sbellard break;
9450e042025SMark Cave-Ayland case 7: /* Query layout */
94667deb562Sblueswir1 case 0xf:
94767deb562Sblueswir1 clear_queue(s);
9488be1f5c8Sbellard put_queue(s, 0xfe);
9496b90a4cdSHenrik Carlqvist put_queue(s, sunkbd_layout_dip_switch(s->sunkbd_layout));
9508be1f5c8Sbellard break;
9518be1f5c8Sbellard default:
9528be1f5c8Sbellard break;
9538be1f5c8Sbellard }
954e80cfcfcSbellard }
955e80cfcfcSbellard
sunmouse_handle_event(DeviceState * dev,QemuConsole * src,InputEvent * evt)95634acb67fSMark Cave-Ayland static void sunmouse_handle_event(DeviceState *dev, QemuConsole *src,
95734acb67fSMark Cave-Ayland InputEvent *evt)
958e80cfcfcSbellard {
95934acb67fSMark Cave-Ayland ESCCChannelState *s = (ESCCChannelState *)dev;
96034acb67fSMark Cave-Ayland InputMoveEvent *move;
96134acb67fSMark Cave-Ayland InputBtnEvent *btn;
96234acb67fSMark Cave-Ayland static const int bmap[INPUT_BUTTON__MAX] = {
96334acb67fSMark Cave-Ayland [INPUT_BUTTON_LEFT] = 0x4,
96434acb67fSMark Cave-Ayland [INPUT_BUTTON_MIDDLE] = 0x2,
96534acb67fSMark Cave-Ayland [INPUT_BUTTON_RIGHT] = 0x1,
96634acb67fSMark Cave-Ayland };
96734acb67fSMark Cave-Ayland
96834acb67fSMark Cave-Ayland switch (evt->type) {
96934acb67fSMark Cave-Ayland case INPUT_EVENT_KIND_REL:
97034acb67fSMark Cave-Ayland move = evt->u.rel.data;
97134acb67fSMark Cave-Ayland if (move->axis == INPUT_AXIS_X) {
97234acb67fSMark Cave-Ayland s->sunmouse_dx += move->value;
97334acb67fSMark Cave-Ayland } else if (move->axis == INPUT_AXIS_Y) {
97434acb67fSMark Cave-Ayland s->sunmouse_dy -= move->value;
97534acb67fSMark Cave-Ayland }
97634acb67fSMark Cave-Ayland break;
97734acb67fSMark Cave-Ayland
97834acb67fSMark Cave-Ayland case INPUT_EVENT_KIND_BTN:
97934acb67fSMark Cave-Ayland btn = evt->u.btn.data;
98034acb67fSMark Cave-Ayland if (bmap[btn->button]) {
98134acb67fSMark Cave-Ayland if (btn->down) {
98234acb67fSMark Cave-Ayland s->sunmouse_buttons |= bmap[btn->button];
98334acb67fSMark Cave-Ayland } else {
98434acb67fSMark Cave-Ayland s->sunmouse_buttons &= ~bmap[btn->button];
98534acb67fSMark Cave-Ayland }
98634acb67fSMark Cave-Ayland /* Indicate we have a supported button event */
98734acb67fSMark Cave-Ayland s->sunmouse_buttons |= 0x80;
98834acb67fSMark Cave-Ayland }
98934acb67fSMark Cave-Ayland break;
99034acb67fSMark Cave-Ayland
99134acb67fSMark Cave-Ayland default:
99234acb67fSMark Cave-Ayland /* keep gcc happy */
99334acb67fSMark Cave-Ayland break;
99434acb67fSMark Cave-Ayland }
99534acb67fSMark Cave-Ayland }
99634acb67fSMark Cave-Ayland
sunmouse_sync(DeviceState * dev)99734acb67fSMark Cave-Ayland static void sunmouse_sync(DeviceState *dev)
99834acb67fSMark Cave-Ayland {
99934acb67fSMark Cave-Ayland ESCCChannelState *s = (ESCCChannelState *)dev;
1000e80cfcfcSbellard int ch;
1001e80cfcfcSbellard
100234acb67fSMark Cave-Ayland if (s->sunmouse_dx == 0 && s->sunmouse_dy == 0 &&
100334acb67fSMark Cave-Ayland (s->sunmouse_buttons & 0x80) == 0) {
100434acb67fSMark Cave-Ayland /* Nothing to do after button event filter */
100534acb67fSMark Cave-Ayland return;
100634acb67fSMark Cave-Ayland }
100734acb67fSMark Cave-Ayland
100834acb67fSMark Cave-Ayland /* Clear our button event flag */
100934acb67fSMark Cave-Ayland s->sunmouse_buttons &= ~0x80;
101034acb67fSMark Cave-Ayland trace_escc_sunmouse_event(s->sunmouse_dx, s->sunmouse_dy,
101134acb67fSMark Cave-Ayland s->sunmouse_buttons);
1012715748faSbellard ch = 0x80 | 0x7; /* protocol start byte, no buttons pressed */
101334acb67fSMark Cave-Ayland ch ^= s->sunmouse_buttons;
1014715748faSbellard put_queue(s, ch);
1015715748faSbellard
101634acb67fSMark Cave-Ayland ch = s->sunmouse_dx;
10170e042025SMark Cave-Ayland if (ch > 127) {
1018715748faSbellard ch = 127;
10190e042025SMark Cave-Ayland } else if (ch < -127) {
1020715748faSbellard ch = -127;
10210e042025SMark Cave-Ayland }
1022715748faSbellard put_queue(s, ch & 0xff);
102334acb67fSMark Cave-Ayland s->sunmouse_dx -= ch;
1024715748faSbellard
102534acb67fSMark Cave-Ayland ch = s->sunmouse_dy;
10260e042025SMark Cave-Ayland if (ch > 127) {
1027715748faSbellard ch = 127;
10280e042025SMark Cave-Ayland } else if (ch < -127) {
1029715748faSbellard ch = -127;
10300e042025SMark Cave-Ayland }
1031715748faSbellard put_queue(s, ch & 0xff);
103234acb67fSMark Cave-Ayland s->sunmouse_dy -= ch;
1033715748faSbellard
10340e042025SMark Cave-Ayland /* MSC protocol specifies two extra motion bytes */
1035715748faSbellard put_queue(s, 0);
1036715748faSbellard put_queue(s, 0);
1037e80cfcfcSbellard }
1038e80cfcfcSbellard
103934acb67fSMark Cave-Ayland static const QemuInputHandler sunmouse_handler = {
104034acb67fSMark Cave-Ayland .name = "QEMU Sun Mouse",
104134acb67fSMark Cave-Ayland .mask = INPUT_EVENT_MASK_BTN | INPUT_EVENT_MASK_REL,
104234acb67fSMark Cave-Ayland .event = sunmouse_handle_event,
104334acb67fSMark Cave-Ayland .sync = sunmouse_sync,
104434acb67fSMark Cave-Ayland };
104534acb67fSMark Cave-Ayland
escc_init1(Object * obj)1046e7c91369Sxiaoqiang zhao static void escc_init1(Object *obj)
10476c319c82SBlue Swirl {
1048e7c91369Sxiaoqiang zhao ESCCState *s = ESCC(obj);
1049e7c91369Sxiaoqiang zhao SysBusDevice *dev = SYS_BUS_DEVICE(obj);
10506c319c82SBlue Swirl unsigned int i;
10516c319c82SBlue Swirl
10528be1f5c8Sbellard for (i = 0; i < 2; i++) {
10536c319c82SBlue Swirl sysbus_init_irq(dev, &s->chn[i].irq);
10548be1f5c8Sbellard s->chn[i].chn = 1 - i;
1055e7c91369Sxiaoqiang zhao }
1056e7c91369Sxiaoqiang zhao s->chn[0].otherchn = &s->chn[1];
1057e7c91369Sxiaoqiang zhao s->chn[1].otherchn = &s->chn[0];
1058e7c91369Sxiaoqiang zhao
1059e7c91369Sxiaoqiang zhao sysbus_init_mmio(dev, &s->mmio);
1060e7c91369Sxiaoqiang zhao }
1061e7c91369Sxiaoqiang zhao
escc_realize(DeviceState * dev,Error ** errp)1062e7c91369Sxiaoqiang zhao static void escc_realize(DeviceState *dev, Error **errp)
1063e7c91369Sxiaoqiang zhao {
1064e7c91369Sxiaoqiang zhao ESCCState *s = ESCC(dev);
1065e7c91369Sxiaoqiang zhao unsigned int i;
1066e7c91369Sxiaoqiang zhao
10674b3eec91Sxiaoqiang zhao s->chn[0].disabled = s->disabled;
10684b3eec91Sxiaoqiang zhao s->chn[1].disabled = s->disabled;
10694b3eec91Sxiaoqiang zhao
10704b3eec91Sxiaoqiang zhao memory_region_init_io(&s->mmio, OBJECT(dev), &escc_mem_ops, s, "escc",
10714b3eec91Sxiaoqiang zhao ESCC_SIZE << s->it_shift);
10724b3eec91Sxiaoqiang zhao
1073e7c91369Sxiaoqiang zhao for (i = 0; i < 2; i++) {
107430650701SAnton Nefedov if (qemu_chr_fe_backend_connected(&s->chn[i].chr)) {
10754b3eec91Sxiaoqiang zhao s->chn[i].clock = s->frequency / 2;
10765345fdb4SMarc-André Lureau qemu_chr_fe_set_handlers(&s->chn[i].chr, serial_can_receive,
107781517ba3SAnton Nefedov serial_receive1, serial_event, NULL,
107839ab61c6SMarc-André Lureau &s->chn[i], NULL, true);
10796c319c82SBlue Swirl }
10808be1f5c8Sbellard }
1081e80cfcfcSbellard
10822cc75c32SLaurent Vivier if (s->chn[0].type == escc_mouse) {
108334acb67fSMark Cave-Ayland s->chn[0].hs = qemu_input_handler_register((DeviceState *)(&s->chn[0]),
108434acb67fSMark Cave-Ayland &sunmouse_handler);
10856c319c82SBlue Swirl }
10862cc75c32SLaurent Vivier if (s->chn[1].type == escc_kbd) {
108765e7545eSGerd Hoffmann s->chn[1].hs = qemu_input_handler_register((DeviceState *)(&s->chn[1]),
108865e7545eSGerd Hoffmann &sunkbd_handler);
10896c319c82SBlue Swirl }
1090e80cfcfcSbellard }
10916c319c82SBlue Swirl
1092312f37d1SRichard Henderson static const Property escc_properties[] = {
10933cf63ff2SPaolo Bonzini DEFINE_PROP_UINT32("frequency", ESCCState, frequency, 0),
10943cf63ff2SPaolo Bonzini DEFINE_PROP_UINT32("it_shift", ESCCState, it_shift, 0),
1095b43047a2SLaurent Vivier DEFINE_PROP_BOOL("bit_swap", ESCCState, bit_swap, false),
10963cf63ff2SPaolo Bonzini DEFINE_PROP_UINT32("disabled", ESCCState, disabled, 0),
10973cf63ff2SPaolo Bonzini DEFINE_PROP_UINT32("chnBtype", ESCCState, chn[0].type, 0),
10983cf63ff2SPaolo Bonzini DEFINE_PROP_UINT32("chnAtype", ESCCState, chn[1].type, 0),
10993cf63ff2SPaolo Bonzini DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr),
11003cf63ff2SPaolo Bonzini DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr),
11016b90a4cdSHenrik Carlqvist DEFINE_PROP_STRING("chnA-sunkbd-layout", ESCCState, chn[1].sunkbd_layout),
1102999e12bbSAnthony Liguori };
1103999e12bbSAnthony Liguori
escc_class_init(ObjectClass * klass,const void * data)1104*12d1a768SPhilippe Mathieu-Daudé static void escc_class_init(ObjectClass *klass, const void *data)
1105999e12bbSAnthony Liguori {
110639bffca2SAnthony Liguori DeviceClass *dc = DEVICE_CLASS(klass);
1107999e12bbSAnthony Liguori
1108e3d08143SPeter Maydell device_class_set_legacy_reset(dc, escc_reset);
1109e7c91369Sxiaoqiang zhao dc->realize = escc_realize;
111039bffca2SAnthony Liguori dc->vmsd = &vmstate_escc;
11114f67d30bSMarc-André Lureau device_class_set_props(dc, escc_properties);
1112f8d4c07cSLaurent Vivier set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
11136c319c82SBlue Swirl }
1114999e12bbSAnthony Liguori
11158c43a6f0SAndreas Färber static const TypeInfo escc_info = {
111681069b20SAndreas Färber .name = TYPE_ESCC,
111739bffca2SAnthony Liguori .parent = TYPE_SYS_BUS_DEVICE,
11183cf63ff2SPaolo Bonzini .instance_size = sizeof(ESCCState),
1119e7c91369Sxiaoqiang zhao .instance_init = escc_init1,
1120999e12bbSAnthony Liguori .class_init = escc_class_init,
11216c319c82SBlue Swirl };
11226c319c82SBlue Swirl
escc_register_types(void)112383f7d43aSAndreas Färber static void escc_register_types(void)
11246c319c82SBlue Swirl {
112539bffca2SAnthony Liguori type_register_static(&escc_info);
11266c319c82SBlue Swirl }
11276c319c82SBlue Swirl
112883f7d43aSAndreas Färber type_init(escc_register_types)
1129