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