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