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