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