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