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