epcom.c revision 1.6 1 /* $NetBSD: epcom.c,v 1.6 2005/12/14 00:32:29 christos 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.6 2005/12/14 00:32:29 christos 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(struct epcom_softc *sc)
199 {
200 struct tty *tp;
201
202 if (sc->sc_iot == epcomcn_sc.sc_iot
203 && sc->sc_hwbase == epcomcn_sc.sc_hwbase) {
204 epcomcn_sc.sc_attached = 1;
205 sc->sc_lcrlo = EPCOMSPEED2BRD(epcomcn_sc.sc_ospeed) & 0xff;
206 sc->sc_lcrmid = EPCOMSPEED2BRD(epcomcn_sc.sc_ospeed) >> 8;
207
208 /* Make sure the console is always "hardwired". */
209 delay(10000); /* wait for output to finish */
210 SET(sc->sc_hwflags, COM_HW_CONSOLE);
211 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR);
212 }
213
214 tp = ttymalloc();
215 tp->t_oproc = epcomstart;
216 tp->t_param = epcomparam;
217 tp->t_hwiflow = epcomhwiflow;
218
219 sc->sc_tty = tp;
220 sc->sc_rbuf = malloc(EPCOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT);
221 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
222 sc->sc_rbavail = EPCOM_RING_SIZE;
223 if (sc->sc_rbuf == NULL) {
224 printf("%s: unable to allocate ring buffer\n",
225 sc->sc_dev.dv_xname);
226 return;
227 }
228 sc->sc_ebuf = sc->sc_rbuf + (EPCOM_RING_SIZE << 1);
229 sc->sc_tbc = 0;
230
231 sc->sc_lcrlo = EPCOMSPEED2BRD(DEFAULT_COMSPEED) & 0xff;
232 sc->sc_lcrmid = EPCOMSPEED2BRD(DEFAULT_COMSPEED) >> 8;
233 sc->sc_lcrhi = cflag2lcrhi(CS8); /* 8N1 */
234
235 tty_attach(tp);
236
237 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
238 int maj;
239
240 /* locate the major number */
241 maj = cdevsw_lookup_major(&epcom_cdevsw);
242
243 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit);
244
245 aprint_normal("%s: console\n", sc->sc_dev.dv_xname);
246 }
247
248 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, epcomsoft, sc);
249
250 #if NRND > 0 && defined(RND_COM)
251 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname,
252 RND_TYPE_TTY, 0);
253 #endif
254
255 /* if there are no enable/disable functions, assume the device
256 is always enabled */
257 if (!sc->enable)
258 sc->enabled = 1;
259
260 /* XXX configure register */
261 /* xxx_config(sc) */
262
263 SET(sc->sc_hwflags, COM_HW_DEV_OK);
264 }
265
266 static int
267 epcomparam(struct tty *tp, struct termios *t)
268 {
269 struct epcom_softc *sc
270 = device_lookup(&epcom_cd, COMUNIT(tp->t_dev));
271 int s;
272
273 if (COM_ISALIVE(sc) == 0)
274 return (EIO);
275
276 if (t->c_ispeed && t->c_ispeed != t->c_ospeed)
277 return (EINVAL);
278
279 /*
280 * For the console, always force CLOCAL and !HUPCL, so that the port
281 * is always active.
282 */
283 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) ||
284 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
285 SET(t->c_cflag, CLOCAL);
286 CLR(t->c_cflag, HUPCL);
287 }
288
289 /*
290 * If there were no changes, don't do anything. This avoids dropping
291 * input and improves performance when all we did was frob things like
292 * VMIN and VTIME.
293 */
294 if (tp->t_ospeed == t->c_ospeed &&
295 tp->t_cflag == t->c_cflag)
296 return (0);
297
298 s = splserial();
299
300 sc->sc_lcrhi = cflag2lcrhi(t->c_cflag);
301 sc->sc_lcrlo = EPCOMSPEED2BRD(t->c_ospeed) & 0xff;
302 sc->sc_lcrmid = EPCOMSPEED2BRD(t->c_ospeed) >> 8;
303
304 /* And copy to tty. */
305 tp->t_ispeed = 0;
306 tp->t_ospeed = t->c_ospeed;
307 tp->t_cflag = t->c_cflag;
308 epcom_set(sc);
309
310 splx(s);
311
312 /*
313 * Update the tty layer's idea of the carrier bit.
314 * We tell tty the carrier is always on.
315 */
316 (void) (*tp->t_linesw->l_modem)(tp, 1);
317
318 #ifdef COM_DEBUG
319 if (com_debug)
320 comstatus(sc, "comparam ");
321 #endif
322
323 if (!ISSET(t->c_cflag, CHWFLOW)) {
324 if (sc->sc_tx_stopped) {
325 sc->sc_tx_stopped = 0;
326 epcomstart(tp);
327 }
328 }
329
330 return (0);
331 }
332
333 static int
334 epcomhwiflow(struct tty *tp, int block)
335 {
336 return (0);
337 }
338
339 static void
340 epcom_filltx(struct epcom_softc *sc)
341 {
342 bus_space_tag_t iot = sc->sc_iot;
343 bus_space_handle_t ioh = sc->sc_ioh;
344 int n;
345
346 n = 0;
347 while ((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_TXFF) == 0) {
348 if (n >= sc->sc_tbc)
349 break;
350 bus_space_write_4(iot, ioh, EPCOM_Data,
351 0xff & *(sc->sc_tba + n));
352 n++;
353 }
354 sc->sc_tbc -= n;
355 sc->sc_tba += n;
356 }
357
358 static void
359 epcomstart(struct tty *tp)
360 {
361 struct epcom_softc *sc
362 = device_lookup(&epcom_cd, COMUNIT(tp->t_dev));
363 int s;
364
365 if (COM_ISALIVE(sc) == 0)
366 return;
367
368 s = spltty();
369 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP))
370 goto out;
371 if (sc->sc_tx_stopped)
372 goto out;
373
374 if (tp->t_outq.c_cc <= tp->t_lowat) {
375 if (ISSET(tp->t_state, TS_ASLEEP)) {
376 CLR(tp->t_state, TS_ASLEEP);
377 wakeup(&tp->t_outq);
378 }
379 selwakeup(&tp->t_wsel);
380 if (tp->t_outq.c_cc == 0)
381 goto out;
382 }
383
384 /* Grab the first contiguous region of buffer space. */
385 {
386 u_char *tba;
387 int tbc;
388
389 tba = tp->t_outq.c_cf;
390 tbc = ndqb(&tp->t_outq, 0);
391
392 (void)splserial();
393
394 sc->sc_tba = tba;
395 sc->sc_tbc = tbc;
396 }
397
398 SET(tp->t_state, TS_BUSY);
399 sc->sc_tx_busy = 1;
400
401 /* Output the first chunk of the contiguous buffer. */
402 epcom_filltx(sc);
403
404 if (!ISSET(sc->sc_ctrl, Ctrl_TIE)) {
405 SET(sc->sc_ctrl, Ctrl_TIE);
406 epcom_set(sc);
407 }
408
409 out:
410 splx(s);
411 return;
412 }
413
414 static void
415 epcom_break(struct epcom_softc *sc, int onoff)
416 {
417 if (onoff)
418 SET(sc->sc_lcrhi, LinCtrlHigh_BRK);
419 else
420 CLR(sc->sc_lcrhi, LinCtrlHigh_BRK);
421 epcom_set(sc);
422 }
423
424 static void
425 epcom_shutdown(struct epcom_softc *sc)
426 {
427 int s;
428
429 s = splserial();
430
431 /* Turn off interrupts. */
432 CLR(sc->sc_ctrl, (Ctrl_TIE|Ctrl_RTIE|Ctrl_RIE));
433
434 /* Clear any break condition set with TIOCSBRK. */
435 epcom_break(sc, 0);
436 epcom_set(sc);
437
438 if (sc->disable) {
439 #ifdef DIAGNOSTIC
440 if (!sc->enabled)
441 panic("epcom_shutdown: not enabled?");
442 #endif
443 (*sc->disable)(sc);
444 sc->enabled = 0;
445 }
446 splx(s);
447 }
448
449 int
450 epcomopen(dev_t dev, int flag, int mode, struct lwp *l)
451 {
452 struct epcom_softc *sc;
453 struct tty *tp;
454 int s, s2;
455 int error;
456
457 sc = device_lookup(&epcom_cd, COMUNIT(dev));
458 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
459 sc->sc_rbuf == NULL)
460 return (ENXIO);
461
462 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0)
463 return (ENXIO);
464
465 #ifdef KGDB
466 /*
467 * If this is the kgdb port, no other use is permitted.
468 */
469 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
470 return (EBUSY);
471 #endif
472
473 tp = sc->sc_tty;
474
475 if (ISSET(tp->t_state, TS_ISOPEN) &&
476 ISSET(tp->t_state, TS_XCLUDE) &&
477 suser(l->l_proc->p_ucred, &l->l_proc->p_acflag) != 0)
478 return (EBUSY);
479
480 s = spltty();
481
482 /*
483 * Do the following iff this is a first open.
484 */
485 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
486 struct termios t;
487
488 tp->t_dev = dev;
489
490 s2 = splserial();
491
492 if (sc->enable) {
493 if ((*sc->enable)(sc)) {
494 splx(s2);
495 splx(s);
496 printf("%s: device enable failed\n",
497 sc->sc_dev.dv_xname);
498 return (EIO);
499 }
500 sc->enabled = 1;
501 #if 0
502 /* XXXXXXXXXXXXXXX */
503 com_config(sc);
504 #endif
505 }
506
507 /* Turn on interrupts. */
508 SET(sc->sc_ctrl, (Ctrl_UARTE|Ctrl_RIE|Ctrl_RTIE));
509 epcom_set(sc);
510
511 #if 0
512 /* Fetch the current modem control status, needed later. */
513 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
514
515 /* Clear PPS capture state on first open. */
516 sc->sc_ppsmask = 0;
517 sc->ppsparam.mode = 0;
518 #endif
519
520 splx(s2);
521
522 /*
523 * Initialize the termios status to the defaults. Add in the
524 * sticky bits from TIOCSFLAGS.
525 */
526 t.c_ispeed = 0;
527 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
528 t.c_ospeed = epcomcn_sc.sc_ospeed;
529 t.c_cflag = epcomcn_sc.sc_cflag;
530 } else {
531 t.c_ospeed = TTYDEF_SPEED;
532 t.c_cflag = TTYDEF_CFLAG;
533 }
534 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
535 SET(t.c_cflag, CLOCAL);
536 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
537 SET(t.c_cflag, CRTSCTS);
538 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
539 SET(t.c_cflag, MDMBUF);
540 /* Make sure epcomparam() will do something. */
541 tp->t_ospeed = 0;
542 (void) epcomparam(tp, &t);
543 tp->t_iflag = TTYDEF_IFLAG;
544 tp->t_oflag = TTYDEF_OFLAG;
545 tp->t_lflag = TTYDEF_LFLAG;
546 ttychars(tp);
547 ttsetwater(tp);
548
549 s2 = splserial();
550
551 /* Clear the input ring, and unblock. */
552 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
553 sc->sc_rbavail = EPCOM_RING_SIZE;
554 epcom_iflush(sc);
555 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
556
557 #ifdef COM_DEBUG
558 if (epcom_debug)
559 comstatus(sc, "epcomopen ");
560 #endif
561
562 splx(s2);
563 }
564
565 splx(s);
566
567 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
568 if (error)
569 goto bad;
570
571 error = (*tp->t_linesw->l_open)(dev, tp);
572 if (error)
573 goto bad;
574
575 return (0);
576
577 bad:
578 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
579 /*
580 * We failed to open the device, and nobody else had it opened.
581 * Clean up the state as appropriate.
582 */
583 epcom_shutdown(sc);
584 }
585
586 return (error);
587 }
588
589 int
590 epcomclose(dev_t dev, int flag, int mode, struct lwp *l)
591 {
592 struct epcom_softc *sc = device_lookup(&epcom_cd, COMUNIT(dev));
593 struct tty *tp = sc->sc_tty;
594
595 /* XXX This is for cons.c. */
596 if (!ISSET(tp->t_state, TS_ISOPEN))
597 return (0);
598
599 (*tp->t_linesw->l_close)(tp, flag);
600 ttyclose(tp);
601
602 if (COM_ISALIVE(sc) == 0)
603 return (0);
604
605 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
606 /*
607 * Although we got a last close, the device may still be in
608 * use; e.g. if this was the dialout node, and there are still
609 * processes waiting for carrier on the non-dialout node.
610 */
611 epcom_shutdown(sc);
612 }
613
614 return (0);
615 }
616
617 int
618 epcomread(dev_t dev, struct uio *uio, int flag)
619 {
620 struct epcom_softc *sc = device_lookup(&epcom_cd, COMUNIT(dev));
621 struct tty *tp = sc->sc_tty;
622
623 if (COM_ISALIVE(sc) == 0)
624 return (EIO);
625
626 return ((*tp->t_linesw->l_read)(tp, uio, flag));
627 }
628
629 int
630 epcomwrite(dev_t dev, struct uio *uio, int flag)
631 {
632 struct epcom_softc *sc = device_lookup(&epcom_cd, COMUNIT(dev));
633 struct tty *tp = sc->sc_tty;
634
635 if (COM_ISALIVE(sc) == 0)
636 return (EIO);
637
638 return ((*tp->t_linesw->l_write)(tp, uio, flag));
639 }
640
641 int
642 epcompoll(dev_t dev, int events, struct lwp *l)
643 {
644 struct epcom_softc *sc = device_lookup(&epcom_cd, COMUNIT(dev));
645 struct tty *tp = sc->sc_tty;
646
647 if (COM_ISALIVE(sc) == 0)
648 return (EIO);
649
650 return ((*tp->t_linesw->l_poll)(tp, events, l));
651 }
652
653 struct tty *
654 epcomtty(dev_t dev)
655 {
656 struct epcom_softc *sc = device_lookup(&epcom_cd, COMUNIT(dev));
657 struct tty *tp = sc->sc_tty;
658
659 return (tp);
660 }
661
662 int
663 epcomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
664 {
665 struct epcom_softc *sc = device_lookup(&epcom_cd, COMUNIT(dev));
666 struct tty *tp = sc->sc_tty;
667 int error;
668 int s;
669
670 if (COM_ISALIVE(sc) == 0)
671 return (EIO);
672
673 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
674 if (error != EPASSTHROUGH)
675 return (error);
676
677 error = ttioctl(tp, cmd, data, flag, l);
678 if (error != EPASSTHROUGH)
679 return (error);
680
681 error = 0;
682
683 s = splserial();
684
685 switch (cmd) {
686 case TIOCSBRK:
687 epcom_break(sc, 1);
688 break;
689
690 case TIOCCBRK:
691 epcom_break(sc, 0);
692 break;
693
694 case TIOCGFLAGS:
695 *(int *)data = sc->sc_swflags;
696 break;
697
698 case TIOCSFLAGS:
699 error = suser(l->l_proc->p_ucred, &l->l_proc->p_acflag);
700 if (error)
701 break;
702 sc->sc_swflags = *(int *)data;
703 break;
704
705 default:
706 error = EPASSTHROUGH;
707 break;
708 }
709
710 splx(s);
711
712 return (error);
713 }
714
715 /*
716 * Stop output on a line.
717 */
718 void
719 epcomstop(struct tty *tp, int flag)
720 {
721 struct epcom_softc *sc
722 = device_lookup(&epcom_cd, COMUNIT(tp->t_dev));
723 int s;
724
725 s = splserial();
726 if (ISSET(tp->t_state, TS_BUSY)) {
727 /* Stop transmitting at the next chunk. */
728 sc->sc_tbc = 0;
729 if (!ISSET(tp->t_state, TS_TTSTOP))
730 SET(tp->t_state, TS_FLUSH);
731 }
732 splx(s);
733 }
734
735 static u_int
736 cflag2lcrhi(tcflag_t cflag)
737 {
738 u_int lcrhi;
739
740 switch (cflag & CSIZE) {
741 case CS7:
742 lcrhi = 0x40;
743 break;
744 case CS6:
745 lcrhi = 0x20;
746 break;
747 case CS8:
748 default:
749 lcrhi = 0x60;
750 break;
751 }
752 lcrhi |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0;
753 lcrhi |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS;
754 lcrhi |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0;
755 lcrhi |= LinCtrlHigh_FEN; /* FIFO always enabled */
756
757 return (lcrhi);
758 }
759
760 static void
761 epcom_iflush(struct epcom_softc *sc)
762 {
763 bus_space_tag_t iot = sc->sc_iot;
764 bus_space_handle_t ioh = sc->sc_ioh;
765 #ifdef DIAGNOSTIC
766 int reg;
767 #endif
768 int timo;
769
770 #ifdef DIAGNOSTIC
771 reg = 0xffff;
772 #endif
773 timo = 50000;
774 /* flush any pending I/O */
775 while ((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_RXFE) == 0
776 && --timo)
777 #ifdef DIAGNOSTIC
778 reg =
779 #else
780 (void)
781 #endif
782 bus_space_read_4(iot, ioh, EPCOM_Data);
783 #ifdef DIAGNOSTIC
784 if (!timo)
785 printf("%s: com_iflush timeout %02x\n", sc->sc_dev.dv_xname,
786 reg);
787 #endif
788 }
789
790 static void
791 epcom_set(struct epcom_softc *sc)
792 {
793 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlLow,
794 sc->sc_lcrlo);
795 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlMid,
796 sc->sc_lcrmid);
797 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlHigh,
798 sc->sc_lcrhi);
799 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_Ctrl,
800 sc->sc_ctrl);
801 }
802
803 int
804 epcomcnattach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
805 int ospeed, tcflag_t cflag)
806 {
807 u_int lcrlo, lcrmid, lcrhi, ctrl, pwrcnt;
808 bus_space_handle_t syscon_ioh;
809
810 cn_tab = &epcomcons;
811 cn_init_magic(&epcom_cnm_state);
812 cn_set_magic("\047\001");
813
814 epcomcn_sc.sc_iot = iot;
815 epcomcn_sc.sc_ioh = ioh;
816 epcomcn_sc.sc_hwbase = iobase;
817 epcomcn_sc.sc_ospeed = ospeed;
818 epcomcn_sc.sc_cflag = cflag;
819
820 lcrhi = cflag2lcrhi(cflag);
821 lcrlo = EPCOMSPEED2BRD(ospeed) & 0xff;
822 lcrmid = EPCOMSPEED2BRD(ospeed) >> 8;
823 ctrl = Ctrl_UARTE;
824
825 bus_space_map(iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
826 EP93XX_APB_SYSCON_SIZE, 0, &syscon_ioh);
827 pwrcnt = bus_space_read_4(iot, syscon_ioh, EP93XX_SYSCON_PwrCnt);
828 pwrcnt &= ~(PwrCnt_UARTBAUD);
829 bus_space_write_4(iot, syscon_ioh, EP93XX_SYSCON_PwrCnt, pwrcnt);
830 bus_space_unmap(iot, syscon_ioh, EP93XX_APB_SYSCON_SIZE);
831
832 bus_space_write_4(iot, ioh, EPCOM_LinCtrlLow, lcrlo);
833 bus_space_write_4(iot, ioh, EPCOM_LinCtrlMid, lcrmid);
834 bus_space_write_4(iot, ioh, EPCOM_LinCtrlHigh, lcrhi);
835 bus_space_write_4(iot, ioh, EPCOM_Ctrl, ctrl);
836
837 return (0);
838 }
839
840 void
841 epcomcnprobe(struct consdev *cp)
842 {
843 cp->cn_pri = CN_REMOTE;
844 }
845
846 void
847 epcomcnpollc(dev_t dev, int on)
848 {
849 }
850
851 void
852 epcomcnputc(dev_t dev, int c)
853 {
854 int s;
855 bus_space_tag_t iot = epcomcn_sc.sc_iot;
856 bus_space_handle_t ioh = epcomcn_sc.sc_ioh;
857
858 s = splserial();
859
860 while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_TXFF) != 0)
861 ;
862
863 bus_space_write_4(iot, ioh, EPCOM_Data, c);
864
865 #ifdef DEBUG
866 if (c == '\r') {
867 while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_TXFE) == 0)
868 ;
869 }
870 #endif
871
872 splx(s);
873 }
874
875 int
876 epcomcngetc(dev_t dev)
877 {
878 int c, sts;
879 int s;
880 bus_space_tag_t iot = epcomcn_sc.sc_iot;
881 bus_space_handle_t ioh = epcomcn_sc.sc_ioh;
882
883 s = splserial();
884
885 while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_RXFE) != 0)
886 ;
887
888 c = bus_space_read_4(iot, ioh, EPCOM_Data);
889 sts = bus_space_read_4(iot, ioh, EPCOM_RXSts);
890 if (ISSET(sts, RXSts_BE)) c = CNC_BREAK;
891 #ifdef DDB
892 extern int db_active;
893 if (!db_active)
894 #endif
895 {
896 int cn_trapped = 0; /* unused */
897
898 cn_check_magic(dev, c, epcom_cnm_state);
899 }
900 c &= 0xff;
901 splx(s);
902
903 return (c);
904 }
905
906 inline static void
907 epcom_txsoft(struct epcom_softc *sc, struct tty *tp)
908 {
909 CLR(tp->t_state, TS_BUSY);
910 if (ISSET(tp->t_state, TS_FLUSH))
911 CLR(tp->t_state, TS_FLUSH);
912 else
913 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
914 (*tp->t_linesw->l_start)(tp);
915 }
916
917 inline static void
918 epcom_rxsoft(struct epcom_softc *sc, struct tty *tp)
919 {
920 int (*rint) __P((int, struct tty *)) = tp->t_linesw->l_rint;
921 u_char *get, *end;
922 u_int cc, scc;
923 u_char sts;
924 int code;
925 int s;
926
927 end = sc->sc_ebuf;
928 get = sc->sc_rbget;
929 scc = cc = EPCOM_RING_SIZE - sc->sc_rbavail;
930 #if 0
931 if (cc == EPCOM_RING_SIZE) {
932 sc->sc_floods++;
933 if (sc->sc_errors++ == 0)
934 callout_reset(&sc->sc_diag_callout, 60 * hz,
935 comdiag, sc);
936 }
937 #endif
938 while (cc) {
939 code = get[0];
940 sts = get[1];
941 if (ISSET(sts, RXSts_OE | RXSts_FE | RXSts_PE | RXSts_BE)) {
942 #if 0
943 if (ISSET(lsr, DR_ROR)) {
944 sc->sc_overflows++;
945 if (sc->sc_errors++ == 0)
946 callout_reset(&sc->sc_diag_callout,
947 60 * hz, comdiag, sc);
948 }
949 #endif
950 if (ISSET(sts, (RXSts_FE|RXSts_BE)))
951 SET(code, TTY_FE);
952 if (ISSET(sts, RXSts_PE))
953 SET(code, TTY_PE);
954 }
955 if ((*rint)(code, tp) == -1) {
956 /*
957 * The line discipline's buffer is out of space.
958 */
959 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
960 /*
961 * We're either not using flow control, or the
962 * line discipline didn't tell us to block for
963 * some reason. Either way, we have no way to
964 * know when there's more space available, so
965 * just drop the rest of the data.
966 */
967 get += cc << 1;
968 if (get >= end)
969 get -= EPCOM_RING_SIZE << 1;
970 cc = 0;
971 } else {
972 /*
973 * Don't schedule any more receive processing
974 * until the line discipline tells us there's
975 * space available (through comhwiflow()).
976 * Leave the rest of the data in the input
977 * buffer.
978 */
979 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
980 }
981 break;
982 }
983 get += 2;
984 if (get >= end)
985 get = sc->sc_rbuf;
986 cc--;
987 }
988
989 if (cc != scc) {
990 sc->sc_rbget = get;
991 s = splserial();
992
993 cc = sc->sc_rbavail += scc - cc;
994 /* Buffers should be ok again, release possible block. */
995 if (cc >= 1) {
996 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
997 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
998 SET(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
999 epcom_set(sc);
1000 }
1001 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
1002 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
1003 #if 0
1004 com_hwiflow(sc);
1005 #endif
1006 }
1007 }
1008 splx(s);
1009 }
1010 }
1011
1012 static void
1013 epcomsoft(void* arg)
1014 {
1015 struct epcom_softc *sc = arg;
1016
1017 if (COM_ISALIVE(sc) == 0)
1018 return;
1019
1020 if (sc->sc_rx_ready) {
1021 sc->sc_rx_ready = 0;
1022 epcom_rxsoft(sc, sc->sc_tty);
1023 }
1024 if (sc->sc_tx_done) {
1025 sc->sc_tx_done = 0;
1026 epcom_txsoft(sc, sc->sc_tty);
1027 }
1028 }
1029
1030 int
1031 epcomintr(void* arg)
1032 {
1033 struct epcom_softc *sc = arg;
1034 bus_space_tag_t iot = sc->sc_iot;
1035 bus_space_handle_t ioh = sc->sc_ioh;
1036 u_char *put, *end;
1037 u_int cc;
1038 u_int flagr;
1039 u_int intr;
1040 u_int32_t c, csts;
1041
1042 intr = bus_space_read_4(iot, ioh, EPCOM_IntIDIntClr);
1043
1044 if (COM_ISALIVE(sc) == 0)
1045 panic("intr on disabled epcom");
1046
1047 flagr = bus_space_read_4(iot, ioh, EPCOM_Flag);
1048
1049 end = sc->sc_ebuf;
1050 put = sc->sc_rbput;
1051 cc = sc->sc_rbavail;
1052
1053 if (!(ISSET(flagr, Flag_RXFE))) {
1054 if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1055 while (cc > 0) {
1056 if (ISSET(flagr, Flag_RXFE))
1057 break;
1058 c = bus_space_read_4(iot, ioh, EPCOM_Data);
1059 csts = bus_space_read_4(iot, ioh, EPCOM_RXSts);
1060 if (ISSET(csts, RXSts_BE)) {
1061 int cn_trapped = 0;
1062
1063 cn_check_magic(sc->sc_tty->t_dev,
1064 CNC_BREAK, epcom_cnm_state);
1065 if (cn_trapped)
1066 goto next;
1067 #if defined(KGDB) && !defined(DDB)
1068 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)){
1069 kgdb_connect(1);
1070 goto next;
1071 }
1072 #endif
1073 } else {
1074 int cn_trapped = 0;
1075
1076 cn_check_magic(sc->sc_tty->t_dev,
1077 (c & 0xff), epcom_cnm_state);
1078 if (cn_trapped)
1079 goto next;
1080 }
1081
1082
1083 put[0] = c & 0xff;
1084 put[1] = csts & 0xf;
1085 put += 2;
1086 if (put >= end)
1087 put = sc->sc_rbuf;
1088 cc--;
1089 next:
1090 flagr = bus_space_read_4(iot, ioh, EPCOM_Flag);
1091 }
1092
1093 /*
1094 * Current string of incoming characters ended because
1095 * no more data was available or we ran out of space.
1096 * Schedule a receive event if any data was received.
1097 * If we're out of space, turn off receive interrupts.
1098 */
1099 sc->sc_rbput = put;
1100 sc->sc_rbavail = cc;
1101 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1102 sc->sc_rx_ready = 1;
1103
1104 /*
1105 * See if we are in danger of overflowing a buffer. If
1106 * so, use hardware flow control to ease the pressure.
1107 */
1108
1109 /* but epcom cannot. X-( */
1110
1111 /*
1112 * If we're out of space, disable receive interrupts
1113 * until the queue has drained a bit.
1114 */
1115 if (!cc) {
1116 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1117 CLR(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
1118 epcom_set(sc);
1119 }
1120 } else {
1121 #ifdef DIAGNOSTIC
1122 panic("epcomintr: we shouldn't reach here");
1123 #endif
1124 CLR(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
1125 epcom_set(sc);
1126 }
1127 }
1128
1129 /*
1130 * Done handling any receive interrupts. See if data can be
1131 * transmitted as well. Schedule tx done event if no data left
1132 * and tty was marked busy.
1133 */
1134
1135 if (!ISSET(flagr, Flag_TXFF) && sc->sc_tbc > 0) {
1136 /* Output the next chunk of the contiguous buffer, if any. */
1137 epcom_filltx(sc);
1138 } else {
1139 /* Disable transmit completion interrupts if necessary. */
1140 if (ISSET(sc->sc_ctrl, Ctrl_TIE)) {
1141 CLR(sc->sc_ctrl, Ctrl_TIE);
1142 epcom_set(sc);
1143 }
1144 if (sc->sc_tx_busy) {
1145 sc->sc_tx_busy = 0;
1146 sc->sc_tx_done = 1;
1147 }
1148 }
1149
1150 /* Wake up the poller. */
1151 softintr_schedule(sc->sc_si);
1152
1153 #if 0 /* XXX: broken */
1154 #if NRND > 0 && defined(RND_COM)
1155 rnd_add_uint32(&sc->rnd_source, intr ^ flagr);
1156 #endif
1157 #endif
1158 return (1);
1159 }
1160