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