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