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