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