xref: /netbsd-src/sys/arch/sh3/dev/sci.c (revision eb961d0e02b7a46a9acfa877b02df48df6637278)
1 /* $NetBSD: sci.c,v 1.41 2006/03/07 10:53:29 uebayasi 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.41 2006/03/07 10:53:29 uebayasi 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 /* Hardware flag masks */
219 #define	SCI_HW_NOIEN	0x01
220 #define	SCI_HW_FIFO	0x02
221 #define	SCI_HW_FLOW	0x08
222 #define	SCI_HW_DEV_OK	0x20
223 #define	SCI_HW_CONSOLE	0x40
224 #define	SCI_HW_KGDB	0x80
225 
226 /* Buffer size for character buffer */
227 #define	SCI_RING_SIZE	2048
228 
229 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
230 u_int sci_rbuf_hiwat = (SCI_RING_SIZE * 1) / 4;
231 u_int sci_rbuf_lowat = (SCI_RING_SIZE * 3) / 4;
232 
233 #define	CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
234 int sciconscflag = CONMODE;
235 int sciisconsole = 0;
236 
237 #ifdef SCICN_SPEED
238 int scicn_speed = SCICN_SPEED;
239 #else
240 int scicn_speed = 9600;
241 #endif
242 
243 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
244 
245 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS
246 #ifdef __NO_SOFT_SERIAL_INTERRUPT
247 volatile int	sci_softintr_scheduled;
248 struct callout sci_soft_ch = CALLOUT_INITIALIZER;
249 #endif
250 #endif
251 
252 u_int sci_rbuf_size = SCI_RING_SIZE;
253 
254 CFATTACH_DECL(sci, sizeof(struct sci_softc),
255     sci_match, sci_attach, NULL, NULL);
256 
257 extern struct cfdriver sci_cd;
258 
259 static int sci_attached;
260 
261 dev_type_open(sciopen);
262 dev_type_close(sciclose);
263 dev_type_read(sciread);
264 dev_type_write(sciwrite);
265 dev_type_ioctl(sciioctl);
266 dev_type_stop(scistop);
267 dev_type_tty(scitty);
268 dev_type_poll(scipoll);
269 
270 const struct cdevsw sci_cdevsw = {
271 	sciopen, sciclose, sciread, sciwrite, sciioctl,
272 	scistop, scitty, scipoll, nommap, ttykqfilter, D_TTY
273 };
274 
275 void InitializeSci (unsigned int);
276 
277 /*
278  * following functions are debugging prupose only
279  */
280 #define	CR      0x0D
281 #define	I2C_ADRS (*(volatile unsigned int *)0xa8000000)
282 #define	USART_ON (unsigned int)~0x08
283 
284 void sci_putc(unsigned char);
285 unsigned char sci_getc(void);
286 int SciErrCheck(void);
287 
288 /*
289  * InitializeSci
290  * : unsigned int bps;
291  * : SCI(Serial Communication Interface)
292  */
293 
294 void
295 InitializeSci(unsigned int bps)
296 {
297 
298 	/* Initialize SCR */
299 	SHREG_SCSCR = 0x00;
300 
301 	/* Serial Mode Register */
302 	SHREG_SCSMR = 0x00;	/* Async,8bit,NonParity,Even,1Stop,NoMulti */
303 
304 	/* Bit Rate Register */
305 	SHREG_SCBRR = divrnd(sh_clock_get_pclock(), 32 * bps) - 1;
306 
307 	/*
308 	 * wait 1mSec, because Send/Recv must begin 1 bit period after
309 	 * BRR is set.
310 	 */
311 	delay(1000);
312 
313 	/* Send permission, Receive permission ON */
314 	SHREG_SCSCR = SCSCR_TE | SCSCR_RE;
315 
316 	/* Serial Status Register */
317 	SHREG_SCSSR &= SCSSR_TDRE;	/* Clear Status */
318 
319 #if 0
320 	I2C_ADRS &= ~0x08;	/* enable RS-232C */
321 #endif
322 }
323 
324 
325 /*
326  * sci_putc
327  *  : unsigned char c;
328  */
329 void
330 sci_putc(unsigned char c)
331 {
332 
333 	/* wait for ready */
334 	while ((SHREG_SCSSR & SCSSR_TDRE) == 0)
335 		;
336 
337 	/* write send data to send register */
338 	SHREG_SCTDR = c;
339 
340 	/* clear ready flag */
341 	SHREG_SCSSR &= ~SCSSR_TDRE;
342 }
343 
344 /*
345  * : SciErrCheck
346  *	0x20 = over run
347  *	0x10 = frame error
348  *	0x80 = parity error
349  */
350 int
351 SciErrCheck(void)
352 {
353 
354 	return(SHREG_SCSSR & (SCSSR_ORER | SCSSR_FER | SCSSR_PER));
355 }
356 
357 /*
358  * sci_getc
359  */
360 unsigned char
361 sci_getc(void)
362 {
363 	unsigned char c, err_c;
364 
365 	while (((err_c = SHREG_SCSSR)
366 		& (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) == 0)
367 		;
368 	if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
369 		SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER);
370 		return(err_c |= 0x80);
371 	}
372 
373 	c = SHREG_SCRDR;
374 
375 	SHREG_SCSSR &= ~SCSSR_RDRF;
376 
377 	return(c);
378 }
379 
380 static int
381 sci_match(struct device *parent, struct cfdata *cfp, void *aux)
382 {
383 
384 	if (strcmp(cfp->cf_name, "sci") || sci_attached)
385 		return 0;
386 
387 	return 1;
388 }
389 
390 static void
391 sci_attach(struct device *parent, struct device *self, void *aux)
392 {
393 	struct sci_softc *sc = (struct sci_softc *)self;
394 	struct tty *tp;
395 
396 	sci_attached = 1;
397 
398 	sc->sc_hwflags = 0;	/* XXX */
399 	sc->sc_swflags = 0;	/* XXX */
400 	sc->sc_fifolen = 0;	/* XXX */
401 
402 	if (sciisconsole) {
403 		SET(sc->sc_hwflags, SCI_HW_CONSOLE);
404 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
405 		printf("\n%s: console\n", sc->sc_dev.dv_xname);
406 	} else {
407 		InitializeSci(9600);
408 		printf("\n");
409 	}
410 
411 	callout_init(&sc->sc_diag_ch);
412 
413 	intc_intr_establish(SH_INTEVT_SCI_ERI, IST_LEVEL, IPL_SERIAL, sciintr,
414 	    sc);
415 	intc_intr_establish(SH_INTEVT_SCI_RXI, IST_LEVEL, IPL_SERIAL, sciintr,
416 	    sc);
417 	intc_intr_establish(SH_INTEVT_SCI_TXI, IST_LEVEL, IPL_SERIAL, sciintr,
418 	    sc);
419 	intc_intr_establish(SH_INTEVT_SCI_TEI, IST_LEVEL, IPL_SERIAL, sciintr,
420 	    sc);
421 
422 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
423 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, scisoft, sc);
424 #endif
425 	SET(sc->sc_hwflags, SCI_HW_DEV_OK);
426 
427 	tp = ttymalloc();
428 	tp->t_oproc = scistart;
429 	tp->t_param = sciparam;
430 	tp->t_hwiflow = NULL;
431 
432 	sc->sc_tty = tp;
433 	sc->sc_rbuf = malloc(sci_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
434 	if (sc->sc_rbuf == NULL) {
435 		printf("%s: unable to allocate ring buffer\n",
436 		    sc->sc_dev.dv_xname);
437 		return;
438 	}
439 	sc->sc_ebuf = sc->sc_rbuf + (sci_rbuf_size << 1);
440 
441 	tty_attach(tp);
442 }
443 
444 /*
445  * Start or restart transmission.
446  */
447 static void
448 scistart(struct tty *tp)
449 {
450 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(tp->t_dev)];
451 	int s;
452 
453 	s = spltty();
454 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
455 		goto out;
456 	if (sc->sc_tx_stopped)
457 		goto out;
458 
459 	if (tp->t_outq.c_cc <= tp->t_lowat) {
460 		if (ISSET(tp->t_state, TS_ASLEEP)) {
461 			CLR(tp->t_state, TS_ASLEEP);
462 			wakeup(&tp->t_outq);
463 		}
464 		selwakeup(&tp->t_wsel);
465 		if (tp->t_outq.c_cc == 0)
466 			goto out;
467 	}
468 
469 	/* Grab the first contiguous region of buffer space. */
470 	{
471 		u_char *tba;
472 		int tbc;
473 
474 		tba = tp->t_outq.c_cf;
475 		tbc = ndqb(&tp->t_outq, 0);
476 
477 		(void)splserial();
478 
479 		sc->sc_tba = tba;
480 		sc->sc_tbc = tbc;
481 	}
482 
483 	SET(tp->t_state, TS_BUSY);
484 	sc->sc_tx_busy = 1;
485 
486 	/* Enable transmit completion interrupts if necessary. */
487 	SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
488 
489 	/* Output the first byte of the contiguous buffer. */
490 	{
491 		if (sc->sc_tbc > 0) {
492 			sci_putc(*(sc->sc_tba));
493 			sc->sc_tba++;
494 			sc->sc_tbc--;
495 		}
496 	}
497 out:
498 	splx(s);
499 	return;
500 }
501 
502 /*
503  * Set SCI tty parameters from termios.
504  * XXX - Should just copy the whole termios after
505  * making sure all the changes could be done.
506  */
507 static int
508 sciparam(struct tty *tp, struct termios *t)
509 {
510 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(tp->t_dev)];
511 	int ospeed = t->c_ospeed;
512 	int s;
513 
514 	if (!device_is_active(&sc->sc_dev))
515 		return (EIO);
516 
517 	/* Check requested parameters. */
518 	if (ospeed < 0)
519 		return (EINVAL);
520 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
521 		return (EINVAL);
522 
523 	/*
524 	 * For the console, always force CLOCAL and !HUPCL, so that the port
525 	 * is always active.
526 	 */
527 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
528 	    ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
529 		SET(t->c_cflag, CLOCAL);
530 		CLR(t->c_cflag, HUPCL);
531 	}
532 
533 	/*
534 	 * If there were no changes, don't do anything.  This avoids dropping
535 	 * input and improves performance when all we did was frob things like
536 	 * VMIN and VTIME.
537 	 */
538 	if (tp->t_ospeed == t->c_ospeed &&
539 	    tp->t_cflag == t->c_cflag)
540 		return (0);
541 
542 #if 0
543 /* XXX (msaitoh) */
544 	lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag);
545 #endif
546 
547 	s = splserial();
548 
549 	/*
550 	 * Set the FIFO threshold based on the receive speed.
551 	 *
552 	 *  * If it's a low speed, it's probably a mouse or some other
553 	 *    interactive device, so set the threshold low.
554 	 *  * If it's a high speed, trim the trigger level down to prevent
555 	 *    overflows.
556 	 *  * Otherwise set it a bit higher.
557 	 */
558 #if 0
559 /* XXX (msaitoh) */
560 	if (ISSET(sc->sc_hwflags, SCI_HW_HAYESP))
561 		sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8;
562 	else if (ISSET(sc->sc_hwflags, SCI_HW_FIFO))
563 		sc->sc_fifo = FIFO_ENABLE |
564 		    (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 :
565 		     t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4);
566 	else
567 		sc->sc_fifo = 0;
568 #endif
569 
570 	/* And copy to tty. */
571 	tp->t_ispeed = 0;
572 	tp->t_ospeed = t->c_ospeed;
573 	tp->t_cflag = t->c_cflag;
574 
575 	if (!sc->sc_heldchange) {
576 		if (sc->sc_tx_busy) {
577 			sc->sc_heldtbc = sc->sc_tbc;
578 			sc->sc_tbc = 0;
579 			sc->sc_heldchange = 1;
580 		}
581 #if 0
582 /* XXX (msaitoh) */
583 		else
584 			sci_loadchannelregs(sc);
585 #endif
586 	}
587 
588 	if (!ISSET(t->c_cflag, CHWFLOW)) {
589 		/* Disable the high water mark. */
590 		sc->sc_r_hiwat = 0;
591 		sc->sc_r_lowat = 0;
592 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
593 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
594 			sci_schedrx(sc);
595 		}
596 	} else {
597 		sc->sc_r_hiwat = sci_rbuf_hiwat;
598 		sc->sc_r_lowat = sci_rbuf_lowat;
599 	}
600 
601 	splx(s);
602 
603 #ifdef SCI_DEBUG
604 	if (sci_debug)
605 		scistatus(sc, "sciparam ");
606 #endif
607 
608 	if (!ISSET(t->c_cflag, CHWFLOW)) {
609 		if (sc->sc_tx_stopped) {
610 			sc->sc_tx_stopped = 0;
611 			scistart(tp);
612 		}
613 	}
614 
615 	return (0);
616 }
617 
618 void
619 sci_iflush(struct sci_softc *sc)
620 {
621 	unsigned char err_c;
622 	volatile unsigned char c;
623 
624 	if (((err_c = SHREG_SCSSR)
625 	     & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
626 
627 		if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) {
628 			SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER);
629 			return;
630 		}
631 
632 		c = SHREG_SCRDR;
633 
634 		SHREG_SCSSR &= ~SCSSR_RDRF;
635 	}
636 }
637 
638 int
639 sciopen(dev_t dev, int flag, int mode, struct lwp *l)
640 {
641 	int unit = SCIUNIT(dev);
642 	struct sci_softc *sc;
643 	struct tty *tp;
644 	int s, s2;
645 	int error;
646 
647 	if (unit >= sci_cd.cd_ndevs)
648 		return (ENXIO);
649 	sc = sci_cd.cd_devs[unit];
650 	if (sc == 0 || !ISSET(sc->sc_hwflags, SCI_HW_DEV_OK) ||
651 	    sc->sc_rbuf == NULL)
652 		return (ENXIO);
653 
654 	if (!device_is_active(&sc->sc_dev))
655 		return (ENXIO);
656 
657 #ifdef KGDB
658 	/*
659 	 * If this is the kgdb port, no other use is permitted.
660 	 */
661 	if (ISSET(sc->sc_hwflags, SCI_HW_KGDB))
662 		return (EBUSY);
663 #endif
664 
665 	tp = sc->sc_tty;
666 
667 	if (ISSET(tp->t_state, TS_ISOPEN) &&
668 	    ISSET(tp->t_state, TS_XCLUDE) &&
669 	    suser(l->l_proc->p_ucred, &l->l_proc->p_acflag) != 0)
670 		return (EBUSY);
671 
672 	s = spltty();
673 
674 	/*
675 	 * Do the following iff this is a first open.
676 	 */
677 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
678 		struct termios t;
679 
680 		tp->t_dev = dev;
681 
682 		s2 = splserial();
683 
684 		/* Turn on interrupts. */
685 		SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE;
686 
687 		splx(s2);
688 
689 		/*
690 		 * Initialize the termios status to the defaults.  Add in the
691 		 * sticky bits from TIOCSFLAGS.
692 		 */
693 		t.c_ispeed = 0;
694 		if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) {
695 			t.c_ospeed = scicn_speed;
696 			t.c_cflag = sciconscflag;
697 		} else {
698 			t.c_ospeed = TTYDEF_SPEED;
699 			t.c_cflag = TTYDEF_CFLAG;
700 		}
701 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
702 			SET(t.c_cflag, CLOCAL);
703 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
704 			SET(t.c_cflag, CRTSCTS);
705 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
706 			SET(t.c_cflag, MDMBUF);
707 		/* Make sure sciparam() will do something. */
708 		tp->t_ospeed = 0;
709 		(void) sciparam(tp, &t);
710 		tp->t_iflag = TTYDEF_IFLAG;
711 		tp->t_oflag = TTYDEF_OFLAG;
712 		tp->t_lflag = TTYDEF_LFLAG;
713 		ttychars(tp);
714 		ttsetwater(tp);
715 
716 		s2 = splserial();
717 
718 		/* Clear the input ring, and unblock. */
719 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
720 		sc->sc_rbavail = sci_rbuf_size;
721 		sci_iflush(sc);
722 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
723 #if 0
724 /* XXX (msaitoh) */
725 		sci_hwiflow(sc);
726 #endif
727 
728 #ifdef SCI_DEBUG
729 		if (sci_debug)
730 			scistatus(sc, "sciopen  ");
731 #endif
732 
733 		splx(s2);
734 	}
735 
736 	splx(s);
737 
738 	error = ttyopen(tp, SCIDIALOUT(dev), ISSET(flag, O_NONBLOCK));
739 	if (error)
740 		goto bad;
741 
742 	error = (*tp->t_linesw->l_open)(dev, tp);
743 	if (error)
744 		goto bad;
745 
746 	return (0);
747 
748 bad:
749 
750 	return (error);
751 }
752 
753 int
754 sciclose(dev_t dev, int flag, int mode, struct lwp *l)
755 {
756 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
757 	struct tty *tp = sc->sc_tty;
758 
759 	/* XXX This is for cons.c. */
760 	if (!ISSET(tp->t_state, TS_ISOPEN))
761 		return (0);
762 
763 	(*tp->t_linesw->l_close)(tp, flag);
764 	ttyclose(tp);
765 
766 	if (!device_is_active(&sc->sc_dev))
767 		return (0);
768 
769 	return (0);
770 }
771 
772 int
773 sciread(dev_t dev, struct uio *uio, int flag)
774 {
775 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
776 	struct tty *tp = sc->sc_tty;
777 
778 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
779 }
780 
781 int
782 sciwrite(dev_t dev, struct uio *uio, int flag)
783 {
784 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
785 	struct tty *tp = sc->sc_tty;
786 
787 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
788 }
789 
790 int
791 scipoll(dev_t dev, int events, struct lwp *l)
792 {
793 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
794 	struct tty *tp = sc->sc_tty;
795 
796 	return ((*tp->t_linesw->l_poll)(tp, events, l));
797 }
798 
799 struct tty *
800 scitty(dev_t dev)
801 {
802 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
803 	struct tty *tp = sc->sc_tty;
804 
805 	return (tp);
806 }
807 
808 int
809 sciioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
810 {
811 	struct sci_softc *sc = sci_cd.cd_devs[SCIUNIT(dev)];
812 	struct tty *tp = sc->sc_tty;
813 	int error;
814 	int s;
815 
816 	if (!device_is_active(&sc->sc_dev))
817 		return (EIO);
818 
819 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
820 	if (error != EPASSTHROUGH)
821 		return (error);
822 
823 	error = ttioctl(tp, cmd, data, flag, l);
824 	if (error != EPASSTHROUGH)
825 		return (error);
826 
827 	error = 0;
828 
829 	s = splserial();
830 
831 	switch (cmd) {
832 	case TIOCSBRK:
833 		sci_break(sc, 1);
834 		break;
835 
836 	case TIOCCBRK:
837 		sci_break(sc, 0);
838 		break;
839 
840 	case TIOCGFLAGS:
841 		*(int *)data = sc->sc_swflags;
842 		break;
843 
844 	case TIOCSFLAGS:
845 		error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
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