Home | History | Annotate | Line # | Download | only in dev
sab.c revision 1.45.2.1
      1 /*	$NetBSD: sab.c,v 1.45.2.1 2011/05/31 03:04:18 rmind Exp $	*/
      2 /*	$OpenBSD: sab.c,v 1.7 2002/04/08 17:49:42 jason Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2001 Jason L. Wright (jason (at) thought.net)
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Jason L. Wright
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * Effort sponsored in part by the Defense Advanced Research Projects
     35  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     36  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     37  *
     38  */
     39 
     40 /*
     41  * SAB82532 Dual UART driver
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: sab.c,v 1.45.2.1 2011/05/31 03:04:18 rmind Exp $");
     46 
     47 #include "opt_kgdb.h"
     48 #include <sys/types.h>
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/conf.h>
     53 #include <sys/file.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/kernel.h>
     56 #include <sys/proc.h>
     57 #include <sys/tty.h>
     58 #include <sys/syslog.h>
     59 #include <sys/kauth.h>
     60 #include <sys/kgdb.h>
     61 #include <sys/intr.h>
     62 
     63 #include <machine/autoconf.h>
     64 #include <machine/openfirm.h>
     65 
     66 #include <dev/cons.h>
     67 
     68 #include <dev/ebus/ebusreg.h>
     69 #include <dev/ebus/ebusvar.h>
     70 #include <sparc64/dev/sab82532reg.h>
     71 
     72 #include "locators.h"
     73 
     74 #define SABUNIT(x)		(minor(x) & 0x7ffff)
     75 #define SABDIALOUT(x)		(minor(x) & 0x80000)
     76 
     77 #define	SABTTY_RBUF_SIZE	1024	/* must be divisible by 2 */
     78 
     79 struct sab_softc {
     80 	struct device		sc_dv;
     81 	struct intrhand *	sc_ih;
     82 	bus_space_tag_t		sc_bt;
     83 	bus_space_handle_t	sc_bh;
     84 	struct sabtty_softc *	sc_child[SAB_NCHAN];
     85 	u_int			sc_nchild;
     86 	void *			sc_softintr;
     87 	int			sc_node;
     88 };
     89 
     90 struct sabtty_attach_args {
     91 	u_int sbt_portno;
     92 };
     93 
     94 struct sabtty_softc {
     95 	struct device		sc_dv;
     96 	struct sab_softc *	sc_parent;
     97 	bus_space_tag_t		sc_bt;
     98 	bus_space_handle_t	sc_bh;
     99 	struct tty *		sc_tty;
    100 	u_int			sc_portno;
    101 	uint8_t			sc_pvr_dtr, sc_pvr_dsr;
    102 	uint8_t			sc_imr0, sc_imr1;
    103 	int			sc_openflags;
    104 	u_char *		sc_txp;
    105 	int			sc_txc;
    106 	int			sc_flags;
    107 #define SABTTYF_STOP		0x0001
    108 #define	SABTTYF_DONE		0x0002
    109 #define	SABTTYF_RINGOVERFLOW	0x0004
    110 #define	SABTTYF_CDCHG		0x0008
    111 #define	SABTTYF_CONS_IN		0x0010
    112 #define	SABTTYF_CONS_OUT	0x0020
    113 #define	SABTTYF_TXDRAIN		0x0040
    114 #define	SABTTYF_DONTDDB		0x0080
    115 #define SABTTYF_IS_RSC		0x0100
    116 	uint8_t			sc_rbuf[SABTTY_RBUF_SIZE];
    117 	uint8_t			*sc_rend, *sc_rput, *sc_rget;
    118 	uint8_t			sc_polling, sc_pollrfc;
    119 };
    120 
    121 struct sabtty_softc *sabtty_cons_input;
    122 struct sabtty_softc *sabtty_cons_output;
    123 
    124 #define	SAB_READ(sc,r)		\
    125     bus_space_read_1((sc)->sc_bt, (sc)->sc_bh, (r))
    126 #define	SAB_WRITE(sc,r,v)	\
    127     bus_space_write_1((sc)->sc_bt, (sc)->sc_bh, (r), (v))
    128 #define	SAB_WRITE_BLOCK(sc,r,p,c)	\
    129     bus_space_write_region_1((sc)->sc_bt, (sc)->sc_bh, (r), (p), (c))
    130 
    131 int sab_match(struct device *, struct cfdata *, void *);
    132 void sab_attach(struct device *, struct device *, void *);
    133 int sab_print(void *, const char *);
    134 int sab_intr(void *);
    135 
    136 void sab_softintr(void *);
    137 void sab_cnputc(dev_t, int);
    138 int sab_cngetc(dev_t);
    139 void sab_cnpollc(dev_t, int);
    140 
    141 int sabtty_match(struct device *, struct cfdata *, void *);
    142 void sabtty_attach(struct device *, struct device *, void *);
    143 void sabtty_start(struct tty *);
    144 int sabtty_param(struct tty *, struct termios *);
    145 int sabtty_intr(struct sabtty_softc *, int *);
    146 void sabtty_softintr(struct sabtty_softc *);
    147 int sabtty_mdmctrl(struct sabtty_softc *, int, int);
    148 void sabtty_cec_wait(struct sabtty_softc *);
    149 void sabtty_tec_wait(struct sabtty_softc *);
    150 void sabtty_reset(struct sabtty_softc *);
    151 void sabtty_flush(struct sabtty_softc *);
    152 int sabtty_speed(int);
    153 void sabtty_console_flags(struct sabtty_softc *);
    154 void sabtty_cnpollc(struct sabtty_softc *, int);
    155 void sabtty_shutdown(void *);
    156 int sabttyparam(struct sabtty_softc *, struct tty *, struct termios *);
    157 
    158 #ifdef KGDB
    159 int sab_kgdb_check(struct sabtty_softc *);
    160 void sab_kgdb_init(struct sabtty_softc *);
    161 #endif
    162 
    163 void sabtty_cnputc(struct sabtty_softc *, int);
    164 int sabtty_cngetc(struct sabtty_softc *);
    165 
    166 CFATTACH_DECL(sab, sizeof(struct sab_softc),
    167     sab_match, sab_attach, NULL, NULL);
    168 
    169 extern struct cfdriver sab_cd;
    170 
    171 CFATTACH_DECL(sabtty, sizeof(struct sabtty_softc),
    172     sabtty_match, sabtty_attach, NULL, NULL);
    173 
    174 extern struct cfdriver sabtty_cd;
    175 
    176 dev_type_open(sabopen);
    177 dev_type_close(sabclose);
    178 dev_type_read(sabread);
    179 dev_type_write(sabwrite);
    180 dev_type_ioctl(sabioctl);
    181 dev_type_stop(sabstop);
    182 dev_type_tty(sabtty);
    183 dev_type_poll(sabpoll);
    184 
    185 static struct cnm_state sabtty_cnm_state;
    186 
    187 const struct cdevsw sabtty_cdevsw = {
    188 	sabopen, sabclose, sabread, sabwrite, sabioctl,
    189 	sabstop, sabtty, sabpoll, nommap, ttykqfilter, D_TTY
    190 };
    191 
    192 struct sabtty_rate {
    193 	int baud;
    194 	int n, m;
    195 };
    196 
    197 struct sabtty_rate sabtty_baudtable[] = {
    198 	{      50,	35,     10 },
    199 	{      75,	47,	9 },
    200 	{     110,	32,	9 },
    201 	{     134,	53,	8 },
    202 	{     150,	47,	8 },
    203 	{     200,	35,	8 },
    204 	{     300,	47,	7 },
    205 	{     600,	47,	6 },
    206 	{    1200,	47,	5 },
    207 	{    1800,	31,	5 },
    208 	{    2400,	47,	4 },
    209 	{    4800,	47,	3 },
    210 	{    9600,	47,	2 },
    211 	{   19200,	47,	1 },
    212 	{   38400,	23,	1 },
    213 	{   57600,	15,	1 },
    214 	{  115200,	 7,	1 },
    215 	{  230400,	 3,	1 },
    216 	{  460800,	 1,	1 },
    217 	{   76800,	11,	1 },
    218 	{  153600,	 5,	1 },
    219 	{  307200,	 3,	1 },
    220 	{  614400,	 3,	0 },
    221 	{  921600,	 0,	1 },
    222 };
    223 
    224 int
    225 sab_match(struct device *parent, struct cfdata *match, void *aux)
    226 {
    227 	struct ebus_attach_args *ea = aux;
    228 	char *compat;
    229 
    230 	if (strcmp(ea->ea_name, "se") == 0 ||
    231 	    strcmp(ea->ea_name, "FJSV,se") == 0)
    232 		return (1);
    233 
    234 	compat = prom_getpropstring(ea->ea_node, "compatible");
    235 	if (compat != NULL && !strcmp(compat, "sab82532"))
    236 		return (1);
    237 
    238 	return (0);
    239 }
    240 
    241 void
    242 sab_attach(struct device *parent, struct device *self, void *aux)
    243 {
    244 	struct sab_softc *sc = (struct sab_softc *)self;
    245 	struct ebus_attach_args *ea = aux;
    246 	uint8_t r;
    247 	u_int i;
    248 	int locs[SABCF_NLOCS];
    249 
    250 	sc->sc_bt = ea->ea_bustag;
    251 	sc->sc_node = ea->ea_node;
    252 
    253 	/* Use prom mapping, if available. */
    254 	if (ea->ea_nvaddr)
    255 		sparc_promaddr_to_handle(sc->sc_bt, ea->ea_vaddr[0],
    256 		    &sc->sc_bh);
    257 	else if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    258 				 ea->ea_reg[0].size, 0, &sc->sc_bh) != 0) {
    259 		printf(": can't map register space\n");
    260 		return;
    261 	}
    262 
    263 	sc->sc_ih = bus_intr_establish(ea->ea_bustag, ea->ea_intr[0],
    264 	    IPL_TTY, sab_intr, sc);
    265 	if (sc->sc_ih == NULL) {
    266 		printf(": can't map interrupt\n");
    267 		return;
    268 	}
    269 
    270 	sc->sc_softintr = softint_establish(SOFTINT_SERIAL, sab_softintr, sc);
    271 	if (sc->sc_softintr == NULL) {
    272 		printf(": can't get soft intr\n");
    273 		return;
    274 	}
    275 
    276 	aprint_normal(": rev ");
    277 	r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_VMASK;
    278 	switch (r) {
    279 	case SAB_VSTR_V_1:
    280 		aprint_normal("1");
    281 		break;
    282 	case SAB_VSTR_V_2:
    283 		aprint_normal("2");
    284 		break;
    285 	case SAB_VSTR_V_32:
    286 		aprint_normal("3.2");
    287 		break;
    288 	default:
    289 		aprint_normal("unknown(0x%x)", r);
    290 		break;
    291 	}
    292 	aprint_normal("\n");
    293 	aprint_naive(": Serial controller\n");
    294 
    295 	/* Let current output drain */
    296 	DELAY(100000);
    297 
    298 	/* Set all pins, except DTR pins to be inputs */
    299 	SAB_WRITE(sc, SAB_PCR, ~(SAB_PVR_DTR_A | SAB_PVR_DTR_B));
    300 	/* Disable port interrupts */
    301 	SAB_WRITE(sc, SAB_PIM, 0xff);
    302 	SAB_WRITE(sc, SAB_PVR, SAB_PVR_DTR_A | SAB_PVR_DTR_B | SAB_PVR_MAGIC);
    303 	SAB_WRITE(sc, SAB_IPC, SAB_IPC_ICPL);
    304 
    305 	for (i = 0; i < SAB_NCHAN; i++) {
    306 		struct sabtty_attach_args stax;
    307 
    308 		stax.sbt_portno = i;
    309 
    310 		locs[SABCF_CHANNEL] = i;
    311 
    312 		sc->sc_child[i] =
    313 		    (struct sabtty_softc *)config_found_sm_loc(self,
    314 		     "sab", locs, &stax, sab_print, config_stdsubmatch);
    315 		if (sc->sc_child[i] != NULL)
    316 			sc->sc_nchild++;
    317 	}
    318 }
    319 
    320 int
    321 sab_print(void *args, const char *name)
    322 {
    323 	struct sabtty_attach_args *sa = args;
    324 
    325 	if (name)
    326 		aprint_normal("sabtty at %s", name);
    327 	aprint_normal(" port %u", sa->sbt_portno);
    328 	return (UNCONF);
    329 }
    330 
    331 int
    332 sab_intr(void *vsc)
    333 {
    334 	struct sab_softc *sc = vsc;
    335 	int r = 0, needsoft = 0;
    336 	uint8_t gis;
    337 
    338 	gis = SAB_READ(sc, SAB_GIS);
    339 
    340 	/* channel A */
    341 	if ((gis & (SAB_GIS_ISA1 | SAB_GIS_ISA0)) && sc->sc_child[0] &&
    342 	    sc->sc_child[0]->sc_tty)
    343 		r |= sabtty_intr(sc->sc_child[0], &needsoft);
    344 
    345 	/* channel B */
    346 	if ((gis & (SAB_GIS_ISB1 | SAB_GIS_ISB0)) && sc->sc_child[1] &&
    347 	    sc->sc_child[1]->sc_tty)
    348 		r |= sabtty_intr(sc->sc_child[1], &needsoft);
    349 
    350 	if (needsoft)
    351 		softint_schedule(sc->sc_softintr);
    352 
    353 	return (r);
    354 }
    355 
    356 void
    357 sab_softintr(void *vsc)
    358 {
    359 	struct sab_softc *sc = vsc;
    360 
    361 	if (sc->sc_child[0] && sc->sc_child[0]->sc_tty)
    362 		sabtty_softintr(sc->sc_child[0]);
    363 	if (sc->sc_child[1] && sc->sc_child[1]->sc_tty)
    364 		sabtty_softintr(sc->sc_child[1]);
    365 }
    366 
    367 int
    368 sabtty_match(struct device *parent, struct cfdata *match, void *aux)
    369 {
    370 
    371 	return (1);
    372 }
    373 
    374 void
    375 sabtty_attach(struct device *parent, struct device *self, void *aux)
    376 {
    377 	struct sabtty_softc *sc = (struct sabtty_softc *)self;
    378 	struct sabtty_attach_args *sa = aux;
    379 	int r;
    380 	int maj;
    381 	int is_kgdb = 0;
    382 
    383 #ifdef KGDB
    384 	is_kgdb = sab_kgdb_check(sc);
    385 #endif
    386 
    387 	if (!is_kgdb) {
    388 		sc->sc_tty = tty_alloc();
    389 		if (sc->sc_tty == NULL) {
    390 			aprint_normal(": failed to allocate tty\n");
    391 			return;
    392 		}
    393 		tty_attach(sc->sc_tty);
    394 		sc->sc_tty->t_oproc = sabtty_start;
    395 		sc->sc_tty->t_param = sabtty_param;
    396 	}
    397 
    398 	sc->sc_parent = (struct sab_softc *)parent;
    399 	sc->sc_bt = sc->sc_parent->sc_bt;
    400 	sc->sc_portno = sa->sbt_portno;
    401 	sc->sc_rend = sc->sc_rbuf + SABTTY_RBUF_SIZE;
    402 
    403 	switch (sa->sbt_portno) {
    404 	case 0:	/* port A */
    405 		sc->sc_pvr_dtr = SAB_PVR_DTR_A;
    406 		sc->sc_pvr_dsr = SAB_PVR_DSR_A;
    407 		r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
    408 		    SAB_CHAN_A, SAB_CHANLEN, &sc->sc_bh);
    409 		break;
    410 	case 1:	/* port B */
    411 		sc->sc_pvr_dtr = SAB_PVR_DTR_B;
    412 		sc->sc_pvr_dsr = SAB_PVR_DSR_B;
    413 		r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
    414 		    SAB_CHAN_B, SAB_CHANLEN, &sc->sc_bh);
    415 		break;
    416 	default:
    417 		aprint_normal(": invalid channel: %u\n", sa->sbt_portno);
    418 		return;
    419 	}
    420 	if (r != 0) {
    421 		aprint_normal(": failed to allocate register subregion\n");
    422 		return;
    423 	}
    424 
    425 	sabtty_console_flags(sc);
    426 
    427 	if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
    428 		struct termios t;
    429 		const char *acc;
    430 
    431 		/* Let residual prom output drain */
    432 		DELAY(100000);
    433 
    434 		switch (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
    435 		case SABTTYF_CONS_IN:
    436 			acc = "input";
    437 			break;
    438 		case SABTTYF_CONS_OUT:
    439 			acc = "output";
    440 			break;
    441 		case SABTTYF_CONS_IN|SABTTYF_CONS_OUT:
    442 		default:
    443 			acc = "i/o";
    444 			break;
    445 		}
    446 
    447 		t.c_ispeed= 0;
    448 		if (sc->sc_flags & SABTTYF_IS_RSC)
    449 			t.c_ospeed = 115200;
    450 		else
    451 			t.c_ospeed = 9600;
    452 		t.c_cflag = CREAD | CS8 | HUPCL;
    453 		sc->sc_tty->t_ospeed = 0;
    454 		sabttyparam(sc, sc->sc_tty, &t);
    455 
    456 		if (sc->sc_flags & SABTTYF_CONS_IN) {
    457 			sabtty_cons_input = sc;
    458 			cn_tab->cn_pollc = sab_cnpollc;
    459 			cn_tab->cn_getc = sab_cngetc;
    460 			maj = cdevsw_lookup_major(&sabtty_cdevsw);
    461 			cn_tab->cn_dev = makedev(maj, device_unit(self));
    462 			shutdownhook_establish(sabtty_shutdown, sc);
    463 			cn_init_magic(&sabtty_cnm_state);
    464 			cn_set_magic("\047\001"); /* default magic is BREAK */
    465 		}
    466 
    467 		if (sc->sc_flags & SABTTYF_CONS_OUT) {
    468 			sabtty_tec_wait(sc);
    469 			sabtty_cons_output = sc;
    470 			cn_tab->cn_putc = sab_cnputc;
    471 			maj = cdevsw_lookup_major(&sabtty_cdevsw);
    472 			cn_tab->cn_dev = makedev(maj, device_unit(self));
    473 		}
    474 		aprint_normal(": console %s", acc);
    475 	} else {
    476 		/* Not a console... */
    477 		sabtty_reset(sc);
    478 
    479 #ifdef KGDB
    480 		if (is_kgdb) {
    481 			sab_kgdb_init(sc);
    482 			aprint_normal(": kgdb");
    483 		}
    484 #endif
    485 	}
    486 
    487 	aprint_normal("\n");
    488 	aprint_naive(": Serial port\n");
    489 }
    490 
    491 int
    492 sabtty_intr(struct sabtty_softc *sc, int *needsoftp)
    493 {
    494 	uint8_t isr0, isr1;
    495 	int i, len = 0, needsoft = 0, r = 0, clearfifo = 0;
    496 
    497 	isr0 = SAB_READ(sc, SAB_ISR0);
    498 	isr1 = SAB_READ(sc, SAB_ISR1);
    499 
    500 	if (isr0 || isr1)
    501 		r = 1;
    502 
    503 	if (isr0 & SAB_ISR0_RPF) {
    504 		len = 32;
    505 		clearfifo = 1;
    506 	}
    507 	if (isr0 & SAB_ISR0_TCD) {
    508 		len = (32 - 1) & SAB_READ(sc, SAB_RBCL);
    509 		clearfifo = 1;
    510 	}
    511 	if (isr0 & SAB_ISR0_TIME) {
    512 		sabtty_cec_wait(sc);
    513 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
    514 	}
    515 	if (isr0 & SAB_ISR0_RFO) {
    516 		sc->sc_flags |= SABTTYF_RINGOVERFLOW;
    517 		clearfifo = 1;
    518 	}
    519 	if (len != 0) {
    520 		uint8_t *ptr, b;
    521 
    522 		ptr = sc->sc_rput;
    523 		for (i = 0; i < len; i++) {
    524 			b = SAB_READ(sc, SAB_RFIFO);
    525 			if (i % 2 == 0) /* skip status byte */
    526 				cn_check_magic(sc->sc_tty->t_dev,
    527 					       b, sabtty_cnm_state);
    528 			*ptr++ = b;
    529 			if (ptr == sc->sc_rend)
    530 				ptr = sc->sc_rbuf;
    531 			if (ptr == sc->sc_rget) {
    532 				if (ptr == sc->sc_rbuf)
    533 					ptr = sc->sc_rend;
    534 				ptr--;
    535 				sc->sc_flags |= SABTTYF_RINGOVERFLOW;
    536 			}
    537 		}
    538 		sc->sc_rput = ptr;
    539 		needsoft = 1;
    540 	}
    541 
    542 	if (clearfifo) {
    543 		sabtty_cec_wait(sc);
    544 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
    545 	}
    546 
    547 	if (isr0 & SAB_ISR0_CDSC) {
    548 		sc->sc_flags |= SABTTYF_CDCHG;
    549 		needsoft = 1;
    550 	}
    551 
    552 	if (isr1 & SAB_ISR1_BRKT)
    553 		cn_check_magic(sc->sc_tty->t_dev,
    554 			       CNC_BREAK, sabtty_cnm_state);
    555 
    556 	if (isr1 & (SAB_ISR1_XPR | SAB_ISR1_ALLS)) {
    557 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_XFW) &&
    558 		    (sc->sc_flags & SABTTYF_STOP) == 0) {
    559 			if (sc->sc_txc < 32)
    560 				len = sc->sc_txc;
    561 			else
    562 				len = 32;
    563 
    564 			if (len > 0) {
    565 				SAB_WRITE_BLOCK(sc, SAB_XFIFO, sc->sc_txp, len);
    566 				sc->sc_txp += len;
    567 				sc->sc_txc -= len;
    568 
    569 				sabtty_cec_wait(sc);
    570 				SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XF);
    571 
    572 				/*
    573 				 * Prevent the false end of xmit from
    574 				 * confusing things below.
    575 				 */
    576 				isr1 &= ~SAB_ISR1_ALLS;
    577 			}
    578 		}
    579 
    580 		if ((sc->sc_txc == 0) || (sc->sc_flags & SABTTYF_STOP)) {
    581 			if ((sc->sc_imr1 & SAB_IMR1_XPR) == 0) {
    582 				sc->sc_imr1 |= SAB_IMR1_XPR;
    583 				sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    584 				SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    585 			}
    586 		}
    587 	}
    588 
    589 	if ((isr1 & SAB_ISR1_ALLS) && ((sc->sc_txc == 0) ||
    590 	    (sc->sc_flags & SABTTYF_STOP))) {
    591 		if (sc->sc_flags & SABTTYF_TXDRAIN)
    592 			wakeup(sc);
    593 		sc->sc_flags &= ~SABTTYF_STOP;
    594 		sc->sc_flags |= SABTTYF_DONE;
    595 		sc->sc_imr1 |= SAB_IMR1_ALLS;
    596 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    597 		needsoft = 1;
    598 	}
    599 
    600 	if (needsoft)
    601 		*needsoftp = needsoft;
    602 	return (r);
    603 }
    604 
    605 void
    606 sabtty_softintr(struct sabtty_softc *sc)
    607 {
    608 	struct tty *tp = sc->sc_tty;
    609 	int s, flags;
    610 	uint8_t r;
    611 
    612 	if (tp == NULL)
    613 		return;
    614 
    615 	if ((tp->t_state & TS_ISOPEN) == 0)
    616 		return;
    617 
    618 	while (sc->sc_rget != sc->sc_rput) {
    619 		int data;
    620 		uint8_t stat;
    621 
    622 		data = sc->sc_rget[0];
    623 		stat = sc->sc_rget[1];
    624 		sc->sc_rget += 2;
    625 		if (stat & SAB_RSTAT_PE)
    626 			data |= TTY_PE;
    627 		if (stat & SAB_RSTAT_FE)
    628 			data |= TTY_FE;
    629 		if (sc->sc_rget == sc->sc_rend)
    630 			sc->sc_rget = sc->sc_rbuf;
    631 
    632 		(*tp->t_linesw->l_rint)(data, tp);
    633 	}
    634 
    635 	s = splhigh();
    636 	flags = sc->sc_flags;
    637 	sc->sc_flags &= ~(SABTTYF_DONE|SABTTYF_CDCHG|SABTTYF_RINGOVERFLOW);
    638 	splx(s);
    639 
    640 	if (flags & SABTTYF_CDCHG) {
    641 		s = spltty();
    642 		r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD;
    643 		splx(s);
    644 
    645 		(*tp->t_linesw->l_modem)(tp, r);
    646 	}
    647 
    648 	if (flags & SABTTYF_RINGOVERFLOW)
    649 		log(LOG_WARNING, "%s: ring overflow\n",
    650 		    device_xname(&sc->sc_dv));
    651 
    652 	if (flags & SABTTYF_DONE) {
    653 		ndflush(&tp->t_outq, sc->sc_txp - tp->t_outq.c_cf);
    654 		tp->t_state &= ~TS_BUSY;
    655 		(*tp->t_linesw->l_start)(tp);
    656 	}
    657 }
    658 
    659 int
    660 sabopen(dev_t dev, int flags, int mode, struct lwp *l)
    661 {
    662 	struct sabtty_softc *sc;
    663 	struct tty *tp;
    664 	struct proc *p;
    665 	int s, s1;
    666 
    667 	sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    668 	if (sc == NULL)
    669 		return (ENXIO);
    670 
    671 	tp = sc->sc_tty;
    672 	tp->t_dev = dev;
    673 	p = l->l_proc;
    674 
    675 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    676 		return (EBUSY);
    677 
    678 	mutex_spin_enter(&tty_lock);
    679 	if ((tp->t_state & TS_ISOPEN) == 0) {
    680 		ttychars(tp);
    681 		tp->t_iflag = TTYDEF_IFLAG;
    682 		tp->t_oflag = TTYDEF_OFLAG;
    683 		tp->t_cflag = TTYDEF_CFLAG;
    684 		if (sc->sc_openflags & TIOCFLAG_CLOCAL)
    685 			tp->t_cflag |= CLOCAL;
    686 		if (sc->sc_openflags & TIOCFLAG_CRTSCTS)
    687 			tp->t_cflag |= CRTSCTS;
    688 		if (sc->sc_openflags & TIOCFLAG_MDMBUF)
    689 			tp->t_cflag |= MDMBUF;
    690 		tp->t_lflag = TTYDEF_LFLAG;
    691 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    692 
    693 		sc->sc_rput = sc->sc_rget = sc->sc_rbuf;
    694 
    695 		ttsetwater(tp);
    696 
    697 		s1 = splhigh();
    698 		sabtty_reset(sc);
    699 		sabtty_param(tp, &tp->t_termios);
    700 		sc->sc_imr0 = SAB_IMR0_PERR | SAB_IMR0_FERR | SAB_IMR0_PLLA;
    701 		SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
    702 		sc->sc_imr1 = SAB_IMR1_BRK | SAB_IMR1_ALLS | SAB_IMR1_XDU |
    703 		    SAB_IMR1_TIN | SAB_IMR1_CSC | SAB_IMR1_XMR | SAB_IMR1_XPR;
    704 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    705 		SAB_WRITE(sc, SAB_CCR0, SAB_READ(sc, SAB_CCR0) | SAB_CCR0_PU);
    706 		sabtty_cec_wait(sc);
    707 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
    708 		sabtty_cec_wait(sc);
    709 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
    710 		sabtty_cec_wait(sc);
    711 		splx(s1);
    712 
    713 		sabtty_flush(sc);
    714 
    715 		if ((sc->sc_openflags & TIOCFLAG_SOFTCAR) ||
    716 		    (SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD))
    717 			tp->t_state |= TS_CARR_ON;
    718 		else
    719 			tp->t_state &= ~TS_CARR_ON;
    720 	}
    721 
    722 	if ((flags & O_NONBLOCK) == 0) {
    723 		while ((tp->t_cflag & CLOCAL) == 0 &&
    724 		    (tp->t_state & TS_CARR_ON) == 0) {
    725 			int error;
    726 
    727 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    728 			if (error != 0) {
    729 				mutex_spin_exit(&tty_lock);
    730 				return (error);
    731 			}
    732 		}
    733 	}
    734 
    735 	mutex_spin_exit(&tty_lock);
    736 
    737 	s = (*tp->t_linesw->l_open)(dev, tp);
    738 	if (s != 0) {
    739 		mutex_spin_enter(&tty_lock);
    740 		if (tp->t_state & TS_ISOPEN) {
    741 			mutex_spin_exit(&tty_lock);
    742 			return (s);
    743 		}
    744 		if (tp->t_cflag & HUPCL) {
    745 			sabtty_mdmctrl(sc, 0, DMSET);
    746 			cv_wait(&lbolt, &tty_lock);
    747 		}
    748 
    749 		if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
    750 			/* Flush and power down if we're not the console */
    751 			sabtty_flush(sc);
    752 			sabtty_reset(sc);
    753 		}
    754 		mutex_spin_exit(&tty_lock);
    755 	}
    756 	return (s);
    757 }
    758 
    759 int
    760 sabclose(dev_t dev, int flags, int mode, struct lwp *l)
    761 {
    762 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    763 	struct sab_softc *bc = sc->sc_parent;
    764 	struct tty *tp = sc->sc_tty;
    765 	int s;
    766 
    767 	(*tp->t_linesw->l_close)(tp, flags);
    768 
    769 	s = spltty();
    770 
    771 	if ((tp->t_state & TS_ISOPEN) == 0) {
    772 		/* Wait for output drain */
    773 		sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    774 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    775 		sc->sc_flags |= SABTTYF_TXDRAIN;
    776 		(void)tsleep(sc, TTIPRI, ttclos, 5 * hz);
    777 		sc->sc_imr1 |= SAB_IMR1_ALLS;
    778 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    779 		sc->sc_flags &= ~SABTTYF_TXDRAIN;
    780 
    781 		if (tp->t_cflag & HUPCL) {
    782 			sabtty_mdmctrl(sc, 0, DMSET);
    783 			(void)tsleep(bc, TTIPRI, ttclos, hz);
    784 		}
    785 
    786 		if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
    787 			/* Flush and power down if we're not the console */
    788 			sabtty_flush(sc);
    789 			sabtty_reset(sc);
    790 		}
    791 	}
    792 
    793 	ttyclose(tp);
    794 	splx(s);
    795 
    796 	return (0);
    797 }
    798 
    799 int
    800 sabread(dev_t dev, struct uio *uio, int flags)
    801 {
    802 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    803 	struct tty *tp = sc->sc_tty;
    804 
    805 	return ((*tp->t_linesw->l_read)(tp, uio, flags));
    806 }
    807 
    808 int
    809 sabwrite(dev_t dev, struct uio *uio, int flags)
    810 {
    811 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    812 	struct tty *tp = sc->sc_tty;
    813 
    814 	return ((*tp->t_linesw->l_write)(tp, uio, flags));
    815 }
    816 
    817 int
    818 sabioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    819 {
    820 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    821 	struct tty *tp = sc->sc_tty;
    822 	int error;
    823 
    824 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flags, l);
    825 	if (error >= 0)
    826 		return (error);
    827 
    828 	error = ttioctl(tp, cmd, data, flags, l);
    829 	if (error >= 0)
    830 		return (error);
    831 
    832 	error = 0;
    833 
    834 	switch (cmd) {
    835 	case TIOCSBRK:
    836 		SAB_WRITE(sc, SAB_DAFO,
    837 		    SAB_READ(sc, SAB_DAFO) | SAB_DAFO_XBRK);
    838 		break;
    839 	case TIOCCBRK:
    840 		SAB_WRITE(sc, SAB_DAFO,
    841 		    SAB_READ(sc, SAB_DAFO) & ~SAB_DAFO_XBRK);
    842 		break;
    843 	case TIOCSDTR:
    844 		sabtty_mdmctrl(sc, TIOCM_DTR, DMBIS);
    845 		break;
    846 	case TIOCCDTR:
    847 		sabtty_mdmctrl(sc, TIOCM_DTR, DMBIC);
    848 		break;
    849 	case TIOCMBIS:
    850 		sabtty_mdmctrl(sc, *((int *)data), DMBIS);
    851 		break;
    852 	case TIOCMBIC:
    853 		sabtty_mdmctrl(sc, *((int *)data), DMBIC);
    854 		break;
    855 	case TIOCMGET:
    856 		*((int *)data) = sabtty_mdmctrl(sc, 0, DMGET);
    857 		break;
    858 	case TIOCMSET:
    859 		sabtty_mdmctrl(sc, *((int *)data), DMSET);
    860 		break;
    861 	case TIOCGFLAGS:
    862 		*((int *)data) = sc->sc_openflags;
    863 		break;
    864 	case TIOCSFLAGS:
    865 		if (kauth_authorize_device_tty(l->l_cred,
    866 		    KAUTH_DEVICE_TTY_PRIVSET, tp))
    867 			error = EPERM;
    868 		else
    869 			sc->sc_openflags = *((int *)data) &
    870 			    (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    871 			     TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    872 		break;
    873 	default:
    874 		error = ENOTTY;
    875 	}
    876 
    877 	return (error);
    878 }
    879 
    880 struct tty *
    881 sabtty(dev_t dev)
    882 {
    883 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    884 
    885 	return (sc->sc_tty);
    886 }
    887 
    888 void
    889 sabstop(struct tty *tp, int flag)
    890 {
    891 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
    892 	int s;
    893 
    894 	s = spltty();
    895 	if (tp->t_state & TS_BUSY) {
    896 		if ((tp->t_state & TS_TTSTOP) == 0)
    897 			tp->t_state |= TS_FLUSH;
    898 		sc->sc_flags |= SABTTYF_STOP;
    899 		sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    900 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    901 	}
    902 	splx(s);
    903 }
    904 
    905 int
    906 sabpoll(dev_t dev, int events, struct lwp *l)
    907 {
    908 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    909 	struct tty *tp = sc->sc_tty;
    910 
    911 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    912 }
    913 
    914 int
    915 sabtty_mdmctrl(struct sabtty_softc *sc, int bits, int how)
    916 {
    917 	uint8_t r;
    918 	int s;
    919 
    920 	s = spltty();
    921 	switch (how) {
    922 	case DMGET:
    923 		bits = 0;
    924 		if (SAB_READ(sc, SAB_STAR) & SAB_STAR_CTS)
    925 			bits |= TIOCM_CTS;
    926 		if ((SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD) == 0)
    927 			bits |= TIOCM_CD;
    928 
    929 		r = SAB_READ(sc, SAB_PVR);
    930 		if ((r & sc->sc_pvr_dtr) == 0)
    931 			bits |= TIOCM_DTR;
    932 		if ((r & sc->sc_pvr_dsr) == 0)
    933 			bits |= TIOCM_DSR;
    934 
    935 		r = SAB_READ(sc, SAB_MODE);
    936 		if ((r & (SAB_MODE_RTS|SAB_MODE_FRTS)) == SAB_MODE_RTS)
    937 			bits |= TIOCM_RTS;
    938 		break;
    939 	case DMSET:
    940 		r = SAB_READ(sc, SAB_MODE);
    941 		if (bits & TIOCM_RTS) {
    942 			r &= ~SAB_MODE_FRTS;
    943 			r |= SAB_MODE_RTS;
    944 		} else
    945 			r |= SAB_MODE_FRTS | SAB_MODE_RTS;
    946 		SAB_WRITE(sc, SAB_MODE, r);
    947 
    948 		r = SAB_READ(sc, SAB_PVR);
    949 		if (bits & TIOCM_DTR)
    950 			r &= ~sc->sc_pvr_dtr;
    951 		else
    952 			r |= sc->sc_pvr_dtr;
    953 		SAB_WRITE(sc, SAB_PVR, r);
    954 		break;
    955 	case DMBIS:
    956 		if (bits & TIOCM_RTS) {
    957 			r = SAB_READ(sc, SAB_MODE);
    958 			r &= ~SAB_MODE_FRTS;
    959 			r |= SAB_MODE_RTS;
    960 			SAB_WRITE(sc, SAB_MODE, r);
    961 		}
    962 		if (bits & TIOCM_DTR) {
    963 			r = SAB_READ(sc, SAB_PVR);
    964 			r &= ~sc->sc_pvr_dtr;
    965 			SAB_WRITE(sc, SAB_PVR, r);
    966 		}
    967 		break;
    968 	case DMBIC:
    969 		if (bits & TIOCM_RTS) {
    970 			r = SAB_READ(sc, SAB_MODE);
    971 			r |= SAB_MODE_FRTS | SAB_MODE_RTS;
    972 			SAB_WRITE(sc, SAB_MODE, r);
    973 		}
    974 		if (bits & TIOCM_DTR) {
    975 			r = SAB_READ(sc, SAB_PVR);
    976 			r |= sc->sc_pvr_dtr;
    977 			SAB_WRITE(sc, SAB_PVR, r);
    978 		}
    979 		break;
    980 	}
    981 	splx(s);
    982 	return (bits);
    983 }
    984 
    985 int
    986 sabttyparam(struct sabtty_softc *sc, struct tty *tp, struct termios *t)
    987 {
    988 	int s, ospeed;
    989 	tcflag_t cflag;
    990 	uint8_t dafo, r;
    991 
    992 	ospeed = sabtty_speed(t->c_ospeed);
    993 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
    994 		return (EINVAL);
    995 
    996 	s = spltty();
    997 
    998 	/* hang up line if ospeed is zero, otherwise raise dtr */
    999 	sabtty_mdmctrl(sc, TIOCM_DTR,
   1000 	    (t->c_ospeed == 0) ? DMBIC : DMBIS);
   1001 
   1002 	dafo = SAB_READ(sc, SAB_DAFO);
   1003 
   1004 	cflag = t->c_cflag;
   1005 
   1006 	if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
   1007 		cflag |= CLOCAL;
   1008 		cflag &= ~HUPCL;
   1009 	}
   1010 
   1011 	if (cflag & CSTOPB)
   1012 		dafo |= SAB_DAFO_STOP;
   1013 	else
   1014 		dafo &= ~SAB_DAFO_STOP;
   1015 
   1016 	dafo &= ~SAB_DAFO_CHL_CSIZE;
   1017 	switch (cflag & CSIZE) {
   1018 	case CS5:
   1019 		dafo |= SAB_DAFO_CHL_CS5;
   1020 		break;
   1021 	case CS6:
   1022 		dafo |= SAB_DAFO_CHL_CS6;
   1023 		break;
   1024 	case CS7:
   1025 		dafo |= SAB_DAFO_CHL_CS7;
   1026 		break;
   1027 	default:
   1028 		dafo |= SAB_DAFO_CHL_CS8;
   1029 		break;
   1030 	}
   1031 
   1032 	dafo &= ~SAB_DAFO_PARMASK;
   1033 	if (cflag & PARENB) {
   1034 		if (cflag & PARODD)
   1035 			dafo |= SAB_DAFO_PAR_ODD;
   1036 		else
   1037 			dafo |= SAB_DAFO_PAR_EVEN;
   1038 	} else
   1039 		dafo |= SAB_DAFO_PAR_NONE;
   1040 	SAB_WRITE(sc, SAB_DAFO, dafo);
   1041 
   1042 	if (!(sc->sc_flags & SABTTYF_IS_RSC) && ospeed != 0) {
   1043 		SAB_WRITE(sc, SAB_BGR, ospeed & 0xff);
   1044 		r = SAB_READ(sc, SAB_CCR2);
   1045 		r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1046 		r |= (ospeed >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1047 		SAB_WRITE(sc, SAB_CCR2, r);
   1048 	}
   1049 
   1050 	r = SAB_READ(sc, SAB_MODE);
   1051 	r |= SAB_MODE_RAC;
   1052 	if (cflag & CRTSCTS) {
   1053 		r &= ~(SAB_MODE_RTS | SAB_MODE_FCTS);
   1054 		r |= SAB_MODE_FRTS;
   1055 		sc->sc_imr1 &= ~SAB_IMR1_CSC;
   1056 	} else {
   1057 		r |= SAB_MODE_RTS | SAB_MODE_FCTS;
   1058 		r &= ~SAB_MODE_FRTS;
   1059 		sc->sc_imr1 |= SAB_IMR1_CSC;
   1060 	}
   1061 	SAB_WRITE(sc, SAB_MODE, r);
   1062 	SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1063 
   1064 	tp->t_cflag = cflag;
   1065 
   1066 	splx(s);
   1067 	return (0);
   1068 }
   1069 
   1070 int
   1071 sabtty_param(struct tty *tp, struct termios *t)
   1072 {
   1073 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
   1074 
   1075 	return (sabttyparam(sc, tp, t));
   1076 }
   1077 
   1078 void
   1079 sabtty_start(struct tty *tp)
   1080 {
   1081 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
   1082 	int s;
   1083 
   1084 	s = spltty();
   1085 	if ((tp->t_state & (TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) == 0) {
   1086 		if (ttypull(tp)) {
   1087 			sc->sc_txc = ndqb(&tp->t_outq, 0);
   1088 			sc->sc_txp = tp->t_outq.c_cf;
   1089 			tp->t_state |= TS_BUSY;
   1090 			sc->sc_imr1 &= ~(SAB_ISR1_XPR | SAB_ISR1_ALLS);
   1091 			SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1092 		}
   1093 	}
   1094 	splx(s);
   1095 }
   1096 
   1097 void
   1098 sabtty_cec_wait(struct sabtty_softc *sc)
   1099 {
   1100 	int i = 50000;
   1101 
   1102 	for (;;) {
   1103 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_CEC) == 0)
   1104 			return;
   1105 		if (--i == 0)
   1106 			break;
   1107 		DELAY(1);
   1108 	}
   1109 }
   1110 
   1111 void
   1112 sabtty_tec_wait(struct sabtty_softc *sc)
   1113 {
   1114 	int i = 200000;
   1115 
   1116 	for (;;) {
   1117 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_TEC) == 0)
   1118 			return;
   1119 		if (--i == 0)
   1120 			break;
   1121 		DELAY(1);
   1122 	}
   1123 }
   1124 
   1125 void
   1126 sabtty_reset(struct sabtty_softc *sc)
   1127 {
   1128 	/* power down */
   1129 	SAB_WRITE(sc, SAB_CCR0, 0);
   1130 
   1131 	/* set basic configuration */
   1132 	SAB_WRITE(sc, SAB_CCR0,
   1133 	    SAB_CCR0_MCE | SAB_CCR0_SC_NRZ | SAB_CCR0_SM_ASYNC);
   1134 	SAB_WRITE(sc, SAB_CCR1, SAB_CCR1_ODS | SAB_CCR1_BCR | SAB_CCR1_CM_7);
   1135 	SAB_WRITE(sc, SAB_CCR2, SAB_CCR2_BDF | SAB_CCR2_SSEL | SAB_CCR2_TOE);
   1136 	SAB_WRITE(sc, SAB_CCR3, 0);
   1137 	SAB_WRITE(sc, SAB_CCR4, SAB_CCR4_MCK4 | SAB_CCR4_EBRG | SAB_CCR4_ICD);
   1138 	SAB_WRITE(sc, SAB_MODE, SAB_MODE_RTS | SAB_MODE_FCTS | SAB_MODE_RAC);
   1139 	SAB_WRITE(sc, SAB_RFC,
   1140 	    SAB_RFC_DPS | SAB_RFC_RFDF | SAB_RFC_RFTH_32CHAR);
   1141 
   1142 	/* clear interrupts */
   1143 	sc->sc_imr0 = sc->sc_imr1 = 0xff;
   1144 	SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
   1145 	SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1146 	(void)SAB_READ(sc, SAB_ISR0);
   1147 	(void)SAB_READ(sc, SAB_ISR1);
   1148 }
   1149 
   1150 void
   1151 sabtty_flush(struct sabtty_softc *sc)
   1152 {
   1153 	/* clear rx fifo */
   1154 	sabtty_cec_wait(sc);
   1155 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1156 
   1157 	/* clear tx fifo */
   1158 	sabtty_cec_wait(sc);
   1159 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
   1160 }
   1161 
   1162 int
   1163 sabtty_speed(int rate)
   1164 {
   1165 	int i, len, r;
   1166 
   1167 	if (rate == 0)
   1168 		return (0);
   1169 	len = sizeof(sabtty_baudtable)/sizeof(sabtty_baudtable[0]);
   1170 	for (i = 0; i < len; i++) {
   1171 		if (rate == sabtty_baudtable[i].baud) {
   1172 			r = sabtty_baudtable[i].n |
   1173 			    (sabtty_baudtable[i].m << 6);
   1174 			return (r);
   1175 		}
   1176 	}
   1177 	return (-1);
   1178 }
   1179 
   1180 void
   1181 sabtty_cnputc(struct sabtty_softc *sc, int c)
   1182 {
   1183 	sabtty_tec_wait(sc);
   1184 	SAB_WRITE(sc, SAB_TIC, c);
   1185 	sabtty_tec_wait(sc);
   1186 }
   1187 
   1188 int
   1189 sabtty_cngetc(struct sabtty_softc *sc)
   1190 {
   1191 	uint8_t r, len;
   1192 
   1193 again:
   1194 	do {
   1195 		r = SAB_READ(sc, SAB_STAR);
   1196 	} while ((r & SAB_STAR_RFNE) == 0);
   1197 
   1198 	/*
   1199 	 * Ok, at least one byte in RFIFO, ask for permission to access RFIFO
   1200 	 * (I hate this chip... hate hate hate).
   1201 	 */
   1202 	sabtty_cec_wait(sc);
   1203 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
   1204 
   1205 	/* Wait for RFIFO to come ready */
   1206 	do {
   1207 		r = SAB_READ(sc, SAB_ISR0);
   1208 	} while ((r & SAB_ISR0_TCD) == 0);
   1209 
   1210 	len = SAB_READ(sc, SAB_RBCL) & (32 - 1);
   1211 	if (len == 0)
   1212 		goto again;	/* Shouldn't happen... */
   1213 
   1214 	r = SAB_READ(sc, SAB_RFIFO);
   1215 
   1216 	/*
   1217 	 * Blow away everything left in the FIFO...
   1218 	 */
   1219 	sabtty_cec_wait(sc);
   1220 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
   1221 	return (r);
   1222 }
   1223 
   1224 void
   1225 sabtty_cnpollc(struct sabtty_softc *sc, int on)
   1226 {
   1227 	uint8_t r;
   1228 
   1229 	if (on) {
   1230 		if (sc->sc_polling)
   1231 			return;
   1232 		SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) | SAB_IPC_VIS);
   1233 		r = sc->sc_pollrfc = SAB_READ(sc, SAB_RFC);
   1234 		r &= ~(SAB_RFC_RFDF);
   1235 		SAB_WRITE(sc, SAB_RFC, r);
   1236 		sabtty_cec_wait(sc);
   1237 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1238 		sc->sc_polling = 1;
   1239 	} else {
   1240 		if (!sc->sc_polling)
   1241 			return;
   1242 		SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) & ~SAB_IPC_VIS);
   1243 		SAB_WRITE(sc, SAB_RFC, sc->sc_pollrfc);
   1244 		sabtty_cec_wait(sc);
   1245 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1246 		sc->sc_polling = 0;
   1247 	}
   1248 }
   1249 
   1250 void
   1251 sab_cnputc(dev_t dev, int c)
   1252 {
   1253 	struct sabtty_softc *sc = sabtty_cons_output;
   1254 
   1255 	if (sc == NULL)
   1256 		return;
   1257 	sabtty_cnputc(sc, c);
   1258 }
   1259 
   1260 void
   1261 sab_cnpollc(dev_t dev, int on)
   1262 {
   1263 	struct sabtty_softc *sc = sabtty_cons_input;
   1264 
   1265 	sabtty_cnpollc(sc, on);
   1266 }
   1267 
   1268 int
   1269 sab_cngetc(dev_t dev)
   1270 {
   1271 	struct sabtty_softc *sc = sabtty_cons_input;
   1272 
   1273 	if (sc == NULL)
   1274 		return (-1);
   1275 	return (sabtty_cngetc(sc));
   1276 }
   1277 
   1278 void
   1279 sabtty_console_flags(struct sabtty_softc *sc)
   1280 {
   1281 	int node, channel, cookie;
   1282 	char buf[255];
   1283 
   1284 	node = sc->sc_parent->sc_node;
   1285 	channel = sc->sc_portno;
   1286 
   1287 	/* Default to channel 0 if there are no explicit prom args */
   1288 	cookie = 0;
   1289 
   1290 	if (node == prom_instance_to_package(prom_stdin())) {
   1291 		if (prom_getoption("input-device", buf, sizeof buf) == 0 &&
   1292 			strcmp("ttyb", buf) == 0)
   1293 				cookie = 1;
   1294 
   1295 		if (channel == cookie)
   1296 			sc->sc_flags |= SABTTYF_CONS_IN;
   1297 	}
   1298 
   1299 	/* Default to same channel if there are no explicit prom args */
   1300 
   1301 	if (node == prom_instance_to_package(prom_stdout())) {
   1302 		if (prom_getoption("output-device", buf, sizeof buf) == 0 &&
   1303 			strcmp("ttyb", buf) == 0)
   1304 				cookie = 1;
   1305 
   1306 		if (channel == cookie)
   1307 			sc->sc_flags |= SABTTYF_CONS_OUT;
   1308 	}
   1309 	/* Are we connected to an E250 RSC? */
   1310 	if (channel == prom_getpropint(node, "ssp-console", -1) ||
   1311 	    channel == prom_getpropint(node, "ssp-control", -1))
   1312 		sc->sc_flags |= SABTTYF_IS_RSC;
   1313 }
   1314 
   1315 void
   1316 sabtty_shutdown(void *vsc)
   1317 {
   1318 	struct sabtty_softc *sc = vsc;
   1319 
   1320 	/* Have to put the chip back into single char mode */
   1321 	sc->sc_flags |= SABTTYF_DONTDDB;
   1322 	SAB_WRITE(sc, SAB_RFC, SAB_READ(sc, SAB_RFC) & ~SAB_RFC_RFDF);
   1323 	sabtty_cec_wait(sc);
   1324 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1325 	sabtty_cec_wait(sc);
   1326 }
   1327 
   1328 #ifdef KGDB
   1329 static int
   1330 sab_kgdb_getc(void *arg)
   1331 {
   1332 	struct sabtty_softc *sc = arg;
   1333 
   1334 	return sabtty_cngetc(sc);
   1335 }
   1336 
   1337 static void
   1338 sab_kgdb_putc(void *arg, int c)
   1339 {
   1340 	struct sabtty_softc *sc = arg;
   1341 
   1342 	return sabtty_cnputc(sc, c);
   1343 }
   1344 
   1345 #ifndef KGDB_DEVRATE
   1346 #define KGDB_DEVRATE    9600
   1347 #endif
   1348 
   1349 int
   1350 sab_kgdb_check(struct sabtty_softc *sc)
   1351 {
   1352 	return strcmp(device_xname(&sc->sc_dv), KGDB_DEVNAME) == 0;
   1353 }
   1354 
   1355 void
   1356 sab_kgdb_init(struct sabtty_softc *sc)
   1357 {
   1358 	int sp;
   1359 	uint8_t dafo, r;
   1360 	extern int kgdb_break_at_attach;
   1361 
   1362 	kgdb_attach(sab_kgdb_getc, sab_kgdb_putc, sc);
   1363 	kgdb_dev = 123;	/* not used, but checked against NODEV */
   1364 
   1365 	/*
   1366 	 * Configure the port to speed/8n1
   1367 	 */
   1368 	dafo = SAB_READ(sc, SAB_DAFO);
   1369 	dafo &= ~(SAB_DAFO_STOP|SAB_DAFO_CHL_CSIZE|SAB_DAFO_PARMASK);
   1370 	dafo |= SAB_DAFO_CHL_CS8|SAB_DAFO_PAR_NONE;
   1371 	SAB_WRITE(sc, SAB_DAFO, dafo);
   1372 	sp = sabtty_speed(KGDB_DEVRATE);
   1373 	SAB_WRITE(sc, SAB_BGR, sp & 0xff);
   1374 	r = SAB_READ(sc, SAB_CCR2);
   1375 	r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1376 	r |= (sp >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1377 	SAB_WRITE(sc, SAB_CCR2, r);
   1378 
   1379 	/*
   1380 	 * If we are booting with -d, break into kgdb now
   1381 	 */
   1382 	if (kgdb_break_at_attach)
   1383 		kgdb_connect(1);
   1384 }
   1385 #endif
   1386