bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 1 | /* |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 2 | * QEMU ESCC (Z8030/Z8530/Z85C30/SCC/ESCC) serial port emulation |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2005 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 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: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 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 |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 24 | |
Peter Maydell | 0430891 | 2016-01-26 18:17:30 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Markus Armbruster | 64552b6 | 2019-08-12 07:23:42 +0200 | [diff] [blame] | 26 | #include "hw/irq.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 27 | #include "hw/qdev-properties.h" |
Eduardo Habkost | ce35e22 | 2020-12-11 17:05:12 -0500 | [diff] [blame] | 28 | #include "hw/qdev-properties-system.h" |
Paolo Bonzini | 83c9f4c | 2013-02-04 15:40:22 +0100 | [diff] [blame] | 29 | #include "hw/sysbus.h" |
Markus Armbruster | d645427 | 2019-08-12 07:23:45 +0200 | [diff] [blame] | 30 | #include "migration/vmstate.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 31 | #include "qemu/module.h" |
Paolo Bonzini | 0d09e41 | 2013-02-05 17:06:20 +0100 | [diff] [blame] | 32 | #include "hw/char/escc.h" |
Paolo Bonzini | 28ecbae | 2012-11-28 12:06:30 +0100 | [diff] [blame] | 33 | #include "ui/console.h" |
Henrik Carlqvist | 6b90a4c | 2023-06-23 20:30:07 +0200 | [diff] [blame] | 34 | |
| 35 | #include "qemu/cutils.h" |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 36 | #include "trace.h" |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 37 | |
| 38 | /* |
Blue Swirl | 09330e9 | 2009-10-24 16:09:01 +0000 | [diff] [blame] | 39 | * Chipset docs: |
| 40 | * "Z80C30/Z85C30/Z80230/Z85230/Z85233 SCC/ESCC User Manual", |
| 41 | * http://www.zilog.com/docs/serial/scc_escc_um.pdf |
| 42 | * |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 43 | * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 44 | * (Slave I/O), also produced as NCR89C105. See |
| 45 | * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 46 | * |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 47 | * The serial ports implement full AMD AM8530 or Zilog Z8530 chips, |
| 48 | * mouse and keyboard ports don't implement all functions and they are |
| 49 | * only asynchronous. There is no DMA. |
| 50 | * |
Laurent Vivier | b43047a | 2019-05-26 00:50:04 +0200 | [diff] [blame] | 51 | * Z85C30 is also used on PowerMacs and m68k Macs. |
| 52 | * |
| 53 | * There are some small differences between Sparc version (sunzilog) |
| 54 | * and PowerMac (pmac): |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 55 | * Offset between control and data registers |
| 56 | * There is some kind of lockup bug, but we can ignore it |
| 57 | * CTS is inverted |
| 58 | * DMA on pmac using DBDMA chip |
| 59 | * pmac can do IRDA and faster rates, sunzilog can only do 38400 |
| 60 | * pmac baud rate generator clock is 3.6864 MHz, sunzilog 4.9152 MHz |
Laurent Vivier | b43047a | 2019-05-26 00:50:04 +0200 | [diff] [blame] | 61 | * |
| 62 | * Linux driver for m68k Macs is the same as for PowerMac (pmac_zilog), |
| 63 | * but registers are grouped by type and not by channel: |
| 64 | * channel is selected by bit 0 of the address (instead of bit 1) |
| 65 | * and register is selected by bit 1 of the address (instead of bit 0). |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 66 | */ |
| 67 | |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 68 | /* |
| 69 | * Modifications: |
| 70 | * 2006-Aug-10 Igor Kovalenko : Renamed KBDQueue to SERIOQueue, implemented |
| 71 | * serial mouse queue. |
| 72 | * Implemented serial mouse protocol. |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 73 | * |
| 74 | * 2010-May-23 Artyom Tarasenko: Reworked IUS logic |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 75 | */ |
| 76 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 77 | #define CHN_C(s) ((s)->chn == escc_chn_b ? 'b' : 'a') |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 78 | |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 79 | #define SERIAL_CTRL 0 |
| 80 | #define SERIAL_DATA 1 |
| 81 | |
| 82 | #define W_CMD 0 |
| 83 | #define CMD_PTR_MASK 0x07 |
| 84 | #define CMD_CMD_MASK 0x38 |
| 85 | #define CMD_HI 0x08 |
| 86 | #define CMD_CLR_TXINT 0x28 |
| 87 | #define CMD_CLR_IUS 0x38 |
| 88 | #define W_INTR 1 |
| 89 | #define INTR_INTALL 0x01 |
| 90 | #define INTR_TXINT 0x02 |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 91 | #define INTR_PAR_SPEC 0x04 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 92 | #define INTR_RXMODEMSK 0x18 |
| 93 | #define INTR_RXINT1ST 0x08 |
| 94 | #define INTR_RXINTALL 0x10 |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 95 | #define INTR_WTRQ_TXRX 0x20 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 96 | #define W_IVEC 2 |
| 97 | #define W_RXCTRL 3 |
| 98 | #define RXCTRL_RXEN 0x01 |
Mark Cave-Ayland | 15a2a1a | 2021-09-03 12:32:23 +0100 | [diff] [blame] | 99 | #define RXCTRL_HUNT 0x10 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 100 | #define W_TXCTRL1 4 |
| 101 | #define TXCTRL1_PAREN 0x01 |
| 102 | #define TXCTRL1_PAREV 0x02 |
| 103 | #define TXCTRL1_1STOP 0x04 |
| 104 | #define TXCTRL1_1HSTOP 0x08 |
| 105 | #define TXCTRL1_2STOP 0x0c |
| 106 | #define TXCTRL1_STPMSK 0x0c |
| 107 | #define TXCTRL1_CLK1X 0x00 |
| 108 | #define TXCTRL1_CLK16X 0x40 |
| 109 | #define TXCTRL1_CLK32X 0x80 |
| 110 | #define TXCTRL1_CLK64X 0xc0 |
| 111 | #define TXCTRL1_CLKMSK 0xc0 |
| 112 | #define W_TXCTRL2 5 |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 113 | #define TXCTRL2_TXCRC 0x01 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 114 | #define TXCTRL2_TXEN 0x08 |
| 115 | #define TXCTRL2_BITMSK 0x60 |
| 116 | #define TXCTRL2_5BITS 0x00 |
| 117 | #define TXCTRL2_7BITS 0x20 |
| 118 | #define TXCTRL2_6BITS 0x40 |
| 119 | #define TXCTRL2_8BITS 0x60 |
| 120 | #define W_SYNC1 6 |
| 121 | #define W_SYNC2 7 |
| 122 | #define W_TXBUF 8 |
| 123 | #define W_MINTR 9 |
Mark Cave-Ayland | 160509a | 2021-09-03 12:32:20 +0100 | [diff] [blame] | 124 | #define MINTR_VIS 0x01 |
| 125 | #define MINTR_NV 0x02 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 126 | #define MINTR_STATUSHI 0x10 |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 127 | #define MINTR_SOFTIACK 0x20 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 128 | #define MINTR_RST_MASK 0xc0 |
| 129 | #define MINTR_RST_B 0x40 |
| 130 | #define MINTR_RST_A 0x80 |
| 131 | #define MINTR_RST_ALL 0xc0 |
| 132 | #define W_MISC1 10 |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 133 | #define MISC1_ENC_MASK 0x60 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 134 | #define W_CLOCK 11 |
| 135 | #define CLOCK_TRXC 0x08 |
| 136 | #define W_BRGLO 12 |
| 137 | #define W_BRGHI 13 |
| 138 | #define W_MISC2 14 |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 139 | #define MISC2_BRG_EN 0x01 |
| 140 | #define MISC2_BRG_SRC 0x02 |
| 141 | #define MISC2_LCL_LOOP 0x10 |
| 142 | #define MISC2_PLLCMD0 0x20 |
| 143 | #define MISC2_PLLCMD1 0x40 |
| 144 | #define MISC2_PLLCMD2 0x80 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 145 | #define W_EXTINT 15 |
| 146 | #define EXTINT_DCD 0x08 |
| 147 | #define EXTINT_SYNCINT 0x10 |
| 148 | #define EXTINT_CTSINT 0x20 |
| 149 | #define EXTINT_TXUNDRN 0x40 |
| 150 | #define EXTINT_BRKINT 0x80 |
| 151 | |
| 152 | #define R_STATUS 0 |
| 153 | #define STATUS_RXAV 0x01 |
| 154 | #define STATUS_ZERO 0x02 |
| 155 | #define STATUS_TXEMPTY 0x04 |
| 156 | #define STATUS_DCD 0x08 |
| 157 | #define STATUS_SYNC 0x10 |
| 158 | #define STATUS_CTS 0x20 |
| 159 | #define STATUS_TXUNDRN 0x40 |
| 160 | #define STATUS_BRK 0x80 |
| 161 | #define R_SPEC 1 |
| 162 | #define SPEC_ALLSENT 0x01 |
| 163 | #define SPEC_BITS8 0x06 |
| 164 | #define R_IVEC 2 |
| 165 | #define IVEC_TXINTB 0x00 |
| 166 | #define IVEC_LONOINT 0x06 |
| 167 | #define IVEC_LORXINTA 0x0c |
| 168 | #define IVEC_LORXINTB 0x04 |
| 169 | #define IVEC_LOTXINTA 0x08 |
| 170 | #define IVEC_HINOINT 0x60 |
| 171 | #define IVEC_HIRXINTA 0x30 |
| 172 | #define IVEC_HIRXINTB 0x20 |
| 173 | #define IVEC_HITXINTA 0x10 |
| 174 | #define R_INTR 3 |
| 175 | #define INTR_EXTINTB 0x01 |
| 176 | #define INTR_TXINTB 0x02 |
| 177 | #define INTR_RXINTB 0x04 |
| 178 | #define INTR_EXTINTA 0x08 |
| 179 | #define INTR_TXINTA 0x10 |
| 180 | #define INTR_RXINTA 0x20 |
| 181 | #define R_IPEN 4 |
| 182 | #define R_TXCTRL1 5 |
| 183 | #define R_TXCTRL2 6 |
| 184 | #define R_BC 7 |
| 185 | #define R_RXBUF 8 |
| 186 | #define R_RXCTRL 9 |
| 187 | #define R_MISC 10 |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 188 | #define MISC_2CLKMISS 0x40 |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 189 | #define R_MISC1 11 |
| 190 | #define R_BRGLO 12 |
| 191 | #define R_BRGHI 13 |
| 192 | #define R_MISC1I 14 |
| 193 | #define R_EXTINT 15 |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 194 | |
Henrik Carlqvist | 6b90a4c | 2023-06-23 20:30:07 +0200 | [diff] [blame] | 195 | static uint8_t sunkbd_layout_dip_switch(const char *sunkbd_layout); |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 196 | static void handle_kbd_command(ESCCChannelState *s, int val); |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 197 | static int serial_can_receive(void *opaque); |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 198 | static void serial_receive_byte(ESCCChannelState *s, int ch); |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 199 | |
Laurent Vivier | b43047a | 2019-05-26 00:50:04 +0200 | [diff] [blame] | 200 | static int reg_shift(ESCCState *s) |
| 201 | { |
| 202 | return s->bit_swap ? s->it_shift + 1 : s->it_shift; |
| 203 | } |
| 204 | |
| 205 | static int chn_shift(ESCCState *s) |
| 206 | { |
| 207 | return s->bit_swap ? s->it_shift : s->it_shift + 1; |
| 208 | } |
| 209 | |
blueswir1 | 67deb56 | 2007-04-18 19:21:38 +0000 | [diff] [blame] | 210 | static void clear_queue(void *opaque) |
| 211 | { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 212 | ESCCChannelState *s = opaque; |
| 213 | ESCCSERIOQueue *q = &s->queue; |
blueswir1 | 67deb56 | 2007-04-18 19:21:38 +0000 | [diff] [blame] | 214 | q->rptr = q->wptr = q->count = 0; |
| 215 | } |
| 216 | |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 217 | static void put_queue(void *opaque, int b) |
| 218 | { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 219 | ESCCChannelState *s = opaque; |
| 220 | ESCCSERIOQueue *q = &s->queue; |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 221 | |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 222 | trace_escc_put_queue(CHN_C(s), b); |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 223 | if (q->count >= ESCC_SERIO_QUEUE_SIZE) { |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 224 | return; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 225 | } |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 226 | q->data[q->wptr] = b; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 227 | if (++q->wptr == ESCC_SERIO_QUEUE_SIZE) { |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 228 | q->wptr = 0; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 229 | } |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 230 | q->count++; |
| 231 | serial_receive_byte(s, 0); |
| 232 | } |
| 233 | |
| 234 | static uint32_t get_queue(void *opaque) |
| 235 | { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 236 | ESCCChannelState *s = opaque; |
| 237 | ESCCSERIOQueue *q = &s->queue; |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 238 | int val; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 239 | |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 240 | if (q->count == 0) { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 241 | return 0; |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 242 | } else { |
| 243 | val = q->data[q->rptr]; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 244 | if (++q->rptr == ESCC_SERIO_QUEUE_SIZE) { |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 245 | q->rptr = 0; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 246 | } |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 247 | q->count--; |
| 248 | } |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 249 | trace_escc_get_queue(CHN_C(s), val); |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 250 | if (q->count > 0) { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 251 | serial_receive_byte(s, 0); |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 252 | } |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 253 | return val; |
| 254 | } |
| 255 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 256 | static int escc_update_irq_chn(ESCCChannelState *s) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 257 | { |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 258 | if ((((s->wregs[W_INTR] & INTR_TXINT) && (s->txint == 1)) || |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 259 | /* tx ints enabled, pending */ |
| 260 | ((((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINT1ST) || |
| 261 | ((s->wregs[W_INTR] & INTR_RXMODEMSK) == INTR_RXINTALL)) && |
| 262 | s->rxint == 1) || |
| 263 | /* rx ints enabled, pending */ |
| 264 | ((s->wregs[W_EXTINT] & EXTINT_BRKINT) && |
| 265 | (s->rregs[R_STATUS] & STATUS_BRK)))) { |
| 266 | /* break int e&p */ |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 267 | return 1; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 268 | } |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 269 | return 0; |
| 270 | } |
| 271 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 272 | static void escc_update_irq(ESCCChannelState *s) |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 273 | { |
| 274 | int irq; |
| 275 | |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 276 | irq = escc_update_irq_chn(s); |
| 277 | irq |= escc_update_irq_chn(s->otherchn); |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 278 | |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 279 | trace_escc_update_irq(irq); |
pbrook | d537cf6 | 2007-04-07 18:14:41 +0000 | [diff] [blame] | 280 | qemu_set_irq(s->irq, irq); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 281 | } |
| 282 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 283 | static void escc_reset_chn(ESCCChannelState *s) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 284 | { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 285 | s->reg = 0; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 286 | s->rx = s->tx = 0; |
| 287 | s->rxint = s->txint = 0; |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 288 | s->rxint_under_svc = s->txint_under_svc = 0; |
blueswir1 | bbbb2f0 | 2007-09-23 11:48:47 +0000 | [diff] [blame] | 289 | s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0; |
blueswir1 | 67deb56 | 2007-04-18 19:21:38 +0000 | [diff] [blame] | 290 | clear_queue(s); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Mark Cave-Ayland | 8e8aa96 | 2021-09-03 12:32:17 +0100 | [diff] [blame] | 293 | static void escc_soft_reset_chn(ESCCChannelState *s) |
| 294 | { |
Mark Cave-Ayland | 99b0f05 | 2021-09-03 12:32:22 +0100 | [diff] [blame] | 295 | escc_reset_chn(s); |
| 296 | |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 297 | s->wregs[W_CMD] = 0; |
| 298 | s->wregs[W_INTR] &= INTR_PAR_SPEC | INTR_WTRQ_TXRX; |
| 299 | s->wregs[W_RXCTRL] &= ~RXCTRL_RXEN; |
| 300 | /* 1 stop bit */ |
| 301 | s->wregs[W_TXCTRL1] |= TXCTRL1_1STOP; |
| 302 | s->wregs[W_TXCTRL2] &= TXCTRL2_TXCRC | TXCTRL2_8BITS; |
| 303 | s->wregs[W_MINTR] &= ~MINTR_SOFTIACK; |
| 304 | s->wregs[W_MISC1] &= MISC1_ENC_MASK; |
Mark Cave-Ayland | 8e8aa96 | 2021-09-03 12:32:17 +0100 | [diff] [blame] | 305 | /* PLL disabled */ |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 306 | s->wregs[W_MISC2] &= MISC2_BRG_EN | MISC2_BRG_SRC | |
| 307 | MISC2_PLLCMD1 | MISC2_PLLCMD2; |
| 308 | s->wregs[W_MISC2] |= MISC2_PLLCMD0; |
Mark Cave-Ayland | 8e8aa96 | 2021-09-03 12:32:17 +0100 | [diff] [blame] | 309 | /* Enable most interrupts */ |
| 310 | s->wregs[W_EXTINT] = EXTINT_DCD | EXTINT_SYNCINT | EXTINT_CTSINT | |
| 311 | EXTINT_TXUNDRN | EXTINT_BRKINT; |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 312 | |
| 313 | s->rregs[R_STATUS] &= STATUS_DCD | STATUS_SYNC | STATUS_CTS | STATUS_BRK; |
| 314 | s->rregs[R_STATUS] |= STATUS_TXEMPTY | STATUS_TXUNDRN; |
Mark Cave-Ayland | 8e8aa96 | 2021-09-03 12:32:17 +0100 | [diff] [blame] | 315 | if (s->disabled) { |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 316 | s->rregs[R_STATUS] |= STATUS_DCD | STATUS_SYNC | STATUS_CTS; |
Mark Cave-Ayland | 8e8aa96 | 2021-09-03 12:32:17 +0100 | [diff] [blame] | 317 | } |
Mark Cave-Ayland | 1f476e7 | 2021-09-03 12:32:19 +0100 | [diff] [blame] | 318 | s->rregs[R_SPEC] &= SPEC_ALLSENT; |
| 319 | s->rregs[R_SPEC] |= SPEC_BITS8; |
| 320 | s->rregs[R_INTR] = 0; |
| 321 | s->rregs[R_MISC] &= MISC_2CLKMISS; |
Mark Cave-Ayland | 8e8aa96 | 2021-09-03 12:32:17 +0100 | [diff] [blame] | 322 | } |
| 323 | |
Mark Cave-Ayland | bf4fbb6 | 2021-09-03 12:32:18 +0100 | [diff] [blame] | 324 | static void escc_hard_reset_chn(ESCCChannelState *s) |
| 325 | { |
Mark Cave-Ayland | 160509a | 2021-09-03 12:32:20 +0100 | [diff] [blame] | 326 | escc_soft_reset_chn(s); |
Mark Cave-Ayland | bf4fbb6 | 2021-09-03 12:32:18 +0100 | [diff] [blame] | 327 | |
Mark Cave-Ayland | 160509a | 2021-09-03 12:32:20 +0100 | [diff] [blame] | 328 | /* |
| 329 | * Hard reset is almost identical to soft reset above, except that the |
| 330 | * values of WR9 (W_MINTR), WR10 (W_MISC1), WR11 (W_CLOCK) and WR14 |
| 331 | * (W_MISC2) have extra bits forced to 0/1 |
| 332 | */ |
| 333 | s->wregs[W_MINTR] &= MINTR_VIS | MINTR_NV; |
| 334 | s->wregs[W_MINTR] |= MINTR_RST_B | MINTR_RST_A; |
| 335 | s->wregs[W_MISC1] = 0; |
Mark Cave-Ayland | bf4fbb6 | 2021-09-03 12:32:18 +0100 | [diff] [blame] | 336 | s->wregs[W_CLOCK] = CLOCK_TRXC; |
Mark Cave-Ayland | 160509a | 2021-09-03 12:32:20 +0100 | [diff] [blame] | 337 | s->wregs[W_MISC2] &= MISC2_PLLCMD1 | MISC2_PLLCMD2; |
| 338 | s->wregs[W_MISC2] |= MISC2_LCL_LOOP | MISC2_PLLCMD0; |
Mark Cave-Ayland | bf4fbb6 | 2021-09-03 12:32:18 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 341 | static void escc_reset(DeviceState *d) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 342 | { |
Andreas Färber | 81069b2 | 2013-07-24 21:30:40 +0200 | [diff] [blame] | 343 | ESCCState *s = ESCC(d); |
Mark Cave-Ayland | 9d248a4 | 2021-09-03 12:32:16 +0100 | [diff] [blame] | 344 | int i, j; |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 345 | |
Mark Cave-Ayland | 9d248a4 | 2021-09-03 12:32:16 +0100 | [diff] [blame] | 346 | for (i = 0; i < 2; i++) { |
| 347 | ESCCChannelState *cs = &s->chn[i]; |
| 348 | |
| 349 | /* |
| 350 | * According to the ESCC datasheet "Miscellaneous Questions" section |
| 351 | * on page 384, the values of the ESCC registers are not guaranteed on |
| 352 | * power-on until an explicit hardware or software reset has been |
| 353 | * issued. For now we zero the registers so that a device reset always |
| 354 | * returns the emulated device to a fixed state. |
| 355 | */ |
| 356 | for (j = 0; j < ESCC_SERIAL_REGS; j++) { |
| 357 | cs->rregs[j] = 0; |
| 358 | cs->wregs[j] = 0; |
| 359 | } |
Mark Cave-Ayland | c29cd47 | 2021-11-18 18:18:34 +0000 | [diff] [blame] | 360 | |
| 361 | /* |
| 362 | * ...but there is an exception. The "Transmit Interrupts and Transmit |
| 363 | * Buffer Empty Bit" section on page 50 of the ESCC datasheet says of |
| 364 | * the STATUS_TXEMPTY bit in R_STATUS: "After a hardware reset |
| 365 | * (including a hardware reset by software), or a channel reset, this |
| 366 | * bit is set to 1". The Sun PROM checks this bit early on startup and |
| 367 | * gets stuck in an infinite loop if it is not set. |
| 368 | */ |
| 369 | cs->rregs[R_STATUS] |= STATUS_TXEMPTY; |
| 370 | |
Mark Cave-Ayland | 9d248a4 | 2021-09-03 12:32:16 +0100 | [diff] [blame] | 371 | escc_reset_chn(cs); |
| 372 | } |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 375 | static inline void set_rxint(ESCCChannelState *s) |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 376 | { |
| 377 | s->rxint = 1; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 378 | /* |
| 379 | * XXX: missing daisy chaining: escc_chn_b rx should have a lower priority |
| 380 | * than chn_a rx/tx/special_condition service |
| 381 | */ |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 382 | s->rxint_under_svc = 1; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 383 | if (s->chn == escc_chn_a) { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 384 | s->rregs[R_INTR] |= INTR_RXINTA; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 385 | if (s->wregs[W_MINTR] & MINTR_STATUSHI) { |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 386 | s->otherchn->rregs[R_IVEC] = IVEC_HIRXINTA; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 387 | } else { |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 388 | s->otherchn->rregs[R_IVEC] = IVEC_LORXINTA; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 389 | } |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 390 | } else { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 391 | s->otherchn->rregs[R_INTR] |= INTR_RXINTB; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 392 | if (s->wregs[W_MINTR] & MINTR_STATUSHI) { |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 393 | s->rregs[R_IVEC] = IVEC_HIRXINTB; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 394 | } else { |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 395 | s->rregs[R_IVEC] = IVEC_LORXINTB; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 396 | } |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 397 | } |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 398 | escc_update_irq(s); |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 401 | static inline void set_txint(ESCCChannelState *s) |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 402 | { |
| 403 | s->txint = 1; |
| 404 | if (!s->rxint_under_svc) { |
| 405 | s->txint_under_svc = 1; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 406 | if (s->chn == escc_chn_a) { |
Aurelien Jarno | f53671c | 2011-01-27 08:21:35 +0100 | [diff] [blame] | 407 | if (s->wregs[W_INTR] & INTR_TXINT) { |
| 408 | s->rregs[R_INTR] |= INTR_TXINTA; |
| 409 | } |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 410 | if (s->wregs[W_MINTR] & MINTR_STATUSHI) { |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 411 | s->otherchn->rregs[R_IVEC] = IVEC_HITXINTA; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 412 | } else { |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 413 | s->otherchn->rregs[R_IVEC] = IVEC_LOTXINTA; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 414 | } |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 415 | } else { |
| 416 | s->rregs[R_IVEC] = IVEC_TXINTB; |
Aurelien Jarno | f53671c | 2011-01-27 08:21:35 +0100 | [diff] [blame] | 417 | if (s->wregs[W_INTR] & INTR_TXINT) { |
| 418 | s->otherchn->rregs[R_INTR] |= INTR_TXINTB; |
| 419 | } |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 420 | } |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 421 | escc_update_irq(s); |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 422 | } |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 425 | static inline void clr_rxint(ESCCChannelState *s) |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 426 | { |
| 427 | s->rxint = 0; |
| 428 | s->rxint_under_svc = 0; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 429 | if (s->chn == escc_chn_a) { |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 430 | if (s->wregs[W_MINTR] & MINTR_STATUSHI) { |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 431 | s->otherchn->rregs[R_IVEC] = IVEC_HINOINT; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 432 | } else { |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 433 | s->otherchn->rregs[R_IVEC] = IVEC_LONOINT; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 434 | } |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 435 | s->rregs[R_INTR] &= ~INTR_RXINTA; |
| 436 | } else { |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 437 | if (s->wregs[W_MINTR] & MINTR_STATUSHI) { |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 438 | s->rregs[R_IVEC] = IVEC_HINOINT; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 439 | } else { |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 440 | s->rregs[R_IVEC] = IVEC_LONOINT; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 441 | } |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 442 | s->otherchn->rregs[R_INTR] &= ~INTR_RXINTB; |
| 443 | } |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 444 | if (s->txint) { |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 445 | set_txint(s); |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 446 | } |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 447 | escc_update_irq(s); |
blueswir1 | 80637a6 | 2008-01-17 21:07:04 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 450 | static inline void clr_txint(ESCCChannelState *s) |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 451 | { |
| 452 | s->txint = 0; |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 453 | s->txint_under_svc = 0; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 454 | if (s->chn == escc_chn_a) { |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 455 | if (s->wregs[W_MINTR] & MINTR_STATUSHI) { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 456 | s->otherchn->rregs[R_IVEC] = IVEC_HINOINT; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 457 | } else { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 458 | s->otherchn->rregs[R_IVEC] = IVEC_LONOINT; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 459 | } |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 460 | s->rregs[R_INTR] &= ~INTR_TXINTA; |
blueswir1 | b9652ca | 2007-04-20 19:35:25 +0000 | [diff] [blame] | 461 | } else { |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 462 | s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 463 | if (s->wregs[W_MINTR] & MINTR_STATUSHI) { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 464 | s->rregs[R_IVEC] = IVEC_HINOINT; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 465 | } else { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 466 | s->rregs[R_IVEC] = IVEC_LONOINT; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 467 | } |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 468 | s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB; |
blueswir1 | b9652ca | 2007-04-20 19:35:25 +0000 | [diff] [blame] | 469 | } |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 470 | if (s->rxint) { |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 471 | set_rxint(s); |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 472 | } |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 473 | escc_update_irq(s); |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 476 | static void escc_update_parameters(ESCCChannelState *s) |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 477 | { |
| 478 | int speed, parity, data_bits, stop_bits; |
| 479 | QEMUSerialSetParams ssp; |
| 480 | |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 481 | if (!qemu_chr_fe_backend_connected(&s->chr) || s->type != escc_serial) { |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 482 | return; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 483 | } |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 484 | |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 485 | if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREN) { |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 486 | if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREV) { |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 487 | parity = 'E'; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 488 | } else { |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 489 | parity = 'O'; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 490 | } |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 491 | } else { |
| 492 | parity = 'N'; |
| 493 | } |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 494 | if ((s->wregs[W_TXCTRL1] & TXCTRL1_STPMSK) == TXCTRL1_2STOP) { |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 495 | stop_bits = 2; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 496 | } else { |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 497 | stop_bits = 1; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 498 | } |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 499 | switch (s->wregs[W_TXCTRL2] & TXCTRL2_BITMSK) { |
| 500 | case TXCTRL2_5BITS: |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 501 | data_bits = 5; |
| 502 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 503 | case TXCTRL2_7BITS: |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 504 | data_bits = 7; |
| 505 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 506 | case TXCTRL2_6BITS: |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 507 | data_bits = 6; |
| 508 | break; |
| 509 | default: |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 510 | case TXCTRL2_8BITS: |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 511 | data_bits = 8; |
| 512 | break; |
| 513 | } |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 514 | speed = s->clock / ((s->wregs[W_BRGLO] | (s->wregs[W_BRGHI] << 8)) + 2); |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 515 | switch (s->wregs[W_TXCTRL1] & TXCTRL1_CLKMSK) { |
| 516 | case TXCTRL1_CLK1X: |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 517 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 518 | case TXCTRL1_CLK16X: |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 519 | speed /= 16; |
| 520 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 521 | case TXCTRL1_CLK32X: |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 522 | speed /= 32; |
| 523 | break; |
| 524 | default: |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 525 | case TXCTRL1_CLK64X: |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 526 | speed /= 64; |
| 527 | break; |
| 528 | } |
| 529 | ssp.speed = speed; |
| 530 | ssp.parity = parity; |
| 531 | ssp.data_bits = data_bits; |
| 532 | ssp.stop_bits = stop_bits; |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 533 | trace_escc_update_parameters(CHN_C(s), speed, parity, data_bits, stop_bits); |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 534 | qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp); |
bellard | 35db099 | 2006-09-09 12:17:15 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 537 | static void escc_mem_write(void *opaque, hwaddr addr, |
Avi Kivity | 23c5e4c | 2011-08-08 16:09:17 +0300 | [diff] [blame] | 538 | uint64_t val, unsigned size) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 539 | { |
Paolo Bonzini | 3cf63ff | 2013-06-25 15:02:38 +0200 | [diff] [blame] | 540 | ESCCState *serial = opaque; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 541 | ESCCChannelState *s; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 542 | uint32_t saddr; |
| 543 | int newreg, channel; |
| 544 | |
| 545 | val &= 0xff; |
Laurent Vivier | b43047a | 2019-05-26 00:50:04 +0200 | [diff] [blame] | 546 | saddr = (addr >> reg_shift(serial)) & 1; |
| 547 | channel = (addr >> chn_shift(serial)) & 1; |
blueswir1 | b3ceef2 | 2007-06-25 19:56:13 +0000 | [diff] [blame] | 548 | s = &serial->chn[channel]; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 549 | switch (saddr) { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 550 | case SERIAL_CTRL: |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 551 | trace_escc_mem_writeb_ctrl(CHN_C(s), s->reg, val & 0xff); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 552 | newreg = 0; |
| 553 | switch (s->reg) { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 554 | case W_CMD: |
| 555 | newreg = val & CMD_PTR_MASK; |
| 556 | val &= CMD_CMD_MASK; |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 557 | switch (val) { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 558 | case CMD_HI: |
| 559 | newreg |= CMD_HI; |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 560 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 561 | case CMD_CLR_TXINT: |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 562 | clr_txint(s); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 563 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 564 | case CMD_CLR_IUS: |
Artyom Tarasenko | 9fc391f | 2010-08-15 16:04:41 +0200 | [diff] [blame] | 565 | if (s->rxint_under_svc) { |
| 566 | s->rxint_under_svc = 0; |
| 567 | if (s->txint) { |
| 568 | set_txint(s); |
| 569 | } |
| 570 | } else if (s->txint_under_svc) { |
| 571 | s->txint_under_svc = 0; |
| 572 | } |
| 573 | escc_update_irq(s); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 574 | break; |
| 575 | default: |
| 576 | break; |
| 577 | } |
| 578 | break; |
Mark Cave-Ayland | 15a2a1a | 2021-09-03 12:32:23 +0100 | [diff] [blame] | 579 | case W_RXCTRL: |
| 580 | s->wregs[s->reg] = val; |
| 581 | if (val & RXCTRL_HUNT) { |
| 582 | s->rregs[R_STATUS] |= STATUS_SYNC; |
| 583 | } |
| 584 | break; |
| 585 | case W_INTR ... W_IVEC: |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 586 | case W_SYNC1 ... W_TXBUF: |
| 587 | case W_MISC1 ... W_CLOCK: |
| 588 | case W_MISC2 ... W_EXTINT: |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 589 | s->wregs[s->reg] = val; |
| 590 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 591 | case W_TXCTRL1: |
Mark Cave-Ayland | 319e89c | 2021-11-18 18:18:35 +0000 | [diff] [blame] | 592 | s->wregs[s->reg] = val; |
| 593 | /* |
| 594 | * The ESCC datasheet states that SPEC_ALLSENT is always set in |
| 595 | * sync mode, and set in async mode when all characters have |
| 596 | * cleared the transmitter. Since writes to SERIAL_DATA use the |
| 597 | * blocking qemu_chr_fe_write_all() function to write each |
| 598 | * character, the guest can never see the state when async data |
| 599 | * is in the process of being transmitted so we can set this bit |
| 600 | * unconditionally regardless of the state of the W_TXCTRL1 mode |
| 601 | * bits. |
| 602 | */ |
| 603 | s->rregs[R_SPEC] |= SPEC_ALLSENT; |
| 604 | escc_update_parameters(s); |
| 605 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 606 | case W_TXCTRL2: |
blueswir1 | 796d828 | 2008-04-12 08:47:27 +0000 | [diff] [blame] | 607 | s->wregs[s->reg] = val; |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 608 | escc_update_parameters(s); |
blueswir1 | 796d828 | 2008-04-12 08:47:27 +0000 | [diff] [blame] | 609 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 610 | case W_BRGLO: |
| 611 | case W_BRGHI: |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 612 | s->wregs[s->reg] = val; |
blueswir1 | 796d828 | 2008-04-12 08:47:27 +0000 | [diff] [blame] | 613 | s->rregs[s->reg] = val; |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 614 | escc_update_parameters(s); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 615 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 616 | case W_MINTR: |
| 617 | switch (val & MINTR_RST_MASK) { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 618 | case 0: |
| 619 | default: |
| 620 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 621 | case MINTR_RST_B: |
Mark Cave-Ayland | 8e8aa96 | 2021-09-03 12:32:17 +0100 | [diff] [blame] | 622 | trace_escc_soft_reset_chn(CHN_C(&serial->chn[0])); |
| 623 | escc_soft_reset_chn(&serial->chn[0]); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 624 | return; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 625 | case MINTR_RST_A: |
Mark Cave-Ayland | 8e8aa96 | 2021-09-03 12:32:17 +0100 | [diff] [blame] | 626 | trace_escc_soft_reset_chn(CHN_C(&serial->chn[1])); |
| 627 | escc_soft_reset_chn(&serial->chn[1]); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 628 | return; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 629 | case MINTR_RST_ALL: |
Mark Cave-Ayland | bf4fbb6 | 2021-09-03 12:32:18 +0100 | [diff] [blame] | 630 | trace_escc_hard_reset(); |
| 631 | escc_hard_reset_chn(&serial->chn[0]); |
| 632 | escc_hard_reset_chn(&serial->chn[1]); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 633 | return; |
| 634 | } |
| 635 | break; |
| 636 | default: |
| 637 | break; |
| 638 | } |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 639 | if (s->reg == 0) { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 640 | s->reg = newreg; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 641 | } else { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 642 | s->reg = 0; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 643 | } |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 644 | break; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 645 | case SERIAL_DATA: |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 646 | trace_escc_mem_writeb_data(CHN_C(s), val); |
Stephen Checkoway | 6b99a11 | 2019-04-19 11:40:41 -0400 | [diff] [blame] | 647 | /* |
| 648 | * Lower the irq when data is written to the Tx buffer and no other |
| 649 | * interrupts are currently pending. The irq will be raised again once |
| 650 | * the Tx buffer becomes empty below. |
| 651 | */ |
| 652 | s->txint = 0; |
| 653 | escc_update_irq(s); |
blueswir1 | 96c4f56 | 2007-08-11 07:54:26 +0000 | [diff] [blame] | 654 | s->tx = val; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 655 | if (s->wregs[W_TXCTRL2] & TXCTRL2_TXEN) { /* tx enabled */ |
Thomas Huth | 02388b5 | 2023-07-16 17:35:19 +0200 | [diff] [blame] | 656 | if (s->wregs[W_MISC2] & MISC2_LCL_LOOP) { |
| 657 | serial_receive_byte(s, s->tx); |
| 658 | } else if (qemu_chr_fe_backend_connected(&s->chr)) { |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 659 | /* |
| 660 | * XXX this blocks entire thread. Rewrite to use |
| 661 | * qemu_chr_fe_write and background I/O callbacks |
| 662 | */ |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 663 | qemu_chr_fe_write_all(&s->chr, &s->tx, 1); |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 664 | } else if (s->type == escc_kbd && !s->disabled) { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 665 | handle_kbd_command(s, val); |
| 666 | } |
| 667 | } |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 668 | s->rregs[R_STATUS] |= STATUS_TXEMPTY; /* Tx buffer empty */ |
| 669 | s->rregs[R_SPEC] |= SPEC_ALLSENT; /* All sent */ |
blueswir1 | 96c4f56 | 2007-08-11 07:54:26 +0000 | [diff] [blame] | 670 | set_txint(s); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 671 | break; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 672 | default: |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 673 | break; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 677 | static uint64_t escc_mem_read(void *opaque, hwaddr addr, |
Avi Kivity | 23c5e4c | 2011-08-08 16:09:17 +0300 | [diff] [blame] | 678 | unsigned size) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 679 | { |
Paolo Bonzini | 3cf63ff | 2013-06-25 15:02:38 +0200 | [diff] [blame] | 680 | ESCCState *serial = opaque; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 681 | ESCCChannelState *s; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 682 | uint32_t saddr; |
| 683 | uint32_t ret; |
| 684 | int channel; |
| 685 | |
Laurent Vivier | b43047a | 2019-05-26 00:50:04 +0200 | [diff] [blame] | 686 | saddr = (addr >> reg_shift(serial)) & 1; |
| 687 | channel = (addr >> chn_shift(serial)) & 1; |
blueswir1 | b3ceef2 | 2007-06-25 19:56:13 +0000 | [diff] [blame] | 688 | s = &serial->chn[channel]; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 689 | switch (saddr) { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 690 | case SERIAL_CTRL: |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 691 | trace_escc_mem_readb_ctrl(CHN_C(s), s->reg, s->rregs[s->reg]); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 692 | ret = s->rregs[s->reg]; |
| 693 | s->reg = 0; |
| 694 | return ret; |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 695 | case SERIAL_DATA: |
| 696 | s->rregs[R_STATUS] &= ~STATUS_RXAV; |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 697 | clr_rxint(s); |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 698 | if (s->type == escc_kbd || s->type == escc_mouse) { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 699 | ret = get_queue(s); |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 700 | } else { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 701 | ret = s->rx; |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 702 | } |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 703 | trace_escc_mem_readb_data(CHN_C(s), ret); |
Marc-André Lureau | fa394ed | 2016-10-22 12:52:59 +0300 | [diff] [blame] | 704 | qemu_chr_fe_accept_input(&s->chr); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 705 | return ret; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 706 | default: |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 707 | break; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 708 | } |
| 709 | return 0; |
| 710 | } |
| 711 | |
Avi Kivity | 23c5e4c | 2011-08-08 16:09:17 +0300 | [diff] [blame] | 712 | static const MemoryRegionOps escc_mem_ops = { |
| 713 | .read = escc_mem_read, |
| 714 | .write = escc_mem_write, |
| 715 | .endianness = DEVICE_NATIVE_ENDIAN, |
| 716 | .valid = { |
| 717 | .min_access_size = 1, |
| 718 | .max_access_size = 1, |
| 719 | }, |
| 720 | }; |
| 721 | |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 722 | static int serial_can_receive(void *opaque) |
| 723 | { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 724 | ESCCChannelState *s = opaque; |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 725 | int ret; |
| 726 | |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 727 | if (((s->wregs[W_RXCTRL] & RXCTRL_RXEN) == 0) /* Rx not enabled */ |
| 728 | || ((s->rregs[R_STATUS] & STATUS_RXAV) == STATUS_RXAV)) { |
| 729 | /* char already available */ |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 730 | ret = 0; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 731 | } else { |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 732 | ret = 1; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 733 | } |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 734 | return ret; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 737 | static void serial_receive_byte(ESCCChannelState *s, int ch) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 738 | { |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 739 | trace_escc_serial_receive_byte(CHN_C(s), ch); |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 740 | s->rregs[R_STATUS] |= STATUS_RXAV; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 741 | s->rx = ch; |
bellard | ba3c64f | 2005-12-05 20:31:52 +0000 | [diff] [blame] | 742 | set_rxint(s); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 745 | static void serial_receive_break(ESCCChannelState *s) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 746 | { |
blueswir1 | 12abac8 | 2007-12-10 20:05:09 +0000 | [diff] [blame] | 747 | s->rregs[R_STATUS] |= STATUS_BRK; |
blueswir1 | b4ed08e | 2009-01-12 17:38:28 +0000 | [diff] [blame] | 748 | escc_update_irq(s); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | static void serial_receive1(void *opaque, const uint8_t *buf, int size) |
| 752 | { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 753 | ESCCChannelState *s = opaque; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 754 | serial_receive_byte(s, buf[0]); |
| 755 | } |
| 756 | |
Philippe Mathieu-Daudé | 083b266 | 2019-12-18 18:20:09 +0100 | [diff] [blame] | 757 | static void serial_event(void *opaque, QEMUChrEvent event) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 758 | { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 759 | ESCCChannelState *s = opaque; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 760 | if (event == CHR_EVENT_BREAK) { |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 761 | serial_receive_break(s); |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 762 | } |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 765 | static const VMStateDescription vmstate_escc_chn = { |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 766 | .name = "escc_chn", |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 767 | .version_id = 2, |
| 768 | .minimum_version_id = 1, |
Richard Henderson | 2f6cab0 | 2023-12-21 14:16:06 +1100 | [diff] [blame] | 769 | .fields = (const VMStateField[]) { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 770 | VMSTATE_UINT32(vmstate_dummy, ESCCChannelState), |
| 771 | VMSTATE_UINT32(reg, ESCCChannelState), |
| 772 | VMSTATE_UINT32(rxint, ESCCChannelState), |
| 773 | VMSTATE_UINT32(txint, ESCCChannelState), |
| 774 | VMSTATE_UINT32(rxint_under_svc, ESCCChannelState), |
| 775 | VMSTATE_UINT32(txint_under_svc, ESCCChannelState), |
| 776 | VMSTATE_UINT8(rx, ESCCChannelState), |
| 777 | VMSTATE_UINT8(tx, ESCCChannelState), |
| 778 | VMSTATE_BUFFER(wregs, ESCCChannelState), |
| 779 | VMSTATE_BUFFER(rregs, ESCCChannelState), |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 780 | VMSTATE_END_OF_LIST() |
bellard | e4a8905 | 2006-09-09 11:38:11 +0000 | [diff] [blame] | 781 | } |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 782 | }; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 783 | |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 784 | static const VMStateDescription vmstate_escc = { |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 785 | .name = "escc", |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 786 | .version_id = 2, |
| 787 | .minimum_version_id = 1, |
Richard Henderson | 2f6cab0 | 2023-12-21 14:16:06 +1100 | [diff] [blame] | 788 | .fields = (const VMStateField[]) { |
Paolo Bonzini | 3cf63ff | 2013-06-25 15:02:38 +0200 | [diff] [blame] | 789 | VMSTATE_STRUCT_ARRAY(chn, ESCCState, 2, 2, vmstate_escc_chn, |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 790 | ESCCChannelState), |
Blue Swirl | bdb78ca | 2009-10-24 16:07:10 +0000 | [diff] [blame] | 791 | VMSTATE_END_OF_LIST() |
| 792 | } |
| 793 | }; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 794 | |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 795 | static void sunkbd_handle_event(DeviceState *dev, QemuConsole *src, |
| 796 | InputEvent *evt) |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 797 | { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 798 | ESCCChannelState *s = (ESCCChannelState *)dev; |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 799 | int qcode, keycode; |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 800 | InputKeyEvent *key; |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 801 | |
Eric Blake | 568c73a | 2015-10-26 16:34:58 -0600 | [diff] [blame] | 802 | assert(evt->type == INPUT_EVENT_KIND_KEY); |
Eric Blake | 32bafa8 | 2016-03-17 16:48:37 -0600 | [diff] [blame] | 803 | key = evt->u.key.data; |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 804 | qcode = qemu_input_key_value_to_qcode(key->key); |
Markus Armbruster | 977c736 | 2017-08-24 10:46:08 +0200 | [diff] [blame] | 805 | trace_escc_sunkbd_event_in(qcode, QKeyCode_str(qcode), |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 806 | key->down); |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 807 | |
| 808 | if (qcode == Q_KEY_CODE_CAPS_LOCK) { |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 809 | if (key->down) { |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 810 | s->caps_lock_mode ^= 1; |
| 811 | if (s->caps_lock_mode == 2) { |
| 812 | return; /* Drop second press */ |
| 813 | } |
| 814 | } else { |
| 815 | s->caps_lock_mode ^= 2; |
| 816 | if (s->caps_lock_mode == 3) { |
| 817 | return; /* Drop first release */ |
| 818 | } |
| 819 | } |
blueswir1 | 43febf4 | 2007-09-21 19:09:35 +0000 | [diff] [blame] | 820 | } |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 821 | |
| 822 | if (qcode == Q_KEY_CODE_NUM_LOCK) { |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 823 | if (key->down) { |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 824 | s->num_lock_mode ^= 1; |
| 825 | if (s->num_lock_mode == 2) { |
| 826 | return; /* Drop second press */ |
| 827 | } |
| 828 | } else { |
| 829 | s->num_lock_mode ^= 2; |
| 830 | if (s->num_lock_mode == 3) { |
| 831 | return; /* Drop first release */ |
| 832 | } |
| 833 | } |
blueswir1 | 43febf4 | 2007-09-21 19:09:35 +0000 | [diff] [blame] | 834 | } |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 835 | |
Daniel P. Berrangé | 9aaf11e | 2022-03-31 13:24:14 +0100 | [diff] [blame] | 836 | if (qcode >= qemu_input_map_qcode_to_sun_len) { |
Daniel P. Berrange | e709a61 | 2018-01-17 16:41:16 +0000 | [diff] [blame] | 837 | return; |
| 838 | } |
| 839 | |
| 840 | keycode = qemu_input_map_qcode_to_sun[qcode]; |
Eric Blake | b5a1b44 | 2016-03-03 09:16:49 -0700 | [diff] [blame] | 841 | if (!key->down) { |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 842 | keycode |= 0x80; |
| 843 | } |
| 844 | trace_escc_sunkbd_event_out(keycode); |
| 845 | put_queue(s, keycode); |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Philippe Mathieu-Daudé | b1be65f | 2023-10-17 15:05:00 +0200 | [diff] [blame] | 848 | static const QemuInputHandler sunkbd_handler = { |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 849 | .name = "sun keyboard", |
| 850 | .mask = INPUT_EVENT_MASK_KEY, |
| 851 | .event = sunkbd_handle_event, |
| 852 | }; |
| 853 | |
Henrik Carlqvist | 6b90a4c | 2023-06-23 20:30:07 +0200 | [diff] [blame] | 854 | static uint8_t sunkbd_layout_dip_switch(const char *kbd_layout) |
| 855 | { |
| 856 | /* Return the value of the dip-switches in a SUN Type 5 keyboard */ |
| 857 | static uint8_t ret = 0xff; |
| 858 | |
| 859 | if ((ret == 0xff) && kbd_layout) { |
| 860 | int i; |
| 861 | struct layout_values { |
| 862 | const char *lang; |
| 863 | uint8_t dip; |
| 864 | } languages[] = |
| 865 | /* |
| 866 | * Dip values from table 3-16 Layouts for Type 4, 5 and 5c Keyboards |
| 867 | */ |
| 868 | { |
| 869 | {"en-us", 0x21}, /* U.S.A. (US5.kt) */ |
| 870 | /* 0x22 is some other US (US_UNIX5.kt) */ |
| 871 | {"fr", 0x23}, /* France (France5.kt) */ |
| 872 | {"da", 0x24}, /* Denmark (Denmark5.kt) */ |
| 873 | {"de", 0x25}, /* Germany (Germany5.kt) */ |
| 874 | {"it", 0x26}, /* Italy (Italy5.kt) */ |
| 875 | {"nl", 0x27}, /* The Netherlands (Netherland5.kt) */ |
| 876 | {"no", 0x28}, /* Norway (Norway.kt) */ |
| 877 | {"pt", 0x29}, /* Portugal (Portugal5.kt) */ |
| 878 | {"es", 0x2a}, /* Spain (Spain5.kt) */ |
| 879 | {"sv", 0x2b}, /* Sweden (Sweden5.kt) */ |
| 880 | {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */ |
| 881 | {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */ |
| 882 | {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */ |
| 883 | {"ko", 0x2f}, /* Korea (Korea5.kt) */ |
| 884 | {"tw", 0x30}, /* Taiwan (Taiwan5.kt) */ |
| 885 | {"ja", 0x31}, /* Japan (Japan5.kt) */ |
| 886 | {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */ |
| 887 | {"hu", 0x33}, /* Hungary (Hungary5.kt) */ |
| 888 | {"pl", 0x34}, /* Poland (Poland5.kt) */ |
| 889 | {"cz", 0x35}, /* Czech (Czech5.kt) */ |
| 890 | {"ru", 0x36}, /* Russia (Russia5.kt) */ |
| 891 | {"lv", 0x37}, /* Latvia (Latvia5.kt) */ |
| 892 | {"tr", 0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */ |
| 893 | {"gr", 0x39}, /* Greece (Greece5.kt) */ |
| 894 | {"ar", 0x3a}, /* Arabic (Arabic5.kt) */ |
| 895 | {"lt", 0x3b}, /* Lithuania (Lithuania5.kt) */ |
| 896 | {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */ |
| 897 | {"be", 0x3c}, /* Belgium (Belgian5.kt) */ |
| 898 | }; |
| 899 | |
| 900 | for (i = 0; |
| 901 | i < sizeof(languages) / sizeof(struct layout_values); |
| 902 | i++) { |
| 903 | if (!strcmp(kbd_layout, languages[i].lang)) { |
| 904 | ret = languages[i].dip; |
| 905 | return ret; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /* Found no known language code */ |
| 910 | if ((kbd_layout[0] >= '0') && (kbd_layout[0] <= '9')) { |
| 911 | unsigned int tmp; |
| 912 | |
| 913 | /* As a fallback we also accept numeric dip switch value */ |
| 914 | if (!qemu_strtoui(kbd_layout, NULL, 0, &tmp)) { |
| 915 | ret = tmp; |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | if (ret == 0xff) { |
| 921 | /* Final fallback if keyboard_layout was not set or recognized */ |
| 922 | ret = 0x21; /* en-us layout */ |
| 923 | } |
| 924 | return ret; |
| 925 | } |
| 926 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 927 | static void handle_kbd_command(ESCCChannelState *s, int val) |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 928 | { |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 929 | trace_escc_kbd_command(val); |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 930 | if (s->led_mode) { /* Ignore led byte */ |
blueswir1 | 43febf4 | 2007-09-21 19:09:35 +0000 | [diff] [blame] | 931 | s->led_mode = 0; |
| 932 | return; |
| 933 | } |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 934 | switch (val) { |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 935 | case 1: /* Reset, return type code */ |
blueswir1 | 67deb56 | 2007-04-18 19:21:38 +0000 | [diff] [blame] | 936 | clear_queue(s); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 937 | put_queue(s, 0xff); |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 938 | put_queue(s, 4); /* Type 4 */ |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 939 | put_queue(s, 0x7f); |
| 940 | break; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 941 | case 0xe: /* Set leds */ |
blueswir1 | 43febf4 | 2007-09-21 19:09:35 +0000 | [diff] [blame] | 942 | s->led_mode = 1; |
| 943 | break; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 944 | case 7: /* Query layout */ |
blueswir1 | 67deb56 | 2007-04-18 19:21:38 +0000 | [diff] [blame] | 945 | case 0xf: |
| 946 | clear_queue(s); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 947 | put_queue(s, 0xfe); |
Henrik Carlqvist | 6b90a4c | 2023-06-23 20:30:07 +0200 | [diff] [blame] | 948 | put_queue(s, sunkbd_layout_dip_switch(s->sunkbd_layout)); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 949 | break; |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 950 | default: |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 951 | break; |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 952 | } |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 953 | } |
| 954 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 955 | static void sunmouse_event(void *opaque, |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 956 | int dx, int dy, int dz, int buttons_state) |
| 957 | { |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 958 | ESCCChannelState *s = opaque; |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 959 | int ch; |
| 960 | |
Blue Swirl | 30c2f23 | 2011-08-07 11:01:05 +0000 | [diff] [blame] | 961 | trace_escc_sunmouse_event(dx, dy, buttons_state); |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 962 | ch = 0x80 | 0x7; /* protocol start byte, no buttons pressed */ |
| 963 | |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 964 | if (buttons_state & MOUSE_EVENT_LBUTTON) { |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 965 | ch ^= 0x4; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 966 | } |
| 967 | if (buttons_state & MOUSE_EVENT_MBUTTON) { |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 968 | ch ^= 0x2; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 969 | } |
| 970 | if (buttons_state & MOUSE_EVENT_RBUTTON) { |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 971 | ch ^= 0x1; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 972 | } |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 973 | |
| 974 | put_queue(s, ch); |
| 975 | |
| 976 | ch = dx; |
| 977 | |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 978 | if (ch > 127) { |
Michael S. Tsirkin | a0d98a7 | 2009-09-30 19:43:55 +0200 | [diff] [blame] | 979 | ch = 127; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 980 | } else if (ch < -127) { |
Michael S. Tsirkin | a0d98a7 | 2009-09-30 19:43:55 +0200 | [diff] [blame] | 981 | ch = -127; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 982 | } |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 983 | |
| 984 | put_queue(s, ch & 0xff); |
| 985 | |
| 986 | ch = -dy; |
| 987 | |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 988 | if (ch > 127) { |
Michael S. Tsirkin | 084bd07 | 2009-09-30 18:56:44 +0000 | [diff] [blame] | 989 | ch = 127; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 990 | } else if (ch < -127) { |
Michael S. Tsirkin | 084bd07 | 2009-09-30 18:56:44 +0000 | [diff] [blame] | 991 | ch = -127; |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 992 | } |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 993 | |
| 994 | put_queue(s, ch & 0xff); |
| 995 | |
Mark Cave-Ayland | 0e04202 | 2021-09-03 12:32:15 +0100 | [diff] [blame] | 996 | /* MSC protocol specifies two extra motion bytes */ |
bellard | 715748f | 2006-09-09 11:35:47 +0000 | [diff] [blame] | 997 | |
| 998 | put_queue(s, 0); |
| 999 | put_queue(s, 0); |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
xiaoqiang zhao | e7c91369 | 2016-05-25 14:39:00 +0800 | [diff] [blame] | 1002 | static void escc_init1(Object *obj) |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1003 | { |
xiaoqiang zhao | e7c91369 | 2016-05-25 14:39:00 +0800 | [diff] [blame] | 1004 | ESCCState *s = ESCC(obj); |
| 1005 | SysBusDevice *dev = SYS_BUS_DEVICE(obj); |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1006 | unsigned int i; |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1007 | |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 1008 | for (i = 0; i < 2; i++) { |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1009 | sysbus_init_irq(dev, &s->chn[i].irq); |
blueswir1 | f930d07 | 2007-10-06 11:28:21 +0000 | [diff] [blame] | 1010 | s->chn[i].chn = 1 - i; |
xiaoqiang zhao | e7c91369 | 2016-05-25 14:39:00 +0800 | [diff] [blame] | 1011 | } |
| 1012 | s->chn[0].otherchn = &s->chn[1]; |
| 1013 | s->chn[1].otherchn = &s->chn[0]; |
| 1014 | |
xiaoqiang zhao | e7c91369 | 2016-05-25 14:39:00 +0800 | [diff] [blame] | 1015 | sysbus_init_mmio(dev, &s->mmio); |
| 1016 | } |
| 1017 | |
| 1018 | static void escc_realize(DeviceState *dev, Error **errp) |
| 1019 | { |
| 1020 | ESCCState *s = ESCC(dev); |
| 1021 | unsigned int i; |
| 1022 | |
xiaoqiang zhao | 4b3eec9 | 2016-06-01 15:58:18 +0800 | [diff] [blame] | 1023 | s->chn[0].disabled = s->disabled; |
| 1024 | s->chn[1].disabled = s->disabled; |
| 1025 | |
| 1026 | memory_region_init_io(&s->mmio, OBJECT(dev), &escc_mem_ops, s, "escc", |
| 1027 | ESCC_SIZE << s->it_shift); |
| 1028 | |
xiaoqiang zhao | e7c91369 | 2016-05-25 14:39:00 +0800 | [diff] [blame] | 1029 | for (i = 0; i < 2; i++) { |
Anton Nefedov | 3065070 | 2017-07-06 15:08:52 +0300 | [diff] [blame] | 1030 | if (qemu_chr_fe_backend_connected(&s->chn[i].chr)) { |
xiaoqiang zhao | 4b3eec9 | 2016-06-01 15:58:18 +0800 | [diff] [blame] | 1031 | s->chn[i].clock = s->frequency / 2; |
Marc-André Lureau | 5345fdb | 2016-10-22 12:52:55 +0300 | [diff] [blame] | 1032 | qemu_chr_fe_set_handlers(&s->chn[i].chr, serial_can_receive, |
Anton Nefedov | 81517ba | 2017-07-06 15:08:49 +0300 | [diff] [blame] | 1033 | serial_receive1, serial_event, NULL, |
Marc-André Lureau | 39ab61c | 2016-10-22 12:53:03 +0300 | [diff] [blame] | 1034 | &s->chn[i], NULL, true); |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1035 | } |
bellard | 8be1f5c | 2005-04-06 20:42:35 +0000 | [diff] [blame] | 1036 | } |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 1037 | |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 1038 | if (s->chn[0].type == escc_mouse) { |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1039 | qemu_add_mouse_event_handler(sunmouse_event, &s->chn[0], 0, |
| 1040 | "QEMU Sun Mouse"); |
| 1041 | } |
Laurent Vivier | 2cc75c3 | 2018-02-14 07:35:58 +0100 | [diff] [blame] | 1042 | if (s->chn[1].type == escc_kbd) { |
Gerd Hoffmann | 65e7545 | 2014-03-25 13:16:21 +0100 | [diff] [blame] | 1043 | s->chn[1].hs = qemu_input_handler_register((DeviceState *)(&s->chn[1]), |
| 1044 | &sunkbd_handler); |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1045 | } |
bellard | e80cfcf | 2004-12-19 23:18:01 +0000 | [diff] [blame] | 1046 | } |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1047 | |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 1048 | static Property escc_properties[] = { |
Paolo Bonzini | 3cf63ff | 2013-06-25 15:02:38 +0200 | [diff] [blame] | 1049 | DEFINE_PROP_UINT32("frequency", ESCCState, frequency, 0), |
| 1050 | DEFINE_PROP_UINT32("it_shift", ESCCState, it_shift, 0), |
Laurent Vivier | b43047a | 2019-05-26 00:50:04 +0200 | [diff] [blame] | 1051 | DEFINE_PROP_BOOL("bit_swap", ESCCState, bit_swap, false), |
Paolo Bonzini | 3cf63ff | 2013-06-25 15:02:38 +0200 | [diff] [blame] | 1052 | DEFINE_PROP_UINT32("disabled", ESCCState, disabled, 0), |
| 1053 | DEFINE_PROP_UINT32("chnBtype", ESCCState, chn[0].type, 0), |
| 1054 | DEFINE_PROP_UINT32("chnAtype", ESCCState, chn[1].type, 0), |
| 1055 | DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr), |
| 1056 | DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr), |
Henrik Carlqvist | 6b90a4c | 2023-06-23 20:30:07 +0200 | [diff] [blame] | 1057 | DEFINE_PROP_STRING("chnA-sunkbd-layout", ESCCState, chn[1].sunkbd_layout), |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 1058 | DEFINE_PROP_END_OF_LIST(), |
| 1059 | }; |
| 1060 | |
| 1061 | static void escc_class_init(ObjectClass *klass, void *data) |
| 1062 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 1063 | DeviceClass *dc = DEVICE_CLASS(klass); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 1064 | |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 1065 | dc->reset = escc_reset; |
xiaoqiang zhao | e7c91369 | 2016-05-25 14:39:00 +0800 | [diff] [blame] | 1066 | dc->realize = escc_realize; |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 1067 | dc->vmsd = &vmstate_escc; |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 1068 | device_class_set_props(dc, escc_properties); |
Laurent Vivier | f8d4c07 | 2015-09-26 18:22:05 +0200 | [diff] [blame] | 1069 | set_bit(DEVICE_CATEGORY_INPUT, dc->categories); |
Anthony Liguori | 999e12b | 2012-01-24 13:12:29 -0600 | [diff] [blame] | 1070 | } |
| 1071 | |
Andreas Färber | 8c43a6f | 2013-01-10 16:19:07 +0100 | [diff] [blame] | 1072 | static const TypeInfo escc_info = { |
Andreas Färber | 81069b2 | 2013-07-24 21:30:40 +0200 | [diff] [blame] | 1073 | .name = TYPE_ESCC, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 1074 | .parent = TYPE_SYS_BUS_DEVICE, |
Paolo Bonzini | 3cf63ff | 2013-06-25 15:02:38 +0200 | [diff] [blame] | 1075 | .instance_size = sizeof(ESCCState), |
xiaoqiang zhao | e7c91369 | 2016-05-25 14:39:00 +0800 | [diff] [blame] | 1076 | .instance_init = escc_init1, |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 1077 | .class_init = escc_class_init, |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1078 | }; |
| 1079 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 1080 | static void escc_register_types(void) |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1081 | { |
Anthony Liguori | 39bffca | 2011-12-07 21:34:16 -0600 | [diff] [blame] | 1082 | type_register_static(&escc_info); |
Blue Swirl | 6c319c8 | 2009-07-15 08:51:32 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Andreas Färber | 83f7d43 | 2012-02-09 15:20:55 +0100 | [diff] [blame] | 1085 | type_init(escc_register_types) |