xref: /netbsd-src/sys/arch/sparc/dev/zs.c (revision 5b84b3983f71fd20a534cfa5d1556623a8aaa717)
1 /*	$NetBSD: zs.c,v 1.102 2005/06/30 12:07:51 macallan 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/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.102 2005/06/30 12:07:51 macallan Exp $");
49 
50 #include "opt_ddb.h"
51 #include "opt_kgdb.h"
52 #include "opt_sparc_arch.h"
53 
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/conf.h>
57 #include <sys/device.h>
58 #include <sys/file.h>
59 #include <sys/ioctl.h>
60 #include <sys/kernel.h>
61 #include <sys/proc.h>
62 #include <sys/tty.h>
63 #include <sys/time.h>
64 #include <sys/syslog.h>
65 
66 #include <machine/bsd_openprom.h>
67 #include <machine/autoconf.h>
68 #include <machine/intr.h>
69 #include <machine/eeprom.h>
70 #include <machine/psl.h>
71 #include <machine/z8530var.h>
72 
73 #include <dev/cons.h>
74 #include <dev/ic/z8530reg.h>
75 
76 #include <sparc/sparc/vaddrs.h>
77 #include <sparc/sparc/auxreg.h>
78 #include <sparc/sparc/auxiotwo.h>
79 #include <sparc/dev/cons.h>
80 #include <dev/sun/kbd_ms_ttyvar.h>
81 
82 #include "kbd.h"
83 #include "ms.h"
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 
92 /*
93  * The Sun provides a 4.9152 MHz clock to the ZS chips.
94  */
95 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
96 
97 #define	ZS_DELAY()		(CPU_ISSUN4C ? (0) : delay(2))
98 
99 /* The layout of this is hardware-dependent (padding, order). */
100 struct zschan {
101 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
102 	u_char		zc_xxx0;
103 	volatile u_char	zc_data;	/* data */
104 	u_char		zc_xxx1;
105 };
106 struct zsdevice {
107 	/* Yes, they are backwards. */
108 	struct	zschan zs_chan_b;
109 	struct	zschan zs_chan_a;
110 };
111 
112 /* ZS channel used as the console device (if any) */
113 void *zs_conschan_get, *zs_conschan_put;
114 
115 static u_char zs_init_reg[16] = {
116 	0,	/* 0: CMD (reset, etc.) */
117 	0,	/* 1: No interrupts yet. */
118 	0,	/* 2: IVECT */
119 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
120 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
121 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
122 	0,	/* 6: TXSYNC/SYNCLO */
123 	0,	/* 7: RXSYNC/SYNCHI */
124 	0,	/* 8: alias for data port */
125 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
126 	0,	/*10: Misc. TX/RX control bits */
127 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
128 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
129 	0,			/*13: BAUDHI (default=9600) */
130 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
131 	ZSWR15_BREAK_IE,
132 };
133 
134 /* Console ops */
135 static int  zscngetc __P((dev_t));
136 static void zscnputc __P((dev_t, int));
137 static void zscnpollc __P((dev_t, int));
138 
139 struct consdev zs_consdev = {
140 	NULL,
141 	NULL,
142 	zscngetc,
143 	zscnputc,
144 	zscnpollc,
145 	NULL,
146 };
147 
148 
149 /****************************************************************
150  * Autoconfig
151  ****************************************************************/
152 
153 /* Definition of the driver for autoconfig. */
154 static int  zs_match_mainbus __P((struct device *, struct cfdata *, void *));
155 static int  zs_match_obio __P((struct device *, struct cfdata *, void *));
156 static void zs_attach_mainbus __P((struct device *, struct device *, void *));
157 static void zs_attach_obio __P((struct device *, struct device *, void *));
158 
159 #if defined(SUN4D)
160 #include <sparc/dev/bootbusvar.h>
161 
162 static int  zs_match_bootbus __P((struct device *, struct cfdata *, void *));
163 static void zs_attach_bootbus __P((struct device *, struct device *, void *));
164 
165 CFATTACH_DECL(zs_bootbus, sizeof(struct zsc_softc),
166     zs_match_bootbus, zs_attach_bootbus, NULL, NULL);
167 #endif /* SUN4D */
168 
169 static void zs_attach __P((struct zsc_softc *, struct zsdevice *, int));
170 static int  zs_print __P((void *, const char *name));
171 
172 CFATTACH_DECL(zs_mainbus, sizeof(struct zsc_softc),
173     zs_match_mainbus, zs_attach_mainbus, NULL, NULL);
174 
175 CFATTACH_DECL(zs_obio, sizeof(struct zsc_softc),
176     zs_match_obio, zs_attach_obio, NULL, NULL);
177 
178 extern struct cfdriver zs_cd;
179 
180 /* softintr(9) cookie, shared by all instances of this driver */
181 static void *zs_sicookie;
182 
183 /* Interrupt handlers. */
184 static int zshard __P((void *));
185 static void zssoft __P((void *));
186 
187 static int zs_get_speed __P((struct zs_chanstate *));
188 
189 /* Console device support */
190 static int zs_console_flags __P((int, int, int));
191 
192 /* Power management hooks */
193 int  zs_enable __P((struct zs_chanstate *));
194 void zs_disable __P((struct zs_chanstate *));
195 
196 
197 /* XXX from dev/ic/z8530tty.c */
198 extern struct tty *zstty_get_tty_from_dev(struct device *);
199 
200 /*
201  * Is the zs chip present?
202  */
203 static int
204 zs_match_mainbus(parent, cf, aux)
205 	struct device *parent;
206 	struct cfdata *cf;
207 	void *aux;
208 {
209 	struct mainbus_attach_args *ma = aux;
210 
211 	if (strcmp(cf->cf_name, ma->ma_name) != 0)
212 		return (0);
213 
214 	return (1);
215 }
216 
217 static int
218 zs_match_obio(parent, cf, aux)
219 	struct device *parent;
220 	struct cfdata *cf;
221 	void *aux;
222 {
223 	union obio_attach_args *uoba = aux;
224 	struct obio4_attach_args *oba;
225 
226 	if (uoba->uoba_isobio4 == 0) {
227 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
228 
229 		if (strcmp(cf->cf_name, sa->sa_name) != 0)
230 			return (0);
231 
232 		return (1);
233 	}
234 
235 	oba = &uoba->uoba_oba4;
236 	return (bus_space_probe(oba->oba_bustag, oba->oba_paddr,
237 			        1, 0, 0, NULL, NULL));
238 }
239 
240 #if defined(SUN4D)
241 static int
242 zs_match_bootbus(parent, cf, aux)
243 	struct device *parent;
244 	struct cfdata *cf;
245 	void *aux;
246 {
247 	struct bootbus_attach_args *baa = aux;
248 
249 	return (strcmp(cf->cf_name, baa->ba_name) == 0);
250 }
251 #endif /* SUN4D */
252 
253 static void
254 zs_attach_mainbus(parent, self, aux)
255 	struct device *parent;
256 	struct device *self;
257 	void *aux;
258 {
259 	struct zsc_softc *zsc = (void *) self;
260 	struct mainbus_attach_args *ma = aux;
261 
262 	zsc->zsc_bustag = ma->ma_bustag;
263 	zsc->zsc_dmatag = ma->ma_dmatag;
264 	zsc->zsc_promunit = prom_getpropint(ma->ma_node, "slave", -2);
265 	zsc->zsc_node = ma->ma_node;
266 
267 	/*
268 	 * For machines with zs on mainbus (all sun4c models), we expect
269 	 * the device registers to be mapped by the PROM.
270 	 */
271 	zs_attach(zsc, ma->ma_promvaddr, ma->ma_pri);
272 }
273 
274 static void
275 zs_attach_obio(parent, self, aux)
276 	struct device *parent;
277 	struct device *self;
278 	void *aux;
279 {
280 	struct zsc_softc *zsc = (void *) self;
281 	union obio_attach_args *uoba = aux;
282 
283 	if (uoba->uoba_isobio4 == 0) {
284 		struct sbus_attach_args *sa = &uoba->uoba_sbus;
285 		void *va;
286 		struct zs_chanstate *cs;
287 		int channel;
288 
289 		if (sa->sa_nintr == 0) {
290 			printf(" no interrupt lines\n");
291 			return;
292 		}
293 
294 		/*
295 		 * Some sun4m models (Javastations) may not map the zs device.
296 		 */
297 		if (sa->sa_npromvaddrs > 0)
298 			va = (void *)sa->sa_promvaddr;
299 		else {
300 			bus_space_handle_t bh;
301 
302 			if (sbus_bus_map(sa->sa_bustag,
303 					 sa->sa_slot,
304 					 sa->sa_offset,
305 					 sa->sa_size,
306 					 BUS_SPACE_MAP_LINEAR, &bh) != 0) {
307 				printf(" cannot map zs registers\n");
308 				return;
309 			}
310 			va = (void *)bh;
311 		}
312 
313 		/*
314 		 * Check if power state can be set, e.g. Tadpole 3GX
315 		 */
316 		if (prom_getpropint(sa->sa_node, "pwr-on-auxio2", 0))
317 		{
318 			printf (" powered via auxio2");
319 			for (channel = 0; channel < 2; channel++) {
320 				cs = &zsc->zsc_cs_store[channel];
321 				cs->enable = zs_enable;
322 				cs->disable = zs_disable;
323 			}
324 		}
325 
326 		zsc->zsc_bustag = sa->sa_bustag;
327 		zsc->zsc_dmatag = sa->sa_dmatag;
328 		zsc->zsc_promunit = prom_getpropint(sa->sa_node, "slave", -2);
329 		zsc->zsc_node = sa->sa_node;
330 		zs_attach(zsc, va, sa->sa_pri);
331 	} else {
332 		struct obio4_attach_args *oba = &uoba->uoba_oba4;
333 		bus_space_handle_t bh;
334 		bus_addr_t paddr = oba->oba_paddr;
335 
336 		/*
337 		 * As for zs on mainbus, we require a PROM mapping.
338 		 */
339 		if (bus_space_map(oba->oba_bustag,
340 				  paddr,
341 				  sizeof(struct zsdevice),
342 				  BUS_SPACE_MAP_LINEAR | OBIO_BUS_MAP_USE_ROM,
343 				  &bh) != 0) {
344 			printf(" cannot map zs registers\n");
345 			return;
346 		}
347 		zsc->zsc_bustag = oba->oba_bustag;
348 		zsc->zsc_dmatag = oba->oba_dmatag;
349 		/*
350 		 * Find prom unit by physical address
351 		 * We're just comparing the address (not the iospace) here
352 		 */
353 		paddr = BUS_ADDR_PADDR(paddr);
354 		if (cpuinfo.cpu_type == CPUTYP_4_100)
355 			/*
356 			 * On the sun4/100, the top-most 4 bits are zero
357 			 * on obio addresses; force them to 1's for the
358 			 * sake of the comparison here.
359 			 */
360 			paddr |= 0xf0000000;
361 		zsc->zsc_promunit =
362 			(paddr == 0xf1000000) ? 0 :
363 			(paddr == 0xf0000000) ? 1 :
364 			(paddr == 0xe0000000) ? 2 : -2;
365 
366 		zs_attach(zsc, (void *)bh, oba->oba_pri);
367 	}
368 }
369 
370 #if defined(SUN4D)
371 static void
372 zs_attach_bootbus(parent, self, aux)
373 	struct device *parent;
374 	struct device *self;
375 	void *aux;
376 {
377 	struct zsc_softc *zsc = (void *) self;
378 	struct bootbus_attach_args *baa = aux;
379 	void *va;
380 
381 	if (baa->ba_nintr == 0) {
382 		printf(": no interrupt lines\n");
383 		return;
384 	}
385 
386 	if (baa->ba_npromvaddrs > 0)
387 		va = (void *) baa->ba_promvaddrs;
388 	else {
389 		bus_space_handle_t bh;
390 
391 		if (bus_space_map(baa->ba_bustag,
392 		    BUS_ADDR(baa->ba_slot, baa->ba_offset),
393 		    baa->ba_size, BUS_SPACE_MAP_LINEAR, &bh) != 0) {
394 			printf(": cannot map zs registers\n");
395 			return;
396 		}
397 		va = (void *) bh;
398 	}
399 
400 	zsc->zsc_bustag = baa->ba_bustag;
401 	zsc->zsc_promunit = prom_getpropint(baa->ba_node, "slave", -2);
402 	zsc->zsc_node = baa->ba_node;
403 	zs_attach(zsc, va, baa->ba_intr[0].oi_pri);
404 }
405 #endif /* SUN4D */
406 
407 /*
408  * Attach a found zs.
409  *
410  * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
411  * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
412  */
413 static void
414 zs_attach(zsc, zsd, pri)
415 	struct zsc_softc *zsc;
416 	struct zsdevice *zsd;
417 	int pri;
418 {
419 	struct zsc_attach_args zsc_args;
420 	struct zs_chanstate *cs;
421 	int s, channel;
422 	static int didintr, prevpri;
423 
424 	if (zsd == NULL) {
425 		printf("configuration incomplete\n");
426 		return;
427 	}
428 
429 	if (!didintr) {
430 		zs_sicookie = softintr_establish(IPL_SOFTSERIAL, zssoft, NULL);
431 		if (zs_sicookie == NULL) {
432 			printf("\n%s: cannot establish soft int handler\n",
433 				zsc->zsc_dev.dv_xname);
434 			return;
435 		}
436 	}
437 	printf(" softpri %d\n", IPL_SOFTSERIAL);
438 
439 	/*
440 	 * Initialize software state for each channel.
441 	 */
442 	for (channel = 0; channel < 2; channel++) {
443 		struct zschan *zc;
444 		struct device *child;
445 
446 		zsc_args.channel = channel;
447 		cs = &zsc->zsc_cs_store[channel];
448 		zsc->zsc_cs[channel] = cs;
449 
450 		simple_lock_init(&cs->cs_lock);
451 		cs->cs_channel = channel;
452 		cs->cs_private = NULL;
453 		cs->cs_ops = &zsops_null;
454 		cs->cs_brg_clk = PCLK / 16;
455 
456 		zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
457 
458 		zsc_args.hwflags = zs_console_flags(zsc->zsc_promunit,
459 						    zsc->zsc_node,
460 						    channel);
461 
462 		if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) {
463 			zsc_args.hwflags |= ZS_HWFLAG_USE_CONSDEV;
464 			zsc_args.consdev = &zs_consdev;
465 		}
466 
467 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_INPUT) != 0) {
468 			zs_conschan_get = zc;
469 		}
470 		if ((zsc_args.hwflags & ZS_HWFLAG_CONSOLE_OUTPUT) != 0) {
471 			zs_conschan_put = zc;
472 		}
473 		/* Childs need to set cn_dev, etc */
474 
475 		cs->cs_reg_csr  = &zc->zc_csr;
476 		cs->cs_reg_data = &zc->zc_data;
477 
478 		bcopy(zs_init_reg, cs->cs_creg, 16);
479 		bcopy(zs_init_reg, cs->cs_preg, 16);
480 
481 		/* XXX: Consult PROM properties for this?! */
482 		cs->cs_defspeed = zs_get_speed(cs);
483 		cs->cs_defcflag = zs_def_cflag;
484 
485 		/* Make these correspond to cs_defcflag (-crtscts) */
486 		cs->cs_rr0_dcd = ZSRR0_DCD;
487 		cs->cs_rr0_cts = 0;
488 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
489 		cs->cs_wr5_rts = 0;
490 
491 		/*
492 		 * Clear the master interrupt enable.
493 		 * The INTENA is common to both channels,
494 		 * so just do it on the A channel.
495 		 */
496 		if (channel == 0) {
497 			zs_write_reg(cs, 9, 0);
498 		}
499 
500 		/*
501 		 * Look for a child driver for this channel.
502 		 * The child attach will setup the hardware.
503 		 */
504 
505 		child = config_found(&zsc->zsc_dev, &zsc_args, zs_print);
506 		if (child == NULL) {
507 			/* No sub-driver.  Just reset it. */
508 			u_char reset = (channel == 0) ?
509 				ZSWR9_A_RESET : ZSWR9_B_RESET;
510 			s = splzs();
511 			zs_write_reg(cs,  9, reset);
512 			splx(s);
513 		}
514 #if (NKBD > 0) || (NMS > 0)
515 		/*
516 		 * If this was a zstty it has a keyboard
517 		 * property on it we need to attach the
518 		 * sunkbd and sunms line disciplines.
519 		 */
520 		if ((child != NULL)
521 		    && (strcmp(child->dv_cfdata->cf_name, "zstty") == 0)
522 		    && (prom_getproplen(zsc->zsc_node, "keyboard") == 0))
523 		{
524 			struct kbd_ms_tty_attach_args kma;
525 			struct tty *tp = zstty_get_tty_from_dev(child);
526 			kma.kmta_tp = tp;
527 			kma.kmta_dev = tp->t_dev;
528 			kma.kmta_consdev = zsc_args.consdev;
529 
530 			/* Attach 'em if we got 'em. */
531 #if (NKBD > 0)
532 			if (channel == 0) {
533 				kma.kmta_name = "keyboard";
534 				config_found(child, &kma, NULL);
535 			}
536 #endif
537 #if (NMS > 0)
538 			if (channel == 1) {
539 				kma.kmta_name = "mouse";
540 				config_found(child, &kma, NULL);
541 			}
542 #endif
543 		}
544 #endif
545 	}
546 
547 	/*
548 	 * Now safe to install interrupt handlers.  Note the arguments
549 	 * to the interrupt handlers aren't used.  Note, we only do this
550 	 * once since both SCCs interrupt at the same level and vector.
551 	 */
552 	if (!didintr) {
553 		didintr = 1;
554 		prevpri = pri;
555 		bus_intr_establish(zsc->zsc_bustag, pri, IPL_SERIAL,
556 				   zshard, NULL);
557 	} else if (pri != prevpri)
558 		panic("broken zs interrupt scheme");
559 
560 	evcnt_attach_dynamic(&zsc->zsc_intrcnt, EVCNT_TYPE_INTR, NULL,
561 	    zsc->zsc_dev.dv_xname, "intr");
562 
563 	/*
564 	 * Set the master interrupt enable and interrupt vector.
565 	 * (common to both channels, do it on A)
566 	 */
567 	cs = zsc->zsc_cs[0];
568 	s = splhigh();
569 	/* interrupt vector */
570 	zs_write_reg(cs, 2, zs_init_reg[2]);
571 	/* master interrupt control (enable) */
572 	zs_write_reg(cs, 9, zs_init_reg[9]);
573 	splx(s);
574 
575 #if 0
576 	/*
577 	 * XXX: L1A hack - We would like to be able to break into
578 	 * the debugger during the rest of autoconfiguration, so
579 	 * lower interrupts just enough to let zs interrupts in.
580 	 * This is done after both zs devices are attached.
581 	 */
582 	if (zsc->zsc_promunit == 1) {
583 		printf("zs1: enabling zs interrupts\n");
584 		(void)splfd(); /* XXX: splzs - 1 */
585 	}
586 #endif
587 
588 }
589 
590 static int
591 zs_print(aux, name)
592 	void *aux;
593 	const char *name;
594 {
595 	struct zsc_attach_args *args = aux;
596 
597 	if (name != NULL)
598 		aprint_normal("%s: ", name);
599 
600 	if (args->channel != -1)
601 		aprint_normal(" channel %d", args->channel);
602 
603 	return (UNCONF);
604 }
605 
606 static volatile int zssoftpending;
607 
608 /*
609  * Our ZS chips all share a common, autovectored interrupt,
610  * so we have to look at all of them on each interrupt.
611  */
612 static int
613 zshard(arg)
614 	void *arg;
615 {
616 	struct zsc_softc *zsc;
617 	int unit, rr3, rval, softreq;
618 
619 	rval = softreq = 0;
620 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
621 		struct zs_chanstate *cs;
622 
623 		zsc = zs_cd.cd_devs[unit];
624 		if (zsc == NULL)
625 			continue;
626 		rr3 = zsc_intr_hard(zsc);
627 		/* Count up the interrupts. */
628 		if (rr3) {
629 			rval |= rr3;
630 			zsc->zsc_intrcnt.ev_count++;
631 		}
632 		if ((cs = zsc->zsc_cs[0]) != NULL)
633 			softreq |= cs->cs_softreq;
634 		if ((cs = zsc->zsc_cs[1]) != NULL)
635 			softreq |= cs->cs_softreq;
636 	}
637 
638 	/* We are at splzs here, so no need to lock. */
639 	if (softreq && (zssoftpending == 0)) {
640 		zssoftpending = 1;
641 		softintr_schedule(zs_sicookie);
642 	}
643 	return (rval);
644 }
645 
646 /*
647  * Similar scheme as for zshard (look at all of them)
648  */
649 static void
650 zssoft(arg)
651 	void *arg;
652 {
653 	struct zsc_softc *zsc;
654 	int s, unit;
655 
656 	/* This is not the only ISR on this IPL. */
657 	if (zssoftpending == 0)
658 		return;
659 
660 	/*
661 	 * The soft intr. bit will be set by zshard only if
662 	 * the variable zssoftpending is zero.  The order of
663 	 * these next two statements prevents our clearing
664 	 * the soft intr bit just after zshard has set it.
665 	 */
666 	/* ienab_bic(IE_ZSSOFT); */
667 	zssoftpending = 0;
668 
669 	/* Make sure we call the tty layer at spltty. */
670 	s = spltty();
671 	for (unit = 0; unit < zs_cd.cd_ndevs; unit++) {
672 		zsc = zs_cd.cd_devs[unit];
673 		if (zsc == NULL)
674 			continue;
675 		(void)zsc_intr_soft(zsc);
676 	}
677 	splx(s);
678 }
679 
680 
681 /*
682  * Compute the current baud rate given a ZS channel.
683  */
684 static int
685 zs_get_speed(cs)
686 	struct zs_chanstate *cs;
687 {
688 	int tconst;
689 
690 	tconst = zs_read_reg(cs, 12);
691 	tconst |= zs_read_reg(cs, 13) << 8;
692 	return (TCONST_TO_BPS(cs->cs_brg_clk, tconst));
693 }
694 
695 /*
696  * MD functions for setting the baud rate and control modes.
697  */
698 int
699 zs_set_speed(cs, bps)
700 	struct zs_chanstate *cs;
701 	int bps;	/* bits per second */
702 {
703 	int tconst, real_bps;
704 
705 	if (bps == 0)
706 		return (0);
707 
708 #ifdef	DIAGNOSTIC
709 	if (cs->cs_brg_clk == 0)
710 		panic("zs_set_speed");
711 #endif
712 
713 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
714 	if (tconst < 0)
715 		return (EINVAL);
716 
717 	/* Convert back to make sure we can do it. */
718 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
719 
720 	/* XXX - Allow some tolerance here? */
721 	if (real_bps != bps)
722 		return (EINVAL);
723 
724 	cs->cs_preg[12] = tconst;
725 	cs->cs_preg[13] = tconst >> 8;
726 
727 	/* Caller will stuff the pending registers. */
728 	return (0);
729 }
730 
731 int
732 zs_set_modes(cs, cflag)
733 	struct zs_chanstate *cs;
734 	int cflag;	/* bits per second */
735 {
736 	int s;
737 
738 	/*
739 	 * Output hardware flow control on the chip is horrendous:
740 	 * if carrier detect drops, the receiver is disabled, and if
741 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
742 	 * Therefore, NEVER set the HFC bit, and instead use the
743 	 * status interrupt to detect CTS changes.
744 	 */
745 	s = splzs();
746 	cs->cs_rr0_pps = 0;
747 	if ((cflag & (CLOCAL | MDMBUF)) != 0) {
748 		cs->cs_rr0_dcd = 0;
749 		if ((cflag & MDMBUF) == 0)
750 			cs->cs_rr0_pps = ZSRR0_DCD;
751 	} else
752 		cs->cs_rr0_dcd = ZSRR0_DCD;
753 	if ((cflag & CRTSCTS) != 0) {
754 		cs->cs_wr5_dtr = ZSWR5_DTR;
755 		cs->cs_wr5_rts = ZSWR5_RTS;
756 		cs->cs_rr0_cts = ZSRR0_CTS;
757 	} else if ((cflag & CDTRCTS) != 0) {
758 		cs->cs_wr5_dtr = 0;
759 		cs->cs_wr5_rts = ZSWR5_DTR;
760 		cs->cs_rr0_cts = ZSRR0_CTS;
761 	} else if ((cflag & MDMBUF) != 0) {
762 		cs->cs_wr5_dtr = 0;
763 		cs->cs_wr5_rts = ZSWR5_DTR;
764 		cs->cs_rr0_cts = ZSRR0_DCD;
765 	} else {
766 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
767 		cs->cs_wr5_rts = 0;
768 		cs->cs_rr0_cts = 0;
769 	}
770 	splx(s);
771 
772 	/* Caller will stuff the pending registers. */
773 	return (0);
774 }
775 
776 
777 /*
778  * Read or write the chip with suitable delays.
779  */
780 
781 u_char
782 zs_read_reg(cs, reg)
783 	struct zs_chanstate *cs;
784 	u_char reg;
785 {
786 	u_char val;
787 
788 	*cs->cs_reg_csr = reg;
789 	ZS_DELAY();
790 	val = *cs->cs_reg_csr;
791 	ZS_DELAY();
792 	return (val);
793 }
794 
795 void
796 zs_write_reg(cs, reg, val)
797 	struct zs_chanstate *cs;
798 	u_char reg, val;
799 {
800 	*cs->cs_reg_csr = reg;
801 	ZS_DELAY();
802 	*cs->cs_reg_csr = val;
803 	ZS_DELAY();
804 }
805 
806 u_char
807 zs_read_csr(cs)
808 	struct zs_chanstate *cs;
809 {
810 	u_char val;
811 
812 	val = *cs->cs_reg_csr;
813 	ZS_DELAY();
814 	return (val);
815 }
816 
817 void
818 zs_write_csr(cs, val)
819 	struct zs_chanstate *cs;
820 	u_char val;
821 {
822 	*cs->cs_reg_csr = val;
823 	ZS_DELAY();
824 }
825 
826 u_char
827 zs_read_data(cs)
828 	struct zs_chanstate *cs;
829 {
830 	u_char val;
831 
832 	val = *cs->cs_reg_data;
833 	ZS_DELAY();
834 	return (val);
835 }
836 
837 void  zs_write_data(cs, val)
838 	struct zs_chanstate *cs;
839 	u_char val;
840 {
841 	*cs->cs_reg_data = val;
842 	ZS_DELAY();
843 }
844 
845 /****************************************************************
846  * Console support functions (Sun specific!)
847  * Note: this code is allowed to know about the layout of
848  * the chip registers, and uses that to keep things simple.
849  * XXX - I think I like the mvme167 code better. -gwr
850  ****************************************************************/
851 
852 /*
853  * Handle user request to enter kernel debugger.
854  */
855 void
856 zs_abort(cs)
857 	struct zs_chanstate *cs;
858 {
859 	struct zschan *zc = zs_conschan_get;
860 	int rr0;
861 
862 	/* Wait for end of break to avoid PROM abort. */
863 	/* XXX - Limit the wait? */
864 	do {
865 		rr0 = zc->zc_csr;
866 		ZS_DELAY();
867 	} while (rr0 & ZSRR0_BREAK);
868 
869 #if defined(KGDB)
870 	zskgdb(cs);
871 #elif defined(DDB)
872 	Debugger();
873 #else
874 	printf("stopping on keyboard abort\n");
875 	callrom();
876 #endif
877 }
878 
879 int  zs_getc __P((void *arg));
880 void zs_putc __P((void *arg, int c));
881 
882 /*
883  * Polled input char.
884  */
885 int
886 zs_getc(arg)
887 	void *arg;
888 {
889 	struct zschan *zc = arg;
890 	int s, c, rr0;
891 	u_int omid;
892 
893 	/* Temporarily direct interrupts at ourselves */
894 	s = splhigh();
895 	omid = setitr(cpuinfo.mid);
896 
897 	/* Wait for a character to arrive. */
898 	do {
899 		rr0 = zc->zc_csr;
900 		ZS_DELAY();
901 	} while ((rr0 & ZSRR0_RX_READY) == 0);
902 
903 	c = zc->zc_data;
904 	ZS_DELAY();
905 	setitr(omid);
906 	splx(s);
907 
908 	/*
909 	 * This is used by the kd driver to read scan codes,
910 	 * so don't translate '\r' ==> '\n' here...
911 	 */
912 	return (c);
913 }
914 
915 /*
916  * Polled output char.
917  */
918 void
919 zs_putc(arg, c)
920 	void *arg;
921 	int c;
922 {
923 	struct zschan *zc = arg;
924 	int s, rr0;
925 	u_int omid;
926 
927 	/* Temporarily direct interrupts at ourselves */
928 	s = splhigh();
929 	omid = setitr(cpuinfo.mid);
930 
931 	/* Wait for transmitter to become ready. */
932 	do {
933 		rr0 = zc->zc_csr;
934 		ZS_DELAY();
935 	} while ((rr0 & ZSRR0_TX_READY) == 0);
936 
937 	/*
938 	 * Send the next character.
939 	 * Now you'd think that this could be followed by a ZS_DELAY()
940 	 * just like all the other chip accesses, but it turns out that
941 	 * the `transmit-ready' interrupt isn't de-asserted until
942 	 * some period of time after the register write completes
943 	 * (more than a couple instructions).  So to avoid stray
944 	 * interrupts we put in the 2us delay regardless of CPU model.
945 	 */
946 	zc->zc_data = c;
947 	delay(2);
948 
949 	setitr(omid);
950 	splx(s);
951 }
952 
953 /*****************************************************************/
954 /*
955  * Polled console input putchar.
956  */
957 int
958 zscngetc(dev)
959 	dev_t dev;
960 {
961 	return (zs_getc(zs_conschan_get));
962 }
963 
964 /*
965  * Polled console output putchar.
966  */
967 void
968 zscnputc(dev, c)
969 	dev_t dev;
970 	int c;
971 {
972 	zs_putc(zs_conschan_put, c);
973 }
974 
975 void
976 zscnpollc(dev, on)
977 	dev_t dev;
978 	int on;
979 {
980 	/* No action needed */
981 }
982 
983 int
984 zs_console_flags(promunit, node, channel)
985 	int promunit;
986 	int node;
987 	int channel;
988 {
989 	int cookie, flags = 0;
990 
991 	switch (prom_version()) {
992 	case PROM_OLDMON:
993 	case PROM_OBP_V0:
994 		/*
995 		 * Use `promunit' and `channel' to derive the PROM
996 		 * stdio handles that correspond to this device.
997 		 */
998 		if (promunit == 0)
999 			cookie = PROMDEV_TTYA + channel;
1000 		else if (promunit == 1 && channel == 0)
1001 			cookie = PROMDEV_KBD;
1002 		else
1003 			cookie = -1;
1004 
1005 		if (cookie == prom_stdin())
1006 			flags |= ZS_HWFLAG_CONSOLE_INPUT;
1007 
1008 		/*
1009 		 * Prevent the keyboard from matching the output device
1010 		 * (note that PROMDEV_KBD == PROMDEV_SCREEN == 0!).
1011 		 */
1012 		if (cookie != PROMDEV_KBD && cookie == prom_stdout())
1013 			flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
1014 
1015 		break;
1016 
1017 	case PROM_OBP_V2:
1018 	case PROM_OBP_V3:
1019 	case PROM_OPENFIRM:
1020 
1021 		/*
1022 		 * Match the nodes and device arguments prepared by
1023 		 * consinit() against our device node and channel.
1024 		 * (The device argument is the part of the OBP path
1025 		 * following the colon, as in `/obio/zs@0,100000:a')
1026 		 */
1027 
1028 		/* Default to channel 0 if there are no explicit prom args */
1029 		cookie = 0;
1030 
1031 		if (node == prom_stdin_node) {
1032 			if (prom_stdin_args[0] != '\0')
1033 				/* Translate (a,b) -> (0,1) */
1034 				cookie = prom_stdin_args[0] - 'a';
1035 
1036 			if (channel == cookie)
1037 				flags |= ZS_HWFLAG_CONSOLE_INPUT;
1038 		}
1039 
1040 		if (node == prom_stdout_node) {
1041 			if (prom_stdout_args[0] != '\0')
1042 				/* Translate (a,b) -> (0,1) */
1043 				cookie = prom_stdout_args[0] - 'a';
1044 
1045 			if (channel == cookie)
1046 				flags |= ZS_HWFLAG_CONSOLE_OUTPUT;
1047 		}
1048 
1049 		break;
1050 
1051 	default:
1052 		break;
1053 	}
1054 
1055 	return (flags);
1056 }
1057 
1058 /*
1059  * Power management hooks for zsopen() and zsclose().
1060  * We use them to power on/off the ports, if necessary.
1061  */
1062 int
1063 zs_enable(cs)
1064 	struct zs_chanstate *cs;
1065 {
1066 	auxiotwoserialendis (ZS_ENABLE);
1067 	cs->enabled = 1;
1068 	return(0);
1069 }
1070 
1071 void
1072 zs_disable(cs)
1073 	struct zs_chanstate *cs;
1074 {
1075 	auxiotwoserialendis (ZS_DISABLE);
1076 	cs->enabled = 0;
1077 }
1078 
1079 
1080 
1081 
1082 
1083 
1084