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