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