1 /* $Id: at91dbgu.c,v 1.21 2026/11/10 03:04:58 thorpej Exp $ */ 2 /* $NetBSD: at91dbgu.c,v 1.21 2026/11/10 03:04:58 thorpej Exp $ */ 3 4 /* 5 * 6 * Copyright (c) 1998, 1999, 2001, 2002, 2004 The NetBSD Foundation, Inc. 7 * All rights reserved. 8 * 9 * This code is derived from software contributed to The NetBSD Foundation 10 * by Jesse Off 11 * 12 * This code is derived from software contributed to The NetBSD Foundation 13 * by Ichiro FUKUHARA and Naoto Shimazaki. 14 * 15 * This code is derived from software contributed to The NetBSD Foundation 16 * by IWAMOTO Toshihiro. 17 * 18 * This code is derived from software contributed to The NetBSD Foundation 19 * by Charles M. Hannum. 20 * 21 * Redistribution and use in source and binary forms, with or without 22 * modification, are permitted provided that the following conditions 23 * are met: 24 * 1. Redistributions of source code must retain the above copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 30 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 31 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 32 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 33 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 34 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 37 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 38 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 40 * POSSIBILITY OF SUCH DAMAGE. 41 */ 42 43 /* 44 * Copyright (c) 1991 The Regents of the University of California. 45 * All rights reserved. 46 * 47 * Redistribution and use in source and binary forms, with or without 48 * modification, are permitted provided that the following conditions 49 * are met: 50 * 1. Redistributions of source code must retain the above copyright 51 * notice, this list of conditions and the following disclaimer. 52 * 2. Redistributions in binary form must reproduce the above copyright 53 * notice, this list of conditions and the following disclaimer in the 54 * documentation and/or other materials provided with the distribution. 55 * 3. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)com.c 7.5 (Berkeley) 5/16/91 72 */ 73 74 /* 75 * TODO: hardware flow control 76 */ 77 78 #include <sys/cdefs.h> 79 __KERNEL_RCSID(0, "$NetBSD: at91dbgu.c,v 1.21 2026/11/10 03:04:58 thorpej Exp $"); 80 81 #include "opt_kgdb.h" 82 83 #include "rnd.h" 84 #ifdef RND_COM 85 #include <sys/rndsource.h> 86 #endif 87 88 #include <sys/param.h> 89 #include <sys/systm.h> 90 #include <sys/types.h> 91 #include <sys/conf.h> 92 #include <sys/file.h> 93 #include <sys/device.h> 94 #include <sys/kernel.h> 95 #include <sys/kmem.h> 96 #include <sys/tty.h> 97 #include <sys/uio.h> 98 #include <sys/vnode.h> 99 #include <sys/kauth.h> 100 101 #include <machine/intr.h> 102 #include <sys/bus.h> 103 104 #include <arm/at91/at91reg.h> 105 #include <arm/at91/at91var.h> 106 #include <arm/at91/at91dbguvar.h> 107 #include <arm/at91/at91dbgureg.h> 108 109 #include <dev/cons.h> 110 111 static int at91dbgu_match(device_t, cfdata_t, void *); 112 static void at91dbgu_attach(device_t, device_t, void *); 113 114 static int at91dbgu_param(struct tty *, struct termios *); 115 static void at91dbgu_start(struct tty *); 116 static int at91dbgu_hwiflow(struct tty *, int); 117 118 #if 0 119 static u_int cflag2lcrhi(tcflag_t); 120 #endif 121 static void at91dbgu_iflush(struct at91dbgu_softc *); 122 static void at91dbgu_set(struct at91dbgu_softc *); 123 124 int at91dbgu_cn_getc(dev_t); 125 void at91dbgu_cn_putc(dev_t, int); 126 void at91dbgu_cn_pollc(dev_t, int); 127 128 static void at91dbgu_soft(void* arg); 129 inline static void at91dbgu_txsoft(struct at91dbgu_softc *, struct tty *); 130 inline static void at91dbgu_rxsoft(struct at91dbgu_softc *, struct tty *); 131 132 void at91dbgu_cn_probe(struct consdev *); 133 void at91dbgu_cn_init(struct consdev *); 134 135 static struct at91dbgu_cons_softc { 136 bus_space_tag_t sc_iot; 137 bus_space_handle_t sc_ioh; 138 bus_addr_t sc_hwbase; 139 int sc_ospeed; 140 tcflag_t sc_cflag; 141 int sc_attached; 142 143 uint8_t *sc_rx_ptr; 144 uint8_t sc_rx_fifo[64]; 145 } dbgu_cn_sc; 146 147 static struct cnm_state at91dbgu_cnm_state; 148 149 CFATTACH_DECL_NEW(at91dbgu, sizeof(struct at91dbgu_softc), 150 at91dbgu_match, at91dbgu_attach, NULL, NULL); 151 152 extern struct cfdriver at91dbgu_cd; 153 154 dev_type_open(at91dbgu_open); 155 dev_type_close(at91dbgu_close); 156 dev_type_read(at91dbgu_read); 157 dev_type_write(at91dbgu_write); 158 dev_type_ioctl(at91dbgu_ioctl); 159 dev_type_stop(at91dbgu_stop); 160 dev_type_tty(at91dbgu_tty); 161 dev_type_poll(at91dbgu_poll); 162 163 const struct cdevsw at91dbgu_cdevsw = { 164 .d_open = at91dbgu_open, 165 .d_close = at91dbgu_close, 166 .d_read = at91dbgu_read, 167 .d_write = at91dbgu_write, 168 .d_ioctl = at91dbgu_ioctl, 169 .d_stop = at91dbgu_stop, 170 .d_tty = at91dbgu_tty, 171 .d_poll = at91dbgu_poll, 172 .d_mmap = nommap, 173 .d_kqfilter = ttykqfilter, 174 .d_discard = nodiscard, 175 .d_flag = D_TTY 176 }; 177 178 struct consdev at91dbgu_cons = { 179 at91dbgu_cn_probe, NULL, at91dbgu_cn_getc, at91dbgu_cn_putc, at91dbgu_cn_pollc, NULL, 180 NULL, NULL, NODEV, CN_REMOTE 181 }; 182 183 #ifndef DEFAULT_COMSPEED 184 #define DEFAULT_COMSPEED 115200 185 #endif 186 187 #define COMUNIT(x) TTUNIT(x) 188 #define COMDIALOUT(x) TTDIALOUT(x) 189 190 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && device_is_active((sc)->sc_dev)) 191 192 static int 193 at91dbgu_match(device_t parent, cfdata_t match, void *aux) 194 { 195 if (strcmp(match->cf_name, "at91dbgu") == 0) 196 return 2; 197 return 0; 198 } 199 200 static int 201 dbgu_intr(void* arg); 202 203 static void 204 at91dbgu_attach(device_t parent, device_t self, void *aux) 205 { 206 struct at91dbgu_softc *sc = device_private(self); 207 struct at91bus_attach_args *sa = aux; 208 struct tty *tp; 209 210 printf("\n"); 211 212 sc->sc_dev = self; 213 bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh); 214 sc->sc_iot = sa->sa_iot; 215 sc->sc_hwbase = sa->sa_addr; 216 217 DBGUREG(DBGU_IDR) = -1; 218 at91_intr_establish(sa->sa_pid, IPL_SERIAL, INTR_HIGH_LEVEL, dbgu_intr, sc); 219 DBGU_INIT(AT91_MSTCLK, 115200U); 220 221 if (sc->sc_iot == dbgu_cn_sc.sc_iot 222 && sc->sc_hwbase == dbgu_cn_sc.sc_hwbase) { 223 dbgu_cn_sc.sc_attached = 1; 224 /* Make sure the console is always "hardwired". */ 225 delay(10000); /* wait for output to finish */ 226 SET(sc->sc_hwflags, COM_HW_CONSOLE); 227 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 228 SET(sc->sc_ier, DBGU_INT_RXRDY); 229 DBGUREG(DBGU_IER) = DBGU_INT_RXRDY; // @@@@@ 230 } 231 232 tp = tty_alloc(); 233 tp->t_oproc = at91dbgu_start; 234 tp->t_param = at91dbgu_param; 235 tp->t_hwiflow = at91dbgu_hwiflow; 236 237 sc->sc_tty = tp; 238 sc->sc_rbuf = kmem_alloc(AT91DBGU_RING_SIZE << 1, KM_SLEEP); 239 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 240 sc->sc_rbavail = AT91DBGU_RING_SIZE; 241 sc->sc_ebuf = sc->sc_rbuf + (AT91DBGU_RING_SIZE << 1); 242 sc->sc_tbc = 0; 243 244 tty_attach(tp); 245 246 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 247 int maj; 248 249 /* locate the major number */ 250 maj = cdevsw_lookup_major(&at91dbgu_cdevsw); 251 252 cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev)); 253 254 aprint_normal("%s: console (maj %u min %u cn_dev %#"PRIx64")\n", 255 device_xname(sc->sc_dev), maj, device_unit(sc->sc_dev), 256 cn_tab->cn_dev); 257 } 258 259 sc->sc_si = softint_establish(SOFTINT_SERIAL, at91dbgu_soft, sc); 260 261 #ifdef RND_COM 262 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev), 263 RND_TYPE_TTY, RND_FLAG_DEFAULT); 264 #endif 265 266 /* if there are no enable/disable functions, assume the device 267 is always enabled */ 268 if (!sc->enable) 269 sc->enabled = 1; 270 271 /* XXX configure register */ 272 /* xxx_config(sc) */ 273 274 SET(sc->sc_hwflags, COM_HW_DEV_OK); 275 } 276 277 static int 278 at91dbgu_param(struct tty *tp, struct termios *t) 279 { 280 struct at91dbgu_softc *sc 281 = device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev)); 282 int s; 283 284 if (COM_ISALIVE(sc) == 0) 285 return (EIO); 286 287 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 288 return (EINVAL); 289 290 /* 291 * For the console, always force CLOCAL and !HUPCL, so that the port 292 * is always active. 293 */ 294 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 295 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 296 SET(t->c_cflag, CLOCAL); 297 CLR(t->c_cflag, HUPCL); 298 } 299 300 /* 301 * If there were no changes, don't do anything. This avoids dropping 302 * input and improves performance when all we did was frob things like 303 * VMIN and VTIME. 304 */ 305 if (tp->t_ospeed == t->c_ospeed && 306 tp->t_cflag == t->c_cflag) 307 return (0); 308 309 s = splserial(); 310 311 sc->sc_brgr = (AT91_MSTCLK / 16 + t->c_ospeed / 2) / t->c_ospeed; 312 313 /* And copy to tty. */ 314 tp->t_ispeed = 0; 315 tp->t_ospeed = t->c_ospeed; 316 tp->t_cflag = t->c_cflag; 317 at91dbgu_set(sc); 318 319 splx(s); 320 321 /* 322 * Update the tty layer's idea of the carrier bit. 323 * We tell tty the carrier is always on. 324 */ 325 (void) (*tp->t_linesw->l_modem)(tp, 1); 326 327 #ifdef COM_DEBUG 328 if (com_debug) 329 comstatus(sc, "comparam "); 330 #endif 331 332 if (!ISSET(t->c_cflag, CHWFLOW)) { 333 if (sc->sc_tx_stopped) { 334 sc->sc_tx_stopped = 0; 335 at91dbgu_start(tp); 336 } 337 } 338 339 return (0); 340 } 341 342 static int 343 at91dbgu_hwiflow(struct tty *tp, int block) 344 { 345 return (0); 346 } 347 348 static void 349 at91dbgu_filltx(struct at91dbgu_softc *sc) 350 { 351 #if 0 352 bus_space_tag_t iot = sc->sc_iot; 353 bus_space_handle_t ioh = sc->sc_ioh; 354 #endif 355 int n; 356 357 n = 0; 358 while (DBGUREG(DBGU_SR) & DBGU_SR_TXRDY) { 359 if (n >= sc->sc_tbc) 360 break; 361 DBGUREG(DBGU_THR) = *(sc->sc_tba + n) & 0xff; 362 n++; 363 } 364 sc->sc_tbc -= n; 365 sc->sc_tba += n; 366 } 367 368 static void 369 at91dbgu_start(struct tty *tp) 370 { 371 struct at91dbgu_softc *sc 372 = device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev)); 373 int s; 374 375 if (COM_ISALIVE(sc) == 0) 376 return; 377 378 s = spltty(); 379 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 380 goto out; 381 if (sc->sc_tx_stopped) 382 goto out; 383 if (!ttypull(tp)) 384 goto out; 385 386 /* Grab the first contiguous region of buffer space. */ 387 { 388 u_char *tba; 389 int tbc; 390 391 tba = tp->t_outq.c_cf; 392 tbc = ndqb(&tp->t_outq, 0); 393 394 (void)splserial(); 395 396 sc->sc_tba = tba; 397 sc->sc_tbc = tbc; 398 } 399 400 SET(tp->t_state, TS_BUSY); 401 sc->sc_tx_busy = 1; 402 403 /* Output the first chunk of the contiguous buffer. */ 404 at91dbgu_filltx(sc); 405 406 SET(sc->sc_ier, DBGU_INT_TXRDY); 407 DBGUREG(DBGU_IER) = DBGU_INT_TXRDY; 408 409 out: 410 splx(s); 411 return; 412 } 413 414 static void 415 at91dbgu_break(struct at91dbgu_softc *sc, int onoff) 416 { 417 // @@@ we must disconnect the TX pin from the DBGU and control 418 // @@@ the pin directly to support this... 419 } 420 421 static void 422 at91dbgu_shutdown(struct at91dbgu_softc *sc) 423 { 424 int s; 425 426 s = splserial(); 427 428 /* Turn off interrupts. */ 429 DBGUREG(DBGU_IDR) = -1; 430 431 /* Clear any break condition set with TIOCSBRK. */ 432 at91dbgu_break(sc, 0); 433 at91dbgu_set(sc); 434 435 if (sc->disable) { 436 #ifdef DIAGNOSTIC 437 if (!sc->enabled) 438 panic("at91dbgu_shutdown: not enabled?"); 439 #endif 440 (*sc->disable)(sc); 441 sc->enabled = 0; 442 } 443 splx(s); 444 } 445 446 int 447 at91dbgu_open(dev_t dev, int flag, int mode, struct lwp *l) 448 { 449 struct at91dbgu_softc *sc; 450 struct tty *tp; 451 int s, s2; 452 int error; 453 454 sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev)); 455 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) || 456 sc->sc_rbuf == NULL) 457 return (ENXIO); 458 459 if (!device_is_active(sc->sc_dev)) 460 return (ENXIO); 461 462 #ifdef KGDB 463 /* 464 * If this is the kgdb port, no other use is permitted. 465 */ 466 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) 467 return (EBUSY); 468 #endif 469 470 tp = sc->sc_tty; 471 472 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 473 return (EBUSY); 474 475 s = spltty(); 476 477 /* 478 * Do the following iff this is a first open. 479 */ 480 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 481 struct termios t; 482 483 tp->t_dev = dev; 484 485 s2 = splserial(); 486 487 if (sc->enable) { 488 if ((*sc->enable)(sc)) { 489 splx(s2); 490 splx(s); 491 printf("%s: device enable failed\n", 492 device_xname(sc->sc_dev)); 493 return (EIO); 494 } 495 sc->enabled = 1; 496 #if 0 497 /* XXXXXXXXXXXXXXX */ 498 com_config(sc); 499 #endif 500 } 501 502 /* Turn on interrupts. */ 503 sc->sc_ier |= DBGU_INT_RXRDY; 504 DBGUREG(DBGU_IER) = DBGU_INT_RXRDY; 505 506 #if 0 507 /* Fetch the current modem control status, needed later. */ 508 sc->sc_msr = bus_space_read_1(sc->sc_iot, sc->sc_ioh, com_msr); 509 510 /* Clear PPS capture state on first open. */ 511 sc->sc_ppsmask = 0; 512 sc->ppsparam.mode = 0; 513 #endif 514 515 splx(s2); 516 517 /* 518 * Initialize the termios status to the defaults. Add in the 519 * sticky bits from TIOCSFLAGS. 520 */ 521 t.c_ispeed = 0; 522 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 523 t.c_ospeed = dbgu_cn_sc.sc_ospeed; 524 t.c_cflag = dbgu_cn_sc.sc_cflag; 525 } else { 526 t.c_ospeed = TTYDEF_SPEED; 527 t.c_cflag = TTYDEF_CFLAG; 528 } 529 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 530 SET(t.c_cflag, CLOCAL); 531 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 532 SET(t.c_cflag, CRTSCTS); 533 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 534 SET(t.c_cflag, MDMBUF); 535 /* Make sure at91dbgu_param() will do something. */ 536 tp->t_ospeed = 0; 537 (void) at91dbgu_param(tp, &t); 538 tp->t_iflag = TTYDEF_IFLAG; 539 tp->t_oflag = TTYDEF_OFLAG; 540 tp->t_lflag = TTYDEF_LFLAG; 541 ttychars(tp); 542 ttsetwater(tp); 543 544 s2 = splserial(); 545 546 /* Clear the input ring, and unblock. */ 547 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 548 sc->sc_rbavail = AT91DBGU_RING_SIZE; 549 at91dbgu_iflush(sc); 550 CLR(sc->sc_rx_flags, RX_ANY_BLOCK); 551 552 #ifdef COM_DEBUG 553 if (at91dbgu_debug) 554 comstatus(sc, "at91dbgu_open "); 555 #endif 556 557 splx(s2); 558 } 559 560 splx(s); 561 562 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 563 if (error) 564 goto bad; 565 566 error = (*tp->t_linesw->l_open)(dev, tp); 567 if (error) 568 goto bad; 569 570 return (0); 571 572 bad: 573 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 574 /* 575 * We failed to open the device, and nobody else had it opened. 576 * Clean up the state as appropriate. 577 */ 578 at91dbgu_shutdown(sc); 579 } 580 581 return (error); 582 } 583 584 int 585 at91dbgu_close(dev_t dev, int flag, int mode, struct lwp *l) 586 { 587 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev)); 588 struct tty *tp = sc->sc_tty; 589 590 /* XXX This is for cons.c. */ 591 if (!ISSET(tp->t_state, TS_ISOPEN)) 592 return (0); 593 594 (*tp->t_linesw->l_close)(tp, flag); 595 ttyclose(tp); 596 597 if (COM_ISALIVE(sc) == 0) 598 return (0); 599 600 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 601 /* 602 * Although we got a last close, the device may still be in 603 * use; e.g. if this was the dialout node, and there are still 604 * processes waiting for carrier on the non-dialout node. 605 */ 606 at91dbgu_shutdown(sc); 607 } 608 609 return (0); 610 } 611 612 int 613 at91dbgu_read(dev_t dev, struct uio *uio, int flag) 614 { 615 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev)); 616 struct tty *tp = sc->sc_tty; 617 618 if (COM_ISALIVE(sc) == 0) 619 return (EIO); 620 621 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 622 } 623 624 int 625 at91dbgu_write(dev_t dev, struct uio *uio, int flag) 626 { 627 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev)); 628 struct tty *tp = sc->sc_tty; 629 630 if (COM_ISALIVE(sc) == 0) 631 return (EIO); 632 633 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 634 } 635 636 int 637 at91dbgu_poll(dev_t dev, int events, struct lwp *l) 638 { 639 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev)); 640 struct tty *tp = sc->sc_tty; 641 642 if (COM_ISALIVE(sc) == 0) 643 return (EIO); 644 645 return ((*tp->t_linesw->l_poll)(tp, events, l)); 646 } 647 648 struct tty * 649 at91dbgu_tty(dev_t dev) 650 { 651 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev)); 652 struct tty *tp = sc->sc_tty; 653 654 return (tp); 655 } 656 657 int 658 at91dbgu_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 659 { 660 struct at91dbgu_softc *sc = device_lookup_private(&at91dbgu_cd, COMUNIT(dev)); 661 struct tty *tp = sc->sc_tty; 662 int error; 663 int s; 664 665 if (COM_ISALIVE(sc) == 0) 666 return (EIO); 667 668 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 669 if (error != EPASSTHROUGH) 670 return (error); 671 672 error = ttioctl(tp, cmd, data, flag, l); 673 if (error != EPASSTHROUGH) 674 return (error); 675 676 error = 0; 677 678 s = splserial(); 679 680 switch (cmd) { 681 case TIOCSBRK: 682 at91dbgu_break(sc, 1); 683 break; 684 685 case TIOCCBRK: 686 at91dbgu_break(sc, 0); 687 break; 688 689 case TIOCGFLAGS: 690 *(int *)data = sc->sc_swflags; 691 break; 692 693 case TIOCSFLAGS: 694 error = kauth_authorize_device_tty(l->l_cred, 695 KAUTH_DEVICE_TTY_PRIVSET, tp); 696 if (error) 697 break; 698 sc->sc_swflags = *(int *)data; 699 break; 700 701 default: 702 error = EPASSTHROUGH; 703 break; 704 } 705 706 splx(s); 707 708 return (error); 709 } 710 711 /* 712 * Stop output on a line. 713 */ 714 void 715 at91dbgu_stop(struct tty *tp, int flag) 716 { 717 struct at91dbgu_softc *sc 718 = device_lookup_private(&at91dbgu_cd, COMUNIT(tp->t_dev)); 719 int s; 720 721 s = splserial(); 722 if (ISSET(tp->t_state, TS_BUSY)) { 723 /* Stop transmitting at the next chunk. */ 724 sc->sc_tbc = 0; 725 if (!ISSET(tp->t_state, TS_TTSTOP)) 726 SET(tp->t_state, TS_FLUSH); 727 } 728 splx(s); 729 } 730 731 #if 0 732 static u_int 733 cflag2lcrhi(tcflag_t cflag) 734 { 735 uint32_t mr; 736 737 switch (cflag & CSIZE) { 738 default: 739 mr = 0x0; 740 break; 741 } 742 #if 0 743 mr |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0; 744 mr |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS; 745 mr |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0; 746 mr |= LinCtrlHigh_FEN; /* FIFO always enabled */ 747 #endif 748 mr |= DBGU_MR_PAR_NONE; 749 return (mr); 750 } 751 #endif 752 753 static void 754 at91dbgu_iflush(struct at91dbgu_softc *sc) 755 { 756 #if 0 757 bus_space_tag_t iot = sc->sc_iot; 758 bus_space_handle_t ioh = sc->sc_ioh; 759 #endif 760 #ifdef DIAGNOSTIC 761 int reg; 762 #endif 763 int timo; 764 765 #ifdef DIAGNOSTIC 766 reg = 0xffff; 767 #endif 768 timo = 50000; 769 /* flush any pending I/O */ 770 while (DBGUREG(DBGU_SR) & DBGU_SR_RXRDY 771 && --timo) 772 #ifdef DIAGNOSTIC 773 reg = 774 #else 775 (void) 776 #endif 777 DBGUREG(DBGU_RHR); 778 #ifdef DIAGNOSTIC 779 if (!timo) 780 printf("%s: com_iflush timeout %02x\n", device_xname(sc->sc_dev), 781 reg); 782 #endif 783 } 784 785 static void 786 at91dbgu_set(struct at91dbgu_softc *sc) 787 { 788 DBGUREG(DBGU_MR) = DBGU_MR_PAR_NONE; 789 DBGUREG(DBGU_BRGR) = sc->sc_brgr; 790 DBGUREG(DBGU_CR) = DBGU_CR_TXEN | DBGU_CR_RXEN; // @@@ just in case 791 } 792 793 void 794 at91dbgu_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh, 795 int ospeed, tcflag_t cflag); 796 void 797 at91dbgu_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh, 798 int ospeed, tcflag_t cflag) 799 { 800 cn_tab = &at91dbgu_cons; 801 cn_init_magic(&at91dbgu_cnm_state); 802 cn_set_magic("\047\001"); 803 804 dbgu_cn_sc.sc_iot = iot; 805 dbgu_cn_sc.sc_ioh = ioh; 806 dbgu_cn_sc.sc_hwbase = iobase; 807 dbgu_cn_sc.sc_ospeed = ospeed; 808 dbgu_cn_sc.sc_cflag = cflag; 809 810 DBGU_INIT(AT91_MSTCLK, ospeed); 811 } 812 813 void at91dbgu_attach_cn(bus_space_tag_t iot, int ospeed, int cflag) 814 { 815 bus_space_handle_t ioh; 816 bus_space_map(iot, AT91DBGU_BASE, AT91DBGU_SIZE, 0, &ioh); 817 at91dbgu_cn_attach(iot, AT91DBGU_BASE, ioh, ospeed, cflag); 818 } 819 820 void 821 at91dbgu_cn_probe(struct consdev *cp) 822 { 823 cp->cn_pri = CN_REMOTE; 824 } 825 826 void 827 at91dbgu_cn_pollc(dev_t dev, int on) 828 { 829 if (on) { 830 // enable polling mode 831 DBGUREG(DBGU_IDR) = DBGU_INT_RXRDY; 832 } else { 833 // disable polling mode 834 DBGUREG(DBGU_IER) = DBGU_INT_RXRDY; 835 } 836 } 837 838 void 839 at91dbgu_cn_putc(dev_t dev, int c) 840 { 841 #if 0 842 bus_space_tag_t iot = dbgu_cn_sc.sc_iot; 843 bus_space_handle_t ioh = dbgu_cn_sc.sc_ioh; 844 #endif 845 DBGU_PUTC(c); 846 847 #ifdef DEBUG 848 if (c == '\r') { 849 int s = splserial(); 850 while((DBGUREG(DBGU_SR) & DBGU_SR_TXEMPTY) == 0) { 851 splx(s); 852 s = splserial(); 853 } 854 splx(s); 855 } 856 #endif 857 858 } 859 860 int 861 at91dbgu_cn_getc(dev_t dev) 862 { 863 int c, sr; 864 int s; 865 #if 0 866 bus_space_tag_t iot = dbgu_cn_sc.sc_iot; 867 bus_space_handle_t ioh = dbgu_cn_sc.sc_ioh; 868 #endif 869 870 s = splserial(); 871 872 while ((c = DBGU_PEEKC()) == -1) { 873 splx(s); 874 s = splserial(); 875 } 876 ; 877 sr = DBGUREG(DBGU_SR); 878 if (ISSET(sr, DBGU_SR_FRAME) && c == 0) { 879 DBGUREG(DBGU_CR) = DBGU_CR_RSTSTA; // reset status bits 880 c = CNC_BREAK; 881 } 882 cn_check_magic(dev, c, &at91dbgu_cnm_state); 883 splx(s); 884 885 c &= 0xff; 886 887 return (c); 888 } 889 890 inline static void 891 at91dbgu_txsoft(struct at91dbgu_softc *sc, struct tty *tp) 892 { 893 CLR(tp->t_state, TS_BUSY); 894 if (ISSET(tp->t_state, TS_FLUSH)) 895 CLR(tp->t_state, TS_FLUSH); 896 else 897 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf)); 898 (*tp->t_linesw->l_start)(tp); 899 } 900 901 inline static void 902 at91dbgu_rxsoft(struct at91dbgu_softc *sc, struct tty *tp) 903 { 904 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint; 905 u_char *get, *end; 906 u_int cc, scc; 907 u_char sts; 908 int code; 909 int s; 910 911 end = sc->sc_ebuf; 912 get = sc->sc_rbget; 913 scc = cc = AT91DBGU_RING_SIZE - sc->sc_rbavail; 914 #if 0 915 if (cc == AT91DBGU_RING_SIZE) { 916 sc->sc_floods++; 917 if (sc->sc_errors++ == 0) 918 callout_reset(&sc->sc_diag_callout, 60 * hz, 919 comdiag, sc); 920 } 921 #endif 922 while (cc) { 923 code = get[0]; 924 sts = get[1]; 925 if (ISSET(sts, DBGU_SR_PARE | DBGU_SR_FRAME | DBGU_SR_OVRE)) { 926 #if 0 927 if (ISSET(lsr, DR_ROR)) { 928 sc->sc_overflows++; 929 if (sc->sc_errors++ == 0) 930 callout_reset(&sc->sc_diag_callout, 931 60 * hz, comdiag, sc); 932 } 933 #endif 934 if (ISSET(sts, (DBGU_SR_FRAME | DBGU_SR_OVRE))) 935 SET(code, TTY_FE); 936 if (ISSET(sts, DBGU_SR_PARE)) 937 SET(code, TTY_PE); 938 } 939 if ((*rint)(code, tp) == -1) { 940 /* 941 * The line discipline's buffer is out of space. 942 */ 943 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 944 /* 945 * We're either not using flow control, or the 946 * line discipline didn't tell us to block for 947 * some reason. Either way, we have no way to 948 * know when there's more space available, so 949 * just drop the rest of the data. 950 */ 951 get += cc << 1; 952 if (get >= end) 953 get -= AT91DBGU_RING_SIZE << 1; 954 cc = 0; 955 } else { 956 /* 957 * Don't schedule any more receive processing 958 * until the line discipline tells us there's 959 * space available (through comhwiflow()). 960 * Leave the rest of the data in the input 961 * buffer. 962 */ 963 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 964 } 965 break; 966 } 967 get += 2; 968 if (get >= end) 969 get = sc->sc_rbuf; 970 cc--; 971 } 972 973 if (cc != scc) { 974 sc->sc_rbget = get; 975 s = splserial(); 976 977 cc = sc->sc_rbavail += scc - cc; 978 /* Buffers should be ok again, release possible block. */ 979 if (cc >= 1) { 980 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 981 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 982 DBGUREG(DBGU_IER) = DBGU_INT_RXRDY; 983 } 984 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) { 985 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED); 986 #if 0 987 com_hwiflow(sc); 988 #endif 989 } 990 } 991 splx(s); 992 } 993 } 994 995 static void 996 at91dbgu_soft(void* arg) 997 { 998 struct at91dbgu_softc *sc = arg; 999 1000 if (COM_ISALIVE(sc) == 0) 1001 return; 1002 1003 if (sc->sc_rx_ready) { 1004 sc->sc_rx_ready = 0; 1005 at91dbgu_rxsoft(sc, sc->sc_tty); 1006 } 1007 if (sc->sc_tx_done) { 1008 sc->sc_tx_done = 0; 1009 at91dbgu_txsoft(sc, sc->sc_tty); 1010 } 1011 } 1012 1013 1014 static int 1015 dbgu_intr(void* arg) 1016 { 1017 struct at91dbgu_softc *sc = arg; 1018 #if 0 1019 bus_space_tag_t iot = sc->sc_iot; 1020 bus_space_handle_t ioh = sc->sc_ioh; 1021 #endif 1022 u_char *put, *end; 1023 u_int cc; 1024 uint32_t imr, sr; 1025 int c = 0; 1026 imr = DBGUREG(DBGU_IMR); 1027 #if 0 1028 if (!imr) 1029 return 0; 1030 #endif 1031 sr = DBGUREG(DBGU_SR); 1032 if (!(sr & imr)) { 1033 if (sr & DBGU_SR_RXRDY) { 1034 // printf("sr=0x%08x imr=0x%08x\n", sr, imr); 1035 } 1036 return 0; 1037 } 1038 1039 end = sc->sc_ebuf; 1040 put = sc->sc_rbput; 1041 cc = sc->sc_rbavail; 1042 1043 // ok, we DO have some interrupts to serve! 1044 if (sr & DBGU_SR_RXRDY) { 1045 bool cn_trapped = false; 1046 c = DBGUREG(DBGU_RHR); 1047 if (ISSET(sr, (DBGU_SR_OVRE | DBGU_SR_FRAME | DBGU_SR_PARE))) 1048 DBGUREG(DBGU_CR) = DBGU_CR_RSTSTA; 1049 if (ISSET(sr, DBGU_SR_FRAME) && c == 0) { 1050 c = CNC_BREAK; 1051 } 1052 cn_trapped = cn_check_magic(cn_tab->cn_dev, c, 1053 &at91dbgu_cnm_state); 1054 if (!cn_trapped && cc) { 1055 put[0] = c & 0xff; 1056 put[1] = sr & 0xff; 1057 put += 2; 1058 if (put >= end) 1059 put = sc->sc_rbuf; 1060 cc--; 1061 /* 1062 * Current string of incoming characters ended because 1063 * no more data was available or we ran out of space. 1064 * Schedule a receive event if any data was received. 1065 * If we're out of space, turn off receive interrupts. 1066 */ 1067 sc->sc_rbput = put; 1068 sc->sc_rbavail = cc; 1069 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) 1070 sc->sc_rx_ready = 1; 1071 1072 /* 1073 * See if we are in danger of overflowing a buffer. If 1074 * so, use hardware flow control to ease the pressure. 1075 */ 1076 1077 /* but at91dbgu cannot (yet). X-( */ 1078 1079 /* 1080 * If we're out of space, disable receive interrupts 1081 * until the queue has drained a bit. 1082 */ 1083 if (!cc) { 1084 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1085 } 1086 } 1087 } 1088 1089 /* 1090 * Done handling any receive interrupts. See if data can be 1091 * transmitted as well. Schedule tx done event if no data left 1092 * and tty was marked busy. 1093 */ 1094 1095 if (ISSET(sr, DBGU_SR_TXRDY) && sc->sc_tbc > 0) { 1096 /* Output the next chunk of the contiguous buffer, if any. */ 1097 at91dbgu_filltx(sc); 1098 } else { 1099 /* Disable transmit completion interrupts if necessary. */ 1100 DBGUREG(DBGU_IDR) = DBGU_INT_TXRDY; 1101 if (sc->sc_tx_busy) { 1102 sc->sc_tx_busy = 0; 1103 sc->sc_tx_done = 1; 1104 } 1105 } 1106 1107 /* Wake up the poller. */ 1108 softint_schedule(sc->sc_si); 1109 #if 0 1110 #ifdef RND_COM 1111 rnd_add_uint32(&sc->rnd_source, imr ^ sr ^ c); 1112 #endif 1113 #endif 1114 1115 return (1); 1116 } 1117