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