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