1 /* 2 * Copyright (c) 1992, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This software was developed by the Computer Systems Engineering group 6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 7 * contributed to Berkeley. 8 * 9 * All advertising materials mentioning features or use of this software 10 * must display the following acknowledgement: 11 * This product includes software developed by the University of 12 * California, Lawrence Berkeley Laboratory. 13 * 14 * Redistribution and use in source and binary forms, with or without 15 * modification, are permitted provided that the following conditions 16 * are met: 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in the 21 * documentation and/or other materials provided with the distribution. 22 * 3. All advertising materials mentioning features or use of this software 23 * must display the following acknowledgement: 24 * This product includes software developed by the University of 25 * California, Berkeley and its contributors. 26 * 4. Neither the name of the University nor the names of its contributors 27 * may be used to endorse or promote products derived from this software 28 * without specific prior written permission. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 40 * SUCH DAMAGE. 41 * 42 * @(#)zs.c 8.1 (Berkeley) 7/19/93 43 * 44 * from: Header: zs.c,v 1.30 93/07/19 23:44:42 torek Exp 45 * $Id: zs.c,v 1.1 1993/10/02 10:22:45 deraadt Exp $ 46 */ 47 48 /* 49 * Zilog Z8530 (ZSCC) driver. 50 * 51 * Runs two tty ports (ttya and ttyb) on zs0, 52 * and runs a keyboard and mouse on zs1. 53 * 54 * This driver knows far too much about chip to usage mappings. 55 */ 56 #define NZS 2 /* XXX */ 57 58 #include <sys/param.h> 59 #include <sys/proc.h> 60 #include <sys/device.h> 61 #include <sys/conf.h> 62 #include <sys/file.h> 63 #include <sys/ioctl.h> 64 #include <sys/tty.h> 65 #include <sys/time.h> 66 #include <sys/kernel.h> 67 #include <sys/syslog.h> 68 69 #include <machine/autoconf.h> 70 #include <machine/cpu.h> 71 72 #include <sparc/sparc/vaddrs.h> 73 #include <sparc/sparc/auxreg.h> 74 75 #include <sparc/dev/kbd.h> 76 #include <sparc/dev/zsreg.h> 77 #include <sparc/dev/zsvar.h> 78 79 #ifdef KGDB 80 #include <machine/remote-sl.h> 81 #endif 82 83 #define ZSMAJOR 12 /* XXX */ 84 85 #define ZS_KBD 2 /* XXX */ 86 #define ZS_MOUSE 3 /* XXX */ 87 88 /* the magic number below was stolen from the Sprite source. */ 89 #define PCLK (19660800/4) /* PCLK pin input clock rate */ 90 91 /* 92 * Select software interrupt bit based on TTY ipl. 93 */ 94 #if PIL_TTY == 1 95 # define IE_ZSSOFT IE_L1 96 #elif PIL_TTY == 4 97 # define IE_ZSSOFT IE_L4 98 #elif PIL_TTY == 6 99 # define IE_ZSSOFT IE_L6 100 #else 101 # error "no suitable software interrupt bit" 102 #endif 103 104 /* 105 * Software state per found chip. This would be called `zs_softc', 106 * but the previous driver had a rather different zs_softc.... 107 */ 108 struct zsinfo { 109 struct device zi_dev; /* base device */ 110 volatile struct zsdevice *zi_zs;/* chip registers */ 111 struct zs_chanstate zi_cs[2]; /* channel A and B software state */ 112 }; 113 114 struct tty zs_tty[NZS * 2]; /* XXX should be dynamic */ 115 116 /* Definition of the driver for autoconfig. */ 117 static int zsmatch(struct device *, struct cfdata *, void *); 118 static void zsattach(struct device *, struct device *, void *); 119 struct cfdriver zscd = 120 { NULL, "zs", zsmatch, zsattach, DV_TTY, sizeof(struct zsinfo) }; 121 122 /* Interrupt handlers. */ 123 static int zshard(void *); 124 static struct intrhand levelhard = { zshard }; 125 static int zssoft(void *); 126 static struct intrhand levelsoft = { zssoft }; 127 128 struct zs_chanstate *zslist; 129 130 /* Routines called from other code. */ 131 static void zsiopen(struct tty *); 132 static void zsiclose(struct tty *); 133 static void zsstart(struct tty *); 134 static void zsstop(struct tty *, int); 135 static int zsparam(struct tty *, struct termios *); 136 137 /* Routines purely local to this driver. */ 138 static int zs_getspeed(volatile struct zschan *); 139 static void zs_reset(volatile struct zschan *, int, int); 140 static void zs_modem(struct zs_chanstate *, int); 141 static void zs_loadchannelregs(volatile struct zschan *, u_char *); 142 143 /* Console stuff. */ 144 static struct tty *zs_ctty; /* console `struct tty *' */ 145 static int zs_consin = -1, zs_consout = -1; 146 static int zscnputc(int); /* console putc function */ 147 static volatile struct zschan *zs_conschan; 148 static struct tty *zs_checkcons(struct zsinfo *, int, struct zs_chanstate *); 149 150 #ifdef KGDB 151 /* KGDB stuff. Must reboot to change zs_kgdbunit. */ 152 extern int kgdb_dev, kgdb_rate; 153 static int zs_kgdb_savedspeed; 154 static void zs_checkkgdb(int, struct zs_chanstate *, struct tty *); 155 #endif 156 157 extern volatile struct zsdevice *findzs(int); 158 static volatile struct zsdevice *zsaddr[NZS]; /* XXX, but saves work */ 159 160 /* 161 * Console keyboard L1-A processing is done in the hardware interrupt code, 162 * so we need to duplicate some of the console keyboard decode state. (We 163 * must not use the regular state as the hardware code keeps ahead of the 164 * software state: the software state tracks the most recent ring input but 165 * the hardware state tracks the most recent ZSCC input.) See also kbd.h. 166 */ 167 static struct conk_state { /* console keyboard state */ 168 char conk_id; /* true => ID coming up (console only) */ 169 char conk_l1; /* true => L1 pressed (console only) */ 170 } zsconk_state; 171 172 int zshardscope; 173 int zsshortcuts; /* number of "shortcut" software interrupts */ 174 175 /* 176 * Match slave number to zs unit number, so that misconfiguration will 177 * not set up the keyboard as ttya, etc. 178 */ 179 static int 180 zsmatch(struct device *parent, struct cfdata *cf, void *aux) 181 { 182 struct romaux *ra = aux; 183 184 return (getpropint(ra->ra_node, "slave", -2) == cf->cf_unit); 185 } 186 187 /* 188 * Attach a found zs. 189 * 190 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR 191 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE? 192 */ 193 static void 194 zsattach(struct device *parent, struct device *dev, void *aux) 195 { 196 register int zs = dev->dv_unit, unit; 197 register struct zsinfo *zi; 198 register struct zs_chanstate *cs; 199 register volatile struct zsdevice *addr; 200 register struct tty *tp, *ctp; 201 register struct romaux *ra = aux; 202 int pri, softcar; 203 static int didintr, prevpri; 204 205 if ((addr = zsaddr[zs]) == NULL) 206 addr = zsaddr[zs] = findzs(zs); 207 if ((void *)addr != ra->ra_vaddr) 208 panic("zsattach"); 209 if (ra->ra_nintr != 1) { 210 printf(": expected 1 interrupt, got %d\n", ra->ra_nintr); 211 return; 212 } 213 pri = ra->ra_intr[0].int_pri; 214 printf(" pri %d, softpri %d\n", pri, PIL_TTY); 215 if (!didintr) { 216 didintr = 1; 217 prevpri = pri; 218 intr_establish(pri, &levelhard); 219 intr_establish(PIL_TTY, &levelsoft); 220 } else if (pri != prevpri) 221 panic("broken zs interrupt scheme"); 222 zi = (struct zsinfo *)dev; 223 zi->zi_zs = addr; 224 unit = zs * 2; 225 cs = zi->zi_cs; 226 tp = &zs_tty[unit]; 227 228 if (unit == 0) { 229 /* Get software carrier flags from options node in OPENPROM. */ 230 extern int optionsnode; 231 232 softcar = 0; 233 if (*getpropstring(optionsnode, "ttya-ignore-cd") == 't') 234 softcar |= 1; 235 if (*getpropstring(optionsnode, "ttyb-ignore-cd") == 't') 236 softcar |= 2; 237 } else 238 softcar = dev->dv_cfdata->cf_flags; 239 240 /* link into interrupt list with order (A,B) (B=A+1) */ 241 cs[0].cs_next = &cs[1]; 242 cs[1].cs_next = zslist; 243 zslist = cs; 244 245 cs->cs_unit = unit; 246 cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_A]); 247 cs->cs_softcar = softcar & 1; 248 cs->cs_zc = &addr->zs_chan[CHAN_A]; 249 tp->t_dev = makedev(ZSMAJOR, unit); 250 tp->t_oproc = zsstart; 251 tp->t_param = zsparam; 252 tp->t_stop = zsstop; 253 if ((ctp = zs_checkcons(zi, unit, cs)) != NULL) 254 tp = ctp; 255 cs->cs_ttyp = tp; 256 #ifdef KGDB 257 if (ctp == NULL) 258 zs_checkkgdb(unit, cs, tp); 259 #endif 260 if (unit == ZS_KBD) { 261 /* 262 * Keyboard: tell /dev/kbd driver how to talk to us. 263 */ 264 tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 265 tp->t_cflag = CS8; 266 kbd_serial(tp, zsiopen, zsiclose); 267 cs->cs_conk = 1; /* do L1-A processing */ 268 } 269 unit++; 270 cs++; 271 tp = &zs_tty[unit]; 272 cs->cs_unit = unit; 273 cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_B]); 274 cs->cs_softcar = softcar & 2; 275 cs->cs_zc = &addr->zs_chan[CHAN_B]; 276 tp->t_dev = makedev(ZSMAJOR, unit); 277 tp->t_oproc = zsstart; 278 tp->t_param = zsparam; 279 tp->t_stop = zsstop; 280 if ((ctp = zs_checkcons(zi, unit, cs)) != NULL) 281 tp = ctp; 282 cs->cs_ttyp = tp; 283 #ifdef KGDB 284 if (ctp == NULL) 285 zs_checkkgdb(unit, cs, tp); 286 #endif 287 if (unit == ZS_MOUSE) { 288 /* 289 * Mouse: tell /dev/mouse driver how to talk to us. 290 */ 291 tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 292 tp->t_cflag = CS8; 293 ms_serial(tp, zsiopen, zsiclose); 294 } 295 } 296 297 /* 298 * Put a channel in a known state. Interrupts may be left disabled 299 * or enabled, as desired. 300 */ 301 static void 302 zs_reset(zc, inten, speed) 303 volatile struct zschan *zc; 304 int inten, speed; 305 { 306 int tconst; 307 static u_char reg[16] = { 308 0, 309 0, 310 0, 311 ZSWR3_RX_8 | ZSWR3_RX_ENABLE, 312 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP, 313 ZSWR5_TX_8 | ZSWR5_TX_ENABLE, 314 0, 315 0, 316 0, 317 0, 318 ZSWR10_NRZ, 319 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD, 320 0, 321 0, 322 ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA, 323 ZSWR15_BREAK_IE | ZSWR15_DCD_IE, 324 }; 325 326 reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR; 327 tconst = BPS_TO_TCONST(PCLK / 16, speed); 328 reg[12] = tconst; 329 reg[13] = tconst >> 8; 330 zs_loadchannelregs(zc, reg); 331 } 332 333 /* 334 * Declare the given tty (which is in fact &cons) as a console input 335 * or output. This happens before the zs chip is attached; the hookup 336 * is finished later, in zs_setcons() below. 337 * 338 * This is used only for ports a and b. The console keyboard is decoded 339 * independently (we always send unit-2 input to /dev/kbd, which will 340 * direct it to /dev/console if appropriate). 341 */ 342 void 343 zsconsole(tp, unit, out) 344 register struct tty *tp; 345 register int unit; 346 int out; 347 { 348 extern int (*v_putc)(); 349 int zs; 350 volatile struct zsdevice *addr; 351 352 if (unit >= ZS_KBD) 353 panic("zsconsole"); 354 if (out) { 355 zs_consout = unit; 356 zs = unit >> 1; 357 if ((addr = zsaddr[zs]) == NULL) 358 addr = zsaddr[zs] = findzs(zs); 359 zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[CHAN_A] : 360 &addr->zs_chan[CHAN_B]; 361 v_putc = zscnputc; 362 } else 363 zs_consin = unit; 364 zs_ctty = tp; 365 } 366 367 /* 368 * Polled console output putchar. 369 */ 370 static int 371 zscnputc(c) 372 int c; 373 { 374 register volatile struct zschan *zc = zs_conschan; 375 register int s; 376 377 if (c == '\n') 378 zscnputc('\r'); 379 /* 380 * Must block output interrupts (i.e., raise to >= splzs) without 381 * lowering current ipl. Need a better way. 382 */ 383 s = splhigh(); 384 #ifdef sun4c /* XXX */ 385 if (s <= (12 << 8)) 386 (void) splzs(); 387 #endif 388 while ((zc->zc_csr & ZSRR0_TX_READY) == 0) 389 continue; 390 zc->zc_data = c; 391 splx(s); 392 } 393 394 /* 395 * Set up the given unit as console input, output, both, or neither, as 396 * needed. Return console tty if it is to receive console input. 397 */ 398 static struct tty * 399 zs_checkcons(struct zsinfo *zi, int unit, struct zs_chanstate *cs) 400 { 401 register struct tty *tp; 402 char *i, *o; 403 404 if ((tp = zs_ctty) == NULL) 405 return (0); 406 i = zs_consin == unit ? "input" : NULL; 407 o = zs_consout == unit ? "output" : NULL; 408 if (i == NULL && o == NULL) 409 return (0); 410 411 /* rewire the minor device (gack) */ 412 tp->t_dev = makedev(major(tp->t_dev), unit); 413 414 /* 415 * Rewire input and/or output. Note that baud rate reflects 416 * input settings, not output settings, but we can do no better 417 * if the console is split across two ports. 418 * 419 * XXX split consoles don't work anyway -- this needs to be 420 * thrown away and redone 421 */ 422 if (i) { 423 tp->t_param = zsparam; 424 tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 425 tp->t_cflag = CS8; 426 ttsetwater(tp); 427 } 428 if (o) { 429 tp->t_oproc = zsstart; 430 tp->t_stop = zsstop; 431 } 432 printf("%s%c: console %s\n", 433 zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o); 434 cs->cs_consio = 1; 435 cs->cs_brkabort = 1; 436 return (tp); 437 } 438 439 #ifdef KGDB 440 /* 441 * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init). 442 * Pick up the current speed and character size and restore the original 443 * speed. 444 */ 445 static void 446 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp) 447 { 448 449 if (kgdb_dev == makedev(ZSMAJOR, unit)) { 450 tp->t_ispeed = tp->t_ospeed = kgdb_rate; 451 tp->t_cflag = CS8; 452 cs->cs_kgdb = 1; 453 cs->cs_speed = zs_kgdb_savedspeed; 454 (void) zsparam(tp, &tp->t_termios); 455 } 456 } 457 #endif 458 459 /* 460 * Compute the current baud rate given a ZSCC channel. 461 */ 462 static int 463 zs_getspeed(zc) 464 register volatile struct zschan *zc; 465 { 466 register int tconst; 467 468 tconst = ZS_READ(zc, 12); 469 tconst |= ZS_READ(zc, 13) << 8; 470 return (TCONST_TO_BPS(PCLK / 16, tconst)); 471 } 472 473 474 /* 475 * Do an internal open. 476 */ 477 static void 478 zsiopen(struct tty *tp) 479 { 480 481 (void) zsparam(tp, &tp->t_termios); 482 ttsetwater(tp); 483 tp->t_state = TS_ISOPEN | TS_CARR_ON; 484 } 485 486 /* 487 * Do an internal close. Eventually we should shut off the chip when both 488 * ports on it are closed. 489 */ 490 static void 491 zsiclose(struct tty *tp) 492 { 493 494 ttylclose(tp, 0); /* ??? */ 495 ttyclose(tp); /* ??? */ 496 tp->t_state = 0; 497 } 498 499 500 /* 501 * Open a zs serial port. This interface may not be used to open 502 * the keyboard and mouse ports. (XXX) 503 */ 504 int 505 zsopen(dev_t dev, int flags, int mode, struct proc *p) 506 { 507 register struct tty *tp; 508 register struct zs_chanstate *cs; 509 struct zsinfo *zi; 510 int unit = minor(dev), zs = unit >> 1, error, s; 511 512 if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL || 513 unit == ZS_KBD || unit == ZS_MOUSE) 514 return (ENXIO); 515 cs = &zi->zi_cs[unit & 1]; 516 if (cs->cs_consio) 517 return (ENXIO); /* ??? */ 518 tp = cs->cs_ttyp; 519 s = spltty(); 520 if ((tp->t_state & TS_ISOPEN) == 0) { 521 ttychars(tp); 522 if (tp->t_ispeed == 0) { 523 tp->t_iflag = TTYDEF_IFLAG; 524 tp->t_oflag = TTYDEF_OFLAG; 525 tp->t_cflag = TTYDEF_CFLAG; 526 tp->t_lflag = TTYDEF_LFLAG; 527 tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 528 } 529 (void) zsparam(tp, &tp->t_termios); 530 ttsetwater(tp); 531 } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) { 532 splx(s); 533 return (EBUSY); 534 } 535 error = 0; 536 for (;;) { 537 /* loop, turning on the device, until carrier present */ 538 zs_modem(cs, 1); 539 if (cs->cs_softcar) 540 tp->t_state |= TS_CARR_ON; 541 if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL || 542 tp->t_state & TS_CARR_ON) 543 break; 544 tp->t_state |= TS_WOPEN; 545 if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 546 ttopen, 0)) 547 break; 548 } 549 splx(s); 550 if (error == 0) 551 error = linesw[tp->t_line].l_open(dev, tp); 552 if (error) 553 zs_modem(cs, 0); 554 return (error); 555 } 556 557 /* 558 * Close a zs serial port. 559 */ 560 int 561 zsclose(dev_t dev, int flags, int mode, struct proc *p) 562 { 563 register struct zs_chanstate *cs; 564 register struct tty *tp; 565 struct zsinfo *zi; 566 int unit = minor(dev), s; 567 568 zi = zscd.cd_devs[unit >> 1]; 569 cs = &zi->zi_cs[unit & 1]; 570 tp = cs->cs_ttyp; 571 linesw[tp->t_line].l_close(tp, flags); 572 if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN || 573 (tp->t_state & TS_ISOPEN) == 0) { 574 zs_modem(cs, 0); 575 /* hold low for 1 second */ 576 (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz); 577 } 578 ttyclose(tp); 579 #ifdef KGDB 580 /* Reset the speed if we're doing kgdb on this port */ 581 if (cs->cs_kgdb) { 582 tp->t_ispeed = tp->t_ospeed = kgdb_rate; 583 (void) zsparam(tp, &tp->t_termios); 584 } 585 #endif 586 return (0); 587 } 588 589 /* 590 * Read/write zs serial port. 591 */ 592 int 593 zsread(dev_t dev, struct uio *uio, int flags) 594 { 595 register struct tty *tp = &zs_tty[minor(dev)]; 596 597 return (linesw[tp->t_line].l_read(tp, uio, flags)); 598 } 599 600 int 601 zswrite(dev_t dev, struct uio *uio, int flags) 602 { 603 register struct tty *tp = &zs_tty[minor(dev)]; 604 605 return (linesw[tp->t_line].l_write(tp, uio, flags)); 606 } 607 608 /* 609 * ZS hardware interrupt. Scan all ZS channels. NB: we know here that 610 * channels are kept in (A,B) pairs. 611 * 612 * Do just a little, then get out; set a software interrupt if more 613 * work is needed. 614 * 615 * We deliberately ignore the vectoring Zilog gives us, and match up 616 * only the number of `reset interrupt under service' operations, not 617 * the order. 618 */ 619 /* ARGSUSED */ 620 int 621 zshard(void *intrarg) 622 { 623 register struct zs_chanstate *a; 624 #define b (a + 1) 625 register volatile struct zschan *zc; 626 register int rr3, intflags = 0, v, i; 627 static int zsrint(struct zs_chanstate *, volatile struct zschan *); 628 static int zsxint(struct zs_chanstate *, volatile struct zschan *); 629 static int zssint(struct zs_chanstate *, volatile struct zschan *); 630 631 for (a = zslist; a != NULL; a = b->cs_next) { 632 rr3 = ZS_READ(a->cs_zc, 3); 633 if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) { 634 intflags |= 2; 635 zc = a->cs_zc; 636 i = a->cs_rbput; 637 if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) { 638 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 639 intflags |= 1; 640 } 641 if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) { 642 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 643 intflags |= 1; 644 } 645 if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) { 646 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 647 intflags |= 1; 648 } 649 a->cs_rbput = i; 650 } 651 if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) { 652 intflags |= 2; 653 zc = b->cs_zc; 654 i = b->cs_rbput; 655 if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) { 656 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 657 intflags |= 1; 658 } 659 if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) { 660 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 661 intflags |= 1; 662 } 663 if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) { 664 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 665 intflags |= 1; 666 } 667 b->cs_rbput = i; 668 } 669 } 670 #undef b 671 if (intflags & 1) { 672 #if sun4c /* XXX -- but this will go away when zshard moves to locore.s */ 673 struct clockframe *p = intrarg; 674 675 if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) { 676 zsshortcuts++; 677 (void) spltty(); 678 if (zshardscope) { 679 LED_ON; 680 LED_OFF; 681 } 682 return (zssoft(intrarg)); 683 } 684 #endif 685 ienab_bis(IE_ZSSOFT); 686 } 687 return (intflags & 2); 688 } 689 690 static int 691 zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 692 { 693 register int c = zc->zc_data; 694 695 if (cs->cs_conk) { 696 register struct conk_state *conk = &zsconk_state; 697 698 /* 699 * Check here for console abort function, so that we 700 * can abort even when interrupts are locking up the 701 * machine. 702 */ 703 if (c == KBD_RESET) { 704 conk->conk_id = 1; /* ignore next byte */ 705 conk->conk_l1 = 0; 706 } else if (conk->conk_id) 707 conk->conk_id = 0; /* stop ignoring bytes */ 708 else if (c == KBD_L1) 709 conk->conk_l1 = 1; /* L1 went down */ 710 else if (c == (KBD_L1|KBD_UP)) 711 conk->conk_l1 = 0; /* L1 went up */ 712 else if (c == KBD_A && conk->conk_l1) { 713 zsabort(); 714 conk->conk_l1 = 0; /* we never see the up */ 715 goto clearit; /* eat the A after L1-A */ 716 } 717 } 718 #ifdef KGDB 719 if (c == FRAME_START && cs->cs_kgdb && 720 (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) { 721 zskgdb(cs->cs_unit); 722 goto clearit; 723 } 724 #endif 725 /* compose receive character and status */ 726 c <<= 8; 727 c |= ZS_READ(zc, 1); 728 729 /* clear receive error & interrupt condition */ 730 zc->zc_csr = ZSWR0_RESET_ERRORS; 731 zc->zc_csr = ZSWR0_CLR_INTR; 732 733 return (ZRING_MAKE(ZRING_RINT, c)); 734 735 clearit: 736 zc->zc_csr = ZSWR0_RESET_ERRORS; 737 zc->zc_csr = ZSWR0_CLR_INTR; 738 return (0); 739 } 740 741 static int 742 zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 743 { 744 register int i = cs->cs_tbc; 745 746 if (i == 0) { 747 zc->zc_csr = ZSWR0_RESET_TXINT; 748 zc->zc_csr = ZSWR0_CLR_INTR; 749 return (ZRING_MAKE(ZRING_XINT, 0)); 750 } 751 cs->cs_tbc = i - 1; 752 zc->zc_data = *cs->cs_tba++; 753 zc->zc_csr = ZSWR0_CLR_INTR; 754 return (0); 755 } 756 757 static int 758 zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 759 { 760 register int rr0; 761 762 rr0 = zc->zc_csr; 763 zc->zc_csr = ZSWR0_RESET_STATUS; 764 zc->zc_csr = ZSWR0_CLR_INTR; 765 /* 766 * The chip's hardware flow control is, as noted in zsreg.h, 767 * busted---if the DCD line goes low the chip shuts off the 768 * receiver (!). If we want hardware CTS flow control but do 769 * not have it, and carrier is now on, turn HFC on; if we have 770 * HFC now but carrier has gone low, turn it off. 771 */ 772 if (rr0 & ZSRR0_DCD) { 773 if (cs->cs_ttyp->t_cflag & CCTS_OFLOW && 774 (cs->cs_creg[3] & ZSWR3_HFC) == 0) { 775 cs->cs_creg[3] |= ZSWR3_HFC; 776 ZS_WRITE(zc, 3, cs->cs_creg[3]); 777 } 778 } else { 779 if (cs->cs_creg[3] & ZSWR3_HFC) { 780 cs->cs_creg[3] &= ~ZSWR3_HFC; 781 ZS_WRITE(zc, 3, cs->cs_creg[3]); 782 } 783 } 784 if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) { 785 zsabort(); 786 return (0); 787 } 788 return (ZRING_MAKE(ZRING_SINT, rr0)); 789 } 790 791 zsabort() 792 { 793 794 printf("stopping on keyboard abort\n"); 795 callrom(); 796 } 797 798 #ifdef KGDB 799 /* 800 * KGDB framing character received: enter kernel debugger. This probably 801 * should time out after a few seconds to avoid hanging on spurious input. 802 */ 803 zskgdb(int unit) 804 { 805 806 printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a'); 807 kgdb_connect(1); 808 } 809 #endif 810 811 /* 812 * Print out a ring or fifo overrun error message. 813 */ 814 static void 815 zsoverrun(int unit, long *ptime, char *what) 816 { 817 818 if (*ptime != time.tv_sec) { 819 *ptime = time.tv_sec; 820 log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1, 821 (unit & 1) + 'a', what); 822 } 823 } 824 825 /* 826 * ZS software interrupt. Scan all channels for deferred interrupts. 827 */ 828 int 829 zssoft(void *arg) 830 { 831 register struct zs_chanstate *cs; 832 register volatile struct zschan *zc; 833 register struct linesw *line; 834 register struct tty *tp; 835 register int get, n, c, cc, unit, s; 836 837 for (cs = zslist; cs != NULL; cs = cs->cs_next) { 838 get = cs->cs_rbget; 839 again: 840 n = cs->cs_rbput; /* atomic */ 841 if (get == n) /* nothing more on this line */ 842 continue; 843 unit = cs->cs_unit; /* set up to handle interrupts */ 844 zc = cs->cs_zc; 845 tp = cs->cs_ttyp; 846 line = &linesw[tp->t_line]; 847 /* 848 * Compute the number of interrupts in the receive ring. 849 * If the count is overlarge, we lost some events, and 850 * must advance to the first valid one. It may get 851 * overwritten if more data are arriving, but this is 852 * too expensive to check and gains nothing (we already 853 * lost out; all we can do at this point is trade one 854 * kind of loss for another). 855 */ 856 n -= get; 857 if (n > ZLRB_RING_SIZE) { 858 zsoverrun(unit, &cs->cs_rotime, "ring"); 859 get += n - ZLRB_RING_SIZE; 860 n = ZLRB_RING_SIZE; 861 } 862 while (--n >= 0) { 863 /* race to keep ahead of incoming interrupts */ 864 c = cs->cs_rbuf[get++ & ZLRB_RING_MASK]; 865 switch (ZRING_TYPE(c)) { 866 867 case ZRING_RINT: 868 c = ZRING_VALUE(c); 869 if (c & ZSRR1_DO) 870 zsoverrun(unit, &cs->cs_fotime, "fifo"); 871 cc = c >> 8; 872 if (c & ZSRR1_FE) 873 cc |= TTY_FE; 874 if (c & ZSRR1_PE) 875 cc |= TTY_PE; 876 /* 877 * this should be done through 878 * bstreams XXX gag choke 879 */ 880 if (unit == ZS_KBD) 881 kbd_rint(cc); 882 else if (unit == ZS_MOUSE) 883 ms_rint(cc); 884 else 885 line->l_rint(cc, tp); 886 break; 887 888 case ZRING_XINT: 889 /* 890 * Transmit done: change registers and resume, 891 * or clear BUSY. 892 */ 893 if (cs->cs_heldchange) { 894 s = splzs(); 895 c = zc->zc_csr; 896 if ((c & ZSRR0_DCD) == 0) 897 cs->cs_preg[3] &= ~ZSWR3_HFC; 898 bcopy((caddr_t)cs->cs_preg, 899 (caddr_t)cs->cs_creg, 16); 900 zs_loadchannelregs(zc, cs->cs_creg); 901 splx(s); 902 cs->cs_heldchange = 0; 903 if (cs->cs_heldtbc && 904 (tp->t_state & TS_TTSTOP) == 0) { 905 cs->cs_tbc = cs->cs_heldtbc - 1; 906 zc->zc_data = *cs->cs_tba++; 907 goto again; 908 } 909 } 910 tp->t_state &= ~TS_BUSY; 911 if (tp->t_state & TS_FLUSH) 912 tp->t_state &= ~TS_FLUSH; 913 else 914 ndflush(&tp->t_outq, 915 cs->cs_tba - tp->t_outq.c_cf); 916 line->l_start(tp); 917 break; 918 919 case ZRING_SINT: 920 /* 921 * Status line change. HFC bit is run in 922 * hardware interrupt, to avoid locking 923 * at splzs here. 924 */ 925 c = ZRING_VALUE(c); 926 if ((c ^ cs->cs_rr0) & ZSRR0_DCD) { 927 cc = (c & ZSRR0_DCD) != 0; 928 if (line->l_modem(tp, cc) == 0) 929 zs_modem(cs, cc); 930 } 931 cs->cs_rr0 = c; 932 break; 933 934 default: 935 log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n", 936 unit >> 1, (unit & 1) + 'a', c); 937 break; 938 } 939 } 940 cs->cs_rbget = get; 941 goto again; 942 } 943 return (1); 944 } 945 946 int 947 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p) 948 { 949 int unit = minor(dev); 950 struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 951 register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp; 952 register int error; 953 954 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p); 955 if (error >= 0) 956 return (error); 957 error = ttioctl(tp, cmd, data, flag); 958 if (error >= 0) 959 return (error); 960 961 switch (cmd) { 962 963 case TIOCSBRK: 964 /* FINISH ME ... need implicit TIOCCBRK in zsclose as well */ 965 966 case TIOCCBRK: 967 968 case TIOCSDTR: 969 970 case TIOCCDTR: 971 972 case TIOCMSET: 973 974 case TIOCMBIS: 975 976 case TIOCMBIC: 977 978 case TIOCMGET: 979 980 default: 981 return (ENOTTY); 982 } 983 return (0); 984 } 985 986 /* 987 * Start or restart transmission. 988 */ 989 static void 990 zsstart(register struct tty *tp) 991 { 992 register struct zs_chanstate *cs; 993 register int s, nch; 994 int unit = minor(tp->t_dev); 995 struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 996 997 cs = &zi->zi_cs[unit & 1]; 998 s = spltty(); 999 1000 /* 1001 * If currently active or delaying, no need to do anything. 1002 */ 1003 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) 1004 goto out; 1005 1006 /* 1007 * If there are sleepers, and output has drained below low 1008 * water mark, awaken. 1009 */ 1010 if (tp->t_outq.c_cc <= tp->t_lowat) { 1011 if (tp->t_state & TS_ASLEEP) { 1012 tp->t_state &= ~TS_ASLEEP; 1013 wakeup((caddr_t)&tp->t_outq); 1014 } 1015 selwakeup(&tp->t_wsel); 1016 } 1017 1018 nch = ndqb(&tp->t_outq, 0); /* XXX */ 1019 if (nch) { 1020 register char *p = tp->t_outq.c_cf; 1021 1022 /* mark busy, enable tx done interrupts, & send first byte */ 1023 tp->t_state |= TS_BUSY; 1024 (void) splzs(); 1025 cs->cs_preg[1] |= ZSWR1_TIE; 1026 cs->cs_creg[1] |= ZSWR1_TIE; 1027 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]); 1028 cs->cs_zc->zc_data = *p; 1029 cs->cs_tba = p + 1; 1030 cs->cs_tbc = nch - 1; 1031 } else { 1032 /* 1033 * Nothing to send, turn off transmit done interrupts. 1034 * This is useful if something is doing polled output. 1035 */ 1036 (void) splzs(); 1037 cs->cs_preg[1] &= ~ZSWR1_TIE; 1038 cs->cs_creg[1] &= ~ZSWR1_TIE; 1039 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]); 1040 } 1041 out: 1042 splx(s); 1043 } 1044 1045 /* 1046 * Stop output, e.g., for ^S or output flush. 1047 */ 1048 static void 1049 zsstop(register struct tty *tp, int flag) 1050 { 1051 register struct zs_chanstate *cs; 1052 register int s, unit = minor(tp->t_dev); 1053 struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 1054 1055 cs = &zi->zi_cs[unit & 1]; 1056 s = splzs(); 1057 if (tp->t_state & TS_BUSY) { 1058 /* 1059 * Device is transmitting; must stop it. 1060 */ 1061 cs->cs_tbc = 0; 1062 if ((tp->t_state & TS_TTSTOP) == 0) 1063 tp->t_state |= TS_FLUSH; 1064 } 1065 splx(s); 1066 } 1067 1068 /* 1069 * Set ZS tty parameters from termios. 1070 * 1071 * This routine makes use of the fact that only registers 1072 * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written. 1073 */ 1074 static int 1075 zsparam(register struct tty *tp, register struct termios *t) 1076 { 1077 int unit = minor(tp->t_dev); 1078 struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 1079 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1]; 1080 register int tmp, tmp5, cflag, s; 1081 1082 /* 1083 * Because PCLK is only run at 4.9 MHz, the fastest we 1084 * can go is 51200 baud (this corresponds to TC=1). 1085 * This is somewhat unfortunate as there is no real 1086 * reason we should not be able to handle higher rates. 1087 */ 1088 tmp = t->c_ospeed; 1089 if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp)) 1090 return (EINVAL); 1091 if (tmp == 0) { 1092 /* stty 0 => drop DTR and RTS */ 1093 zs_modem(cs, 0); 1094 return (0); 1095 } 1096 tmp = BPS_TO_TCONST(PCLK / 16, tmp); 1097 if (tmp < 2) 1098 return (EINVAL); 1099 1100 cflag = t->c_cflag; 1101 tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp); 1102 tp->t_cflag = cflag; 1103 1104 /* 1105 * Block interrupts so that state will not 1106 * be altered until we are done setting it up. 1107 */ 1108 s = splzs(); 1109 cs->cs_preg[12] = tmp; 1110 cs->cs_preg[13] = tmp >> 8; 1111 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE; 1112 switch (cflag & CSIZE) { 1113 case CS5: 1114 tmp = ZSWR3_RX_5; 1115 tmp5 = ZSWR5_TX_5; 1116 break; 1117 case CS6: 1118 tmp = ZSWR3_RX_6; 1119 tmp5 = ZSWR5_TX_6; 1120 break; 1121 case CS7: 1122 tmp = ZSWR3_RX_7; 1123 tmp5 = ZSWR5_TX_7; 1124 break; 1125 case CS8: 1126 default: 1127 tmp = ZSWR3_RX_8; 1128 tmp5 = ZSWR5_TX_8; 1129 break; 1130 } 1131 1132 /* 1133 * Output hardware flow control on the chip is horrendous: if 1134 * carrier detect drops, the receiver is disabled. Hence we 1135 * can only do this when the carrier is on. 1136 */ 1137 if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD) 1138 tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE; 1139 else 1140 tmp |= ZSWR3_RX_ENABLE; 1141 cs->cs_preg[3] = tmp; 1142 cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS; 1143 1144 tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB); 1145 if ((cflag & PARODD) == 0) 1146 tmp |= ZSWR4_EVENP; 1147 if (cflag & PARENB) 1148 tmp |= ZSWR4_PARENB; 1149 cs->cs_preg[4] = tmp; 1150 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR; 1151 cs->cs_preg[10] = ZSWR10_NRZ; 1152 cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD; 1153 cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA; 1154 cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE; 1155 1156 /* 1157 * If nothing is being transmitted, set up new current values, 1158 * else mark them as pending. 1159 */ 1160 if (cs->cs_heldchange == 0) { 1161 if (cs->cs_ttyp->t_state & TS_BUSY) { 1162 cs->cs_heldtbc = cs->cs_tbc; 1163 cs->cs_tbc = 0; 1164 cs->cs_heldchange = 1; 1165 } else { 1166 bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16); 1167 zs_loadchannelregs(cs->cs_zc, cs->cs_creg); 1168 } 1169 } 1170 splx(s); 1171 return (0); 1172 } 1173 1174 /* 1175 * Raise or lower modem control (DTR/RTS) signals. If a character is 1176 * in transmission, the change is deferred. 1177 */ 1178 static void 1179 zs_modem(struct zs_chanstate *cs, int onoff) 1180 { 1181 int s, bis, and; 1182 1183 if (onoff) { 1184 bis = ZSWR5_DTR | ZSWR5_RTS; 1185 and = ~0; 1186 } else { 1187 bis = 0; 1188 and = ~(ZSWR5_DTR | ZSWR5_RTS); 1189 } 1190 s = splzs(); 1191 cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and; 1192 if (cs->cs_heldchange == 0) { 1193 if (cs->cs_ttyp->t_state & TS_BUSY) { 1194 cs->cs_heldtbc = cs->cs_tbc; 1195 cs->cs_tbc = 0; 1196 cs->cs_heldchange = 1; 1197 } else { 1198 cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and; 1199 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]); 1200 } 1201 } 1202 splx(s); 1203 } 1204 1205 /* 1206 * Write the given register set to the given zs channel in the proper order. 1207 * The channel must not be transmitting at the time. The receiver will 1208 * be disabled for the time it takes to write all the registers. 1209 */ 1210 static void 1211 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg) 1212 { 1213 int i; 1214 1215 zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */ 1216 i = zc->zc_data; /* drain fifo */ 1217 i = zc->zc_data; 1218 i = zc->zc_data; 1219 ZS_WRITE(zc, 4, reg[4]); 1220 ZS_WRITE(zc, 10, reg[10]); 1221 ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE); 1222 ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE); 1223 ZS_WRITE(zc, 1, reg[1]); 1224 ZS_WRITE(zc, 9, reg[9]); 1225 ZS_WRITE(zc, 11, reg[11]); 1226 ZS_WRITE(zc, 12, reg[12]); 1227 ZS_WRITE(zc, 13, reg[13]); 1228 ZS_WRITE(zc, 14, reg[14]); 1229 ZS_WRITE(zc, 15, reg[15]); 1230 ZS_WRITE(zc, 3, reg[3]); 1231 ZS_WRITE(zc, 5, reg[5]); 1232 } 1233 1234 #ifdef KGDB 1235 /* 1236 * Get a character from the given kgdb channel. Called at splhigh(). 1237 */ 1238 static int 1239 zs_kgdb_getc(void *arg) 1240 { 1241 register volatile struct zschan *zc = (volatile struct zschan *)arg; 1242 1243 while ((zc->zc_csr & ZSRR0_RX_READY) == 0) 1244 continue; 1245 return (zc->zc_data); 1246 } 1247 1248 /* 1249 * Put a character to the given kgdb channel. Called at splhigh(). 1250 */ 1251 static void 1252 zs_kgdb_putc(void *arg, int c) 1253 { 1254 register volatile struct zschan *zc = (volatile struct zschan *)arg; 1255 1256 while ((zc->zc_csr & ZSRR0_TX_READY) == 0) 1257 continue; 1258 zc->zc_data = c; 1259 } 1260 1261 /* 1262 * Set up for kgdb; called at boot time before configuration. 1263 * KGDB interrupts will be enabled later when zs0 is configured. 1264 */ 1265 void 1266 zs_kgdb_init() 1267 { 1268 volatile struct zsdevice *addr; 1269 volatile struct zschan *zc; 1270 int unit, zs; 1271 1272 if (major(kgdb_dev) != ZSMAJOR) 1273 return; 1274 unit = minor(kgdb_dev); 1275 /* 1276 * Unit must be 0 or 1 (zs0). 1277 */ 1278 if ((unsigned)unit >= ZS_KBD) { 1279 printf("zs_kgdb_init: bad minor dev %d\n", unit); 1280 return; 1281 } 1282 zs = unit >> 1; 1283 if ((addr = zsaddr[zs]) == NULL) 1284 addr = zsaddr[zs] = findzs(zs); 1285 unit &= 1; 1286 zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B]; 1287 zs_kgdb_savedspeed = zs_getspeed(zc); 1288 printf("zs_kgdb_init: attaching zs%d%c at %d baud\n", 1289 zs, unit + 'a', kgdb_rate); 1290 zs_reset(zc, 1, kgdb_rate); 1291 kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc); 1292 } 1293 #endif /* KGDB */ 1294