1 /* $Id: at91usart.c,v 1.3 2009/10/23 06:53:13 snj Exp $ */ 2 /* $NetBSD: at91usart.c,v 1.3 2009/10/23 06:53:13 snj 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.3 2009/10/23 06:53:13 snj Exp $"); 81 82 #include "opt_ddb.h" 83 #include "opt_kgdb.h" 84 85 #include "rnd.h" 86 #if NRND > 0 && defined(RND_COM) 87 #include <sys/rnd.h> 88 #endif 89 90 #ifdef NOTYET 91 /* 92 * Override cnmagic(9) macro before including <sys/systm.h>. 93 * We need to know if cn_check_magic triggered debugger, so set a flag. 94 * Callers of cn_check_magic must declare int cn_trapped = 0; 95 * XXX: this is *ugly*! 96 */ 97 #define cn_trap() \ 98 do { \ 99 console_debugger(); \ 100 cn_trapped = 1; \ 101 } while (/* CONSTCOND */ 0) 102 #endif /* NOTYET */ 103 104 105 #include <sys/param.h> 106 #include <sys/systm.h> 107 #include <sys/types.h> 108 #include <sys/conf.h> 109 #include <sys/file.h> 110 #include <sys/device.h> 111 #include <sys/kernel.h> 112 #include <sys/malloc.h> 113 #include <sys/tty.h> 114 #include <sys/uio.h> 115 #include <sys/vnode.h> 116 #include <sys/kauth.h> 117 118 #include <machine/intr.h> 119 #include <machine/bus.h> 120 121 #include <arm/at91/at91reg.h> 122 #include <arm/at91/at91var.h> 123 #include <arm/at91/at91usartreg.h> 124 #include <arm/at91/at91usartvar.h> 125 126 #include <dev/cons.h> 127 128 static int at91usart_param(struct tty *, struct termios *); 129 static void at91usart_start(struct tty *); 130 static int at91usart_hwiflow(struct tty *, int); 131 132 #if 0 133 static u_int cflag2lcrhi(tcflag_t); 134 #endif 135 static void at91usart_set(struct at91usart_softc *); 136 137 #if NOTYET 138 int at91usart_cn_getc(dev_t); 139 void at91usart_cn_putc(dev_t, int); 140 void at91usart_cn_pollc(dev_t, int); 141 void at91usart_cn_probe(struct consdev *); 142 void at91usart_cn_init(struct consdev *); 143 144 static struct at91usart_cons_softc { 145 bus_space_tag_t sc_iot; 146 bus_space_handle_t sc_ioh; 147 bus_addr_t sc_hwbase; 148 int sc_ospeed; 149 tcflag_t sc_cflag; 150 int sc_attached; 151 152 u_int8_t *sc_rx_ptr; 153 u_int8_t sc_rx_fifo[64]; 154 } usart_cn_sc; 155 156 static struct cnm_state at91usart_cnm_state; 157 #endif /* NOTYET */ 158 159 static void at91usart_soft(void* arg); 160 inline static void at91usart_txsoft(struct at91usart_softc *, struct tty *); 161 inline static void at91usart_rxsoft(struct at91usart_softc *, struct tty *, unsigned csr); 162 163 #define PDC_BLOCK_SIZE 64 164 165 //CFATTACH_DECL(at91usart, sizeof(struct at91usart_softc), 166 // at91usart_match, at91usart_attach, NULL, NULL); 167 168 //#define USART_DEBUG 10 169 170 #ifdef USART_DEBUG 171 int usart_debug = USART_DEBUG; 172 #define DPRINTFN(n,fmt) if (usart_debug >= (n)) printf fmt 173 #else 174 #define DPRINTFN(n,fmt) 175 #endif 176 177 extern struct cfdriver at91usart_cd; 178 179 dev_type_open(at91usart_open); 180 dev_type_close(at91usart_close); 181 dev_type_read(at91usart_read); 182 dev_type_write(at91usart_write); 183 dev_type_ioctl(at91usart_ioctl); 184 dev_type_stop(at91usart_stop); 185 dev_type_tty(at91usart_tty); 186 dev_type_poll(at91usart_poll); 187 188 const struct cdevsw at91usart_cdevsw = { 189 at91usart_open, at91usart_close, at91usart_read, at91usart_write, at91usart_ioctl, 190 at91usart_stop, at91usart_tty, at91usart_poll, nommap, ttykqfilter, D_TTY 191 }; 192 193 #if NOTYET 194 struct consdev at91usart_cons = { 195 at91usart_cn_probe, NULL, at91usart_cn_getc, at91usart_cn_putc, at91usart_cn_pollc, NULL, 196 NULL, NULL, NODEV, CN_REMOTE 197 }; 198 #endif /* NOTYET */ 199 200 #ifndef DEFAULT_COMSPEED 201 #define DEFAULT_COMSPEED 115200 202 #endif 203 204 #define COMUNIT_MASK 0x7ffff 205 #define COMDIALOUT_MASK 0x80000 206 207 #define COMUNIT(x) (minor(x) & COMUNIT_MASK) 208 #define COMDIALOUT(x) (minor(x) & COMDIALOUT_MASK) 209 210 #define COM_ISALIVE(sc) ((sc)->enabled != 0 && device_is_active((sc)->sc_dev)) 211 212 static inline void 213 at91usart_writereg(struct at91usart_softc *sc, int reg, u_int val) 214 { 215 bus_space_write_4(sc->sc_iot, sc->sc_ioh, reg, val); 216 } 217 218 static inline u_int 219 at91usart_readreg(struct at91usart_softc *sc, int reg) 220 { 221 return bus_space_read_4(sc->sc_iot, sc->sc_ioh, reg); 222 } 223 #if 0 224 static int 225 at91usart_match(device_t parent, cfdata_t cf, void *aux) 226 { 227 if (strcmp(cf->cf_name, "at91usart") == 0) 228 return 1; 229 return 0; 230 } 231 #endif 232 static int at91usart_intr(void* arg); 233 234 void 235 at91usart_attach_subr(struct at91usart_softc *sc, struct at91bus_attach_args *sa) 236 { 237 struct tty *tp; 238 int err; 239 240 printf("\n"); 241 242 if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh)) 243 panic("%s: Cannot map registers", device_xname(sc->sc_dev)); 244 245 sc->sc_iot = sa->sa_iot; 246 sc->sc_hwbase = sa->sa_addr; 247 sc->sc_dmat = sa->sa_dmat; 248 sc->sc_pid = sa->sa_pid; 249 250 /* allocate fifos */ 251 err = at91pdc_alloc_fifo(sc->sc_dmat, &sc->sc_rx_fifo, AT91USART_RING_SIZE, BUS_DMA_READ | BUS_DMA_STREAMING); 252 if (err) 253 panic("%s: cannot allocate rx fifo", device_xname(sc->sc_dev)); 254 255 err = at91pdc_alloc_fifo(sc->sc_dmat, &sc->sc_tx_fifo, AT91USART_RING_SIZE, BUS_DMA_WRITE | BUS_DMA_STREAMING); 256 if (err) 257 panic("%s: cannot allocate tx fifo", device_xname(sc->sc_dev)); 258 259 /* initialize uart */ 260 at91_peripheral_clock(sc->sc_pid, 1); 261 262 at91usart_writereg(sc, US_IDR, -1); 263 at91usart_writereg(sc, US_RTOR, 12); // 12-bit timeout 264 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS); 265 at91_intr_establish(sa->sa_pid, IPL_TTY, INTR_HIGH_LEVEL, at91usart_intr, sc); 266 USART_INIT(sc, 115200U); 267 268 #ifdef NOTYET 269 if (sc->sc_iot == usart_cn_sc.sc_iot 270 && sc->sc_hwbase == usart_cn_sc.sc_hwbase) { 271 usart_cn_sc.sc_attached = 1; 272 /* Make sure the console is always "hardwired". */ 273 delay(10000); /* wait for output to finish */ 274 SET(sc->sc_hwflags, COM_HW_CONSOLE); 275 SET(sc->sc_swflags, TIOCFLAG_SOFTCAR); 276 SET(sc->sc_ier, USART_INT_RXRDY); 277 USARTREG(USART_IER) = USART_INT_RXRDY; // @@@@@ 278 } 279 #endif // NOTYET 280 281 tp = ttymalloc(); 282 tp->t_oproc = at91usart_start; 283 tp->t_param = at91usart_param; 284 tp->t_hwiflow = at91usart_hwiflow; 285 286 sc->sc_tty = tp; 287 288 tty_attach(tp); 289 290 #if NOTYET 291 if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 292 int maj; 293 294 /* locate the major number */ 295 maj = cdevsw_lookup_major(&at91usart_cdevsw); 296 297 cn_tab->cn_dev = makedev(maj, device_unit(sc->sc_dev)); 298 299 aprint_normal("%s: console (maj %u min %u cn_dev %u)\n", 300 device_xname(sc->sc_dev), maj, device_unit(sc->sc_dev), 301 cn_tab->cn_dev); 302 } 303 #endif /* NOTYET */ 304 305 sc->sc_si = softint_establish(SOFTINT_SERIAL, at91usart_soft, sc); 306 307 #if NRND > 0 && defined(RND_COM) 308 rnd_attach_source(&sc->rnd_source, device_xname(sc->sc_dev), 309 RND_TYPE_TTY, 0); 310 #endif 311 312 /* if there are no enable/disable functions, assume the device 313 is always enabled */ 314 if (!sc->enable) 315 sc->enabled = 1; 316 317 /* XXX configure register */ 318 /* xxx_config(sc) */ 319 320 SET(sc->sc_hwflags, COM_HW_DEV_OK); 321 } 322 323 static int 324 at91usart_param(struct tty *tp, struct termios *t) 325 { 326 struct at91usart_softc *sc 327 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev)); 328 int s; 329 330 if (COM_ISALIVE(sc) == 0) 331 return (EIO); 332 333 if (t->c_ispeed && t->c_ispeed != t->c_ospeed) 334 return (EINVAL); 335 336 /* 337 * For the console, always force CLOCAL and !HUPCL, so that the port 338 * is always active. 339 */ 340 if (ISSET(sc->sc_swflags, TIOCFLAG_SOFTCAR) || 341 ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 342 SET(t->c_cflag, CLOCAL); 343 CLR(t->c_cflag, HUPCL); 344 } 345 346 /* 347 * If there were no changes, don't do anything. This avoids dropping 348 * input and improves performance when all we did was frob things like 349 * VMIN and VTIME. 350 */ 351 if (tp->t_ospeed == t->c_ospeed && 352 tp->t_cflag == t->c_cflag) 353 return (0); 354 355 s = spltty(); 356 357 sc->sc_brgr = (AT91_MSTCLK / 16 + t->c_ospeed / 2) / t->c_ospeed; 358 359 /* And copy to tty. */ 360 tp->t_ispeed = 0; 361 tp->t_ospeed = t->c_ospeed; 362 tp->t_cflag = t->c_cflag; 363 at91usart_set(sc); 364 365 splx(s); 366 367 /* 368 * Update the tty layer's idea of the carrier bit. 369 * We tell tty the carrier is always on. 370 */ 371 (void) (*tp->t_linesw->l_modem)(tp, 1); 372 373 #ifdef COM_DEBUG 374 if (com_debug) 375 comstatus(sc, "comparam "); 376 #endif 377 378 /* tell the upper layer about hwflow.. */ 379 if (sc->hwflow) 380 (*sc->hwflow)(sc, t->c_cflag); 381 382 return (0); 383 } 384 385 static int 386 at91usart_hwiflow(struct tty *tp, int block) 387 { 388 if (block) { 389 /* tty discipline wants to block */ 390 } else { 391 /* tty discipline wants to unblock */ 392 } 393 return (0); 394 } 395 396 static __inline void 397 at91usart_start_tx(struct at91usart_softc *sc) 398 { 399 if (!sc->start_tx) 400 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTEN); 401 else 402 (*sc->start_tx)(sc); 403 } 404 405 static __inline void 406 at91usart_stop_tx(struct at91usart_softc *sc) 407 { 408 if (!sc->stop_tx) 409 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS); 410 else 411 (*sc->stop_tx)(sc); 412 } 413 414 static __inline void 415 at91usart_rx_started(struct at91usart_softc *sc) 416 { 417 if (sc->rx_started) 418 (*sc->rx_started)(sc); 419 } 420 421 static __inline void 422 at91usart_rx_stopped(struct at91usart_softc *sc) 423 { 424 if (sc->rx_stopped) 425 (*sc->rx_stopped)(sc); 426 } 427 428 static __inline void 429 at91usart_rx_rts_ctl(struct at91usart_softc *sc, int enabled) 430 { 431 if (sc->rx_rts_ctl) 432 (*sc->rx_rts_ctl)(sc, enabled); 433 } 434 435 static void 436 at91usart_filltx(struct at91usart_softc *sc) 437 { 438 struct tty *tp = sc->sc_tty; 439 int len; 440 void *dst; 441 442 // post write handler 443 AT91PDC_FIFO_POSTWRITE(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, 444 &sc->sc_tx_fifo); 445 446 // copy more data to fifo: 447 if (sc->sc_tbc > 0 448 && (dst = AT91PDC_FIFO_WRPTR(&sc->sc_tx_fifo, &len)) != NULL) { 449 // copy data to fifo 450 if (len > sc->sc_tbc) 451 len = sc->sc_tbc; 452 memcpy(dst, sc->sc_tba, len); 453 sc->sc_tba += len; 454 if ((sc->sc_tbc -= len) <= 0) 455 CLR(tp->t_state, TS_BUSY); 456 // update fifo 457 AT91PDC_FIFO_WRITTEN(&sc->sc_tx_fifo, len); 458 // tell tty interface we've sent some bytes 459 ndflush(&tp->t_outq, len); 460 } 461 462 // start sending data... 463 if (AT91PDC_FIFO_PREWRITE(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, 464 US_PDC, &sc->sc_tx_fifo, PDC_BLOCK_SIZE)) { 465 at91usart_start_tx(sc); 466 SET(sc->sc_ier, US_CSR_TXEMPTY | US_CSR_ENDTX); 467 } else { 468 CLR(sc->sc_ier, US_CSR_ENDTX); 469 } 470 } 471 472 static void 473 at91usart_start(struct tty *tp) 474 { 475 struct at91usart_softc *sc 476 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev)); 477 int s; 478 479 if (COM_ISALIVE(sc) == 0) { 480 DPRINTFN(5, ("%s: %s / COM_ISALIVE == 0\n", device_xname(sc->sc_dev), __FUNCTION__)); 481 return; 482 } 483 484 s = spltty(); 485 if (ISSET(tp->t_state, TS_BUSY | TS_TIMEOUT | TS_TTSTOP)) { 486 DPRINTFN(5, ("%s: %s: TS_BUSY || TS_TIMEOUT || TS_TTSTOP\n", device_xname(sc->sc_dev), __FUNCTION__)); 487 goto out; 488 } 489 490 if (!ttypull(tp)) 491 goto out; 492 493 /* Grab the first contiguous region of buffer space. */ 494 { 495 u_char *tba; 496 int tbc; 497 498 tba = tp->t_outq.c_cf; 499 tbc = ndqb(&tp->t_outq, 0); 500 501 sc->sc_tba = tba; 502 sc->sc_tbc = tbc; 503 } 504 505 SET(tp->t_state, TS_BUSY); 506 507 /* Output the first chunk of the contiguous buffer. */ 508 at91usart_filltx(sc); 509 at91usart_writereg(sc, US_IER, sc->sc_ier); 510 DPRINTFN(5, ("%s: %s, ier=%08x (csr=%08x)\n", device_xname(sc->sc_dev), __FUNCTION__, sc->sc_ier, at91usart_readreg(sc, US_CSR))); 511 512 out: 513 splx(s); 514 515 return; 516 } 517 518 static __inline__ void 519 at91usart_break(struct at91usart_softc *sc, int onoff) 520 { 521 at91usart_writereg(sc, US_CR, onoff ? US_CR_STTBRK : US_CR_STPBRK); 522 } 523 524 static void 525 at91usart_shutdown(struct at91usart_softc *sc) 526 { 527 int s; 528 529 s = spltty(); 530 531 /* turn of dma */ 532 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS); 533 at91usart_writereg(sc, US_PDC + PDC_TNCR, 0); 534 at91usart_writereg(sc, US_PDC + PDC_TCR, 0); 535 at91usart_writereg(sc, US_PDC + PDC_RNCR, 0); 536 at91usart_writereg(sc, US_PDC + PDC_RCR, 0); 537 538 /* Turn off interrupts. */ 539 at91usart_writereg(sc, US_IDR, -1); 540 541 /* Clear any break condition set with TIOCSBRK. */ 542 at91usart_break(sc, 0); 543 at91usart_set(sc); 544 545 if (sc->disable) { 546 #ifdef DIAGNOSTIC 547 if (!sc->enabled) 548 panic("at91usart_shutdown: not enabled?"); 549 #endif 550 (*sc->disable)(sc); 551 sc->enabled = 0; 552 } 553 splx(s); 554 } 555 556 int 557 at91usart_open(dev_t dev, int flag, int mode, struct lwp *l) 558 { 559 struct at91usart_softc *sc; 560 struct tty *tp; 561 int s; 562 int error; 563 564 sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 565 if (sc == NULL || !ISSET(sc->sc_hwflags, COM_HW_DEV_OK)) 566 return (ENXIO); 567 568 if (!device_is_active(sc->sc_dev)) 569 return (ENXIO); 570 571 #ifdef KGDB 572 /* 573 * If this is the kgdb port, no other use is permitted. 574 */ 575 if (ISSET(sc->sc_hwflags, COM_HW_KGDB)) 576 return (EBUSY); 577 #endif 578 579 tp = sc->sc_tty; 580 581 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp)) 582 return (EBUSY); 583 584 s = spltty(); 585 586 /* 587 * Do the following iff this is a first open. 588 */ 589 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 590 struct termios t; 591 592 tp->t_dev = dev; 593 594 if (sc->enable) { 595 if ((*sc->enable)(sc)) { 596 splx(s); 597 printf("%s: device enable failed\n", 598 device_xname(sc->sc_dev)); 599 return (EIO); 600 } 601 sc->enabled = 1; 602 #if 0 603 /* XXXXXXXXXXXXXXX */ 604 com_config(sc); 605 #endif 606 } 607 608 /* reset fifos: */ 609 AT91PDC_RESET_FIFO(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, &sc->sc_rx_fifo, 0); 610 AT91PDC_RESET_FIFO(sc->sc_iot, sc->sc_ioh, sc->sc_dmat, US_PDC, &sc->sc_tx_fifo, 1); 611 612 /* reset receive */ 613 at91usart_writereg(sc, US_CR, US_CR_RSTSTA | US_CR_STTTO); 614 615 /* Turn on interrupts. */ 616 sc->sc_ier = US_CSR_ENDRX|US_CSR_RXBUFF|US_CSR_TIMEOUT|US_CSR_RXBRK; 617 at91usart_writereg(sc, US_IER, sc->sc_ier); 618 619 /* enable DMA: */ 620 at91usart_writereg(sc, US_PDC + PDC_PTCR, PDC_PTCR_RXTEN); 621 622 /* 623 * Initialize the termios status to the defaults. Add in the 624 * sticky bits from TIOCSFLAGS. 625 */ 626 t.c_ispeed = 0; 627 /* if (ISSET(sc->sc_hwflags, COM_HW_CONSOLE)) { 628 t.c_ospeed = usart_cn_sc.sc_ospeed; 629 t.c_cflag = usart_cn_sc.sc_cflag; 630 } else*/ { 631 t.c_ospeed = TTYDEF_SPEED; 632 t.c_cflag = TTYDEF_CFLAG; 633 } 634 if (ISSET(sc->sc_swflags, TIOCFLAG_CLOCAL)) 635 SET(t.c_cflag, CLOCAL); 636 if (ISSET(sc->sc_swflags, TIOCFLAG_CRTSCTS)) 637 SET(t.c_cflag, CRTSCTS); 638 if (ISSET(sc->sc_swflags, TIOCFLAG_MDMBUF)) 639 SET(t.c_cflag, MDMBUF); 640 641 /* Make sure at91usart_param() will do something. */ 642 tp->t_ospeed = 0; 643 (void) at91usart_param(tp, &t); 644 tp->t_iflag = TTYDEF_IFLAG; 645 tp->t_oflag = TTYDEF_OFLAG; 646 tp->t_lflag = TTYDEF_LFLAG; 647 ttychars(tp); 648 ttsetwater(tp); 649 650 /* and unblock. */ 651 CLR(sc->sc_rx_flags, RX_ANY_BLOCK); 652 653 #ifdef COM_DEBUG 654 if (at91usart_debug) 655 comstatus(sc, "at91usart_open "); 656 #endif 657 658 } 659 660 splx(s); 661 662 error = ttyopen(tp, COMDIALOUT(dev), ISSET(flag, O_NONBLOCK)); 663 if (error) 664 goto bad; 665 666 error = (*tp->t_linesw->l_open)(dev, tp); 667 if (error) 668 goto bad; 669 670 return (0); 671 672 bad: 673 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 674 /* 675 * We failed to open the device, and nobody else had it opened. 676 * Clean up the state as appropriate. 677 */ 678 at91usart_shutdown(sc); 679 } 680 681 return (error); 682 } 683 684 int 685 at91usart_close(dev_t dev, int flag, int mode, struct lwp *l) 686 { 687 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 688 struct tty *tp = sc->sc_tty; 689 690 /* XXX This is for cons.c. */ 691 if (!ISSET(tp->t_state, TS_ISOPEN)) 692 return (0); 693 694 (*tp->t_linesw->l_close)(tp, flag); 695 ttyclose(tp); 696 697 if (COM_ISALIVE(sc) == 0) 698 return (0); 699 700 if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) { 701 /* 702 * Although we got a last close, the device may still be in 703 * use; e.g. if this was the dialout node, and there are still 704 * processes waiting for carrier on the non-dialout node. 705 */ 706 at91usart_shutdown(sc); 707 } 708 709 return (0); 710 } 711 712 int 713 at91usart_read(dev_t dev, struct uio *uio, int flag) 714 { 715 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 716 struct tty *tp = sc->sc_tty; 717 718 if (COM_ISALIVE(sc) == 0) 719 return (EIO); 720 721 return ((*tp->t_linesw->l_read)(tp, uio, flag)); 722 } 723 724 int 725 at91usart_write(dev_t dev, struct uio *uio, int flag) 726 { 727 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 728 struct tty *tp = sc->sc_tty; 729 730 if (COM_ISALIVE(sc) == 0) 731 return (EIO); 732 733 return ((*tp->t_linesw->l_write)(tp, uio, flag)); 734 } 735 736 int 737 at91usart_poll(dev_t dev, int events, struct lwp *l) 738 { 739 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 740 struct tty *tp = sc->sc_tty; 741 742 if (COM_ISALIVE(sc) == 0) 743 return (EIO); 744 745 return ((*tp->t_linesw->l_poll)(tp, events, l)); 746 } 747 748 struct tty * 749 at91usart_tty(dev_t dev) 750 { 751 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 752 struct tty *tp = sc->sc_tty; 753 754 return (tp); 755 } 756 757 int 758 at91usart_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l) 759 { 760 struct at91usart_softc *sc = device_lookup_private(&at91usart_cd, COMUNIT(dev)); 761 struct tty *tp = sc->sc_tty; 762 int error; 763 int s; 764 765 if (COM_ISALIVE(sc) == 0) 766 return (EIO); 767 768 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l); 769 if (error != EPASSTHROUGH) 770 return (error); 771 772 error = ttioctl(tp, cmd, data, flag, l); 773 if (error != EPASSTHROUGH) 774 return (error); 775 776 error = 0; 777 778 s = spltty(); 779 780 switch (cmd) { 781 case TIOCSBRK: 782 at91usart_break(sc, 1); 783 break; 784 785 case TIOCCBRK: 786 at91usart_break(sc, 0); 787 break; 788 789 case TIOCGFLAGS: 790 *(int *)data = sc->sc_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 sc->sc_swflags = *(int *)data; 799 break; 800 801 default: 802 error = EPASSTHROUGH; 803 break; 804 } 805 806 splx(s); 807 808 return (error); 809 } 810 811 /* 812 * Stop output on a line. 813 */ 814 void 815 at91usart_stop(struct tty *tp, int flag) 816 { 817 struct at91usart_softc *sc 818 = device_lookup_private(&at91usart_cd, COMUNIT(tp->t_dev)); 819 int s; 820 821 s = spltty(); 822 if (ISSET(tp->t_state, TS_BUSY)) { 823 /* Stop transmitting at the next chunk. */ 824 sc->sc_tbc = 0; 825 if (!ISSET(tp->t_state, TS_TTSTOP)) 826 SET(tp->t_state, TS_FLUSH); 827 } 828 splx(s); 829 } 830 831 #if 0 832 static u_int 833 cflag2lcrhi(tcflag_t cflag) 834 { 835 u_int32_t mr; 836 837 switch (cflag & CSIZE) { 838 default: 839 mr = 0x0; 840 break; 841 } 842 #if 0 843 mr |= (cflag & PARENB) ? LinCtrlHigh_PEN : 0; 844 mr |= (cflag & PARODD) ? 0 : LinCtrlHigh_EPS; 845 mr |= (cflag & CSTOPB) ? LinCtrlHigh_STP2 : 0; 846 mr |= LinCtrlHigh_FEN; /* FIFO always enabled */ 847 #endif 848 mr |= USART_MR_PAR_NONE; 849 return (mr); 850 } 851 #endif 852 853 854 static void 855 at91usart_set(struct at91usart_softc *sc) 856 { 857 at91usart_writereg(sc, US_MR, US_MR_CHRL_8 | US_MR_PAR_NONE | US_MR_NBSTOP_1); 858 at91usart_writereg(sc, US_BRGR, sc->sc_brgr); 859 at91usart_writereg(sc, US_CR, US_CR_TXEN | US_CR_RXEN); // @@@ just in case 860 } 861 862 #if NOTYET 863 int 864 at91usart_cn_attach(bus_space_tag_t iot, bus_addr_t iobase, bus_space_handle_t ioh, 865 u_int32_t mstclk, int ospeed, tcflag_t cflag) 866 { 867 cn_tab = &at91usart_cons; 868 cn_init_magic(&at91usart_cnm_state); 869 cn_set_magic("\047\001"); 870 871 usart_cn_sc.sc_iot = iot; 872 usart_cn_sc.sc_ioh = ioh; 873 usart_cn_sc.sc_hwbase = iobase; 874 usart_cn_sc.sc_ospeed = ospeed; 875 usart_cn_sc.sc_cflag = cflag; 876 877 USART_INIT(mstclk, ospeed); 878 879 return (0); 880 } 881 882 void 883 at91usart_cn_probe(struct consdev *cp) 884 { 885 cp->cn_pri = CN_REMOTE; 886 } 887 888 void 889 at91usart_cn_pollc(dev_t dev, int on) 890 { 891 if (on) { 892 // enable polling mode 893 USARTREG(US_IDR) = USART_INT_RXRDY; 894 } else { 895 // disable polling mode 896 USARTREG(US_IER) = USART_INT_RXRDY; 897 } 898 } 899 900 void 901 at91usart_cn_putc(dev_t dev, int c) 902 { 903 int s; 904 #if 0 905 bus_space_tag_t iot = usart_cn_sc.sc_iot; 906 bus_space_handle_t ioh = usart_cn_sc.sc_ioh; 907 #endif 908 s = spltty(); 909 910 USART_PUTC(c); 911 912 #ifdef DEBUG 913 if (c == '\r') { 914 while((USARTREG(USART_SR) & USART_SR_TXEMPTY) == 0) 915 ; 916 } 917 #endif 918 919 splx(s); 920 } 921 922 int 923 at91usart_cn_getc(dev_t dev) 924 { 925 int c, sr; 926 int s; 927 #if 0 928 bus_space_tag_t iot = usart_cn_sc.sc_iot; 929 bus_space_handle_t ioh = usart_cn_sc.sc_ioh; 930 #endif 931 932 s = spltty(); 933 934 while ((c = USART_PEEKC()) == -1) { 935 splx(s); 936 s = spltty(); 937 } 938 ; 939 sr = USARTREG(USART_SR); 940 if (ISSET(sr, USART_SR_FRAME) && c == 0) { 941 USARTREG(USART_CR) = USART_CR_RSTSTA; // reset status bits 942 c = CNC_BREAK; 943 } 944 #ifdef DDB 945 extern int db_active; 946 if (!db_active) 947 #endif 948 { 949 int cn_trapped = 0; /* unused */ 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