xref: /netbsd-src/sys/arch/sh3/dev/sci.c (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /* $NetBSD: sci.c,v 1.45 2006/10/01 20:31:50 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.45 2006/10/01 20:31:50 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 (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
669 		return (EBUSY);
670 
671 	s = spltty();
672 
673 	/*
674 	 * Do the following iff this is a first open.
675 	 */
676 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
677 		struct termios t;
678 
679 		tp->t_dev = dev;
680 
681 		s2 = splserial();
682 
683 		/* Turn on interrupts. */
684 		SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
685 
686 		splx(s2);
687 
688 		/*
689 		 * Initialize the termios status to the defaults.  Add in the
690 		 * sticky bits from TIOCSFLAGS.
691 		 */
692 		t.c_ispeed = 0;
693 		if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
694 			t.c_ospeed = scicn_speed;
695 			t.c_cflag = sciconscflag;
696 		} else {
697 			t.c_ospeed = TTYDEF_SPEED;
698 			t.c_cflag = TTYDEF_CFLAG;
699 		}
700 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
701 			SET(t.c_cflag, CLOCAL);
702 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
703 			SET(t.c_cflag, CRTSCTS);
704 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
705 			SET(t.c_cflag, MDMBUF);
706 		/* Make sure sciparam() will do something. */
707 		tp->t_ospeed = 0;
708 		(void) sciparam(tp, &t);
709 		tp->t_iflag = TTYDEF_IFLAG;
710 		tp->t_oflag = TTYDEF_OFLAG;
711 		tp->t_lflag = TTYDEF_LFLAG;
712 		ttychars(tp);
713 		ttsetwater(tp);
714 
715 		s2 = splserial();
716 
717 		/* Clear the input ring, and unblock. */
718 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
719 		sc->sc_rbavail = sci_rbuf_size;
720 		sci_iflush(sc);
721 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
722 #if 0
723 /* XXX (msaitoh) */
724 		sci_hwiflow(sc);
725 #endif
726 
727 #ifdef SCI_DEBUG
728 		if (sci_debug)
729 			scistatus(sc, "sciopen  ");
730 #endif
731 
732 		splx(s2);
733 	}
734 
735 	splx(s);
736 
737 	error = ttyopen(tp, SCIDIALOUT(dev), ISSET(flag, O_NONBLOCK));
738 	if (error)
739 		goto bad;
740 
741 	error = (*tp->t_linesw->l_open)(dev, tp);
742 	if (error)
743 		goto bad;
744 
745 	return (0);
746 
747 bad:
748 
749 	return (error);
750 }
751 
752 int
753 sciclose(dev_t dev, int flag, int mode, struct lwp *l)
754 {
755 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
756 	struct tty *tp = sc->sc_tty;
757 
758 	/* XXX This is for cons.c. */
759 	if (!ISSET(tp->t_state, TS_ISOPEN))
760 		return (0);
761 
762 	(*tp->t_linesw->l_close)(tp, flag);
763 	ttyclose(tp);
764 
765 	if (!device_is_active(&sc->sc_dev))
766 		return (0);
767 
768 	return (0);
769 }
770 
771 int
772 sciread(dev_t dev, struct uio *uio, int flag)
773 {
774 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
775 	struct tty *tp = sc->sc_tty;
776 
777 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
778 }
779 
780 int
781 sciwrite(dev_t dev, struct uio *uio, int flag)
782 {
783 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
784 	struct tty *tp = sc->sc_tty;
785 
786 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
787 }
788 
789 int
790 scipoll(dev_t dev, int events, struct lwp *l)
791 {
792 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
793 	struct tty *tp = sc->sc_tty;
794 
795 	return ((*tp->t_linesw->l_poll)(tp, events, l));
796 }
797 
798 struct tty *
799 scitty(dev_t dev)
800 {
801 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
802 	struct tty *tp = sc->sc_tty;
803 
804 	return (tp);
805 }
806 
807 int
808 sciioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
809 {
810 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
811 	struct tty *tp = sc->sc_tty;
812 	int error;
813 	int s;
814 
815 	if (!device_is_active(&sc->sc_dev))
816 		return (EIO);
817 
818 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
819 	if (error != EPASSTHROUGH)
820 		return (error);
821 
822 	error = ttioctl(tp, cmd, data, flag, l);
823 	if (error != EPASSTHROUGH)
824 		return (error);
825 
826 	error = 0;
827 
828 	s = splserial();
829 
830 	switch (cmd) {
831 	case TIOCSBRK:
832 		sci_break(sc, 1);
833 		break;
834 
835 	case TIOCCBRK:
836 		sci_break(sc, 0);
837 		break;
838 
839 	case TIOCGFLAGS:
840 		*(int *)data = sc->sc_swflags;
841 		break;
842 
843 	case TIOCSFLAGS:
844 		error = kauth_authorize_device_tty(l->l_cred,
845 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
846 		if (error)
847 			break;
848 		sc->sc_swflags = *(int *)data;
849 		break;
850 
851 	default:
852 		error = EPASSTHROUGH;
853 		break;
854 	}
855 
856 	splx(s);
857 
858 	return (error);
859 }
860 
861 integrate void
862 sci_schedrx(struct sci_softc *sc)
863 {
864 
865 	sc->sc_rx_ready = 1;
866 
867 	/* Wake up the poller. */
868 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
869 	softintr_schedule(sc->sc_si);
870 #else
871 #ifndef __NO_SOFT_SERIAL_INTERRUPT
872 	setsoftserial();
873 #else
874 	if (!sci_softintr_scheduled) {
875 		sci_softintr_scheduled = 1;
876 		callout_reset(&sci_soft_ch, 1, scisoft, NULL);
877 	}
878 #endif
879 #endif
880 }
881 
882 void
883 sci_break(struct sci_softc *sc, int onoff)
884 {
885 
886 	if (onoff)
887 		SHREG_SCSSR &= ~SCSSR_TDRE;
888 	else
889 		SHREG_SCSSR |= SCSSR_TDRE;
890 
891 #if 0	/* XXX */
892 	if (!sc->sc_heldchange) {
893 		if (sc->sc_tx_busy) {
894 			sc->sc_heldtbc = sc->sc_tbc;
895 			sc->sc_tbc = 0;
896 			sc->sc_heldchange = 1;
897 		} else
898 			sci_loadchannelregs(sc);
899 	}
900 #endif
901 }
902 
903 /*
904  * Stop output, e.g., for ^S or output flush.
905  */
906 void
907 scistop(struct tty *tp, int flag)
908 {
909 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(tp->t_dev)];
910 	int s;
911 
912 	s = splserial();
913 	if (ISSET(tp->t_state, TS_BUSY)) {
914 		/* Stop transmitting at the next chunk. */
915 		sc->sc_tbc = 0;
916 		sc->sc_heldtbc = 0;
917 		if (!ISSET(tp->t_state, TS_TTSTOP))
918 			SET(tp->t_state, TS_FLUSH);
919 	}
920 	splx(s);
921 }
922 
923 void
924 scidiag(void *arg)
925 {
926 	struct sci_softc *sc = arg;
927 	int overflows, floods;
928 	int s;
929 
930 	s = splserial();
931 	overflows = sc->sc_overflows;
932 	sc->sc_overflows = 0;
933 	floods = sc->sc_floods;
934 	sc->sc_floods = 0;
935 	sc->sc_errors = 0;
936 	splx(s);
937 
938 	log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n",
939 	    sc->sc_dev.dv_xname,
940 	    overflows, overflows == 1 ? "" : "s",
941 	    floods, floods == 1 ? "" : "s");
942 }
943 
944 integrate void
945 sci_rxsoft(struct sci_softc *sc, struct tty *tp)
946 {
947 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
948 	u_char *get, *end;
949 	u_int cc, scc;
950 	u_char ssr;
951 	int code;
952 	int s;
953 
954 	end = sc->sc_ebuf;
955 	get = sc->sc_rbget;
956 	scc = cc = sci_rbuf_size - sc->sc_rbavail;
957 
958 	if (cc == sci_rbuf_size) {
959 		sc->sc_floods++;
960 		if (sc->sc_errors++ == 0)
961 			callout_reset(&sc->sc_diag_ch, 60 * hz, scidiag, sc);
962 	}
963 
964 	while (cc) {
965 		code = get[0];
966 		ssr = get[1];
967 		if (ISSET(ssr, SCSSR_FER | SCSSR_PER)) {
968 			if (ISSET(ssr, SCSSR_FER))
969 				SET(code, TTY_FE);
970 			if (ISSET(ssr, SCSSR_PER))
971 				SET(code, TTY_PE);
972 		}
973 		if ((*rint)(code, tp) == -1) {
974 			/*
975 			 * The line discipline's buffer is out of space.
976 			 */
977 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
978 				/*
979 				 * We're either not using flow control, or the
980 				 * line discipline didn't tell us to block for
981 				 * some reason.  Either way, we have no way to
982 				 * know when there's more space available, so
983 				 * just drop the rest of the data.
984 				 */
985 				get += cc << 1;
986 				if (get >= end)
987 					get -= sci_rbuf_size << 1;
988 				cc = 0;
989 			} else {
990 				/*
991 				 * Don't schedule any more receive processing
992 				 * until the line discipline tells us there's
993 				 * space available (through scihwiflow()).
994 				 * Leave the rest of the data in the input
995 				 * buffer.
996 				 */
997 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
998 			}
999 			break;
1000 		}
1001 		get += 2;
1002 		if (get >= end)
1003 			get = sc->sc_rbuf;
1004 		cc--;
1005 	}
1006 
1007 	if (cc != scc) {
1008 		sc->sc_rbget = get;
1009 		s = splserial();
1010 		cc = sc->sc_rbavail += scc - cc;
1011 		/* Buffers should be ok again, release possible block. */
1012 		if (cc >= sc->sc_r_lowat) {
1013 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1014 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1015 				SHREG_SCSCR |= SCSCR_RIE;
1016 			}
1017 #if 0
1018 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1019 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1020 				sci_hwiflow(sc);
1021 			}
1022 #endif
1023 		}
1024 		splx(s);
1025 	}
1026 }
1027 
1028 integrate void
1029 sci_txsoft(sc, tp)
1030 	struct sci_softc *sc;
1031 	struct tty *tp;
1032 {
1033 
1034 	CLR(tp->t_state, TS_BUSY);
1035 	if (ISSET(tp->t_state, TS_FLUSH))
1036 		CLR(tp->t_state, TS_FLUSH);
1037 	else
1038 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1039 	(*tp->t_linesw->l_start)(tp);
1040 }
1041 
1042 integrate void
1043 sci_stsoft(struct sci_softc *sc, struct tty *tp)
1044 {
1045 #if 0
1046 /* XXX (msaitoh) */
1047 	u_char msr, delta;
1048 	int s;
1049 
1050 	s = splserial();
1051 	msr = sc->sc_msr;
1052 	delta = sc->sc_msr_delta;
1053 	sc->sc_msr_delta = 0;
1054 	splx(s);
1055 
1056 	if (ISSET(delta, sc->sc_msr_dcd)) {
1057 		/*
1058 		 * Inform the tty layer that carrier detect changed.
1059 		 */
1060 		(void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD));
1061 	}
1062 
1063 	if (ISSET(delta, sc->sc_msr_cts)) {
1064 		/* Block or unblock output according to flow control. */
1065 		if (ISSET(msr, sc->sc_msr_cts)) {
1066 			sc->sc_tx_stopped = 0;
1067 			(*tp->t_linesw->l_start)(tp);
1068 		} else {
1069 			sc->sc_tx_stopped = 1;
1070 		}
1071 	}
1072 
1073 #ifdef SCI_DEBUG
1074 	if (sci_debug)
1075 		scistatus(sc, "sci_stsoft");
1076 #endif
1077 #endif
1078 }
1079 
1080 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1081 void
1082 scisoft(void *arg)
1083 {
1084 	struct sci_softc *sc = arg;
1085 	struct tty *tp;
1086 
1087 	if (!device_is_active(&sc->sc_dev))
1088 		return;
1089 
1090 	{
1091 #else
1092 void
1093 #ifndef __NO_SOFT_SERIAL_INTERRUPT
1094 scisoft()
1095 #else
1096 scisoft(void *arg)
1097 #endif
1098 {
1099 	struct sci_softc	*sc;
1100 	struct tty	*tp;
1101 	int	unit;
1102 #ifdef __NO_SOFT_SERIAL_INTERRUPT
1103 	int s;
1104 
1105 	s = splsoftserial();
1106 	sci_softintr_scheduled = 0;
1107 #endif
1108 
1109 	for (unit = 0; unit < sci_cd.cd_ndevs; unit++) {
1110 		sc = sci_cd.cd_devs[unit];
1111 		if (sc == NULL || !ISSET(sc->sc_hwflags, SCI_HW_DEV_OK))
1112 			continue;
1113 
1114 		if (!device_is_active(&sc->sc_dev))
1115 			continue;
1116 
1117 		tp = sc->sc_tty;
1118 		if (tp == NULL)
1119 			continue;
1120 		if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0)
1121 			continue;
1122 #endif
1123 		tp = sc->sc_tty;
1124 
1125 		if (sc->sc_rx_ready) {
1126 			sc->sc_rx_ready = 0;
1127 			sci_rxsoft(sc, tp);
1128 		}
1129 
1130 #if 0
1131 		if (sc->sc_st_check) {
1132 			sc->sc_st_check = 0;
1133 			sci_stsoft(sc, tp);
1134 		}
1135 #endif
1136 
1137 		if (sc->sc_tx_done) {
1138 			sc->sc_tx_done = 0;
1139 			sci_txsoft(sc, tp);
1140 		}
1141 	}
1142 
1143 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
1144 #ifdef __NO_SOFT_SERIAL_INTERRUPT
1145 	splx(s);
1146 #endif
1147 #endif
1148 }
1149 
1150 int
1151 sciintr(void *arg)
1152 {
1153 	struct sci_softc *sc = arg;
1154 	u_char *put, *end;
1155 	u_int cc;
1156 	u_short ssr;
1157 
1158 	if (!device_is_active(&sc->sc_dev))
1159 		return (0);
1160 
1161 	end = sc->sc_ebuf;
1162 	put = sc->sc_rbput;
1163 	cc = sc->sc_rbavail;
1164 
1165 	do {
1166 		ssr = SHREG_SCSSR;
1167 		if (ISSET(ssr, SCSSR_FER)) {
1168 			SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_PER | SCSSR_FER);
1169 #if defined(DDB) || defined(KGDB)
1170 #ifdef SH4
1171 			if ((SHREG_SCSPTR & SCSPTR_SPB0DT) != 0) {
1172 #else
1173 			if ((SHREG_SCSPDR & SCSPDR_SCP0DT) != 0) {
1174 #endif
1175 #ifdef DDB
1176 				if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
1177 					console_debugger();
1178 				}
1179 #endif
1180 #ifdef KGDB
1181 				if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) {
1182 					kgdb_connect(1);
1183 				}
1184 #endif
1185 			}
1186 #endif /* DDB || KGDB */
1187 		}
1188 		if ((SHREG_SCSSR & SCSSR_RDRF) != 0) {
1189 			if (cc > 0) {
1190 				put[0] = SHREG_SCRDR;
1191 				put[1] = SHREG_SCSSR & 0x00ff;
1192 
1193 				put += 2;
1194 				if (put >= end)
1195 					put = sc->sc_rbuf;
1196 				cc--;
1197 			}
1198 
1199 			SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER |
1200 			    SCSSR_RDRF);
1201 
1202 				/*
1203 				 * Current string of incoming characters ended because
1204 				 * no more data was available or we ran out of space.
1205 				 * Schedule a receive event if any data was received.
1206 				 * If we're out of space, turn off receive interrupts.
1207 				 */
1208 			sc->sc_rbput = put;
1209 			sc->sc_rbavail = cc;
1210 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1211 				sc->sc_rx_ready = 1;
1212 
1213 				/*
1214 				 * See if we are in danger of overflowing a buffer. If
1215 				 * so, use hardware flow control to ease the pressure.
1216 				 */
1217 			if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) &&
1218 			    cc < sc->sc_r_hiwat) {
1219 				SET(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1220 #if 0
1221 				sci_hwiflow(sc);
1222 #endif
1223 			}
1224 
1225 				/*
1226 				 * If we're out of space, disable receive interrupts
1227 				 * until the queue has drained a bit.
1228 				 */
1229 			if (!cc) {
1230 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1231 				SHREG_SCSCR &= ~SCSCR_RIE;
1232 			}
1233 		} else {
1234 			if (SHREG_SCSSR & SCSSR_RDRF) {
1235 				SHREG_SCSCR &= ~(SCSCR_TIE | SCSCR_RIE);
1236 				delay(10);
1237 				SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
1238 				continue;
1239 			}
1240 		}
1241 	} while (SHREG_SCSSR & SCSSR_RDRF);
1242 
1243 #if 0
1244 	msr = bus_space_read_1(iot, ioh, sci_msr);
1245 	delta = msr ^ sc->sc_msr;
1246 	sc->sc_msr = msr;
1247 	if (ISSET(delta, sc->sc_msr_mask)) {
1248 		SET(sc->sc_msr_delta, delta);
1249 
1250 		/*
1251 		 * Pulse-per-second clock signal on edge of DCD?
1252 		 */
1253 		if (ISSET(delta, sc->sc_ppsmask)) {
1254 			struct timeval tv;
1255 			if (ISSET(msr, sc->sc_ppsmask) ==
1256 			    sc->sc_ppsassert) {
1257 				/* XXX nanotime() */
1258 				microtime(&tv);
1259 				TIMEVAL_TO_TIMESPEC(&tv,
1260 						    &sc->ppsinfo.assert_timestamp);
1261 				if (sc->ppsparam.mode & PPS_OFFSETASSERT) {
1262 					timespecadd(&sc->ppsinfo.assert_timestamp,
1263 						    &sc->ppsparam.assert_offset,
1264 						    &sc->ppsinfo.assert_timestamp);
1265 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp);
1266 				}
1267 
1268 #ifdef PPS_SYNC
1269 				if (sc->ppsparam.mode & PPS_HARDPPSONASSERT)
1270 					hardpps(&tv, tv.tv_usec);
1271 #endif
1272 				sc->ppsinfo.assert_sequence++;
1273 				sc->ppsinfo.current_mode =
1274 					sc->ppsparam.mode;
1275 
1276 			} else if (ISSET(msr, sc->sc_ppsmask) ==
1277 				   sc->sc_ppsclear) {
1278 				/* XXX nanotime() */
1279 				microtime(&tv);
1280 				TIMEVAL_TO_TIMESPEC(&tv,
1281 						    &sc->ppsinfo.clear_timestamp);
1282 				if (sc->ppsparam.mode & PPS_OFFSETCLEAR) {
1283 					timespecadd(&sc->ppsinfo.clear_timestamp,
1284 						    &sc->ppsparam.clear_offset,
1285 						    &sc->ppsinfo.clear_timestamp);
1286 					TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp);
1287 				}
1288 
1289 #ifdef PPS_SYNC
1290 				if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR)
1291 					hardpps(&tv, tv.tv_usec);
1292 #endif
1293 				sc->ppsinfo.clear_sequence++;
1294 				sc->ppsinfo.current_mode =
1295 					sc->ppsparam.mode;
1296 			}
1297 		}
1298 
1299 		/*
1300 		 * Stop output immediately if we lose the output
1301 		 * flow control signal or carrier detect.
1302 		 */
1303 		if (ISSET(~msr, sc->sc_msr_mask)) {
1304 			sc->sc_tbc = 0;
1305 			sc->sc_heldtbc = 0;
1306 #ifdef SCI_DEBUG
1307 			if (sci_debug)
1308 				scistatus(sc, "sciintr  ");
1309 #endif
1310 		}
1311 
1312 		sc->sc_st_check = 1;
1313 	}
1314 #endif
1315 
1316 	/*
1317 	 * Done handling any receive interrupts. See if data can be
1318 	 * transmitted as well. Schedule tx done event if no data left
1319 	 * and tty was marked busy.
1320 	 */
1321 	if ((SHREG_SCSSR & SCSSR_TDRE) != 0) {
1322 		/*
1323 		 * If we've delayed a parameter change, do it now, and restart
1324 		 * output.
1325 		 */
1326 		if (sc->sc_heldchange) {
1327 			sc->sc_heldchange = 0;
1328 			sc->sc_tbc = sc->sc_heldtbc;
1329 			sc->sc_heldtbc = 0;
1330 		}
1331 
1332 		/* Output the next chunk of the contiguous buffer, if any. */
1333 		if (sc->sc_tbc > 0) {
1334 			sci_putc(*(sc->sc_tba));
1335 			sc->sc_tba++;
1336 			sc->sc_tbc--;
1337 		} else {
1338 			/* Disable transmit completion interrupts if necessary. */
1339 #if 0
1340 			if (ISSET(sc->sc_ier, IER_ETXRDY))
1341 #endif
1342 				SHREG_SCSCR &= ~SCSCR_TIE;
1343 
1344 			if (sc->sc_tx_busy) {
1345 				sc->sc_tx_busy = 0;
1346 				sc->sc_tx_done = 1;
1347 			}
1348 		}
1349 	}
1350 
1351 	/* Wake up the poller. */
1352 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1353 	softintr_schedule(sc->sc_si);
1354 #else
1355 #ifndef __NO_SOFT_SERIAL_INTERRUPT
1356 	setsoftserial();
1357 #else
1358 	if (!sci_softintr_scheduled) {
1359 		sci_softintr_scheduled = 1;
1360 		callout_reset(&sci_soft_ch, 1, scisoft, 1);
1361 	}
1362 #endif
1363 #endif
1364 
1365 #if NRND > 0 && defined(RND_SCI)
1366 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
1367 #endif
1368 
1369 	return (1);
1370 }
1371 
1372 void
1373 scicnprobe(cp)
1374 	struct consdev *cp;
1375 {
1376 	int maj;
1377 
1378 	/* locate the major number */
1379 	maj = cdevsw_lookup_major(&sci_cdevsw);
1380 
1381 	/* Initialize required fields. */
1382 	cp->cn_dev = makedev(maj, 0);
1383 #ifdef SCICONSOLE
1384 	cp->cn_pri = CN_REMOTE;
1385 #else
1386 	cp->cn_pri = CN_NORMAL;
1387 #endif
1388 }
1389 
1390 void
1391 scicninit(struct consdev *cp)
1392 {
1393 
1394 	InitializeSci(scicn_speed);
1395 	sciisconsole = 1;
1396 }
1397 
1398 int
1399 scicngetc(dev_t dev)
1400 {
1401 	int c;
1402 	int s;
1403 
1404 	s = splserial();
1405 	c = sci_getc();
1406 	splx(s);
1407 
1408 	return (c);
1409 }
1410 
1411 void
1412 scicnputc(dev_t dev, int c)
1413 {
1414 	int s;
1415 
1416 	s = splserial();
1417 	sci_putc((u_char)c);
1418 	splx(s);
1419 }
1420