1 /* $NetBSD: zs.c,v 1.31 2026/11/11 03:05:12 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Gordon W. Ross. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Zilog Z8530 Dual UART driver (machine-dependent part) 34 * 35 * Runs two serial lines per chip using slave drivers. 36 * Plain tty/async lines use the zs_async slave. 37 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves. 38 */ 39 40 #include <sys/cdefs.h> 41 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.31 2026/11/11 03:05:12 thorpej Exp $"); 42 43 #include "opt_ddb.h" 44 #include "opt_kgdb.h" 45 46 #include <sys/param.h> 47 #include <sys/systm.h> 48 #include <sys/conf.h> 49 #include <sys/device.h> 50 #include <sys/file.h> 51 #include <sys/ioctl.h> 52 #include <sys/kernel.h> 53 #include <sys/proc.h> 54 #include <sys/tty.h> 55 #include <sys/time.h> 56 #include <sys/syslog.h> 57 #include <sys/intr.h> 58 59 #include <ddb/db_active.h> 60 61 #include <machine/autoconf.h> 62 #include <machine/promlib.h> 63 #include <machine/cpu.h> 64 #include <machine/eeprom.h> 65 #include <machine/psl.h> 66 #include <machine/z8530var.h> 67 68 #include <dev/cons.h> 69 #include <dev/ic/z8530reg.h> 70 #include <dev/sun/kbd_ms_ttyvar.h> 71 72 #include <sun2/dev/cons.h> 73 74 #include "ioconf.h" 75 #include "kbd.h" /* NKBD */ 76 #include "ms.h" /* NMS */ 77 78 /* 79 * Some warts needed by z8530tty.c - 80 * The default parity REALLY needs to be the same as the PROM uses, 81 * or you can not see messages done with printf during boot-up... 82 */ 83 int zs_def_cflag = (CREAD | CS8 | HUPCL); 84 85 /* ZS channel used as the console device (if any) */ 86 void *zs_conschan_get, *zs_conschan_put; 87 88 static uint8_t zs_init_reg[16] = { 89 0, /* 0: CMD (reset, etc.) */ 90 0, /* 1: No interrupts yet. */ 91 #ifdef ZS_INIT_IVECT 92 ZS_INIT_IVECT, /* 2: IVECT */ 93 #else 94 0, /* 2: IVECT */ 95 #endif 96 ZSWR3_RX_8 | ZSWR3_RX_ENABLE, 97 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP, 98 ZSWR5_TX_8 | ZSWR5_TX_ENABLE, 99 0, /* 6: TXSYNC/SYNCLO */ 100 0, /* 7: RXSYNC/SYNCHI */ 101 0, /* 8: alias for data port */ 102 #ifdef ZS_INIT_IVECT 103 ZSWR9_MASTER_IE, 104 #else 105 ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR, 106 #endif 107 0, /*10: Misc. TX/RX control bits */ 108 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD, 109 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */ 110 0, /*13: BAUDHI (default=9600) */ 111 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK, 112 ZSWR15_BREAK_IE, 113 }; 114 115 /* Console ops */ 116 static int zscngetc(dev_t); 117 static void zscnputc(dev_t, int); 118 static void zscnpollc(dev_t, int); 119 120 struct consdev zs_consdev = { 121 NULL, 122 NULL, 123 zscngetc, 124 zscnputc, 125 zscnpollc, 126 NULL, 127 }; 128 129 130 /**************************************************************** 131 * Autoconfig 132 ****************************************************************/ 133 134 static int zs_print(void *, const char *name); 135 136 /* Interrupt handlers. */ 137 int zscheckintr(void *); 138 static int zshard(void *); 139 static void zssoft(void *); 140 141 static int zs_get_speed(struct zs_chanstate *); 142 143 /* 144 * Attach a found zs. 145 * 146 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR 147 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE? 148 */ 149 void 150 zs_attach(struct zsc_softc *zsc, struct zsdevice *zsd, int pri) 151 { 152 struct zsc_attach_args zsc_args; 153 struct zs_chanstate *cs; 154 int channel; 155 156 memset(&zsc_args, 0, sizeof zsc_args); 157 if (zsd == NULL) { 158 aprint_error(": configuration incomplete\n"); 159 return; 160 } 161 162 #if 0 163 /* we should use ipl2si(softpri) but it isn't exported */ 164 aprint_normal(" softpri %d\n", _IPL_SOFT_LEVEL3); 165 #else 166 aprint_normal("\n"); 167 #endif 168 169 /* 170 * Initialize software state for each channel. 171 */ 172 for (channel = 0; channel < 2; channel++) { 173 struct zschan *zc; 174 device_t child; 175 176 zsc_args.channel = channel; 177 zsc_args.hwflags = 0; 178 cs = &zsc->zsc_cs_store[channel]; 179 zsc->zsc_cs[channel] = cs; 180 181 zs_lock_init(cs); 182 cs->cs_channel = channel; 183 cs->cs_private = NULL; 184 cs->cs_ops = &zsops_null; 185 cs->cs_brg_clk = PCLK / 16; 186 187 zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b; 188 189 zsc_args.consdev = NULL; 190 zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit, 191 zsc->zsc_node, 192 channel); 193 194 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) { 195 zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV; 196 zsc_args.consdev = &zs_consdev; 197 } 198 199 if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) { 200 zs_conschan_get = zc; 201 } 202 if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) { 203 zs_conschan_put = zc; 204 } 205 206 /* Children need to set cn_dev, etc */ 207 cs->cs_reg_csr = &zc->zc_csr; 208 cs->cs_reg_data = &zc->zc_data; 209 210 memcpy(cs->cs_creg, zs_init_reg, 16); 211 memcpy(cs->cs_preg, zs_init_reg, 16); 212 213 /* XXX: Consult PROM properties for this?! */ 214 cs->cs_defspeed = zs_get_speed(cs); 215 cs->cs_defcflag = zs_def_cflag; 216 217 /* Make these correspond to cs_defcflag (-crtscts) */ 218 cs->cs_rr0_dcd = ZSRR0_DCD; 219 cs->cs_rr0_cts = 0; 220 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS; 221 cs->cs_wr5_rts = 0; 222 223 /* 224 * Clear the master interrupt enable. 225 * The INTENA is common to both channels, 226 * so just do it on the A channel. 227 */ 228 if (channel == 0) { 229 zs_write_reg(cs, 9, 0); 230 } 231 232 /* 233 * Look for a child driver for this channel. 234 * The child attach will setup the hardware. 235 */ 236 if ((child = config_found(zsc->zsc_dev, (void *)&zsc_args, 237 zs_print, CFARGS_NONE)) == NULL) { 238 /* No sub-driver. Just reset it. */ 239 uint8_t reset = (channel == 0) ? 240 ZSWR9_A_RESET : ZSWR9_B_RESET; 241 zs_lock_chan(cs); 242 zs_write_reg(cs, 9, reset); 243 zs_unlock_chan(cs); 244 } 245 #if (NKBD > 0) || (NMS > 0) 246 /* 247 * If this was a zstty it has a keyboard 248 * property on it we need to attach the 249 * sunkbd and sunms line disciplines. 250 */ 251 if (child 252 && device_is_a(child, "zstty")) { 253 struct kbd_ms_tty_attach_args kma; 254 struct zstty_softc { 255 /* 256 * The following are the only fields 257 * we need here 258 */ 259 device_t zst_dev; 260 struct tty *zst_tty; 261 struct zs_chanstate *zst_cs; 262 } *zst = device_private(child); 263 struct tty *tp; 264 265 kma.kmta_tp = tp = zst->zst_tty; 266 if (tp != NULL) { 267 kma.kmta_dev = tp->t_dev; 268 kma.kmta_consdev = zsc_args.consdev; 269 270 /* Attach 'em if we got 'em. */ 271 switch(zs_peripheral_type(zsc->zsc_promunit, 272 zsc->zsc_node, 273 channel)) { 274 case ZS_PERIPHERAL_SUNKBD: 275 #if (NKBD > 0) 276 kma.kmta_name = "keyboard"; 277 config_found(child, (void *)&kma, NULL, 278 CFARGS_NONE); 279 #endif 280 break; 281 case ZS_PERIPHERAL_SUNMS: 282 #if (NMS > 0) 283 kma.kmta_name = "mouse"; 284 config_found(child, (void *)&kma, NULL, 285 CFARGS_NONE); 286 #endif 287 break; 288 default: 289 break; 290 } 291 } 292 } 293 #endif 294 } 295 296 /* 297 * Now safe to install interrupt handlers. 298 */ 299 bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL, 0, zshard, zsc); 300 if ((zsc->zsc_softintr = softint_establish(SOFTINT_SERIAL, 301 zssoft, zsc)) == NULL) 302 panic("%s: could not establish soft interrupt", __func__); 303 304 evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL, 305 device_xname(zsc->zsc_dev), "intr"); 306 307 308 /* 309 * Set the master interrupt enable and interrupt vector. 310 * (common to both channels, do it on A) 311 */ 312 cs = zsc->zsc_cs[0]; 313 zs_lock_chan(cs); 314 /* interrupt vector */ 315 zs_write_reg(cs, 2, zs_init_reg[2]); 316 /* master interrupt control (enable) */ 317 zs_write_reg(cs, 9, zs_init_reg[9]); 318 zs_unlock_chan(cs); 319 320 } 321 322 static int 323 zs_print(void *aux, const char *name) 324 { 325 struct zsc_attach_args *args = aux; 326 327 if (name != NULL) 328 aprint_normal("%s: ", name); 329 330 if (args->channel != -1) 331 aprint_normal(" channel %d", args->channel); 332 333 return (UNCONF); 334 } 335 336 static int 337 zshard(void *arg) 338 { 339 struct zsc_softc *zsc = arg; 340 int rval; 341 uint8_t rr3; 342 343 rval = 0; 344 while ((rr3 = zsc_intr_hard(zsc))) { 345 /* Count up the interrupts. */ 346 rval |= rr3; 347 zsc->zsc_intrcnt.ev_count++; 348 } 349 if (((zsc->zsc_cs[0] && zsc->zsc_cs[0]->cs_softreq) || 350 (zsc->zsc_cs[1] && zsc->zsc_cs[1]->cs_softreq)) && 351 zsc->zsc_softintr) { 352 softint_schedule(zsc->zsc_softintr); 353 } 354 return (rval); 355 } 356 357 int 358 zscheckintr(void *arg) 359 { 360 struct zsc_softc *zsc; 361 int unit, rval; 362 363 rval = 0; 364 for (unit = 0; unit < zs_cd.cd_ndevs; unit++) { 365 366 zsc = device_lookup_private(&zs_cd, unit); 367 if (zsc == NULL) 368 continue; 369 rval = (zshard((void *)zsc) || rval); 370 } 371 return (rval); 372 } 373 374 375 static void 376 zssoft(void *arg) 377 { 378 struct zsc_softc *zsc = arg; 379 int s; 380 381 /* Make sure we call the tty layer at spltty. */ 382 s = spltty(); 383 (void)zsc_intr_soft(zsc); 384 385 splx(s); 386 } 387 388 389 /* 390 * Compute the current baud rate given a ZS channel. 391 */ 392 static int 393 zs_get_speed(struct zs_chanstate *cs) 394 { 395 int tconst; 396 397 tconst = zs_read_reg(cs, 12); 398 tconst |= zs_read_reg(cs, 13) << 8; 399 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst)); 400 } 401 402 /* 403 * MD functions for setting the baud rate and control modes. 404 */ 405 int 406 zs_set_speed(struct zs_chanstate *cs, int bps) 407 { 408 int tconst, real_bps; 409 410 if (bps == 0) 411 return (0); 412 413 #ifdef DIAGNOSTIC 414 if (cs->cs_brg_clk == 0) 415 panic("zs_set_speed"); 416 #endif 417 418 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps); 419 if (tconst < 0) 420 return (EINVAL); 421 422 /* Convert back to make sure we can do it. */ 423 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst); 424 425 /* XXX - Allow some tolerance here? */ 426 if (real_bps != bps) 427 return (EINVAL); 428 429 cs->cs_preg[12] = tconst; 430 cs->cs_preg[13] = tconst >> 8; 431 432 /* Caller will stuff the pending registers. */ 433 return (0); 434 } 435 436 int 437 zs_set_modes(struct zs_chanstate *cs, int cflag /* bits per second */) 438 { 439 440 /* 441 * Output hardware flow control on the chip is horrendous: 442 * if carrier detect drops, the receiver is disabled, and if 443 * CTS drops, the transmitter is stopped IN MID CHARACTER! 444 * Therefore, NEVER set the HFC bit, and instead use the 445 * status interrupt to detect CTS changes. 446 */ 447 zs_lock_chan(cs); 448 cs->cs_rr0_pps = 0; 449 if ((cflag & (CLOCAL | MDMBUF)) != 0) { 450 cs->cs_rr0_dcd = 0; 451 if ((cflag & MDMBUF) == 0) 452 cs->cs_rr0_pps = ZSRR0_DCD; 453 } else 454 cs->cs_rr0_dcd = ZSRR0_DCD; 455 if ((cflag & CRTSCTS) != 0) { 456 cs->cs_wr5_dtr = ZSWR5_DTR; 457 cs->cs_wr5_rts = ZSWR5_RTS; 458 cs->cs_rr0_cts = ZSRR0_CTS; 459 } else if ((cflag & CDTRCTS) != 0) { 460 cs->cs_wr5_dtr = 0; 461 cs->cs_wr5_rts = ZSWR5_DTR; 462 cs->cs_rr0_cts = ZSRR0_CTS; 463 } else if ((cflag & MDMBUF) != 0) { 464 cs->cs_wr5_dtr = 0; 465 cs->cs_wr5_rts = ZSWR5_DTR; 466 cs->cs_rr0_cts = ZSRR0_DCD; 467 } else { 468 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS; 469 cs->cs_wr5_rts = 0; 470 cs->cs_rr0_cts = 0; 471 } 472 zs_unlock_chan(cs); 473 474 /* Caller will stuff the pending registers. */ 475 return (0); 476 } 477 478 479 /* 480 * Read or write the chip with suitable delays. 481 */ 482 483 uint8_t 484 zs_read_reg(struct zs_chanstate *cs, uint8_t reg) 485 { 486 uint8_t val; 487 488 *cs->cs_reg_csr = reg; 489 ZS_DELAY(); 490 val = *cs->cs_reg_csr; 491 ZS_DELAY(); 492 return (val); 493 } 494 495 void 496 zs_write_reg(struct zs_chanstate *cs, uint8_t reg, uint8_t val) 497 { 498 *cs->cs_reg_csr = reg; 499 ZS_DELAY(); 500 *cs->cs_reg_csr = val; 501 ZS_DELAY(); 502 } 503 504 uint8_t 505 zs_read_csr(struct zs_chanstate *cs) 506 { 507 uint8_t val; 508 509 val = *cs->cs_reg_csr; 510 ZS_DELAY(); 511 return (val); 512 } 513 514 void 515 zs_write_csr(struct zs_chanstate *cs, uint8_t val) 516 { 517 *cs->cs_reg_csr = val; 518 ZS_DELAY(); 519 } 520 521 uint8_t 522 zs_read_data(struct zs_chanstate *cs) 523 { 524 uint8_t val; 525 526 val = *cs->cs_reg_data; 527 ZS_DELAY(); 528 return (val); 529 } 530 531 void 532 zs_write_data(struct zs_chanstate *cs, uint8_t val) 533 { 534 *cs->cs_reg_data = val; 535 ZS_DELAY(); 536 } 537 538 /**************************************************************** 539 * Console support functions (Sun specific!) 540 * Note: this code is allowed to know about the layout of 541 * the chip registers, and uses that to keep things simple. 542 * XXX - I think I like the mvme167 code better. -gwr 543 ****************************************************************/ 544 545 /* 546 * Handle user request to enter kernel debugger. 547 */ 548 void 549 zs_abort(void) 550 { 551 volatile struct zschan *zc = zs_conschan_get; 552 int rr0; 553 554 /* Wait for end of break to avoid PROM abort. */ 555 /* XXX - Limit the wait? */ 556 do { 557 rr0 = zc->zc_csr; 558 ZS_DELAY(); 559 } while (rr0 & ZSRR0_BREAK); 560 561 #if defined(KGDB) 562 zskgdb(); 563 #elif defined(DDB) 564 if (!db_active) 565 cpu_Debugger(); 566 else 567 /* Debugger is probably hozed */ 568 callrom(); 569 #else 570 printf("stopping on keyboard abort\n"); 571 callrom(); 572 #endif 573 } 574 575 /* 576 * Polled input char. 577 */ 578 int 579 zs_getc(void *arg) 580 { 581 volatile struct zschan *zc = arg; 582 int s, c, rr0; 583 584 s = splhigh(); 585 /* Wait for a character to arrive. */ 586 do { 587 rr0 = zc->zc_csr; 588 ZS_DELAY(); 589 } while ((rr0 & ZSRR0_RX_READY) == 0); 590 591 c = zc->zc_data; 592 ZS_DELAY(); 593 splx(s); 594 595 /* 596 * This is used by the kd driver to read scan codes, 597 * so don't translate '\r' ==> '\n' here... 598 */ 599 return (c); 600 } 601 602 /* 603 * Polled output char. 604 */ 605 void 606 zs_putc(void *arg, int c) 607 { 608 volatile struct zschan *zc = arg; 609 int s, rr0; 610 611 s = splhigh(); 612 613 /* Wait for transmitter to become ready. */ 614 do { 615 rr0 = zc->zc_csr; 616 ZS_DELAY(); 617 } while ((rr0 & ZSRR0_TX_READY) == 0); 618 619 /* 620 * Send the next character. 621 * Now you'd think that this could be followed by a ZS_DELAY() 622 * just like all the other chip accesses, but it turns out that 623 * the `transmit-ready' interrupt isn't de-asserted until 624 * some period of time after the register write completes 625 * (more than a couple instructions). So to avoid stray 626 * interrupts we put in the 2us delay regardless of CPU model. 627 */ 628 zc->zc_data = c; 629 delay(2); 630 631 splx(s); 632 } 633 634 /*****************************************************************/ 635 636 /* 637 * Polled console input putchar. 638 */ 639 static int 640 zscngetc(dev_t dev) 641 { 642 return (zs_getc(zs_conschan_get)); 643 } 644 645 /* 646 * Polled console output putchar. 647 */ 648 static void 649 zscnputc(dev_t dev, int c) 650 { 651 zs_putc(zs_conschan_put, c); 652 } 653 654 int swallow_zsintrs; 655 656 static void 657 zscnpollc(dev_t dev, int on) 658 { 659 /* 660 * Need to tell zs driver to acknowledge all interrupts or we get 661 * annoying spurious interrupt messages. This is because mucking 662 * with spl() levels during polling does not prevent interrupts from 663 * being generated. 664 */ 665 666 if (on) swallow_zsintrs++; 667 else swallow_zsintrs--; 668 } 669