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