xref: /netbsd-src/sys/arch/arm/sa11x0/sa11x0_com.c (revision 50728e7823a76d5bd1a7bfa3a4eac400269b1339)
1 /*      $NetBSD: sa11x0_com.c,v 1.44 2008/06/11 22:37:21 cegger Exp $        */
2 
3 /*-
4  * Copyright (c) 1998, 1999, 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by IWAMOTO Toshihiro.
9  *
10  * This code is derived from software contributed to The NetBSD Foundation
11  * by Charles M. Hannum.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
24  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
26  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 /*
36  * Copyright (c) 1991 The Regents of the University of California.
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in the
46  *    documentation and/or other materials provided with the distribution.
47  * 3. Neither the name of the University nor the names of its contributors
48  *    may be used to endorse or promote products derived from this software
49  *    without specific prior written permission.
50  *
51  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61  * SUCH DAMAGE.
62  *
63  *	@(#)com.c	7.5 (Berkeley) 5/16/91
64  */
65 
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: sa11x0_com.c,v 1.44 2008/06/11 22:37:21 cegger Exp $");
68 
69 #include "opt_com.h"
70 #include "opt_ddb.h"
71 #include "opt_ddbparam.h"
72 #include "opt_kgdb.h"
73 #include "opt_multiprocessor.h"
74 #include "opt_lockdebug.h"
75 
76 #include "rnd.h"
77 #if NRND > 0 && defined(RND_COM)
78 #include <sys/rnd.h>
79 #endif
80 
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/types.h>
84 #include <sys/conf.h>
85 #include <sys/file.h>
86 #include <sys/device.h>
87 #include <sys/kernel.h>
88 #include <sys/malloc.h>
89 #include <sys/tty.h>
90 #include <sys/uio.h>
91 #include <sys/vnode.h>
92 #include <sys/kauth.h>
93 
94 #include <dev/cons.h>
95 
96 #include <machine/bus.h>
97 #include <arm/sa11x0/sa11x0_reg.h>
98 #include <arm/sa11x0/sa11x0_var.h>
99 #include <arm/sa11x0/sa11x0_comreg.h>
100 #include <arm/sa11x0/sa11x0_comvar.h>
101 #include <arm/sa11x0/sa11x0_gpioreg.h>
102 
103 #ifdef hpcarm
104 #include <hpc/include/platid.h>
105 #include <hpc/include/platid_mask.h>
106 #endif
107 
108 #include "sacom.h"
109 
110 dev_type_open(sacomopen);
111 dev_type_close(sacomclose);
112 dev_type_read(sacomread);
113 dev_type_write(sacomwrite);
114 dev_type_ioctl(sacomioctl);
115 dev_type_stop(sacomstop);
116 dev_type_tty(sacomtty);
117 dev_type_poll(sacompoll);
118 
119 const struct cdevsw sacom_cdevsw = {
120 	sacomopen, sacomclose, sacomread, sacomwrite, sacomioctl,
121 	sacomstop, sacomtty, sacompoll, nommap, ttykqfilter, D_TTY
122 };
123 
124 static	int	sacom_match(struct device *, struct cfdata *, void *);
125 static	void	sacom_attach(struct device *, struct device *, void *);
126 static	void	sacom_filltx(struct sacom_softc *);
127 static	void	sacom_attach_subr(struct sacom_softc *);
128 #if defined(DDB) || defined(KGDB)
129 static	void	sacom_enable_debugport(struct sacom_softc *);
130 #endif
131 int		sacom_detach(struct device *, int);
132 void		sacom_config(struct sacom_softc *);
133 int		sacom_activate(struct device *, enum devact);
134 void		sacom_shutdown(struct sacom_softc *);
135 static	u_int	cflag2cr0(tcflag_t);
136 int		sacomparam(struct tty *, struct termios *);
137 void		sacomstart(struct tty *);
138 int		sacomhwiflow(struct tty *, int);
139 
140 void		sacom_loadchannelregs(struct sacom_softc *);
141 void		sacom_hwiflow(struct sacom_softc *);
142 void		sacom_break(struct sacom_softc *, int);
143 void		sacom_modem(struct sacom_softc *, int);
144 void		tiocm_to_sacom(struct sacom_softc *, u_long, int);
145 int		sacom_to_tiocm(struct sacom_softc *);
146 void		sacom_iflush(struct sacom_softc *);
147 
148 int		sacominit(bus_space_tag_t, bus_addr_t, int, tcflag_t,
149 			  bus_space_handle_t *);
150 int		sacom_is_console(bus_space_tag_t, bus_addr_t,
151 				 bus_space_handle_t *);
152 
153 void 		sacomsoft(void *);
154 
155 static inline void sacom_rxsoft(struct sacom_softc *, struct tty *);
156 static inline void sacom_txsoft(struct sacom_softc *, struct tty *);
157 static inline void sacom_stsoft(struct sacom_softc *, struct tty *);
158 static inline void sacom_schedrx(struct sacom_softc *);
159 
160 #ifdef hpcarm
161 /* HPCARM specific functions */
162 static void	sacom_j720_init(struct sa11x0_softc *, struct sacom_softc *);
163 #endif
164 
165 #define COMUNIT_MASK	0x7ffff
166 #define COMDIALOUT_MASK	0x80000
167 
168 #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
169 #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
170 
171 #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
172 			 device_is_active(&(sc)->sc_dev))
173 
174 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
175 #define COM_LOCK(sc)
176 #define COM_UNLOCK(sc)
177 
178 int		sacomintr(void *);
179 int		sacomcngetc(dev_t);
180 void		sacomcnputc(dev_t, int);
181 void		sacomcnpollc(dev_t, int);
182 
183 void		sacomcnprobe(struct consdev *);
184 void		sacomcninit(struct consdev *);
185 
186 extern struct bus_space sa11x0_bs_tag;
187 
188 static bus_space_tag_t sacomconstag;
189 static bus_space_handle_t sacomconsioh;
190 static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */
191 
192 static int sacomconsattached;
193 static int sacomconsrate;
194 static tcflag_t sacomconscflag;
195 
196 CFATTACH_DECL(sacom, sizeof(struct sacom_softc),
197     sacom_match, sacom_attach, NULL, NULL);
198 extern struct cfdriver sacom_cd;
199 
200 #ifdef hpcarm
201 struct platid_data sacom_platid_table[] = {
202 	{ &platid_mask_MACH_HP_JORNADA_7XX, sacom_j720_init },
203 	{ NULL, NULL }
204 };
205 #endif
206 
207 struct consdev sacomcons = {
208 	NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL,
209 	NULL, NULL, NODEV, CN_NORMAL
210 };
211 
212 #ifndef CONMODE
213 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
214 #endif
215 #ifndef CONSPEED
216 #define CONSPEED 9600
217 #endif
218 #ifndef CONADDR
219 #define CONADDR SACOM3_BASE
220 #endif
221 
222 static int
223 sacom_match(struct device *parent, struct cfdata *match, void *aux)
224 {
225 
226 	return 1;
227 }
228 
229 void
230 sacom_attach(struct device *parent, struct device *self, void *aux)
231 {
232 	struct sacom_softc *sc = (struct sacom_softc*)self;
233 	struct sa11x0_attach_args *sa = aux;
234 
235 #ifdef hpcarm
236 	struct platid_data *p;
237 	void (*mdinit)(struct device *, struct sacom_softc *);
238 #endif
239 
240 	printf("\n");
241 
242 	sc->sc_iot = sa->sa_iot;
243 	sc->sc_baseaddr = sa->sa_addr;
244 
245 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
246 	    &sc->sc_ioh)) {
247 		printf("%s: unable to map registers\n", sc->sc_dev.dv_xname);
248 		return;
249 	}
250 
251 	printf("%s: ", sc->sc_dev.dv_xname);
252 	switch (sc->sc_baseaddr) {
253 	case SACOM1_BASE:
254 		printf("SA-11x0 UART1\n");
255 		break;
256 	case SACOM2_BASE:
257 		printf("SA-11x0 UART2 (IRDA)\n");
258 		break;
259 	case SACOM3_BASE:
260 		printf("SA-11x0 UART3\n");
261 		break;
262 	default:
263 		printf("unknown SA-11x0 UART\n");
264 		break;
265 	}
266 
267 	sacom_attach_subr(sc);
268 
269 #ifdef hpcarm
270 	/* Do hpcarm specific initialization, if any */
271 	if ((p = platid_search_data(&platid, sacom_platid_table)) != NULL) {
272 		mdinit = p->data;
273 		(mdinit)(parent, sc);
274 	}
275 #endif
276 
277 	sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc);
278 }
279 
280 void
281 sacom_attach_subr(struct sacom_softc *sc)
282 {
283 	bus_addr_t iobase = sc->sc_baseaddr;
284 	bus_space_tag_t iot = sc->sc_iot;
285 	struct tty *tp;
286 
287 	/* XXX Do we need to disable interrupts here? */
288 
289 	if (iot == sacomconstag && iobase == sacomconsaddr) {
290 		sacomconsattached = 1;
291 		sc->sc_speed = SACOMSPEED(sacomconsrate);
292 
293 		/* Make sure the console is always "hardwired". */
294 		delay(10000);			/* wait for output to finish */
295 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
296 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
297 	}
298 
299 	tp = ttymalloc();
300 	tp->t_oproc = sacomstart;
301 	tp->t_param = sacomparam;
302 	tp->t_hwiflow = sacomhwiflow;
303 
304 	sc->sc_tty = tp;
305 	sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
306 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
307 	sc->sc_rbavail = SACOM_RING_SIZE;
308 	if (sc->sc_rbuf == NULL) {
309 		printf("%s: unable to allocate ring buffer\n",
310 		    sc->sc_dev.dv_xname);
311 		return;
312 	}
313 	sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1);
314 	sc->sc_tbc = 0;
315 
316 	tty_attach(tp);
317 
318 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
319 		int maj;
320 
321 		/* locate the major number */
322 		maj = cdevsw_lookup_major(&sacom_cdevsw);
323 
324 		cn_tab->cn_dev = makedev(maj, device_unit(&sc->sc_dev));
325 
326 		delay(10000); /* XXX */
327 		printf("%s: console\n", sc->sc_dev.dv_xname);
328 		delay(10000); /* XXX */
329 	}
330 
331 
332 	sc->sc_si = softint_establish(SOFTINT_SERIAL, sacomsoft, sc);
333 
334 #if NRND > 0 && defined(RND_COM)
335 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
336 			  RND_TYPE_TTY, 0);
337 #endif
338 
339 	/* if there are no enable/disable functions, assume the device
340 	   is always enabled */
341 	if (!sc->enable)
342 		sc->enabled = 1;
343 
344 	sacom_config(sc);
345 
346 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
347 }
348 
349 /* This is necessary when dynamically changing SAIP configuration. */
350 int
351 sacom_detach(struct device *self, int flags)
352 {
353 	struct sacom_softc *sc = (struct sacom_softc *)self;
354 	int maj, mn;
355 
356 	/* locate the major number */
357 	maj = cdevsw_lookup_major(&sacom_cdevsw);
358 
359 	/* Nuke the vnodes for any open instances. */
360 	mn = device_unit(self);
361 	vdevgone(maj, mn, mn, VCHR);
362 
363 	mn |= COMDIALOUT_MASK;
364 	vdevgone(maj, mn, mn, VCHR);
365 
366 	/* Free the receive buffer. */
367 	free(sc->sc_rbuf, M_DEVBUF);
368 
369 	/* Detach and free the tty. */
370 	tty_detach(sc->sc_tty);
371 	ttyfree(sc->sc_tty);
372 
373 	/* Unhook the soft interrupt handler. */
374 	softint_disestablish(sc->sc_si);
375 
376 #if NRND > 0 && defined(RND_COM)
377 	/* Unhook the entropy source. */
378 	rnd_detach_source(&sc->rnd_source);
379 #endif
380 
381 	return 0;
382 }
383 
384 void
385 sacom_config(struct sacom_softc *sc)
386 {
387 	bus_space_tag_t iot = sc->sc_iot;
388 	bus_space_handle_t ioh = sc->sc_ioh;
389 
390 	/* Disable engine before configuring the device. */
391 	sc->sc_cr3 = 0;
392 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
393 
394 #ifdef DDB
395 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE))
396 		sacom_enable_debugport(sc);
397 #endif
398 }
399 
400 #ifdef DDB
401 static void
402 sacom_enable_debugport(struct sacom_softc *sc)
403 {
404 	bus_space_tag_t iot = sc->sc_iot;
405 	bus_space_handle_t ioh = sc->sc_ioh;
406 	int s;
407 
408 	s = splserial();
409 	COM_LOCK(sc);
410 	sc->sc_cr3 = CR3_RXE | CR3_TXE;
411 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
412 	COM_UNLOCK(sc);
413 	splx(s);
414 }
415 #endif
416 
417 int
418 sacom_activate(struct device *self, enum devact act)
419 {
420 	struct sacom_softc *sc = (struct sacom_softc *)self;
421 	int s, rv = 0;
422 
423 	s = splserial();
424 	COM_LOCK(sc);
425 	switch (act) {
426 	case DVACT_ACTIVATE:
427 		rv = EOPNOTSUPP;
428 		break;
429 
430 	case DVACT_DEACTIVATE:
431 		if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) {
432 			rv = EBUSY;
433 			break;
434 		}
435 
436 		if (sc->disable != NULL && sc->enabled != 0) {
437 			(*sc->disable)(sc);
438 			sc->enabled = 0;
439 		}
440 		break;
441 	}
442 
443 	COM_UNLOCK(sc);
444 	splx(s);
445 	return rv;
446 }
447 
448 void
449 sacom_shutdown(struct sacom_softc *sc)
450 {
451 	struct tty *tp = sc->sc_tty;
452 	int s;
453 
454 	s = splserial();
455 	COM_LOCK(sc);
456 
457 	/* Clear any break condition set with TIOCSBRK. */
458 	sacom_break(sc, 0);
459 
460 	/*
461 	 * Hang up if necessary.  Wait a bit, so the other side has time to
462 	 * notice even if we immediately open the port again.
463 	 * Avoid tsleeping above splhigh().
464 	 */
465 	if (ISSET(tp->t_cflag, HUPCL)) {
466 		sacom_modem(sc, 0);
467 		COM_UNLOCK(sc);
468 		splx(s);
469 		/* XXX tsleep will only timeout */
470 		(void) tsleep(sc, TTIPRI, ttclos, hz);
471 		s = splserial();
472 		COM_LOCK(sc);
473 	}
474 
475 	/* Turn off interrupts. */
476 	sc->sc_cr3 = 0;
477 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3);
478 
479 	if (sc->disable) {
480 #ifdef DIAGNOSTIC
481 		if (!sc->enabled)
482 			panic("sacom_shutdown: not enabled?");
483 #endif
484 		(*sc->disable)(sc);
485 		sc->enabled = 0;
486 	}
487 	COM_UNLOCK(sc);
488 	splx(s);
489 }
490 
491 int
492 sacomopen(dev_t dev, int flag, int mode, struct lwp *l)
493 {
494 	struct sacom_softc *sc;
495 	struct tty *tp;
496 	int s, s2;
497 	int error;
498 
499 	sc = device_lookup_private(&sacom_cd, COMUNIT(dev));
500 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
501 		sc->sc_rbuf == NULL)
502 		return ENXIO;
503 
504 	if (!device_is_active(&sc->sc_dev))
505 		return ENXIO;
506 
507 	tp = sc->sc_tty;
508 
509 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
510 		return (EBUSY);
511 
512 	s = spltty();
513 
514 	/*
515 	 * Do the following iff this is a first open.
516 	 */
517 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
518 		struct termios t;
519 
520 		tp->t_dev = dev;
521 
522 		s2 = splserial();
523 		COM_LOCK(sc);
524 
525 		if (sc->enable) {
526 			if ((*sc->enable)(sc)) {
527 				COM_UNLOCK(sc);
528 				splx(s2);
529 				splx(s);
530 				printf("%s: device enable failed\n",
531 				       sc->sc_dev.dv_xname);
532 				return EIO;
533 			}
534 			sc->enabled = 1;
535 			sacom_config(sc);
536 		}
537 
538 		/* Turn on interrupts. */
539 		sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE;
540 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3,
541 				  sc->sc_cr3);
542 
543 
544 		COM_UNLOCK(sc);
545 		splx(s2);
546 
547 		/*
548 		 * Initialize the termios status to the defaults.  Add in the
549 		 * sticky bits from TIOCSFLAGS.
550 		 */
551 		t.c_ispeed = 0;
552 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
553 			t.c_ospeed = sacomconsrate;
554 			t.c_cflag = sacomconscflag;
555 		} else {
556 			t.c_ospeed = TTYDEF_SPEED;
557 			t.c_cflag = TTYDEF_CFLAG;
558 		}
559 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
560 			SET(t.c_cflag, CLOCAL);
561 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
562 			SET(t.c_cflag, CRTSCTS);
563 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
564 			SET(t.c_cflag, MDMBUF);
565 		/* Make sure sacomparam() will do something. */
566 		tp->t_ospeed = 0;
567 		(void) sacomparam(tp, &t);
568 		tp->t_iflag = TTYDEF_IFLAG;
569 		tp->t_oflag = TTYDEF_OFLAG;
570 		tp->t_lflag = TTYDEF_LFLAG;
571 		ttychars(tp);
572 		ttsetwater(tp);
573 
574 		s2 = splserial();
575 		COM_LOCK(sc);
576 
577 		/*
578 		 * Turn on DTR.  We must always do this, even if carrier is not
579 		 * present, because otherwise we'd have to use TIOCSDTR
580 		 * immediately after setting CLOCAL, which applications do not
581 		 * expect.  We always assert DTR while the device is open
582 		 * unless explicitly requested to deassert it.
583 		 */
584 		sacom_modem(sc, 1);
585 
586 		/* Clear the input ring, and unblock. */
587 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
588 		sc->sc_rbavail = SACOM_RING_SIZE;
589 		sacom_iflush(sc);
590 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
591 		sacom_hwiflow(sc);
592 
593 #ifdef COM_DEBUG
594 		if (sacom_debug)
595 			comstatus(sc, "sacomopen  ");
596 #endif
597 
598 		COM_UNLOCK(sc);
599 		splx(s2);
600 	}
601 
602 	splx(s);
603 
604 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
605 	if (error)
606 		goto bad;
607 
608 	error = (*tp->t_linesw->l_open)(dev, tp);
609 	if (error)
610 		goto bad;
611 
612 	return 0;
613 
614 bad:
615 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
616 		/*
617 		 * We failed to open the device, and nobody else had it opened.
618 		 * Clean up the state as appropriate.
619 		 */
620 		sacom_shutdown(sc);
621 	}
622 
623 	return error;
624 }
625 
626 int
627 sacomclose(dev_t dev, int flag, int mode, struct lwp *l)
628 {
629 	struct sacom_softc *sc =
630 		device_lookup_private(&sacom_cd, COMUNIT(dev));
631 	struct tty *tp = sc->sc_tty;
632 
633 	/* XXX This is for cons.c. */
634 	if (!ISSET(tp->t_state, TS_ISOPEN))
635 		return 0;
636 
637 	(*tp->t_linesw->l_close)(tp, flag);
638 	ttyclose(tp);
639 
640 	if (COM_ISALIVE(sc) == 0)
641 		return 0;
642 
643 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
644 		/*
645 		 * Although we got a last close, the device may still be in
646 		 * use; e.g. if this was the dialout node, and there are still
647 		 * processes waiting for carrier on the non-dialout node.
648 		 */
649 		sacom_shutdown(sc);
650 	}
651 
652 	return 0;
653 }
654 
655 int
656 sacomread(dev_t dev, struct uio *uio, int flag)
657 {
658 	struct sacom_softc *sc =
659 		device_lookup_private(&sacom_cd, COMUNIT(dev));
660 	struct tty *tp = sc->sc_tty;
661 
662 	if (COM_ISALIVE(sc) == 0)
663 		return EIO;
664 
665 	return (*tp->t_linesw->l_read)(tp, uio, flag);
666 }
667 
668 int
669 sacomwrite(dev_t dev, struct uio *uio, int flag)
670 {
671 	struct sacom_softc *sc =
672 		device_lookup_private(&sacom_cd, COMUNIT(dev));
673 	struct tty *tp = sc->sc_tty;
674 
675 	if (COM_ISALIVE(sc) == 0)
676 		return EIO;
677 
678 	return (*tp->t_linesw->l_write)(tp, uio, flag);
679 }
680 
681 int
682 sacompoll(dev_t dev, int events, struct lwp *l)
683 {
684 	struct sacom_softc *sc =
685 		device_lookup_private(&sacom_cd, COMUNIT(dev));
686 	struct tty *tp = sc->sc_tty;
687 
688 	if (COM_ISALIVE(sc) == 0)
689 		return EIO;
690 
691 	return (*tp->t_linesw->l_poll)(tp, events, l);
692 }
693 
694 struct tty *
695 sacomtty(dev_t dev)
696 {
697 	struct sacom_softc *sc =
698 		device_lookup_private(&sacom_cd, COMUNIT(dev));
699 	struct tty *tp = sc->sc_tty;
700 
701 	return tp;
702 }
703 
704 int
705 sacomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
706 {
707 	struct sacom_softc *sc =
708 		device_lookup_private(&sacom_cd, COMUNIT(dev));
709 	struct tty *tp = sc->sc_tty;
710 	int error;
711 	int s;
712 
713 	if (COM_ISALIVE(sc) == 0)
714 		return EIO;
715 
716 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
717 	if (error != EPASSTHROUGH)
718 		return error;
719 
720 	error = ttioctl(tp, cmd, data, flag, l);
721 	if (error != EPASSTHROUGH)
722 		return error;
723 
724 	error = 0;
725 
726 	s = splserial();
727 	COM_LOCK(sc);
728 
729 	switch (cmd) {
730 	case TIOCSBRK:
731 		sacom_break(sc, 1);
732 		break;
733 
734 	case TIOCCBRK:
735 		sacom_break(sc, 0);
736 		break;
737 
738 	case TIOCSDTR:
739 		sacom_modem(sc, 1);
740 		break;
741 
742 	case TIOCCDTR:
743 		sacom_modem(sc, 0);
744 		break;
745 
746 	case TIOCGFLAGS:
747 		*(int *)data = sc->sc_swflags;
748 		break;
749 
750 	case TIOCSFLAGS:
751 		error = kauth_authorize_device_tty(l->l_cred,
752 		    KAUTH_DEVICE_TTY_PRIVSET, tp);
753 		if (error)
754 			break;
755 		sc->sc_swflags = *(int *)data;
756 		break;
757 
758 	case TIOCMSET:
759 	case TIOCMBIS:
760 	case TIOCMBIC:
761 		tiocm_to_sacom(sc, cmd, *(int *)data);
762 		break;
763 
764 	case TIOCMGET:
765 		*(int *)data = sacom_to_tiocm(sc);
766 		break;
767 
768 	default:
769 		error = EPASSTHROUGH;
770 		break;
771 	}
772 
773 	COM_UNLOCK(sc);
774 	splx(s);
775 
776 #ifdef COM_DEBUG
777 	if (sacom_debug)
778 		comstatus(sc, "comioctl ");
779 #endif
780 
781 	return error;
782 }
783 
784 static inline void
785 sacom_schedrx(struct sacom_softc *sc)
786 {
787 
788 	sc->sc_rx_ready = 1;
789 
790 	/* Wake up the poller. */
791 	softint_schedule(sc->sc_si);
792 }
793 
794 void
795 sacom_break(struct sacom_softc *sc, int onoff)
796 {
797 
798 	if (onoff)
799 		SET(sc->sc_cr3, CR3_BRK);
800 	else
801 		CLR(sc->sc_cr3, CR3_BRK);
802 
803 	if (!sc->sc_heldchange) {
804 		if (sc->sc_tx_busy) {
805 			sc->sc_heldtbc = sc->sc_tbc;
806 			sc->sc_tbc = 0;
807 			sc->sc_heldchange = 1;
808 		} else
809 			sacom_loadchannelregs(sc);
810 	}
811 }
812 
813 void
814 sacom_modem(struct sacom_softc *sc, int onoff)
815 {
816 	if (!sc->sc_heldchange) {
817 		if (sc->sc_tx_busy) {
818 			sc->sc_heldtbc = sc->sc_tbc;
819 			sc->sc_tbc = 0;
820 			sc->sc_heldchange = 1;
821 		} else
822 			sacom_loadchannelregs(sc);
823 	}
824 }
825 
826 void
827 tiocm_to_sacom(struct sacom_softc *sc, u_long how, int ttybits)
828 {
829 }
830 
831 int
832 sacom_to_tiocm(struct sacom_softc *sc)
833 {
834 	int ttybits = 0;
835 
836 	if (sc->sc_cr3 != 0)
837 		SET(ttybits, TIOCM_LE);
838 
839 	return ttybits;
840 }
841 
842 static u_int
843 cflag2cr0(tcflag_t cflag)
844 {
845 	u_int cr0;
846 
847 	cr0  = (cflag & PARENB) ? CR0_PE : 0;
848 	cr0 |= (cflag & PARODD) ? 0 : CR0_OES;
849 	cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0;
850 	cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0;
851 
852 	return cr0;
853 }
854 
855 int
856 sacomparam(struct tty *tp, struct termios *t)
857 {
858 	struct sacom_softc *sc =
859 		device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
860 	int ospeed = SACOMSPEED(t->c_ospeed);
861 	u_int cr0;
862 	int s;
863 
864 	if (COM_ISALIVE(sc) == 0)
865 		return EIO;
866 
867 	/* Check requested parameters. */
868 	if (ospeed < 0)
869 		return EINVAL;
870 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
871 		return EINVAL;
872 
873 	/*
874 	 * For the console, always force CLOCAL and !HUPCL, so that the port
875 	 * is always active.
876 	 */
877 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
878 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
879 		SET(t->c_cflag, CLOCAL);
880 		CLR(t->c_cflag, HUPCL);
881 	}
882 
883 	/*
884 	 * If there were no changes, don't do anything.  This avoids dropping
885 	 * input and improves performance when all we did was frob things like
886 	 * VMIN and VTIME.
887 	 */
888 	if (tp->t_ospeed == t->c_ospeed &&
889 	    tp->t_cflag == t->c_cflag)
890 		return 0;
891 
892 	cr0 = cflag2cr0(t->c_cflag);
893 
894 	s = splserial();
895 	COM_LOCK(sc);
896 
897 	sc->sc_cr0 = cr0;
898 
899 	sc->sc_speed = ospeed;
900 
901 	/* And copy to tty. */
902 	tp->t_ispeed = 0;
903 	tp->t_ospeed = t->c_ospeed;
904 	tp->t_cflag = t->c_cflag;
905 
906 	if (!sc->sc_heldchange) {
907 		if (sc->sc_tx_busy) {
908 			sc->sc_heldtbc = sc->sc_tbc;
909 			sc->sc_tbc = 0;
910 			sc->sc_heldchange = 1;
911 		} else
912 			sacom_loadchannelregs(sc);
913 	}
914 
915 	if (!ISSET(t->c_cflag, CHWFLOW)) {
916 		/* Disable the high water mark. */
917 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
918 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
919 			sacom_schedrx(sc);
920 		}
921 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) {
922 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED);
923 			sacom_hwiflow(sc);
924 		}
925 	}
926 
927 	COM_UNLOCK(sc);
928 	splx(s);
929 
930 	(void) (*tp->t_linesw->l_modem)(tp, 1);
931 
932 #ifdef COM_DEBUG
933 	if (sacom_debug)
934 		comstatus(sc, "comparam ");
935 #endif
936 
937 	return 0;
938 }
939 
940 void
941 sacom_iflush(struct sacom_softc *sc)
942 {
943 	bus_space_tag_t iot = sc->sc_iot;
944 	bus_space_handle_t ioh = sc->sc_ioh;
945 #ifdef DIAGNOSTIC
946 	int reg;
947 #endif
948 	int timo;
949 
950 #ifdef DIAGNOSTIC
951 	reg = 0xffff;
952 #endif
953 	timo = 50;
954 	/* flush any pending I/O */
955 	if (sc->sc_cr3 & CR3_RXE) {
956 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE)
957 	    && --timo)
958 #ifdef DIAGNOSTIC
959 		reg =
960 #else
961 		    (void)
962 #endif
963 		    bus_space_read_4(iot, ioh, SACOM_DR);
964 	}
965 #if 0
966 	/* XXX is it good idea to wait TX finish? */
967 	if (sc->sc_cr3 & CR3_TXE) {
968 	timo = 500;
969 	while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY)
970 	    && --timo)
971 		delay(100);
972 	}
973 #endif
974 #ifdef DIAGNOSTIC
975 	if (!timo)
976 		printf("%s: sacom_iflush timeout %02x\n", sc->sc_dev.dv_xname,
977 		       reg);
978 #endif
979 }
980 
981 void
982 sacom_loadchannelregs(struct sacom_softc *sc)
983 {
984 	bus_space_tag_t iot = sc->sc_iot;
985 	bus_space_handle_t ioh = sc->sc_ioh;
986 
987 	/* XXXXX necessary? */
988 	sacom_iflush(sc);
989 
990 	/* Need to stop engines first. */
991 	bus_space_write_4(iot, ioh, SACOM_CR3, 0);
992 
993 	bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8);
994 	bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff);
995 
996 	bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0);
997 	bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
998 }
999 
1000 int
1001 sacomhwiflow(struct tty *tp, int block)
1002 {
1003 #if 0
1004 	struct sacom_softc *sc =
1005 		device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
1006 	int s;
1007 
1008 	if (COM_ISALIVE(sc) == 0)
1009 		return 0;
1010 
1011 	if (sc->sc_mcr_rts == 0)
1012 		return 0;
1013 
1014 	s = splserial();
1015 	COM_LOCK(sc);
1016 
1017 	if (block) {
1018 		if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1019 			SET(sc->sc_rx_flags, RX_TTY_BLOCKED);
1020 			sacom_hwiflow(sc);
1021 		}
1022 	} else {
1023 		if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) {
1024 			CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1025 			sacom_schedrx(sc);
1026 		}
1027 		if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1028 			CLR(sc->sc_rx_flags, RX_TTY_BLOCKED);
1029 			sacom_hwiflow(sc);
1030 		}
1031 	}
1032 
1033 	COM_UNLOCK(sc);
1034 	splx(s);
1035 #endif
1036 	return 1;
1037 }
1038 
1039 /*
1040  * (un)block input via hw flowcontrol
1041  */
1042 void
1043 sacom_hwiflow(struct sacom_softc *sc)
1044 {
1045 #if 0
1046 	bus_space_tag_t iot = sc->sc_iot;
1047 	bus_space_handle_t ioh = sc->sc_ioh;
1048 
1049 	/* XXX implement */
1050 #endif
1051 }
1052 
1053 
1054 void
1055 sacomstart(struct tty *tp)
1056 {
1057 	struct sacom_softc *sc =
1058 		device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
1059 	bus_space_tag_t iot = sc->sc_iot;
1060 	bus_space_handle_t ioh = sc->sc_ioh;
1061 	int s;
1062 
1063 	if (COM_ISALIVE(sc) == 0)
1064 		return;
1065 
1066 	s = spltty();
1067 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
1068 		goto out;
1069 	if (!ttypull(tp))
1070 		goto out;
1071 
1072 	/* Grab the first contiguous region of buffer space. */
1073 	{
1074 		u_char *tba;
1075 		int tbc;
1076 
1077 		tba = tp->t_outq.c_cf;
1078 		tbc = ndqb(&tp->t_outq, 0);
1079 
1080 		(void)splserial();
1081 		COM_LOCK(sc);
1082 
1083 		sc->sc_tba = tba;
1084 		sc->sc_tbc = tbc;
1085 	}
1086 
1087 	SET(tp->t_state, TS_BUSY);
1088 	sc->sc_tx_busy = 1;
1089 
1090 	/* Enable transmit completion interrupts if necessary. */
1091 	if (!ISSET(sc->sc_cr3, CR3_TIE)) {
1092 		SET(sc->sc_cr3, CR3_TIE);
1093 		bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1094 	}
1095 
1096 	/* Output the first chunk of the contiguous buffer. */
1097 	sacom_filltx(sc);
1098 
1099 	COM_UNLOCK(sc);
1100 out:
1101 	splx(s);
1102 	return;
1103 }
1104 
1105 void
1106 sacom_filltx(struct sacom_softc *sc)
1107 {
1108 	bus_space_tag_t iot = sc->sc_iot;
1109 	bus_space_handle_t ioh = sc->sc_ioh;
1110 	int c, n;
1111 
1112 	n = 0;
1113 	while (bus_space_read_4(iot, ioh, SACOM_SR1) & SR1_TNF) {
1114 		if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc)
1115 			break;
1116 		c = *(sc->sc_tba + n);
1117 		c &= 0xff;
1118 		bus_space_write_4(iot, ioh, SACOM_DR, c);
1119 		n++;
1120 	}
1121 	sc->sc_tbc -= n;
1122 	sc->sc_tba += n;
1123 }
1124 
1125 /*
1126  * Stop output on a line.
1127  */
1128 void
1129 sacomstop(struct tty *tp, int flag)
1130 {
1131 	struct sacom_softc *sc =
1132 		device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev));
1133 	int s;
1134 
1135 	s = splserial();
1136 	COM_LOCK(sc);
1137 	if (ISSET(tp->t_state, TS_BUSY)) {
1138 		/* Stop transmitting at the next chunk. */
1139 		sc->sc_tbc = 0;
1140 		sc->sc_heldtbc = 0;
1141 		if (!ISSET(tp->t_state, TS_TTSTOP))
1142 			SET(tp->t_state, TS_FLUSH);
1143 	}
1144 	COM_UNLOCK(sc);
1145 	splx(s);
1146 }
1147 
1148 static inline void
1149 sacom_rxsoft(struct sacom_softc *sc, struct tty *tp)
1150 {
1151 	int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
1152 	u_char *get, *end;
1153 	u_int cc, scc;
1154 	u_char sr1;
1155 	int code;
1156 	int s;
1157 
1158 	end = sc->sc_ebuf;
1159 	get = sc->sc_rbget;
1160 	scc = cc = SACOM_RING_SIZE - sc->sc_rbavail;
1161 
1162 	while (cc) {
1163 		code = get[0];
1164 		sr1 = get[1];
1165 		if (ISSET(sr1, SR1_FRE))
1166 			SET(code, TTY_FE);
1167 		if (ISSET(sr1, SR1_PRE))
1168 			SET(code, TTY_PE);
1169 		if ((*rint)(code, tp) == -1) {
1170 			/*
1171 			 * The line discipline's buffer is out of space.
1172 			 */
1173 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1174 				/*
1175 				 * We're either not using flow control, or the
1176 				 * line discipline didn't tell us to block for
1177 				 * some reason.  Either way, we have no way to
1178 				 * know when there's more space available, so
1179 				 * just drop the rest of the data.
1180 				 */
1181 				get += cc << 1;
1182 				if (get >= end)
1183 					get -= SACOM_RING_SIZE << 1;
1184 				cc = 0;
1185 			} else {
1186 				/*
1187 				 * Don't schedule any more receive processing
1188 				 * until the line discipline tells us there's
1189 				 * space available (through comhwiflow()).
1190 				 * Leave the rest of the data in the input
1191 				 * buffer.
1192 				 */
1193 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1194 			}
1195 			break;
1196 		}
1197 		get += 2;
1198 		if (get >= end)
1199 			get = sc->sc_rbuf;
1200 		cc--;
1201 	}
1202 
1203 	if (cc != scc) {
1204 		sc->sc_rbget = get;
1205 		s = splserial();
1206 		COM_LOCK(sc);
1207 
1208 		cc = sc->sc_rbavail += scc - cc;
1209 		/* Buffers should be ok again, release possible block. */
1210 		if (cc >= 1) {
1211 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1212 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1213 				SET(sc->sc_cr3, CR3_RIE);
1214 				bus_space_write_4(sc->sc_iot, sc->sc_ioh,
1215 						  SACOM_CR3, sc->sc_cr3);
1216 			}
1217 		}
1218 		COM_UNLOCK(sc);
1219 		splx(s);
1220 	}
1221 }
1222 
1223 static inline void
1224 sacom_txsoft(struct sacom_softc *sc, struct tty *tp)
1225 {
1226 
1227 	CLR(tp->t_state, TS_BUSY);
1228 	if (ISSET(tp->t_state, TS_FLUSH))
1229 		CLR(tp->t_state, TS_FLUSH);
1230 	else
1231 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
1232 	(*tp->t_linesw->l_start)(tp);
1233 }
1234 
1235 static inline void
1236 sacom_stsoft(struct sacom_softc *sc, struct tty *tp)
1237 {
1238 }
1239 
1240 void
1241 sacomsoft(void *arg)
1242 {
1243 	struct sacom_softc *sc = arg;
1244 	struct tty *tp;
1245 
1246 	if (COM_ISALIVE(sc) == 0)
1247 		return;
1248 
1249 	tp = sc->sc_tty;
1250 
1251 	if (sc->sc_rx_ready) {
1252 		sc->sc_rx_ready = 0;
1253 		sacom_rxsoft(sc, tp);
1254 	}
1255 
1256 	if (sc->sc_st_check) {
1257 		sc->sc_st_check = 0;
1258 		sacom_stsoft(sc, tp);
1259 	}
1260 
1261 	if (sc->sc_tx_done) {
1262 		sc->sc_tx_done = 0;
1263 		sacom_txsoft(sc, tp);
1264 	}
1265 }
1266 
1267 int
1268 sacomintr(void *arg)
1269 {
1270 	struct sacom_softc *sc = arg;
1271 	bus_space_tag_t iot = sc->sc_iot;
1272 	bus_space_handle_t ioh = sc->sc_ioh;
1273 	u_char *put, *end;
1274 	u_int cc;
1275 	u_int sr0, sr1;
1276 
1277 	if (COM_ISALIVE(sc) == 0)
1278 		return 0;
1279 
1280 	COM_LOCK(sc);
1281 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
1282 	if (!sr0) {
1283 		COM_UNLOCK(sc);
1284 		return 0;
1285 	}
1286 	if (ISSET(sr0, SR0_EIF))
1287 		/* XXX silently discard error bits */
1288 		bus_space_read_4(iot, ioh, SACOM_DR);
1289 	if (ISSET(sr0, SR0_RBB))
1290 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB);
1291 	if (ISSET(sr0, SR0_REB)) {
1292 		bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB);
1293 #if defined(DDB) || defined(KGDB)
1294 #ifndef DDB_BREAK_CHAR
1295 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1296 			console_debugger();
1297 		}
1298 #endif
1299 #endif /* DDB || KGDB */
1300 	}
1301 
1302 
1303 	end = sc->sc_ebuf;
1304 	put = sc->sc_rbput;
1305 	cc = sc->sc_rbavail;
1306 
1307 	sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1308 	if (ISSET(sr0, SR0_RFS | SR0_RID)) {
1309 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1310 			while (cc > 0) {
1311 				if (!ISSET(sr1, SR1_RNE)) {
1312 					bus_space_write_4(iot, ioh, SACOM_SR0,
1313 							  SR0_RID);
1314 					break;
1315 				}
1316 				put[0] = bus_space_read_4(iot, ioh, SACOM_DR);
1317 				put[1] = sr1;
1318 #if defined(DDB) && defined(DDB_BREAK_CHAR)
1319 				if (put[0] == DDB_BREAK_CHAR &&
1320 				    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
1321 					console_debugger();
1322 
1323 					sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1324 					continue;
1325 				}
1326 #endif
1327 				put += 2;
1328 				if (put >= end)
1329 					put = sc->sc_rbuf;
1330 				cc--;
1331 
1332 				sr1 = bus_space_read_4(iot, ioh, SACOM_SR1);
1333 			}
1334 
1335 			/*
1336 			 * Current string of incoming characters ended because
1337 			 * no more data was available or we ran out of space.
1338 			 * Schedule a receive event if any data was received.
1339 			 * If we're out of space, turn off receive interrupts.
1340 			 */
1341 			sc->sc_rbput = put;
1342 			sc->sc_rbavail = cc;
1343 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1344 				sc->sc_rx_ready = 1;
1345 
1346 			/* XXX do RX hardware flow control */
1347 
1348 			/*
1349 			 * If we're out of space, disable receive interrupts
1350 			 * until the queue has drained a bit.
1351 			 */
1352 			if (!cc) {
1353 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1354 				CLR(sc->sc_cr3, CR3_RIE);
1355 				bus_space_write_4(iot, ioh, SACOM_CR3,
1356 						  sc->sc_cr3);
1357 			}
1358 		} else {
1359 #ifdef DIAGNOSTIC
1360 			panic("sacomintr: we shouldn't reach here");
1361 #endif
1362 			CLR(sc->sc_cr3, CR3_RIE);
1363 			bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3);
1364 		}
1365 	}
1366 
1367 	/*
1368 	 * Done handling any receive interrupts. See if data can be
1369 	 * transmitted as well. Schedule tx done event if no data left
1370 	 * and tty was marked busy.
1371 	 */
1372 	sr0 = bus_space_read_4(iot, ioh, SACOM_SR0);
1373 	if (ISSET(sr0, SR0_TFS)) {
1374 		/*
1375 		 * If we've delayed a parameter change, do it now, and restart
1376 		 * output.
1377 		 * XXX sacom_loadchannelregs() waits TX completion,
1378 		 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case
1379 		 */
1380 		if (sc->sc_heldchange) {
1381 			sacom_loadchannelregs(sc);
1382 			sc->sc_heldchange = 0;
1383 			sc->sc_tbc = sc->sc_heldtbc;
1384 			sc->sc_heldtbc = 0;
1385 		}
1386 
1387 		/* Output the next chunk of the contiguous buffer, if any. */
1388 		if (sc->sc_tbc > 0) {
1389 			sacom_filltx(sc);
1390 		} else {
1391 			/* Disable transmit completion interrupts if necessary. */
1392 			if (ISSET(sc->sc_cr3, CR3_TIE)) {
1393 				CLR(sc->sc_cr3, CR3_TIE);
1394 				bus_space_write_4(iot, ioh, SACOM_CR3,
1395 						  sc->sc_cr3);
1396 			}
1397 			if (sc->sc_tx_busy) {
1398 				sc->sc_tx_busy = 0;
1399 				sc->sc_tx_done = 1;
1400 			}
1401 		}
1402 	}
1403 	COM_UNLOCK(sc);
1404 
1405 	/* Wake up the poller. */
1406 	softint_schedule(sc->sc_si);
1407 
1408 #if NRND > 0 && defined(RND_COM)
1409 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
1410 #endif
1411 	return 1;
1412 }
1413 
1414 static void
1415 sacom_j720_init(struct sa11x0_softc *parent, struct sacom_softc *sc) {
1416 
1417 	/* XXX  this should be done at sc->enable function */
1418 	bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
1419 	    SAGPIO_PCR, 0xa0000);
1420 	bus_space_write_4(parent->sc_iot, parent->sc_gpioh,
1421 	    SAGPIO_PSR, 0x100);
1422 }
1423 
1424 /* Initialization for serial console */
1425 int
1426 sacominit(bus_space_tag_t iot, bus_addr_t iobase, int baud, tcflag_t cflag,
1427     bus_space_handle_t *iohp)
1428 {
1429 	int brd, cr0;
1430 
1431 	if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp))
1432 		printf("register map failed\n");
1433 
1434 	/* wait for the Tx queue to drain and disable the UART */
1435 	while (bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY)
1436 		continue;
1437 	bus_space_write_4(iot, *iohp, SACOM_CR3, 0);
1438 
1439 	cr0  = cflag2cr0(cflag);
1440 	bus_space_write_4(iot, *iohp, SACOM_CR0, cr0);
1441 
1442 	brd = SACOMSPEED(baud);
1443 	sacomconsrate = baud;
1444 	sacomconsaddr = iobase;
1445 	sacomconscflag = cflag;
1446 	/* XXX assumes little endian */
1447 	bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8);
1448 	bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff);
1449 
1450 	/* enable the UART */
1451 	bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE);
1452 
1453 	return 0;
1454 }
1455 
1456 void
1457 sacomcnprobe(struct consdev *cp)
1458 {
1459 	cp->cn_pri = CN_REMOTE;
1460 }
1461 
1462 void
1463 sacomcninit(struct consdev *cp)
1464 {
1465 	if (cp == NULL) {
1466 		/* XXX cp == NULL means that MMU is disabled. */
1467 		sacomconsioh = SACOM3_BASE;
1468 		sacomconstag = &sa11x0_bs_tag;
1469 		cn_tab = &sacomcons;
1470 		return;
1471 	}
1472 
1473 	if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED,
1474 			  CONMODE, &sacomconsioh))
1475 		panic("can't init serial console @%x", CONADDR);
1476 	cn_tab = &sacomcons;
1477 	sacomconstag = &sa11x0_bs_tag;
1478 }
1479 
1480 int
1481 sacomcngetc(dev_t dev)
1482 {
1483 	int c, s;
1484 
1485 	s = spltty();	/* XXX do we need this? */
1486 
1487 	while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1488 		 & SR1_RNE)) {
1489 #if defined(DDB) || defined(KGDB)
1490 #ifndef DDB_BREAK_CHAR
1491 		u_int sr0;
1492 		extern int db_active;
1493 
1494 		sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0);
1495 		if (ISSET(sr0, SR0_RBB))
1496 			bus_space_write_4(sacomconstag, sacomconsioh,
1497 					  SACOM_SR0, SR0_RBB);
1498 		if (ISSET(sr0, SR0_REB)) {
1499 			bus_space_write_4(sacomconstag, sacomconsioh,
1500 					  SACOM_SR0, SR0_REB);
1501 			if (db_active == 0)
1502 				console_debugger();
1503 		}
1504 #endif
1505 #endif /* DDB || KGDB */
1506 	}
1507 
1508 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
1509 	c &= 0xff;
1510 	splx(s);
1511 
1512 	return c;
1513 }
1514 
1515 void
1516 sacomcnputc(dev_t dev, int c)
1517 {
1518 	int s;
1519 
1520 	s = spltty();	/* XXX do we need this? */
1521 
1522 	while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1523 	    & SR1_TNF))
1524 		continue;
1525 
1526 	bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c);
1527 	splx(s);
1528 }
1529 
1530 void
1531 sacomcnpollc(dev_t dev, int on)
1532 {
1533 
1534 }
1535 
1536 #if 0
1537 #ifdef DEBUG
1538 int
1539 sacomcncharpoll(void)
1540 {
1541 	int c;
1542 
1543 	if (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1)
1544 		 & SR1_RNE))
1545 		return -1;
1546 
1547 	c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR);
1548 	c &= 0xff;
1549 
1550 	return c;
1551 }
1552 #endif
1553 #endif
1554 
1555 /* helper function to identify the com ports used by
1556  console or KGDB (and not yet autoconf attached) */
1557 int
1558 sacom_is_console(bus_space_tag_t iot, bus_addr_t iobase,
1559     bus_space_handle_t *ioh)
1560 {
1561 	bus_space_handle_t help;
1562 
1563 	if (!sacomconsattached &&
1564 	    iot == sacomconstag && iobase == sacomconsaddr)
1565 		help = sacomconsioh;
1566 	else
1567 		return 0;
1568 
1569 	if (ioh)
1570 		*ioh = help;
1571 	return 1;
1572 }
1573