xref: /netbsd-src/sys/arch/sparc/dev/zs.c (revision bada23909e740596d0a3785a73bd3583a9807fb8)
1 /*	$NetBSD: zs.c,v 1.68 1999/03/05 08:30:33 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 "opt_ddb.h"
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/conf.h>
52 #include <sys/device.h>
53 #include <sys/file.h>
54 #include <sys/ioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/proc.h>
57 #include <sys/tty.h>
58 #include <sys/time.h>
59 #include <sys/syslog.h>
60 
61 #include <machine/bsd_openprom.h>
62 #include <machine/autoconf.h>
63 #include <machine/conf.h>
64 #include <machine/cpu.h>
65 #include <machine/eeprom.h>
66 #include <machine/psl.h>
67 #include <machine/z8530var.h>
68 
69 #include <dev/cons.h>
70 #include <dev/ic/z8530reg.h>
71 
72 #include <sparc/sparc/vaddrs.h>
73 #include <sparc/sparc/auxreg.h>
74 #include <sparc/dev/cons.h>
75 
76 #include "kbd.h"	/* NKBD */
77 #include "zs.h" 	/* NZS */
78 
79 /* Make life easier for the initialized arrays here. */
80 #if NZS < 3
81 #undef  NZS
82 #define NZS 3
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 Sun provides a 4.9152 MHz clock to the ZS chips.
95  */
96 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
97 
98 /*
99  * Select software interrupt bit based on TTY ipl.
100  */
101 #if PIL_TTY == 1
102 # define IE_ZSSOFT IE_L1
103 #elif PIL_TTY == 4
104 # define IE_ZSSOFT IE_L4
105 #elif PIL_TTY == 6
106 # define IE_ZSSOFT IE_L6
107 #else
108 # error "no suitable software interrupt bit"
109 #endif
110 
111 #define	ZS_DELAY()		(CPU_ISSUN4C ? (0) : delay(2))
112 
113 /* The layout of this is hardware-dependent (padding, order). */
114 struct zschan {
115 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
116 	u_char		zc_xxx0;
117 	volatile u_char	zc_data;	/* data */
118 	u_char		zc_xxx1;
119 };
120 struct zsdevice {
121 	/* Yes, they are backwards. */
122 	struct	zschan zs_chan_b;
123 	struct	zschan zs_chan_a;
124 };
125 
126 /* Saved PROM mappings */
127 static struct zsdevice *zsaddr[NZS];
128 
129 /* Flags from cninit() */
130 static int zs_hwflags[NZS][2];
131 
132 /* Default speed for each channel */
133 static int zs_defspeed[NZS][2] = {
134 	{ 9600, 	/* ttya */
135 	  9600 },	/* ttyb */
136 	{ 1200, 	/* keyboard */
137 	  1200 },	/* mouse */
138 	{ 9600, 	/* ttyc */
139 	  9600 },	/* ttyd */
140 };
141 
142 static u_char zs_init_reg[16] = {
143 	0,	/* 0: CMD (reset, etc.) */
144 	0,	/* 1: No interrupts yet. */
145 	0,	/* 2: IVECT */
146 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
147 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
148 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
149 	0,	/* 6: TXSYNC/SYNCLO */
150 	0,	/* 7: RXSYNC/SYNCHI */
151 	0,	/* 8: alias for data port */
152 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
153 	0,	/*10: Misc. TX/RX control bits */
154 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
155 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
156 	0,			/*13: BAUDHI (default=9600) */
157 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
158 	ZSWR15_BREAK_IE,
159 };
160 
161 struct zschan *
162 zs_get_chan_addr(zs_unit, channel)
163 	int zs_unit, channel;
164 {
165 	struct zsdevice	*addr;
166 	struct zschan	*zc;
167 
168 	if (zs_unit >= NZS)
169 		return (NULL);
170 	addr = zsaddr[zs_unit];
171 	if (addr == NULL)
172 		addr = zsaddr[zs_unit] = findzs(zs_unit);
173 	if (addr == NULL)
174 		return (NULL);
175 	if (channel == 0) {
176 		zc = &addr->zs_chan_a;
177 	} else {
178 		zc = &addr->zs_chan_b;
179 	}
180 	return (zc);
181 }
182 
183 
184 /****************************************************************
185  * Autoconfig
186  ****************************************************************/
187 
188 /* Definition of the driver for autoconfig. */
189 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
190 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
191 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
192 static void zs_attach_obio __P((struct device *, struct device *, void *));
193 
194 static void zs_attach __P((struct zsc_softc *, int));
195 static int  zs_print __P((void *, const char *name));
196 
197 struct cfattach zs_mainbus_ca = {
198 	sizeof(struct zsc_softc), zs_match_mainbus, zs_attach_mainbus
199 };
200 
201 struct cfattach zs_obio_ca = {
202 	sizeof(struct zsc_softc), zs_match_obio, zs_attach_obio
203 };
204 
205 extern struct cfdriver zs_cd;
206 
207 /* Interrupt handlers. */
208 static int zshard __P((void *));
209 static int zssoft __P((void *));
210 static struct intrhand levelsoft = { zssoft };
211 
212 static int zs_get_speed __P((struct zs_chanstate *));
213 
214 
215 /*
216  * Is the zs chip present?
217  */
218 static int
219 zs_match_mainbus(parent, cf, aux)
220 	struct device *parent;
221 	struct cfdata *cf;
222 	void *aux;
223 {
224 	struct mainbus_attach_args *ma = aux;
225 
226 	if (strcmp(cf->cf_driver->cd_name, ma->ma_name) != 0)
227 		return (0);
228 
229 	return (getpropint(ma->ma_node, "slave", -2) == cf->cf_unit);
230 }
231 
232 static int
233 zs_match_obio(parent, cf, aux)
234 	struct device *parent;
235 	struct cfdata *cf;
236 	void *aux;
237 {
238 	union obio_attach_args *uoba = aux;
239 	struct obio4_attach_args *oba;
240 
241 	if (uoba->uoba_isobio4 == 0) {
242 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
243 
244 		if (strcmp(cf->cf_driver->cd_name, sa->sa_name) != 0)
245 			return (0);
246 
247 		return (getpropint(sa->sa_node, "slave", -2) == cf->cf_unit);
248 	}
249 
250 	oba = &uoba->uoba_oba4;
251 	return (bus_space_probe(oba->oba_bustag, 0, oba->oba_paddr,
252 			        1, 0, 0, NULL, NULL));
253 }
254 
255 static void
256 zs_attach_mainbus(parent, self, aux)
257 	struct device *parent;
258 	struct device *self;
259 	void *aux;
260 {
261 	struct zsc_softc *zsc = (void *) self;
262 	struct mainbus_attach_args *ma = aux;
263 	int zs_unit = zsc->zsc_dev.dv_unit;
264 
265 	zsc->zsc_bustag = ma->ma_bustag;
266 	zsc->zsc_dmatag = ma->ma_dmatag;
267 
268 	/* Use the mapping setup by the Sun PROM. */
269 	if (zsaddr[zs_unit] == NULL)
270 		zsaddr[zs_unit] = findzs(zs_unit);
271 	if ((void*)zsaddr[zs_unit] != ma->ma_promvaddr)
272 		panic("zsattach_mainbus");
273 
274 	zs_attach(zsc, ma->ma_pri);
275 }
276 
277 static void
278 zs_attach_obio(parent, self, aux)
279 	struct device *parent;
280 	struct device *self;
281 	void *aux;
282 {
283 	struct zsc_softc *zsc = (void *) self;
284 	union obio_attach_args *uoba = aux;
285 	int zs_unit = zsc->zsc_dev.dv_unit;
286 
287 	/* Use the mapping setup by the Sun PROM. */
288 	if (zsaddr[zs_unit] == NULL)
289 		zsaddr[zs_unit] = findzs(zs_unit);
290 
291 	if (uoba->uoba_isobio4 == 0) {
292 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
293 		zsc->zsc_bustag = sa->sa_bustag;
294 		zsc->zsc_dmatag = sa->sa_dmatag;
295 		zs_attach(zsc, sa->sa_pri);
296 	} else {
297 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
298 		zsc->zsc_bustag = oba->oba_bustag;
299 		zsc->zsc_dmatag = oba->oba_dmatag;
300 		zs_attach(zsc, oba->oba_pri);
301 	}
302 }
303 /*
304  * Attach a found zs.
305  *
306  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
307  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
308  */
309 static void
310 zs_attach(zsc, pri)
311 	struct zsc_softc *zsc;
312 	int pri;
313 {
314 	struct zsc_attach_args zsc_args;
315 	volatile struct zschan *zc;
316 	struct zs_chanstate *cs;
317 	int s, zs_unit, channel;
318 	static int didintr, prevpri;
319 
320 	printf(" softpri %d\n", PIL_TTY);
321 
322 	/*
323 	 * Initialize software state for each channel.
324 	 */
325 	zs_unit = zsc->zsc_dev.dv_unit;
326 	for (channel = 0; channel < 2; channel++) {
327 		zsc_args.channel = channel;
328 		zsc_args.hwflags = zs_hwflags[zs_unit][channel];
329 		cs = &zsc->zsc_cs_store[channel];
330 		zsc->zsc_cs[channel] = cs;
331 
332 		cs->cs_channel = channel;
333 		cs->cs_private = NULL;
334 		cs->cs_ops = &zsops_null;
335 		cs->cs_brg_clk = PCLK / 16;
336 
337 		zc = zs_get_chan_addr(zs_unit, channel);
338 		cs->cs_reg_csr  = &zc->zc_csr;
339 		cs->cs_reg_data = &zc->zc_data;
340 
341 		bcopy(zs_init_reg, cs->cs_creg, 16);
342 		bcopy(zs_init_reg, cs->cs_preg, 16);
343 
344 		/* XXX: Get these from the PROM properties! */
345 		/* XXX: See the mvme167 code.  Better. */
346 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE)
347 			cs->cs_defspeed = zs_get_speed(cs);
348 		else
349 			cs->cs_defspeed = zs_defspeed[zs_unit][channel];
350 		cs->cs_defcflag = zs_def_cflag;
351 
352 		/* Make these correspond to cs_defcflag (-crtscts) */
353 		cs->cs_rr0_dcd = ZSRR0_DCD;
354 		cs->cs_rr0_cts = 0;
355 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
356 		cs->cs_wr5_rts = 0;
357 
358 		/*
359 		 * Clear the master interrupt enable.
360 		 * The INTENA is common to both channels,
361 		 * so just do it on the A channel.
362 		 */
363 		if (channel == 0) {
364 			zs_write_reg(cs, 9, 0);
365 		}
366 
367 		/*
368 		 * Look for a child driver for this channel.
369 		 * The child attach will setup the hardware.
370 		 */
371 		if (!config_found(&zsc->zsc_dev, (void *)&zsc_args, zs_print)) {
372 			/* No sub-driver.  Just reset it. */
373 			u_char reset = (channel == 0) ?
374 				ZSWR9_A_RESET : ZSWR9_B_RESET;
375 			s = splzs();
376 			zs_write_reg(cs,  9, reset);
377 			splx(s);
378 		}
379 	}
380 
381 	/*
382 	 * Now safe to install interrupt handlers.  Note the arguments
383 	 * to the interrupt handlers aren't used.  Note, we only do this
384 	 * once since both SCCs interrupt at the same level and vector.
385 	 */
386 	if (!didintr) {
387 		didintr = 1;
388 		prevpri = pri;
389 		bus_intr_establish(zsc->zsc_bustag, pri, 0, zshard, NULL);
390 		intr_establish(PIL_TTY, &levelsoft);
391 	} else if (pri != prevpri)
392 		panic("broken zs interrupt scheme");
393 
394 	evcnt_attach(&zsc->zsc_dev, "intr", &zsc->zsc_intrcnt);
395 
396 	/*
397 	 * Set the master interrupt enable and interrupt vector.
398 	 * (common to both channels, do it on A)
399 	 */
400 	cs = zsc->zsc_cs[0];
401 	s = splhigh();
402 	/* interrupt vector */
403 	zs_write_reg(cs, 2, zs_init_reg[2]);
404 	/* master interrupt control (enable) */
405 	zs_write_reg(cs, 9, zs_init_reg[9]);
406 	splx(s);
407 
408 #if 0
409 	/*
410 	 * XXX: L1A hack - We would like to be able to break into
411 	 * the debugger during the rest of autoconfiguration, so
412 	 * lower interrupts just enough to let zs interrupts in.
413 	 * This is done after both zs devices are attached.
414 	 */
415 	if (zs_unit == 1) {
416 		printf("zs1: enabling zs interrupts\n");
417 		(void)splfd(); /* XXX: splzs - 1 */
418 	}
419 #endif
420 }
421 
422 static int
423 zs_print(aux, name)
424 	void *aux;
425 	const char *name;
426 {
427 	struct zsc_attach_args *args = aux;
428 
429 	if (name != NULL)
430 		printf("%s: ", name);
431 
432 	if (args->channel != -1)
433 		printf(" channel %d", args->channel);
434 
435 	return (UNCONF);
436 }
437 
438 static volatile int zssoftpending;
439 
440 /*
441  * Our ZS chips all share a common, autovectored interrupt,
442  * so we have to look at all of them on each interrupt.
443  */
444 static int
445 zshard(arg)
446 	void *arg;
447 {
448 	register struct zsc_softc *zsc;
449 	register int unit, rr3, rval, softreq;
450 
451 	rval = softreq = 0;
452 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
453 		zsc = zs_cd.cd_devs[unit];
454 		if (zsc == NULL)
455 			continue;
456 		rr3 = zsc_intr_hard(zsc);
457 		/* Count up the interrupts. */
458 		if (rr3) {
459 			rval |= rr3;
460 			zsc->zsc_intrcnt.ev_count++;
461 		}
462 		softreq |= zsc->zsc_cs[0]->cs_softreq;
463 		softreq |= zsc->zsc_cs[1]->cs_softreq;
464 	}
465 
466 	/* We are at splzs here, so no need to lock. */
467 	if (softreq && (zssoftpending == 0)) {
468 		zssoftpending = IE_ZSSOFT;
469 #if defined(SUN4M)
470 		if (CPU_ISSUN4M)
471 			raise(0, PIL_TTY);
472 		else
473 #endif
474 			ienab_bis(IE_ZSSOFT);
475 	}
476 	return (rval);
477 }
478 
479 /*
480  * Similar scheme as for zshard (look at all of them)
481  */
482 static int
483 zssoft(arg)
484 	void *arg;
485 {
486 	register struct zsc_softc *zsc;
487 	register int s, unit;
488 
489 	/* This is not the only ISR on this IPL. */
490 	if (zssoftpending == 0)
491 		return (0);
492 
493 	/*
494 	 * The soft intr. bit will be set by zshard only if
495 	 * the variable zssoftpending is zero.  The order of
496 	 * these next two statements prevents our clearing
497 	 * the soft intr bit just after zshard has set it.
498 	 */
499 	/* ienab_bic(IE_ZSSOFT); */
500 	zssoftpending = 0;
501 
502 	/* Make sure we call the tty layer at spltty. */
503 	s = spltty();
504 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
505 		zsc = zs_cd.cd_devs[unit];
506 		if (zsc == NULL)
507 			continue;
508 		(void)zsc_intr_soft(zsc);
509 	}
510 	splx(s);
511 	return (1);
512 }
513 
514 
515 /*
516  * Compute the current baud rate given a ZS channel.
517  */
518 static int
519 zs_get_speed(cs)
520 	struct zs_chanstate *cs;
521 {
522 	int tconst;
523 
524 	tconst = zs_read_reg(cs, 12);
525 	tconst |= zs_read_reg(cs, 13) << 8;
526 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
527 }
528 
529 /*
530  * MD functions for setting the baud rate and control modes.
531  */
532 int
533 zs_set_speed(cs, bps)
534 	struct zs_chanstate *cs;
535 	int bps;	/* bits per second */
536 {
537 	int tconst, real_bps;
538 
539 	if (bps == 0)
540 		return (0);
541 
542 #ifdef	DIAGNOSTIC
543 	if (cs->cs_brg_clk == 0)
544 		panic("zs_set_speed");
545 #endif
546 
547 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
548 	if (tconst < 0)
549 		return (EINVAL);
550 
551 	/* Convert back to make sure we can do it. */
552 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
553 
554 	/* XXX - Allow some tolerance here? */
555 	if (real_bps != bps)
556 		return (EINVAL);
557 
558 	cs->cs_preg[12] = tconst;
559 	cs->cs_preg[13] = tconst >> 8;
560 
561 	/* Caller will stuff the pending registers. */
562 	return (0);
563 }
564 
565 int
566 zs_set_modes(cs, cflag)
567 	struct zs_chanstate *cs;
568 	int cflag;	/* bits per second */
569 {
570 	int s;
571 
572 	/*
573 	 * Output hardware flow control on the chip is horrendous:
574 	 * if carrier detect drops, the receiver is disabled, and if
575 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
576 	 * Therefore, NEVER set the HFC bit, and instead use the
577 	 * status interrupt to detect CTS changes.
578 	 */
579 	s = splzs();
580 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
581 		cs->cs_rr0_dcd = 0;
582 	else
583 		cs->cs_rr0_dcd = ZSRR0_DCD;
584 	if ((cflag & CRTSCTS) != 0) {
585 		cs->cs_wr5_dtr = ZSWR5_DTR;
586 		cs->cs_wr5_rts = ZSWR5_RTS;
587 		cs->cs_rr0_cts = ZSRR0_CTS;
588 	} else if ((cflag & CDTRCTS) != 0) {
589 		cs->cs_wr5_dtr = 0;
590 		cs->cs_wr5_rts = ZSWR5_DTR;
591 		cs->cs_rr0_cts = ZSRR0_CTS;
592 	} else if ((cflag & MDMBUF) != 0) {
593 		cs->cs_wr5_dtr = 0;
594 		cs->cs_wr5_rts = ZSWR5_DTR;
595 		cs->cs_rr0_cts = ZSRR0_DCD;
596 	} else {
597 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
598 		cs->cs_wr5_rts = 0;
599 		cs->cs_rr0_cts = 0;
600 	}
601 	splx(s);
602 
603 	/* Caller will stuff the pending registers. */
604 	return (0);
605 }
606 
607 
608 /*
609  * Read or write the chip with suitable delays.
610  */
611 
612 u_char
613 zs_read_reg(cs, reg)
614 	struct zs_chanstate *cs;
615 	u_char reg;
616 {
617 	u_char val;
618 
619 	*cs->cs_reg_csr = reg;
620 	ZS_DELAY();
621 	val = *cs->cs_reg_csr;
622 	ZS_DELAY();
623 	return (val);
624 }
625 
626 void
627 zs_write_reg(cs, reg, val)
628 	struct zs_chanstate *cs;
629 	u_char reg, val;
630 {
631 	*cs->cs_reg_csr = reg;
632 	ZS_DELAY();
633 	*cs->cs_reg_csr = val;
634 	ZS_DELAY();
635 }
636 
637 u_char
638 zs_read_csr(cs)
639 	struct zs_chanstate *cs;
640 {
641 	register u_char val;
642 
643 	val = *cs->cs_reg_csr;
644 	ZS_DELAY();
645 	return (val);
646 }
647 
648 void  zs_write_csr(cs, val)
649 	struct zs_chanstate *cs;
650 	u_char val;
651 {
652 	*cs->cs_reg_csr = val;
653 	ZS_DELAY();
654 }
655 
656 u_char zs_read_data(cs)
657 	struct zs_chanstate *cs;
658 {
659 	register u_char val;
660 
661 	val = *cs->cs_reg_data;
662 	ZS_DELAY();
663 	return (val);
664 }
665 
666 void  zs_write_data(cs, val)
667 	struct zs_chanstate *cs;
668 	u_char val;
669 {
670 	*cs->cs_reg_data = val;
671 	ZS_DELAY();
672 }
673 
674 /****************************************************************
675  * Console support functions (Sun specific!)
676  * Note: this code is allowed to know about the layout of
677  * the chip registers, and uses that to keep things simple.
678  * XXX - I think I like the mvme167 code better. -gwr
679  ****************************************************************/
680 
681 extern void Debugger __P((void));
682 void *zs_conschan;
683 
684 /*
685  * Handle user request to enter kernel debugger.
686  */
687 void
688 zs_abort(cs)
689 	struct zs_chanstate *cs;
690 {
691 	register volatile struct zschan *zc = zs_conschan;
692 	int rr0;
693 
694 	/* Wait for end of break to avoid PROM abort. */
695 	/* XXX - Limit the wait? */
696 	do {
697 		rr0 = zc->zc_csr;
698 		ZS_DELAY();
699 	} while (rr0 & ZSRR0_BREAK);
700 
701 #if defined(KGDB)
702 	zskgdb(cs);
703 #elif defined(DDB)
704 	Debugger();
705 #else
706 	printf("stopping on keyboard abort\n");
707 	callrom();
708 #endif
709 }
710 
711 /*
712  * Polled input char.
713  */
714 int
715 zs_getc(arg)
716 	void *arg;
717 {
718 	register volatile struct zschan *zc = arg;
719 	register int s, c, rr0;
720 
721 	s = splhigh();
722 	/* Wait for a character to arrive. */
723 	do {
724 		rr0 = zc->zc_csr;
725 		ZS_DELAY();
726 	} while ((rr0 & ZSRR0_RX_READY) == 0);
727 
728 	c = zc->zc_data;
729 	ZS_DELAY();
730 	splx(s);
731 
732 	/*
733 	 * This is used by the kd driver to read scan codes,
734 	 * so don't translate '\r' ==> '\n' here...
735 	 */
736 	return (c);
737 }
738 
739 /*
740  * Polled output char.
741  */
742 void
743 zs_putc(arg, c)
744 	void *arg;
745 	int c;
746 {
747 	register volatile struct zschan *zc = arg;
748 	register int s, rr0;
749 
750 	s = splhigh();
751 
752 	/* Wait for transmitter to become ready. */
753 	do {
754 		rr0 = zc->zc_csr;
755 		ZS_DELAY();
756 	} while ((rr0 & ZSRR0_TX_READY) == 0);
757 
758 	/*
759 	 * Send the next character.
760 	 * Now you'd think that this could be followed by a ZS_DELAY()
761 	 * just like all the other chip accesses, but it turns out that
762 	 * the `transmit-ready' interrupt isn't de-asserted until
763 	 * some period of time after the register write completes
764 	 * (more than a couple instructions).  So to avoid stray
765 	 * interrupts we put in the 2us delay regardless of cpu model.
766 	 */
767 	zc->zc_data = c;
768 	delay(2);
769 
770 	splx(s);
771 }
772 
773 /*****************************************************************/
774 
775 static void zscninit __P((struct consdev *));
776 static int  zscngetc __P((dev_t));
777 static void zscnputc __P((dev_t, int));
778 
779 /*
780  * Console table shared by ttya, ttyb
781  */
782 struct consdev consdev_tty = {
783 	nullcnprobe,
784 	zscninit,
785 	zscngetc,
786 	zscnputc,
787 	nullcnpollc,
788 };
789 
790 static void
791 zscninit(cn)
792 	struct consdev *cn;
793 {
794 }
795 
796 /*
797  * Polled console input putchar.
798  */
799 static int
800 zscngetc(dev)
801 	dev_t dev;
802 {
803 	return (zs_getc(zs_conschan));
804 }
805 
806 /*
807  * Polled console output putchar.
808  */
809 static void
810 zscnputc(dev, c)
811 	dev_t dev;
812 	int c;
813 {
814 	zs_putc(zs_conschan, c);
815 }
816 
817 /*****************************************************************/
818 
819 static void prom_cninit __P((struct consdev *));
820 static int  prom_cngetc __P((dev_t));
821 static void prom_cnputc __P((dev_t, int));
822 
823 /*
824  * The console is set to this one initially,
825  * which lets us use the PROM until consinit()
826  * is called to select a real console.
827  */
828 struct consdev consdev_prom = {
829 	nullcnprobe,
830 	prom_cninit,
831 	prom_cngetc,
832 	prom_cnputc,
833 	nullcnpollc,
834 };
835 
836 /*
837  * The console table pointer is statically initialized
838  * to point to the PROM (output only) table, so that
839  * early calls to printf will work.
840  */
841 struct consdev *cn_tab = &consdev_prom;
842 
843 void
844 nullcnprobe(cn)
845 	struct consdev *cn;
846 {
847 }
848 
849 static void
850 prom_cninit(cn)
851 	struct consdev *cn;
852 {
853 }
854 
855 /*
856  * PROM console input putchar.
857  * (dummy - this is output only) (WHY?????!)
858  */
859 static int
860 prom_cngetc(dev)
861 	dev_t dev;
862 {
863 	return (prom_getchar());
864 }
865 
866 /*
867  * PROM console output putchar.
868  */
869 static void
870 prom_cnputc(dev, c)
871 	dev_t dev;
872 	int c;
873 {
874 
875 	prom_putchar(c);
876 }
877 
878 /*****************************************************************/
879 
880 extern struct consdev consdev_kd;
881 
882 static char *prom_inSrc_name[] = {
883 	"keyboard/display",
884 	"ttya", "ttyb",
885 	"ttyc", "ttyd" };
886 
887 
888 static int get_serial_promdev __P((int));
889 
890 int
891 get_serial_promdev(io)
892 	int io;
893 {
894 	char *prop, *cp, buffer[128];
895 	int node;
896 
897 	node = findroot();
898 	prop = (io == 0) ? "stdin-path" : "stdout-path";
899 
900 	cp = getpropstringA(node, prop, buffer, sizeof buffer);
901 
902 	/*
903 	 * At this point we assume the device path is in the form
904 	 *   ....device@x,y:a for ttya and ...device@x,y:b for ttyb, etc.
905 	 */
906 	while (*cp != 0)
907 		cp++;
908 	cp -= 2;
909 
910 	if (cp >= buffer) {
911 		/* XXX: only allows tty's a->z, assumes PROMDEV_TTYx contig */
912 		if (cp[0] == ':' && cp[1] >= 'a' && cp[1] <= 'z')
913 			return (PROMDEV_TTYA + (cp[1] - 'a'));
914 	}
915 
916 	printf("Warning: unparseable %s property\n", prop);
917 	return (-1);
918 }
919 
920 /*
921  * This function replaces sys/dev/cninit.c
922  * Determine which device is the console using
923  * the PROM "input source" and "output sink".
924  */
925 void
926 consinit()
927 {
928 	struct zschan *zc;
929 	struct consdev *cn;
930 	int channel, zs_unit, zstty_unit;
931 	int inSource, outSink;
932 	int node;
933 	char *devtype;
934 	extern int fbnode;
935 
936 	switch (prom_version()) {
937 	case PROM_OLDMON:
938 	case PROM_OBP_V0:
939 		/* The stdio handles identify the device type */
940 		inSource = prom_stdin();
941 		outSink  = prom_stdout();
942 		break;
943 	case PROM_OBP_V2:
944 	case PROM_OBP_V3:
945 	case PROM_OPENFIRM:
946 		/*
947 		 * We need to probe the PROM device tree.
948 		 *
949 		 * Translate the STDIO package instance (`ihandle') -- that
950 		 * the PROM has already opened for us -- to a device tree
951 		 * node (i.e. a `phandle').
952 		 */
953 
954 		if ((node = prom_instance_to_package(prom_stdin())) == 0) {
955 			printf("consinit: cannot convert stdin ihandle\n");
956 			inSource = -1;
957 			goto setup_output;
958 		}
959 
960 		if (prom_node_has_property(node, "keyboard")) {
961 			inSource = PROMDEV_KBD;
962 		} else if (strcmp(getpropstring(node, "device_type"),
963 				  "serial") == 0) {
964 			inSource = get_serial_promdev(0);
965 		} else {
966 			/* not serial, not keyboard. what is it?!? */
967 			inSource = -1;
968 		}
969 
970 setup_output:
971 		if ((node = prom_instance_to_package(prom_stdout())) == 0) {
972 			printf("consinit: cannot convert stdout ihandle\n");
973 			outSink = -1;
974 			goto setup_console;
975 		}
976 		devtype = getpropstring(node, "device_type");
977 		if (strcmp(devtype, "display") == 0) {
978 			/* frame buffer output */
979 			outSink = PROMDEV_SCREEN;
980 			fbnode = node;
981 		} else if (strcmp(devtype, "serial") == 0) {
982 			outSink = get_serial_promdev(1);
983 		} else {
984 			/* not screen, not serial. Whatzit? */
985 			outSink = -1;
986 		}
987 		break;
988 
989 	default:
990 		inSource = -1;
991 		outSink = -1;
992 	}
993 
994 setup_console:
995 	if (inSource != outSink) {
996 		printf("cninit: mismatched PROM output selector\n");
997 		printf("inSource=%x; Sink=%x\n", inSource, outSink);
998 	}
999 
1000 	switch (inSource) {
1001 	default:
1002 		printf("cninit: invalid inSource=0x%x\n", inSource);
1003 		prom_abort();
1004 		inSource = PROMDEV_KBD;
1005 		/* fall through */
1006 
1007 	case 0:	/* keyboard/display */
1008 #if NKBD > 0
1009 		zs_unit = 1;	/* XXX - config info! */
1010 		channel = 0;
1011 		cn = &consdev_kd;
1012 		/* Set cn_dev, cn_pri in kd.c */
1013 		break;
1014 #else	/* NKBD */
1015 		printf("cninit: kdb/display not configured\n");
1016 		callrom();
1017 		inSource = PROMDEV_TTYA;
1018 		/* fall through */
1019 #endif	/* NKBD */
1020 
1021 	case PROMDEV_TTYA:
1022 	case PROMDEV_TTYB:
1023 		zstty_unit = inSource - PROMDEV_TTYA;
1024 		zs_unit = 0;	/* XXX - config info! */
1025 		channel = zstty_unit & 1;
1026 		cn = &consdev_tty;
1027 		cn->cn_dev = makedev(zs_major, zstty_unit);
1028 		cn->cn_pri = CN_REMOTE;
1029 		break;
1030 
1031 	}
1032 	/* Now that inSource has been validated, print it. */
1033 	printf("console is %s\n", prom_inSrc_name[inSource]);
1034 
1035 	zc = zs_get_chan_addr(zs_unit, channel);
1036 	if (zc == NULL) {
1037 		printf("cninit: zs not mapped.\n");
1038 		return;
1039 	}
1040 	zs_conschan = zc;
1041 	zs_hwflags[zs_unit][channel] = ZS_HWFLAG_CONSOLE;
1042 	cn_tab = cn;
1043 	(*cn->cn_init)(cn);
1044 #ifdef	KGDB
1045 	zs_kgdb_init();
1046 #endif
1047 }
1048