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