1 /* $Id: at91usart.c,v 1.15 2026/11/07 22:20:10 thorpej Exp $ */ 2 /* $NetBSD: at91usart.c,v 1.15 2026/11/07 22:20:10 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 2007 Embedtronics Oy. All rights reserved. 6 * 7 * Copyright (c) 1998, 1999, 2001, 2002, 2004 The NetBSD Foundation, Inc. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to The NetBSD Foundation 11 * by Jesse Off 12 * 13 * This code is derived from software contributed to The NetBSD Foundation 14 * by Ichiro FUKUHARA and Naoto Shimazaki. 15 * 16 * This code is derived from software contributed to The NetBSD Foundation 17 * by IWAMOTO Toshihiro. 18 * 19 * This code is derived from software contributed to The NetBSD Foundation 20 * by Charles M. Hannum. 21 * 22 * Redistribution and use in source and binary forms, with or without 23 * modification, are permitted provided that the following conditions 24 * are met: 25 * 1. Redistributions of source code must retain the above copyright 26 * notice, this list of conditions and the following disclaimer. 27 * 2. Redistributions in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in the 29 * documentation and/or other materials provided with the distribution. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 32 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 33 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 34 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 35 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 36 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 37 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 38 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 39 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 41 * POSSIBILITY OF SUCH DAMAGE. 42 */ 43 44 /* 45 * Copyright (c) 1991 The Regents of the University of California. 46 * All rights reserved. 47 * 48 * Redistribution and use in source and binary forms, with or without 49 * modification, are permitted provided that the following conditions 50 * are met: 51 * 1. Redistributions of source code must retain the above copyright 52 * notice, this list of conditions and the following disclaimer. 53 * 2. Redistributions in binary form must reproduce the above copyright 54 * notice, this list of conditions and the following disclaimer in the 55 * documentation and/or other materials provided with the distribution. 56 * 3. Neither the name of the University nor the names of its contributors 57 * may be used to endorse or promote products derived from this software 58 * without specific prior written permission. 59 * 60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 70 * SUCH DAMAGE. 71 * 72 * @(#)com.c 7.5 (Berkeley) 5/16/91 73 */ 74 75 /* 76 * TODO: hardware flow control 77 */ 78 79 #include <sys/cdefs.h> 80 __KERNEL_RCSID(0, "$NetBSD: at91usart.c,v 1.15 2026/11/07 22:20:10 thorpej Exp $"); 81 82 #include "opt_kgdb.h" 83 84 #ifdef RND_COM 85 #include <sys/rndsource.h> 86 #endif 87 88 #ifdef NOTYET 89 /* 90 * Override cnmagic(9) macro before including <sys/systm.h>. 91 * We need to know if cn_check_magic triggered debugger, so set a flag. 92 * Callers of cn_check_magic must declare int cn_trapped = 0; 93 * XXX: this is *ugly*! 94 */ 95 #define cn_trap() \ 96 do { \ 97 console_debugger(); \ 98 cn_trapped = 1; \ 99 } while (/* CONSTCOND */ 0) 100 #endif /* NOTYET */ 101 102 103 #include <sys/param.h> 104 #include <sys/systm.h> 105 #include <sys/types.h> 106 #include <sys/conf.h> 107 #include <sys/file.h> 108 #include <sys/device.h> 109 #include <sys/kernel.h> 110 #include <sys/tty.h> 111 #include <sys/uio.h> 112 #include <sys/vnode.h> 113 #include <sys/kauth.h> 114 115 #include <machine/intr.h> 116 #include <sys/bus.h> 117 118 #include <arm/at91/at91reg.h> 119 #include <arm/at91/at91var.h> 120 #include <arm/at91/at91usartreg.h> 121 #include <arm/at91/at91usartvar.h> 122 123 #include <dev/cons.h> 124 125 static int at91usart_param(struct tty *, struct termios *); 126 static void at91usart_start(struct tty *); 127 static int at91usart_hwiflow(struct tty *, int); 128 129 #if 0 130 static u_int cflag2lcrhi(tcflag_t); 131 #endif 132 static void at91usart_set(struct at91usart_softc *); 133 134 #if NOTYET 135 int at91usart_cn_getc(dev_t); 136 void at91usart_cn_putc(dev_t, int); 137 void at91usart_cn_pollc(dev_t, int); 138 void at91usart_cn_probe(struct consdev *); 139 void at91usart_cn_init(struct consdev *); 140 141 static struct at91usart_cons_softc { 142 bus_space_tag_t sc_iot; 143 bus_space_handle_t sc_ioh; 144 bus_addr_t sc_hwbase; 145 int sc_ospeed; 146 tcflag_t sc_cflag; 147 int sc_attached; 148 149 uint8_t *sc_rx_ptr; 150 uint8_t sc_rx_fifo[64]; 151 } usart_cn_sc; 152 153 static struct cnm_state at91usart_cnm_state; 154 #endif /* NOTYET */ 155 156 static void at91usart_soft(void* arg); 157 inline static void at91usart_txsoft(struct at91usart_softc *, struct tty *); 158 inline static void at91usart_rxsoft(struct at91usart_softc *, struct tty *, unsigned csr); 159 160 #define PDC_BLOCK_SIZE 64 161 162 //CFATTACH_DECL_NEW(at91usart, sizeof(struct at91usart_softc), 163 // at91usart_match, at91usart_attach, NULL, NULL); 164 165 //#define USART_DEBUG 10 166 167 #ifdef USART_DEBUG 168 int usart_debug = USART_DEBUG; 169 #define DPRINTFN(n,fmt) if (usart_debug >= (n)) printf fmt 170 #else 171 #define DPRINTFN(n,fmt) 172 #endif 173 174 extern struct cfdriver at91usart_cd; 175 176 dev_type_open(at91usart_open); 177 dev_type_close(at91usart_close); 178 dev_type_read(at91usart_read); 179 dev_type_write(at91usart_write); 180 dev_type_ioctl(at91usart_ioctl); 181 dev_type_stop(at91usart_stop); 182 dev_type_tty(at91usart_tty); 183 dev_type_poll(at91usart_poll); 184 185 const struct cdevsw at91usart_cdevsw = { 186 .d_open = at91usart_open, 187 .d_close = at91usart_close, 188 .d_read = at91usart_read, 189 .d_write = at91usart_write, 190 .d_ioctl = at91usart_ioctl, 191 .d_stop = at91usart_stop, 192 .d_tty = at91usart_tty, 193 .d_poll = at91usart_poll, 194 .d_mmap = nommap, 195 .d_kqfilter = ttykqfilter, 196 .d_discard = nodiscard, 197 .d_flag = D_TTY 198 }; 199 200 #if NOTYET 201 struct consdev at91usart_cons = { 202 at91usart_cn_probe, NULL, at91usart_cn_getc, at91usart_cn_putc, at91usart_cn_pollc, NULL, 203 NULL, NULL, NODEV, CN_REMOTE 204 }; 205 #endif /* NOTYET */ 206 207 #ifndef DEFAULT_COMSPEED 208 #define DEFAULT_COMSPEED 115200 209 #endif 210 211 #define COMUNIT(x) TTUNIT(x) 212 #define COMDIALOUT(x) TTDIALOUT(x) 213 214 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && device_is_active((sc)->sc_dev)) 215 216 static inline void 217 at91usart_writereg(struct at91usart_softc *sc, int reg, u_int val) 218 { 219 bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, val); 220 } 221 222 static inline u_int 223 at91usart_readreg(struct at91usart_softc *sc, int reg) 224 { 225 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg); 226 } 227 #if 0 228 static int 229 at91usart_match(device_t parent, cfdata_t cf, void *aux) 230 { 231 if (strcmp(cf->cf_name, "at91usart") == 0) 232 return 1; 233 return 0; 234 } 235 #endif 236 static int at91usart_intr(void* arg); 237 238 void 239 at91usart_attach_subr(struct at91usart_softc *sc, struct at91bus_attach_args *sa) 240 { 241 struct tty *tp; 242 int err; 243 244 printf("\n"); 245 246 if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh)) 247 panic("%s: Cannot map registers", device_xname(sc->sc_dev)); 248 249 sc->sc_iot = sa->sa_iot; 250 sc->sc_hwbase = sa->sa_addr; 251 sc->sc_dmat = sa->sa_dmat; 252 sc->sc_pid = sa->sa_pid; 253 254 /* allocate fifos */ 255 err = at91pdc_alloc_fifo(sc->sc_dmat, &sc->sc_rx_fifo, AT91USART_RING_SIZE, BUS_DMA_READ | BUS_DMA_STREAMING); 256 if (err) 257 panic("%s: cannot allocate rx fifo", device_xname(sc->sc_dev)); 258 259 err = at91pdc_alloc_fifo(sc->sc_dmat, &sc->sc_tx_fifo, AT91USART_RING_SIZE, BUS_DMA_WRITE | BUS_DMA_STREAMING); 260 if (err) 261 panic("%s: cannot allocate tx fifo", device_xname(sc->sc_dev)); 262 263 /* initialize uart */ 264 at91_peripheral_clock(sc->sc_pid, 1); 265 266 at91usart_writereg(sc, US_IDR, -1); 267 at91usart_writereg(sc, US_RTOR, 12); // 12-bit timeout 268 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS); 269 at91_intr_establish(sa->sa_pid, IPL_TTY, INTR_HIGH_LEVEL, at91usart_intr, sc); 270 USART_INIT(sc, 115200U); 271 272 #ifdef NOTYET 273 if (sc->sc_iot == usart_cn_sc.sc_iot 274 && sc->sc_hwbase == usart_cn_sc.sc_hwbase) { 275 usart_cn_sc.sc_attached = 1; 276 /* Make sure the console is always "hardwired". */ 277 delay(10000); /* wait for output to finish */ 278 SET(sc->sc_hwflags, COM_HW_CONSOLE); 279 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 280 SET(sc->sc_ier, USART_INT_RXRDY); 281 USARTREG(USART_IER) = USART_INT_RXRDY; // @@@@@ 282 } 283 #endif // NOTYET 284 285 tp = tty_alloc(); 286 tp->t_oproc = at91usart_start; 287 tp->t_param = at91usart_param; 288 tp->t_hwiflow = at91usart_hwiflow; 289 290 sc->sc_tty = tp; 291 292 tty_attach(tp); 293 294 #if NOTYET 295 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 296 int maj; 297 298 /* locate the major number */ 299 maj = cdevsw_lookup_major(&at91usart_cdevsw); 300 301 cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev)); 302 303 aprint_normal("%s: console (maj %u min %u cn_dev %u)\n", 304 device_xname(sc->sc_dev), maj, device_unit(sc->sc_dev), 305 cn_tab->cn_dev); 306 } 307 #endif /* NOTYET */ 308 309 sc->sc_si = softint_establish(SOFTINT_SERIAL, at91usart_soft, sc); 310 311 #ifdef RND_COM 312 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev), 313 RND_TYPE_TTY, RND_FLAG_DEFAULT); 314 #endif 315 316 /* if there are no enable/disable functions, assume the device 317 is always enabled */ 318 if (!sc->enable) 319 sc->enabled = 1; 320 321 /* XXX configure register */ 322 /* xxx_config(sc) */ 323 324 SET(sc->sc_hwflags, COM_HW_DEV_OK); 325 } 326 327 static int 328 at91usart_param(struct tty *tp, struct termios *t) 329 { 330 struct at91usart_softc *sc 331 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev)); 332 int s; 333 334 if (COM_ISALIVE(sc) == 0) 335 return (EIO); 336 337 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 338 return (EINVAL); 339 340 /* 341 * For the console, always force CLOCAL and !HUPCL, so that the port 342 * is always active. 343 */ 344 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 345 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 346 SET(t->c_cflag, CLOCAL); 347 CLR(t->c_cflag, HUPCL); 348 } 349 350 /* 351 * If there were no changes, don't do anything. This avoids dropping 352 * input and improves performance when all we did was frob things like 353 * VMIN and VTIME. 354 */ 355 if (tp->t_ospeed == t->c_ospeed && 356 tp->t_cflag == t->c_cflag) 357 return (0); 358 359 s = spltty(); 360 361 sc->sc_brgr = (AT91_MSTCLK / 16 + t->c_ospeed / 2) / t->c_ospeed; 362 363 /* And copy to tty. */ 364 tp->t_ispeed = 0; 365 tp->t_ospeed = t->c_ospeed; 366 tp->t_cflag = t->c_cflag; 367 at91usart_set(sc); 368 369 splx(s); 370 371 /* 372 * Update the tty layer's idea of the carrier bit. 373 * We tell tty the carrier is always on. 374 */ 375 (void) (*tp->t_linesw->l_modem)(tp, 1); 376 377 #ifdef COM_DEBUG 378 if (com_debug) 379 comstatus(sc, "comparam "); 380 #endif 381 382 /* tell the upper layer about hwflow.. */ 383 if (sc->hwflow) 384 (*sc->hwflow)(sc, t->c_cflag); 385 386 return (0); 387 } 388 389 static int 390 at91usart_hwiflow(struct tty *tp, int block) 391 { 392 if (block) { 393 /* tty discipline wants to block */ 394 } else { 395 /* tty discipline wants to unblock */ 396 } 397 return (0); 398 } 399 400 static __inline void 401 at91usart_start_tx(struct at91usart_softc *sc) 402 { 403 if (!sc->start_tx) 404 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTEN); 405 else 406 (*sc->start_tx)(sc); 407 } 408 409 static __inline void 410 at91usart_stop_tx(struct at91usart_softc *sc) 411 { 412 if (!sc->stop_tx) 413 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS); 414 else 415 (*sc->stop_tx)(sc); 416 } 417 418 static __inline void 419 at91usart_rx_started(struct at91usart_softc *sc) 420 { 421 if (sc->rx_started) 422 (*sc->rx_started)(sc); 423 } 424 425 static __inline void 426 at91usart_rx_stopped(struct at91usart_softc *sc) 427 { 428 if (sc->rx_stopped) 429 (*sc->rx_stopped)(sc); 430 } 431 432 static __inline void 433 at91usart_rx_rts_ctl(struct at91usart_softc *sc, int enabled) 434 { 435 if (sc->rx_rts_ctl) 436 (*sc->rx_rts_ctl)(sc, enabled); 437 } 438 439 static void 440 at91usart_filltx(struct at91usart_softc *sc) 441 { 442 struct tty *tp = sc->sc_tty; 443 int len; 444 void *dst; 445 446 // post write handler 447 AT91PDC_FIFO_POSTWRITE(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, 448 &sc->sc_tx_fifo); 449 450 // copy more data to fifo: 451 if (sc->sc_tbc > 0 452 && (dst = AT91PDC_FIFO_WRPTR(&sc->sc_tx_fifo, &len)) != NULL) { 453 // copy data to fifo 454 if (len > sc->sc_tbc) 455 len = sc->sc_tbc; 456 memcpy(dst, sc->sc_tba, len); 457 sc->sc_tba += len; 458 if ((sc->sc_tbc -= len) <= 0) 459 CLR(tp->t_state, TS_BUSY); 460 // update fifo 461 AT91PDC_FIFO_WRITTEN(&sc->sc_tx_fifo, len); 462 // tell tty interface we've sent some bytes 463 ndflush(&tp->t_outq, len); 464 } 465 466 // start sending data... 467 if (AT91PDC_FIFO_PREWRITE(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, 468 US_PDC, &sc->sc_tx_fifo, PDC_BLOCK_SIZE)) { 469 at91usart_start_tx(sc); 470 SET(sc->sc_ier, US_CSR_TXEMPTY | US_CSR_ENDTX); 471 } else { 472 CLR(sc->sc_ier, US_CSR_ENDTX); 473 } 474 } 475 476 static void 477 at91usart_start(struct tty *tp) 478 { 479 struct at91usart_softc *sc 480 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev)); 481 int s; 482 483 if (COM_ISALIVE(sc) == 0) { 484 DPRINTFN(5, ("%s: %s / COM_ISALIVE == 0\n", device_xname(sc->sc_dev), __FUNCTION__)); 485 return; 486 } 487 488 s = spltty(); 489 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) { 490 DPRINTFN(5, ("%s: %s: TS_BUSY || TS_TIMEOUT || TS_TTSTOP\n", device_xname(sc->sc_dev), __FUNCTION__)); 491 goto out; 492 } 493 494 if (!ttypull(tp)) 495 goto out; 496 497 /* Grab the first contiguous region of buffer space. */ 498 { 499 u_char *tba; 500 int tbc; 501 502 tba = tp->t_outq.c_cf; 503 tbc = ndqb(&tp->t_outq, 0); 504 505 sc->sc_tba = tba; 506 sc->sc_tbc = tbc; 507 } 508 509 SET(tp->t_state, TS_BUSY); 510 511 /* Output the first chunk of the contiguous buffer. */ 512 at91usart_filltx(sc); 513 at91usart_writereg(sc, US_IER, sc->sc_ier); 514 DPRINTFN(5, ("%s: %s, ier=%08x (csr=%08x)\n", device_xname(sc->sc_dev), __FUNCTION__, sc->sc_ier, at91usart_readreg(sc, US_CSR))); 515 516 out: 517 splx(s); 518 519 return; 520 } 521 522 static __inline__ void 523 at91usart_break(struct at91usart_softc *sc, int onoff) 524 { 525 at91usart_writereg(sc, US_CR, onoff ? US_CR_STTBRK : US_CR_STPBRK); 526 } 527 528 static void 529 at91usart_shutdown(struct at91usart_softc *sc) 530 { 531 int s; 532 533 s = spltty(); 534 535 /* turn of dma */ 536 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS); 537 at91usart_writereg(sc, US_PDC + PDC_TNCR, 0); 538 at91usart_writereg(sc, US_PDC + PDC_TCR, 0); 539 at91usart_writereg(sc, US_PDC + PDC_RNCR, 0); 540 at91usart_writereg(sc, US_PDC + PDC_RCR, 0); 541 542 /* Turn off interrupts. */ 543 at91usart_writereg(sc, US_IDR, -1); 544 545 /* Clear any break condition set with TIOCSBRK. */ 546 at91usart_break(sc, 0); 547 at91usart_set(sc); 548 549 if (sc->disable) { 550 #ifdef DIAGNOSTIC 551 if (!sc->enabled) 552 panic("at91usart_shutdown: not enabled?"); 553 #endif 554 (*sc->disable)(sc); 555 sc->enabled = 0; 556 } 557 splx(s); 558 } 559 560 int 561 at91usart_open(dev_t dev, int flag, int mode, struct lwp *l) 562 { 563 struct at91usart_softc *sc; 564 struct tty *tp; 565 int s; 566 int error; 567 568 sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 569 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK)) 570 return (ENXIO); 571 572 if (!device_is_active(sc->sc_dev)) 573 return (ENXIO); 574 575 #ifdef KGDB 576 /* 577 * If this is the kgdb port, no other use is permitted. 578 */ 579 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) 580 return (EBUSY); 581 #endif 582 583 tp = sc->sc_tty; 584 585 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 586 return (EBUSY); 587 588 s = spltty(); 589 590 /* 591 * Do the following iff this is a first open. 592 */ 593 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 594 struct termios t; 595 596 tp->t_dev = dev; 597 598 if (sc->enable) { 599 if ((*sc->enable)(sc)) { 600 splx(s); 601 printf("%s: device enable failed\n", 602 device_xname(sc->sc_dev)); 603 return (EIO); 604 } 605 sc->enabled = 1; 606 #if 0 607 /* XXXXXXXXXXXXXXX */ 608 com_config(sc); 609 #endif 610 } 611 612 /* reset fifos: */ 613 AT91PDC_RESET_FIFO(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, &sc->sc_rx_fifo, 0); 614 AT91PDC_RESET_FIFO(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, &sc->sc_tx_fifo, 1); 615 616 /* reset receive */ 617 at91usart_writereg(sc, US_CR, US_CR_RSTSTA | US_CR_STTTO); 618 619 /* Turn on interrupts. */ 620 sc->sc_ier = US_CSR_ENDRX|US_CSR_RXBUFF|US_CSR_TIMEOUT|US_CSR_RXBRK; 621 at91usart_writereg(sc, US_IER, sc->sc_ier); 622 623 /* enable DMA: */ 624 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_RXTEN); 625 626 /* 627 * Initialize the termios status to the defaults. Add in the 628 * sticky bits from TIOCSFLAGS. 629 */ 630 t.c_ispeed = 0; 631 /* if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 632 t.c_ospeed = usart_cn_sc.sc_ospeed; 633 t.c_cflag = usart_cn_sc.sc_cflag; 634 } else*/ { 635 t.c_ospeed = TTYDEF_SPEED; 636 t.c_cflag = TTYDEF_CFLAG; 637 } 638 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 639 SET(t.c_cflag, CLOCAL); 640 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 641 SET(t.c_cflag, CRTSCTS); 642 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 643 SET(t.c_cflag, MDMBUF); 644 645 /* Make sure at91usart_param() will do something. */ 646 tp->t_ospeed = 0; 647 (void) at91usart_param(tp, &t); 648 tp->t_iflag = TTYDEF_IFLAG; 649 tp->t_oflag = TTYDEF_OFLAG; 650 tp->t_lflag = TTYDEF_LFLAG; 651 ttychars(tp); 652 ttsetwater(tp); 653 654 /* and unblock. */ 655 CLR(sc->sc_rx_flags, RX_ANY_BLOCK); 656 657 #ifdef COM_DEBUG 658 if (at91usart_debug) 659 comstatus(sc, "at91usart_open "); 660 #endif 661 662 } 663 664 splx(s); 665 666 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 667 if (error) 668 goto bad; 669 670 error = (*tp->t_linesw->l_open)(dev, tp); 671 if (error) 672 goto bad; 673 674 return (0); 675 676 bad: 677 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 678 /* 679 * We failed to open the device, and nobody else had it opened. 680 * Clean up the state as appropriate. 681 */ 682 at91usart_shutdown(sc); 683 } 684 685 return (error); 686 } 687 688 int 689 at91usart_close(dev_t dev, int flag, int mode, struct lwp *l) 690 { 691 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 692 struct tty *tp = sc->sc_tty; 693 694 /* XXX This is for cons.c. */ 695 if (!ISSET(tp->t_state, TS_ISOPEN)) 696 return (0); 697 698 (*tp->t_linesw->l_close)(tp, flag); 699 ttyclose(tp); 700 701 if (COM_ISALIVE(sc) == 0) 702 return (0); 703 704 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 705 /* 706 * Although we got a last close, the device may still be in 707 * use; e.g. if this was the dialout node, and there are still 708 * processes waiting for carrier on the non-dialout node. 709 */ 710 at91usart_shutdown(sc); 711 } 712 713 return (0); 714 } 715 716 int 717 at91usart_read(dev_t dev, struct uio *uio, int flag) 718 { 719 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 720 struct tty *tp = sc->sc_tty; 721 722 if (COM_ISALIVE(sc) == 0) 723 return (EIO); 724 725 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 726 } 727 728 int 729 at91usart_write(dev_t dev, struct uio *uio, int flag) 730 { 731 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 732 struct tty *tp = sc->sc_tty; 733 734 if (COM_ISALIVE(sc) == 0) 735 return (EIO); 736 737 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 738 } 739 740 int 741 at91usart_poll(dev_t dev, int events, struct lwp *l) 742 { 743 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 744 struct tty *tp = sc->sc_tty; 745 746 if (COM_ISALIVE(sc) == 0) 747 return (EIO); 748 749 return ((*tp->t_linesw->l_poll)(tp, events, l)); 750 } 751 752 struct tty * 753 at91usart_tty(dev_t dev) 754 { 755 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 756 struct tty *tp = sc->sc_tty; 757 758 return (tp); 759 } 760 761 int 762 at91usart_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 763 { 764 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 765 struct tty *tp = sc->sc_tty; 766 int error; 767 int s; 768 769 if (COM_ISALIVE(sc) == 0) 770 return (EIO); 771 772 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 773 if (error != EPASSTHROUGH) 774 return (error); 775 776 error = ttioctl(tp, cmd, data, flag, l); 777 if (error != EPASSTHROUGH) 778 return (error); 779 780 error = 0; 781 782 s = spltty(); 783 784 switch (cmd) { 785 case TIOCSBRK: 786 at91usart_break(sc, 1); 787 break; 788 789 case TIOCCBRK: 790 at91usart_break(sc, 0); 791 break; 792 793 case TIOCGFLAGS: 794 *(int *)data = sc->sc_swflags; 795 break; 796 797 case TIOCSFLAGS: 798 error = kauth_authorize_device_tty(l->l_cred, 799 KAUTH_DEVICE_TTY_PRIVSET, tp); 800 if (error) 801 break; 802 sc->sc_swflags = *(int *)data; 803 break; 804 805 default: 806 error = EPASSTHROUGH; 807 break; 808 } 809 810 splx(s); 811 812 return (error); 813 } 814 815 /* 816 * Stop output on a line. 817 */ 818 void 819 at91usart_stop(struct tty *tp, int flag) 820 { 821 struct at91usart_softc *sc 822 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev)); 823 int s; 824 825 s = spltty(); 826 if (ISSET(tp->t_state, TS_BUSY)) { 827 /* Stop transmitting at the next chunk. */ 828 sc->sc_tbc = 0; 829 if (!ISSET(tp->t_state, TS_TTSTOP)) 830 SET(tp->t_state, TS_FLUSH); 831 } 832 splx(s); 833 } 834 835 #if 0 836 static u_int 837 cflag2lcrhi(tcflag_t cflag) 838 { 839 uint32_t mr; 840 841 switch (cflag & CSIZE) { 842 default: 843 mr = 0x0; 844 break; 845 } 846 #if 0 847 mr |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0; 848 mr |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS; 849 mr |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0; 850 mr |= LinCtrlHigh_FEN; /* FIFO always enabled */ 851 #endif 852 mr |= USART_MR_PAR_NONE; 853 return (mr); 854 } 855 #endif 856 857 858 static void 859 at91usart_set(struct at91usart_softc *sc) 860 { 861 at91usart_writereg(sc, US_MR, US_MR_CHRL_8 | US_MR_PAR_NONE | US_MR_NBSTOP_1); 862 at91usart_writereg(sc, US_BRGR, sc->sc_brgr); 863 at91usart_writereg(sc, US_CR, US_CR_TXEN | US_CR_RXEN); // @@@ just in case 864 } 865 866 #if NOTYET 867 int 868 at91usart_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh, 869 uint32_t mstclk, int ospeed, tcflag_t cflag) 870 { 871 cn_tab = &at91usart_cons; 872 cn_init_magic(&at91usart_cnm_state); 873 cn_set_magic("\047\001"); 874 875 usart_cn_sc.sc_iot = iot; 876 usart_cn_sc.sc_ioh = ioh; 877 usart_cn_sc.sc_hwbase = iobase; 878 usart_cn_sc.sc_ospeed = ospeed; 879 usart_cn_sc.sc_cflag = cflag; 880 881 USART_INIT(mstclk, ospeed); 882 883 return (0); 884 } 885 886 void 887 at91usart_cn_probe(struct consdev *cp) 888 { 889 cp->cn_pri = CN_REMOTE; 890 } 891 892 void 893 at91usart_cn_pollc(dev_t dev, int on) 894 { 895 if (on) { 896 // enable polling mode 897 USARTREG(US_IDR) = USART_INT_RXRDY; 898 } else { 899 // disable polling mode 900 USARTREG(US_IER) = USART_INT_RXRDY; 901 } 902 } 903 904 void 905 at91usart_cn_putc(dev_t dev, int c) 906 { 907 int s; 908 #if 0 909 bus_space_tag_t iot = usart_cn_sc.sc_iot; 910 bus_space_handle_t ioh = usart_cn_sc.sc_ioh; 911 #endif 912 s = spltty(); 913 914 USART_PUTC(c); 915 916 #ifdef DEBUG 917 if (c == '\r') { 918 while((USARTREG(USART_SR) & USART_SR_TXEMPTY) == 0) 919 ; 920 } 921 #endif 922 923 splx(s); 924 } 925 926 int 927 at91usart_cn_getc(dev_t dev) 928 { 929 int c, sr; 930 int s; 931 #if 0 932 bus_space_tag_t iot = usart_cn_sc.sc_iot; 933 bus_space_handle_t ioh = usart_cn_sc.sc_ioh; 934 #endif 935 936 s = spltty(); 937 938 while ((c = USART_PEEKC()) == -1) { 939 splx(s); 940 s = spltty(); 941 } 942 ; 943 sr = USARTREG(USART_SR); 944 if (ISSET(sr, USART_SR_FRAME) && c == 0) { 945 USARTREG(USART_CR) = USART_CR_RSTSTA; // reset status bits 946 c = CNC_BREAK; 947 } 948 if (!db_active) { 949 int cn_trapped __unused = 0; 950 951 cn_check_magic(dev, c, at91usart_cnm_state); 952 } 953 splx(s); 954 955 c &= 0xff; 956 957 return (c); 958 } 959 #endif /* NOTYET */ 960 961 inline static void 962 at91usart_rxsoft(struct at91usart_softc *sc, struct tty *tp, unsigned csr) 963 { 964 u_char *start, *get, *end; 965 int cc; 966 967 AT91PDC_FIFO_POSTREAD(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, 968 &sc->sc_rx_fifo); 969 970 if (ISSET(csr, US_CSR_TIMEOUT | US_CSR_RXBRK)) 971 at91usart_rx_stopped(sc); 972 973 while ((start = AT91PDC_FIFO_RDPTR(&sc->sc_rx_fifo, &cc)) != NULL) { 974 int (*rint)(int, struct tty *) = tp->t_linesw->l_rint; 975 int code; 976 977 if (!ISSET(csr, US_CSR_TIMEOUT | US_CSR_RXBRK)) 978 at91usart_rx_started(sc); 979 980 for (get = start, end = start + cc; get < end; get++) { 981 code = *get; 982 if ((*rint)(code, tp) == -1) { 983 /* 984 * The line discipline's buffer is out of space. 985 */ 986 if (!ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) { 987 /* 988 * We're either not using flow control, or the 989 * line discipline didn't tell us to block for 990 * some reason. Either way, we have no way to 991 * know when there's more space available, so 992 * just drop the rest of the data. 993 */ 994 get = end; 995 printf("%s: receive missing data!\n", 996 device_xname(sc->sc_dev)); 997 } else { 998 /* 999 * Don't schedule any more receive processing 1000 * until the line discipline tells us there's 1001 * space available (through comhwiflow()). 1002 * Leave the rest of the data in the input 1003 * buffer. 1004 */ 1005 SET(sc->sc_rx_flags, RX_TTY_OVERFLOWED); 1006 } 1007 break; 1008 } 1009 } 1010 1011 // tell we've read some bytes... 1012 AT91PDC_FIFO_READ(&sc->sc_rx_fifo, get - start); 1013 1014 if (ISSET(sc->sc_rx_flags, RX_TTY_BLOCKED)) 1015 break; 1016 } 1017 1018 // h/w flow control hook: 1019 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 1020 at91usart_rx_rts_ctl(sc, (AT91PDC_FIFO_SPACE(&sc->sc_rx_fifo) > PDC_BLOCK_SIZE * 2)); 1021 1022 // write next pointer if USART is ready: 1023 if (AT91PDC_FIFO_PREREAD(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, 1024 &sc->sc_rx_fifo, PDC_BLOCK_SIZE)) { 1025 SET(sc->sc_ier, US_CSR_ENDRX | US_CSR_RXBUFF | US_CSR_TIMEOUT | US_CSR_RXBRK); 1026 } else { 1027 CLR(sc->sc_ier, US_CSR_ENDRX | US_CSR_RXBUFF | US_CSR_TIMEOUT | US_CSR_RXBRK); 1028 } 1029 } 1030 1031 inline static void 1032 at91usart_txsoft(struct at91usart_softc *sc, struct tty *tp) 1033 { 1034 at91usart_filltx(sc); 1035 if (!ISSET(tp->t_state, TS_BUSY)) 1036 (*tp->t_linesw->l_start)(tp); 1037 } 1038 1039 1040 static void 1041 at91usart_soft(void* arg) 1042 { 1043 struct at91usart_softc *sc = arg; 1044 int s; 1045 u_int csr; 1046 1047 if (COM_ISALIVE(sc) == 0) 1048 return; 1049 1050 s = spltty(); 1051 csr = sc->sc_csr; 1052 while (csr != 0) { 1053 if ((csr &= sc->sc_ier) == 0) 1054 break; 1055 // splx(s); 1056 DPRINTFN(5, ("%s: %s / csr = 0x%08x\n", device_xname(sc->sc_dev), __FUNCTION__, csr)); 1057 if (ISSET(csr, US_CSR_ENDRX | US_CSR_RXBUFF | US_CSR_TIMEOUT | US_CSR_RXBRK)) { 1058 /* receive interrupt */ 1059 if (ISSET(csr, US_CSR_RXBRK)) { 1060 // break received! 1061 at91usart_writereg(sc, US_CR, US_CR_RSTSTA | US_CR_STTTO); 1062 } else if (ISSET(csr, US_CSR_TIMEOUT)) { 1063 // timeout received 1064 at91usart_writereg(sc, US_CR, US_CR_STTTO); 1065 } 1066 at91usart_rxsoft(sc, sc->sc_tty, csr); 1067 } 1068 if (ISSET(csr, US_CSR_TXEMPTY)) { 1069 at91usart_stop_tx(sc); 1070 CLR(sc->sc_ier, US_CSR_TXEMPTY); 1071 if (AT91PDC_FIFO_EMPTY(&sc->sc_tx_fifo)) { 1072 // everything sent! 1073 if (ISSET(sc->sc_tty->t_state, TS_FLUSH)) 1074 CLR(sc->sc_tty->t_state, TS_FLUSH); 1075 } 1076 } 1077 if (ISSET(csr, US_CSR_TXEMPTY | US_CSR_ENDTX)) { 1078 /* transmit interrupt! */ 1079 at91usart_txsoft(sc, sc->sc_tty); 1080 } 1081 // s = spltty(); 1082 csr = at91usart_readreg(sc, US_CSR); 1083 } 1084 sc->sc_csr = 0; 1085 at91usart_writereg(sc, US_IER, sc->sc_ier); // re-enable interrupts 1086 splx(s); 1087 } 1088 1089 1090 static int 1091 at91usart_intr(void* arg) 1092 { 1093 struct at91usart_softc *sc = arg; 1094 u_int csr, imr; 1095 1096 // get out if interrupts are not enabled 1097 imr = at91usart_readreg(sc, US_IMR); 1098 if (!imr) 1099 return 0; 1100 // get out if pending interrupt is not enabled 1101 csr = at91usart_readreg(sc, US_CSR); 1102 DPRINTFN(6,("%s: csr=%08X imr=%08X\n", device_xname(sc->sc_dev), csr, imr)); 1103 if (!ISSET(csr, imr)) 1104 return 0; 1105 1106 // ok, we DO have some interrupts to serve! let softint do it 1107 sc->sc_csr = csr; 1108 at91usart_writereg(sc, US_IDR, -1); 1109 1110 /* Wake up the poller. */ 1111 softint_schedule(sc->sc_si); 1112 1113 /* we're done for now */ 1114 return (1); 1115 1116 } 1117