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