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