z8530tty.c revision 1.34 1 /* $NetBSD: z8530tty.c,v 1.34 1997/11/03 04:34:18 gwr Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997
5 * Charles M. Hannum. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Charles M. Hannum.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1994 Gordon W. Ross
35 * Copyright (c) 1992, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * This software was developed by the Computer Systems Engineering group
39 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
40 * contributed to Berkeley.
41 *
42 * All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Lawrence Berkeley Laboratory.
46 *
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. All advertising materials mentioning features or use of this software
56 * must display the following acknowledgement:
57 * This product includes software developed by the University of
58 * California, Berkeley and its contributors.
59 * 4. Neither the name of the University nor the names of its contributors
60 * may be used to endorse or promote products derived from this software
61 * without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * SUCH DAMAGE.
74 *
75 * @(#)zs.c 8.1 (Berkeley) 7/19/93
76 */
77
78 /*
79 * Zilog Z8530 Dual UART driver (tty interface)
80 *
81 * This is the "slave" driver that will be attached to
82 * the "zsc" driver for plain "tty" async. serial lines.
83 *
84 * Credits, history:
85 *
86 * The original version of this code was the sparc/dev/zs.c driver
87 * as distributed with the Berkeley 4.4 Lite release. Since then,
88 * Gordon Ross reorganized the code into the current parent/child
89 * driver scheme, separating the Sun keyboard and mouse support
90 * into independent child drivers.
91 *
92 * RTS/CTS flow-control support was a collaboration of:
93 * Gordon Ross <gwr (at) netbsd.org>,
94 * Bill Studenmund <wrstuden (at) loki.stanford.edu>
95 * Ian Dall <Ian.Dall (at) dsto.defence.gov.au>
96 */
97
98 #include <sys/param.h>
99 #include <sys/systm.h>
100 #include <sys/proc.h>
101 #include <sys/device.h>
102 #include <sys/conf.h>
103 #include <sys/file.h>
104 #include <sys/ioctl.h>
105 #include <sys/malloc.h>
106 #include <sys/tty.h>
107 #include <sys/time.h>
108 #include <sys/kernel.h>
109 #include <sys/syslog.h>
110
111 #include <dev/ic/z8530reg.h>
112 #include <machine/z8530var.h>
113
114 #include "locators.h"
115
116 /*
117 * How many input characters we can buffer.
118 * The port-specific var.h may override this.
119 * Note: must be a power of two!
120 */
121 #ifndef ZSTTY_RING_SIZE
122 #define ZSTTY_RING_SIZE 2048
123 #endif
124
125 /*
126 * Make this an option variable one can patch.
127 * But be warned: this must be a power of 2!
128 */
129 int zstty_rbuf_size = ZSTTY_RING_SIZE;
130
131 /* This should usually be 3/4 of ZSTTY_RING_SIZE */
132 int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE - (ZSTTY_RING_SIZE >> 2));
133
134 struct zstty_softc {
135 struct device zst_dev; /* required first: base device */
136 struct tty *zst_tty;
137 struct zs_chanstate *zst_cs;
138
139 int zst_hwflags; /* see z8530var.h */
140 int zst_swflags; /* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
141
142 /*
143 * Printing an overrun error message often takes long enough to
144 * cause another overrun, so we only print one per second.
145 */
146 long zst_rotime; /* time of last ring overrun */
147 long zst_fotime; /* time of last fifo overrun */
148
149 /*
150 * The receive ring buffer.
151 */
152 int zst_rbget; /* ring buffer `get' index */
153 volatile int zst_rbput; /* ring buffer `put' index */
154 int zst_ringmask;
155 int zst_rbhiwat;
156
157 u_short *zst_rbuf; /* rr1, data pairs */
158
159 /*
160 * The transmit byte count and address are used for pseudo-DMA
161 * output in the hardware interrupt code. PDMA can be suspended
162 * to get pending changes done; heldtbc is used for this. It can
163 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
164 */
165 int zst_tbc; /* transmit byte count */
166 u_char *zst_tba; /* transmit buffer address */
167 int zst_heldtbc; /* held tbc while xmission stopped */
168
169 /* Flags to communicate with zstty_softint() */
170 volatile char zst_rx_blocked; /* input block at ring */
171 volatile char zst_rx_overrun; /* ring overrun */
172 volatile char zst_tx_busy; /* working on an output chunk */
173 volatile char zst_tx_done; /* done with one output chunk */
174 volatile char zst_tx_stopped; /* H/W level stop (lost CTS) */
175 volatile char zst_st_check; /* got a status interrupt */
176 char pad[2];
177 };
178
179
180 /* Definition of the driver for autoconfig. */
181 #ifdef __BROKEN_INDIRECT_CONFIG
182 static int zstty_match(struct device *, void *, void *);
183 #else
184 static int zstty_match(struct device *, struct cfdata *, void *);
185 #endif
186 static void zstty_attach(struct device *, struct device *, void *);
187
188 struct cfattach zstty_ca = {
189 sizeof(struct zstty_softc), zstty_match, zstty_attach
190 };
191
192 struct cfdriver zstty_cd = {
193 NULL, "zstty", DV_TTY
194 };
195
196 struct zsops zsops_tty;
197
198 /* Routines called from other code. */
199 cdev_decl(zs); /* open, close, read, write, ioctl, stop, ... */
200
201 static void zsstart __P((struct tty *));
202 static int zsparam __P((struct tty *, struct termios *));
203 static void zs_modem __P((struct zstty_softc *zst, int onoff));
204 static int zshwiflow __P((struct tty *, int));
205 static void zs_hwiflow __P((struct zstty_softc *));
206
207 /*
208 * zstty_match: how is this zs channel configured?
209 */
210 #ifdef __BROKEN_INDIRECT_CONFIG
211 int
212 zstty_match(parent, vcf, aux)
213 struct device *parent;
214 void *vcf, *aux;
215 {
216 struct cfdata *cf = vcf;
217 struct zsc_attach_args *args = aux;
218
219 /* Exact match is better than wildcard. */
220 if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
221 return 2;
222
223 /* This driver accepts wildcard. */
224 if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
225 return 1;
226
227 return 0;
228 }
229 #else /* __BROKEN_INDIRECT_CONFIG */
230 int
231 zstty_match(parent, cf, aux)
232 struct device *parent;
233 struct cfdata *cf;
234 void *aux;
235 {
236 struct zsc_attach_args *args = aux;
237
238 /* Exact match is better than wildcard. */
239 if (cf->cf_loc[ZSCCF_CHANNEL] == args->channel)
240 return 2;
241
242 /* This driver accepts wildcard. */
243 if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT)
244 return 1;
245
246 return 0;
247 }
248 #endif /* __BROKEN_INDIRECT_CONFIG */
249
250 void
251 zstty_attach(parent, self, aux)
252 struct device *parent, *self;
253 void *aux;
254
255 {
256 struct zsc_softc *zsc = (void *) parent;
257 struct zstty_softc *zst = (void *) self;
258 struct cfdata *cf = self->dv_cfdata;
259 struct zsc_attach_args *args = aux;
260 struct zs_chanstate *cs;
261 struct tty *tp;
262 int channel, s, tty_unit;
263 dev_t dev;
264
265 tty_unit = zst->zst_dev.dv_unit;
266 channel = args->channel;
267 cs = zsc->zsc_cs[channel];
268 cs->cs_private = zst;
269 cs->cs_ops = &zsops_tty;
270
271 zst->zst_cs = cs;
272 zst->zst_swflags = cf->cf_flags; /* softcar, etc. */
273 zst->zst_hwflags = args->hwflags;
274 dev = makedev(zs_major, tty_unit);
275
276 if (zst->zst_swflags)
277 printf(" flags 0x%x", zst->zst_swflags);
278
279 if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE)
280 printf(" (console)");
281 else {
282 #ifdef KGDB
283 /*
284 * Allow kgdb to "take over" this port. Returns true
285 * if this serial port is in-use by kgdb.
286 */
287 if (zs_check_kgdb(cs, dev)) {
288 printf(" (kgdb)\n");
289 /*
290 * This is the kgdb port (exclusive use)
291 * so skip the normal attach code.
292 */
293 return;
294 }
295 #endif
296 }
297 printf("\n");
298
299 tp = ttymalloc();
300 tp->t_dev = dev;
301 tp->t_oproc = zsstart;
302 tp->t_param = zsparam;
303 tp->t_hwiflow = zshwiflow;
304 tty_attach(tp);
305
306 zst->zst_tty = tp;
307 zst->zst_rbhiwat = zstty_rbuf_size; /* impossible value */
308 zst->zst_ringmask = zstty_rbuf_size - 1;
309 zst->zst_rbuf = malloc(zstty_rbuf_size * sizeof(zst->zst_rbuf[0]),
310 M_DEVBUF, M_WAITOK);
311
312 /* XXX - Do we need an MD hook here? */
313
314 /*
315 * Hardware init
316 */
317 if (zst->zst_hwflags & ZS_HWFLAG_CONSOLE) {
318 /* Call zsparam similar to open. */
319 struct termios t;
320
321 s = splzs();
322
323 /* Turn on interrupts. */
324 cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
325 zs_write_reg(cs, 1, cs->cs_creg[1]);
326
327 /* Fetch the current modem control status, needed later. */
328 cs->cs_rr0 = zs_read_csr(cs);
329
330 splx(s);
331
332 /* Setup the "new" parameters in t. */
333 t.c_ispeed = 0;
334 t.c_ospeed = cs->cs_defspeed;
335 t.c_cflag = cs->cs_defcflag;
336 /* Make sure zsparam will see changes. */
337 tp->t_ospeed = 0;
338 (void) zsparam(tp, &t);
339 /* Make sure DTR is on now. */
340 zs_modem(zst, 1);
341 } else {
342 /* Not the console; may need reset. */
343 int reset;
344 reset = (channel == 0) ?
345 ZSWR9_A_RESET : ZSWR9_B_RESET;
346 s = splzs();
347 zs_write_reg(cs, 9, reset);
348 splx(s);
349 /* Will raise DTR in open. */
350 zs_modem(zst, 0);
351 }
352 }
353
354
355 /*
356 * Return pointer to our tty.
357 */
358 struct tty *
359 zstty(dev)
360 dev_t dev;
361 {
362 struct zstty_softc *zst;
363 int unit = minor(dev);
364
365 #ifdef DIAGNOSTIC
366 if (unit >= zstty_cd.cd_ndevs)
367 panic("zstty");
368 #endif
369 zst = zstty_cd.cd_devs[unit];
370 return (zst->zst_tty);
371 }
372
373
374 /*
375 * Open a zs serial (tty) port.
376 */
377 int
378 zsopen(dev, flags, mode, p)
379 dev_t dev;
380 int flags;
381 int mode;
382 struct proc *p;
383 {
384 register struct tty *tp;
385 register struct zs_chanstate *cs;
386 struct zstty_softc *zst;
387 int error, s, s2, unit;
388
389 unit = minor(dev);
390 if (unit >= zstty_cd.cd_ndevs)
391 return (ENXIO);
392 zst = zstty_cd.cd_devs[unit];
393 if (zst == NULL)
394 return (ENXIO);
395 tp = zst->zst_tty;
396 cs = zst->zst_cs;
397
398 /* If KGDB took the line, then tp==NULL */
399 if (tp == NULL)
400 return (EBUSY);
401
402 if ((tp->t_state & TS_ISOPEN) != 0 &&
403 (tp->t_state & TS_XCLUDE) != 0 &&
404 p->p_ucred->cr_uid != 0)
405 return (EBUSY);
406
407 s = spltty();
408
409 if ((tp->t_state & TS_ISOPEN) == 0) {
410 /* First open. */
411 struct termios t;
412
413 s2 = splzs();
414
415 /* Turn on interrupts. */
416 cs->cs_creg[1] = cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
417 zs_write_reg(cs, 1, cs->cs_creg[1]);
418
419 /* Fetch the current modem control status, needed later. */
420 cs->cs_rr0 = zs_read_csr(cs);
421
422 splx(s2);
423
424 /*
425 * Setup the "new" parameters in t.
426 * Can not use tp->t because zsparam
427 * deals only with what has changed.
428 */
429 t.c_ispeed = 0;
430 t.c_ospeed = cs->cs_defspeed;
431 t.c_cflag = cs->cs_defcflag;
432 if (zst->zst_swflags & TIOCFLAG_CLOCAL)
433 t.c_cflag |= CLOCAL;
434 if (zst->zst_swflags & TIOCFLAG_CRTSCTS)
435 t.c_cflag |= CRTSCTS;
436 if (zst->zst_swflags & TIOCFLAG_MDMBUF)
437 t.c_cflag |= MDMBUF;
438 /* Make sure zsparam will see changes. */
439 tp->t_ospeed = 0;
440 (void) zsparam(tp, &t);
441 /*
442 * Note: zsparam has done: cflag, ispeed, ospeed
443 * so we just need to do: iflag, oflag, lflag, cc
444 * For "raw" mode, just leave all zeros.
445 */
446 if ((zst->zst_hwflags & ZS_HWFLAG_RAW) == 0) {
447 tp->t_iflag = TTYDEF_IFLAG;
448 tp->t_oflag = TTYDEF_OFLAG;
449 tp->t_lflag = TTYDEF_LFLAG;
450 }
451 ttychars(tp);
452 ttsetwater(tp);
453
454 /*
455 * Turn on DTR. We must always do this, even if carrier is not
456 * present, because otherwise we'd have to use TIOCSDTR
457 * immediately after setting CLOCAL, which applications do not
458 * expect. We always assert DTR while the device is open
459 * unless explicitly requested to deassert it.
460 */
461 zs_modem(zst, 1);
462
463 s2 = splzs();
464
465 /* Clear the input ring, and unblock. */
466 zst->zst_rbget = zst->zst_rbput;
467 zs_iflush(cs);
468 zst->zst_rx_blocked = 0;
469 zs_hwiflow(zst);
470
471 splx(s2);
472 }
473 error = 0;
474
475 /* If we're doing a blocking open... */
476 if ((flags & O_NONBLOCK) == 0)
477 /* ...then wait for carrier. */
478 while ((tp->t_state & TS_CARR_ON) == 0 &&
479 (tp->t_cflag & (CLOCAL | MDMBUF)) == 0) {
480 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
481 ttopen, 0);
482 if (error) {
483 /*
484 * If the open was interrupted and nobody
485 * else has the device open, then hang up.
486 */
487 if ((tp->t_state & TS_ISOPEN) == 0) {
488 zs_modem(zst, 0);
489 tp->t_state &= ~TS_WOPEN;
490 ttwakeup(tp);
491 }
492 break;
493 }
494 tp->t_state |= TS_WOPEN;
495 }
496
497 splx(s);
498 if (error == 0)
499 error = (*linesw[tp->t_line].l_open)(dev, tp);
500 return (error);
501 }
502
503 /*
504 * Close a zs serial port.
505 */
506 int
507 zsclose(dev, flags, mode, p)
508 dev_t dev;
509 int flags;
510 int mode;
511 struct proc *p;
512 {
513 struct zstty_softc *zst;
514 register struct zs_chanstate *cs;
515 register struct tty *tp;
516 int s;
517
518 zst = zstty_cd.cd_devs[minor(dev)];
519 cs = zst->zst_cs;
520 tp = zst->zst_tty;
521
522 /* XXX This is for cons.c. */
523 if ((tp->t_state & TS_ISOPEN) == 0)
524 return 0;
525
526 (*linesw[tp->t_line].l_close)(tp, flags);
527 ttyclose(tp);
528
529 s = splzs();
530
531 /* If we were asserting flow control, then deassert it. */
532 zst->zst_rx_blocked = 1;
533 zs_hwiflow(zst);
534
535 splx(s);
536
537 /* Clear any break condition set with TIOCSBRK. */
538 zs_break(cs, 0);
539
540 /*
541 * Hang up if necessary. Wait a bit, so the other side has time to
542 * notice even if we immediately open the port again.
543 */
544 if ((tp->t_cflag & HUPCL) != 0) {
545 zs_modem(zst, 0);
546 (void) tsleep(cs, TTIPRI, ttclos, hz);
547 }
548
549 s = splzs();
550
551 /* Turn off interrupts. */
552 cs->cs_creg[1] = cs->cs_preg[1] = 0;
553 zs_write_reg(cs, 1, cs->cs_creg[1]);
554
555 splx(s);
556
557 return (0);
558 }
559
560 /*
561 * Read/write zs serial port.
562 */
563 int
564 zsread(dev, uio, flags)
565 dev_t dev;
566 struct uio *uio;
567 int flags;
568 {
569 register struct zstty_softc *zst;
570 register struct tty *tp;
571
572 zst = zstty_cd.cd_devs[minor(dev)];
573 tp = zst->zst_tty;
574 return (linesw[tp->t_line].l_read(tp, uio, flags));
575 }
576
577 int
578 zswrite(dev, uio, flags)
579 dev_t dev;
580 struct uio *uio;
581 int flags;
582 {
583 register struct zstty_softc *zst;
584 register struct tty *tp;
585
586 zst = zstty_cd.cd_devs[minor(dev)];
587 tp = zst->zst_tty;
588 return (linesw[tp->t_line].l_write(tp, uio, flags));
589 }
590
591 int
592 zsioctl(dev, cmd, data, flag, p)
593 dev_t dev;
594 u_long cmd;
595 caddr_t data;
596 int flag;
597 struct proc *p;
598 {
599 register struct zstty_softc *zst;
600 register struct zs_chanstate *cs;
601 register struct tty *tp;
602 register struct linesw *line;
603 register int error;
604
605 zst = zstty_cd.cd_devs[minor(dev)];
606 cs = zst->zst_cs;
607 tp = zst->zst_tty;
608 line = &linesw[tp->t_line];
609
610 error = (*line->l_ioctl)(tp, cmd, data, flag, p);
611 if (error >= 0)
612 return (error);
613
614 error = ttioctl(tp, cmd, data, flag, p);
615 if (error >= 0)
616 return (error);
617
618 #ifdef ZS_MD_IOCTL
619 error = ZS_MD_IOCTL;
620 if (error >= 0)
621 return (error);
622 #endif /* ZS_MD_IOCTL */
623
624 switch (cmd) {
625 case TIOCSBRK:
626 zs_break(cs, 1);
627 break;
628
629 case TIOCCBRK:
630 zs_break(cs, 0);
631 break;
632
633 case TIOCGFLAGS:
634 *(int *)data = zst->zst_swflags;
635 break;
636
637 case TIOCSFLAGS:
638 error = suser(p->p_ucred, &p->p_acflag);
639 if (error)
640 return (error);
641 zst->zst_swflags = *(int *)data;
642 break;
643
644 case TIOCSDTR:
645 zs_modem(zst, 1);
646 break;
647
648 case TIOCCDTR:
649 zs_modem(zst, 0);
650 break;
651
652 case TIOCMSET:
653 case TIOCMBIS:
654 case TIOCMBIC:
655 case TIOCMGET:
656 default:
657 return (ENOTTY);
658 }
659 return (0);
660 }
661
662 /*
663 * Start or restart transmission.
664 */
665 static void
666 zsstart(tp)
667 register struct tty *tp;
668 {
669 register struct zstty_softc *zst;
670 register struct zs_chanstate *cs;
671 register int s;
672
673 zst = zstty_cd.cd_devs[minor(tp->t_dev)];
674 cs = zst->zst_cs;
675
676 s = spltty();
677 if ((tp->t_state & TS_BUSY) != 0)
678 goto out;
679 if ((tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) != 0)
680 goto stopped;
681
682 if (zst->zst_tx_stopped)
683 goto stopped;
684
685 if (tp->t_outq.c_cc <= tp->t_lowat) {
686 if ((tp->t_state & TS_ASLEEP) != 0) {
687 tp->t_state &= ~TS_ASLEEP;
688 wakeup((caddr_t)&tp->t_outq);
689 }
690 selwakeup(&tp->t_wsel);
691 if (tp->t_outq.c_cc == 0)
692 goto stopped;
693 }
694
695 /* Grab the first contiguous region of buffer space. */
696 {
697 u_char *tba;
698 int tbc;
699
700 tba = tp->t_outq.c_cf;
701 tbc = ndqb(&tp->t_outq, 0);
702
703 (void) splzs();
704
705 zst->zst_tba = tba;
706 zst->zst_tbc = tbc;
707 }
708
709 tp->t_state |= TS_BUSY;
710 zst->zst_tx_busy = 1;
711
712 /* Enable transmit completion interrupts if necessary. */
713 if ((cs->cs_preg[1] & ZSWR1_TIE) == 0) {
714 cs->cs_preg[1] |= ZSWR1_TIE;
715 cs->cs_creg[1] = cs->cs_preg[1];
716 zs_write_reg(cs, 1, cs->cs_creg[1]);
717 }
718
719 /* Output the first character of the contiguous buffer. */
720 zs_write_data(cs, *zst->zst_tba);
721 zst->zst_tbc--;
722 zst->zst_tba++;
723 splx(s);
724 return;
725
726 stopped:
727 /* Disable transmit completion interrupts if necessary. */
728 if ((cs->cs_preg[1] & ZSWR1_TIE) != 0) {
729 cs->cs_preg[1] &= ~ZSWR1_TIE;
730 cs->cs_creg[1] = cs->cs_preg[1];
731 zs_write_reg(cs, 1, cs->cs_creg[1]);
732 }
733 out:
734 splx(s);
735 return;
736 }
737
738 /*
739 * Stop output, e.g., for ^S or output flush.
740 */
741 void
742 zsstop(tp, flag)
743 struct tty *tp;
744 int flag;
745 {
746 register struct zstty_softc *zst;
747 register struct zs_chanstate *cs;
748 register int s;
749
750 zst = zstty_cd.cd_devs[minor(tp->t_dev)];
751 cs = zst->zst_cs;
752
753 s = splzs();
754 if (tp->t_state & TS_BUSY) {
755 /*
756 * Device is transmitting; must stop it.
757 * Also clear _heldtbc to prevent any
758 * flow-control event from resuming.
759 */
760 zst->zst_tbc = 0;
761 zst->zst_heldtbc = 0;
762 if ((tp->t_state & TS_TTSTOP) == 0)
763 tp->t_state |= TS_FLUSH;
764 }
765 splx(s);
766 }
767
768 /*
769 * Set ZS tty parameters from termios.
770 * XXX - Should just copy the whole termios after
771 * making sure all the changes could be done.
772 */
773 static int
774 zsparam(tp, t)
775 register struct tty *tp;
776 register struct termios *t;
777 {
778 struct zstty_softc *zst;
779 struct zs_chanstate *cs;
780 register struct linesw *line;
781 int s, bps, cflag, error;
782 u_char tmp3, tmp4, tmp5;
783
784 zst = zstty_cd.cd_devs[minor(tp->t_dev)];
785 cs = zst->zst_cs;
786 line = &linesw[tp->t_line];
787 bps = t->c_ospeed;
788 cflag = t->c_cflag;
789
790 if (bps < 0 || (t->c_ispeed && t->c_ispeed != bps))
791 return (EINVAL);
792
793 /*
794 * For the console, always force CLOCAL and !HUPCL, so that the port
795 * is always active.
796 */
797 if ((zst->zst_swflags & TIOCFLAG_SOFTCAR) != 0 ||
798 (zst->zst_hwflags & (ZS_HWFLAG_NO_DCD | ZS_HWFLAG_CONSOLE)) != 0) {
799 cflag |= CLOCAL;
800 cflag &= ~HUPCL;
801 }
802
803 /*
804 * Only whack the UART when params change.
805 * Some callers need to clear tp->t_ospeed
806 * to make sure initialization gets done.
807 */
808 if (tp->t_ospeed == bps &&
809 tp->t_cflag == cflag)
810 return (0);
811
812 /*
813 * Call MD functions to deal with changed
814 * clock modes or H/W flow control modes.
815 * The BRG divisor is set now. (reg 12,13)
816 */
817 error = zs_set_speed(cs, bps);
818 if (error)
819 return (error);
820 error = zs_set_modes(cs, cflag);
821 if (error)
822 return (error);
823
824 /* OK, we are now committed to do it. */
825 tp->t_cflag = cflag;
826 tp->t_ospeed = bps;
827 tp->t_ispeed = bps;
828
829 /*
830 * Block interrupts so that state will not
831 * be altered until we are done setting it up.
832 *
833 * Initial values in cs_preg are set before
834 * our attach routine is called. The master
835 * interrupt enable is handled by zsc.c
836 *
837 */
838 s = splzs();
839
840 cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
841 if ((cs->cs_rr0_mask & ZSRR0_DCD) != 0)
842 cs->cs_preg[15] |= ZSWR15_DCD_IE;
843 else
844 cs->cs_preg[15] &= ~ZSWR15_DCD_IE;
845 if ((cs->cs_rr0_mask & ZSRR0_CTS) != 0)
846 cs->cs_preg[15] |= ZSWR15_CTS_IE;
847 else
848 cs->cs_preg[15] &= ~ZSWR15_CTS_IE;
849
850 /* Recompute character size bits. */
851 tmp3 = cs->cs_preg[3] & ~ZSWR3_RXSIZE;
852 tmp5 = cs->cs_preg[5] & ~ZSWR5_TXSIZE;
853 switch (cflag & CSIZE) {
854 case CS5:
855 /* These are |= 0 but let the optimizer deal with it. */
856 tmp3 |= ZSWR3_RX_5;
857 tmp5 |= ZSWR5_TX_5;
858 break;
859 case CS6:
860 tmp3 |= ZSWR3_RX_6;
861 tmp5 |= ZSWR5_TX_6;
862 break;
863 case CS7:
864 tmp3 |= ZSWR3_RX_7;
865 tmp5 |= ZSWR5_TX_7;
866 break;
867 case CS8:
868 default:
869 tmp3 |= ZSWR3_RX_8;
870 tmp5 |= ZSWR5_TX_8;
871 break;
872 }
873
874 #if 0
875 /* Raise or lower DTR and RTS as appropriate. */
876 if (bps) {
877 /* Raise DTR and RTS */
878 tmp5 |= cs->cs_wr5_dtr;
879 } else {
880 /* Drop DTR and RTS */
881 /* XXX: Should SOFTCAR prevent this? */
882 tmp5 &= ~cs->cs_wr5_dtr;
883 }
884 #endif
885
886 cs->cs_preg[3] = tmp3;
887 cs->cs_preg[5] = tmp5;
888
889 /*
890 * Recompute the stop bits and parity bits. Note that
891 * zs_set_speed() may have set clock selection bits etc.
892 * in wr4, so those must preserved.
893 */
894 tmp4 = cs->cs_preg[4];
895 /* Recompute stop bits. */
896 tmp4 &= ~ZSWR4_SBMASK;
897 tmp4 |= (cflag & CSTOPB) ?
898 ZSWR4_TWOSB : ZSWR4_ONESB;
899 /* Recompute parity bits. */
900 tmp4 &= ~ZSWR4_PARMASK;
901 if ((cflag & PARODD) == 0)
902 tmp4 |= ZSWR4_EVENP;
903 if (cflag & PARENB)
904 tmp4 |= ZSWR4_PARENB;
905 cs->cs_preg[4] = tmp4;
906
907 /* The MD function zs_set_modes handled CRTSCTS, etc. */
908
909 /*
910 * If nothing is being transmitted, set up new current values,
911 * else mark them as pending.
912 */
913 if (!cs->cs_heldchange) {
914 if (zst->zst_tx_busy) {
915 zst->zst_heldtbc = zst->zst_tbc;
916 zst->zst_tbc = 0;
917 cs->cs_heldchange = 1;
918 } else
919 zs_loadchannelregs(cs);
920 }
921
922 if ((cflag & CHWFLOW) == 0) {
923 /* This impossible value prevents a "high water" trigger. */
924 zst->zst_rbhiwat = zstty_rbuf_size;
925 if (zst->zst_rx_blocked) {
926 zst->zst_rx_blocked = 0;
927 zs_hwiflow(zst);
928 }
929 } else {
930 zst->zst_rbhiwat = zstty_rbuf_hiwat;
931 }
932
933 splx(s);
934
935 /*
936 * Update the tty layer's idea of the carrier bit, in case we changed
937 * CLOCAL or MDMBUF. We don't hang up here; we only do that by
938 * explicit request.
939 */
940 (void) (*line->l_modem)(tp, (cs->cs_rr0 & cs->cs_rr0_dcd) != 0);
941
942 if ((cflag & CHWFLOW) == 0) {
943 if (zst->zst_tx_stopped) {
944 zst->zst_tx_stopped = 0;
945 zsstart(tp);
946 }
947 }
948
949 return (0);
950 }
951
952 /*
953 * Raise or lower modem control (DTR/RTS) signals. If a character is
954 * in transmission, the change is deferred.
955 */
956 static void
957 zs_modem(zst, onoff)
958 struct zstty_softc *zst;
959 int onoff;
960 {
961 struct zs_chanstate *cs;
962 int s;
963
964 cs = zst->zst_cs;
965 if (cs->cs_wr5_dtr == 0)
966 return;
967
968 s = splzs();
969 if (onoff)
970 cs->cs_preg[5] |= cs->cs_wr5_dtr;
971 else
972 cs->cs_preg[5] &= ~cs->cs_wr5_dtr;
973
974 if (!cs->cs_heldchange) {
975 if (zst->zst_tx_busy) {
976 zst->zst_heldtbc = zst->zst_tbc;
977 zst->zst_tbc = 0;
978 cs->cs_heldchange = 1;
979 } else
980 zs_loadchannelregs(cs);
981 }
982 splx(s);
983 }
984
985 /*
986 * Try to block or unblock input using hardware flow-control.
987 * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
988 * if this function returns non-zero, the TS_TBLOCK flag will
989 * be set or cleared according to the "block" arg passed.
990 */
991 int
992 zshwiflow(tp, block)
993 struct tty *tp;
994 int block;
995 {
996 register struct zstty_softc *zst;
997 register struct zs_chanstate *cs;
998 int s;
999
1000 zst = zstty_cd.cd_devs[minor(tp->t_dev)];
1001 cs = zst->zst_cs;
1002 if (cs->cs_wr5_rts == 0)
1003 return (0);
1004
1005 s = splzs();
1006 if (block) {
1007 if (!zst->zst_rx_blocked) {
1008 zst->zst_rx_blocked = 1;
1009 zs_hwiflow(zst);
1010 }
1011 } else {
1012 if (zst->zst_rx_blocked) {
1013 zst->zst_rx_blocked = 0;
1014 zs_hwiflow(zst);
1015 }
1016 }
1017 splx(s);
1018 return 1;
1019 }
1020
1021 /*
1022 * Internal version of zshwiflow
1023 * called at splzs
1024 */
1025 static void
1026 zs_hwiflow(zst)
1027 register struct zstty_softc *zst;
1028 {
1029 register struct zs_chanstate *cs;
1030
1031 cs = zst->zst_cs;
1032 if (cs->cs_wr5_rts == 0)
1033 return;
1034
1035 if (zst->zst_rx_blocked) {
1036 cs->cs_preg[5] &= ~cs->cs_wr5_rts;
1037 cs->cs_creg[5] &= ~cs->cs_wr5_rts;
1038 } else {
1039 cs->cs_preg[5] |= cs->cs_wr5_rts;
1040 cs->cs_creg[5] |= cs->cs_wr5_rts;
1041 }
1042 zs_write_reg(cs, 5, cs->cs_creg[5]);
1043 }
1044
1045
1046 /****************************************************************
1047 * Interface to the lower layer (zscc)
1048 ****************************************************************/
1049
1050 static void zstty_rxint __P((struct zs_chanstate *));
1051 static void zstty_txint __P((struct zs_chanstate *));
1052 static void zstty_stint __P((struct zs_chanstate *));
1053 static void zstty_softint __P((struct zs_chanstate *));
1054
1055 static void zsoverrun __P((struct zstty_softc *, long *, char *));
1056
1057 /*
1058 * receiver ready interrupt.
1059 * called at splzs
1060 */
1061 static void
1062 zstty_rxint(cs)
1063 register struct zs_chanstate *cs;
1064 {
1065 register struct zstty_softc *zst;
1066 register int cc, put, put_next, ringmask;
1067 register u_char c, rr0, rr1;
1068 register u_short ch_rr1;
1069
1070 zst = cs->cs_private;
1071 put = zst->zst_rbput;
1072 ringmask = zst->zst_ringmask;
1073
1074 nextchar:
1075
1076 /*
1077 * First read the status, because reading the received char
1078 * destroys the status of this char.
1079 */
1080 rr1 = zs_read_reg(cs, 1);
1081 c = zs_read_data(cs);
1082 ch_rr1 = (c << 8) | rr1;
1083
1084 if (ch_rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
1085 /* Clear the receive error. */
1086 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
1087 }
1088
1089 /* XXX: Check for the stop character? */
1090
1091 zst->zst_rbuf[put] = ch_rr1;
1092 put_next = (put + 1) & ringmask;
1093
1094 /* Would overrun if increment makes (put==get). */
1095 if (put_next == zst->zst_rbget) {
1096 zst->zst_rx_overrun = 1;
1097 } else {
1098 /* OK, really increment. */
1099 put = put_next;
1100 }
1101
1102 /* Keep reading until the FIFO is empty. */
1103 rr0 = zs_read_csr(cs);
1104 if (rr0 & ZSRR0_RX_READY)
1105 goto nextchar;
1106
1107 /* Done reading. */
1108 zst->zst_rbput = put;
1109
1110 /*
1111 * If ring is getting too full, try to block input.
1112 */
1113 cc = put - zst->zst_rbget;
1114 if (cc < 0)
1115 cc += zstty_rbuf_size;
1116 if ((cc > zst->zst_rbhiwat) && (zst->zst_rx_blocked == 0)) {
1117 zst->zst_rx_blocked = 1;
1118 zs_hwiflow(zst);
1119 }
1120
1121 /* Ask for softint() call. */
1122 cs->cs_softreq = 1;
1123 }
1124
1125 /*
1126 * transmitter ready interrupt. (splzs)
1127 */
1128 static void
1129 zstty_txint(cs)
1130 register struct zs_chanstate *cs;
1131 {
1132 register struct zstty_softc *zst;
1133 register int count;
1134
1135 zst = cs->cs_private;
1136
1137 /*
1138 * If we suspended output for a "held" change,
1139 * then handle that now and resume.
1140 * Do flow-control changes ASAP.
1141 * When the only change is for flow control,
1142 * avoid hitting other registers, because that
1143 * often makes the stupid zs drop input...
1144 */
1145 if (cs->cs_heldchange) {
1146 zs_loadchannelregs(cs);
1147 cs->cs_heldchange = 0;
1148 count = zst->zst_heldtbc;
1149 } else
1150 count = zst->zst_tbc;
1151
1152 /*
1153 * If our transmit buffer still has data,
1154 * just send the next character.
1155 */
1156 if (count > 0) {
1157 /* Send the next char. */
1158 zst->zst_tbc = --count;
1159 zs_write_data(cs, *zst->zst_tba);
1160 zst->zst_tba++;
1161 return;
1162 }
1163
1164 zs_write_csr(cs, ZSWR0_RESET_TXINT);
1165
1166 /* Ask the softint routine for more output. */
1167 zst->zst_tx_busy = 0;
1168 zst->zst_tx_done = 1;
1169 cs->cs_softreq = 1;
1170 }
1171
1172 /*
1173 * status change interrupt. (splzs)
1174 */
1175 static void
1176 zstty_stint(cs)
1177 register struct zs_chanstate *cs;
1178 {
1179 register struct zstty_softc *zst;
1180 register u_char rr0, delta;
1181
1182 zst = cs->cs_private;
1183
1184 rr0 = zs_read_csr(cs);
1185 zs_write_csr(cs, ZSWR0_RESET_STATUS);
1186
1187 /*
1188 * Check here for console break, so that we can abort
1189 * even when interrupts are locking up the machine.
1190 */
1191 if ((rr0 & ZSRR0_BREAK) &&
1192 (zst->zst_hwflags & ZS_HWFLAG_CONSOLE))
1193 {
1194 zs_abort(cs);
1195 return;
1196 }
1197
1198 delta = rr0 ^ cs->cs_rr0;
1199 cs->cs_rr0 = rr0;
1200 if ((delta & cs->cs_rr0_mask) != 0) {
1201 cs->cs_rr0_delta |= delta;
1202
1203 /*
1204 * Stop output immediately if we lose the output
1205 * flow control signal or carrier detect.
1206 */
1207 if ((~rr0 & cs->cs_rr0_mask) != 0) {
1208 zst->zst_tbc = 0;
1209 zst->zst_heldtbc = 0;
1210 }
1211
1212 zst->zst_st_check = 1;
1213 }
1214
1215 /* Ask for softint() call. */
1216 cs->cs_softreq = 1;
1217 }
1218
1219 /*
1220 * Print out a ring or fifo overrun error message.
1221 */
1222 static void
1223 zsoverrun(zst, ptime, what)
1224 struct zstty_softc *zst;
1225 long *ptime;
1226 char *what;
1227 {
1228
1229 if (*ptime != time.tv_sec) {
1230 *ptime = time.tv_sec;
1231 log(LOG_WARNING, "%s: %s overrun\n",
1232 zst->zst_dev.dv_xname, what);
1233 }
1234 }
1235
1236 /*
1237 * Software interrupt. Called at zssoft
1238 *
1239 * The main job to be done here is to empty the input ring
1240 * by passing its contents up to the tty layer. The ring is
1241 * always emptied during this operation, therefore the ring
1242 * must not be larger than the space after "high water" in
1243 * the tty layer, or the tty layer might drop our input.
1244 *
1245 * Note: an "input blockage" condition is assumed to exist if
1246 * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
1247 */
1248 static void
1249 zstty_softint(cs)
1250 struct zs_chanstate *cs;
1251 {
1252 register struct zstty_softc *zst;
1253 register struct tty *tp;
1254 register struct linesw *line;
1255 register int get, c, s, t;
1256 int ringmask, overrun;
1257 register u_short ring_data;
1258 register u_char rr0, delta;
1259
1260 zst = cs->cs_private;
1261 tp = zst->zst_tty;
1262 line = &linesw[tp->t_line];
1263 ringmask = zst->zst_ringmask;
1264 overrun = 0;
1265
1266 /*
1267 * Raise to tty priority while servicing the ring.
1268 */
1269 s = spltty();
1270
1271 if (zst->zst_rx_overrun) {
1272 zst->zst_rx_overrun = 0;
1273 zsoverrun(zst, &zst->zst_rotime, "ring");
1274 }
1275
1276 /*
1277 * Copy data from the receive ring into the tty layer.
1278 */
1279 get = zst->zst_rbget;
1280 while (get != zst->zst_rbput) {
1281 ring_data = zst->zst_rbuf[get];
1282 get = (get + 1) & ringmask;
1283
1284 if (ring_data & ZSRR1_DO)
1285 overrun++;
1286 /* low byte of ring_data is rr1 */
1287 c = (ring_data >> 8) & 0xff;
1288 if (ring_data & ZSRR1_FE)
1289 c |= TTY_FE;
1290 if (ring_data & ZSRR1_PE)
1291 c |= TTY_PE;
1292
1293 (*line->l_rint)(c, tp);
1294 }
1295 zst->zst_rbget = get;
1296
1297 /*
1298 * If the overrun flag is set now, it was set while
1299 * copying char/status pairs from the ring, which
1300 * means this was a hardware (fifo) overrun.
1301 */
1302 if (overrun) {
1303 zsoverrun(zst, &zst->zst_fotime, "fifo");
1304 }
1305
1306 /*
1307 * We have emptied the input ring. Maybe unblock input.
1308 * Note: an "input blockage" condition is assumed to exist
1309 * when EITHER zst_rx_blocked or the TS_TBLOCK flag is set,
1310 * so unblock here ONLY if TS_TBLOCK has not been set.
1311 */
1312 if (zst->zst_rx_blocked && ((tp->t_state & TS_TBLOCK) == 0)) {
1313 t = splzs();
1314 zst->zst_rx_blocked = 0;
1315 zs_hwiflow(zst);
1316 splx(t);
1317 }
1318
1319 /*
1320 * Do any deferred work for status interrupts.
1321 * The rr0 was saved in the h/w interrupt to
1322 * avoid another splzs in here.
1323 */
1324 if (zst->zst_st_check) {
1325 zst->zst_st_check = 0;
1326
1327 t = splzs();
1328 rr0 = cs->cs_rr0;
1329 delta = cs->cs_rr0_delta;
1330 cs->cs_rr0_delta = 0;
1331 splx(t);
1332
1333 if ((delta & cs->cs_rr0_dcd) != 0) {
1334 /*
1335 * Inform the tty layer that carrier detect changed.
1336 */
1337 (void) (*line->l_modem)(tp, (rr0 & cs->cs_rr0_dcd) != 0);
1338 }
1339
1340 if ((delta & cs->cs_rr0_cts) != 0) {
1341 /* Block or unblock output according to flow control. */
1342 if ((rr0 & cs->cs_rr0_cts) != 0) {
1343 zst->zst_tx_stopped = 0;
1344 (*line->l_start)(tp);
1345 } else {
1346 zst->zst_tx_stopped = 1;
1347 }
1348 }
1349 }
1350
1351 if (zst->zst_tx_done) {
1352 zst->zst_tx_done = 0;
1353
1354 tp->t_state &= ~TS_BUSY;
1355 if (tp->t_state & TS_FLUSH)
1356 tp->t_state &= ~TS_FLUSH;
1357 else
1358 ndflush(&tp->t_outq,
1359 (int)(zst->zst_tba - tp->t_outq.c_cf));
1360 (*line->l_start)(tp);
1361 }
1362
1363 splx(s);
1364 }
1365
1366 struct zsops zsops_tty = {
1367 zstty_rxint, /* receive char available */
1368 zstty_stint, /* external/status */
1369 zstty_txint, /* xmit buffer empty */
1370 zstty_softint, /* process software interrupt */
1371 };
1372
1373