1 /* $NetBSD: plcom.c,v 1.3 2002/03/17 19:40:37 atatat Exp $ */ 2 3 /*- 4 * Copyright (c) 2001 ARM Ltd 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the company may not be used to endorse or promote 16 * products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 * 31 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 32 * All rights reserved. 33 * 34 * This code is derived from software contributed to The NetBSD Foundation 35 * by Charles M. Hannum. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by the NetBSD 48 * Foundation, Inc. and its contributors. 49 * 4. Neither the name of The NetBSD Foundation nor the names of its 50 * contributors may be used to endorse or promote products derived 51 * from this software without specific prior written permission. 52 * 53 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 54 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 56 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 57 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 58 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 59 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 60 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 61 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 62 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 63 * POSSIBILITY OF SUCH DAMAGE. 64 */ 65 66 /* 67 * Copyright (c) 1991 The Regents of the University of California. 68 * All rights reserved. 69 * 70 * Redistribution and use in source and binary forms, with or without 71 * modification, are permitted provided that the following conditions 72 * are met: 73 * 1. Redistributions of source code must retain the above copyright 74 * notice, this list of conditions and the following disclaimer. 75 * 2. Redistributions in binary form must reproduce the above copyright 76 * notice, this list of conditions and the following disclaimer in the 77 * documentation and/or other materials provided with the distribution. 78 * 3. All advertising materials mentioning features or use of this software 79 * must display the following acknowledgement: 80 * This product includes software developed by the University of 81 * California, Berkeley and its contributors. 82 * 4. Neither the name of the University nor the names of its contributors 83 * may be used to endorse or promote products derived from this software 84 * without specific prior written permission. 85 * 86 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 87 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 88 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 89 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 90 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 91 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 92 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 93 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 94 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 95 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 96 * SUCH DAMAGE. 97 * 98 * @(#)com.c 7.5 (Berkeley) 5/16/91 99 */ 100 101 /* 102 * COM driver for the Prime Cell PL010 UART, which is similar to the 16C550, 103 * but has a completely different programmer's model. 104 * Derived from the NS16550AF com driver. 105 */ 106 107 #include "opt_plcom.h" 108 #include "opt_ddb.h" 109 #include "opt_kgdb.h" 110 111 #include "rnd.h" 112 #if NRND > 0 && defined(RND_COM) 113 #include <sys/rnd.h> 114 #endif 115 116 /* 117 * Override cnmagic(9) macro before including <sys/systm.h>. 118 * We need to know if cn_check_magic triggered debugger, so set a flag. 119 * Callers of cn_check_magic must declare int cn_trapped = 0; 120 * XXX: this is *ugly*! 121 */ 122 #define cn_trap() \ 123 do { \ 124 console_debugger(); \ 125 cn_trapped = 1; \ 126 } while (/* CONSTCOND */ 0) 127 128 #include <sys/param.h> 129 #include <sys/systm.h> 130 #include <sys/ioctl.h> 131 #include <sys/select.h> 132 #include <sys/tty.h> 133 #include <sys/proc.h> 134 #include <sys/user.h> 135 #include <sys/conf.h> 136 #include <sys/file.h> 137 #include <sys/uio.h> 138 #include <sys/kernel.h> 139 #include <sys/syslog.h> 140 #include <sys/types.h> 141 #include <sys/device.h> 142 #include <sys/malloc.h> 143 #include <sys/timepps.h> 144 #include <sys/vnode.h> 145 146 #include <machine/intr.h> 147 #include <machine/bus.h> 148 149 #include <evbarm/dev/plcomreg.h> 150 #include <evbarm/dev/plcomvar.h> 151 152 #include <dev/cons.h> 153 154 static void plcom_enable_debugport (struct plcom_softc *); 155 156 void plcom_config (struct plcom_softc *); 157 void plcom_shutdown (struct plcom_softc *); 158 int plcomspeed (long, long); 159 static u_char cflag2lcr (tcflag_t); 160 int plcomparam (struct tty *, struct termios *); 161 void plcomstart (struct tty *); 162 int plcomhwiflow (struct tty *, int); 163 164 void plcom_loadchannelregs (struct plcom_softc *); 165 void plcom_hwiflow (struct plcom_softc *); 166 void plcom_break (struct plcom_softc *, int); 167 void plcom_modem (struct plcom_softc *, int); 168 void tiocm_to_plcom (struct plcom_softc *, u_long, int); 169 int plcom_to_tiocm (struct plcom_softc *); 170 void plcom_iflush (struct plcom_softc *); 171 172 int plcom_common_getc (dev_t, bus_space_tag_t, bus_space_handle_t); 173 void plcom_common_putc (dev_t, bus_space_tag_t, bus_space_handle_t, int); 174 175 int plcominit (bus_space_tag_t, bus_addr_t, int, int, tcflag_t, 176 bus_space_handle_t *); 177 178 /* XXX: This belongs elsewhere */ 179 cdev_decl(plcom); 180 181 int plcomcngetc (dev_t); 182 void plcomcnputc (dev_t, int); 183 void plcomcnpollc (dev_t, int); 184 185 #define integrate static inline 186 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 187 void plcomsoft (void *); 188 #else 189 #ifndef __NO_SOFT_SERIAL_INTERRUPT 190 void plcomsoft (void); 191 #else 192 void plcomsoft (void *); 193 struct callout plcomsoft_callout = CALLOUT_INITIALIZER; 194 #endif 195 #endif 196 integrate void plcom_rxsoft (struct plcom_softc *, struct tty *); 197 integrate void plcom_txsoft (struct plcom_softc *, struct tty *); 198 integrate void plcom_stsoft (struct plcom_softc *, struct tty *); 199 integrate void plcom_schedrx (struct plcom_softc *); 200 void plcomdiag (void *); 201 202 extern struct cfdriver plcom_cd; 203 204 /* 205 * Make this an option variable one can patch. 206 * But be warned: this must be a power of 2! 207 */ 208 u_int plcom_rbuf_size = PLCOM_RING_SIZE; 209 210 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */ 211 u_int plcom_rbuf_hiwat = (PLCOM_RING_SIZE * 1) / 4; 212 u_int plcom_rbuf_lowat = (PLCOM_RING_SIZE * 3) / 4; 213 214 static int plcomconsunit = -1; 215 static bus_space_tag_t plcomconstag; 216 static bus_space_handle_t plcomconsioh; 217 static int plcomconsattached; 218 static int plcomconsrate; 219 static tcflag_t plcomconscflag; 220 static struct cnm_state plcom_cnm_state; 221 222 static int ppscap = 223 PPS_TSFMT_TSPEC | 224 PPS_CAPTUREASSERT | 225 PPS_CAPTURECLEAR | 226 #ifdef PPS_SYNC 227 PPS_HARDPPSONASSERT | PPS_HARDPPSONCLEAR | 228 #endif /* PPS_SYNC */ 229 PPS_OFFSETASSERT | PPS_OFFSETCLEAR; 230 231 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS 232 #ifdef __NO_SOFT_SERIAL_INTERRUPT 233 volatile int plcom_softintr_scheduled; 234 #endif 235 #endif 236 237 #ifdef KGDB 238 #include <sys/kgdb.h> 239 240 static int plcom_kgdb_unit; 241 static bus_space_tag_t plcom_kgdb_iot; 242 static bus_space_handle_t plcom_kgdb_ioh; 243 static int plcom_kgdb_attached; 244 245 int plcom_kgdb_getc (void *); 246 void plcom_kgdb_putc (void *, int); 247 #endif /* KGDB */ 248 249 #define PLCOMUNIT_MASK 0x7ffff 250 #define PLCOMDIALOUT_MASK 0x80000 251 252 #define PLCOMUNIT(x) (minor(x) & PLCOMUNIT_MASK) 253 #define PLCOMDIALOUT(x) (minor(x) & PLCOMDIALOUT_MASK) 254 255 #define PLCOM_ISALIVE(sc) ((sc)->enabled != 0 && \ 256 ISSET((sc)->sc_dev.dv_flags, DVF_ACTIVE)) 257 258 #define BR BUS_SPACE_BARRIER_READ 259 #define BW BUS_SPACE_BARRIER_WRITE 260 #define PLCOM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, PLCOM_UART_SIZE, (f)) 261 262 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(PLCOM_MPLOCK) 263 264 #define PLCOM_LOCK(sc) simple_lock(&(sc)->sc_lock) 265 #define PLCOM_UNLOCK(sc) simple_unlock(&(sc)->sc_lock) 266 267 #else 268 269 #define PLCOM_LOCK(sc) 270 #define PLCOM_UNLOCK(sc) 271 272 #endif 273 274 int 275 plcomspeed(long speed, long frequency) 276 { 277 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */ 278 279 int x, err; 280 281 #if 0 282 if (speed == 0) 283 return 0; 284 #endif 285 if (speed <= 0) 286 return -1; 287 x = divrnd(frequency / 16, speed); 288 if (x <= 0) 289 return -1; 290 err = divrnd(((quad_t)frequency) * 1000 / 16, speed * x) - 1000; 291 if (err < 0) 292 err = -err; 293 if (err > PLCOM_TOLERANCE) 294 return -1; 295 return x; 296 297 #undef divrnd 298 } 299 300 #ifdef PLCOM_DEBUG 301 int plcom_debug = 0; 302 303 void plcomstatus (struct plcom_softc *, char *); 304 void 305 plcomstatus(struct plcom_softc *sc, char *str) 306 { 307 struct tty *tp = sc->sc_tty; 308 309 printf("%s: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n", 310 sc->sc_dev.dv_xname, str, 311 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-", 312 ISSET(sc->sc_msr, MSR_DCD) ? "+" : "-", 313 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-", 314 ISSET(sc->sc_mcr, MCR_DTR) ? "+" : "-", 315 sc->sc_tx_stopped ? "+" : "-"); 316 317 printf("%s: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n", 318 sc->sc_dev.dv_xname, str, 319 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-", 320 ISSET(sc->sc_msr, MSR_CTS) ? "+" : "-", 321 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-", 322 ISSET(sc->sc_mcr, MCR_RTS) ? "+" : "-", 323 sc->sc_rx_flags); 324 } 325 #endif 326 327 int 328 plcomprobe1(bus_space_tag_t iot, bus_space_handle_t ioh) 329 { 330 int data; 331 332 /* Disable the UART. */ 333 bus_space_write_1(iot, ioh, plcom_cr, 0); 334 /* Make sure the FIFO is off. */ 335 bus_space_write_1(iot, ioh, plcom_lcr, LCR_8BITS); 336 /* Disable interrupts. */ 337 bus_space_write_1(iot, ioh, plcom_iir, 0); 338 339 /* Make sure we swallow anything in the receiving register. */ 340 data = bus_space_read_1(iot, ioh, plcom_dr); 341 342 if (bus_space_read_1(iot, ioh, plcom_lcr) != LCR_8BITS) 343 return 0; 344 345 data = bus_space_read_1(iot, ioh, plcom_fr) & (FR_RXFF | FR_RXFE); 346 347 if (data != FR_RXFE) 348 return 0; 349 350 return 1; 351 } 352 353 static void 354 plcom_enable_debugport(struct plcom_softc *sc) 355 { 356 int s; 357 358 /* Turn on line break interrupt, set carrier. */ 359 s = splserial(); 360 PLCOM_LOCK(sc); 361 sc->sc_cr = CR_RIE | CR_RTIE | CR_UARTEN; 362 bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr); 363 SET(sc->sc_mcr, MCR_DTR | MCR_RTS); 364 sc->sc_set_mcr(sc->sc_set_mcr_arg, sc->sc_dev.dv_unit, sc->sc_mcr); 365 PLCOM_UNLOCK(sc); 366 splx(s); 367 } 368 369 void 370 plcom_attach_subr(struct plcom_softc *sc) 371 { 372 int unit = sc->sc_iounit; 373 bus_space_tag_t iot = sc->sc_iot; 374 bus_space_handle_t ioh = sc->sc_ioh; 375 struct tty *tp; 376 377 callout_init(&sc->sc_diag_callout); 378 #if (defined(MULTIPROCESSOR) || defined(LOCKDEBUG)) && defined(PLCOM_MPLOCK) 379 simple_lock_init(&sc->sc_lock); 380 #endif 381 382 /* Disable interrupts before configuring the device. */ 383 sc->sc_cr = 0; 384 385 if (plcomconstag && unit == plcomconsunit) { 386 plcomconsattached = 1; 387 388 plcomconstag = iot; 389 plcomconsioh = ioh; 390 391 /* Make sure the console is always "hardwired". */ 392 delay(1000); /* wait for output to finish */ 393 SET(sc->sc_hwflags, PLCOM_HW_CONSOLE); 394 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 395 /* Must re-enable the console immediately, or we will 396 hang when trying to print. */ 397 sc->sc_cr = CR_UARTEN; 398 } 399 400 bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr); 401 402 /* The PL010 has a 16-byte fifo, but the tx interrupt triggers when 403 there is space for 8 more bytes. */ 404 sc->sc_fifolen = 8; 405 printf("\n"); 406 407 if (ISSET(sc->sc_hwflags, PLCOM_HW_TXFIFO_DISABLE)) { 408 sc->sc_fifolen = 1; 409 printf("%s: txfifo disabled\n", sc->sc_dev.dv_xname); 410 } 411 412 if (sc->sc_fifolen > 1) 413 SET(sc->sc_hwflags, PLCOM_HW_FIFO); 414 415 tp = ttymalloc(); 416 tp->t_oproc = plcomstart; 417 tp->t_param = plcomparam; 418 tp->t_hwiflow = plcomhwiflow; 419 420 sc->sc_tty = tp; 421 sc->sc_rbuf = malloc(plcom_rbuf_size << 1, M_DEVBUF, M_NOWAIT); 422 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 423 sc->sc_rbavail = plcom_rbuf_size; 424 if (sc->sc_rbuf == NULL) { 425 printf("%s: unable to allocate ring buffer\n", 426 sc->sc_dev.dv_xname); 427 return; 428 } 429 sc->sc_ebuf = sc->sc_rbuf + (plcom_rbuf_size << 1); 430 431 tty_attach(tp); 432 433 if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) { 434 int maj; 435 436 /* locate the major number */ 437 for (maj = 0; maj < nchrdev; maj++) 438 if (cdevsw[maj].d_open == plcomopen) 439 break; 440 441 cn_tab->cn_dev = makedev(maj, sc->sc_dev.dv_unit); 442 443 printf("%s: console\n", sc->sc_dev.dv_xname); 444 } 445 446 #ifdef KGDB 447 /* 448 * Allow kgdb to "take over" this port. If this is 449 * the kgdb device, it has exclusive use. 450 */ 451 if (iot == plcom_kgdb_iot && unit == plcom_kgdb_unit) { 452 plcom_kgdb_attached = 1; 453 454 SET(sc->sc_hwflags, PLCOM_HW_KGDB); 455 printf("%s: kgdb\n", sc->sc_dev.dv_xname); 456 } 457 #endif 458 459 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 460 sc->sc_si = softintr_establish(IPL_SOFTSERIAL, plcomsoft, sc); 461 #endif 462 463 #if NRND > 0 && defined(RND_COM) 464 rnd_attach_source(&sc->rnd_source, sc->sc_dev.dv_xname, 465 RND_TYPE_TTY, 0); 466 #endif 467 468 /* if there are no enable/disable functions, assume the device 469 is always enabled */ 470 if (!sc->enable) 471 sc->enabled = 1; 472 473 plcom_config(sc); 474 475 SET(sc->sc_hwflags, PLCOM_HW_DEV_OK); 476 } 477 478 void 479 plcom_config(struct plcom_softc *sc) 480 { 481 bus_space_tag_t iot = sc->sc_iot; 482 bus_space_handle_t ioh = sc->sc_ioh; 483 484 /* Disable interrupts before configuring the device. */ 485 sc->sc_cr = 0; 486 bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr); 487 488 if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE|PLCOM_HW_KGDB)) 489 plcom_enable_debugport(sc); 490 } 491 492 int 493 plcom_detach(self, flags) 494 struct device *self; 495 int flags; 496 { 497 struct plcom_softc *sc = (struct plcom_softc *)self; 498 int maj, mn; 499 500 /* locate the major number */ 501 for (maj = 0; maj < nchrdev; maj++) 502 if (cdevsw[maj].d_open == plcomopen) 503 break; 504 505 /* Nuke the vnodes for any open instances. */ 506 mn = self->dv_unit; 507 vdevgone(maj, mn, mn, VCHR); 508 509 mn |= PLCOMDIALOUT_MASK; 510 vdevgone(maj, mn, mn, VCHR); 511 512 /* Free the receive buffer. */ 513 free(sc->sc_rbuf, M_DEVBUF); 514 515 /* Detach and free the tty. */ 516 tty_detach(sc->sc_tty); 517 ttyfree(sc->sc_tty); 518 519 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 520 /* Unhook the soft interrupt handler. */ 521 softintr_disestablish(sc->sc_si); 522 #endif 523 524 #if NRND > 0 && defined(RND_COM) 525 /* Unhook the entropy source. */ 526 rnd_detach_source(&sc->rnd_source); 527 #endif 528 529 return 0; 530 } 531 532 int 533 plcom_activate(struct device *self, enum devact act) 534 { 535 struct plcom_softc *sc = (struct plcom_softc *)self; 536 int s, rv = 0; 537 538 s = splserial(); 539 PLCOM_LOCK(sc); 540 switch (act) { 541 case DVACT_ACTIVATE: 542 rv = EOPNOTSUPP; 543 break; 544 545 case DVACT_DEACTIVATE: 546 if (sc->sc_hwflags & (PLCOM_HW_CONSOLE|PLCOM_HW_KGDB)) { 547 rv = EBUSY; 548 break; 549 } 550 551 if (sc->disable != NULL && sc->enabled != 0) { 552 (*sc->disable)(sc); 553 sc->enabled = 0; 554 } 555 break; 556 } 557 558 PLCOM_UNLOCK(sc); 559 splx(s); 560 return rv; 561 } 562 563 void 564 plcom_shutdown(struct plcom_softc *sc) 565 { 566 struct tty *tp = sc->sc_tty; 567 int s; 568 569 s = splserial(); 570 PLCOM_LOCK(sc); 571 572 /* If we were asserting flow control, then deassert it. */ 573 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED); 574 plcom_hwiflow(sc); 575 576 /* Clear any break condition set with TIOCSBRK. */ 577 plcom_break(sc, 0); 578 579 /* Turn off PPS capture on last close. */ 580 sc->sc_ppsmask = 0; 581 sc->ppsparam.mode = 0; 582 583 /* 584 * Hang up if necessary. Wait a bit, so the other side has time to 585 * notice even if we immediately open the port again. 586 * Avoid tsleeping above splhigh(). 587 */ 588 if (ISSET(tp->t_cflag, HUPCL)) { 589 plcom_modem(sc, 0); 590 PLCOM_UNLOCK(sc); 591 splx(s); 592 /* XXX tsleep will only timeout */ 593 (void) tsleep(sc, TTIPRI, ttclos, hz); 594 s = splserial(); 595 PLCOM_LOCK(sc); 596 } 597 598 /* Turn off interrupts. */ 599 if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) 600 /* interrupt on break */ 601 sc->sc_cr = CR_RIE | CR_RTIE | CR_UARTEN; 602 else 603 sc->sc_cr = 0; 604 bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr); 605 606 if (sc->disable) { 607 #ifdef DIAGNOSTIC 608 if (!sc->enabled) 609 panic("plcom_shutdown: not enabled?"); 610 #endif 611 (*sc->disable)(sc); 612 sc->enabled = 0; 613 } 614 PLCOM_UNLOCK(sc); 615 splx(s); 616 } 617 618 int 619 plcomopen(dev_t dev, int flag, int mode, struct proc *p) 620 { 621 struct plcom_softc *sc; 622 struct tty *tp; 623 int s, s2; 624 int error; 625 626 sc = device_lookup(&plcom_cd, PLCOMUNIT(dev)); 627 if (sc == NULL || !ISSET(sc->sc_hwflags, PLCOM_HW_DEV_OK) || 628 sc->sc_rbuf == NULL) 629 return ENXIO; 630 631 if (ISSET(sc->sc_dev.dv_flags, DVF_ACTIVE) == 0) 632 return ENXIO; 633 634 #ifdef KGDB 635 /* 636 * If this is the kgdb port, no other use is permitted. 637 */ 638 if (ISSET(sc->sc_hwflags, PLCOM_HW_KGDB)) 639 return EBUSY; 640 #endif 641 642 tp = sc->sc_tty; 643 644 if (ISSET(tp->t_state, TS_ISOPEN) && 645 ISSET(tp->t_state, TS_XCLUDE) && 646 p->p_ucred->cr_uid != 0) 647 return EBUSY; 648 649 s = spltty(); 650 651 /* 652 * Do the following iff this is a first open. 653 */ 654 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 655 struct termios t; 656 657 tp->t_dev = dev; 658 659 s2 = splserial(); 660 PLCOM_LOCK(sc); 661 662 if (sc->enable) { 663 if ((*sc->enable)(sc)) { 664 PLCOM_UNLOCK(sc); 665 splx(s2); 666 splx(s); 667 printf("%s: device enable failed\n", 668 sc->sc_dev.dv_xname); 669 return EIO; 670 } 671 sc->enabled = 1; 672 plcom_config(sc); 673 } 674 675 /* Turn on interrupts. */ 676 /* IER_ERXRDY | IER_ERLS | IER_EMSC; */ 677 sc->sc_cr = CR_RIE | CR_RTIE | CR_MSIE | CR_UARTEN; 678 bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr); 679 680 /* Fetch the current modem control status, needed later. */ 681 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, plcom_fr); 682 683 /* Clear PPS capture state on first open. */ 684 sc->sc_ppsmask = 0; 685 sc->ppsparam.mode = 0; 686 687 PLCOM_UNLOCK(sc); 688 splx(s2); 689 690 /* 691 * Initialize the termios status to the defaults. Add in the 692 * sticky bits from TIOCSFLAGS. 693 */ 694 t.c_ispeed = 0; 695 if (ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) { 696 t.c_ospeed = plcomconsrate; 697 t.c_cflag = plcomconscflag; 698 } else { 699 t.c_ospeed = TTYDEF_SPEED; 700 t.c_cflag = TTYDEF_CFLAG; 701 } 702 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 703 SET(t.c_cflag, CLOCAL); 704 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 705 SET(t.c_cflag, CRTSCTS); 706 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 707 SET(t.c_cflag, MDMBUF); 708 /* Make sure plcomparam() will do something. */ 709 tp->t_ospeed = 0; 710 (void) plcomparam(tp, &t); 711 tp->t_iflag = TTYDEF_IFLAG; 712 tp->t_oflag = TTYDEF_OFLAG; 713 tp->t_lflag = TTYDEF_LFLAG; 714 ttychars(tp); 715 ttsetwater(tp); 716 717 s2 = splserial(); 718 PLCOM_LOCK(sc); 719 720 /* 721 * Turn on DTR. We must always do this, even if carrier is not 722 * present, because otherwise we'd have to use TIOCSDTR 723 * immediately after setting CLOCAL, which applications do not 724 * expect. We always assert DTR while the device is open 725 * unless explicitly requested to deassert it. 726 */ 727 plcom_modem(sc, 1); 728 729 /* Clear the input ring, and unblock. */ 730 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 731 sc->sc_rbavail = plcom_rbuf_size; 732 plcom_iflush(sc); 733 CLR(sc->sc_rx_flags, RX_ANY_BLOCK); 734 plcom_hwiflow(sc); 735 736 #ifdef PLCOM_DEBUG 737 if (plcom_debug) 738 plcomstatus(sc, "plcomopen "); 739 #endif 740 741 PLCOM_UNLOCK(sc); 742 splx(s2); 743 } 744 745 splx(s); 746 747 error = ttyopen(tp, PLCOMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 748 if (error) 749 goto bad; 750 751 error = (*tp->t_linesw->l_open)(dev, tp); 752 if (error) 753 goto bad; 754 755 return 0; 756 757 bad: 758 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 759 /* 760 * We failed to open the device, and nobody else had it opened. 761 * Clean up the state as appropriate. 762 */ 763 plcom_shutdown(sc); 764 } 765 766 return error; 767 } 768 769 int 770 plcomclose(dev_t dev, int flag, int mode, struct proc *p) 771 { 772 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(dev)); 773 struct tty *tp = sc->sc_tty; 774 775 /* XXX This is for cons.c. */ 776 if (!ISSET(tp->t_state, TS_ISOPEN)) 777 return 0; 778 779 (*tp->t_linesw->l_close)(tp, flag); 780 ttyclose(tp); 781 782 if (PLCOM_ISALIVE(sc) == 0) 783 return 0; 784 785 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 786 /* 787 * Although we got a last close, the device may still be in 788 * use; e.g. if this was the dialout node, and there are still 789 * processes waiting for carrier on the non-dialout node. 790 */ 791 plcom_shutdown(sc); 792 } 793 794 return 0; 795 } 796 797 int 798 plcomread(dev_t dev, struct uio *uio, int flag) 799 { 800 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(dev)); 801 struct tty *tp = sc->sc_tty; 802 803 if (PLCOM_ISALIVE(sc) == 0) 804 return EIO; 805 806 return (*tp->t_linesw->l_read)(tp, uio, flag); 807 } 808 809 int 810 plcomwrite(dev_t dev, struct uio *uio, int flag) 811 { 812 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(dev)); 813 struct tty *tp = sc->sc_tty; 814 815 if (PLCOM_ISALIVE(sc) == 0) 816 return EIO; 817 818 return (*tp->t_linesw->l_write)(tp, uio, flag); 819 } 820 821 int 822 plcompoll(dev_t dev, int events, struct proc *p) 823 { 824 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(dev)); 825 struct tty *tp = sc->sc_tty; 826 827 if (PLCOM_ISALIVE(sc) == 0) 828 return EIO; 829 830 return (*tp->t_linesw->l_poll)(tp, events, p); 831 } 832 833 struct tty * 834 plcomtty(dev_t dev) 835 { 836 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(dev)); 837 struct tty *tp = sc->sc_tty; 838 839 return tp; 840 } 841 842 int 843 plcomioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 844 { 845 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(dev)); 846 struct tty *tp = sc->sc_tty; 847 int error; 848 int s; 849 850 if (PLCOM_ISALIVE(sc) == 0) 851 return EIO; 852 853 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p); 854 if (error != EPASSTHROUGH) 855 return error; 856 857 error = ttioctl(tp, cmd, data, flag, p); 858 if (error != EPASSTHROUGH) 859 return error; 860 861 error = 0; 862 863 s = splserial(); 864 PLCOM_LOCK(sc); 865 866 switch (cmd) { 867 case TIOCSBRK: 868 plcom_break(sc, 1); 869 break; 870 871 case TIOCCBRK: 872 plcom_break(sc, 0); 873 break; 874 875 case TIOCSDTR: 876 plcom_modem(sc, 1); 877 break; 878 879 case TIOCCDTR: 880 plcom_modem(sc, 0); 881 break; 882 883 case TIOCGFLAGS: 884 *(int *)data = sc->sc_swflags; 885 break; 886 887 case TIOCSFLAGS: 888 error = suser(p->p_ucred, &p->p_acflag); 889 if (error) 890 break; 891 sc->sc_swflags = *(int *)data; 892 break; 893 894 case TIOCMSET: 895 case TIOCMBIS: 896 case TIOCMBIC: 897 tiocm_to_plcom(sc, cmd, *(int *)data); 898 break; 899 900 case TIOCMGET: 901 *(int *)data = plcom_to_tiocm(sc); 902 break; 903 904 case PPS_IOC_CREATE: 905 break; 906 907 case PPS_IOC_DESTROY: 908 break; 909 910 case PPS_IOC_GETPARAMS: { 911 pps_params_t *pp; 912 pp = (pps_params_t *)data; 913 *pp = sc->ppsparam; 914 break; 915 } 916 917 case PPS_IOC_SETPARAMS: { 918 pps_params_t *pp; 919 int mode; 920 pp = (pps_params_t *)data; 921 if (pp->mode & ~ppscap) { 922 error = EINVAL; 923 break; 924 } 925 sc->ppsparam = *pp; 926 /* 927 * Compute msr masks from user-specified timestamp state. 928 */ 929 mode = sc->ppsparam.mode; 930 #ifdef PPS_SYNC 931 if (mode & PPS_HARDPPSONASSERT) { 932 mode |= PPS_CAPTUREASSERT; 933 /* XXX revoke any previous HARDPPS source */ 934 } 935 if (mode & PPS_HARDPPSONCLEAR) { 936 mode |= PPS_CAPTURECLEAR; 937 /* XXX revoke any previous HARDPPS source */ 938 } 939 #endif /* PPS_SYNC */ 940 switch (mode & PPS_CAPTUREBOTH) { 941 case 0: 942 sc->sc_ppsmask = 0; 943 break; 944 945 case PPS_CAPTUREASSERT: 946 sc->sc_ppsmask = MSR_DCD; 947 sc->sc_ppsassert = MSR_DCD; 948 sc->sc_ppsclear = -1; 949 break; 950 951 case PPS_CAPTURECLEAR: 952 sc->sc_ppsmask = MSR_DCD; 953 sc->sc_ppsassert = -1; 954 sc->sc_ppsclear = 0; 955 break; 956 957 case PPS_CAPTUREBOTH: 958 sc->sc_ppsmask = MSR_DCD; 959 sc->sc_ppsassert = MSR_DCD; 960 sc->sc_ppsclear = 0; 961 break; 962 963 default: 964 error = EINVAL; 965 break; 966 } 967 break; 968 } 969 970 case PPS_IOC_GETCAP: 971 *(int*)data = ppscap; 972 break; 973 974 case PPS_IOC_FETCH: { 975 pps_info_t *pi; 976 pi = (pps_info_t *)data; 977 *pi = sc->ppsinfo; 978 break; 979 } 980 981 case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */ 982 /* 983 * Some GPS clocks models use the falling rather than 984 * rising edge as the on-the-second signal. 985 * The old API has no way to specify PPS polarity. 986 */ 987 sc->sc_ppsmask = MSR_DCD; 988 #ifndef PPS_TRAILING_EDGE 989 sc->sc_ppsassert = MSR_DCD; 990 sc->sc_ppsclear = -1; 991 TIMESPEC_TO_TIMEVAL((struct timeval *)data, 992 &sc->ppsinfo.assert_timestamp); 993 #else 994 sc->sc_ppsassert = -1 995 sc->sc_ppsclear = 0; 996 TIMESPEC_TO_TIMEVAL((struct timeval *)data, 997 &sc->ppsinfo.clear_timestamp); 998 #endif 999 break; 1000 1001 default: 1002 error = EPASSTHROUGH; 1003 break; 1004 } 1005 1006 PLCOM_UNLOCK(sc); 1007 splx(s); 1008 1009 #ifdef PLCOM_DEBUG 1010 if (plcom_debug) 1011 plcomstatus(sc, "plcomioctl "); 1012 #endif 1013 1014 return error; 1015 } 1016 1017 integrate void 1018 plcom_schedrx(struct plcom_softc *sc) 1019 { 1020 1021 sc->sc_rx_ready = 1; 1022 1023 /* Wake up the poller. */ 1024 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 1025 softintr_schedule(sc->sc_si); 1026 #else 1027 #ifndef __NO_SOFT_SERIAL_INTERRUPT 1028 setsoftserial(); 1029 #else 1030 if (!plcom_softintr_scheduled) { 1031 plcom_softintr_scheduled = 1; 1032 callout_reset(&plcomsoft_callout, 1, plcomsoft, NULL); 1033 } 1034 #endif 1035 #endif 1036 } 1037 1038 void 1039 plcom_break(struct plcom_softc *sc, int onoff) 1040 { 1041 1042 if (onoff) 1043 SET(sc->sc_lcr, LCR_BRK); 1044 else 1045 CLR(sc->sc_lcr, LCR_BRK); 1046 1047 if (!sc->sc_heldchange) { 1048 if (sc->sc_tx_busy) { 1049 sc->sc_heldtbc = sc->sc_tbc; 1050 sc->sc_tbc = 0; 1051 sc->sc_heldchange = 1; 1052 } else 1053 plcom_loadchannelregs(sc); 1054 } 1055 } 1056 1057 void 1058 plcom_modem(struct plcom_softc *sc, int onoff) 1059 { 1060 1061 if (sc->sc_mcr_dtr == 0) 1062 return; 1063 1064 if (onoff) 1065 SET(sc->sc_mcr, sc->sc_mcr_dtr); 1066 else 1067 CLR(sc->sc_mcr, sc->sc_mcr_dtr); 1068 1069 if (!sc->sc_heldchange) { 1070 if (sc->sc_tx_busy) { 1071 sc->sc_heldtbc = sc->sc_tbc; 1072 sc->sc_tbc = 0; 1073 sc->sc_heldchange = 1; 1074 } else 1075 plcom_loadchannelregs(sc); 1076 } 1077 } 1078 1079 void 1080 tiocm_to_plcom(struct plcom_softc *sc, u_long how, int ttybits) 1081 { 1082 u_char plcombits; 1083 1084 plcombits = 0; 1085 if (ISSET(ttybits, TIOCM_DTR)) 1086 SET(plcombits, MCR_DTR); 1087 if (ISSET(ttybits, TIOCM_RTS)) 1088 SET(plcombits, MCR_RTS); 1089 1090 switch (how) { 1091 case TIOCMBIC: 1092 CLR(sc->sc_mcr, plcombits); 1093 break; 1094 1095 case TIOCMBIS: 1096 SET(sc->sc_mcr, plcombits); 1097 break; 1098 1099 case TIOCMSET: 1100 CLR(sc->sc_mcr, MCR_DTR | MCR_RTS); 1101 SET(sc->sc_mcr, plcombits); 1102 break; 1103 } 1104 1105 if (!sc->sc_heldchange) { 1106 if (sc->sc_tx_busy) { 1107 sc->sc_heldtbc = sc->sc_tbc; 1108 sc->sc_tbc = 0; 1109 sc->sc_heldchange = 1; 1110 } else 1111 plcom_loadchannelregs(sc); 1112 } 1113 } 1114 1115 int 1116 plcom_to_tiocm(struct plcom_softc *sc) 1117 { 1118 u_char plcombits; 1119 int ttybits = 0; 1120 1121 plcombits = sc->sc_mcr; 1122 if (ISSET(plcombits, MCR_DTR)) 1123 SET(ttybits, TIOCM_DTR); 1124 if (ISSET(plcombits, MCR_RTS)) 1125 SET(ttybits, TIOCM_RTS); 1126 1127 plcombits = sc->sc_msr; 1128 if (ISSET(plcombits, MSR_DCD)) 1129 SET(ttybits, TIOCM_CD); 1130 if (ISSET(plcombits, MSR_CTS)) 1131 SET(ttybits, TIOCM_CTS); 1132 if (ISSET(plcombits, MSR_DSR)) 1133 SET(ttybits, TIOCM_DSR); 1134 1135 if (sc->sc_cr != 0) 1136 SET(ttybits, TIOCM_LE); 1137 1138 return ttybits; 1139 } 1140 1141 static u_char 1142 cflag2lcr(tcflag_t cflag) 1143 { 1144 u_char lcr = 0; 1145 1146 switch (ISSET(cflag, CSIZE)) { 1147 case CS5: 1148 SET(lcr, LCR_5BITS); 1149 break; 1150 case CS6: 1151 SET(lcr, LCR_6BITS); 1152 break; 1153 case CS7: 1154 SET(lcr, LCR_7BITS); 1155 break; 1156 case CS8: 1157 SET(lcr, LCR_8BITS); 1158 break; 1159 } 1160 if (ISSET(cflag, PARENB)) { 1161 SET(lcr, LCR_PEN); 1162 if (!ISSET(cflag, PARODD)) 1163 SET(lcr, LCR_EPS); 1164 } 1165 if (ISSET(cflag, CSTOPB)) 1166 SET(lcr, LCR_STP2); 1167 1168 return lcr; 1169 } 1170 1171 int 1172 plcomparam(struct tty *tp, struct termios *t) 1173 { 1174 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(tp->t_dev)); 1175 int ospeed; 1176 u_char lcr; 1177 int s; 1178 1179 if (PLCOM_ISALIVE(sc) == 0) 1180 return EIO; 1181 1182 ospeed = plcomspeed(t->c_ospeed, sc->sc_frequency); 1183 1184 /* Check requested parameters. */ 1185 if (ospeed < 0) 1186 return EINVAL; 1187 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 1188 return EINVAL; 1189 1190 /* 1191 * For the console, always force CLOCAL and !HUPCL, so that the port 1192 * is always active. 1193 */ 1194 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 1195 ISSET(sc->sc_hwflags, PLCOM_HW_CONSOLE)) { 1196 SET(t->c_cflag, CLOCAL); 1197 CLR(t->c_cflag, HUPCL); 1198 } 1199 1200 /* 1201 * If there were no changes, don't do anything. This avoids dropping 1202 * input and improves performance when all we did was frob things like 1203 * VMIN and VTIME. 1204 */ 1205 if (tp->t_ospeed == t->c_ospeed && 1206 tp->t_cflag == t->c_cflag) 1207 return 0; 1208 1209 lcr = ISSET(sc->sc_lcr, LCR_BRK) | cflag2lcr(t->c_cflag); 1210 1211 s = splserial(); 1212 PLCOM_LOCK(sc); 1213 1214 sc->sc_lcr = lcr; 1215 1216 /* 1217 * PL010 has a fixed-length FIFO trigger point. 1218 */ 1219 if (ISSET(sc->sc_hwflags, PLCOM_HW_FIFO)) 1220 sc->sc_fifo = 1; 1221 else 1222 sc->sc_fifo = 0; 1223 1224 if (sc->sc_fifo) 1225 SET(sc->sc_lcr, LCR_FEN); 1226 1227 /* 1228 * If we're not in a mode that assumes a connection is present, then 1229 * ignore carrier changes. 1230 */ 1231 if (ISSET(t->c_cflag, CLOCAL | MDMBUF)) 1232 sc->sc_msr_dcd = 0; 1233 else 1234 sc->sc_msr_dcd = MSR_DCD; 1235 /* 1236 * Set the flow control pins depending on the current flow control 1237 * mode. 1238 */ 1239 if (ISSET(t->c_cflag, CRTSCTS)) { 1240 sc->sc_mcr_dtr = MCR_DTR; 1241 sc->sc_mcr_rts = MCR_RTS; 1242 sc->sc_msr_cts = MSR_CTS; 1243 } else if (ISSET(t->c_cflag, MDMBUF)) { 1244 /* 1245 * For DTR/DCD flow control, make sure we don't toggle DTR for 1246 * carrier detection. 1247 */ 1248 sc->sc_mcr_dtr = 0; 1249 sc->sc_mcr_rts = MCR_DTR; 1250 sc->sc_msr_cts = MSR_DCD; 1251 } else { 1252 /* 1253 * If no flow control, then always set RTS. This will make 1254 * the other side happy if it mistakenly thinks we're doing 1255 * RTS/CTS flow control. 1256 */ 1257 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS; 1258 sc->sc_mcr_rts = 0; 1259 sc->sc_msr_cts = 0; 1260 if (ISSET(sc->sc_mcr, MCR_DTR)) 1261 SET(sc->sc_mcr, MCR_RTS); 1262 else 1263 CLR(sc->sc_mcr, MCR_RTS); 1264 } 1265 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd; 1266 1267 #if 0 1268 if (ospeed == 0) 1269 CLR(sc->sc_mcr, sc->sc_mcr_dtr); 1270 else 1271 SET(sc->sc_mcr, sc->sc_mcr_dtr); 1272 #endif 1273 1274 sc->sc_dlbl = ospeed; 1275 sc->sc_dlbh = ospeed >> 8; 1276 1277 /* And copy to tty. */ 1278 tp->t_ispeed = 0; 1279 tp->t_ospeed = t->c_ospeed; 1280 tp->t_cflag = t->c_cflag; 1281 1282 if (!sc->sc_heldchange) { 1283 if (sc->sc_tx_busy) { 1284 sc->sc_heldtbc = sc->sc_tbc; 1285 sc->sc_tbc = 0; 1286 sc->sc_heldchange = 1; 1287 } else 1288 plcom_loadchannelregs(sc); 1289 } 1290 1291 if (!ISSET(t->c_cflag, CHWFLOW)) { 1292 /* Disable the high water mark. */ 1293 sc->sc_r_hiwat = 0; 1294 sc->sc_r_lowat = 0; 1295 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) { 1296 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 1297 plcom_schedrx(sc); 1298 } 1299 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) { 1300 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED); 1301 plcom_hwiflow(sc); 1302 } 1303 } else { 1304 sc->sc_r_hiwat = plcom_rbuf_hiwat; 1305 sc->sc_r_lowat = plcom_rbuf_lowat; 1306 } 1307 1308 PLCOM_UNLOCK(sc); 1309 splx(s); 1310 1311 /* 1312 * Update the tty layer's idea of the carrier bit, in case we changed 1313 * CLOCAL or MDMBUF. We don't hang up here; we only do that by 1314 * explicit request. 1315 */ 1316 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MSR_DCD)); 1317 1318 #ifdef PLCOM_DEBUG 1319 if (plcom_debug) 1320 plcomstatus(sc, "plcomparam "); 1321 #endif 1322 1323 if (!ISSET(t->c_cflag, CHWFLOW)) { 1324 if (sc->sc_tx_stopped) { 1325 sc->sc_tx_stopped = 0; 1326 plcomstart(tp); 1327 } 1328 } 1329 1330 return 0; 1331 } 1332 1333 void 1334 plcom_iflush(struct plcom_softc *sc) 1335 { 1336 bus_space_tag_t iot = sc->sc_iot; 1337 bus_space_handle_t ioh = sc->sc_ioh; 1338 #ifdef DIAGNOSTIC 1339 int reg; 1340 #endif 1341 int timo; 1342 1343 #ifdef DIAGNOSTIC 1344 reg = 0xffff; 1345 #endif 1346 timo = 50000; 1347 /* flush any pending I/O */ 1348 while (! ISSET(bus_space_read_1(iot, ioh, plcom_fr), FR_RXFE) 1349 && --timo) 1350 #ifdef DIAGNOSTIC 1351 reg = 1352 #else 1353 (void) 1354 #endif 1355 bus_space_read_1(iot, ioh, plcom_dr); 1356 #ifdef DIAGNOSTIC 1357 if (!timo) 1358 printf("%s: plcom_iflush timeout %02x\n", sc->sc_dev.dv_xname, 1359 reg); 1360 #endif 1361 } 1362 1363 void 1364 plcom_loadchannelregs(struct plcom_softc *sc) 1365 { 1366 bus_space_tag_t iot = sc->sc_iot; 1367 bus_space_handle_t ioh = sc->sc_ioh; 1368 1369 /* XXXXX necessary? */ 1370 plcom_iflush(sc); 1371 1372 bus_space_write_1(iot, ioh, plcom_cr, 0); 1373 1374 bus_space_write_1(iot, ioh, plcom_dlbl, sc->sc_dlbl); 1375 bus_space_write_1(iot, ioh, plcom_dlbh, sc->sc_dlbh); 1376 bus_space_write_1(iot, ioh, plcom_lcr, sc->sc_lcr); 1377 sc->sc_set_mcr(sc->sc_set_mcr_arg, sc->sc_dev.dv_unit, 1378 sc->sc_mcr_active = sc->sc_mcr); 1379 1380 bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr); 1381 } 1382 1383 int 1384 plcomhwiflow(struct tty *tp, int block) 1385 { 1386 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(tp->t_dev)); 1387 int s; 1388 1389 if (PLCOM_ISALIVE(sc) == 0) 1390 return 0; 1391 1392 if (sc->sc_mcr_rts == 0) 1393 return 0; 1394 1395 s = splserial(); 1396 PLCOM_LOCK(sc); 1397 1398 if (block) { 1399 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 1400 SET(sc->sc_rx_flags, RX_TTY_BLOCKED); 1401 plcom_hwiflow(sc); 1402 } 1403 } else { 1404 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) { 1405 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 1406 plcom_schedrx(sc); 1407 } 1408 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 1409 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED); 1410 plcom_hwiflow(sc); 1411 } 1412 } 1413 1414 PLCOM_UNLOCK(sc); 1415 splx(s); 1416 return 1; 1417 } 1418 1419 /* 1420 * (un)block input via hw flowcontrol 1421 */ 1422 void 1423 plcom_hwiflow(struct plcom_softc *sc) 1424 { 1425 if (sc->sc_mcr_rts == 0) 1426 return; 1427 1428 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) { 1429 CLR(sc->sc_mcr, sc->sc_mcr_rts); 1430 CLR(sc->sc_mcr_active, sc->sc_mcr_rts); 1431 } else { 1432 SET(sc->sc_mcr, sc->sc_mcr_rts); 1433 SET(sc->sc_mcr_active, sc->sc_mcr_rts); 1434 } 1435 sc->sc_set_mcr(sc->sc_set_mcr_arg, sc->sc_dev.dv_unit, 1436 sc->sc_mcr_active); 1437 } 1438 1439 1440 void 1441 plcomstart(struct tty *tp) 1442 { 1443 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(tp->t_dev)); 1444 bus_space_tag_t iot = sc->sc_iot; 1445 bus_space_handle_t ioh = sc->sc_ioh; 1446 int s; 1447 1448 if (PLCOM_ISALIVE(sc) == 0) 1449 return; 1450 1451 s = spltty(); 1452 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 1453 goto out; 1454 if (sc->sc_tx_stopped) 1455 goto out; 1456 1457 if (tp->t_outq.c_cc <= tp->t_lowat) { 1458 if (ISSET(tp->t_state, TS_ASLEEP)) { 1459 CLR(tp->t_state, TS_ASLEEP); 1460 wakeup(&tp->t_outq); 1461 } 1462 selwakeup(&tp->t_wsel); 1463 if (tp->t_outq.c_cc == 0) 1464 goto out; 1465 } 1466 1467 /* Grab the first contiguous region of buffer space. */ 1468 { 1469 u_char *tba; 1470 int tbc; 1471 1472 tba = tp->t_outq.c_cf; 1473 tbc = ndqb(&tp->t_outq, 0); 1474 1475 (void)splserial(); 1476 PLCOM_LOCK(sc); 1477 1478 sc->sc_tba = tba; 1479 sc->sc_tbc = tbc; 1480 } 1481 1482 SET(tp->t_state, TS_BUSY); 1483 sc->sc_tx_busy = 1; 1484 1485 /* Enable transmit completion interrupts if necessary. */ 1486 if (!ISSET(sc->sc_cr, CR_TIE)) { 1487 SET(sc->sc_cr, CR_TIE); 1488 bus_space_write_1(iot, ioh, plcom_cr, sc->sc_cr); 1489 } 1490 1491 /* Output the first chunk of the contiguous buffer. */ 1492 { 1493 int n; 1494 1495 n = sc->sc_tbc; 1496 if (n > sc->sc_fifolen) 1497 n = sc->sc_fifolen; 1498 bus_space_write_multi_1(iot, ioh, plcom_dr, sc->sc_tba, n); 1499 sc->sc_tbc -= n; 1500 sc->sc_tba += n; 1501 } 1502 PLCOM_UNLOCK(sc); 1503 out: 1504 splx(s); 1505 return; 1506 } 1507 1508 /* 1509 * Stop output on a line. 1510 */ 1511 void 1512 plcomstop(struct tty *tp, int flag) 1513 { 1514 struct plcom_softc *sc = device_lookup(&plcom_cd, PLCOMUNIT(tp->t_dev)); 1515 int s; 1516 1517 s = splserial(); 1518 PLCOM_LOCK(sc); 1519 if (ISSET(tp->t_state, TS_BUSY)) { 1520 /* Stop transmitting at the next chunk. */ 1521 sc->sc_tbc = 0; 1522 sc->sc_heldtbc = 0; 1523 if (!ISSET(tp->t_state, TS_TTSTOP)) 1524 SET(tp->t_state, TS_FLUSH); 1525 } 1526 PLCOM_UNLOCK(sc); 1527 splx(s); 1528 } 1529 1530 void 1531 plcomdiag(void *arg) 1532 { 1533 struct plcom_softc *sc = arg; 1534 int overflows, floods; 1535 int s; 1536 1537 s = splserial(); 1538 PLCOM_LOCK(sc); 1539 overflows = sc->sc_overflows; 1540 sc->sc_overflows = 0; 1541 floods = sc->sc_floods; 1542 sc->sc_floods = 0; 1543 sc->sc_errors = 0; 1544 PLCOM_UNLOCK(sc); 1545 splx(s); 1546 1547 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n", 1548 sc->sc_dev.dv_xname, 1549 overflows, overflows == 1 ? "" : "s", 1550 floods, floods == 1 ? "" : "s"); 1551 } 1552 1553 integrate void 1554 plcom_rxsoft(struct plcom_softc *sc, struct tty *tp) 1555 { 1556 int (*rint) (int, struct tty *) = tp->t_linesw->l_rint; 1557 u_char *get, *end; 1558 u_int cc, scc; 1559 u_char rsr; 1560 int code; 1561 int s; 1562 1563 end = sc->sc_ebuf; 1564 get = sc->sc_rbget; 1565 scc = cc = plcom_rbuf_size - sc->sc_rbavail; 1566 1567 if (cc == plcom_rbuf_size) { 1568 sc->sc_floods++; 1569 if (sc->sc_errors++ == 0) 1570 callout_reset(&sc->sc_diag_callout, 60 * hz, 1571 plcomdiag, sc); 1572 } 1573 1574 while (cc) { 1575 code = get[0]; 1576 rsr = get[1]; 1577 if (ISSET(rsr, RSR_OE | RSR_BE | RSR_FE | RSR_PE)) { 1578 if (ISSET(rsr, RSR_OE)) { 1579 sc->sc_overflows++; 1580 if (sc->sc_errors++ == 0) 1581 callout_reset(&sc->sc_diag_callout, 1582 60 * hz, plcomdiag, sc); 1583 } 1584 if (ISSET(rsr, RSR_BE | RSR_FE)) 1585 SET(code, TTY_FE); 1586 if (ISSET(rsr, RSR_PE)) 1587 SET(code, TTY_PE); 1588 } 1589 if ((*rint)(code, tp) == -1) { 1590 /* 1591 * The line discipline's buffer is out of space. 1592 */ 1593 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 1594 /* 1595 * We're either not using flow control, or the 1596 * line discipline didn't tell us to block for 1597 * some reason. Either way, we have no way to 1598 * know when there's more space available, so 1599 * just drop the rest of the data. 1600 */ 1601 get += cc << 1; 1602 if (get >= end) 1603 get -= plcom_rbuf_size << 1; 1604 cc = 0; 1605 } else { 1606 /* 1607 * Don't schedule any more receive processing 1608 * until the line discipline tells us there's 1609 * space available (through plcomhwiflow()). 1610 * Leave the rest of the data in the input 1611 * buffer. 1612 */ 1613 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 1614 } 1615 break; 1616 } 1617 get += 2; 1618 if (get >= end) 1619 get = sc->sc_rbuf; 1620 cc--; 1621 } 1622 1623 if (cc != scc) { 1624 sc->sc_rbget = get; 1625 s = splserial(); 1626 PLCOM_LOCK(sc); 1627 1628 cc = sc->sc_rbavail += scc - cc; 1629 /* Buffers should be ok again, release possible block. */ 1630 if (cc >= sc->sc_r_lowat) { 1631 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 1632 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1633 SET(sc->sc_cr, CR_RIE | CR_RTIE); 1634 bus_space_write_1(sc->sc_iot, sc->sc_ioh, plcom_cr, sc->sc_cr); 1635 } 1636 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) { 1637 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED); 1638 plcom_hwiflow(sc); 1639 } 1640 } 1641 PLCOM_UNLOCK(sc); 1642 splx(s); 1643 } 1644 } 1645 1646 integrate void 1647 plcom_txsoft(struct plcom_softc *sc, struct tty *tp) 1648 { 1649 1650 CLR(tp->t_state, TS_BUSY); 1651 if (ISSET(tp->t_state, TS_FLUSH)) 1652 CLR(tp->t_state, TS_FLUSH); 1653 else 1654 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf)); 1655 (*tp->t_linesw->l_start)(tp); 1656 } 1657 1658 integrate void 1659 plcom_stsoft(struct plcom_softc *sc, struct tty *tp) 1660 { 1661 u_char msr, delta; 1662 int s; 1663 1664 s = splserial(); 1665 PLCOM_LOCK(sc); 1666 msr = sc->sc_msr; 1667 delta = sc->sc_msr_delta; 1668 sc->sc_msr_delta = 0; 1669 PLCOM_UNLOCK(sc); 1670 splx(s); 1671 1672 if (ISSET(delta, sc->sc_msr_dcd)) { 1673 /* 1674 * Inform the tty layer that carrier detect changed. 1675 */ 1676 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD)); 1677 } 1678 1679 if (ISSET(delta, sc->sc_msr_cts)) { 1680 /* Block or unblock output according to flow control. */ 1681 if (ISSET(msr, sc->sc_msr_cts)) { 1682 sc->sc_tx_stopped = 0; 1683 (*tp->t_linesw->l_start)(tp); 1684 } else { 1685 sc->sc_tx_stopped = 1; 1686 } 1687 } 1688 1689 #ifdef PLCOM_DEBUG 1690 if (plcom_debug) 1691 plcomstatus(sc, "plcom_stsoft"); 1692 #endif 1693 } 1694 1695 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 1696 void 1697 plcomsoft(void *arg) 1698 { 1699 struct plcom_softc *sc = arg; 1700 struct tty *tp; 1701 1702 if (PLCOM_ISALIVE(sc) == 0) 1703 return; 1704 1705 { 1706 #else 1707 void 1708 #ifndef __NO_SOFT_SERIAL_INTERRUPT 1709 plcomsoft(void) 1710 #else 1711 plcomsoft(void *arg) 1712 #endif 1713 { 1714 struct plcom_softc *sc; 1715 struct tty *tp; 1716 int unit; 1717 #ifdef __NO_SOFT_SERIAL_INTERRUPT 1718 int s; 1719 1720 s = splsoftserial(); 1721 plcom_softintr_scheduled = 0; 1722 #endif 1723 1724 for (unit = 0; unit < plcom_cd.cd_ndevs; unit++) { 1725 sc = device_lookup(&plcom_cd, unit); 1726 if (sc == NULL || !ISSET(sc->sc_hwflags, PLCOM_HW_DEV_OK)) 1727 continue; 1728 1729 if (PLCOM_ISALIVE(sc) == 0) 1730 continue; 1731 1732 tp = sc->sc_tty; 1733 if (tp == NULL) 1734 continue; 1735 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) 1736 continue; 1737 #endif 1738 tp = sc->sc_tty; 1739 1740 if (sc->sc_rx_ready) { 1741 sc->sc_rx_ready = 0; 1742 plcom_rxsoft(sc, tp); 1743 } 1744 1745 if (sc->sc_st_check) { 1746 sc->sc_st_check = 0; 1747 plcom_stsoft(sc, tp); 1748 } 1749 1750 if (sc->sc_tx_done) { 1751 sc->sc_tx_done = 0; 1752 plcom_txsoft(sc, tp); 1753 } 1754 } 1755 1756 #ifndef __HAVE_GENERIC_SOFT_INTERRUPTS 1757 #ifdef __NO_SOFT_SERIAL_INTERRUPT 1758 splx(s); 1759 #endif 1760 #endif 1761 } 1762 1763 #ifdef __ALIGN_BRACKET_LEVEL_FOR_CTAGS 1764 /* there has got to be a better way to do plcomsoft() */ 1765 }} 1766 #endif 1767 1768 int 1769 plcomintr(void *arg) 1770 { 1771 struct plcom_softc *sc = arg; 1772 bus_space_tag_t iot = sc->sc_iot; 1773 bus_space_handle_t ioh = sc->sc_ioh; 1774 u_char *put, *end; 1775 u_int cc; 1776 u_char rsr, iir; 1777 1778 if (PLCOM_ISALIVE(sc) == 0) 1779 return 0; 1780 1781 PLCOM_LOCK(sc); 1782 iir = bus_space_read_1(iot, ioh, plcom_iir); 1783 if (! ISSET(iir, IIR_IMASK)) { 1784 PLCOM_UNLOCK(sc); 1785 return 0; 1786 } 1787 1788 end = sc->sc_ebuf; 1789 put = sc->sc_rbput; 1790 cc = sc->sc_rbavail; 1791 1792 do { 1793 u_char msr, delta, fr; 1794 1795 fr = bus_space_read_1(iot, ioh, plcom_fr); 1796 1797 if (!ISSET(fr, FR_RXFE) && 1798 !ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 1799 while (cc > 0) { 1800 int cn_trapped = 0; 1801 put[0] = bus_space_read_1(iot, ioh, 1802 plcom_dr); 1803 rsr = bus_space_read_1(iot, ioh, plcom_rsr); 1804 /* Clear any error status. */ 1805 if (ISSET(rsr, 1806 (RSR_BE | RSR_OE | RSR_PE | RSR_FE))) 1807 bus_space_write_1(iot, ioh, plcom_ecr, 1808 0); 1809 if (ISSET(rsr, RSR_BE)) { 1810 int cn_trapped = 0; 1811 cn_check_magic(sc->sc_tty->t_dev, 1812 CNC_BREAK, plcom_cnm_state); 1813 if (cn_trapped) 1814 continue; 1815 #if defined(KGDB) 1816 if (ISSET(sc->sc_hwflags, 1817 PLCOM_HW_KGDB)) { 1818 kgdb_connect(1); 1819 continue; 1820 } 1821 #endif 1822 } 1823 1824 put[1] = rsr; 1825 cn_check_magic(sc->sc_tty->t_dev, 1826 put[0], plcom_cnm_state); 1827 if (cn_trapped) { 1828 fr = bus_space_read_1(iot, ioh, 1829 plcom_fr); 1830 if (ISSET(fr, FR_RXFE)) 1831 break; 1832 1833 continue; 1834 } 1835 put += 2; 1836 if (put >= end) 1837 put = sc->sc_rbuf; 1838 cc--; 1839 1840 fr = bus_space_read_1(iot, ioh, plcom_fr); 1841 if (ISSET(fr, FR_RXFE)) 1842 break; 1843 } 1844 1845 /* 1846 * Current string of incoming characters ended because 1847 * no more data was available or we ran out of space. 1848 * Schedule a receive event if any data was received. 1849 * If we're out of space, turn off receive interrupts. 1850 */ 1851 sc->sc_rbput = put; 1852 sc->sc_rbavail = cc; 1853 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) 1854 sc->sc_rx_ready = 1; 1855 1856 /* 1857 * See if we are in danger of overflowing a buffer. If 1858 * so, use hardware flow control to ease the pressure. 1859 */ 1860 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) && 1861 cc < sc->sc_r_hiwat) { 1862 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED); 1863 plcom_hwiflow(sc); 1864 } 1865 1866 /* 1867 * If we're out of space, disable receive interrupts 1868 * until the queue has drained a bit. 1869 */ 1870 if (!cc) { 1871 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1872 CLR(sc->sc_cr, CR_RIE | CR_RTIE); 1873 bus_space_write_1(iot, ioh, plcom_cr, 1874 sc->sc_cr); 1875 } 1876 } else { 1877 if (ISSET(iir, IIR_RIS)) { 1878 bus_space_write_1(iot, ioh, plcom_cr, 0); 1879 delay(10); 1880 bus_space_write_1(iot, ioh, plcom_cr, 1881 sc->sc_cr); 1882 continue; 1883 } 1884 } 1885 1886 msr = bus_space_read_1(iot, ioh, plcom_fr); 1887 delta = msr ^ sc->sc_msr; 1888 sc->sc_msr = msr; 1889 /* Clear any pending modem status interrupt. */ 1890 if (iir & IIR_MIS) 1891 bus_space_write_1(iot, ioh, plcom_icr, 0); 1892 /* 1893 * Pulse-per-second (PSS) signals on edge of DCD? 1894 * Process these even if line discipline is ignoring DCD. 1895 */ 1896 if (delta & sc->sc_ppsmask) { 1897 struct timeval tv; 1898 if ((msr & sc->sc_ppsmask) == sc->sc_ppsassert) { 1899 /* XXX nanotime() */ 1900 microtime(&tv); 1901 TIMEVAL_TO_TIMESPEC(&tv, 1902 &sc->ppsinfo.assert_timestamp); 1903 if (sc->ppsparam.mode & PPS_OFFSETASSERT) { 1904 timespecadd(&sc->ppsinfo.assert_timestamp, 1905 &sc->ppsparam.assert_offset, 1906 &sc->ppsinfo.assert_timestamp); 1907 } 1908 1909 #ifdef PPS_SYNC 1910 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT) 1911 hardpps(&tv, tv.tv_usec); 1912 #endif 1913 sc->ppsinfo.assert_sequence++; 1914 sc->ppsinfo.current_mode = sc->ppsparam.mode; 1915 1916 } else if ((msr & sc->sc_ppsmask) == sc->sc_ppsclear) { 1917 /* XXX nanotime() */ 1918 microtime(&tv); 1919 TIMEVAL_TO_TIMESPEC(&tv, 1920 &sc->ppsinfo.clear_timestamp); 1921 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) { 1922 timespecadd(&sc->ppsinfo.clear_timestamp, 1923 &sc->ppsparam.clear_offset, 1924 &sc->ppsinfo.clear_timestamp); 1925 } 1926 1927 #ifdef PPS_SYNC 1928 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR) 1929 hardpps(&tv, tv.tv_usec); 1930 #endif 1931 sc->ppsinfo.clear_sequence++; 1932 sc->ppsinfo.current_mode = sc->ppsparam.mode; 1933 } 1934 } 1935 1936 /* 1937 * Process normal status changes 1938 */ 1939 if (ISSET(delta, sc->sc_msr_mask)) { 1940 SET(sc->sc_msr_delta, delta); 1941 1942 /* 1943 * Stop output immediately if we lose the output 1944 * flow control signal or carrier detect. 1945 */ 1946 if (ISSET(~msr, sc->sc_msr_mask)) { 1947 sc->sc_tbc = 0; 1948 sc->sc_heldtbc = 0; 1949 #ifdef PLCOM_DEBUG 1950 if (plcom_debug) 1951 plcomstatus(sc, "plcomintr "); 1952 #endif 1953 } 1954 1955 sc->sc_st_check = 1; 1956 } 1957 1958 /* 1959 * Done handling any receive interrupts. See if data 1960 * can be * transmitted as well. Schedule tx done 1961 * event if no data left * and tty was marked busy. 1962 */ 1963 if (ISSET(iir, IIR_TIS)) { 1964 /* 1965 * If we've delayed a parameter change, do it 1966 * now, and restart * output. 1967 */ 1968 if (sc->sc_heldchange) { 1969 plcom_loadchannelregs(sc); 1970 sc->sc_heldchange = 0; 1971 sc->sc_tbc = sc->sc_heldtbc; 1972 sc->sc_heldtbc = 0; 1973 } 1974 1975 /* 1976 * Output the next chunk of the contiguous 1977 * buffer, if any. 1978 */ 1979 if (sc->sc_tbc > 0) { 1980 int n; 1981 1982 n = sc->sc_tbc; 1983 if (n > sc->sc_fifolen) 1984 n = sc->sc_fifolen; 1985 bus_space_write_multi_1(iot, ioh, plcom_dr, 1986 sc->sc_tba, n); 1987 sc->sc_tbc -= n; 1988 sc->sc_tba += n; 1989 } else { 1990 /* 1991 * Disable transmit plcompletion 1992 * interrupts if necessary. 1993 */ 1994 if (ISSET(sc->sc_cr, CR_TIE)) { 1995 CLR(sc->sc_cr, CR_TIE); 1996 bus_space_write_1(iot, ioh, plcom_cr, 1997 sc->sc_cr); 1998 } 1999 if (sc->sc_tx_busy) { 2000 sc->sc_tx_busy = 0; 2001 sc->sc_tx_done = 1; 2002 } 2003 } 2004 } 2005 } while (ISSET((iir = bus_space_read_1(iot, ioh, plcom_iir)), 2006 IIR_IMASK)); 2007 2008 PLCOM_UNLOCK(sc); 2009 2010 /* Wake up the poller. */ 2011 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 2012 softintr_schedule(sc->sc_si); 2013 #else 2014 #ifndef __NO_SOFT_SERIAL_INTERRUPT 2015 setsoftserial(); 2016 #else 2017 if (!plcom_softintr_scheduled) { 2018 plcom_softintr_scheduled = 1; 2019 callout_reset(&plcomsoft_callout, 1, plcomsoft, NULL); 2020 } 2021 #endif 2022 #endif 2023 2024 #if NRND > 0 && defined(RND_COM) 2025 rnd_add_uint32(&sc->rnd_source, iir | rsr); 2026 #endif 2027 2028 return 1; 2029 } 2030 2031 /* 2032 * The following functions are polled getc and putc routines, shared 2033 * by the console and kgdb glue. 2034 * 2035 * The read-ahead code is so that you can detect pending in-band 2036 * cn_magic in polled mode while doing output rather than having to 2037 * wait until the kernel decides it needs input. 2038 */ 2039 2040 #define MAX_READAHEAD 20 2041 static int plcom_readahead[MAX_READAHEAD]; 2042 static int plcom_readaheadcount = 0; 2043 2044 int 2045 plcom_common_getc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh) 2046 { 2047 int s = splserial(); 2048 u_char stat, c; 2049 2050 /* got a character from reading things earlier */ 2051 if (plcom_readaheadcount > 0) { 2052 int i; 2053 2054 c = plcom_readahead[0]; 2055 for (i = 1; i < plcom_readaheadcount; i++) { 2056 plcom_readahead[i-1] = plcom_readahead[i]; 2057 } 2058 plcom_readaheadcount--; 2059 splx(s); 2060 return c; 2061 } 2062 2063 /* block until a character becomes available */ 2064 while (ISSET(stat = bus_space_read_1(iot, ioh, plcom_fr), FR_RXFE)) 2065 ; 2066 2067 c = bus_space_read_1(iot, ioh, plcom_dr); 2068 stat = bus_space_read_1(iot, ioh, plcom_iir); 2069 { 2070 int cn_trapped = 0; /* unused */ 2071 #ifdef DDB 2072 extern int db_active; 2073 if (!db_active) 2074 #endif 2075 cn_check_magic(dev, c, plcom_cnm_state); 2076 } 2077 splx(s); 2078 return c; 2079 } 2080 2081 void 2082 plcom_common_putc(dev_t dev, bus_space_tag_t iot, bus_space_handle_t ioh, 2083 int c) 2084 { 2085 int s = splserial(); 2086 int timo; 2087 2088 int cin, stat; 2089 if (plcom_readaheadcount < MAX_READAHEAD 2090 && !ISSET(stat = bus_space_read_1(iot, ioh, plcom_fr), FR_RXFE)) { 2091 int cn_trapped = 0; 2092 cin = bus_space_read_1(iot, ioh, plcom_dr); 2093 stat = bus_space_read_1(iot, ioh, plcom_iir); 2094 cn_check_magic(dev, cin, plcom_cnm_state); 2095 plcom_readahead[plcom_readaheadcount++] = cin; 2096 } 2097 2098 /* wait for any pending transmission to finish */ 2099 timo = 150000; 2100 while (!ISSET(bus_space_read_1(iot, ioh, plcom_fr), FR_TXFE) && --timo) 2101 continue; 2102 2103 bus_space_write_1(iot, ioh, plcom_dr, c); 2104 PLCOM_BARRIER(iot, ioh, BR | BW); 2105 2106 /* wait for this transmission to complete */ 2107 timo = 1500000; 2108 while (!ISSET(bus_space_read_1(iot, ioh, plcom_fr), FR_TXFE) && --timo) 2109 continue; 2110 2111 splx(s); 2112 } 2113 2114 /* 2115 * Initialize UART for use as console or KGDB line. 2116 */ 2117 int 2118 plcominit(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency, 2119 tcflag_t cflag, bus_space_handle_t *iohp) 2120 { 2121 bus_space_handle_t ioh; 2122 2123 if (bus_space_map(iot, iobase, PLCOM_UART_SIZE, 0, &ioh)) 2124 return ENOMEM; /* ??? */ 2125 2126 rate = plcomspeed(rate, frequency); 2127 bus_space_write_1(iot, ioh, plcom_cr, 0); 2128 bus_space_write_1(iot, ioh, plcom_dlbl, rate); 2129 bus_space_write_1(iot, ioh, plcom_dlbh, rate >> 8); 2130 bus_space_write_1(iot, ioh, plcom_lcr, cflag2lcr(cflag) | LCR_FEN); 2131 bus_space_write_1(iot, ioh, plcom_cr, CR_UARTEN); 2132 2133 #if 0 2134 /* Ought to do something like this, but we have no sc to 2135 dereference. */ 2136 sc->sc_set_mcr(sc->sc_set_mcr_arg, sc->sc_dev.dv_unit, 2137 MCR_DTR | MCR_RTS); 2138 #endif 2139 2140 *iohp = ioh; 2141 return 0; 2142 } 2143 2144 /* 2145 * Following are all routines needed for PLCOM to act as console 2146 */ 2147 struct consdev plcomcons = { 2148 NULL, NULL, plcomcngetc, plcomcnputc, plcomcnpollc, NULL, 2149 NODEV, CN_NORMAL 2150 }; 2151 2152 2153 int 2154 plcomcnattach(bus_space_tag_t iot, bus_addr_t iobase, int rate, int frequency, 2155 tcflag_t cflag, int unit) 2156 { 2157 int res; 2158 2159 res = plcominit(iot, iobase, rate, frequency, cflag, &plcomconsioh); 2160 if (res) 2161 return res; 2162 2163 cn_tab = &plcomcons; 2164 cn_init_magic(&plcom_cnm_state); 2165 cn_set_magic("\047\001"); /* default magic is BREAK */ 2166 2167 plcomconstag = iot; 2168 plcomconsunit = unit; 2169 plcomconsrate = rate; 2170 plcomconscflag = cflag; 2171 2172 return 0; 2173 } 2174 2175 void 2176 plcomcndetach(void) 2177 { 2178 bus_space_unmap(plcomconstag, plcomconsioh, PLCOM_UART_SIZE); 2179 plcomconstag = NULL; 2180 2181 cn_tab = NULL; 2182 } 2183 2184 int 2185 plcomcngetc(dev_t dev) 2186 { 2187 return plcom_common_getc(dev, plcomconstag, plcomconsioh); 2188 } 2189 2190 /* 2191 * Console kernel output character routine. 2192 */ 2193 void 2194 plcomcnputc(dev_t dev, int c) 2195 { 2196 plcom_common_putc(dev, plcomconstag, plcomconsioh, c); 2197 } 2198 2199 void 2200 plcomcnpollc(dev_t dev, int on) 2201 { 2202 2203 } 2204 2205 #ifdef KGDB 2206 int 2207 plcom_kgdb_attach(bus_space_tag_t iot, bus_addr_t iobase, int rate, 2208 int frequency, tcflag_t cflag, int unit) 2209 { 2210 int res; 2211 2212 if (iot == plcomconstag && iobase == plcomconsunit) 2213 return EBUSY; /* cannot share with console */ 2214 2215 res = plcominit(iot, iobase, rate, frequency, cflag, &plcom_kgdb_ioh); 2216 if (res) 2217 return res; 2218 2219 kgdb_attach(plcom_kgdb_getc, plcom_kgdb_putc, NULL); 2220 kgdb_dev = 123; /* unneeded, only to satisfy some tests */ 2221 2222 plcom_kgdb_iot = iot; 2223 plcom_kgdb_unit = unit; 2224 2225 return 0; 2226 } 2227 2228 /* ARGSUSED */ 2229 int 2230 plcom_kgdb_getc(void *arg) 2231 { 2232 return plcom_common_getc(NODEV, plcom_kgdb_iot, plcom_kgdb_ioh); 2233 } 2234 2235 /* ARGSUSED */ 2236 void 2237 plcom_kgdb_putc(void *arg, int c) 2238 { 2239 plcom_common_putc(NODEV, plcom_kgdb_iot, plcom_kgdb_ioh, c); 2240 } 2241 #endif /* KGDB */ 2242 2243 /* helper function to identify the plcom ports used by 2244 console or KGDB (and not yet autoconf attached) */ 2245 int 2246 plcom_is_console(bus_space_tag_t iot, int unit, 2247 bus_space_handle_t *ioh) 2248 { 2249 bus_space_handle_t help; 2250 2251 if (!plcomconsattached && 2252 iot == plcomconstag && unit == plcomconsunit) 2253 help = plcomconsioh; 2254 #ifdef KGDB 2255 else if (!plcom_kgdb_attached && 2256 iot == plcom_kgdb_iot && unit == plcom_kgdb_unit) 2257 help = plcom_kgdb_ioh; 2258 #endif 2259 else 2260 return 0; 2261 2262 if (ioh) 2263 *ioh = help; 2264 return 1; 2265 } 2266