1 /* $NetBSD: sa11x0_com.c,v 1.47 2010/08/19 15:17:53 tsutsui Exp $ */ 2 3 /*- 4 * Copyright (c) 1998, 1999, 2001 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by IWAMOTO Toshihiro. 9 * 10 * This code is derived from software contributed to The NetBSD Foundation 11 * by Charles M. Hannum. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 * POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 /* 36 * Copyright (c) 1991 The Regents of the University of California. 37 * All rights reserved. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 3. Neither the name of the University nor the names of its contributors 48 * may be used to endorse or promote products derived from this software 49 * without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 61 * SUCH DAMAGE. 62 * 63 * @(#)com.c 7.5 (Berkeley) 5/16/91 64 */ 65 66 #include <sys/cdefs.h> 67 __KERNEL_RCSID(0, "$NetBSD: sa11x0_com.c,v 1.47 2010/08/19 15:17:53 tsutsui Exp $"); 68 69 #include "opt_com.h" 70 #include "opt_ddb.h" 71 #include "opt_ddbparam.h" 72 #include "opt_kgdb.h" 73 #include "opt_multiprocessor.h" 74 #include "opt_lockdebug.h" 75 76 #include "rnd.h" 77 #if NRND > 0 && defined(RND_COM) 78 #include <sys/rnd.h> 79 #endif 80 81 #include <sys/param.h> 82 #include <sys/systm.h> 83 #include <sys/types.h> 84 #include <sys/conf.h> 85 #include <sys/file.h> 86 #include <sys/device.h> 87 #include <sys/kernel.h> 88 #include <sys/malloc.h> 89 #include <sys/tty.h> 90 #include <sys/uio.h> 91 #include <sys/vnode.h> 92 #include <sys/kauth.h> 93 94 #include <dev/cons.h> 95 96 #include <machine/bus.h> 97 #include <arm/sa11x0/sa11x0_reg.h> 98 #include <arm/sa11x0/sa11x0_var.h> 99 #include <arm/sa11x0/sa11x0_comreg.h> 100 #include <arm/sa11x0/sa11x0_comvar.h> 101 #include <arm/sa11x0/sa11x0_gpioreg.h> 102 103 #ifdef hpcarm 104 #include <hpc/include/platid.h> 105 #include <hpc/include/platid_mask.h> 106 #endif 107 108 #include "sacom.h" 109 110 dev_type_open(sacomopen); 111 dev_type_close(sacomclose); 112 dev_type_read(sacomread); 113 dev_type_write(sacomwrite); 114 dev_type_ioctl(sacomioctl); 115 dev_type_stop(sacomstop); 116 dev_type_tty(sacomtty); 117 dev_type_poll(sacompoll); 118 119 const struct cdevsw sacom_cdevsw = { 120 sacomopen, sacomclose, sacomread, sacomwrite, sacomioctl, 121 sacomstop, sacomtty, sacompoll, nommap, ttykqfilter, D_TTY 122 }; 123 124 static int sacom_match(device_t, cfdata_t, void *); 125 static void sacom_attach(device_t, device_t, void *); 126 static void sacom_filltx(struct sacom_softc *); 127 static void sacom_attach_subr(struct sacom_softc *); 128 #if defined(DDB) || defined(KGDB) 129 static void sacom_enable_debugport(struct sacom_softc *); 130 #endif 131 int sacom_detach(device_t, int); 132 void sacom_config(struct sacom_softc *); 133 int sacom_activate(device_t, enum devact); 134 void sacom_shutdown(struct sacom_softc *); 135 static u_int cflag2cr0(tcflag_t); 136 int sacomparam(struct tty *, struct termios *); 137 void sacomstart(struct tty *); 138 int sacomhwiflow(struct tty *, int); 139 140 void sacom_loadchannelregs(struct sacom_softc *); 141 void sacom_hwiflow(struct sacom_softc *); 142 void sacom_break(struct sacom_softc *, int); 143 void sacom_modem(struct sacom_softc *, int); 144 void tiocm_to_sacom(struct sacom_softc *, u_long, int); 145 int sacom_to_tiocm(struct sacom_softc *); 146 void sacom_iflush(struct sacom_softc *); 147 148 int sacominit(bus_space_tag_t, bus_addr_t, int, tcflag_t, 149 bus_space_handle_t *); 150 int sacom_is_console(bus_space_tag_t, bus_addr_t, 151 bus_space_handle_t *); 152 153 void sacomsoft(void *); 154 155 static inline void sacom_rxsoft(struct sacom_softc *, struct tty *); 156 static inline void sacom_txsoft(struct sacom_softc *, struct tty *); 157 static inline void sacom_stsoft(struct sacom_softc *, struct tty *); 158 static inline void sacom_schedrx(struct sacom_softc *); 159 160 #ifdef hpcarm 161 /* HPCARM specific functions */ 162 static void sacom_j720_init(device_t, device_t); 163 #endif 164 165 #define COMUNIT_MASK 0x7ffff 166 #define COMDIALOUT_MASK 0x80000 167 168 #define COMUNIT(x) (minor(x) & COMUNIT_MASK) 169 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK) 170 171 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && \ 172 device_is_active((sc)->sc_dev)) 173 174 #define COM_BARRIER(t, h, f) bus_space_barrier((t), (h), 0, COM_NPORTS, (f)) 175 #define COM_LOCK(sc) 176 #define COM_UNLOCK(sc) 177 178 int sacomintr(void *); 179 int sacomcngetc(dev_t); 180 void sacomcnputc(dev_t, int); 181 void sacomcnpollc(dev_t, int); 182 183 void sacomcnprobe(struct consdev *); 184 void sacomcninit(struct consdev *); 185 186 extern struct bus_space sa11x0_bs_tag; 187 188 static bus_space_tag_t sacomconstag; 189 static bus_space_handle_t sacomconsioh; 190 static bus_addr_t sacomconsaddr = SACOM3_BASE; /* XXX */ 191 192 static int sacomconsattached; 193 static int sacomconsrate; 194 static tcflag_t sacomconscflag; 195 196 CFATTACH_DECL_NEW(sacom, sizeof(struct sacom_softc), 197 sacom_match, sacom_attach, NULL, NULL); 198 extern struct cfdriver sacom_cd; 199 200 #ifdef hpcarm 201 struct platid_data sacom_platid_table[] = { 202 { &platid_mask_MACH_HP_JORNADA_7XX, sacom_j720_init }, 203 { NULL, NULL } 204 }; 205 #endif 206 207 struct consdev sacomcons = { 208 NULL, NULL, sacomcngetc, sacomcnputc, sacomcnpollc, NULL, 209 NULL, NULL, NODEV, CN_NORMAL 210 }; 211 212 #ifndef CONMODE 213 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */ 214 #endif 215 #ifndef CONSPEED 216 #define CONSPEED 9600 217 #endif 218 #ifndef CONADDR 219 #define CONADDR SACOM3_BASE 220 #endif 221 222 static int 223 sacom_match(device_t parent, cfdata_t match, void *aux) 224 { 225 226 return 1; 227 } 228 229 void 230 sacom_attach(device_t parent, device_t self, void *aux) 231 { 232 struct sacom_softc *sc = device_private(self); 233 struct sa11x0_attach_args *sa = aux; 234 235 #ifdef hpcarm 236 struct platid_data *p; 237 void (*mdinit)(device_t, device_t); 238 #endif 239 240 aprint_normal("\n"); 241 242 sc->sc_dev = self; 243 sc->sc_iot = sa->sa_iot; 244 sc->sc_baseaddr = sa->sa_addr; 245 246 if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, 247 &sc->sc_ioh)) { 248 aprint_normal_dev(self, "unable to map registers\n"); 249 return; 250 } 251 252 switch (sc->sc_baseaddr) { 253 case SACOM1_BASE: 254 aprint_normal_dev(self, "SA-11x0 UART1\n"); 255 break; 256 case SACOM2_BASE: 257 aprint_normal_dev(self, "SA-11x0 UART2 (IRDA)\n"); 258 break; 259 case SACOM3_BASE: 260 aprint_normal_dev(self, "SA-11x0 UART3\n"); 261 break; 262 default: 263 aprint_normal_dev(self, "unknown SA-11x0 UART\n"); 264 break; 265 } 266 267 sacom_attach_subr(sc); 268 269 #ifdef hpcarm 270 /* Do hpcarm specific initialization, if any */ 271 if ((p = platid_search_data(&platid, sacom_platid_table)) != NULL) { 272 mdinit = p->data; 273 (*mdinit)(parent, self); 274 } 275 #endif 276 277 sa11x0_intr_establish(0, sa->sa_intr, 1, IPL_SERIAL, sacomintr, sc); 278 } 279 280 void 281 sacom_attach_subr(struct sacom_softc *sc) 282 { 283 bus_addr_t iobase = sc->sc_baseaddr; 284 bus_space_tag_t iot = sc->sc_iot; 285 struct tty *tp; 286 287 /* XXX Do we need to disable interrupts here? */ 288 289 if (iot == sacomconstag && iobase == sacomconsaddr) { 290 sacomconsattached = 1; 291 sc->sc_speed = SACOMSPEED(sacomconsrate); 292 293 /* Make sure the console is always "hardwired". */ 294 delay(10000); /* wait for output to finish */ 295 SET(sc->sc_hwflags, COM_HW_CONSOLE); 296 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 297 } 298 299 tp = ttymalloc(); 300 tp->t_oproc = sacomstart; 301 tp->t_param = sacomparam; 302 tp->t_hwiflow = sacomhwiflow; 303 304 sc->sc_tty = tp; 305 sc->sc_rbuf = malloc(SACOM_RING_SIZE << 1, M_DEVBUF, M_NOWAIT); 306 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 307 sc->sc_rbavail = SACOM_RING_SIZE; 308 if (sc->sc_rbuf == NULL) { 309 aprint_normal_dev(sc->sc_dev, "unable to allocate ring buffer\n"); 310 return; 311 } 312 sc->sc_ebuf = sc->sc_rbuf + (SACOM_RING_SIZE << 1); 313 sc->sc_tbc = 0; 314 315 tty_attach(tp); 316 317 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 318 int maj; 319 320 /* locate the major number */ 321 maj = cdevsw_lookup_major(&sacom_cdevsw); 322 323 cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev)); 324 325 delay(10000); /* XXX */ 326 aprint_normal_dev(sc->sc_dev, "console\n"); 327 delay(10000); /* XXX */ 328 } 329 330 331 sc->sc_si = softint_establish(SOFTINT_SERIAL, sacomsoft, sc); 332 333 #if NRND > 0 && defined(RND_COM) 334 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev), 335 RND_TYPE_TTY, 0); 336 #endif 337 338 /* if there are no enable/disable functions, assume the device 339 is always enabled */ 340 if (!sc->enable) 341 sc->enabled = 1; 342 343 sacom_config(sc); 344 345 SET(sc->sc_hwflags, COM_HW_DEV_OK); 346 } 347 348 /* This is necessary when dynamically changing SAIP configuration. */ 349 int 350 sacom_detach(device_t dev, int flags) 351 { 352 struct sacom_softc *sc = device_private(dev); 353 int maj, mn; 354 355 if (sc->sc_hwflags & (COM_HW_CONSOLE|COM_HW_KGDB)) 356 return EBUSY; 357 358 if (sc->disable != NULL && sc->enabled != 0) { 359 (*sc->disable)(sc); 360 sc->enabled = 0; 361 } 362 363 /* locate the major number */ 364 maj = cdevsw_lookup_major(&sacom_cdevsw); 365 366 /* Nuke the vnodes for any open instances. */ 367 mn = device_unit(dev); 368 vdevgone(maj, mn, mn, VCHR); 369 370 mn |= COMDIALOUT_MASK; 371 vdevgone(maj, mn, mn, VCHR); 372 373 /* Free the receive buffer. */ 374 free(sc->sc_rbuf, M_DEVBUF); 375 376 /* Detach and free the tty. */ 377 tty_detach(sc->sc_tty); 378 ttyfree(sc->sc_tty); 379 380 /* Unhook the soft interrupt handler. */ 381 softint_disestablish(sc->sc_si); 382 383 #if NRND > 0 && defined(RND_COM) 384 /* Unhook the entropy source. */ 385 rnd_detach_source(&sc->rnd_source); 386 #endif 387 388 return 0; 389 } 390 391 void 392 sacom_config(struct sacom_softc *sc) 393 { 394 bus_space_tag_t iot = sc->sc_iot; 395 bus_space_handle_t ioh = sc->sc_ioh; 396 397 /* Disable engine before configuring the device. */ 398 sc->sc_cr3 = 0; 399 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3); 400 401 #ifdef DDB 402 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) 403 sacom_enable_debugport(sc); 404 #endif 405 } 406 407 #ifdef DDB 408 static void 409 sacom_enable_debugport(struct sacom_softc *sc) 410 { 411 bus_space_tag_t iot = sc->sc_iot; 412 bus_space_handle_t ioh = sc->sc_ioh; 413 int s; 414 415 s = splserial(); 416 COM_LOCK(sc); 417 sc->sc_cr3 = CR3_RXE | CR3_TXE; 418 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3); 419 COM_UNLOCK(sc); 420 splx(s); 421 } 422 #endif 423 424 int 425 sacom_activate(device_t dev, enum devact act) 426 { 427 struct sacom_softc *sc = device_private(dev); 428 429 switch (act) { 430 case DVACT_DEACTIVATE: 431 sc->enabled = 0; 432 return 0; 433 default: 434 return EOPNOTSUPP; 435 } 436 } 437 438 void 439 sacom_shutdown(struct sacom_softc *sc) 440 { 441 struct tty *tp = sc->sc_tty; 442 int s; 443 444 s = splserial(); 445 COM_LOCK(sc); 446 447 /* Clear any break condition set with TIOCSBRK. */ 448 sacom_break(sc, 0); 449 450 /* 451 * Hang up if necessary. Wait a bit, so the other side has time to 452 * notice even if we immediately open the port again. 453 * Avoid tsleeping above splhigh(). 454 */ 455 if (ISSET(tp->t_cflag, HUPCL)) { 456 sacom_modem(sc, 0); 457 COM_UNLOCK(sc); 458 splx(s); 459 /* XXX tsleep will only timeout */ 460 (void) tsleep(sc, TTIPRI, ttclos, hz); 461 s = splserial(); 462 COM_LOCK(sc); 463 } 464 465 /* Turn off interrupts. */ 466 sc->sc_cr3 = 0; 467 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, sc->sc_cr3); 468 469 if (sc->disable) { 470 #ifdef DIAGNOSTIC 471 if (!sc->enabled) 472 panic("sacom_shutdown: not enabled?"); 473 #endif 474 (*sc->disable)(sc); 475 sc->enabled = 0; 476 } 477 COM_UNLOCK(sc); 478 splx(s); 479 } 480 481 int 482 sacomopen(dev_t dev, int flag, int mode, struct lwp *l) 483 { 484 struct sacom_softc *sc; 485 struct tty *tp; 486 int s, s2; 487 int error; 488 489 sc = device_lookup_private(&sacom_cd, COMUNIT(dev)); 490 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK) || 491 sc->sc_rbuf == NULL) 492 return ENXIO; 493 494 if (!device_is_active(sc->sc_dev)) 495 return ENXIO; 496 497 tp = sc->sc_tty; 498 499 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 500 return (EBUSY); 501 502 s = spltty(); 503 504 /* 505 * Do the following iff this is a first open. 506 */ 507 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 508 struct termios t; 509 510 tp->t_dev = dev; 511 512 s2 = splserial(); 513 COM_LOCK(sc); 514 515 if (sc->enable) { 516 if ((*sc->enable)(sc)) { 517 COM_UNLOCK(sc); 518 splx(s2); 519 splx(s); 520 aprint_normal_dev(sc->sc_dev, "device enable failed\n"); 521 return EIO; 522 } 523 sc->enabled = 1; 524 sacom_config(sc); 525 } 526 527 /* Turn on interrupts. */ 528 sc->sc_cr3 = CR3_RXE | CR3_TXE | CR3_RIE | CR3_TIE; 529 bus_space_write_4(sc->sc_iot, sc->sc_ioh, SACOM_CR3, 530 sc->sc_cr3); 531 532 533 COM_UNLOCK(sc); 534 splx(s2); 535 536 /* 537 * Initialize the termios status to the defaults. Add in the 538 * sticky bits from TIOCSFLAGS. 539 */ 540 t.c_ispeed = 0; 541 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 542 t.c_ospeed = sacomconsrate; 543 t.c_cflag = sacomconscflag; 544 } else { 545 t.c_ospeed = TTYDEF_SPEED; 546 t.c_cflag = TTYDEF_CFLAG; 547 } 548 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 549 SET(t.c_cflag, CLOCAL); 550 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 551 SET(t.c_cflag, CRTSCTS); 552 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 553 SET(t.c_cflag, MDMBUF); 554 /* Make sure sacomparam() will do something. */ 555 tp->t_ospeed = 0; 556 (void) sacomparam(tp, &t); 557 tp->t_iflag = TTYDEF_IFLAG; 558 tp->t_oflag = TTYDEF_OFLAG; 559 tp->t_lflag = TTYDEF_LFLAG; 560 ttychars(tp); 561 ttsetwater(tp); 562 563 s2 = splserial(); 564 COM_LOCK(sc); 565 566 /* 567 * Turn on DTR. We must always do this, even if carrier is not 568 * present, because otherwise we'd have to use TIOCSDTR 569 * immediately after setting CLOCAL, which applications do not 570 * expect. We always assert DTR while the device is open 571 * unless explicitly requested to deassert it. 572 */ 573 sacom_modem(sc, 1); 574 575 /* Clear the input ring, and unblock. */ 576 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 577 sc->sc_rbavail = SACOM_RING_SIZE; 578 sacom_iflush(sc); 579 CLR(sc->sc_rx_flags, RX_ANY_BLOCK); 580 sacom_hwiflow(sc); 581 582 #ifdef COM_DEBUG 583 if (sacom_debug) 584 comstatus(sc, "sacomopen "); 585 #endif 586 587 COM_UNLOCK(sc); 588 splx(s2); 589 } 590 591 splx(s); 592 593 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 594 if (error) 595 goto bad; 596 597 error = (*tp->t_linesw->l_open)(dev, tp); 598 if (error) 599 goto bad; 600 601 return 0; 602 603 bad: 604 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 605 /* 606 * We failed to open the device, and nobody else had it opened. 607 * Clean up the state as appropriate. 608 */ 609 sacom_shutdown(sc); 610 } 611 612 return error; 613 } 614 615 int 616 sacomclose(dev_t dev, int flag, int mode, struct lwp *l) 617 { 618 struct sacom_softc *sc = 619 device_lookup_private(&sacom_cd, COMUNIT(dev)); 620 struct tty *tp = sc->sc_tty; 621 622 /* XXX This is for cons.c. */ 623 if (!ISSET(tp->t_state, TS_ISOPEN)) 624 return 0; 625 626 (*tp->t_linesw->l_close)(tp, flag); 627 ttyclose(tp); 628 629 if (COM_ISALIVE(sc) == 0) 630 return 0; 631 632 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 633 /* 634 * Although we got a last close, the device may still be in 635 * use; e.g. if this was the dialout node, and there are still 636 * processes waiting for carrier on the non-dialout node. 637 */ 638 sacom_shutdown(sc); 639 } 640 641 return 0; 642 } 643 644 int 645 sacomread(dev_t dev, struct uio *uio, int flag) 646 { 647 struct sacom_softc *sc = 648 device_lookup_private(&sacom_cd, COMUNIT(dev)); 649 struct tty *tp = sc->sc_tty; 650 651 if (COM_ISALIVE(sc) == 0) 652 return EIO; 653 654 return (*tp->t_linesw->l_read)(tp, uio, flag); 655 } 656 657 int 658 sacomwrite(dev_t dev, struct uio *uio, int flag) 659 { 660 struct sacom_softc *sc = 661 device_lookup_private(&sacom_cd, COMUNIT(dev)); 662 struct tty *tp = sc->sc_tty; 663 664 if (COM_ISALIVE(sc) == 0) 665 return EIO; 666 667 return (*tp->t_linesw->l_write)(tp, uio, flag); 668 } 669 670 int 671 sacompoll(dev_t dev, int events, struct lwp *l) 672 { 673 struct sacom_softc *sc = 674 device_lookup_private(&sacom_cd, COMUNIT(dev)); 675 struct tty *tp = sc->sc_tty; 676 677 if (COM_ISALIVE(sc) == 0) 678 return EIO; 679 680 return (*tp->t_linesw->l_poll)(tp, events, l); 681 } 682 683 struct tty * 684 sacomtty(dev_t dev) 685 { 686 struct sacom_softc *sc = 687 device_lookup_private(&sacom_cd, COMUNIT(dev)); 688 struct tty *tp = sc->sc_tty; 689 690 return tp; 691 } 692 693 int 694 sacomioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 695 { 696 struct sacom_softc *sc = 697 device_lookup_private(&sacom_cd, COMUNIT(dev)); 698 struct tty *tp = sc->sc_tty; 699 int error; 700 int s; 701 702 if (COM_ISALIVE(sc) == 0) 703 return EIO; 704 705 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 706 if (error != EPASSTHROUGH) 707 return error; 708 709 error = ttioctl(tp, cmd, data, flag, l); 710 if (error != EPASSTHROUGH) 711 return error; 712 713 error = 0; 714 715 s = splserial(); 716 COM_LOCK(sc); 717 718 switch (cmd) { 719 case TIOCSBRK: 720 sacom_break(sc, 1); 721 break; 722 723 case TIOCCBRK: 724 sacom_break(sc, 0); 725 break; 726 727 case TIOCSDTR: 728 sacom_modem(sc, 1); 729 break; 730 731 case TIOCCDTR: 732 sacom_modem(sc, 0); 733 break; 734 735 case TIOCGFLAGS: 736 *(int *)data = sc->sc_swflags; 737 break; 738 739 case TIOCSFLAGS: 740 error = kauth_authorize_device_tty(l->l_cred, 741 KAUTH_DEVICE_TTY_PRIVSET, tp); 742 if (error) 743 break; 744 sc->sc_swflags = *(int *)data; 745 break; 746 747 case TIOCMSET: 748 case TIOCMBIS: 749 case TIOCMBIC: 750 tiocm_to_sacom(sc, cmd, *(int *)data); 751 break; 752 753 case TIOCMGET: 754 *(int *)data = sacom_to_tiocm(sc); 755 break; 756 757 default: 758 error = EPASSTHROUGH; 759 break; 760 } 761 762 COM_UNLOCK(sc); 763 splx(s); 764 765 #ifdef COM_DEBUG 766 if (sacom_debug) 767 comstatus(sc, "comioctl "); 768 #endif 769 770 return error; 771 } 772 773 static inline void 774 sacom_schedrx(struct sacom_softc *sc) 775 { 776 777 sc->sc_rx_ready = 1; 778 779 /* Wake up the poller. */ 780 softint_schedule(sc->sc_si); 781 } 782 783 void 784 sacom_break(struct sacom_softc *sc, int onoff) 785 { 786 787 if (onoff) 788 SET(sc->sc_cr3, CR3_BRK); 789 else 790 CLR(sc->sc_cr3, CR3_BRK); 791 792 if (!sc->sc_heldchange) { 793 if (sc->sc_tx_busy) { 794 sc->sc_heldtbc = sc->sc_tbc; 795 sc->sc_tbc = 0; 796 sc->sc_heldchange = 1; 797 } else 798 sacom_loadchannelregs(sc); 799 } 800 } 801 802 void 803 sacom_modem(struct sacom_softc *sc, int onoff) 804 { 805 if (!sc->sc_heldchange) { 806 if (sc->sc_tx_busy) { 807 sc->sc_heldtbc = sc->sc_tbc; 808 sc->sc_tbc = 0; 809 sc->sc_heldchange = 1; 810 } else 811 sacom_loadchannelregs(sc); 812 } 813 } 814 815 void 816 tiocm_to_sacom(struct sacom_softc *sc, u_long how, int ttybits) 817 { 818 } 819 820 int 821 sacom_to_tiocm(struct sacom_softc *sc) 822 { 823 int ttybits = 0; 824 825 if (sc->sc_cr3 != 0) 826 SET(ttybits, TIOCM_LE); 827 828 return ttybits; 829 } 830 831 static u_int 832 cflag2cr0(tcflag_t cflag) 833 { 834 u_int cr0; 835 836 cr0 = (cflag & PARENB) ? CR0_PE : 0; 837 cr0 |= (cflag & PARODD) ? 0 : CR0_OES; 838 cr0 |= (cflag & CSTOPB) ? CR0_SBS : 0; 839 cr0 |= ((cflag & CSIZE) == CS8) ? CR0_DSS : 0; 840 841 return cr0; 842 } 843 844 int 845 sacomparam(struct tty *tp, struct termios *t) 846 { 847 struct sacom_softc *sc = 848 device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev)); 849 int ospeed = SACOMSPEED(t->c_ospeed); 850 u_int cr0; 851 int s; 852 853 if (COM_ISALIVE(sc) == 0) 854 return EIO; 855 856 /* Check requested parameters. */ 857 if (ospeed < 0) 858 return EINVAL; 859 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 860 return EINVAL; 861 862 /* 863 * For the console, always force CLOCAL and !HUPCL, so that the port 864 * is always active. 865 */ 866 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 867 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 868 SET(t->c_cflag, CLOCAL); 869 CLR(t->c_cflag, HUPCL); 870 } 871 872 /* 873 * If there were no changes, don't do anything. This avoids dropping 874 * input and improves performance when all we did was frob things like 875 * VMIN and VTIME. 876 */ 877 if (tp->t_ospeed == t->c_ospeed && 878 tp->t_cflag == t->c_cflag) 879 return 0; 880 881 cr0 = cflag2cr0(t->c_cflag); 882 883 s = splserial(); 884 COM_LOCK(sc); 885 886 sc->sc_cr0 = cr0; 887 888 sc->sc_speed = ospeed; 889 890 /* And copy to tty. */ 891 tp->t_ispeed = 0; 892 tp->t_ospeed = t->c_ospeed; 893 tp->t_cflag = t->c_cflag; 894 895 if (!sc->sc_heldchange) { 896 if (sc->sc_tx_busy) { 897 sc->sc_heldtbc = sc->sc_tbc; 898 sc->sc_tbc = 0; 899 sc->sc_heldchange = 1; 900 } else 901 sacom_loadchannelregs(sc); 902 } 903 904 if (!ISSET(t->c_cflag, CHWFLOW)) { 905 /* Disable the high water mark. */ 906 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) { 907 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 908 sacom_schedrx(sc); 909 } 910 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) { 911 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED); 912 sacom_hwiflow(sc); 913 } 914 } 915 916 COM_UNLOCK(sc); 917 splx(s); 918 919 (void) (*tp->t_linesw->l_modem)(tp, 1); 920 921 #ifdef COM_DEBUG 922 if (sacom_debug) 923 comstatus(sc, "comparam "); 924 #endif 925 926 return 0; 927 } 928 929 void 930 sacom_iflush(struct sacom_softc *sc) 931 { 932 bus_space_tag_t iot = sc->sc_iot; 933 bus_space_handle_t ioh = sc->sc_ioh; 934 #ifdef DIAGNOSTIC 935 int reg; 936 #endif 937 int timo; 938 939 #ifdef DIAGNOSTIC 940 reg = 0xffff; 941 #endif 942 timo = 50; 943 /* flush any pending I/O */ 944 if (sc->sc_cr3 & CR3_RXE) { 945 while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_RNE) 946 && --timo) 947 #ifdef DIAGNOSTIC 948 reg = 949 #else 950 (void) 951 #endif 952 bus_space_read_4(iot, ioh, SACOM_DR); 953 } 954 #if 0 955 /* XXX is it good idea to wait TX finish? */ 956 if (sc->sc_cr3 & CR3_TXE) { 957 timo = 500; 958 while (ISSET(bus_space_read_4(iot, ioh, SACOM_SR1), SR1_TBY) 959 && --timo) 960 delay(100); 961 } 962 #endif 963 #ifdef DIAGNOSTIC 964 if (!timo) 965 aprint_debug_dev(sc->sc_dev, "sacom_iflush timeout %02x\n", reg); 966 #endif 967 } 968 969 void 970 sacom_loadchannelregs(struct sacom_softc *sc) 971 { 972 bus_space_tag_t iot = sc->sc_iot; 973 bus_space_handle_t ioh = sc->sc_ioh; 974 975 /* XXXXX necessary? */ 976 sacom_iflush(sc); 977 978 /* Need to stop engines first. */ 979 bus_space_write_4(iot, ioh, SACOM_CR3, 0); 980 981 bus_space_write_4(iot, ioh, SACOM_CR1, sc->sc_speed >> 8); 982 bus_space_write_4(iot, ioh, SACOM_CR2, sc->sc_speed & 0xff); 983 984 bus_space_write_4(iot, ioh, SACOM_CR0, sc->sc_cr0); 985 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3); 986 } 987 988 int 989 sacomhwiflow(struct tty *tp, int block) 990 { 991 #if 0 992 struct sacom_softc *sc = 993 device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev)); 994 int s; 995 996 if (COM_ISALIVE(sc) == 0) 997 return 0; 998 999 if (sc->sc_mcr_rts == 0) 1000 return 0; 1001 1002 s = splserial(); 1003 COM_LOCK(sc); 1004 1005 if (block) { 1006 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 1007 SET(sc->sc_rx_flags, RX_TTY_BLOCKED); 1008 sacom_hwiflow(sc); 1009 } 1010 } else { 1011 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) { 1012 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 1013 sacom_schedrx(sc); 1014 } 1015 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 1016 CLR(sc->sc_rx_flags, RX_TTY_BLOCKED); 1017 sacom_hwiflow(sc); 1018 } 1019 } 1020 1021 COM_UNLOCK(sc); 1022 splx(s); 1023 #endif 1024 return 1; 1025 } 1026 1027 /* 1028 * (un)block input via hw flowcontrol 1029 */ 1030 void 1031 sacom_hwiflow(struct sacom_softc *sc) 1032 { 1033 #if 0 1034 bus_space_tag_t iot = sc->sc_iot; 1035 bus_space_handle_t ioh = sc->sc_ioh; 1036 1037 /* XXX implement */ 1038 #endif 1039 } 1040 1041 1042 void 1043 sacomstart(struct tty *tp) 1044 { 1045 struct sacom_softc *sc = 1046 device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev)); 1047 bus_space_tag_t iot = sc->sc_iot; 1048 bus_space_handle_t ioh = sc->sc_ioh; 1049 int s; 1050 1051 if (COM_ISALIVE(sc) == 0) 1052 return; 1053 1054 s = spltty(); 1055 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 1056 goto out; 1057 if (!ttypull(tp)) 1058 goto out; 1059 1060 /* Grab the first contiguous region of buffer space. */ 1061 { 1062 u_char *tba; 1063 int tbc; 1064 1065 tba = tp->t_outq.c_cf; 1066 tbc = ndqb(&tp->t_outq, 0); 1067 1068 (void)splserial(); 1069 COM_LOCK(sc); 1070 1071 sc->sc_tba = tba; 1072 sc->sc_tbc = tbc; 1073 } 1074 1075 SET(tp->t_state, TS_BUSY); 1076 sc->sc_tx_busy = 1; 1077 1078 /* Enable transmit completion interrupts if necessary. */ 1079 if (!ISSET(sc->sc_cr3, CR3_TIE)) { 1080 SET(sc->sc_cr3, CR3_TIE); 1081 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3); 1082 } 1083 1084 /* Output the first chunk of the contiguous buffer. */ 1085 sacom_filltx(sc); 1086 1087 COM_UNLOCK(sc); 1088 out: 1089 splx(s); 1090 return; 1091 } 1092 1093 void 1094 sacom_filltx(struct sacom_softc *sc) 1095 { 1096 bus_space_tag_t iot = sc->sc_iot; 1097 bus_space_handle_t ioh = sc->sc_ioh; 1098 int c, n; 1099 1100 n = 0; 1101 while (bus_space_read_4(iot, ioh, SACOM_SR1) & SR1_TNF) { 1102 if (n == SACOM_TXFIFOLEN || n == sc->sc_tbc) 1103 break; 1104 c = *(sc->sc_tba + n); 1105 c &= 0xff; 1106 bus_space_write_4(iot, ioh, SACOM_DR, c); 1107 n++; 1108 } 1109 sc->sc_tbc -= n; 1110 sc->sc_tba += n; 1111 } 1112 1113 /* 1114 * Stop output on a line. 1115 */ 1116 void 1117 sacomstop(struct tty *tp, int flag) 1118 { 1119 struct sacom_softc *sc = 1120 device_lookup_private(&sacom_cd, COMUNIT(tp->t_dev)); 1121 int s; 1122 1123 s = splserial(); 1124 COM_LOCK(sc); 1125 if (ISSET(tp->t_state, TS_BUSY)) { 1126 /* Stop transmitting at the next chunk. */ 1127 sc->sc_tbc = 0; 1128 sc->sc_heldtbc = 0; 1129 if (!ISSET(tp->t_state, TS_TTSTOP)) 1130 SET(tp->t_state, TS_FLUSH); 1131 } 1132 COM_UNLOCK(sc); 1133 splx(s); 1134 } 1135 1136 static inline void 1137 sacom_rxsoft(struct sacom_softc *sc, struct tty *tp) 1138 { 1139 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint; 1140 u_char *get, *end; 1141 u_int cc, scc; 1142 u_char sr1; 1143 int code; 1144 int s; 1145 1146 end = sc->sc_ebuf; 1147 get = sc->sc_rbget; 1148 scc = cc = SACOM_RING_SIZE - sc->sc_rbavail; 1149 1150 while (cc) { 1151 code = get[0]; 1152 sr1 = get[1]; 1153 if (ISSET(sr1, SR1_FRE)) 1154 SET(code, TTY_FE); 1155 if (ISSET(sr1, SR1_PRE)) 1156 SET(code, TTY_PE); 1157 if ((*rint)(code, tp) == -1) { 1158 /* 1159 * The line discipline's buffer is out of space. 1160 */ 1161 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 1162 /* 1163 * We're either not using flow control, or the 1164 * line discipline didn't tell us to block for 1165 * some reason. Either way, we have no way to 1166 * know when there's more space available, so 1167 * just drop the rest of the data. 1168 */ 1169 get += cc << 1; 1170 if (get >= end) 1171 get -= SACOM_RING_SIZE << 1; 1172 cc = 0; 1173 } else { 1174 /* 1175 * Don't schedule any more receive processing 1176 * until the line discipline tells us there's 1177 * space available (through comhwiflow()). 1178 * Leave the rest of the data in the input 1179 * buffer. 1180 */ 1181 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 1182 } 1183 break; 1184 } 1185 get += 2; 1186 if (get >= end) 1187 get = sc->sc_rbuf; 1188 cc--; 1189 } 1190 1191 if (cc != scc) { 1192 sc->sc_rbget = get; 1193 s = splserial(); 1194 COM_LOCK(sc); 1195 1196 cc = sc->sc_rbavail += scc - cc; 1197 /* Buffers should be ok again, release possible block. */ 1198 if (cc >= 1) { 1199 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 1200 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1201 SET(sc->sc_cr3, CR3_RIE); 1202 bus_space_write_4(sc->sc_iot, sc->sc_ioh, 1203 SACOM_CR3, sc->sc_cr3); 1204 } 1205 } 1206 COM_UNLOCK(sc); 1207 splx(s); 1208 } 1209 } 1210 1211 static inline void 1212 sacom_txsoft(struct sacom_softc *sc, struct tty *tp) 1213 { 1214 1215 CLR(tp->t_state, TS_BUSY); 1216 if (ISSET(tp->t_state, TS_FLUSH)) 1217 CLR(tp->t_state, TS_FLUSH); 1218 else 1219 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf)); 1220 (*tp->t_linesw->l_start)(tp); 1221 } 1222 1223 static inline void 1224 sacom_stsoft(struct sacom_softc *sc, struct tty *tp) 1225 { 1226 } 1227 1228 void 1229 sacomsoft(void *arg) 1230 { 1231 struct sacom_softc *sc = arg; 1232 struct tty *tp; 1233 1234 if (COM_ISALIVE(sc) == 0) 1235 return; 1236 1237 tp = sc->sc_tty; 1238 1239 if (sc->sc_rx_ready) { 1240 sc->sc_rx_ready = 0; 1241 sacom_rxsoft(sc, tp); 1242 } 1243 1244 if (sc->sc_st_check) { 1245 sc->sc_st_check = 0; 1246 sacom_stsoft(sc, tp); 1247 } 1248 1249 if (sc->sc_tx_done) { 1250 sc->sc_tx_done = 0; 1251 sacom_txsoft(sc, tp); 1252 } 1253 } 1254 1255 int 1256 sacomintr(void *arg) 1257 { 1258 struct sacom_softc *sc = arg; 1259 bus_space_tag_t iot = sc->sc_iot; 1260 bus_space_handle_t ioh = sc->sc_ioh; 1261 u_char *put, *end; 1262 u_int cc; 1263 u_int sr0, sr1; 1264 1265 if (COM_ISALIVE(sc) == 0) 1266 return 0; 1267 1268 COM_LOCK(sc); 1269 sr0 = bus_space_read_4(iot, ioh, SACOM_SR0); 1270 if (!sr0) { 1271 COM_UNLOCK(sc); 1272 return 0; 1273 } 1274 if (ISSET(sr0, SR0_EIF)) 1275 /* XXX silently discard error bits */ 1276 bus_space_read_4(iot, ioh, SACOM_DR); 1277 if (ISSET(sr0, SR0_RBB)) 1278 bus_space_write_4(iot, ioh, SACOM_SR0, SR0_RBB); 1279 if (ISSET(sr0, SR0_REB)) { 1280 bus_space_write_4(iot, ioh, SACOM_SR0, SR0_REB); 1281 #if defined(DDB) || defined(KGDB) 1282 #ifndef DDB_BREAK_CHAR 1283 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 1284 console_debugger(); 1285 } 1286 #endif 1287 #endif /* DDB || KGDB */ 1288 } 1289 1290 1291 end = sc->sc_ebuf; 1292 put = sc->sc_rbput; 1293 cc = sc->sc_rbavail; 1294 1295 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1); 1296 if (ISSET(sr0, SR0_RFS | SR0_RID)) { 1297 if (!ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 1298 while (cc > 0) { 1299 if (!ISSET(sr1, SR1_RNE)) { 1300 bus_space_write_4(iot, ioh, SACOM_SR0, 1301 SR0_RID); 1302 break; 1303 } 1304 put[0] = bus_space_read_4(iot, ioh, SACOM_DR); 1305 put[1] = sr1; 1306 #if defined(DDB) && defined(DDB_BREAK_CHAR) 1307 if (put[0] == DDB_BREAK_CHAR && 1308 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 1309 console_debugger(); 1310 1311 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1); 1312 continue; 1313 } 1314 #endif 1315 put += 2; 1316 if (put >= end) 1317 put = sc->sc_rbuf; 1318 cc--; 1319 1320 sr1 = bus_space_read_4(iot, ioh, SACOM_SR1); 1321 } 1322 1323 /* 1324 * Current string of incoming characters ended because 1325 * no more data was available or we ran out of space. 1326 * Schedule a receive event if any data was received. 1327 * If we're out of space, turn off receive interrupts. 1328 */ 1329 sc->sc_rbput = put; 1330 sc->sc_rbavail = cc; 1331 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) 1332 sc->sc_rx_ready = 1; 1333 1334 /* XXX do RX hardware flow control */ 1335 1336 /* 1337 * If we're out of space, disable receive interrupts 1338 * until the queue has drained a bit. 1339 */ 1340 if (!cc) { 1341 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1342 CLR(sc->sc_cr3, CR3_RIE); 1343 bus_space_write_4(iot, ioh, SACOM_CR3, 1344 sc->sc_cr3); 1345 } 1346 } else { 1347 #ifdef DIAGNOSTIC 1348 panic("sacomintr: we shouldn't reach here"); 1349 #endif 1350 CLR(sc->sc_cr3, CR3_RIE); 1351 bus_space_write_4(iot, ioh, SACOM_CR3, sc->sc_cr3); 1352 } 1353 } 1354 1355 /* 1356 * Done handling any receive interrupts. See if data can be 1357 * transmitted as well. Schedule tx done event if no data left 1358 * and tty was marked busy. 1359 */ 1360 sr0 = bus_space_read_4(iot, ioh, SACOM_SR0); 1361 if (ISSET(sr0, SR0_TFS)) { 1362 /* 1363 * If we've delayed a parameter change, do it now, and restart 1364 * output. 1365 * XXX sacom_loadchannelregs() waits TX completion, 1366 * XXX resulting in ~0.1s hang (300bps, 4 bytes) in worst case 1367 */ 1368 if (sc->sc_heldchange) { 1369 sacom_loadchannelregs(sc); 1370 sc->sc_heldchange = 0; 1371 sc->sc_tbc = sc->sc_heldtbc; 1372 sc->sc_heldtbc = 0; 1373 } 1374 1375 /* Output the next chunk of the contiguous buffer, if any. */ 1376 if (sc->sc_tbc > 0) { 1377 sacom_filltx(sc); 1378 } else { 1379 /* Disable transmit completion interrupts if necessary. */ 1380 if (ISSET(sc->sc_cr3, CR3_TIE)) { 1381 CLR(sc->sc_cr3, CR3_TIE); 1382 bus_space_write_4(iot, ioh, SACOM_CR3, 1383 sc->sc_cr3); 1384 } 1385 if (sc->sc_tx_busy) { 1386 sc->sc_tx_busy = 0; 1387 sc->sc_tx_done = 1; 1388 } 1389 } 1390 } 1391 COM_UNLOCK(sc); 1392 1393 /* Wake up the poller. */ 1394 softint_schedule(sc->sc_si); 1395 1396 #if NRND > 0 && defined(RND_COM) 1397 rnd_add_uint32(&sc->rnd_source, iir | lsr); 1398 #endif 1399 return 1; 1400 } 1401 1402 static void 1403 sacom_j720_init(device_t parent, device_t self) 1404 { 1405 struct sa11x0_softc *sasc; 1406 1407 sasc = device_private(parent); 1408 1409 /* XXX this should be done at sc->enable function */ 1410 bus_space_write_4(sasc->sc_iot, sasc->sc_gpioh, 1411 SAGPIO_PCR, 0xa0000); 1412 bus_space_write_4(sasc->sc_iot, sasc->sc_gpioh, 1413 SAGPIO_PSR, 0x100); 1414 } 1415 1416 /* Initialization for serial console */ 1417 int 1418 sacominit(bus_space_tag_t iot, bus_addr_t iobase, int baud, tcflag_t cflag, 1419 bus_space_handle_t *iohp) 1420 { 1421 int brd, cr0; 1422 1423 if (bus_space_map(iot, iobase, SACOM_NPORTS, 0, iohp)) 1424 aprint_normal("register map failed\n"); 1425 1426 /* wait for the Tx queue to drain and disable the UART */ 1427 while (bus_space_read_4(iot, *iohp, SACOM_SR1) & SR1_TBY) 1428 continue; 1429 bus_space_write_4(iot, *iohp, SACOM_CR3, 0); 1430 1431 cr0 = cflag2cr0(cflag); 1432 bus_space_write_4(iot, *iohp, SACOM_CR0, cr0); 1433 1434 brd = SACOMSPEED(baud); 1435 sacomconsrate = baud; 1436 sacomconsaddr = iobase; 1437 sacomconscflag = cflag; 1438 /* XXX assumes little endian */ 1439 bus_space_write_4(iot, *iohp, SACOM_CR1, brd >> 8); 1440 bus_space_write_4(iot, *iohp, SACOM_CR2, brd & 0xff); 1441 1442 /* enable the UART */ 1443 bus_space_write_4(iot, *iohp, SACOM_CR3, CR3_RXE | CR3_TXE); 1444 1445 return 0; 1446 } 1447 1448 void 1449 sacomcnprobe(struct consdev *cp) 1450 { 1451 cp->cn_pri = CN_REMOTE; 1452 } 1453 1454 void 1455 sacomcninit(struct consdev *cp) 1456 { 1457 if (cp == NULL) { 1458 /* XXX cp == NULL means that MMU is disabled. */ 1459 sacomconsioh = SACOM3_BASE; 1460 sacomconstag = &sa11x0_bs_tag; 1461 cn_tab = &sacomcons; 1462 return; 1463 } 1464 1465 if (sacominit(&sa11x0_bs_tag, CONADDR, CONSPEED, 1466 CONMODE, &sacomconsioh)) 1467 panic("can't init serial console @%x", CONADDR); 1468 cn_tab = &sacomcons; 1469 sacomconstag = &sa11x0_bs_tag; 1470 } 1471 1472 int 1473 sacomcngetc(dev_t dev) 1474 { 1475 int c, s; 1476 1477 s = spltty(); /* XXX do we need this? */ 1478 1479 while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1) 1480 & SR1_RNE)) { 1481 #if defined(DDB) || defined(KGDB) 1482 #ifndef DDB_BREAK_CHAR 1483 u_int sr0; 1484 extern int db_active; 1485 1486 sr0 = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR0); 1487 if (ISSET(sr0, SR0_RBB)) 1488 bus_space_write_4(sacomconstag, sacomconsioh, 1489 SACOM_SR0, SR0_RBB); 1490 if (ISSET(sr0, SR0_REB)) { 1491 bus_space_write_4(sacomconstag, sacomconsioh, 1492 SACOM_SR0, SR0_REB); 1493 if (db_active == 0) 1494 console_debugger(); 1495 } 1496 #endif 1497 #endif /* DDB || KGDB */ 1498 } 1499 1500 c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR); 1501 c &= 0xff; 1502 splx(s); 1503 1504 return c; 1505 } 1506 1507 void 1508 sacomcnputc(dev_t dev, int c) 1509 { 1510 int s; 1511 1512 s = spltty(); /* XXX do we need this? */ 1513 1514 while (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1) 1515 & SR1_TNF)) 1516 continue; 1517 1518 bus_space_write_4(sacomconstag, sacomconsioh, SACOM_DR, c); 1519 splx(s); 1520 } 1521 1522 void 1523 sacomcnpollc(dev_t dev, int on) 1524 { 1525 1526 } 1527 1528 #if 0 1529 #ifdef DEBUG 1530 int 1531 sacomcncharpoll(void) 1532 { 1533 int c; 1534 1535 if (!(bus_space_read_4(sacomconstag, sacomconsioh, SACOM_SR1) 1536 & SR1_RNE)) 1537 return -1; 1538 1539 c = bus_space_read_4(sacomconstag, sacomconsioh, SACOM_DR); 1540 c &= 0xff; 1541 1542 return c; 1543 } 1544 #endif 1545 #endif 1546 1547 /* helper function to identify the com ports used by 1548 console or KGDB (and not yet autoconf attached) */ 1549 int 1550 sacom_is_console(bus_space_tag_t iot, bus_addr_t iobase, 1551 bus_space_handle_t *ioh) 1552 { 1553 bus_space_handle_t help; 1554 1555 if (!sacomconsattached && 1556 iot == sacomconstag && iobase == sacomconsaddr) 1557 help = sacomconsioh; 1558 else 1559 return 0; 1560 1561 if (ioh) 1562 *ioh = help; 1563 return 1; 1564 } 1565