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