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