z8530tty.c revision 1.2 1 /* $NetBSD: z8530tty.c,v 1.2 1996/01/30 22:35:11 gwr Exp $ */
2
3 /*
4 * Copyright (c) 1994 Gordon W. Ross
5 * Copyright (c) 1992, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This software was developed by the Computer Systems Engineering group
9 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 * contributed to Berkeley.
11 *
12 * All advertising materials mentioning features or use of this software
13 * must display the following acknowledgement:
14 * This product includes software developed by the University of
15 * California, Lawrence Berkeley Laboratory.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 * 3. All advertising materials mentioning features or use of this software
26 * must display the following acknowledgement:
27 * This product includes software developed by the University of
28 * California, Berkeley and its contributors.
29 * 4. Neither the name of the University nor the names of its contributors
30 * may be used to endorse or promote products derived from this software
31 * without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * SUCH DAMAGE.
44 *
45 * @(#)zs.c 8.1 (Berkeley) 7/19/93
46 */
47
48 /*
49 * Zilog Z8530 Dual UART driver (tty interface)
50 *
51 * This is the "slave" driver that will be attached to
52 * the "zsc" driver for plain "tty" async. serial lines.
53 */
54
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/proc.h>
58 #include <sys/device.h>
59 #include <sys/conf.h>
60 #include <sys/file.h>
61 #include <sys/ioctl.h>
62 #include <sys/tty.h>
63 #include <sys/time.h>
64 #include <sys/kernel.h>
65 #include <sys/syslog.h>
66
67 #include <dev/ic/z8530reg.h>
68 #include <machine/z8530var.h>
69
70 #ifdef KGDB
71 extern int zs_check_kgdb();
72 #endif
73
74 /*
75 * Allow the MD var.h to override the default CFLAG so that
76 * console messages during boot come out with correct parity.
77 */
78 #ifndef ZSTTY_DEF_CFLAG
79 #define ZSTTY_DEF_CFLAG TTYDEF_CFLAG
80 #endif
81
82 /*
83 * How many input characters we can buffer.
84 * The port-specific var.h may override this.
85 * Note: must be a power of two!
86 */
87 #ifndef ZSTTY_RING_SIZE
88 #define ZSTTY_RING_SIZE 1024
89 #endif
90 #define ZSTTY_RING_MASK (ZSTTY_RING_SIZE-1)
91
92 struct zstty_softc {
93 struct device zst_dev; /* required first: base device */
94 struct tty *zst_tty;
95 struct zs_chanstate *zst_cs;
96
97 int zst_hwflags; /* see z8530var.h */
98 int zst_swflags; /* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
99
100 /* Flags to communicate with zstty_softint() */
101 volatile int zst_intr_flags;
102 #define INTR_RX_OVERRUN 1
103 #define INTR_TX_EMPTY 2
104 #define INTR_ST_CHECK 4
105
106 /*
107 * The transmit byte count and address are used for pseudo-DMA
108 * output in the hardware interrupt code. PDMA can be suspended
109 * to get pending changes done; heldtbc is used for this. It can
110 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
111 */
112 int zst_tbc; /* transmit byte count */
113 caddr_t zst_tba; /* transmit buffer address */
114 int zst_heldtbc; /* held tbc while xmission stopped */
115
116 /*
117 * Printing an overrun error message often takes long enough to
118 * cause another overrun, so we only print one per second.
119 */
120 long zst_rotime; /* time of last ring overrun */
121 long zst_fotime; /* time of last fifo overrun */
122
123 /*
124 * The receive ring buffer.
125 */
126 u_int zst_rbget; /* ring buffer `get' index */
127 volatile u_int zst_rbput; /* ring buffer `put' index */
128 u_short zst_rbuf[ZSTTY_RING_SIZE]; /* rr1, data pairs */
129 };
130
131
132 /* Definition of the driver for autoconfig. */
133 static int zstty_match(struct device *, void *, void *);
134 static void zstty_attach(struct device *, struct device *, void *);
135
136 struct cfdriver zsttycd = {
137 NULL, "zstty", zstty_match, zstty_attach,
138 DV_TTY, sizeof(struct zstty_softc), NULL,
139 };
140
141 struct zsops zsops_tty;
142
143 /* Routines called from other code. */
144 cdev_decl(zs); /* open, close, read, write, ioctl, stop, ... */
145
146 static void zsstart(struct tty *);
147 static int zsparam(struct tty *, struct termios *);
148 static void zs_modem(struct zstty_softc *zst, int onoff);
149
150 /*
151 * zstty_match: how is this zs channel configured?
152 */
153 int
154 zstty_match(parent, match, aux)
155 struct device *parent;
156 void *match, *aux;
157 {
158 struct cfdata *cf = match;
159 struct zsc_attach_args *args = aux;
160
161 /* Exact match is better than wildcard. */
162 if (cf->cf_loc[0] == args->channel)
163 return 2;
164
165 /* This driver accepts wildcard. */
166 if (cf->cf_loc[0] == -1)
167 return 1;
168
169 return 0;
170 }
171
172 void
173 zstty_attach(parent, self, aux)
174 struct device *parent, *self;
175 void *aux;
176
177 {
178 struct zsc_softc *zsc = (void *) parent;
179 struct zstty_softc *zst = (void *) self;
180 struct zsc_attach_args *args = aux;
181 struct zs_chanstate *cs;
182 struct cfdata *cf;
183 struct tty *tp;
184 int channel, tty_unit;
185 dev_t dev;
186
187 cf = zst->zst_dev.dv_cfdata;
188 tty_unit = cf->cf_unit;
189 channel = args->channel;
190 cs = &zsc->zsc_cs[channel];
191 cs->cs_private = zst;
192 cs->cs_ops = &zsops_tty;
193
194 zst->zst_cs = cs;
195 zst->zst_swflags = cf->cf_flags; /* softcar, etc. */
196 zst->zst_hwflags = args->hwflags;
197 dev = makedev(ZSTTY_MAJOR, tty_unit);
198
199 if (zst->zst_swflags)
200 printf(" flags 0x%x", zst->zst_swflags);
201
202 if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE)
203 printf(" (console)");
204 else {
205 #ifdef KGDB
206 /*
207 * Allow kgdb to "take over" this port. If this port is
208 * NOT the kgdb port, zs_check_kgdb() will return zero.
209 * If it IS the kgdb port, it will print "kgdb,...\n"
210 * and then return non-zero.
211 */
212 if (zs_check_kgdb(cs, dev)) {
213 /*
214 * This is the kgdb port (exclusive use)
215 * so skip the normal attach code.
216 */
217 return;
218 }
219 #endif
220 }
221 printf("\n");
222
223 tp = zst->zst_tty = ttymalloc();
224 tp->t_dev = dev;
225 tp->t_oproc = zsstart;
226 tp->t_param = zsparam;
227 tp->t_sc = zst; /* XXX - Quick access! */
228
229 /*
230 * Hardware init
231 */
232 if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE) {
233 /* This unit is the console. */
234 zst->zst_swflags |= TIOCFLAG_SOFTCAR;
235 /* Call _param so interrupts get enabled. */
236 cs->cs_defspeed = zs_getspeed(cs);
237 tp->t_ispeed = cs->cs_defspeed;
238 tp->t_ospeed = cs->cs_defspeed;
239 tp->t_cflag = ZSTTY_DEF_CFLAG;
240 (void) zsparam(tp, &tp->t_termios);
241 } else {
242 /* Not the console; may need reset. */
243 int reset, s;
244 reset = (channel == 0) ?
245 ZSWR9_A_RESET : ZSWR9_B_RESET;
246 s = splzs();
247 zs_write_reg(cs, 9, reset);
248 splx(s);
249 }
250
251 /*
252 * Initialize state of modem control lines (DTR).
253 * If softcar is set, turn on DTR now and leave it.
254 * otherwise, turn off DTR now, and raise in open.
255 * (Keeps modem from answering too early.)
256 */
257 zs_modem(zst, (zst->zst_swflags & TIOCFLAG_SOFTCAR) ? 1 : 0);
258 }
259
260
261 /*
262 * Return pointer to our tty.
263 */
264 struct tty *
265 zstty(dev)
266 dev_t dev;
267 {
268 struct zstty_softc *zst;
269 int unit = minor(dev);
270
271 #ifdef DIAGNOSTIC
272 if (unit >= zsttycd.cd_ndevs)
273 panic("zstty");
274 #endif
275 zst = zsttycd.cd_devs[unit];
276 return (zst->zst_tty);
277 }
278
279
280 /*
281 * Open a zs serial (tty) port.
282 */
283 int
284 zsopen(dev, flags, mode, p)
285 dev_t dev;
286 int flags;
287 int mode;
288 struct proc *p;
289 {
290 register struct tty *tp;
291 register struct zs_chanstate *cs;
292 struct zstty_softc *zst;
293 int error, s, unit;
294
295 unit = minor(dev);
296 if (unit >= zsttycd.cd_ndevs)
297 return (ENXIO);
298 zst = zsttycd.cd_devs[unit];
299 if (zst == NULL)
300 return (ENXIO);
301 tp = zst->zst_tty;
302 cs = zst->zst_cs;
303
304 /* If KGDB took the line, then tp==NULL */
305 if (tp == NULL)
306 return (EBUSY);
307
308 /* It's simpler to do this up here. */
309 if (((tp->t_state & (TS_ISOPEN | TS_XCLUDE))
310 == (TS_ISOPEN | TS_XCLUDE))
311 && (p->p_ucred->cr_uid != 0) )
312 {
313 return (EBUSY);
314 }
315
316 s = spltty();
317
318 if ((tp->t_state & TS_ISOPEN) == 0) {
319 /* First open. */
320 ttychars(tp);
321 tp->t_iflag = TTYDEF_IFLAG;
322 tp->t_oflag = TTYDEF_OFLAG;
323 tp->t_cflag = ZSTTY_DEF_CFLAG;
324 if (zst->zst_swflags & TIOCFLAG_CLOCAL)
325 tp->t_cflag |= CLOCAL;
326 if (zst->zst_swflags & TIOCFLAG_CRTSCTS)
327 tp->t_cflag |= CRTSCTS;
328 if (zst->zst_swflags & TIOCFLAG_MDMBUF)
329 tp->t_cflag |= MDMBUF;
330 tp->t_lflag = TTYDEF_LFLAG;
331 tp->t_ispeed = tp->t_ospeed = cs->cs_defspeed;
332 (void) zsparam(tp, &tp->t_termios);
333 ttsetwater(tp);
334 /* Flush any pending input. */
335 zst->zst_rbget = zst->zst_rbput;
336 zs_iflush(cs); /* XXX */
337 /* Turn on DTR */
338 zs_modem(zst, 1);
339 if (zst->zst_swflags & TIOCFLAG_SOFTCAR) {
340 tp->t_state |= TS_CARR_ON;
341 }
342 }
343 error = 0;
344
345 /* Wait for carrier. */
346 for (;;) {
347 register int rr0;
348
349 /* Might never get status intr if carrier already on. */
350 rr0 = zs_read_csr(cs);
351 if (rr0 & ZSRR0_DCD) {
352 tp->t_state |= TS_CARR_ON;
353 break;
354 }
355
356 if ((tp->t_state & TS_CARR_ON) ||
357 (tp->t_cflag & CLOCAL) ||
358 (flags & O_NONBLOCK) )
359 {
360 break;
361 }
362
363 tp->t_state |= TS_WOPEN;
364 error = ttysleep(tp, (caddr_t)&tp->t_rawq,
365 TTIPRI | PCATCH, ttopen, 0);
366 if (error) {
367 if ((tp->t_state & TS_ISOPEN) == 0) {
368 /* Never get here with softcar */
369 zs_modem(zst, 0);
370 tp->t_state &= ~TS_WOPEN;
371 ttwakeup(tp);
372 }
373 break;
374 }
375 }
376
377 splx(s);
378
379 if (error == 0)
380 error = linesw[tp->t_line].l_open(dev, tp);
381
382 return (error);
383 }
384
385 /*
386 * Close a zs serial port.
387 */
388 int
389 zsclose(dev, flags, mode, p)
390 dev_t dev;
391 int flags;
392 int mode;
393 struct proc *p;
394 {
395 struct zstty_softc *zst;
396 register struct zs_chanstate *cs;
397 register struct tty *tp;
398 struct zsinfo *zi;
399 int hup, s;
400
401 zst = zsttycd.cd_devs[minor(dev)];
402 cs = zst->zst_cs;
403 tp = zst->zst_tty;
404
405 /* XXX This is for cons.c. */
406 if ((tp->t_state & TS_ISOPEN) == 0)
407 return 0;
408
409 (*linesw[tp->t_line].l_close)(tp, flags);
410 hup = tp->t_cflag & HUPCL;
411 if (zst->zst_swflags & TIOCFLAG_SOFTCAR)
412 hup = 0;
413 if (hup) {
414 zs_modem(zst, 0);
415 /* hold low for 1 second */
416 (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
417 }
418 if (cs->cs_creg[5] & ZSWR5_BREAK) {
419 zs_break(cs, 0);
420 }
421 /* XXX - turn off interrupts? */
422
423 ttyclose(tp);
424 return (0);
425 }
426
427 /*
428 * Read/write zs serial port.
429 */
430 int
431 zsread(dev, uio, flags)
432 dev_t dev;
433 struct uio *uio;
434 int flags;
435 {
436 register struct zstty_softc *zst;
437 register struct tty *tp;
438
439 zst = zsttycd.cd_devs[minor(dev)];
440 tp = zst->zst_tty;
441 return (linesw[tp->t_line].l_read(tp, uio, flags));
442 }
443
444 int
445 zswrite(dev, uio, flags)
446 dev_t dev;
447 struct uio *uio;
448 int flags;
449 {
450 register struct zstty_softc *zst;
451 register struct tty *tp;
452
453 zst = zsttycd.cd_devs[minor(dev)];
454 tp = zst->zst_tty;
455 return (linesw[tp->t_line].l_write(tp, uio, flags));
456 }
457
458 #define TIOCFLAG_ALL (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | \
459 TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF )
460
461 int
462 zsioctl(dev, cmd, data, flag, p)
463 dev_t dev;
464 u_long cmd;
465 caddr_t data;
466 int flag;
467 struct proc *p;
468 {
469 register struct zstty_softc *zst;
470 register struct zs_chanstate *cs;
471 register struct tty *tp;
472 register int error, tmp;
473
474 zst = zsttycd.cd_devs[minor(dev)];
475 cs = zst->zst_cs;
476 tp = zst->zst_tty;
477
478 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
479 if (error >= 0)
480 return (error);
481 error = ttioctl(tp, cmd, data, flag, p);
482 if (error >= 0)
483 return (error);
484
485 switch (cmd) {
486
487 case TIOCSBRK:
488 zs_break(cs, 1);
489 break;
490
491 case TIOCCBRK:
492 zs_break(cs, 0);
493 break;
494
495 case TIOCGFLAGS:
496 *(int *)data = zst->zst_swflags;
497 break;
498
499 case TIOCSFLAGS:
500 error = suser(p->p_ucred, &p->p_acflag);
501 if (error != 0)
502 return (EPERM);
503 tmp = *(int *)data;
504 /* Check for random bits... */
505 if (tmp & ~TIOCFLAG_ALL)
506 return(EINVAL);
507 /* Silently enforce softcar on the console. */
508 if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE)
509 tmp |= TIOCFLAG_SOFTCAR;
510 /* These flags take effect during open. */
511 zst->zst_swflags = tmp;
512 break;
513
514 case TIOCSDTR:
515 zs_modem(zst, 1);
516 break;
517
518 case TIOCCDTR:
519 zs_modem(zst, 0);
520 break;
521
522 case TIOCMSET:
523 case TIOCMBIS:
524 case TIOCMBIC:
525 case TIOCMGET:
526 default:
527 return (ENOTTY);
528 }
529 return (0);
530 }
531
532 /*
533 * Start or restart transmission.
534 */
535 static void
536 zsstart(tp)
537 register struct tty *tp;
538 {
539 register struct zstty_softc *zst;
540 register struct zs_chanstate *cs;
541 register int s, nch;
542
543 zst = tp->t_sc;
544 cs = zst->zst_cs;
545
546 s = spltty();
547
548 /*
549 * If currently active or delaying, no need to do anything.
550 */
551 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
552 goto out;
553
554 /*
555 * If there are sleepers, and output has drained below low
556 * water mark, awaken.
557 */
558 if (tp->t_outq.c_cc <= tp->t_lowat) {
559 if (tp->t_state & TS_ASLEEP) {
560 tp->t_state &= ~TS_ASLEEP;
561 wakeup((caddr_t)&tp->t_outq);
562 }
563 selwakeup(&tp->t_wsel);
564 }
565
566 nch = ndqb(&tp->t_outq, 0); /* XXX */
567 if (nch) {
568 register char *p = tp->t_outq.c_cf;
569
570 /* mark busy, enable tx done interrupts, & send first byte */
571 tp->t_state |= TS_BUSY;
572 (void) splzs();
573
574 cs->cs_preg[1] |= ZSWR1_TIE;
575 cs->cs_creg[1] |= ZSWR1_TIE;
576 zs_write_reg(cs, 1, cs->cs_creg[1]);
577 zs_write_data(cs, *p);
578 zst->zst_tba = p + 1;
579 zst->zst_tbc = nch - 1;
580 } else {
581 /*
582 * Nothing to send, turn off transmit done interrupts.
583 * This is useful if something is doing polled output.
584 */
585 (void) splzs();
586 cs->cs_preg[1] &= ~ZSWR1_TIE;
587 cs->cs_creg[1] &= ~ZSWR1_TIE;
588 zs_write_reg(cs, 1, cs->cs_creg[1]);
589 }
590 out:
591 splx(s);
592 }
593
594 /*
595 * Stop output, e.g., for ^S or output flush.
596 */
597 int
598 zsstop(tp, flag)
599 struct tty *tp;
600 int flag;
601 {
602 register struct zstty_softc *zst;
603 register struct zs_chanstate *cs;
604 register int s;
605
606 zst = tp->t_sc;
607 cs = zst->zst_cs;
608
609 s = splzs();
610 if (tp->t_state & TS_BUSY) {
611 /*
612 * Device is transmitting; must stop it.
613 */
614 zst->zst_tbc = 0;
615 if ((tp->t_state & TS_TTSTOP) == 0)
616 tp->t_state |= TS_FLUSH;
617 }
618 splx(s);
619 return (0);
620 }
621
622 /*
623 * Set ZS tty parameters from termios.
624 * XXX - Should just copy the whole termios after
625 * making sure all the changes could be done.
626 * XXX - Only whack the UART when params change...
627 */
628 static int
629 zsparam(tp, t)
630 register struct tty *tp;
631 register struct termios *t;
632 {
633 register struct zstty_softc *zst;
634 register struct zs_chanstate *cs;
635 register int s, bps, cflag, tconst;
636 u_char tmp3, tmp4, tmp5, reset;
637
638 zst = tp->t_sc;
639 cs = zst->zst_cs;
640
641 /*
642 * Because PCLK is only run at 4.9 MHz, the fastest we
643 * can go is 51200 baud (this corresponds to TC=1).
644 * This is somewhat unfortunate as there is no real
645 * reason we should not be able to handle higher rates.
646 */
647 bps = t->c_ospeed;
648 if (bps < 0 || (t->c_ispeed && t->c_ispeed != bps))
649 return (EINVAL);
650 if (bps == 0) {
651 /* stty 0 => drop DTR and RTS */
652 zs_modem(zst, 0);
653 return (0);
654 }
655 tconst = BPS_TO_TCONST(cs->cs_pclk_div16, bps);
656 if (tconst < 0)
657 return (EINVAL);
658
659 /* Convert back to make sure we can do it. */
660 bps = TCONST_TO_BPS(cs->cs_pclk_div16, tconst);
661 if (bps != t->c_ospeed)
662 return (EINVAL);
663 tp->t_ispeed = tp->t_ospeed = bps;
664
665 cflag = t->c_cflag;
666 tp->t_cflag = cflag;
667
668 /*
669 * Block interrupts so that state will not
670 * be altered until we are done setting it up.
671 */
672 s = splzs();
673
674 /*
675 * Initial values in cs_preg are set before
676 * our attach routine is called. The master
677 * interrupt enable is handled by zsc.c
678 */
679
680 cs->cs_preg[12] = tconst;
681 cs->cs_preg[13] = tconst >> 8;
682
683 switch (cflag & CSIZE) {
684 case CS5:
685 tmp3 = ZSWR3_RX_5;
686 tmp5 = ZSWR5_TX_5;
687 break;
688 case CS6:
689 tmp3 = ZSWR3_RX_6;
690 tmp5 = ZSWR5_TX_6;
691 break;
692 case CS7:
693 tmp3 = ZSWR3_RX_7;
694 tmp5 = ZSWR5_TX_7;
695 break;
696 case CS8:
697 default:
698 tmp3 = ZSWR3_RX_8;
699 tmp5 = ZSWR5_TX_8;
700 break;
701 }
702
703 /*
704 * Output hardware flow control on the chip is horrendous: if
705 * carrier detect drops, the receiver is disabled. Hence we
706 * can only do this when the carrier is on.
707 */
708 tmp3 |= ZSWR3_RX_ENABLE;
709 if (cflag & CCTS_OFLOW) {
710 if (zs_read_csr(cs) & ZSRR0_DCD)
711 tmp3 |= ZSWR3_HFC;
712 }
713
714 cs->cs_preg[3] = tmp3;
715 cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
716
717 tmp4 = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
718 if ((cflag & PARODD) == 0)
719 tmp4 |= ZSWR4_EVENP;
720 if (cflag & PARENB)
721 tmp4 |= ZSWR4_PARENB;
722 cs->cs_preg[4] = tmp4;
723
724 /*
725 * If nothing is being transmitted, set up new current values,
726 * else mark them as pending.
727 */
728 if (cs->cs_heldchange == 0) {
729 if (tp->t_state & TS_BUSY) {
730 zst->zst_heldtbc = zst->zst_tbc;
731 zst->zst_tbc = 0;
732 cs->cs_heldchange = 1;
733 } else {
734 zs_loadchannelregs(cs);
735 }
736 }
737 splx(s);
738 return (0);
739 }
740
741 /*
742 * Raise or lower modem control (DTR/RTS) signals. If a character is
743 * in transmission, the change is deferred.
744 */
745 static void
746 zs_modem(zst, onoff)
747 struct zstty_softc *zst;
748 int onoff;
749 {
750 struct zs_chanstate *cs;
751 struct tty *tp;
752 int s, bis, and;
753
754 cs = zst->zst_cs;
755 tp = zst->zst_tty;
756
757 if (onoff) {
758 bis = ZSWR5_DTR | ZSWR5_RTS;
759 and = ~0;
760 } else {
761 bis = 0;
762 and = ~(ZSWR5_DTR | ZSWR5_RTS);
763 }
764 s = splzs();
765 cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
766 if (cs->cs_heldchange == 0) {
767 if (tp->t_state & TS_BUSY) {
768 zst->zst_heldtbc = zst->zst_tbc;
769 zst->zst_tbc = 0;
770 cs->cs_heldchange = 1;
771 } else {
772 cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
773 zs_write_reg(cs, 5, cs->cs_creg[5]);
774 }
775 }
776 splx(s);
777 }
778
779
780 /****************************************************************
781 * Interface to the lower layer (zscc)
782 ****************************************************************/
783
784 static int
785 zstty_rxint(cs)
786 register struct zs_chanstate *cs;
787 {
788 register struct zstty_softc *zst;
789 register put, put_next;
790 register u_char c, rr0, rr1;
791
792 zst = cs->cs_private;
793 put = zst->zst_rbput;
794
795 nextchar:
796 /* Read the input data ASAP. */
797 c = zs_read_data(cs);
798
799 /* Save the status register too. */
800 rr1 = zs_read_reg(cs, 1);
801
802 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
803 /* Clear the receive error. */
804 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
805 }
806
807 zst->zst_rbuf[put] = (c << 8) | rr1;
808 put_next = (put + 1) & ZSTTY_RING_MASK;
809
810 /* Would overrun if increment makes (put==get). */
811 if (put_next == zst->zst_rbget) {
812 zst->zst_intr_flags |= INTR_RX_OVERRUN;
813 } else {
814 /* OK, really increment. */
815 put = put_next;
816 }
817
818 /* Keep reading until the FIFO is empty. */
819 rr0 = zs_read_csr(cs);
820 if (rr0 & ZSRR0_RX_READY)
821 goto nextchar;
822
823 /* Done reading. */
824 zst->zst_rbput = put;
825
826 /* Ask for softint() call. */
827 cs->cs_softreq = 1;
828 return(1);
829 }
830
831 static int
832 zstty_txint(cs)
833 register struct zs_chanstate *cs;
834 {
835 register struct zstty_softc *zst;
836 register int count, rval;
837
838 zst = cs->cs_private;
839 count = zst->zst_tbc;
840
841 if (count > 0) {
842 /* Send the next char. */
843 zs_write_data(cs, *zst->zst_tba);
844 zst->zst_tba++;
845 zst->zst_tbc = --count;
846 rval = 0;
847 } else {
848 /* Nothing more to send. */
849 zs_write_csr(cs, ZSWR0_RESET_TXINT);
850 zst->zst_intr_flags |= INTR_TX_EMPTY;
851 rval = 1; /* want softcall */
852 }
853
854 cs->cs_softreq = rval;
855 return (rval);
856 }
857
858 static int
859 zstty_stint(cs)
860 register struct zs_chanstate *cs;
861 {
862 register struct zstty_softc *zst;
863 register int rr0;
864
865 zst = cs->cs_private;
866
867 rr0 = zs_read_csr(cs);
868 zs_write_csr(cs, ZSWR0_RESET_STATUS);
869
870 if ((rr0 & ZSRR0_BREAK) &&
871 (zst->zst_hwflags & ZS_HWFLAG_CONSOLE))
872 {
873 zs_abort();
874 return (0);
875 }
876
877 zst->zst_intr_flags |= INTR_ST_CHECK;
878 /* Ask for softint() call. */
879 cs->cs_softreq = 1;
880 return (1);
881 }
882
883 /*
884 * Print out a ring or fifo overrun error message.
885 */
886 static void
887 zsoverrun(zst, ptime, what)
888 struct zstty_softc *zst;
889 long *ptime;
890 char *what;
891 {
892
893 if (*ptime != time.tv_sec) {
894 *ptime = time.tv_sec;
895 log(LOG_WARNING, "%s: %s overrun\n",
896 zst->zst_dev.dv_xname, what);
897 }
898 }
899
900 static int
901 zstty_softint(cs)
902 struct zs_chanstate *cs;
903 {
904 register struct zstty_softc *zst;
905 register struct linesw *line;
906 register struct tty *tp;
907 register int get, c, s;
908 int intr_flags;
909 register u_short ring_data;
910 register u_char rr0, rr1;
911
912 zst = cs->cs_private;
913 tp = zst->zst_tty;
914 line = &linesw[tp->t_line];
915
916 /* Atomically get and clear flags. */
917 s = splzs();
918 intr_flags = zst->zst_intr_flags;
919 zst->zst_intr_flags = 0;
920 splx(s);
921
922 if (intr_flags & INTR_RX_OVERRUN) {
923 /* May turn this on again below. */
924 intr_flags &= ~INTR_RX_OVERRUN;
925 zsoverrun(zst, "ring");
926 }
927
928 /*
929 * Copy data from the receive ring into the tty layer.
930 */
931 get = zst->zst_rbget;
932 while (get != zst->zst_rbput) {
933 ring_data = zst->zst_rbuf[get];
934 get = (get + 1) & ZSTTY_RING_MASK;
935
936 if (ring_data & ZSRR1_DO)
937 intr_flags |= INTR_RX_OVERRUN;
938 /* low byte of ring_data is rr1 */
939 c = (ring_data >> 8) & 0xff;
940 if (ring_data & ZSRR1_FE)
941 c |= TTY_FE;
942 if (ring_data & ZSRR1_PE)
943 c |= TTY_PE;
944
945 line->l_rint(c, tp);
946 }
947 zst->zst_rbget = get;
948
949 /* If set, it is from the loop above. */
950 if (intr_flags & INTR_RX_OVERRUN) {
951 zsoverrun(zst, "fifo");
952 }
953
954 if (intr_flags & INTR_TX_EMPTY) {
955 /*
956 * Transmit done. Change registers and resume,
957 * or just clear BUSY.
958 */
959 if (cs->cs_heldchange) {
960 s = splzs();
961 rr0 = zs_read_csr(cs);
962 if ((rr0 & ZSRR0_DCD) == 0)
963 cs->cs_preg[3] &= ~ZSWR3_HFC;
964 zs_loadchannelregs(cs);
965 splx(s);
966 cs->cs_heldchange = 0;
967
968 if (zst->zst_heldtbc &&
969 (tp->t_state & TS_TTSTOP) == 0)
970 {
971 zst->zst_tbc = zst->zst_heldtbc - 1;
972 zs_write_data(cs, *zst->zst_tba);
973 zst->zst_tba++;
974 goto tx_resumed;
975 }
976 }
977 tp->t_state &= ~TS_BUSY;
978 if (tp->t_state & TS_FLUSH)
979 tp->t_state &= ~TS_FLUSH;
980 else
981 ndflush(&tp->t_outq, zst->zst_tba -
982 (caddr_t) tp->t_outq.c_cf);
983 line->l_start(tp);
984 tx_resumed:
985 }
986
987 if (intr_flags & INTR_ST_CHECK) {
988 /*
989 * Status line change.
990 *
991 * The chip's hardware flow control is, as noted in zsreg.h,
992 * busted---if the DCD line goes low the chip shuts off the
993 * receiver (!). If we want hardware CTS flow control but do
994 * not have it, and carrier is now on, turn HFC on; if we have
995 * HFC now but carrier has gone low, turn it off.
996 */
997 s = splzs();
998 rr0 = zs_read_csr(cs);
999 if (rr0 & ZSRR0_DCD) {
1000 if (tp->t_cflag & CCTS_OFLOW &&
1001 (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
1002 cs->cs_creg[3] |= ZSWR3_HFC;
1003 zs_write_reg(cs, 3, cs->cs_creg[3]);
1004 }
1005 } else {
1006 if (cs->cs_creg[3] & ZSWR3_HFC) {
1007 cs->cs_creg[3] &= ~ZSWR3_HFC;
1008 zs_write_reg(cs, 3, cs->cs_creg[3]);
1009 }
1010 }
1011 splx(s);
1012
1013 /* Was there a change on DCD? */
1014 if ((rr0 ^ cs->cs_rr0) & ZSRR0_DCD) {
1015 c = ((rr0 & ZSRR0_DCD) != 0);
1016 if (line->l_modem(tp, c) == 0)
1017 zs_modem(zst, c);
1018 }
1019 cs->cs_rr0 = rr0;
1020 }
1021
1022 return (1);
1023 }
1024
1025 struct zsops zsops_tty = {
1026 zstty_rxint, /* receive char available */
1027 zstty_stint, /* external/status */
1028 zstty_txint, /* xmit buffer empty */
1029 zstty_softint, /* process software interrupt */
1030 };
1031
1032