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