1 /* $NetBSD: sbscn.c,v 1.8 2003/03/28 07:10:35 he Exp $ */ 2 3 /* 4 * Copyright 2000, 2001 5 * Broadcom Corporation. All rights reserved. 6 * 7 * This software is furnished under license and may be used and copied only 8 * in accordance with the following terms and conditions. Subject to these 9 * conditions, you may download, copy, install, use, modify and distribute 10 * modified or unmodified copies of this software in source and/or binary 11 * form. No title or ownership is transferred hereby. 12 * 13 * 1) Any source code used, modified or distributed must reproduce and 14 * retain this copyright notice and list of conditions as they appear in 15 * the source file. 16 * 17 * 2) No right is granted to use any trade name, trademark, or logo of 18 * Broadcom Corporation. The "Broadcom Corporation" name may not be 19 * used to endorse or promote products derived from this software 20 * without the prior written permission of Broadcom Corporation. 21 * 22 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR IMPLIED 23 * WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED WARRANTIES OF 24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR 25 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL BROADCOM BE LIABLE 26 * FOR ANY DAMAGES WHATSOEVER, AND IN PARTICULAR, BROADCOM SHALL NOT BE 27 * LIABLE FOR DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 32 * OR OTHERWISE), EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #define SBSCN_DEBUG 36 37 /* from: $NetBSD: com.c,v 1.172 2000/05/03 19:19:04 thorpej Exp */ 38 39 /*- 40 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc. 41 * All rights reserved. 42 * 43 * This code is derived from software contributed to The NetBSD Foundation 44 * by Charles M. Hannum. 45 * 46 * Redistribution and use in source and binary forms, with or without 47 * modification, are permitted provided that the following conditions 48 * are met: 49 * 1. Redistributions of source code must retain the above copyright 50 * notice, this list of conditions and the following disclaimer. 51 * 2. Redistributions in binary form must reproduce the above copyright 52 * notice, this list of conditions and the following disclaimer in the 53 * documentation and/or other materials provided with the distribution. 54 * 3. All advertising materials mentioning features or use of this software 55 * must display the following acknowledgement: 56 * This product includes software developed by the NetBSD 57 * Foundation, Inc. and its contributors. 58 * 4. Neither the name of The NetBSD Foundation nor the names of its 59 * contributors may be used to endorse or promote products derived 60 * from this software without specific prior written permission. 61 * 62 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 63 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 64 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 65 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 66 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 67 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 68 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 69 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 70 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 71 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 72 * POSSIBILITY OF SUCH DAMAGE. 73 */ 74 75 /* 76 * Copyright (c) 1991 The Regents of the University of California. 77 * All rights reserved. 78 * 79 * Redistribution and use in source and binary forms, with or without 80 * modification, are permitted provided that the following conditions 81 * are met: 82 * 1. Redistributions of source code must retain the above copyright 83 * notice, this list of conditions and the following disclaimer. 84 * 2. Redistributions in binary form must reproduce the above copyright 85 * notice, this list of conditions and the following disclaimer in the 86 * documentation and/or other materials provided with the distribution. 87 * 3. All advertising materials mentioning features or use of this software 88 * must display the following acknowledgement: 89 * This product includes software developed by the University of 90 * California, Berkeley and its contributors. 91 * 4. Neither the name of the University nor the names of its contributors 92 * may be used to endorse or promote products derived from this software 93 * without specific prior written permission. 94 * 95 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 96 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 97 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 98 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 99 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 100 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 101 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 102 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 103 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 104 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 105 * SUCH DAMAGE. 106 * 107 * @(#)com.c 7.5 (Berkeley) 5/16/91 108 */ 109 110 /* 111 * `sbscn' driver, supports SiByte SB-1250 SOC DUART. 112 * 113 * This DUART is quite similar in programming model to the scn2681/68681 114 * DUARTs supported by the NetBSD/amiga `mfc' and NetBSD/pc532 `scn' 115 * driver, but substantial rework would have been necessary to make 116 * those drivers sane w.r.t. bus_space (which would then have been 117 * required on NetBSD/sbmips very early on), and to accommodate the 118 * different register mappings. 119 * 120 * So, another driver. Eventually there should be One True Driver, 121 * but we're not here to save the world. 122 */ 123 124 #include "opt_ddb.h" 125 126 #include "rnd.h" 127 #if NRND > 0 && defined(RND_SBSCN) 128 #include <sys/rnd.h> 129 #endif 130 131 #include <sys/param.h> 132 #include <sys/systm.h> 133 #include <sys/ioctl.h> 134 #include <sys/select.h> 135 #include <sys/tty.h> 136 #include <sys/proc.h> 137 #include <sys/user.h> 138 #include <sys/conf.h> 139 #include <sys/file.h> 140 #include <sys/uio.h> 141 #include <sys/kernel.h> 142 #include <sys/syslog.h> 143 #include <sys/types.h> 144 #include <sys/device.h> 145 #include <sys/malloc.h> 146 #include <sys/vnode.h> 147 148 #include <mips/sibyte/dev/sbobiovar.h> 149 #if 0 150 #include <mips/sibyte/dev/sbscnreg.h> 151 #endif 152 #include <mips/sibyte/dev/sbscnvar.h> 153 #include <dev/cons.h> 154 #include <machine/locore.h> 155 156 void sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr); 157 #if defined(DDB) || defined(KGDB) 158 static void sbscn_enable_debugport(struct sbscn_channel *ch); 159 #endif 160 void sbscn_config(struct sbscn_channel *ch); 161 void sbscn_shutdown(struct sbscn_channel *ch); 162 int sbscn_speed(long, long *); 163 static int cflag2modes(tcflag_t, u_char *, u_char *); 164 int sbscn_param(struct tty *, struct termios *); 165 void sbscn_start(struct tty *); 166 int sbscn_hwiflow(struct tty *, int); 167 168 void sbscn_loadchannelregs(struct sbscn_channel *); 169 void sbscn_dohwiflow(struct sbscn_channel *); 170 void sbscn_break(struct sbscn_channel *, int); 171 void sbscn_modem(struct sbscn_channel *, int); 172 void tiocm_to_sbscn(struct sbscn_channel *, int, int); 173 int sbscn_to_tiocm(struct sbscn_channel *); 174 void sbscn_iflush(struct sbscn_channel *); 175 176 int sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag); 177 int sbscn_common_getc(u_long addr, int chan); 178 void sbscn_common_putc(u_long addr, int chan, int c); 179 void sbscn_intr(void *arg, uint32_t status, uint32_t pc); 180 181 int sbscn_cngetc(dev_t dev); 182 void sbscn_cnputc(dev_t dev, int c); 183 void sbscn_cnpollc(dev_t dev, int on); 184 185 extern struct cfdriver sbscn_cd; 186 187 dev_type_open(sbscnopen); 188 dev_type_close(sbscnclose); 189 dev_type_read(sbscnread); 190 dev_type_write(sbscnwrite); 191 dev_type_ioctl(sbscnioctl); 192 dev_type_stop(sbscnstop); 193 dev_type_tty(sbscntty); 194 dev_type_poll(sbscnpoll); 195 196 const struct cdevsw sbscn_cdevsw = { 197 sbscnopen, sbscnclose, sbscnread, sbscnwrite, sbscnioctl, 198 sbscnstop, sbscntty, sbscnpoll, nommap, ttykqfilter, D_TTY 199 }; 200 201 #define integrate static inline 202 void sbscn_soft(void *); 203 integrate void sbscn_rxsoft(struct sbscn_channel *, struct tty *); 204 integrate void sbscn_txsoft(struct sbscn_channel *, struct tty *); 205 integrate void sbscn_stsoft(struct sbscn_channel *, struct tty *); 206 integrate void sbscn_schedrx(struct sbscn_channel *); 207 void sbscn_diag(void *); 208 209 /* 210 * Make this an option variable one can patch. 211 * But be warned: this must be a power of 2! 212 */ 213 u_int sbscn_rbuf_size = SBSCN_RING_SIZE; 214 215 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */ 216 u_int sbscn_rbuf_hiwat = (SBSCN_RING_SIZE * 1) / 4; 217 u_int sbscn_rbuf_lowat = (SBSCN_RING_SIZE * 3) / 4; 218 219 static int sbscn_cons_present; 220 static int sbscn_cons_attached; 221 static u_long sbscn_cons_addr; 222 static int sbscn_cons_chan; 223 static int sbscn_cons_rate; 224 static tcflag_t sbscn_cons_cflag; 225 226 #ifdef KGDB 227 #include <sys/kgdb.h> 228 229 static int sbscn_kgdb_present; 230 static int sbscn_kgdb_attached; 231 static u_long sbscn_kgdb_addr; 232 static int sbscn_kgdb_chan; 233 234 int sbscn_kgdb_getc(void *); 235 void sbscn_kgdb_putc(void *, int); 236 #endif /* KGDB */ 237 238 static int sbscn_match(struct device *, struct cfdata *, void *); 239 static void sbscn_attach(struct device *, struct device *, void *); 240 241 CFATTACH_DECL(sbscn, sizeof(struct sbscn_softc), 242 sbscn_match, sbscn_attach, NULL, NULL); 243 244 #define READ_REG(rp) (mips3_ld((uint64_t *)(rp))) 245 #define WRITE_REG(rp, val) (mips3_sd((uint64_t *)(rp), (val))) 246 247 /* 248 * input and output signals are actually the _inverse_ of the bits in the 249 * input and output port registers! 250 */ 251 #define GET_INPUT_SIGNALS(ch) \ 252 ((~READ_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x280))) & (ch)->ch_i_mask) 253 #define SET_OUTPUT_SIGNALS(ch, val) \ 254 do { \ 255 int sigs_to_set, sigs_to_clr; \ 256 \ 257 sigs_to_set = (ch)->ch_o_mask & val; \ 258 sigs_to_clr = (ch)->ch_o_mask & ~val; \ 259 \ 260 /* set signals by clearing op bits, and vice versa */ \ 261 WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2c0), \ 262 sigs_to_set); \ 263 WRITE_REG(MIPS_PHYS_TO_KSEG1((ch)->ch_sc->sc_addr + 0x2b0), \ 264 sigs_to_clr); \ 265 } while (0) 266 267 static int 268 sbscn_match(struct device *parent, struct cfdata *match, void *aux) 269 { 270 struct sbobio_attach_args *sap = aux; 271 272 if (sap->sa_locs.sa_type != SBOBIO_DEVTYPE_DUART) 273 return (0); 274 275 return 1; 276 } 277 278 static void 279 sbscn_attach(struct device *parent, struct device *self, void *aux) 280 { 281 struct sbscn_softc *sc = (struct sbscn_softc *)self; 282 struct sbobio_attach_args *sap = aux; 283 int i; 284 285 sc->sc_addr = sap->sa_base + sap->sa_locs.sa_offset; 286 287 printf("\n"); 288 for (i = 0; i < 2; i++) 289 sbscn_attach_channel(sc, i, sap->sa_locs.sa_intr[i]); 290 291 /* init duart_opcr */ 292 WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x270), 0); 293 /* init duart_aux_ctrl */ 294 WRITE_REG(MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x210), 0x0f); /* XXX */ 295 } 296 297 void 298 sbscn_attach_channel(struct sbscn_softc *sc, int chan, int intr) 299 { 300 struct sbscn_channel *ch = &sc->sc_channels[chan]; 301 u_long chan_addr; 302 struct tty *tp; 303 304 ch->ch_sc = sc; 305 ch->ch_num = chan; 306 307 chan_addr = sc->sc_addr + (0x100 * chan); 308 ch->ch_base = (void *)MIPS_PHYS_TO_KSEG1(chan_addr); 309 ch->ch_isr_base = 310 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x220 + (0x20 * chan)); 311 ch->ch_imr_base = 312 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x230 + (0x20 * chan)); 313 #ifdef XXXCGDnotyet 314 ch->ch_inchg_base = 315 (void *)MIPS_PHYS_TO_KSEG1(sc->sc_addr + 0x2d0 + (0x10 * chan)); 316 #endif 317 318 ch->ch_i_dcd = ch->ch_i_dcd_pin = 0 /* XXXCGD */; 319 ch->ch_i_cts = ch->ch_i_cts_pin = 0 /* XXXCGD */; 320 ch->ch_i_dsr = ch->ch_i_dsr_pin = 0 /* XXXCGD */; 321 ch->ch_i_ri = ch->ch_i_ri_pin = 0 /* XXXCGD */; 322 ch->ch_i_mask = 323 ch->ch_i_dcd | ch->ch_i_cts | ch->ch_i_dsr | ch->ch_i_ri; 324 ch->ch_o_dtr = ch->ch_o_dtr_pin = 0 /* XXXCGD */; 325 ch->ch_o_rts = ch->ch_o_rts_pin = 0 /* XXXCGD */; 326 ch->ch_o_mask = ch->ch_o_dtr | ch->ch_o_rts; 327 328 ch->ch_intrhand = cpu_intr_establish(intr, IPL_SERIAL, sbscn_intr, ch); 329 callout_init(&ch->ch_diag_callout); 330 331 /* Disable interrupts before configuring the device. */ 332 ch->ch_imr = 0; 333 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 334 335 if (sbscn_cons_present && 336 sbscn_cons_addr == chan_addr && sbscn_cons_chan == chan) { 337 sbscn_cons_attached = 1; 338 339 /* Make sure the console is always "hardwired". */ 340 delay(1000); /* wait for output to finish */ 341 SET(ch->ch_hwflags, SBSCN_HW_CONSOLE); 342 SET(ch->ch_swflags, TIOCFLAG_SOFTCAR); 343 } 344 345 tp = ttymalloc(); 346 tp->t_oproc = sbscn_start; 347 tp->t_param = sbscn_param; 348 tp->t_hwiflow = sbscn_hwiflow; 349 350 ch->ch_tty = tp; 351 ch->ch_rbuf = malloc(sbscn_rbuf_size << 1, M_DEVBUF, M_NOWAIT); 352 if (ch->ch_rbuf == NULL) { 353 printf("%s: channel %d: unable to allocate ring buffer\n", 354 sc->sc_dev.dv_xname, chan); 355 return; 356 } 357 ch->ch_ebuf = ch->ch_rbuf + (sbscn_rbuf_size << 1); 358 359 tty_attach(tp); 360 361 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) { 362 int maj; 363 364 /* locate the major number */ 365 maj = cdevsw_lookup_major(&sbscn_cdevsw); 366 367 cn_tab->cn_dev = makedev(maj, (sc->sc_dev.dv_unit << 1) + chan); 368 369 printf("%s: channel %d: console\n", sc->sc_dev.dv_xname, chan); 370 } 371 372 #ifdef KGDB 373 /* 374 * Allow kgdb to "take over" this port. If this is 375 * the kgdb device, it has exclusive use. 376 */ 377 if (sbscn_kgdb_present && 378 sbscn_kgdb_addr == chan_addr && sbscn_kgdb_chan == chan) { 379 sbscn_kgdb_attached = 1; 380 381 SET(sc->sc_hwflags, SBSCN_HW_KGDB); 382 printf("%s: channel %d: kgdb\n", sc->sc_dev.dv_xname, chan); 383 } 384 #endif 385 386 ch->ch_si = softintr_establish(IPL_SOFTSERIAL, sbscn_soft, ch); 387 388 #if NRND > 0 && defined(RND_SBSCN) 389 rnd_attach_source(&ch->ch_rnd_source, sc->sc_dev.dv_xname, 390 RND_TYPE_TTY, 0); 391 #endif 392 393 sbscn_config(ch); 394 395 SET(ch->ch_hwflags, SBSCN_HW_DEV_OK); 396 } 397 398 int 399 sbscn_speed(long speed, long *brcp) 400 { 401 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */ 402 403 int x, err; 404 int frequency = 100000000; 405 406 *brcp = divrnd(frequency / 20, speed) - 1; 407 408 if (speed <= 0) 409 return (-1); 410 x = divrnd(frequency / 20, speed); 411 if (x <= 0) 412 return (-1); 413 err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000; 414 if (err < 0) 415 err = -err; 416 if (err > SBSCN_TOLERANCE) 417 return (-1); 418 *brcp = x - 1; 419 return (0); 420 421 #undef divrnd 422 } 423 424 #ifdef SBSCN_DEBUG 425 void sbscn_status(struct sbscn_channel *, char *); 426 427 int sbscn_debug = 0 /* XXXCGD */; 428 429 void 430 sbscn_status(struct sbscn_channel *ch, char *str) 431 { 432 struct sbscn_softc *sc = ch->ch_sc; 433 struct tty *tp = ch->ch_tty; 434 435 printf("%s: chan %d: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n", 436 sc->sc_dev.dv_xname, ch->ch_num, str, 437 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-", 438 ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-", 439 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-", 440 ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-", 441 ch->ch_tx_stopped ? "+" : "-"); 442 443 printf("%s: chan %d: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n", 444 sc->sc_dev.dv_xname, ch->ch_num, str, 445 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-", 446 ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-", 447 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-", 448 ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-", 449 ch->ch_rx_flags); 450 } 451 #endif 452 453 #if defined(DDB) || defined(KGDB) 454 static void 455 sbscn_enable_debugport(struct sbscn_channel *ch) 456 { 457 int s; 458 459 /* Turn on line break interrupt, set carrier. */ 460 s = splserial(); 461 462 #if 0 /* DO NOT turn on break interrupt at this time. */ 463 ch->ch_imr = 0x04; 464 #else 465 ch->ch_imr = 0x00; 466 #endif 467 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 468 SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts); 469 SET_OUTPUT_SIGNALS(ch, ch->ch_oports); 470 471 splx(s); 472 } 473 #endif 474 475 void 476 sbscn_config(struct sbscn_channel *ch) 477 { 478 479 /* Disable interrupts before configuring the device. */ 480 ch->ch_imr = 0x00; 481 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 482 483 #ifdef DDB 484 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) 485 sbscn_enable_debugport(ch); 486 #endif 487 488 #ifdef KGDB 489 /* 490 * Allow kgdb to "take over" this port. If this is 491 * the kgdb device, it has exclusive use. 492 */ 493 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) 494 sbscn_enable_debugport(ch); 495 #endif 496 } 497 498 void 499 sbscn_shutdown(struct sbscn_channel *ch) 500 { 501 struct tty *tp = ch->ch_tty; 502 int s; 503 504 s = splserial(); 505 506 /* If we were asserting flow control, then deassert it. */ 507 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED); 508 sbscn_dohwiflow(ch); 509 510 /* Clear any break condition set with TIOCSBRK. */ 511 sbscn_break(ch, 0); 512 513 /* 514 * Hang up if necessary. Wait a bit, so the other side has time to 515 * notice even if we immediately open the port again. 516 * Avoid tsleeping above splhigh(). 517 */ 518 if (ISSET(tp->t_cflag, HUPCL)) { 519 sbscn_modem(ch, 0); 520 splx(s); 521 /* XXX tsleep will only timeout */ 522 (void) tsleep(ch, TTIPRI, ttclos, hz); 523 s = splserial(); 524 } 525 526 /* Turn off interrupts. */ 527 #ifdef DDB 528 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) 529 #if 0 /* DO NOT turn on break interrupt at this time. */ 530 ch->ch_imr = 0x04; /* interrupt on break */ 531 #else 532 ch->ch_imr = 0x00; 533 #endif 534 else 535 #endif 536 ch->ch_imr = 0; 537 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 538 539 splx(s); 540 } 541 542 int 543 sbscnopen(dev_t dev, int flag, int mode, struct proc *p) 544 { 545 int unit = SBSCN_UNIT(dev); 546 int chan = SBSCN_CHAN(dev); 547 struct sbscn_softc *sc; 548 struct sbscn_channel *ch; 549 struct tty *tp; 550 int s, s2; 551 int error; 552 553 if (unit >= sbscn_cd.cd_ndevs) 554 return (ENXIO); 555 sc = sbscn_cd.cd_devs[unit]; 556 if (sc == 0) 557 return (ENXIO); 558 ch = &sc->sc_channels[chan]; 559 if (!ISSET(ch->ch_hwflags, SBSCN_HW_DEV_OK) || ch->ch_rbuf == NULL) 560 return (ENXIO); 561 562 #ifdef KGDB 563 /* 564 * If this is the kgdb port, no other use is permitted. 565 */ 566 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) 567 return (EBUSY); 568 #endif 569 570 tp = ch->ch_tty; 571 572 if (ISSET(tp->t_state, TS_ISOPEN) && 573 ISSET(tp->t_state, TS_XCLUDE) && 574 p->p_ucred->cr_uid != 0) 575 return (EBUSY); 576 577 s = spltty(); 578 579 /* 580 * Do the following iff this is a first open. 581 */ 582 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 583 struct termios t; 584 585 tp->t_dev = dev; 586 587 s2 = splserial(); 588 589 /* Turn on receive, break, and status change interrupts. */ 590 #if 0 /* DO NOT turn on break or status change interrupt at this time. */ 591 ch->ch_imr = 0xe; 592 #else 593 ch->ch_imr = 0x2; 594 #endif 595 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 596 597 /* Fetch the current modem control status, needed later. */ 598 ch->ch_iports = GET_INPUT_SIGNALS(ch); 599 ch->ch_iports_delta = 0; 600 splx(s2); 601 602 /* 603 * Initialize the termios status to the defaults. Add in the 604 * sticky bits from TIOCSFLAGS. 605 */ 606 t.c_ispeed = 0; 607 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) { 608 t.c_ospeed = sbscn_cons_rate; 609 t.c_cflag = sbscn_cons_cflag; 610 } else { 611 t.c_ospeed = TTYDEF_SPEED; 612 t.c_cflag = TTYDEF_CFLAG; 613 } 614 if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL)) 615 SET(t.c_cflag, CLOCAL); 616 if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS)) 617 SET(t.c_cflag, CRTSCTS); 618 if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF)) 619 SET(t.c_cflag, MDMBUF); 620 /* Make sure sbscn_param() will do something. */ 621 tp->t_ospeed = 0; 622 (void) sbscn_param(tp, &t); 623 tp->t_iflag = TTYDEF_IFLAG; 624 tp->t_oflag = TTYDEF_OFLAG; 625 tp->t_lflag = TTYDEF_LFLAG; 626 ttychars(tp); 627 ttsetwater(tp); 628 629 s2 = splserial(); 630 631 /* 632 * Turn on DTR. We must always do this, even if carrier is not 633 * present, because otherwise we'd have to use TIOCSDTR 634 * immediately after setting CLOCAL, which applications do not 635 * expect. We always assert DTR while the device is open 636 * unless explicitly requested to deassert it. 637 */ 638 sbscn_modem(ch, 1); 639 640 /* Clear the input ring, and unblock. */ 641 ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf; 642 ch->ch_rbavail = sbscn_rbuf_size; 643 sbscn_iflush(ch); 644 CLR(ch->ch_rx_flags, RX_ANY_BLOCK); 645 sbscn_dohwiflow(ch); 646 647 #ifdef SBSCN_DEBUG 648 if (sbscn_debug) 649 sbscn_status(ch, "sbscnopen "); 650 #endif 651 652 splx(s2); 653 } 654 655 splx(s); 656 657 error = ttyopen(tp, SBSCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK)); 658 if (error) 659 goto bad; 660 661 error = (*tp->t_linesw->l_open)(dev, tp); 662 if (error) 663 goto bad; 664 665 return (0); 666 667 bad: 668 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 669 /* 670 * We failed to open the device, and nobody else had it opened. 671 * Clean up the state as appropriate. 672 */ 673 sbscn_shutdown(ch); 674 } 675 676 return (error); 677 } 678 679 int 680 sbscnclose(dev_t dev, int flag, int mode, struct proc *p) 681 { 682 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)]; 683 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 684 struct tty *tp = ch->ch_tty; 685 686 /* XXX This is for cons.c. */ 687 if (!ISSET(tp->t_state, TS_ISOPEN)) 688 return (0); 689 690 (*tp->t_linesw->l_close)(tp, flag); 691 ttyclose(tp); 692 693 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 694 /* 695 * Although we got a last close, the device may still be in 696 * use; e.g. if this was the dialout node, and there are still 697 * processes waiting for carrier on the non-dialout node. 698 */ 699 sbscn_shutdown(ch); 700 } 701 702 return (0); 703 } 704 705 int 706 sbscnread(dev_t dev, struct uio *uio, int flag) 707 { 708 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)]; 709 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 710 struct tty *tp = ch->ch_tty; 711 712 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 713 } 714 715 int 716 sbscnwrite(dev_t dev, struct uio *uio, int flag) 717 { 718 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)]; 719 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 720 struct tty *tp = ch->ch_tty; 721 722 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 723 } 724 725 int 726 sbscnpoll(dev_t dev, int events, struct proc *p) 727 { 728 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)]; 729 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 730 struct tty *tp = ch->ch_tty; 731 732 return ((*tp->t_linesw->l_poll)(tp, events, p)); 733 } 734 735 struct tty * 736 sbscntty(dev_t dev) 737 { 738 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)]; 739 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 740 struct tty *tp = ch->ch_tty; 741 742 return (tp); 743 } 744 745 int 746 sbscnioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p) 747 { 748 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(dev)]; 749 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 750 struct tty *tp = ch->ch_tty; 751 int error; 752 int s; 753 754 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p); 755 if (error != EPASSTHROUGH) 756 return (error); 757 758 error = ttioctl(tp, cmd, data, flag, p); 759 if (error != EPASSTHROUGH) 760 return (error); 761 762 error = 0; 763 764 s = splserial(); 765 766 switch (cmd) { 767 case TIOCSBRK: 768 sbscn_break(ch, 1); 769 break; 770 771 case TIOCCBRK: 772 sbscn_break(ch, 0); 773 break; 774 775 case TIOCSDTR: 776 sbscn_modem(ch, 1); 777 break; 778 779 case TIOCCDTR: 780 sbscn_modem(ch, 0); 781 break; 782 783 case TIOCGFLAGS: 784 *(int *)data = ch->ch_swflags; 785 break; 786 787 case TIOCSFLAGS: 788 error = suser(p->p_ucred, &p->p_acflag); 789 if (error) 790 break; 791 ch->ch_swflags = *(int *)data; 792 break; 793 794 case TIOCMSET: 795 case TIOCMBIS: 796 case TIOCMBIC: 797 tiocm_to_sbscn(ch, cmd, *(int *)data); 798 break; 799 800 case TIOCMGET: 801 *(int *)data = sbscn_to_tiocm(ch); 802 break; 803 804 default: 805 error = EPASSTHROUGH; 806 break; 807 } 808 809 splx(s); 810 811 #ifdef SBSCN_DEBUG 812 if (sbscn_debug) 813 sbscn_status(ch, "sbscn_ioctl "); 814 #endif 815 816 return (error); 817 } 818 819 integrate void 820 sbscn_schedrx(struct sbscn_channel *ch) 821 { 822 823 ch->ch_rx_ready = 1; 824 825 /* Wake up the poller. */ 826 softintr_schedule(ch->ch_si); 827 } 828 829 void 830 sbscn_break(struct sbscn_channel *ch, int onoff) 831 { 832 833 /* XXXCGD delay break until not busy */ 834 if (onoff) 835 WRITE_REG(ch->ch_base + 0x50, 0x60); 836 else 837 WRITE_REG(ch->ch_base + 0x50, 0x70); 838 839 #if 0 840 if (!ch->ch_heldchange) { 841 if (ch->ch_tx_busy) { 842 ch->ch_heldtbc = ch->ch_tbc; 843 ch->ch_tbc = 0; 844 ch->ch_heldchange = 1; 845 } else 846 sbscn_loadchannelregs(ch); 847 } 848 #endif 849 } 850 851 void 852 sbscn_modem(struct sbscn_channel *ch, int onoff) 853 { 854 855 if (ch->ch_o_dtr == 0) 856 return; 857 858 if (onoff) 859 SET(ch->ch_oports, ch->ch_o_dtr); 860 else 861 CLR(ch->ch_oports, ch->ch_o_dtr); 862 863 if (!ch->ch_heldchange) { 864 if (ch->ch_tx_busy) { 865 ch->ch_heldtbc = ch->ch_tbc; 866 ch->ch_tbc = 0; 867 ch->ch_heldchange = 1; 868 } else 869 sbscn_loadchannelregs(ch); 870 } 871 } 872 873 void 874 tiocm_to_sbscn(struct sbscn_channel *ch, int how, int ttybits) 875 { 876 u_char bits; 877 878 bits = 0; 879 if (ISSET(ttybits, TIOCM_DTR)) 880 SET(bits, ch->ch_o_dtr); 881 if (ISSET(ttybits, TIOCM_RTS)) 882 SET(bits, ch->ch_o_rts); 883 884 switch (how) { 885 case TIOCMBIC: 886 CLR(ch->ch_oports, bits); 887 break; 888 889 case TIOCMBIS: 890 SET(ch->ch_oports, bits); 891 break; 892 893 case TIOCMSET: 894 ch->ch_oports = bits; 895 break; 896 } 897 898 if (!ch->ch_heldchange) { 899 if (ch->ch_tx_busy) { 900 ch->ch_heldtbc = ch->ch_tbc; 901 ch->ch_tbc = 0; 902 ch->ch_heldchange = 1; 903 } else 904 sbscn_loadchannelregs(ch); 905 } 906 } 907 908 int 909 sbscn_to_tiocm(struct sbscn_channel *ch) 910 { 911 u_char hwbits; 912 int ttybits = 0; 913 914 hwbits = ch->ch_oports; 915 if (ISSET(hwbits, ch->ch_o_dtr)) 916 SET(ttybits, TIOCM_DTR); 917 if (ISSET(hwbits, ch->ch_o_rts)) 918 SET(ttybits, TIOCM_RTS); 919 920 hwbits = ch->ch_iports; 921 if (ISSET(hwbits, ch->ch_i_dcd)) 922 SET(ttybits, TIOCM_CD); 923 if (ISSET(hwbits, ch->ch_i_cts)) 924 SET(ttybits, TIOCM_CTS); 925 if (ISSET(hwbits, ch->ch_i_dsr)) 926 SET(ttybits, TIOCM_DSR); 927 if (ISSET(hwbits, ch->ch_i_ri)) 928 SET(ttybits, TIOCM_RI); 929 930 if (ch->ch_imr != 0) 931 SET(ttybits, TIOCM_LE); 932 933 return (ttybits); 934 } 935 936 static int 937 cflag2modes(cflag, mode1p, mode2p) 938 tcflag_t cflag; 939 u_char *mode1p; 940 u_char *mode2p; 941 { 942 u_char mode1; 943 u_char mode2; 944 int err = 0; 945 946 mode1 = mode2 = 0; 947 948 switch (ISSET(cflag, CSIZE)) { 949 case CS7: 950 mode1 |= 2; /* XXX */ 951 break; 952 default: 953 err = -1; 954 /* FALLTHRU for sanity */ 955 case CS8: 956 mode1 |= 3; /* XXX */ 957 break; 958 } 959 if (!ISSET(cflag, PARENB)) 960 mode1 |= 2 << 3; 961 else { 962 mode1 |= 0 << 3; 963 if (ISSET(cflag, PARODD)) 964 mode1 |= 1 << 2; 965 } 966 967 if (ISSET(cflag, CSTOPB)) 968 mode2 |= 1 << 3; /* two stop bits XXX not std */ 969 970 if (ISSET(cflag, CRTSCTS)) { 971 mode1 |= 1 << 7; 972 mode2 |= 1 << 4; 973 } 974 975 *mode1p = mode1; 976 *mode2p = mode2; 977 return (err); 978 } 979 980 int 981 sbscn_param(struct tty *tp, struct termios *t) 982 { 983 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)]; 984 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)]; 985 long brc; 986 u_char mode1, mode2; 987 int s; 988 989 /* XXX reset to console parameters if console? */ 990 #if 0 991 XXX disable, enable. 992 #endif 993 994 /* Check requested parameters. */ 995 if (sbscn_speed(t->c_ospeed, &brc) < 0) 996 return (EINVAL); 997 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 998 return (EINVAL); 999 1000 /* 1001 * For the console, always force CLOCAL and !HUPCL, so that the port 1002 * is always active. 1003 */ 1004 if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) || 1005 ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) { 1006 SET(t->c_cflag, CLOCAL); 1007 CLR(t->c_cflag, HUPCL); 1008 } 1009 1010 /* 1011 * If there were no changes, don't do anything. This avoids dropping 1012 * input and improves performance when all we did was frob things like 1013 * VMIN and VTIME. 1014 */ 1015 if (tp->t_ospeed == t->c_ospeed && 1016 tp->t_cflag == t->c_cflag) 1017 return (0); 1018 1019 if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0) 1020 return (EINVAL); 1021 1022 s = splserial(); 1023 1024 ch->ch_mode1 = mode1; 1025 ch->ch_mode2 = mode2; 1026 1027 /* 1028 * If we're not in a mode that assumes a connection is present, then 1029 * ignore carrier changes. 1030 */ 1031 if (ISSET(t->c_cflag, CLOCAL | MDMBUF)) 1032 ch->ch_i_dcd = 0; 1033 else 1034 ch->ch_i_dcd = ch->ch_i_dcd_pin; 1035 /* 1036 * Set the flow control pins depending on the current flow control 1037 * mode. 1038 */ 1039 if (ISSET(t->c_cflag, CRTSCTS)) { 1040 ch->ch_o_dtr = ch->ch_o_dtr_pin; 1041 ch->ch_o_rts = ch->ch_o_rts_pin; 1042 ch->ch_i_cts = ch->ch_i_cts_pin; 1043 /* hw controle enable bits in mod regs set by cflag2modes */ 1044 } else if (ISSET(t->c_cflag, MDMBUF)) { 1045 /* 1046 * For DTR/DCD flow control, make sure we don't toggle DTR for 1047 * carrier detection. 1048 */ 1049 ch->ch_o_dtr = 0; 1050 ch->ch_o_rts = ch->ch_o_dtr_pin; 1051 ch->ch_i_cts = ch->ch_i_dcd_pin; 1052 } else { 1053 /* 1054 * If no flow control, then always set RTS. This will make 1055 * the other side happy if it mistakenly thinks we're doing 1056 * RTS/CTS flow control. 1057 */ 1058 ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin; 1059 ch->ch_o_rts = 0; 1060 ch->ch_i_cts = 0; 1061 if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin)) 1062 SET(ch->ch_oports, ch->ch_o_rts_pin); 1063 else 1064 CLR(ch->ch_oports, ch->ch_o_rts_pin); 1065 } 1066 /* XXX maybe mask the ports which generate intrs? */ 1067 1068 ch->ch_brc = brc; 1069 1070 /* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */ 1071 1072 /* And copy to tty. */ 1073 tp->t_ispeed = 0; 1074 tp->t_ospeed = t->c_ospeed; 1075 tp->t_cflag = t->c_cflag; 1076 1077 if (!ch->ch_heldchange) { 1078 if (ch->ch_tx_busy) { 1079 ch->ch_heldtbc = ch->ch_tbc; 1080 ch->ch_tbc = 0; 1081 ch->ch_heldchange = 1; 1082 } else 1083 sbscn_loadchannelregs(ch); 1084 } 1085 1086 if (!ISSET(t->c_cflag, CHWFLOW)) { 1087 /* Disable the high water mark. */ 1088 ch->ch_r_hiwat = 0; 1089 ch->ch_r_lowat = 0; 1090 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) { 1091 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED); 1092 sbscn_schedrx(ch); 1093 } 1094 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) { 1095 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED); 1096 sbscn_dohwiflow(ch); 1097 } 1098 } else { 1099 ch->ch_r_hiwat = sbscn_rbuf_hiwat; 1100 ch->ch_r_lowat = sbscn_rbuf_lowat; 1101 } 1102 1103 splx(s); 1104 1105 /* 1106 * Update the tty layer's idea of the carrier bit, in case we changed 1107 * CLOCAL or MDMBUF. We don't hang up here; we only do that by 1108 * explicit request. 1109 */ 1110 (void) (*tp->t_linesw->l_modem)(tp, 1111 ISSET(ch->ch_iports, ch->ch_i_dcd)); 1112 1113 #ifdef SBSCN_DEBUG 1114 if (sbscn_debug) 1115 sbscn_status(ch, "sbscnparam "); 1116 #endif 1117 1118 if (!ISSET(t->c_cflag, CHWFLOW)) { 1119 if (ch->ch_tx_stopped) { 1120 ch->ch_tx_stopped = 0; 1121 sbscn_start(tp); 1122 } 1123 } 1124 1125 return (0); 1126 } 1127 1128 void 1129 sbscn_iflush(struct sbscn_channel *ch) 1130 { 1131 #ifdef DIAGNOSTIC 1132 int reg; 1133 #endif 1134 int timo; 1135 1136 #ifdef DIAGNOSTIC 1137 reg = 0xffff; 1138 #endif 1139 timo = 50000; 1140 /* flush any pending I/O */ 1141 while (ISSET(READ_REG(ch->ch_base + 0x20), 0x01) 1142 && --timo) 1143 #ifdef DIAGNOSTIC 1144 reg = 1145 #else 1146 (void) 1147 #endif 1148 READ_REG(ch->ch_base + 0x60); 1149 #ifdef DIAGNOSTIC 1150 if (!timo) 1151 printf("%s: sbscn_iflush timeout %02x\n", 1152 ch->ch_sc->sc_dev.dv_xname, reg & 0xff); 1153 #endif 1154 } 1155 1156 void 1157 sbscn_loadchannelregs(struct sbscn_channel *ch) 1158 { 1159 1160 /* XXXXX necessary? */ 1161 sbscn_iflush(ch); 1162 1163 WRITE_REG(ch->ch_imr_base, 0); 1164 1165 // XXX disable? 1166 WRITE_REG(ch->ch_base + 0x00, ch->ch_mode1); 1167 WRITE_REG(ch->ch_base + 0x10, ch->ch_mode2); 1168 WRITE_REG(ch->ch_base + 0x30, ch->ch_brc); 1169 1170 ch->ch_oports_active = ch->ch_oports; 1171 SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active); 1172 1173 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1174 } 1175 1176 int 1177 sbscn_hwiflow(struct tty *tp, int block) 1178 { 1179 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)]; 1180 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)]; 1181 int s; 1182 1183 if (ch->ch_o_rts == 0) 1184 return (0); 1185 1186 s = splserial(); 1187 if (block) { 1188 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) { 1189 SET(ch->ch_rx_flags, RX_TTY_BLOCKED); 1190 sbscn_dohwiflow(ch); 1191 } 1192 } else { 1193 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) { 1194 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED); 1195 sbscn_schedrx(ch); 1196 } 1197 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) { 1198 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED); 1199 sbscn_dohwiflow(ch); 1200 } 1201 } 1202 splx(s); 1203 return (1); 1204 } 1205 1206 /* 1207 * (un)block input via hw flowcontrol 1208 */ 1209 void 1210 sbscn_dohwiflow(struct sbscn_channel *ch) 1211 { 1212 1213 if (ch->ch_o_rts == 0) 1214 return; 1215 1216 if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) { 1217 CLR(ch->ch_oports, ch->ch_o_rts); 1218 CLR(ch->ch_oports_active, ch->ch_o_rts); 1219 } else { 1220 SET(ch->ch_oports, ch->ch_o_rts); 1221 SET(ch->ch_oports_active, ch->ch_o_rts); 1222 } 1223 SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active); 1224 } 1225 1226 void 1227 sbscn_start(struct tty *tp) 1228 { 1229 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)]; 1230 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)]; 1231 int s; 1232 1233 s = spltty(); 1234 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 1235 goto out; 1236 if (ch->ch_tx_stopped) 1237 goto out; 1238 1239 if (tp->t_outq.c_cc <= tp->t_lowat) { 1240 if (ISSET(tp->t_state, TS_ASLEEP)) { 1241 CLR(tp->t_state, TS_ASLEEP); 1242 wakeup(&tp->t_outq); 1243 } 1244 selwakeup(&tp->t_wsel); 1245 if (tp->t_outq.c_cc == 0) 1246 goto out; 1247 } 1248 1249 /* Grab the first contiguous region of buffer space. */ 1250 { 1251 u_char *tba; 1252 int tbc; 1253 1254 tba = tp->t_outq.c_cf; 1255 tbc = ndqb(&tp->t_outq, 0); 1256 1257 (void)splserial(); 1258 1259 ch->ch_tba = tba; 1260 ch->ch_tbc = tbc; 1261 } 1262 1263 SET(tp->t_state, TS_BUSY); 1264 ch->ch_tx_busy = 1; 1265 1266 /* Enable transmit completion interrupts if necessary. */ 1267 if (!ISSET(ch->ch_imr, 0x1)) { 1268 SET(ch->ch_imr, 0x1); 1269 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1270 } 1271 1272 /* Output the first chunk of the contiguous buffer. */ 1273 { 1274 u_char c; 1275 1276 while (ch->ch_tbc && READ_REG(ch->ch_base + 0x20) & 0x04) { 1277 c = *ch->ch_tba++; 1278 ch->ch_tbc--; 1279 WRITE_REG(ch->ch_base + 0x70, c); 1280 } 1281 } 1282 out: 1283 splx(s); 1284 return; 1285 } 1286 1287 /* 1288 * Stop output on a line. 1289 */ 1290 void 1291 sbscnstop(struct tty *tp, int flag) 1292 { 1293 struct sbscn_softc *sc = sbscn_cd.cd_devs[SBSCN_UNIT(tp->t_dev)]; 1294 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)]; 1295 int s; 1296 1297 s = splserial(); 1298 if (ISSET(tp->t_state, TS_BUSY)) { 1299 /* Stop transmitting at the next chunk. */ 1300 ch->ch_tbc = 0; 1301 ch->ch_heldtbc = 0; 1302 if (!ISSET(tp->t_state, TS_TTSTOP)) 1303 SET(tp->t_state, TS_FLUSH); 1304 } 1305 splx(s); 1306 } 1307 1308 void 1309 sbscn_diag(arg) 1310 void *arg; 1311 { 1312 struct sbscn_channel *ch = arg; 1313 struct sbscn_softc *sc = ch->ch_sc; 1314 int overflows, floods; 1315 int s; 1316 1317 s = splserial(); 1318 overflows = ch->ch_overflows; 1319 ch->ch_overflows = 0; 1320 floods = ch->ch_floods; 1321 ch->ch_floods = 0; 1322 ch->ch_errors = 0; 1323 splx(s); 1324 1325 log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n", 1326 sc->sc_dev.dv_xname, ch->ch_num, 1327 overflows, overflows == 1 ? "" : "s", 1328 floods, floods == 1 ? "" : "s"); 1329 } 1330 1331 integrate void 1332 sbscn_rxsoft(struct sbscn_channel *ch, struct tty *tp) 1333 { 1334 int (*rint)(int c, struct tty *tp) = tp->t_linesw->l_rint; 1335 u_char *get, *end; 1336 u_int cc, scc; 1337 u_char sr; 1338 int code; 1339 int s; 1340 1341 end = ch->ch_ebuf; 1342 get = ch->ch_rbget; 1343 scc = cc = sbscn_rbuf_size - ch->ch_rbavail; 1344 1345 if (cc == sbscn_rbuf_size) { 1346 ch->ch_floods++; 1347 if (ch->ch_errors++ == 0) 1348 callout_reset(&ch->ch_diag_callout, 60 * hz, 1349 sbscn_diag, ch); 1350 } 1351 1352 while (cc) { 1353 code = get[0]; 1354 sr = get[1]; 1355 if (ISSET(sr, 0xf0)) { 1356 if (ISSET(sr, 0x10)) { 1357 ch->ch_overflows++; 1358 if (ch->ch_errors++ == 0) 1359 callout_reset(&ch->ch_diag_callout, 1360 60 * hz, sbscn_diag, ch); 1361 } 1362 if (ISSET(sr, 0xc0)) 1363 SET(code, TTY_FE); 1364 if (ISSET(sr, 0x20)) 1365 SET(code, TTY_PE); 1366 } 1367 if ((*rint)(code, tp) == -1) { 1368 /* 1369 * The line discipline's buffer is out of space. 1370 */ 1371 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) { 1372 /* 1373 * We're either not using flow control, or the 1374 * line discipline didn't tell us to block for 1375 * some reason. Either way, we have no way to 1376 * know when there's more space available, so 1377 * just drop the rest of the data. 1378 */ 1379 get += cc << 1; 1380 if (get >= end) 1381 get -= sbscn_rbuf_size << 1; 1382 cc = 0; 1383 } else { 1384 /* 1385 * Don't schedule any more receive processing 1386 * until the line discipline tells us there's 1387 * space available (through comhwiflow()). 1388 * Leave the rest of the data in the input 1389 * buffer. 1390 */ 1391 SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED); 1392 } 1393 break; 1394 } 1395 get += 2; 1396 if (get >= end) 1397 get = ch->ch_rbuf; 1398 cc--; 1399 } 1400 1401 if (cc != scc) { 1402 ch->ch_rbget = get; 1403 s = splserial(); 1404 cc = ch->ch_rbavail += scc - cc; 1405 /* Buffers should be ok again, release possible block. */ 1406 if (cc >= ch->ch_r_lowat) { 1407 if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) { 1408 CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED); 1409 SET(ch->ch_imr, 0x02); 1410 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1411 } 1412 if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) { 1413 CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED); 1414 sbscn_dohwiflow(ch); 1415 } 1416 } 1417 splx(s); 1418 } 1419 } 1420 1421 integrate void 1422 sbscn_txsoft(struct sbscn_channel *ch, struct tty *tp) 1423 { 1424 1425 CLR(tp->t_state, TS_BUSY); 1426 if (ISSET(tp->t_state, TS_FLUSH)) 1427 CLR(tp->t_state, TS_FLUSH); 1428 else 1429 ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf)); 1430 (*tp->t_linesw->l_start)(tp); 1431 } 1432 1433 integrate void 1434 sbscn_stsoft(struct sbscn_channel *ch, struct tty *tp) 1435 { 1436 u_char iports, delta; 1437 int s; 1438 1439 s = splserial(); 1440 iports = ch->ch_iports; 1441 delta = ch->ch_iports_delta; 1442 ch->ch_iports_delta = 0; 1443 splx(s); 1444 1445 if (ISSET(delta, ch->ch_i_dcd)) { 1446 /* 1447 * Inform the tty layer that carrier detect changed. 1448 */ 1449 (void) (*tp->t_linesw->l_modem)(tp, 1450 ISSET(iports, ch->ch_i_dcd)); 1451 } 1452 1453 if (ISSET(delta, ch->ch_i_cts)) { 1454 /* Block or unblock output according to flow control. */ 1455 if (ISSET(iports, ch->ch_i_cts)) { 1456 ch->ch_tx_stopped = 0; 1457 (*tp->t_linesw->l_start)(tp); 1458 } else { 1459 ch->ch_tx_stopped = 1; 1460 } 1461 } 1462 1463 #ifdef SBSCN_DEBUG 1464 if (sbscn_debug) 1465 sbscn_status(ch, "sbscn_stsoft"); 1466 #endif 1467 } 1468 1469 void 1470 sbscn_soft(void *arg) 1471 { 1472 struct sbscn_channel *ch = arg; 1473 struct tty *tp = ch->ch_tty; 1474 1475 if (ch->ch_rx_ready) { 1476 ch->ch_rx_ready = 0; 1477 sbscn_rxsoft(ch, tp); 1478 } 1479 1480 if (ch->ch_st_check) { 1481 ch->ch_st_check = 0; 1482 sbscn_stsoft(ch, tp); 1483 } 1484 1485 if (ch->ch_tx_done) { 1486 ch->ch_tx_done = 0; 1487 sbscn_txsoft(ch, tp); 1488 } 1489 } 1490 1491 void 1492 sbscn_intr(void *arg, uint32_t status, uint32_t pc) 1493 { 1494 struct sbscn_channel *ch = arg; 1495 u_char *put, *end; 1496 u_int cc; 1497 u_char isr, sr; 1498 1499 /* read ISR */ 1500 isr = READ_REG(ch->ch_isr_base) & ch->ch_imr; 1501 if (isr == 0) 1502 return; 1503 1504 end = ch->ch_ebuf; 1505 put = ch->ch_rbput; 1506 cc = ch->ch_rbavail; 1507 1508 do { 1509 u_char iports, delta; 1510 1511 if (isr & 0x02) { 1512 1513 sr = READ_REG(ch->ch_base + 0x20); 1514 /* XXX sr 0x01 bit must be set at this point */ 1515 1516 #if defined(DDB) || defined(KGDB) 1517 if ((sr & 0x80) == 0x80) { 1518 #ifdef DDB 1519 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) { 1520 (void)READ_REG(ch->ch_base + 0x60); 1521 console_debugger(); 1522 continue; 1523 } 1524 #endif 1525 #ifdef KGDB 1526 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) { 1527 (void)READ_REG(ch->ch_base + 0x60); 1528 kgdb_connect(1); 1529 continue; 1530 } 1531 #endif 1532 } 1533 #endif /* DDB || KGDB */ 1534 1535 if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) { 1536 while (cc > 0) { 1537 put[0] = READ_REG(ch->ch_base + 0x60); 1538 put[1] = sr; 1539 put += 2; 1540 if (put >= end) 1541 put = ch->ch_rbuf; 1542 cc--; 1543 1544 sr = READ_REG(ch->ch_base + 0x20); 1545 if (!ISSET(sr, 0x02)) 1546 break; 1547 } 1548 1549 /* 1550 * Current string of incoming characters ended 1551 * because no more data was available or we 1552 * ran out of space. Schedule a receive event 1553 * if any data was received. If we're out of 1554 * space, turn off receive interrupts. 1555 */ 1556 ch->ch_rbput = put; 1557 ch->ch_rbavail = cc; 1558 if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) 1559 ch->ch_rx_ready = 1; 1560 1561 /* 1562 * See if we are in danger of overflowing a 1563 * buffer. If so, use hardware flow control 1564 * to ease the pressure. 1565 */ 1566 if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) && 1567 cc < ch->ch_r_hiwat) { 1568 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED); 1569 sbscn_dohwiflow(ch); 1570 } 1571 1572 /* 1573 * If we're out of space, disable receive 1574 * interrupts until the queue has drained 1575 * a bit. 1576 */ 1577 if (!cc) { 1578 SET(ch->ch_rx_flags, 1579 RX_IBUF_OVERFLOWED); 1580 CLR(ch->ch_imr, 0x02); 1581 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1582 } 1583 } else { 1584 /* XXX panic? */ 1585 CLR(ch->ch_imr, 0x02); 1586 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1587 continue; 1588 } 1589 } 1590 1591 if (isr & 0x01) { 1592 CLR(ch->ch_imr, 0x01); 1593 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1594 } 1595 1596 #if 0 1597 XXX 1598 if (isr & 0x08) { 1599 clear input signal change! 1600 } 1601 #endif 1602 iports = GET_INPUT_SIGNALS(ch); 1603 delta = iports ^ ch->ch_iports; 1604 ch->ch_iports = iports; 1605 1606 /* 1607 * Process normal status changes 1608 */ 1609 if (ISSET(delta, ch->ch_i_mask)) { 1610 SET(ch->ch_iports_delta, delta); 1611 1612 /* 1613 * Stop output immediately if we lose the output 1614 * flow control signal or carrier detect. 1615 */ 1616 if (ISSET(~iports, ch->ch_i_mask)) { 1617 ch->ch_tbc = 0; 1618 ch->ch_heldtbc = 0; 1619 #ifdef SBSCN_DEBUG 1620 if (sbscn_debug) 1621 sbscn_status(ch, "sbscn_intr "); 1622 #endif 1623 } 1624 1625 ch->ch_st_check = 1; 1626 } 1627 } while ((isr = (READ_REG(ch->ch_isr_base) & ch->ch_imr)) != 0); 1628 1629 /* 1630 * Done handling any receive interrupts and status changes, and 1631 * clearing the tx-ready interrupt if it was set. See if data can 1632 * be transmitted as well. Schedule tx done event if no data left 1633 * and tty was marked busy. 1634 */ 1635 sr = READ_REG(ch->ch_base + 0x20); 1636 if (ISSET(sr, 0x4)) { 1637 1638 /* 1639 * If we've delayed a parameter change, do it now, and restart 1640 * output. 1641 */ 1642 if (ch->ch_heldchange) { 1643 sbscn_loadchannelregs(ch); 1644 ch->ch_heldchange = 0; 1645 ch->ch_tbc = ch->ch_heldtbc; 1646 ch->ch_heldtbc = 0; 1647 } 1648 1649 /* Output the next chunk of the contiguous buffer, if any. */ 1650 if (ch->ch_tbc > 0) { 1651 int wrote1; 1652 u_char c; 1653 1654 wrote1 = 0; 1655 while (ch->ch_tbc && 1656 READ_REG(ch->ch_base + 0x20) & 0x04) { 1657 wrote1 = 1; 1658 c = *ch->ch_tba++; 1659 ch->ch_tbc--; 1660 WRITE_REG(ch->ch_base + 0x70, c); 1661 } 1662 if (wrote1) { 1663 SET(ch->ch_imr, 0x01); 1664 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1665 } 1666 } else { 1667 /* 1668 * transmit completion interrupts already disabled, 1669 * mark the channel tx state as done. 1670 */ 1671 if (ch->ch_tx_busy) { 1672 ch->ch_tx_busy = 0; 1673 ch->ch_tx_done = 1; 1674 } 1675 } 1676 } 1677 1678 /* Wake up the poller. */ 1679 softintr_schedule(ch->ch_si); 1680 1681 #if NRND > 0 && defined(RND_SBSCN) 1682 rnd_add_uint32(&ch->ch_rnd_source, isr | sr); 1683 #endif 1684 } 1685 1686 /* 1687 * The following functions are polled getc and putc routines, shared 1688 * by the console and kgdb glue. 1689 */ 1690 1691 int 1692 sbscn_common_getc(u_long addr, int chan) 1693 { 1694 int s = splhigh(); 1695 u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100)); 1696 int c; 1697 1698 /* block until a character becomes available */ 1699 while ((READ_REG(base + 0x20) & 0x01) == 0) 1700 continue; 1701 1702 c = READ_REG(base + 0x60) & 0xff; 1703 splx(s); 1704 return (c); 1705 } 1706 1707 void 1708 sbscn_common_putc(u_long addr, int chan, int c) 1709 { 1710 int s = splhigh(); 1711 int timo; 1712 u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100)); 1713 1714 /* wait for any pending transmission to finish */ 1715 timo = 1500000; 1716 while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo) 1717 continue; 1718 1719 WRITE_REG(base + 0x70, c); 1720 1721 /* wait for this transmission to complete */ 1722 timo = 15000000; 1723 while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo) 1724 continue; 1725 1726 splx(s); 1727 } 1728 1729 /* 1730 * Initialize UART for use as console or KGDB line. 1731 */ 1732 int 1733 sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag) 1734 { 1735 #if 1 1736 u_long chanregbase = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100)); 1737 u_long imaskreg = MIPS_PHYS_TO_KSEG1(addr + 0x230 + (chan * 0x20)); 1738 u_char mode1, mode2; 1739 u_long brc; 1740 volatile int timo; 1741 1742 WRITE_REG(imaskreg, 0); /* disable channel intrs */ 1743 1744 /* XXX should we really do the following? how about only if enabled? */ 1745 /* wait for any pending transmission to finish */ 1746 timo = 1500000; 1747 while ((READ_REG(chanregbase + 0x20) & 0x08) == 0 && --timo) 1748 continue; 1749 1750 /* XXX: wait a little. THIS SHOULD NOT BE NECESSARY!!! (?) */ 1751 timo = 1500000; 1752 while (--timo) 1753 ; 1754 1755 WRITE_REG(chanregbase + 0x50, 2 << 4); /* reset receiver */ 1756 WRITE_REG(chanregbase + 0x50, 3 << 4); /* reset transmitter */ 1757 1758 /* set up the line for use */ 1759 (void)cflag2modes(cflag, &mode1, &mode2); 1760 (void)sbscn_speed(rate, &brc); 1761 WRITE_REG(chanregbase + 0x00, mode1); 1762 WRITE_REG(chanregbase + 0x10, mode2); 1763 WRITE_REG(chanregbase + 0x30, brc); 1764 1765 /* enable transmit and receive */ 1766 #define M_DUART_RX_EN 0x01 1767 #define M_DUART_TX_EN 0x04 1768 WRITE_REG(chanregbase + 0x50,M_DUART_RX_EN | M_DUART_TX_EN); 1769 #endif 1770 1771 /* XXX: wait a little. THIS SHOULD NOT BE NECESSARY!!! (?) */ 1772 timo = 1500000; 1773 while (--timo) 1774 ; 1775 1776 #if 0 /* XXXCGD */ 1777 bus_space_handle_t ioh; 1778 1779 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) 1780 return (ENOMEM); /* ??? */ 1781 1782 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS); 1783 bus_space_write_1(iot, ioh, com_efr, 0); 1784 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB); 1785 rate = comspeed(rate, frequency); 1786 bus_space_write_1(iot, ioh, com_dlbl, rate); 1787 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8); 1788 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag)); 1789 bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS); 1790 bus_space_write_1(iot, ioh, com_fifo, 1791 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1); 1792 bus_space_write_1(iot, ioh, com_ier, 0); 1793 1794 *iohp = ioh; 1795 return (0); 1796 #endif /* XXXCGD */ 1797 /* XXXCGD */ 1798 return (0); 1799 } 1800 1801 /* 1802 * Following are all routines needed for sbscn to act as console 1803 */ 1804 int 1805 sbscn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag) 1806 { 1807 int res; 1808 static struct consdev sbscn_cons = { 1809 NULL, NULL, sbscn_cngetc, sbscn_cnputc, sbscn_cnpollc, NULL, 1810 NULL, NULL, NODEV, CN_NORMAL 1811 }; 1812 1813 res = sbscn_init(addr, chan, rate, cflag); 1814 if (res) 1815 return (res); 1816 1817 cn_tab = &sbscn_cons; 1818 1819 sbscn_cons_present = 1; 1820 sbscn_cons_addr = addr; 1821 sbscn_cons_chan = chan; 1822 sbscn_cons_rate = rate; 1823 sbscn_cons_cflag = cflag; 1824 1825 return (0); 1826 } 1827 1828 int 1829 sbscn_cngetc(dev_t dev) 1830 { 1831 1832 return (sbscn_common_getc(sbscn_cons_addr, sbscn_cons_chan)); 1833 } 1834 1835 /* 1836 * Console kernel output character routine. 1837 */ 1838 void 1839 sbscn_cnputc(dev_t dev, int c) 1840 { 1841 1842 sbscn_common_putc(sbscn_cons_addr, sbscn_cons_chan, c); 1843 } 1844 1845 void 1846 sbscn_cnpollc(dev_t dev, int on) 1847 { 1848 1849 } 1850 1851 #ifdef KGDB 1852 int 1853 sbscn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag) 1854 { 1855 int res; 1856 1857 if (!sbscn_cons_present && 1858 sbscn_cons_addr == addr && sbscn_cons_chan == chan) 1859 return (EBUSY); /* cannot share with console */ 1860 1861 res = sbscn_init(addr, chan, rate, cflag); 1862 if (res) 1863 return (res); 1864 1865 kgdb_attach(sbscn_kgdb_getc, sbscn_kgdb_putc, NULL); 1866 kgdb_dev = 123; /* unneeded, only to satisfy some tests */ 1867 1868 sbscn_kgdb_present = 1; 1869 sbscn_kgdb_addr = addr; 1870 sbscn_kgdb_chan = chan; 1871 1872 return (0); 1873 } 1874 1875 /* ARGSUSED */ 1876 int 1877 sbscn_kgdb_getc(arg) 1878 void *arg; 1879 { 1880 1881 return (sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan)); 1882 } 1883 1884 /* ARGSUSED */ 1885 void 1886 sbscn_kgdb_putc(arg, c) 1887 void *arg; 1888 int c; 1889 { 1890 1891 sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan, c); 1892 } 1893 #endif /* KGDB */ 1894 1895 /* 1896 * helper function to identify the sbscn channels used by 1897 * console or KGDB (and not yet autoconf attached) 1898 */ 1899 int 1900 sbscn_is_console(u_long addr, int chan) 1901 { 1902 1903 if (sbscn_cons_present && !sbscn_cons_attached && 1904 sbscn_cons_addr == addr && sbscn_cons_chan == chan) 1905 return (1); 1906 #ifdef KGDB 1907 if (sbscn_kgdb_present && !sbscn_kgdb_attached && 1908 sbscn_kgdb_addr == addr && sbscn_kgdb_chan == chan) 1909 return (1); 1910 #endif 1911 return (0); 1912 } 1913