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