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