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