xref: /netbsd-src/sys/arch/sun3/dev/zs.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: zs.c,v 1.60 2000/03/06 21:36:12 thorpej 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/cpu.h>
61 #include <machine/mon.h>
62 #include <machine/z8530var.h>
63 
64 #include <sun3/sun3/machdep.h>
65 #ifdef	_SUN3X_
66 #include <sun3/sun3x/obio.h>
67 #else
68 #include <sun3/sun3/obio.h>
69 #endif
70 #include <sun3/dev/zs_cons.h>
71 
72 #include <dev/cons.h>
73 #include <dev/ic/z8530reg.h>
74 
75 #include "kbd.h"	/* NKBD */
76 #include "zsc.h"	/* NZSC */
77 #define NZS NZSC
78 
79 /* Make life easier for the initialized arrays here. */
80 #if NZS < 2
81 #undef  NZS
82 #define NZS 2
83 #endif
84 
85 /*
86  * Some warts needed by z8530tty.c -
87  * The default parity REALLY needs to be the same as the PROM uses,
88  * or you can not see messages done with printf during boot-up...
89  */
90 int zs_def_cflag = (CREAD | CS8 | HUPCL);
91 int zs_major = 12;
92 
93 /*
94  * The Sun3 provides a 4.9152 MHz clock to the ZS chips.
95  */
96 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
97 
98 /*
99  * Define interrupt levels.
100  */
101 #define ZSHARD_PRI	6	/* Wired on the CPU board... */
102 #define ZSSOFT_PRI	3	/* Want tty pri (4) but this is OK. */
103 
104 #define ZS_DELAY()			delay(2)
105 
106 /* The layout of this is hardware-dependent (padding, order). */
107 struct zschan {
108 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
109 	u_char		zc_xxx0;
110 	volatile u_char	zc_data;	/* data */
111 	u_char		zc_xxx1;
112 };
113 struct zsdevice {
114 	/* Yes, they are backwards. */
115 	struct	zschan zs_chan_b;
116 	struct	zschan zs_chan_a;
117 };
118 
119 
120 /* Default OBIO addresses. */
121 static int zs_physaddr[NZS] = {
122 	OBIO_ZS_KBD_MS,
123 	OBIO_ZS_TTY_AB };
124 
125 /* Saved PROM mappings */
126 static struct zsdevice *zsaddr[NZS];
127 
128 /* Flags from cninit() */
129 static int zs_hwflags[NZS][2];
130 
131 /* Default speed for each channel */
132 static int zs_defspeed[NZS][2] = {
133 	{ 1200, 	/* keyboard */
134 	  1200 },	/* mouse */
135 	{ 9600, 	/* ttya */
136 	  9600 },	/* ttyb */
137 };
138 
139 static u_char zs_init_reg[16] = {
140 	0,	/* 0: CMD (reset, etc.) */
141 	0,	/* 1: No interrupts yet. */
142 	0x18 + ZSHARD_PRI,	/* IVECT */
143 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
144 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
145 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
146 	0,	/* 6: TXSYNC/SYNCLO */
147 	0,	/* 7: RXSYNC/SYNCHI */
148 	0,	/* 8: alias for data port */
149 	ZSWR9_MASTER_IE,
150 	0,	/*10: Misc. TX/RX control bits */
151 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
152 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
153 	0,			/*13: BAUDHI (default=9600) */
154 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
155 	ZSWR15_BREAK_IE,
156 };
157 
158 
159 /* Find PROM mappings (for console support). */
160 void
161 zs_init()
162 {
163 	int i;
164 
165 	for (i = 0; i < NZS; i++) {
166 		zsaddr[i] = (struct zsdevice *)
167 			obio_find_mapping(zs_physaddr[i], sizeof(struct zschan));
168 	}
169 }
170 
171 struct zschan *
172 zs_get_chan_addr(zs_unit, channel)
173 	int zs_unit, channel;
174 {
175 	struct zsdevice *addr;
176 	struct zschan *zc;
177 
178 	if (zs_unit >= NZS)
179 		return NULL;
180 	addr = zsaddr[zs_unit];
181 	if (addr == NULL)
182 		return NULL;
183 	if (channel == 0) {
184 		zc = &addr->zs_chan_a;
185 	} else {
186 		zc = &addr->zs_chan_b;
187 	}
188 	return (zc);
189 }
190 
191 
192 /****************************************************************
193  * Autoconfig
194  ****************************************************************/
195 
196 /* Definition of the driver for autoconfig. */
197 static int	zs_match __P((struct device *, struct cfdata *, void *));
198 static void	zs_attach __P((struct device *, struct device *, void *));
199 static int  zs_print __P((void *, const char *name));
200 
201 struct cfattach zsc_ca = {
202 	sizeof(struct zsc_softc), zs_match, zs_attach
203 };
204 
205 extern struct cfdriver zsc_cd;
206 
207 static int zshard __P((void *));
208 static int zssoft __P((void *));
209 static int zs_get_speed __P((struct zs_chanstate *));
210 
211 
212 /*
213  * Is the zs chip present?
214  */
215 static int
216 zs_match(parent, cf, aux)
217 	struct device *parent;
218 	struct cfdata *cf;
219 	void *aux;
220 {
221 	struct confargs *ca = aux;
222 	int unit = cf->cf_unit;
223 	void *va;
224 
225 	/*
226 	 * This driver only supports its wired-in mappings,
227 	 * because the console support depends on those.
228 	 */
229 	if (ca->ca_paddr != zs_physaddr[unit])
230 		return (0);
231 
232 	/* Make sure zs_init() found mappings. */
233 	va = zsaddr[unit];
234 	if (va == NULL)
235 		return (0);
236 
237 	/* This returns -1 on a fault (bus error). */
238 	if (peek_byte(va) == -1)
239 		return (0);
240 
241 	/* Default interrupt priority (always splbio==2) */
242 	if (ca->ca_intpri == -1)
243 		ca->ca_intpri = ZSHARD_PRI;
244 
245 	return (1);
246 }
247 
248 /*
249  * Attach a found zs.
250  *
251  * Match slave number to zs unit number, so that misconfiguration will
252  * not set up the keyboard as ttya, etc.
253  */
254 static void
255 zs_attach(parent, self, aux)
256 	struct device *parent;
257 	struct device *self;
258 	void *aux;
259 {
260 	struct zsc_softc *zsc = (void *) self;
261 	struct confargs *ca = aux;
262 	struct zsc_attach_args zsc_args;
263 	volatile struct zschan *zc;
264 	struct zs_chanstate *cs;
265 	int s, zs_unit, channel;
266 	static int didintr;
267 
268 	zs_unit = zsc->zsc_dev.dv_unit;
269 
270 	printf(": (softpri %d)\n", ZSSOFT_PRI);
271 
272 	/* Use the mapping setup by the Sun PROM. */
273 	if (zsaddr[zs_unit] == NULL)
274 		panic("zs_attach: zs%d not mapped\n", zs_unit);
275 
276 	/*
277 	 * Initialize software state for each channel.
278 	 */
279 	for (channel = 0; channel < 2; channel++) {
280 		zsc_args.channel = channel;
281 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
282 		cs = &zsc->zsc_cs_store[channel];
283 		zsc->zsc_cs[channel] = cs;
284 
285 		cs->cs_channel = channel;
286 		cs->cs_private = NULL;
287 		cs->cs_ops = &zsops_null;
288 		cs->cs_brg_clk = PCLK / 16;
289 
290 		zc = zs_get_chan_addr(zs_unit, channel);
291 		cs->cs_reg_csr  = &zc->zc_csr;
292 		cs->cs_reg_data = &zc->zc_data;
293 
294 		bcopy(zs_init_reg, cs->cs_creg, 16);
295 		bcopy(zs_init_reg, cs->cs_preg, 16);
296 
297 		/* XXX: Get these from the EEPROM instead? */
298 		/* XXX: See the mvme167 code.  Better. */
299 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
300 			cs->cs_defspeed = zs_get_speed(cs);
301 		else
302 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
303 		cs->cs_defcflag = zs_def_cflag;
304 
305 		/* Make these correspond to cs_defcflag (-crtscts) */
306 		cs->cs_rr0_dcd = ZSRR0_DCD;
307 		cs->cs_rr0_cts = 0;
308 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
309 		cs->cs_wr5_rts = 0;
310 
311 		/*
312 		 * Clear the master interrupt enable.
313 		 * The INTENA is common to both channels,
314 		 * so just do it on the A channel.
315 		 */
316 		if (channel == 0) {
317 			zs_write_reg(cs, 9, 0);
318 		}
319 
320 		/*
321 		 * Look for a child driver for this channel.
322 		 * The child attach will setup the hardware.
323 		 */
324 		if (!config_found(self, (void *)&zsc_args, zs_print)) {
325 			/* No sub-driver.  Just reset it. */
326 			u_char reset = (channel == 0) ?
327 				ZSWR9_A_RESET : ZSWR9_B_RESET;
328 			s = splhigh();
329 			zs_write_reg(cs,  9, reset);
330 			splx(s);
331 		}
332 	}
333 
334 	/*
335 	 * Now safe to install interrupt handlers.  Note the arguments
336 	 * to the interrupt handlers aren't used.  Note, we only do this
337 	 * once since both SCCs interrupt at the same level and vector.
338 	 */
339 	if (!didintr) {
340 		didintr = 1;
341 		isr_add_autovect(zssoft, NULL, ZSSOFT_PRI);
342 		isr_add_autovect(zshard, NULL, ca->ca_intpri);
343 	}
344 	/* XXX; evcnt_attach() ? */
345 
346 	/*
347 	 * Set the master interrupt enable and interrupt vector.
348 	 * (common to both channels, do it on A)
349 	 */
350 	cs = zsc->zsc_cs[0];
351 	s = splhigh();
352 	/* interrupt vector */
353 	zs_write_reg(cs, 2, zs_init_reg[2]);
354 	/* master interrupt control (enable) */
355 	zs_write_reg(cs, 9, zs_init_reg[9]);
356 	splx(s);
357 
358 	/*
359 	 * XXX: L1A hack - We would like to be able to break into
360 	 * the debugger during the rest of autoconfiguration, so
361 	 * lower interrupts just enough to let zs interrupts in.
362 	 * This is done after both zs devices are attached.
363 	 */
364 	if (zs_unit == 1) {
365 		(void)spl5(); /* splzs - 1 */
366 	}
367 }
368 
369 static int
370 zs_print(aux, name)
371 	void *aux;
372 	const char *name;
373 {
374 	struct zsc_attach_args *args = aux;
375 
376 	if (name != NULL)
377 		printf("%s: ", name);
378 
379 	if (args->channel != -1)
380 		printf(" channel %d", args->channel);
381 
382 	return UNCONF;
383 }
384 
385 static volatile int zssoftpending;
386 
387 /*
388  * Our ZS chips all share a common, autovectored interrupt,
389  * so we have to look at all of them on each interrupt.
390  */
391 static int
392 zshard(arg)
393 	void *arg;
394 {
395 	register struct zsc_softc *zsc;
396 	register int unit, rval, softreq;
397 
398 	rval = softreq = 0;
399 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
400 		zsc = zsc_cd.cd_devs[unit];
401 		if (zsc == NULL)
402 			continue;
403 		rval |= zsc_intr_hard(zsc);
404 		softreq |= zsc->zsc_cs[0]->cs_softreq;
405 		softreq |= zsc->zsc_cs[1]->cs_softreq;
406 	}
407 
408 	/* We are at splzs here, so no need to lock. */
409 	if (softreq && (zssoftpending == 0)) {
410 		zssoftpending = ZSSOFT_PRI;
411 		isr_soft_request(ZSSOFT_PRI);
412 	}
413 	return (rval);
414 }
415 
416 /*
417  * Similar scheme as for zshard (look at all of them)
418  */
419 static int
420 zssoft(arg)
421 	void *arg;
422 {
423 	register struct zsc_softc *zsc;
424 	register int s, unit;
425 
426 	/* This is not the only ISR on this IPL. */
427 	if (zssoftpending == 0)
428 		return (0);
429 
430 	/*
431 	 * The soft intr. bit will be set by zshard only if
432 	 * the variable zssoftpending is zero.  The order of
433 	 * these next two statements prevents our clearing
434 	 * the soft intr bit just after zshard has set it.
435 	 */
436 	isr_soft_clear(ZSSOFT_PRI);
437 	zssoftpending = 0;
438 
439 	/* Make sure we call the tty layer at spltty. */
440 	s = spltty();
441 	for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) {
442 		zsc = zsc_cd.cd_devs[unit];
443 		if (zsc == NULL)
444 			continue;
445 		(void) zsc_intr_soft(zsc);
446 	}
447 	splx(s);
448 	return (1);
449 }
450 
451 
452 /*
453  * Compute the current baud rate given a ZS channel.
454  */
455 static int
456 zs_get_speed(cs)
457 	struct zs_chanstate *cs;
458 {
459 	int tconst;
460 
461 	tconst = zs_read_reg(cs, 12);
462 	tconst |= zs_read_reg(cs, 13) << 8;
463 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
464 }
465 
466 /*
467  * MD functions for setting the baud rate and control modes.
468  */
469 int
470 zs_set_speed(cs, bps)
471 	struct zs_chanstate *cs;
472 	int bps;	/* bits per second */
473 {
474 	int tconst, real_bps;
475 
476 	if (bps == 0)
477 		return (0);
478 
479 #ifdef	DIAGNOSTIC
480 	if (cs->cs_brg_clk == 0)
481 		panic("zs_set_speed");
482 #endif
483 
484 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
485 	if (tconst < 0)
486 		return (EINVAL);
487 
488 	/* Convert back to make sure we can do it. */
489 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
490 
491 	/* XXX - Allow some tolerance here? */
492 	if (real_bps != bps)
493 		return (EINVAL);
494 
495 	cs->cs_preg[12] = tconst;
496 	cs->cs_preg[13] = tconst >> 8;
497 
498 	/* Caller will stuff the pending registers. */
499 	return (0);
500 }
501 
502 int
503 zs_set_modes(cs, cflag)
504 	struct zs_chanstate *cs;
505 	int cflag;	/* bits per second */
506 {
507 	int s;
508 
509 	/*
510 	 * Output hardware flow control on the chip is horrendous:
511 	 * if carrier detect drops, the receiver is disabled, and if
512 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
513 	 * Therefore, NEVER set the HFC bit, and instead use the
514 	 * status interrupt to detect CTS changes.
515 	 */
516 	s = splzs();
517 	cs->cs_rr0_pps = 0;
518 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
519 		cs->cs_rr0_dcd = 0;
520 		if ((cflag & MDMBUF) == 0)
521 			cs->cs_rr0_pps = ZSRR0_DCD;
522 	} else
523 		cs->cs_rr0_dcd = ZSRR0_DCD;
524 	if ((cflag & CRTSCTS) != 0) {
525 		cs->cs_wr5_dtr = ZSWR5_DTR;
526 		cs->cs_wr5_rts = ZSWR5_RTS;
527 		cs->cs_rr0_cts = ZSRR0_CTS;
528 	} else if ((cflag & MDMBUF) != 0) {
529 		cs->cs_wr5_dtr = 0;
530 		cs->cs_wr5_rts = ZSWR5_DTR;
531 		cs->cs_rr0_cts = ZSRR0_DCD;
532 	} else {
533 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
534 		cs->cs_wr5_rts = 0;
535 		cs->cs_rr0_cts = 0;
536 	}
537 	splx(s);
538 
539 	/* Caller will stuff the pending registers. */
540 	return (0);
541 }
542 
543 
544 /*
545  * Read or write the chip with suitable delays.
546  */
547 
548 u_char
549 zs_read_reg(cs, reg)
550 	struct zs_chanstate *cs;
551 	u_char reg;
552 {
553 	u_char val;
554 
555 	*cs->cs_reg_csr = reg;
556 	ZS_DELAY();
557 	val = *cs->cs_reg_csr;
558 	ZS_DELAY();
559 	return val;
560 }
561 
562 void
563 zs_write_reg(cs, reg, val)
564 	struct zs_chanstate *cs;
565 	u_char reg, val;
566 {
567 	*cs->cs_reg_csr = reg;
568 	ZS_DELAY();
569 	*cs->cs_reg_csr = val;
570 	ZS_DELAY();
571 }
572 
573 u_char zs_read_csr(cs)
574 	struct zs_chanstate *cs;
575 {
576 	register u_char val;
577 
578 	val = *cs->cs_reg_csr;
579 	ZS_DELAY();
580 	return val;
581 }
582 
583 void  zs_write_csr(cs, val)
584 	struct zs_chanstate *cs;
585 	u_char val;
586 {
587 	*cs->cs_reg_csr = val;
588 	ZS_DELAY();
589 }
590 
591 u_char zs_read_data(cs)
592 	struct zs_chanstate *cs;
593 {
594 	register u_char val;
595 
596 	val = *cs->cs_reg_data;
597 	ZS_DELAY();
598 	return val;
599 }
600 
601 void  zs_write_data(cs, val)
602 	struct zs_chanstate *cs;
603 	u_char val;
604 {
605 	*cs->cs_reg_data = val;
606 	ZS_DELAY();
607 }
608 
609 /****************************************************************
610  * Console support functions (Sun3 specific!)
611  * Note: this code is allowed to know about the layout of
612  * the chip registers, and uses that to keep things simple.
613  * XXX - I think I like the mvme167 code better. -gwr
614  ****************************************************************/
615 
616 void *zs_conschan;
617 
618 /*
619  * Handle user request to enter kernel debugger.
620  */
621 void
622 zs_abort(cs)
623 	struct zs_chanstate *cs;
624 {
625 	register volatile struct zschan *zc = zs_conschan;
626 	int rr0;
627 
628 	/* Wait for end of break to avoid PROM abort. */
629 	/* XXX - Limit the wait? */
630 	do {
631 		rr0 = zc->zc_csr;
632 		ZS_DELAY();
633 	} while (rr0 & ZSRR0_BREAK);
634 
635 	/* This is always available on the Sun3. */
636 	Debugger();
637 }
638 
639 /*
640  * Polled input char.
641  */
642 int
643 zs_getc(arg)
644 	void *arg;
645 {
646 	register volatile struct zschan *zc = arg;
647 	register int s, c, rr0;
648 
649 	s = splhigh();
650 	/* Wait for a character to arrive. */
651 	do {
652 		rr0 = zc->zc_csr;
653 		ZS_DELAY();
654 	} while ((rr0 & ZSRR0_RX_READY) == 0);
655 
656 	c = zc->zc_data;
657 	ZS_DELAY();
658 	splx(s);
659 
660 	/*
661 	 * This is used by the kd driver to read scan codes,
662 	 * so don't translate '\r' ==> '\n' here...
663 	 */
664 	return (c);
665 }
666 
667 /*
668  * Polled output char.
669  */
670 void
671 zs_putc(arg, c)
672 	void *arg;
673 	int c;
674 {
675 	register volatile struct zschan *zc = arg;
676 	register int s, rr0;
677 
678 	s = splhigh();
679 	/* Wait for transmitter to become ready. */
680 	do {
681 		rr0 = zc->zc_csr;
682 		ZS_DELAY();
683 	} while ((rr0 & ZSRR0_TX_READY) == 0);
684 
685 	zc->zc_data = c;
686 	ZS_DELAY();
687 	splx(s);
688 }
689 
690 /*****************************************************************/
691 
692 static void zscninit __P((struct consdev *));
693 static int  zscngetc __P((dev_t));
694 static void zscnputc __P((dev_t, int));
695 
696 /*
697  * Console table shared by ttya, ttyb
698  */
699 struct consdev consdev_tty = {
700 	nullcnprobe,
701 	zscninit,
702 	zscngetc,
703 	zscnputc,
704 	nullcnpollc,
705 	NULL,
706 };
707 
708 static void
709 zscninit(cn)
710 	struct consdev *cn;
711 {
712 }
713 
714 /*
715  * Polled console input putchar.
716  */
717 static int
718 zscngetc(dev)
719 	dev_t dev;
720 {
721 	return (zs_getc(zs_conschan));
722 }
723 
724 /*
725  * Polled console output putchar.
726  */
727 static void
728 zscnputc(dev, c)
729 	dev_t dev;
730 	int c;
731 {
732 	zs_putc(zs_conschan, c);
733 }
734 
735 /*****************************************************************/
736 
737 static void prom_cninit __P((struct consdev *));
738 static int  prom_cngetc __P((dev_t));
739 static void prom_cnputc __P((dev_t, int));
740 
741 /*
742  * The console is set to this one initially,
743  * which lets us use the PROM until consinit()
744  * is called to select a real console.
745  */
746 struct consdev consdev_prom = {
747 	nullcnprobe,
748 	prom_cninit,
749 	prom_cngetc,
750 	prom_cnputc,
751 	nullcnpollc,
752 };
753 
754 /*
755  * The console table pointer is statically initialized
756  * to point to the PROM (output only) table, so that
757  * early calls to printf will work.
758  */
759 struct consdev *cn_tab = &consdev_prom;
760 
761 void
762 nullcnprobe(cn)
763 	struct consdev *cn;
764 {
765 }
766 
767 static void
768 prom_cninit(cn)
769 	struct consdev *cn;
770 {
771 }
772 
773 /*
774  * PROM console input putchar.
775  * (dummy - this is output only)
776  */
777 static int
778 prom_cngetc(dev)
779 	dev_t dev;
780 {
781 	return (0);
782 }
783 
784 /*
785  * PROM console output putchar.
786  */
787 static void
788 prom_cnputc(dev, c)
789 	dev_t dev;
790 	int c;
791 {
792 	(*romVectorPtr->putChar)(c & 0x7f);
793 }
794 
795 /*****************************************************************/
796 
797 extern struct consdev consdev_kd;
798 
799 static struct {
800 	int zs_unit, channel;
801 } zstty_conf[NZS*2] = {
802 	/* XXX: knowledge from the config file here... */
803 	{ 1, 0 },	/* ttya */
804 	{ 1, 1 },	/* ttyb */
805 	{ 0, 0 },	/* ttyc */
806 	{ 0, 1 },	/* ttyd */
807 };
808 
809 static char *prom_inSrc_name[] = {
810 	"keyboard/display",
811 	"ttya", "ttyb",
812 	"ttyc", "ttyd" };
813 
814 /*
815  * This function replaces sys/dev/cninit.c
816  * Determine which device is the console using
817  * the PROM "input source" and "output sink".
818  */
819 void
820 cninit()
821 {
822 	struct sunromvec *v;
823 	struct zschan *zc;
824 	struct consdev *cn;
825 	int channel, zs_unit, zstty_unit;
826 	u_char inSource, outSink;
827 
828 	/* Get the zs driver ready for console duty. */
829 	zs_init();
830 
831 	v = romVectorPtr;
832 	inSource = *v->inSource;
833 	outSink  = *v->outSink;
834 	if (inSource != outSink) {
835 		mon_printf("cninit: mismatched PROM output selector\n");
836 	}
837 
838 	switch (inSource) {
839 	default:
840 		mon_printf("cninit: invalid inSource=%d\n", inSource);
841 		sunmon_abort();
842 		inSource = 0;
843 		/* fall through */
844 
845 	case 0:	/* keyboard/display */
846 #if NKBD > 0
847 		zs_unit = 0;
848 		channel = 0;
849 		cn = &consdev_kd;
850 		/* Set cn_dev, cn_pri in kd.c */
851 		break;
852 #else	/* NKBD */
853 		mon_printf("cninit: kdb/display not configured\n");
854 		sunmon_abort();
855 		inSource = 1;
856 		/* fall through */
857 #endif	/* NKBD */
858 
859 	case 1:	/* ttya */
860 	case 2:	/* ttyb */
861 	case 3:	/* ttyc (rewired keyboard connector) */
862 	case 4:	/* ttyd (rewired mouse connector)   */
863 		zstty_unit = inSource - 1;
864 		zs_unit = zstty_conf[zstty_unit].zs_unit;
865 		channel = zstty_conf[zstty_unit].channel;
866 		cn = &consdev_tty;
867 		cn->cn_dev = makedev(zs_major, zstty_unit);
868 		cn->cn_pri = CN_REMOTE;
869 		break;
870 
871 	}
872 	/* Now that inSource has been validated, print it. */
873 	mon_printf("console is %s\n", prom_inSrc_name[inSource]);
874 
875 	zc = zs_get_chan_addr(zs_unit, channel);
876 	if (zc == NULL) {
877 		mon_printf("cninit: zs not mapped.\n");
878 		return;
879 	}
880 	zs_conschan = zc;
881 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
882 	cn_tab = cn;
883 	(*cn->cn_init)(cn);
884 #ifdef	KGDB
885 	zs_kgdb_init();
886 #endif
887 }
888