xref: /netbsd-src/sys/dev/tc/zs_ioasic.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: zs_ioasic.c,v 1.35 2007/11/09 00:05:38 ad Exp $ */
2 
3 /*-
4  * Copyright (c) 1996, 1998 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, Ken Hornstein, and by Jason R. Thorpe of the
9  * Numerical Aerospace Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * Zilog Z8530 Dual UART driver (machine-dependent part).  This driver
42  * handles Z8530 chips attached to the DECstation/Alpha IOASIC.  Modified
43  * for NetBSD/alpha by Ken Hornstein and Jason R. Thorpe.  NetBSD/pmax
44  * adaption by Mattias Drochner.  Merge work by Tohru Nishimura.
45  *
46  * Runs two serial lines per chip using slave drivers.
47  * Plain tty/async lines use the zstty slave.
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: zs_ioasic.c,v 1.35 2007/11/09 00:05:38 ad Exp $");
52 
53 #include "opt_ddb.h"
54 #include "opt_kgdb.h"
55 #include "zskbd.h"
56 
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/conf.h>
60 #include <sys/device.h>
61 #include <sys/malloc.h>
62 #include <sys/file.h>
63 #include <sys/ioctl.h>
64 #include <sys/kernel.h>
65 #include <sys/proc.h>
66 #include <sys/tty.h>
67 #include <sys/time.h>
68 #include <sys/syslog.h>
69 #include <sys/intr.h>
70 
71 #include <machine/autoconf.h>
72 #include <machine/z8530var.h>
73 
74 #include <dev/cons.h>
75 #include <dev/ic/z8530reg.h>
76 
77 #include <dev/tc/tcvar.h>
78 #include <dev/tc/ioasicreg.h>
79 #include <dev/tc/ioasicvar.h>
80 
81 #include <dev/tc/zs_ioasicvar.h>
82 
83 #if defined(__alpha__) || defined(alpha)
84 #include <machine/rpb.h>
85 #endif
86 #if defined(pmax)
87 #include <pmax/pmax/pmaxtype.h>
88 #endif
89 
90 /*
91  * Helpers for console support.
92  */
93 static void	zs_ioasic_cninit(tc_addr_t, tc_offset_t, int);
94 static int	zs_ioasic_cngetc(dev_t);
95 static void	zs_ioasic_cnputc(dev_t, int);
96 static void	zs_ioasic_cnpollc(dev_t, int);
97 
98 struct consdev zs_ioasic_cons = {
99 	NULL, NULL, zs_ioasic_cngetc, zs_ioasic_cnputc,
100 	zs_ioasic_cnpollc, NULL, NULL, NULL, NODEV, CN_NORMAL,
101 };
102 
103 static tc_offset_t zs_ioasic_console_offset;
104 static int zs_ioasic_console_channel;
105 static int zs_ioasic_console;
106 static struct zs_chanstate zs_ioasic_conschanstate_store;
107 
108 static int	zs_ioasic_isconsole(tc_offset_t, int);
109 static void	zs_putc(struct zs_chanstate *, int);
110 
111 /*
112  * Some warts needed by z8530tty.c
113  */
114 int zs_def_cflag = (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8;
115 
116 /*
117  * ZS chips are feeded a 7.372 MHz clock.
118  */
119 #define	PCLK	(9600 * 768)	/* PCLK pin input clock rate */
120 
121 /* The layout of this is hardware-dependent (padding, order). */
122 struct zshan {
123 #if defined(__alpha__) || defined(alpha)
124 	volatile u_int	zc_csr;		/* ctrl,status, and indirect access */
125 	u_int		zc_pad0;
126 	volatile u_int	zc_data;	/* data */
127 	u_int		sc_pad1;
128 #endif
129 #if defined(pmax)
130 	volatile u_int16_t zc_csr;	/* ctrl,status, and indirect access */
131 	unsigned : 16;
132 	volatile u_int16_t zc_data;	/* data */
133 	unsigned : 16;
134 #endif
135 };
136 
137 struct zsdevice {
138 	/* Yes, they are backwards. */
139 	struct	zshan zs_chan_b;
140 	struct	zshan zs_chan_a;
141 };
142 
143 static const u_char zs_ioasic_init_reg[16] = {
144 	0,	/* 0: CMD (reset, etc.) */
145 	0,	/* 1: No interrupts yet. */
146 	0xf0,	/* 2: IVECT */
147 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
148 	ZSWR4_CLK_X16 | ZSWR4_ONESB,
149 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
150 	0,	/* 6: TXSYNC/SYNCLO */
151 	0,	/* 7: RXSYNC/SYNCHI */
152 	0,	/* 8: alias for data port */
153 	ZSWR9_MASTER_IE | ZSWR9_VECTOR_INCL_STAT,
154 	0,	/*10: Misc. TX/RX control bits */
155 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
156 	22,	/*12: BAUDLO (default=9600) */
157 	0,	/*13: BAUDHI (default=9600) */
158 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
159 	ZSWR15_BREAK_IE,
160 };
161 
162 static struct zshan *
163 zs_ioasic_get_chan_addr(tc_addr_t zsaddr, int channel)
164 {
165 	struct zsdevice *addr;
166 	struct zshan *zc;
167 
168 #if defined(__alpha__) || defined(alpha)
169 	addr = (struct zsdevice *)TC_DENSE_TO_SPARSE(zsaddr);
170 #endif
171 #if defined(pmax)
172 	addr = (struct zsdevice *)MIPS_PHYS_TO_KSEG1(zsaddr);
173 #endif
174 
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_ioasic_match(struct device *, struct cfdata *, void *);
190 static void	zs_ioasic_attach(struct device *, struct device *, void *);
191 static int	zs_ioasic_print(void *, const char *name);
192 static int	zs_ioasic_submatch(struct device *, struct cfdata *,
193 				   const int *, void *);
194 
195 CFATTACH_DECL(zsc_ioasic, sizeof(struct zsc_softc),
196     zs_ioasic_match, zs_ioasic_attach, NULL, NULL);
197 
198 /* Interrupt handlers. */
199 static int	zs_ioasic_hardintr(void *);
200 static void	zs_ioasic_softintr(void *);
201 
202 /*
203  * Is the zs chip present?
204  */
205 static int
206 zs_ioasic_match(struct device *parent, struct cfdata *cf, void *aux)
207 {
208 	struct ioasicdev_attach_args *d = aux;
209 	tc_addr_t zs_addr;
210 
211 	/*
212 	 * Make sure that we're looking for the right kind of device.
213 	 */
214 	if (strncmp(d->iada_modname, "z8530   ", TC_ROM_LLEN) != 0 &&
215 	    strncmp(d->iada_modname, "scc", TC_ROM_LLEN) != 0)
216 		return (0);
217 
218 	/*
219 	 * Find out the device address, and check it for validity.
220 	 */
221 	zs_addr = TC_DENSE_TO_SPARSE((tc_addr_t)d->iada_addr);
222 	if (tc_badaddr(zs_addr))
223 		return (0);
224 
225 	return (1);
226 }
227 
228 /*
229  * Attach a found zs.
230  */
231 static void
232 zs_ioasic_attach(struct device *parent, struct device *self, void *aux)
233 {
234 	struct zsc_softc *zs = device_private(self);
235 	struct zsc_attach_args zs_args;
236 	struct zs_chanstate *cs;
237 	struct ioasicdev_attach_args *d = aux;
238 	struct zshan *zc;
239 	int s, channel;
240 	u_long zflg;
241 	int locs[ZSCCF_NLOCS];
242 
243 	printf("\n");
244 
245 	/*
246 	 * Initialize software state for each channel.
247 	 */
248 	for (channel = 0; channel < 2; channel++) {
249 		zs_args.channel = channel;
250 		zs_args.hwflags = 0;
251 
252 		if (zs_ioasic_isconsole(d->iada_offset, channel)) {
253 			cs = &zs_ioasic_conschanstate_store;
254 			zs_args.hwflags |= ZS_HWFLAG_CONSOLE;
255 		} else {
256 			cs = malloc(sizeof(struct zs_chanstate),
257 					M_DEVBUF, M_NOWAIT|M_ZERO);
258 			zs_lock_init(cs);
259 			zc = zs_ioasic_get_chan_addr(d->iada_addr, channel);
260 			cs->cs_reg_csr = (volatile void *)&zc->zc_csr;
261 
262 			bcopy(zs_ioasic_init_reg, cs->cs_creg, 16);
263 			bcopy(zs_ioasic_init_reg, cs->cs_preg, 16);
264 
265 			cs->cs_defcflag = zs_def_cflag;
266 			cs->cs_defspeed = 9600;		/* XXX */
267 			(void) zs_set_modes(cs, cs->cs_defcflag);
268 		}
269 
270 		zs->zsc_cs[channel] = cs;
271 		zs->zsc_addroffset = d->iada_offset; /* cookie only */
272 		cs->cs_channel = channel;
273 		cs->cs_ops = &zsops_null;
274 		cs->cs_brg_clk = PCLK / 16;
275 
276 		/*
277 		 * DCD and CTS interrupts are only meaningful on
278 		 * SCC 0/B, and RTS and DTR only on B of SCC 0 & 1.
279 		 *
280 		 * XXX This is sorta gross.
281 		 */
282 		if (d->iada_offset == 0x00100000 && channel == 1) {
283 			cs->cs_creg[15] |= ZSWR15_DCD_IE;
284 			cs->cs_preg[15] |= ZSWR15_DCD_IE;
285 			zflg = ZIP_FLAGS_DCDCTS;
286 		} else
287 			zflg = 0;
288 		if (channel == 1)
289 			zflg |= ZIP_FLAGS_DTRRTS;
290 		cs->cs_private = (void *)zflg;
291 
292 		/*
293 		 * Clear the master interrupt enable.
294 		 * The INTENA is common to both channels,
295 		 * so just do it on the A channel.
296 		 */
297 		if (channel == 0) {
298 			zs_write_reg(cs, 9, 0);
299 		}
300 
301 		/*
302 		 * Set up the flow/modem control channel pointer to
303 		 * deal with the weird wiring on the TC Alpha and
304 		 * DECstation.
305 		 */
306 		if (channel == 1)
307 			cs->cs_ctl_chan = zs->zsc_cs[0];
308 		else
309 			cs->cs_ctl_chan = NULL;
310 
311 		locs[ZSCCF_CHANNEL] = channel;
312 
313 		/*
314 		 * Look for a child driver for this channel.
315 		 * The child attach will setup the hardware.
316 		 */
317 		if (config_found_sm_loc(self, "zsc", locs, (void *)&zs_args,
318 				zs_ioasic_print, zs_ioasic_submatch) == NULL) {
319 			/* No sub-driver.  Just reset it. */
320 			u_char reset = (channel == 0) ?
321 				ZSWR9_A_RESET : ZSWR9_B_RESET;
322 			s = splhigh();
323 			zs_write_reg(cs, 9, reset);
324 			splx(s);
325 		}
326 	}
327 
328 	/*
329 	 * Set up the ioasic interrupt handler.
330 	 */
331 	ioasic_intr_establish(parent, d->iada_cookie, TC_IPL_TTY,
332 	    zs_ioasic_hardintr, zs);
333 	zs->zsc_sih = softint_establish(SOFTINT_SERIAL,
334 	    zs_ioasic_softintr, zs);
335 	if (zs->zsc_sih == NULL)
336 		panic("zs_ioasic_attach: unable to register softintr");
337 
338 	/*
339 	 * Set the master interrupt enable and interrupt vector.  The
340 	 * Sun does this only on one channel.  The old Alpha SCC driver
341 	 * did it on both.  We'll do it on both.
342 	 */
343 	s = splhigh();
344 	/* interrupt vector */
345 	zs_write_reg(zs->zsc_cs[0], 2, zs_ioasic_init_reg[2]);
346 	zs_write_reg(zs->zsc_cs[1], 2, zs_ioasic_init_reg[2]);
347 
348 	/* master interrupt control (enable) */
349 	zs_write_reg(zs->zsc_cs[0], 9, zs_ioasic_init_reg[9]);
350 	zs_write_reg(zs->zsc_cs[1], 9, zs_ioasic_init_reg[9]);
351 #if defined(__alpha__) || defined(alpha)
352 	/* ioasic interrupt enable */
353 	*(volatile u_int *)(ioasic_base + IOASIC_IMSK) |=
354 		    IOASIC_INTR_SCC_1 | IOASIC_INTR_SCC_0;
355 	tc_mb();
356 #endif
357 	splx(s);
358 }
359 
360 static int
361 zs_ioasic_print(void *aux, const char *name)
362 {
363 	struct zsc_attach_args *args = aux;
364 
365 	if (name != NULL)
366 		aprint_normal("%s:", name);
367 
368 	if (args->channel != -1)
369 		aprint_normal(" channel %d", args->channel);
370 
371 	return (UNCONF);
372 }
373 
374 static int
375 zs_ioasic_submatch(struct device *parent, struct cfdata *cf, const int *locs,
376     void *aux)
377 {
378 	struct zsc_softc *zs = (void *)parent;
379 	struct zsc_attach_args *pa = aux;
380 	const char *defname = "";
381 
382 	if (cf->cf_loc[ZSCCF_CHANNEL] != ZSCCF_CHANNEL_DEFAULT &&
383 	    cf->cf_loc[ZSCCF_CHANNEL] != locs[ZSCCF_CHANNEL])
384 		return (0);
385 
386 	if (cf->cf_loc[ZSCCF_CHANNEL] == ZSCCF_CHANNEL_DEFAULT) {
387 		if (pa->channel == 0) {
388 #if defined(pmax)
389 			if (systype == DS_MAXINE)
390 				return (0);
391 #endif
392 			if (zs->zsc_addroffset == 0x100000)
393 				defname = "vsms";
394 			else
395 				defname = "lkkbd";
396 		}
397 		else if (zs->zsc_addroffset == 0x100000)
398 			defname = "zstty";
399 #if defined(pmax)
400 		else if (systype == DS_MAXINE)
401 			return (0);
402 #endif
403 #if defined(__alpha__) || defined(alpha)
404 		else if (cputype == ST_DEC_3000_300)
405 			return (0);
406 #endif
407 		else
408 			defname = "zstty"; /* 3min/3max+, DEC3000/500 */
409 
410 		if (strcmp(cf->cf_name, defname))
411 			return (0);
412 	}
413 	return (config_match(parent, cf, aux));
414 }
415 
416 /*
417  * Hardware interrupt handler.
418  */
419 static int
420 zs_ioasic_hardintr(void *arg)
421 {
422 	struct zsc_softc *zsc = arg;
423 
424 	/*
425 	 * Call the upper-level MI hardware interrupt handler.
426 	 */
427 	zsc_intr_hard(zsc);
428 
429 	/*
430 	 * Check to see if we need to schedule any software-level
431 	 * processing interrupts.
432 	 */
433 	if (zsc->zsc_cs[0]->cs_softreq | zsc->zsc_cs[1]->cs_softreq)
434 		softint_schedule(zsc->zsc_sih);
435 
436 	return (1);
437 }
438 
439 /*
440  * Software-level interrupt (character processing, lower priority).
441  */
442 static void
443 zs_ioasic_softintr(void *arg)
444 {
445 	struct zsc_softc *zsc = arg;
446 	int s;
447 
448 	s = spltty();
449 	(void) zsc_intr_soft(zsc);
450 	splx(s);
451 }
452 
453 /*
454  * MD functions for setting the baud rate and control modes.
455  */
456 int
457 zs_set_speed(struct zs_chanstate *cs, int bps /*bits per second*/)
458 {
459 	int tconst, real_bps;
460 
461 	if (bps == 0)
462 		return (0);
463 
464 #ifdef DIAGNOSTIC
465 	if (cs->cs_brg_clk == 0)
466 		panic("zs_set_speed");
467 #endif
468 
469 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps);
470 	if (tconst < 0)
471 		return (EINVAL);
472 
473 	/* Convert back to make sure we can do it. */
474 	real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst);
475 
476 	/* XXX - Allow some tolerance here? */
477 	if (real_bps != bps)
478 		return (EINVAL);
479 
480 	cs->cs_preg[12] = tconst;
481 	cs->cs_preg[13] = tconst >> 8;
482 
483 	/* Caller will stuff the pending registers. */
484 	return (0);
485 }
486 
487 int
488 zs_set_modes(struct zs_chanstate *cs, int cflag)
489 {
490 	u_long privflags = (u_long)cs->cs_private;
491 	int s;
492 
493 	/*
494 	 * Output hardware flow control on the chip is horrendous:
495 	 * if carrier detect drops, the receiver is disabled, and if
496 	 * CTS drops, the transmitter is stoped IN MID CHARACTER!
497 	 * Therefore, NEVER set the HFC bit, and instead use the
498 	 * status interrupt to detect CTS changes.
499 	 */
500 	s = splzs();
501 	if ((cflag & (CLOCAL | MDMBUF)) != 0)
502 		cs->cs_rr0_dcd = 0;
503 	else
504 		cs->cs_rr0_dcd = ZSRR0_DCD;
505 	if ((cflag & CRTSCTS) != 0) {
506 		cs->cs_wr5_dtr = ZSWR5_DTR;
507 		cs->cs_wr5_rts = ZSWR5_RTS;
508 		cs->cs_rr0_cts = ZSRR0_CTS;
509 	} else if ((cflag & CDTRCTS) != 0) {
510 		cs->cs_wr5_dtr = 0;
511 		cs->cs_wr5_rts = ZSWR5_DTR;
512 		cs->cs_rr0_cts = ZSRR0_CTS;
513 	} else if ((cflag & MDMBUF) != 0) {
514 		cs->cs_wr5_dtr = 0;
515 		cs->cs_wr5_rts = ZSWR5_DTR;
516 		cs->cs_rr0_cts = ZSRR0_DCD;
517 	} else {
518 		cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS;
519 		cs->cs_wr5_rts = 0;
520 		cs->cs_rr0_cts = 0;
521 	}
522 
523 	if ((privflags & ZIP_FLAGS_DCDCTS) == 0) {
524 		cs->cs_rr0_dcd &= ~(ZSRR0_CTS|ZSRR0_DCD);
525 		cs->cs_rr0_cts &= ~(ZSRR0_CTS|ZSRR0_DCD);
526 	}
527 	if ((privflags & ZIP_FLAGS_DTRRTS) == 0) {
528 		cs->cs_wr5_dtr &= ~(ZSWR5_RTS|ZSWR5_DTR);
529 		cs->cs_wr5_rts &= ~(ZSWR5_RTS|ZSWR5_DTR);
530 	}
531 	splx(s);
532 
533 	/* Caller will stuff the pending registers. */
534 	return (0);
535 }
536 
537 /*
538  * Functions to read and write individual registers in a channel.
539  * The ZS chip requires a 1.6 uSec. recovery time between accesses,
540  * and the Alpha TC hardware does NOT take care of this for you.
541  * The delay is now handled inside the chip access functions.
542  * These could be inlines, but with the delay, speed is moot.
543  */
544 #if defined(pmax)
545 #undef	DELAY
546 #define	DELAY(x)
547 #endif
548 
549 u_int
550 zs_read_reg(struct zs_chanstate *cs, u_int reg)
551 {
552 	volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
553 	unsigned val;
554 
555 	zc->zc_csr = reg << 8;
556 	tc_wmb();
557 	DELAY(5);
558 	val = (zc->zc_csr >> 8) & 0xff;
559 	/* tc_mb(); */
560 	DELAY(5);
561 	return (val);
562 }
563 
564 void
565 zs_write_reg(struct zs_chanstate *cs, u_int reg, u_int val)
566 {
567 	volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
568 
569 	zc->zc_csr = reg << 8;
570 	tc_wmb();
571 	DELAY(5);
572 	zc->zc_csr = val << 8;
573 	tc_wmb();
574 	DELAY(5);
575 }
576 
577 u_int
578 zs_read_csr(struct zs_chanstate *cs)
579 {
580 	volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
581 	unsigned val;
582 
583 	val = (zc->zc_csr >> 8) & 0xff;
584 	/* tc_mb(); */
585 	DELAY(5);
586 	return (val);
587 }
588 
589 void
590 zs_write_csr(struct zs_chanstate *cs, u_int val)
591 {
592 	volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
593 
594 	zc->zc_csr = val << 8;
595 	tc_wmb();
596 	DELAY(5);
597 }
598 
599 u_int
600 zs_read_data(struct zs_chanstate *cs)
601 {
602 	volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
603 	unsigned val;
604 
605 	val = (zc->zc_data) >> 8 & 0xff;
606 	/* tc_mb(); */
607 	DELAY(5);
608 	return (val);
609 }
610 
611 void
612 zs_write_data(struct zs_chanstate *cs, u_int val)
613 {
614 	volatile struct zshan *zc = (volatile void *)cs->cs_reg_csr;
615 
616 	zc->zc_data = val << 8;
617 	tc_wmb();
618 	DELAY(5);
619 }
620 
621 /****************************************************************
622  * Console support functions
623  ****************************************************************/
624 
625 /*
626  * Handle user request to enter kernel debugger.
627  */
628 void
629 zs_abort(struct zs_chanstate *cs)
630 {
631 	int rr0;
632 
633 	/* Wait for end of break. */
634 	/* XXX - Limit the wait? */
635 	do {
636 		rr0 = zs_read_csr(cs);
637 	} while (rr0 & ZSRR0_BREAK);
638 
639 #if defined(KGDB)
640 	zskgdb(cs);
641 #elif defined(DDB)
642 	Debugger();
643 #else
644 	printf("zs_abort: ignoring break on console\n");
645 #endif
646 }
647 
648 /*
649  * Polled input char.
650  */
651 int
652 zs_getc(struct zs_chanstate *cs)
653 {
654 	int s, c, rr0;
655 
656 	s = splhigh();
657 	/* Wait for a character to arrive. */
658 	do {
659 		rr0 = zs_read_csr(cs);
660 	} while ((rr0 & ZSRR0_RX_READY) == 0);
661 
662 	c = zs_read_data(cs);
663 	splx(s);
664 
665 	/*
666 	 * This is used by the kd driver to read scan codes,
667 	 * so don't translate '\r' ==> '\n' here...
668 	 */
669 	return (c);
670 }
671 
672 /*
673  * Polled output char.
674  */
675 static void
676 zs_putc(struct zs_chanstate *cs, int c)
677 {
678 	register int s, rr0;
679 
680 	s = splhigh();
681 	/* Wait for transmitter to become ready. */
682 	do {
683 		rr0 = zs_read_csr(cs);
684 	} while ((rr0 & ZSRR0_TX_READY) == 0);
685 
686 	zs_write_data(cs, c);
687 
688 	/* Wait for the character to be transmitted. */
689 	do {
690 		rr0 = zs_read_csr(cs);
691 	} while ((rr0 & ZSRR0_TX_READY) == 0);
692 	splx(s);
693 }
694 
695 /*****************************************************************/
696 
697 /*
698  * zs_ioasic_cninit --
699  *	Initialize the serial channel for either a keyboard or
700  *	a serial console.
701  */
702 static void
703 zs_ioasic_cninit(tc_addr_t ioasic_addr, tc_offset_t zs_offset, int channel)
704 {
705 	struct zs_chanstate *cs;
706 	tc_addr_t zs_addr;
707 	struct zshan *zc;
708 	u_long zflg;
709 
710 	/*
711 	 * Initialize the console finder helpers.
712 	 */
713 	zs_ioasic_console_offset = zs_offset;
714 	zs_ioasic_console_channel = channel;
715 	zs_ioasic_console = 1;
716 
717 	/*
718 	 * Pointer to channel state.
719 	 */
720 	cs = &zs_ioasic_conschanstate_store;
721 
722 	/*
723 	 * Compute the physical address of the chip, "map" it via
724 	 * K0SEG, and then get the address of the actual channel.
725 	 */
726 #if defined(__alpha__) || defined(alpha)
727 	zs_addr = ALPHA_PHYS_TO_K0SEG(ioasic_addr + zs_offset);
728 #endif
729 #if defined(pmax)
730 	zs_addr = MIPS_PHYS_TO_KSEG1(ioasic_addr + zs_offset);
731 #endif
732 	zc = zs_ioasic_get_chan_addr(zs_addr, channel);
733 
734 	/* Setup temporary chanstate. */
735 	cs->cs_reg_csr = (volatile void *)&zc->zc_csr;
736 
737 	cs->cs_channel = channel;
738 	cs->cs_ops = &zsops_null;
739 	cs->cs_brg_clk = PCLK / 16;
740 
741 	/* Initialize the pending registers. */
742 	bcopy(zs_ioasic_init_reg, cs->cs_preg, 16);
743 	/* cs->cs_preg[5] |= (ZSWR5_DTR | ZSWR5_RTS); */
744 
745 	/*
746 	 * DCD and CTS interrupts are only meaningful on
747 	 * SCC 0/B, and RTS and DTR only on B of SCC 0 & 1.
748 	 *
749 	 * XXX This is sorta gross.
750 	 */
751 	if (zs_offset == 0x00100000 && channel == 1)
752 		zflg = ZIP_FLAGS_DCDCTS;
753 	else
754 		zflg = 0;
755 	if (channel == 1)
756 		zflg |= ZIP_FLAGS_DTRRTS;
757 	cs->cs_private = (void *)zflg;
758 
759 	/* Clear the master interrupt enable. */
760 	zs_write_reg(cs, 9, 0);
761 
762 	/* Reset the whole SCC chip. */
763 	zs_write_reg(cs, 9, ZSWR9_HARD_RESET);
764 
765 	/* Copy "pending" to "current" and H/W. */
766 	zs_loadchannelregs(cs);
767 }
768 
769 /*
770  * zs_ioasic_cnattach --
771  *	Initialize and attach a serial console.
772  */
773 void
774 zs_ioasic_cnattach(tc_addr_t ioasic_addr, tc_offset_t zs_offset, int channel)
775 {
776 	struct zs_chanstate *cs = &zs_ioasic_conschanstate_store;
777 	extern const struct cdevsw zstty_cdevsw;
778 
779 	zs_ioasic_cninit(ioasic_addr, zs_offset, channel);
780 	zs_lock_init(cs);
781 	cs->cs_defspeed = 9600;
782 	cs->cs_defcflag = (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8;
783 
784 	/* Point the console at the SCC. */
785 	cn_tab = &zs_ioasic_cons;
786 	cn_tab->cn_pri = CN_REMOTE;
787 	cn_tab->cn_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw),
788 				 (zs_offset == 0x100000) ? 0 : 1);
789 }
790 
791 /*
792  * zs_ioasic_lk201_cnattach --
793  *	Initialize and attach a keyboard.
794  */
795 int
796 zs_ioasic_lk201_cnattach(tc_addr_t ioasic_addr, tc_offset_t zs_offset,
797     int channel)
798 {
799 #if (NZSKBD > 0)
800 	struct zs_chanstate *cs = &zs_ioasic_conschanstate_store;
801 
802 	zs_ioasic_cninit(ioasic_addr, zs_offset, channel);
803 	zs_lock_init(cs);
804 	cs->cs_defspeed = 4800;
805 	cs->cs_defcflag = (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8;
806 	return (zskbd_cnattach(cs));
807 #else
808 	return (ENXIO);
809 #endif
810 }
811 
812 static int
813 zs_ioasic_isconsole(tc_offset_t offset, int channel)
814 {
815 
816 	if (zs_ioasic_console &&
817 	    offset == zs_ioasic_console_offset &&
818 	    channel == zs_ioasic_console_channel)
819 		return (1);
820 
821 	return (0);
822 }
823 
824 /*
825  * Polled console input putchar.
826  */
827 static int
828 zs_ioasic_cngetc(dev_t dev)
829 {
830 
831 	return (zs_getc(&zs_ioasic_conschanstate_store));
832 }
833 
834 /*
835  * Polled console output putchar.
836  */
837 static void
838 zs_ioasic_cnputc(dev_t dev, int c)
839 {
840 
841 	zs_putc(&zs_ioasic_conschanstate_store, c);
842 }
843 
844 /*
845  * Set polling/no polling on console.
846  */
847 static void
848 zs_ioasic_cnpollc(dev_t dev, int onoff)
849 {
850 
851 	/* XXX ??? */
852 }
853