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