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