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