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