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