Home | History | Annotate | Line # | Download | only in dev
sab.c revision 1.18
      1 /*	$NetBSD: sab.c,v 1.18 2004/07/17 21:23:10 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.18 2004/07/17 21:23:10 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 		switch (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
    424 		case SABTTYF_CONS_IN:
    425 			acc = "input";
    426 			break;
    427 		case SABTTYF_CONS_OUT:
    428 			acc = "output";
    429 			break;
    430 		case SABTTYF_CONS_IN|SABTTYF_CONS_OUT:
    431 		default:
    432 			acc = "i/o";
    433 			break;
    434 		}
    435 
    436 		t.c_ispeed= 0;
    437 		t.c_ospeed = 9600;
    438 		t.c_cflag = CREAD | CS8 | HUPCL;
    439 		sc->sc_tty->t_ospeed = 0;
    440 		sabttyparam(sc, sc->sc_tty, &t);
    441 
    442 		if (sc->sc_flags & SABTTYF_CONS_IN) {
    443 			sabtty_cons_input = sc;
    444 			cn_tab->cn_pollc = sab_cnpollc;
    445 			cn_tab->cn_getc = sab_cngetc;
    446 			maj = cdevsw_lookup_major(&sabtty_cdevsw);
    447 			cn_tab->cn_dev = makedev(maj, self->dv_unit);
    448 			shutdownhook_establish(sabtty_shutdown, sc);
    449 			cn_init_magic(&sabtty_cnm_state);
    450 			cn_set_magic("\047\001"); /* default magic is BREAK */
    451 		}
    452 
    453 		if (sc->sc_flags & SABTTYF_CONS_OUT) {
    454 			sabtty_tec_wait(sc);
    455 			sabtty_cons_output = sc;
    456 			cn_tab->cn_putc = sab_cnputc;
    457 			maj = cdevsw_lookup_major(&sabtty_cdevsw);
    458 			cn_tab->cn_dev = makedev(maj, self->dv_unit);
    459 		}
    460 		aprint_normal(": console %s", acc);
    461 	} else {
    462 		/* Not a console... */
    463 		sabtty_reset(sc);
    464 	}
    465 
    466 	aprint_normal("\n");
    467 }
    468 
    469 int
    470 sabtty_intr(sc, needsoftp)
    471 	struct sabtty_softc *sc;
    472 	int *needsoftp;
    473 {
    474 	u_int8_t isr0, isr1;
    475 	int i, len = 0, needsoft = 0, r = 0, clearfifo = 0;
    476 
    477 	isr0 = SAB_READ(sc, SAB_ISR0);
    478 	isr1 = SAB_READ(sc, SAB_ISR1);
    479 
    480 	if (isr0 || isr1)
    481 		r = 1;
    482 
    483 	if (isr0 & SAB_ISR0_RPF) {
    484 		len = 32;
    485 		clearfifo = 1;
    486 	}
    487 	if (isr0 & SAB_ISR0_TCD) {
    488 		len = (32 - 1) & SAB_READ(sc, SAB_RBCL);
    489 		clearfifo = 1;
    490 	}
    491 	if (isr0 & SAB_ISR0_TIME) {
    492 		sabtty_cec_wait(sc);
    493 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
    494 	}
    495 	if (isr0 & SAB_ISR0_RFO) {
    496 		sc->sc_flags |= SABTTYF_RINGOVERFLOW;
    497 		clearfifo = 1;
    498 	}
    499 	if (len != 0) {
    500 		u_int8_t *ptr, b;
    501 
    502 		ptr = sc->sc_rput;
    503 		for (i = 0; i < len; i++) {
    504 			b = SAB_READ(sc, SAB_RFIFO);
    505 			if (i % 2 == 0) /* skip status byte */
    506 				cn_check_magic(sc->sc_tty->t_dev,
    507 					       b, sabtty_cnm_state);
    508 			*ptr++ = b;
    509 			if (ptr == sc->sc_rend)
    510 				ptr = sc->sc_rbuf;
    511 			if (ptr == sc->sc_rget) {
    512 				if (ptr == sc->sc_rbuf)
    513 					ptr = sc->sc_rend;
    514 				ptr--;
    515 				sc->sc_flags |= SABTTYF_RINGOVERFLOW;
    516 			}
    517 		}
    518 		sc->sc_rput = ptr;
    519 		needsoft = 1;
    520 	}
    521 
    522 	if (clearfifo) {
    523 		sabtty_cec_wait(sc);
    524 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
    525 	}
    526 
    527 	if (isr0 & SAB_ISR0_CDSC) {
    528 		sc->sc_flags |= SABTTYF_CDCHG;
    529 		needsoft = 1;
    530 	}
    531 
    532 	if (isr1 & SAB_ISR1_BRKT)
    533 		cn_check_magic(sc->sc_tty->t_dev,
    534 			       CNC_BREAK, sabtty_cnm_state);
    535 
    536 	if (isr1 & (SAB_ISR1_XPR | SAB_ISR1_ALLS)) {
    537 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_XFW) &&
    538 		    (sc->sc_flags & SABTTYF_STOP) == 0) {
    539 			if (sc->sc_txc < 32)
    540 				len = sc->sc_txc;
    541 			else
    542 				len = 32;
    543 
    544 			if (len > 0) {
    545 				SAB_WRITE_BLOCK(sc, SAB_XFIFO, sc->sc_txp, len);
    546 				sc->sc_txp += len;
    547 				sc->sc_txc -= len;
    548 
    549 				sabtty_cec_wait(sc);
    550 				SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XF);
    551 
    552 				/*
    553 				 * Prevent the false end of xmit from
    554 				 * confusing things below.
    555 				 */
    556 				isr1 &= ~SAB_ISR1_ALLS;
    557 			}
    558 		}
    559 
    560 		if ((sc->sc_txc == 0) || (sc->sc_flags & SABTTYF_STOP)) {
    561 			if ((sc->sc_imr1 & SAB_IMR1_XPR) == 0) {
    562 				sc->sc_imr1 |= SAB_IMR1_XPR;
    563 				sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    564 				SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    565 			}
    566 		}
    567 	}
    568 
    569 	if ((isr1 & SAB_ISR1_ALLS) && ((sc->sc_txc == 0) ||
    570 	    (sc->sc_flags & SABTTYF_STOP))) {
    571 		if (sc->sc_flags & SABTTYF_TXDRAIN)
    572 			wakeup(sc);
    573 		sc->sc_flags &= ~SABTTYF_STOP;
    574 		sc->sc_flags |= SABTTYF_DONE;
    575 		sc->sc_imr1 |= SAB_IMR1_ALLS;
    576 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    577 		needsoft = 1;
    578 	}
    579 
    580 	if (needsoft)
    581 		*needsoftp = needsoft;
    582 	return (r);
    583 }
    584 
    585 void
    586 sabtty_softintr(sc)
    587 	struct sabtty_softc *sc;
    588 {
    589 	struct tty *tp = sc->sc_tty;
    590 	int s, flags;
    591 	u_int8_t r;
    592 
    593 	if (tp == NULL)
    594 		return;
    595 
    596 	if ((tp->t_state & TS_ISOPEN) == 0)
    597 		return;
    598 
    599 	while (sc->sc_rget != sc->sc_rput) {
    600 		int data;
    601 		u_int8_t stat;
    602 
    603 		data = sc->sc_rget[0];
    604 		stat = sc->sc_rget[1];
    605 		sc->sc_rget += 2;
    606 		if (stat & SAB_RSTAT_PE)
    607 			data |= TTY_PE;
    608 		if (stat & SAB_RSTAT_FE)
    609 			data |= TTY_FE;
    610 		if (sc->sc_rget == sc->sc_rend)
    611 			sc->sc_rget = sc->sc_rbuf;
    612 
    613 		(*tp->t_linesw->l_rint)(data, tp);
    614 	}
    615 
    616 	s = splhigh();
    617 	flags = sc->sc_flags;
    618 	sc->sc_flags &= ~(SABTTYF_DONE|SABTTYF_CDCHG|SABTTYF_RINGOVERFLOW);
    619 	splx(s);
    620 
    621 	if (flags & SABTTYF_CDCHG) {
    622 		s = spltty();
    623 		r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD;
    624 		splx(s);
    625 
    626 		(*tp->t_linesw->l_modem)(tp, r);
    627 	}
    628 
    629 	if (flags & SABTTYF_RINGOVERFLOW)
    630 		log(LOG_WARNING, "%s: ring overflow\n", sc->sc_dv.dv_xname);
    631 
    632 	if (flags & SABTTYF_DONE) {
    633 		ndflush(&tp->t_outq, sc->sc_txp - tp->t_outq.c_cf);
    634 		tp->t_state &= ~TS_BUSY;
    635 		(*tp->t_linesw->l_start)(tp);
    636 	}
    637 }
    638 
    639 int
    640 sabopen(dev, flags, mode, p)
    641 	dev_t dev;
    642 	int flags, mode;
    643 	struct proc *p;
    644 {
    645 	struct sabtty_softc *sc;
    646 	struct tty *tp;
    647 	int s, s1;
    648 
    649 	sc = device_lookup(&sabtty_cd, SABUNIT(dev));
    650 	if (sc == NULL)
    651 		return (ENXIO);
    652 
    653 	tp = sc->sc_tty;
    654 	tp->t_dev = dev;
    655 
    656 	if ((tp->t_state & TS_ISOPEN) == 0) {
    657 		ttychars(tp);
    658 		tp->t_iflag = TTYDEF_IFLAG;
    659 		tp->t_oflag = TTYDEF_OFLAG;
    660 		tp->t_cflag = TTYDEF_CFLAG;
    661 		if (sc->sc_openflags & TIOCFLAG_CLOCAL)
    662 			tp->t_cflag |= CLOCAL;
    663 		if (sc->sc_openflags & TIOCFLAG_CRTSCTS)
    664 			tp->t_cflag |= CRTSCTS;
    665 		if (sc->sc_openflags & TIOCFLAG_MDMBUF)
    666 			tp->t_cflag |= MDMBUF;
    667 		tp->t_lflag = TTYDEF_LFLAG;
    668 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    669 
    670 		sc->sc_rput = sc->sc_rget = sc->sc_rbuf;
    671 
    672 		s = spltty();
    673 
    674 		ttsetwater(tp);
    675 
    676 		s1 = splhigh();
    677 		sabtty_reset(sc);
    678 		sabtty_param(tp, &tp->t_termios);
    679 		sc->sc_imr0 = SAB_IMR0_PERR | SAB_IMR0_FERR | SAB_IMR0_PLLA;
    680 		SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
    681 		sc->sc_imr1 = SAB_IMR1_BRK | SAB_IMR1_ALLS | SAB_IMR1_XDU |
    682 		    SAB_IMR1_TIN | SAB_IMR1_CSC | SAB_IMR1_XMR | SAB_IMR1_XPR;
    683 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    684 		SAB_WRITE(sc, SAB_CCR0, SAB_READ(sc, SAB_CCR0) | SAB_CCR0_PU);
    685 		sabtty_cec_wait(sc);
    686 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
    687 		sabtty_cec_wait(sc);
    688 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
    689 		sabtty_cec_wait(sc);
    690 		splx(s1);
    691 
    692 		sabtty_flush(sc);
    693 
    694 		if ((sc->sc_openflags & TIOCFLAG_SOFTCAR) ||
    695 		    (SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD))
    696 			tp->t_state |= TS_CARR_ON;
    697 		else
    698 			tp->t_state &= ~TS_CARR_ON;
    699 	} else if ((tp->t_state & TS_XCLUDE) &&
    700 	    (!suser(p->p_ucred, &p->p_acflag))) {
    701 		return (EBUSY);
    702 	} else {
    703 		s = spltty();
    704 	}
    705 
    706 	if ((flags & O_NONBLOCK) == 0) {
    707 		while ((tp->t_cflag & CLOCAL) == 0 &&
    708 		    (tp->t_state & TS_CARR_ON) == 0) {
    709 			int error;
    710 
    711 			error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
    712 			    "sabttycd", 0);
    713 			if (error != 0) {
    714 				splx(s);
    715 				return (error);
    716 			}
    717 		}
    718 	}
    719 
    720 	splx(s);
    721 
    722 	s = (*tp->t_linesw->l_open)(dev, tp);
    723 	if (s != 0) {
    724 		if (tp->t_state & TS_ISOPEN)
    725 			return (s);
    726 
    727 		if (tp->t_cflag & HUPCL) {
    728 			sabtty_mdmctrl(sc, 0, DMSET);
    729 			(void)tsleep(sc, TTIPRI, ttclos, hz);
    730 		}
    731 
    732 		if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
    733 			/* Flush and power down if we're not the console */
    734 			sabtty_flush(sc);
    735 			sabtty_reset(sc);
    736 		}
    737 	}
    738 	return (s);
    739 }
    740 
    741 int
    742 sabclose(dev, flags, mode, p)
    743 	dev_t dev;
    744 	int flags, mode;
    745 	struct proc *p;
    746 {
    747 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
    748 	struct sab_softc *bc = sc->sc_parent;
    749 	struct tty *tp = sc->sc_tty;
    750 	int s;
    751 
    752 	(*tp->t_linesw->l_close)(tp, flags);
    753 
    754 	s = spltty();
    755 
    756 	if ((tp->t_state & TS_ISOPEN) == 0) {
    757 		/* Wait for output drain */
    758 		sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    759 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    760 		sc->sc_flags |= SABTTYF_TXDRAIN;
    761 		(void)tsleep(sc, TTIPRI, ttclos, 5 * hz);
    762 		sc->sc_imr1 |= SAB_IMR1_ALLS;
    763 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    764 		sc->sc_flags &= ~SABTTYF_TXDRAIN;
    765 
    766 		if (tp->t_cflag & HUPCL) {
    767 			sabtty_mdmctrl(sc, 0, DMSET);
    768 			(void)tsleep(bc, TTIPRI, ttclos, hz);
    769 		}
    770 
    771 		if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
    772 			/* Flush and power down if we're not the console */
    773 			sabtty_flush(sc);
    774 			sabtty_reset(sc);
    775 		}
    776 	}
    777 
    778 	ttyclose(tp);
    779 	splx(s);
    780 
    781 	return (0);
    782 }
    783 
    784 int
    785 sabread(dev, uio, flags)
    786 	dev_t dev;
    787 	struct uio *uio;
    788 	int flags;
    789 {
    790 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
    791 	struct tty *tp = sc->sc_tty;
    792 
    793 	return ((*tp->t_linesw->l_read)(tp, uio, flags));
    794 }
    795 
    796 int
    797 sabwrite(dev, uio, flags)
    798 	dev_t dev;
    799 	struct uio *uio;
    800 	int flags;
    801 {
    802 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
    803 	struct tty *tp = sc->sc_tty;
    804 
    805 	return ((*tp->t_linesw->l_write)(tp, uio, flags));
    806 }
    807 
    808 int
    809 sabioctl(dev, cmd, data, flags, p)
    810 	dev_t dev;
    811 	u_long cmd;
    812 	caddr_t data;
    813 	int flags;
    814 	struct proc *p;
    815 {
    816 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
    817 	struct tty *tp = sc->sc_tty;
    818 	int error;
    819 
    820 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flags, p);
    821 	if (error >= 0)
    822 		return (error);
    823 
    824 	error = ttioctl(tp, cmd, data, flags, p);
    825 	if (error >= 0)
    826 		return (error);
    827 
    828 	error = 0;
    829 
    830 	switch (cmd) {
    831 	case TIOCSBRK:
    832 		SAB_WRITE(sc, SAB_DAFO,
    833 		    SAB_READ(sc, SAB_DAFO) | SAB_DAFO_XBRK);
    834 		break;
    835 	case TIOCCBRK:
    836 		SAB_WRITE(sc, SAB_DAFO,
    837 		    SAB_READ(sc, SAB_DAFO) & ~SAB_DAFO_XBRK);
    838 		break;
    839 	case TIOCSDTR:
    840 		sabtty_mdmctrl(sc, TIOCM_DTR, DMBIS);
    841 		break;
    842 	case TIOCCDTR:
    843 		sabtty_mdmctrl(sc, TIOCM_DTR, DMBIC);
    844 		break;
    845 	case TIOCMBIS:
    846 		sabtty_mdmctrl(sc, *((int *)data), DMBIS);
    847 		break;
    848 	case TIOCMBIC:
    849 		sabtty_mdmctrl(sc, *((int *)data), DMBIC);
    850 		break;
    851 	case TIOCMGET:
    852 		*((int *)data) = sabtty_mdmctrl(sc, 0, DMGET);
    853 		break;
    854 	case TIOCMSET:
    855 		sabtty_mdmctrl(sc, *((int *)data), DMSET);
    856 		break;
    857 	case TIOCGFLAGS:
    858 		*((int *)data) = sc->sc_openflags;
    859 		break;
    860 	case TIOCSFLAGS:
    861 		if (suser(p->p_ucred, &p->p_acflag))
    862 			error = EPERM;
    863 		else
    864 			sc->sc_openflags = *((int *)data) &
    865 			    (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    866 			     TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    867 		break;
    868 	default:
    869 		error = ENOTTY;
    870 	}
    871 
    872 	return (error);
    873 }
    874 
    875 struct tty *
    876 sabtty(dev)
    877 	dev_t dev;
    878 {
    879 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
    880 
    881 	return (sc->sc_tty);
    882 }
    883 
    884 void
    885 sabstop(tp, flag)
    886 	struct tty *tp;
    887 	int flag;
    888 {
    889 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
    890 	int s;
    891 
    892 	s = spltty();
    893 	if (tp->t_state & TS_BUSY) {
    894 		if ((tp->t_state & TS_TTSTOP) == 0)
    895 			tp->t_state |= TS_FLUSH;
    896 		sc->sc_flags |= SABTTYF_STOP;
    897 		sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    898 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    899 	}
    900 	splx(s);
    901 }
    902 
    903 int
    904 sabpoll(dev, events, p)
    905 	dev_t dev;
    906 	int events;
    907 	struct proc *p;
    908 {
    909 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
    910 	struct tty *tp = sc->sc_tty;
    911 
    912 	return ((*tp->t_linesw->l_poll)(tp, events, p));
    913 }
    914 
    915 int
    916 sabtty_mdmctrl(sc, bits, how)
    917 	struct sabtty_softc *sc;
    918 	int bits, how;
    919 {
    920 	u_int8_t r;
    921 	int s;
    922 
    923 	s = spltty();
    924 	switch (how) {
    925 	case DMGET:
    926 		bits = 0;
    927 		if (SAB_READ(sc, SAB_STAR) & SAB_STAR_CTS)
    928 			bits |= TIOCM_CTS;
    929 		if ((SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD) == 0)
    930 			bits |= TIOCM_CD;
    931 
    932 		r = SAB_READ(sc, SAB_PVR);
    933 		if ((r & sc->sc_pvr_dtr) == 0)
    934 			bits |= TIOCM_DTR;
    935 		if ((r & sc->sc_pvr_dsr) == 0)
    936 			bits |= TIOCM_DSR;
    937 
    938 		r = SAB_READ(sc, SAB_MODE);
    939 		if ((r & (SAB_MODE_RTS|SAB_MODE_FRTS)) == SAB_MODE_RTS)
    940 			bits |= TIOCM_RTS;
    941 		break;
    942 	case DMSET:
    943 		r = SAB_READ(sc, SAB_MODE);
    944 		if (bits & TIOCM_RTS) {
    945 			r &= ~SAB_MODE_FRTS;
    946 			r |= SAB_MODE_RTS;
    947 		} else
    948 			r |= SAB_MODE_FRTS | SAB_MODE_RTS;
    949 		SAB_WRITE(sc, SAB_MODE, r);
    950 
    951 		r = SAB_READ(sc, SAB_PVR);
    952 		if (bits & TIOCM_DTR)
    953 			r &= ~sc->sc_pvr_dtr;
    954 		else
    955 			r |= sc->sc_pvr_dtr;
    956 		SAB_WRITE(sc, SAB_PVR, r);
    957 		break;
    958 	case DMBIS:
    959 		if (bits & TIOCM_RTS) {
    960 			r = SAB_READ(sc, SAB_MODE);
    961 			r &= ~SAB_MODE_FRTS;
    962 			r |= SAB_MODE_RTS;
    963 			SAB_WRITE(sc, SAB_MODE, r);
    964 		}
    965 		if (bits & TIOCM_DTR) {
    966 			r = SAB_READ(sc, SAB_PVR);
    967 			r &= ~sc->sc_pvr_dtr;
    968 			SAB_WRITE(sc, SAB_PVR, r);
    969 		}
    970 		break;
    971 	case DMBIC:
    972 		if (bits & TIOCM_RTS) {
    973 			r = SAB_READ(sc, SAB_MODE);
    974 			r |= SAB_MODE_FRTS | SAB_MODE_RTS;
    975 			SAB_WRITE(sc, SAB_MODE, r);
    976 		}
    977 		if (bits & TIOCM_DTR) {
    978 			r = SAB_READ(sc, SAB_PVR);
    979 			r |= sc->sc_pvr_dtr;
    980 			SAB_WRITE(sc, SAB_PVR, r);
    981 		}
    982 		break;
    983 	}
    984 	splx(s);
    985 	return (bits);
    986 }
    987 
    988 int
    989 sabttyparam(sc, tp, t)
    990 	struct sabtty_softc *sc;
    991 	struct tty *tp;
    992 	struct termios *t;
    993 {
    994 	int s, ospeed;
    995 	tcflag_t cflag;
    996 	u_int8_t dafo, r;
    997 
    998 	ospeed = sabtty_speed(t->c_ospeed);
    999 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
   1000 		return (EINVAL);
   1001 
   1002 	s = spltty();
   1003 
   1004 	/* hang up line if ospeed is zero, otherwise raise dtr */
   1005 	sabtty_mdmctrl(sc, TIOCM_DTR,
   1006 	    (t->c_ospeed == 0) ? DMBIC : DMBIS);
   1007 
   1008 	dafo = SAB_READ(sc, SAB_DAFO);
   1009 
   1010 	cflag = t->c_cflag;
   1011 
   1012 	if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
   1013 		cflag |= CLOCAL;
   1014 		cflag &= ~HUPCL;
   1015 	}
   1016 
   1017 	if (cflag & CSTOPB)
   1018 		dafo |= SAB_DAFO_STOP;
   1019 	else
   1020 		dafo &= ~SAB_DAFO_STOP;
   1021 
   1022 	dafo &= ~SAB_DAFO_CHL_CSIZE;
   1023 	switch (cflag & CSIZE) {
   1024 	case CS5:
   1025 		dafo |= SAB_DAFO_CHL_CS5;
   1026 		break;
   1027 	case CS6:
   1028 		dafo |= SAB_DAFO_CHL_CS6;
   1029 		break;
   1030 	case CS7:
   1031 		dafo |= SAB_DAFO_CHL_CS7;
   1032 		break;
   1033 	default:
   1034 		dafo |= SAB_DAFO_CHL_CS8;
   1035 		break;
   1036 	}
   1037 
   1038 	dafo &= ~SAB_DAFO_PARMASK;
   1039 	if (cflag & PARENB) {
   1040 		if (cflag & PARODD)
   1041 			dafo |= SAB_DAFO_PAR_ODD;
   1042 		else
   1043 			dafo |= SAB_DAFO_PAR_EVEN;
   1044 	} else
   1045 		dafo |= SAB_DAFO_PAR_NONE;
   1046 
   1047 	if (ospeed != 0) {
   1048 		SAB_WRITE(sc, SAB_BGR, ospeed & 0xff);
   1049 		r = SAB_READ(sc, SAB_CCR2);
   1050 		r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1051 		r |= (ospeed >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1052 		SAB_WRITE(sc, SAB_CCR2, r);
   1053 	}
   1054 
   1055 	r = SAB_READ(sc, SAB_MODE);
   1056 	r |= SAB_MODE_RAC;
   1057 	if (cflag & CRTSCTS) {
   1058 		r &= ~(SAB_MODE_RTS | SAB_MODE_FCTS);
   1059 		r |= SAB_MODE_FRTS;
   1060 		sc->sc_imr1 &= ~SAB_IMR1_CSC;
   1061 	} else {
   1062 		r |= SAB_MODE_RTS | SAB_MODE_FCTS;
   1063 		r &= ~SAB_MODE_FRTS;
   1064 		sc->sc_imr1 |= SAB_IMR1_CSC;
   1065 	}
   1066 	SAB_WRITE(sc, SAB_MODE, r);
   1067 	SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1068 
   1069 	tp->t_cflag = cflag;
   1070 
   1071 	splx(s);
   1072 	return (0);
   1073 }
   1074 
   1075 int
   1076 sabtty_param(tp, t)
   1077 	struct tty *tp;
   1078 	struct termios *t;
   1079 {
   1080 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
   1081 
   1082 	return (sabttyparam(sc, tp, t));
   1083 }
   1084 
   1085 void
   1086 sabtty_start(tp)
   1087 	struct tty *tp;
   1088 {
   1089 	struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
   1090 	int s;
   1091 
   1092 	s = spltty();
   1093 	if ((tp->t_state & (TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) == 0) {
   1094 		if (tp->t_outq.c_cc <= tp->t_lowat) {
   1095 			if (tp->t_state & TS_ASLEEP) {
   1096 				tp->t_state &= ~TS_ASLEEP;
   1097 				wakeup(&tp->t_outq);
   1098 			}
   1099 			selwakeup(&tp->t_wsel);
   1100 		}
   1101 		if (tp->t_outq.c_cc) {
   1102 			sc->sc_txc = ndqb(&tp->t_outq, 0);
   1103 			sc->sc_txp = tp->t_outq.c_cf;
   1104 			tp->t_state |= TS_BUSY;
   1105 			sc->sc_imr1 &= ~(SAB_ISR1_XPR | SAB_ISR1_ALLS);
   1106 			SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1107 		}
   1108 	}
   1109 	splx(s);
   1110 }
   1111 
   1112 void
   1113 sabtty_cec_wait(sc)
   1114 	struct sabtty_softc *sc;
   1115 {
   1116 	int i = 50000;
   1117 
   1118 	for (;;) {
   1119 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_CEC) == 0)
   1120 			return;
   1121 		if (--i == 0)
   1122 			break;
   1123 		DELAY(1);
   1124 	}
   1125 }
   1126 
   1127 void
   1128 sabtty_tec_wait(sc)
   1129 	struct sabtty_softc *sc;
   1130 {
   1131 	int i = 200000;
   1132 
   1133 	for (;;) {
   1134 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_TEC) == 0)
   1135 			return;
   1136 		if (--i == 0)
   1137 			break;
   1138 		DELAY(1);
   1139 	}
   1140 }
   1141 
   1142 void
   1143 sabtty_reset(sc)
   1144 	struct sabtty_softc *sc;
   1145 {
   1146 	/* power down */
   1147 	SAB_WRITE(sc, SAB_CCR0, 0);
   1148 
   1149 	/* set basic configuration */
   1150 	SAB_WRITE(sc, SAB_CCR0,
   1151 	    SAB_CCR0_MCE | SAB_CCR0_SC_NRZ | SAB_CCR0_SM_ASYNC);
   1152 	SAB_WRITE(sc, SAB_CCR1, SAB_CCR1_ODS | SAB_CCR1_BCR | SAB_CCR1_CM_7);
   1153 	SAB_WRITE(sc, SAB_CCR2, SAB_CCR2_BDF | SAB_CCR2_SSEL | SAB_CCR2_TOE);
   1154 	SAB_WRITE(sc, SAB_CCR3, 0);
   1155 	SAB_WRITE(sc, SAB_CCR4, SAB_CCR4_MCK4 | SAB_CCR4_EBRG);
   1156 	SAB_WRITE(sc, SAB_MODE, SAB_MODE_RTS | SAB_MODE_FCTS | SAB_MODE_RAC);
   1157 	SAB_WRITE(sc, SAB_RFC,
   1158 	    SAB_RFC_DPS | SAB_RFC_RFDF | SAB_RFC_RFTH_32CHAR);
   1159 
   1160 	/* clear interrupts */
   1161 	sc->sc_imr0 = sc->sc_imr1 = 0xff;
   1162 	SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
   1163 	SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1164 	SAB_READ(sc, SAB_ISR0);
   1165 	SAB_READ(sc, SAB_ISR1);
   1166 }
   1167 
   1168 void
   1169 sabtty_flush(sc)
   1170 	struct sabtty_softc *sc;
   1171 {
   1172 	/* clear rx fifo */
   1173 	sabtty_cec_wait(sc);
   1174 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1175 
   1176 	/* clear tx fifo */
   1177 	sabtty_cec_wait(sc);
   1178 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
   1179 }
   1180 
   1181 int
   1182 sabtty_speed(rate)
   1183 	int rate;
   1184 {
   1185 	int i, len, r;
   1186 
   1187 	if (rate == 0)
   1188 		return (0);
   1189 	len = sizeof(sabtty_baudtable)/sizeof(sabtty_baudtable[0]);
   1190 	for (i = 0; i < len; i++) {
   1191 		if (rate == sabtty_baudtable[i].baud) {
   1192 			r = sabtty_baudtable[i].n |
   1193 			    (sabtty_baudtable[i].m << 6);
   1194 			return (r);
   1195 		}
   1196 	}
   1197 	return (-1);
   1198 }
   1199 
   1200 void
   1201 sabtty_cnputc(sc, c)
   1202 	struct sabtty_softc *sc;
   1203 	int c;
   1204 {
   1205 	sabtty_tec_wait(sc);
   1206 	SAB_WRITE(sc, SAB_TIC, c);
   1207 	sabtty_tec_wait(sc);
   1208 }
   1209 
   1210 int
   1211 sabtty_cngetc(sc)
   1212 	struct sabtty_softc *sc;
   1213 {
   1214 	u_int8_t r, len;
   1215 
   1216 again:
   1217 	do {
   1218 		r = SAB_READ(sc, SAB_STAR);
   1219 	} while ((r & SAB_STAR_RFNE) == 0);
   1220 
   1221 	/*
   1222 	 * Ok, at least one byte in RFIFO, ask for permission to access RFIFO
   1223 	 * (I hate this chip... hate hate hate).
   1224 	 */
   1225 	sabtty_cec_wait(sc);
   1226 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
   1227 
   1228 	/* Wait for RFIFO to come ready */
   1229 	do {
   1230 		r = SAB_READ(sc, SAB_ISR0);
   1231 	} while ((r & SAB_ISR0_TCD) == 0);
   1232 
   1233 	len = SAB_READ(sc, SAB_RBCL) & (32 - 1);
   1234 	if (len == 0)
   1235 		goto again;	/* Shouldn't happen... */
   1236 
   1237 	r = SAB_READ(sc, SAB_RFIFO);
   1238 
   1239 	/*
   1240 	 * Blow away everything left in the FIFO...
   1241 	 */
   1242 	sabtty_cec_wait(sc);
   1243 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
   1244 	return (r);
   1245 }
   1246 
   1247 void
   1248 sabtty_cnpollc(sc, on)
   1249 	struct sabtty_softc *sc;
   1250 	int on;
   1251 {
   1252 	u_int8_t r;
   1253 
   1254 	if (on) {
   1255 		if (sc->sc_polling)
   1256 			return;
   1257 		SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) | SAB_IPC_VIS);
   1258 		r = sc->sc_pollrfc = SAB_READ(sc, SAB_RFC);
   1259 		r &= ~(SAB_RFC_RFDF);
   1260 		SAB_WRITE(sc, SAB_RFC, r);
   1261 		sabtty_cec_wait(sc);
   1262 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1263 		sc->sc_polling = 1;
   1264 	} else {
   1265 		if (!sc->sc_polling)
   1266 			return;
   1267 		SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) & ~SAB_IPC_VIS);
   1268 		SAB_WRITE(sc, SAB_RFC, sc->sc_pollrfc);
   1269 		sabtty_cec_wait(sc);
   1270 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1271 		sc->sc_polling = 0;
   1272 	}
   1273 }
   1274 
   1275 void
   1276 sab_cnputc(dev, c)
   1277 	dev_t dev;
   1278 	int c;
   1279 {
   1280 	struct sabtty_softc *sc = sabtty_cons_output;
   1281 
   1282 	if (sc == NULL)
   1283 		return;
   1284 	sabtty_cnputc(sc, c);
   1285 }
   1286 
   1287 void
   1288 sab_cnpollc(dev, on)
   1289 	dev_t dev;
   1290 	int on;
   1291 {
   1292 	struct sabtty_softc *sc = sabtty_cons_input;
   1293 
   1294 	sabtty_cnpollc(sc, on);
   1295 }
   1296 
   1297 int
   1298 sab_cngetc(dev)
   1299 	dev_t dev;
   1300 {
   1301 	struct sabtty_softc *sc = sabtty_cons_input;
   1302 
   1303 	if (sc == NULL)
   1304 		return (-1);
   1305 	return (sabtty_cngetc(sc));
   1306 }
   1307 
   1308 void
   1309 sabtty_console_flags(sc)
   1310 	struct sabtty_softc *sc;
   1311 {
   1312 	int node, channel, cookie;
   1313 	char buf[255];
   1314 
   1315 	node = sc->sc_parent->sc_node;
   1316 	channel = sc->sc_portno;
   1317 
   1318 	/* Default to channel 0 if there are no explicit prom args */
   1319 	cookie = 0;
   1320 
   1321 	if (node == prom_instance_to_package(prom_stdin())) {
   1322 		if (prom_getoption("input-device", buf, sizeof buf) == 0 &&
   1323 			strcmp("ttyb", buf) == 0)
   1324 				cookie = 1;
   1325 
   1326 		if (channel == cookie)
   1327 			sc->sc_flags |= SABTTYF_CONS_IN;
   1328 	}
   1329 
   1330 	/* Default to same channel if there are no explicit prom args */
   1331 
   1332 	if (node == prom_instance_to_package(prom_stdout())) {
   1333 		if (prom_getoption("output-device", buf, sizeof buf) == 0 &&
   1334 			strcmp("ttyb", buf) == 0)
   1335 				cookie = 1;
   1336 
   1337 		if (channel == cookie)
   1338 			sc->sc_flags |= SABTTYF_CONS_OUT;
   1339 	}
   1340 }
   1341 
   1342 void
   1343 sabtty_shutdown(vsc)
   1344 	void *vsc;
   1345 {
   1346 	struct sabtty_softc *sc = vsc;
   1347 
   1348 	/* Have to put the chip back into single char mode */
   1349 	sc->sc_flags |= SABTTYF_DONTDDB;
   1350 	SAB_WRITE(sc, SAB_RFC, SAB_READ(sc, SAB_RFC) & ~SAB_RFC_RFDF);
   1351 	sabtty_cec_wait(sc);
   1352 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1353 	sabtty_cec_wait(sc);
   1354 }
   1355