1 /* $NetBSD: zynq_uart.c,v 1.8 2026/11/10 03:04:59 thorpej Exp $ */ 2 3 /* 4 * Copyright (c) 2012 Genetec Corporation. All rights reserved. 5 * Written by Hiroyuki Bessho, Hashimoto Kenichi for Genetec Corporation. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY GENETEC CORPORATION ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORPORATION 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 */ 29 30 /* 31 * derived from sys/dev/ic/com.c 32 */ 33 34 /*- 35 * Copyright (c) 1998, 1999, 2004, 2008 The NetBSD Foundation, Inc. 36 * All rights reserved. 37 * 38 * This code is derived from software contributed to The NetBSD Foundation 39 * by Charles M. Hannum. 40 * 41 * Redistribution and use in source and binary forms, with or without 42 * modification, are permitted provided that the following conditions 43 * are met: 44 * 1. Redistributions of source code must retain the above copyright 45 * notice, this list of conditions and the following disclaimer. 46 * 2. Redistributions in binary form must reproduce the above copyright 47 * notice, this list of conditions and the following disclaimer in the 48 * documentation and/or other materials provided with the distribution. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 51 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 52 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 53 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 54 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 55 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 56 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 57 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 58 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 59 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 60 * POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 /* 64 * Copyright (c) 1991 The Regents of the University of California. 65 * All rights reserved. 66 * 67 * Redistribution and use in source and binary forms, with or without 68 * modification, are permitted provided that the following conditions 69 * are met: 70 * 1. Redistributions of source code must retain the above copyright 71 * notice, this list of conditions and the following disclaimer. 72 * 2. Redistributions in binary form must reproduce the above copyright 73 * notice, this list of conditions and the following disclaimer in the 74 * documentation and/or other materials provided with the distribution. 75 * 3. Neither the name of the University nor the names of its contributors 76 * may be used to endorse or promote products derived from this software 77 * without specific prior written permission. 78 * 79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 89 * SUCH DAMAGE. 90 * 91 * @(#)com.c 7.5 (Berkeley) 5/16/91 92 */ 93 94 /* 95 * driver for UART in Zynq-7000. 96 */ 97 98 #include <sys/cdefs.h> 99 __KERNEL_RCSID(0, "$NetBSD: zynq_uart.c,v 1.8 2026/11/10 03:04:59 thorpej Exp $"); 100 101 #include "opt_soc.h" 102 #include "opt_console.h" 103 #include "opt_com.h" 104 #include "opt_ddb.h" 105 #include "opt_kgdb.h" 106 #include "opt_ntp.h" 107 108 #include <sys/param.h> 109 110 #include <sys/bus.h> 111 #include <sys/conf.h> 112 #include <dev/cons.h> 113 #include <sys/device.h> 114 #include <sys/file.h> 115 #include <sys/kauth.h> 116 #include <sys/kernel.h> 117 #include <sys/kmem.h> 118 #include <sys/poll.h> 119 #include <sys/proc.h> 120 #include <sys/systm.h> 121 #include <sys/tty.h> 122 123 #ifdef RND_COM 124 #include <sys/rndsource.h> 125 #endif 126 127 #include <arm/xilinx/zynq_uartreg.h> 128 #include <arm/xilinx/zynq_uartvar.h> 129 130 #ifndef ZYNQUART_RING_SIZE 131 #define ZYNQUART_RING_SIZE 2048 132 #endif 133 134 #define UART_SIZE 0x00000048 135 136 typedef struct zynquart_softc { 137 device_t sc_dev; 138 139 struct zynquart_regs { 140 bus_space_tag_t ur_iot; 141 bus_space_handle_t ur_ioh; 142 bus_addr_t ur_iobase; 143 } sc_regs; 144 145 #define sc_bt sc_regs.ur_iot 146 #define sc_bh sc_regs.ur_ioh 147 148 uint32_t sc_intrspec_enb; 149 uint32_t sc_cr; 150 uint32_t sc_mcr; 151 uint32_t sc_msr; 152 153 uint sc_init_cnt; 154 155 bus_addr_t sc_addr; 156 bus_size_t sc_size; 157 158 u_char sc_hwflags; 159 /* Hardware flag masks */ 160 #define ZYNQUART_HW_FLOW __BIT(0) 161 #define ZYNQUART_HW_DEV_OK __BIT(1) 162 #define ZYNQUART_HW_CONSOLE __BIT(2) 163 #define ZYNQUART_HW_KGDB __BIT(3) 164 165 bool enabled; 166 167 u_char sc_swflags; 168 169 u_char sc_rx_flags; 170 #define ZYNQUART_RX_TTY_BLOCKED __BIT(0) 171 #define ZYNQUART_RX_TTY_OVERFLOWED __BIT(1) 172 #define ZYNQUART_RX_IBUF_BLOCKED __BIT(2) 173 #define ZYNQUART_RX_IBUF_OVERFLOWED __BIT(3) 174 #define ZYNQUART_RX_ANY_BLOCK \ 175 (ZYNQUART_RX_TTY_BLOCKED|ZYNQUART_RX_TTY_OVERFLOWED| \ 176 ZYNQUART_RX_IBUF_BLOCKED|ZYNQUART_RX_IBUF_OVERFLOWED) 177 178 bool sc_tx_busy, sc_tx_done, sc_tx_stopped; 179 bool sc_rx_ready,sc_st_check; 180 u_short sc_txfifo_len, sc_txfifo_thresh; 181 182 uint16_t *sc_rbuf; 183 u_int sc_rbuf_size; 184 u_int sc_rbuf_in; 185 u_int sc_rbuf_out; 186 #define ZYNQUART_RBUF_AVAIL(sc) \ 187 ((sc->sc_rbuf_out <= sc->sc_rbuf_in) ? \ 188 (sc->sc_rbuf_in - sc->sc_rbuf_out) : \ 189 (sc->sc_rbuf_size - (sc->sc_rbuf_out - sc->sc_rbuf_in))) 190 191 #define ZYNQUART_RBUF_SPACE(sc) \ 192 ((sc->sc_rbuf_in <= sc->sc_rbuf_out ? \ 193 sc->sc_rbuf_size - (sc->sc_rbuf_out - sc->sc_rbuf_in) : \ 194 sc->sc_rbuf_in - sc->sc_rbuf_out) - 1) 195 /* increment ringbuffer pointer */ 196 #define ZYNQUART_RBUF_INC(sc,v,i) (((v) + (i))&((sc->sc_rbuf_size)-1)) 197 u_int sc_r_lowat; 198 u_int sc_r_hiwat; 199 200 /* output chunk */ 201 u_char *sc_tba; 202 u_int sc_tbc; 203 u_int sc_heldtbc; 204 /* pending parameter changes */ 205 u_char sc_pending; 206 #define ZYNQUART_PEND_PARAM __BIT(0) 207 #define ZYNQUART_PEND_SPEED __BIT(1) 208 209 210 struct callout sc_diag_callout; 211 kmutex_t sc_lock; 212 void *sc_ih; /* interrupt handler */ 213 void *sc_si; /* soft interrupt */ 214 struct tty *sc_tty; 215 216 /* power management hooks */ 217 int (*enable)(struct zynquart_softc *); 218 void (*disable)(struct zynquart_softc *); 219 220 struct { 221 ulong err; 222 ulong brk; 223 ulong prerr; 224 ulong frmerr; 225 ulong ovrrun; 226 } sc_errors; 227 228 struct zynquart_baudrate_ratio { 229 uint16_t numerator; /* UBIR */ 230 uint16_t modulator; /* UBMR */ 231 } sc_ratio; 232 233 } zynquart_softc_t; 234 235 236 int zynquartspeed(long, struct zynquart_baudrate_ratio *); 237 int zynquartparam(struct tty *, struct termios *); 238 void zynquartstart(struct tty *); 239 int zynquarthwiflow(struct tty *, int); 240 241 void zynquart_shutdown(struct zynquart_softc *); 242 void zynquart_loadchannelregs(struct zynquart_softc *); 243 void zynquart_hwiflow(struct zynquart_softc *); 244 void zynquart_break(struct zynquart_softc *, bool); 245 void zynquart_modem(struct zynquart_softc *, int); 246 void tiocm_to_zynquart(struct zynquart_softc *, u_long, int); 247 int zynquart_to_tiocm(struct zynquart_softc *); 248 void zynquart_iflush(struct zynquart_softc *); 249 250 int zynquart_common_getc(dev_t, struct zynquart_regs *); 251 void zynquart_common_putc(dev_t, struct zynquart_regs *, int); 252 253 254 int zynquart_init(struct zynquart_regs *, int, tcflag_t); 255 256 int zynquartcngetc(dev_t); 257 void zynquartcnputc(dev_t, int); 258 259 static void zynquartintr_read(struct zynquart_softc *); 260 static void zynquartintr_send(struct zynquart_softc *); 261 262 static void zynquart_enable_debugport(struct zynquart_softc *); 263 static void zynquart_disable_all_interrupts(struct zynquart_softc *); 264 static void zynquart_control_rxint(struct zynquart_softc *, bool); 265 static void zynquart_control_txint(struct zynquart_softc *, bool); 266 267 static uint32_t cflag_to_zynquart(tcflag_t, uint32_t); 268 269 #define integrate static inline 270 void zynquartsoft(void *); 271 integrate void zynquart_rxsoft(struct zynquart_softc *, struct tty *); 272 integrate void zynquart_txsoft(struct zynquart_softc *, struct tty *); 273 integrate void zynquart_stsoft(struct zynquart_softc *, struct tty *); 274 integrate void zynquart_schedrx(struct zynquart_softc *); 275 void zynquartdiag(void *); 276 static void zynquart_load_speed(struct zynquart_softc *); 277 static void zynquart_load_params(struct zynquart_softc *); 278 integrate void zynquart_load_pendings(struct zynquart_softc *); 279 280 281 extern struct cfdriver zynquart_cd; 282 283 dev_type_open(zynquartopen); 284 dev_type_close(zynquartclose); 285 dev_type_read(zynquartread); 286 dev_type_write(zynquartwrite); 287 dev_type_ioctl(zynquartioctl); 288 dev_type_stop(zynquartstop); 289 dev_type_tty(zynquarttty); 290 dev_type_poll(zynquartpoll); 291 292 const struct cdevsw zynquart_cdevsw = { 293 .d_open = zynquartopen, 294 .d_close = zynquartclose, 295 .d_read = zynquartread, 296 .d_write = zynquartwrite, 297 .d_ioctl = zynquartioctl, 298 .d_stop = zynquartstop, 299 .d_tty = zynquarttty, 300 .d_poll = zynquartpoll, 301 .d_mmap = nommap, 302 .d_kqfilter = ttykqfilter, 303 .d_discard = nodiscard, 304 .d_flag = D_TTY 305 }; 306 307 /* 308 * Make this an option variable one can patch. 309 * But be warned: this must be a power of 2! 310 */ 311 u_int zynquart_rbuf_size = ZYNQUART_RING_SIZE; 312 313 /* Stop input when 3/4 of the ring is full; restart when only 1/4 is full. */ 314 u_int zynquart_rbuf_hiwat = (ZYNQUART_RING_SIZE * 1) / 4; 315 u_int zynquart_rbuf_lowat = (ZYNQUART_RING_SIZE * 3) / 4; 316 317 static struct zynquart_regs zynquartconsregs; 318 static int zynquartconsattached; 319 static int zynquartconsrate; 320 static tcflag_t zynquartconscflag; 321 static struct cnm_state zynquart_cnm_state; 322 323 u_int zynquart_freq; 324 u_int zynquart_freqdiv; 325 326 #ifdef KGDB 327 #include <sys/kgdb.h> 328 329 static struct zynquart_regs zynquart_kgdb_regs; 330 static int zynquart_kgdb_attached; 331 332 int zynquart_kgdb_getc(void *); 333 void zynquart_kgdb_putc(void *, int); 334 #endif /* KGDB */ 335 336 #define ZYNQUART_UNIT_MASK 0x7ffff 337 #define ZYNQUART_DIALOUT_MASK 0x80000 338 339 #define ZYNQUART_UNIT(x) (minor(x) & ZYNQUART_UNIT_MASK) 340 #define ZYNQUART_DIALOUT(x) (minor(x) & ZYNQUART_DIALOUT_MASK) 341 342 #define ZYNQUART_ISALIVE(sc) ((sc)->enabled != 0 && \ 343 device_is_active((sc)->sc_dev)) 344 345 #define BR BUS_SPACE_BARRIER_READ 346 #define BW BUS_SPACE_BARRIER_WRITE 347 #define ZYNQUART_BARRIER(r, f) \ 348 bus_space_barrier((r)->ur_iot, (r)->ur_ioh, 0, UART_SIZE, (f)) 349 350 CFATTACH_DECL_NEW(zynquart, sizeof(struct zynquart_softc), 351 zynquart_match, zynquart_attach, NULL, NULL); 352 353 void 354 zynquart_attach_common(device_t parent, device_t self, 355 bus_space_tag_t iot, paddr_t iobase, size_t size, int flags) 356 { 357 zynquart_softc_t *sc = device_private(self); 358 struct zynquart_regs *regsp = &sc->sc_regs; 359 struct tty *tp; 360 bus_space_handle_t ioh; 361 362 aprint_naive("\n"); 363 aprint_normal("\n"); 364 365 sc->sc_dev = self; 366 367 if (size <= 0) 368 size = UART_SIZE; 369 370 regsp->ur_iot = iot; 371 regsp->ur_iobase = iobase; 372 373 if (bus_space_map(iot, regsp->ur_iobase, size, 0, &ioh)) { 374 return; 375 } 376 regsp->ur_ioh = ioh; 377 378 callout_init(&sc->sc_diag_callout, 0); 379 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_HIGH); 380 381 sc->sc_cr = bus_space_read_4(iot, ioh, UART_CONTROL); 382 sc->sc_cr |= CR_TXEN | CR_RXEN; 383 sc->sc_cr &= ~(CR_TXDIS | CR_RXDIS); 384 bus_space_write_4(iot, ioh, UART_CONTROL, sc->sc_cr); 385 386 /* Disable interrupts before configuring the device. */ 387 zynquart_disable_all_interrupts(sc); 388 389 if (regsp->ur_iobase == zynquartconsregs.ur_iobase) { 390 zynquartconsattached = 1; 391 392 /* Make sure the console is always "hardwired". */ 393 SET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE); 394 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 395 } 396 397 tp = tty_alloc(); 398 tp->t_oproc = zynquartstart; 399 tp->t_param = zynquartparam; 400 tp->t_hwiflow = zynquarthwiflow; 401 402 sc->sc_tty = tp; 403 sc->sc_rbuf = kmem_alloc(sizeof (*sc->sc_rbuf) * zynquart_rbuf_size, 404 KM_SLEEP); 405 sc->sc_rbuf_size = zynquart_rbuf_size; 406 sc->sc_rbuf_in = sc->sc_rbuf_out = 0; 407 sc->sc_txfifo_len = 64; 408 sc->sc_txfifo_thresh = 32; 409 410 tty_attach(tp); 411 412 if (ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) { 413 int maj; 414 415 /* locate the major number */ 416 maj = cdevsw_lookup_major(&zynquart_cdevsw); 417 418 if (maj != NODEVMAJOR) { 419 tp->t_dev = cn_tab->cn_dev = makedev(maj, 420 device_unit(sc->sc_dev)); 421 422 aprint_normal_dev(sc->sc_dev, "console\n"); 423 } 424 } 425 426 /* reset receive time out */ 427 bus_space_write_4(iot, ioh, UART_RCVR_TIMEOUT, 0); 428 bus_space_write_4(iot, ioh, UART_RCVR_FIFO_TRIGGER, 1); 429 430 #ifdef KGDB 431 /* 432 * Allow kgdb to "take over" this port. If this is 433 * not the console and is the kgdb device, it has 434 * exclusive use. If it's the console _and_ the 435 * kgdb device, it doesn't. 436 */ 437 if (regsp->ur_iobase == zynquart_kgdb_regs.ur_iobase) { 438 if (!ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) { 439 zynquart_kgdb_attached = 1; 440 441 SET(sc->sc_hwflags, ZYNQUART_HW_KGDB); 442 } 443 aprint_normal_dev(sc->sc_dev, "kgdb\n"); 444 } 445 #endif 446 447 sc->sc_si = softint_establish(SOFTINT_SERIAL, zynquartsoft, sc); 448 449 #ifdef RND_COM 450 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev), 451 RND_TYPE_TTY, 0); 452 #endif 453 454 /* if there are no enable/disable functions, assume the device 455 is always enabled */ 456 if (!sc->enable) 457 sc->enabled = 1; 458 459 zynquart_enable_debugport(sc); 460 461 SET(sc->sc_hwflags, ZYNQUART_HW_DEV_OK); 462 } 463 464 int 465 zynquartspeed(long speed, struct zynquart_baudrate_ratio *ratio) 466 { 467 return 0; 468 } 469 470 #ifdef ZYNQUART_DEBUG 471 int zynquart_debug = 0; 472 473 void zynquartstatus(struct zynquart_softc *, const char *); 474 void 475 zynquartstatus(struct zynquart_softc *sc, const char *str) 476 { 477 struct tty *tp = sc->sc_tty; 478 479 aprint_normal_dev(sc->sc_dev, 480 "%s %cclocal %cdcd %cts_carr_on %cdtr %ctx_stopped\n", 481 str, 482 ISSET(tp->t_cflag, CLOCAL) ? '+' : '-', 483 ISSET(sc->sc_msr, MSR_DCD) ? '+' : '-', 484 ISSET(tp->t_state, TS_CARR_ON) ? '+' : '-', 485 ISSET(sc->sc_mcr, MCR_DTR) ? '+' : '-', 486 sc->sc_tx_stopped ? '+' : '-'); 487 488 aprint_normal_dev(sc->sc_dev, 489 "%s %ccrtscts %ccts %cts_ttstop %crts rx_flags=0x%x\n", 490 str, 491 ISSET(tp->t_cflag, CRTSCTS) ? '+' : '-', 492 ISSET(sc->sc_msr, MSR_CTS) ? '+' : '-', 493 ISSET(tp->t_state, TS_TTSTOP) ? '+' : '-', 494 ISSET(sc->sc_mcr, MCR_RTS) ? '+' : '-', 495 sc->sc_rx_flags); 496 } 497 #endif 498 499 #if 0 500 int 501 zynquart_detach(device_t self, int flags) 502 { 503 struct zynquart_softc *sc = device_private(self); 504 int maj, mn; 505 506 if (ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) 507 return EBUSY; 508 509 /* locate the major number */ 510 maj = cdevsw_lookup_major(&zynquart_cdevsw); 511 512 /* Nuke the vnodes for any open instances. */ 513 mn = device_unit(self); 514 vdevgone(maj, mn, mn, VCHR); 515 516 mn |= ZYNQUART_DIALOUT_MASK; 517 vdevgone(maj, mn, mn, VCHR); 518 519 if (sc->sc_rbuf == NULL) { 520 /* 521 * Ring buffer allocation failed in the zynquart_attach_subr, 522 * only the tty is allocated, and nothing else. 523 */ 524 tty_free(sc->sc_tty); 525 return 0; 526 } 527 528 /* Free the receive buffer. */ 529 kmem_free(sc->sc_rbuf, sizeof(*sc->sc_rbuf) * sc->sc_rbuf_size); 530 531 /* Detach and free the tty. */ 532 tty_detach(sc->sc_tty); 533 tty_free(sc->sc_tty); 534 535 /* Unhook the soft interrupt handler. */ 536 softint_disestablish(sc->sc_si); 537 538 #ifdef RND_COM 539 /* Unhook the entropy source. */ 540 rnd_detach_source(&sc->rnd_source); 541 #endif 542 callout_destroy(&sc->sc_diag_callout); 543 544 /* Destroy the lock. */ 545 mutex_destroy(&sc->sc_lock); 546 547 return (0); 548 } 549 #endif 550 551 #ifdef notyet 552 int 553 zynquart_activate(device_t self, enum devact act) 554 { 555 struct zynquart_softc *sc = device_private(self); 556 int rv = 0; 557 558 switch (act) { 559 case DVACT_ACTIVATE: 560 rv = EOPNOTSUPP; 561 break; 562 563 case DVACT_DEACTIVATE: 564 if (sc->sc_hwflags & (ZYNQUART_HW_CONSOLE|ZYNQUART_HW_KGDB)) { 565 rv = EBUSY; 566 break; 567 } 568 569 if (sc->disable != NULL && sc->enabled != 0) { 570 (*sc->disable)(sc); 571 sc->enabled = 0; 572 } 573 break; 574 } 575 576 return (rv); 577 } 578 #endif 579 580 void 581 zynquart_shutdown(struct zynquart_softc *sc) 582 { 583 struct tty *tp = sc->sc_tty; 584 585 mutex_spin_enter(&sc->sc_lock); 586 587 /* If we were asserting flow control, then deassert it. */ 588 SET(sc->sc_rx_flags, ZYNQUART_RX_IBUF_BLOCKED); 589 zynquart_hwiflow(sc); 590 591 /* Clear any break condition set with TIOCSBRK. */ 592 zynquart_break(sc, false); 593 594 /* 595 * Hang up if necessary. Wait a bit, so the other side has time to 596 * notice even if we immediately open the port again. 597 * Avoid tsleeping above splhigh(). 598 */ 599 if (ISSET(tp->t_cflag, HUPCL)) { 600 zynquart_modem(sc, 0); 601 mutex_spin_exit(&sc->sc_lock); 602 /* XXX will only timeout */ 603 (void) kpause(ttclos, false, hz, NULL); 604 mutex_spin_enter(&sc->sc_lock); 605 } 606 607 /* Turn off interrupts. */ 608 zynquart_disable_all_interrupts(sc); 609 /* re-enable recv interrupt for console or kgdb port */ 610 zynquart_enable_debugport(sc); 611 612 mutex_spin_exit(&sc->sc_lock); 613 614 #ifdef notyet 615 if (sc->disable) { 616 #ifdef DIAGNOSTIC 617 if (!sc->enabled) 618 panic("zynquart_shutdown: not enabled?"); 619 #endif 620 (*sc->disable)(sc); 621 sc->enabled = 0; 622 } 623 #endif 624 } 625 626 int 627 zynquartopen(dev_t dev, int flag, int mode, struct lwp *l) 628 { 629 struct zynquart_softc *sc; 630 struct tty *tp; 631 int s; 632 int error; 633 634 sc = device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev)); 635 if (sc == NULL || !ISSET(sc->sc_hwflags, ZYNQUART_HW_DEV_OK) || 636 sc->sc_rbuf == NULL) 637 return (ENXIO); 638 639 if (!device_is_active(sc->sc_dev)) 640 return (ENXIO); 641 642 #ifdef KGDB 643 /* 644 * If this is the kgdb port, no other use is permitted. 645 */ 646 if (ISSET(sc->sc_hwflags, ZYNQUART_HW_KGDB)) 647 return (EBUSY); 648 #endif 649 650 tp = sc->sc_tty; 651 652 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 653 return (EBUSY); 654 655 s = spltty(); 656 657 /* 658 * Do the following iff this is a first open. 659 */ 660 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 661 struct termios t; 662 663 tp->t_dev = dev; 664 665 666 #ifdef notyet 667 if (sc->enable) { 668 if ((*sc->enable)(sc)) { 669 splx(s); 670 aprint_error_dev(sc->sc_dev, 671 "device enable failed\n"); 672 return (EIO); 673 } 674 sc->enabled = 1; 675 } 676 #endif 677 678 mutex_spin_enter(&sc->sc_lock); 679 680 zynquart_disable_all_interrupts(sc); 681 682 /* Fetch the current modem control status, needed later. */ 683 684 #ifdef ZYNQUART_PPS 685 /* Clear PPS capture state on first open. */ 686 mutex_spin_enter(&timecounter_lock); 687 memset(&sc->sc_pps_state, 0, sizeof(sc->sc_pps_state)); 688 sc->sc_pps_state.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 689 pps_init(&sc->sc_pps_state); 690 mutex_spin_exit(&timecounter_lock); 691 #endif 692 693 mutex_spin_exit(&sc->sc_lock); 694 695 /* 696 * Initialize the termios status to the defaults. Add in the 697 * sticky bits from TIOCSFLAGS. 698 */ 699 if (ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) { 700 t.c_ospeed = zynquartconsrate; 701 t.c_cflag = zynquartconscflag; 702 } else { 703 t.c_ospeed = TTYDEF_SPEED; 704 t.c_cflag = TTYDEF_CFLAG; 705 } 706 t.c_ispeed = t.c_ospeed; 707 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 708 SET(t.c_cflag, CLOCAL); 709 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 710 SET(t.c_cflag, CRTSCTS); 711 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 712 SET(t.c_cflag, MDMBUF); 713 /* Make sure zynquartparam() will do something. */ 714 tp->t_ospeed = 0; 715 (void) zynquartparam(tp, &t); 716 tp->t_iflag = TTYDEF_IFLAG; 717 tp->t_oflag = TTYDEF_OFLAG; 718 tp->t_lflag = TTYDEF_LFLAG; 719 ttychars(tp); 720 ttsetwater(tp); 721 722 mutex_spin_enter(&sc->sc_lock); 723 724 /* 725 * Turn on DTR. We must always do this, even if carrier is not 726 * present, because otherwise we'd have to use TIOCSDTR 727 * immediately after setting CLOCAL, which applications do not 728 * expect. We always assert DTR while the device is open 729 * unless explicitly requested to deassert it. 730 */ 731 zynquart_modem(sc, 1); 732 733 /* Clear the input ring, and unblock. */ 734 sc->sc_rbuf_in = sc->sc_rbuf_out = 0; 735 zynquart_iflush(sc); 736 CLR(sc->sc_rx_flags, ZYNQUART_RX_ANY_BLOCK); 737 zynquart_hwiflow(sc); 738 739 /* Turn on interrupts. */ 740 zynquart_control_rxint(sc, true); 741 742 #ifdef ZYNQUART_DEBUG 743 if (zynquart_debug) 744 zynquartstatus(sc, "zynquartopen "); 745 #endif 746 747 mutex_spin_exit(&sc->sc_lock); 748 } 749 750 splx(s); 751 752 #if 0 753 error = ttyopen(tp, ZYNQUART_DIALOUT(dev), ISSET(flag, O_NONBLOCK)); 754 #else 755 error = ttyopen(tp, 1, ISSET(flag, O_NONBLOCK)); 756 #endif 757 if (error) 758 goto bad; 759 760 error = (*tp->t_linesw->l_open)(dev, tp); 761 if (error) 762 goto bad; 763 764 return (0); 765 766 bad: 767 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 768 /* 769 * We failed to open the device, and nobody else had it opened. 770 * Clean up the state as appropriate. 771 */ 772 zynquart_shutdown(sc); 773 } 774 775 return (error); 776 } 777 778 int 779 zynquartclose(dev_t dev, int flag, int mode, struct lwp *l) 780 { 781 struct zynquart_softc *sc = 782 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev)); 783 struct tty *tp = sc->sc_tty; 784 785 /* XXX This is for cons.c. */ 786 if (!ISSET(tp->t_state, TS_ISOPEN)) 787 return (0); 788 789 (*tp->t_linesw->l_close)(tp, flag); 790 ttyclose(tp); 791 792 if (ZYNQUART_ISALIVE(sc) == 0) 793 return (0); 794 795 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 796 /* 797 * Although we got a last close, the device may still be in 798 * use; e.g. if this was the dialout node, and there are still 799 * processes waiting for carrier on the non-dialout node. 800 */ 801 zynquart_shutdown(sc); 802 } 803 804 return (0); 805 } 806 807 int 808 zynquartread(dev_t dev, struct uio *uio, int flag) 809 { 810 struct zynquart_softc *sc = 811 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev)); 812 struct tty *tp = sc->sc_tty; 813 814 if (ZYNQUART_ISALIVE(sc) == 0) 815 return (EIO); 816 817 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 818 } 819 820 int 821 zynquartwrite(dev_t dev, struct uio *uio, int flag) 822 { 823 struct zynquart_softc *sc = 824 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev)); 825 struct tty *tp = sc->sc_tty; 826 827 if (ZYNQUART_ISALIVE(sc) == 0) 828 return (EIO); 829 830 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 831 } 832 833 int 834 zynquartpoll(dev_t dev, int events, struct lwp *l) 835 { 836 struct zynquart_softc *sc = 837 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev)); 838 struct tty *tp = sc->sc_tty; 839 840 if (ZYNQUART_ISALIVE(sc) == 0) 841 return (POLLHUP); 842 843 return ((*tp->t_linesw->l_poll)(tp, events, l)); 844 } 845 846 struct tty * 847 zynquarttty(dev_t dev) 848 { 849 struct zynquart_softc *sc = 850 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev)); 851 struct tty *tp = sc->sc_tty; 852 853 return (tp); 854 } 855 856 int 857 zynquartioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 858 { 859 struct zynquart_softc *sc; 860 struct tty *tp; 861 int error; 862 863 sc = device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(dev)); 864 if (sc == NULL) 865 return ENXIO; 866 if (ZYNQUART_ISALIVE(sc) == 0) 867 return (EIO); 868 869 tp = sc->sc_tty; 870 871 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 872 if (error != EPASSTHROUGH) 873 return (error); 874 875 error = ttioctl(tp, cmd, data, flag, l); 876 if (error != EPASSTHROUGH) 877 return (error); 878 879 error = 0; 880 switch (cmd) { 881 case TIOCSFLAGS: 882 error = kauth_authorize_device_tty(l->l_cred, 883 KAUTH_DEVICE_TTY_PRIVSET, tp); 884 break; 885 default: 886 /* nothing */ 887 break; 888 } 889 if (error) { 890 return error; 891 } 892 893 mutex_spin_enter(&sc->sc_lock); 894 895 switch (cmd) { 896 case TIOCSBRK: 897 zynquart_break(sc, true); 898 break; 899 900 case TIOCCBRK: 901 zynquart_break(sc, false); 902 break; 903 904 case TIOCSDTR: 905 zynquart_modem(sc, 1); 906 break; 907 908 case TIOCCDTR: 909 zynquart_modem(sc, 0); 910 break; 911 912 case TIOCGFLAGS: 913 *(int *)data = sc->sc_swflags; 914 break; 915 916 case TIOCSFLAGS: 917 sc->sc_swflags = *(int *)data; 918 break; 919 920 case TIOCMSET: 921 case TIOCMBIS: 922 case TIOCMBIC: 923 tiocm_to_zynquart(sc, cmd, *(int *)data); 924 break; 925 926 case TIOCMGET: 927 *(int *)data = zynquart_to_tiocm(sc); 928 break; 929 930 #ifdef notyet 931 case PPS_IOC_CREATE: 932 case PPS_IOC_DESTROY: 933 case PPS_IOC_GETPARAMS: 934 case PPS_IOC_SETPARAMS: 935 case PPS_IOC_GETCAP: 936 case PPS_IOC_FETCH: 937 #ifdef PPS_SYNC 938 case PPS_IOC_KCBIND: 939 #endif 940 mutex_spin_enter(&timecounter_lock); 941 error = pps_ioctl(cmd, data, &sc->sc_pps_state); 942 mutex_spin_exit(&timecounter_lock); 943 break; 944 945 case TIOCDCDTIMESTAMP: /* XXX old, overloaded API used by xntpd v3 */ 946 mutex_spin_enter(&timecounter_lock); 947 #ifndef PPS_TRAILING_EDGE 948 TIMESPEC_TO_TIMEVAL((struct timeval *)data, 949 &sc->sc_pps_state.ppsinfo.assert_timestamp); 950 #else 951 TIMESPEC_TO_TIMEVAL((struct timeval *)data, 952 &sc->sc_pps_state.ppsinfo.clear_timestamp); 953 #endif 954 mutex_spin_exit(&timecounter_lock); 955 break; 956 #endif 957 958 default: 959 error = EPASSTHROUGH; 960 break; 961 } 962 963 mutex_spin_exit(&sc->sc_lock); 964 965 #ifdef ZYNQUART_DEBUG 966 if (zynquart_debug) 967 zynquartstatus(sc, "zynquartioctl "); 968 #endif 969 970 return (error); 971 } 972 973 integrate void 974 zynquart_schedrx(struct zynquart_softc *sc) 975 { 976 sc->sc_rx_ready = 1; 977 978 /* Wake up the poller. */ 979 softint_schedule(sc->sc_si); 980 } 981 982 void 983 zynquart_break(struct zynquart_softc *sc, bool onoff) 984 { 985 bus_space_tag_t iot = sc->sc_regs.ur_iot; 986 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 987 988 if (onoff) 989 SET(sc->sc_cr, CR_STPBRK); 990 else 991 CLR(sc->sc_cr, CR_STPBRK); 992 993 bus_space_write_4(iot, ioh, UART_CONTROL, sc->sc_cr); 994 } 995 996 void 997 zynquart_modem(struct zynquart_softc *sc, int onoff) 998 { 999 #ifdef notyet 1000 if (sc->sc_mcr_dtr == 0) 1001 return; 1002 1003 if (onoff) 1004 SET(sc->sc_mcr, sc->sc_mcr_dtr); 1005 else 1006 CLR(sc->sc_mcr, sc->sc_mcr_dtr); 1007 1008 if (!sc->sc_heldchange) { 1009 if (sc->sc_tx_busy) { 1010 sc->sc_heldtbc = sc->sc_tbc; 1011 sc->sc_tbc = 0; 1012 sc->sc_heldchange = 1; 1013 } else 1014 zynquart_loadchannelregs(sc); 1015 } 1016 #endif 1017 } 1018 1019 void 1020 tiocm_to_zynquart(struct zynquart_softc *sc, u_long how, int ttybits) 1021 { 1022 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1023 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1024 1025 u_char combits; 1026 1027 combits = 0; 1028 if (ISSET(ttybits, TIOCM_DTR)) 1029 SET(combits, MODEMCR_DTR); 1030 if (ISSET(ttybits, TIOCM_RTS)) 1031 SET(combits, MODEMCR_RTS); 1032 1033 switch (how) { 1034 case TIOCMBIC: 1035 CLR(sc->sc_mcr, combits); 1036 break; 1037 1038 case TIOCMBIS: 1039 SET(sc->sc_mcr, combits); 1040 break; 1041 1042 case TIOCMSET: 1043 CLR(sc->sc_mcr, MODEMCR_DTR | MODEMCR_RTS); 1044 SET(sc->sc_mcr, combits); 1045 break; 1046 } 1047 1048 bus_space_write_4(iot, ioh, UART_MODEM_CTRL, sc->sc_mcr); 1049 } 1050 1051 int 1052 zynquart_to_tiocm(struct zynquart_softc *sc) 1053 { 1054 #ifdef notyet 1055 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1056 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1057 #endif 1058 uint32_t combits; 1059 int ttybits = 0; 1060 1061 combits = sc->sc_mcr; 1062 if (ISSET(combits, MODEMCR_DTR)) 1063 SET(ttybits, TIOCM_DTR); 1064 if (ISSET(combits, MODEMCR_RTS)) 1065 SET(ttybits, TIOCM_RTS); 1066 1067 combits = sc->sc_msr; 1068 if (ISSET(combits, MODEMSR_DCD)) 1069 SET(ttybits, TIOCM_CD); 1070 if (ISSET(combits, MODEMSR_CTS)) 1071 SET(ttybits, TIOCM_CTS); 1072 if (ISSET(combits, MODEMSR_DSR)) 1073 SET(ttybits, TIOCM_DSR); 1074 if (ISSET(combits, MODEMSR_RI | MODEMSR_TERI)) 1075 SET(ttybits, TIOCM_RI); 1076 1077 #ifdef notyet 1078 combits = bus_space_read_4(iot, ioh, UART_INTRPT_MASK); 1079 if (ISSET(sc->sc_imr, IER_ERXRDY | IER_ETXRDY | IER_ERLS | IER_EMSC)) 1080 SET(ttybits, TIOCM_LE); 1081 #endif 1082 1083 return (ttybits); 1084 } 1085 1086 static uint32_t 1087 cflag_to_zynquart(tcflag_t cflag, uint32_t oldval) 1088 { 1089 uint32_t val = oldval; 1090 1091 CLR(val, MR_CHMODE | MR_NBSTOP | MR_PAR | MR_CHRL | MR_CLKS); 1092 1093 switch (cflag & CSIZE) { 1094 case CS5: 1095 /* not suppreted. use 7-bits */ 1096 case CS6: 1097 SET(val, CHRL_6BIT); 1098 break; 1099 case CS7: 1100 SET(val, CHRL_7BIT); 1101 break; 1102 case CS8: 1103 SET(val, CHRL_8BIT); 1104 break; 1105 } 1106 1107 if (ISSET(cflag, PARENB)) { 1108 /* odd parity */ 1109 if (!ISSET(cflag, PARODD)) 1110 SET(val, PAR_ODD); 1111 else 1112 SET(val, PAR_EVEN); 1113 } else { 1114 SET(val, PAR_NONE); 1115 } 1116 1117 if (ISSET(cflag, CSTOPB)) 1118 SET(val, NBSTOP_2); 1119 1120 return val; 1121 } 1122 1123 int 1124 zynquartparam(struct tty *tp, struct termios *t) 1125 { 1126 struct zynquart_softc *sc = 1127 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(tp->t_dev)); 1128 struct zynquart_baudrate_ratio ratio; 1129 uint32_t mcr; 1130 bool change_speed = tp->t_ospeed != t->c_ospeed; 1131 1132 if (ZYNQUART_ISALIVE(sc) == 0) 1133 return (EIO); 1134 1135 /* Check requested parameters. */ 1136 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 1137 return (EINVAL); 1138 1139 /* 1140 * For the console, always force CLOCAL and !HUPCL, so that the port 1141 * is always active. 1142 */ 1143 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 1144 ISSET(sc->sc_hwflags, ZYNQUART_HW_CONSOLE)) { 1145 SET(t->c_cflag, CLOCAL); 1146 CLR(t->c_cflag, HUPCL); 1147 } 1148 1149 /* 1150 * If there were no changes, don't do anything. This avoids dropping 1151 * input and improves performance when all we did was frob things like 1152 * VMIN and VTIME. 1153 */ 1154 if ( !change_speed && tp->t_cflag == t->c_cflag) 1155 return (0); 1156 1157 if (change_speed) { 1158 /* calculate baudrate modulator value */ 1159 if (zynquartspeed(t->c_ospeed, &ratio) < 0) 1160 return (EINVAL); 1161 sc->sc_ratio = ratio; 1162 } 1163 1164 mcr = cflag_to_zynquart(t->c_cflag, sc->sc_mcr); 1165 1166 mutex_spin_enter(&sc->sc_lock); 1167 1168 #if 0 1169 /* flow control stuff. not yet */ 1170 /* 1171 * If we're not in a mode that assumes a connection is present, then 1172 * ignore carrier changes. 1173 */ 1174 if (ISSET(t->c_cflag, CLOCAL | MDMBUF)) 1175 sc->sc_msr_dcd = 0; 1176 else 1177 sc->sc_msr_dcd = MSR_DCD; 1178 /* 1179 * Set the flow control pins depending on the current flow control 1180 * mode. 1181 */ 1182 if (ISSET(t->c_cflag, CRTSCTS)) { 1183 sc->sc_mcr_dtr = MCR_DTR; 1184 sc->sc_mcr_rts = MCR_RTS; 1185 sc->sc_msr_cts = MSR_CTS; 1186 sc->sc_efr = EFR_AUTORTS | EFR_AUTOCTS; 1187 } else if (ISSET(t->c_cflag, MDMBUF)) { 1188 /* 1189 * For DTR/DCD flow control, make sure we don't toggle DTR for 1190 * carrier detection. 1191 */ 1192 sc->sc_mcr_dtr = 0; 1193 sc->sc_mcr_rts = MCR_DTR; 1194 sc->sc_msr_cts = MSR_DCD; 1195 sc->sc_efr = 0; 1196 } else { 1197 /* 1198 * If no flow control, then always set RTS. This will make 1199 * the other side happy if it mistakenly thinks we're doing 1200 * RTS/CTS flow control. 1201 */ 1202 sc->sc_mcr_dtr = MCR_DTR | MCR_RTS; 1203 sc->sc_mcr_rts = 0; 1204 sc->sc_msr_cts = 0; 1205 sc->sc_efr = 0; 1206 if (ISSET(sc->sc_mcr, MCR_DTR)) 1207 SET(sc->sc_mcr, MCR_RTS); 1208 else 1209 CLR(sc->sc_mcr, MCR_RTS); 1210 } 1211 sc->sc_msr_mask = sc->sc_msr_cts | sc->sc_msr_dcd; 1212 #endif 1213 1214 /* And copy to tty. */ 1215 tp->t_ispeed = t->c_ospeed; 1216 tp->t_ospeed = t->c_ospeed; 1217 tp->t_cflag = t->c_cflag; 1218 1219 if (!change_speed && mcr == sc->sc_mcr) { 1220 /* noop */ 1221 } else if (!sc->sc_pending && !sc->sc_tx_busy) { 1222 if (mcr != sc->sc_mcr) { 1223 sc->sc_mcr = mcr; 1224 zynquart_load_params(sc); 1225 } 1226 if (change_speed) 1227 zynquart_load_speed(sc); 1228 } else { 1229 if (!sc->sc_pending) { 1230 sc->sc_heldtbc = sc->sc_tbc; 1231 sc->sc_tbc = 0; 1232 } 1233 sc->sc_pending |= 1234 (mcr == sc->sc_mcr ? 0 : ZYNQUART_PEND_PARAM) | 1235 (change_speed ? 0 : ZYNQUART_PEND_SPEED); 1236 sc->sc_mcr = mcr; 1237 } 1238 1239 if (!ISSET(t->c_cflag, CHWFLOW)) { 1240 /* Disable the high water mark. */ 1241 sc->sc_r_hiwat = 0; 1242 sc->sc_r_lowat = 0; 1243 if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED)) { 1244 CLR(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED); 1245 zynquart_schedrx(sc); 1246 } 1247 if (ISSET(sc->sc_rx_flags, 1248 ZYNQUART_RX_TTY_BLOCKED|ZYNQUART_RX_IBUF_BLOCKED)) { 1249 CLR(sc->sc_rx_flags, 1250 ZYNQUART_RX_TTY_BLOCKED|ZYNQUART_RX_IBUF_BLOCKED); 1251 zynquart_hwiflow(sc); 1252 } 1253 } else { 1254 sc->sc_r_hiwat = zynquart_rbuf_hiwat; 1255 sc->sc_r_lowat = zynquart_rbuf_lowat; 1256 } 1257 1258 mutex_spin_exit(&sc->sc_lock); 1259 1260 /* 1261 * Update the tty layer's idea of the carrier bit, in case we changed 1262 * CLOCAL or MDMBUF. We don't hang up here; we only do that by 1263 * explicit request. 1264 */ 1265 (void) (*tp->t_linesw->l_modem)(tp, ISSET(sc->sc_msr, MODEMSR_DCD)); 1266 1267 #ifdef ZYNQUART_DEBUG 1268 if (zynquart_debug) 1269 zynquartstatus(sc, "zynquartparam "); 1270 #endif 1271 1272 if (!ISSET(t->c_cflag, CHWFLOW)) { 1273 if (sc->sc_tx_stopped) { 1274 sc->sc_tx_stopped = 0; 1275 zynquartstart(tp); 1276 } 1277 } 1278 1279 return (0); 1280 } 1281 1282 void 1283 zynquart_iflush(struct zynquart_softc *sc) 1284 { 1285 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1286 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1287 #ifdef DIAGNOSTIC 1288 uint32_t reg = 0xffff; 1289 #endif 1290 int timo; 1291 1292 timo = 50000; 1293 /* flush any pending I/O */ 1294 while (!ISSET(bus_space_read_4(iot, ioh, UART_CHANNEL_STS), STS_REMPTY) && 1295 --timo) 1296 #ifdef DIAGNOSTIC 1297 reg = 1298 #else 1299 (void) 1300 #endif 1301 bus_space_read_4(iot, ioh, UART_TX_RX_FIFO); 1302 1303 #ifdef DIAGNOSTIC 1304 if (!timo) 1305 aprint_error_dev(sc->sc_dev, "zynquart_iflush timeout %02x\n", reg); 1306 #endif 1307 } 1308 1309 int 1310 zynquarthwiflow(struct tty *tp, int block) 1311 { 1312 struct zynquart_softc *sc = 1313 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(tp->t_dev)); 1314 1315 if (ZYNQUART_ISALIVE(sc) == 0) 1316 return (0); 1317 1318 #ifdef notyet 1319 if (sc->sc_mcr_rts == 0) 1320 return (0); 1321 #endif 1322 1323 mutex_spin_enter(&sc->sc_lock); 1324 1325 if (block) { 1326 if (!ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED)) { 1327 SET(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED); 1328 zynquart_hwiflow(sc); 1329 } 1330 } else { 1331 if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED)) { 1332 CLR(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED); 1333 zynquart_schedrx(sc); 1334 } 1335 if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED)) { 1336 CLR(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED); 1337 zynquart_hwiflow(sc); 1338 } 1339 } 1340 1341 mutex_spin_exit(&sc->sc_lock); 1342 return (1); 1343 } 1344 1345 /* 1346 * (un)block input via hw flowcontrol 1347 */ 1348 void 1349 zynquart_hwiflow(struct zynquart_softc *sc) 1350 { 1351 #ifdef notyet 1352 struct zynquart_regs *regsp= &sc->sc_regs; 1353 1354 if (sc->sc_mcr_rts == 0) 1355 return; 1356 1357 if (ISSET(sc->sc_rx_flags, RX_ANY_BLOCK)) { 1358 CLR(sc->sc_mcr, sc->sc_mcr_rts); 1359 CLR(sc->sc_mcr_active, sc->sc_mcr_rts); 1360 } else { 1361 SET(sc->sc_mcr, sc->sc_mcr_rts); 1362 SET(sc->sc_mcr_active, sc->sc_mcr_rts); 1363 } 1364 UR_WRITE_1(regsp, ZYNQUART_REG_MCR, sc->sc_mcr_active); 1365 #endif 1366 } 1367 1368 1369 void 1370 zynquartstart(struct tty *tp) 1371 { 1372 struct zynquart_softc *sc = 1373 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(tp->t_dev)); 1374 int s; 1375 u_char *tba; 1376 int tbc; 1377 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1378 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1379 1380 if (ZYNQUART_ISALIVE(sc) == 0) 1381 return; 1382 1383 s = spltty(); 1384 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) 1385 goto out; 1386 if (sc->sc_tx_stopped) 1387 goto out; 1388 if (!ttypull(tp)) 1389 goto out; 1390 1391 /* Grab the first contiguous region of buffer space. */ 1392 tba = tp->t_outq.c_cf; 1393 tbc = ndqb(&tp->t_outq, 0); 1394 1395 mutex_spin_enter(&sc->sc_lock); 1396 1397 sc->sc_tba = tba; 1398 sc->sc_tbc = tbc; 1399 1400 SET(tp->t_state, TS_BUSY); 1401 sc->sc_tx_busy = 1; 1402 1403 while (sc->sc_tbc > 0 && 1404 !(bus_space_read_4(iot, ioh, UART_CHANNEL_STS) & STS_TFUL)) { 1405 bus_space_write_4(iot, ioh, UART_TX_RX_FIFO, *sc->sc_tba); 1406 sc->sc_tbc--; 1407 sc->sc_tba++; 1408 } 1409 1410 /* Enable transmit completion interrupts */ 1411 zynquart_control_txint(sc, true); 1412 1413 mutex_spin_exit(&sc->sc_lock); 1414 out: 1415 splx(s); 1416 return; 1417 } 1418 1419 /* 1420 * Stop output on a line. 1421 */ 1422 void 1423 zynquartstop(struct tty *tp, int flag) 1424 { 1425 struct zynquart_softc *sc = 1426 device_lookup_private(&zynquart_cd, ZYNQUART_UNIT(tp->t_dev)); 1427 1428 mutex_spin_enter(&sc->sc_lock); 1429 if (ISSET(tp->t_state, TS_BUSY)) { 1430 /* Stop transmitting at the next chunk. */ 1431 sc->sc_tbc = 0; 1432 sc->sc_heldtbc = 0; 1433 if (!ISSET(tp->t_state, TS_TTSTOP)) 1434 SET(tp->t_state, TS_FLUSH); 1435 } 1436 mutex_spin_exit(&sc->sc_lock); 1437 } 1438 1439 void 1440 zynquartdiag(void *arg) 1441 { 1442 #ifdef notyet 1443 struct zynquart_softc *sc = arg; 1444 int overflows, floods; 1445 1446 mutex_spin_enter(&sc->sc_lock); 1447 overflows = sc->sc_overflows; 1448 sc->sc_overflows = 0; 1449 floods = sc->sc_floods; 1450 sc->sc_floods = 0; 1451 sc->sc_errors = 0; 1452 mutex_spin_exit(&sc->sc_lock); 1453 1454 log(LOG_WARNING, "%s: %d silo overflow%s, %d ibuf flood%s\n", 1455 device_xname(sc->sc_dev), 1456 overflows, overflows == 1 ? "" : "s", 1457 floods, floods == 1 ? "" : "s"); 1458 #endif 1459 } 1460 1461 integrate void 1462 zynquart_rxsoft(struct zynquart_softc *sc, struct tty *tp) 1463 { 1464 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint; 1465 u_int cc, scc, outp; 1466 uint16_t data; 1467 u_int code; 1468 1469 scc = cc = ZYNQUART_RBUF_AVAIL(sc); 1470 1471 #if 0 1472 if (cc == zynquart_rbuf_size-1) { 1473 sc->sc_floods++; 1474 if (sc->sc_errors++ == 0) 1475 callout_reset(&sc->sc_diag_callout, 60 * hz, 1476 zynquartdiag, sc); 1477 } 1478 #endif 1479 1480 /* If not yet open, drop the entire buffer content here */ 1481 if (!ISSET(tp->t_state, TS_ISOPEN)) { 1482 sc->sc_rbuf_out = sc->sc_rbuf_in; 1483 cc = 0; 1484 } 1485 1486 outp = sc->sc_rbuf_out; 1487 1488 #define ERRBITS (INT_PARE|INT_FRAME|INT_ROVR) 1489 1490 while (cc) { 1491 data = sc->sc_rbuf[outp]; 1492 code = data & 0xff; 1493 if (ISSET(__SHIFTOUT(data, ERROR_BITS), ERRBITS)) { 1494 if (sc->sc_errors.err == 0) 1495 callout_reset(&sc->sc_diag_callout, 1496 60 * hz, zynquartdiag, sc); 1497 if (ISSET(__SHIFTOUT(data, ERROR_BITS), INT_ROVR)) 1498 sc->sc_errors.ovrrun++; 1499 if (ISSET(__SHIFTOUT(data, ERROR_BITS), INT_FRAME)) { 1500 sc->sc_errors.frmerr++; 1501 SET(code, TTY_FE); 1502 } 1503 if (ISSET(__SHIFTOUT(data, ERROR_BITS), INT_PARE)) { 1504 sc->sc_errors.prerr++; 1505 SET(code, TTY_PE); 1506 } 1507 } 1508 if ((*rint)(code, tp) == -1) { 1509 /* 1510 * The line discipline's buffer is out of space. 1511 */ 1512 if (!ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_BLOCKED)) { 1513 /* 1514 * We're either not using flow control, or the 1515 * line discipline didn't tell us to block for 1516 * some reason. Either way, we have no way to 1517 * know when there's more space available, so 1518 * just drop the rest of the data. 1519 */ 1520 sc->sc_rbuf_out = sc->sc_rbuf_in; 1521 cc = 0; 1522 } else { 1523 /* 1524 * Don't schedule any more receive processing 1525 * until the line discipline tells us there's 1526 * space available (through zynquarthwiflow()). 1527 * Leave the rest of the data in the input 1528 * buffer. 1529 */ 1530 SET(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED); 1531 } 1532 break; 1533 } 1534 outp = ZYNQUART_RBUF_INC(sc, outp, 1); 1535 cc--; 1536 } 1537 1538 if (cc != scc) { 1539 sc->sc_rbuf_out = outp; 1540 mutex_spin_enter(&sc->sc_lock); 1541 1542 cc = ZYNQUART_RBUF_SPACE(sc); 1543 1544 /* Buffers should be ok again, release possible block. */ 1545 if (cc >= sc->sc_r_lowat) { 1546 if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_IBUF_OVERFLOWED)) { 1547 CLR(sc->sc_rx_flags, ZYNQUART_RX_IBUF_OVERFLOWED); 1548 zynquart_control_rxint(sc, true); 1549 } 1550 if (ISSET(sc->sc_rx_flags, ZYNQUART_RX_IBUF_BLOCKED)) { 1551 CLR(sc->sc_rx_flags, ZYNQUART_RX_IBUF_BLOCKED); 1552 zynquart_hwiflow(sc); 1553 } 1554 } 1555 mutex_spin_exit(&sc->sc_lock); 1556 } 1557 } 1558 1559 integrate void 1560 zynquart_txsoft(struct zynquart_softc *sc, struct tty *tp) 1561 { 1562 1563 CLR(tp->t_state, TS_BUSY); 1564 if (ISSET(tp->t_state, TS_FLUSH)) 1565 CLR(tp->t_state, TS_FLUSH); 1566 else 1567 ndflush(&tp->t_outq, (int)(sc->sc_tba - tp->t_outq.c_cf)); 1568 (*tp->t_linesw->l_start)(tp); 1569 } 1570 1571 integrate void 1572 zynquart_stsoft(struct zynquart_softc *sc, struct tty *tp) 1573 { 1574 #ifdef notyet 1575 u_char msr, delta; 1576 1577 mutex_spin_enter(&sc->sc_lock); 1578 msr = sc->sc_msr; 1579 delta = sc->sc_msr_delta; 1580 sc->sc_msr_delta = 0; 1581 mutex_spin_exit(&sc->sc_lock); 1582 1583 if (ISSET(delta, sc->sc_msr_dcd)) { 1584 /* 1585 * Inform the tty layer that carrier detect changed. 1586 */ 1587 (void) (*tp->t_linesw->l_modem)(tp, ISSET(msr, MSR_DCD)); 1588 } 1589 1590 if (ISSET(delta, sc->sc_msr_cts)) { 1591 /* Block or unblock output according to flow control. */ 1592 if (ISSET(msr, sc->sc_msr_cts)) { 1593 sc->sc_tx_stopped = 0; 1594 (*tp->t_linesw->l_start)(tp); 1595 } else { 1596 sc->sc_tx_stopped = 1; 1597 } 1598 } 1599 1600 #endif 1601 #ifdef ZYNQUART_DEBUG 1602 if (zynquart_debug) 1603 zynquartstatus(sc, "zynquart_stsoft"); 1604 #endif 1605 } 1606 1607 void 1608 zynquartsoft(void *arg) 1609 { 1610 struct zynquart_softc *sc = arg; 1611 struct tty *tp; 1612 1613 if (ZYNQUART_ISALIVE(sc) == 0) 1614 return; 1615 1616 tp = sc->sc_tty; 1617 1618 if (sc->sc_rx_ready) { 1619 sc->sc_rx_ready = 0; 1620 zynquart_rxsoft(sc, tp); 1621 } 1622 1623 if (sc->sc_st_check) { 1624 sc->sc_st_check = 0; 1625 zynquart_stsoft(sc, tp); 1626 } 1627 1628 if (sc->sc_tx_done) { 1629 sc->sc_tx_done = 0; 1630 zynquart_txsoft(sc, tp); 1631 } 1632 } 1633 1634 int 1635 zynquartintr(void *arg) 1636 { 1637 struct zynquart_softc *sc = arg; 1638 uint32_t sts; 1639 uint32_t int_sts; 1640 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1641 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1642 1643 if (ZYNQUART_ISALIVE(sc) == 0) 1644 return (0); 1645 1646 mutex_spin_enter(&sc->sc_lock); 1647 1648 int_sts = bus_space_read_4(iot, ioh, UART_CHNL_INT_STS); 1649 do { 1650 sts = bus_space_read_4(iot, ioh, UART_CHANNEL_STS); 1651 if (!(sts & STS_REMPTY)) 1652 zynquartintr_read(sc); 1653 } while (!(sts & STS_REMPTY)); 1654 1655 if (sts & STS_TEMPTY) 1656 zynquartintr_send(sc); 1657 1658 bus_space_write_4(iot, ioh, UART_CHNL_INT_STS, int_sts); 1659 1660 mutex_spin_exit(&sc->sc_lock); 1661 1662 /* Wake up the poller. */ 1663 softint_schedule(sc->sc_si); 1664 1665 #ifdef RND_COM 1666 rnd_add_uint32(&sc->rnd_source, iir | lsr); 1667 #endif 1668 1669 return (1); 1670 } 1671 1672 1673 /* 1674 * called when there is least one character in rxfifo 1675 * 1676 */ 1677 1678 static void 1679 zynquartintr_read(struct zynquart_softc *sc) 1680 { 1681 int cc; 1682 uint16_t rd; 1683 uint32_t sts; 1684 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1685 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1686 1687 cc = ZYNQUART_RBUF_SPACE(sc); 1688 1689 /* clear aging timer interrupt */ 1690 bus_space_write_4(iot, ioh, UART_CHNL_INT_STS, INT_TIMEOUT); 1691 1692 while (cc > 0) { 1693 sc->sc_rbuf[sc->sc_rbuf_in] = rd = 1694 bus_space_read_4(iot, ioh, UART_TX_RX_FIFO); 1695 1696 if (! cn_check_magic(sc->sc_tty->t_dev, rd & 0xff, 1697 &zynquart_cnm_state)) { 1698 sc->sc_rbuf_in = 1699 ZYNQUART_RBUF_INC(sc, sc->sc_rbuf_in, 1); 1700 cc--; 1701 } 1702 1703 sts = bus_space_read_4(iot, ioh, UART_CHANNEL_STS); 1704 if (sts & STS_REMPTY) 1705 break; 1706 } 1707 1708 /* 1709 * Current string of incoming characters ended because 1710 * no more data was available or we ran out of space. 1711 * Schedule a receive event if any data was received. 1712 * If we're out of space, turn off receive interrupts. 1713 */ 1714 if (!ISSET(sc->sc_rx_flags, ZYNQUART_RX_TTY_OVERFLOWED)) 1715 sc->sc_rx_ready = 1; 1716 /* 1717 * See if we are in danger of overflowing a buffer. If 1718 * so, use hardware flow control to ease the pressure. 1719 */ 1720 if (!ISSET(sc->sc_rx_flags, ZYNQUART_RX_IBUF_BLOCKED) && 1721 cc < sc->sc_r_hiwat) { 1722 sc->sc_rx_flags |= ZYNQUART_RX_IBUF_BLOCKED; 1723 zynquart_hwiflow(sc); 1724 } 1725 1726 /* 1727 * If we're out of space, disable receive interrupts 1728 * until the queue has drained a bit. 1729 */ 1730 if (!cc) { 1731 sc->sc_rx_flags |= ZYNQUART_RX_IBUF_OVERFLOWED; 1732 zynquart_control_rxint(sc, false); 1733 } 1734 } 1735 1736 void 1737 zynquartintr_send(struct zynquart_softc *sc) 1738 { 1739 uint32_t sts; 1740 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1741 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1742 1743 sts = bus_space_read_4(iot, ioh, UART_CHANNEL_STS); 1744 1745 if (sc->sc_pending) { 1746 if (sts & STS_TEMPTY) { 1747 zynquart_load_pendings(sc); 1748 sc->sc_tbc = sc->sc_heldtbc; 1749 sc->sc_heldtbc = 0; 1750 } else { 1751 /* wait for TX fifo empty */ 1752 zynquart_control_txint(sc, true); 1753 return; 1754 } 1755 } 1756 1757 while (sc->sc_tbc > 0 && 1758 !(bus_space_read_4(iot, ioh, UART_CHANNEL_STS) & STS_TFUL)) { 1759 bus_space_write_4(iot, ioh, UART_TX_RX_FIFO, *sc->sc_tba); 1760 sc->sc_tbc--; 1761 sc->sc_tba++; 1762 } 1763 1764 if (sc->sc_tbc > 0) 1765 zynquart_control_txint(sc, true); 1766 else { 1767 /* no more chars to send. 1768 we don't need tx interrupt any more. */ 1769 zynquart_control_txint(sc, false); 1770 if (sc->sc_tx_busy) { 1771 sc->sc_tx_busy = 0; 1772 sc->sc_tx_done = 1; 1773 } 1774 } 1775 } 1776 1777 static void 1778 zynquart_disable_all_interrupts(struct zynquart_softc *sc) 1779 { 1780 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1781 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1782 1783 bus_space_write_4(iot, ioh, UART_INTRPT_DIS, 0xffffffff); 1784 } 1785 1786 static void 1787 zynquart_control_rxint(struct zynquart_softc *sc, bool enable) 1788 { 1789 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1790 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1791 uint32_t mask = INT_TIMEOUT | INT_PARE | INT_FRAME | INT_ROVR | INT_RFUL | INT_RTRIG; 1792 uint32_t sts; 1793 1794 /* clear */ 1795 sts = bus_space_read_4(iot, ioh, UART_CHNL_INT_STS); 1796 bus_space_write_4(iot, ioh, UART_CHNL_INT_STS, sts); 1797 1798 if (enable) 1799 bus_space_write_4(iot, ioh, UART_INTRPT_EN, mask); 1800 else 1801 bus_space_write_4(iot, ioh, UART_INTRPT_DIS, mask); 1802 } 1803 1804 static void 1805 zynquart_control_txint(struct zynquart_softc *sc, bool enable) 1806 { 1807 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1808 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1809 uint32_t mask = INT_TEMPTY; 1810 1811 if (enable) 1812 bus_space_write_4(iot, ioh, UART_INTRPT_EN, mask); 1813 else 1814 bus_space_write_4(iot, ioh, UART_INTRPT_DIS, mask); 1815 } 1816 1817 1818 static void 1819 zynquart_load_params(struct zynquart_softc *sc) 1820 { 1821 bus_space_tag_t iot = sc->sc_regs.ur_iot; 1822 bus_space_handle_t ioh = sc->sc_regs.ur_ioh; 1823 1824 bus_space_write_4(iot, ioh, UART_MODE, sc->sc_mcr); 1825 } 1826 1827 static void 1828 zynquart_load_speed(struct zynquart_softc *sc) 1829 { 1830 /* bus_space_tag_t iot = sc->sc_regs.ur_iot; */ 1831 /* bus_space_handle_t ioh = sc->sc_regs.ur_ioh; */ 1832 1833 /* XXX */ 1834 } 1835 1836 1837 static void 1838 zynquart_load_pendings(struct zynquart_softc *sc) 1839 { 1840 if (sc->sc_pending & ZYNQUART_PEND_PARAM) 1841 zynquart_load_params(sc); 1842 if (sc->sc_pending & ZYNQUART_PEND_SPEED) 1843 zynquart_load_speed(sc); 1844 sc->sc_pending = 0; 1845 } 1846 1847 /* 1848 * The following functions are polled getc and putc routines, shared 1849 * by the console and kgdb glue. 1850 * 1851 * The read-ahead code is so that you can detect pending in-band 1852 * cn_magic in polled mode while doing output rather than having to 1853 * wait until the kernel decides it needs input. 1854 */ 1855 1856 #define READAHEAD_RING_LEN 16 1857 static int zynquart_readahead[READAHEAD_RING_LEN]; 1858 static int zynquart_readahead_in = 0; 1859 static int zynquart_readahead_out = 0; 1860 #define READAHEAD_IS_EMPTY() (zynquart_readahead_in==zynquart_readahead_out) 1861 #define READAHEAD_IS_FULL() \ 1862 (((zynquart_readahead_in+1) & (READAHEAD_RING_LEN-1)) ==zynquart_readahead_out) 1863 1864 int 1865 zynquart_common_getc(dev_t dev, struct zynquart_regs *regsp) 1866 { 1867 int s = splserial(); 1868 u_char c; 1869 bus_space_tag_t iot = regsp->ur_iot; 1870 bus_space_handle_t ioh = regsp->ur_ioh; 1871 uint32_t sts; 1872 1873 /* got a character from reading things earlier */ 1874 if (zynquart_readahead_in != zynquart_readahead_out) { 1875 1876 c = zynquart_readahead[zynquart_readahead_out]; 1877 zynquart_readahead_out = (zynquart_readahead_out + 1) & 1878 (READAHEAD_RING_LEN-1); 1879 splx(s); 1880 return (c); 1881 } 1882 1883 /* block until a character becomes available */ 1884 while ((sts = bus_space_read_4(iot, ioh, UART_CHANNEL_STS)) 1885 & STS_REMPTY) 1886 continue; 1887 1888 c = 0xff & bus_space_read_4(iot, ioh, UART_TX_RX_FIFO); 1889 1890 cn_check_magic(dev, c, &zynquart_cnm_state); 1891 splx(s); 1892 return (c); 1893 } 1894 1895 void 1896 zynquart_common_putc(dev_t dev, struct zynquart_regs *regsp, int c) 1897 { 1898 int s = splserial(); 1899 int cin, timo; 1900 bus_space_tag_t iot = regsp->ur_iot; 1901 bus_space_handle_t ioh = regsp->ur_ioh; 1902 1903 if (!READAHEAD_IS_FULL() && 1904 !(bus_space_read_4(iot, ioh, UART_CHANNEL_STS) & STS_REMPTY)) { 1905 cin = bus_space_read_4(iot, ioh, UART_TX_RX_FIFO); 1906 cn_check_magic(dev, cin & 0xff, &zynquart_cnm_state); 1907 zynquart_readahead_in = (zynquart_readahead_in + 1) & 1908 (READAHEAD_RING_LEN-1); 1909 } 1910 1911 /* wait for any pending transmission to finish */ 1912 timo = 150000; 1913 do { 1914 if (!(bus_space_read_4(iot, ioh, UART_CHANNEL_STS) & STS_TFUL)) { 1915 bus_space_write_4(iot, ioh, UART_TX_RX_FIFO, c); 1916 break; 1917 } 1918 } while(--timo > 0); 1919 1920 ZYNQUART_BARRIER(regsp, BR | BW); 1921 1922 splx(s); 1923 } 1924 1925 /* 1926 * Initialize UART for use as console or KGDB line. 1927 */ 1928 int 1929 zynquart_init(struct zynquart_regs *regsp, int rate, tcflag_t cflag) 1930 { 1931 struct zynquart_baudrate_ratio ratio; 1932 1933 if (bus_space_map(regsp->ur_iot, regsp->ur_iobase, UART_SIZE, 0, 1934 ®sp->ur_ioh)) 1935 return ENOMEM; /* ??? */ 1936 1937 if (zynquartspeed(rate, &ratio) < 0) 1938 return EINVAL; 1939 1940 /* clear status registers */ 1941 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, UART_CHNL_INT_STS, 0xffff); 1942 bus_space_write_4(regsp->ur_iot, regsp->ur_ioh, UART_CHANNEL_STS, 0xffff); 1943 1944 return (0); 1945 } 1946 1947 1948 1949 /* 1950 * Following are all routines needed for UART to act as console 1951 */ 1952 struct consdev zynquartcons = { 1953 .cn_getc = zynquartcngetc, 1954 .cn_putc = zynquartcnputc, 1955 .cn_pollc = nullcnpollc, 1956 .cn_dev = NODEV, 1957 .cn_pri = CN_NORMAL, 1958 }; 1959 1960 1961 int 1962 zynquart_cons_attach(bus_space_tag_t iot, paddr_t iobase, u_int rate, 1963 tcflag_t cflag) 1964 { 1965 struct zynquart_regs regs; 1966 int res; 1967 1968 regs.ur_iot = iot; 1969 regs.ur_iobase = iobase; 1970 1971 res = zynquart_init(®s, rate, cflag); 1972 if (res) 1973 return (res); 1974 1975 cn_tab = &zynquartcons; 1976 cn_init_magic(&zynquart_cnm_state); 1977 cn_set_magic("\047\001"); /* default magic is BREAK */ 1978 1979 zynquartconsrate = rate; 1980 zynquartconscflag = cflag; 1981 1982 zynquartconsregs = regs; 1983 1984 return 0; 1985 } 1986 1987 int 1988 zynquartcngetc(dev_t dev) 1989 { 1990 return (zynquart_common_getc(dev, &zynquartconsregs)); 1991 } 1992 1993 /* 1994 * Console kernel output character routine. 1995 */ 1996 void 1997 zynquartcnputc(dev_t dev, int c) 1998 { 1999 zynquart_common_putc(dev, &zynquartconsregs, c); 2000 } 2001 2002 #ifdef KGDB 2003 int 2004 zynquart_kgdb_attach(bus_space_tag_t iot, paddr_t iobase, u_int rate, 2005 tcflag_t cflag) 2006 { 2007 int res; 2008 2009 if (iot == zynquartconsregs.ur_iot && 2010 iobase == zynquartconsregs.ur_iobase) { 2011 #if !defined(DDB) 2012 return (EBUSY); /* cannot share with console */ 2013 #else 2014 zynquart_kgdb_regs.ur_iot = iot; 2015 zynquart_kgdb_regs.ur_ioh = zynquartconsregs.ur_ioh; 2016 zynquart_kgdb_regs.ur_iobase = iobase; 2017 #endif 2018 } else { 2019 zynquart_kgdb_regs.ur_iot = iot; 2020 zynquart_kgdb_regs.ur_iobase = iobase; 2021 2022 res = zynquart_init(&zynquart_kgdb_regs, rate, cflag); 2023 if (res) 2024 return (res); 2025 2026 /* 2027 * XXXfvdl this shouldn't be needed, but the cn_magic goo 2028 * expects this to be initialized 2029 */ 2030 cn_init_magic(&zynquart_cnm_state); 2031 cn_set_magic("\047\001"); 2032 } 2033 2034 kgdb_attach(zynquart_kgdb_getc, zynquart_kgdb_putc, &zynquart_kgdb_regs); 2035 kgdb_dev = 123; /* unneeded, only to satisfy some tests */ 2036 2037 return (0); 2038 } 2039 2040 /* ARGSUSED */ 2041 int 2042 zynquart_kgdb_getc(void *arg) 2043 { 2044 struct zynquart_regs *regs = arg; 2045 2046 return (zynquart_common_getc(NODEV, regs)); 2047 } 2048 2049 /* ARGSUSED */ 2050 void 2051 zynquart_kgdb_putc(void *arg, int c) 2052 { 2053 struct zynquart_regs *regs = arg; 2054 2055 zynquart_common_putc(NODEV, regs, c); 2056 } 2057 #endif /* KGDB */ 2058 2059 /* helper function to identify the zynquart ports used by 2060 console or KGDB (and not yet autoconf attached) */ 2061 int 2062 zynquart_is_console(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t *ioh) 2063 { 2064 bus_space_handle_t help; 2065 2066 if (!zynquartconsattached && 2067 iot == zynquartconsregs.ur_iot && iobase == zynquartconsregs.ur_iobase) 2068 help = zynquartconsregs.ur_ioh; 2069 #ifdef KGDB 2070 else if (!zynquart_kgdb_attached && 2071 iot == zynquart_kgdb_regs.ur_iot && iobase == zynquart_kgdb_regs.ur_iobase) 2072 help = zynquart_kgdb_regs.ur_ioh; 2073 #endif 2074 else 2075 return (0); 2076 2077 if (ioh) 2078 *ioh = help; 2079 return (1); 2080 } 2081 2082 #ifdef notyet 2083 2084 bool 2085 zynquart_cleanup(device_t self, int how) 2086 { 2087 /* 2088 * this routine exists to serve as a shutdown hook for systems that 2089 * have firmware which doesn't interact properly with a zynquart device in 2090 * FIFO mode. 2091 */ 2092 struct zynquart_softc *sc = device_private(self); 2093 2094 if (ISSET(sc->sc_hwflags, ZYNQUART_HW_FIFO)) 2095 UR_WRITE_1(&sc->sc_regs, ZYNQUART_REG_FIFO, 0); 2096 2097 return true; 2098 } 2099 #endif 2100 2101 #ifdef notyet 2102 bool 2103 zynquart_suspend(device_t self PMF_FN_ARGS) 2104 { 2105 struct zynquart_softc *sc = device_private(self); 2106 2107 UR_WRITE_1(&sc->sc_regs, ZYNQUART_REG_IER, 0); 2108 (void)CSR_READ_1(&sc->sc_regs, ZYNQUART_REG_IIR); 2109 2110 return true; 2111 } 2112 #endif 2113 2114 #ifdef notyet 2115 bool 2116 zynquart_resume(device_t self PMF_FN_ARGS) 2117 { 2118 struct zynquart_softc *sc = device_private(self); 2119 2120 mutex_spin_enter(&sc->sc_lock); 2121 zynquart_loadchannelregs(sc); 2122 mutex_spin_exit(&sc->sc_lock); 2123 2124 return true; 2125 } 2126 #endif 2127 2128 static void 2129 zynquart_enable_debugport(struct zynquart_softc *sc) 2130 { 2131 /* bus_space_tag_t iot = sc->sc_regs.ur_iot; */ 2132 /* bus_space_handle_t ioh = sc->sc_regs.ur_ioh; */ 2133 } 2134 2135 2136 void 2137 zynquart_set_frequency(u_int freq, u_int div) 2138 { 2139 zynquart_freq = freq; 2140 zynquart_freqdiv = div; 2141 } 2142