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