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