1 /* $NetBSD: sci.c,v 1.62 2019/11/10 21:16:32 chs 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.62 2019/11/10 21:16:32 chs 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_WAITOK); 420 sc->sc_ebuf = sc->sc_rbuf + (sci_rbuf_size << 1); 421 422 tty_attach(tp); 423 } 424 425 /* 426 * Start or restart transmission. 427 */ 428 static void 429 scistart(struct tty *tp) 430 { 431 struct sci_softc *sc = device_lookup_private(&sci_cd,SCIUNIT(tp->t_dev)); 432 int s; 433 434 s = spltty(); 435 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 436 goto out; 437 if (sc->sc_tx_stopped) 438 goto out; 439 if (!ttypull(tp)) 440 goto out; 441 442 /* Grab the first contiguous region of buffer space. */ 443 { 444 u_char *tba; 445 int tbc; 446 447 tba = tp->t_outq.c_cf; 448 tbc = ndqb(&tp->t_outq, 0); 449 450 (void)splserial(); 451 452 sc->sc_tba = tba; 453 sc->sc_tbc = tbc; 454 } 455 456 SET(tp->t_state, TS_BUSY); 457 sc->sc_tx_busy = 1; 458 459 /* Enable transmit completion interrupts if necessary. */ 460 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE; 461 462 /* Output the first byte of the contiguous buffer. */ 463 { 464 if (sc->sc_tbc > 0) { 465 sci_putc(*(sc->sc_tba)); 466 sc->sc_tba++; 467 sc->sc_tbc--; 468 } 469 } 470 out: 471 splx(s); 472 return; 473 } 474 475 /* 476 * Set SCI tty parameters from termios. 477 * XXX - Should just copy the whole termios after 478 * making sure all the changes could be done. 479 */ 480 static int 481 sciparam(struct tty *tp, struct termios *t) 482 { 483 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev)); 484 int ospeed = t->c_ospeed; 485 int s; 486 487 if (!device_is_active(sc->sc_dev)) 488 return (EIO); 489 490 /* Check requested parameters. */ 491 if (ospeed < 0) 492 return (EINVAL); 493 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 494 return (EINVAL); 495 496 /* 497 * For the console, always force CLOCAL and !HUPCL, so that the port 498 * is always active. 499 */ 500 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 501 ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) { 502 SET(t->c_cflag, CLOCAL); 503 CLR(t->c_cflag, HUPCL); 504 } 505 506 /* 507 * If there were no changes, don't do anything. This avoids dropping 508 * input and improves performance when all we did was frob things like 509 * VMIN and VTIME. 510 */ 511 if (tp->t_ospeed == t->c_ospeed && 512 tp->t_cflag == t->c_cflag) 513 return (0); 514 515 #if 0 516 /* XXX (msaitoh) */ 517 lcr = ISSET(sc->sc_lcr, LCR_SBREAK) | cflag2lcr(t->c_cflag); 518 #endif 519 520 s = splserial(); 521 522 /* 523 * Set the FIFO threshold based on the receive speed. 524 * 525 * * If it's a low speed, it's probably a mouse or some other 526 * interactive device, so set the threshold low. 527 * * If it's a high speed, trim the trigger level down to prevent 528 * overflows. 529 * * Otherwise set it a bit higher. 530 */ 531 #if 0 532 /* XXX (msaitoh) */ 533 if (ISSET(sc->sc_hwflags, SCI_HW_HAYESP)) 534 sc->sc_fifo = FIFO_DMA_MODE | FIFO_ENABLE | FIFO_TRIGGER_8; 535 else if (ISSET(sc->sc_hwflags, SCI_HW_FIFO)) 536 sc->sc_fifo = FIFO_ENABLE | 537 (t->c_ospeed <= 1200 ? FIFO_TRIGGER_1 : 538 t->c_ospeed <= 38400 ? FIFO_TRIGGER_8 : FIFO_TRIGGER_4); 539 else 540 sc->sc_fifo = 0; 541 #endif 542 543 /* And copy to tty. */ 544 tp->t_ispeed = 0; 545 tp->t_ospeed = t->c_ospeed; 546 tp->t_cflag = t->c_cflag; 547 548 if (!sc->sc_heldchange) { 549 if (sc->sc_tx_busy) { 550 sc->sc_heldtbc = sc->sc_tbc; 551 sc->sc_tbc = 0; 552 sc->sc_heldchange = 1; 553 } 554 #if 0 555 /* XXX (msaitoh) */ 556 else 557 sci_loadchannelregs(sc); 558 #endif 559 } 560 561 if (!ISSET(t->c_cflag, CHWFLOW)) { 562 /* Disable the high water mark. */ 563 sc->sc_r_hiwat = 0; 564 sc->sc_r_lowat = 0; 565 if (ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) { 566 CLR(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 567 sci_schedrx(sc); 568 } 569 } else { 570 sc->sc_r_hiwat = sci_rbuf_hiwat; 571 sc->sc_r_lowat = sci_rbuf_lowat; 572 } 573 574 splx(s); 575 576 #ifdef SCI_DEBUG 577 if (sci_debug) 578 scistatus(sc, "sciparam "); 579 #endif 580 581 if (!ISSET(t->c_cflag, CHWFLOW)) { 582 if (sc->sc_tx_stopped) { 583 sc->sc_tx_stopped = 0; 584 scistart(tp); 585 } 586 } 587 588 return (0); 589 } 590 591 void 592 sci_iflush(struct sci_softc *sc) 593 { 594 unsigned char err_c; 595 596 if (((err_c = SHREG_SCSSR) 597 & (SCSSR_RDRF | SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) { 598 599 if ((err_c & (SCSSR_ORER | SCSSR_FER | SCSSR_PER)) != 0) { 600 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER); 601 return; 602 } 603 604 (void)SHREG_SCRDR; 605 606 SHREG_SCSSR &= ~SCSSR_RDRF; 607 } 608 } 609 610 int 611 sciopen(dev_t dev, int flag, int mode, struct lwp *l) 612 { 613 struct sci_softc *sc; 614 struct tty *tp; 615 int s, s2; 616 int error; 617 618 sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 619 if (sc == 0 || !ISSET(sc->sc_hwflags, SCI_HW_DEV_OK) || 620 sc->sc_rbuf == NULL) 621 return (ENXIO); 622 623 if (!device_is_active(sc->sc_dev)) 624 return (ENXIO); 625 626 #ifdef KGDB 627 /* 628 * If this is the kgdb port, no other use is permitted. 629 */ 630 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) 631 return (EBUSY); 632 #endif 633 634 tp = sc->sc_tty; 635 636 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 637 return (EBUSY); 638 639 s = spltty(); 640 641 /* 642 * Do the following iff this is a first open. 643 */ 644 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 645 struct termios t; 646 647 tp->t_dev = dev; 648 649 s2 = splserial(); 650 651 /* Turn on interrupts. */ 652 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE; 653 654 splx(s2); 655 656 /* 657 * Initialize the termios status to the defaults. Add in the 658 * sticky bits from TIOCSFLAGS. 659 */ 660 t.c_ispeed = 0; 661 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) { 662 t.c_ospeed = scicn_speed; 663 t.c_cflag = sciconscflag; 664 } else { 665 t.c_ospeed = TTYDEF_SPEED; 666 t.c_cflag = TTYDEF_CFLAG; 667 } 668 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 669 SET(t.c_cflag, CLOCAL); 670 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 671 SET(t.c_cflag, CRTSCTS); 672 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 673 SET(t.c_cflag, MDMBUF); 674 /* Make sure sciparam() will do something. */ 675 tp->t_ospeed = 0; 676 (void) sciparam(tp, &t); 677 tp->t_iflag = TTYDEF_IFLAG; 678 tp->t_oflag = TTYDEF_OFLAG; 679 tp->t_lflag = TTYDEF_LFLAG; 680 ttychars(tp); 681 ttsetwater(tp); 682 683 s2 = splserial(); 684 685 /* Clear the input ring, and unblock. */ 686 sc->sc_rbput = sc->sc_rbget = sc->sc_rbuf; 687 sc->sc_rbavail = sci_rbuf_size; 688 sci_iflush(sc); 689 CLR(sc->sc_rx_flags, RX_ANY_BLOCK); 690 #if 0 691 /* XXX (msaitoh) */ 692 sci_hwiflow(sc); 693 #endif 694 695 #ifdef SCI_DEBUG 696 if (sci_debug) 697 scistatus(sc, "sciopen "); 698 #endif 699 700 splx(s2); 701 } 702 703 splx(s); 704 705 error = ttyopen(tp, SCIDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 706 if (error) 707 goto bad; 708 709 error = (*tp->t_linesw->l_open)(dev, tp); 710 if (error) 711 goto bad; 712 713 return (0); 714 715 bad: 716 717 return (error); 718 } 719 720 int 721 sciclose(dev_t dev, int flag, int mode, struct lwp *l) 722 { 723 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 724 struct tty *tp = sc->sc_tty; 725 726 /* XXX This is for cons.c. */ 727 if (!ISSET(tp->t_state, TS_ISOPEN)) 728 return (0); 729 730 (*tp->t_linesw->l_close)(tp, flag); 731 ttyclose(tp); 732 733 if (!device_is_active(sc->sc_dev)) 734 return (0); 735 736 return (0); 737 } 738 739 int 740 sciread(dev_t dev, struct uio *uio, int flag) 741 { 742 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 743 struct tty *tp = sc->sc_tty; 744 745 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 746 } 747 748 int 749 sciwrite(dev_t dev, struct uio *uio, int flag) 750 { 751 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 752 struct tty *tp = sc->sc_tty; 753 754 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 755 } 756 757 int 758 scipoll(dev_t dev, int events, struct lwp *l) 759 { 760 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 761 struct tty *tp = sc->sc_tty; 762 763 return ((*tp->t_linesw->l_poll)(tp, events, l)); 764 } 765 766 struct tty * 767 scitty(dev_t dev) 768 { 769 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 770 struct tty *tp = sc->sc_tty; 771 772 return (tp); 773 } 774 775 int 776 sciioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 777 { 778 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(dev)); 779 struct tty *tp = sc->sc_tty; 780 int error; 781 int s; 782 783 if (!device_is_active(sc->sc_dev)) 784 return (EIO); 785 786 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 787 if (error != EPASSTHROUGH) 788 return (error); 789 790 error = ttioctl(tp, cmd, data, flag, l); 791 if (error != EPASSTHROUGH) 792 return (error); 793 794 error = 0; 795 796 s = splserial(); 797 798 switch (cmd) { 799 case TIOCSBRK: 800 sci_break(sc, 1); 801 break; 802 803 case TIOCCBRK: 804 sci_break(sc, 0); 805 break; 806 807 case TIOCGFLAGS: 808 *(int *)data = sc->sc_swflags; 809 break; 810 811 case TIOCSFLAGS: 812 error = kauth_authorize_device_tty(l->l_cred, 813 KAUTH_DEVICE_TTY_PRIVSET, tp); 814 if (error) 815 break; 816 sc->sc_swflags = *(int *)data; 817 break; 818 819 default: 820 error = EPASSTHROUGH; 821 break; 822 } 823 824 splx(s); 825 826 return (error); 827 } 828 829 integrate void 830 sci_schedrx(struct sci_softc *sc) 831 { 832 833 sc->sc_rx_ready = 1; 834 835 /* Wake up the poller. */ 836 softint_schedule(sc->sc_si); 837 } 838 839 void 840 sci_break(struct sci_softc *sc, int onoff) 841 { 842 843 if (onoff) 844 SHREG_SCSSR &= ~SCSSR_TDRE; 845 else 846 SHREG_SCSSR |= SCSSR_TDRE; 847 848 #if 0 /* XXX */ 849 if (!sc->sc_heldchange) { 850 if (sc->sc_tx_busy) { 851 sc->sc_heldtbc = sc->sc_tbc; 852 sc->sc_tbc = 0; 853 sc->sc_heldchange = 1; 854 } else 855 sci_loadchannelregs(sc); 856 } 857 #endif 858 } 859 860 /* 861 * Stop output, e.g., for ^S or output flush. 862 */ 863 void 864 scistop(struct tty *tp, int flag) 865 { 866 struct sci_softc *sc = device_lookup_private(&sci_cd, SCIUNIT(tp->t_dev)); 867 int s; 868 869 s = splserial(); 870 if (ISSET(tp->t_state, TS_BUSY)) { 871 /* Stop transmitting at the next chunk. */ 872 sc->sc_tbc = 0; 873 sc->sc_heldtbc = 0; 874 if (!ISSET(tp->t_state, TS_TTSTOP)) 875 SET(tp->t_state, TS_FLUSH); 876 } 877 splx(s); 878 } 879 880 void 881 scidiag(void *arg) 882 { 883 struct sci_softc *sc = arg; 884 int overflows, floods; 885 int s; 886 887 s = splserial(); 888 overflows = sc->sc_overflows; 889 sc->sc_overflows = 0; 890 floods = sc->sc_floods; 891 sc->sc_floods = 0; 892 sc->sc_errors = 0; 893 splx(s); 894 895 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n", 896 device_xname(sc->sc_dev), 897 overflows, overflows == 1 ? "" : "s", 898 floods, floods == 1 ? "" : "s"); 899 } 900 901 integrate void 902 sci_rxsoft(struct sci_softc *sc, struct tty *tp) 903 { 904 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint; 905 u_char *get, *end; 906 u_int cc, scc; 907 u_char ssr; 908 int code; 909 int s; 910 911 end = sc->sc_ebuf; 912 get = sc->sc_rbget; 913 scc = cc = sci_rbuf_size - sc->sc_rbavail; 914 915 if (cc == sci_rbuf_size) { 916 sc->sc_floods++; 917 if (sc->sc_errors++ == 0) 918 callout_reset(&sc->sc_diag_ch, 60 * hz, scidiag, sc); 919 } 920 921 while (cc) { 922 code = get[0]; 923 ssr = get[1]; 924 if (ISSET(ssr, SCSSR_FER | SCSSR_PER)) { 925 if (ISSET(ssr, SCSSR_FER)) 926 SET(code, TTY_FE); 927 if (ISSET(ssr, SCSSR_PER)) 928 SET(code, TTY_PE); 929 } 930 if ((*rint)(code, tp) == -1) { 931 /* 932 * The line discipline's buffer is out of space. 933 */ 934 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 935 /* 936 * We're either not using flow control, or the 937 * line discipline didn't tell us to block for 938 * some reason. Either way, we have no way to 939 * know when there's more space available, so 940 * just drop the rest of the data. 941 */ 942 get += cc << 1; 943 if (get >= end) 944 get -= sci_rbuf_size << 1; 945 cc = 0; 946 } else { 947 /* 948 * Don't schedule any more receive processing 949 * until the line discipline tells us there's 950 * space available (through scihwiflow()). 951 * Leave the rest of the data in the input 952 * buffer. 953 */ 954 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 955 } 956 break; 957 } 958 get += 2; 959 if (get >= end) 960 get = sc->sc_rbuf; 961 cc--; 962 } 963 964 if (cc != scc) { 965 sc->sc_rbget = get; 966 s = splserial(); 967 cc = sc->sc_rbavail += scc - cc; 968 /* Buffers should be ok again, release possible block. */ 969 if (cc >= sc->sc_r_lowat) { 970 if (ISSET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED)) { 971 CLR(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 972 SHREG_SCSCR |= SCSCR_RIE; 973 } 974 #if 0 975 if (ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED)) { 976 CLR(sc->sc_rx_flags, RX_IBUF_BLOCKED); 977 sci_hwiflow(sc); 978 } 979 #endif 980 } 981 splx(s); 982 } 983 } 984 985 integrate void 986 sci_txsoft(struct sci_softc *sc, struct tty *tp) 987 { 988 989 CLR(tp->t_state, TS_BUSY); 990 if (ISSET(tp->t_state, TS_FLUSH)) 991 CLR(tp->t_state, TS_FLUSH); 992 else 993 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf)); 994 (*tp->t_linesw->l_start)(tp); 995 } 996 997 integrate void 998 sci_stsoft(struct sci_softc *sc, struct tty *tp) 999 { 1000 #if 0 1001 /* XXX (msaitoh) */ 1002 u_char msr, delta; 1003 int s; 1004 1005 s = splserial(); 1006 msr = sc->sc_msr; 1007 delta = sc->sc_msr_delta; 1008 sc->sc_msr_delta = 0; 1009 splx(s); 1010 1011 if (ISSET(delta, sc->sc_msr_dcd)) { 1012 /* 1013 * Inform the tty layer that carrier detect changed. 1014 */ 1015 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD)); 1016 } 1017 1018 if (ISSET(delta, sc->sc_msr_cts)) { 1019 /* Block or unblock output according to flow control. */ 1020 if (ISSET(msr, sc->sc_msr_cts)) { 1021 sc->sc_tx_stopped = 0; 1022 (*tp->t_linesw->l_start)(tp); 1023 } else { 1024 sc->sc_tx_stopped = 1; 1025 } 1026 } 1027 1028 #ifdef SCI_DEBUG 1029 if (sci_debug) 1030 scistatus(sc, "sci_stsoft"); 1031 #endif 1032 #endif 1033 } 1034 1035 void 1036 scisoft(void *arg) 1037 { 1038 struct sci_softc *sc = arg; 1039 struct tty *tp; 1040 1041 if (!device_is_active(sc->sc_dev)) 1042 return; 1043 1044 tp = sc->sc_tty; 1045 1046 if (sc->sc_rx_ready) { 1047 sc->sc_rx_ready = 0; 1048 sci_rxsoft(sc, tp); 1049 } 1050 1051 #if 0 1052 if (sc->sc_st_check) { 1053 sc->sc_st_check = 0; 1054 sci_stsoft(sc, tp); 1055 } 1056 #endif 1057 1058 if (sc->sc_tx_done) { 1059 sc->sc_tx_done = 0; 1060 sci_txsoft(sc, tp); 1061 } 1062 } 1063 1064 int 1065 sciintr(void *arg) 1066 { 1067 struct sci_softc *sc = arg; 1068 u_char *put, *end; 1069 u_int cc; 1070 u_short ssr; 1071 1072 if (!device_is_active(sc->sc_dev)) 1073 return (0); 1074 1075 end = sc->sc_ebuf; 1076 put = sc->sc_rbput; 1077 cc = sc->sc_rbavail; 1078 1079 do { 1080 ssr = SHREG_SCSSR; 1081 if (ISSET(ssr, SCSSR_FER)) { 1082 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_PER | SCSSR_FER); 1083 #if defined(DDB) || defined(KGDB) 1084 #ifdef SH4 1085 if ((SHREG_SCSPTR & SCSPTR_SPB0DT) != 0) { 1086 #else 1087 if ((SHREG_SCSPDR & SCSPDR_SCP0DT) != 0) { 1088 #endif 1089 #ifdef DDB 1090 if (ISSET(sc->sc_hwflags, SCI_HW_CONSOLE)) { 1091 console_debugger(); 1092 } 1093 #endif 1094 #ifdef KGDB 1095 if (ISSET(sc->sc_hwflags, SCI_HW_KGDB)) { 1096 kgdb_connect(1); 1097 } 1098 #endif 1099 } 1100 #endif /* DDB || KGDB */ 1101 } 1102 if ((SHREG_SCSSR & SCSSR_RDRF) != 0) { 1103 if (cc > 0) { 1104 put[0] = SHREG_SCRDR; 1105 put[1] = SHREG_SCSSR & 0x00ff; 1106 1107 put += 2; 1108 if (put >= end) 1109 put = sc->sc_rbuf; 1110 cc--; 1111 } 1112 1113 SHREG_SCSSR &= ~(SCSSR_ORER | SCSSR_FER | SCSSR_PER | 1114 SCSSR_RDRF); 1115 1116 /* 1117 * Current string of incoming characters ended because 1118 * no more data was available or we ran out of space. 1119 * Schedule a receive event if any data was received. 1120 * If we're out of space, turn off receive interrupts. 1121 */ 1122 sc->sc_rbput = put; 1123 sc->sc_rbavail = cc; 1124 if (!ISSET(sc->sc_rx_flags, RX_TTY_OVERFLOWED)) 1125 sc->sc_rx_ready = 1; 1126 1127 /* 1128 * See if we are in danger of overflowing a buffer. If 1129 * so, use hardware flow control to ease the pressure. 1130 */ 1131 if (!ISSET(sc->sc_rx_flags, RX_IBUF_BLOCKED) && 1132 cc < sc->sc_r_hiwat) { 1133 SET(sc->sc_rx_flags, RX_IBUF_BLOCKED); 1134 #if 0 1135 sci_hwiflow(sc); 1136 #endif 1137 } 1138 1139 /* 1140 * If we're out of space, disable receive interrupts 1141 * until the queue has drained a bit. 1142 */ 1143 if (!cc) { 1144 SET(sc->sc_rx_flags, RX_IBUF_OVERFLOWED); 1145 SHREG_SCSCR &= ~SCSCR_RIE; 1146 } 1147 } else { 1148 if (SHREG_SCSSR & SCSSR_RDRF) { 1149 SHREG_SCSCR &= ~(SCSCR_TIE | SCSCR_RIE); 1150 delay(10); 1151 SHREG_SCSCR |= SCSCR_TIE | SCSCR_RIE; 1152 continue; 1153 } 1154 } 1155 } while (SHREG_SCSSR & SCSSR_RDRF); 1156 1157 #if 0 1158 msr = bus_space_read_1(iot, ioh, sci_msr); 1159 delta = msr ^ sc->sc_msr; 1160 sc->sc_msr = msr; 1161 if (ISSET(delta, sc->sc_msr_mask)) { 1162 SET(sc->sc_msr_delta, delta); 1163 1164 /* 1165 * Pulse-per-second clock signal on edge of DCD? 1166 */ 1167 if (ISSET(delta, sc->sc_ppsmask)) { 1168 struct timeval tv; 1169 if (ISSET(msr, sc->sc_ppsmask) == 1170 sc->sc_ppsassert) { 1171 /* XXX nanotime() */ 1172 microtime(&tv); 1173 TIMEVAL_TO_TIMESPEC(&tv, 1174 &sc->ppsinfo.assert_timestamp); 1175 if (sc->ppsparam.mode & PPS_OFFSETASSERT) { 1176 timespecadd(&sc->ppsinfo.assert_timestamp, 1177 &sc->ppsparam.assert_offset, 1178 &sc->ppsinfo.assert_timestamp); 1179 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.assert_timestamp); 1180 } 1181 1182 #ifdef PPS_SYNC 1183 if (sc->ppsparam.mode & PPS_HARDPPSONASSERT) 1184 hardpps(&tv, tv.tv_usec); 1185 #endif 1186 sc->ppsinfo.assert_sequence++; 1187 sc->ppsinfo.current_mode = 1188 sc->ppsparam.mode; 1189 1190 } else if (ISSET(msr, sc->sc_ppsmask) == 1191 sc->sc_ppsclear) { 1192 /* XXX nanotime() */ 1193 microtime(&tv); 1194 TIMEVAL_TO_TIMESPEC(&tv, 1195 &sc->ppsinfo.clear_timestamp); 1196 if (sc->ppsparam.mode & PPS_OFFSETCLEAR) { 1197 timespecadd(&sc->ppsinfo.clear_timestamp, 1198 &sc->ppsparam.clear_offset, 1199 &sc->ppsinfo.clear_timestamp); 1200 TIMESPEC_TO_TIMEVAL(&tv, &sc->ppsinfo.clear_timestamp); 1201 } 1202 1203 #ifdef PPS_SYNC 1204 if (sc->ppsparam.mode & PPS_HARDPPSONCLEAR) 1205 hardpps(&tv, tv.tv_usec); 1206 #endif 1207 sc->ppsinfo.clear_sequence++; 1208 sc->ppsinfo.current_mode = 1209 sc->ppsparam.mode; 1210 } 1211 } 1212 1213 /* 1214 * Stop output immediately if we lose the output 1215 * flow control signal or carrier detect. 1216 */ 1217 if (ISSET(~msr, sc->sc_msr_mask)) { 1218 sc->sc_tbc = 0; 1219 sc->sc_heldtbc = 0; 1220 #ifdef SCI_DEBUG 1221 if (sci_debug) 1222 scistatus(sc, "sciintr "); 1223 #endif 1224 } 1225 1226 sc->sc_st_check = 1; 1227 } 1228 #endif 1229 1230 /* 1231 * Done handling any receive interrupts. See if data can be 1232 * transmitted as well. Schedule tx done event if no data left 1233 * and tty was marked busy. 1234 */ 1235 if ((SHREG_SCSSR & SCSSR_TDRE) != 0) { 1236 /* 1237 * If we've delayed a parameter change, do it now, and restart 1238 * output. 1239 */ 1240 if (sc->sc_heldchange) { 1241 sc->sc_heldchange = 0; 1242 sc->sc_tbc = sc->sc_heldtbc; 1243 sc->sc_heldtbc = 0; 1244 } 1245 1246 /* Output the next chunk of the contiguous buffer, if any. */ 1247 if (sc->sc_tbc > 0) { 1248 sci_putc(*(sc->sc_tba)); 1249 sc->sc_tba++; 1250 sc->sc_tbc--; 1251 } else { 1252 /* Disable transmit completion interrupts if necessary. */ 1253 #if 0 1254 if (ISSET(sc->sc_ier, IER_ETXRDY)) 1255 #endif 1256 SHREG_SCSCR &= ~SCSCR_TIE; 1257 1258 if (sc->sc_tx_busy) { 1259 sc->sc_tx_busy = 0; 1260 sc->sc_tx_done = 1; 1261 } 1262 } 1263 } 1264 1265 /* Wake up the poller. */ 1266 softint_schedule(sc->sc_si); 1267 1268 #ifdef RND_SCI 1269 rnd_add_uint32(&sc->rnd_source, iir | lsr); 1270 #endif 1271 1272 return (1); 1273 } 1274 1275 void 1276 scicnprobe(struct consdev *cp) 1277 { 1278 int maj; 1279 1280 /* locate the major number */ 1281 maj = cdevsw_lookup_major(&sci_cdevsw); 1282 1283 /* Initialize required fields. */ 1284 cp->cn_dev = makedev(maj, 0); 1285 #ifdef SCICONSOLE 1286 cp->cn_pri = CN_REMOTE; 1287 #else 1288 cp->cn_pri = CN_NORMAL; 1289 #endif 1290 } 1291 1292 void 1293 scicninit(struct consdev *cp) 1294 { 1295 1296 InitializeSci(scicn_speed); 1297 sciisconsole = 1; 1298 } 1299 1300 int 1301 scicngetc(dev_t dev) 1302 { 1303 int c; 1304 int s; 1305 1306 s = splserial(); 1307 c = sci_getc(); 1308 splx(s); 1309 1310 return (c); 1311 } 1312 1313 void 1314 scicnputc(dev_t dev, int c) 1315 { 1316 int s; 1317 1318 s = splserial(); 1319 sci_putc((u_char)c); 1320 splx(s); 1321 } 1322