xref: /netbsd-src/sys/arch/mips/sibyte/dev/sbscn.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $NetBSD: sbscn.c,v 1.28 2009/03/14 15:36:10 dsl Exp $ */
2 
3 /*
4  * Copyright 2000, 2001
5  * Broadcom Corporation. All rights reserved.
6  *
7  * This software is furnished under license and may be used and copied only
8  * in accordance with the following terms and conditions.  Subject to these
9  * conditions, you may download, copy, install, use, modify and distribute
10  * modified or unmodified copies of this software in source and/or binary
11  * form. No title or ownership is transferred hereby.
12  *
13  * 1) Any source code used, modified or distributed must reproduce and
14  *    retain this copyright notice and list of conditions as they appear in
15  *    the source file.
16  *
17  * 2) No right is granted to use any trade name, trademark, or logo of
18  *    Broadcom Corporation.  The "Broadcom Corporation" name may not be
19  *    used to endorse or promote products derived from this software
20  *    without the prior written permission of Broadcom Corporation.
21  *
22  * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED
23  *    WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF
24  *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
25  *    NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE
26  *    FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE
27  *    LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30  *    BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  *    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32  *    OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */
36 
37 /*-
38  * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
39  * All rights reserved.
40  *
41  * This code is derived from software contributed to The NetBSD Foundation
42  * by Charles M. Hannum.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the above copyright
48  *    notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  *
53  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
54  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
57  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
58  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
59  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
60  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
61  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
62  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
63  * POSSIBILITY OF SUCH DAMAGE.
64  */
65 
66 /*
67  * Copyright (c) 1991 The Regents of the University of California.
68  * All rights reserved.
69  *
70  * Redistribution and use in source and binary forms, with or without
71  * modification, are permitted provided that the following conditions
72  * are met:
73  * 1. Redistributions of source code must retain the above copyright
74  *    notice, this list of conditions and the following disclaimer.
75  * 2. Redistributions in binary form must reproduce the above copyright
76  *    notice, this list of conditions and the following disclaimer in the
77  *    documentation and/or other materials provided with the distribution.
78  * 3. Neither the name of the University nor the names of its contributors
79  *    may be used to endorse or promote products derived from this software
80  *    without specific prior written permission.
81  *
82  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
83  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
84  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
85  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
86  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
87  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
88  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
89  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
90  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
91  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
92  * SUCH DAMAGE.
93  *
94  *	@(#)com.c	7.5 (Berkeley) 5/16/91
95  */
96 
97 /*
98  * `sbscn' driver, supports SiByte SB-1250 SOC DUART.
99  *
100  * This DUART is quite similar in programming model to the scn2681/68681
101  * DUARTs supported by the NetBSD/amiga `mfc' and NetBSD/pc532 `scn'
102  * driver, but substantial rework would have been necessary to make
103  * those drivers sane w.r.t. bus_space (which would then have been
104  * required on NetBSD/sbmips very early on), and to accommodate the
105  * different register mappings.
106  *
107  * So, another driver.  Eventually there should be One True Driver,
108  * but we're not here to save the world.
109  */
110 
111 #include <sys/cdefs.h>
112 __KERNEL_RCSID(0, "$NetBSD: sbscn.c,v 1.28 2009/03/14 15:36:10 dsl Exp $");
113 
114 #define	SBSCN_DEBUG
115 
116 #include "opt_ddb.h"
117 
118 #include "rnd.h"
119 #if NRND > 0 && defined(RND_SBSCN)
120 #include <sys/rnd.h>
121 #endif
122 
123 #include <sys/param.h>
124 #include <sys/systm.h>
125 #include <sys/ioctl.h>
126 #include <sys/select.h>
127 #include <sys/tty.h>
128 #include <sys/proc.h>
129 #include <sys/user.h>
130 #include <sys/conf.h>
131 #include <sys/file.h>
132 #include <sys/uio.h>
133 #include <sys/kernel.h>
134 #include <sys/syslog.h>
135 #include <sys/types.h>
136 #include <sys/device.h>
137 #include <sys/malloc.h>
138 #include <sys/vnode.h>
139 #include <sys/kauth.h>
140 #include <sys/intr.h>
141 
142 #include <mips/sibyte/dev/sbobiovar.h>
143 #if 0
144 #include <mips/sibyte/dev/sbscnreg.h>
145 #endif
146 #include <mips/sibyte/dev/sbscnvar.h>
147 #include <dev/cons.h>
148 #include <machine/locore.h>
149 
150 void	sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr);
151 #if defined(DDB) || defined(KGDB)
152 static void sbscn_enable_debugport(struct sbscn_channel *ch);
153 #endif
154 void	sbscn_config(struct sbscn_channel *ch);
155 void	sbscn_shutdown(struct sbscn_channel *ch);
156 int	sbscn_speed(long, long *);
157 static int cflag2modes(tcflag_t, u_char *, u_char *);
158 int	sbscn_param(struct tty *, struct termios *);
159 void	sbscn_start(struct tty *);
160 int	sbscn_hwiflow(struct tty *, int);
161 
162 void	sbscn_loadchannelregs(struct sbscn_channel *);
163 void	sbscn_dohwiflow(struct sbscn_channel *);
164 void	sbscn_break(struct sbscn_channel *, int);
165 void	sbscn_modem(struct sbscn_channel *, int);
166 void	tiocm_to_sbscn(struct sbscn_channel *, int, int);
167 int	sbscn_to_tiocm(struct sbscn_channel *);
168 void	sbscn_iflush(struct sbscn_channel *);
169 
170 int	sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag);
171 int	sbscn_common_getc(u_long addr, int chan);
172 void	sbscn_common_putc(u_long addr, int chan, int c);
173 void	sbscn_intr(void *arg, uint32_t status, uint32_t pc);
174 
175 int	sbscn_cngetc(dev_t dev);
176 void	sbscn_cnputc(dev_t dev, int c);
177 void	sbscn_cnpollc(dev_t dev, int on);
178 
179 extern struct cfdriver sbscn_cd;
180 
181 dev_type_open(sbscnopen);
182 dev_type_close(sbscnclose);
183 dev_type_read(sbscnread);
184 dev_type_write(sbscnwrite);
185 dev_type_ioctl(sbscnioctl);
186 dev_type_stop(sbscnstop);
187 dev_type_tty(sbscntty);
188 dev_type_poll(sbscnpoll);
189 
190 const struct cdevsw sbscn_cdevsw = {
191 	sbscnopen, sbscnclose, sbscnread, sbscnwrite, sbscnioctl,
192 	sbscnstop, sbscntty, sbscnpoll, nommap, ttykqfilter, D_TTY
193 };
194 
195 #define	integrate	static inline
196 void 	sbscn_soft(void *);
197 integrate void sbscn_rxsoft(struct sbscn_channel *, struct tty *);
198 integrate void sbscn_txsoft(struct sbscn_channel *, struct tty *);
199 integrate void sbscn_stsoft(struct sbscn_channel *, struct tty *);
200 integrate void sbscn_schedrx(struct sbscn_channel *);
201 void	sbscn_diag(void *);
202 
203 /*
204  * Make this an option variable one can patch.
205  * But be warned:  this must be a power of 2!
206  */
207 u_int sbscn_rbuf_size = SBSCN_RING_SIZE;
208 
209 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */
210 u_int sbscn_rbuf_hiwat = (SBSCN_RING_SIZE * 1) / 4;
211 u_int sbscn_rbuf_lowat = (SBSCN_RING_SIZE * 3) / 4;
212 
213 static int	sbscn_cons_present;
214 static int	sbscn_cons_attached;
215 static u_long	sbscn_cons_addr;
216 static int	sbscn_cons_chan;
217 static int	sbscn_cons_rate;
218 static tcflag_t	sbscn_cons_cflag;
219 
220 #ifdef KGDB
221 #include <sys/kgdb.h>
222 
223 static int	sbscn_kgdb_present;
224 static int	sbscn_kgdb_attached;
225 static u_long	sbscn_kgdb_addr;
226 static int	sbscn_kgdb_chan;
227 
228 int	sbscn_kgdb_getc(void *);
229 void	sbscn_kgdb_putc(void *, int);
230 #endif /* KGDB */
231 
232 static int	sbscn_match(struct device *, struct cfdata *, void *);
233 static void	sbscn_attach(struct device *, struct device *, void *);
234 
235 CFATTACH_DECL(sbscn, sizeof(struct sbscn_softc),
236     sbscn_match, sbscn_attach, NULL, NULL);
237 
238 #define	READ_REG(rp)		(mips3_ld((int64_t *)__UNVOLATILE(rp)))
239 #define	WRITE_REG(rp, val)	(mips3_sd((uint64_t *)__UNVOLATILE(rp), (val)))
240 
241 /*
242  * input and output signals are actually the _inverse_ of the bits in the
243  * input and output port registers!
244  */
245 #define	GET_INPUT_SIGNALS(ch) \
246     ((~READ_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x280))) & (ch)->ch_i_mask)
247 #define	SET_OUTPUT_SIGNALS(ch, val) 					\
248     do {								\
249 	int sigs_to_set, sigs_to_clr;					\
250 									\
251 	sigs_to_set = (ch)->ch_o_mask & val;				\
252 	sigs_to_clr = (ch)->ch_o_mask & ~val;				\
253 									\
254 	/* set signals by clearing op bits, and vice versa */		\
255 	WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2c0),	\
256 	    sigs_to_set);						\
257 	WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2b0),	\
258 	    sigs_to_clr);						\
259     } while (0)
260 
261 static int
262 sbscn_match(struct device *parent, struct cfdata *match, void *aux)
263 {
264 	struct sbobio_attach_args *sap = aux;
265 
266 	if (sap->sa_locs.sa_type != SBOBIO_DEVTYPE_DUART)
267 		return (0);
268 
269 	return 1;
270 }
271 
272 static void
273 sbscn_attach(struct device *parent, struct device *self, void *aux)
274 {
275 	struct sbscn_softc *sc = (struct sbscn_softc *)self;
276 	struct sbobio_attach_args *sap = aux;
277 	int i;
278 
279 	sc->sc_addr = sap->sa_base + sap->sa_locs.sa_offset;
280 
281 	printf("\n");
282 	for (i = 0; i < 2; i++)
283 		sbscn_attach_channel(sc, i, sap->sa_locs.sa_intr[i]);
284 
285 	/* init duart_opcr */
286 	WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x270), 0);
287 	/* init duart_aux_ctrl */
288 	WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x210), 0x0f); /* XXX */
289 }
290 
291 void
292 sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr)
293 {
294 	struct sbscn_channel *ch = &sc->sc_channels[chan];
295 	u_long chan_addr;
296 	struct tty *tp;
297 
298 	ch->ch_sc = sc;
299 	ch->ch_num = chan;
300 
301 	chan_addr = sc->sc_addr + (0x100 * chan);
302 	ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr);
303 	ch->ch_isr_base =
304 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x220 + (0x20 * chan));
305 	ch->ch_imr_base =
306 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x230 + (0x20 * chan));
307 #ifdef XXXCGDnotyet
308 	ch->ch_inchg_base =
309 	    (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x2d0 + (0x10 * chan));
310 #endif
311 
312 	ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */;
313 	ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */;
314 	ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */;
315 	ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */;
316 	ch->ch_i_mask =
317 	    ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri;
318 	ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */;
319 	ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */;
320 	ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts;
321 
322 	ch->ch_intrhand = cpu_intr_establish(intr, IPL_SERIAL, sbscn_intr, ch);
323 	callout_init(&ch->ch_diag_callout, 0);
324 
325 	/* Disable interrupts before configuring the device. */
326 	ch->ch_imr = 0;
327 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
328 
329 	if (sbscn_cons_present &&
330 	    sbscn_cons_addr == chan_addr && sbscn_cons_chan == chan) {
331 		sbscn_cons_attached = 1;
332 
333 		/* Make sure the console is always "hardwired". */
334 		delay(1000);			/* wait for output to finish */
335 		SET(ch->ch_hwflags, SBSCN_HW_CONSOLE);
336 		SET(ch->ch_swflags, TIOCFLAG_SOFTCAR);
337 	}
338 
339 	tp = ttymalloc();
340 	tp->t_oproc = sbscn_start;
341 	tp->t_param = sbscn_param;
342 	tp->t_hwiflow = sbscn_hwiflow;
343 
344 	ch->ch_tty = tp;
345 	ch->ch_rbuf = malloc(sbscn_rbuf_size << 1, M_DEVBUF, M_NOWAIT);
346 	if (ch->ch_rbuf == NULL) {
347 		printf("%s: channel %d: unable to allocate ring buffer\n",
348 		    sc->sc_dev.dv_xname, chan);
349 		return;
350 	}
351 	ch->ch_ebuf = ch->ch_rbuf + (sbscn_rbuf_size << 1);
352 
353 	tty_attach(tp);
354 
355 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
356 		int maj;
357 
358 		/* locate the major number */
359 		maj = cdevsw_lookup_major(&sbscn_cdevsw);
360 
361 		cn_tab->cn_dev = makedev(maj,
362 		    (device_unit(&sc->sc_dev) << 1) + chan);
363 
364 		printf("%s: channel %d: console\n", sc->sc_dev.dv_xname, chan);
365 	}
366 
367 #ifdef KGDB
368 	/*
369 	 * Allow kgdb to "take over" this port.  If this is
370 	 * the kgdb device, it has exclusive use.
371 	 */
372 	if (sbscn_kgdb_present &&
373 	    sbscn_kgdb_addr == chan_addr && sbscn_kgdb_chan == chan) {
374 		sbscn_kgdb_attached = 1;
375 
376 		SET(sc->sc_hwflags, SBSCN_HW_KGDB);
377 		printf("%s: channel %d: kgdb\n", sc->sc_dev.dv_xname, chan);
378 	}
379 #endif
380 
381 	ch->ch_si = softint_establish(SOFTINT_SERIAL, sbscn_soft, ch);
382 
383 #if NRND > 0 && defined(RND_SBSCN)
384 	rnd_attach_source(&ch->ch_rnd_source, sc->sc_dev.dv_xname,
385 			  RND_TYPE_TTY, 0);
386 #endif
387 
388 	sbscn_config(ch);
389 
390 	SET(ch->ch_hwflags, SBSCN_HW_DEV_OK);
391 }
392 
393 int
394 sbscn_speed(long speed, long *brcp)
395 {
396 #define	divrnd(n, q)	(((n)*2/(q)+1)/2)	/* divide and round off */
397 
398 	int x, err;
399 	int frequency = 100000000;
400 
401 	*brcp = divrnd(frequency / 20, speed) - 1;
402 
403 	if (speed <= 0)
404 		return (-1);
405 	x = divrnd(frequency / 20, speed);
406 	if (x <= 0)
407 		return (-1);
408 	err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000;
409 	if (err < 0)
410 		err = -err;
411 	if (err > SBSCN_TOLERANCE)
412 		return (-1);
413 	*brcp = x - 1;
414 	return (0);
415 
416 #undef	divrnd
417 }
418 
419 #ifdef SBSCN_DEBUG
420 void	sbscn_status(struct sbscn_channel *, const char *);
421 
422 int	sbscn_debug = 0 /* XXXCGD */;
423 
424 void
425 sbscn_status(struct sbscn_channel *ch, const char *str)
426 {
427 	struct sbscn_softc *sc = ch->ch_sc;
428 	struct tty *tp = ch->ch_tty;
429 
430 	printf("%s: chan %d: %s %sclocal  %sdcd %sts_carr_on %sdtr %stx_stopped\n",
431 	    sc->sc_dev.dv_xname, ch->ch_num, str,
432 	    ISSET(tp->t_cflag, CLOCAL) ? "+" : "-",
433 	    ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-",
434 	    ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-",
435 	    ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-",
436 	    ch->ch_tx_stopped ? "+" : "-");
437 
438 	printf("%s: chan %d: %s %scrtscts %scts %sts_ttstop  %srts %xrx_flags\n",
439 	    sc->sc_dev.dv_xname, ch->ch_num, str,
440 	    ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-",
441 	    ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-",
442 	    ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-",
443 	    ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-",
444 	    ch->ch_rx_flags);
445 }
446 #endif
447 
448 #if defined(DDB) || defined(KGDB)
449 static void
450 sbscn_enable_debugport(struct sbscn_channel *ch)
451 {
452 	int s;
453 
454 	/* Turn on line break interrupt, set carrier. */
455 	s = splserial();
456 
457 #if 0	/* DO NOT turn on break interrupt at this time. */
458 	ch->ch_imr = 0x04;
459 #else
460 	ch->ch_imr = 0x00;
461 #endif
462 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
463 	SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts);
464 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports);
465 
466 	splx(s);
467 }
468 #endif
469 
470 void
471 sbscn_config(struct sbscn_channel *ch)
472 {
473 
474 	/* Disable interrupts before configuring the device. */
475 	ch->ch_imr = 0x00;
476 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
477 
478 #ifdef DDB
479 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
480 		sbscn_enable_debugport(ch);
481 #endif
482 
483 #ifdef KGDB
484 	/*
485 	 * Allow kgdb to "take over" this port.  If this is
486 	 * the kgdb device, it has exclusive use.
487 	 */
488 	if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
489 		sbscn_enable_debugport(ch);
490 #endif
491 }
492 
493 void
494 sbscn_shutdown(struct sbscn_channel *ch)
495 {
496 	struct tty *tp = ch->ch_tty;
497 	int s;
498 
499 	s = splserial();
500 
501 	/* If we were asserting flow control, then deassert it. */
502 	SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
503 	sbscn_dohwiflow(ch);
504 
505 	/* Clear any break condition set with TIOCSBRK. */
506 	sbscn_break(ch, 0);
507 
508 	/*
509 	 * Hang up if necessary.  Wait a bit, so the other side has time to
510 	 * notice even if we immediately open the port again.
511 	 * Avoid tsleeping above splhigh().
512 	 */
513 	if (ISSET(tp->t_cflag, HUPCL)) {
514 		sbscn_modem(ch, 0);
515 		splx(s);
516 		/* XXX tsleep will only timeout */
517 		(void) tsleep(ch, TTIPRI, ttclos, hz);
518 		s = splserial();
519 	}
520 
521 	/* Turn off interrupts. */
522 #ifdef DDB
523 	if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE))
524 #if 0	/* DO NOT turn on break interrupt at this time. */
525 		ch->ch_imr = 0x04; /* interrupt on break */
526 #else
527 		ch->ch_imr = 0x00;
528 #endif
529 	else
530 #endif
531 		ch->ch_imr = 0;
532 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
533 
534 	splx(s);
535 }
536 
537 int
538 sbscnopen(dev_t dev, int flag, int mode, struct lwp *l)
539 {
540 	int chan = SBSCN_CHAN(dev);
541 	struct sbscn_softc *sc;
542 	struct sbscn_channel *ch;
543 	struct tty *tp;
544 	int s, s2;
545 	int error;
546 
547 	sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
548 	if (sc == NULL)
549 		return (ENXIO);
550 	ch = &sc->sc_channels[chan];
551 	if (!ISSET(ch->ch_hwflags, SBSCN_HW_DEV_OK) || ch->ch_rbuf == NULL)
552 		return (ENXIO);
553 
554 #ifdef KGDB
555 	/*
556 	 * If this is the kgdb port, no other use is permitted.
557 	 */
558 	if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB))
559 		return (EBUSY);
560 #endif
561 
562 	tp = ch->ch_tty;
563 
564 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
565 		return (EBUSY);
566 
567 	s = spltty();
568 
569 	/*
570 	 * Do the following iff this is a first open.
571 	 */
572 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
573 		struct termios t;
574 
575 		tp->t_dev = dev;
576 
577 		s2 = splserial();
578 
579 		/* Turn on receive, break, and status change interrupts. */
580 #if 0	/* DO NOT turn on break or status change interrupt at this time. */
581 		ch->ch_imr = 0xe;
582 #else
583 		ch->ch_imr = 0x2;
584 #endif
585 		WRITE_REG(ch->ch_imr_base, ch->ch_imr);
586 
587 		/* Fetch the current modem control status, needed later. */
588 		ch->ch_iports = GET_INPUT_SIGNALS(ch);
589 		ch->ch_iports_delta = 0;
590 		splx(s2);
591 
592 		/*
593 		 * Initialize the termios status to the defaults.  Add in the
594 		 * sticky bits from TIOCSFLAGS.
595 		 */
596 		t.c_ispeed = 0;
597 		if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
598 			t.c_ospeed = sbscn_cons_rate;
599 			t.c_cflag = sbscn_cons_cflag;
600 		} else {
601 			t.c_ospeed = TTYDEF_SPEED;
602 			t.c_cflag = TTYDEF_CFLAG;
603 		}
604 		if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL))
605 			SET(t.c_cflag, CLOCAL);
606 		if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS))
607 			SET(t.c_cflag, CRTSCTS);
608 		if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF))
609 			SET(t.c_cflag, MDMBUF);
610 		/* Make sure sbscn_param() will do something. */
611 		tp->t_ospeed = 0;
612 		(void) sbscn_param(tp, &t);
613 		tp->t_iflag = TTYDEF_IFLAG;
614 		tp->t_oflag = TTYDEF_OFLAG;
615 		tp->t_lflag = TTYDEF_LFLAG;
616 		ttychars(tp);
617 		ttsetwater(tp);
618 
619 		s2 = splserial();
620 
621 		/*
622 		 * Turn on DTR.  We must always do this, even if carrier is not
623 		 * present, because otherwise we'd have to use TIOCSDTR
624 		 * immediately after setting CLOCAL, which applications do not
625 		 * expect.  We always assert DTR while the device is open
626 		 * unless explicitly requested to deassert it.
627 		 */
628 		sbscn_modem(ch, 1);
629 
630 		/* Clear the input ring, and unblock. */
631 		ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf;
632 		ch->ch_rbavail = sbscn_rbuf_size;
633 		sbscn_iflush(ch);
634 		CLR(ch->ch_rx_flags, RX_ANY_BLOCK);
635 		sbscn_dohwiflow(ch);
636 
637 #ifdef SBSCN_DEBUG
638 		if (sbscn_debug)
639 			sbscn_status(ch, "sbscnopen  ");
640 #endif
641 
642 		splx(s2);
643 	}
644 
645 	splx(s);
646 
647 	error = ttyopen(tp, SBSCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK));
648 	if (error)
649 		goto bad;
650 
651 	error = (*tp->t_linesw->l_open)(dev, tp);
652 	if (error)
653 		goto bad;
654 
655 	return (0);
656 
657 bad:
658 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
659 		/*
660 		 * We failed to open the device, and nobody else had it opened.
661 		 * Clean up the state as appropriate.
662 		 */
663 		sbscn_shutdown(ch);
664 	}
665 
666 	return (error);
667 }
668 
669 int
670 sbscnclose(dev_t dev, int flag, int mode, struct lwp *l)
671 {
672 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
673 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
674 	struct tty *tp = ch->ch_tty;
675 
676 	/* XXX This is for cons.c. */
677 	if (!ISSET(tp->t_state, TS_ISOPEN))
678 		return (0);
679 
680 	(*tp->t_linesw->l_close)(tp, flag);
681 	ttyclose(tp);
682 
683 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
684 		/*
685 		 * Although we got a last close, the device may still be in
686 		 * use; e.g. if this was the dialout node, and there are still
687 		 * processes waiting for carrier on the non-dialout node.
688 		 */
689 		sbscn_shutdown(ch);
690 	}
691 
692 	return (0);
693 }
694 
695 int
696 sbscnread(dev_t dev, struct uio *uio, int flag)
697 {
698 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
699 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
700 	struct tty *tp = ch->ch_tty;
701 
702 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
703 }
704 
705 int
706 sbscnwrite(dev_t dev, struct uio *uio, int flag)
707 {
708 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
709 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
710 	struct tty *tp = ch->ch_tty;
711 
712 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
713 }
714 
715 int
716 sbscnpoll(dev_t dev, int events, struct lwp *l)
717 {
718 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
719 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
720 	struct tty *tp = ch->ch_tty;
721 
722 	return ((*tp->t_linesw->l_poll)(tp, events, l));
723 }
724 
725 struct tty *
726 sbscntty(dev_t dev)
727 {
728 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
729 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
730 	struct tty *tp = ch->ch_tty;
731 
732 	return (tp);
733 }
734 
735 int
736 sbscnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
737 {
738 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev));
739 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)];
740 	struct tty *tp = ch->ch_tty;
741 	int error;
742 	int s;
743 
744 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
745 	if (error != EPASSTHROUGH)
746 		return (error);
747 
748 	error = ttioctl(tp, cmd, data, flag, l);
749 	if (error != EPASSTHROUGH)
750 		return (error);
751 
752 	error = 0;
753 
754 	s = splserial();
755 
756 	switch (cmd) {
757 	case TIOCSBRK:
758 		sbscn_break(ch, 1);
759 		break;
760 
761 	case TIOCCBRK:
762 		sbscn_break(ch, 0);
763 		break;
764 
765 	case TIOCSDTR:
766 		sbscn_modem(ch, 1);
767 		break;
768 
769 	case TIOCCDTR:
770 		sbscn_modem(ch, 0);
771 		break;
772 
773 	case TIOCGFLAGS:
774 		*(int *)data = ch->ch_swflags;
775 		break;
776 
777 	case TIOCSFLAGS:
778 		error = kauth_authorize_device_tty(l->l_cred,
779 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
780 		if (error)
781 			break;
782 		ch->ch_swflags = *(int *)data;
783 		break;
784 
785 	case TIOCMSET:
786 	case TIOCMBIS:
787 	case TIOCMBIC:
788 		tiocm_to_sbscn(ch, cmd, *(int *)data);
789 		break;
790 
791 	case TIOCMGET:
792 		*(int *)data = sbscn_to_tiocm(ch);
793 		break;
794 
795 	default:
796 		error = EPASSTHROUGH;
797 		break;
798 	}
799 
800 	splx(s);
801 
802 #ifdef SBSCN_DEBUG
803 	if (sbscn_debug)
804 		sbscn_status(ch, "sbscn_ioctl ");
805 #endif
806 
807 	return (error);
808 }
809 
810 integrate void
811 sbscn_schedrx(struct sbscn_channel *ch)
812 {
813 
814 	ch->ch_rx_ready = 1;
815 
816 	/* Wake up the poller. */
817 	softint_schedule(ch->ch_si);
818 }
819 
820 void
821 sbscn_break(struct sbscn_channel *ch, int onoff)
822 {
823 
824 /* XXXCGD delay break until not busy */
825 	if (onoff)
826 		WRITE_REG(ch->ch_base + 0x50, 0x60);
827 	else
828 		WRITE_REG(ch->ch_base + 0x50, 0x70);
829 
830 #if 0
831 	if (!ch->ch_heldchange) {
832 		if (ch->ch_tx_busy) {
833 			ch->ch_heldtbc = ch->ch_tbc;
834 			ch->ch_tbc = 0;
835 			ch->ch_heldchange = 1;
836 		} else
837 			sbscn_loadchannelregs(ch);
838 	}
839 #endif
840 }
841 
842 void
843 sbscn_modem(struct sbscn_channel *ch, int onoff)
844 {
845 
846 	if (ch->ch_o_dtr == 0)
847 		return;
848 
849 	if (onoff)
850 		SET(ch->ch_oports, ch->ch_o_dtr);
851 	else
852 		CLR(ch->ch_oports, ch->ch_o_dtr);
853 
854 	if (!ch->ch_heldchange) {
855 		if (ch->ch_tx_busy) {
856 			ch->ch_heldtbc = ch->ch_tbc;
857 			ch->ch_tbc = 0;
858 			ch->ch_heldchange = 1;
859 		} else
860 			sbscn_loadchannelregs(ch);
861 	}
862 }
863 
864 void
865 tiocm_to_sbscn(struct sbscn_channel *ch, int how, int ttybits)
866 {
867 	u_char bits;
868 
869 	bits = 0;
870 	if (ISSET(ttybits, TIOCM_DTR))
871 		SET(bits, ch->ch_o_dtr);
872 	if (ISSET(ttybits, TIOCM_RTS))
873 		SET(bits, ch->ch_o_rts);
874 
875 	switch (how) {
876 	case TIOCMBIC:
877 		CLR(ch->ch_oports, bits);
878 		break;
879 
880 	case TIOCMBIS:
881 		SET(ch->ch_oports, bits);
882 		break;
883 
884 	case TIOCMSET:
885 		ch->ch_oports = bits;
886 		break;
887 	}
888 
889 	if (!ch->ch_heldchange) {
890 		if (ch->ch_tx_busy) {
891 			ch->ch_heldtbc = ch->ch_tbc;
892 			ch->ch_tbc = 0;
893 			ch->ch_heldchange = 1;
894 		} else
895 			sbscn_loadchannelregs(ch);
896 	}
897 }
898 
899 int
900 sbscn_to_tiocm(struct sbscn_channel *ch)
901 {
902 	u_char hwbits;
903 	int ttybits = 0;
904 
905 	hwbits = ch->ch_oports;
906 	if (ISSET(hwbits, ch->ch_o_dtr))
907 		SET(ttybits, TIOCM_DTR);
908 	if (ISSET(hwbits, ch->ch_o_rts))
909 		SET(ttybits, TIOCM_RTS);
910 
911 	hwbits = ch->ch_iports;
912 	if (ISSET(hwbits, ch->ch_i_dcd))
913 		SET(ttybits, TIOCM_CD);
914 	if (ISSET(hwbits, ch->ch_i_cts))
915 		SET(ttybits, TIOCM_CTS);
916 	if (ISSET(hwbits, ch->ch_i_dsr))
917 		SET(ttybits, TIOCM_DSR);
918 	if (ISSET(hwbits, ch->ch_i_ri))
919 		SET(ttybits, TIOCM_RI);
920 
921 	if (ch->ch_imr != 0)
922 		SET(ttybits, TIOCM_LE);
923 
924 	return (ttybits);
925 }
926 
927 static int
928 cflag2modes(tcflag_t cflag, u_char *mode1p, u_char *mode2p)
929 {
930 	u_char mode1;
931 	u_char mode2;
932 	int err = 0;
933 
934 	mode1 = mode2 = 0;
935 
936 	switch (ISSET(cflag, CSIZE)) {
937 	case CS7:
938 		mode1 |= 2;			/* XXX */
939 		break;
940 	default:
941 		err = -1;
942 		/* FALLTHRU for sanity */
943 	case CS8:
944 		mode1 |= 3;			/* XXX */
945 		break;
946 	}
947 	if (!ISSET(cflag, PARENB))
948 		mode1 |= 2 << 3;
949 	else {
950 		mode1 |= 0 << 3;
951 		if (ISSET(cflag, PARODD))
952 			mode1 |= 1 << 2;
953 	}
954 
955 	if (ISSET(cflag, CSTOPB))
956 		mode2 |= 1 << 3;		/* two stop bits XXX not std */
957 
958 	if (ISSET(cflag, CRTSCTS)) {
959 		mode1 |= 1 << 7;
960 		mode2 |= 1 << 4;
961 	}
962 
963 	*mode1p = mode1;
964 	*mode2p = mode2;
965 	return (err);
966 }
967 
968 int
969 sbscn_param(struct tty *tp, struct termios *t)
970 {
971 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev));
972 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
973 	long brc;
974 	u_char mode1, mode2;
975 	int s;
976 
977 /* XXX reset to console parameters if console? */
978 #if 0
979 XXX disable, enable.
980 #endif
981 
982 	/* Check requested parameters. */
983 	if (sbscn_speed(t->c_ospeed, &brc) < 0)
984 		return (EINVAL);
985 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
986 		return (EINVAL);
987 
988 	/*
989 	 * For the console, always force CLOCAL and !HUPCL, so that the port
990 	 * is always active.
991 	 */
992 	if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) ||
993 	    ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
994 		SET(t->c_cflag, CLOCAL);
995 		CLR(t->c_cflag, HUPCL);
996 	}
997 
998 	/*
999 	 * If there were no changes, don't do anything.  This avoids dropping
1000 	 * input and improves performance when all we did was frob things like
1001 	 * VMIN and VTIME.
1002 	 */
1003 	if (tp->t_ospeed == t->c_ospeed &&
1004 	    tp->t_cflag == t->c_cflag)
1005 		return (0);
1006 
1007 	if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0)
1008 		return (EINVAL);
1009 
1010 	s = splserial();
1011 
1012 	ch->ch_mode1 = mode1;
1013 	ch->ch_mode2 = mode2;
1014 
1015 	/*
1016 	 * If we're not in a mode that assumes a connection is present, then
1017 	 * ignore carrier changes.
1018 	 */
1019 	if (ISSET(t->c_cflag, CLOCAL | MDMBUF))
1020 		ch->ch_i_dcd = 0;
1021 	else
1022 		ch->ch_i_dcd = ch->ch_i_dcd_pin;
1023 	/*
1024 	 * Set the flow control pins depending on the current flow control
1025 	 * mode.
1026 	 */
1027 	if (ISSET(t->c_cflag, CRTSCTS)) {
1028 		ch->ch_o_dtr = ch->ch_o_dtr_pin;
1029 		ch->ch_o_rts = ch->ch_o_rts_pin;
1030 		ch->ch_i_cts = ch->ch_i_cts_pin;
1031 		/* hw controle enable bits in mod regs set by cflag2modes */
1032 	} else if (ISSET(t->c_cflag, MDMBUF)) {
1033 		/*
1034 		 * For DTR/DCD flow control, make sure we don't toggle DTR for
1035 		 * carrier detection.
1036 		 */
1037 		ch->ch_o_dtr = 0;
1038 		ch->ch_o_rts = ch->ch_o_dtr_pin;
1039 		ch->ch_i_cts = ch->ch_i_dcd_pin;
1040 	} else {
1041 		/*
1042 		 * If no flow control, then always set RTS.  This will make
1043 		 * the other side happy if it mistakenly thinks we're doing
1044 		 * RTS/CTS flow control.
1045 		 */
1046 		ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin;
1047 		ch->ch_o_rts = 0;
1048 		ch->ch_i_cts = 0;
1049 		if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin))
1050 			SET(ch->ch_oports, ch->ch_o_rts_pin);
1051 		else
1052 			CLR(ch->ch_oports, ch->ch_o_rts_pin);
1053 	}
1054 	/* XXX maybe mask the ports which generate intrs? */
1055 
1056 	ch->ch_brc = brc;
1057 
1058 	/* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */
1059 
1060 	/* And copy to tty. */
1061 	tp->t_ispeed = 0;
1062 	tp->t_ospeed = t->c_ospeed;
1063 	tp->t_cflag = t->c_cflag;
1064 
1065 	if (!ch->ch_heldchange) {
1066 		if (ch->ch_tx_busy) {
1067 			ch->ch_heldtbc = ch->ch_tbc;
1068 			ch->ch_tbc = 0;
1069 			ch->ch_heldchange = 1;
1070 		} else
1071 			sbscn_loadchannelregs(ch);
1072 	}
1073 
1074 	if (!ISSET(t->c_cflag, CHWFLOW)) {
1075 		/* Disable the high water mark. */
1076 		ch->ch_r_hiwat = 0;
1077 		ch->ch_r_lowat = 0;
1078 		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1079 			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1080 			sbscn_schedrx(ch);
1081 		}
1082 		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
1083 			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
1084 			sbscn_dohwiflow(ch);
1085 		}
1086 	} else {
1087 		ch->ch_r_hiwat = sbscn_rbuf_hiwat;
1088 		ch->ch_r_lowat = sbscn_rbuf_lowat;
1089 	}
1090 
1091 	splx(s);
1092 
1093 	/*
1094 	 * Update the tty layer's idea of the carrier bit, in case we changed
1095 	 * CLOCAL or MDMBUF.  We don't hang up here; we only do that by
1096 	 * explicit request.
1097 	 */
1098 	(void) (*tp->t_linesw->l_modem)(tp,
1099 	    ISSET(ch->ch_iports, ch->ch_i_dcd));
1100 
1101 #ifdef SBSCN_DEBUG
1102 	if (sbscn_debug)
1103 		sbscn_status(ch, "sbscnparam ");
1104 #endif
1105 
1106 	if (!ISSET(t->c_cflag, CHWFLOW)) {
1107 		if (ch->ch_tx_stopped) {
1108 			ch->ch_tx_stopped = 0;
1109 			sbscn_start(tp);
1110 		}
1111 	}
1112 
1113 	return (0);
1114 }
1115 
1116 void
1117 sbscn_iflush(struct sbscn_channel *ch)
1118 {
1119 #ifdef DIAGNOSTIC
1120 	int reg;
1121 #endif
1122 	int timo;
1123 
1124 #ifdef DIAGNOSTIC
1125 	reg = 0xffff;
1126 #endif
1127 	timo = 50000;
1128 	/* flush any pending I/O */
1129 	while (ISSET(READ_REG(ch->ch_base + 0x20), 0x01)
1130 	    && --timo)
1131 #ifdef DIAGNOSTIC
1132 		reg =
1133 #else
1134 		    (void)
1135 #endif
1136 		    READ_REG(ch->ch_base + 0x60);
1137 #ifdef DIAGNOSTIC
1138 	if (!timo)
1139 		printf("%s: sbscn_iflush timeout %02x\n",
1140 		    ch->ch_sc->sc_dev.dv_xname, reg & 0xff);
1141 #endif
1142 }
1143 
1144 void
1145 sbscn_loadchannelregs(struct sbscn_channel *ch)
1146 {
1147 
1148 	/* XXXXX necessary? */
1149 	sbscn_iflush(ch);
1150 
1151 	WRITE_REG(ch->ch_imr_base, 0);
1152 
1153 // XXX disable?
1154 	WRITE_REG(ch->ch_base + 0x00, ch->ch_mode1);
1155 	WRITE_REG(ch->ch_base + 0x10, ch->ch_mode2);
1156 	WRITE_REG(ch->ch_base + 0x30, ch->ch_brc);
1157 
1158 	ch->ch_oports_active = ch->ch_oports;
1159 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
1160 
1161 	WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1162 }
1163 
1164 int
1165 sbscn_hwiflow(struct tty *tp, int block)
1166 {
1167 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev));
1168 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1169 	int s;
1170 
1171 	if (ch->ch_o_rts == 0)
1172 		return (0);
1173 
1174 	s = splserial();
1175 	if (block) {
1176 		if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1177 			SET(ch->ch_rx_flags, RX_TTY_BLOCKED);
1178 			sbscn_dohwiflow(ch);
1179 		}
1180 	} else {
1181 		if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) {
1182 			CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1183 			sbscn_schedrx(ch);
1184 		}
1185 		if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1186 			CLR(ch->ch_rx_flags, RX_TTY_BLOCKED);
1187 			sbscn_dohwiflow(ch);
1188 		}
1189 	}
1190 	splx(s);
1191 	return (1);
1192 }
1193 
1194 /*
1195  * (un)block input via hw flowcontrol
1196  */
1197 void
1198 sbscn_dohwiflow(struct sbscn_channel *ch)
1199 {
1200 
1201 	if (ch->ch_o_rts == 0)
1202 		return;
1203 
1204 	if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) {
1205 		CLR(ch->ch_oports, ch->ch_o_rts);
1206 		CLR(ch->ch_oports_active, ch->ch_o_rts);
1207 	} else {
1208 		SET(ch->ch_oports, ch->ch_o_rts);
1209 		SET(ch->ch_oports_active, ch->ch_o_rts);
1210 	}
1211 	SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active);
1212 }
1213 
1214 void
1215 sbscn_start(struct tty *tp)
1216 {
1217 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev));
1218 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1219 	int s;
1220 
1221 	s = spltty();
1222 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1223 		goto out;
1224 	if (ch->ch_tx_stopped)
1225 		goto out;
1226 	if (!ttypull(tp))
1227 		goto out;
1228 
1229 	/* Grab the first contiguous region of buffer space. */
1230 	{
1231 		u_char *tba;
1232 		int tbc;
1233 
1234 		tba = tp->t_outq.c_cf;
1235 		tbc = ndqb(&tp->t_outq, 0);
1236 
1237 		(void)splserial();
1238 
1239 		ch->ch_tba = tba;
1240 		ch->ch_tbc = tbc;
1241 	}
1242 
1243 	SET(tp->t_state, TS_BUSY);
1244 	ch->ch_tx_busy = 1;
1245 
1246 	/* Enable transmit completion interrupts if necessary. */
1247 	if (!ISSET(ch->ch_imr, 0x1)) {
1248 		SET(ch->ch_imr, 0x1);
1249 		WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1250 	}
1251 
1252 	/* Output the first chunk of the contiguous buffer. */
1253 	{
1254 		u_char c;
1255 
1256 		while (ch->ch_tbc && READ_REG(ch->ch_base + 0x20) & 0x04) {
1257 			c = *ch->ch_tba++;
1258 			ch->ch_tbc--;
1259 			WRITE_REG(ch->ch_base + 0x70, c);
1260 		}
1261 	}
1262 out:
1263 	splx(s);
1264 	return;
1265 }
1266 
1267 /*
1268  * Stop output on a line.
1269  */
1270 void
1271 sbscnstop(struct tty *tp, int flag)
1272 {
1273 	struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev));
1274 	struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)];
1275 	int s;
1276 
1277 	s = splserial();
1278 	if (ISSET(tp->t_state, TS_BUSY)) {
1279 		/* Stop transmitting at the next chunk. */
1280 		ch->ch_tbc = 0;
1281 		ch->ch_heldtbc = 0;
1282 		if (!ISSET(tp->t_state, TS_TTSTOP))
1283 			SET(tp->t_state, TS_FLUSH);
1284 	}
1285 	splx(s);
1286 }
1287 
1288 void
1289 sbscn_diag(void *arg)
1290 {
1291 	struct sbscn_channel *ch = arg;
1292 	struct sbscn_softc *sc = ch->ch_sc;
1293 	int overflows, floods;
1294 	int s;
1295 
1296 	s = splserial();
1297 	overflows = ch->ch_overflows;
1298 	ch->ch_overflows = 0;
1299 	floods = ch->ch_floods;
1300 	ch->ch_floods = 0;
1301 	ch->ch_errors = 0;
1302 	splx(s);
1303 
1304 	log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n",
1305 	    sc->sc_dev.dv_xname, ch->ch_num,
1306 	    overflows, overflows == 1 ? "" : "s",
1307 	    floods, floods == 1 ? "" : "s");
1308 }
1309 
1310 integrate void
1311 sbscn_rxsoft(struct sbscn_channel *ch, struct tty *tp)
1312 {
1313 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1314 	u_char *get, *end;
1315 	u_int cc, scc;
1316 	u_char sr;
1317 	int code;
1318 	int s;
1319 
1320 	end = ch->ch_ebuf;
1321 	get = ch->ch_rbget;
1322 	scc = cc = sbscn_rbuf_size - ch->ch_rbavail;
1323 
1324 	if (cc == sbscn_rbuf_size) {
1325 		ch->ch_floods++;
1326 		if (ch->ch_errors++ == 0)
1327 			callout_reset(&ch->ch_diag_callout, 60 * hz,
1328 			    sbscn_diag, ch);
1329 	}
1330 
1331 	while (cc) {
1332 		code = get[0];
1333 		sr = get[1];
1334 		if (ISSET(sr, 0xf0)) {
1335 			if (ISSET(sr, 0x10)) {
1336 				ch->ch_overflows++;
1337 				if (ch->ch_errors++ == 0)
1338 					callout_reset(&ch->ch_diag_callout,
1339 					    60 * hz, sbscn_diag, ch);
1340 			}
1341 			if (ISSET(sr, 0xc0))
1342 				SET(code, TTY_FE);
1343 			if (ISSET(sr, 0x20))
1344 				SET(code, TTY_PE);
1345 		}
1346 		if ((*rint)(code, tp) == -1) {
1347 			/*
1348 			 * The line discipline's buffer is out of space.
1349 			 */
1350 			if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) {
1351 				/*
1352 				 * We're either not using flow control, or the
1353 				 * line discipline didn't tell us to block for
1354 				 * some reason.  Either way, we have no way to
1355 				 * know when there's more space available, so
1356 				 * just drop the rest of the data.
1357 				 */
1358 				get += cc << 1;
1359 				if (get >= end)
1360 					get -= sbscn_rbuf_size << 1;
1361 				cc = 0;
1362 			} else {
1363 				/*
1364 				 * Don't schedule any more receive processing
1365 				 * until the line discipline tells us there's
1366 				 * space available (through comhwiflow()).
1367 				 * Leave the rest of the data in the input
1368 				 * buffer.
1369 				 */
1370 				SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED);
1371 			}
1372 			break;
1373 		}
1374 		get += 2;
1375 		if (get >= end)
1376 			get = ch->ch_rbuf;
1377 		cc--;
1378 	}
1379 
1380 	if (cc != scc) {
1381 		ch->ch_rbget = get;
1382 		s = splserial();
1383 		cc = ch->ch_rbavail += scc - cc;
1384 		/* Buffers should be ok again, release possible block. */
1385 		if (cc >= ch->ch_r_lowat) {
1386 			if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1387 				CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED);
1388 				SET(ch->ch_imr, 0x02);
1389 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1390 			}
1391 			if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) {
1392 				CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1393 				sbscn_dohwiflow(ch);
1394 			}
1395 		}
1396 		splx(s);
1397 	}
1398 }
1399 
1400 integrate void
1401 sbscn_txsoft(struct sbscn_channel *ch, struct tty *tp)
1402 {
1403 
1404 	CLR(tp->t_state, TS_BUSY);
1405 	if (ISSET(tp->t_state, TS_FLUSH))
1406 		CLR(tp->t_state, TS_FLUSH);
1407 	else
1408 		ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf));
1409 	(*tp->t_linesw->l_start)(tp);
1410 }
1411 
1412 integrate void
1413 sbscn_stsoft(struct sbscn_channel *ch, struct tty *tp)
1414 {
1415 	u_char iports, delta;
1416 	int s;
1417 
1418 	s = splserial();
1419 	iports = ch->ch_iports;
1420 	delta = ch->ch_iports_delta;
1421 	ch->ch_iports_delta = 0;
1422 	splx(s);
1423 
1424 	if (ISSET(delta, ch->ch_i_dcd)) {
1425 		/*
1426 		 * Inform the tty layer that carrier detect changed.
1427 		 */
1428 		(void) (*tp->t_linesw->l_modem)(tp,
1429 		    ISSET(iports, ch->ch_i_dcd));
1430 	}
1431 
1432 	if (ISSET(delta, ch->ch_i_cts)) {
1433 		/* Block or unblock output according to flow control. */
1434 		if (ISSET(iports, ch->ch_i_cts)) {
1435 			ch->ch_tx_stopped = 0;
1436 			(*tp->t_linesw->l_start)(tp);
1437 		} else {
1438 			ch->ch_tx_stopped = 1;
1439 		}
1440 	}
1441 
1442 #ifdef SBSCN_DEBUG
1443 	if (sbscn_debug)
1444 		sbscn_status(ch, "sbscn_stsoft");
1445 #endif
1446 }
1447 
1448 void
1449 sbscn_soft(void *arg)
1450 {
1451 	struct sbscn_channel *ch = arg;
1452 	struct tty *tp = ch->ch_tty;
1453 
1454 	if (ch->ch_rx_ready) {
1455 		ch->ch_rx_ready = 0;
1456 		sbscn_rxsoft(ch, tp);
1457 	}
1458 
1459 	if (ch->ch_st_check) {
1460 		ch->ch_st_check = 0;
1461 		sbscn_stsoft(ch, tp);
1462 	}
1463 
1464 	if (ch->ch_tx_done) {
1465 		ch->ch_tx_done = 0;
1466 		sbscn_txsoft(ch, tp);
1467 	}
1468 }
1469 
1470 void
1471 sbscn_intr(void *arg, uint32_t status, uint32_t pc)
1472 {
1473 	struct sbscn_channel *ch = arg;
1474 	u_char *put, *end;
1475 	u_int cc;
1476 	u_char isr, sr;
1477 
1478 	/* read ISR */
1479 	isr = READ_REG(ch->ch_isr_base) & ch->ch_imr;
1480 	if (isr == 0)
1481 		return;
1482 
1483 	end = ch->ch_ebuf;
1484 	put = ch->ch_rbput;
1485 	cc = ch->ch_rbavail;
1486 
1487 	do {
1488 		u_char	iports, delta;
1489 
1490 		if (isr & 0x02) {
1491 
1492 			sr = READ_REG(ch->ch_base + 0x20);
1493 			/* XXX sr 0x01 bit must be set at this point */
1494 
1495 #if defined(DDB) || defined(KGDB)
1496 			if ((sr & 0x80) == 0x80) {
1497 #ifdef DDB
1498 				if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) {
1499 					(void)READ_REG(ch->ch_base + 0x60);
1500 					console_debugger();
1501 					continue;
1502 				}
1503 #endif
1504 #ifdef KGDB
1505 				if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) {
1506 					(void)READ_REG(ch->ch_base + 0x60);
1507 					kgdb_connect(1);
1508 					continue;
1509 				}
1510 #endif
1511 			}
1512 #endif /* DDB || KGDB */
1513 
1514 		    	if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) {
1515 				while (cc > 0) {
1516 					put[0] = READ_REG(ch->ch_base + 0x60);
1517 					put[1] = sr;
1518 					put += 2;
1519 					if (put >= end)
1520 						put = ch->ch_rbuf;
1521 					cc--;
1522 
1523 					sr = READ_REG(ch->ch_base + 0x20);
1524 					if (!ISSET(sr, 0x02))
1525 						break;
1526 				}
1527 
1528 				/*
1529 				 * Current string of incoming characters ended
1530 				 * because no more data was available or we
1531 				 * ran out of space.  Schedule a receive event
1532 				 * if any data was received.  If we're out of
1533 				 * space, turn off receive interrupts.
1534 				 */
1535 				ch->ch_rbput = put;
1536 				ch->ch_rbavail = cc;
1537 				if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED))
1538 					ch->ch_rx_ready = 1;
1539 
1540 				/*
1541 				 * See if we are in danger of overflowing a
1542 				 * buffer. If so, use hardware flow control
1543 				 * to ease the pressure.
1544 				 */
1545 				if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) &&
1546 				    cc < ch->ch_r_hiwat) {
1547 					SET(ch->ch_rx_flags, RX_IBUF_BLOCKED);
1548 					sbscn_dohwiflow(ch);
1549 				}
1550 
1551 				/*
1552 				 * If we're out of space, disable receive
1553 				 * interrupts until the queue has drained
1554 				 * a bit.
1555 				 */
1556 				if (!cc) {
1557 					SET(ch->ch_rx_flags,
1558 					    RX_IBUF_OVERFLOWED);
1559 					CLR(ch->ch_imr, 0x02);
1560 					WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1561 				}
1562 			} else {
1563 				/* XXX panic? */
1564 				CLR(ch->ch_imr, 0x02);
1565 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1566 				continue;
1567 			}
1568 		}
1569 
1570 		if (isr & 0x01) {
1571 			CLR(ch->ch_imr, 0x01);
1572 			WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1573 		}
1574 
1575 #if 0
1576 XXX
1577 		if (isr & 0x08) {
1578 			clear input signal change!
1579 		}
1580 #endif
1581 		iports = GET_INPUT_SIGNALS(ch);
1582 		delta = iports ^ ch->ch_iports;
1583 		ch->ch_iports = iports;
1584 
1585 		/*
1586 		 * Process normal status changes
1587 		 */
1588 		if (ISSET(delta, ch->ch_i_mask)) {
1589 			SET(ch->ch_iports_delta, delta);
1590 
1591 			/*
1592 			 * Stop output immediately if we lose the output
1593 			 * flow control signal or carrier detect.
1594 			 */
1595 			if (ISSET(~iports, ch->ch_i_mask)) {
1596 				ch->ch_tbc = 0;
1597 				ch->ch_heldtbc = 0;
1598 #ifdef SBSCN_DEBUG
1599 				if (sbscn_debug)
1600 					sbscn_status(ch, "sbscn_intr  ");
1601 #endif
1602 			}
1603 
1604 			ch->ch_st_check = 1;
1605 		}
1606 	} while ((isr = (READ_REG(ch->ch_isr_base) & ch->ch_imr)) != 0);
1607 
1608 	/*
1609 	 * Done handling any receive interrupts and status changes, and
1610 	 * clearing the tx-ready interrupt if it was set.  See if data can
1611 	 * be transmitted as well. Schedule tx done event if no data left
1612 	 * and tty was marked busy.
1613 	 */
1614 	sr = READ_REG(ch->ch_base + 0x20);
1615 	if (ISSET(sr, 0x4)) {
1616 
1617 		/*
1618 		 * If we've delayed a parameter change, do it now, and restart
1619 		 * output.
1620 		 */
1621 		if (ch->ch_heldchange) {
1622 			sbscn_loadchannelregs(ch);
1623 			ch->ch_heldchange = 0;
1624 			ch->ch_tbc = ch->ch_heldtbc;
1625 			ch->ch_heldtbc = 0;
1626 		}
1627 
1628 		/* Output the next chunk of the contiguous buffer, if any. */
1629 		if (ch->ch_tbc > 0) {
1630 			int wrote1;
1631 			u_char c;
1632 
1633 			wrote1 = 0;
1634 			while (ch->ch_tbc &&
1635 			    READ_REG(ch->ch_base + 0x20) & 0x04) {
1636 				wrote1 = 1;
1637 				c = *ch->ch_tba++;
1638 				ch->ch_tbc--;
1639 				WRITE_REG(ch->ch_base + 0x70, c);
1640 			}
1641 			if (wrote1) {
1642 				SET(ch->ch_imr, 0x01);
1643 				WRITE_REG(ch->ch_imr_base, ch->ch_imr);
1644 			}
1645 		} else {
1646 			/*
1647 			 * transmit completion interrupts already disabled,
1648 			 * mark the channel tx state as done.
1649 			 */
1650 			if (ch->ch_tx_busy) {
1651 				ch->ch_tx_busy = 0;
1652 				ch->ch_tx_done = 1;
1653 			}
1654 		}
1655 	}
1656 
1657 	/* Wake up the poller. */
1658 	softint_schedule(ch->ch_si);
1659 
1660 #if NRND > 0 && defined(RND_SBSCN)
1661 	rnd_add_uint32(&ch->ch_rnd_source, isr | sr);
1662 #endif
1663 }
1664 
1665 /*
1666  * The following functions are polled getc and putc routines, shared
1667  * by the console and kgdb glue.
1668  */
1669 
1670 int
1671 sbscn_common_getc(u_long addr, int chan)
1672 {
1673 	int s = splhigh();
1674 	u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1675 	int c;
1676 
1677 	/* block until a character becomes available */
1678 	while ((READ_REG(base + 0x20) & 0x01) == 0)
1679 		continue;
1680 
1681 	c = READ_REG(base + 0x60) & 0xff;
1682 	splx(s);
1683 	return (c);
1684 }
1685 
1686 void
1687 sbscn_common_putc(u_long addr, int chan, int c)
1688 {
1689 	int s = splhigh();
1690 	int timo;
1691 	u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1692 
1693 	/* wait for any pending transmission to finish */
1694 	timo = 1500000;
1695 	while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
1696 		continue;
1697 
1698 	WRITE_REG(base + 0x70, c);
1699 
1700 	/* wait for this transmission to complete */
1701 	timo = 15000000;
1702 	while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo)
1703 		continue;
1704 
1705 	splx(s);
1706 }
1707 
1708 /*
1709  * Initialize UART for use as console or KGDB line.
1710  */
1711 int
1712 sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag)
1713 {
1714 #if 1
1715 	u_long chanregbase = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100));
1716 	u_long imaskreg = MIPS_PHYS_TO_KSEG1(addr + 0x230 + (chan * 0x20));
1717 	u_char mode1, mode2;
1718 	u_long brc;
1719 	volatile int timo;
1720 
1721 	WRITE_REG(imaskreg, 0);			/* disable channel intrs */
1722 
1723 	/* XXX should we really do the following?  how about only if enabled? */
1724 	/* wait for any pending transmission to finish */
1725 	timo = 1500000;
1726 	while ((READ_REG(chanregbase + 0x20) & 0x08) == 0 && --timo)
1727 		continue;
1728 
1729 	/* XXX: wait a little.  THIS SHOULD NOT BE NECESSARY!!! (?) */
1730 	timo = 1500000;
1731 	while (--timo)
1732 		;
1733 
1734 	WRITE_REG(chanregbase + 0x50, 2 << 4);	/* reset receiver */
1735 	WRITE_REG(chanregbase + 0x50, 3 << 4);	/* reset transmitter */
1736 
1737 	/* set up the line for use */
1738 	(void)cflag2modes(cflag, &mode1, &mode2);
1739 	(void)sbscn_speed(rate, &brc);
1740 	WRITE_REG(chanregbase + 0x00, mode1);
1741 	WRITE_REG(chanregbase + 0x10, mode2);
1742 	WRITE_REG(chanregbase + 0x30, brc);
1743 
1744 	/* enable transmit and receive */
1745 #define	M_DUART_RX_EN			0x01
1746 #define	M_DUART_TX_EN			0x04
1747 	WRITE_REG(chanregbase + 0x50,M_DUART_RX_EN | M_DUART_TX_EN);
1748 #endif
1749 
1750 	/* XXX: wait a little.  THIS SHOULD NOT BE NECESSARY!!! (?) */
1751 	timo = 1500000;
1752 	while (--timo)
1753 		;
1754 
1755 #if 0 /* XXXCGD */
1756 	bus_space_handle_t ioh;
1757 
1758 	if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh))
1759 		return (ENOMEM); /* ??? */
1760 
1761 	bus_space_write_1(iot, ioh, com_lcr, LCR_EERS);
1762 	bus_space_write_1(iot, ioh, com_efr, 0);
1763 	bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB);
1764 	rate = comspeed(rate, frequency);
1765 	bus_space_write_1(iot, ioh, com_dlbl, rate);
1766 	bus_space_write_1(iot, ioh, com_dlbh, rate >> 8);
1767 	bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag));
1768 	bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS);
1769 	bus_space_write_1(iot, ioh, com_fifo,
1770 	    FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1);
1771 	bus_space_write_1(iot, ioh, com_ier, 0);
1772 
1773 	*iohp = ioh;
1774 	return (0);
1775 #endif /* XXXCGD */
1776 	/* XXXCGD */
1777 	return (0);
1778 }
1779 
1780 /*
1781  * Following are all routines needed for sbscn to act as console
1782  */
1783 int
1784 sbscn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag)
1785 {
1786 	int res;
1787 	static struct consdev sbscn_cons = {
1788 		NULL, NULL, sbscn_cngetc, sbscn_cnputc, sbscn_cnpollc, NULL,
1789 		    NULL, NULL, NODEV, CN_NORMAL
1790 	};
1791 
1792 	res = sbscn_init(addr, chan, rate, cflag);
1793 	if (res)
1794 		return (res);
1795 
1796 	cn_tab = &sbscn_cons;
1797 
1798 	sbscn_cons_present = 1;
1799 	sbscn_cons_addr = addr;
1800 	sbscn_cons_chan = chan;
1801 	sbscn_cons_rate = rate;
1802 	sbscn_cons_cflag = cflag;
1803 
1804 	return (0);
1805 }
1806 
1807 int
1808 sbscn_cngetc(dev_t dev)
1809 {
1810 
1811 	return (sbscn_common_getc(sbscn_cons_addr, sbscn_cons_chan));
1812 }
1813 
1814 /*
1815  * Console kernel output character routine.
1816  */
1817 void
1818 sbscn_cnputc(dev_t dev, int c)
1819 {
1820 
1821 	sbscn_common_putc(sbscn_cons_addr, sbscn_cons_chan, c);
1822 }
1823 
1824 void
1825 sbscn_cnpollc(dev_t dev, int on)
1826 {
1827 
1828 }
1829 
1830 #ifdef KGDB
1831 int
1832 sbscn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag)
1833 {
1834 	int res;
1835 
1836 	if (!sbscn_cons_present &&
1837 	    sbscn_cons_addr == addr && sbscn_cons_chan == chan)
1838 		return (EBUSY); /* cannot share with console */
1839 
1840 	res = sbscn_init(addr, chan, rate, cflag);
1841 	if (res)
1842 		return (res);
1843 
1844 	kgdb_attach(sbscn_kgdb_getc, sbscn_kgdb_putc, NULL);
1845 	kgdb_dev = 123; /* unneeded, only to satisfy some tests */
1846 
1847 	sbscn_kgdb_present = 1;
1848 	sbscn_kgdb_addr = addr;
1849 	sbscn_kgdb_chan = chan;
1850 
1851 	return (0);
1852 }
1853 
1854 /* ARGSUSED */
1855 int
1856 sbscn_kgdb_getc(void *arg)
1857 {
1858 
1859 	return (sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan));
1860 }
1861 
1862 /* ARGSUSED */
1863 void
1864 sbscn_kgdb_putc(void *arg, int c)
1865 {
1866 
1867 	sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan, c);
1868 }
1869 #endif /* KGDB */
1870 
1871 /*
1872  * helper function to identify the sbscn channels used by
1873  * console or KGDB (and not yet autoconf attached)
1874  */
1875 int
1876 sbscn_is_console(u_long addr, int chan)
1877 {
1878 
1879 	if (sbscn_cons_present && !sbscn_cons_attached &&
1880 	    sbscn_cons_addr == addr && sbscn_cons_chan == chan)
1881 		return (1);
1882 #ifdef KGDB
1883 	if (sbscn_kgdb_present && !sbscn_kgdb_attached &&
1884 	    sbscn_kgdb_addr == addr && sbscn_kgdb_chan == chan)
1885 		return (1);
1886 #endif
1887 	return (0);
1888 }
1889