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