xref: /netbsd-src/sys/arch/sparc/dev/zs.c (revision 2a399c6883d870daece976daec6ffa7bb7f934ce)
1 /*	$NetBSD: zs.c,v 1.54 1997/11/12 22:18:54 pk Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Zilog Z8530 Dual UART driver (machine-dependent part)
41  *
42  * Runs two serial lines per chip using slave drivers.
43  * Plain tty/async lines use the zs_async slave.
44  * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves.
45  */
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/conf.h>
50 #include <sys/device.h>
51 #include <sys/file.h>
52 #include <sys/ioctl.h>
53 #include <sys/kernel.h>
54 #include <sys/proc.h>
55 #include <sys/tty.h>
56 #include <sys/time.h>
57 #include <sys/syslog.h>
58 
59 #include <machine/autoconf.h>
60 #include <machine/bsd_openprom.h>
61 #include <machine/conf.h>
62 #include <machine/cpu.h>
63 #include <machine/eeprom.h>
64 #include <machine/psl.h>
65 #include <machine/z8530var.h>
66 
67 #include <dev/cons.h>
68 #include <dev/ic/z8530reg.h>
69 
70 #include <sparc/sparc/vaddrs.h>
71 #include <sparc/sparc/auxreg.h>
72 #include <sparc/dev/cons.h>
73 
74 #include "kbd.h"	/* NKBD */
75 #include "zs.h" 	/* NZS */
76 
77 /* Make life easier for the initialized arrays here. */
78 #if NZS < 3
79 #undef  NZS
80 #define NZS 3
81 #endif
82 
83 /*
84  * Some warts needed by z8530tty.c -
85  * The default parity REALLY needs to be the same as the PROM uses,
86  * or you can not see messages done with printf during boot-up...
87  */
88 int zs_def_cflag = (CREAD | CS8 | HUPCL);
89 int zs_major = 12;
90 
91 /*
92  * The Sun provides a 4.9152 MHz clock to the ZS chips.
93  */
94 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
95 
96 /*
97  * Select software interrupt bit based on TTY ipl.
98  */
99 #if PIL_TTY == 1
100 # define IE_ZSSOFT IE_L1
101 #elif PIL_TTY == 4
102 # define IE_ZSSOFT IE_L4
103 #elif PIL_TTY == 6
104 # define IE_ZSSOFT IE_L6
105 #else
106 # error "no suitable software interrupt bit"
107 #endif
108 
109 /*
110  * The next three variables provide a shortcut to the channel state
111  * structure used by zscnputc().
112  */
113 int zs_console_unit = -1;
114 int zs_console_channel = -1;
115 struct zs_chanstate *zs_conschanstate;
116 
117 #define	ZS_DELAY()		(CPU_ISSUN4C ? (0) : delay(2))
118 
119 /* The layout of this is hardware-dependent (padding, order). */
120 struct zschan {
121 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
122 	u_char		zc_xxx0;
123 	volatile u_char	zc_data;	/* data */
124 	u_char		zc_xxx1;
125 };
126 struct zsdevice {
127 	/* Yes, they are backwards. */
128 	struct	zschan zs_chan_b;
129 	struct	zschan zs_chan_a;
130 };
131 
132 /* Saved PROM mappings */
133 static struct zsdevice *zsaddr[NZS];
134 
135 /* Flags from cninit() */
136 static int zs_hwflags[NZS][2];
137 
138 /* Default speed for each channel */
139 static int zs_defspeed[NZS][2] = {
140 	{ 9600, 	/* ttya */
141 	  9600 },	/* ttyb */
142 	{ 1200, 	/* keyboard */
143 	  1200 },	/* mouse */
144 	{ 9600, 	/* ttyc */
145 	  9600 },	/* ttyd */
146 };
147 
148 static u_char zs_init_reg[16] = {
149 	0,	/* 0: CMD (reset, etc.) */
150 	0,	/* 1: No interrupts yet. */
151 	0,	/* 2: IVECT */
152 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
153 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
154 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
155 	0,	/* 6: TXSYNC/SYNCLO */
156 	0,	/* 7: RXSYNC/SYNCHI */
157 	0,	/* 8: alias for data port */
158 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
159 	0,	/*10: Misc. TX/RX control bits */
160 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
161 	14,	/*12: BAUDLO (default=9600) */
162 	0,	/*13: BAUDHI (default=9600) */
163 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
164 	ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
165 };
166 
167 struct zschan *
168 zs_get_chan_addr(zs_unit, channel)
169 	int zs_unit, channel;
170 {
171 	struct zsdevice *addr;
172 	struct zschan *zc;
173 
174 	if (zs_unit >= NZS)
175 		return NULL;
176 	addr = zsaddr[zs_unit];
177 	if (addr == NULL)
178 		addr = zsaddr[zs_unit] = findzs(zs_unit);
179 	if (addr == NULL)
180 		return NULL;
181 	if (channel == 0) {
182 		zc = &addr->zs_chan_a;
183 	} else {
184 		zc = &addr->zs_chan_b;
185 	}
186 	return (zc);
187 }
188 
189 
190 /****************************************************************
191  * Autoconfig
192  ****************************************************************/
193 
194 /* Definition of the driver for autoconfig. */
195 static int	zs_match __P((struct device *, struct cfdata *, void *));
196 static void	zs_attach __P((struct device *, struct device *, void *));
197 static int  zs_print __P((void *, const char *name));
198 
199 struct cfattach zs_ca = {
200 	sizeof(struct zsc_softc), zs_match, zs_attach
201 };
202 
203 struct cfdriver zs_cd = {
204 	NULL, "zs", DV_DULL
205 };
206 
207 /* Interrupt handlers. */
208 static int zshard __P((void *));
209 static int zssoft __P((void *));
210 static struct intrhand levelhard = { zshard };
211 static struct intrhand levelsoft = { zssoft };
212 
213 static int zs_get_speed __P((struct zs_chanstate *));
214 
215 
216 /*
217  * Is the zs chip present?
218  */
219 static int
220 zs_match(parent, cf, aux)
221 	struct device *parent;
222 	struct cfdata *cf;
223 	void *aux;
224 {
225 	struct confargs *ca = aux;
226 	struct romaux *ra = &ca->ca_ra;
227 
228 	if (strcmp(cf->cf_driver->cd_name, ra->ra_name))
229 		return (0);
230 	if ((ca->ca_bustype == BUS_MAIN && !CPU_ISSUN4) ||
231 	    (ca->ca_bustype == BUS_OBIO && CPU_ISSUN4M))
232 		return (getpropint(ra->ra_node, "slave", -2) == cf->cf_unit);
233 	ra->ra_len = NBPG;
234 	return (probeget(ra->ra_vaddr, 1) != -1);
235 }
236 
237 /*
238  * Attach a found zs.
239  *
240  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
241  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
242  */
243 static void
244 zs_attach(parent, self, aux)
245 	struct device *parent;
246 	struct device *self;
247 	void *aux;
248 {
249 	struct zsc_softc *zsc = (void *) self;
250 	struct confargs *ca = aux;
251 	struct romaux *ra = &ca->ca_ra;
252 	struct zsc_attach_args zsc_args;
253 	volatile struct zschan *zc;
254 	struct zs_chanstate *cs;
255 	int pri, s, zs_unit, channel;
256 	static int didintr, prevpri;
257 
258 	zs_unit = zsc->zsc_dev.dv_unit;
259 
260 	/* Use the mapping setup by the Sun PROM. */
261 	if (zsaddr[zs_unit] == NULL)
262 		zsaddr[zs_unit] = findzs(zs_unit);
263 
264 	if (ca->ca_bustype==BUS_MAIN)
265 		if ((void*)zsaddr[zs_unit] != ra->ra_vaddr)
266 			panic("zsattach");
267 	if (ra->ra_nintr != 1) {
268 		printf(": expected 1 interrupt, got %d\n", ra->ra_nintr);
269 		return;
270 	}
271 	pri = ra->ra_intr[0].int_pri;
272 	printf(" pri %d, softpri %d\n", pri, PIL_TTY);
273 
274 	/*
275 	 * Initialize software state for each channel.
276 	 */
277 	for (channel = 0; channel < 2; channel++) {
278 		zsc_args.channel = channel;
279 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
280 		cs = &zsc->zsc_cs_store[channel];
281 		zsc->zsc_cs[channel] = cs;
282 		if (zs_unit == zs_console_unit &&
283 		    channel == zs_console_channel) {
284 			zs_conschanstate = cs;
285 		}
286 
287 		cs->cs_channel = channel;
288 		cs->cs_private = NULL;
289 		cs->cs_ops = &zsops_null;
290 		cs->cs_brg_clk = PCLK / 16;
291 
292 		zc = zs_get_chan_addr(zs_unit, channel);
293 		cs->cs_reg_csr  = &zc->zc_csr;
294 		cs->cs_reg_data = &zc->zc_data;
295 
296 		bcopy(zs_init_reg, cs->cs_creg, 16);
297 		bcopy(zs_init_reg, cs->cs_preg, 16);
298 
299 		/* XXX: Get these from the PROM properties! */
300 		/* XXX: See the mvme167 code.  Better. */
301 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
302 			cs->cs_defspeed = zs_get_speed(cs);
303 		else
304 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
305 		cs->cs_defcflag = zs_def_cflag;
306 
307 		/* Make these correspond to cs_defcflag (-crtscts) */
308 		cs->cs_rr0_dcd = ZSRR0_DCD;
309 		cs->cs_rr0_cts = 0;
310 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
311 		cs->cs_wr5_rts = 0;
312 
313 		/*
314 		 * Clear the master interrupt enable.
315 		 * The INTENA is common to both channels,
316 		 * so just do it on the A channel.
317 		 */
318 		if (channel == 0) {
319 			zs_write_reg(cs, 9, 0);
320 		}
321 
322 		/*
323 		 * Look for a child driver for this channel.
324 		 * The child attach will setup the hardware.
325 		 */
326 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
327 			/* No sub-driver.  Just reset it. */
328 			u_char reset = (channel == 0) ?
329 				ZSWR9_A_RESET : ZSWR9_B_RESET;
330 			s = splhigh();
331 			zs_write_reg(cs,  9, reset);
332 			splx(s);
333 		}
334 	}
335 
336 	/*
337 	 * Now safe to install interrupt handlers.  Note the arguments
338 	 * to the interrupt handlers aren't used.  Note, we only do this
339 	 * once since both SCCs interrupt at the same level and vector.
340 	 */
341 	if (!didintr) {
342 		didintr = 1;
343 		prevpri = pri;
344 		intr_establish(pri, &levelhard);
345 		intr_establish(PIL_TTY, &levelsoft);
346 	} else if (pri != prevpri)
347 		panic("broken zs interrupt scheme");
348 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
349 
350 	/*
351 	 * Set the master interrupt enable and interrupt vector.
352 	 * (common to both channels, do it on A)
353 	 */
354 	cs = zsc->zsc_cs[0];
355 	s = splhigh();
356 	/* interrupt vector */
357 	zs_write_reg(cs, 2, zs_init_reg[2]);
358 	/* master interrupt control (enable) */
359 	zs_write_reg(cs, 9, zs_init_reg[9]);
360 	splx(s);
361 
362 #if 0
363 	/*
364 	 * XXX: L1A hack - We would like to be able to break into
365 	 * the debugger during the rest of autoconfiguration, so
366 	 * lower interrupts just enough to let zs interrupts in.
367 	 * This is done after both zs devices are attached.
368 	 */
369 	if (zs_unit == 1) {
370 		printf("zs1: enabling zs interrupts\n");
371 		(void)splfd(); /* XXX: splzs - 1 */
372 	}
373 #endif
374 }
375 
376 static int
377 zs_print(aux, name)
378 	void *aux;
379 	const char *name;
380 {
381 	struct zsc_attach_args *args = aux;
382 
383 	if (name != NULL)
384 		printf("%s: ", name);
385 
386 	if (args->channel != -1)
387 		printf(" channel %d", args->channel);
388 
389 	return UNCONF;
390 }
391 
392 static volatile int zssoftpending;
393 
394 /*
395  * Our ZS chips all share a common, autovectored interrupt,
396  * so we have to look at all of them on each interrupt.
397  */
398 static int
399 zshard(arg)
400 	void *arg;
401 {
402 	register struct zsc_softc *zsc;
403 	register int unit, rr3, rval, softreq;
404 
405 	rval = softreq = 0;
406 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
407 		zsc = zs_cd.cd_devs[unit];
408 		if (zsc == NULL)
409 			continue;
410 		rr3 = zsc_intr_hard(zsc);
411 		/* Count up the interrupts. */
412 		if (rr3) {
413 			rval |= rr3;
414 			zsc->zsc_intrcnt.ev_count++;
415 		}
416 		softreq |= zsc->zsc_cs[0]->cs_softreq;
417 		softreq |= zsc->zsc_cs[1]->cs_softreq;
418 	}
419 
420 	/* We are at splzs here, so no need to lock. */
421 	if (softreq && (zssoftpending == 0)) {
422 		zssoftpending = IE_ZSSOFT;
423 #if defined(SUN4M)
424 		if (CPU_ISSUN4M)
425 			raise(0, PIL_TTY);
426 		else
427 #endif
428 		ienab_bis(IE_ZSSOFT);
429 	}
430 	return (rval);
431 }
432 
433 /*
434  * Similar scheme as for zshard (look at all of them)
435  */
436 static int
437 zssoft(arg)
438 	void *arg;
439 {
440 	register struct zsc_softc *zsc;
441 	register int s, unit;
442 
443 	/* This is not the only ISR on this IPL. */
444 	if (zssoftpending == 0)
445 		return (0);
446 
447 	/*
448 	 * The soft intr. bit will be set by zshard only if
449 	 * the variable zssoftpending is zero.  The order of
450 	 * these next two statements prevents our clearing
451 	 * the soft intr bit just after zshard has set it.
452 	 */
453 	/* ienab_bic(IE_ZSSOFT); */
454 	zssoftpending = 0;
455 
456 	/* Make sure we call the tty layer at spltty. */
457 	s = spltty();
458 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
459 		zsc = zs_cd.cd_devs[unit];
460 		if (zsc == NULL)
461 			continue;
462 		(void) zsc_intr_soft(zsc);
463 	}
464 	splx(s);
465 	return (1);
466 }
467 
468 
469 /*
470  * Compute the current baud rate given a ZS channel.
471  */
472 static int
473 zs_get_speed(cs)
474 	struct zs_chanstate *cs;
475 {
476 	int tconst;
477 
478 	tconst = zs_read_reg(cs, 12);
479 	tconst |= zs_read_reg(cs, 13) << 8;
480 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
481 }
482 
483 /*
484  * MD functions for setting the baud rate and control modes.
485  */
486 int
487 zs_set_speed(cs, bps)
488 	struct zs_chanstate *cs;
489 	int bps;	/* bits per second */
490 {
491 	int tconst, real_bps;
492 
493 	if (bps == 0)
494 		return (0);
495 
496 #ifdef	DIAGNOSTIC
497 	if (cs->cs_brg_clk == 0)
498 		panic("zs_set_speed");
499 #endif
500 
501 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
502 	if (tconst < 0)
503 		return (EINVAL);
504 
505 	/* Convert back to make sure we can do it. */
506 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
507 
508 	/* XXX - Allow some tolerance here? */
509 	if (real_bps != bps)
510 		return (EINVAL);
511 
512 	cs->cs_preg[12] = tconst;
513 	cs->cs_preg[13] = tconst >> 8;
514 
515 	/* Caller will stuff the pending registers. */
516 	return (0);
517 }
518 
519 int
520 zs_set_modes(cs, cflag)
521 	struct zs_chanstate *cs;
522 	int cflag;	/* bits per second */
523 {
524 	int s;
525 
526 	/*
527 	 * Output hardware flow control on the chip is horrendous:
528 	 * if carrier detect drops, the receiver is disabled, and if
529 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
530 	 * Therefore, NEVER set the HFC bit, and instead use the
531 	 * status interrupt to detect CTS changes.
532 	 */
533 	s = splzs();
534 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
535 		cs->cs_rr0_dcd = 0;
536 	else
537 		cs->cs_rr0_dcd = ZSRR0_DCD;
538 	if ((cflag & CRTSCTS) != 0) {
539 		cs->cs_wr5_dtr = ZSWR5_DTR;
540 		cs->cs_wr5_rts = ZSWR5_RTS;
541 		cs->cs_rr0_cts = ZSRR0_CTS;
542 	} else if ((cflag & CDTRCTS) != 0) {
543 		cs->cs_wr5_dtr = 0;
544 		cs->cs_wr5_rts = ZSWR5_DTR;
545 		cs->cs_rr0_cts = ZSRR0_CTS;
546 	} else if ((cflag & MDMBUF) != 0) {
547 		cs->cs_wr5_dtr = 0;
548 		cs->cs_wr5_rts = ZSWR5_DTR;
549 		cs->cs_rr0_cts = ZSRR0_DCD;
550 	} else {
551 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
552 		cs->cs_wr5_rts = 0;
553 		cs->cs_rr0_cts = 0;
554 	}
555 	splx(s);
556 
557 	/* Caller will stuff the pending registers. */
558 	return (0);
559 }
560 
561 
562 /*
563  * Read or write the chip with suitable delays.
564  */
565 
566 u_char
567 zs_read_reg(cs, reg)
568 	struct zs_chanstate *cs;
569 	u_char reg;
570 {
571 	u_char val;
572 
573 	*cs->cs_reg_csr = reg;
574 	ZS_DELAY();
575 	val = *cs->cs_reg_csr;
576 	ZS_DELAY();
577 	return val;
578 }
579 
580 void
581 zs_write_reg(cs, reg, val)
582 	struct zs_chanstate *cs;
583 	u_char reg, val;
584 {
585 	*cs->cs_reg_csr = reg;
586 	ZS_DELAY();
587 	*cs->cs_reg_csr = val;
588 	ZS_DELAY();
589 }
590 
591 u_char zs_read_csr(cs)
592 	struct zs_chanstate *cs;
593 {
594 	register u_char val;
595 
596 	val = *cs->cs_reg_csr;
597 	ZS_DELAY();
598 	return val;
599 }
600 
601 void  zs_write_csr(cs, val)
602 	struct zs_chanstate *cs;
603 	u_char val;
604 {
605 	*cs->cs_reg_csr = val;
606 	ZS_DELAY();
607 }
608 
609 u_char zs_read_data(cs)
610 	struct zs_chanstate *cs;
611 {
612 	register u_char val;
613 
614 	val = *cs->cs_reg_data;
615 	ZS_DELAY();
616 	return val;
617 }
618 
619 void  zs_write_data(cs, val)
620 	struct zs_chanstate *cs;
621 	u_char val;
622 {
623 	*cs->cs_reg_data = val;
624 	ZS_DELAY();
625 }
626 
627 /****************************************************************
628  * Console support functions (Sun specific!)
629  * Note: this code is allowed to know about the layout of
630  * the chip registers, and uses that to keep things simple.
631  * XXX - I think I like the mvme167 code better. -gwr
632  ****************************************************************/
633 
634 extern void Debugger __P((void));
635 void *zs_conschan;
636 
637 /*
638  * Handle user request to enter kernel debugger.
639  */
640 void
641 zs_abort(cs)
642 	struct zs_chanstate *cs;
643 {
644 	register volatile struct zschan *zc = zs_conschan;
645 	int rr0;
646 
647 	/* Wait for end of break to avoid PROM abort. */
648 	/* XXX - Limit the wait? */
649 	do {
650 		rr0 = zc->zc_csr;
651 		ZS_DELAY();
652 	} while (rr0 & ZSRR0_BREAK);
653 
654 #if defined(KGDB)
655 	zskgdb(cs);
656 #elif defined(DDB)
657 	Debugger();
658 #else
659 	printf("stopping on keyboard abort\n");
660 	callrom();
661 #endif
662 }
663 
664 /*
665  * Polled input char.
666  */
667 int
668 zs_getc(arg)
669 	void *arg;
670 {
671 	register volatile struct zschan *zc = arg;
672 	register int s, c, rr0;
673 
674 	s = splhigh();
675 	/* Wait for a character to arrive. */
676 	do {
677 		rr0 = zc->zc_csr;
678 		ZS_DELAY();
679 	} while ((rr0 & ZSRR0_RX_READY) == 0);
680 
681 	c = zc->zc_data;
682 	ZS_DELAY();
683 	splx(s);
684 
685 	/*
686 	 * This is used by the kd driver to read scan codes,
687 	 * so don't translate '\r' ==> '\n' here...
688 	 */
689 	return (c);
690 }
691 
692 /*
693  * Polled output char.
694  */
695 void
696 zs_putc(arg, c)
697 	void *arg;
698 	int c;
699 {
700 	register volatile struct zschan *zc = arg;
701 	register int s, rr0;
702 
703 	s = splhigh();
704 	/* Wait for transmitter to become ready. */
705 	do {
706 		rr0 = zc->zc_csr;
707 		ZS_DELAY();
708 	} while ((rr0 & ZSRR0_TX_READY) == 0);
709 
710 	/*
711 	 * If the transmitter was busy doing regular tty I/O (ZSWR1_TIE on),
712 	 * defer our output until the transmit interrupt runs. We still
713 	 * sync with TX_READY so we can get by with a single-char "queue".
714 	 */
715 	if (zs_conschanstate && (zs_conschanstate->cs_preg[1] & ZSWR1_TIE)) {
716 		/*
717 		 * If a previous held character has not yet gone out, we can
718 		 * send it now;  zsxint() will field the interrupt for our
719 		 * char, but doesn't care. We're running at sufficiently
720 		 * high spl for this to work.
721 		 */
722 		if (zs_conschanstate->cs_heldchar != 0)
723 			zc->zc_data = zs_conschanstate->cs_heldchar;
724 		zs_conschanstate->cs_heldchar = c;
725 		ZS_DELAY();
726 		splx(s);
727 		return;
728 	}
729 
730 	zc->zc_data = c;
731 	ZS_DELAY();
732 	splx(s);
733 }
734 
735 /*****************************************************************/
736 
737 static void zscninit __P((struct consdev *));
738 static int  zscngetc __P((dev_t));
739 static void zscnputc __P((dev_t, int));
740 
741 /*
742  * Console table shared by ttya, ttyb
743  */
744 struct consdev consdev_tty = {
745 	nullcnprobe,
746 	zscninit,
747 	zscngetc,
748 	zscnputc,
749 	nullcnpollc,
750 };
751 
752 static void
753 zscninit(cn)
754 	struct consdev *cn;
755 {
756 }
757 
758 /*
759  * Polled console input putchar.
760  */
761 static int
762 zscngetc(dev)
763 	dev_t dev;
764 {
765 	return (zs_getc(zs_conschan));
766 }
767 
768 /*
769  * Polled console output putchar.
770  */
771 static void
772 zscnputc(dev, c)
773 	dev_t dev;
774 	int c;
775 {
776 	zs_putc(zs_conschan, c);
777 }
778 
779 /*****************************************************************/
780 
781 static void prom_cninit __P((struct consdev *));
782 static int  prom_cngetc __P((dev_t));
783 static void prom_cnputc __P((dev_t, int));
784 
785 /*
786  * The console is set to this one initially,
787  * which lets us use the PROM until consinit()
788  * is called to select a real console.
789  */
790 struct consdev consdev_prom = {
791 	nullcnprobe,
792 	prom_cninit,
793 	prom_cngetc,
794 	prom_cnputc,
795 	nullcnpollc,
796 };
797 
798 /*
799  * The console table pointer is statically initialized
800  * to point to the PROM (output only) table, so that
801  * early calls to printf will work.
802  */
803 struct consdev *cn_tab = &consdev_prom;
804 
805 void
806 nullcnprobe(cn)
807 	struct consdev *cn;
808 {
809 }
810 
811 static void
812 prom_cninit(cn)
813 	struct consdev *cn;
814 {
815 }
816 
817 /*
818  * PROM console input putchar.
819  * (dummy - this is output only)
820  */
821 static int
822 prom_cngetc(dev)
823 	dev_t dev;
824 {
825 	return (0);
826 }
827 
828 /*
829  * PROM console output putchar.
830  */
831 static void
832 prom_cnputc(dev, c)
833 	dev_t dev;
834 	int c;
835 {
836 	char c0 = (c & 0x7f);
837 
838 	if (promvec->pv_romvec_vers > 2)
839 		(*promvec->pv_v2devops.v2_write)
840 			(*promvec->pv_v2bootargs.v2_fd1, &c0, 1);
841 	else
842 		(*promvec->pv_putchar)(c);
843 }
844 
845 /*****************************************************************/
846 
847 extern struct consdev consdev_kd;
848 
849 static char *prom_inSrc_name[] = {
850 	"keyboard/display",
851 	"ttya", "ttyb",
852 	"ttyc", "ttyd" };
853 
854 /*
855  * This function replaces sys/dev/cninit.c
856  * Determine which device is the console using
857  * the PROM "input source" and "output sink".
858  */
859 void
860 consinit()
861 {
862 	struct zschan *zc;
863 	struct consdev *cn;
864 	int channel, zs_unit, zstty_unit;
865 	int inSource, outSink;
866 
867 	if (promvec->pv_romvec_vers > 2) {
868 		/* We need to probe the PROM device tree */
869 		register int node,fd;
870 		char buffer[128];
871 		register struct nodeops *no;
872 		register struct v2devops *op;
873 		register char *cp;
874 		extern int fbnode;
875 
876 		inSource = outSink = -1;
877 		no = promvec->pv_nodeops;
878 		op = &promvec->pv_v2devops;
879 
880 		node = findroot();
881 		if (no->no_proplen(node, "stdin-path") >= sizeof(buffer)) {
882 			printf("consinit: increase buffer size and recompile\n");
883 			goto setup_output;
884 		}
885 		/* XXX: fix above */
886 
887 		no->no_getprop(node, "stdin-path",buffer);
888 
889 		/*
890 		 * Open an "instance" of this device.
891 		 * You'd think it would be appropriate to call v2_close()
892 		 * on the handle when we're done with it. But that seems
893 		 * to cause the device to shut down somehow; for the moment,
894 		 * we simply leave it open...
895 		 */
896 		if ((fd = op->v2_open(buffer)) == 0 ||
897 		     (node = op->v2_fd_phandle(fd)) == 0) {
898 			printf("consinit: bogus stdin path %s.\n",buffer);
899 			goto setup_output;
900 		}
901 		if (no->no_proplen(node,"keyboard") >= 0) {
902 			inSource = PROMDEV_KBD;
903 			goto setup_output;
904 		}
905 		if (strcmp(getpropstring(node,"device_type"),"serial") != 0) {
906 			/* not a serial, not keyboard. what is it?!? */
907 			inSource = -1;
908 			goto setup_output;
909 		}
910 		/*
911 		 * At this point we assume the device path is in the form
912 		 *   ....device@x,y:a for ttya and ...device@x,y:b for ttyb.
913 		 * If it isn't, we defer to the ROM
914 		 */
915 		cp = buffer;
916 		while (*cp)
917 		    cp++;
918 		cp -= 2;
919 #ifdef DEBUG
920 		if (cp < buffer)
921 		    panic("consinit: bad stdin path %s",buffer);
922 #endif
923 		/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
924 		if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
925 		    inSource = PROMDEV_TTYA + (cp[1] - 'a');
926 		/* else use rom */
927 setup_output:
928 		node = findroot();
929 		if (no->no_proplen(node, "stdout-path") >= sizeof(buffer)) {
930 			printf("consinit: increase buffer size and recompile\n");
931 			goto setup_console;
932 		}
933 		/* XXX: fix above */
934 
935 		no->no_getprop(node, "stdout-path", buffer);
936 
937 		if ((fd = op->v2_open(buffer)) == 0 ||
938 		     (node = op->v2_fd_phandle(fd)) == 0) {
939 			printf("consinit: bogus stdout path %s.\n",buffer);
940 			goto setup_output;
941 		}
942 		if (strcmp(getpropstring(node,"device_type"),"display") == 0) {
943 			/* frame buffer output */
944 			outSink = PROMDEV_SCREEN;
945 			fbnode = node;
946 		} else if (strcmp(getpropstring(node,"device_type"), "serial")
947 			   != 0) {
948 			/* not screen, not serial. Whatzit? */
949 			outSink = -1;
950 		} else { /* serial console. which? */
951 			/*
952 			 * At this point we assume the device path is in the
953 			 * form:
954 			 * ....device@x,y:a for ttya, etc.
955 			 * If it isn't, we defer to the ROM
956 			 */
957 			cp = buffer;
958 			while (*cp)
959 			    cp++;
960 			cp -= 2;
961 #ifdef DEBUG
962 			if (cp < buffer)
963 				panic("consinit: bad stdout path %s",buffer);
964 #endif
965 			/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
966 			if (cp[0]==':' && cp[1] >= 'a' && cp[1] <= 'z')
967 			    outSink = PROMDEV_TTYA + (cp[1] - 'a');
968 			else outSink = -1;
969 		}
970 	} else {
971 		inSource = *promvec->pv_stdin;
972 		outSink  = *promvec->pv_stdout;
973 	}
974 
975 setup_console:
976 
977 	if (inSource != outSink) {
978 		printf("cninit: mismatched PROM output selector\n");
979 	}
980 
981 	switch (inSource) {
982 	default:
983 		printf("cninit: invalid inSource=%d\n", inSource);
984 		callrom();
985 		inSource = PROMDEV_KBD;
986 		/* fall through */
987 
988 	case 0:	/* keyboard/display */
989 #if NKBD > 0
990 		zs_unit = 1;	/* XXX - config info! */
991 		channel = 0;
992 		cn = &consdev_kd;
993 		/* Set cn_dev, cn_pri in kd.c */
994 		break;
995 #else	/* NKBD */
996 		printf("cninit: kdb/display not configured\n");
997 		callrom();
998 		inSource = PROMDEV_TTYA;
999 		/* fall through */
1000 #endif	/* NKBD */
1001 
1002 	case PROMDEV_TTYA:
1003 	case PROMDEV_TTYB:
1004 		zstty_unit = inSource - PROMDEV_TTYA;
1005 		zs_unit = 0;	/* XXX - config info! */
1006 		channel = zstty_unit & 1;
1007 		cn = &consdev_tty;
1008 		cn->cn_dev = makedev(zs_major, zstty_unit);
1009 		cn->cn_pri = CN_REMOTE;
1010 		zs_console_unit = zs_unit;
1011 		zs_console_channel = channel;
1012 		break;
1013 
1014 	}
1015 	/* Now that inSource has been validated, print it. */
1016 	printf("console is %s\n", prom_inSrc_name[inSource]);
1017 
1018 	zc = zs_get_chan_addr(zs_unit, channel);
1019 	if (zc == NULL) {
1020 		printf("cninit: zs not mapped.\n");
1021 		return;
1022 	}
1023 	zs_conschan = zc;
1024 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
1025 	cn_tab = cn;
1026 	(*cn->cn_init)(cn);
1027 #ifdef	KGDB
1028 	zs_kgdb_init();
1029 #endif
1030 }
1031