xref: /netbsd/sys/arch/arm/ixp12x0/ixp12x0_com.c (revision c4a72b64)
1 /*	$NetBSD: ixp12x0_com.c,v 1.9 2002/12/03 09:28:27 ichiro Exp $ */
2 /*
3  * Copyright (c) 1998, 1999, 2001, 2002 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Ichiro FUKUHARA and Naoto Shimazaki.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by IWAMOTO Toshihiro.
11  *
12  * This code is derived from software contributed to The NetBSD Foundation
13  * by Charles M. Hannum.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *        This product includes software developed by the NetBSD
26  *        Foundation, Inc. and its contributors.
27  * 4. Neither the name of The NetBSD Foundation nor the names of its
28  *    contributors may be used to endorse or promote products derived
29  *    from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
32  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
33  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
34  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
35  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
36  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
39  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  */
43 
44 /*
45  * Copyright (c) 1991 The Regents of the University of California.
46  * All rights reserved.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. All advertising materials mentioning features or use of this software
57  *    must display the following acknowledgement:
58  *      This product includes software developed by the University of
59  *      California, Berkeley and its contributors.
60  * 4. Neither the name of the University nor the names of its contributors
61  *    may be used to endorse or promote products derived from this software
62  *    without specific prior written permission.
63  *
64  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
65  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
66  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
67  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
68  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
69  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
70  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
72  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
73  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
74  * SUCH DAMAGE.
75  *
76  *      @(#)com.c       7.5 (Berkeley) 5/16/91
77  */
78 
79 
80 #include "opt_com.h"
81 #include "opt_ddb.h"
82 #include "opt_kgdb.h"
83 
84 #include "rnd.h"
85 #if NRND > 0 && defined(RND_COM)
86 #include <sys/rnd.h>
87 #endif
88 
89 #include <sys/param.h>
90 #include <sys/systm.h>
91 #include <sys/types.h>
92 #include <sys/conf.h>
93 #include <sys/file.h>
94 #include <sys/device.h>
95 #include <sys/kernel.h>
96 #include <sys/malloc.h>
97 #include <sys/tty.h>
98 #include <sys/uio.h>
99 #include <sys/vnode.h>
100 
101 #include <machine/intr.h>
102 #include <machine/bus.h>
103 
104 #include <arm/ixp12x0/ixp12x0_comreg.h>
105 #include <arm/ixp12x0/ixp12x0_comvar.h>
106 #include <arm/ixp12x0/ixp12x0reg.h>
107 #include <arm/ixp12x0/ixp12x0var.h>
108 
109 #include <arm/ixp12x0/ixpsipvar.h>
110 
111 #include <dev/cons.h>
112 #include "ixpcom.h"
113 
114 static int	ixpcomparam(struct tty *, struct termios *);
115 static void	ixpcomstart(struct tty *);
116 static int	ixpcomhwiflow(struct tty *, int);
117 
118 static u_int	cflag2cr(tcflag_t);
119 static void	ixpcom_iflush(struct ixpcom_softc *);
120 static void	ixpcom_set_cr(struct ixpcom_softc *);
121 
122 int             ixpcomcngetc(dev_t);
123 void            ixpcomcnputc(dev_t, int);
124 void            ixpcomcnpollc(dev_t, int);
125 
126 static void	ixpcomsoft(void* arg);
127 inline static void	ixpcom_txsoft(struct ixpcom_softc *, struct tty *);
128 inline static void	ixpcom_rxsoft(struct ixpcom_softc *, struct tty *);
129 
130 void            ixpcomcnprobe(struct consdev *);
131 void            ixpcomcninit(struct consdev *);
132 
133 u_int32_t	ixpcom_cr = 0;		/* tell cr to *_intr.c */
134 u_int32_t	ixpcom_imask = 0;	/* intrrupt mask from *_intr.c */
135 
136 static bus_space_tag_t ixpcomconstag;
137 static bus_space_handle_t ixpcomconsioh;
138 static bus_addr_t ixpcomconsaddr = IXPCOM_UART_BASE; /* XXX initial value */
139 
140 static int ixpcomconsattached;
141 static int ixpcomconsrate;
142 static tcflag_t ixpcomconscflag;
143 static struct cnm_state ixpcom_cnm_state;
144 
145 struct ixpcom_softc* ixpcom_sc = NULL;
146 
147 extern struct cfdriver ixpcom_cd;
148 
149 dev_type_open(ixpcomopen);
150 dev_type_close(ixpcomclose);
151 dev_type_read(ixpcomread);
152 dev_type_write(ixpcomwrite);
153 dev_type_stop(ixpcomstop);
154 dev_type_tty(ixpcomtty);
155 dev_type_poll(ixpcompoll);
156 
157 const struct cdevsw ixpcom_cdevsw = {
158 	ixpcomopen, ixpcomclose, ixpcomread, ixpcomwrite, nullioctl,
159 	ixpcomstop, ixpcomtty, ixpcompoll, nommap, ttykqfilter, D_TTY
160 };
161 
162 struct consdev ixpcomcons = {
163 	NULL, NULL, ixpcomcngetc, ixpcomcnputc, ixpcomcnpollc, NULL,
164 	NODEV, CN_NORMAL
165 };
166 
167 #ifndef DEFAULT_COMSPEED
168 #define DEFAULT_COMSPEED 38400
169 #endif
170 
171 #define COMUNIT_MASK    0x7ffff
172 #define COMDIALOUT_MASK 0x80000
173 
174 #define COMUNIT(x)	(minor(x) & COMUNIT_MASK)
175 #define COMDIALOUT(x)	(minor(x) & COMDIALOUT_MASK)
176 
177 #define COM_ISALIVE(sc)	((sc)->enabled != 0 && \
178 			ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE))
179 
180 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f))
181 
182 #define	COM_LOCK(sc);
183 #define	COM_UNLOCK(sc);
184 
185 #define SET(t, f)	(t) |= (f)
186 #define CLR(t, f)	(t) &= ~(f)
187 #define ISSET(t, f)	((t) & (f))
188 
189 #define CFLAGS2CR_MASK	(CR_PE | CR_OES | CR_SBS | CR_DSS | CR_BRD)
190 
191 void
192 ixpcom_attach_subr(sc)
193 	struct ixpcom_softc *sc;
194 {
195 	bus_addr_t iobase = sc->sc_baseaddr;
196 	bus_space_tag_t iot = sc->sc_iot;
197 	struct tty *tp;
198 
199 	ixpcom_sc = sc;
200 
201 	if (iot == ixpcomconstag && iobase == ixpcomconsaddr) {
202 		ixpcomconsattached = 1;
203 		sc->sc_speed = IXPCOMSPEED2BRD(ixpcomconsrate);
204 
205 		/* Make sure the console is always "hardwired". */
206 		delay(10000);	/* wait for output to finish */
207 		SET(sc->sc_hwflags, COM_HW_CONSOLE);
208 		SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
209 	}
210 
211 	tp = ttymalloc();
212 	tp->t_oproc = ixpcomstart;
213 	tp->t_param = ixpcomparam;
214 	tp->t_hwiflow = ixpcomhwiflow;
215 
216 	sc->sc_tty = tp;
217 	sc->sc_rbuf = malloc(IXPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
218 	sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
219 	sc->sc_rbavail = IXPCOM_RING_SIZE;
220 	if (sc->sc_rbuf == NULL) {
221 		printf("%s: unable to allocate ring buffer\n",
222 		    sc->sc_dev.dv_xname);
223 		return;
224 	}
225 	sc->sc_ebuf = sc->sc_rbuf + (IXPCOM_RING_SIZE << 1);
226 	sc->sc_tbc = 0;
227 
228 	sc->sc_rie = sc->sc_xie = 0;
229 	ixpcom_cr = IXPCOMSPEED2BRD(DEFAULT_COMSPEED)
230 		| CR_UE | sc->sc_rie | sc->sc_xie | DSS_8BIT;
231 
232 	tty_attach(tp);
233 
234 	if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
235 		int maj;
236 
237 		/* locate the major number */
238 		maj = cdevsw_lookup_major(&ixpcom_cdevsw);
239 
240 		cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
241 
242 		delay(10000); /* XXX */
243 		printf("%s: console\n", sc->sc_dev.dv_xname);
244 		delay(10000); /* XXX */
245 	}
246 
247 	sc->sc_si = softintr_establish(IPL_SOFTSERIAL, ixpcomsoft, sc);
248 
249 #if NRND > 0 && defined(RND_COM)
250 	rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
251 			  RND_TYPE_TTY, 0);
252 #endif
253 
254 	/* if there are no enable/disable functions, assume the device
255 	   is always enabled */
256 	if (!sc->enable)
257 		sc->enabled = 1;
258 
259 	/* XXX configure register */
260 	/* xxx_config(sc) */
261 
262 	SET(sc->sc_hwflags, COM_HW_DEV_OK);
263 }
264 
265 static int
266 ixpcomparam(tp, t)
267 	struct tty *tp;
268 	struct termios *t;
269 {
270 	struct ixpcom_softc *sc
271 		= device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
272 	u_int32_t cr;
273 	int s;
274 
275 	if (COM_ISALIVE(sc) == 0)
276 		return (EIO);
277 
278 	cr = IXPCOMSPEED2BRD(t->c_ospeed);
279 
280 	if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
281 		return (EINVAL);
282 
283 	/*
284 	 * For the console, always force CLOCAL and !HUPCL, so that the port
285 	 * is always active.
286 	 */
287 	if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
288 	    ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
289 		SET(t->c_cflag, CLOCAL);
290 		CLR(t->c_cflag, HUPCL);
291 	}
292 
293 	/*
294 	 * If there were no changes, don't do anything.  This avoids dropping
295 	 * input and improves performance when all we did was frob things like
296 	 * VMIN and VTIME.
297 	 */
298 	if (tp->t_ospeed == t->c_ospeed &&
299 	    tp->t_cflag == t->c_cflag)
300 		return (0);
301 
302 	cr |= cflag2cr(t->c_cflag);
303 
304 	s = splserial();
305 	COM_LOCK(sc);
306 
307 	ixpcom_cr = (ixpcom_cr & ~CFLAGS2CR_MASK) | cr;
308 
309 	/*
310 	 * ixpcom don't have any hardware flow control.
311 	 * we skip it.
312 	 */
313 
314 	/* And copy to tty. */
315 	tp->t_ispeed = 0;
316 	tp->t_ospeed = t->c_ospeed;
317 	tp->t_cflag = t->c_cflag;
318 
319 	if (!sc->sc_heldchange) {
320 		if (sc->sc_tx_busy) {
321 			sc->sc_heldtbc = sc->sc_tbc;
322 			sc->sc_tbc = 0;
323 			sc->sc_heldchange = 1;
324 		} else
325 			ixpcom_set_cr(sc);
326 	}
327 
328 	COM_UNLOCK(sc);
329 	splx(s);
330 
331 	/*
332 	 * Update the tty layer's idea of the carrier bit.
333 	 * We tell tty the carrier is always on.
334 	 */
335 	(void) (*tp->t_linesw->l_modem)(tp, 1);
336 
337 #ifdef COM_DEBUG
338 	if (com_debug)
339 		comstatus(sc, "comparam ");
340 #endif
341 
342 	if (!ISSET(t->c_cflag, CHWFLOW)) {
343 		if (sc->sc_tx_stopped) {
344 			sc->sc_tx_stopped = 0;
345 			ixpcomstart(tp);
346 		}
347 	}
348 
349 	return (0);
350 }
351 
352 static int
353 ixpcomhwiflow(tp, block)
354 	struct tty *tp;
355 	int block;
356 {
357 	return (0);
358 }
359 
360 static void
361 ixpcom_filltx(struct ixpcom_softc *sc)
362 {
363 	bus_space_tag_t iot = sc->sc_iot;
364 	bus_space_handle_t ioh = sc->sc_ioh;
365 	int n;
366 
367 	n = 0;
368         while (bus_space_read_4(iot, ioh, IXPCOM_SR) & SR_TXR) {
369 		if (n >= sc->sc_tbc)
370 			break;
371 		bus_space_write_4(iot, ioh, IXPCOM_DR,
372 				  0xff & *(sc->sc_tba + n));
373 		n++;
374         }
375         sc->sc_tbc -= n;
376         sc->sc_tba += n;
377 }
378 
379 static void
380 ixpcomstart(tp)
381 	struct tty *tp;
382 {
383 	struct ixpcom_softc *sc
384 		= device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
385 	int s;
386 
387 	if (COM_ISALIVE(sc) == 0)
388 		return;
389 
390 	s = spltty();
391 	if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
392 		goto out;
393 	if (sc->sc_tx_stopped)
394 		goto out;
395 
396 	if (tp->t_outq.c_cc <= tp->t_lowat) {
397 		if (ISSET(tp->t_state, TS_ASLEEP)) {
398 			CLR(tp->t_state, TS_ASLEEP);
399 			wakeup(&tp->t_outq);
400 		}
401 		selwakeup(&tp->t_wsel);
402 		if (tp->t_outq.c_cc == 0)
403 			goto out;
404 	}
405 
406 	/* Grab the first contiguous region of buffer space. */
407 	{
408 		u_char *tba;
409 		int tbc;
410 
411 		tba = tp->t_outq.c_cf;
412 		tbc = ndqb(&tp->t_outq, 0);
413 
414 		(void)splserial();
415 		COM_LOCK(sc);
416 
417 		sc->sc_tba = tba;
418 		sc->sc_tbc = tbc;
419 	}
420 
421 	SET(tp->t_state, TS_BUSY);
422 	sc->sc_tx_busy = 1;
423 
424 	/* Enable transmit completion interrupts if necessary. */
425 	if (!ISSET(sc->sc_xie, CR_XIE)) {
426 		SET(sc->sc_xie, CR_XIE);
427 		ixpcom_set_cr(sc);
428 	}
429 
430 	/* Output the first chunk of the contiguous buffer. */
431 	ixpcom_filltx(sc);
432 
433 	COM_UNLOCK(sc);
434 out:
435 	splx(s);
436 	return;
437 }
438 
439 static void
440 ixpcom_break(struct ixpcom_softc *sc, int onoff)
441 {
442 	if (onoff)
443 		SET(ixpcom_cr, CR_BRK);
444 	else
445 		CLR(ixpcom_cr, CR_BRK);
446 	if (!sc->sc_heldchange) {
447 		if (sc->sc_tx_busy) {
448 			sc->sc_heldtbc = sc->sc_tbc;
449 			sc->sc_tbc = 0;
450 			sc->sc_heldchange = 1;
451 		} else
452 			ixpcom_set_cr(sc);
453 	}
454 }
455 
456 static void
457 ixpcom_shutdown(struct ixpcom_softc *sc)
458 {
459 	int s;
460 
461 	s = splserial();
462 	COM_LOCK(sc);
463 
464 	/* Clear any break condition set with TIOCSBRK. */
465 	ixpcom_break(sc, 0);
466 
467 	/* Turn off interrupts. */
468 	sc->sc_rie = sc->sc_xie = 0;
469 	ixpcom_set_cr(sc);
470 
471 	if (sc->disable) {
472 #ifdef DIAGNOSTIC
473 		if (!sc->enabled)
474 			panic("ixpcom_shutdown: not enabled?");
475 #endif
476 		(*sc->disable)(sc);
477 		sc->enabled = 0;
478 	}
479 	COM_UNLOCK(sc);
480 	splx(s);
481 }
482 
483 int
484 ixpcomopen(dev, flag, mode, p)
485 	dev_t dev;
486 	int flag, mode;
487 	struct proc *p;
488 {
489 	struct ixpcom_softc *sc;
490 	struct tty *tp;
491 	int s, s2;
492 	int error;
493 
494 	sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
495 	if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
496 		sc->sc_rbuf == NULL)
497 		return (ENXIO);
498 
499 	if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
500 		return (ENXIO);
501 
502 #ifdef KGDB
503 	/*
504 	 * If this is the kgdb port, no other use is permitted.
505 	 */
506 	if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
507 		return (EBUSY);
508 #endif
509 
510 	tp = sc->sc_tty;
511 
512 	if (ISSET(tp->t_state, TS_ISOPEN) &&
513 	    ISSET(tp->t_state, TS_XCLUDE) &&
514 		p->p_ucred->cr_uid != 0)
515 		return (EBUSY);
516 
517 	s = spltty();
518 
519 	/*
520 	 * Do the following iff this is a first open.
521 	 */
522 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
523 		struct termios t;
524 
525 		tp->t_dev = dev;
526 
527 		s2 = splserial();
528 		COM_LOCK(sc);
529 
530 		if (sc->enable) {
531 			if ((*sc->enable)(sc)) {
532 				COM_UNLOCK(sc);
533 				splx(s2);
534 				splx(s);
535 				printf("%s: device enable failed\n",
536 				       sc->sc_dev.dv_xname);
537 				return (EIO);
538 			}
539 			sc->enabled = 1;
540 #if 0
541 /* XXXXXXXXXXXXXXX */
542 			com_config(sc);
543 #endif
544 		}
545 
546 		/* Turn on interrupts. */
547 		SET(sc->sc_rie, CR_RIE);
548 		ixpcom_set_cr(sc);
549 
550 #if 0
551 		/* Fetch the current modem control status, needed later. */
552 		sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
553 
554 		/* Clear PPS capture state on first open. */
555 		sc->sc_ppsmask = 0;
556 		sc->ppsparam.mode = 0;
557 #endif
558 
559 		COM_UNLOCK(sc);
560 		splx(s2);
561 
562 		/*
563 		 * Initialize the termios status to the defaults.  Add in the
564 		 * sticky bits from TIOCSFLAGS.
565 		 */
566 		t.c_ispeed = 0;
567 		if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
568 			t.c_ospeed = ixpcomconsrate;
569 			t.c_cflag = ixpcomconscflag;
570 		} else {
571 			t.c_ospeed = TTYDEF_SPEED;
572 			t.c_cflag = TTYDEF_CFLAG;
573 		}
574 		if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
575 			SET(t.c_cflag, CLOCAL);
576 		if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
577 			SET(t.c_cflag, CRTSCTS);
578 		if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
579 			SET(t.c_cflag, MDMBUF);
580 		/* Make sure ixpcomparam() will do something. */
581 		tp->t_ospeed = 0;
582 		(void) ixpcomparam(tp, &t);
583 		tp->t_iflag = TTYDEF_IFLAG;
584 		tp->t_oflag = TTYDEF_OFLAG;
585 		tp->t_lflag = TTYDEF_LFLAG;
586 		ttychars(tp);
587 		ttsetwater(tp);
588 
589 		s2 = splserial();
590 		COM_LOCK(sc);
591 
592 		/* Clear the input ring, and unblock. */
593 		sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
594 		sc->sc_rbavail = IXPCOM_RING_SIZE;
595 		ixpcom_iflush(sc);
596 		CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
597 
598 #ifdef COM_DEBUG
599 		if (ixpcom_debug)
600 			comstatus(sc, "ixpcomopen  ");
601 #endif
602 
603 		COM_UNLOCK(sc);
604 		splx(s2);
605 	}
606 
607 	splx(s);
608 
609 	error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
610 	if (error)
611 		goto bad;
612 
613 	error = (*tp->t_linesw->l_open)(dev, tp);
614 	if (error)
615 		goto bad;
616 
617 	return (0);
618 
619 bad:
620 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
621 		/*
622 		 * We failed to open the device, and nobody else had it opened.
623 		 * Clean up the state as appropriate.
624 		 */
625 		ixpcom_shutdown(sc);
626 	}
627 
628 	return (error);
629 }
630 
631 int
632 ixpcomclose(dev, flag, mode, p)
633 	dev_t dev;
634 	int flag, mode;
635 	struct proc *p;
636 {
637 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
638 	struct tty *tp = sc->sc_tty;
639 
640 	/* XXX This is for cons.c. */
641 	if (!ISSET(tp->t_state, TS_ISOPEN))
642 		return (0);
643 
644 	(*tp->t_linesw->l_close)(tp, flag);
645 	ttyclose(tp);
646 
647 	if (COM_ISALIVE(sc) == 0)
648 		return (0);
649 
650 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
651 		/*
652 		 * Although we got a last close, the device may still be in
653 		 * use; e.g. if this was the dialout node, and there are still
654 		 * processes waiting for carrier on the non-dialout node.
655 		 */
656 		ixpcom_shutdown(sc);
657 	}
658 
659 	return (0);
660 }
661 
662 int
663 ixpcomread(dev, uio, flag)
664 	dev_t dev;
665 	struct uio *uio;
666 	int flag;
667 {
668 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
669 	struct tty *tp = sc->sc_tty;
670 
671 	if (COM_ISALIVE(sc) == 0)
672 		return (EIO);
673 
674 	return ((*tp->t_linesw->l_read)(tp, uio, flag));
675 }
676 
677 int
678 ixpcomwrite(dev, uio, flag)
679 	dev_t dev;
680 	struct uio *uio;
681 	int flag;
682 {
683 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
684 	struct tty *tp = sc->sc_tty;
685 
686 	if (COM_ISALIVE(sc) == 0)
687 		return (EIO);
688 
689 	return ((*tp->t_linesw->l_write)(tp, uio, flag));
690 }
691 
692 int
693 ixpcompoll(dev, events, p)
694 	dev_t dev;
695 	int events;
696 	struct proc *p;
697 {
698 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
699 	struct tty *tp = sc->sc_tty;
700 
701 	if (COM_ISALIVE(sc) == 0)
702 		return (EIO);
703 
704 	return ((*tp->t_linesw->l_poll)(tp, events, p));
705 }
706 
707 struct tty *
708 ixpcomtty(dev)
709 	dev_t dev;
710 {
711 	struct ixpcom_softc *sc = device_lookup(&ixpcom_cd, COMUNIT(dev));
712 	struct tty *tp = sc->sc_tty;
713 
714 	return (tp);
715 }
716 
717 /*
718  * Stop output on a line.
719  */
720 void
721 ixpcomstop(tp, flag)
722 	struct tty *tp;
723 	int flag;
724 {
725 	struct ixpcom_softc *sc
726 		= device_lookup(&ixpcom_cd, COMUNIT(tp->t_dev));
727 	int s;
728 
729 	s = splserial();
730 	COM_LOCK(sc);
731 	if (ISSET(tp->t_state, TS_BUSY)) {
732 		/* Stop transmitting at the next chunk. */
733 		sc->sc_tbc = 0;
734 		sc->sc_heldtbc = 0;
735 		if (!ISSET(tp->t_state, TS_TTSTOP))
736 			SET(tp->t_state, TS_FLUSH);
737 	}
738 	COM_UNLOCK(sc);
739 	splx(s);
740 }
741 
742 static u_int
743 cflag2cr(cflag)
744 	tcflag_t cflag;
745 {
746 	u_int cr;
747 
748 	cr  = (cflag & PARENB) ? CR_PE : 0;
749 	cr |= (cflag & PARODD) ? CR_OES : 0;
750 	cr |= (cflag & CSTOPB) ? CR_SBS : 0;
751 	cr |= ((cflag & CSIZE) == CS8) ? DSS_8BIT : 0;
752 
753 	return (cr);
754 }
755 
756 static void
757 ixpcom_iflush(sc)
758 	struct ixpcom_softc *sc;
759 {
760 	bus_space_tag_t iot = sc->sc_iot;
761 	bus_space_handle_t ioh = sc->sc_ioh;
762 #ifdef DIAGNOSTIC
763 	int reg;
764 #endif
765 	int timo;
766 
767 #ifdef DIAGNOSTIC
768 	reg = 0xffff;
769 #endif
770 	timo = 50000;
771 	/* flush any pending I/O */
772 	while (ISSET(bus_space_read_4(iot, ioh, IXPCOM_SR), SR_RXR)
773 	       && --timo)
774 #ifdef DIAGNOSTIC
775 		reg =
776 #else
777 			(void)
778 #endif
779 			bus_space_read_4(iot, ioh, IXPCOM_DR);
780 #ifdef DIAGNOSTIC
781 	if (!timo)
782 		printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
783 		       reg);
784 #endif
785 }
786 
787 static void
788 ixpcom_set_cr(struct ixpcom_softc *sc)
789 {
790 	/* XXX */
791 	ixpcom_cr &= ~(CR_RIE | CR_XIE);
792 	ixpcom_cr |= sc->sc_rie | sc->sc_xie;
793 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, IXPCOM_CR,
794 			  ixpcom_cr & ~ixpcom_imask);
795 }
796 
797 /* Initialization for serial console */
798 static int
799 ixpcominit(bus_space_tag_t iot, bus_addr_t iobase, int baud,
800     tcflag_t cflag, bus_space_handle_t *iohp)
801 {
802 	int cr;
803 
804 	if (bus_space_map(iot, iobase, IXPCOM_UART_SIZE, 0, iohp))
805 		printf("register map failed\n");
806 
807 	cr = cflag2cr(cflag);
808 	cr |= IXPCOMSPEED2BRD(baud);
809 	cr |= CR_UE;
810 	ixpcom_cr = cr;
811 
812 	/* enable the UART */
813 	bus_space_write_4(iot, *iohp, IXPCOM_CR, cr);
814 
815 	return (0);
816 }
817 
818 int
819 ixpcomcnattach(iot, iobase, rate, cflag)
820 	bus_space_tag_t iot;
821 	bus_addr_t iobase;
822 	int rate;
823 	tcflag_t cflag;
824 {
825 	int res;
826 
827 	if ((res = ixpcominit(iot, iobase, rate, cflag, &ixpcomconsioh)))
828 		return (res);
829 	cn_tab = &ixpcomcons;
830 	cn_init_magic(&ixpcom_cnm_state);
831 	/*
832 	 * XXX
833 	 *
834 	 * ixpcom cannot detect a break.  It can only detect framing
835 	 * errors.  And, a sequence of "framing error, framing error"
836 	 * meens a break.  So I made this hack:
837 	 *
838 	 * 1. Set default magic is a sequence of "BREAK, BREAK".
839 	 * 2. Tell cn_check_magic() a "framing error" as BREAK.
840 	 *
841 	 * see ixpcom_intr() too.
842 	 *
843 	 */
844 	/* default magic is a couple of BREAK. */
845 	cn_set_magic("\047\001\047\001");
846 
847 	ixpcomconstag = iot;
848 	ixpcomconsaddr = iobase;
849 	ixpcomconsrate = rate;
850 	ixpcomconscflag = cflag;
851 
852 	return (0);
853 }
854 
855 void
856 ixpcomcninit(cp)
857 	struct consdev *cp;
858 {
859 	printf("ixpcomcninit called\n");
860 #if 0
861 	if (cp == NULL) {
862 		/* XXX cp == NULL means that MMU is disabled. */
863 		ixpcomconsioh = IXPCOM_UART_BASE;
864 		ixpcomconstag = &ixpcom_bs_tag;
865 		cn_tab = &ixpcomcons;
866 
867 		IXPREG(IXPCOM_UART_BASE + IXPCOM_CR)
868 			= IXPCOMSPEED2BRD(DEFAULT_COMSPEED)
869 			| DSS_8BIT
870 			| (CR_UE | CR_RIE | CR_XIE);
871 		return;
872 	}
873 
874 	if (ixpcominit(&ixpcom_bs_tag, CONADDR, CONSPEED,
875                           CONMODE, &ixpcomconsioh))
876 		panic("can't init serial console @%x", CONADDR);
877 	cn_tab = &ixpcomcons;
878 	ixpcomconstag = &ixpcom_bs_tag;
879 #endif
880 }
881 
882 void
883 ixpcomcnprobe(cp)
884 	struct consdev *cp;
885 {
886 	cp->cn_pri = CN_REMOTE;
887 }
888 
889 void
890 ixpcomcnpollc(dev, on)
891 	dev_t dev;
892 	int on;
893 {
894 }
895 
896 void
897 ixpcomcnputc(dev, c)
898 	dev_t dev;
899 	int c;
900 {
901 	int s;
902 
903 	s = splserial();
904 
905 	while(!(bus_space_read_4(ixpcomconstag, ixpcomconsioh, IXPCOM_SR)
906 	    & SR_TXR))
907 		;
908 
909 	bus_space_write_4(ixpcomconstag, ixpcomconsioh, IXPCOM_DR, c);
910 
911 #ifdef DEBUG
912 	if (c == '\r') {
913 		while(!(bus_space_read_4(ixpcomconstag, ixpcomconsioh,
914 					 IXPCOM_SR)
915 			& SR_TXE))
916 			;
917 	}
918 #endif
919 
920 	splx(s);
921 }
922 
923 int
924 ixpcomcngetc(dev)
925         dev_t dev;
926 {
927         int c, s;
928 
929         s = splserial();
930 
931 	while(!(bus_space_read_4(ixpcomconstag, ixpcomconsioh, IXPCOM_SR)
932 	    & SR_RXR))
933 		;
934 
935 	c = bus_space_read_4(ixpcomconstag, ixpcomconsioh, IXPCOM_DR);
936 	c &= 0xff;
937 	splx(s);
938 
939 	return (c);
940 }
941 
942 inline static void
943 ixpcom_txsoft(sc, tp)
944 	struct ixpcom_softc *sc;
945 	struct tty *tp;
946 {
947 	CLR(tp->t_state, TS_BUSY);
948 	if (ISSET(tp->t_state, TS_FLUSH))
949 		CLR(tp->t_state, TS_FLUSH);
950         else
951 		ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
952 	(*tp->t_linesw->l_start)(tp);
953 }
954 
955 inline static void
956 ixpcom_rxsoft(sc, tp)
957 	struct ixpcom_softc *sc;
958 	struct tty *tp;
959 {
960 	int (*rint) __P((int c, struct tty *tp)) = tp->t_linesw->l_rint;
961 	u_char *get, *end;
962 	u_int cc, scc;
963 	u_char lsr;
964 	int code;
965 	int s;
966 
967 	end = sc->sc_ebuf;
968 	get = sc->sc_rbget;
969 	scc = cc = IXPCOM_RING_SIZE - sc->sc_rbavail;
970 #if 0
971 	if (cc == IXPCOM_RING_SIZE) {
972 		sc->sc_floods++;
973 		if (sc->sc_errors++ == 0)
974 			callout_reset(&sc->sc_diag_callout, 60 * hz,
975 			    comdiag, sc);
976 	}
977 #endif
978 	while (cc) {
979 		code = get[0];
980 		lsr = get[1];
981 		if (ISSET(lsr, DR_ROR | DR_FRE | DR_PRE)) {
982 #if 0
983 			if (ISSET(lsr, DR_ROR)) {
984 				sc->sc_overflows++;
985 				if (sc->sc_errors++ == 0)
986 					callout_reset(&sc->sc_diag_callout,
987 					    60 * hz, comdiag, sc);
988 			}
989 #endif
990 			if (ISSET(lsr, DR_FRE))
991 				SET(code, TTY_FE);
992 			if (ISSET(lsr, DR_PRE))
993 				SET(code, TTY_PE);
994 		}
995 		if ((*rint)(code, tp) == -1) {
996 			/*
997 			 * The line discipline's buffer is out of space.
998 			 */
999 			if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
1000 				/*
1001 				 * We're either not using flow control, or the
1002 				 * line discipline didn't tell us to block for
1003 				 * some reason.  Either way, we have no way to
1004 				 * know when there's more space available, so
1005 				 * just drop the rest of the data.
1006 				 */
1007 				get += cc << 1;
1008 				if (get >= end)
1009 					get -= IXPCOM_RING_SIZE << 1;
1010 				cc = 0;
1011 			} else {
1012 				/*
1013 				 * Don't schedule any more receive processing
1014 				 * until the line discipline tells us there's
1015 				 * space available (through comhwiflow()).
1016 				 * Leave the rest of the data in the input
1017 				 * buffer.
1018 				 */
1019 				SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
1020 			}
1021 			break;
1022 		}
1023 		get += 2;
1024 		if (get >= end)
1025 			get = sc->sc_rbuf;
1026 		cc--;
1027 	}
1028 
1029 	if (cc != scc) {
1030 		sc->sc_rbget = get;
1031 		s = splserial();
1032 		COM_LOCK(sc);
1033 
1034 		cc = sc->sc_rbavail += scc - cc;
1035 		/* Buffers should be ok again, release possible block. */
1036 		if (cc >= 1) {
1037 			if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1038 				CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1039 				SET(sc->sc_rie, CR_RIE);
1040 				ixpcom_set_cr(sc);
1041 			}
1042 			if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1043 				CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1044 #if 0
1045 				com_hwiflow(sc);
1046 #endif
1047 			}
1048 		}
1049 		COM_UNLOCK(sc);
1050 		splx(s);
1051 	}
1052 }
1053 
1054 static void
1055 ixpcomsoft(void* arg)
1056 {
1057 	struct ixpcom_softc *sc = arg;
1058 
1059 	if (COM_ISALIVE(sc) == 0)
1060 		return;
1061 
1062 	if (sc->sc_rx_ready) {
1063 		sc->sc_rx_ready = 0;
1064 		ixpcom_rxsoft(sc, sc->sc_tty);
1065 	}
1066 	if (sc->sc_tx_done) {
1067 		sc->sc_tx_done = 0;
1068 		ixpcom_txsoft(sc, sc->sc_tty);
1069 	}
1070 }
1071 
1072 int
1073 ixpcomintr(void* arg)
1074 {
1075 	struct ixpcom_softc *sc = arg;
1076 	bus_space_tag_t iot = sc->sc_iot;
1077 	bus_space_handle_t ioh = sc->sc_ioh;
1078 	u_char *put, *end;
1079 	u_int cc;
1080 	u_int cr;
1081 	u_int sr;
1082 	u_int32_t c;
1083 
1084 	if (COM_ISALIVE(sc) == 0)
1085 		return (0);
1086 
1087 	COM_LOCK(sc);
1088 	cr = bus_space_read_4(iot, ioh, IXPCOM_CR) & CR_UE;
1089 
1090 	if (!cr) {
1091 		COM_UNLOCK(sc);
1092 		return (0);
1093 	}
1094 
1095 	sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1096 	if (!ISSET(sr, SR_TXR | SR_RXR))
1097 		return (0);
1098 
1099 	/*
1100 	 * XXX
1101 	 *
1102 	 * ixpcom cannot detect a break.  It can only detect framing
1103 	 * errors.  And, a sequence of "framing error, framing error"
1104 	 * meens a break.  So I made this hack:
1105 	 *
1106 	 * 1. Set default magic is a sequence of "BREAK, BREAK".
1107 	 * 2. Tell cn_check_magic() a "framing error" as BREAK.
1108 	 *
1109 	 * see ixpcomcnattach() too.
1110 	 *
1111 	 */
1112 	if (ISSET(sr, SR_FRE)) {
1113 		cn_check_magic(sc->sc_tty->t_dev,
1114 			       CNC_BREAK, ixpcom_cnm_state);
1115 	}
1116 
1117 	end = sc->sc_ebuf;
1118 	put = sc->sc_rbput;
1119 	cc = sc->sc_rbavail;
1120 
1121 	if (ISSET(sr, SR_RXR)) {
1122 		if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1123 			while (cc > 0) {
1124 				if (!ISSET(sr, SR_RXR))
1125 					break;
1126 				c = bus_space_read_4(iot, ioh, IXPCOM_DR);
1127 				if (ISSET(c, DR_FRE)) {
1128 					cn_check_magic(sc->sc_tty->t_dev,
1129 						       CNC_BREAK,
1130 						       ixpcom_cnm_state);
1131 				}
1132 				put[0] = c & 0xff;
1133 				put[1] = (c >> 8) & 0xff;
1134 				cn_check_magic(sc->sc_tty->t_dev,
1135 					       put[0], ixpcom_cnm_state);
1136 				put += 2;
1137 				if (put >= end)
1138 					put = sc->sc_rbuf;
1139 				cc--;
1140 
1141 				sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1142 			}
1143 
1144 			/*
1145 			 * Current string of incoming characters ended because
1146 			 * no more data was available or we ran out of space.
1147 			 * Schedule a receive event if any data was received.
1148 			 * If we're out of space, turn off receive interrupts.
1149 			 */
1150 			sc->sc_rbput = put;
1151 			sc->sc_rbavail = cc;
1152 			if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1153 				sc->sc_rx_ready = 1;
1154 
1155 			/*
1156 			 * See if we are in danger of overflowing a buffer. If
1157 			 * so, use hardware flow control to ease the pressure.
1158 			 */
1159 
1160 			/* but ixpcom cannot. X-( */
1161 
1162 			/*
1163 			 * If we're out of space, disable receive interrupts
1164 			 * until the queue has drained a bit.
1165 			 */
1166 			if (!cc) {
1167 				SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1168 				CLR(sc->sc_rie, CR_RIE);
1169 				ixpcom_set_cr(sc);
1170 			}
1171 		} else {
1172 #ifdef DIAGNOSTIC
1173 			panic("ixpcomintr: we shouldn't reach here");
1174 #endif
1175 			CLR(sc->sc_rie, CR_RIE);
1176 			ixpcom_set_cr(sc);
1177 		}
1178 	}
1179 
1180 	/*
1181 	 * Done handling any receive interrupts. See if data can be
1182 	 * transmitted as well. Schedule tx done event if no data left
1183 	 * and tty was marked busy.
1184 	 */
1185 	sr = bus_space_read_4(iot, ioh, IXPCOM_SR);
1186 	if (ISSET(sr, SR_TXR)) {
1187 		/*
1188 		 * If we've delayed a parameter change, do it now, and restart
1189 		 * output.
1190 		 */
1191 		if (sc->sc_heldchange) {
1192 			ixpcom_set_cr(sc);
1193 			sc->sc_heldchange = 0;
1194 			sc->sc_tbc = sc->sc_heldtbc;
1195 			sc->sc_heldtbc = 0;
1196 		}
1197 
1198 		/* Output the next chunk of the contiguous buffer, if any. */
1199 		if (sc->sc_tbc > 0) {
1200 			ixpcom_filltx(sc);
1201 		} else {
1202 			/* Disable transmit completion interrupts if necessary. */
1203 			if (ISSET(sc->sc_xie, CR_XIE)) {
1204 				CLR(sc->sc_xie, CR_XIE);
1205 				ixpcom_set_cr(sc);
1206 			}
1207 			if (sc->sc_tx_busy) {
1208 				sc->sc_tx_busy = 0;
1209 				sc->sc_tx_done = 1;
1210 			}
1211 		}
1212 	}
1213 	COM_UNLOCK(sc);
1214 
1215 	/* Wake up the poller. */
1216 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
1217 	softintr_schedule(sc->sc_si);
1218 #else
1219 	setsoftserial();
1220 #endif
1221 
1222 #if NRND > 0 && defined(RND_COM)
1223 	rnd_add_uint32(&sc->rnd_source, iir | lsr);
1224 #endif
1225 	return (1);
1226 }
1227 
1228