xref: /netbsd-src/sys/arch/sh3/dev/scif.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: scif.c,v 1.62 2013/11/09 21:31:45 christos Exp $ */
2 
3 /*-
4  * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*-
30  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
31  * All rights reserved.
32  *
33  * This code is derived from software contributed to The NetBSD Foundation
34  * by Charles M. Hannum.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55  * POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 /*
59  * Copyright (c) 1991 The Regents of the University of California.
60  * All rights reserved.
61  *
62  * Redistribution and use in source and binary forms, with or without
63  * modification, are permitted provided that the following conditions
64  * are met:
65  * 1. Redistributions of source code must retain the above copyright
66  *    notice, this list of conditions and the following disclaimer.
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in the
69  *    documentation and/or other materials provided with the distribution.
70  * 3. Neither the name of the University nor the names of its contributors
71  *    may be used to endorse or promote products derived from this software
72  *    without specific prior written permission.
73  *
74  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
75  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
76  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
78  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
79  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
80  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
81  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
82  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
83  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
84  * SUCH DAMAGE.
85  *
86  *	@(#)com.c	7.5 (Berkeley) 5/16/91
87  */
88 
89 /*
90  * SH internal serial driver
91  *
92  * This code is derived from both z8530tty.c and com.c
93  */
94 
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: scif.c,v 1.62 2013/11/09 21:31:45 christos Exp $");
97 
98 #include "opt_kgdb.h"
99 #include "opt_scif.h"
100 
101 #include <sys/param.h>
102 #include <sys/systm.h>
103 #include <sys/tty.h>
104 #include <sys/proc.h>
105 #include <sys/conf.h>
106 #include <sys/file.h>
107 #include <sys/syslog.h>
108 #include <sys/kernel.h>
109 #include <sys/device.h>
110 #include <sys/malloc.h>
111 #include <sys/kgdb.h>
112 #include <sys/kauth.h>
113 #include <sys/intr.h>
114 
115 #include <dev/cons.h>
116 
117 #include <sh3/clock.h>
118 #include <sh3/exception.h>
119 #include <sh3/scifreg.h>
120 
121 #include <sh3/dev/scifvar.h>
122 
123 #include "locators.h"
124 
125 
126 struct scif_softc {
127 	device_t sc_dev;
128 
129 	struct tty *sc_tty;
130 	void *sc_si;
131 
132 	callout_t sc_diag_ch;
133 
134 #if 0
135 	bus_space_tag_t sc_iot;		/* ISA i/o space identifier */
136 	bus_space_handle_t   sc_ioh;	/* ISA io handle */
137 
138 	int sc_drq;
139 
140 	int sc_frequency;
141 #endif
142 
143 	u_int sc_overflows,
144 	      sc_floods,
145 	      sc_errors;		/* number of retries so far */
146 	u_char sc_status[7];		/* copy of registers */
147 
148 	int sc_hwflags;
149 	int sc_swflags;
150 	u_int sc_fifolen;
151 
152 	u_int sc_r_hiwat,
153 	      sc_r_lowat;
154 	u_char *volatile sc_rbget,
155 	       *volatile sc_rbput;
156  	volatile u_int sc_rbavail;
157 	u_char *sc_rbuf,
158 	       *sc_ebuf;
159 
160  	u_char *sc_tba;			/* transmit buffer address */
161  	u_int sc_tbc,			/* transmit byte count */
162 	      sc_heldtbc;
163 
164 	volatile u_char sc_rx_flags,
165 #define	RX_TTY_BLOCKED		0x01
166 #define	RX_TTY_OVERFLOWED	0x02
167 #define	RX_IBUF_BLOCKED		0x04
168 #define	RX_IBUF_OVERFLOWED	0x08
169 #define	RX_ANY_BLOCK		0x0f
170 			sc_tx_busy,	/* working on an output chunk */
171 			sc_tx_done,	/* done with one output chunk */
172 			sc_tx_stopped,	/* H/W level stop (lost CTS) */
173 			sc_st_check,	/* got a status interrupt */
174 			sc_rx_ready;
175 
176 	volatile u_char sc_heldchange;
177 };
178 
179 
180 static int scif_match(device_t, cfdata_t, void *);
181 static void scif_attach(device_t, device_t, void *);
182 
183 CFATTACH_DECL_NEW(scif, sizeof(struct scif_softc),
184     scif_match, scif_attach, NULL, NULL);
185 
186 static int scif_attached = 0;	/* XXX: FIXME: don't limit to just one! */
187 
188 extern struct cfdriver scif_cd;
189 
190 #define	SCIFUNIT_MASK		0x7ffff
191 #define	SCIFDIALOUT_MASK	0x80000
192 
193 #define	SCIFUNIT(x)	(minor(x) & SCIFUNIT_MASK)
194 #define	SCIFDIALOUT(x)	(minor(x) & SCIFDIALOUT_MASK)
195 
196 
197 /* console */
198 dev_type_cnprobe(scifcnprobe);
199 dev_type_cninit(scifcninit);
200 dev_type_cngetc(scifcngetc);
201 dev_type_cnputc(scifcnputc);
202 
203 
204 /* cdevsw */
205 dev_type_open(scifopen);
206 dev_type_close(scifclose);
207 dev_type_read(scifread);
208 dev_type_write(scifwrite);
209 dev_type_ioctl(scifioctl);
210 dev_type_stop(scifstop);
211 dev_type_tty(sciftty);
212 dev_type_poll(scifpoll);
213 
214 const struct cdevsw scif_cdevsw = {
215 	scifopen, scifclose, scifread, scifwrite, scifioctl,
216 	scifstop, sciftty, scifpoll, nommap, ttykqfilter, D_TTY
217 };
218 
219 
220 /* struct tty */
221 static void scifstart(struct tty *);
222 static int scifparam(struct tty *, struct termios *);
223 
224 
225 void InitializeScif (unsigned int);
226 int ScifErrCheck(void);
227 void scif_putc(unsigned char);
228 unsigned char scif_getc(void);
229 
230 static int scifintr(void *);
231 static void scifsoft(void *);
232 static void scif_rxsoft(struct scif_softc *, struct tty *);
233 static void scif_txsoft(struct scif_softc *, struct tty *);
234 #if 0
235 static void scif_stsoft(struct scif_softc *, struct tty *);
236 #endif
237 static void scif_schedrx(struct scif_softc *);
238 static void scifdiag(void *);
239 
240 static void scif_break(struct scif_softc *, int);
241 static void scif_iflush(struct scif_softc *);
242 
243 
244 /* Hardware flag masks (sc_hwflags) */
245 #define	SCIF_HW_NOIEN	0x01
246 #define	SCIF_HW_FIFO	0x02
247 #define	SCIF_HW_FLOW	0x08
248 #define	SCIF_HW_DEV_OK	0x20
249 #define	SCIF_HW_CONSOLE	0x40
250 #define	SCIF_HW_KGDB	0x80
251 
252 
253 /* Buffer size for character buffer */
254 #define	SCIF_RING_SIZE	2048
255 static unsigned int scif_rbuf_size = SCIF_RING_SIZE;
256 
257 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
258 static unsigned int scif_rbuf_hiwat = (SCIF_RING_SIZE * 1) / 4;
259 static unsigned int scif_rbuf_lowat = (SCIF_RING_SIZE * 3) / 4;
260 
261 
262 #ifdef SCIFCN_SPEED
263 unsigned int scifcn_speed = SCIFCN_SPEED;
264 #else
265 unsigned int scifcn_speed = 9600;
266 #endif
267 
268 #define	CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
269 int scifconscflag = CONMODE;
270 
271 static int scifisconsole = 0;
272 
273 #ifdef KGDB
274 static int kgdb_attached = 0;
275 #endif
276 
277 
278 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
279 
280 
281 /* XXX: uwe
282  * Prepare for bus_spacification.  The difference in access widths is
283  * still handled by the magic definitions in scifreg.h
284  */
285 #define scif_smr_read()		SHREG_SCSMR2
286 #define scif_smr_write(v)	(SHREG_SCSMR2 = (v))
287 
288 #define scif_brr_read()		SHREG_SCBRR2
289 #define scif_brr_write(v)	(SHREG_SCBRR2 = (v))
290 
291 #define scif_scr_read()		SHREG_SCSCR2
292 #define scif_scr_write(v)	(SHREG_SCSCR2 = (v))
293 
294 #define scif_ftdr_write(v)	(SHREG_SCFTDR2 = (v))
295 
296 #define scif_ssr_read()		SHREG_SCSSR2
297 #define scif_ssr_write(v)	(SHREG_SCSSR2 = (v))
298 
299 #define scif_frdr_read()	SHREG_SCFRDR2
300 
301 #define scif_fcr_read()		SHREG_SCFCR2
302 #define scif_fcr_write(v)	(SHREG_SCFCR2 = (v))
303 
304 #define scif_fdr_read()		SHREG_SCFDR2
305 
306 #ifdef SH4 /* additional registers in sh4 */
307 
308 #define scif_sptr_read()	SHREG_SCSPTR2
309 #define scif_sptr_write(v)	(SHREG_SCSPTR2 = (v))
310 
311 #define scif_lsr_read()		SHREG_SCLSR2
312 #define scif_lsr_write(v)	(SHREG_SCLSR2 = (v))
313 
314 #endif /* SH4 */
315 
316 
317 void
318 InitializeScif(unsigned int bps)
319 {
320 
321 	/* Initialize SCR */
322 	scif_scr_write(0x00);
323 
324 #if 0
325 	scif_fcr_write(SCFCR2_TFRST | SCFCR2_RFRST | SCFCR2_MCE);
326 #else
327 	scif_fcr_write(SCFCR2_TFRST | SCFCR2_RFRST);
328 #endif
329 	/* Serial Mode Register */
330 	scif_smr_write(0x00);	/* 8bit,NonParity,Even,1Stop */
331 
332 	/* Bit Rate Register */
333 	scif_brr_write(divrnd(sh_clock_get_pclock(), 32 * bps) - 1);
334 
335 	/*
336 	 * wait 2m Sec, because Send/Recv must begin 1 bit period after
337 	 * BRR is set.
338 	 */
339 	delay(2000);
340 
341 #if 0
342 	scif_fcr_write(FIFO_RCV_TRIGGER_14 | FIFO_XMT_TRIGGER_1 | SCFCR2_MCE);
343 #else
344 	scif_fcr_write(FIFO_RCV_TRIGGER_14 | FIFO_XMT_TRIGGER_1);
345 #endif
346 
347 	/* Send permission, Receive permission ON */
348 	scif_scr_write(SCSCR2_TE | SCSCR2_RE);
349 
350 	/* Serial Status Register */
351 	scif_ssr_write(scif_ssr_read() & SCSSR2_TDFE); /* Clear Status */
352 }
353 
354 int
355 ScifErrCheck(void)
356 {
357 
358 	return (scif_ssr_read() & (SCSSR2_ER | SCSSR2_FER | SCSSR2_PER));
359 }
360 
361 void
362 scif_putc(unsigned char c)
363 {
364 
365 	/* wait for ready */
366 	while ((scif_fdr_read() & SCFDR2_TXCNT) == SCFDR2_TXF_FULL)
367 		continue;
368 
369 	/* write send data to send register */
370 	scif_ftdr_write(c);
371 
372 	/* clear ready flag */
373 	scif_ssr_write(scif_ssr_read() & ~(SCSSR2_TDFE | SCSSR2_TEND));
374 }
375 
376 unsigned char
377 scif_getc(void)
378 {
379 	unsigned char c, err_c;
380 #ifdef SH4
381 	unsigned short err_c2 = 0; /* XXXGCC: -Wuninitialized */
382 #endif
383 
384 	for (;;) {
385 		/* wait for ready */
386 		while ((scif_fdr_read() & SCFDR2_RECVCNT) == 0)
387 			continue;
388 
389 		c = scif_frdr_read();
390 		err_c = scif_ssr_read();
391 		scif_ssr_write(scif_ssr_read()
392 			& ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_RDF | SCSSR2_DR));
393 #ifdef SH4
394 		if (CPU_IS_SH4) {
395 			err_c2 = scif_lsr_read();
396 			scif_lsr_write(scif_lsr_read() & ~SCLSR2_ORER);
397 		}
398 #endif
399 		if ((err_c & (SCSSR2_ER | SCSSR2_BRK | SCSSR2_FER
400 		    | SCSSR2_PER)) == 0)
401 		{
402 #ifdef SH4
403 			if (CPU_IS_SH4 && ((err_c2 & SCLSR2_ORER) == 0))
404 #endif
405 			return(c);
406 		}
407 	}
408 
409 }
410 
411 static int
412 scif_match(device_t parent, cfdata_t cfp, void *aux)
413 {
414 
415 	if (scif_attached)
416 		return 0;
417 
418 	if (strcmp(cfp->cf_name, "scif") != 0)
419 		return 0;
420 
421 	return 1;
422 }
423 
424 static void
425 scif_attach(device_t parent, device_t self, void *aux)
426 {
427 	struct scif_softc *sc;
428 	struct tty *tp;
429 
430 	sc = device_private(self);
431 	sc->sc_dev = self;
432 
433 	scif_attached = 1;
434 
435 	sc->sc_hwflags = 0;	/* XXX */
436 	sc->sc_swflags = 0;	/* XXX */
437 	sc->sc_fifolen = 16;
438 
439 	aprint_normal("\n");
440 	if (scifisconsole) {
441 		aprint_naive(" (console)\n");
442 		aprint_normal_dev(self, "console\n");
443 		SET(sc->sc_hwflags, SCIF_HW_CONSOLE);
444 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
445 	}
446 #ifdef KGDB
447 	else if (kgdb_attached) {
448 		aprint_naive(" (kgdb)\n");
449 		aprint_normal_dev(self, "kgdb\n");
450 		SET(sc->sc_hwflags, SCIF_HW_KGDB);
451 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
452 	}
453 #endif
454 	else {
455 		aprint_naive("\n");
456 		InitializeScif(9600); /* XXX */
457 	}
458 
459 	callout_init(&sc->sc_diag_ch, 0);
460 #ifdef SH4
461 	intc_intr_establish(SH4_INTEVT_SCIF_ERI, IST_LEVEL, IPL_SERIAL,
462 	    scifintr, sc);
463 	intc_intr_establish(SH4_INTEVT_SCIF_RXI, IST_LEVEL, IPL_SERIAL,
464 	    scifintr, sc);
465 	intc_intr_establish(SH4_INTEVT_SCIF_BRI, IST_LEVEL, IPL_SERIAL,
466 	    scifintr, sc);
467 	intc_intr_establish(SH4_INTEVT_SCIF_TXI, IST_LEVEL, IPL_SERIAL,
468 	    scifintr, sc);
469 #else
470 	intc_intr_establish(SH7709_INTEVT2_SCIF_ERI, IST_LEVEL, IPL_SERIAL,
471 	    scifintr, sc);
472 	intc_intr_establish(SH7709_INTEVT2_SCIF_RXI, IST_LEVEL, IPL_SERIAL,
473 	    scifintr, sc);
474 	intc_intr_establish(SH7709_INTEVT2_SCIF_BRI, IST_LEVEL, IPL_SERIAL,
475 	    scifintr, sc);
476 	intc_intr_establish(SH7709_INTEVT2_SCIF_TXI, IST_LEVEL, IPL_SERIAL,
477 	    scifintr, sc);
478 #endif
479 
480 	sc->sc_si = softint_establish(SOFTINT_SERIAL, scifsoft, sc);
481 	SET(sc->sc_hwflags, SCIF_HW_DEV_OK);
482 
483 	tp = tty_alloc();
484 	tp->t_oproc = scifstart;
485 	tp->t_param = scifparam;
486 	tp->t_hwiflow = NULL;
487 
488 	sc->sc_tty = tp;
489 	sc->sc_rbuf = malloc(scif_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
490 	if (sc->sc_rbuf == NULL) {
491 		aprint_error_dev(self, "unable to allocate ring buffer\n");
492 		return;
493 	}
494 	sc->sc_ebuf = sc->sc_rbuf + (scif_rbuf_size << 1);
495 
496 	tty_attach(tp);
497 
498 	/* XXX: TODO */
499 	if (!pmf_device_register(self, NULL, NULL))
500 		aprint_error_dev(self, "unable to establish power handler\n");
501 }
502 
503 /*
504  * Start or restart transmission.
505  */
506 static void
507 scifstart(struct tty *tp)
508 {
509 	struct scif_softc *sc;
510 	int s;
511 
512 	sc = device_lookup_private(&scif_cd, SCIFUNIT(tp->t_dev));
513 
514 	s = spltty();
515 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
516 		goto out;
517 	if (sc->sc_tx_stopped)
518 		goto out;
519 	if (!ttypull(tp))
520 		goto out;
521 
522 	/* Grab the first contiguous region of buffer space. */
523 	{
524 		u_char *tba;
525 		int tbc;
526 
527 		tba = tp->t_outq.c_cf;
528 		tbc = ndqb(&tp->t_outq, 0);
529 
530 		(void)splserial();
531 
532 		sc->sc_tba = tba;
533 		sc->sc_tbc = tbc;
534 	}
535 
536 	SET(tp->t_state, TS_BUSY);
537 	sc->sc_tx_busy = 1;
538 
539 	/* Enable transmit completion interrupts if necessary. */
540 	scif_scr_write(scif_scr_read() | SCSCR2_TIE | SCSCR2_RIE);
541 
542 	/* Output the first chunk of the contiguous buffer. */
543 	{
544 		int n;
545 		int maxchars;
546 		int i;
547 
548 		n = sc->sc_tbc;
549 		maxchars = sc->sc_fifolen
550 			- ((scif_fdr_read() & SCFDR2_TXCNT) >> 8);
551 		if (n > maxchars)
552 			n = maxchars;
553 
554 		for (i = 0; i < n; i++) {
555 			scif_putc(*(sc->sc_tba));
556 			sc->sc_tba++;
557 		}
558 		sc->sc_tbc -= n;
559 	}
560 out:
561 	splx(s);
562 	return;
563 }
564 
565 /*
566  * Set SCIF tty parameters from termios.
567  * XXX - Should just copy the whole termios after
568  * making sure all the changes could be done.
569  */
570 static int
571 scifparam(struct tty *tp, struct termios *t)
572 {
573 	struct scif_softc *sc;
574 	int ospeed = t->c_ospeed;
575 	int s;
576 
577 	sc = device_lookup_private(&scif_cd, SCIFUNIT(tp->t_dev));
578 	if (!device_is_active(sc->sc_dev))
579 		return (EIO);
580 
581 	/* Check requested parameters. */
582 	if (ospeed < 0)
583 		return (EINVAL);
584 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
585 		return (EINVAL);
586 
587 	/*
588 	 * For the console, always force CLOCAL and !HUPCL, so that the port
589 	 * is always active.
590 	 */
591 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
592 	    ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
593 		SET(t->c_cflag, CLOCAL);
594 		CLR(t->c_cflag, HUPCL);
595 	}
596 
597 	/*
598 	 * If there were no changes, don't do anything.  This avoids dropping
599 	 * input and improves performance when all we did was frob things like
600 	 * VMIN and VTIME.
601 	 */
602 	if (tp->t_ospeed == t->c_ospeed &&
603 	    tp->t_cflag == t->c_cflag)
604 		return (0);
605 
606 #if 0
607 /* XXX (msaitoh) */
608 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
609 #endif
610 
611 	s = splserial();
612 
613 	/*
614 	 * Set the flow control pins depending on the current flow control
615 	 * mode.
616 	 */
617 	if (ISSET(t->c_cflag, CRTSCTS)) {
618 		scif_fcr_write(scif_fcr_read() | SCFCR2_MCE);
619 	} else {
620 		scif_fcr_write(scif_fcr_read() & ~SCFCR2_MCE);
621 	}
622 
623 	scif_brr_write(divrnd(sh_clock_get_pclock(), 32 * ospeed) -1);
624 
625 	/*
626 	 * Set the FIFO threshold based on the receive speed.
627 	 *
628 	 *  * If it's a low speed, it's probably a mouse or some other
629 	 *    interactive device, so set the threshold low.
630 	 *  * If it's a high speed, trim the trigger level down to prevent
631 	 *    overflows.
632 	 *  * Otherwise set it a bit higher.
633 	 */
634 #if 0
635 /* XXX (msaitoh) */
636 	if (ISSET(sc->sc_hwflags, SCIF_HW_HAYESP))
637 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
638 	else if (ISSET(sc->sc_hwflags, SCIF_HW_FIFO))
639 		sc->sc_fifo = FIFO_ENABLE |
640 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
641 		     t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
642 	else
643 		sc->sc_fifo = 0;
644 #endif
645 
646 	/* And copy to tty. */
647 	tp->t_ispeed = 0;
648 	tp->t_ospeed = t->c_ospeed;
649 	tp->t_cflag = t->c_cflag;
650 
651 	if (!sc->sc_heldchange) {
652 		if (sc->sc_tx_busy) {
653 			sc->sc_heldtbc = sc->sc_tbc;
654 			sc->sc_tbc = 0;
655 			sc->sc_heldchange = 1;
656 		}
657 #if 0
658 /* XXX (msaitoh) */
659 		else
660 			scif_loadchannelregs(sc);
661 #endif
662 	}
663 
664 	if (!ISSET(t->c_cflag, CHWFLOW)) {
665 		/* Disable the high water mark. */
666 		sc->sc_r_hiwat = 0;
667 		sc->sc_r_lowat = 0;
668 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
669 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
670 			scif_schedrx(sc);
671 		}
672 	} else {
673 		sc->sc_r_hiwat = scif_rbuf_hiwat;
674 		sc->sc_r_lowat = scif_rbuf_lowat;
675 	}
676 
677 	splx(s);
678 
679 #ifdef SCIF_DEBUG
680 	if (scif_debug)
681 		scifstatus(sc, "scifparam ");
682 #endif
683 
684 	if (!ISSET(t->c_cflag, CHWFLOW)) {
685 		if (sc->sc_tx_stopped) {
686 			sc->sc_tx_stopped = 0;
687 			scifstart(tp);
688 		}
689 	}
690 
691 	return (0);
692 }
693 
694 static void
695 scif_iflush(struct scif_softc *sc)
696 {
697 	int i;
698 
699 	i = scif_fdr_read() & SCFDR2_RECVCNT;
700 
701 	while (i > 0) {
702 		(void)scif_frdr_read();
703 		scif_ssr_write(scif_ssr_read() & ~(SCSSR2_RDF | SCSSR2_DR));
704 		i--;
705 	}
706 }
707 
708 int
709 scifopen(dev_t dev, int flag, int mode, struct lwp *l)
710 {
711 	struct scif_softc *sc;
712 	struct tty *tp;
713 	int s, s2;
714 	int error;
715 
716 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
717 	if (sc == 0 || !ISSET(sc->sc_hwflags, SCIF_HW_DEV_OK) ||
718 	    sc->sc_rbuf == NULL)
719 		return (ENXIO);
720 
721 	if (!device_is_active(sc->sc_dev))
722 		return (ENXIO);
723 
724 #ifdef KGDB
725 	/*
726 	 * If this is the kgdb port, no other use is permitted.
727 	 */
728 	if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB))
729 		return (EBUSY);
730 #endif /* KGDB */
731 
732 	tp = sc->sc_tty;
733 
734 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
735 		return (EBUSY);
736 
737 	s = spltty();
738 
739 	/*
740 	 * Do the following iff this is a first open.
741 	 */
742 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
743 		struct termios t;
744 
745 		tp->t_dev = dev;
746 
747 		s2 = splserial();
748 
749 		/* Turn on interrupts. */
750 		scif_scr_write(scif_scr_read() | SCSCR2_TIE | SCSCR2_RIE);
751 
752 		splx(s2);
753 
754 		/*
755 		 * Initialize the termios status to the defaults.  Add in the
756 		 * sticky bits from TIOCSFLAGS.
757 		 */
758 		t.c_ispeed = 0;
759 		if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
760 			t.c_ospeed = scifcn_speed;	/* XXX (msaitoh) */
761 			t.c_cflag = scifconscflag;
762 		} else {
763 			t.c_ospeed = TTYDEF_SPEED;
764 			t.c_cflag = TTYDEF_CFLAG;
765 		}
766 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
767 			SET(t.c_cflag, CLOCAL);
768 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
769 			SET(t.c_cflag, CRTSCTS);
770 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
771 			SET(t.c_cflag, MDMBUF);
772 		/* Make sure scifparam() will do something. */
773 		tp->t_ospeed = 0;
774 		(void) scifparam(tp, &t);
775 		tp->t_iflag = TTYDEF_IFLAG;
776 		tp->t_oflag = TTYDEF_OFLAG;
777 		tp->t_lflag = TTYDEF_LFLAG;
778 		ttychars(tp);
779 		ttsetwater(tp);
780 
781 		s2 = splserial();
782 
783 		/* Clear the input ring, and unblock. */
784 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
785 		sc->sc_rbavail = scif_rbuf_size;
786 		scif_iflush(sc);
787 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
788 #if 0
789 /* XXX (msaitoh) */
790 		scif_hwiflow(sc);
791 #endif
792 
793 #ifdef SCIF_DEBUG
794 		if (scif_debug)
795 			scifstatus(sc, "scifopen  ");
796 #endif
797 
798 		splx(s2);
799 	}
800 
801 	splx(s);
802 
803 	error = ttyopen(tp, SCIFDIALOUT(dev), ISSET(flag, O_NONBLOCK));
804 	if (error)
805 		goto bad;
806 
807 	error = (*tp->t_linesw->l_open)(dev, tp);
808 	if (error)
809 		goto bad;
810 
811 	return (0);
812 
813 bad:
814 
815 	return (error);
816 }
817 
818 int
819 scifclose(dev_t dev, int flag, int mode, struct lwp *l)
820 {
821 	struct scif_softc *sc;
822 	struct tty *tp;
823 
824 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
825 	tp = sc->sc_tty;
826 
827 	/* XXX This is for cons.c. */
828 	if (!ISSET(tp->t_state, TS_ISOPEN))
829 		return (0);
830 
831 	(*tp->t_linesw->l_close)(tp, flag);
832 	ttyclose(tp);
833 
834 	if (!device_is_active(sc->sc_dev))
835 		return (0);
836 
837 	return (0);
838 }
839 
840 int
841 scifread(dev_t dev, struct uio *uio, int flag)
842 {
843 	struct scif_softc *sc;
844 	struct tty *tp;
845 
846 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
847 	tp = sc->sc_tty;
848 
849 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
850 }
851 
852 int
853 scifwrite(dev_t dev, struct uio *uio, int flag)
854 {
855 	struct scif_softc *sc;
856 	struct tty *tp;
857 
858 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
859 	tp = sc->sc_tty;
860 
861 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
862 }
863 
864 int
865 scifpoll(dev_t dev, int events, struct lwp *l)
866 {
867 	struct scif_softc *sc;
868 	struct tty *tp;
869 
870 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
871 	tp = sc->sc_tty;
872 
873 	return ((*tp->t_linesw->l_poll)(tp, events, l));
874 }
875 
876 struct tty *
877 sciftty(dev_t dev)
878 {
879 	struct scif_softc *sc;
880 	struct tty *tp;
881 
882 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
883 	tp = sc->sc_tty;
884 
885 	return (tp);
886 }
887 
888 int
889 scifioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
890 {
891 	struct scif_softc *sc;
892 	struct tty *tp;
893 	int error;
894 	int s;
895 
896 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
897 	if (!device_is_active(sc->sc_dev))
898 		return (EIO);
899 
900 	tp = sc->sc_tty;
901 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
902 	if (error != EPASSTHROUGH)
903 		return (error);
904 
905 	error = ttioctl(tp, cmd, data, flag, l);
906 	if (error != EPASSTHROUGH)
907 		return (error);
908 
909 	error = 0;
910 
911 	s = splserial();
912 
913 	switch (cmd) {
914 	case TIOCSBRK:
915 		scif_break(sc, 1);
916 		break;
917 
918 	case TIOCCBRK:
919 		scif_break(sc, 0);
920 		break;
921 
922 	case TIOCGFLAGS:
923 		*(int *)data = sc->sc_swflags;
924 		break;
925 
926 	case TIOCSFLAGS:
927 		error = kauth_authorize_device_tty(l->l_cred,
928 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
929 		if (error)
930 			break;
931 		sc->sc_swflags = *(int *)data;
932 		break;
933 
934 	default:
935 		error = EPASSTHROUGH;
936 		break;
937 	}
938 
939 	splx(s);
940 
941 	return (error);
942 }
943 
944 static void
945 scif_schedrx(struct scif_softc *sc)
946 {
947 
948 	sc->sc_rx_ready = 1;
949 
950 	/* Wake up the poller. */
951 	softint_schedule(sc->sc_si);
952 }
953 
954 static void
955 scif_break(struct scif_softc *sc, int onoff)
956 {
957 
958 	if (onoff)
959 		scif_ssr_write(scif_ssr_read() & ~SCSSR2_TDFE);
960 	else
961 		scif_ssr_write(scif_ssr_read() | SCSSR2_TDFE);
962 
963 #if 0	/* XXX */
964 	if (!sc->sc_heldchange) {
965 		if (sc->sc_tx_busy) {
966 			sc->sc_heldtbc = sc->sc_tbc;
967 			sc->sc_tbc = 0;
968 			sc->sc_heldchange = 1;
969 		} else
970 			scif_loadchannelregs(sc);
971 	}
972 #endif
973 }
974 
975 /*
976  * Stop output, e.g., for ^S or output flush.
977  */
978 void
979 scifstop(struct tty *tp, int flag)
980 {
981 	struct scif_softc *sc;
982 	int s;
983 
984 	sc = device_lookup_private(&scif_cd, SCIFUNIT(tp->t_dev));
985 
986 	s = splserial();
987 	if (ISSET(tp->t_state, TS_BUSY)) {
988 		/* Stop transmitting at the next chunk. */
989 		sc->sc_tbc = 0;
990 		sc->sc_heldtbc = 0;
991 		if (!ISSET(tp->t_state, TS_TTSTOP))
992 			SET(tp->t_state, TS_FLUSH);
993 	}
994 	splx(s);
995 }
996 
997 static void
998 scifdiag(void *arg)
999 {
1000 	struct scif_softc *sc = arg;
1001 	int overflows, floods;
1002 	int s;
1003 
1004 	s = splserial();
1005 	overflows = sc->sc_overflows;
1006 	sc->sc_overflows = 0;
1007 	floods = sc->sc_floods;
1008 	sc->sc_floods = 0;
1009 	sc->sc_errors = 0;
1010 	splx(s);
1011 
1012 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1013 	    device_xname(sc->sc_dev),
1014 	    overflows, overflows == 1 ? "" : "s",
1015 	    floods, floods == 1 ? "" : "s");
1016 }
1017 
1018 static void
1019 scif_rxsoft(struct scif_softc *sc, struct tty *tp)
1020 {
1021 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1022 	u_char *get, *end;
1023 	u_int cc, scc;
1024 	u_char ssr2;
1025 	int code;
1026 	int s;
1027 
1028 	end = sc->sc_ebuf;
1029 	get = sc->sc_rbget;
1030 	scc = cc = scif_rbuf_size - sc->sc_rbavail;
1031 
1032 	if (cc == scif_rbuf_size) {
1033 		sc->sc_floods++;
1034 		if (sc->sc_errors++ == 0)
1035 			callout_reset(&sc->sc_diag_ch, 60 * hz, scifdiag, sc);
1036 	}
1037 
1038 	while (cc) {
1039 		code = get[0];
1040 		ssr2 = get[1];
1041 		if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) {
1042 			if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER))
1043 				SET(code, TTY_FE);
1044 			if (ISSET(ssr2, SCSSR2_PER))
1045 				SET(code, TTY_PE);
1046 		}
1047 		if ((*rint)(code, tp) == -1) {
1048 			/*
1049 			 * The line discipline's buffer is out of space.
1050 			 */
1051 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1052 				/*
1053 				 * We're either not using flow control, or the
1054 				 * line discipline didn't tell us to block for
1055 				 * some reason.  Either way, we have no way to
1056 				 * know when there's more space available, so
1057 				 * just drop the rest of the data.
1058 				 */
1059 				get += cc << 1;
1060 				if (get >= end)
1061 					get -= scif_rbuf_size << 1;
1062 				cc = 0;
1063 			} else {
1064 				/*
1065 				 * Don't schedule any more receive processing
1066 				 * until the line discipline tells us there's
1067 				 * space available (through scifhwiflow()).
1068 				 * Leave the rest of the data in the input
1069 				 * buffer.
1070 				 */
1071 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1072 			}
1073 			break;
1074 		}
1075 		get += 2;
1076 		if (get >= end)
1077 			get = sc->sc_rbuf;
1078 		cc--;
1079 	}
1080 
1081 	if (cc != scc) {
1082 		sc->sc_rbget = get;
1083 		s = splserial();
1084 		cc = sc->sc_rbavail += scc - cc;
1085 		/* Buffers should be ok again, release possible block. */
1086 		if (cc >= sc->sc_r_lowat) {
1087 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1088 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1089 				scif_scr_write(scif_scr_read() | SCSCR2_RIE);
1090 			}
1091 #if 0
1092 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1093 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1094 				scif_hwiflow(sc);
1095 			}
1096 #endif
1097 		}
1098 		splx(s);
1099 	}
1100 }
1101 
1102 static void
1103 scif_txsoft(struct scif_softc *sc, struct tty *tp)
1104 {
1105 
1106 	CLR(tp->t_state, TS_BUSY);
1107 	if (ISSET(tp->t_state, TS_FLUSH))
1108 		CLR(tp->t_state, TS_FLUSH);
1109 	else
1110 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1111 	(*tp->t_linesw->l_start)(tp);
1112 }
1113 
1114 #if 0 /* XXX (msaitoh) */
1115 static void
1116 scif_stsoft(struct scif_softc *sc, struct tty *tp)
1117 {
1118 	u_char msr, delta;
1119 	int s;
1120 
1121 	s = splserial();
1122 	msr = sc->sc_msr;
1123 	delta = sc->sc_msr_delta;
1124 	sc->sc_msr_delta = 0;
1125 	splx(s);
1126 
1127 	if (ISSET(delta, sc->sc_msr_dcd)) {
1128 		/*
1129 		 * Inform the tty layer that carrier detect changed.
1130 		 */
1131 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1132 	}
1133 
1134 	if (ISSET(delta, sc->sc_msr_cts)) {
1135 		/* Block or unblock output according to flow control. */
1136 		if (ISSET(msr, sc->sc_msr_cts)) {
1137 			sc->sc_tx_stopped = 0;
1138 			(*tp->t_linesw->l_start)(tp);
1139 		} else {
1140 			sc->sc_tx_stopped = 1;
1141 		}
1142 	}
1143 
1144 #ifdef SCIF_DEBUG
1145 	if (scif_debug)
1146 		scifstatus(sc, "scif_stsoft");
1147 #endif
1148 }
1149 #endif /* 0 */
1150 
1151 static void
1152 scifsoft(void *arg)
1153 {
1154 	struct scif_softc *sc = arg;
1155 	struct tty *tp;
1156 
1157 	if (!device_is_active(sc->sc_dev))
1158 		return;
1159 
1160 	tp = sc->sc_tty;
1161 
1162 	if (sc->sc_rx_ready) {
1163 		sc->sc_rx_ready = 0;
1164 		scif_rxsoft(sc, tp);
1165 	}
1166 
1167 #if 0
1168 	if (sc->sc_st_check) {
1169 		sc->sc_st_check = 0;
1170 		scif_stsoft(sc, tp);
1171 	}
1172 #endif
1173 
1174 	if (sc->sc_tx_done) {
1175 		sc->sc_tx_done = 0;
1176 		scif_txsoft(sc, tp);
1177 	}
1178 }
1179 
1180 static int
1181 scifintr(void *arg)
1182 {
1183 	struct scif_softc *sc = arg;
1184 	u_char *put, *end;
1185 	u_int cc;
1186 	u_short ssr2;
1187 	int count;
1188 
1189 	if (!device_is_active(sc->sc_dev))
1190 		return (0);
1191 
1192 	end = sc->sc_ebuf;
1193 	put = sc->sc_rbput;
1194 	cc = sc->sc_rbavail;
1195 
1196 	do {
1197 		ssr2 = scif_ssr_read();
1198 		if (ISSET(ssr2, SCSSR2_BRK)) {
1199 			scif_ssr_write(scif_ssr_read()
1200 				& ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_DR));
1201 #ifdef DDB
1202 			if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
1203 				console_debugger();
1204 			}
1205 #endif /* DDB */
1206 #ifdef KGDB
1207 			if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB)) {
1208 				kgdb_connect(1);
1209 			}
1210 #endif /* KGDB */
1211 		}
1212 		count = scif_fdr_read() & SCFDR2_RECVCNT;
1213 		if (count != 0) {
1214 			for (;;) {
1215 				u_char c = scif_frdr_read();
1216 				u_char err = (u_char)(scif_ssr_read() & 0x00ff);
1217 
1218 				scif_ssr_write(scif_ssr_read()
1219 				    & ~(SCSSR2_ER | SCSSR2_RDF | SCSSR2_DR));
1220 #ifdef SH4
1221 				if (CPU_IS_SH4)
1222 					scif_lsr_write(scif_lsr_read()
1223 						       & ~SCLSR2_ORER);
1224 #endif
1225 				if ((cc > 0) && (count > 0)) {
1226 					put[0] = c;
1227 					put[1] = err;
1228 					put += 2;
1229 					if (put >= end)
1230 						put = sc->sc_rbuf;
1231 					cc--;
1232 					count--;
1233 				} else
1234 					break;
1235 			}
1236 
1237 			/*
1238 			 * Current string of incoming characters ended because
1239 			 * no more data was available or we ran out of space.
1240 			 * Schedule a receive event if any data was received.
1241 			 * If we're out of space, turn off receive interrupts.
1242 			 */
1243 			sc->sc_rbput = put;
1244 			sc->sc_rbavail = cc;
1245 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1246 				sc->sc_rx_ready = 1;
1247 
1248 			/*
1249 			 * See if we are in danger of overflowing a buffer. If
1250 			 * so, use hardware flow control to ease the pressure.
1251 			 */
1252 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1253 			    cc < sc->sc_r_hiwat) {
1254 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1255 #if 0
1256 				scif_hwiflow(sc);
1257 #endif
1258 			}
1259 
1260 			/*
1261 			 * If we're out of space, disable receive interrupts
1262 			 * until the queue has drained a bit.
1263 			 */
1264 			if (!cc) {
1265 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1266 				scif_scr_write(scif_scr_read() & ~SCSCR2_RIE);
1267 			}
1268 		} else {
1269 			if (scif_ssr_read() & (SCSSR2_RDF | SCSSR2_DR)) {
1270 				scif_scr_write(scif_scr_read()
1271 					       & ~(SCSCR2_TIE | SCSCR2_RIE));
1272 				delay(10);
1273 				scif_scr_write(scif_scr_read()
1274 					       | SCSCR2_TIE | SCSCR2_RIE);
1275 				continue;
1276 			}
1277 		}
1278 	} while (scif_ssr_read() & (SCSSR2_RDF | SCSSR2_DR));
1279 
1280 #if 0
1281 	msr = bus_space_read_1(iot, ioh, scif_msr);
1282 	delta = msr ^ sc->sc_msr;
1283 	sc->sc_msr = msr;
1284 	if (ISSET(delta, sc->sc_msr_mask)) {
1285 		SET(sc->sc_msr_delta, delta);
1286 
1287 		/*
1288 		 * Pulse-per-second clock signal on edge of DCD?
1289 		 */
1290 		if (ISSET(delta, sc->sc_ppsmask)) {
1291 			struct timeval tv;
1292 			if (ISSET(msr, sc->sc_ppsmask) ==
1293 			    sc->sc_ppsassert) {
1294 				/* XXX nanotime() */
1295 				microtime(&tv);
1296 				TIMEVAL_TO_TIMESPEC(&tv,
1297 						    &sc->ppsinfo.assert_timestamp);
1298 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1299 					timespecadd(&sc->ppsinfo.assert_timestamp,
1300 						    &sc->ppsparam.assert_offset,
1301 						    &sc->ppsinfo.assert_timestamp);
1302 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
1303 				}
1304 
1305 #ifdef PPS_SYNC
1306 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1307 					hardpps(&tv, tv.tv_usec);
1308 #endif
1309 				sc->ppsinfo.assert_sequence++;
1310 				sc->ppsinfo.current_mode =
1311 					sc->ppsparam.mode;
1312 
1313 			} else if (ISSET(msr, sc->sc_ppsmask) ==
1314 				   sc->sc_ppsclear) {
1315 				/* XXX nanotime() */
1316 				microtime(&tv);
1317 				TIMEVAL_TO_TIMESPEC(&tv,
1318 						    &sc->ppsinfo.clear_timestamp);
1319 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1320 					timespecadd(&sc->ppsinfo.clear_timestamp,
1321 						    &sc->ppsparam.clear_offset,
1322 						    &sc->ppsinfo.clear_timestamp);
1323 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
1324 				}
1325 
1326 #ifdef PPS_SYNC
1327 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1328 					hardpps(&tv, tv.tv_usec);
1329 #endif
1330 				sc->ppsinfo.clear_sequence++;
1331 				sc->ppsinfo.current_mode =
1332 					sc->ppsparam.mode;
1333 			}
1334 		}
1335 
1336 		/*
1337 		 * Stop output immediately if we lose the output
1338 		 * flow control signal or carrier detect.
1339 		 */
1340 		if (ISSET(~msr, sc->sc_msr_mask)) {
1341 			sc->sc_tbc = 0;
1342 			sc->sc_heldtbc = 0;
1343 #ifdef SCIF_DEBUG
1344 			if (scif_debug)
1345 				scifstatus(sc, "scifintr  ");
1346 #endif
1347 		}
1348 
1349 		sc->sc_st_check = 1;
1350 	}
1351 #endif
1352 
1353 	/*
1354 	 * Done handling any receive interrupts. See if data can be
1355 	 * transmitted as well. Schedule tx done event if no data left
1356 	 * and tty was marked busy.
1357 	 */
1358 	if (((scif_fdr_read() & SCFDR2_TXCNT) >> 8) != 16) { /* XXX (msaitoh) */
1359 		/*
1360 		 * If we've delayed a parameter change, do it now, and restart
1361 		 * output.
1362 		 */
1363 		if (sc->sc_heldchange) {
1364 			sc->sc_heldchange = 0;
1365 			sc->sc_tbc = sc->sc_heldtbc;
1366 			sc->sc_heldtbc = 0;
1367 		}
1368 
1369 		/* Output the next chunk of the contiguous buffer, if any. */
1370 		if (sc->sc_tbc > 0) {
1371 			int n;
1372 			int maxchars;
1373 			int i;
1374 
1375 			n = sc->sc_tbc;
1376 			maxchars = sc->sc_fifolen -
1377 				((scif_fdr_read() & SCFDR2_TXCNT) >> 8);
1378 			if (n > maxchars)
1379 				n = maxchars;
1380 
1381 			for (i = 0; i < n; i++) {
1382 				scif_putc(*(sc->sc_tba));
1383 				sc->sc_tba++;
1384 			}
1385 			sc->sc_tbc -= n;
1386 		} else {
1387 			/* Disable transmit completion interrupts if necessary. */
1388 #if 0
1389 			if (ISSET(sc->sc_ier, IER_ETXRDY))
1390 #endif
1391 				scif_scr_write(scif_scr_read() & ~SCSCR2_TIE);
1392 
1393 			if (sc->sc_tx_busy) {
1394 				sc->sc_tx_busy = 0;
1395 				sc->sc_tx_done = 1;
1396 			}
1397 		}
1398 	}
1399 
1400 	/* Wake up the poller. */
1401 	softint_schedule(sc->sc_si);
1402 
1403 #ifdef RND_SCIF
1404 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
1405 #endif
1406 
1407 	return (1);
1408 }
1409 
1410 void
1411 scifcnprobe(struct consdev *cp)
1412 {
1413 	int maj;
1414 
1415 	/* locate the major number */
1416 	maj = cdevsw_lookup_major(&scif_cdevsw);
1417 
1418 	/* Initialize required fields. */
1419 	cp->cn_dev = makedev(maj, 0);
1420 #ifdef SCIFCONSOLE
1421 	cp->cn_pri = CN_REMOTE;
1422 #else
1423 	cp->cn_pri = CN_NORMAL;
1424 #endif
1425 }
1426 
1427 void
1428 scifcninit(struct consdev *cp)
1429 {
1430 
1431 	InitializeScif(scifcn_speed);
1432 	scifisconsole = 1;
1433 }
1434 
1435 int
1436 scifcngetc(dev_t dev)
1437 {
1438 	int c;
1439 	int s;
1440 
1441 	s = splserial();
1442 	c = scif_getc();
1443 	splx(s);
1444 
1445 	return (c);
1446 }
1447 
1448 void
1449 scifcnputc(dev_t dev, int c)
1450 {
1451 	int s;
1452 
1453 	s = splserial();
1454 	scif_putc((u_char)c);
1455 	splx(s);
1456 }
1457 
1458 #ifdef KGDB
1459 int
1460 scif_kgdb_init(void)
1461 {
1462 
1463 	if (strcmp(kgdb_devname, "scif") != 0)
1464 		return (1);
1465 
1466 	if (scifisconsole)
1467 		return (1);	/* can't share with console */
1468 
1469 	InitializeScif(kgdb_rate);
1470 
1471 	kgdb_attach((int (*)(void *))scifcngetc,
1472 	    (void (*)(void *, int))scifcnputc, NULL);
1473 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1474 	kgdb_attached = 1;
1475 
1476 	return (0);
1477 }
1478 #endif /* KGDB */
1479