1 /* $NetBSD: sbscn.c,v 1.43 2017/07/24 09:56:46 mrg 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.43 2017/07/24 09:56:46 mrg 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/malloc.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 = malloc(sbscn_rbuf_size << 1, M_DEVBUF, M_NOWAIT); 356 if (ch->ch_rbuf == NULL) { 357 aprint_error_dev(sc->sc_dev, 358 "channel %d: unable to allocate ring buffer\n", 359 chan); 360 return; 361 } 362 ch->ch_ebuf = ch->ch_rbuf + (sbscn_rbuf_size << 1); 363 364 tty_attach(tp); 365 366 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) { 367 int maj; 368 369 /* locate the major number */ 370 maj = cdevsw_lookup_major(&sbscn_cdevsw); 371 372 cn_tab->cn_dev = makedev(maj, 373 (device_unit(sc->sc_dev) << 1) + chan); 374 375 aprint_normal_dev(sc->sc_dev, "channel %d: %s\n", 376 chan, "console"); 377 } 378 379 #ifdef KGDB 380 /* 381 * Allow kgdb to "take over" this port. If this is 382 * the kgdb device, it has exclusive use. 383 */ 384 if (sbscn_kgdb_present && 385 sbscn_kgdb_addr == chan_addr && sbscn_kgdb_chan == chan) { 386 sbscn_kgdb_attached = 1; 387 388 SET(sc->sc_hwflags, SBSCN_HW_KGDB); 389 aprint_normal_dev(sc->sc_dev, "channel %d: %s\n", 390 chan, "kgdb"); 391 } 392 #endif 393 394 ch->ch_si = softint_establish(SOFTINT_SERIAL, sbscn_soft, ch); 395 396 #ifdef RND_SBSCN 397 rnd_attach_source(&ch->ch_rnd_source, device_xname(sc->sc_dev), 398 RND_TYPE_TTY, RND_FLAG_COLLECT_TIME| 399 RND_FLAG_ESTIMATE_TIME); 400 #endif 401 402 sbscn_config(ch); 403 404 SET(ch->ch_hwflags, SBSCN_HW_DEV_OK); 405 } 406 407 int 408 sbscn_speed(long speed, long *brcp) 409 { 410 #define divrnd(n, q) (((n)*2/(q)+1)/2) /* divide and round off */ 411 412 int x, err; 413 int frequency = 100000000; 414 415 *brcp = divrnd(frequency / 20, speed) - 1; 416 417 if (speed <= 0) 418 return (-1); 419 x = divrnd(frequency / 20, speed); 420 if (x <= 0) 421 return (-1); 422 err = divrnd(((quad_t)frequency) * 1000 / 20, speed * x) - 1000; 423 if (err < 0) 424 err = -err; 425 if (err > SBSCN_TOLERANCE) 426 return (-1); 427 *brcp = x - 1; 428 return (0); 429 430 #undef divrnd 431 } 432 433 #ifdef SBSCN_DEBUG 434 void sbscn_status(struct sbscn_channel *, const char *); 435 436 int sbscn_debug = 0 /* XXXCGD */; 437 438 void 439 sbscn_status(struct sbscn_channel *ch, const char *str) 440 { 441 struct sbscn_softc *sc = ch->ch_sc; 442 struct tty *tp = ch->ch_tty; 443 444 aprint_normal_dev(sc->sc_dev, 445 "chan %d: %s %sclocal %sdcd %sts_carr_on %sdtr %stx_stopped\n", 446 ch->ch_num, str, 447 ISSET(tp->t_cflag, CLOCAL) ? "+" : "-", 448 ISSET(ch->ch_iports, ch->ch_i_dcd) ? "+" : "-", 449 ISSET(tp->t_state, TS_CARR_ON) ? "+" : "-", 450 ISSET(ch->ch_oports, ch->ch_o_dtr) ? "+" : "-", 451 ch->ch_tx_stopped ? "+" : "-"); 452 453 aprint_normal_dev(sc->sc_dev, 454 "chan %d: %s %scrtscts %scts %sts_ttstop %srts %xrx_flags\n", 455 ch->ch_num, str, 456 ISSET(tp->t_cflag, CRTSCTS) ? "+" : "-", 457 ISSET(ch->ch_iports, ch->ch_i_cts) ? "+" : "-", 458 ISSET(tp->t_state, TS_TTSTOP) ? "+" : "-", 459 ISSET(ch->ch_oports, ch->ch_o_rts) ? "+" : "-", 460 ch->ch_rx_flags); 461 } 462 #endif 463 464 #if defined(DDB) || defined(KGDB) 465 static void 466 sbscn_enable_debugport(struct sbscn_channel *ch) 467 { 468 int s; 469 470 /* Turn on line break interrupt, set carrier. */ 471 s = splserial(); 472 473 #if 0 /* DO NOT turn on break interrupt at this time. */ 474 ch->ch_imr = 0x04; 475 #else 476 ch->ch_imr = 0x00; 477 #endif 478 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 479 SET(ch->ch_oports, ch->ch_o_dtr | ch->ch_o_rts); 480 SET_OUTPUT_SIGNALS(ch, ch->ch_oports); 481 482 splx(s); 483 } 484 #endif 485 486 void 487 sbscn_config(struct sbscn_channel *ch) 488 { 489 490 /* Disable interrupts before configuring the device. */ 491 ch->ch_imr = 0x00; 492 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 493 494 #ifdef DDB 495 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) 496 sbscn_enable_debugport(ch); 497 #endif 498 499 #ifdef KGDB 500 /* 501 * Allow kgdb to "take over" this port. If this is 502 * the kgdb device, it has exclusive use. 503 */ 504 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) 505 sbscn_enable_debugport(ch); 506 #endif 507 } 508 509 void 510 sbscn_shutdown(struct sbscn_channel *ch) 511 { 512 struct tty *tp = ch->ch_tty; 513 int s; 514 515 s = splserial(); 516 517 /* If we were asserting flow control, then deassert it. */ 518 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED); 519 sbscn_dohwiflow(ch); 520 521 /* Clear any break condition set with TIOCSBRK. */ 522 sbscn_break(ch, 0); 523 524 /* 525 * Hang up if necessary. Wait a bit, so the other side has time to 526 * notice even if we immediately open the port again. 527 * Avoid tsleeping above splhigh(). 528 */ 529 if (ISSET(tp->t_cflag, HUPCL)) { 530 sbscn_modem(ch, 0); 531 splx(s); 532 /* XXX tsleep will only timeout */ 533 (void) tsleep(ch, TTIPRI, ttclos, hz); 534 s = splserial(); 535 } 536 537 /* Turn off interrupts. */ 538 #ifdef DDB 539 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) 540 #if 0 /* DO NOT turn on break interrupt at this time. */ 541 ch->ch_imr = 0x04; /* interrupt on break */ 542 #else 543 ch->ch_imr = 0x00; 544 #endif 545 else 546 #endif 547 ch->ch_imr = 0; 548 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 549 550 splx(s); 551 } 552 553 int 554 sbscnopen(dev_t dev, int flag, int mode, struct lwp *l) 555 { 556 int chan = SBSCN_CHAN(dev); 557 struct sbscn_softc *sc; 558 struct sbscn_channel *ch; 559 struct tty *tp; 560 int s, s2; 561 int error; 562 563 sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev)); 564 if (sc == NULL) 565 return (ENXIO); 566 ch = &sc->sc_channels[chan]; 567 if (!ISSET(ch->ch_hwflags, SBSCN_HW_DEV_OK) || ch->ch_rbuf == NULL) 568 return (ENXIO); 569 570 #ifdef KGDB 571 /* 572 * If this is the kgdb port, no other use is permitted. 573 */ 574 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) 575 return (EBUSY); 576 #endif 577 578 tp = ch->ch_tty; 579 580 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 581 return (EBUSY); 582 583 s = spltty(); 584 585 /* 586 * Do the following iff this is a first open. 587 */ 588 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 589 struct termios t; 590 591 tp->t_dev = dev; 592 593 s2 = splserial(); 594 595 /* Turn on receive, break, and status change interrupts. */ 596 #if 0 /* DO NOT turn on break or status change interrupt at this time. */ 597 ch->ch_imr = 0xe; 598 #else 599 ch->ch_imr = 0x2; 600 #endif 601 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 602 603 /* Fetch the current modem control status, needed later. */ 604 ch->ch_iports = GET_INPUT_SIGNALS(ch); 605 ch->ch_iports_delta = 0; 606 splx(s2); 607 608 /* 609 * Initialize the termios status to the defaults. Add in the 610 * sticky bits from TIOCSFLAGS. 611 */ 612 t.c_ispeed = 0; 613 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) { 614 t.c_ospeed = sbscn_cons_rate; 615 t.c_cflag = sbscn_cons_cflag; 616 } else { 617 t.c_ospeed = TTYDEF_SPEED; 618 t.c_cflag = TTYDEF_CFLAG; 619 } 620 if (ISSET(ch->ch_swflags, TIOCFLAG_CLOCAL)) 621 SET(t.c_cflag, CLOCAL); 622 if (ISSET(ch->ch_swflags, TIOCFLAG_CRTSCTS)) 623 SET(t.c_cflag, CRTSCTS); 624 if (ISSET(ch->ch_swflags, TIOCFLAG_MDMBUF)) 625 SET(t.c_cflag, MDMBUF); 626 /* Make sure sbscn_param() will do something. */ 627 tp->t_ospeed = 0; 628 (void) sbscn_param(tp, &t); 629 tp->t_iflag = TTYDEF_IFLAG; 630 tp->t_oflag = TTYDEF_OFLAG; 631 tp->t_lflag = TTYDEF_LFLAG; 632 ttychars(tp); 633 ttsetwater(tp); 634 635 s2 = splserial(); 636 637 /* 638 * Turn on DTR. We must always do this, even if carrier is not 639 * present, because otherwise we'd have to use TIOCSDTR 640 * immediately after setting CLOCAL, which applications do not 641 * expect. We always assert DTR while the device is open 642 * unless explicitly requested to deassert it. 643 */ 644 sbscn_modem(ch, 1); 645 646 /* Clear the input ring, and unblock. */ 647 ch->ch_rbput = ch->ch_rbget = ch->ch_rbuf; 648 ch->ch_rbavail = sbscn_rbuf_size; 649 sbscn_iflush(ch); 650 CLR(ch->ch_rx_flags, RX_ANY_BLOCK); 651 sbscn_dohwiflow(ch); 652 653 #ifdef SBSCN_DEBUG 654 if (sbscn_debug) 655 sbscn_status(ch, "sbscnopen "); 656 #endif 657 658 splx(s2); 659 } 660 661 splx(s); 662 663 error = ttyopen(tp, SBSCN_DIALOUT(dev), ISSET(flag, O_NONBLOCK)); 664 if (error) 665 goto bad; 666 667 error = (*tp->t_linesw->l_open)(dev, tp); 668 if (error) 669 goto bad; 670 671 return (0); 672 673 bad: 674 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 675 /* 676 * We failed to open the device, and nobody else had it opened. 677 * Clean up the state as appropriate. 678 */ 679 sbscn_shutdown(ch); 680 } 681 682 return (error); 683 } 684 685 int 686 sbscnclose(dev_t dev, int flag, int mode, struct lwp *l) 687 { 688 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev)); 689 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 690 struct tty *tp = ch->ch_tty; 691 692 /* XXX This is for cons.c. */ 693 if (!ISSET(tp->t_state, TS_ISOPEN)) 694 return (0); 695 696 (*tp->t_linesw->l_close)(tp, flag); 697 ttyclose(tp); 698 699 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 700 /* 701 * Although we got a last close, the device may still be in 702 * use; e.g. if this was the dialout node, and there are still 703 * processes waiting for carrier on the non-dialout node. 704 */ 705 sbscn_shutdown(ch); 706 } 707 708 return (0); 709 } 710 711 int 712 sbscnread(dev_t dev, struct uio *uio, int flag) 713 { 714 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev)); 715 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 716 struct tty *tp = ch->ch_tty; 717 718 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 719 } 720 721 int 722 sbscnwrite(dev_t dev, struct uio *uio, int flag) 723 { 724 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev)); 725 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 726 struct tty *tp = ch->ch_tty; 727 728 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 729 } 730 731 int 732 sbscnpoll(dev_t dev, int events, struct lwp *l) 733 { 734 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev)); 735 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 736 struct tty *tp = ch->ch_tty; 737 738 return ((*tp->t_linesw->l_poll)(tp, events, l)); 739 } 740 741 struct tty * 742 sbscntty(dev_t dev) 743 { 744 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev)); 745 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 746 struct tty *tp = ch->ch_tty; 747 748 return (tp); 749 } 750 751 int 752 sbscnioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 753 { 754 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(dev)); 755 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(dev)]; 756 struct tty *tp = ch->ch_tty; 757 int error; 758 int s; 759 760 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 761 if (error != EPASSTHROUGH) 762 return (error); 763 764 error = ttioctl(tp, cmd, data, flag, l); 765 if (error != EPASSTHROUGH) 766 return (error); 767 768 error = 0; 769 770 s = splserial(); 771 772 switch (cmd) { 773 case TIOCSBRK: 774 sbscn_break(ch, 1); 775 break; 776 777 case TIOCCBRK: 778 sbscn_break(ch, 0); 779 break; 780 781 case TIOCSDTR: 782 sbscn_modem(ch, 1); 783 break; 784 785 case TIOCCDTR: 786 sbscn_modem(ch, 0); 787 break; 788 789 case TIOCGFLAGS: 790 *(int *)data = ch->ch_swflags; 791 break; 792 793 case TIOCSFLAGS: 794 error = kauth_authorize_device_tty(l->l_cred, 795 KAUTH_DEVICE_TTY_PRIVSET, tp); 796 if (error) 797 break; 798 ch->ch_swflags = *(int *)data; 799 break; 800 801 case TIOCMSET: 802 case TIOCMBIS: 803 case TIOCMBIC: 804 tiocm_to_sbscn(ch, cmd, *(int *)data); 805 break; 806 807 case TIOCMGET: 808 *(int *)data = sbscn_to_tiocm(ch); 809 break; 810 811 default: 812 error = EPASSTHROUGH; 813 break; 814 } 815 816 splx(s); 817 818 #ifdef SBSCN_DEBUG 819 if (sbscn_debug) 820 sbscn_status(ch, "sbscn_ioctl "); 821 #endif 822 823 return (error); 824 } 825 826 integrate void 827 sbscn_schedrx(struct sbscn_channel *ch) 828 { 829 830 ch->ch_rx_ready = 1; 831 832 /* Wake up the poller. */ 833 softint_schedule(ch->ch_si); 834 } 835 836 void 837 sbscn_break(struct sbscn_channel *ch, int onoff) 838 { 839 840 /* XXXCGD delay break until not busy */ 841 if (onoff) 842 WRITE_REG(ch->ch_base + 0x50, 0x60); 843 else 844 WRITE_REG(ch->ch_base + 0x50, 0x70); 845 846 #if 0 847 if (!ch->ch_heldchange) { 848 if (ch->ch_tx_busy) { 849 ch->ch_heldtbc = ch->ch_tbc; 850 ch->ch_tbc = 0; 851 ch->ch_heldchange = 1; 852 } else 853 sbscn_loadchannelregs(ch); 854 } 855 #endif 856 } 857 858 void 859 sbscn_modem(struct sbscn_channel *ch, int onoff) 860 { 861 862 if (ch->ch_o_dtr == 0) 863 return; 864 865 if (onoff) 866 SET(ch->ch_oports, ch->ch_o_dtr); 867 else 868 CLR(ch->ch_oports, ch->ch_o_dtr); 869 870 if (!ch->ch_heldchange) { 871 if (ch->ch_tx_busy) { 872 ch->ch_heldtbc = ch->ch_tbc; 873 ch->ch_tbc = 0; 874 ch->ch_heldchange = 1; 875 } else 876 sbscn_loadchannelregs(ch); 877 } 878 } 879 880 void 881 tiocm_to_sbscn(struct sbscn_channel *ch, int how, int ttybits) 882 { 883 u_char bits; 884 885 bits = 0; 886 if (ISSET(ttybits, TIOCM_DTR)) 887 SET(bits, ch->ch_o_dtr); 888 if (ISSET(ttybits, TIOCM_RTS)) 889 SET(bits, ch->ch_o_rts); 890 891 switch (how) { 892 case TIOCMBIC: 893 CLR(ch->ch_oports, bits); 894 break; 895 896 case TIOCMBIS: 897 SET(ch->ch_oports, bits); 898 break; 899 900 case TIOCMSET: 901 ch->ch_oports = bits; 902 break; 903 } 904 905 if (!ch->ch_heldchange) { 906 if (ch->ch_tx_busy) { 907 ch->ch_heldtbc = ch->ch_tbc; 908 ch->ch_tbc = 0; 909 ch->ch_heldchange = 1; 910 } else 911 sbscn_loadchannelregs(ch); 912 } 913 } 914 915 int 916 sbscn_to_tiocm(struct sbscn_channel *ch) 917 { 918 u_char hwbits; 919 int ttybits = 0; 920 921 hwbits = ch->ch_oports; 922 if (ISSET(hwbits, ch->ch_o_dtr)) 923 SET(ttybits, TIOCM_DTR); 924 if (ISSET(hwbits, ch->ch_o_rts)) 925 SET(ttybits, TIOCM_RTS); 926 927 hwbits = ch->ch_iports; 928 if (ISSET(hwbits, ch->ch_i_dcd)) 929 SET(ttybits, TIOCM_CD); 930 if (ISSET(hwbits, ch->ch_i_cts)) 931 SET(ttybits, TIOCM_CTS); 932 if (ISSET(hwbits, ch->ch_i_dsr)) 933 SET(ttybits, TIOCM_DSR); 934 if (ISSET(hwbits, ch->ch_i_ri)) 935 SET(ttybits, TIOCM_RI); 936 937 if (ch->ch_imr != 0) 938 SET(ttybits, TIOCM_LE); 939 940 return (ttybits); 941 } 942 943 static int 944 cflag2modes(tcflag_t cflag, u_char *mode1p, u_char *mode2p) 945 { 946 u_char mode1; 947 u_char mode2; 948 int err = 0; 949 950 mode1 = mode2 = 0; 951 952 switch (ISSET(cflag, CSIZE)) { 953 case CS7: 954 mode1 |= 2; /* XXX */ 955 break; 956 default: 957 err = -1; 958 /* FALLTHRU for sanity */ 959 case CS8: 960 mode1 |= 3; /* XXX */ 961 break; 962 } 963 if (!ISSET(cflag, PARENB)) 964 mode1 |= 2 << 3; 965 else { 966 mode1 |= 0 << 3; 967 if (ISSET(cflag, PARODD)) 968 mode1 |= 1 << 2; 969 } 970 971 if (ISSET(cflag, CSTOPB)) 972 mode2 |= 1 << 3; /* two stop bits XXX not std */ 973 974 if (ISSET(cflag, CRTSCTS)) { 975 mode1 |= 1 << 7; 976 mode2 |= 1 << 4; 977 } 978 979 *mode1p = mode1; 980 *mode2p = mode2; 981 return (err); 982 } 983 984 int 985 sbscn_param(struct tty *tp, struct termios *t) 986 { 987 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev)); 988 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)]; 989 long brc; 990 u_char mode1, mode2; 991 int s; 992 993 /* XXX reset to console parameters if console? */ 994 #if 0 995 XXX disable, enable. 996 #endif 997 998 /* Check requested parameters. */ 999 if (sbscn_speed(t->c_ospeed, &brc) < 0) 1000 return (EINVAL); 1001 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 1002 return (EINVAL); 1003 1004 /* 1005 * For the console, always force CLOCAL and !HUPCL, so that the port 1006 * is always active. 1007 */ 1008 if (ISSET(ch->ch_swflags, TIOCFLAG_SOFTCAR) || 1009 ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) { 1010 SET(t->c_cflag, CLOCAL); 1011 CLR(t->c_cflag, HUPCL); 1012 } 1013 1014 /* 1015 * If there were no changes, don't do anything. This avoids dropping 1016 * input and improves performance when all we did was frob things like 1017 * VMIN and VTIME. 1018 */ 1019 if (tp->t_ospeed == t->c_ospeed && 1020 tp->t_cflag == t->c_cflag) 1021 return (0); 1022 1023 if (cflag2modes(t->c_cflag, &mode1, &mode2) < 0) 1024 return (EINVAL); 1025 1026 s = splserial(); 1027 1028 ch->ch_mode1 = mode1; 1029 ch->ch_mode2 = mode2; 1030 1031 /* 1032 * If we're not in a mode that assumes a connection is present, then 1033 * ignore carrier changes. 1034 */ 1035 if (ISSET(t->c_cflag, CLOCAL | MDMBUF)) 1036 ch->ch_i_dcd = 0; 1037 else 1038 ch->ch_i_dcd = ch->ch_i_dcd_pin; 1039 /* 1040 * Set the flow control pins depending on the current flow control 1041 * mode. 1042 */ 1043 if (ISSET(t->c_cflag, CRTSCTS)) { 1044 ch->ch_o_dtr = ch->ch_o_dtr_pin; 1045 ch->ch_o_rts = ch->ch_o_rts_pin; 1046 ch->ch_i_cts = ch->ch_i_cts_pin; 1047 /* hw controle enable bits in mod regs set by cflag2modes */ 1048 } else if (ISSET(t->c_cflag, MDMBUF)) { 1049 /* 1050 * For DTR/DCD flow control, make sure we don't toggle DTR for 1051 * carrier detection. 1052 */ 1053 ch->ch_o_dtr = 0; 1054 ch->ch_o_rts = ch->ch_o_dtr_pin; 1055 ch->ch_i_cts = ch->ch_i_dcd_pin; 1056 } else { 1057 /* 1058 * If no flow control, then always set RTS. This will make 1059 * the other side happy if it mistakenly thinks we're doing 1060 * RTS/CTS flow control. 1061 */ 1062 ch->ch_o_dtr = ch->ch_o_dtr_pin | ch->ch_o_rts_pin; 1063 ch->ch_o_rts = 0; 1064 ch->ch_i_cts = 0; 1065 if (ISSET(ch->ch_oports, ch->ch_o_dtr_pin)) 1066 SET(ch->ch_oports, ch->ch_o_rts_pin); 1067 else 1068 CLR(ch->ch_oports, ch->ch_o_rts_pin); 1069 } 1070 /* XXX maybe mask the ports which generate intrs? */ 1071 1072 ch->ch_brc = brc; 1073 1074 /* XXX maybe set fifo-full receive mode if RTSCTS and high speed? */ 1075 1076 /* And copy to tty. */ 1077 tp->t_ispeed = 0; 1078 tp->t_ospeed = t->c_ospeed; 1079 tp->t_cflag = t->c_cflag; 1080 1081 if (!ch->ch_heldchange) { 1082 if (ch->ch_tx_busy) { 1083 ch->ch_heldtbc = ch->ch_tbc; 1084 ch->ch_tbc = 0; 1085 ch->ch_heldchange = 1; 1086 } else 1087 sbscn_loadchannelregs(ch); 1088 } 1089 1090 if (!ISSET(t->c_cflag, CHWFLOW)) { 1091 /* Disable the high water mark. */ 1092 ch->ch_r_hiwat = 0; 1093 ch->ch_r_lowat = 0; 1094 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) { 1095 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED); 1096 sbscn_schedrx(ch); 1097 } 1098 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED)) { 1099 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED|RX_IBUF_BLOCKED); 1100 sbscn_dohwiflow(ch); 1101 } 1102 } else { 1103 ch->ch_r_hiwat = sbscn_rbuf_hiwat; 1104 ch->ch_r_lowat = sbscn_rbuf_lowat; 1105 } 1106 1107 splx(s); 1108 1109 /* 1110 * Update the tty layer's idea of the carrier bit, in case we changed 1111 * CLOCAL or MDMBUF. We don't hang up here; we only do that by 1112 * explicit request. 1113 */ 1114 (void) (*tp->t_linesw->l_modem)(tp, 1115 ISSET(ch->ch_iports, ch->ch_i_dcd)); 1116 1117 #ifdef SBSCN_DEBUG 1118 if (sbscn_debug) 1119 sbscn_status(ch, "sbscnparam "); 1120 #endif 1121 1122 if (!ISSET(t->c_cflag, CHWFLOW)) { 1123 if (ch->ch_tx_stopped) { 1124 ch->ch_tx_stopped = 0; 1125 sbscn_start(tp); 1126 } 1127 } 1128 1129 return (0); 1130 } 1131 1132 void 1133 sbscn_iflush(struct sbscn_channel *ch) 1134 { 1135 #ifdef DIAGNOSTIC 1136 int reg; 1137 #endif 1138 int timo; 1139 1140 #ifdef DIAGNOSTIC 1141 reg = 0xffff; 1142 #endif 1143 timo = 50000; 1144 /* flush any pending I/O */ 1145 while (ISSET(READ_REG(ch->ch_base + 0x20), 0x01) 1146 && --timo) 1147 #ifdef DIAGNOSTIC 1148 reg = 1149 #else 1150 (void) 1151 #endif 1152 READ_REG(ch->ch_base + 0x60); 1153 #ifdef DIAGNOSTIC 1154 if (!timo) 1155 aprint_debug_dev(ch->ch_sc->sc_dev, 1156 "sbscn_iflush timeout %02x\n", reg & 0xff); 1157 #endif 1158 } 1159 1160 void 1161 sbscn_loadchannelregs(struct sbscn_channel *ch) 1162 { 1163 1164 /* XXXXX necessary? */ 1165 sbscn_iflush(ch); 1166 1167 WRITE_REG(ch->ch_imr_base, 0); 1168 1169 // XXX disable? 1170 WRITE_REG(ch->ch_base + 0x00, ch->ch_mode1); 1171 WRITE_REG(ch->ch_base + 0x10, ch->ch_mode2); 1172 WRITE_REG(ch->ch_base + 0x30, ch->ch_brc); 1173 1174 ch->ch_oports_active = ch->ch_oports; 1175 SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active); 1176 1177 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1178 } 1179 1180 int 1181 sbscn_hwiflow(struct tty *tp, int block) 1182 { 1183 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev)); 1184 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)]; 1185 int s; 1186 1187 if (ch->ch_o_rts == 0) 1188 return (0); 1189 1190 s = splserial(); 1191 if (block) { 1192 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) { 1193 SET(ch->ch_rx_flags, RX_TTY_BLOCKED); 1194 sbscn_dohwiflow(ch); 1195 } 1196 } else { 1197 if (ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) { 1198 CLR(ch->ch_rx_flags, RX_TTY_OVERFLOWED); 1199 sbscn_schedrx(ch); 1200 } 1201 if (ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) { 1202 CLR(ch->ch_rx_flags, RX_TTY_BLOCKED); 1203 sbscn_dohwiflow(ch); 1204 } 1205 } 1206 splx(s); 1207 return (1); 1208 } 1209 1210 /* 1211 * (un)block input via hw flowcontrol 1212 */ 1213 void 1214 sbscn_dohwiflow(struct sbscn_channel *ch) 1215 { 1216 1217 if (ch->ch_o_rts == 0) 1218 return; 1219 1220 if (ISSET(ch->ch_rx_flags, RX_ANY_BLOCK)) { 1221 CLR(ch->ch_oports, ch->ch_o_rts); 1222 CLR(ch->ch_oports_active, ch->ch_o_rts); 1223 } else { 1224 SET(ch->ch_oports, ch->ch_o_rts); 1225 SET(ch->ch_oports_active, ch->ch_o_rts); 1226 } 1227 SET_OUTPUT_SIGNALS(ch, ch->ch_oports_active); 1228 } 1229 1230 void 1231 sbscn_start(struct tty *tp) 1232 { 1233 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev)); 1234 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)]; 1235 int s; 1236 1237 s = spltty(); 1238 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 1239 goto out; 1240 if (ch->ch_tx_stopped) 1241 goto out; 1242 if (!ttypull(tp)) 1243 goto out; 1244 1245 /* Grab the first contiguous region of buffer space. */ 1246 { 1247 u_char *tba; 1248 int tbc; 1249 1250 tba = tp->t_outq.c_cf; 1251 tbc = ndqb(&tp->t_outq, 0); 1252 1253 (void)splserial(); 1254 1255 ch->ch_tba = tba; 1256 ch->ch_tbc = tbc; 1257 } 1258 1259 SET(tp->t_state, TS_BUSY); 1260 ch->ch_tx_busy = 1; 1261 1262 /* Enable transmit completion interrupts if necessary. */ 1263 if (!ISSET(ch->ch_imr, 0x1)) { 1264 SET(ch->ch_imr, 0x1); 1265 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1266 } 1267 1268 /* Output the first chunk of the contiguous buffer. */ 1269 { 1270 u_char c; 1271 1272 while (ch->ch_tbc && READ_REG(ch->ch_base + 0x20) & 0x04) { 1273 c = *ch->ch_tba++; 1274 ch->ch_tbc--; 1275 WRITE_REG(ch->ch_base + 0x70, c); 1276 } 1277 } 1278 out: 1279 splx(s); 1280 return; 1281 } 1282 1283 /* 1284 * Stop output on a line. 1285 */ 1286 void 1287 sbscnstop(struct tty *tp, int flag) 1288 { 1289 struct sbscn_softc *sc = device_lookup_private(&sbscn_cd, SBSCN_UNIT(tp->t_dev)); 1290 struct sbscn_channel *ch = &sc->sc_channels[SBSCN_CHAN(tp->t_dev)]; 1291 int s; 1292 1293 s = splserial(); 1294 if (ISSET(tp->t_state, TS_BUSY)) { 1295 /* Stop transmitting at the next chunk. */ 1296 ch->ch_tbc = 0; 1297 ch->ch_heldtbc = 0; 1298 if (!ISSET(tp->t_state, TS_TTSTOP)) 1299 SET(tp->t_state, TS_FLUSH); 1300 } 1301 splx(s); 1302 } 1303 1304 void 1305 sbscn_diag(void *arg) 1306 { 1307 struct sbscn_channel *ch = arg; 1308 struct sbscn_softc *sc = ch->ch_sc; 1309 int overflows, floods; 1310 int s; 1311 1312 s = splserial(); 1313 overflows = ch->ch_overflows; 1314 ch->ch_overflows = 0; 1315 floods = ch->ch_floods; 1316 ch->ch_floods = 0; 1317 ch->ch_errors = 0; 1318 splx(s); 1319 1320 log(LOG_WARNING, "%s: channel %d: %d fifo overflow%s, %d ibuf flood%s\n", 1321 device_xname(sc->sc_dev), ch->ch_num, 1322 overflows, overflows == 1 ? "" : "s", 1323 floods, floods == 1 ? "" : "s"); 1324 } 1325 1326 integrate void 1327 sbscn_rxsoft(struct sbscn_channel *ch, struct tty *tp) 1328 { 1329 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint; 1330 u_char *get, *end; 1331 u_int cc, scc; 1332 u_char sr; 1333 int code; 1334 int s; 1335 1336 end = ch->ch_ebuf; 1337 get = ch->ch_rbget; 1338 scc = cc = sbscn_rbuf_size - ch->ch_rbavail; 1339 1340 if (cc == sbscn_rbuf_size) { 1341 ch->ch_floods++; 1342 if (ch->ch_errors++ == 0) 1343 callout_reset(&ch->ch_diag_callout, 60 * hz, 1344 sbscn_diag, ch); 1345 } 1346 1347 while (cc) { 1348 code = get[0]; 1349 sr = get[1]; 1350 if (ISSET(sr, 0xf0)) { 1351 if (ISSET(sr, 0x10)) { 1352 ch->ch_overflows++; 1353 if (ch->ch_errors++ == 0) 1354 callout_reset(&ch->ch_diag_callout, 1355 60 * hz, sbscn_diag, ch); 1356 } 1357 if (ISSET(sr, 0xc0)) 1358 SET(code, TTY_FE); 1359 if (ISSET(sr, 0x20)) 1360 SET(code, TTY_PE); 1361 } 1362 if ((*rint)(code, tp) == -1) { 1363 /* 1364 * The line discipline's buffer is out of space. 1365 */ 1366 if (!ISSET(ch->ch_rx_flags, RX_TTY_BLOCKED)) { 1367 /* 1368 * We're either not using flow control, or the 1369 * line discipline didn't tell us to block for 1370 * some reason. Either way, we have no way to 1371 * know when there's more space available, so 1372 * just drop the rest of the data. 1373 */ 1374 get += cc << 1; 1375 if (get >= end) 1376 get -= sbscn_rbuf_size << 1; 1377 cc = 0; 1378 } else { 1379 /* 1380 * Don't schedule any more receive processing 1381 * until the line discipline tells us there's 1382 * space available (through comhwiflow()). 1383 * Leave the rest of the data in the input 1384 * buffer. 1385 */ 1386 SET(ch->ch_rx_flags, RX_TTY_OVERFLOWED); 1387 } 1388 break; 1389 } 1390 get += 2; 1391 if (get >= end) 1392 get = ch->ch_rbuf; 1393 cc--; 1394 } 1395 1396 if (cc != scc) { 1397 ch->ch_rbget = get; 1398 s = splserial(); 1399 cc = ch->ch_rbavail += scc - cc; 1400 /* Buffers should be ok again, release possible block. */ 1401 if (cc >= ch->ch_r_lowat) { 1402 if (ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) { 1403 CLR(ch->ch_rx_flags, RX_IBUF_OVERFLOWED); 1404 SET(ch->ch_imr, 0x02); 1405 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1406 } 1407 if (ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED)) { 1408 CLR(ch->ch_rx_flags, RX_IBUF_BLOCKED); 1409 sbscn_dohwiflow(ch); 1410 } 1411 } 1412 splx(s); 1413 } 1414 } 1415 1416 integrate void 1417 sbscn_txsoft(struct sbscn_channel *ch, struct tty *tp) 1418 { 1419 1420 CLR(tp->t_state, TS_BUSY); 1421 if (ISSET(tp->t_state, TS_FLUSH)) 1422 CLR(tp->t_state, TS_FLUSH); 1423 else 1424 ndflush(&tp->t_outq, (int)(ch->ch_tba - tp->t_outq.c_cf)); 1425 (*tp->t_linesw->l_start)(tp); 1426 } 1427 1428 integrate void 1429 sbscn_stsoft(struct sbscn_channel *ch, struct tty *tp) 1430 { 1431 u_char iports, delta; 1432 int s; 1433 1434 s = splserial(); 1435 iports = ch->ch_iports; 1436 delta = ch->ch_iports_delta; 1437 ch->ch_iports_delta = 0; 1438 splx(s); 1439 1440 if (ISSET(delta, ch->ch_i_dcd)) { 1441 /* 1442 * Inform the tty layer that carrier detect changed. 1443 */ 1444 (void) (*tp->t_linesw->l_modem)(tp, 1445 ISSET(iports, ch->ch_i_dcd)); 1446 } 1447 1448 if (ISSET(delta, ch->ch_i_cts)) { 1449 /* Block or unblock output according to flow control. */ 1450 if (ISSET(iports, ch->ch_i_cts)) { 1451 ch->ch_tx_stopped = 0; 1452 (*tp->t_linesw->l_start)(tp); 1453 } else { 1454 ch->ch_tx_stopped = 1; 1455 } 1456 } 1457 1458 #ifdef SBSCN_DEBUG 1459 if (sbscn_debug) 1460 sbscn_status(ch, "sbscn_stsoft"); 1461 #endif 1462 } 1463 1464 void 1465 sbscn_soft(void *arg) 1466 { 1467 struct sbscn_channel *ch = arg; 1468 struct tty *tp = ch->ch_tty; 1469 1470 if (ch->ch_rx_ready) { 1471 ch->ch_rx_ready = 0; 1472 sbscn_rxsoft(ch, tp); 1473 } 1474 1475 if (ch->ch_st_check) { 1476 ch->ch_st_check = 0; 1477 sbscn_stsoft(ch, tp); 1478 } 1479 1480 if (ch->ch_tx_done) { 1481 ch->ch_tx_done = 0; 1482 sbscn_txsoft(ch, tp); 1483 } 1484 } 1485 1486 void 1487 sbscn_intr(void *arg, uint32_t status, vaddr_t pc) 1488 { 1489 struct sbscn_channel *ch = arg; 1490 u_char *put, *end; 1491 u_int cc; 1492 u_char isr, sr; 1493 1494 /* read ISR */ 1495 isr = READ_REG(ch->ch_isr_base) & ch->ch_imr; 1496 if (isr == 0) 1497 return; 1498 1499 end = ch->ch_ebuf; 1500 put = ch->ch_rbput; 1501 cc = ch->ch_rbavail; 1502 1503 do { 1504 u_char iports, delta; 1505 1506 if (isr & 0x02) { 1507 1508 sr = READ_REG(ch->ch_base + 0x20); 1509 /* XXX sr 0x01 bit must be set at this point */ 1510 1511 #if defined(DDB) || defined(KGDB) 1512 if ((sr & 0x80) == 0x80) { 1513 #ifdef DDB 1514 if (ISSET(ch->ch_hwflags, SBSCN_HW_CONSOLE)) { 1515 (void)READ_REG(ch->ch_base + 0x60); 1516 console_debugger(); 1517 continue; 1518 } 1519 #endif 1520 #ifdef KGDB 1521 if (ISSET(ch->ch_hwflags, SBSCN_HW_KGDB)) { 1522 (void)READ_REG(ch->ch_base + 0x60); 1523 kgdb_connect(1); 1524 continue; 1525 } 1526 #endif 1527 } 1528 #endif /* DDB || KGDB */ 1529 1530 if (!ISSET(ch->ch_rx_flags, RX_IBUF_OVERFLOWED)) { 1531 while (cc > 0) { 1532 put[0] = READ_REG(ch->ch_base + 0x60); 1533 put[1] = sr; 1534 put += 2; 1535 if (put >= end) 1536 put = ch->ch_rbuf; 1537 cc--; 1538 1539 sr = READ_REG(ch->ch_base + 0x20); 1540 if (!ISSET(sr, 0x02)) 1541 break; 1542 } 1543 1544 /* 1545 * Current string of incoming characters ended 1546 * because no more data was available or we 1547 * ran out of space. Schedule a receive event 1548 * if any data was received. If we're out of 1549 * space, turn off receive interrupts. 1550 */ 1551 ch->ch_rbput = put; 1552 ch->ch_rbavail = cc; 1553 if (!ISSET(ch->ch_rx_flags, RX_TTY_OVERFLOWED)) 1554 ch->ch_rx_ready = 1; 1555 1556 /* 1557 * See if we are in danger of overflowing a 1558 * buffer. If so, use hardware flow control 1559 * to ease the pressure. 1560 */ 1561 if (!ISSET(ch->ch_rx_flags, RX_IBUF_BLOCKED) && 1562 cc < ch->ch_r_hiwat) { 1563 SET(ch->ch_rx_flags, RX_IBUF_BLOCKED); 1564 sbscn_dohwiflow(ch); 1565 } 1566 1567 /* 1568 * If we're out of space, disable receive 1569 * interrupts until the queue has drained 1570 * a bit. 1571 */ 1572 if (!cc) { 1573 SET(ch->ch_rx_flags, 1574 RX_IBUF_OVERFLOWED); 1575 CLR(ch->ch_imr, 0x02); 1576 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1577 } 1578 } else { 1579 /* XXX panic? */ 1580 CLR(ch->ch_imr, 0x02); 1581 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1582 continue; 1583 } 1584 } 1585 1586 if (isr & 0x01) { 1587 CLR(ch->ch_imr, 0x01); 1588 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1589 } 1590 1591 #if 0 1592 XXX 1593 if (isr & 0x08) { 1594 clear input signal change! 1595 } 1596 #endif 1597 iports = GET_INPUT_SIGNALS(ch); 1598 delta = iports ^ ch->ch_iports; 1599 ch->ch_iports = iports; 1600 1601 /* 1602 * Process normal status changes 1603 */ 1604 if (ISSET(delta, ch->ch_i_mask)) { 1605 SET(ch->ch_iports_delta, delta); 1606 1607 /* 1608 * Stop output immediately if we lose the output 1609 * flow control signal or carrier detect. 1610 */ 1611 if (ISSET(~iports, ch->ch_i_mask)) { 1612 ch->ch_tbc = 0; 1613 ch->ch_heldtbc = 0; 1614 #ifdef SBSCN_DEBUG 1615 if (sbscn_debug) 1616 sbscn_status(ch, "sbscn_intr "); 1617 #endif 1618 } 1619 1620 ch->ch_st_check = 1; 1621 } 1622 } while ((isr = (READ_REG(ch->ch_isr_base) & ch->ch_imr)) != 0); 1623 1624 /* 1625 * Done handling any receive interrupts and status changes, and 1626 * clearing the tx-ready interrupt if it was set. See if data can 1627 * be transmitted as well. Schedule tx done event if no data left 1628 * and tty was marked busy. 1629 */ 1630 sr = READ_REG(ch->ch_base + 0x20); 1631 if (ISSET(sr, 0x4)) { 1632 1633 /* 1634 * If we've delayed a parameter change, do it now, and restart 1635 * output. 1636 */ 1637 if (ch->ch_heldchange) { 1638 sbscn_loadchannelregs(ch); 1639 ch->ch_heldchange = 0; 1640 ch->ch_tbc = ch->ch_heldtbc; 1641 ch->ch_heldtbc = 0; 1642 } 1643 1644 /* Output the next chunk of the contiguous buffer, if any. */ 1645 if (ch->ch_tbc > 0) { 1646 int wrote1; 1647 u_char c; 1648 1649 wrote1 = 0; 1650 while (ch->ch_tbc && 1651 READ_REG(ch->ch_base + 0x20) & 0x04) { 1652 wrote1 = 1; 1653 c = *ch->ch_tba++; 1654 ch->ch_tbc--; 1655 WRITE_REG(ch->ch_base + 0x70, c); 1656 } 1657 if (wrote1) { 1658 SET(ch->ch_imr, 0x01); 1659 WRITE_REG(ch->ch_imr_base, ch->ch_imr); 1660 } 1661 } else { 1662 /* 1663 * transmit completion interrupts already disabled, 1664 * mark the channel tx state as done. 1665 */ 1666 if (ch->ch_tx_busy) { 1667 ch->ch_tx_busy = 0; 1668 ch->ch_tx_done = 1; 1669 } 1670 } 1671 } 1672 1673 /* Wake up the poller. */ 1674 softint_schedule(ch->ch_si); 1675 1676 #ifdef RND_SBSCN 1677 rnd_add_uint32(&ch->ch_rnd_source, isr | sr); 1678 #endif 1679 } 1680 1681 /* 1682 * The following functions are polled getc and putc routines, shared 1683 * by the console and kgdb glue. 1684 */ 1685 1686 int 1687 sbscn_common_getc(u_long addr, int chan) 1688 { 1689 int s = splhigh(); 1690 u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100)); 1691 int c; 1692 1693 /* block until a character becomes available */ 1694 while ((READ_REG(base + 0x20) & 0x01) == 0) 1695 continue; 1696 1697 c = READ_REG(base + 0x60) & 0xff; 1698 splx(s); 1699 return (c); 1700 } 1701 1702 void 1703 sbscn_common_putc(u_long addr, int chan, int c) 1704 { 1705 int s = splhigh(); 1706 int timo; 1707 u_long base = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100)); 1708 1709 /* wait for any pending transmission to finish */ 1710 timo = 1500000; 1711 while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo) 1712 continue; 1713 1714 WRITE_REG(base + 0x70, c); 1715 1716 /* wait for this transmission to complete */ 1717 timo = 15000000; 1718 while ((READ_REG(base + 0x20) & 0x08) == 0 && --timo) 1719 continue; 1720 1721 splx(s); 1722 } 1723 1724 /* 1725 * Initialize UART for use as console or KGDB line. 1726 */ 1727 int 1728 sbscn_init(u_long addr, int chan, int rate, tcflag_t cflag) 1729 { 1730 #if 1 1731 u_long chanregbase = MIPS_PHYS_TO_KSEG1(addr + (chan * 0x100)); 1732 u_long imaskreg = MIPS_PHYS_TO_KSEG1(addr + 0x230 + (chan * 0x20)); 1733 u_char mode1, mode2; 1734 u_long brc; 1735 volatile int timo; 1736 1737 WRITE_REG(imaskreg, 0); /* disable channel intrs */ 1738 1739 /* XXX should we really do the following? how about only if enabled? */ 1740 /* wait for any pending transmission to finish */ 1741 timo = 1500000; 1742 while ((READ_REG(chanregbase + 0x20) & 0x08) == 0 && --timo) 1743 continue; 1744 1745 /* XXX: wait a little. THIS SHOULD NOT BE NECESSARY!!! (?) */ 1746 timo = 1500000; 1747 while (--timo) 1748 ; 1749 1750 WRITE_REG(chanregbase + 0x50, 2 << 4); /* reset receiver */ 1751 WRITE_REG(chanregbase + 0x50, 3 << 4); /* reset transmitter */ 1752 1753 /* set up the line for use */ 1754 (void)cflag2modes(cflag, &mode1, &mode2); 1755 (void)sbscn_speed(rate, &brc); 1756 WRITE_REG(chanregbase + 0x00, mode1); 1757 WRITE_REG(chanregbase + 0x10, mode2); 1758 WRITE_REG(chanregbase + 0x30, brc); 1759 1760 /* enable transmit and receive */ 1761 #define M_DUART_RX_EN 0x01 1762 #define M_DUART_TX_EN 0x04 1763 WRITE_REG(chanregbase + 0x50,M_DUART_RX_EN | M_DUART_TX_EN); 1764 #endif 1765 1766 /* XXX: wait a little. THIS SHOULD NOT BE NECESSARY!!! (?) */ 1767 timo = 1500000; 1768 while (--timo) 1769 ; 1770 1771 #if 0 /* XXXCGD */ 1772 bus_space_handle_t ioh; 1773 1774 if (bus_space_map(iot, iobase, COM_NPORTS, 0, &ioh)) 1775 return (ENOMEM); /* ??? */ 1776 1777 bus_space_write_1(iot, ioh, com_lcr, LCR_EERS); 1778 bus_space_write_1(iot, ioh, com_efr, 0); 1779 bus_space_write_1(iot, ioh, com_lcr, LCR_DLAB); 1780 rate = comspeed(rate, frequency); 1781 bus_space_write_1(iot, ioh, com_dlbl, rate); 1782 bus_space_write_1(iot, ioh, com_dlbh, rate >> 8); 1783 bus_space_write_1(iot, ioh, com_lcr, cflag2lcr(cflag)); 1784 bus_space_write_1(iot, ioh, com_mcr, MCR_DTR | MCR_RTS); 1785 bus_space_write_1(iot, ioh, com_fifo, 1786 FIFO_ENABLE | FIFO_RCV_RST | FIFO_XMT_RST | FIFO_TRIGGER_1); 1787 bus_space_write_1(iot, ioh, com_ier, 0); 1788 1789 *iohp = ioh; 1790 return (0); 1791 #endif /* XXXCGD */ 1792 /* XXXCGD */ 1793 return (0); 1794 } 1795 1796 /* 1797 * Following are all routines needed for sbscn to act as console 1798 */ 1799 int 1800 sbscn_cnattach(u_long addr, int chan, int rate, tcflag_t cflag) 1801 { 1802 int res; 1803 static struct consdev sbscn_cons = { 1804 NULL, NULL, sbscn_cngetc, sbscn_cnputc, sbscn_cnpollc, NULL, 1805 NULL, NULL, NODEV, CN_NORMAL 1806 }; 1807 1808 res = sbscn_init(addr, chan, rate, cflag); 1809 if (res) 1810 return (res); 1811 1812 cn_tab = &sbscn_cons; 1813 1814 sbscn_cons_present = 1; 1815 sbscn_cons_addr = addr; 1816 sbscn_cons_chan = chan; 1817 sbscn_cons_rate = rate; 1818 sbscn_cons_cflag = cflag; 1819 1820 return (0); 1821 } 1822 1823 int 1824 sbscn_cngetc(dev_t dev) 1825 { 1826 1827 return (sbscn_common_getc(sbscn_cons_addr, sbscn_cons_chan)); 1828 } 1829 1830 /* 1831 * Console kernel output character routine. 1832 */ 1833 void 1834 sbscn_cnputc(dev_t dev, int c) 1835 { 1836 1837 sbscn_common_putc(sbscn_cons_addr, sbscn_cons_chan, c); 1838 } 1839 1840 void 1841 sbscn_cnpollc(dev_t dev, int on) 1842 { 1843 1844 } 1845 1846 #ifdef KGDB 1847 int 1848 sbscn_kgdb_attach(u_long addr, int chan, int rate, tcflag_t cflag) 1849 { 1850 int res; 1851 1852 if (!sbscn_cons_present && 1853 sbscn_cons_addr == addr && sbscn_cons_chan == chan) 1854 return (EBUSY); /* cannot share with console */ 1855 1856 res = sbscn_init(addr, chan, rate, cflag); 1857 if (res) 1858 return (res); 1859 1860 kgdb_attach(sbscn_kgdb_getc, sbscn_kgdb_putc, NULL); 1861 kgdb_dev = 123; /* unneeded, only to satisfy some tests */ 1862 1863 sbscn_kgdb_present = 1; 1864 sbscn_kgdb_addr = addr; 1865 sbscn_kgdb_chan = chan; 1866 1867 return (0); 1868 } 1869 1870 /* ARGSUSED */ 1871 int 1872 sbscn_kgdb_getc(void *arg) 1873 { 1874 1875 return (sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan)); 1876 } 1877 1878 /* ARGSUSED */ 1879 void 1880 sbscn_kgdb_putc(void *arg, int c) 1881 { 1882 1883 sbscn_common_getc(sbscn_kgdb_addr, sbscn_kgdb_chan, c); 1884 } 1885 #endif /* KGDB */ 1886 1887 /* 1888 * helper function to identify the sbscn channels used by 1889 * console or KGDB (and not yet autoconf attached) 1890 */ 1891 int 1892 sbscn_is_console(u_long addr, int chan) 1893 { 1894 1895 if (sbscn_cons_present && !sbscn_cons_attached && 1896 sbscn_cons_addr == addr && sbscn_cons_chan == chan) 1897 return (1); 1898 #ifdef KGDB 1899 if (sbscn_kgdb_present && !sbscn_kgdb_attached && 1900 sbscn_kgdb_addr == addr && sbscn_kgdb_chan == chan) 1901 return (1); 1902 #endif 1903 return (0); 1904 } 1905