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