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