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