xref: /netbsd-src/sys/arch/sh3/dev/scif.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: scif.c,v 1.57 2008/04/28 20:23:35 martin 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.57 2008/04/28 20:23:35 martin 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 = ttymalloc();
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 
499 /*
500  * Start or restart transmission.
501  */
502 static void
503 scifstart(struct tty *tp)
504 {
505 	struct scif_softc *sc;
506 	int s;
507 
508 	sc = device_lookup_private(&scif_cd, SCIFUNIT(tp->t_dev));
509 
510 	s = spltty();
511 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
512 		goto out;
513 	if (sc->sc_tx_stopped)
514 		goto out;
515 	if (!ttypull(tp))
516 		goto out;
517 
518 	/* Grab the first contiguous region of buffer space. */
519 	{
520 		u_char *tba;
521 		int tbc;
522 
523 		tba = tp->t_outq.c_cf;
524 		tbc = ndqb(&tp->t_outq, 0);
525 
526 		(void)splserial();
527 
528 		sc->sc_tba = tba;
529 		sc->sc_tbc = tbc;
530 	}
531 
532 	SET(tp->t_state, TS_BUSY);
533 	sc->sc_tx_busy = 1;
534 
535 	/* Enable transmit completion interrupts if necessary. */
536 	scif_scr_write(scif_scr_read() | SCSCR2_TIE | SCSCR2_RIE);
537 
538 	/* Output the first chunk of the contiguous buffer. */
539 	{
540 		int n;
541 		int maxchars;
542 		int i;
543 
544 		n = sc->sc_tbc;
545 		maxchars = sc->sc_fifolen
546 			- ((scif_fdr_read() & SCFDR2_TXCNT) >> 8);
547 		if (n > maxchars)
548 			n = maxchars;
549 
550 		for (i = 0; i < n; i++) {
551 			scif_putc(*(sc->sc_tba));
552 			sc->sc_tba++;
553 		}
554 		sc->sc_tbc -= n;
555 	}
556 out:
557 	splx(s);
558 	return;
559 }
560 
561 /*
562  * Set SCIF tty parameters from termios.
563  * XXX - Should just copy the whole termios after
564  * making sure all the changes could be done.
565  */
566 static int
567 scifparam(struct tty *tp, struct termios *t)
568 {
569 	struct scif_softc *sc;
570 	int ospeed = t->c_ospeed;
571 	int s;
572 
573 	sc = device_lookup_private(&scif_cd, SCIFUNIT(tp->t_dev));
574 	if (!device_is_active(sc->sc_dev))
575 		return (EIO);
576 
577 	/* Check requested parameters. */
578 	if (ospeed < 0)
579 		return (EINVAL);
580 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
581 		return (EINVAL);
582 
583 	/*
584 	 * For the console, always force CLOCAL and !HUPCL, so that the port
585 	 * is always active.
586 	 */
587 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
588 	    ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
589 		SET(t->c_cflag, CLOCAL);
590 		CLR(t->c_cflag, HUPCL);
591 	}
592 
593 	/*
594 	 * If there were no changes, don't do anything.  This avoids dropping
595 	 * input and improves performance when all we did was frob things like
596 	 * VMIN and VTIME.
597 	 */
598 	if (tp->t_ospeed == t->c_ospeed &&
599 	    tp->t_cflag == t->c_cflag)
600 		return (0);
601 
602 #if 0
603 /* XXX (msaitoh) */
604 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
605 #endif
606 
607 	s = splserial();
608 
609 	/*
610 	 * Set the flow control pins depending on the current flow control
611 	 * mode.
612 	 */
613 	if (ISSET(t->c_cflag, CRTSCTS)) {
614 		scif_fcr_write(scif_fcr_read() | SCFCR2_MCE);
615 	} else {
616 		scif_fcr_write(scif_fcr_read() & ~SCFCR2_MCE);
617 	}
618 
619 	scif_brr_write(divrnd(sh_clock_get_pclock(), 32 * ospeed) -1);
620 
621 	/*
622 	 * Set the FIFO threshold based on the receive speed.
623 	 *
624 	 *  * If it's a low speed, it's probably a mouse or some other
625 	 *    interactive device, so set the threshold low.
626 	 *  * If it's a high speed, trim the trigger level down to prevent
627 	 *    overflows.
628 	 *  * Otherwise set it a bit higher.
629 	 */
630 #if 0
631 /* XXX (msaitoh) */
632 	if (ISSET(sc->sc_hwflags, SCIF_HW_HAYESP))
633 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
634 	else if (ISSET(sc->sc_hwflags, SCIF_HW_FIFO))
635 		sc->sc_fifo = FIFO_ENABLE |
636 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
637 		     t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
638 	else
639 		sc->sc_fifo = 0;
640 #endif
641 
642 	/* And copy to tty. */
643 	tp->t_ispeed = 0;
644 	tp->t_ospeed = t->c_ospeed;
645 	tp->t_cflag = t->c_cflag;
646 
647 	if (!sc->sc_heldchange) {
648 		if (sc->sc_tx_busy) {
649 			sc->sc_heldtbc = sc->sc_tbc;
650 			sc->sc_tbc = 0;
651 			sc->sc_heldchange = 1;
652 		}
653 #if 0
654 /* XXX (msaitoh) */
655 		else
656 			scif_loadchannelregs(sc);
657 #endif
658 	}
659 
660 	if (!ISSET(t->c_cflag, CHWFLOW)) {
661 		/* Disable the high water mark. */
662 		sc->sc_r_hiwat = 0;
663 		sc->sc_r_lowat = 0;
664 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
665 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
666 			scif_schedrx(sc);
667 		}
668 	} else {
669 		sc->sc_r_hiwat = scif_rbuf_hiwat;
670 		sc->sc_r_lowat = scif_rbuf_lowat;
671 	}
672 
673 	splx(s);
674 
675 #ifdef SCIF_DEBUG
676 	if (scif_debug)
677 		scifstatus(sc, "scifparam ");
678 #endif
679 
680 	if (!ISSET(t->c_cflag, CHWFLOW)) {
681 		if (sc->sc_tx_stopped) {
682 			sc->sc_tx_stopped = 0;
683 			scifstart(tp);
684 		}
685 	}
686 
687 	return (0);
688 }
689 
690 static void
691 scif_iflush(struct scif_softc *sc)
692 {
693 	int i;
694 	unsigned char c;
695 
696 	i = scif_fdr_read() & SCFDR2_RECVCNT;
697 
698 	while (i > 0) {
699 		c = scif_frdr_read();
700 		scif_ssr_write(scif_ssr_read() & ~(SCSSR2_RDF | SCSSR2_DR));
701 		i--;
702 	}
703 }
704 
705 int
706 scifopen(dev_t dev, int flag, int mode, struct lwp *l)
707 {
708 	struct scif_softc *sc;
709 	struct tty *tp;
710 	int s, s2;
711 	int error;
712 
713 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
714 	if (sc == 0 || !ISSET(sc->sc_hwflags, SCIF_HW_DEV_OK) ||
715 	    sc->sc_rbuf == NULL)
716 		return (ENXIO);
717 
718 	if (!device_is_active(sc->sc_dev))
719 		return (ENXIO);
720 
721 #ifdef KGDB
722 	/*
723 	 * If this is the kgdb port, no other use is permitted.
724 	 */
725 	if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB))
726 		return (EBUSY);
727 #endif /* KGDB */
728 
729 	tp = sc->sc_tty;
730 
731 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
732 		return (EBUSY);
733 
734 	s = spltty();
735 
736 	/*
737 	 * Do the following iff this is a first open.
738 	 */
739 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
740 		struct termios t;
741 
742 		tp->t_dev = dev;
743 
744 		s2 = splserial();
745 
746 		/* Turn on interrupts. */
747 		scif_scr_write(scif_scr_read() | SCSCR2_TIE | SCSCR2_RIE);
748 
749 		splx(s2);
750 
751 		/*
752 		 * Initialize the termios status to the defaults.  Add in the
753 		 * sticky bits from TIOCSFLAGS.
754 		 */
755 		t.c_ispeed = 0;
756 		if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
757 			t.c_ospeed = scifcn_speed;	/* XXX (msaitoh) */
758 			t.c_cflag = scifconscflag;
759 		} else {
760 			t.c_ospeed = TTYDEF_SPEED;
761 			t.c_cflag = TTYDEF_CFLAG;
762 		}
763 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
764 			SET(t.c_cflag, CLOCAL);
765 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
766 			SET(t.c_cflag, CRTSCTS);
767 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
768 			SET(t.c_cflag, MDMBUF);
769 		/* Make sure scifparam() will do something. */
770 		tp->t_ospeed = 0;
771 		(void) scifparam(tp, &t);
772 		tp->t_iflag = TTYDEF_IFLAG;
773 		tp->t_oflag = TTYDEF_OFLAG;
774 		tp->t_lflag = TTYDEF_LFLAG;
775 		ttychars(tp);
776 		ttsetwater(tp);
777 
778 		s2 = splserial();
779 
780 		/* Clear the input ring, and unblock. */
781 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
782 		sc->sc_rbavail = scif_rbuf_size;
783 		scif_iflush(sc);
784 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
785 #if 0
786 /* XXX (msaitoh) */
787 		scif_hwiflow(sc);
788 #endif
789 
790 #ifdef SCIF_DEBUG
791 		if (scif_debug)
792 			scifstatus(sc, "scifopen  ");
793 #endif
794 
795 		splx(s2);
796 	}
797 
798 	splx(s);
799 
800 	error = ttyopen(tp, SCIFDIALOUT(dev), ISSET(flag, O_NONBLOCK));
801 	if (error)
802 		goto bad;
803 
804 	error = (*tp->t_linesw->l_open)(dev, tp);
805 	if (error)
806 		goto bad;
807 
808 	return (0);
809 
810 bad:
811 
812 	return (error);
813 }
814 
815 int
816 scifclose(dev_t dev, int flag, int mode, struct lwp *l)
817 {
818 	struct scif_softc *sc;
819 	struct tty *tp;
820 
821 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
822 	tp = sc->sc_tty;
823 
824 	/* XXX This is for cons.c. */
825 	if (!ISSET(tp->t_state, TS_ISOPEN))
826 		return (0);
827 
828 	(*tp->t_linesw->l_close)(tp, flag);
829 	ttyclose(tp);
830 
831 	if (!device_is_active(sc->sc_dev))
832 		return (0);
833 
834 	return (0);
835 }
836 
837 int
838 scifread(dev_t dev, struct uio *uio, int flag)
839 {
840 	struct scif_softc *sc;
841 	struct tty *tp;
842 
843 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
844 	tp = sc->sc_tty;
845 
846 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
847 }
848 
849 int
850 scifwrite(dev_t dev, struct uio *uio, int flag)
851 {
852 	struct scif_softc *sc;
853 	struct tty *tp;
854 
855 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
856 	tp = sc->sc_tty;
857 
858 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
859 }
860 
861 int
862 scifpoll(dev_t dev, int events, struct lwp *l)
863 {
864 	struct scif_softc *sc;
865 	struct tty *tp;
866 
867 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
868 	tp = sc->sc_tty;
869 
870 	return ((*tp->t_linesw->l_poll)(tp, events, l));
871 }
872 
873 struct tty *
874 sciftty(dev_t dev)
875 {
876 	struct scif_softc *sc;
877 	struct tty *tp;
878 
879 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
880 	tp = sc->sc_tty;
881 
882 	return (tp);
883 }
884 
885 int
886 scifioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
887 {
888 	struct scif_softc *sc;
889 	struct tty *tp;
890 	int error;
891 	int s;
892 
893 	sc = device_lookup_private(&scif_cd, SCIFUNIT(dev));
894 	if (!device_is_active(sc->sc_dev))
895 		return (EIO);
896 
897 	tp = sc->sc_tty;
898 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
899 	if (error != EPASSTHROUGH)
900 		return (error);
901 
902 	error = ttioctl(tp, cmd, data, flag, l);
903 	if (error != EPASSTHROUGH)
904 		return (error);
905 
906 	error = 0;
907 
908 	s = splserial();
909 
910 	switch (cmd) {
911 	case TIOCSBRK:
912 		scif_break(sc, 1);
913 		break;
914 
915 	case TIOCCBRK:
916 		scif_break(sc, 0);
917 		break;
918 
919 	case TIOCGFLAGS:
920 		*(int *)data = sc->sc_swflags;
921 		break;
922 
923 	case TIOCSFLAGS:
924 		error = kauth_authorize_device_tty(l->l_cred,
925 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
926 		if (error)
927 			break;
928 		sc->sc_swflags = *(int *)data;
929 		break;
930 
931 	default:
932 		error = EPASSTHROUGH;
933 		break;
934 	}
935 
936 	splx(s);
937 
938 	return (error);
939 }
940 
941 static void
942 scif_schedrx(struct scif_softc *sc)
943 {
944 
945 	sc->sc_rx_ready = 1;
946 
947 	/* Wake up the poller. */
948 	softint_schedule(sc->sc_si);
949 }
950 
951 static void
952 scif_break(struct scif_softc *sc, int onoff)
953 {
954 
955 	if (onoff)
956 		scif_ssr_write(scif_ssr_read() & ~SCSSR2_TDFE);
957 	else
958 		scif_ssr_write(scif_ssr_read() | SCSSR2_TDFE);
959 
960 #if 0	/* XXX */
961 	if (!sc->sc_heldchange) {
962 		if (sc->sc_tx_busy) {
963 			sc->sc_heldtbc = sc->sc_tbc;
964 			sc->sc_tbc = 0;
965 			sc->sc_heldchange = 1;
966 		} else
967 			scif_loadchannelregs(sc);
968 	}
969 #endif
970 }
971 
972 /*
973  * Stop output, e.g., for ^S or output flush.
974  */
975 void
976 scifstop(struct tty *tp, int flag)
977 {
978 	struct scif_softc *sc;
979 	int s;
980 
981 	sc = device_lookup_private(&scif_cd, SCIFUNIT(tp->t_dev));
982 
983 	s = splserial();
984 	if (ISSET(tp->t_state, TS_BUSY)) {
985 		/* Stop transmitting at the next chunk. */
986 		sc->sc_tbc = 0;
987 		sc->sc_heldtbc = 0;
988 		if (!ISSET(tp->t_state, TS_TTSTOP))
989 			SET(tp->t_state, TS_FLUSH);
990 	}
991 	splx(s);
992 }
993 
994 static void
995 scifdiag(void *arg)
996 {
997 	struct scif_softc *sc = arg;
998 	int overflows, floods;
999 	int s;
1000 
1001 	s = splserial();
1002 	overflows = sc->sc_overflows;
1003 	sc->sc_overflows = 0;
1004 	floods = sc->sc_floods;
1005 	sc->sc_floods = 0;
1006 	sc->sc_errors = 0;
1007 	splx(s);
1008 
1009 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
1010 	    device_xname(sc->sc_dev),
1011 	    overflows, overflows == 1 ? "" : "s",
1012 	    floods, floods == 1 ? "" : "s");
1013 }
1014 
1015 static void
1016 scif_rxsoft(struct scif_softc *sc, struct tty *tp)
1017 {
1018 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1019 	u_char *get, *end;
1020 	u_int cc, scc;
1021 	u_char ssr2;
1022 	int code;
1023 	int s;
1024 
1025 	end = sc->sc_ebuf;
1026 	get = sc->sc_rbget;
1027 	scc = cc = scif_rbuf_size - sc->sc_rbavail;
1028 
1029 	if (cc == scif_rbuf_size) {
1030 		sc->sc_floods++;
1031 		if (sc->sc_errors++ == 0)
1032 			callout_reset(&sc->sc_diag_ch, 60 * hz, scifdiag, sc);
1033 	}
1034 
1035 	while (cc) {
1036 		code = get[0];
1037 		ssr2 = get[1];
1038 		if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER | SCSSR2_PER)) {
1039 			if (ISSET(ssr2, SCSSR2_BRK | SCSSR2_FER))
1040 				SET(code, TTY_FE);
1041 			if (ISSET(ssr2, SCSSR2_PER))
1042 				SET(code, TTY_PE);
1043 		}
1044 		if ((*rint)(code, tp) == -1) {
1045 			/*
1046 			 * The line discipline's buffer is out of space.
1047 			 */
1048 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1049 				/*
1050 				 * We're either not using flow control, or the
1051 				 * line discipline didn't tell us to block for
1052 				 * some reason.  Either way, we have no way to
1053 				 * know when there's more space available, so
1054 				 * just drop the rest of the data.
1055 				 */
1056 				get += cc << 1;
1057 				if (get >= end)
1058 					get -= scif_rbuf_size << 1;
1059 				cc = 0;
1060 			} else {
1061 				/*
1062 				 * Don't schedule any more receive processing
1063 				 * until the line discipline tells us there's
1064 				 * space available (through scifhwiflow()).
1065 				 * Leave the rest of the data in the input
1066 				 * buffer.
1067 				 */
1068 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1069 			}
1070 			break;
1071 		}
1072 		get += 2;
1073 		if (get >= end)
1074 			get = sc->sc_rbuf;
1075 		cc--;
1076 	}
1077 
1078 	if (cc != scc) {
1079 		sc->sc_rbget = get;
1080 		s = splserial();
1081 		cc = sc->sc_rbavail += scc - cc;
1082 		/* Buffers should be ok again, release possible block. */
1083 		if (cc >= sc->sc_r_lowat) {
1084 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1085 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1086 				scif_scr_write(scif_scr_read() | SCSCR2_RIE);
1087 			}
1088 #if 0
1089 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1090 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1091 				scif_hwiflow(sc);
1092 			}
1093 #endif
1094 		}
1095 		splx(s);
1096 	}
1097 }
1098 
1099 static void
1100 scif_txsoft(struct scif_softc *sc, struct tty *tp)
1101 {
1102 
1103 	CLR(tp->t_state, TS_BUSY);
1104 	if (ISSET(tp->t_state, TS_FLUSH))
1105 		CLR(tp->t_state, TS_FLUSH);
1106 	else
1107 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1108 	(*tp->t_linesw->l_start)(tp);
1109 }
1110 
1111 #if 0 /* XXX (msaitoh) */
1112 static void
1113 scif_stsoft(struct scif_softc *sc, struct tty *tp)
1114 {
1115 	u_char msr, delta;
1116 	int s;
1117 
1118 	s = splserial();
1119 	msr = sc->sc_msr;
1120 	delta = sc->sc_msr_delta;
1121 	sc->sc_msr_delta = 0;
1122 	splx(s);
1123 
1124 	if (ISSET(delta, sc->sc_msr_dcd)) {
1125 		/*
1126 		 * Inform the tty layer that carrier detect changed.
1127 		 */
1128 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1129 	}
1130 
1131 	if (ISSET(delta, sc->sc_msr_cts)) {
1132 		/* Block or unblock output according to flow control. */
1133 		if (ISSET(msr, sc->sc_msr_cts)) {
1134 			sc->sc_tx_stopped = 0;
1135 			(*tp->t_linesw->l_start)(tp);
1136 		} else {
1137 			sc->sc_tx_stopped = 1;
1138 		}
1139 	}
1140 
1141 #ifdef SCIF_DEBUG
1142 	if (scif_debug)
1143 		scifstatus(sc, "scif_stsoft");
1144 #endif
1145 }
1146 #endif /* 0 */
1147 
1148 static void
1149 scifsoft(void *arg)
1150 {
1151 	struct scif_softc *sc = arg;
1152 	struct tty *tp;
1153 
1154 	if (!device_is_active(sc->sc_dev))
1155 		return;
1156 
1157 	tp = sc->sc_tty;
1158 
1159 	if (sc->sc_rx_ready) {
1160 		sc->sc_rx_ready = 0;
1161 		scif_rxsoft(sc, tp);
1162 	}
1163 
1164 #if 0
1165 	if (sc->sc_st_check) {
1166 		sc->sc_st_check = 0;
1167 		scif_stsoft(sc, tp);
1168 	}
1169 #endif
1170 
1171 	if (sc->sc_tx_done) {
1172 		sc->sc_tx_done = 0;
1173 		scif_txsoft(sc, tp);
1174 	}
1175 }
1176 
1177 static int
1178 scifintr(void *arg)
1179 {
1180 	struct scif_softc *sc = arg;
1181 	u_char *put, *end;
1182 	u_int cc;
1183 	u_short ssr2;
1184 	int count;
1185 
1186 	if (!device_is_active(sc->sc_dev))
1187 		return (0);
1188 
1189 	end = sc->sc_ebuf;
1190 	put = sc->sc_rbput;
1191 	cc = sc->sc_rbavail;
1192 
1193 	do {
1194 		ssr2 = scif_ssr_read();
1195 		if (ISSET(ssr2, SCSSR2_BRK)) {
1196 			scif_ssr_write(scif_ssr_read()
1197 				& ~(SCSSR2_ER | SCSSR2_BRK | SCSSR2_DR));
1198 #ifdef DDB
1199 			if (ISSET(sc->sc_hwflags, SCIF_HW_CONSOLE)) {
1200 				console_debugger();
1201 			}
1202 #endif /* DDB */
1203 #ifdef KGDB
1204 			if (ISSET(sc->sc_hwflags, SCIF_HW_KGDB)) {
1205 				kgdb_connect(1);
1206 			}
1207 #endif /* KGDB */
1208 		}
1209 		count = scif_fdr_read() & SCFDR2_RECVCNT;
1210 		if (count != 0) {
1211 			for (;;) {
1212 				u_char c = scif_frdr_read();
1213 				u_char err = (u_char)(scif_ssr_read() & 0x00ff);
1214 
1215 				scif_ssr_write(scif_ssr_read()
1216 				    & ~(SCSSR2_ER | SCSSR2_RDF | SCSSR2_DR));
1217 #ifdef SH4
1218 				if (CPU_IS_SH4)
1219 					scif_lsr_write(scif_lsr_read()
1220 						       & ~SCLSR2_ORER);
1221 #endif
1222 				if ((cc > 0) && (count > 0)) {
1223 					put[0] = c;
1224 					put[1] = err;
1225 					put += 2;
1226 					if (put >= end)
1227 						put = sc->sc_rbuf;
1228 					cc--;
1229 					count--;
1230 				} else
1231 					break;
1232 			}
1233 
1234 			/*
1235 			 * Current string of incoming characters ended because
1236 			 * no more data was available or we ran out of space.
1237 			 * Schedule a receive event if any data was received.
1238 			 * If we're out of space, turn off receive interrupts.
1239 			 */
1240 			sc->sc_rbput = put;
1241 			sc->sc_rbavail = cc;
1242 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1243 				sc->sc_rx_ready = 1;
1244 
1245 			/*
1246 			 * See if we are in danger of overflowing a buffer. If
1247 			 * so, use hardware flow control to ease the pressure.
1248 			 */
1249 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1250 			    cc < sc->sc_r_hiwat) {
1251 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1252 #if 0
1253 				scif_hwiflow(sc);
1254 #endif
1255 			}
1256 
1257 			/*
1258 			 * If we're out of space, disable receive interrupts
1259 			 * until the queue has drained a bit.
1260 			 */
1261 			if (!cc) {
1262 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1263 				scif_scr_write(scif_scr_read() & ~SCSCR2_RIE);
1264 			}
1265 		} else {
1266 			if (scif_ssr_read() & (SCSSR2_RDF | SCSSR2_DR)) {
1267 				scif_scr_write(scif_scr_read()
1268 					       & ~(SCSCR2_TIE | SCSCR2_RIE));
1269 				delay(10);
1270 				scif_scr_write(scif_scr_read()
1271 					       | SCSCR2_TIE | SCSCR2_RIE);
1272 				continue;
1273 			}
1274 		}
1275 	} while (scif_ssr_read() & (SCSSR2_RDF | SCSSR2_DR));
1276 
1277 #if 0
1278 	msr = bus_space_read_1(iot, ioh, scif_msr);
1279 	delta = msr ^ sc->sc_msr;
1280 	sc->sc_msr = msr;
1281 	if (ISSET(delta, sc->sc_msr_mask)) {
1282 		SET(sc->sc_msr_delta, delta);
1283 
1284 		/*
1285 		 * Pulse-per-second clock signal on edge of DCD?
1286 		 */
1287 		if (ISSET(delta, sc->sc_ppsmask)) {
1288 			struct timeval tv;
1289 			if (ISSET(msr, sc->sc_ppsmask) ==
1290 			    sc->sc_ppsassert) {
1291 				/* XXX nanotime() */
1292 				microtime(&tv);
1293 				TIMEVAL_TO_TIMESPEC(&tv,
1294 						    &sc->ppsinfo.assert_timestamp);
1295 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1296 					timespecadd(&sc->ppsinfo.assert_timestamp,
1297 						    &sc->ppsparam.assert_offset,
1298 						    &sc->ppsinfo.assert_timestamp);
1299 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
1300 				}
1301 
1302 #ifdef PPS_SYNC
1303 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1304 					hardpps(&tv, tv.tv_usec);
1305 #endif
1306 				sc->ppsinfo.assert_sequence++;
1307 				sc->ppsinfo.current_mode =
1308 					sc->ppsparam.mode;
1309 
1310 			} else if (ISSET(msr, sc->sc_ppsmask) ==
1311 				   sc->sc_ppsclear) {
1312 				/* XXX nanotime() */
1313 				microtime(&tv);
1314 				TIMEVAL_TO_TIMESPEC(&tv,
1315 						    &sc->ppsinfo.clear_timestamp);
1316 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1317 					timespecadd(&sc->ppsinfo.clear_timestamp,
1318 						    &sc->ppsparam.clear_offset,
1319 						    &sc->ppsinfo.clear_timestamp);
1320 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
1321 				}
1322 
1323 #ifdef PPS_SYNC
1324 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1325 					hardpps(&tv, tv.tv_usec);
1326 #endif
1327 				sc->ppsinfo.clear_sequence++;
1328 				sc->ppsinfo.current_mode =
1329 					sc->ppsparam.mode;
1330 			}
1331 		}
1332 
1333 		/*
1334 		 * Stop output immediately if we lose the output
1335 		 * flow control signal or carrier detect.
1336 		 */
1337 		if (ISSET(~msr, sc->sc_msr_mask)) {
1338 			sc->sc_tbc = 0;
1339 			sc->sc_heldtbc = 0;
1340 #ifdef SCIF_DEBUG
1341 			if (scif_debug)
1342 				scifstatus(sc, "scifintr  ");
1343 #endif
1344 		}
1345 
1346 		sc->sc_st_check = 1;
1347 	}
1348 #endif
1349 
1350 	/*
1351 	 * Done handling any receive interrupts. See if data can be
1352 	 * transmitted as well. Schedule tx done event if no data left
1353 	 * and tty was marked busy.
1354 	 */
1355 	if (((scif_fdr_read() & SCFDR2_TXCNT) >> 8) != 16) { /* XXX (msaitoh) */
1356 		/*
1357 		 * If we've delayed a parameter change, do it now, and restart
1358 		 * output.
1359 		 */
1360 		if (sc->sc_heldchange) {
1361 			sc->sc_heldchange = 0;
1362 			sc->sc_tbc = sc->sc_heldtbc;
1363 			sc->sc_heldtbc = 0;
1364 		}
1365 
1366 		/* Output the next chunk of the contiguous buffer, if any. */
1367 		if (sc->sc_tbc > 0) {
1368 			int n;
1369 			int maxchars;
1370 			int i;
1371 
1372 			n = sc->sc_tbc;
1373 			maxchars = sc->sc_fifolen -
1374 				((scif_fdr_read() & SCFDR2_TXCNT) >> 8);
1375 			if (n > maxchars)
1376 				n = maxchars;
1377 
1378 			for (i = 0; i < n; i++) {
1379 				scif_putc(*(sc->sc_tba));
1380 				sc->sc_tba++;
1381 			}
1382 			sc->sc_tbc -= n;
1383 		} else {
1384 			/* Disable transmit completion interrupts if necessary. */
1385 #if 0
1386 			if (ISSET(sc->sc_ier, IER_ETXRDY))
1387 #endif
1388 				scif_scr_write(scif_scr_read() & ~SCSCR2_TIE);
1389 
1390 			if (sc->sc_tx_busy) {
1391 				sc->sc_tx_busy = 0;
1392 				sc->sc_tx_done = 1;
1393 			}
1394 		}
1395 	}
1396 
1397 	/* Wake up the poller. */
1398 	softint_schedule(sc->sc_si);
1399 
1400 #if NRND > 0 && defined(RND_SCIF)
1401 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
1402 #endif
1403 
1404 	return (1);
1405 }
1406 
1407 void
1408 scifcnprobe(struct consdev *cp)
1409 {
1410 	int maj;
1411 
1412 	/* locate the major number */
1413 	maj = cdevsw_lookup_major(&scif_cdevsw);
1414 
1415 	/* Initialize required fields. */
1416 	cp->cn_dev = makedev(maj, 0);
1417 #ifdef SCIFCONSOLE
1418 	cp->cn_pri = CN_REMOTE;
1419 #else
1420 	cp->cn_pri = CN_NORMAL;
1421 #endif
1422 }
1423 
1424 void
1425 scifcninit(struct consdev *cp)
1426 {
1427 
1428 	InitializeScif(scifcn_speed);
1429 	scifisconsole = 1;
1430 }
1431 
1432 int
1433 scifcngetc(dev_t dev)
1434 {
1435 	int c;
1436 	int s;
1437 
1438 	s = splserial();
1439 	c = scif_getc();
1440 	splx(s);
1441 
1442 	return (c);
1443 }
1444 
1445 void
1446 scifcnputc(dev_t dev, int c)
1447 {
1448 	int s;
1449 
1450 	s = splserial();
1451 	scif_putc((u_char)c);
1452 	splx(s);
1453 }
1454 
1455 #ifdef KGDB
1456 int
1457 scif_kgdb_init()
1458 {
1459 
1460 	if (strcmp(kgdb_devname, "scif") != 0)
1461 		return (1);
1462 
1463 	if (scifisconsole)
1464 		return (1);	/* can't share with console */
1465 
1466 	InitializeScif(kgdb_rate);
1467 
1468 	kgdb_attach((int (*)(void *))scifcngetc,
1469 	    (void (*)(void *, int))scifcnputc, NULL);
1470 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1471 	kgdb_attached = 1;
1472 
1473 	return (0);
1474 }
1475 #endif /* KGDB */
1476