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