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.3 1993/10/13 02:36:44 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 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 227 if(!zs_tty[unit]) 228 zs_tty[unit] = ttymalloc(); 229 tp = zs_tty[unit]; 230 if(!zs_tty[unit+1]) 231 zs_tty[unit+1] = ttymalloc(); 232 233 if (unit == 0) { 234 /* Get software carrier flags from options node in OPENPROM. */ 235 extern int optionsnode; 236 237 softcar = 0; 238 if (*getpropstring(optionsnode, "ttya-ignore-cd") == 't') 239 softcar |= 1; 240 if (*getpropstring(optionsnode, "ttyb-ignore-cd") == 't') 241 softcar |= 2; 242 } else 243 softcar = dev->dv_cfdata->cf_flags; 244 245 /* link into interrupt list with order (A,B) (B=A+1) */ 246 cs[0].cs_next = &cs[1]; 247 cs[1].cs_next = zslist; 248 zslist = cs; 249 250 cs->cs_unit = unit; 251 cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_A]); 252 cs->cs_softcar = softcar & 1; 253 cs->cs_zc = &addr->zs_chan[CHAN_A]; 254 tp->t_dev = makedev(ZSMAJOR, unit); 255 tp->t_oproc = zsstart; 256 tp->t_param = zsparam; 257 /*tp->t_stop = zsstop;*/ 258 if ((ctp = zs_checkcons(zi, unit, cs)) != NULL) 259 tp = ctp; 260 cs->cs_ttyp = tp; 261 #ifdef KGDB 262 if (ctp == NULL) 263 zs_checkkgdb(unit, cs, tp); 264 #endif 265 if (unit == ZS_KBD) { 266 /* 267 * Keyboard: tell /dev/kbd driver how to talk to us. 268 */ 269 tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 270 tp->t_cflag = CS8; 271 kbd_serial(tp, zsiopen, zsiclose); 272 cs->cs_conk = 1; /* do L1-A processing */ 273 } 274 unit++; 275 cs++; 276 tp = zs_tty[unit]; 277 cs->cs_unit = unit; 278 cs->cs_speed = zs_getspeed(&addr->zs_chan[CHAN_B]); 279 cs->cs_softcar = softcar & 2; 280 cs->cs_zc = &addr->zs_chan[CHAN_B]; 281 tp->t_dev = makedev(ZSMAJOR, unit); 282 tp->t_oproc = zsstart; 283 tp->t_param = zsparam; 284 /*tp->t_stop = zsstop;*/ 285 if ((ctp = zs_checkcons(zi, unit, cs)) != NULL) 286 tp = ctp; 287 cs->cs_ttyp = tp; 288 #ifdef KGDB 289 if (ctp == NULL) 290 zs_checkkgdb(unit, cs, tp); 291 #endif 292 if (unit == ZS_MOUSE) { 293 /* 294 * Mouse: tell /dev/mouse driver how to talk to us. 295 */ 296 tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 297 tp->t_cflag = CS8; 298 ms_serial(tp, zsiopen, zsiclose); 299 } 300 } 301 302 /* 303 * Put a channel in a known state. Interrupts may be left disabled 304 * or enabled, as desired. 305 */ 306 static void 307 zs_reset(zc, inten, speed) 308 volatile struct zschan *zc; 309 int inten, speed; 310 { 311 int tconst; 312 static u_char reg[16] = { 313 0, 314 0, 315 0, 316 ZSWR3_RX_8 | ZSWR3_RX_ENABLE, 317 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP, 318 ZSWR5_TX_8 | ZSWR5_TX_ENABLE, 319 0, 320 0, 321 0, 322 0, 323 ZSWR10_NRZ, 324 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD, 325 0, 326 0, 327 ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA, 328 ZSWR15_BREAK_IE | ZSWR15_DCD_IE, 329 }; 330 331 reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR; 332 tconst = BPS_TO_TCONST(PCLK / 16, speed); 333 reg[12] = tconst; 334 reg[13] = tconst >> 8; 335 zs_loadchannelregs(zc, reg); 336 } 337 338 /* 339 * Declare the given tty (which is in fact &cons) as a console input 340 * or output. This happens before the zs chip is attached; the hookup 341 * is finished later, in zs_setcons() below. 342 * 343 * This is used only for ports a and b. The console keyboard is decoded 344 * independently (we always send unit-2 input to /dev/kbd, which will 345 * direct it to /dev/console if appropriate). 346 */ 347 void 348 zsconsole(tp, unit, out, fnstop) 349 register struct tty *tp; 350 register int unit; 351 int out; 352 void (**fnstop) __P((struct tty *, int)); 353 { 354 extern int (*v_putc)(); 355 int zs; 356 volatile struct zsdevice *addr; 357 358 if (unit >= ZS_KBD) 359 panic("zsconsole"); 360 if (out) { 361 zs_consout = unit; 362 zs = unit >> 1; 363 if ((addr = zsaddr[zs]) == NULL) 364 addr = zsaddr[zs] = findzs(zs); 365 zs_conschan = (unit & 1) == 0 ? &addr->zs_chan[CHAN_A] : 366 &addr->zs_chan[CHAN_B]; 367 v_putc = zscnputc; 368 } else 369 zs_consin = unit; 370 if(fnstop) 371 *fnstop = &zsstop; 372 zs_ctty = tp; 373 } 374 375 /* 376 * Polled console output putchar. 377 */ 378 static int 379 zscnputc(c) 380 int c; 381 { 382 register volatile struct zschan *zc = zs_conschan; 383 register int s; 384 385 if (c == '\n') 386 zscnputc('\r'); 387 /* 388 * Must block output interrupts (i.e., raise to >= splzs) without 389 * lowering current ipl. Need a better way. 390 */ 391 s = splhigh(); 392 #ifdef sun4c /* XXX */ 393 if (s <= (12 << 8)) 394 (void) splzs(); 395 #endif 396 while ((zc->zc_csr & ZSRR0_TX_READY) == 0) 397 continue; 398 zc->zc_data = c; 399 splx(s); 400 } 401 402 /* 403 * Set up the given unit as console input, output, both, or neither, as 404 * needed. Return console tty if it is to receive console input. 405 */ 406 static struct tty * 407 zs_checkcons(struct zsinfo *zi, int unit, struct zs_chanstate *cs) 408 { 409 register struct tty *tp; 410 char *i, *o; 411 412 if ((tp = zs_ctty) == NULL) 413 return (0); 414 i = zs_consin == unit ? "input" : NULL; 415 o = zs_consout == unit ? "output" : NULL; 416 if (i == NULL && o == NULL) 417 return (0); 418 419 /* rewire the minor device (gack) */ 420 tp->t_dev = makedev(major(tp->t_dev), unit); 421 422 /* 423 * Rewire input and/or output. Note that baud rate reflects 424 * input settings, not output settings, but we can do no better 425 * if the console is split across two ports. 426 * 427 * XXX split consoles don't work anyway -- this needs to be 428 * thrown away and redone 429 */ 430 if (i) { 431 tp->t_param = zsparam; 432 tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 433 tp->t_cflag = CS8; 434 ttsetwater(tp); 435 } 436 if (o) { 437 tp->t_oproc = zsstart; 438 /*tp->t_stop = zsstop;*/ 439 } 440 printf("%s%c: console %s\n", 441 zi->zi_dev.dv_xname, (unit & 1) + 'a', i ? (o ? "i/o" : i) : o); 442 cs->cs_consio = 1; 443 cs->cs_brkabort = 1; 444 return (tp); 445 } 446 447 #ifdef KGDB 448 /* 449 * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init). 450 * Pick up the current speed and character size and restore the original 451 * speed. 452 */ 453 static void 454 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp) 455 { 456 457 if (kgdb_dev == makedev(ZSMAJOR, unit)) { 458 tp->t_ispeed = tp->t_ospeed = kgdb_rate; 459 tp->t_cflag = CS8; 460 cs->cs_kgdb = 1; 461 cs->cs_speed = zs_kgdb_savedspeed; 462 (void) zsparam(tp, &tp->t_termios); 463 } 464 } 465 #endif 466 467 /* 468 * Compute the current baud rate given a ZSCC channel. 469 */ 470 static int 471 zs_getspeed(zc) 472 register volatile struct zschan *zc; 473 { 474 register int tconst; 475 476 tconst = ZS_READ(zc, 12); 477 tconst |= ZS_READ(zc, 13) << 8; 478 return (TCONST_TO_BPS(PCLK / 16, tconst)); 479 } 480 481 482 /* 483 * Do an internal open. 484 */ 485 static void 486 zsiopen(struct tty *tp) 487 { 488 489 (void) zsparam(tp, &tp->t_termios); 490 ttsetwater(tp); 491 tp->t_state = TS_ISOPEN | TS_CARR_ON; 492 } 493 494 /* 495 * Do an internal close. Eventually we should shut off the chip when both 496 * ports on it are closed. 497 */ 498 static void 499 zsiclose(struct tty *tp) 500 { 501 502 ttylclose(tp, 0); /* ??? */ 503 ttyclose(tp); /* ??? */ 504 tp->t_state = 0; 505 } 506 507 508 /* 509 * Open a zs serial port. This interface may not be used to open 510 * the keyboard and mouse ports. (XXX) 511 */ 512 int 513 zsopen(dev_t dev, int flags, int mode, struct proc *p) 514 { 515 register struct tty *tp; 516 register struct zs_chanstate *cs; 517 struct zsinfo *zi; 518 int unit = minor(dev), zs = unit >> 1, error, s; 519 520 if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL || 521 unit == ZS_KBD || unit == ZS_MOUSE) 522 return (ENXIO); 523 cs = &zi->zi_cs[unit & 1]; 524 if (cs->cs_consio) 525 return (ENXIO); /* ??? */ 526 tp = cs->cs_ttyp; 527 s = spltty(); 528 if ((tp->t_state & TS_ISOPEN) == 0) { 529 ttychars(tp); 530 if (tp->t_ispeed == 0) { 531 tp->t_iflag = TTYDEF_IFLAG; 532 tp->t_oflag = TTYDEF_OFLAG; 533 tp->t_cflag = TTYDEF_CFLAG; 534 tp->t_lflag = TTYDEF_LFLAG; 535 tp->t_ispeed = tp->t_ospeed = cs->cs_speed; 536 } 537 (void) zsparam(tp, &tp->t_termios); 538 ttsetwater(tp); 539 } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) { 540 splx(s); 541 return (EBUSY); 542 } 543 error = 0; 544 for (;;) { 545 /* loop, turning on the device, until carrier present */ 546 zs_modem(cs, 1); 547 if (cs->cs_softcar) 548 tp->t_state |= TS_CARR_ON; 549 if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL || 550 tp->t_state & TS_CARR_ON) 551 break; 552 tp->t_state |= TS_WOPEN; 553 if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH, 554 ttopen, 0)) 555 break; 556 } 557 splx(s); 558 if (error == 0) 559 error = linesw[tp->t_line].l_open(dev, tp); 560 if (error) 561 zs_modem(cs, 0); 562 return (error); 563 } 564 565 /* 566 * Close a zs serial port. 567 */ 568 int 569 zsclose(dev_t dev, int flags, int mode, struct proc *p) 570 { 571 register struct zs_chanstate *cs; 572 register struct tty *tp; 573 struct zsinfo *zi; 574 int unit = minor(dev), s; 575 576 zi = zscd.cd_devs[unit >> 1]; 577 cs = &zi->zi_cs[unit & 1]; 578 tp = cs->cs_ttyp; 579 linesw[tp->t_line].l_close(tp, flags); 580 if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN || 581 (tp->t_state & TS_ISOPEN) == 0) { 582 zs_modem(cs, 0); 583 /* hold low for 1 second */ 584 (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz); 585 } 586 ttyclose(tp); 587 #ifdef KGDB 588 /* Reset the speed if we're doing kgdb on this port */ 589 if (cs->cs_kgdb) { 590 tp->t_ispeed = tp->t_ospeed = kgdb_rate; 591 (void) zsparam(tp, &tp->t_termios); 592 } 593 #endif 594 return (0); 595 } 596 597 /* 598 * Read/write zs serial port. 599 */ 600 int 601 zsread(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_read(tp, uio, flags)); 606 } 607 608 int 609 zswrite(dev_t dev, struct uio *uio, int flags) 610 { 611 register struct tty *tp = zs_tty[minor(dev)]; 612 613 return (linesw[tp->t_line].l_write(tp, uio, flags)); 614 } 615 616 /* 617 * ZS hardware interrupt. Scan all ZS channels. NB: we know here that 618 * channels are kept in (A,B) pairs. 619 * 620 * Do just a little, then get out; set a software interrupt if more 621 * work is needed. 622 * 623 * We deliberately ignore the vectoring Zilog gives us, and match up 624 * only the number of `reset interrupt under service' operations, not 625 * the order. 626 */ 627 /* ARGSUSED */ 628 int 629 zshard(void *intrarg) 630 { 631 register struct zs_chanstate *a; 632 #define b (a + 1) 633 register volatile struct zschan *zc; 634 register int rr3, intflags = 0, v, i; 635 static int zsrint(struct zs_chanstate *, volatile struct zschan *); 636 static int zsxint(struct zs_chanstate *, volatile struct zschan *); 637 static int zssint(struct zs_chanstate *, volatile struct zschan *); 638 639 for (a = zslist; a != NULL; a = b->cs_next) { 640 rr3 = ZS_READ(a->cs_zc, 3); 641 if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) { 642 intflags |= 2; 643 zc = a->cs_zc; 644 i = a->cs_rbput; 645 if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) { 646 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 647 intflags |= 1; 648 } 649 if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) { 650 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 651 intflags |= 1; 652 } 653 if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) { 654 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 655 intflags |= 1; 656 } 657 a->cs_rbput = i; 658 } 659 if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) { 660 intflags |= 2; 661 zc = b->cs_zc; 662 i = b->cs_rbput; 663 if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) { 664 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 665 intflags |= 1; 666 } 667 if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) { 668 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 669 intflags |= 1; 670 } 671 if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) { 672 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v; 673 intflags |= 1; 674 } 675 b->cs_rbput = i; 676 } 677 } 678 #undef b 679 if (intflags & 1) { 680 #if sun4c /* XXX -- but this will go away when zshard moves to locore.s */ 681 struct clockframe *p = intrarg; 682 683 if ((p->psr & PSR_PIL) < (PIL_TTY << 8)) { 684 zsshortcuts++; 685 (void) spltty(); 686 if (zshardscope) { 687 LED_ON; 688 LED_OFF; 689 } 690 return (zssoft(intrarg)); 691 } 692 #endif 693 ienab_bis(IE_ZSSOFT); 694 } 695 return (intflags & 2); 696 } 697 698 static int 699 zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 700 { 701 register int c = zc->zc_data; 702 703 if (cs->cs_conk) { 704 register struct conk_state *conk = &zsconk_state; 705 706 /* 707 * Check here for console abort function, so that we 708 * can abort even when interrupts are locking up the 709 * machine. 710 */ 711 if (c == KBD_RESET) { 712 conk->conk_id = 1; /* ignore next byte */ 713 conk->conk_l1 = 0; 714 } else if (conk->conk_id) 715 conk->conk_id = 0; /* stop ignoring bytes */ 716 else if (c == KBD_L1) 717 conk->conk_l1 = 1; /* L1 went down */ 718 else if (c == (KBD_L1|KBD_UP)) 719 conk->conk_l1 = 0; /* L1 went up */ 720 else if (c == KBD_A && conk->conk_l1) { 721 zsabort(); 722 conk->conk_l1 = 0; /* we never see the up */ 723 goto clearit; /* eat the A after L1-A */ 724 } 725 } 726 #ifdef KGDB 727 if (c == FRAME_START && cs->cs_kgdb && 728 (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) { 729 zskgdb(cs->cs_unit); 730 goto clearit; 731 } 732 #endif 733 /* compose receive character and status */ 734 c <<= 8; 735 c |= ZS_READ(zc, 1); 736 737 /* clear receive error & interrupt condition */ 738 zc->zc_csr = ZSWR0_RESET_ERRORS; 739 zc->zc_csr = ZSWR0_CLR_INTR; 740 741 return (ZRING_MAKE(ZRING_RINT, c)); 742 743 clearit: 744 zc->zc_csr = ZSWR0_RESET_ERRORS; 745 zc->zc_csr = ZSWR0_CLR_INTR; 746 return (0); 747 } 748 749 static int 750 zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 751 { 752 register int i = cs->cs_tbc; 753 754 if (i == 0) { 755 zc->zc_csr = ZSWR0_RESET_TXINT; 756 zc->zc_csr = ZSWR0_CLR_INTR; 757 return (ZRING_MAKE(ZRING_XINT, 0)); 758 } 759 cs->cs_tbc = i - 1; 760 zc->zc_data = *cs->cs_tba++; 761 zc->zc_csr = ZSWR0_CLR_INTR; 762 return (0); 763 } 764 765 static int 766 zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc) 767 { 768 register int rr0; 769 770 rr0 = zc->zc_csr; 771 zc->zc_csr = ZSWR0_RESET_STATUS; 772 zc->zc_csr = ZSWR0_CLR_INTR; 773 /* 774 * The chip's hardware flow control is, as noted in zsreg.h, 775 * busted---if the DCD line goes low the chip shuts off the 776 * receiver (!). If we want hardware CTS flow control but do 777 * not have it, and carrier is now on, turn HFC on; if we have 778 * HFC now but carrier has gone low, turn it off. 779 */ 780 if (rr0 & ZSRR0_DCD) { 781 if (cs->cs_ttyp->t_cflag & CCTS_OFLOW && 782 (cs->cs_creg[3] & ZSWR3_HFC) == 0) { 783 cs->cs_creg[3] |= ZSWR3_HFC; 784 ZS_WRITE(zc, 3, cs->cs_creg[3]); 785 } 786 } else { 787 if (cs->cs_creg[3] & ZSWR3_HFC) { 788 cs->cs_creg[3] &= ~ZSWR3_HFC; 789 ZS_WRITE(zc, 3, cs->cs_creg[3]); 790 } 791 } 792 if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) { 793 zsabort(); 794 return (0); 795 } 796 return (ZRING_MAKE(ZRING_SINT, rr0)); 797 } 798 799 zsabort() 800 { 801 802 printf("stopping on keyboard abort\n"); 803 callrom(); 804 } 805 806 #ifdef KGDB 807 /* 808 * KGDB framing character received: enter kernel debugger. This probably 809 * should time out after a few seconds to avoid hanging on spurious input. 810 */ 811 zskgdb(int unit) 812 { 813 814 printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a'); 815 kgdb_connect(1); 816 } 817 #endif 818 819 /* 820 * Print out a ring or fifo overrun error message. 821 */ 822 static void 823 zsoverrun(int unit, long *ptime, char *what) 824 { 825 826 if (*ptime != time.tv_sec) { 827 *ptime = time.tv_sec; 828 log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1, 829 (unit & 1) + 'a', what); 830 } 831 } 832 833 /* 834 * ZS software interrupt. Scan all channels for deferred interrupts. 835 */ 836 int 837 zssoft(void *arg) 838 { 839 register struct zs_chanstate *cs; 840 register volatile struct zschan *zc; 841 register struct linesw *line; 842 register struct tty *tp; 843 register int get, n, c, cc, unit, s; 844 845 for (cs = zslist; cs != NULL; cs = cs->cs_next) { 846 get = cs->cs_rbget; 847 again: 848 n = cs->cs_rbput; /* atomic */ 849 if (get == n) /* nothing more on this line */ 850 continue; 851 unit = cs->cs_unit; /* set up to handle interrupts */ 852 zc = cs->cs_zc; 853 tp = cs->cs_ttyp; 854 line = &linesw[tp->t_line]; 855 /* 856 * Compute the number of interrupts in the receive ring. 857 * If the count is overlarge, we lost some events, and 858 * must advance to the first valid one. It may get 859 * overwritten if more data are arriving, but this is 860 * too expensive to check and gains nothing (we already 861 * lost out; all we can do at this point is trade one 862 * kind of loss for another). 863 */ 864 n -= get; 865 if (n > ZLRB_RING_SIZE) { 866 zsoverrun(unit, &cs->cs_rotime, "ring"); 867 get += n - ZLRB_RING_SIZE; 868 n = ZLRB_RING_SIZE; 869 } 870 while (--n >= 0) { 871 /* race to keep ahead of incoming interrupts */ 872 c = cs->cs_rbuf[get++ & ZLRB_RING_MASK]; 873 switch (ZRING_TYPE(c)) { 874 875 case ZRING_RINT: 876 c = ZRING_VALUE(c); 877 if (c & ZSRR1_DO) 878 zsoverrun(unit, &cs->cs_fotime, "fifo"); 879 cc = c >> 8; 880 if (c & ZSRR1_FE) 881 cc |= TTY_FE; 882 if (c & ZSRR1_PE) 883 cc |= TTY_PE; 884 /* 885 * this should be done through 886 * bstreams XXX gag choke 887 */ 888 if (unit == ZS_KBD) 889 kbd_rint(cc); 890 else if (unit == ZS_MOUSE) 891 ms_rint(cc); 892 else 893 line->l_rint(cc, tp); 894 break; 895 896 case ZRING_XINT: 897 /* 898 * Transmit done: change registers and resume, 899 * or clear BUSY. 900 */ 901 if (cs->cs_heldchange) { 902 s = splzs(); 903 c = zc->zc_csr; 904 if ((c & ZSRR0_DCD) == 0) 905 cs->cs_preg[3] &= ~ZSWR3_HFC; 906 bcopy((caddr_t)cs->cs_preg, 907 (caddr_t)cs->cs_creg, 16); 908 zs_loadchannelregs(zc, cs->cs_creg); 909 splx(s); 910 cs->cs_heldchange = 0; 911 if (cs->cs_heldtbc && 912 (tp->t_state & TS_TTSTOP) == 0) { 913 cs->cs_tbc = cs->cs_heldtbc - 1; 914 zc->zc_data = *cs->cs_tba++; 915 goto again; 916 } 917 } 918 tp->t_state &= ~TS_BUSY; 919 if (tp->t_state & TS_FLUSH) 920 tp->t_state &= ~TS_FLUSH; 921 else 922 ndflush(&tp->t_outq, 923 (u_char *)cs->cs_tba - tp->t_outq.c_cf); 924 line->l_start(tp); 925 break; 926 927 case ZRING_SINT: 928 /* 929 * Status line change. HFC bit is run in 930 * hardware interrupt, to avoid locking 931 * at splzs here. 932 */ 933 c = ZRING_VALUE(c); 934 if ((c ^ cs->cs_rr0) & ZSRR0_DCD) { 935 cc = (c & ZSRR0_DCD) != 0; 936 if (line->l_modem(tp, cc) == 0) 937 zs_modem(cs, cc); 938 } 939 cs->cs_rr0 = c; 940 break; 941 942 default: 943 log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n", 944 unit >> 1, (unit & 1) + 'a', c); 945 break; 946 } 947 } 948 cs->cs_rbget = get; 949 goto again; 950 } 951 return (1); 952 } 953 954 int 955 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p) 956 { 957 int unit = minor(dev); 958 struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 959 register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp; 960 register int error; 961 962 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag); 963 if (error >= 0) 964 return (error); 965 error = ttioctl(tp, cmd, data, flag); 966 if (error >= 0) 967 return (error); 968 969 switch (cmd) { 970 971 case TIOCSBRK: 972 /* FINISH ME ... need implicit TIOCCBRK in zsclose as well */ 973 974 case TIOCCBRK: 975 976 case TIOCSDTR: 977 978 case TIOCCDTR: 979 980 case TIOCMSET: 981 982 case TIOCMBIS: 983 984 case TIOCMBIC: 985 986 case TIOCMGET: 987 988 default: 989 return (ENOTTY); 990 } 991 return (0); 992 } 993 994 /* 995 * Start or restart transmission. 996 */ 997 static void 998 zsstart(register struct tty *tp) 999 { 1000 register struct zs_chanstate *cs; 1001 register int s, nch; 1002 int unit = minor(tp->t_dev); 1003 struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 1004 1005 cs = &zi->zi_cs[unit & 1]; 1006 s = spltty(); 1007 1008 /* 1009 * If currently active or delaying, no need to do anything. 1010 */ 1011 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP)) 1012 goto out; 1013 1014 /* 1015 * If there are sleepers, and output has drained below low 1016 * water mark, awaken. 1017 */ 1018 if (tp->t_outq.c_cc <= tp->t_lowat) { 1019 if (tp->t_state & TS_ASLEEP) { 1020 tp->t_state &= ~TS_ASLEEP; 1021 wakeup((caddr_t)&tp->t_outq); 1022 } 1023 selwakeup(&tp->t_wsel); 1024 } 1025 1026 nch = ndqb(&tp->t_outq, 0); /* XXX */ 1027 if (nch) { 1028 register char *p = tp->t_outq.c_cf; 1029 1030 /* mark busy, enable tx done interrupts, & send first byte */ 1031 tp->t_state |= TS_BUSY; 1032 (void) splzs(); 1033 cs->cs_preg[1] |= ZSWR1_TIE; 1034 cs->cs_creg[1] |= ZSWR1_TIE; 1035 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]); 1036 cs->cs_zc->zc_data = *p; 1037 cs->cs_tba = p + 1; 1038 cs->cs_tbc = nch - 1; 1039 } else { 1040 /* 1041 * Nothing to send, turn off transmit done interrupts. 1042 * This is useful if something is doing polled output. 1043 */ 1044 (void) splzs(); 1045 cs->cs_preg[1] &= ~ZSWR1_TIE; 1046 cs->cs_creg[1] &= ~ZSWR1_TIE; 1047 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]); 1048 } 1049 out: 1050 splx(s); 1051 } 1052 1053 /* 1054 * Stop output, e.g., for ^S or output flush. 1055 */ 1056 void 1057 zsstop(register struct tty *tp, int flag) 1058 { 1059 register struct zs_chanstate *cs; 1060 register int s, unit = minor(tp->t_dev); 1061 struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 1062 1063 cs = &zi->zi_cs[unit & 1]; 1064 s = splzs(); 1065 if (tp->t_state & TS_BUSY) { 1066 /* 1067 * Device is transmitting; must stop it. 1068 */ 1069 cs->cs_tbc = 0; 1070 if ((tp->t_state & TS_TTSTOP) == 0) 1071 tp->t_state |= TS_FLUSH; 1072 } 1073 splx(s); 1074 } 1075 1076 /* 1077 * Set ZS tty parameters from termios. 1078 * 1079 * This routine makes use of the fact that only registers 1080 * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written. 1081 */ 1082 static int 1083 zsparam(register struct tty *tp, register struct termios *t) 1084 { 1085 int unit = minor(tp->t_dev); 1086 struct zsinfo *zi = zscd.cd_devs[unit >> 1]; 1087 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1]; 1088 register int tmp, tmp5, cflag, s; 1089 1090 /* 1091 * Because PCLK is only run at 4.9 MHz, the fastest we 1092 * can go is 51200 baud (this corresponds to TC=1). 1093 * This is somewhat unfortunate as there is no real 1094 * reason we should not be able to handle higher rates. 1095 */ 1096 tmp = t->c_ospeed; 1097 if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp)) 1098 return (EINVAL); 1099 if (tmp == 0) { 1100 /* stty 0 => drop DTR and RTS */ 1101 zs_modem(cs, 0); 1102 return (0); 1103 } 1104 tmp = BPS_TO_TCONST(PCLK / 16, tmp); 1105 if (tmp < 2) 1106 return (EINVAL); 1107 1108 cflag = t->c_cflag; 1109 tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp); 1110 tp->t_cflag = cflag; 1111 1112 /* 1113 * Block interrupts so that state will not 1114 * be altered until we are done setting it up. 1115 */ 1116 s = splzs(); 1117 cs->cs_preg[12] = tmp; 1118 cs->cs_preg[13] = tmp >> 8; 1119 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE; 1120 switch (cflag & CSIZE) { 1121 case CS5: 1122 tmp = ZSWR3_RX_5; 1123 tmp5 = ZSWR5_TX_5; 1124 break; 1125 case CS6: 1126 tmp = ZSWR3_RX_6; 1127 tmp5 = ZSWR5_TX_6; 1128 break; 1129 case CS7: 1130 tmp = ZSWR3_RX_7; 1131 tmp5 = ZSWR5_TX_7; 1132 break; 1133 case CS8: 1134 default: 1135 tmp = ZSWR3_RX_8; 1136 tmp5 = ZSWR5_TX_8; 1137 break; 1138 } 1139 1140 /* 1141 * Output hardware flow control on the chip is horrendous: if 1142 * carrier detect drops, the receiver is disabled. Hence we 1143 * can only do this when the carrier is on. 1144 */ 1145 if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD) 1146 tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE; 1147 else 1148 tmp |= ZSWR3_RX_ENABLE; 1149 cs->cs_preg[3] = tmp; 1150 cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS; 1151 1152 tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB); 1153 if ((cflag & PARODD) == 0) 1154 tmp |= ZSWR4_EVENP; 1155 if (cflag & PARENB) 1156 tmp |= ZSWR4_PARENB; 1157 cs->cs_preg[4] = tmp; 1158 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR; 1159 cs->cs_preg[10] = ZSWR10_NRZ; 1160 cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD; 1161 cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA; 1162 cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE; 1163 1164 /* 1165 * If nothing is being transmitted, set up new current values, 1166 * else mark them as pending. 1167 */ 1168 if (cs->cs_heldchange == 0) { 1169 if (cs->cs_ttyp->t_state & TS_BUSY) { 1170 cs->cs_heldtbc = cs->cs_tbc; 1171 cs->cs_tbc = 0; 1172 cs->cs_heldchange = 1; 1173 } else { 1174 bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16); 1175 zs_loadchannelregs(cs->cs_zc, cs->cs_creg); 1176 } 1177 } 1178 splx(s); 1179 return (0); 1180 } 1181 1182 /* 1183 * Raise or lower modem control (DTR/RTS) signals. If a character is 1184 * in transmission, the change is deferred. 1185 */ 1186 static void 1187 zs_modem(struct zs_chanstate *cs, int onoff) 1188 { 1189 int s, bis, and; 1190 1191 if (onoff) { 1192 bis = ZSWR5_DTR | ZSWR5_RTS; 1193 and = ~0; 1194 } else { 1195 bis = 0; 1196 and = ~(ZSWR5_DTR | ZSWR5_RTS); 1197 } 1198 s = splzs(); 1199 cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and; 1200 if (cs->cs_heldchange == 0) { 1201 if (cs->cs_ttyp->t_state & TS_BUSY) { 1202 cs->cs_heldtbc = cs->cs_tbc; 1203 cs->cs_tbc = 0; 1204 cs->cs_heldchange = 1; 1205 } else { 1206 cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and; 1207 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]); 1208 } 1209 } 1210 splx(s); 1211 } 1212 1213 /* 1214 * Write the given register set to the given zs channel in the proper order. 1215 * The channel must not be transmitting at the time. The receiver will 1216 * be disabled for the time it takes to write all the registers. 1217 */ 1218 static void 1219 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg) 1220 { 1221 int i; 1222 1223 zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */ 1224 i = zc->zc_data; /* drain fifo */ 1225 i = zc->zc_data; 1226 i = zc->zc_data; 1227 ZS_WRITE(zc, 4, reg[4]); 1228 ZS_WRITE(zc, 10, reg[10]); 1229 ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE); 1230 ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE); 1231 ZS_WRITE(zc, 1, reg[1]); 1232 ZS_WRITE(zc, 9, reg[9]); 1233 ZS_WRITE(zc, 11, reg[11]); 1234 ZS_WRITE(zc, 12, reg[12]); 1235 ZS_WRITE(zc, 13, reg[13]); 1236 ZS_WRITE(zc, 14, reg[14]); 1237 ZS_WRITE(zc, 15, reg[15]); 1238 ZS_WRITE(zc, 3, reg[3]); 1239 ZS_WRITE(zc, 5, reg[5]); 1240 } 1241 1242 #ifdef KGDB 1243 /* 1244 * Get a character from the given kgdb channel. Called at splhigh(). 1245 */ 1246 static int 1247 zs_kgdb_getc(void *arg) 1248 { 1249 register volatile struct zschan *zc = (volatile struct zschan *)arg; 1250 1251 while ((zc->zc_csr & ZSRR0_RX_READY) == 0) 1252 continue; 1253 return (zc->zc_data); 1254 } 1255 1256 /* 1257 * Put a character to the given kgdb channel. Called at splhigh(). 1258 */ 1259 static void 1260 zs_kgdb_putc(void *arg, int c) 1261 { 1262 register volatile struct zschan *zc = (volatile struct zschan *)arg; 1263 1264 while ((zc->zc_csr & ZSRR0_TX_READY) == 0) 1265 continue; 1266 zc->zc_data = c; 1267 } 1268 1269 /* 1270 * Set up for kgdb; called at boot time before configuration. 1271 * KGDB interrupts will be enabled later when zs0 is configured. 1272 */ 1273 void 1274 zs_kgdb_init() 1275 { 1276 volatile struct zsdevice *addr; 1277 volatile struct zschan *zc; 1278 int unit, zs; 1279 1280 if (major(kgdb_dev) != ZSMAJOR) 1281 return; 1282 unit = minor(kgdb_dev); 1283 /* 1284 * Unit must be 0 or 1 (zs0). 1285 */ 1286 if ((unsigned)unit >= ZS_KBD) { 1287 printf("zs_kgdb_init: bad minor dev %d\n", unit); 1288 return; 1289 } 1290 zs = unit >> 1; 1291 if ((addr = zsaddr[zs]) == NULL) 1292 addr = zsaddr[zs] = findzs(zs); 1293 unit &= 1; 1294 zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B]; 1295 zs_kgdb_savedspeed = zs_getspeed(zc); 1296 printf("zs_kgdb_init: attaching zs%d%c at %d baud\n", 1297 zs, unit + 'a', kgdb_rate); 1298 zs_reset(zc, 1, kgdb_rate); 1299 kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc); 1300 } 1301 #endif /* KGDB */ 1302