epcom.c revision 1.27 1 /* $NetBSD: epcom.c,v 1.27 2014/07/25 08:10:32 dholland 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.27 2014/07/25 08:10:32 dholland Exp $");
77
78 #include "opt_ddb.h"
79 #include "opt_kgdb.h"
80 #include "epcom.h"
81
82 #include "rnd.h"
83 #ifdef 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 <sys/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 .d_open = epcomopen,
166 .d_close = epcomclose,
167 .d_read = epcomread,
168 .d_write = epcomwrite,
169 .d_ioctl = epcomioctl,
170 .d_stop = epcomstop,
171 .d_tty = epcomtty,
172 .d_poll = epcompoll,
173 .d_mmap = nommap,
174 .d_kqfilter = ttykqfilter,
175 .d_discard = nodiscard,
176 .d_flag = D_TTY
177 };
178
179 struct consdev epcomcons = {
180 NULL, NULL, epcomcngetc, epcomcnputc, epcomcnpollc, NULL,
181 NULL, NULL, NODEV, CN_NORMAL
182 };
183
184 #ifndef DEFAULT_COMSPEED
185 #define DEFAULT_COMSPEED 115200
186 #endif
187
188 #define COMUNIT_MASK 0x7ffff
189 #define COMDIALOUT_MASK 0x80000
190
191 #define COMUNIT(x) (minor(x) & COMUNIT_MASK)
192 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK)
193
194 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \
195 device_is_active((sc)->sc_dev))
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 = tty_alloc();
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 device_xname(sc->sc_dev));
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, device_unit(sc->sc_dev));
244
245 aprint_normal("%s: console\n", device_xname(sc->sc_dev));
246 }
247
248 sc->sc_si = softint_establish(SOFTINT_SERIAL, epcomsoft, sc);
249
250 #ifdef RND_COM
251 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev),
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_private(&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_private(&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 if (!ttypull(tp))
374 goto out;
375
376 /* Grab the first contiguous region of buffer space. */
377 {
378 u_char *tba;
379 int tbc;
380
381 tba = tp->t_outq.c_cf;
382 tbc = ndqb(&tp->t_outq, 0);
383
384 (void)splserial();
385
386 sc->sc_tba = tba;
387 sc->sc_tbc = tbc;
388 }
389
390 SET(tp->t_state, TS_BUSY);
391 sc->sc_tx_busy = 1;
392
393 /* Output the first chunk of the contiguous buffer. */
394 epcom_filltx(sc);
395
396 if (!ISSET(sc->sc_ctrl, Ctrl_TIE)) {
397 SET(sc->sc_ctrl, Ctrl_TIE);
398 epcom_set(sc);
399 }
400
401 out:
402 splx(s);
403 return;
404 }
405
406 static void
407 epcom_break(struct epcom_softc *sc, int onoff)
408 {
409 if (onoff)
410 SET(sc->sc_lcrhi, LinCtrlHigh_BRK);
411 else
412 CLR(sc->sc_lcrhi, LinCtrlHigh_BRK);
413 epcom_set(sc);
414 }
415
416 static void
417 epcom_shutdown(struct epcom_softc *sc)
418 {
419 int s;
420
421 s = splserial();
422
423 /* Turn off interrupts. */
424 CLR(sc->sc_ctrl, (Ctrl_TIE|Ctrl_RTIE|Ctrl_RIE));
425
426 /* Clear any break condition set with TIOCSBRK. */
427 epcom_break(sc, 0);
428 epcom_set(sc);
429
430 if (sc->disable) {
431 #ifdef DIAGNOSTIC
432 if (!sc->enabled)
433 panic("epcom_shutdown: not enabled?");
434 #endif
435 (*sc->disable)(sc);
436 sc->enabled = 0;
437 }
438 splx(s);
439 }
440
441 int
442 epcomopen(dev_t dev, int flag, int mode, struct lwp *l)
443 {
444 struct epcom_softc *sc;
445 struct tty *tp;
446 int s, s2;
447 int error;
448
449 sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
450 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) ||
451 sc->sc_rbuf == NULL)
452 return (ENXIO);
453
454 if (!device_is_active(sc->sc_dev))
455 return (ENXIO);
456
457 #ifdef KGDB
458 /*
459 * If this is the kgdb port, no other use is permitted.
460 */
461 if (ISSET(sc->sc_hwflags, COM_HW_KGDB))
462 return (EBUSY);
463 #endif
464
465 tp = sc->sc_tty;
466
467 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
468 return (EBUSY);
469
470 s = spltty();
471
472 /*
473 * Do the following iff this is a first open.
474 */
475 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
476 struct termios t;
477
478 tp->t_dev = dev;
479
480 s2 = splserial();
481
482 if (sc->enable) {
483 if ((*sc->enable)(sc)) {
484 splx(s2);
485 splx(s);
486 printf("%s: device enable failed\n",
487 device_xname(sc->sc_dev));
488 return (EIO);
489 }
490 sc->enabled = 1;
491 #if 0
492 /* XXXXXXXXXXXXXXX */
493 com_config(sc);
494 #endif
495 }
496
497 /* Turn on interrupts. */
498 SET(sc->sc_ctrl, (Ctrl_UARTE|Ctrl_RIE|Ctrl_RTIE));
499 epcom_set(sc);
500
501 #if 0
502 /* Fetch the current modem control status, needed later. */
503 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr);
504
505 /* Clear PPS capture state on first open. */
506 sc->sc_ppsmask = 0;
507 sc->ppsparam.mode = 0;
508 #endif
509
510 splx(s2);
511
512 /*
513 * Initialize the termios status to the defaults. Add in the
514 * sticky bits from TIOCSFLAGS.
515 */
516 t.c_ispeed = 0;
517 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) {
518 t.c_ospeed = epcomcn_sc.sc_ospeed;
519 t.c_cflag = epcomcn_sc.sc_cflag;
520 } else {
521 t.c_ospeed = TTYDEF_SPEED;
522 t.c_cflag = TTYDEF_CFLAG;
523 }
524 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL))
525 SET(t.c_cflag, CLOCAL);
526 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS))
527 SET(t.c_cflag, CRTSCTS);
528 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF))
529 SET(t.c_cflag, MDMBUF);
530 /* Make sure epcomparam() will do something. */
531 tp->t_ospeed = 0;
532 (void) epcomparam(tp, &t);
533 tp->t_iflag = TTYDEF_IFLAG;
534 tp->t_oflag = TTYDEF_OFLAG;
535 tp->t_lflag = TTYDEF_LFLAG;
536 ttychars(tp);
537 ttsetwater(tp);
538
539 s2 = splserial();
540
541 /* Clear the input ring, and unblock. */
542 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf;
543 sc->sc_rbavail = EPCOM_RING_SIZE;
544 epcom_iflush(sc);
545 CLR(sc->sc_rx_flags, RX_ANY_BLOCK);
546
547 #ifdef COM_DEBUG
548 if (epcom_debug)
549 comstatus(sc, "epcomopen ");
550 #endif
551
552 splx(s2);
553 }
554
555 splx(s);
556
557 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK));
558 if (error)
559 goto bad;
560
561 error = (*tp->t_linesw->l_open)(dev, tp);
562 if (error)
563 goto bad;
564
565 return (0);
566
567 bad:
568 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
569 /*
570 * We failed to open the device, and nobody else had it opened.
571 * Clean up the state as appropriate.
572 */
573 epcom_shutdown(sc);
574 }
575
576 return (error);
577 }
578
579 int
580 epcomclose(dev_t dev, int flag, int mode, struct lwp *l)
581 {
582 struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
583 struct tty *tp = sc->sc_tty;
584
585 /* XXX This is for cons.c. */
586 if (!ISSET(tp->t_state, TS_ISOPEN))
587 return (0);
588
589 (*tp->t_linesw->l_close)(tp, flag);
590 ttyclose(tp);
591
592 if (COM_ISALIVE(sc) == 0)
593 return (0);
594
595 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
596 /*
597 * Although we got a last close, the device may still be in
598 * use; e.g. if this was the dialout node, and there are still
599 * processes waiting for carrier on the non-dialout node.
600 */
601 epcom_shutdown(sc);
602 }
603
604 return (0);
605 }
606
607 int
608 epcomread(dev_t dev, struct uio *uio, int flag)
609 {
610 struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
611 struct tty *tp = sc->sc_tty;
612
613 if (COM_ISALIVE(sc) == 0)
614 return (EIO);
615
616 return ((*tp->t_linesw->l_read)(tp, uio, flag));
617 }
618
619 int
620 epcomwrite(dev_t dev, struct uio *uio, int flag)
621 {
622 struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
623 struct tty *tp = sc->sc_tty;
624
625 if (COM_ISALIVE(sc) == 0)
626 return (EIO);
627
628 return ((*tp->t_linesw->l_write)(tp, uio, flag));
629 }
630
631 int
632 epcompoll(dev_t dev, int events, struct lwp *l)
633 {
634 struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
635 struct tty *tp = sc->sc_tty;
636
637 if (COM_ISALIVE(sc) == 0)
638 return (EIO);
639
640 return ((*tp->t_linesw->l_poll)(tp, events, l));
641 }
642
643 struct tty *
644 epcomtty(dev_t dev)
645 {
646 struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
647 struct tty *tp = sc->sc_tty;
648
649 return (tp);
650 }
651
652 int
653 epcomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
654 {
655 struct epcom_softc *sc = device_lookup_private(&epcom_cd, COMUNIT(dev));
656 struct tty *tp = sc->sc_tty;
657 int error;
658 int s;
659
660 if (COM_ISALIVE(sc) == 0)
661 return (EIO);
662
663 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
664 if (error != EPASSTHROUGH)
665 return (error);
666
667 error = ttioctl(tp, cmd, data, flag, l);
668 if (error != EPASSTHROUGH)
669 return (error);
670
671 error = 0;
672
673 s = splserial();
674
675 switch (cmd) {
676 case TIOCSBRK:
677 epcom_break(sc, 1);
678 break;
679
680 case TIOCCBRK:
681 epcom_break(sc, 0);
682 break;
683
684 case TIOCGFLAGS:
685 *(int *)data = sc->sc_swflags;
686 break;
687
688 case TIOCSFLAGS:
689 error = kauth_authorize_device_tty(l->l_cred,
690 KAUTH_DEVICE_TTY_PRIVSET, tp);
691 if (error)
692 break;
693 sc->sc_swflags = *(int *)data;
694 break;
695
696 default:
697 error = EPASSTHROUGH;
698 break;
699 }
700
701 splx(s);
702
703 return (error);
704 }
705
706 /*
707 * Stop output on a line.
708 */
709 void
710 epcomstop(struct tty *tp, int flag)
711 {
712 struct epcom_softc *sc
713 = device_lookup_private(&epcom_cd, COMUNIT(tp->t_dev));
714 int s;
715
716 s = splserial();
717 if (ISSET(tp->t_state, TS_BUSY)) {
718 /* Stop transmitting at the next chunk. */
719 sc->sc_tbc = 0;
720 if (!ISSET(tp->t_state, TS_TTSTOP))
721 SET(tp->t_state, TS_FLUSH);
722 }
723 splx(s);
724 }
725
726 static u_int
727 cflag2lcrhi(tcflag_t cflag)
728 {
729 u_int lcrhi;
730
731 switch (cflag & CSIZE) {
732 case CS7:
733 lcrhi = 0x40;
734 break;
735 case CS6:
736 lcrhi = 0x20;
737 break;
738 case CS8:
739 default:
740 lcrhi = 0x60;
741 break;
742 }
743 lcrhi |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0;
744 lcrhi |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS;
745 lcrhi |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0;
746 lcrhi |= LinCtrlHigh_FEN; /* FIFO always enabled */
747
748 return (lcrhi);
749 }
750
751 static void
752 epcom_iflush(struct epcom_softc *sc)
753 {
754 bus_space_tag_t iot = sc->sc_iot;
755 bus_space_handle_t ioh = sc->sc_ioh;
756 #ifdef DIAGNOSTIC
757 int reg;
758 #endif
759 int timo;
760
761 #ifdef DIAGNOSTIC
762 reg = 0xffff;
763 #endif
764 timo = 50000;
765 /* flush any pending I/O */
766 while ((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_RXFE) == 0
767 && --timo)
768 #ifdef DIAGNOSTIC
769 reg =
770 #else
771 (void)
772 #endif
773 bus_space_read_4(iot, ioh, EPCOM_Data);
774 #ifdef DIAGNOSTIC
775 if (!timo)
776 printf("%s: com_iflush timeout %02x\n", device_xname(sc->sc_dev),
777 reg);
778 #endif
779 }
780
781 static void
782 epcom_set(struct epcom_softc *sc)
783 {
784 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlLow,
785 sc->sc_lcrlo);
786 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlMid,
787 sc->sc_lcrmid);
788 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_LinCtrlHigh,
789 sc->sc_lcrhi);
790 bus_space_write_4(sc->sc_iot, sc->sc_ioh, EPCOM_Ctrl,
791 sc->sc_ctrl);
792 }
793
794 int
795 epcomcnattach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh,
796 int ospeed, tcflag_t cflag)
797 {
798 u_int lcrlo, lcrmid, lcrhi, ctrl, pwrcnt;
799 bus_space_handle_t syscon_ioh;
800
801 cn_tab = &epcomcons;
802 cn_init_magic(&epcom_cnm_state);
803 cn_set_magic("\047\001");
804
805 epcomcn_sc.sc_iot = iot;
806 epcomcn_sc.sc_ioh = ioh;
807 epcomcn_sc.sc_hwbase = iobase;
808 epcomcn_sc.sc_ospeed = ospeed;
809 epcomcn_sc.sc_cflag = cflag;
810
811 lcrhi = cflag2lcrhi(cflag);
812 lcrlo = EPCOMSPEED2BRD(ospeed) & 0xff;
813 lcrmid = EPCOMSPEED2BRD(ospeed) >> 8;
814 ctrl = Ctrl_UARTE;
815
816 bus_space_map(iot, EP93XX_APB_HWBASE + EP93XX_APB_SYSCON,
817 EP93XX_APB_SYSCON_SIZE, 0, &syscon_ioh);
818 pwrcnt = bus_space_read_4(iot, syscon_ioh, EP93XX_SYSCON_PwrCnt);
819 pwrcnt &= ~(PwrCnt_UARTBAUD);
820 bus_space_write_4(iot, syscon_ioh, EP93XX_SYSCON_PwrCnt, pwrcnt);
821 bus_space_unmap(iot, syscon_ioh, EP93XX_APB_SYSCON_SIZE);
822
823 bus_space_write_4(iot, ioh, EPCOM_LinCtrlLow, lcrlo);
824 bus_space_write_4(iot, ioh, EPCOM_LinCtrlMid, lcrmid);
825 bus_space_write_4(iot, ioh, EPCOM_LinCtrlHigh, lcrhi);
826 bus_space_write_4(iot, ioh, EPCOM_Ctrl, ctrl);
827
828 return (0);
829 }
830
831 void
832 epcomcnprobe(struct consdev *cp)
833 {
834 cp->cn_pri = CN_REMOTE;
835 }
836
837 void
838 epcomcnpollc(dev_t dev, int on)
839 {
840 }
841
842 void
843 epcomcnputc(dev_t dev, int c)
844 {
845 int s;
846 bus_space_tag_t iot = epcomcn_sc.sc_iot;
847 bus_space_handle_t ioh = epcomcn_sc.sc_ioh;
848
849 s = splserial();
850
851 while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_TXFF) != 0)
852 ;
853
854 bus_space_write_4(iot, ioh, EPCOM_Data, c);
855
856 #ifdef DEBUG
857 if (c == '\r') {
858 while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_TXFE) == 0)
859 ;
860 }
861 #endif
862
863 splx(s);
864 }
865
866 int
867 epcomcngetc(dev_t dev)
868 {
869 int c, sts;
870 int s;
871 bus_space_tag_t iot = epcomcn_sc.sc_iot;
872 bus_space_handle_t ioh = epcomcn_sc.sc_ioh;
873
874 s = splserial();
875
876 while((bus_space_read_4(iot, ioh, EPCOM_Flag) & Flag_RXFE) != 0)
877 ;
878
879 c = bus_space_read_4(iot, ioh, EPCOM_Data);
880 sts = bus_space_read_4(iot, ioh, EPCOM_RXSts);
881 if (ISSET(sts, RXSts_BE)) c = CNC_BREAK;
882 #ifdef DDB
883 extern int db_active;
884 if (!db_active)
885 #endif
886 {
887 int cn_trapped __unused = 0;
888
889 cn_check_magic(dev, c, epcom_cnm_state);
890 }
891 c &= 0xff;
892 splx(s);
893
894 return (c);
895 }
896
897 inline static void
898 epcom_txsoft(struct epcom_softc *sc, struct tty *tp)
899 {
900 CLR(tp->t_state, TS_BUSY);
901 if (ISSET(tp->t_state, TS_FLUSH))
902 CLR(tp->t_state, TS_FLUSH);
903 else
904 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf));
905 (*tp->t_linesw->l_start)(tp);
906 }
907
908 inline static void
909 epcom_rxsoft(struct epcom_softc *sc, struct tty *tp)
910 {
911 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint;
912 u_char *get, *end;
913 u_int cc, scc;
914 u_char sts;
915 int code;
916 int s;
917
918 end = sc->sc_ebuf;
919 get = sc->sc_rbget;
920 scc = cc = EPCOM_RING_SIZE - sc->sc_rbavail;
921 #if 0
922 if (cc == EPCOM_RING_SIZE) {
923 sc->sc_floods++;
924 if (sc->sc_errors++ == 0)
925 callout_reset(&sc->sc_diag_callout, 60 * hz,
926 comdiag, sc);
927 }
928 #endif
929 while (cc) {
930 code = get[0];
931 sts = get[1];
932 if (ISSET(sts, RXSts_OE | RXSts_FE | RXSts_PE | RXSts_BE)) {
933 #if 0
934 if (ISSET(lsr, DR_ROR)) {
935 sc->sc_overflows++;
936 if (sc->sc_errors++ == 0)
937 callout_reset(&sc->sc_diag_callout,
938 60 * hz, comdiag, sc);
939 }
940 #endif
941 if (ISSET(sts, (RXSts_FE|RXSts_BE)))
942 SET(code, TTY_FE);
943 if (ISSET(sts, RXSts_PE))
944 SET(code, TTY_PE);
945 }
946 if ((*rint)(code, tp) == -1) {
947 /*
948 * The line discipline's buffer is out of space.
949 */
950 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) {
951 /*
952 * We're either not using flow control, or the
953 * line discipline didn't tell us to block for
954 * some reason. Either way, we have no way to
955 * know when there's more space available, so
956 * just drop the rest of the data.
957 */
958 get += cc << 1;
959 if (get >= end)
960 get -= EPCOM_RING_SIZE << 1;
961 cc = 0;
962 } else {
963 /*
964 * Don't schedule any more receive processing
965 * until the line discipline tells us there's
966 * space available (through comhwiflow()).
967 * Leave the rest of the data in the input
968 * buffer.
969 */
970 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED);
971 }
972 break;
973 }
974 get += 2;
975 if (get >= end)
976 get = sc->sc_rbuf;
977 cc--;
978 }
979
980 if (cc != scc) {
981 sc->sc_rbget = get;
982 s = splserial();
983
984 cc = sc->sc_rbavail += scc - cc;
985 /* Buffers should be ok again, release possible block. */
986 if (cc >= 1) {
987 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
988 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
989 SET(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
990 epcom_set(sc);
991 }
992 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) {
993 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED);
994 #if 0
995 com_hwiflow(sc);
996 #endif
997 }
998 }
999 splx(s);
1000 }
1001 }
1002
1003 static void
1004 epcomsoft(void* arg)
1005 {
1006 struct epcom_softc *sc = arg;
1007
1008 if (COM_ISALIVE(sc) == 0)
1009 return;
1010
1011 if (sc->sc_rx_ready) {
1012 sc->sc_rx_ready = 0;
1013 epcom_rxsoft(sc, sc->sc_tty);
1014 }
1015 if (sc->sc_tx_done) {
1016 sc->sc_tx_done = 0;
1017 epcom_txsoft(sc, sc->sc_tty);
1018 }
1019 }
1020
1021 int
1022 epcomintr(void* arg)
1023 {
1024 struct epcom_softc *sc = arg;
1025 bus_space_tag_t iot = sc->sc_iot;
1026 bus_space_handle_t ioh = sc->sc_ioh;
1027 u_char *put, *end;
1028 u_int cc;
1029 u_int flagr;
1030 uint32_t c, csts;
1031
1032 (void) bus_space_read_4(iot, ioh, EPCOM_IntIDIntClr);
1033
1034 if (COM_ISALIVE(sc) == 0)
1035 panic("intr on disabled epcom");
1036
1037 flagr = bus_space_read_4(iot, ioh, EPCOM_Flag);
1038
1039 end = sc->sc_ebuf;
1040 put = sc->sc_rbput;
1041 cc = sc->sc_rbavail;
1042
1043 if (!(ISSET(flagr, Flag_RXFE))) {
1044 if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) {
1045 while (cc > 0) {
1046 if (ISSET(flagr, Flag_RXFE))
1047 break;
1048 c = bus_space_read_4(iot, ioh, EPCOM_Data);
1049 csts = bus_space_read_4(iot, ioh, EPCOM_RXSts);
1050 if (ISSET(csts, RXSts_BE)) {
1051 int cn_trapped = 0;
1052
1053 cn_check_magic(sc->sc_tty->t_dev,
1054 CNC_BREAK, epcom_cnm_state);
1055 if (cn_trapped)
1056 goto next;
1057 #if defined(KGDB) && !defined(DDB)
1058 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)){
1059 kgdb_connect(1);
1060 goto next;
1061 }
1062 #endif
1063 } else {
1064 int cn_trapped = 0;
1065
1066 cn_check_magic(sc->sc_tty->t_dev,
1067 (c & 0xff), epcom_cnm_state);
1068 if (cn_trapped)
1069 goto next;
1070 }
1071
1072
1073 put[0] = c & 0xff;
1074 put[1] = csts & 0xf;
1075 put += 2;
1076 if (put >= end)
1077 put = sc->sc_rbuf;
1078 cc--;
1079 next:
1080 flagr = bus_space_read_4(iot, ioh, EPCOM_Flag);
1081 }
1082
1083 /*
1084 * Current string of incoming characters ended because
1085 * no more data was available or we ran out of space.
1086 * Schedule a receive event if any data was received.
1087 * If we're out of space, turn off receive interrupts.
1088 */
1089 sc->sc_rbput = put;
1090 sc->sc_rbavail = cc;
1091 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED))
1092 sc->sc_rx_ready = 1;
1093
1094 /*
1095 * See if we are in danger of overflowing a buffer. If
1096 * so, use hardware flow control to ease the pressure.
1097 */
1098
1099 /* but epcom cannot. X-( */
1100
1101 /*
1102 * If we're out of space, disable receive interrupts
1103 * until the queue has drained a bit.
1104 */
1105 if (!cc) {
1106 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED);
1107 CLR(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
1108 epcom_set(sc);
1109 }
1110 } else {
1111 #ifdef DIAGNOSTIC
1112 panic("epcomintr: we shouldn't reach here");
1113 #endif
1114 CLR(sc->sc_ctrl, (Ctrl_RIE|Ctrl_RTIE));
1115 epcom_set(sc);
1116 }
1117 }
1118
1119 /*
1120 * Done handling any receive interrupts. See if data can be
1121 * transmitted as well. Schedule tx done event if no data left
1122 * and tty was marked busy.
1123 */
1124
1125 if (!ISSET(flagr, Flag_TXFF) && sc->sc_tbc > 0) {
1126 /* Output the next chunk of the contiguous buffer, if any. */
1127 epcom_filltx(sc);
1128 } else {
1129 /* Disable transmit completion interrupts if necessary. */
1130 if (ISSET(sc->sc_ctrl, Ctrl_TIE)) {
1131 CLR(sc->sc_ctrl, Ctrl_TIE);
1132 epcom_set(sc);
1133 }
1134 if (sc->sc_tx_busy) {
1135 sc->sc_tx_busy = 0;
1136 sc->sc_tx_done = 1;
1137 }
1138 }
1139
1140 /* Wake up the poller. */
1141 softint_schedule(sc->sc_si);
1142
1143 #if 0 /* XXX: broken */
1144 #ifdef RND_COM
1145 rnd_add_uint32(&sc->rnd_source, intr ^ flagr);
1146 #endif
1147 #endif
1148 return (1);
1149 }
1150