z8530tty.c revision 1.20 1 /* $NetBSD: z8530tty.c,v 1.20 1997/11/01 15:51:23 mycroft 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 if ((tp->t_state & TS_ISOPEN) != 0 &&
370 (tp->t_state & TS_XCLUDE) != 0 &&
371 p->p_ucred->cr_uid != 0)
372 return (EBUSY);
373
374 s = spltty();
375
376 if ((tp->t_state & TS_ISOPEN) == 0) {
377 /* First open. */
378 struct termios t;
379
380 /* Turn on interrupts. */
381 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
382
383 /* Fetch the current modem control status, needed later. */
384 cs->cs_rr0 = zs_read_csr(cs);
385
386 /*
387 * Setup the "new" parameters in t.
388 * Can not use tp->t because zsparam
389 * deals only with what has changed.
390 */
391 t.c_ispeed = 0;
392 t.c_ospeed = cs->cs_defspeed;
393 t.c_cflag = cs->cs_defcflag;
394 if (zst->zst_swflags & TIOCFLAG_CLOCAL)
395 t.c_cflag |= CLOCAL;
396 if (zst->zst_swflags & TIOCFLAG_CRTSCTS)
397 t.c_cflag |= CRTSCTS;
398 if (zst->zst_swflags & TIOCFLAG_MDMBUF)
399 t.c_cflag |= MDMBUF;
400 /* Make sure zsparam will see changes. */
401 tp->t_ospeed = 0;
402 (void) zsparam(tp, &t);
403 /*
404 * Note: zsparam has done: cflag, ispeed, ospeed
405 * so we just need to do: iflag, oflag, lflag, cc
406 * For "raw" mode, just leave all zeros.
407 */
408 if ((zst->zst_hwflags & ZS_HWFLAG_RAW) == 0) {
409 tp->t_iflag = TTYDEF_IFLAG;
410 tp->t_oflag = TTYDEF_OFLAG;
411 tp->t_lflag = TTYDEF_LFLAG;
412 }
413 ttychars(tp);
414 ttsetwater(tp);
415
416 /*
417 * Turn on DTR. We must always do this, even if carrier is not
418 * present, because otherwise we'd have to use TIOCSDTR
419 * immediately after setting CLOCAL. We will drop DTR only on
420 * the next high-low transition of DCD, or by explicit request.
421 */
422 zs_modem(zst, 1);
423
424 /* Clear the input ring, and unblock. */
425 zst->zst_rbget = zst->zst_rbput;
426 zs_iflush(cs);
427 /* Turn on RTS. */
428 zs_hwiflow(zst, 0);
429 }
430 error = 0;
431
432 /* In this section, we may touch the chip. */
433 (void)splzs();
434
435 /* If we're doing a blocking open... */
436 if ((flags & O_NONBLOCK) == 0)
437 /* ...then wait for carrier. */
438 while ((tp->t_state & TS_CARR_ON) == 0 &&
439 (tp->t_cflag & (CLOCAL | MDMBUF)) == 0) {
440 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
441 ttopen, 0);
442 if (error) {
443 /*
444 * If the open was interrupted and nobody
445 * else has the device open, then hang up.
446 */
447 if ((tp->t_state & TS_ISOPEN) == 0) {
448 zs_modem(zst, 0);
449 tp->t_state &= ~TS_WOPEN;
450 ttwakeup(tp);
451 }
452 break;
453 }
454 tp->t_state |= TS_WOPEN;
455 }
456
457 splx(s);
458 if (error == 0)
459 error = (*linesw[tp->t_line].l_open)(dev, tp);
460 return (error);
461 }
462
463 /*
464 * Close a zs serial port.
465 */
466 int
467 zsclose(dev, flags, mode, p)
468 dev_t dev;
469 int flags;
470 int mode;
471 struct proc *p;
472 {
473 struct zstty_softc *zst;
474 register struct zs_chanstate *cs;
475 register struct tty *tp;
476 int s;
477
478 zst = zstty_cd.cd_devs[minor(dev)];
479 cs = zst->zst_cs;
480 tp = zst->zst_tty;
481
482 /* XXX This is for cons.c. */
483 if ((tp->t_state & TS_ISOPEN) == 0)
484 return 0;
485
486 (*linesw[tp->t_line].l_close)(tp, flags);
487 ttyclose(tp);
488
489 /* If we were asserting flow control, then deassert it. */
490 zs_hwiflow(zst, 1);
491 /* Clear any break condition set with TIOCSBRK. */
492 zs_break(cs, 0);
493 /*
494 * Hang up if necessary. Wait a bit, so the other side has time to
495 * notice even if we immediately open the port again.
496 */
497 if ((tp->t_cflag & HUPCL) != 0) {
498 zs_modem(zst, 0);
499 (void) tsleep(cs, TTIPRI, ttclos, hz);
500 }
501
502 s = splzs();
503 /* Turn off interrupts. */
504 cs->cs_creg[1] = cs->cs_preg[1] = 0;
505 zs_write_reg(cs, 1, cs->cs_creg[1]);
506 splx(s);
507
508 return (0);
509 }
510
511 /*
512 * Read/write zs serial port.
513 */
514 int
515 zsread(dev, uio, flags)
516 dev_t dev;
517 struct uio *uio;
518 int flags;
519 {
520 register struct zstty_softc *zst;
521 register struct tty *tp;
522
523 zst = zstty_cd.cd_devs[minor(dev)];
524 tp = zst->zst_tty;
525 return (linesw[tp->t_line].l_read(tp, uio, flags));
526 }
527
528 int
529 zswrite(dev, uio, flags)
530 dev_t dev;
531 struct uio *uio;
532 int flags;
533 {
534 register struct zstty_softc *zst;
535 register struct tty *tp;
536
537 zst = zstty_cd.cd_devs[minor(dev)];
538 tp = zst->zst_tty;
539 return (linesw[tp->t_line].l_write(tp, uio, flags));
540 }
541
542 #define TIOCFLAG_ALL (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL | \
543 TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF )
544
545 int
546 zsioctl(dev, cmd, data, flag, p)
547 dev_t dev;
548 u_long cmd;
549 caddr_t data;
550 int flag;
551 struct proc *p;
552 {
553 register struct zstty_softc *zst;
554 register struct zs_chanstate *cs;
555 register struct tty *tp;
556 register int error;
557
558 zst = zstty_cd.cd_devs[minor(dev)];
559 cs = zst->zst_cs;
560 tp = zst->zst_tty;
561
562 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
563 if (error >= 0)
564 return (error);
565
566 error = ttioctl(tp, cmd, data, flag, p);
567 if (error >= 0)
568 return (error);
569
570 #ifdef ZS_MD_IOCTL
571 error = ZS_MD_IOCTL;
572 if (error >= 0)
573 return (error);
574 #endif /* ZS_MD_IOCTL */
575
576 switch (cmd) {
577 case TIOCSBRK:
578 zs_break(cs, 1);
579 break;
580
581 case TIOCCBRK:
582 zs_break(cs, 0);
583 break;
584
585 case TIOCGFLAGS:
586 *(int *)data = zst->zst_swflags;
587 break;
588
589 case TIOCSFLAGS:
590 error = suser(p->p_ucred, &p->p_acflag);
591 if (error)
592 return (error);
593 zst->zst_swflags = *(int *)data;
594 break;
595
596 case TIOCSDTR:
597 zs_modem(zst, 1);
598 break;
599
600 case TIOCCDTR:
601 zs_modem(zst, 0);
602 break;
603
604 case TIOCMSET:
605 case TIOCMBIS:
606 case TIOCMBIC:
607 case TIOCMGET:
608 default:
609 return (ENOTTY);
610 }
611 return (0);
612 }
613
614 /*
615 * Start or restart transmission.
616 */
617 static void
618 zsstart(tp)
619 register struct tty *tp;
620 {
621 register struct zstty_softc *zst;
622 register struct zs_chanstate *cs;
623 register int s;
624
625 zst = zstty_cd.cd_devs[minor(tp->t_dev)];
626 cs = zst->zst_cs;
627
628 s = spltty();
629 if ((tp->t_state & TS_BUSY) != 0)
630 goto out;
631 if ((tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) != 0)
632 goto stopped;
633
634 if (zst->zst_tx_stopped)
635 goto stopped;
636
637 if (tp->t_outq.c_cc <= tp->t_lowat) {
638 if ((tp->t_state & TS_ASLEEP) != 0) {
639 tp->t_state &= ~TS_ASLEEP;
640 wakeup((caddr_t)&tp->t_outq);
641 }
642 selwakeup(&tp->t_wsel);
643 if (tp->t_outq.c_cc == 0)
644 goto stopped;
645 }
646
647 /* Grab the first contiguous region of buffer space. */
648 {
649 u_char *tba;
650 int tbc;
651
652 tba = tp->t_outq.c_cf;
653 tbc = ndqb(&tp->t_outq, 0);
654
655 (void) splzs();
656
657 zst->zst_tba = tba;
658 zst->zst_tbc = tbc;
659 }
660
661 tp->t_state |= TS_BUSY;
662 zst->zst_tx_busy = 1;
663
664 /* Enable transmit completion interrupts if necessary. */
665 if ((cs->cs_preg[1] & ZSWR1_TIE) == 0) {
666 cs->cs_preg[1] |= ZSWR1_TIE;
667 cs->cs_creg[1] = cs->cs_preg[1];
668 zs_write_reg(cs, 1, cs->cs_creg[1]);
669 }
670
671 /* Output the first character of the contiguous buffer. */
672 zs_write_data(cs, *zst->zst_tba);
673 zst->zst_tbc--;
674 zst->zst_tba++;
675 splx(s);
676 return;
677
678 stopped:
679 /* Disable transmit completion interrupts if necessary. */
680 if ((cs->cs_preg[1] & ZSWR1_TIE) != 0) {
681 cs->cs_preg[1] &= ~ZSWR1_TIE;
682 cs->cs_creg[1] = cs->cs_preg[1];
683 zs_write_reg(cs, 1, cs->cs_creg[1]);
684 }
685 out:
686 splx(s);
687 return;
688 }
689
690 /*
691 * Stop output, e.g., for ^S or output flush.
692 */
693 void
694 zsstop(tp, flag)
695 struct tty *tp;
696 int flag;
697 {
698 register struct zstty_softc *zst;
699 register struct zs_chanstate *cs;
700 register int s;
701
702 zst = zstty_cd.cd_devs[minor(tp->t_dev)];
703 cs = zst->zst_cs;
704
705 s = splzs();
706 if (tp->t_state & TS_BUSY) {
707 /*
708 * Device is transmitting; must stop it.
709 * Also clear _heldtbc to prevent any
710 * flow-control event from resuming.
711 */
712 zst->zst_tbc = 0;
713 zst->zst_heldtbc = 0;
714 if ((tp->t_state & TS_TTSTOP) == 0)
715 tp->t_state |= TS_FLUSH;
716 }
717 splx(s);
718 }
719
720 /*
721 * Set ZS tty parameters from termios.
722 * XXX - Should just copy the whole termios after
723 * making sure all the changes could be done.
724 */
725 static int
726 zsparam(tp, t)
727 register struct tty *tp;
728 register struct termios *t;
729 {
730 struct zstty_softc *zst;
731 struct zs_chanstate *cs;
732 int s, bps, cflag, error;
733 u_char tmp3, tmp4, tmp5;
734
735 zst = zstty_cd.cd_devs[minor(tp->t_dev)];
736 cs = zst->zst_cs;
737 bps = t->c_ospeed;
738 cflag = t->c_cflag;
739
740 if (bps < 0 || (t->c_ispeed && t->c_ispeed != bps))
741 return (EINVAL);
742
743 /*
744 * For the console, always force CLOCAL and !HUPCL, so that the port
745 * is always active.
746 */
747 if ((zst->zst_swflags & TIOCFLAG_SOFTCAR) != 0 ||
748 (zst->zst_hwflags & (ZS_HWFLAG_NO_DCD | ZS_HWFLAG_CONSOLE)) != 0) {
749 t->c_cflag |= CLOCAL;
750 t->c_cflag &= ~HUPCL;
751 }
752
753 /*
754 * Only whack the UART when params change.
755 * Some callers need to clear tp->t_ospeed
756 * to make sure initialization gets done.
757 */
758 if (tp->t_ospeed == bps &&
759 tp->t_cflag == cflag)
760 return (0);
761
762 /*
763 * Call MD functions to deal with changed
764 * clock modes or H/W flow control modes.
765 * The BRG divisor is set now. (reg 12,13)
766 */
767 error = zs_set_speed(cs, bps);
768 if (error)
769 return (error);
770 error = zs_set_modes(cs, cflag);
771 if (error)
772 return (error);
773
774 /* OK, we are now committed to do it. */
775 tp->t_cflag = cflag;
776 tp->t_ospeed = bps;
777 tp->t_ispeed = bps;
778
779 /*
780 * Block interrupts so that state will not
781 * be altered until we are done setting it up.
782 *
783 * Initial values in cs_preg are set before
784 * our attach routine is called. The master
785 * interrupt enable is handled by zsc.c
786 *
787 */
788 s = splzs();
789
790 /* Recompute character size bits. */
791 tmp3 = cs->cs_preg[3] & ~ZSWR3_RXSIZE;
792 tmp5 = cs->cs_preg[5] & ~ZSWR5_TXSIZE;
793 switch (cflag & CSIZE) {
794 case CS5:
795 /* These are |= 0 but let the optimizer deal with it. */
796 tmp3 |= ZSWR3_RX_5;
797 tmp5 |= ZSWR5_TX_5;
798 break;
799 case CS6:
800 tmp3 |= ZSWR3_RX_6;
801 tmp5 |= ZSWR5_TX_6;
802 break;
803 case CS7:
804 tmp3 |= ZSWR3_RX_7;
805 tmp5 |= ZSWR5_TX_7;
806 break;
807 case CS8:
808 default:
809 tmp3 |= ZSWR3_RX_8;
810 tmp5 |= ZSWR5_TX_8;
811 break;
812 }
813
814 #if 0
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 #endif
825
826 cs->cs_preg[3] = tmp3;
827 cs->cs_preg[5] = tmp5;
828
829 /*
830 * Recompute the stop bits and parity bits. Note that
831 * zs_set_speed() may have set clock selection bits etc.
832 * in wr4, so those must preserved.
833 */
834 tmp4 = cs->cs_preg[4];
835 /* Recompute stop bits. */
836 tmp4 &= ~ZSWR4_SBMASK;
837 tmp4 |= (cflag & CSTOPB) ?
838 ZSWR4_TWOSB : ZSWR4_ONESB;
839 /* Recompute parity bits. */
840 tmp4 &= ~ZSWR4_PARMASK;
841 if ((cflag & PARODD) == 0)
842 tmp4 |= ZSWR4_EVENP;
843 if (cflag & PARENB)
844 tmp4 |= ZSWR4_PARENB;
845 cs->cs_preg[4] = tmp4;
846
847 /* The MD function zs_set_modes handled CRTSCTS, etc. */
848
849 /*
850 * If nothing is being transmitted, set up new current values,
851 * else mark them as pending.
852 */
853 if (cs->cs_heldchange == 0) {
854 if (zst->zst_tx_busy) {
855 zst->zst_heldtbc = zst->zst_tbc;
856 zst->zst_tbc = 0;
857 cs->cs_heldchange = 0xFFFF;
858 } else {
859 zs_loadchannelregs(cs);
860 }
861 }
862
863 splx(s);
864
865 /*
866 * Update the tty layer's idea of the carrier bit, in case we changed
867 * CLOCAL or MDMBUF. We don't hang up here; we only do that if we
868 * lose carrier while carrier detection is on.
869 */
870 (void) (*linesw[tp->t_line].l_modem)(tp, cs->cs_rr0 & cs->cs_rr0_dcd);
871
872 /* If we can throttle input, enable "high water" detection. */
873 if (cflag & CHWFLOW) {
874 zst->zst_rbhiwat = zstty_rbuf_hiwat;
875 } else {
876 /* This impossible value prevents a "high water" trigger. */
877 zst->zst_rbhiwat = zstty_rbuf_size;
878 /* XXX: Lost hwi ability, so unblock and restart. */
879 zst->zst_rx_blocked = 0;
880 if (zst->zst_tx_stopped) {
881 zst->zst_tx_stopped = 0;
882 zsstart(tp);
883 }
884 }
885
886 return (0);
887 }
888
889 /*
890 * Raise or lower modem control (DTR/RTS) signals. If a character is
891 * in transmission, the change is deferred.
892 */
893 static void
894 zs_modem(zst, onoff)
895 struct zstty_softc *zst;
896 int onoff;
897 {
898 struct zs_chanstate *cs;
899 int s, clr, set;
900
901 cs = zst->zst_cs;
902 if (cs->cs_wr5_dtr == 0)
903 return;
904
905 if (onoff) {
906 clr = 0;
907 set = cs->cs_wr5_dtr;
908 } else {
909 clr = cs->cs_wr5_dtr;
910 set = 0;
911 }
912
913 s = splzs();
914 cs->cs_preg[5] &= ~clr;
915 cs->cs_preg[5] |= set;
916 if (cs->cs_heldchange == 0) {
917 if (zst->zst_tx_busy) {
918 zst->zst_heldtbc = zst->zst_tbc;
919 zst->zst_tbc = 0;
920 cs->cs_heldchange = (1<<5);
921 } else {
922 cs->cs_creg[5] = cs->cs_preg[5];
923 zs_write_reg(cs, 5, cs->cs_creg[5]);
924 }
925 }
926 splx(s);
927 }
928
929 /*
930 * Try to block or unblock input using hardware flow-control.
931 * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
932 * if this function returns non-zero, the TS_TBLOCK flag will
933 * be set or cleared according to the "stop" arg passed.
934 */
935 int
936 zshwiflow(tp, stop)
937 struct tty *tp;
938 int stop;
939 {
940 register struct zstty_softc *zst;
941 register struct zs_chanstate *cs;
942 int s;
943
944 zst = zstty_cd.cd_devs[minor(tp->t_dev)];
945 cs = zst->zst_cs;
946
947 /* Can not do this without some bit assigned as RTS. */
948 if (cs->cs_wr5_rts == 0)
949 return (0);
950
951 s = splzs();
952 if (stop) {
953 /*
954 * The tty layer is asking us to block input.
955 * If we already did it, just return TRUE.
956 */
957 if (zst->zst_rx_blocked)
958 goto out;
959 zst->zst_rx_blocked = 1;
960 } else {
961 /*
962 * The tty layer is asking us to resume input.
963 * The input ring is always empty by now.
964 */
965 zst->zst_rx_blocked = 0;
966 }
967 zs_hwiflow(zst, stop);
968 out:
969 splx(s);
970 return 1;
971 }
972
973 /*
974 * Internal version of zshwiflow
975 * called at splzs
976 */
977 static void
978 zs_hwiflow(zst, stop)
979 register struct zstty_softc *zst;
980 int stop;
981 {
982 register struct zs_chanstate *cs;
983 register int clr, set;
984
985 cs = zst->zst_cs;
986
987 if (cs->cs_wr5_rts == 0)
988 return;
989
990 if (stop) {
991 /* Block input (Lower RTS) */
992 clr = cs->cs_wr5_rts;
993 set = 0;
994 } else {
995 /* Unblock input (Raise RTS) */
996 clr = 0;
997 set = cs->cs_wr5_rts;
998 }
999
1000 cs->cs_preg[5] &= ~clr;
1001 cs->cs_preg[5] |= set;
1002 if (cs->cs_heldchange == 0) {
1003 if (zst->zst_tx_busy) {
1004 zst->zst_heldtbc = zst->zst_tbc;
1005 zst->zst_tbc = 0;
1006 cs->cs_heldchange = (1<<5);
1007 } else {
1008 cs->cs_creg[5] = cs->cs_preg[5];
1009 zs_write_reg(cs, 5, cs->cs_creg[5]);
1010 }
1011 }
1012 }
1013
1014
1015 /****************************************************************
1016 * Interface to the lower layer (zscc)
1017 ****************************************************************/
1018
1019 static void zstty_rxint __P((struct zs_chanstate *));
1020 static void zstty_txint __P((struct zs_chanstate *));
1021 static void zstty_stint __P((struct zs_chanstate *));
1022 static void zstty_softint __P((struct zs_chanstate *));
1023
1024 static void zsoverrun __P((struct zstty_softc *, long *, char *));
1025
1026 /*
1027 * receiver ready interrupt.
1028 * called at splzs
1029 */
1030 static void
1031 zstty_rxint(cs)
1032 register struct zs_chanstate *cs;
1033 {
1034 register struct zstty_softc *zst;
1035 register int cc, put, put_next, ringmask;
1036 register u_char c, rr0, rr1;
1037 register u_short ch_rr1;
1038
1039 zst = cs->cs_private;
1040 put = zst->zst_rbput;
1041 ringmask = zst->zst_ringmask;
1042
1043 nextchar:
1044
1045 /*
1046 * First read the status, because reading the received char
1047 * destroys the status of this char.
1048 */
1049 rr1 = zs_read_reg(cs, 1);
1050 c = zs_read_data(cs);
1051 ch_rr1 = (c << 8) | rr1;
1052
1053 if (ch_rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
1054 /* Clear the receive error. */
1055 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
1056 }
1057
1058 /* XXX: Check for the stop character? */
1059
1060 zst->zst_rbuf[put] = ch_rr1;
1061 put_next = (put + 1) & ringmask;
1062
1063 /* Would overrun if increment makes (put==get). */
1064 if (put_next == zst->zst_rbget) {
1065 zst->zst_rx_overrun = 1;
1066 } else {
1067 /* OK, really increment. */
1068 put = put_next;
1069 }
1070
1071 /* Keep reading until the FIFO is empty. */
1072 rr0 = zs_read_csr(cs);
1073 if (rr0 & ZSRR0_RX_READY)
1074 goto nextchar;
1075
1076 /* Done reading. */
1077 zst->zst_rbput = put;
1078
1079 /*
1080 * If ring is getting too full, try to block input.
1081 */
1082 cc = put - zst->zst_rbget;
1083 if (cc < 0)
1084 cc += zstty_rbuf_size;
1085 if ((cc > zst->zst_rbhiwat) && (zst->zst_rx_blocked == 0)) {
1086 zst->zst_rx_blocked = 1;
1087 zs_hwiflow(zst, 1);
1088 }
1089
1090 /* Ask for softint() call. */
1091 cs->cs_softreq = 1;
1092 }
1093
1094 /*
1095 * transmitter ready interrupt. (splzs)
1096 */
1097 static void
1098 zstty_txint(cs)
1099 register struct zs_chanstate *cs;
1100 {
1101 register struct zstty_softc *zst;
1102 register int count;
1103
1104 zst = cs->cs_private;
1105
1106 /*
1107 * If we suspended output for a "held" change,
1108 * then handle that now and resume.
1109 * Do flow-control changes ASAP.
1110 * When the only change is for flow control,
1111 * avoid hitting other registers, because that
1112 * often makes the stupid zs drop input...
1113 */
1114 if (cs->cs_heldchange) {
1115 if (cs->cs_heldchange == (1<<5)) {
1116 /* Avoid whacking the chip... */
1117 cs->cs_creg[5] = cs->cs_preg[5];
1118 zs_write_reg(cs, 5, cs->cs_creg[5]);
1119 } else
1120 zs_loadchannelregs(cs);
1121 cs->cs_heldchange = 0;
1122 count = zst->zst_heldtbc;
1123 } else
1124 count = zst->zst_tbc;
1125
1126 /*
1127 * If our transmit buffer still has data,
1128 * just send the next character.
1129 */
1130 if (count > 0) {
1131 /* Send the next char. */
1132 zst->zst_tbc = --count;
1133 zs_write_data(cs, *zst->zst_tba);
1134 zst->zst_tba++;
1135 return;
1136 }
1137
1138 zs_write_csr(cs, ZSWR0_RESET_TXINT);
1139
1140 /* Ask the softint routine for more output. */
1141 zst->zst_tx_busy = 0;
1142 zst->zst_tx_done = 1;
1143 cs->cs_softreq = 1;
1144 }
1145
1146 /*
1147 * status change interrupt. (splzs)
1148 */
1149 static void
1150 zstty_stint(cs)
1151 register struct zs_chanstate *cs;
1152 {
1153 register struct zstty_softc *zst;
1154 register u_char rr0, delta;
1155
1156 zst = cs->cs_private;
1157
1158 rr0 = zs_read_csr(cs);
1159 zs_write_csr(cs, ZSWR0_RESET_STATUS);
1160
1161 /*
1162 * Check here for console break, so that we can abort
1163 * even when interrupts are locking up the machine.
1164 */
1165 if ((rr0 & ZSRR0_BREAK) &&
1166 (zst->zst_hwflags & ZS_HWFLAG_CONSOLE))
1167 {
1168 zs_abort(cs);
1169 return;
1170 }
1171
1172 /*
1173 * We have to accumulate status line changes here.
1174 * Otherwise, if we get multiple status interrupts
1175 * before the softint runs, we could fail to notice
1176 * some status line changes in the softint routine.
1177 * Fix from Bill Studenmund, October 1996.
1178 */
1179 delta = (cs->cs_rr0 ^ rr0);
1180 cs->cs_rr0_delta |= delta;
1181 cs->cs_rr0 = rr0;
1182
1183 /*
1184 * Need to handle CTS output flow control here.
1185 * Output remains stopped as long as either the
1186 * zst_tx_stopped or TS_TTSTOP flag is set.
1187 * Never restart here; the softint routine will
1188 * do that after things are ready to move.
1189 */
1190 if ((delta & cs->cs_rr0_cts) &&
1191 ((rr0 & cs->cs_rr0_cts) == 0))
1192 {
1193 zst->zst_tbc = 0;
1194 zst->zst_heldtbc = 0;
1195 zst->zst_tx_stopped = 1;
1196 }
1197 zst->zst_st_check = 1;
1198
1199 /* Ask for softint() call. */
1200 cs->cs_softreq = 1;
1201 }
1202
1203 /*
1204 * Print out a ring or fifo overrun error message.
1205 */
1206 static void
1207 zsoverrun(zst, ptime, what)
1208 struct zstty_softc *zst;
1209 long *ptime;
1210 char *what;
1211 {
1212
1213 if (*ptime != time.tv_sec) {
1214 *ptime = time.tv_sec;
1215 log(LOG_WARNING, "%s: %s overrun\n",
1216 zst->zst_dev.dv_xname, what);
1217 }
1218 }
1219
1220 /*
1221 * Software interrupt. Called at zssoft
1222 *
1223 * The main job to be done here is to empty the input ring
1224 * by passing its contents up to the tty layer. The ring is
1225 * always emptied during this operation, therefore the ring
1226 * must not be larger than the space after "high water" in
1227 * the tty layer, or the tty layer might drop our input.
1228 *
1229 * Note: an "input blockage" condition is assumed to exist if
1230 * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
1231 */
1232 static void
1233 zstty_softint(cs)
1234 struct zs_chanstate *cs;
1235 {
1236 register struct zstty_softc *zst;
1237 register struct linesw *line;
1238 register struct tty *tp;
1239 register int get, c, s, t;
1240 int ringmask, overrun;
1241 register u_short ring_data;
1242 register u_char rr0, delta;
1243
1244 zst = cs->cs_private;
1245 tp = zst->zst_tty;
1246 line = &linesw[tp->t_line];
1247 ringmask = zst->zst_ringmask;
1248 overrun = 0;
1249
1250 /*
1251 * Raise to tty priority while servicing the ring.
1252 */
1253 s = spltty();
1254
1255 if (zst->zst_rx_overrun) {
1256 zst->zst_rx_overrun = 0;
1257 zsoverrun(zst, &zst->zst_rotime, "ring");
1258 }
1259
1260 /*
1261 * Copy data from the receive ring into the tty layer.
1262 */
1263 get = zst->zst_rbget;
1264 while (get != zst->zst_rbput) {
1265 ring_data = zst->zst_rbuf[get];
1266 get = (get + 1) & ringmask;
1267
1268 if (ring_data & ZSRR1_DO)
1269 overrun++;
1270 /* low byte of ring_data is rr1 */
1271 c = (ring_data >> 8) & 0xff;
1272 if (ring_data & ZSRR1_FE)
1273 c |= TTY_FE;
1274 if (ring_data & ZSRR1_PE)
1275 c |= TTY_PE;
1276
1277 line->l_rint(c, tp);
1278 }
1279 zst->zst_rbget = get;
1280
1281 /*
1282 * If the overrun flag is set now, it was set while
1283 * copying char/status pairs from the ring, which
1284 * means this was a hardware (fifo) overrun.
1285 */
1286 if (overrun) {
1287 zsoverrun(zst, &zst->zst_fotime, "fifo");
1288 }
1289
1290 /*
1291 * We have emptied the input ring. Maybe unblock input.
1292 * Note: an "input blockage" condition is assumed to exist
1293 * when EITHER zst_rx_blocked or the TS_TBLOCK flag is set,
1294 * so unblock here ONLY if TS_TBLOCK has not been set.
1295 */
1296 if (zst->zst_rx_blocked && ((tp->t_state & TS_TBLOCK) == 0)) {
1297 t = splzs();
1298 zst->zst_rx_blocked = 0;
1299 zs_hwiflow(zst, 0); /* unblock input */
1300 splx(t);
1301 }
1302
1303 /*
1304 * Do any deferred work for status interrupts.
1305 * The rr0 was saved in the h/w interrupt to
1306 * avoid another splzs in here.
1307 */
1308 if (zst->zst_st_check) {
1309 zst->zst_st_check = 0;
1310
1311 t = splzs();
1312 rr0 = cs->cs_rr0;
1313 delta = cs->cs_rr0_delta;
1314 cs->cs_rr0_delta = 0;
1315 splx(t);
1316
1317 /* Note, the MD code may use DCD for something else. */
1318 if (delta & cs->cs_rr0_dcd) {
1319 c = ((rr0 & cs->cs_rr0_dcd) != 0);
1320 if (line->l_modem(tp, c) == 0)
1321 zs_modem(zst, c);
1322 }
1323
1324 /* Note, cs_rr0_cts is set only with H/W flow control. */
1325 if (delta & cs->cs_rr0_cts) {
1326 /*
1327 * Only do restart here. Stop is handled
1328 * at the h/w interrupt level.
1329 */
1330 if (rr0 & cs->cs_rr0_cts) {
1331 zst->zst_tx_stopped = 0;
1332 /* tp->t_state &= ~TS_TTSTOP; */
1333 (*line->l_start)(tp);
1334 }
1335 }
1336 }
1337
1338 if (zst->zst_tx_done) {
1339 zst->zst_tx_done = 0;
1340 tp->t_state &= ~TS_BUSY;
1341 if (tp->t_state & TS_FLUSH)
1342 tp->t_state &= ~TS_FLUSH;
1343 else
1344 ndflush(&tp->t_outq, zst->zst_tba -
1345 (caddr_t) tp->t_outq.c_cf);
1346 line->l_start(tp);
1347 }
1348
1349 splx(s);
1350 }
1351
1352 struct zsops zsops_tty = {
1353 zstty_rxint, /* receive char available */
1354 zstty_stint, /* external/status */
1355 zstty_txint, /* xmit buffer empty */
1356 zstty_softint, /* process software interrupt */
1357 };
1358
1359