1 /* 2 * QEMU ESCC (Z8030/Z8530/Z85C30/SCC/ESCC) serial port emulation 3 * 4 * Copyright (c) 2003-2005 Fabrice Bellard 5 * 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 */ 24 25 #include "qemu/osdep.h" 26 #include "hw/irq.h" 27 #include "hw/qdev-properties.h" 28 #include "hw/qdev-properties-system.h" 29 #include "hw/sysbus.h" 30 #include "migration/vmstate.h" 31 #include "qemu/module.h" 32 #include "hw/char/escc.h" 33 #include "ui/console.h" 34 35 #include "qemu/cutils.h" 36 #include "trace.h" 37 38 /* 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 * 43 * On Sparc32 this is the serial port, mouse and keyboard part of chip STP2001 44 * (Slave I/O), also produced as NCR89C105. See 45 * http://www.ibiblio.org/pub/historic-linux/early-ports/Sparc/NCR/NCR89C105.txt 46 * 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 * 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): 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 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). 66 */ 67 68 /* 69 * Modifications: 70 * 2006-Aug-10 Igor Kovalenko : Renamed KBDQueue to SERIOQueue, implemented 71 * serial mouse queue. 72 * Implemented serial mouse protocol. 73 * 74 * 2010-May-23 Artyom Tarasenko: Reworked IUS logic 75 */ 76 77 #define CHN_C(s) ((s)->chn == escc_chn_b ? 'b' : 'a') 78 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 91 #define INTR_PAR_SPEC 0x04 92 #define INTR_RXMODEMSK 0x18 93 #define INTR_RXINT1ST 0x08 94 #define INTR_RXINTALL 0x10 95 #define INTR_WTRQ_TXRX 0x20 96 #define W_IVEC 2 97 #define W_RXCTRL 3 98 #define RXCTRL_RXEN 0x01 99 #define RXCTRL_HUNT 0x10 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 113 #define TXCTRL2_TXCRC 0x01 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 124 #define MINTR_VIS 0x01 125 #define MINTR_NV 0x02 126 #define MINTR_STATUSHI 0x10 127 #define MINTR_SOFTIACK 0x20 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 133 #define MISC1_ENC_MASK 0x60 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 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 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 188 #define MISC_2CLKMISS 0x40 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 194 195 static uint8_t sunkbd_layout_dip_switch(const char *sunkbd_layout); 196 static void handle_kbd_command(ESCCChannelState *s, int val); 197 static int serial_can_receive(void *opaque); 198 static void serial_receive_byte(ESCCChannelState *s, int ch); 199 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 210 static void clear_queue(void *opaque) 211 { 212 ESCCChannelState *s = opaque; 213 ESCCSERIOQueue *q = &s->queue; 214 q->rptr = q->wptr = q->count = 0; 215 } 216 217 static void put_queue(void *opaque, int b) 218 { 219 ESCCChannelState *s = opaque; 220 ESCCSERIOQueue *q = &s->queue; 221 222 trace_escc_put_queue(CHN_C(s), b); 223 if (q->count >= ESCC_SERIO_QUEUE_SIZE) { 224 return; 225 } 226 q->data[q->wptr] = b; 227 if (++q->wptr == ESCC_SERIO_QUEUE_SIZE) { 228 q->wptr = 0; 229 } 230 q->count++; 231 serial_receive_byte(s, 0); 232 } 233 234 static uint32_t get_queue(void *opaque) 235 { 236 ESCCChannelState *s = opaque; 237 ESCCSERIOQueue *q = &s->queue; 238 int val; 239 240 if (q->count == 0) { 241 return 0; 242 } else { 243 val = q->data[q->rptr]; 244 if (++q->rptr == ESCC_SERIO_QUEUE_SIZE) { 245 q->rptr = 0; 246 } 247 q->count--; 248 } 249 trace_escc_get_queue(CHN_C(s), val); 250 if (q->count > 0) { 251 serial_receive_byte(s, 0); 252 } 253 return val; 254 } 255 256 static int escc_update_irq_chn(ESCCChannelState *s) 257 { 258 if ((((s->wregs[W_INTR] & INTR_TXINT) && (s->txint == 1)) || 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 */ 267 return 1; 268 } 269 return 0; 270 } 271 272 static void escc_update_irq(ESCCChannelState *s) 273 { 274 int irq; 275 276 irq = escc_update_irq_chn(s); 277 irq |= escc_update_irq_chn(s->otherchn); 278 279 trace_escc_update_irq(irq); 280 qemu_set_irq(s->irq, irq); 281 } 282 283 static void escc_reset_chn(ESCCChannelState *s) 284 { 285 s->reg = 0; 286 s->rx = s->tx = 0; 287 s->rxint = s->txint = 0; 288 s->rxint_under_svc = s->txint_under_svc = 0; 289 s->e0_mode = s->led_mode = s->caps_lock_mode = s->num_lock_mode = 0; 290 clear_queue(s); 291 } 292 293 static void escc_soft_reset_chn(ESCCChannelState *s) 294 { 295 escc_reset_chn(s); 296 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; 305 /* PLL disabled */ 306 s->wregs[W_MISC2] &= MISC2_BRG_EN | MISC2_BRG_SRC | 307 MISC2_PLLCMD1 | MISC2_PLLCMD2; 308 s->wregs[W_MISC2] |= MISC2_PLLCMD0; 309 /* Enable most interrupts */ 310 s->wregs[W_EXTINT] = EXTINT_DCD | EXTINT_SYNCINT | EXTINT_CTSINT | 311 EXTINT_TXUNDRN | EXTINT_BRKINT; 312 313 s->rregs[R_STATUS] &= STATUS_DCD | STATUS_SYNC | STATUS_CTS | STATUS_BRK; 314 s->rregs[R_STATUS] |= STATUS_TXEMPTY | STATUS_TXUNDRN; 315 if (s->disabled) { 316 s->rregs[R_STATUS] |= STATUS_DCD | STATUS_SYNC | STATUS_CTS; 317 } 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; 322 } 323 324 static void escc_hard_reset_chn(ESCCChannelState *s) 325 { 326 escc_soft_reset_chn(s); 327 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; 336 s->wregs[W_CLOCK] = CLOCK_TRXC; 337 s->wregs[W_MISC2] &= MISC2_PLLCMD1 | MISC2_PLLCMD2; 338 s->wregs[W_MISC2] |= MISC2_LCL_LOOP | MISC2_PLLCMD0; 339 } 340 341 static void escc_reset(DeviceState *d) 342 { 343 ESCCState *s = ESCC(d); 344 int i, j; 345 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 } 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 371 escc_reset_chn(cs); 372 } 373 } 374 375 static inline void set_rxint(ESCCChannelState *s) 376 { 377 s->rxint = 1; 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 */ 382 s->rxint_under_svc = 1; 383 if (s->chn == escc_chn_a) { 384 s->rregs[R_INTR] |= INTR_RXINTA; 385 if (s->wregs[W_MINTR] & MINTR_STATUSHI) { 386 s->otherchn->rregs[R_IVEC] = IVEC_HIRXINTA; 387 } else { 388 s->otherchn->rregs[R_IVEC] = IVEC_LORXINTA; 389 } 390 } else { 391 s->otherchn->rregs[R_INTR] |= INTR_RXINTB; 392 if (s->wregs[W_MINTR] & MINTR_STATUSHI) { 393 s->rregs[R_IVEC] = IVEC_HIRXINTB; 394 } else { 395 s->rregs[R_IVEC] = IVEC_LORXINTB; 396 } 397 } 398 escc_update_irq(s); 399 } 400 401 static inline void set_txint(ESCCChannelState *s) 402 { 403 s->txint = 1; 404 if (!s->rxint_under_svc) { 405 s->txint_under_svc = 1; 406 if (s->chn == escc_chn_a) { 407 if (s->wregs[W_INTR] & INTR_TXINT) { 408 s->rregs[R_INTR] |= INTR_TXINTA; 409 } 410 if (s->wregs[W_MINTR] & MINTR_STATUSHI) { 411 s->otherchn->rregs[R_IVEC] = IVEC_HITXINTA; 412 } else { 413 s->otherchn->rregs[R_IVEC] = IVEC_LOTXINTA; 414 } 415 } else { 416 s->rregs[R_IVEC] = IVEC_TXINTB; 417 if (s->wregs[W_INTR] & INTR_TXINT) { 418 s->otherchn->rregs[R_INTR] |= INTR_TXINTB; 419 } 420 } 421 escc_update_irq(s); 422 } 423 } 424 425 static inline void clr_rxint(ESCCChannelState *s) 426 { 427 s->rxint = 0; 428 s->rxint_under_svc = 0; 429 if (s->chn == escc_chn_a) { 430 if (s->wregs[W_MINTR] & MINTR_STATUSHI) { 431 s->otherchn->rregs[R_IVEC] = IVEC_HINOINT; 432 } else { 433 s->otherchn->rregs[R_IVEC] = IVEC_LONOINT; 434 } 435 s->rregs[R_INTR] &= ~INTR_RXINTA; 436 } else { 437 if (s->wregs[W_MINTR] & MINTR_STATUSHI) { 438 s->rregs[R_IVEC] = IVEC_HINOINT; 439 } else { 440 s->rregs[R_IVEC] = IVEC_LONOINT; 441 } 442 s->otherchn->rregs[R_INTR] &= ~INTR_RXINTB; 443 } 444 if (s->txint) { 445 set_txint(s); 446 } 447 escc_update_irq(s); 448 } 449 450 static inline void clr_txint(ESCCChannelState *s) 451 { 452 s->txint = 0; 453 s->txint_under_svc = 0; 454 if (s->chn == escc_chn_a) { 455 if (s->wregs[W_MINTR] & MINTR_STATUSHI) { 456 s->otherchn->rregs[R_IVEC] = IVEC_HINOINT; 457 } else { 458 s->otherchn->rregs[R_IVEC] = IVEC_LONOINT; 459 } 460 s->rregs[R_INTR] &= ~INTR_TXINTA; 461 } else { 462 s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB; 463 if (s->wregs[W_MINTR] & MINTR_STATUSHI) { 464 s->rregs[R_IVEC] = IVEC_HINOINT; 465 } else { 466 s->rregs[R_IVEC] = IVEC_LONOINT; 467 } 468 s->otherchn->rregs[R_INTR] &= ~INTR_TXINTB; 469 } 470 if (s->rxint) { 471 set_rxint(s); 472 } 473 escc_update_irq(s); 474 } 475 476 static void escc_update_parameters(ESCCChannelState *s) 477 { 478 int speed, parity, data_bits, stop_bits; 479 QEMUSerialSetParams ssp; 480 481 if (!qemu_chr_fe_backend_connected(&s->chr) || s->type != escc_serial) { 482 return; 483 } 484 485 if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREN) { 486 if (s->wregs[W_TXCTRL1] & TXCTRL1_PAREV) { 487 parity = 'E'; 488 } else { 489 parity = 'O'; 490 } 491 } else { 492 parity = 'N'; 493 } 494 if ((s->wregs[W_TXCTRL1] & TXCTRL1_STPMSK) == TXCTRL1_2STOP) { 495 stop_bits = 2; 496 } else { 497 stop_bits = 1; 498 } 499 switch (s->wregs[W_TXCTRL2] & TXCTRL2_BITMSK) { 500 case TXCTRL2_5BITS: 501 data_bits = 5; 502 break; 503 case TXCTRL2_7BITS: 504 data_bits = 7; 505 break; 506 case TXCTRL2_6BITS: 507 data_bits = 6; 508 break; 509 default: 510 case TXCTRL2_8BITS: 511 data_bits = 8; 512 break; 513 } 514 speed = s->clock / ((s->wregs[W_BRGLO] | (s->wregs[W_BRGHI] << 8)) + 2); 515 switch (s->wregs[W_TXCTRL1] & TXCTRL1_CLKMSK) { 516 case TXCTRL1_CLK1X: 517 break; 518 case TXCTRL1_CLK16X: 519 speed /= 16; 520 break; 521 case TXCTRL1_CLK32X: 522 speed /= 32; 523 break; 524 default: 525 case TXCTRL1_CLK64X: 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; 533 trace_escc_update_parameters(CHN_C(s), speed, parity, data_bits, stop_bits); 534 qemu_chr_fe_ioctl(&s->chr, CHR_IOCTL_SERIAL_SET_PARAMS, &ssp); 535 } 536 537 static void escc_mem_write(void *opaque, hwaddr addr, 538 uint64_t val, unsigned size) 539 { 540 ESCCState *serial = opaque; 541 ESCCChannelState *s; 542 uint32_t saddr; 543 int newreg, channel; 544 545 val &= 0xff; 546 saddr = (addr >> reg_shift(serial)) & 1; 547 channel = (addr >> chn_shift(serial)) & 1; 548 s = &serial->chn[channel]; 549 switch (saddr) { 550 case SERIAL_CTRL: 551 trace_escc_mem_writeb_ctrl(CHN_C(s), s->reg, val & 0xff); 552 newreg = 0; 553 switch (s->reg) { 554 case W_CMD: 555 newreg = val & CMD_PTR_MASK; 556 val &= CMD_CMD_MASK; 557 switch (val) { 558 case CMD_HI: 559 newreg |= CMD_HI; 560 break; 561 case CMD_CLR_TXINT: 562 clr_txint(s); 563 break; 564 case CMD_CLR_IUS: 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); 574 break; 575 default: 576 break; 577 } 578 break; 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: 586 case W_SYNC1 ... W_TXBUF: 587 case W_MISC1 ... W_CLOCK: 588 case W_MISC2 ... W_EXTINT: 589 s->wregs[s->reg] = val; 590 break; 591 case W_TXCTRL1: 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; 606 case W_TXCTRL2: 607 s->wregs[s->reg] = val; 608 escc_update_parameters(s); 609 break; 610 case W_BRGLO: 611 case W_BRGHI: 612 s->wregs[s->reg] = val; 613 s->rregs[s->reg] = val; 614 escc_update_parameters(s); 615 break; 616 case W_MINTR: 617 switch (val & MINTR_RST_MASK) { 618 case 0: 619 default: 620 break; 621 case MINTR_RST_B: 622 trace_escc_soft_reset_chn(CHN_C(&serial->chn[0])); 623 escc_soft_reset_chn(&serial->chn[0]); 624 return; 625 case MINTR_RST_A: 626 trace_escc_soft_reset_chn(CHN_C(&serial->chn[1])); 627 escc_soft_reset_chn(&serial->chn[1]); 628 return; 629 case MINTR_RST_ALL: 630 trace_escc_hard_reset(); 631 escc_hard_reset_chn(&serial->chn[0]); 632 escc_hard_reset_chn(&serial->chn[1]); 633 return; 634 } 635 break; 636 default: 637 break; 638 } 639 if (s->reg == 0) { 640 s->reg = newreg; 641 } else { 642 s->reg = 0; 643 } 644 break; 645 case SERIAL_DATA: 646 trace_escc_mem_writeb_data(CHN_C(s), val); 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); 654 s->tx = val; 655 if (s->wregs[W_TXCTRL2] & TXCTRL2_TXEN) { /* tx enabled */ 656 if (qemu_chr_fe_backend_connected(&s->chr)) { 657 /* 658 * XXX this blocks entire thread. Rewrite to use 659 * qemu_chr_fe_write and background I/O callbacks 660 */ 661 qemu_chr_fe_write_all(&s->chr, &s->tx, 1); 662 } else if (s->type == escc_kbd && !s->disabled) { 663 handle_kbd_command(s, val); 664 } 665 } 666 s->rregs[R_STATUS] |= STATUS_TXEMPTY; /* Tx buffer empty */ 667 s->rregs[R_SPEC] |= SPEC_ALLSENT; /* All sent */ 668 set_txint(s); 669 break; 670 default: 671 break; 672 } 673 } 674 675 static uint64_t escc_mem_read(void *opaque, hwaddr addr, 676 unsigned size) 677 { 678 ESCCState *serial = opaque; 679 ESCCChannelState *s; 680 uint32_t saddr; 681 uint32_t ret; 682 int channel; 683 684 saddr = (addr >> reg_shift(serial)) & 1; 685 channel = (addr >> chn_shift(serial)) & 1; 686 s = &serial->chn[channel]; 687 switch (saddr) { 688 case SERIAL_CTRL: 689 trace_escc_mem_readb_ctrl(CHN_C(s), s->reg, s->rregs[s->reg]); 690 ret = s->rregs[s->reg]; 691 s->reg = 0; 692 return ret; 693 case SERIAL_DATA: 694 s->rregs[R_STATUS] &= ~STATUS_RXAV; 695 clr_rxint(s); 696 if (s->type == escc_kbd || s->type == escc_mouse) { 697 ret = get_queue(s); 698 } else { 699 ret = s->rx; 700 } 701 trace_escc_mem_readb_data(CHN_C(s), ret); 702 qemu_chr_fe_accept_input(&s->chr); 703 return ret; 704 default: 705 break; 706 } 707 return 0; 708 } 709 710 static const MemoryRegionOps escc_mem_ops = { 711 .read = escc_mem_read, 712 .write = escc_mem_write, 713 .endianness = DEVICE_NATIVE_ENDIAN, 714 .valid = { 715 .min_access_size = 1, 716 .max_access_size = 1, 717 }, 718 }; 719 720 static int serial_can_receive(void *opaque) 721 { 722 ESCCChannelState *s = opaque; 723 int ret; 724 725 if (((s->wregs[W_RXCTRL] & RXCTRL_RXEN) == 0) /* Rx not enabled */ 726 || ((s->rregs[R_STATUS] & STATUS_RXAV) == STATUS_RXAV)) { 727 /* char already available */ 728 ret = 0; 729 } else { 730 ret = 1; 731 } 732 return ret; 733 } 734 735 static void serial_receive_byte(ESCCChannelState *s, int ch) 736 { 737 trace_escc_serial_receive_byte(CHN_C(s), ch); 738 s->rregs[R_STATUS] |= STATUS_RXAV; 739 s->rx = ch; 740 set_rxint(s); 741 } 742 743 static void serial_receive_break(ESCCChannelState *s) 744 { 745 s->rregs[R_STATUS] |= STATUS_BRK; 746 escc_update_irq(s); 747 } 748 749 static void serial_receive1(void *opaque, const uint8_t *buf, int size) 750 { 751 ESCCChannelState *s = opaque; 752 serial_receive_byte(s, buf[0]); 753 } 754 755 static void serial_event(void *opaque, QEMUChrEvent event) 756 { 757 ESCCChannelState *s = opaque; 758 if (event == CHR_EVENT_BREAK) { 759 serial_receive_break(s); 760 } 761 } 762 763 static const VMStateDescription vmstate_escc_chn = { 764 .name = "escc_chn", 765 .version_id = 2, 766 .minimum_version_id = 1, 767 .fields = (VMStateField[]) { 768 VMSTATE_UINT32(vmstate_dummy, ESCCChannelState), 769 VMSTATE_UINT32(reg, ESCCChannelState), 770 VMSTATE_UINT32(rxint, ESCCChannelState), 771 VMSTATE_UINT32(txint, ESCCChannelState), 772 VMSTATE_UINT32(rxint_under_svc, ESCCChannelState), 773 VMSTATE_UINT32(txint_under_svc, ESCCChannelState), 774 VMSTATE_UINT8(rx, ESCCChannelState), 775 VMSTATE_UINT8(tx, ESCCChannelState), 776 VMSTATE_BUFFER(wregs, ESCCChannelState), 777 VMSTATE_BUFFER(rregs, ESCCChannelState), 778 VMSTATE_END_OF_LIST() 779 } 780 }; 781 782 static const VMStateDescription vmstate_escc = { 783 .name = "escc", 784 .version_id = 2, 785 .minimum_version_id = 1, 786 .fields = (VMStateField[]) { 787 VMSTATE_STRUCT_ARRAY(chn, ESCCState, 2, 2, vmstate_escc_chn, 788 ESCCChannelState), 789 VMSTATE_END_OF_LIST() 790 } 791 }; 792 793 static void sunkbd_handle_event(DeviceState *dev, QemuConsole *src, 794 InputEvent *evt) 795 { 796 ESCCChannelState *s = (ESCCChannelState *)dev; 797 int qcode, keycode; 798 InputKeyEvent *key; 799 800 assert(evt->type == INPUT_EVENT_KIND_KEY); 801 key = evt->u.key.data; 802 qcode = qemu_input_key_value_to_qcode(key->key); 803 trace_escc_sunkbd_event_in(qcode, QKeyCode_str(qcode), 804 key->down); 805 806 if (qcode == Q_KEY_CODE_CAPS_LOCK) { 807 if (key->down) { 808 s->caps_lock_mode ^= 1; 809 if (s->caps_lock_mode == 2) { 810 return; /* Drop second press */ 811 } 812 } else { 813 s->caps_lock_mode ^= 2; 814 if (s->caps_lock_mode == 3) { 815 return; /* Drop first release */ 816 } 817 } 818 } 819 820 if (qcode == Q_KEY_CODE_NUM_LOCK) { 821 if (key->down) { 822 s->num_lock_mode ^= 1; 823 if (s->num_lock_mode == 2) { 824 return; /* Drop second press */ 825 } 826 } else { 827 s->num_lock_mode ^= 2; 828 if (s->num_lock_mode == 3) { 829 return; /* Drop first release */ 830 } 831 } 832 } 833 834 if (qcode >= qemu_input_map_qcode_to_sun_len) { 835 return; 836 } 837 838 keycode = qemu_input_map_qcode_to_sun[qcode]; 839 if (!key->down) { 840 keycode |= 0x80; 841 } 842 trace_escc_sunkbd_event_out(keycode); 843 put_queue(s, keycode); 844 } 845 846 static QemuInputHandler sunkbd_handler = { 847 .name = "sun keyboard", 848 .mask = INPUT_EVENT_MASK_KEY, 849 .event = sunkbd_handle_event, 850 }; 851 852 static uint8_t sunkbd_layout_dip_switch(const char *kbd_layout) 853 { 854 /* Return the value of the dip-switches in a SUN Type 5 keyboard */ 855 static uint8_t ret = 0xff; 856 857 if ((ret == 0xff) && kbd_layout) { 858 int i; 859 struct layout_values { 860 const char *lang; 861 uint8_t dip; 862 } languages[] = 863 /* 864 * Dip values from table 3-16 Layouts for Type 4, 5 and 5c Keyboards 865 */ 866 { 867 {"en-us", 0x21}, /* U.S.A. (US5.kt) */ 868 /* 0x22 is some other US (US_UNIX5.kt) */ 869 {"fr", 0x23}, /* France (France5.kt) */ 870 {"da", 0x24}, /* Denmark (Denmark5.kt) */ 871 {"de", 0x25}, /* Germany (Germany5.kt) */ 872 {"it", 0x26}, /* Italy (Italy5.kt) */ 873 {"nl", 0x27}, /* The Netherlands (Netherland5.kt) */ 874 {"no", 0x28}, /* Norway (Norway.kt) */ 875 {"pt", 0x29}, /* Portugal (Portugal5.kt) */ 876 {"es", 0x2a}, /* Spain (Spain5.kt) */ 877 {"sv", 0x2b}, /* Sweden (Sweden5.kt) */ 878 {"fr-ch", 0x2c}, /* Switzerland/French (Switzer_Fr5.kt) */ 879 {"de-ch", 0x2d}, /* Switzerland/German (Switzer_Ge5.kt) */ 880 {"en-gb", 0x2e}, /* Great Britain (UK5.kt) */ 881 {"ko", 0x2f}, /* Korea (Korea5.kt) */ 882 {"tw", 0x30}, /* Taiwan (Taiwan5.kt) */ 883 {"ja", 0x31}, /* Japan (Japan5.kt) */ 884 {"fr-ca", 0x32}, /* Canada/French (Canada_Fr5.kt) */ 885 {"hu", 0x33}, /* Hungary (Hungary5.kt) */ 886 {"pl", 0x34}, /* Poland (Poland5.kt) */ 887 {"cz", 0x35}, /* Czech (Czech5.kt) */ 888 {"ru", 0x36}, /* Russia (Russia5.kt) */ 889 {"lv", 0x37}, /* Latvia (Latvia5.kt) */ 890 {"tr", 0x38}, /* Turkey-Q5 (TurkeyQ5.kt) */ 891 {"gr", 0x39}, /* Greece (Greece5.kt) */ 892 {"ar", 0x3a}, /* Arabic (Arabic5.kt) */ 893 {"lt", 0x3b}, /* Lithuania (Lithuania5.kt) */ 894 {"nl-be", 0x3c}, /* Belgium (Belgian5.kt) */ 895 {"be", 0x3c}, /* Belgium (Belgian5.kt) */ 896 }; 897 898 for (i = 0; 899 i < sizeof(languages) / sizeof(struct layout_values); 900 i++) { 901 if (!strcmp(kbd_layout, languages[i].lang)) { 902 ret = languages[i].dip; 903 return ret; 904 } 905 } 906 907 /* Found no known language code */ 908 if ((kbd_layout[0] >= '0') && (kbd_layout[0] <= '9')) { 909 unsigned int tmp; 910 911 /* As a fallback we also accept numeric dip switch value */ 912 if (!qemu_strtoui(kbd_layout, NULL, 0, &tmp)) { 913 ret = tmp; 914 } 915 } 916 } 917 918 if (ret == 0xff) { 919 /* Final fallback if keyboard_layout was not set or recognized */ 920 ret = 0x21; /* en-us layout */ 921 } 922 return ret; 923 } 924 925 static void handle_kbd_command(ESCCChannelState *s, int val) 926 { 927 trace_escc_kbd_command(val); 928 if (s->led_mode) { /* Ignore led byte */ 929 s->led_mode = 0; 930 return; 931 } 932 switch (val) { 933 case 1: /* Reset, return type code */ 934 clear_queue(s); 935 put_queue(s, 0xff); 936 put_queue(s, 4); /* Type 4 */ 937 put_queue(s, 0x7f); 938 break; 939 case 0xe: /* Set leds */ 940 s->led_mode = 1; 941 break; 942 case 7: /* Query layout */ 943 case 0xf: 944 clear_queue(s); 945 put_queue(s, 0xfe); 946 put_queue(s, sunkbd_layout_dip_switch(s->sunkbd_layout)); 947 break; 948 default: 949 break; 950 } 951 } 952 953 static void sunmouse_event(void *opaque, 954 int dx, int dy, int dz, int buttons_state) 955 { 956 ESCCChannelState *s = opaque; 957 int ch; 958 959 trace_escc_sunmouse_event(dx, dy, buttons_state); 960 ch = 0x80 | 0x7; /* protocol start byte, no buttons pressed */ 961 962 if (buttons_state & MOUSE_EVENT_LBUTTON) { 963 ch ^= 0x4; 964 } 965 if (buttons_state & MOUSE_EVENT_MBUTTON) { 966 ch ^= 0x2; 967 } 968 if (buttons_state & MOUSE_EVENT_RBUTTON) { 969 ch ^= 0x1; 970 } 971 972 put_queue(s, ch); 973 974 ch = dx; 975 976 if (ch > 127) { 977 ch = 127; 978 } else if (ch < -127) { 979 ch = -127; 980 } 981 982 put_queue(s, ch & 0xff); 983 984 ch = -dy; 985 986 if (ch > 127) { 987 ch = 127; 988 } else if (ch < -127) { 989 ch = -127; 990 } 991 992 put_queue(s, ch & 0xff); 993 994 /* MSC protocol specifies two extra motion bytes */ 995 996 put_queue(s, 0); 997 put_queue(s, 0); 998 } 999 1000 static void escc_init1(Object *obj) 1001 { 1002 ESCCState *s = ESCC(obj); 1003 SysBusDevice *dev = SYS_BUS_DEVICE(obj); 1004 unsigned int i; 1005 1006 for (i = 0; i < 2; i++) { 1007 sysbus_init_irq(dev, &s->chn[i].irq); 1008 s->chn[i].chn = 1 - i; 1009 } 1010 s->chn[0].otherchn = &s->chn[1]; 1011 s->chn[1].otherchn = &s->chn[0]; 1012 1013 sysbus_init_mmio(dev, &s->mmio); 1014 } 1015 1016 static void escc_realize(DeviceState *dev, Error **errp) 1017 { 1018 ESCCState *s = ESCC(dev); 1019 unsigned int i; 1020 1021 s->chn[0].disabled = s->disabled; 1022 s->chn[1].disabled = s->disabled; 1023 1024 memory_region_init_io(&s->mmio, OBJECT(dev), &escc_mem_ops, s, "escc", 1025 ESCC_SIZE << s->it_shift); 1026 1027 for (i = 0; i < 2; i++) { 1028 if (qemu_chr_fe_backend_connected(&s->chn[i].chr)) { 1029 s->chn[i].clock = s->frequency / 2; 1030 qemu_chr_fe_set_handlers(&s->chn[i].chr, serial_can_receive, 1031 serial_receive1, serial_event, NULL, 1032 &s->chn[i], NULL, true); 1033 } 1034 } 1035 1036 if (s->chn[0].type == escc_mouse) { 1037 qemu_add_mouse_event_handler(sunmouse_event, &s->chn[0], 0, 1038 "QEMU Sun Mouse"); 1039 } 1040 if (s->chn[1].type == escc_kbd) { 1041 s->chn[1].hs = qemu_input_handler_register((DeviceState *)(&s->chn[1]), 1042 &sunkbd_handler); 1043 } 1044 } 1045 1046 static Property escc_properties[] = { 1047 DEFINE_PROP_UINT32("frequency", ESCCState, frequency, 0), 1048 DEFINE_PROP_UINT32("it_shift", ESCCState, it_shift, 0), 1049 DEFINE_PROP_BOOL("bit_swap", ESCCState, bit_swap, false), 1050 DEFINE_PROP_UINT32("disabled", ESCCState, disabled, 0), 1051 DEFINE_PROP_UINT32("chnBtype", ESCCState, chn[0].type, 0), 1052 DEFINE_PROP_UINT32("chnAtype", ESCCState, chn[1].type, 0), 1053 DEFINE_PROP_CHR("chrB", ESCCState, chn[0].chr), 1054 DEFINE_PROP_CHR("chrA", ESCCState, chn[1].chr), 1055 DEFINE_PROP_STRING("chnA-sunkbd-layout", ESCCState, chn[1].sunkbd_layout), 1056 DEFINE_PROP_END_OF_LIST(), 1057 }; 1058 1059 static void escc_class_init(ObjectClass *klass, void *data) 1060 { 1061 DeviceClass *dc = DEVICE_CLASS(klass); 1062 1063 dc->reset = escc_reset; 1064 dc->realize = escc_realize; 1065 dc->vmsd = &vmstate_escc; 1066 device_class_set_props(dc, escc_properties); 1067 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 1068 } 1069 1070 static const TypeInfo escc_info = { 1071 .name = TYPE_ESCC, 1072 .parent = TYPE_SYS_BUS_DEVICE, 1073 .instance_size = sizeof(ESCCState), 1074 .instance_init = escc_init1, 1075 .class_init = escc_class_init, 1076 }; 1077 1078 static void escc_register_types(void) 1079 { 1080 type_register_static(&escc_info); 1081 } 1082 1083 type_init(escc_register_types) 1084