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