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