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