1 /* $NetBSD: sci.c,v 1.52 2009/03/14 15:36:13 dsl Exp $ */ 2 3 /*- 4 * Copyright (C) 1999 T.Horiuchi and SAITOH Masanobu. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 /*- 30 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 31 * All rights reserved. 32 * 33 * This code is derived from software contributed to The NetBSD Foundation 34 * by Charles M. Hannum. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 46 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 47 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 48 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 49 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 50 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 51 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 52 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 53 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 54 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 55 * POSSIBILITY OF SUCH DAMAGE. 56 */ 57 58 /* 59 * Copyright (c) 1991 The Regents of the University of California. 60 * All rights reserved. 61 * 62 * Redistribution and use in source and binary forms, with or without 63 * modification, are permitted provided that the following conditions 64 * are met: 65 * 1. Redistributions of source code must retain the above copyright 66 * notice, this list of conditions and the following disclaimer. 67 * 2. Redistributions in binary form must reproduce the above copyright 68 * notice, this list of conditions and the following disclaimer in the 69 * documentation and/or other materials provided with the distribution. 70 * 3. Neither the name of the University nor the names of its contributors 71 * may be used to endorse or promote products derived from this software 72 * without specific prior written permission. 73 * 74 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 75 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 76 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 77 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 78 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 79 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 80 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 81 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 82 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 83 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 84 * SUCH DAMAGE. 85 * 86 * @(#)com.c 7.5 (Berkeley) 5/16/91 87 */ 88 89 /* 90 * SH internal serial driver 91 * 92 * This code is derived from both z8530tty.c and com.c 93 */ 94 95 #include <sys/cdefs.h> 96 __KERNEL_RCSID(0, "$NetBSD: sci.c,v 1.52 2009/03/14 15:36:13 dsl Exp $"); 97 98 #include "opt_kgdb.h" 99 #include "opt_sci.h" 100 101 #include <sys/param.h> 102 #include <sys/systm.h> 103 #include <sys/tty.h> 104 #include <sys/proc.h> 105 #include <sys/conf.h> 106 #include <sys/file.h> 107 #include <sys/syslog.h> 108 #include <sys/kernel.h> 109 #include <sys/device.h> 110 #include <sys/malloc.h> 111 #include <sys/kauth.h> 112 #include <sys/intr.h> 113 114 #include <dev/cons.h> 115 116 #include <sh3/clock.h> 117 #include <sh3/scireg.h> 118 #include <sh3/pfcreg.h> 119 #include <sh3/tmureg.h> 120 #include <sh3/exception.h> 121 122 static void scistart(struct tty *); 123 static int sciparam(struct tty *, struct termios *); 124 125 void scicnprobe(struct consdev *); 126 void scicninit(struct consdev *); 127 void scicnputc(dev_t, int); 128 int scicngetc(dev_t); 129 void scicnpoolc(dev_t, int); 130 int sciintr(void *); 131 132 struct sci_softc { 133 struct device sc_dev; /* boilerplate */ 134 struct tty *sc_tty; 135 void *sc_si; 136 callout_t sc_diag_ch; 137 138 #if 0 139 bus_space_tag_t sc_iot; /* ISA i/o space identifier */ 140 bus_space_handle_t sc_ioh; /* ISA io handle */ 141 142 int sc_drq; 143 144 int sc_frequency; 145 #endif 146 147 u_int sc_overflows, 148 sc_floods, 149 sc_errors; /* number of retries so far */ 150 u_char sc_status[7]; /* copy of registers */ 151 152 int sc_hwflags; 153 int sc_swflags; 154 u_int sc_fifolen; /* XXX always 0? */ 155 156 u_int sc_r_hiwat, 157 sc_r_lowat; 158 u_char *volatile sc_rbget, 159 *volatile sc_rbput; 160 volatile u_int sc_rbavail; 161 u_char *sc_rbuf, 162 *sc_ebuf; 163 164 u_char *sc_tba; /* transmit buffer address */ 165 u_int sc_tbc, /* transmit byte count */ 166 sc_heldtbc; 167 168 volatile u_char sc_rx_flags, /* receiver blocked */ 169 #define RX_TTY_BLOCKED 0x01 170 #define RX_TTY_OVERFLOWED 0x02 171 #define RX_IBUF_BLOCKED 0x04 172 #define RX_IBUF_OVERFLOWED 0x08 173 #define RX_ANY_BLOCK 0x0f 174 sc_tx_busy, /* working on an output chunk */ 175 sc_tx_done, /* done with one output chunk */ 176 sc_tx_stopped, /* H/W level stop (lost CTS) */ 177 sc_st_check, /* got a status interrupt */ 178 sc_rx_ready; 179 180 volatile u_char sc_heldchange; 181 }; 182 183 /* controller driver configuration */ 184 static int sci_match(struct device *, struct cfdata *, void *); 185 static void sci_attach(struct device *, struct device *, void *); 186 187 void sci_break(struct sci_softc *, int); 188 void sci_iflush(struct sci_softc *); 189 190 #define integrate static inline 191 void scisoft(void *); 192 193 integrate void sci_rxsoft(struct sci_softc *, struct tty *); 194 integrate void sci_txsoft(struct sci_softc *, struct tty *); 195 integrate void sci_stsoft(struct sci_softc *, struct tty *); 196 integrate void sci_schedrx(struct sci_softc *); 197 void scidiag(void *); 198 199 #define SCIUNIT_MASK 0x7ffff 200 #define SCIDIALOUT_MASK 0x80000 201 202 #define SCIUNIT(x) (minor(x) & SCIUNIT_MASK) 203 #define SCIDIALOUT(x) (minor(x) & SCIDIALOUT_MASK) 204 205 /* Hardware flag masks */ 206 #define SCI_HW_NOIEN 0x01 207 #define SCI_HW_FIFO 0x02 208 #define SCI_HW_FLOW 0x08 209 #define SCI_HW_DEV_OK 0x20 210 #define SCI_HW_CONSOLE 0x40 211 #define SCI_HW_KGDB 0x80 212 213 /* Buffer size for character buffer */ 214 #define SCI_RING_SIZE 2048 215 216 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */ 217 u_int sci_rbuf_hiwat = (SCI_RING_SIZE * 1) / 4; 218 u_int sci_rbuf_lowat = (SCI_RING_SIZE * 3) / 4; 219 220 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */ 221 int sciconscflag = CONMODE; 222 int sciisconsole = 0; 223 224 #ifdef SCICN_SPEED 225 int scicn_speed = SCICN_SPEED; 226 #else 227 int scicn_speed = 9600; 228 #endif 229 230 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */ 231 232 u_int sci_rbuf_size = SCI_RING_SIZE; 233 234 CFATTACH_DECL(sci, sizeof(struct sci_softc), 235 sci_match, sci_attach, NULL, NULL); 236 237 extern struct cfdriver sci_cd; 238 239 static int sci_attached; 240 241 dev_type_open(sciopen); 242 dev_type_close(sciclose); 243 dev_type_read(sciread); 244 dev_type_write(sciwrite); 245 dev_type_ioctl(sciioctl); 246 dev_type_stop(scistop); 247 dev_type_tty(scitty); 248 dev_type_poll(scipoll); 249 250 const struct cdevsw sci_cdevsw = { 251 sciopen, sciclose, sciread, sciwrite, sciioctl, 252 scistop, scitty, scipoll, nommap, ttykqfilter, D_TTY 253 }; 254 255 void InitializeSci (unsigned int); 256 257 /* 258 * following functions are debugging prupose only 259 */ 260 #define CR 0x0D 261 #define I2C_ADRS (*(volatile unsigned int *)0xa8000000) 262 #define USART_ON (unsigned int)~0x08 263 264 void sci_putc(unsigned char); 265 unsigned char sci_getc(void); 266 int SciErrCheck(void); 267 268 /* 269 * InitializeSci 270 * : unsigned int bps; 271 * : SCI(Serial Communication Interface) 272 */ 273 274 void 275 InitializeSci(unsigned int bps) 276 { 277 278 /* Initialize SCR */ 279 SHREG_SCSCR = 0x00; 280 281 /* Serial Mode Register */ 282 SHREG_SCSMR = 0x00; /* Async,8bit,NonParity,Even,1Stop,NoMulti */ 283 284 /* Bit Rate Register */ 285 SHREG_SCBRR = divrnd(sh_clock_get_pclock(), 32 * bps) - 1; 286 287 /* 288 * wait 1mSec, because Send/Recv must begin 1 bit period after 289 * BRR is set. 290 */ 291 delay(1000); 292 293 /* Send permission, Receive permission ON */ 294 SHREG_SCSCR = SCSCR_TE | SCSCR_RE; 295 296 /* Serial Status Register */ 297 SHREG_SCSSR &= SCSSR_TDRE; /* Clear Status */ 298 299 #if 0 300 I2C_ADRS &= ~0x08; /* enable RS-232C */ 301 #endif 302 } 303 304 305 /* 306 * sci_putc 307 * : unsigned char c; 308 */ 309 void 310 sci_putc(unsigned char c) 311 { 312 313 /* wait for ready */ 314 while ((SHREG_SCSSR & SCSSR_TDRE) == 0) 315 ; 316 317 /* write send data to send register */ 318 SHREG_SCTDR = c; 319 320 /* clear ready flag */ 321 SHREG_SCSSR &= ~SCSSR_TDRE; 322 } 323 324 /* 325 * : SciErrCheck 326 * 0x20 = over run 327 * 0x10 = frame error 328 * 0x80 = parity error 329 */ 330 int 331 SciErrCheck(void) 332 { 333 334 return(SHREG_SCSSR & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)); 335 } 336 337 /* 338 * sci_getc 339 */ 340 unsigned char 341 sci_getc(void) 342 { 343 unsigned char c, err_c; 344 345 while (((err_c = SHREG_SCSSR) 346 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) == 0) 347 ; 348 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) { 349 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER); 350 return(err_c |= 0x80); 351 } 352 353 c = SHREG_SCRDR; 354 355 SHREG_SCSSR &= ~SCSSR_RDRF; 356 357 return(c); 358 } 359 360 static int 361 sci_match(struct device *parent, struct cfdata *cfp, void *aux) 362 { 363 364 if (strcmp(cfp->cf_name, "sci") || sci_attached) 365 return 0; 366 367 return 1; 368 } 369 370 static void 371 sci_attach(struct device *parent, struct device *self, void *aux) 372 { 373 struct sci_softc *sc = (struct sci_softc *)self; 374 struct tty *tp; 375 376 sci_attached = 1; 377 378 sc->sc_hwflags = 0; /* XXX */ 379 sc->sc_swflags = 0; /* XXX */ 380 sc->sc_fifolen = 0; /* XXX */ 381 382 if (sciisconsole) { 383 SET(sc->sc_hwflags, SCI_HW_CONSOLE); 384 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 385 printf("\n%s: console\n", sc->sc_dev.dv_xname); 386 } else { 387 InitializeSci(9600); 388 printf("\n"); 389 } 390 391 callout_init(&sc->sc_diag_ch, 0); 392 393 intc_intr_establish(SH_INTEVT_SCI_ERI, IST_LEVEL, IPL_SERIAL, sciintr, 394 sc); 395 intc_intr_establish(SH_INTEVT_SCI_RXI, IST_LEVEL, IPL_SERIAL, sciintr, 396 sc); 397 intc_intr_establish(SH_INTEVT_SCI_TXI, IST_LEVEL, IPL_SERIAL, sciintr, 398 sc); 399 intc_intr_establish(SH_INTEVT_SCI_TEI, IST_LEVEL, IPL_SERIAL, sciintr, 400 sc); 401 402 sc->sc_si = softint_establish(SOFTINT_SERIAL, scisoft, sc); 403 SET(sc->sc_hwflags, SCI_HW_DEV_OK); 404 405 tp = ttymalloc(); 406 tp->t_oproc = scistart; 407 tp->t_param = sciparam; 408 tp->t_hwiflow = NULL; 409 410 sc->sc_tty = tp; 411 sc->sc_rbuf = malloc(sci_rbuf_size << 1, M_DEVBUF, M_NOWAIT); 412 if (sc->sc_rbuf == NULL) { 413 printf("%s: unable to allocate ring buffer\n", 414 sc->sc_dev.dv_xname); 415 return; 416 } 417 sc->sc_ebuf = sc->sc_rbuf + (sci_rbuf_size << 1); 418 419 tty_attach(tp); 420 } 421 422 /* 423 * Start or restart transmission. 424 */ 425 static void 426 scistart(struct tty *tp) 427 { 428 struct sci_softc *sc = device_lookup_private(&sci_cd,SCIUNIT(tp->t_dev)); 429 int s; 430 431 s = spltty(); 432 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 433 goto out; 434 if (sc->sc_tx_stopped) 435 goto out; 436 if (!ttypull(tp)) 437 goto out; 438 439 /* Grab the first contiguous region of buffer space. */ 440 { 441 u_char *tba; 442 int tbc; 443 444 tba = tp->t_outq.c_cf; 445 tbc = ndqb(&tp->t_outq, 0); 446 447 (void)splserial(); 448 449 sc->sc_tba = tba; 450 sc->sc_tbc = tbc; 451 } 452 453 SET(tp->t_state, TS_BUSY); 454 sc->sc_tx_busy = 1; 455 456 /* Enable transmit completion interrupts if necessary. */ 457 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE; 458 459 /* Output the first byte of the contiguous buffer. */ 460 { 461 if (sc->sc_tbc > 0) { 462 sci_putc(*(sc->sc_tba)); 463 sc->sc_tba++; 464 sc->sc_tbc--; 465 } 466 } 467 out: 468 splx(s); 469 return; 470 } 471 472 /* 473 * Set SCI tty parameters from termios. 474 * XXX - Should just copy the whole termios after 475 * making sure all the changes could be done. 476 */ 477 static int 478 sciparam(struct tty *tp, struct termios *t) 479 { 480 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev)); 481 int ospeed = t->c_ospeed; 482 int s; 483 484 if (!device_is_active(&sc->sc_dev)) 485 return (EIO); 486 487 /* Check requested parameters. */ 488 if (ospeed < 0) 489 return (EINVAL); 490 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 491 return (EINVAL); 492 493 /* 494 * For the console, always force CLOCAL and !HUPCL, so that the port 495 * is always active. 496 */ 497 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 498 ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) { 499 SET(t->c_cflag, CLOCAL); 500 CLR(t->c_cflag, HUPCL); 501 } 502 503 /* 504 * If there were no changes, don't do anything. This avoids dropping 505 * input and improves performance when all we did was frob things like 506 * VMIN and VTIME. 507 */ 508 if (tp->t_ospeed == t->c_ospeed && 509 tp->t_cflag == t->c_cflag) 510 return (0); 511 512 #if 0 513 /* XXX (msaitoh) */ 514 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); 515 #endif 516 517 s = splserial(); 518 519 /* 520 * Set the FIFO threshold based on the receive speed. 521 * 522 * * If it's a low speed, it's probably a mouse or some other 523 * interactive device, so set the threshold low. 524 * * If it's a high speed, trim the trigger level down to prevent 525 * overflows. 526 * * Otherwise set it a bit higher. 527 */ 528 #if 0 529 /* XXX (msaitoh) */ 530 if (ISSET(sc->sc_hwflags, SCI_HW_HAYESP)) 531 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8; 532 else if (ISSET(sc->sc_hwflags, SCI_HW_FIFO)) 533 sc->sc_fifo = FIFO_ENABLE | 534 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 : 535 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4); 536 else 537 sc->sc_fifo = 0; 538 #endif 539 540 /* And copy to tty. */ 541 tp->t_ispeed = 0; 542 tp->t_ospeed = t->c_ospeed; 543 tp->t_cflag = t->c_cflag; 544 545 if (!sc->sc_heldchange) { 546 if (sc->sc_tx_busy) { 547 sc->sc_heldtbc = sc->sc_tbc; 548 sc->sc_tbc = 0; 549 sc->sc_heldchange = 1; 550 } 551 #if 0 552 /* XXX (msaitoh) */ 553 else 554 sci_loadchannelregs(sc); 555 #endif 556 } 557 558 if (!ISSET(t->c_cflag, CHWFLOW)) { 559 /* Disable the high water mark. */ 560 sc->sc_r_hiwat = 0; 561 sc->sc_r_lowat = 0; 562 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) { 563 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 564 sci_schedrx(sc); 565 } 566 } else { 567 sc->sc_r_hiwat = sci_rbuf_hiwat; 568 sc->sc_r_lowat = sci_rbuf_lowat; 569 } 570 571 splx(s); 572 573 #ifdef SCI_DEBUG 574 if (sci_debug) 575 scistatus(sc, "sciparam "); 576 #endif 577 578 if (!ISSET(t->c_cflag, CHWFLOW)) { 579 if (sc->sc_tx_stopped) { 580 sc->sc_tx_stopped = 0; 581 scistart(tp); 582 } 583 } 584 585 return (0); 586 } 587 588 void 589 sci_iflush(struct sci_softc *sc) 590 { 591 unsigned char err_c; 592 volatile unsigned char c; 593 594 if (((err_c = SHREG_SCSSR) 595 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) { 596 597 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) { 598 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER); 599 return; 600 } 601 602 c = SHREG_SCRDR; 603 604 SHREG_SCSSR &= ~SCSSR_RDRF; 605 } 606 } 607 608 int 609 sciopen(dev_t dev, int flag, int mode, struct lwp *l) 610 { 611 struct sci_softc *sc; 612 struct tty *tp; 613 int s, s2; 614 int error; 615 616 sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 617 if (sc == 0 || !ISSET(sc->sc_hwflags, SCI_HW_DEV_OK) || 618 sc->sc_rbuf == NULL) 619 return (ENXIO); 620 621 if (!device_is_active(&sc->sc_dev)) 622 return (ENXIO); 623 624 #ifdef KGDB 625 /* 626 * If this is the kgdb port, no other use is permitted. 627 */ 628 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) 629 return (EBUSY); 630 #endif 631 632 tp = sc->sc_tty; 633 634 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 635 return (EBUSY); 636 637 s = spltty(); 638 639 /* 640 * Do the following iff this is a first open. 641 */ 642 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 643 struct termios t; 644 645 tp->t_dev = dev; 646 647 s2 = splserial(); 648 649 /* Turn on interrupts. */ 650 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE; 651 652 splx(s2); 653 654 /* 655 * Initialize the termios status to the defaults. Add in the 656 * sticky bits from TIOCSFLAGS. 657 */ 658 t.c_ispeed = 0; 659 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) { 660 t.c_ospeed = scicn_speed; 661 t.c_cflag = sciconscflag; 662 } else { 663 t.c_ospeed = TTYDEF_SPEED; 664 t.c_cflag = TTYDEF_CFLAG; 665 } 666 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 667 SET(t.c_cflag, CLOCAL); 668 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 669 SET(t.c_cflag, CRTSCTS); 670 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 671 SET(t.c_cflag, MDMBUF); 672 /* Make sure sciparam() will do something. */ 673 tp->t_ospeed = 0; 674 (void) sciparam(tp, &t); 675 tp->t_iflag = TTYDEF_IFLAG; 676 tp->t_oflag = TTYDEF_OFLAG; 677 tp->t_lflag = TTYDEF_LFLAG; 678 ttychars(tp); 679 ttsetwater(tp); 680 681 s2 = splserial(); 682 683 /* Clear the input ring, and unblock. */ 684 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 685 sc->sc_rbavail = sci_rbuf_size; 686 sci_iflush(sc); 687 CLR(sc->sc_rx_flags, RX_ANY_BLOCK); 688 #if 0 689 /* XXX (msaitoh) */ 690 sci_hwiflow(sc); 691 #endif 692 693 #ifdef SCI_DEBUG 694 if (sci_debug) 695 scistatus(sc, "sciopen "); 696 #endif 697 698 splx(s2); 699 } 700 701 splx(s); 702 703 error = ttyopen(tp, SCIDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 704 if (error) 705 goto bad; 706 707 error = (*tp->t_linesw->l_open)(dev, tp); 708 if (error) 709 goto bad; 710 711 return (0); 712 713 bad: 714 715 return (error); 716 } 717 718 int 719 sciclose(dev_t dev, int flag, int mode, struct lwp *l) 720 { 721 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 722 struct tty *tp = sc->sc_tty; 723 724 /* XXX This is for cons.c. */ 725 if (!ISSET(tp->t_state, TS_ISOPEN)) 726 return (0); 727 728 (*tp->t_linesw->l_close)(tp, flag); 729 ttyclose(tp); 730 731 if (!device_is_active(&sc->sc_dev)) 732 return (0); 733 734 return (0); 735 } 736 737 int 738 sciread(dev_t dev, struct uio *uio, int flag) 739 { 740 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 741 struct tty *tp = sc->sc_tty; 742 743 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 744 } 745 746 int 747 sciwrite(dev_t dev, struct uio *uio, int flag) 748 { 749 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 750 struct tty *tp = sc->sc_tty; 751 752 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 753 } 754 755 int 756 scipoll(dev_t dev, int events, struct lwp *l) 757 { 758 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 759 struct tty *tp = sc->sc_tty; 760 761 return ((*tp->t_linesw->l_poll)(tp, events, l)); 762 } 763 764 struct tty * 765 scitty(dev_t dev) 766 { 767 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 768 struct tty *tp = sc->sc_tty; 769 770 return (tp); 771 } 772 773 int 774 sciioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 775 { 776 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 777 struct tty *tp = sc->sc_tty; 778 int error; 779 int s; 780 781 if (!device_is_active(&sc->sc_dev)) 782 return (EIO); 783 784 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 785 if (error != EPASSTHROUGH) 786 return (error); 787 788 error = ttioctl(tp, cmd, data, flag, l); 789 if (error != EPASSTHROUGH) 790 return (error); 791 792 error = 0; 793 794 s = splserial(); 795 796 switch (cmd) { 797 case TIOCSBRK: 798 sci_break(sc, 1); 799 break; 800 801 case TIOCCBRK: 802 sci_break(sc, 0); 803 break; 804 805 case TIOCGFLAGS: 806 *(int *)data = sc->sc_swflags; 807 break; 808 809 case TIOCSFLAGS: 810 error = kauth_authorize_device_tty(l->l_cred, 811 KAUTH_DEVICE_TTY_PRIVSET, tp); 812 if (error) 813 break; 814 sc->sc_swflags = *(int *)data; 815 break; 816 817 default: 818 error = EPASSTHROUGH; 819 break; 820 } 821 822 splx(s); 823 824 return (error); 825 } 826 827 integrate void 828 sci_schedrx(struct sci_softc *sc) 829 { 830 831 sc->sc_rx_ready = 1; 832 833 /* Wake up the poller. */ 834 softint_schedule(sc->sc_si); 835 } 836 837 void 838 sci_break(struct sci_softc *sc, int onoff) 839 { 840 841 if (onoff) 842 SHREG_SCSSR &= ~SCSSR_TDRE; 843 else 844 SHREG_SCSSR |= SCSSR_TDRE; 845 846 #if 0 /* XXX */ 847 if (!sc->sc_heldchange) { 848 if (sc->sc_tx_busy) { 849 sc->sc_heldtbc = sc->sc_tbc; 850 sc->sc_tbc = 0; 851 sc->sc_heldchange = 1; 852 } else 853 sci_loadchannelregs(sc); 854 } 855 #endif 856 } 857 858 /* 859 * Stop output, e.g., for ^S or output flush. 860 */ 861 void 862 scistop(struct tty *tp, int flag) 863 { 864 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev)); 865 int s; 866 867 s = splserial(); 868 if (ISSET(tp->t_state, TS_BUSY)) { 869 /* Stop transmitting at the next chunk. */ 870 sc->sc_tbc = 0; 871 sc->sc_heldtbc = 0; 872 if (!ISSET(tp->t_state, TS_TTSTOP)) 873 SET(tp->t_state, TS_FLUSH); 874 } 875 splx(s); 876 } 877 878 void 879 scidiag(void *arg) 880 { 881 struct sci_softc *sc = arg; 882 int overflows, floods; 883 int s; 884 885 s = splserial(); 886 overflows = sc->sc_overflows; 887 sc->sc_overflows = 0; 888 floods = sc->sc_floods; 889 sc->sc_floods = 0; 890 sc->sc_errors = 0; 891 splx(s); 892 893 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n", 894 sc->sc_dev.dv_xname, 895 overflows, overflows == 1 ? "" : "s", 896 floods, floods == 1 ? "" : "s"); 897 } 898 899 integrate void 900 sci_rxsoft(struct sci_softc *sc, struct tty *tp) 901 { 902 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint; 903 u_char *get, *end; 904 u_int cc, scc; 905 u_char ssr; 906 int code; 907 int s; 908 909 end = sc->sc_ebuf; 910 get = sc->sc_rbget; 911 scc = cc = sci_rbuf_size - sc->sc_rbavail; 912 913 if (cc == sci_rbuf_size) { 914 sc->sc_floods++; 915 if (sc->sc_errors++ == 0) 916 callout_reset(&sc->sc_diag_ch, 60 * hz, scidiag, sc); 917 } 918 919 while (cc) { 920 code = get[0]; 921 ssr = get[1]; 922 if (ISSET(ssr, SCSSR_FER | SCSSR_PER)) { 923 if (ISSET(ssr, SCSSR_FER)) 924 SET(code, TTY_FE); 925 if (ISSET(ssr, SCSSR_PER)) 926 SET(code, TTY_PE); 927 } 928 if ((*rint)(code, tp) == -1) { 929 /* 930 * The line discipline's buffer is out of space. 931 */ 932 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 933 /* 934 * We're either not using flow control, or the 935 * line discipline didn't tell us to block for 936 * some reason. Either way, we have no way to 937 * know when there's more space available, so 938 * just drop the rest of the data. 939 */ 940 get += cc << 1; 941 if (get >= end) 942 get -= sci_rbuf_size << 1; 943 cc = 0; 944 } else { 945 /* 946 * Don't schedule any more receive processing 947 * until the line discipline tells us there's 948 * space available (through scihwiflow()). 949 * Leave the rest of the data in the input 950 * buffer. 951 */ 952 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 953 } 954 break; 955 } 956 get += 2; 957 if (get >= end) 958 get = sc->sc_rbuf; 959 cc--; 960 } 961 962 if (cc != scc) { 963 sc->sc_rbget = get; 964 s = splserial(); 965 cc = sc->sc_rbavail += scc - cc; 966 /* Buffers should be ok again, release possible block. */ 967 if (cc >= sc->sc_r_lowat) { 968 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 969 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 970 SHREG_SCSCR |= SCSCR_RIE; 971 } 972 #if 0 973 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) { 974 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED); 975 sci_hwiflow(sc); 976 } 977 #endif 978 } 979 splx(s); 980 } 981 } 982 983 integrate void 984 sci_txsoft(struct sci_softc *sc, struct tty *tp) 985 { 986 987 CLR(tp->t_state, TS_BUSY); 988 if (ISSET(tp->t_state, TS_FLUSH)) 989 CLR(tp->t_state, TS_FLUSH); 990 else 991 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf)); 992 (*tp->t_linesw->l_start)(tp); 993 } 994 995 integrate void 996 sci_stsoft(struct sci_softc *sc, struct tty *tp) 997 { 998 #if 0 999 /* XXX (msaitoh) */ 1000 u_char msr, delta; 1001 int s; 1002 1003 s = splserial(); 1004 msr = sc->sc_msr; 1005 delta = sc->sc_msr_delta; 1006 sc->sc_msr_delta = 0; 1007 splx(s); 1008 1009 if (ISSET(delta, sc->sc_msr_dcd)) { 1010 /* 1011 * Inform the tty layer that carrier detect changed. 1012 */ 1013 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD)); 1014 } 1015 1016 if (ISSET(delta, sc->sc_msr_cts)) { 1017 /* Block or unblock output according to flow control. */ 1018 if (ISSET(msr, sc->sc_msr_cts)) { 1019 sc->sc_tx_stopped = 0; 1020 (*tp->t_linesw->l_start)(tp); 1021 } else { 1022 sc->sc_tx_stopped = 1; 1023 } 1024 } 1025 1026 #ifdef SCI_DEBUG 1027 if (sci_debug) 1028 scistatus(sc, "sci_stsoft"); 1029 #endif 1030 #endif 1031 } 1032 1033 void 1034 scisoft(void *arg) 1035 { 1036 struct sci_softc *sc = arg; 1037 struct tty *tp; 1038 1039 if (!device_is_active(&sc->sc_dev)) 1040 return; 1041 1042 tp = sc->sc_tty; 1043 1044 if (sc->sc_rx_ready) { 1045 sc->sc_rx_ready = 0; 1046 sci_rxsoft(sc, tp); 1047 } 1048 1049 #if 0 1050 if (sc->sc_st_check) { 1051 sc->sc_st_check = 0; 1052 sci_stsoft(sc, tp); 1053 } 1054 #endif 1055 1056 if (sc->sc_tx_done) { 1057 sc->sc_tx_done = 0; 1058 sci_txsoft(sc, tp); 1059 } 1060 } 1061 1062 int 1063 sciintr(void *arg) 1064 { 1065 struct sci_softc *sc = arg; 1066 u_char *put, *end; 1067 u_int cc; 1068 u_short ssr; 1069 1070 if (!device_is_active(&sc->sc_dev)) 1071 return (0); 1072 1073 end = sc->sc_ebuf; 1074 put = sc->sc_rbput; 1075 cc = sc->sc_rbavail; 1076 1077 do { 1078 ssr = SHREG_SCSSR; 1079 if (ISSET(ssr, SCSSR_FER)) { 1080 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_PER | SCSSR_FER); 1081 #if defined(DDB) || defined(KGDB) 1082 #ifdef SH4 1083 if ((SHREG_SCSPTR & SCSPTR_SPB0DT) != 0) { 1084 #else 1085 if ((SHREG_SCSPDR & SCSPDR_SCP0DT) != 0) { 1086 #endif 1087 #ifdef DDB 1088 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) { 1089 console_debugger(); 1090 } 1091 #endif 1092 #ifdef KGDB 1093 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) { 1094 kgdb_connect(1); 1095 } 1096 #endif 1097 } 1098 #endif /* DDB || KGDB */ 1099 } 1100 if ((SHREG_SCSSR & SCSSR_RDRF) != 0) { 1101 if (cc > 0) { 1102 put[0] = SHREG_SCRDR; 1103 put[1] = SHREG_SCSSR & 0x00ff; 1104 1105 put += 2; 1106 if (put >= end) 1107 put = sc->sc_rbuf; 1108 cc--; 1109 } 1110 1111 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER | 1112 SCSSR_RDRF); 1113 1114 /* 1115 * Current string of incoming characters ended because 1116 * no more data was available or we ran out of space. 1117 * Schedule a receive event if any data was received. 1118 * If we're out of space, turn off receive interrupts. 1119 */ 1120 sc->sc_rbput = put; 1121 sc->sc_rbavail = cc; 1122 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) 1123 sc->sc_rx_ready = 1; 1124 1125 /* 1126 * See if we are in danger of overflowing a buffer. If 1127 * so, use hardware flow control to ease the pressure. 1128 */ 1129 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) && 1130 cc < sc->sc_r_hiwat) { 1131 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED); 1132 #if 0 1133 sci_hwiflow(sc); 1134 #endif 1135 } 1136 1137 /* 1138 * If we're out of space, disable receive interrupts 1139 * until the queue has drained a bit. 1140 */ 1141 if (!cc) { 1142 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1143 SHREG_SCSCR &= ~SCSCR_RIE; 1144 } 1145 } else { 1146 if (SHREG_SCSSR & SCSSR_RDRF) { 1147 SHREG_SCSCR &= ~(SCSCR_TIE | SCSCR_RIE); 1148 delay(10); 1149 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE; 1150 continue; 1151 } 1152 } 1153 } while (SHREG_SCSSR & SCSSR_RDRF); 1154 1155 #if 0 1156 msr = bus_space_read_1(iot, ioh, sci_msr); 1157 delta = msr ^ sc->sc_msr; 1158 sc->sc_msr = msr; 1159 if (ISSET(delta, sc->sc_msr_mask)) { 1160 SET(sc->sc_msr_delta, delta); 1161 1162 /* 1163 * Pulse-per-second clock signal on edge of DCD? 1164 */ 1165 if (ISSET(delta, sc->sc_ppsmask)) { 1166 struct timeval tv; 1167 if (ISSET(msr, sc->sc_ppsmask) == 1168 sc->sc_ppsassert) { 1169 /* XXX nanotime() */ 1170 microtime(&tv); 1171 TIMEVAL_TO_TIMESPEC(&tv, 1172 &sc->ppsinfo.assert_timestamp); 1173 if (sc->ppsparam.mode & PPS_OFFSETASSERT) { 1174 timespecadd(&sc->ppsinfo.assert_timestamp, 1175 &sc->ppsparam.assert_offset, 1176 &sc->ppsinfo.assert_timestamp); 1177 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp); 1178 } 1179 1180 #ifdef PPS_SYNC 1181 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT) 1182 hardpps(&tv, tv.tv_usec); 1183 #endif 1184 sc->ppsinfo.assert_sequence++; 1185 sc->ppsinfo.current_mode = 1186 sc->ppsparam.mode; 1187 1188 } else if (ISSET(msr, sc->sc_ppsmask) == 1189 sc->sc_ppsclear) { 1190 /* XXX nanotime() */ 1191 microtime(&tv); 1192 TIMEVAL_TO_TIMESPEC(&tv, 1193 &sc->ppsinfo.clear_timestamp); 1194 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) { 1195 timespecadd(&sc->ppsinfo.clear_timestamp, 1196 &sc->ppsparam.clear_offset, 1197 &sc->ppsinfo.clear_timestamp); 1198 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp); 1199 } 1200 1201 #ifdef PPS_SYNC 1202 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR) 1203 hardpps(&tv, tv.tv_usec); 1204 #endif 1205 sc->ppsinfo.clear_sequence++; 1206 sc->ppsinfo.current_mode = 1207 sc->ppsparam.mode; 1208 } 1209 } 1210 1211 /* 1212 * Stop output immediately if we lose the output 1213 * flow control signal or carrier detect. 1214 */ 1215 if (ISSET(~msr, sc->sc_msr_mask)) { 1216 sc->sc_tbc = 0; 1217 sc->sc_heldtbc = 0; 1218 #ifdef SCI_DEBUG 1219 if (sci_debug) 1220 scistatus(sc, "sciintr "); 1221 #endif 1222 } 1223 1224 sc->sc_st_check = 1; 1225 } 1226 #endif 1227 1228 /* 1229 * Done handling any receive interrupts. See if data can be 1230 * transmitted as well. Schedule tx done event if no data left 1231 * and tty was marked busy. 1232 */ 1233 if ((SHREG_SCSSR & SCSSR_TDRE) != 0) { 1234 /* 1235 * If we've delayed a parameter change, do it now, and restart 1236 * output. 1237 */ 1238 if (sc->sc_heldchange) { 1239 sc->sc_heldchange = 0; 1240 sc->sc_tbc = sc->sc_heldtbc; 1241 sc->sc_heldtbc = 0; 1242 } 1243 1244 /* Output the next chunk of the contiguous buffer, if any. */ 1245 if (sc->sc_tbc > 0) { 1246 sci_putc(*(sc->sc_tba)); 1247 sc->sc_tba++; 1248 sc->sc_tbc--; 1249 } else { 1250 /* Disable transmit completion interrupts if necessary. */ 1251 #if 0 1252 if (ISSET(sc->sc_ier, IER_ETXRDY)) 1253 #endif 1254 SHREG_SCSCR &= ~SCSCR_TIE; 1255 1256 if (sc->sc_tx_busy) { 1257 sc->sc_tx_busy = 0; 1258 sc->sc_tx_done = 1; 1259 } 1260 } 1261 } 1262 1263 /* Wake up the poller. */ 1264 softint_schedule(sc->sc_si); 1265 1266 #if NRND > 0 && defined(RND_SCI) 1267 rnd_add_uint32(&sc->rnd_source, iir | lsr); 1268 #endif 1269 1270 return (1); 1271 } 1272 1273 void 1274 scicnprobe(struct consdev *cp) 1275 { 1276 int maj; 1277 1278 /* locate the major number */ 1279 maj = cdevsw_lookup_major(&sci_cdevsw); 1280 1281 /* Initialize required fields. */ 1282 cp->cn_dev = makedev(maj, 0); 1283 #ifdef SCICONSOLE 1284 cp->cn_pri = CN_REMOTE; 1285 #else 1286 cp->cn_pri = CN_NORMAL; 1287 #endif 1288 } 1289 1290 void 1291 scicninit(struct consdev *cp) 1292 { 1293 1294 InitializeSci(scicn_speed); 1295 sciisconsole = 1; 1296 } 1297 1298 int 1299 scicngetc(dev_t dev) 1300 { 1301 int c; 1302 int s; 1303 1304 s = splserial(); 1305 c = sci_getc(); 1306 splx(s); 1307 1308 return (c); 1309 } 1310 1311 void 1312 scicnputc(dev_t dev, int c) 1313 { 1314 int s; 1315 1316 s = splserial(); 1317 sci_putc((u_char)c); 1318 splx(s); 1319 } 1320