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