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