zs.c revision 1.45 1 /* $NetBSD: zs.c,v 1.45 1996/12/10 22:55:08 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 *, 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 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, cf, aux)
229 struct device *parent;
230 struct cfdata *cf;
231 void *aux;
232 {
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 void (**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 defined(SUN4M)
846 if (CPU_ISSUN4M)
847 raise(0, PIL_TTY);
848 else
849 #endif
850 ienab_bis(IE_ZSSOFT);
851 }
852 return (intflags & ZSHARD_WAS_SERVICED);
853 }
854
855 static int
856 zsrint(cs, zc)
857 register struct zs_chanstate *cs;
858 register volatile struct zschan *zc;
859 {
860 register u_int c = zc->zc_data;
861
862 ZS_DELAY();
863 if (cs->cs_conk) {
864 register struct conk_state *conk = &zsconk_state;
865
866 /*
867 * Check here for console abort function, so that we
868 * can abort even when interrupts are locking up the
869 * machine.
870 */
871 if (c == KBD_RESET) {
872 conk->conk_id = 1; /* ignore next byte */
873 conk->conk_l1 = 0;
874 } else if (conk->conk_id)
875 conk->conk_id = 0; /* stop ignoring bytes */
876 else if (c == KBD_L1)
877 conk->conk_l1 = 1; /* L1 went down */
878 else if (c == (KBD_L1|KBD_UP))
879 conk->conk_l1 = 0; /* L1 went up */
880 else if (c == KBD_A && conk->conk_l1) {
881 zsabort();
882 conk->conk_l1 = 0; /* we never see the up */
883 goto clearit; /* eat the A after L1-A */
884 }
885 }
886 #ifdef KGDB
887 if (c == FRAME_START && cs->cs_kgdb &&
888 (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
889 zskgdb(cs->cs_unit);
890 goto clearit;
891 }
892 #endif
893 /* compose receive character and status */
894 c <<= 8;
895 c |= ZS_READ(zc, 1);
896
897 /* clear receive error & interrupt condition */
898 zc->zc_csr = ZSWR0_RESET_ERRORS;
899 ZS_DELAY();
900 zc->zc_csr = ZSWR0_CLR_INTR;
901 ZS_DELAY();
902
903 return (ZRING_MAKE(ZRING_RINT, c));
904
905 clearit:
906 zc->zc_csr = ZSWR0_RESET_ERRORS;
907 ZS_DELAY();
908 zc->zc_csr = ZSWR0_CLR_INTR;
909 ZS_DELAY();
910 return (0);
911 }
912
913 static int
914 zsxint(cs, zc)
915 register struct zs_chanstate *cs;
916 register volatile struct zschan *zc;
917 {
918 register int i = cs->cs_tbc;
919
920 if (i == 0) {
921 zc->zc_csr = ZSWR0_RESET_TXINT;
922 ZS_DELAY();
923 zc->zc_csr = ZSWR0_CLR_INTR;
924 ZS_DELAY();
925 return (ZRING_MAKE(ZRING_XINT, 0));
926 }
927 cs->cs_tbc = i - 1;
928 zc->zc_data = *cs->cs_tba++;
929 ZS_DELAY();
930 zc->zc_csr = ZSWR0_CLR_INTR;
931 ZS_DELAY();
932 return (0);
933 }
934
935 static int
936 zssint(cs, zc)
937 register struct zs_chanstate *cs;
938 register volatile struct zschan *zc;
939 {
940 register u_int rr0;
941
942 rr0 = zc->zc_csr;
943 ZS_DELAY();
944 zc->zc_csr = ZSWR0_RESET_STATUS;
945 ZS_DELAY();
946 zc->zc_csr = ZSWR0_CLR_INTR;
947 ZS_DELAY();
948 /*
949 * The chip's hardware flow control is, as noted in zsreg.h,
950 * busted---if the DCD line goes low the chip shuts off the
951 * receiver (!). If we want hardware CTS flow control but do
952 * not have it, and carrier is now on, turn HFC on; if we have
953 * HFC now but carrier has gone low, turn it off.
954 */
955 if (rr0 & ZSRR0_DCD) {
956 if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
957 (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
958 cs->cs_creg[3] |= ZSWR3_HFC;
959 ZS_WRITE(zc, 3, cs->cs_creg[3]);
960 }
961 } else {
962 if (cs->cs_creg[3] & ZSWR3_HFC) {
963 cs->cs_creg[3] &= ~ZSWR3_HFC;
964 ZS_WRITE(zc, 3, cs->cs_creg[3]);
965 }
966 }
967 if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
968 /*
969 * XXX This might not be necessary. Test and
970 * delete if it isn't.
971 */
972 if (CPU_ISSUN4) {
973 while (zc->zc_csr & ZSRR0_BREAK)
974 ZS_DELAY();
975 }
976 zsabort();
977 return (0);
978 }
979 return (ZRING_MAKE(ZRING_SINT, rr0));
980 }
981
982 void
983 zsabort()
984 {
985
986 #ifdef DDB
987 Debugger();
988 #else
989 printf("stopping on keyboard abort\n");
990 callrom();
991 #endif
992 }
993
994 #ifdef KGDB
995 /*
996 * KGDB framing character received: enter kernel debugger. This probably
997 * should time out after a few seconds to avoid hanging on spurious input.
998 */
999 void
1000 zskgdb(unit)
1001 int unit;
1002 {
1003
1004 printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
1005 kgdb_connect(1);
1006 }
1007 #endif
1008
1009 /*
1010 * Print out a ring or fifo overrun error message.
1011 */
1012 static void
1013 zsoverrun(unit, ptime, what)
1014 int unit;
1015 long *ptime;
1016 char *what;
1017 {
1018
1019 if (*ptime != time.tv_sec) {
1020 *ptime = time.tv_sec;
1021 log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
1022 (unit & 1) + 'a', what);
1023 }
1024 }
1025
1026 /*
1027 * ZS software interrupt. Scan all channels for deferred interrupts.
1028 */
1029 int
1030 zssoft(arg)
1031 void *arg;
1032 {
1033 register struct zs_chanstate *cs;
1034 register volatile struct zschan *zc;
1035 register struct linesw *line;
1036 register struct tty *tp;
1037 register int get, n, c, cc, unit, s, ringmask, ringsize;
1038 int retval = 0;
1039
1040 for (cs = zslist; cs != NULL; cs = cs->cs_next) {
1041 ringmask = cs->cs_ringmask;
1042 get = cs->cs_rbget;
1043 again:
1044 n = cs->cs_rbput; /* atomic */
1045 if (get == n) /* nothing more on this line */
1046 continue;
1047 retval = 1;
1048 unit = cs->cs_unit; /* set up to handle interrupts */
1049 zc = cs->cs_zc;
1050 tp = cs->cs_ttyp;
1051 line = &linesw[tp->t_line];
1052 /*
1053 * Compute the number of interrupts in the receive ring.
1054 * If the count is overlarge, we lost some events, and
1055 * must advance to the first valid one. It may get
1056 * overwritten if more data are arriving, but this is
1057 * too expensive to check and gains nothing (we already
1058 * lost out; all we can do at this point is trade one
1059 * kind of loss for another).
1060 */
1061 ringsize = ringmask + 1;
1062 n -= get;
1063 if (n > ringsize) {
1064 zsoverrun(unit, &cs->cs_rotime, "ring");
1065 get += n - ringsize;
1066 n = ringsize;
1067 }
1068 while (--n >= 0) {
1069 /* race to keep ahead of incoming interrupts */
1070 c = cs->cs_rbuf[get++ & ringmask];
1071 switch (ZRING_TYPE(c)) {
1072
1073 case ZRING_RINT:
1074 c = ZRING_VALUE(c);
1075 if (c & ZSRR1_DO)
1076 zsoverrun(unit, &cs->cs_fotime, "fifo");
1077 cc = c >> 8;
1078 if (c & ZSRR1_FE)
1079 cc |= TTY_FE;
1080 if (c & ZSRR1_PE)
1081 cc |= TTY_PE;
1082 /*
1083 * this should be done through
1084 * bstreams XXX gag choke
1085 */
1086 if (unit == ZS_KBD)
1087 kbd_rint(cc);
1088 else if (unit == ZS_MOUSE)
1089 ms_rint(cc);
1090 else
1091 line->l_rint(cc, tp);
1092 break;
1093
1094 case ZRING_XINT:
1095 /*
1096 * Transmit done: change registers and resume,
1097 * or clear BUSY.
1098 */
1099 if (cs->cs_heldchange) {
1100 s = splzs();
1101 c = zc->zc_csr;
1102 ZS_DELAY();
1103 if ((c & ZSRR0_DCD) == 0)
1104 cs->cs_preg[3] &= ~ZSWR3_HFC;
1105 bcopy((caddr_t)cs->cs_preg,
1106 (caddr_t)cs->cs_creg, 16);
1107 zs_loadchannelregs(zc, cs->cs_creg);
1108 splx(s);
1109 cs->cs_heldchange = 0;
1110 if (cs->cs_heldtbc &&
1111 (tp->t_state & TS_TTSTOP) == 0) {
1112 cs->cs_tbc = cs->cs_heldtbc - 1;
1113 zc->zc_data = *cs->cs_tba++;
1114 ZS_DELAY();
1115 goto again;
1116 }
1117 }
1118 tp->t_state &= ~TS_BUSY;
1119 if (tp->t_state & TS_FLUSH)
1120 tp->t_state &= ~TS_FLUSH;
1121 else
1122 ndflush(&tp->t_outq,
1123 cs->cs_tba - (caddr_t)tp->t_outq.c_cf);
1124 line->l_start(tp);
1125 break;
1126
1127 case ZRING_SINT:
1128 /*
1129 * Status line change. HFC bit is run in
1130 * hardware interrupt, to avoid locking
1131 * at splzs here.
1132 */
1133 c = ZRING_VALUE(c);
1134 if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
1135 cc = (c & ZSRR0_DCD) != 0;
1136 if (line->l_modem(tp, cc) == 0)
1137 zs_modem(cs, cc);
1138 }
1139 cs->cs_rr0 = c;
1140 break;
1141
1142 default:
1143 log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
1144 unit >> 1, (unit & 1) + 'a', c);
1145 break;
1146 }
1147 }
1148 cs->cs_rbget = get;
1149 goto again;
1150 }
1151 return (retval);
1152 }
1153
1154 int
1155 zsioctl(dev, cmd, data, flag, p)
1156 dev_t dev;
1157 u_long cmd;
1158 caddr_t data;
1159 int flag;
1160 struct proc *p;
1161 {
1162 int unit = minor(dev);
1163 struct zs_softc *sc = zs_cd.cd_devs[unit >> 1];
1164 register struct zs_chanstate *cs = &sc->sc_cs[unit & 1];
1165 register struct tty *tp = cs->cs_ttyp;
1166 register int error, s;
1167
1168 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
1169 if (error >= 0)
1170 return (error);
1171 error = ttioctl(tp, cmd, data, flag, p);
1172 if (error >= 0)
1173 return (error);
1174
1175 switch (cmd) {
1176 case TIOCSBRK:
1177 s = splzs();
1178 cs->cs_preg[5] |= ZSWR5_BREAK;
1179 cs->cs_creg[5] |= ZSWR5_BREAK;
1180 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1181 splx(s);
1182 break;
1183 case TIOCCBRK:
1184 s = splzs();
1185 cs->cs_preg[5] &= ~ZSWR5_BREAK;
1186 cs->cs_creg[5] &= ~ZSWR5_BREAK;
1187 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1188 splx(s);
1189 break;
1190 case TIOCGFLAGS: {
1191 int bits = 0;
1192
1193 if (cs->cs_softcar)
1194 bits |= TIOCFLAG_SOFTCAR;
1195 if (cs->cs_creg[15] & ZSWR15_DCD_IE)
1196 bits |= TIOCFLAG_CLOCAL;
1197 if (cs->cs_creg[3] & ZSWR3_HFC)
1198 bits |= TIOCFLAG_CRTSCTS;
1199 *(int *)data = bits;
1200 break;
1201 }
1202 case TIOCSFLAGS: {
1203 int userbits;
1204
1205 error = suser(p->p_ucred, &p->p_acflag);
1206 if (error != 0)
1207 return (EPERM);
1208
1209 userbits = *(int *)data;
1210
1211 /*
1212 * can have `local' or `softcar', and `rtscts' or `mdmbuf'
1213 # defaulting to software flow control.
1214 */
1215 if (userbits & TIOCFLAG_SOFTCAR && userbits & TIOCFLAG_CLOCAL)
1216 return(EINVAL);
1217 if (userbits & TIOCFLAG_MDMBUF) /* don't support this (yet?) */
1218 return(ENXIO);
1219
1220 s = splzs();
1221 if ((userbits & TIOCFLAG_SOFTCAR) || cs->cs_consio) {
1222 cs->cs_softcar = 1; /* turn on softcar */
1223 cs->cs_preg[15] &= ~ZSWR15_DCD_IE; /* turn off dcd */
1224 cs->cs_creg[15] &= ~ZSWR15_DCD_IE;
1225 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1226 } else if (userbits & TIOCFLAG_CLOCAL) {
1227 cs->cs_softcar = 0; /* turn off softcar */
1228 cs->cs_preg[15] |= ZSWR15_DCD_IE; /* turn on dcd */
1229 cs->cs_creg[15] |= ZSWR15_DCD_IE;
1230 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1231 tp->t_termios.c_cflag |= CLOCAL;
1232 }
1233 if (userbits & TIOCFLAG_CRTSCTS) {
1234 cs->cs_preg[15] |= ZSWR15_CTS_IE;
1235 cs->cs_creg[15] |= ZSWR15_CTS_IE;
1236 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1237 cs->cs_preg[3] |= ZSWR3_HFC;
1238 cs->cs_creg[3] |= ZSWR3_HFC;
1239 ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
1240 tp->t_termios.c_cflag |= CRTSCTS;
1241 } else {
1242 /* no mdmbuf, so we must want software flow control */
1243 cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
1244 cs->cs_creg[15] &= ~ZSWR15_CTS_IE;
1245 ZS_WRITE(cs->cs_zc, 15, cs->cs_creg[15]);
1246 cs->cs_preg[3] &= ~ZSWR3_HFC;
1247 cs->cs_creg[3] &= ~ZSWR3_HFC;
1248 ZS_WRITE(cs->cs_zc, 3, cs->cs_creg[3]);
1249 tp->t_termios.c_cflag &= ~CRTSCTS;
1250 }
1251 splx(s);
1252 break;
1253 }
1254 case TIOCSDTR:
1255 zs_modem(cs, 1);
1256 break;
1257 case TIOCCDTR:
1258 zs_modem(cs, 0);
1259 break;
1260 case TIOCMSET:
1261 case TIOCMGET:
1262 case TIOCMBIS:
1263 case TIOCMBIC:
1264 default:
1265 return (ENOTTY);
1266 }
1267 return (0);
1268 }
1269
1270 /*
1271 * Start or restart transmission.
1272 */
1273 static void
1274 zsstart(tp)
1275 register struct tty *tp;
1276 {
1277 register struct zs_chanstate *cs;
1278 register int s, nch;
1279 int unit = minor(tp->t_dev);
1280 struct zs_softc *sc = zs_cd.cd_devs[unit >> 1];
1281
1282 cs = &sc->sc_cs[unit & 1];
1283 s = spltty();
1284
1285 /*
1286 * If currently active or delaying, no need to do anything.
1287 */
1288 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
1289 goto out;
1290
1291 /*
1292 * If there are sleepers, and output has drained below low
1293 * water mark, awaken.
1294 */
1295 if (tp->t_outq.c_cc <= tp->t_lowat) {
1296 if (tp->t_state & TS_ASLEEP) {
1297 tp->t_state &= ~TS_ASLEEP;
1298 wakeup((caddr_t)&tp->t_outq);
1299 }
1300 selwakeup(&tp->t_wsel);
1301 }
1302
1303 nch = ndqb(&tp->t_outq, 0); /* XXX */
1304 if (nch) {
1305 register char *p = tp->t_outq.c_cf;
1306
1307 /* mark busy, enable tx done interrupts, & send first byte */
1308 tp->t_state |= TS_BUSY;
1309 (void) splzs();
1310 cs->cs_preg[1] |= ZSWR1_TIE;
1311 cs->cs_creg[1] |= ZSWR1_TIE;
1312 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1313 cs->cs_zc->zc_data = *p;
1314 ZS_DELAY();
1315 cs->cs_tba = p + 1;
1316 cs->cs_tbc = nch - 1;
1317 } else {
1318 /*
1319 * Nothing to send, turn off transmit done interrupts.
1320 * This is useful if something is doing polled output.
1321 */
1322 (void) splzs();
1323 cs->cs_preg[1] &= ~ZSWR1_TIE;
1324 cs->cs_creg[1] &= ~ZSWR1_TIE;
1325 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1326 }
1327 out:
1328 splx(s);
1329 }
1330
1331 /*
1332 * Stop output, e.g., for ^S or output flush.
1333 */
1334 void
1335 zsstop(tp, flag)
1336 register struct tty *tp;
1337 int flag;
1338 {
1339 register struct zs_chanstate *cs;
1340 register int s, unit = minor(tp->t_dev);
1341 struct zs_softc *sc = zs_cd.cd_devs[unit >> 1];
1342
1343 cs = &sc->sc_cs[unit & 1];
1344 s = splzs();
1345 if (tp->t_state & TS_BUSY) {
1346 /*
1347 * Device is transmitting; must stop it.
1348 */
1349 cs->cs_tbc = 0;
1350 if ((tp->t_state & TS_TTSTOP) == 0)
1351 tp->t_state |= TS_FLUSH;
1352 }
1353 splx(s);
1354 }
1355
1356 /*
1357 * Set ZS tty parameters from termios.
1358 *
1359 * This routine makes use of the fact that only registers
1360 * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
1361 */
1362 static int
1363 zsparam(tp, t)
1364 register struct tty *tp;
1365 register struct termios *t;
1366 {
1367 int unit = minor(tp->t_dev);
1368 struct zs_softc *sc = zs_cd.cd_devs[unit >> 1];
1369 register struct zs_chanstate *cs = &sc->sc_cs[unit & 1];
1370 register int tmp, tmp5, cflag, s;
1371
1372 /*
1373 * Because PCLK is only run at 4.9 MHz, the fastest we
1374 * can go is 51200 baud (this corresponds to TC=1).
1375 * This is somewhat unfortunate as there is no real
1376 * reason we should not be able to handle higher rates.
1377 */
1378 tmp = t->c_ospeed;
1379 if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
1380 return (EINVAL);
1381 if (tmp == 0) {
1382 /* stty 0 => drop DTR and RTS */
1383 zs_modem(cs, 0);
1384 return (0);
1385 }
1386 tmp = BPS_TO_TCONST(PCLK / 16, tmp);
1387 if (tmp < 2)
1388 return (EINVAL);
1389
1390 cflag = t->c_cflag;
1391 tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
1392 tp->t_cflag = cflag;
1393
1394 /*
1395 * Block interrupts so that state will not
1396 * be altered until we are done setting it up.
1397 */
1398 s = splzs();
1399 cs->cs_preg[12] = tmp;
1400 cs->cs_preg[13] = tmp >> 8;
1401 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
1402 switch (cflag & CSIZE) {
1403 case CS5:
1404 tmp = ZSWR3_RX_5;
1405 tmp5 = ZSWR5_TX_5;
1406 break;
1407 case CS6:
1408 tmp = ZSWR3_RX_6;
1409 tmp5 = ZSWR5_TX_6;
1410 break;
1411 case CS7:
1412 tmp = ZSWR3_RX_7;
1413 tmp5 = ZSWR5_TX_7;
1414 break;
1415 case CS8:
1416 default:
1417 tmp = ZSWR3_RX_8;
1418 tmp5 = ZSWR5_TX_8;
1419 break;
1420 }
1421
1422 /*
1423 * Output hardware flow control on the chip is horrendous: if
1424 * carrier detect drops, the receiver is disabled. Hence we
1425 * can only do this when the carrier is on.
1426 */
1427 tmp |= ZSWR3_RX_ENABLE;
1428 if (cflag & CCTS_OFLOW) {
1429 if (cs->cs_zc->zc_csr & ZSRR0_DCD)
1430 tmp |= ZSWR3_HFC;
1431 ZS_DELAY();
1432 }
1433 cs->cs_preg[3] = tmp;
1434 cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1435
1436 tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
1437 if ((cflag & PARODD) == 0)
1438 tmp |= ZSWR4_EVENP;
1439 if (cflag & PARENB)
1440 tmp |= ZSWR4_PARENB;
1441 cs->cs_preg[4] = tmp;
1442 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
1443 cs->cs_preg[10] = ZSWR10_NRZ;
1444 cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
1445 cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
1446 cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
1447
1448 /*
1449 * If nothing is being transmitted, set up new current values,
1450 * else mark them as pending.
1451 */
1452 if (cs->cs_heldchange == 0) {
1453 if (cs->cs_ttyp->t_state & TS_BUSY) {
1454 cs->cs_heldtbc = cs->cs_tbc;
1455 cs->cs_tbc = 0;
1456 cs->cs_heldchange = 1;
1457 } else {
1458 bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1459 zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1460 }
1461 }
1462 splx(s);
1463 return (0);
1464 }
1465
1466 /*
1467 * Raise or lower modem control (DTR/RTS) signals. If a character is
1468 * in transmission, the change is deferred.
1469 */
1470 static void
1471 zs_modem(cs, onoff)
1472 struct zs_chanstate *cs;
1473 int onoff;
1474 {
1475 int s, bis, and;
1476
1477 if (onoff) {
1478 bis = ZSWR5_DTR | ZSWR5_RTS;
1479 and = ~0;
1480 } else {
1481 bis = 0;
1482 and = ~(ZSWR5_DTR | ZSWR5_RTS);
1483 }
1484 s = splzs();
1485 cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
1486 if (cs->cs_heldchange == 0) {
1487 if (cs->cs_ttyp->t_state & TS_BUSY) {
1488 cs->cs_heldtbc = cs->cs_tbc;
1489 cs->cs_tbc = 0;
1490 cs->cs_heldchange = 1;
1491 } else {
1492 cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
1493 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1494 }
1495 }
1496 splx(s);
1497 }
1498
1499 /*
1500 * Write the given register set to the given zs channel in the proper order.
1501 * The channel must not be transmitting at the time. The receiver will
1502 * be disabled for the time it takes to write all the registers.
1503 */
1504 static void
1505 zs_loadchannelregs(zc, reg)
1506 volatile struct zschan *zc;
1507 u_char *reg;
1508 {
1509 int i;
1510
1511 zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */
1512 ZS_DELAY();
1513 i = zc->zc_data; /* drain fifo */
1514 ZS_DELAY();
1515 i = zc->zc_data;
1516 ZS_DELAY();
1517 i = zc->zc_data;
1518 ZS_DELAY();
1519 ZS_WRITE(zc, 4, reg[4]);
1520 ZS_WRITE(zc, 10, reg[10]);
1521 ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1522 ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1523 ZS_WRITE(zc, 1, reg[1]);
1524 ZS_WRITE(zc, 9, reg[9]);
1525 ZS_WRITE(zc, 11, reg[11]);
1526 ZS_WRITE(zc, 12, reg[12]);
1527 ZS_WRITE(zc, 13, reg[13]);
1528 ZS_WRITE(zc, 14, reg[14]);
1529 ZS_WRITE(zc, 15, reg[15]);
1530 ZS_WRITE(zc, 3, reg[3]);
1531 ZS_WRITE(zc, 5, reg[5]);
1532 }
1533
1534 #ifdef KGDB
1535 /*
1536 * Get a character from the given kgdb channel. Called at splhigh().
1537 */
1538 static int
1539 zs_kgdb_getc(arg)
1540 void *arg;
1541 {
1542 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1543 u_char c;
1544
1545 while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
1546 ZS_DELAY();
1547 c = zc->zc_data;
1548 ZS_DELAY();
1549 return c;
1550 }
1551
1552 /*
1553 * Put a character to the given kgdb channel. Called at splhigh().
1554 */
1555 static void
1556 zs_kgdb_putc(arg, c)
1557 void *arg;
1558 int c;
1559 {
1560 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1561
1562 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
1563 ZS_DELAY();
1564 zc->zc_data = c;
1565 ZS_DELAY();
1566 }
1567
1568 /*
1569 * Set up for kgdb; called at boot time before configuration.
1570 * KGDB interrupts will be enabled later when zs0 is configured.
1571 */
1572 void
1573 zs_kgdb_init()
1574 {
1575 volatile struct zsdevice *addr;
1576 volatile struct zschan *zc;
1577 int unit, zs;
1578
1579 if (major(kgdb_dev) != ZSMAJOR)
1580 return;
1581 unit = minor(kgdb_dev);
1582 /*
1583 * Unit must be 0 or 1 (zs0).
1584 */
1585 if ((unsigned)unit >= ZS_KBD) {
1586 printf("zs_kgdb_init: bad minor dev %d\n", unit);
1587 return;
1588 }
1589 zs = unit >> 1;
1590 if ((addr = zsaddr[zs]) == NULL)
1591 addr = zsaddr[zs] = (volatile struct zsdevice *)findzs(zs);
1592 unit &= 1;
1593 zc = unit == 0 ? &addr->zs_chan[ZS_CHAN_A] : &addr->zs_chan[ZS_CHAN_B];
1594 zs_kgdb_savedspeed = zs_getspeed(zc);
1595 printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
1596 zs, unit + 'a', kgdb_rate);
1597 zs_reset(zc, 1, kgdb_rate);
1598 kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
1599 }
1600 #endif /* KGDB */
1601