z8530tty.c revision 1.131 1 /* $NetBSD: z8530tty.c,v 1.131 2014/11/15 19:18:18 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1993, 1994, 1995, 1996, 1997, 1998, 1999
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) 1992, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * This software was developed by the Computer Systems Engineering group
38 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
39 * contributed to Berkeley.
40 *
41 * All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Lawrence Berkeley Laboratory.
45 *
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
48 * are met:
49 * 1. Redistributions of source code must retain the above copyright
50 * notice, this list of conditions and the following disclaimer.
51 * 2. Redistributions in binary form must reproduce the above copyright
52 * notice, this list of conditions and the following disclaimer in the
53 * documentation and/or other materials provided with the distribution.
54 * 3. Neither the name of the University nor the names of its contributors
55 * may be used to endorse or promote products derived from this software
56 * without specific prior written permission.
57 *
58 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 * SUCH DAMAGE.
69 *
70 * @(#)zs.c 8.1 (Berkeley) 7/19/93
71 */
72
73 /*
74 * Copyright (c) 1994 Gordon W. Ross
75 *
76 * This software was developed by the Computer Systems Engineering group
77 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
78 * contributed to Berkeley.
79 *
80 * All advertising materials mentioning features or use of this software
81 * must display the following acknowledgement:
82 * This product includes software developed by the University of
83 * California, Lawrence Berkeley Laboratory.
84 *
85 * Redistribution and use in source and binary forms, with or without
86 * modification, are permitted provided that the following conditions
87 * are met:
88 * 1. Redistributions of source code must retain the above copyright
89 * notice, this list of conditions and the following disclaimer.
90 * 2. Redistributions in binary form must reproduce the above copyright
91 * notice, this list of conditions and the following disclaimer in the
92 * documentation and/or other materials provided with the distribution.
93 * 3. All advertising materials mentioning features or use of this software
94 * must display the following acknowledgement:
95 * This product includes software developed by the University of
96 * California, Berkeley and its contributors.
97 * 4. Neither the name of the University nor the names of its contributors
98 * may be used to endorse or promote products derived from this software
99 * without specific prior written permission.
100 *
101 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
102 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
103 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
104 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
105 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
106 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
107 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
108 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
109 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
110 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
111 * SUCH DAMAGE.
112 *
113 * @(#)zs.c 8.1 (Berkeley) 7/19/93
114 */
115
116 /*
117 * Zilog Z8530 Dual UART driver (tty interface)
118 *
119 * This is the "slave" driver that will be attached to
120 * the "zsc" driver for plain "tty" async. serial lines.
121 *
122 * Credits, history:
123 *
124 * The original version of this code was the sparc/dev/zs.c driver
125 * as distributed with the Berkeley 4.4 Lite release. Since then,
126 * Gordon Ross reorganized the code into the current parent/child
127 * driver scheme, separating the Sun keyboard and mouse support
128 * into independent child drivers.
129 *
130 * RTS/CTS flow-control support was a collaboration of:
131 * Gordon Ross <gwr (at) NetBSD.org>,
132 * Bill Studenmund <wrstuden (at) loki.stanford.edu>
133 * Ian Dall <Ian.Dall (at) dsto.defence.gov.au>
134 *
135 * The driver was massively overhauled in November 1997 by Charles Hannum,
136 * fixing *many* bugs, and substantially improving performance.
137 */
138
139 #include <sys/cdefs.h>
140 __KERNEL_RCSID(0, "$NetBSD: z8530tty.c,v 1.131 2014/11/15 19:18:18 christos Exp $");
141
142 #include "opt_kgdb.h"
143 #include "opt_ntp.h"
144
145 #include <sys/param.h>
146 #include <sys/systm.h>
147 #include <sys/proc.h>
148 #include <sys/device.h>
149 #include <sys/conf.h>
150 #include <sys/file.h>
151 #include <sys/ioctl.h>
152 #include <sys/malloc.h>
153 #include <sys/timepps.h>
154 #include <sys/tty.h>
155 #include <sys/time.h>
156 #include <sys/kernel.h>
157 #include <sys/syslog.h>
158 #include <sys/kauth.h>
159
160 #include <dev/ic/z8530reg.h>
161 #include <machine/z8530var.h>
162
163 #include <dev/cons.h>
164
165 #include "ioconf.h"
166 #include "locators.h"
167
168 /*
169 * How many input characters we can buffer.
170 * The port-specific var.h may override this.
171 * Note: must be a power of two!
172 */
173 #ifndef ZSTTY_RING_SIZE
174 #define ZSTTY_RING_SIZE 2048
175 #endif
176
177 static struct cnm_state zstty_cnm_state;
178 /*
179 * Make this an option variable one can patch.
180 * But be warned: this must be a power of 2!
181 */
182 u_int zstty_rbuf_size = ZSTTY_RING_SIZE;
183
184 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
185 u_int zstty_rbuf_hiwat = (ZSTTY_RING_SIZE * 1) / 4;
186 u_int zstty_rbuf_lowat = (ZSTTY_RING_SIZE * 3) / 4;
187
188 struct zstty_softc {
189 device_t zst_dev; /* required first: base device */
190 struct tty *zst_tty;
191 struct zs_chanstate *zst_cs;
192
193 struct callout zst_diag_ch;
194
195 u_int zst_overflows,
196 zst_floods,
197 zst_errors;
198
199 int zst_hwflags, /* see z8530var.h */
200 zst_swflags; /* TIOCFLAG_SOFTCAR, ... <ttycom.h> */
201
202 u_int zst_r_hiwat,
203 zst_r_lowat;
204 uint8_t *volatile zst_rbget,
205 *volatile zst_rbput;
206 volatile u_int zst_rbavail;
207 uint8_t *zst_rbuf,
208 *zst_ebuf;
209
210 /*
211 * The transmit byte count and address are used for pseudo-DMA
212 * output in the hardware interrupt code. PDMA can be suspended
213 * to get pending changes done; heldtbc is used for this. It can
214 * also be stopped for ^S; this sets TS_TTSTOP in tp->t_state.
215 */
216 uint8_t *zst_tba; /* transmit buffer address */
217 u_int zst_tbc, /* transmit byte count */
218 zst_heldtbc; /* held tbc while xmission stopped */
219
220 /* Flags to communicate with zstty_softint() */
221 volatile uint8_t zst_rx_flags, /* receiver blocked */
222 #define RX_TTY_BLOCKED 0x01
223 #define RX_TTY_OVERFLOWED 0x02
224 #define RX_IBUF_BLOCKED 0x04
225 #define RX_IBUF_OVERFLOWED 0x08
226 #define RX_ANY_BLOCK 0x0f
227 zst_tx_busy, /* working on an output chunk */
228 zst_tx_done, /* done with one output chunk */
229 zst_tx_stopped, /* H/W level stop (lost CTS) */
230 zst_st_check, /* got a status interrupt */
231 zst_rx_ready;
232
233 /* PPS signal on DCD, with or without inkernel clock disciplining */
234 uint8_t zst_ppsmask; /* pps signal mask */
235 struct pps_state zst_pps_state;
236 };
237
238 /* Definition of the driver for autoconfig. */
239 static int zstty_match(device_t, cfdata_t, void *);
240 static void zstty_attach(device_t, device_t, void *);
241
242 CFATTACH_DECL_NEW(zstty, sizeof(struct zstty_softc),
243 zstty_match, zstty_attach, NULL, NULL);
244
245 dev_type_open(zsopen);
246 dev_type_close(zsclose);
247 dev_type_read(zsread);
248 dev_type_write(zswrite);
249 dev_type_ioctl(zsioctl);
250 dev_type_stop(zsstop);
251 dev_type_tty(zstty);
252 dev_type_poll(zspoll);
253
254 const struct cdevsw zstty_cdevsw = {
255 .d_open = zsopen,
256 .d_close = zsclose,
257 .d_read = zsread,
258 .d_write = zswrite,
259 .d_ioctl = zsioctl,
260 .d_stop = zsstop,
261 .d_tty = zstty,
262 .d_poll = zspoll,
263 .d_mmap = nommap,
264 .d_kqfilter = ttykqfilter,
265 .d_discard = nodiscard,
266 .d_flag = D_TTY
267 };
268
269 struct zsops zsops_tty;
270
271 static void zs_shutdown(struct zstty_softc *);
272 static void zsstart(struct tty *);
273 static int zsparam(struct tty *, struct termios *);
274 static void zs_modem(struct zstty_softc *, int);
275 static void tiocm_to_zs(struct zstty_softc *, u_long, int);
276 static int zs_to_tiocm(struct zstty_softc *);
277 static int zshwiflow(struct tty *, int);
278 static void zs_hwiflow(struct zstty_softc *);
279 static void zs_maskintr(struct zstty_softc *);
280
281 /* Low-level routines. */
282 static void zstty_rxint (struct zs_chanstate *);
283 static void zstty_stint (struct zs_chanstate *, int);
284 static void zstty_txint (struct zs_chanstate *);
285 static void zstty_softint(struct zs_chanstate *);
286 static void zstty_softint1(struct zs_chanstate *);
287
288 #define ZSUNIT(x) TTUNIT(x)
289 #define ZSDIALOUT(x) TTDIALOUT(x)
290
291 struct tty *zstty_get_tty_from_dev(device_t);
292
293 /*
294 * XXX get the (struct tty *) out of a (device_t) we trust to be a
295 * (struct zstty_softc *) - needed by sparc/dev/zs.c, sparc64/dev/zs.c,
296 * sun3/dev/zs.c and sun2/dev/zs.c will probably need it at some point
297 */
298
299 struct tty *
300 zstty_get_tty_from_dev(device_t dev)
301 {
302 struct zstty_softc *sc = device_private(dev);
303
304 return sc->zst_tty;
305 }
306
307 /*
308 * zstty_match: how is this zs channel configured?
309 */
310 int
311 zstty_match(device_t parent, cfdata_t cf, void *aux)
312 {
313 struct zsc_attach_args *args = aux;
314
315 /* Exact match is better than wildcard. */
316 if (cf->zsccf_channel == args->channel)
317 return 2;
318
319 /* This driver accepts wildcard. */
320 if (cf->zsccf_channel == ZSCCF_CHANNEL_DEFAULT)
321 return 1;
322
323 return 0;
324 }
325
326 void
327 zstty_attach(device_t parent, device_t self, void *aux)
328 {
329 struct zstty_softc *zst = device_private(self);
330 struct zsc_softc *zsc = device_private(parent);
331 cfdata_t cf = device_cfdata(self);
332 struct zsc_attach_args *args = aux;
333 struct zs_chanstate *cs;
334 struct tty *tp;
335 int channel, tty_unit;
336 dev_t dev;
337 const char *i, *o;
338 int dtr_on;
339 int resetbit;
340
341 zst->zst_dev = self;
342
343 callout_init(&zst->zst_diag_ch, 0);
344 cn_init_magic(&zstty_cnm_state);
345
346 tty_unit = device_unit(self);
347 channel = args->channel;
348 cs = zsc->zsc_cs[channel];
349 cs->cs_private = zst;
350 cs->cs_ops = &zsops_tty;
351
352 zst->zst_cs = cs;
353 zst->zst_swflags = cf->cf_flags; /* softcar, etc. */
354 zst->zst_hwflags = args->hwflags;
355 dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), tty_unit);
356
357 if (zst->zst_swflags)
358 aprint_normal(" flags 0x%x", zst->zst_swflags);
359
360 /*
361 * Check whether we serve as a console device.
362 * XXX - split console input/output channels aren't
363 * supported yet on /dev/console
364 */
365 i = o = NULL;
366 if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
367 i = "input";
368 if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
369 args->consdev->cn_dev = dev;
370 cn_tab->cn_pollc = args->consdev->cn_pollc;
371 cn_tab->cn_getc = args->consdev->cn_getc;
372 }
373 cn_tab->cn_dev = dev;
374 /* Set console magic to BREAK */
375 cn_set_magic("\047\001");
376 }
377 if ((zst->zst_hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
378 o = "output";
379 if ((args->hwflags & ZS_HWFLAG_USE_CONSDEV) != 0) {
380 cn_tab->cn_putc = args->consdev->cn_putc;
381 }
382 cn_tab->cn_dev = dev;
383 }
384 if (i != NULL || o != NULL)
385 aprint_normal(" (console %s)", i ? (o ? "i/o" : i) : o);
386
387 #ifdef KGDB
388 if (zs_check_kgdb(cs, dev)) {
389 /*
390 * Allow kgdb to "take over" this port. Returns true
391 * if this serial port is in-use by kgdb.
392 */
393 aprint_normal(" (kgdb)\n");
394 /*
395 * This is the kgdb port (exclusive use)
396 * so skip the normal attach code.
397 */
398 return;
399 }
400 #endif
401 aprint_normal("\n");
402
403 tp = tty_alloc();
404 tp->t_dev = dev;
405 tp->t_oproc = zsstart;
406 tp->t_param = zsparam;
407 tp->t_hwiflow = zshwiflow;
408 tty_attach(tp);
409
410 zst->zst_tty = tp;
411 zst->zst_rbuf = malloc(zstty_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
412 if (zst->zst_rbuf == NULL) {
413 aprint_error_dev(zst->zst_dev,
414 "unable to allocate ring buffer\n");
415 return;
416 }
417 zst->zst_ebuf = zst->zst_rbuf + (zstty_rbuf_size << 1);
418 /* Disable the high water mark. */
419 zst->zst_r_hiwat = 0;
420 zst->zst_r_lowat = 0;
421 zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
422 zst->zst_rbavail = zstty_rbuf_size;
423
424 /* if there are no enable/disable functions, assume the device
425 is always enabled */
426 if (!cs->enable)
427 cs->enabled = 1;
428
429 /*
430 * Hardware init
431 */
432 dtr_on = 0;
433 resetbit = 0;
434 if (ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
435 /* Call zsparam similar to open. */
436 struct termios t;
437
438 /* Wait a while for previous console output to complete */
439 DELAY(10000);
440
441 /* Setup the "new" parameters in t. */
442 t.c_ispeed = 0;
443 t.c_ospeed = cs->cs_defspeed;
444 t.c_cflag = cs->cs_defcflag;
445
446 /*
447 * Turn on receiver and status interrupts.
448 * We defer the actual write of the register to zsparam(),
449 * but we must make sure status interrupts are turned on by
450 * the time zsparam() reads the initial rr0 state.
451 */
452 SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
453
454 /* Make sure zsparam will see changes. */
455 tp->t_ospeed = 0;
456 (void) zsparam(tp, &t);
457
458 /* Make sure DTR is on now. */
459 dtr_on = 1;
460
461 } else if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_NORESET)) {
462 /* Not the console; may need reset. */
463 resetbit = (channel == 0) ? ZSWR9_A_RESET : ZSWR9_B_RESET;
464 }
465
466 mutex_spin_enter(&cs->cs_lock);
467 if (resetbit)
468 zs_write_reg(cs, 9, resetbit);
469 zs_modem(zst, dtr_on);
470 mutex_spin_exit(&cs->cs_lock);
471 }
472
473
474 /*
475 * Return pointer to our tty.
476 */
477 struct tty *
478 zstty(dev_t dev)
479 {
480 struct zstty_softc *zst;
481
482 zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
483
484 return (zst->zst_tty);
485 }
486
487
488 void
489 zs_shutdown(struct zstty_softc *zst)
490 {
491 struct zs_chanstate *cs = zst->zst_cs;
492 struct tty *tp = zst->zst_tty;
493
494 mutex_spin_enter(&cs->cs_lock);
495
496 /* If we were asserting flow control, then deassert it. */
497 SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
498 zs_hwiflow(zst);
499
500 /* Clear any break condition set with TIOCSBRK. */
501 zs_break(cs, 0);
502
503 /*
504 * Hang up if necessary. Wait a bit, so the other side has time to
505 * notice even if we immediately open the port again.
506 */
507 if (ISSET(tp->t_cflag, HUPCL)) {
508 zs_modem(zst, 0);
509 mutex_spin_exit(&cs->cs_lock);
510 /*
511 * XXX - another process is not prevented from opening
512 * the device during our sleep.
513 */
514 (void) tsleep(cs, TTIPRI, ttclos, hz);
515 /* Re-check state in case we were opened during our sleep */
516 if (ISSET(tp->t_state, TS_ISOPEN) || tp->t_wopen != 0)
517 return;
518
519 mutex_spin_enter(&cs->cs_lock);
520 }
521
522 /* Turn off interrupts if not the console. */
523 if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
524 CLR(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
525 cs->cs_creg[1] = cs->cs_preg[1];
526 zs_write_reg(cs, 1, cs->cs_creg[1]);
527 }
528
529 /* Call the power management hook. */
530 if (cs->disable) {
531 #ifdef DIAGNOSTIC
532 if (!cs->enabled)
533 panic("%s: not enabled?", __func__);
534 #endif
535 (*cs->disable)(zst->zst_cs);
536 }
537
538 mutex_spin_exit(&cs->cs_lock);
539 }
540
541 /*
542 * Open a zs serial (tty) port.
543 */
544 int
545 zsopen(dev_t dev, int flags, int mode, struct lwp *l)
546 {
547 struct zstty_softc *zst;
548 struct zs_chanstate *cs;
549 struct tty *tp;
550 int error;
551
552 zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
553 if (zst == NULL)
554 return (ENXIO);
555
556 tp = zst->zst_tty;
557 cs = zst->zst_cs;
558
559 /* If KGDB took the line, then tp==NULL */
560 if (tp == NULL)
561 return (EBUSY);
562
563 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
564 return (EBUSY);
565
566 mutex_spin_enter(&tty_lock);
567
568 /*
569 * Do the following iff this is a first open.
570 */
571 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
572 struct termios t;
573
574 tp->t_dev = dev;
575
576 /* Call the power management hook. */
577 if (cs->enable) {
578 if ((*cs->enable)(cs)) {
579 mutex_spin_exit(&tty_lock);
580 printf("%s: device enable failed\n",
581 device_xname(zst->zst_dev));
582 return (EIO);
583 }
584 }
585
586 /*
587 * Initialize the termios status to the defaults. Add in the
588 * sticky bits from TIOCSFLAGS.
589 */
590 t.c_ispeed = 0;
591 t.c_ospeed = cs->cs_defspeed;
592 t.c_cflag = cs->cs_defcflag;
593 if (ISSET(zst->zst_swflags, TIOCFLAG_CLOCAL))
594 SET(t.c_cflag, CLOCAL);
595 if (ISSET(zst->zst_swflags, TIOCFLAG_CRTSCTS))
596 SET(t.c_cflag, CRTSCTS);
597 if (ISSET(zst->zst_swflags, TIOCFLAG_CDTRCTS))
598 SET(t.c_cflag, CDTRCTS);
599 if (ISSET(zst->zst_swflags, TIOCFLAG_MDMBUF))
600 SET(t.c_cflag, MDMBUF);
601
602 mutex_spin_enter(&cs->cs_lock);
603
604 /*
605 * Turn on receiver and status interrupts.
606 * We defer the actual write of the register to zsparam(),
607 * but we must make sure status interrupts are turned on by
608 * the time zsparam() reads the initial rr0 state.
609 */
610 SET(cs->cs_preg[1], ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE);
611
612 /* Clear PPS capture state on first open. */
613 mutex_spin_enter(&timecounter_lock);
614 zst->zst_ppsmask = 0;
615 memset(&zst->zst_pps_state, 0, sizeof(zst->zst_pps_state));
616 zst->zst_pps_state.ppscap =
617 PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
618 pps_init(&zst->zst_pps_state);
619 mutex_spin_exit(&timecounter_lock);
620
621 mutex_spin_exit(&cs->cs_lock);
622
623 /* Make sure zsparam will see changes. */
624 tp->t_ospeed = 0;
625 (void) zsparam(tp, &t);
626
627 /*
628 * Note: zsparam has done: cflag, ispeed, ospeed
629 * so we just need to do: iflag, oflag, lflag, cc
630 * For "raw" mode, just leave all zeros.
631 */
632 if (!ISSET(zst->zst_hwflags, ZS_HWFLAG_RAW)) {
633 tp->t_iflag = TTYDEF_IFLAG;
634 tp->t_oflag = TTYDEF_OFLAG;
635 tp->t_lflag = TTYDEF_LFLAG;
636 } else {
637 tp->t_iflag = 0;
638 tp->t_oflag = 0;
639 tp->t_lflag = 0;
640 }
641 ttychars(tp);
642 ttsetwater(tp);
643
644 mutex_spin_enter(&cs->cs_lock);
645
646 /*
647 * Turn on DTR. We must always do this, even if carrier is not
648 * present, because otherwise we'd have to use TIOCSDTR
649 * immediately after setting CLOCAL, which applications do not
650 * expect. We always assert DTR while the device is open
651 * unless explicitly requested to deassert it.
652 */
653 zs_modem(zst, 1);
654
655 /* Clear the input ring, and unblock. */
656 zst->zst_rbget = zst->zst_rbput = zst->zst_rbuf;
657 zst->zst_rbavail = zstty_rbuf_size;
658 zs_iflush(cs);
659 CLR(zst->zst_rx_flags, RX_ANY_BLOCK);
660 zs_hwiflow(zst);
661
662 mutex_spin_exit(&cs->cs_lock);
663 }
664
665 mutex_spin_exit(&tty_lock);
666
667 error = ttyopen(tp, ZSDIALOUT(dev), ISSET(flags, O_NONBLOCK));
668 if (error)
669 goto bad;
670
671 error = (*tp->t_linesw->l_open)(dev, tp);
672 if (error)
673 goto bad;
674
675 return (0);
676
677 bad:
678 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
679 /*
680 * We failed to open the device, and nobody else had it opened.
681 * Clean up the state as appropriate.
682 */
683 zs_shutdown(zst);
684 }
685
686 return (error);
687 }
688
689 /*
690 * Close a zs serial port.
691 */
692 int
693 zsclose(dev_t dev, int flags, int mode, struct lwp *l)
694 {
695 struct zstty_softc *zst;
696 struct tty *tp;
697
698 zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
699 tp = zst->zst_tty;
700
701 /* XXX This is for cons.c. */
702 if (!ISSET(tp->t_state, TS_ISOPEN))
703 return 0;
704
705 (*tp->t_linesw->l_close)(tp, flags);
706 ttyclose(tp);
707
708 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
709 /*
710 * Although we got a last close, the device may still be in
711 * use; e.g. if this was the dialout node, and there are still
712 * processes waiting for carrier on the non-dialout node.
713 */
714 zs_shutdown(zst);
715 }
716
717 return (0);
718 }
719
720 /*
721 * Read/write zs serial port.
722 */
723 int
724 zsread(dev_t dev, struct uio *uio, int flags)
725 {
726 struct zstty_softc *zst;
727 struct tty *tp;
728
729 zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
730 tp = zst->zst_tty;
731
732 return ((*tp->t_linesw->l_read)(tp, uio, flags));
733 }
734
735 int
736 zswrite(dev_t dev, struct uio *uio, int flags)
737 {
738 struct zstty_softc *zst;
739 struct tty *tp;
740
741 zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
742 tp = zst->zst_tty;
743
744 return ((*tp->t_linesw->l_write)(tp, uio, flags));
745 }
746
747 int
748 zspoll(dev_t dev, int events, struct lwp *l)
749 {
750 struct zstty_softc *zst;
751 struct tty *tp;
752
753 zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
754 tp = zst->zst_tty;
755
756 return ((*tp->t_linesw->l_poll)(tp, events, l));
757 }
758
759 int
760 zsioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
761 {
762 struct zstty_softc *zst;
763 struct zs_chanstate *cs;
764 struct tty *tp;
765 int error;
766
767 zst = device_lookup_private(&zstty_cd, ZSUNIT(dev));
768 cs = zst->zst_cs;
769 tp = zst->zst_tty;
770 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
771 if (error != EPASSTHROUGH)
772 return (error);
773
774 error = ttioctl(tp, cmd, data, flag, l);
775 if (error != EPASSTHROUGH)
776 return (error);
777
778 #ifdef ZS_MD_IOCTL
779 error = ZS_MD_IOCTL(cs, cmd, data);
780 if (error != EPASSTHROUGH)
781 return (error);
782 #endif /* ZS_MD_IOCTL */
783
784 error = 0;
785
786 mutex_spin_enter(&cs->cs_lock);
787
788 switch (cmd) {
789 case TIOCSBRK:
790 zs_break(cs, 1);
791 break;
792
793 case TIOCCBRK:
794 zs_break(cs, 0);
795 break;
796
797 case TIOCGFLAGS:
798 *(int *)data = zst->zst_swflags;
799 break;
800
801 case TIOCSFLAGS:
802 error = kauth_authorize_device_tty(l->l_cred,
803 KAUTH_DEVICE_TTY_PRIVSET, tp);
804 if (error)
805 break;
806 zst->zst_swflags = *(int *)data;
807 break;
808
809 case TIOCSDTR:
810 zs_modem(zst, 1);
811 break;
812
813 case TIOCCDTR:
814 zs_modem(zst, 0);
815 break;
816
817 case TIOCMSET:
818 case TIOCMBIS:
819 case TIOCMBIC:
820 tiocm_to_zs(zst, cmd, *(int *)data);
821 break;
822
823 case TIOCMGET:
824 *(int *)data = zs_to_tiocm(zst);
825 break;
826
827 case PPS_IOC_CREATE:
828 case PPS_IOC_DESTROY:
829 case PPS_IOC_GETPARAMS:
830 case PPS_IOC_SETPARAMS:
831 case PPS_IOC_GETCAP:
832 case PPS_IOC_FETCH:
833 #ifdef PPS_SYNC
834 case PPS_IOC_KCBIND:
835 #endif
836 mutex_spin_enter(&timecounter_lock);
837 error = pps_ioctl(cmd, data, &zst->zst_pps_state);
838 if (zst->zst_pps_state.ppsparam.mode & PPS_CAPTUREBOTH)
839 zst->zst_ppsmask = ZSRR0_DCD;
840 else
841 zst->zst_ppsmask = 0;
842 mutex_spin_exit(&timecounter_lock);
843 break;
844
845 case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */
846 if (cs->cs_rr0_pps == 0) {
847 error = EINVAL;
848 break;
849 }
850 mutex_spin_enter(&timecounter_lock);
851 #ifndef PPS_TRAILING_EDGE
852 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
853 &zst->zst_pps_state.ppsinfo.assert_timestamp);
854 #else
855 TIMESPEC_TO_TIMEVAL((struct timeval *)data,
856 &zst->zst_pps_state.ppsinfo.clear_timestamp);
857 #endif
858 mutex_spin_exit(&timecounter_lock);
859 /*
860 * Now update interrupts.
861 */
862 zs_maskintr(zst);
863 /*
864 * If nothing is being transmitted, set up new current values,
865 * else mark them as pending.
866 */
867 if (!cs->cs_heldchange) {
868 if (zst->zst_tx_busy) {
869 zst->zst_heldtbc = zst->zst_tbc;
870 zst->zst_tbc = 0;
871 cs->cs_heldchange = 1;
872 } else
873 zs_loadchannelregs(cs);
874 }
875
876 break;
877
878 default:
879 error = EPASSTHROUGH;
880 break;
881 }
882
883 mutex_spin_exit(&cs->cs_lock);
884
885 return (error);
886 }
887
888 /*
889 * Start or restart transmission.
890 */
891 static void
892 zsstart(struct tty *tp)
893 {
894 struct zstty_softc *zst;
895 struct zs_chanstate *cs;
896 u_char *tba;
897 int tbc;
898
899 zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
900 cs = zst->zst_cs;
901
902 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
903 return;
904 if (zst->zst_tx_stopped)
905 return;
906 if (!ttypull(tp))
907 return;
908
909 /* Grab the first contiguous region of buffer space. */
910 tba = tp->t_outq.c_cf;
911 tbc = ndqb(&tp->t_outq, 0);
912
913 mutex_spin_enter(&cs->cs_lock);
914
915 zst->zst_tba = tba;
916 zst->zst_tbc = tbc;
917 SET(tp->t_state, TS_BUSY);
918 zst->zst_tx_busy = 1;
919
920 #ifdef ZS_TXDMA
921 if (zst->zst_tbc > 1) {
922 zs_dma_setup(cs, zst->zst_tba, zst->zst_tbc);
923 mutex_spin_exit(&cs->cs_lock);
924 return;
925 }
926 #endif
927
928 /* Output the first character of the contiguous buffer. */
929 zs_write_data(cs, *zst->zst_tba);
930 zst->zst_tbc--;
931 zst->zst_tba++;
932
933 mutex_spin_exit(&cs->cs_lock);
934 }
935
936 /*
937 * Stop output, e.g., for ^S or output flush.
938 */
939 void
940 zsstop(struct tty *tp, int flag)
941 {
942 struct zstty_softc *zst;
943
944 zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
945
946 mutex_spin_enter(&zst->zst_cs->cs_lock);
947 if (ISSET(tp->t_state, TS_BUSY)) {
948 /* Stop transmitting at the next chunk. */
949 zst->zst_tbc = 0;
950 zst->zst_heldtbc = 0;
951 if (!ISSET(tp->t_state, TS_TTSTOP))
952 SET(tp->t_state, TS_FLUSH);
953 }
954 mutex_spin_exit(&zst->zst_cs->cs_lock);
955 }
956
957 /*
958 * Set ZS tty parameters from termios.
959 * XXX - Should just copy the whole termios after
960 * making sure all the changes could be done.
961 */
962 static int
963 zsparam(struct tty *tp, struct termios *t)
964 {
965 struct zstty_softc *zst;
966 struct zs_chanstate *cs;
967 int ospeed;
968 tcflag_t cflag;
969 uint8_t tmp3, tmp4, tmp5;
970 int error;
971
972 zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
973 cs = zst->zst_cs;
974 ospeed = t->c_ospeed;
975 cflag = t->c_cflag;
976
977 /* Check requested parameters. */
978 if (ospeed < 0)
979 return (EINVAL);
980 if (t->c_ispeed && t->c_ispeed != ospeed)
981 return (EINVAL);
982
983 /*
984 * For the console, always force CLOCAL and !HUPCL, so that the port
985 * is always active.
986 */
987 if (ISSET(zst->zst_swflags, TIOCFLAG_SOFTCAR) ||
988 ISSET(zst->zst_hwflags, ZS_HWFLAG_CONSOLE)) {
989 SET(cflag, CLOCAL);
990 CLR(cflag, HUPCL);
991 }
992
993 /*
994 * Only whack the UART when params change.
995 * Some callers need to clear tp->t_ospeed
996 * to make sure initialization gets done.
997 */
998 if (tp->t_ospeed == ospeed &&
999 tp->t_cflag == cflag)
1000 return (0);
1001
1002 /*
1003 * Call MD functions to deal with changed
1004 * clock modes or H/W flow control modes.
1005 * The BRG divisor is set now. (reg 12,13)
1006 */
1007 error = zs_set_speed(cs, ospeed);
1008 if (error)
1009 return (error);
1010 error = zs_set_modes(cs, cflag);
1011 if (error)
1012 return (error);
1013
1014 /*
1015 * Block interrupts so that state will not
1016 * be altered until we are done setting it up.
1017 *
1018 * Initial values in cs_preg are set before
1019 * our attach routine is called. The master
1020 * interrupt enable is handled by zsc.c
1021 *
1022 */
1023 mutex_spin_enter(&cs->cs_lock);
1024
1025 /*
1026 * Recalculate which status ints to enable.
1027 */
1028 zs_maskintr(zst);
1029
1030 /* Recompute character size bits. */
1031 tmp3 = cs->cs_preg[3];
1032 tmp5 = cs->cs_preg[5];
1033 CLR(tmp3, ZSWR3_RXSIZE);
1034 CLR(tmp5, ZSWR5_TXSIZE);
1035 switch (ISSET(cflag, CSIZE)) {
1036 case CS5:
1037 SET(tmp3, ZSWR3_RX_5);
1038 SET(tmp5, ZSWR5_TX_5);
1039 break;
1040 case CS6:
1041 SET(tmp3, ZSWR3_RX_6);
1042 SET(tmp5, ZSWR5_TX_6);
1043 break;
1044 case CS7:
1045 SET(tmp3, ZSWR3_RX_7);
1046 SET(tmp5, ZSWR5_TX_7);
1047 break;
1048 case CS8:
1049 SET(tmp3, ZSWR3_RX_8);
1050 SET(tmp5, ZSWR5_TX_8);
1051 break;
1052 }
1053 cs->cs_preg[3] = tmp3;
1054 cs->cs_preg[5] = tmp5;
1055
1056 /*
1057 * Recompute the stop bits and parity bits. Note that
1058 * zs_set_speed() may have set clock selection bits etc.
1059 * in wr4, so those must preserved.
1060 */
1061 tmp4 = cs->cs_preg[4];
1062 CLR(tmp4, ZSWR4_SBMASK | ZSWR4_PARMASK);
1063 if (ISSET(cflag, CSTOPB))
1064 SET(tmp4, ZSWR4_TWOSB);
1065 else
1066 SET(tmp4, ZSWR4_ONESB);
1067 if (!ISSET(cflag, PARODD))
1068 SET(tmp4, ZSWR4_EVENP);
1069 if (ISSET(cflag, PARENB))
1070 SET(tmp4, ZSWR4_PARENB);
1071 cs->cs_preg[4] = tmp4;
1072
1073 /* And copy to tty. */
1074 tp->t_ispeed = 0;
1075 tp->t_ospeed = ospeed;
1076 tp->t_cflag = cflag;
1077
1078 /*
1079 * If nothing is being transmitted, set up new current values,
1080 * else mark them as pending.
1081 */
1082 if (!cs->cs_heldchange) {
1083 if (zst->zst_tx_busy) {
1084 zst->zst_heldtbc = zst->zst_tbc;
1085 zst->zst_tbc = 0;
1086 cs->cs_heldchange = 1;
1087 } else
1088 zs_loadchannelregs(cs);
1089 }
1090
1091 /*
1092 * If hardware flow control is disabled, turn off the buffer water
1093 * marks and unblock any soft flow control state. Otherwise, enable
1094 * the water marks.
1095 */
1096 if (!ISSET(cflag, CHWFLOW)) {
1097 zst->zst_r_hiwat = 0;
1098 zst->zst_r_lowat = 0;
1099 if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1100 CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1101 zst->zst_rx_ready = 1;
1102 cs->cs_softreq = 1;
1103 }
1104 if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1105 CLR(zst->zst_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1106 zs_hwiflow(zst);
1107 }
1108 } else {
1109 zst->zst_r_hiwat = zstty_rbuf_hiwat;
1110 zst->zst_r_lowat = zstty_rbuf_lowat;
1111 }
1112
1113 /*
1114 * Force a recheck of the hardware carrier and flow control status,
1115 * since we may have changed which bits we're looking at.
1116 */
1117 zstty_stint(cs, 1);
1118
1119 mutex_spin_exit(&cs->cs_lock);
1120
1121 /*
1122 * If hardware flow control is disabled, unblock any hard flow control
1123 * state.
1124 */
1125 if (!ISSET(cflag, CHWFLOW)) {
1126 if (zst->zst_tx_stopped) {
1127 zst->zst_tx_stopped = 0;
1128 zsstart(tp);
1129 }
1130 }
1131
1132 zstty_softint1(cs);
1133
1134 return (0);
1135 }
1136
1137 /*
1138 * Compute interrupt enable bits and set in the pending bits. Called both
1139 * in zsparam() and when PPS (pulse per second timing) state changes.
1140 * Must be called at splzs().
1141 */
1142 static void
1143 zs_maskintr(struct zstty_softc *zst)
1144 {
1145 struct zs_chanstate *cs = zst->zst_cs;
1146 uint8_t tmp15;
1147
1148 cs->cs_rr0_mask = cs->cs_rr0_cts | cs->cs_rr0_dcd;
1149 if (zst->zst_ppsmask != 0)
1150 cs->cs_rr0_mask |= cs->cs_rr0_pps;
1151 tmp15 = cs->cs_preg[15];
1152 if (ISSET(cs->cs_rr0_mask, ZSRR0_DCD))
1153 SET(tmp15, ZSWR15_DCD_IE);
1154 else
1155 CLR(tmp15, ZSWR15_DCD_IE);
1156 if (ISSET(cs->cs_rr0_mask, ZSRR0_CTS))
1157 SET(tmp15, ZSWR15_CTS_IE);
1158 else
1159 CLR(tmp15, ZSWR15_CTS_IE);
1160 cs->cs_preg[15] = tmp15;
1161 }
1162
1163
1164 /*
1165 * Raise or lower modem control (DTR/RTS) signals. If a character is
1166 * in transmission, the change is deferred.
1167 * Called at splzs() and with the channel lock held.
1168 */
1169 static void
1170 zs_modem(struct zstty_softc *zst, int onoff)
1171 {
1172 struct zs_chanstate *cs = zst->zst_cs, *ccs;
1173
1174 if (cs->cs_wr5_dtr == 0)
1175 return;
1176
1177 ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1178
1179 if (onoff)
1180 SET(ccs->cs_preg[5], cs->cs_wr5_dtr);
1181 else
1182 CLR(ccs->cs_preg[5], cs->cs_wr5_dtr);
1183
1184 if (!cs->cs_heldchange) {
1185 if (zst->zst_tx_busy) {
1186 zst->zst_heldtbc = zst->zst_tbc;
1187 zst->zst_tbc = 0;
1188 cs->cs_heldchange = 1;
1189 } else
1190 zs_loadchannelregs(cs);
1191 }
1192 }
1193
1194 /*
1195 * Set modem bits.
1196 * Called at splzs() and with the channel lock held.
1197 */
1198 static void
1199 tiocm_to_zs(struct zstty_softc *zst, u_long how, int ttybits)
1200 {
1201 struct zs_chanstate *cs = zst->zst_cs, *ccs;
1202 uint8_t zsbits;
1203
1204 ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1205
1206 zsbits = 0;
1207 if (ISSET(ttybits, TIOCM_DTR))
1208 SET(zsbits, ZSWR5_DTR);
1209 if (ISSET(ttybits, TIOCM_RTS))
1210 SET(zsbits, ZSWR5_RTS);
1211
1212 switch (how) {
1213 case TIOCMBIC:
1214 CLR(ccs->cs_preg[5], zsbits);
1215 break;
1216
1217 case TIOCMBIS:
1218 SET(ccs->cs_preg[5], zsbits);
1219 break;
1220
1221 case TIOCMSET:
1222 CLR(ccs->cs_preg[5], ZSWR5_RTS | ZSWR5_DTR);
1223 SET(ccs->cs_preg[5], zsbits);
1224 break;
1225 }
1226
1227 if (!cs->cs_heldchange) {
1228 if (zst->zst_tx_busy) {
1229 zst->zst_heldtbc = zst->zst_tbc;
1230 zst->zst_tbc = 0;
1231 cs->cs_heldchange = 1;
1232 } else
1233 zs_loadchannelregs(cs);
1234 }
1235 }
1236
1237 /*
1238 * Get modem bits.
1239 * Called at splzs() and with the channel lock held.
1240 */
1241 static int
1242 zs_to_tiocm(struct zstty_softc *zst)
1243 {
1244 struct zs_chanstate *cs = zst->zst_cs, *ccs;
1245 uint8_t zsbits;
1246 int ttybits = 0;
1247
1248 ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1249
1250 zsbits = ccs->cs_preg[5];
1251 if (ISSET(zsbits, ZSWR5_DTR))
1252 SET(ttybits, TIOCM_DTR);
1253 if (ISSET(zsbits, ZSWR5_RTS))
1254 SET(ttybits, TIOCM_RTS);
1255
1256 zsbits = cs->cs_rr0;
1257 if (ISSET(zsbits, ZSRR0_DCD))
1258 SET(ttybits, TIOCM_CD);
1259 if (ISSET(zsbits, ZSRR0_CTS))
1260 SET(ttybits, TIOCM_CTS);
1261
1262 return (ttybits);
1263 }
1264
1265 /*
1266 * Try to block or unblock input using hardware flow-control.
1267 * This is called by kern/tty.c if MDMBUF|CRTSCTS is set, and
1268 * if this function returns non-zero, the TS_TBLOCK flag will
1269 * be set or cleared according to the "block" arg passed.
1270 */
1271 int
1272 zshwiflow(struct tty *tp, int block)
1273 {
1274 struct zstty_softc *zst;
1275 struct zs_chanstate *cs;
1276
1277 zst = device_lookup_private(&zstty_cd, ZSUNIT(tp->t_dev));
1278 cs = zst->zst_cs;
1279
1280 if (cs->cs_wr5_rts == 0)
1281 return (0);
1282
1283 mutex_spin_enter(&cs->cs_lock);
1284 if (block) {
1285 if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1286 SET(zst->zst_rx_flags, RX_TTY_BLOCKED);
1287 zs_hwiflow(zst);
1288 }
1289 } else {
1290 if (ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1291 CLR(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1292 zst->zst_rx_ready = 1;
1293 cs->cs_softreq = 1;
1294 }
1295 if (ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1296 CLR(zst->zst_rx_flags, RX_TTY_BLOCKED);
1297 zs_hwiflow(zst);
1298 }
1299 }
1300 mutex_spin_exit(&cs->cs_lock);
1301 return (1);
1302 }
1303
1304 /*
1305 * Internal version of zshwiflow
1306 * Called at splzs() and with the channel lock held.
1307 */
1308 static void
1309 zs_hwiflow(struct zstty_softc *zst)
1310 {
1311 struct zs_chanstate *cs = zst->zst_cs, *ccs;
1312
1313 if (cs->cs_wr5_rts == 0)
1314 return;
1315
1316 ccs = (cs->cs_ctl_chan != NULL ? cs->cs_ctl_chan : cs);
1317
1318 if (ISSET(zst->zst_rx_flags, RX_ANY_BLOCK)) {
1319 CLR(ccs->cs_preg[5], cs->cs_wr5_rts);
1320 CLR(ccs->cs_creg[5], cs->cs_wr5_rts);
1321 } else {
1322 SET(ccs->cs_preg[5], cs->cs_wr5_rts);
1323 SET(ccs->cs_creg[5], cs->cs_wr5_rts);
1324 }
1325 zs_write_reg(ccs, 5, ccs->cs_creg[5]);
1326 }
1327
1328
1329 /****************************************************************
1330 * Interface to the lower layer (zscc)
1331 ****************************************************************/
1332
1333 #define integrate static inline
1334 integrate void zstty_rxsoft(struct zstty_softc *, struct tty *);
1335 integrate void zstty_txsoft(struct zstty_softc *, struct tty *);
1336 integrate void zstty_stsoft(struct zstty_softc *, struct tty *);
1337 static void zstty_diag(void *);
1338
1339 /*
1340 * Receiver Ready interrupt.
1341 * Called at splzs() and with the channel lock held.
1342 */
1343 static void
1344 zstty_rxint(struct zs_chanstate *cs)
1345 {
1346 struct zstty_softc *zst = cs->cs_private;
1347 uint8_t *put, *end;
1348 u_int cc;
1349 uint8_t rr0, rr1, c;
1350
1351 end = zst->zst_ebuf;
1352 put = zst->zst_rbput;
1353 cc = zst->zst_rbavail;
1354
1355 while (cc > 0) {
1356 /*
1357 * First read the status, because reading the received char
1358 * destroys the status of this char.
1359 */
1360 rr1 = zs_read_reg(cs, 1);
1361 c = zs_read_data(cs);
1362
1363 if (ISSET(rr1, ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
1364 /* Clear the receive error. */
1365 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
1366 }
1367
1368 cn_check_magic(zst->zst_tty->t_dev, c, zstty_cnm_state);
1369 put[0] = c;
1370 put[1] = rr1;
1371 put += 2;
1372 if (put >= end)
1373 put = zst->zst_rbuf;
1374 cc--;
1375
1376 rr0 = zs_read_csr(cs);
1377 if (!ISSET(rr0, ZSRR0_RX_READY))
1378 break;
1379 }
1380
1381 /*
1382 * Current string of incoming characters ended because
1383 * no more data was available or we ran out of space.
1384 * Schedule a receive event if any data was received.
1385 * If we're out of space, turn off receive interrupts.
1386 */
1387 zst->zst_rbput = put;
1388 zst->zst_rbavail = cc;
1389 if (!ISSET(zst->zst_rx_flags, RX_TTY_OVERFLOWED)) {
1390 zst->zst_rx_ready = 1;
1391 cs->cs_softreq = 1;
1392 }
1393
1394 /*
1395 * See if we are in danger of overflowing a buffer. If
1396 * so, use hardware flow control to ease the pressure.
1397 */
1398 if (!ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED) &&
1399 cc < zst->zst_r_hiwat) {
1400 SET(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1401 zs_hwiflow(zst);
1402 }
1403
1404 /*
1405 * If we're out of space, disable receive interrupts
1406 * until the queue has drained a bit.
1407 */
1408 if (!cc) {
1409 SET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1410 CLR(cs->cs_preg[1], ZSWR1_RIE);
1411 cs->cs_creg[1] = cs->cs_preg[1];
1412 zs_write_reg(cs, 1, cs->cs_creg[1]);
1413 }
1414
1415 #if 0
1416 printf("%xH%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
1417 #endif
1418 }
1419
1420 /*
1421 * Transmitter Ready interrupt.
1422 * Called at splzs() and with the channel lock held.
1423 */
1424 static void
1425 zstty_txint(struct zs_chanstate *cs)
1426 {
1427 struct zstty_softc *zst = cs->cs_private;
1428
1429 zs_write_csr(cs, ZSWR0_RESET_TXINT);
1430
1431 /*
1432 * If we've delayed a parameter change, do it now, and restart
1433 * output.
1434 */
1435 if (cs->cs_heldchange) {
1436 zs_loadchannelregs(cs);
1437 cs->cs_heldchange = 0;
1438 zst->zst_tbc = zst->zst_heldtbc;
1439 zst->zst_heldtbc = 0;
1440 }
1441
1442 /* Output the next character in the buffer, if any. */
1443 if (zst->zst_tbc > 0) {
1444 zs_write_data(cs, *zst->zst_tba);
1445 zst->zst_tbc--;
1446 zst->zst_tba++;
1447 } else {
1448 if (zst->zst_tx_busy) {
1449 zst->zst_tx_busy = 0;
1450 zst->zst_tx_done = 1;
1451 cs->cs_softreq = 1;
1452 }
1453 }
1454 }
1455
1456 /*
1457 * Status Change interrupt.
1458 * Called at splzs() and with the channel lock held.
1459 */
1460 static void
1461 zstty_stint(struct zs_chanstate *cs, int force)
1462 {
1463 struct zstty_softc *zst = cs->cs_private;
1464 uint8_t rr0, delta;
1465
1466 rr0 = zs_read_csr(cs);
1467 zs_write_csr(cs, ZSWR0_RESET_STATUS);
1468
1469 /*
1470 * Check here for console break, so that we can abort
1471 * even when interrupts are locking up the machine.
1472 */
1473 if (ISSET(rr0, ZSRR0_BREAK))
1474 cn_check_magic(zst->zst_tty->t_dev, CNC_BREAK, zstty_cnm_state);
1475
1476 if (!force)
1477 delta = rr0 ^ cs->cs_rr0;
1478 else
1479 delta = cs->cs_rr0_mask;
1480 cs->cs_rr0 = rr0;
1481
1482 if (ISSET(delta, cs->cs_rr0_mask)) {
1483 SET(cs->cs_rr0_delta, delta);
1484
1485 /*
1486 * Pulse-per-second clock signal on edge of DCD?
1487 */
1488 if (ISSET(delta, zst->zst_ppsmask)) {
1489 if (zst->zst_pps_state.ppsparam.mode &
1490 PPS_CAPTUREBOTH) {
1491 mutex_spin_enter(&timecounter_lock);
1492 pps_capture(&zst->zst_pps_state);
1493 pps_event(&zst->zst_pps_state,
1494 (ISSET(cs->cs_rr0, zst->zst_ppsmask))
1495 ? PPS_CAPTUREASSERT
1496 : PPS_CAPTURECLEAR);
1497 mutex_spin_exit(&timecounter_lock);
1498 }
1499 }
1500
1501 /*
1502 * Stop output immediately if we lose the output
1503 * flow control signal or carrier detect.
1504 */
1505 if (ISSET(~rr0, cs->cs_rr0_mask)) {
1506 zst->zst_tbc = 0;
1507 zst->zst_heldtbc = 0;
1508 }
1509
1510 zst->zst_st_check = 1;
1511 cs->cs_softreq = 1;
1512 }
1513 }
1514
1515 void
1516 zstty_diag(void *arg)
1517 {
1518 struct zstty_softc *zst = arg;
1519 int overflows, floods;
1520
1521 mutex_spin_enter(&zst->zst_cs->cs_lock);
1522 overflows = zst->zst_overflows;
1523 zst->zst_overflows = 0;
1524 floods = zst->zst_floods;
1525 zst->zst_floods = 0;
1526 zst->zst_errors = 0;
1527 mutex_spin_exit(&zst->zst_cs->cs_lock);
1528
1529 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1530 device_xname(zst->zst_dev),
1531 overflows, overflows == 1 ? "" : "s",
1532 floods, floods == 1 ? "" : "s");
1533 }
1534
1535 integrate void
1536 zstty_rxsoft(struct zstty_softc *zst, struct tty *tp)
1537 {
1538 struct zs_chanstate *cs = zst->zst_cs;
1539 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1540 uint8_t *get, *end;
1541 u_int cc, scc;
1542 uint8_t rr1;
1543 int code;
1544
1545 end = zst->zst_ebuf;
1546 get = zst->zst_rbget;
1547 scc = cc = zstty_rbuf_size - zst->zst_rbavail;
1548
1549 if (cc == zstty_rbuf_size) {
1550 zst->zst_floods++;
1551 if (zst->zst_errors++ == 0)
1552 callout_reset(&zst->zst_diag_ch, 60 * hz,
1553 zstty_diag, zst);
1554 }
1555
1556 /* If not yet open, drop the entire buffer content here */
1557 if (!ISSET(tp->t_state, TS_ISOPEN)) {
1558 get += cc << 1;
1559 if (get >= end)
1560 get -= zstty_rbuf_size << 1;
1561 cc = 0;
1562 }
1563 while (cc) {
1564 code = get[0];
1565 rr1 = get[1];
1566 if (ISSET(rr1, ZSRR1_DO | ZSRR1_FE | ZSRR1_PE)) {
1567 if (ISSET(rr1, ZSRR1_DO)) {
1568 zst->zst_overflows++;
1569 if (zst->zst_errors++ == 0)
1570 callout_reset(&zst->zst_diag_ch,
1571 60 * hz, zstty_diag, zst);
1572 }
1573 if (ISSET(rr1, ZSRR1_FE))
1574 SET(code, TTY_FE);
1575 if (ISSET(rr1, ZSRR1_PE))
1576 SET(code, TTY_PE);
1577 }
1578 if ((*rint)(code, tp) == -1) {
1579 /*
1580 * The line discipline's buffer is out of space.
1581 */
1582 if (!ISSET(zst->zst_rx_flags, RX_TTY_BLOCKED)) {
1583 /*
1584 * We're either not using flow control, or the
1585 * line discipline didn't tell us to block for
1586 * some reason. Either way, we have no way to
1587 * know when there's more space available, so
1588 * just drop the rest of the data.
1589 */
1590 get += cc << 1;
1591 if (get >= end)
1592 get -= zstty_rbuf_size << 1;
1593 cc = 0;
1594 } else {
1595 /*
1596 * Don't schedule any more receive processing
1597 * until the line discipline tells us there's
1598 * space available (through comhwiflow()).
1599 * Leave the rest of the data in the input
1600 * buffer.
1601 */
1602 SET(zst->zst_rx_flags, RX_TTY_OVERFLOWED);
1603 }
1604 break;
1605 }
1606 get += 2;
1607 if (get >= end)
1608 get = zst->zst_rbuf;
1609 cc--;
1610 }
1611
1612 if (cc != scc) {
1613 zst->zst_rbget = get;
1614 mutex_spin_enter(&cs->cs_lock);
1615 cc = zst->zst_rbavail += scc - cc;
1616 /* Buffers should be ok again, release possible block. */
1617 if (cc >= zst->zst_r_lowat) {
1618 if (ISSET(zst->zst_rx_flags, RX_IBUF_OVERFLOWED)) {
1619 CLR(zst->zst_rx_flags, RX_IBUF_OVERFLOWED);
1620 SET(cs->cs_preg[1], ZSWR1_RIE);
1621 cs->cs_creg[1] = cs->cs_preg[1];
1622 zs_write_reg(cs, 1, cs->cs_creg[1]);
1623 }
1624 if (ISSET(zst->zst_rx_flags, RX_IBUF_BLOCKED)) {
1625 CLR(zst->zst_rx_flags, RX_IBUF_BLOCKED);
1626 zs_hwiflow(zst);
1627 }
1628 }
1629 mutex_spin_exit(&cs->cs_lock);
1630 }
1631
1632 #if 0
1633 printf("%xS%04d\n", zst->zst_rx_flags, zst->zst_rbavail);
1634 #endif
1635 }
1636
1637 integrate void
1638 zstty_txsoft(struct zstty_softc *zst, struct tty *tp)
1639 {
1640 struct zs_chanstate *cs = zst->zst_cs;
1641
1642 mutex_spin_enter(&cs->cs_lock);
1643 CLR(tp->t_state, TS_BUSY);
1644 if (ISSET(tp->t_state, TS_FLUSH))
1645 CLR(tp->t_state, TS_FLUSH);
1646 else
1647 ndflush(&tp->t_outq, (int)(zst->zst_tba - tp->t_outq.c_cf));
1648 mutex_spin_exit(&cs->cs_lock);
1649 (*tp->t_linesw->l_start)(tp);
1650 }
1651
1652 integrate void
1653 zstty_stsoft(struct zstty_softc *zst, struct tty *tp)
1654 {
1655 struct zs_chanstate *cs = zst->zst_cs;
1656 uint8_t rr0, delta;
1657
1658 mutex_spin_enter(&cs->cs_lock);
1659 rr0 = cs->cs_rr0;
1660 delta = cs->cs_rr0_delta;
1661 cs->cs_rr0_delta = 0;
1662 mutex_spin_exit(&cs->cs_lock);
1663
1664 if (ISSET(delta, cs->cs_rr0_dcd)) {
1665 /*
1666 * Inform the tty layer that carrier detect changed.
1667 */
1668 mutex_spin_exit(&tty_lock);
1669 (void) (*tp->t_linesw->l_modem)(tp, ISSET(rr0, ZSRR0_DCD));
1670 mutex_spin_enter(&tty_lock);
1671 }
1672
1673 if (ISSET(delta, cs->cs_rr0_cts)) {
1674 /* Block or unblock output according to flow control. */
1675 if (ISSET(rr0, cs->cs_rr0_cts)) {
1676 zst->zst_tx_stopped = 0;
1677 (*tp->t_linesw->l_start)(tp);
1678 } else {
1679 zst->zst_tx_stopped = 1;
1680 }
1681 }
1682 }
1683
1684 /*
1685 * Software interrupt. Called at zssoft
1686 *
1687 * The main job to be done here is to empty the input ring
1688 * by passing its contents up to the tty layer. The ring is
1689 * always emptied during this operation, therefore the ring
1690 * must not be larger than the space after "high water" in
1691 * the tty layer, or the tty layer might drop our input.
1692 *
1693 * Note: an "input blockage" condition is assumed to exist if
1694 * EITHER the TS_TBLOCK flag or zst_rx_blocked flag is set.
1695 */
1696 static void
1697 zstty_softint(struct zs_chanstate *cs)
1698 {
1699
1700 zstty_softint1(cs);
1701 }
1702
1703 static void
1704 zstty_softint1(struct zs_chanstate *cs)
1705 {
1706 struct zstty_softc *zst = cs->cs_private;
1707 struct tty *tp = zst->zst_tty;
1708
1709
1710 if (zst->zst_rx_ready) {
1711 zst->zst_rx_ready = 0;
1712 zstty_rxsoft(zst, tp);
1713 }
1714
1715 if (zst->zst_st_check) {
1716 zst->zst_st_check = 0;
1717 zstty_stsoft(zst, tp);
1718 }
1719
1720 if (zst->zst_tx_done) {
1721 zst->zst_tx_done = 0;
1722 zstty_txsoft(zst, tp);
1723 }
1724 }
1725
1726 struct zsops zsops_tty = {
1727 zstty_rxint, /* receive char available */
1728 zstty_stint, /* external/status */
1729 zstty_txint, /* xmit buffer empty */
1730 zstty_softint, /* process software interrupt */
1731 };
1732
1733 #ifdef ZS_TXDMA
1734 void
1735 zstty_txdma_int(void *arg)
1736 {
1737 struct zs_chanstate *cs = arg;
1738 struct zstty_softc *zst = cs->cs_private;
1739
1740 zst->zst_tba += zst->zst_tbc;
1741 zst->zst_tbc = 0;
1742
1743 if (zst->zst_tx_busy) {
1744 zst->zst_tx_busy = 0;
1745 zst->zst_tx_done = 1;
1746 cs->cs_softreq = 1;
1747 }
1748 }
1749 #endif
1750