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